blob: 5c4faddedb06266250c1b66f58bd4c7f78e7c7d6 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * screen.c: code for displaying on the screen
12 *
13 * Output to the screen (console, terminal emulator or GUI window) is minimized
14 * by remembering what is already on the screen, and only updating the parts
15 * that changed.
16 *
17 * ScreenLines[off] Contains a copy of the whole screen, as it is currently
18 * displayed (excluding text written by external commands).
19 * ScreenAttrs[off] Contains the associated attributes.
20 * LineOffset[row] Contains the offset into ScreenLines*[] and ScreenAttrs[]
21 * for each line.
22 * LineWraps[row] Flag for each line whether it wraps to the next line.
23 *
24 * For double-byte characters, two consecutive bytes in ScreenLines[] can form
25 * one character which occupies two display cells.
26 * For UTF-8 a multi-byte character is converted to Unicode and stored in
27 * ScreenLinesUC[]. ScreenLines[] contains the first byte only. For an ASCII
28 * character without composing chars ScreenLinesUC[] will be 0. When the
29 * character occupies two display cells the next byte in ScreenLines[] is 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +000030 * ScreenLinesC[][] contain up to 'maxcombine' composing characters
Bram Moolenaar071d4272004-06-13 20:20:40 +000031 * (drawn on top of the first character). They are 0 when not used.
32 * ScreenLines2[] is only used for euc-jp to store the second byte if the
33 * first byte is 0x8e (single-width character).
34 *
35 * The screen_*() functions write to the screen and handle updating
36 * ScreenLines[].
37 *
38 * update_screen() is the function that updates all windows and status lines.
39 * It is called form the main loop when must_redraw is non-zero. It may be
Bram Moolenaar2c7a7632007-05-10 18:19:11 +000040 * called from other places when an immediate screen update is needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +000041 *
42 * The part of the buffer that is displayed in a window is set with:
43 * - w_topline (first buffer line in window)
44 * - w_topfill (filler line above the first line)
45 * - w_leftcol (leftmost window cell in window),
46 * - w_skipcol (skipped window cells of first line)
47 *
48 * Commands that only move the cursor around in a window, do not need to take
49 * action to update the display. The main loop will check if w_topline is
50 * valid and update it (scroll the window) when needed.
51 *
52 * Commands that scroll a window change w_topline and must call
53 * check_cursor() to move the cursor into the visible part of the window, and
54 * call redraw_later(VALID) to have the window displayed by update_screen()
55 * later.
56 *
57 * Commands that change text in the buffer must call changed_bytes() or
58 * changed_lines() to mark the area that changed and will require updating
59 * later. The main loop will call update_screen(), which will update each
60 * window that shows the changed buffer. This assumes text above the change
61 * can remain displayed as it is. Text after the change may need updating for
62 * scrolling, folding and syntax highlighting.
63 *
64 * Commands that change how a window is displayed (e.g., setting 'list') or
65 * invalidate the contents of a window in another way (e.g., change fold
66 * settings), must call redraw_later(NOT_VALID) to have the whole window
67 * redisplayed by update_screen() later.
68 *
69 * Commands that change how a buffer is displayed (e.g., setting 'tabstop')
70 * must call redraw_curbuf_later(NOT_VALID) to have all the windows for the
71 * buffer redisplayed by update_screen() later.
72 *
Bram Moolenaar600dddc2006-03-12 22:05:10 +000073 * Commands that change highlighting and possibly cause a scroll too must call
74 * redraw_later(SOME_VALID) to update the whole window but still use scrolling
75 * to avoid redrawing everything. But the length of displayed lines must not
76 * change, use NOT_VALID then.
77 *
Bram Moolenaar071d4272004-06-13 20:20:40 +000078 * Commands that move the window position must call redraw_later(NOT_VALID).
79 * TODO: should minimize redrawing by scrolling when possible.
80 *
81 * Commands that change everything (e.g., resizing the screen) must call
82 * redraw_all_later(NOT_VALID) or redraw_all_later(CLEAR).
83 *
84 * Things that are handled indirectly:
85 * - When messages scroll the screen up, msg_scrolled will be set and
86 * update_screen() called to redraw.
87 */
88
89#include "vim.h"
90
91/*
92 * The attributes that are actually active for writing to the screen.
93 */
94static int screen_attr = 0;
95
96/*
97 * Positioning the cursor is reduced by remembering the last position.
98 * Mostly used by windgoto() and screen_char().
99 */
100static int screen_cur_row, screen_cur_col; /* last known cursor position */
101
102#ifdef FEAT_SEARCH_EXTRA
103/*
104 * Struct used for highlighting 'hlsearch' matches for the last use search
105 * pattern or a ":match" item.
106 * For 'hlsearch' there is one pattern for all windows. For ":match" there is
107 * a different pattern for each window.
108 */
109typedef struct
110{
111 regmmatch_T rm; /* points to the regexp program; contains last found
112 match (may continue in next line) */
113 buf_T *buf; /* the buffer to search for a match */
114 linenr_T lnum; /* the line to search for a match */
115 int attr; /* attributes to be used for a match */
116 int attr_cur; /* attributes currently active in win_line() */
117 linenr_T first_lnum; /* first lnum to search for multi-line pat */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000118 colnr_T startcol; /* in win_line() points to char where HL starts */
119 colnr_T endcol; /* in win_line() points to char where HL ends */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120} match_T;
121
122static match_T search_hl; /* used for 'hlsearch' highlight matching */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000123static match_T match_hl[3]; /* used for ":match" highlight matching */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124#endif
125
126#ifdef FEAT_FOLDING
127static foldinfo_T win_foldinfo; /* info for 'foldcolumn' */
128#endif
129
130/*
131 * Buffer for one screen line (characters and attributes).
132 */
133static schar_T *current_ScreenLine;
134
135static void win_update __ARGS((win_T *wp));
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000136static void win_draw_end __ARGS((win_T *wp, int c1, int c2, int row, int endrow, hlf_T hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137#ifdef FEAT_FOLDING
138static void fold_line __ARGS((win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row));
139static void fill_foldcolumn __ARGS((char_u *p, win_T *wp, int closed, linenr_T lnum));
140static void copy_text_attr __ARGS((int off, char_u *buf, int len, int attr));
141#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000142static int win_line __ARGS((win_T *, linenr_T, int, int, int nochange));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143static int char_needs_redraw __ARGS((int off_from, int off_to, int cols));
144#ifdef FEAT_RIGHTLEFT
145static void screen_line __ARGS((int row, int coloff, int endcol, int clear_width, int rlflag));
146# define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c), (rl))
147#else
148static void screen_line __ARGS((int row, int coloff, int endcol, int clear_width));
149# define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c))
150#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151#ifdef FEAT_VERTSPLIT
152static void draw_vsep_win __ARGS((win_T *wp, int row));
153#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +0000154#ifdef FEAT_STL_OPT
155static void redraw_custum_statusline __ARGS((win_T *wp));
156#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157#ifdef FEAT_SEARCH_EXTRA
158static void start_search_hl __ARGS((void));
159static void end_search_hl __ARGS((void));
160static void prepare_search_hl __ARGS((win_T *wp, linenr_T lnum));
161static void next_search_hl __ARGS((win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol));
162#endif
163static void screen_start_highlight __ARGS((int attr));
164static void screen_char __ARGS((unsigned off, int row, int col));
165#ifdef FEAT_MBYTE
166static void screen_char_2 __ARGS((unsigned off, int row, int col));
167#endif
168static void screenclear2 __ARGS((void));
169static void lineclear __ARGS((unsigned off, int width));
170static void lineinvalid __ARGS((unsigned off, int width));
171#ifdef FEAT_VERTSPLIT
172static void linecopy __ARGS((int to, int from, win_T *wp));
173static void redraw_block __ARGS((int row, int end, win_T *wp));
174#endif
175static int win_do_lines __ARGS((win_T *wp, int row, int line_count, int mayclear, int del));
176static void win_rest_invalid __ARGS((win_T *wp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177static void msg_pos_mode __ARGS((void));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000178#if defined(FEAT_WINDOWS)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000179static void draw_tabline __ARGS((void));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000180#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
182static int fillchar_status __ARGS((int *attr, int is_curwin));
183#endif
184#ifdef FEAT_VERTSPLIT
185static int fillchar_vsep __ARGS((int *attr));
186#endif
187#ifdef FEAT_STL_OPT
Bram Moolenaar9372a112005-12-06 19:59:18 +0000188static void win_redr_custom __ARGS((win_T *wp, int draw_ruler));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189#endif
190#ifdef FEAT_CMDL_INFO
191static void win_redr_ruler __ARGS((win_T *wp, int always));
192#endif
193
194#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
195/* Ugly global: overrule attribute used by screen_char() */
196static int screen_char_attr = 0;
197#endif
198
199/*
200 * Redraw the current window later, with update_screen(type).
201 * Set must_redraw only if not already set to a higher value.
202 * e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
203 */
204 void
205redraw_later(type)
206 int type;
207{
208 redraw_win_later(curwin, type);
209}
210
211 void
212redraw_win_later(wp, type)
213 win_T *wp;
214 int type;
215{
216 if (wp->w_redr_type < type)
217 {
218 wp->w_redr_type = type;
219 if (type >= NOT_VALID)
220 wp->w_lines_valid = 0;
221 if (must_redraw < type) /* must_redraw is the maximum of all windows */
222 must_redraw = type;
223 }
224}
225
226/*
227 * Force a complete redraw later. Also resets the highlighting. To be used
228 * after executing a shell command that messes up the screen.
229 */
230 void
231redraw_later_clear()
232{
233 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000234#ifdef FEAT_GUI
235 if (gui.in_use)
236 /* Use a code that will reset gui.highlight_mask in
237 * gui_stop_highlight(). */
238 screen_attr = HL_ALL + 1;
239 else
240#endif
241 /* Use attributes that is very unlikely to appear in text. */
242 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243}
244
245/*
246 * Mark all windows to be redrawn later.
247 */
248 void
249redraw_all_later(type)
250 int type;
251{
252 win_T *wp;
253
254 FOR_ALL_WINDOWS(wp)
255 {
256 redraw_win_later(wp, type);
257 }
258}
259
260/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000261 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262 */
263 void
264redraw_curbuf_later(type)
265 int type;
266{
267 redraw_buf_later(curbuf, type);
268}
269
270 void
271redraw_buf_later(buf, type)
272 buf_T *buf;
273 int type;
274{
275 win_T *wp;
276
277 FOR_ALL_WINDOWS(wp)
278 {
279 if (wp->w_buffer == buf)
280 redraw_win_later(wp, type);
281 }
282}
283
284/*
285 * Changed something in the current window, at buffer line "lnum", that
286 * requires that line and possibly other lines to be redrawn.
287 * Used when entering/leaving Insert mode with the cursor on a folded line.
288 * Used to remove the "$" from a change command.
289 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
290 * may become invalid and the whole window will have to be redrawn.
291 */
292/*ARGSUSED*/
293 void
294redrawWinline(lnum, invalid)
295 linenr_T lnum;
296 int invalid; /* window line height is invalid now */
297{
298#ifdef FEAT_FOLDING
299 int i;
300#endif
301
302 if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
303 curwin->w_redraw_top = lnum;
304 if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
305 curwin->w_redraw_bot = lnum;
306 redraw_later(VALID);
307
308#ifdef FEAT_FOLDING
309 if (invalid)
310 {
311 /* A w_lines[] entry for this lnum has become invalid. */
312 i = find_wl_entry(curwin, lnum);
313 if (i >= 0)
314 curwin->w_lines[i].wl_valid = FALSE;
315 }
316#endif
317}
318
319/*
320 * update all windows that are editing the current buffer
321 */
322 void
323update_curbuf(type)
324 int type;
325{
326 redraw_curbuf_later(type);
327 update_screen(type);
328}
329
330/*
331 * update_screen()
332 *
333 * Based on the current value of curwin->w_topline, transfer a screenfull
334 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
335 */
336 void
337update_screen(type)
338 int type;
339{
340 win_T *wp;
341 static int did_intro = FALSE;
342#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
343 int did_one;
344#endif
345
346 if (!screen_valid(TRUE))
347 return;
348
349 if (must_redraw)
350 {
351 if (type < must_redraw) /* use maximal type */
352 type = must_redraw;
353 must_redraw = 0;
354 }
355
356 /* Need to update w_lines[]. */
357 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
358 type = NOT_VALID;
359
360 if (!redrawing())
361 {
362 redraw_later(type); /* remember type for next time */
363 must_redraw = type;
364 if (type > INVERTED_ALL)
365 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
366 return;
367 }
368
369 updating_screen = TRUE;
370#ifdef FEAT_SYN_HL
371 ++display_tick; /* let syntax code know we're in a next round of
372 * display updating */
373#endif
374
375 /*
376 * if the screen was scrolled up when displaying a message, scroll it down
377 */
378 if (msg_scrolled)
379 {
380 clear_cmdline = TRUE;
381 if (msg_scrolled > Rows - 5) /* clearing is faster */
382 type = CLEAR;
383 else if (type != CLEAR)
384 {
385 check_for_delay(FALSE);
386 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, NULL) == FAIL)
387 type = CLEAR;
388 FOR_ALL_WINDOWS(wp)
389 {
390 if (W_WINROW(wp) < msg_scrolled)
391 {
392 if (W_WINROW(wp) + wp->w_height > msg_scrolled
393 && wp->w_redr_type < REDRAW_TOP
394 && wp->w_lines_valid > 0
395 && wp->w_topline == wp->w_lines[0].wl_lnum)
396 {
397 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
398 wp->w_redr_type = REDRAW_TOP;
399 }
400 else
401 {
402 wp->w_redr_type = NOT_VALID;
403#ifdef FEAT_WINDOWS
404 if (W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp)
405 <= msg_scrolled)
406 wp->w_redr_status = TRUE;
407#endif
408 }
409 }
410 }
411 redraw_cmdline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000412#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000413 redraw_tabline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000414#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415 }
416 msg_scrolled = 0;
417 need_wait_return = FALSE;
418 }
419
420 /* reset cmdline_row now (may have been changed temporarily) */
421 compute_cmdrow();
422
423 /* Check for changed highlighting */
424 if (need_highlight_changed)
425 highlight_changed();
426
427 if (type == CLEAR) /* first clear screen */
428 {
429 screenclear(); /* will reset clear_cmdline */
430 type = NOT_VALID;
431 }
432
433 if (clear_cmdline) /* going to clear cmdline (done below) */
434 check_for_delay(FALSE);
435
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000436#ifdef FEAT_LINEBREAK
437 /* Force redraw when width of 'number' column changes. */
438 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000439 && curwin->w_nrwidth != (curwin->w_p_nu ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000440 curwin->w_redr_type = NOT_VALID;
441#endif
442
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443 /*
444 * Only start redrawing if there is really something to do.
445 */
446 if (type == INVERTED)
447 update_curswant();
448 if (curwin->w_redr_type < type
449 && !((type == VALID
450 && curwin->w_lines[0].wl_valid
451#ifdef FEAT_DIFF
452 && curwin->w_topfill == curwin->w_old_topfill
453 && curwin->w_botfill == curwin->w_old_botfill
454#endif
455 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
456#ifdef FEAT_VISUAL
457 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000458 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
460 && curwin->w_old_visual_mode == VIsual_mode
461 && (curwin->w_valid & VALID_VIRTCOL)
462 && curwin->w_old_curswant == curwin->w_curswant)
463#endif
464 ))
465 curwin->w_redr_type = type;
466
Bram Moolenaar5a305422006-04-28 22:38:25 +0000467#ifdef FEAT_WINDOWS
468 /* Redraw the tab pages line if needed. */
469 if (redraw_tabline || type >= NOT_VALID)
470 draw_tabline();
471#endif
472
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473#ifdef FEAT_SYN_HL
474 /*
475 * Correct stored syntax highlighting info for changes in each displayed
476 * buffer. Each buffer must only be done once.
477 */
478 FOR_ALL_WINDOWS(wp)
479 {
480 if (wp->w_buffer->b_mod_set)
481 {
482# ifdef FEAT_WINDOWS
483 win_T *wwp;
484
485 /* Check if we already did this buffer. */
486 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
487 if (wwp->w_buffer == wp->w_buffer)
488 break;
489# endif
490 if (
491# ifdef FEAT_WINDOWS
492 wwp == wp &&
493# endif
494 syntax_present(wp->w_buffer))
495 syn_stack_apply_changes(wp->w_buffer);
496 }
497 }
498#endif
499
500 /*
501 * Go from top to bottom through the windows, redrawing the ones that need
502 * it.
503 */
504#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
505 did_one = FALSE;
506#endif
507#ifdef FEAT_SEARCH_EXTRA
508 search_hl.rm.regprog = NULL;
509#endif
510 FOR_ALL_WINDOWS(wp)
511 {
512 if (wp->w_redr_type != 0)
513 {
514 cursor_off();
515#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
516 if (!did_one)
517 {
518 did_one = TRUE;
519# ifdef FEAT_SEARCH_EXTRA
520 start_search_hl();
521# endif
522# ifdef FEAT_CLIPBOARD
523 /* When Visual area changed, may have to update selection. */
524 if (clip_star.available && clip_isautosel())
525 clip_update_selection();
526# endif
527#ifdef FEAT_GUI
528 /* Remove the cursor before starting to do anything, because
529 * scrolling may make it difficult to redraw the text under
530 * it. */
531 if (gui.in_use)
532 gui_undraw_cursor();
533#endif
534 }
535#endif
536 win_update(wp);
537 }
538
539#ifdef FEAT_WINDOWS
540 /* redraw status line after the window to minimize cursor movement */
541 if (wp->w_redr_status)
542 {
543 cursor_off();
544 win_redr_status(wp);
545 }
546#endif
547 }
548#if defined(FEAT_SEARCH_EXTRA)
549 end_search_hl();
550#endif
551
552#ifdef FEAT_WINDOWS
553 /* Reset b_mod_set flags. Going through all windows is probably faster
554 * than going through all buffers (there could be many buffers). */
555 for (wp = firstwin; wp != NULL; wp = wp->w_next)
556 wp->w_buffer->b_mod_set = FALSE;
557#else
558 curbuf->b_mod_set = FALSE;
559#endif
560
561 updating_screen = FALSE;
562#ifdef FEAT_GUI
563 gui_may_resize_shell();
564#endif
565
566 /* Clear or redraw the command line. Done last, because scrolling may
567 * mess up the command line. */
568 if (clear_cmdline || redraw_cmdline)
569 showmode();
570
571 /* May put up an introductory message when not editing a file */
572 if (!did_intro && bufempty()
573 && curbuf->b_fname == NULL
574#ifdef FEAT_WINDOWS
575 && firstwin->w_next == NULL
576#endif
577 && vim_strchr(p_shm, SHM_INTRO) == NULL)
578 intro_message(FALSE);
579 did_intro = TRUE;
580
581#ifdef FEAT_GUI
582 /* Redraw the cursor and update the scrollbars when all screen updating is
583 * done. */
584 if (gui.in_use)
585 {
586 out_flush(); /* required before updating the cursor */
587 if (did_one)
588 gui_update_cursor(FALSE, FALSE);
589 gui_update_scrollbars(FALSE);
590 }
591#endif
592}
593
594#if defined(FEAT_SIGNS) || defined(FEAT_GUI)
595static void update_prepare __ARGS((void));
596static void update_finish __ARGS((void));
597
598/*
599 * Prepare for updating one or more windows.
600 */
601 static void
602update_prepare()
603{
604 cursor_off();
605 updating_screen = TRUE;
606#ifdef FEAT_GUI
607 /* Remove the cursor before starting to do anything, because scrolling may
608 * make it difficult to redraw the text under it. */
609 if (gui.in_use)
610 gui_undraw_cursor();
611#endif
612#ifdef FEAT_SEARCH_EXTRA
613 start_search_hl();
614#endif
615}
616
617/*
618 * Finish updating one or more windows.
619 */
620 static void
621update_finish()
622{
623 if (redraw_cmdline)
624 showmode();
625
626# ifdef FEAT_SEARCH_EXTRA
627 end_search_hl();
628# endif
629
630 updating_screen = FALSE;
631
632# ifdef FEAT_GUI
633 gui_may_resize_shell();
634
635 /* Redraw the cursor and update the scrollbars when all screen updating is
636 * done. */
637 if (gui.in_use)
638 {
639 out_flush(); /* required before updating the cursor */
640 gui_update_cursor(FALSE, FALSE);
641 gui_update_scrollbars(FALSE);
642 }
643# endif
644}
645#endif
646
647#if defined(FEAT_SIGNS) || defined(PROTO)
648 void
649update_debug_sign(buf, lnum)
650 buf_T *buf;
651 linenr_T lnum;
652{
653 win_T *wp;
654 int doit = FALSE;
655
656# ifdef FEAT_FOLDING
657 win_foldinfo.fi_level = 0;
658# endif
659
660 /* update/delete a specific mark */
661 FOR_ALL_WINDOWS(wp)
662 {
663 if (buf != NULL && lnum > 0)
664 {
665 if (wp->w_buffer == buf && lnum >= wp->w_topline
666 && lnum < wp->w_botline)
667 {
668 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
669 wp->w_redraw_top = lnum;
670 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
671 wp->w_redraw_bot = lnum;
672 redraw_win_later(wp, VALID);
673 }
674 }
675 else
676 redraw_win_later(wp, VALID);
677 if (wp->w_redr_type != 0)
678 doit = TRUE;
679 }
680
681 if (!doit)
682 return;
683
684 /* update all windows that need updating */
685 update_prepare();
686
687# ifdef FEAT_WINDOWS
688 for (wp = firstwin; wp; wp = wp->w_next)
689 {
690 if (wp->w_redr_type != 0)
691 win_update(wp);
692 if (wp->w_redr_status)
693 win_redr_status(wp);
694 }
695# else
696 if (curwin->w_redr_type != 0)
697 win_update(curwin);
698# endif
699
700 update_finish();
701}
702#endif
703
704
705#if defined(FEAT_GUI) || defined(PROTO)
706/*
707 * Update a single window, its status line and maybe the command line msg.
708 * Used for the GUI scrollbar.
709 */
710 void
711updateWindow(wp)
712 win_T *wp;
713{
714 update_prepare();
715
716#ifdef FEAT_CLIPBOARD
717 /* When Visual area changed, may have to update selection. */
718 if (clip_star.available && clip_isautosel())
719 clip_update_selection();
720#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000721
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000723
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724#ifdef FEAT_WINDOWS
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000725 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000726 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000727 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000728
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 if (wp->w_redr_status
730# ifdef FEAT_CMDL_INFO
731 || p_ru
732# endif
733# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000734 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735# endif
736 )
737 win_redr_status(wp);
738#endif
739
740 update_finish();
741}
742#endif
743
744/*
745 * Update a single window.
746 *
747 * This may cause the windows below it also to be redrawn (when clearing the
748 * screen or scrolling lines).
749 *
750 * How the window is redrawn depends on wp->w_redr_type. Each type also
751 * implies the one below it.
752 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000753 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
755 * INVERTED redraw the changed part of the Visual area
756 * INVERTED_ALL redraw the whole Visual area
757 * VALID 1. scroll up/down to adjust for a changed w_topline
758 * 2. update lines at the top when scrolled down
759 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000760 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 * b_mod_top and b_mod_bot.
762 * - if wp->w_redraw_top non-zero, redraw lines between
763 * wp->w_redraw_top and wp->w_redr_bot.
764 * - continue redrawing when syntax status is invalid.
765 * 4. if scrolled up, update lines at the bottom.
766 * This results in three areas that may need updating:
767 * top: from first row to top_end (when scrolled down)
768 * mid: from mid_start to mid_end (update inversion or changed text)
769 * bot: from bot_start to last row (when scrolled up)
770 */
771 static void
772win_update(wp)
773 win_T *wp;
774{
775 buf_T *buf = wp->w_buffer;
776 int type;
777 int top_end = 0; /* Below last row of the top area that needs
778 updating. 0 when no top area updating. */
779 int mid_start = 999;/* first row of the mid area that needs
780 updating. 999 when no mid area updating. */
781 int mid_end = 0; /* Below last row of the mid area that needs
782 updating. 0 when no mid area updating. */
783 int bot_start = 999;/* first row of the bot area that needs
784 updating. 999 when no bot area updating */
785#ifdef FEAT_VISUAL
786 int scrolled_down = FALSE; /* TRUE when scrolled down when
787 w_topline got smaller a bit */
788#endif
789#ifdef FEAT_SEARCH_EXTRA
790 int top_to_mod = FALSE; /* redraw above mod_top */
791#endif
792
793 int row; /* current window row to display */
794 linenr_T lnum; /* current buffer lnum to display */
795 int idx; /* current index in w_lines[] */
796 int srow; /* starting row of the current line */
797
798 int eof = FALSE; /* if TRUE, we hit the end of the file */
799 int didline = FALSE; /* if TRUE, we finished the last line */
800 int i;
801 long j;
802 static int recursive = FALSE; /* being called recursively */
803 int old_botline = wp->w_botline;
804#ifdef FEAT_FOLDING
805 long fold_count;
806#endif
807#ifdef FEAT_SYN_HL
808 /* remember what happened to the previous line, to know if
809 * check_visual_highlight() can be used */
810#define DID_NONE 1 /* didn't update a line */
811#define DID_LINE 2 /* updated a normal line */
812#define DID_FOLD 3 /* updated a folded line */
813 int did_update = DID_NONE;
814 linenr_T syntax_last_parsed = 0; /* last parsed text line */
815#endif
816 linenr_T mod_top = 0;
817 linenr_T mod_bot = 0;
818#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
819 int save_got_int;
820#endif
821
822 type = wp->w_redr_type;
823
824 if (type == NOT_VALID)
825 {
826#ifdef FEAT_WINDOWS
827 wp->w_redr_status = TRUE;
828#endif
829 wp->w_lines_valid = 0;
830 }
831
832 /* Window is zero-height: nothing to draw. */
833 if (wp->w_height == 0)
834 {
835 wp->w_redr_type = 0;
836 return;
837 }
838
839#ifdef FEAT_VERTSPLIT
840 /* Window is zero-width: Only need to draw the separator. */
841 if (wp->w_width == 0)
842 {
843 /* draw the vertical separator right of this window */
844 draw_vsep_win(wp, 0);
845 wp->w_redr_type = 0;
846 return;
847 }
848#endif
849
850#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000851 /* Setup for ":match" and 'hlsearch' highlighting. Disable any previous
852 * match */
853 for (i = 0; i < 3; ++i)
854 {
855 match_hl[i].rm = wp->w_match[i];
856 if (wp->w_match_id[i] == 0)
857 match_hl[i].attr = 0;
858 else
859 match_hl[i].attr = syn_id2attr(wp->w_match_id[i]);
860 match_hl[i].buf = buf;
861 match_hl[i].lnum = 0;
862 match_hl[i].first_lnum = 0;
863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 search_hl.buf = buf;
865 search_hl.lnum = 0;
866 search_hl.first_lnum = 0;
867#endif
868
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000869#ifdef FEAT_LINEBREAK
870 /* Force redraw when width of 'number' column changes. */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000871 i = wp->w_p_nu ? number_width(wp) : 0;
872 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000873 {
874 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000875 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000876 }
877 else
878#endif
879
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
881 {
882 /*
883 * When there are both inserted/deleted lines and specific lines to be
884 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
885 * everything (only happens when redrawing is off for while).
886 */
887 type = NOT_VALID;
888 }
889 else
890 {
891 /*
892 * Set mod_top to the first line that needs displaying because of
893 * changes. Set mod_bot to the first line after the changes.
894 */
895 mod_top = wp->w_redraw_top;
896 if (wp->w_redraw_bot != 0)
897 mod_bot = wp->w_redraw_bot + 1;
898 else
899 mod_bot = 0;
900 wp->w_redraw_top = 0; /* reset for next time */
901 wp->w_redraw_bot = 0;
902 if (buf->b_mod_set)
903 {
904 if (mod_top == 0 || mod_top > buf->b_mod_top)
905 {
906 mod_top = buf->b_mod_top;
907#ifdef FEAT_SYN_HL
908 /* Need to redraw lines above the change that may be included
909 * in a pattern match. */
910 if (syntax_present(buf))
911 {
912 mod_top -= buf->b_syn_sync_linebreaks;
913 if (mod_top < 1)
914 mod_top = 1;
915 }
916#endif
917 }
918 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
919 mod_bot = buf->b_mod_bot;
920
921#ifdef FEAT_SEARCH_EXTRA
922 /* When 'hlsearch' is on and using a multi-line search pattern, a
923 * change in one line may make the Search highlighting in a
924 * previous line invalid. Simple solution: redraw all visible
925 * lines above the change.
926 * Same for a ":match" pattern.
927 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000928 if (search_hl.rm.regprog != NULL
929 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000931 else
932 for (i = 0; i < 3; ++i)
933 if (match_hl[i].rm.regprog != NULL
934 && re_multiline(match_hl[i].rm.regprog))
935 {
936 top_to_mod = TRUE;
937 break;
938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939#endif
940 }
941#ifdef FEAT_FOLDING
942 if (mod_top != 0 && hasAnyFolding(wp))
943 {
944 linenr_T lnumt, lnumb;
945
946 /*
947 * A change in a line can cause lines above it to become folded or
948 * unfolded. Find the top most buffer line that may be affected.
949 * If the line was previously folded and displayed, get the first
950 * line of that fold. If the line is folded now, get the first
951 * folded line. Use the minimum of these two.
952 */
953
954 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
955 * the line below it. If there is no valid entry, use w_topline.
956 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
957 * to this line. If there is no valid entry, use MAXLNUM. */
958 lnumt = wp->w_topline;
959 lnumb = MAXLNUM;
960 for (i = 0; i < wp->w_lines_valid; ++i)
961 if (wp->w_lines[i].wl_valid)
962 {
963 if (wp->w_lines[i].wl_lastlnum < mod_top)
964 lnumt = wp->w_lines[i].wl_lastlnum + 1;
965 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
966 {
967 lnumb = wp->w_lines[i].wl_lnum;
968 /* When there is a fold column it might need updating
969 * in the next line ("J" just above an open fold). */
970 if (wp->w_p_fdc > 0)
971 ++lnumb;
972 }
973 }
974
975 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
976 if (mod_top > lnumt)
977 mod_top = lnumt;
978
979 /* Now do the same for the bottom line (one above mod_bot). */
980 --mod_bot;
981 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
982 ++mod_bot;
983 if (mod_bot < lnumb)
984 mod_bot = lnumb;
985 }
986#endif
987
988 /* When a change starts above w_topline and the end is below
989 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000990 * If the end of the change is above w_topline: do like no change was
991 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 if (mod_top != 0 && mod_top < wp->w_topline)
993 {
994 if (mod_bot > wp->w_topline)
995 mod_top = wp->w_topline;
996#ifdef FEAT_SYN_HL
997 else if (syntax_present(buf))
998 top_end = 1;
999#endif
1000 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001001
1002 /* When line numbers are displayed need to redraw all lines below
1003 * inserted/deleted lines. */
1004 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1005 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 }
1007
1008 /*
1009 * When only displaying the lines at the top, set top_end. Used when
1010 * window has scrolled down for msg_scrolled.
1011 */
1012 if (type == REDRAW_TOP)
1013 {
1014 j = 0;
1015 for (i = 0; i < wp->w_lines_valid; ++i)
1016 {
1017 j += wp->w_lines[i].wl_size;
1018 if (j >= wp->w_upd_rows)
1019 {
1020 top_end = j;
1021 break;
1022 }
1023 }
1024 if (top_end == 0)
1025 /* not found (cannot happen?): redraw everything */
1026 type = NOT_VALID;
1027 else
1028 /* top area defined, the rest is VALID */
1029 type = VALID;
1030 }
1031
1032 /*
1033 * If there are no changes on the screen that require a complete redraw,
1034 * handle three cases:
1035 * 1: we are off the top of the screen by a few lines: scroll down
1036 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1037 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1038 * w_lines[] that needs updating.
1039 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001040 if ((type == VALID || type == SOME_VALID
1041 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042#ifdef FEAT_DIFF
1043 && !wp->w_botfill && !wp->w_old_botfill
1044#endif
1045 )
1046 {
1047 if (mod_top != 0 && wp->w_topline == mod_top)
1048 {
1049 /*
1050 * w_topline is the first changed line, the scrolling will be done
1051 * further down.
1052 */
1053 }
1054 else if (wp->w_lines[0].wl_valid
1055 && (wp->w_topline < wp->w_lines[0].wl_lnum
1056#ifdef FEAT_DIFF
1057 || (wp->w_topline == wp->w_lines[0].wl_lnum
1058 && wp->w_topfill > wp->w_old_topfill)
1059#endif
1060 ))
1061 {
1062 /*
1063 * New topline is above old topline: May scroll down.
1064 */
1065#ifdef FEAT_FOLDING
1066 if (hasAnyFolding(wp))
1067 {
1068 linenr_T ln;
1069
1070 /* count the number of lines we are off, counting a sequence
1071 * of folded lines as one */
1072 j = 0;
1073 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1074 {
1075 ++j;
1076 if (j >= wp->w_height - 2)
1077 break;
1078 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1079 }
1080 }
1081 else
1082#endif
1083 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1084 if (j < wp->w_height - 2) /* not too far off */
1085 {
1086 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1087#ifdef FEAT_DIFF
1088 /* insert extra lines for previously invisible filler lines */
1089 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1090 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1091 - wp->w_old_topfill;
1092#endif
1093 if (i < wp->w_height - 2) /* less than a screen off */
1094 {
1095 /*
1096 * Try to insert the correct number of lines.
1097 * If not the last window, delete the lines at the bottom.
1098 * win_ins_lines may fail when the terminal can't do it.
1099 */
1100 if (i > 0)
1101 check_for_delay(FALSE);
1102 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1103 {
1104 if (wp->w_lines_valid != 0)
1105 {
1106 /* Need to update rows that are new, stop at the
1107 * first one that scrolled down. */
1108 top_end = i;
1109#ifdef FEAT_VISUAL
1110 scrolled_down = TRUE;
1111#endif
1112
1113 /* Move the entries that were scrolled, disable
1114 * the entries for the lines to be redrawn. */
1115 if ((wp->w_lines_valid += j) > wp->w_height)
1116 wp->w_lines_valid = wp->w_height;
1117 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1118 wp->w_lines[idx] = wp->w_lines[idx - j];
1119 while (idx >= 0)
1120 wp->w_lines[idx--].wl_valid = FALSE;
1121 }
1122 }
1123 else
1124 mid_start = 0; /* redraw all lines */
1125 }
1126 else
1127 mid_start = 0; /* redraw all lines */
1128 }
1129 else
1130 mid_start = 0; /* redraw all lines */
1131 }
1132 else
1133 {
1134 /*
1135 * New topline is at or below old topline: May scroll up.
1136 * When topline didn't change, find first entry in w_lines[] that
1137 * needs updating.
1138 */
1139
1140 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1141 j = -1;
1142 row = 0;
1143 for (i = 0; i < wp->w_lines_valid; i++)
1144 {
1145 if (wp->w_lines[i].wl_valid
1146 && wp->w_lines[i].wl_lnum == wp->w_topline)
1147 {
1148 j = i;
1149 break;
1150 }
1151 row += wp->w_lines[i].wl_size;
1152 }
1153 if (j == -1)
1154 {
1155 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1156 * lines */
1157 mid_start = 0;
1158 }
1159 else
1160 {
1161 /*
1162 * Try to delete the correct number of lines.
1163 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1164 */
1165#ifdef FEAT_DIFF
1166 /* If the topline didn't change, delete old filler lines,
1167 * otherwise delete filler lines of the new topline... */
1168 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1169 row += wp->w_old_topfill;
1170 else
1171 row += diff_check_fill(wp, wp->w_topline);
1172 /* ... but don't delete new filler lines. */
1173 row -= wp->w_topfill;
1174#endif
1175 if (row > 0)
1176 {
1177 check_for_delay(FALSE);
1178 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1179 bot_start = wp->w_height - row;
1180 else
1181 mid_start = 0; /* redraw all lines */
1182 }
1183 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1184 {
1185 /*
1186 * Skip the lines (below the deleted lines) that are still
1187 * valid and don't need redrawing. Copy their info
1188 * upwards, to compensate for the deleted lines. Set
1189 * bot_start to the first row that needs redrawing.
1190 */
1191 bot_start = 0;
1192 idx = 0;
1193 for (;;)
1194 {
1195 wp->w_lines[idx] = wp->w_lines[j];
1196 /* stop at line that didn't fit, unless it is still
1197 * valid (no lines deleted) */
1198 if (row > 0 && bot_start + row
1199 + (int)wp->w_lines[j].wl_size > wp->w_height)
1200 {
1201 wp->w_lines_valid = idx + 1;
1202 break;
1203 }
1204 bot_start += wp->w_lines[idx++].wl_size;
1205
1206 /* stop at the last valid entry in w_lines[].wl_size */
1207 if (++j >= wp->w_lines_valid)
1208 {
1209 wp->w_lines_valid = idx;
1210 break;
1211 }
1212 }
1213#ifdef FEAT_DIFF
1214 /* Correct the first entry for filler lines at the top
1215 * when it won't get updated below. */
1216 if (wp->w_p_diff && bot_start > 0)
1217 wp->w_lines[0].wl_size =
1218 plines_win_nofill(wp, wp->w_topline, TRUE)
1219 + wp->w_topfill;
1220#endif
1221 }
1222 }
1223 }
1224
1225 /* When starting redraw in the first line, redraw all lines. When
1226 * there is only one window it's probably faster to clear the screen
1227 * first. */
1228 if (mid_start == 0)
1229 {
1230 mid_end = wp->w_height;
1231 if (lastwin == firstwin)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001232 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001234#ifdef FEAT_WINDOWS
1235 /* The screen was cleared, redraw the tab pages line. */
1236 if (redraw_tabline)
1237 draw_tabline();
1238#endif
1239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 }
1241 }
1242 else
1243 {
1244 /* Not VALID or INVERTED: redraw all lines. */
1245 mid_start = 0;
1246 mid_end = wp->w_height;
1247 }
1248
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001249 if (type == SOME_VALID)
1250 {
1251 /* SOME_VALID: redraw all lines. */
1252 mid_start = 0;
1253 mid_end = wp->w_height;
1254 type = NOT_VALID;
1255 }
1256
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257#ifdef FEAT_VISUAL
1258 /* check if we are updating or removing the inverted part */
1259 if ((VIsual_active && buf == curwin->w_buffer)
1260 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1261 {
1262 linenr_T from, to;
1263
1264 if (VIsual_active)
1265 {
1266 if (VIsual_active
1267 && (VIsual_mode != wp->w_old_visual_mode
1268 || type == INVERTED_ALL))
1269 {
1270 /*
1271 * If the type of Visual selection changed, redraw the whole
1272 * selection. Also when the ownership of the X selection is
1273 * gained or lost.
1274 */
1275 if (curwin->w_cursor.lnum < VIsual.lnum)
1276 {
1277 from = curwin->w_cursor.lnum;
1278 to = VIsual.lnum;
1279 }
1280 else
1281 {
1282 from = VIsual.lnum;
1283 to = curwin->w_cursor.lnum;
1284 }
1285 /* redraw more when the cursor moved as well */
1286 if (wp->w_old_cursor_lnum < from)
1287 from = wp->w_old_cursor_lnum;
1288 if (wp->w_old_cursor_lnum > to)
1289 to = wp->w_old_cursor_lnum;
1290 if (wp->w_old_visual_lnum < from)
1291 from = wp->w_old_visual_lnum;
1292 if (wp->w_old_visual_lnum > to)
1293 to = wp->w_old_visual_lnum;
1294 }
1295 else
1296 {
1297 /*
1298 * Find the line numbers that need to be updated: The lines
1299 * between the old cursor position and the current cursor
1300 * position. Also check if the Visual position changed.
1301 */
1302 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1303 {
1304 from = curwin->w_cursor.lnum;
1305 to = wp->w_old_cursor_lnum;
1306 }
1307 else
1308 {
1309 from = wp->w_old_cursor_lnum;
1310 to = curwin->w_cursor.lnum;
1311 if (from == 0) /* Visual mode just started */
1312 from = to;
1313 }
1314
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001315 if (VIsual.lnum != wp->w_old_visual_lnum
1316 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 {
1318 if (wp->w_old_visual_lnum < from
1319 && wp->w_old_visual_lnum != 0)
1320 from = wp->w_old_visual_lnum;
1321 if (wp->w_old_visual_lnum > to)
1322 to = wp->w_old_visual_lnum;
1323 if (VIsual.lnum < from)
1324 from = VIsual.lnum;
1325 if (VIsual.lnum > to)
1326 to = VIsual.lnum;
1327 }
1328 }
1329
1330 /*
1331 * If in block mode and changed column or curwin->w_curswant:
1332 * update all lines.
1333 * First compute the actual start and end column.
1334 */
1335 if (VIsual_mode == Ctrl_V)
1336 {
1337 colnr_T fromc, toc;
1338
1339 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
1340 ++toc;
1341 if (curwin->w_curswant == MAXCOL)
1342 toc = MAXCOL;
1343
1344 if (fromc != wp->w_old_cursor_fcol
1345 || toc != wp->w_old_cursor_lcol)
1346 {
1347 if (from > VIsual.lnum)
1348 from = VIsual.lnum;
1349 if (to < VIsual.lnum)
1350 to = VIsual.lnum;
1351 }
1352 wp->w_old_cursor_fcol = fromc;
1353 wp->w_old_cursor_lcol = toc;
1354 }
1355 }
1356 else
1357 {
1358 /* Use the line numbers of the old Visual area. */
1359 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1360 {
1361 from = wp->w_old_cursor_lnum;
1362 to = wp->w_old_visual_lnum;
1363 }
1364 else
1365 {
1366 from = wp->w_old_visual_lnum;
1367 to = wp->w_old_cursor_lnum;
1368 }
1369 }
1370
1371 /*
1372 * There is no need to update lines above the top of the window.
1373 */
1374 if (from < wp->w_topline)
1375 from = wp->w_topline;
1376
1377 /*
1378 * If we know the value of w_botline, use it to restrict the update to
1379 * the lines that are visible in the window.
1380 */
1381 if (wp->w_valid & VALID_BOTLINE)
1382 {
1383 if (from >= wp->w_botline)
1384 from = wp->w_botline - 1;
1385 if (to >= wp->w_botline)
1386 to = wp->w_botline - 1;
1387 }
1388
1389 /*
1390 * Find the minimal part to be updated.
1391 * Watch out for scrolling that made entries in w_lines[] invalid.
1392 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1393 * top_end; need to redraw from top_end to the "to" line.
1394 * A middle mouse click with a Visual selection may change the text
1395 * above the Visual area and reset wl_valid, do count these for
1396 * mid_end (in srow).
1397 */
1398 if (mid_start > 0)
1399 {
1400 lnum = wp->w_topline;
1401 idx = 0;
1402 srow = 0;
1403 if (scrolled_down)
1404 mid_start = top_end;
1405 else
1406 mid_start = 0;
1407 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1408 {
1409 if (wp->w_lines[idx].wl_valid)
1410 mid_start += wp->w_lines[idx].wl_size;
1411 else if (!scrolled_down)
1412 srow += wp->w_lines[idx].wl_size;
1413 ++idx;
1414# ifdef FEAT_FOLDING
1415 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1416 lnum = wp->w_lines[idx].wl_lnum;
1417 else
1418# endif
1419 ++lnum;
1420 }
1421 srow += mid_start;
1422 mid_end = wp->w_height;
1423 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1424 {
1425 if (wp->w_lines[idx].wl_valid
1426 && wp->w_lines[idx].wl_lnum >= to + 1)
1427 {
1428 /* Only update until first row of this line */
1429 mid_end = srow;
1430 break;
1431 }
1432 srow += wp->w_lines[idx].wl_size;
1433 }
1434 }
1435 }
1436
1437 if (VIsual_active && buf == curwin->w_buffer)
1438 {
1439 wp->w_old_visual_mode = VIsual_mode;
1440 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1441 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001442 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 wp->w_old_curswant = curwin->w_curswant;
1444 }
1445 else
1446 {
1447 wp->w_old_visual_mode = 0;
1448 wp->w_old_cursor_lnum = 0;
1449 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001450 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 }
1452#endif /* FEAT_VISUAL */
1453
1454#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1455 /* reset got_int, otherwise regexp won't work */
1456 save_got_int = got_int;
1457 got_int = 0;
1458#endif
1459#ifdef FEAT_FOLDING
1460 win_foldinfo.fi_level = 0;
1461#endif
1462
1463 /*
1464 * Update all the window rows.
1465 */
1466 idx = 0; /* first entry in w_lines[].wl_size */
1467 row = 0;
1468 srow = 0;
1469 lnum = wp->w_topline; /* first line shown in window */
1470 for (;;)
1471 {
1472 /* stop updating when reached the end of the window (check for _past_
1473 * the end of the window is at the end of the loop) */
1474 if (row == wp->w_height)
1475 {
1476 didline = TRUE;
1477 break;
1478 }
1479
1480 /* stop updating when hit the end of the file */
1481 if (lnum > buf->b_ml.ml_line_count)
1482 {
1483 eof = TRUE;
1484 break;
1485 }
1486
1487 /* Remember the starting row of the line that is going to be dealt
1488 * with. It is used further down when the line doesn't fit. */
1489 srow = row;
1490
1491 /*
1492 * Update a line when it is in an area that needs updating, when it
1493 * has changes or w_lines[idx] is invalid.
1494 * bot_start may be halfway a wrapped line after using
1495 * win_del_lines(), check if the current line includes it.
1496 * When syntax folding is being used, the saved syntax states will
1497 * already have been updated, we can't see where the syntax state is
1498 * the same again, just update until the end of the window.
1499 */
1500 if (row < top_end
1501 || (row >= mid_start && row < mid_end)
1502#ifdef FEAT_SEARCH_EXTRA
1503 || top_to_mod
1504#endif
1505 || idx >= wp->w_lines_valid
1506 || (row + wp->w_lines[idx].wl_size > bot_start)
1507 || (mod_top != 0
1508 && (lnum == mod_top
1509 || (lnum >= mod_top
1510 && (lnum < mod_bot
1511#ifdef FEAT_SYN_HL
1512 || did_update == DID_FOLD
1513 || (did_update == DID_LINE
1514 && syntax_present(buf)
1515 && (
1516# ifdef FEAT_FOLDING
1517 (foldmethodIsSyntax(wp)
1518 && hasAnyFolding(wp)) ||
1519# endif
1520 syntax_check_changed(lnum)))
1521#endif
1522 )))))
1523 {
1524#ifdef FEAT_SEARCH_EXTRA
1525 if (lnum == mod_top)
1526 top_to_mod = FALSE;
1527#endif
1528
1529 /*
1530 * When at start of changed lines: May scroll following lines
1531 * up or down to minimize redrawing.
1532 * Don't do this when the change continues until the end.
1533 * Don't scroll when dollar_vcol is non-zero, keep the "$".
1534 */
1535 if (lnum == mod_top
1536 && mod_bot != MAXLNUM
1537 && !(dollar_vcol != 0 && mod_bot == mod_top + 1))
1538 {
1539 int old_rows = 0;
1540 int new_rows = 0;
1541 int xtra_rows;
1542 linenr_T l;
1543
1544 /* Count the old number of window rows, using w_lines[], which
1545 * should still contain the sizes for the lines as they are
1546 * currently displayed. */
1547 for (i = idx; i < wp->w_lines_valid; ++i)
1548 {
1549 /* Only valid lines have a meaningful wl_lnum. Invalid
1550 * lines are part of the changed area. */
1551 if (wp->w_lines[i].wl_valid
1552 && wp->w_lines[i].wl_lnum == mod_bot)
1553 break;
1554 old_rows += wp->w_lines[i].wl_size;
1555#ifdef FEAT_FOLDING
1556 if (wp->w_lines[i].wl_valid
1557 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1558 {
1559 /* Must have found the last valid entry above mod_bot.
1560 * Add following invalid entries. */
1561 ++i;
1562 while (i < wp->w_lines_valid
1563 && !wp->w_lines[i].wl_valid)
1564 old_rows += wp->w_lines[i++].wl_size;
1565 break;
1566 }
1567#endif
1568 }
1569
1570 if (i >= wp->w_lines_valid)
1571 {
1572 /* We can't find a valid line below the changed lines,
1573 * need to redraw until the end of the window.
1574 * Inserting/deleting lines has no use. */
1575 bot_start = 0;
1576 }
1577 else
1578 {
1579 /* Able to count old number of rows: Count new window
1580 * rows, and may insert/delete lines */
1581 j = idx;
1582 for (l = lnum; l < mod_bot; ++l)
1583 {
1584#ifdef FEAT_FOLDING
1585 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1586 ++new_rows;
1587 else
1588#endif
1589#ifdef FEAT_DIFF
1590 if (l == wp->w_topline)
1591 new_rows += plines_win_nofill(wp, l, TRUE)
1592 + wp->w_topfill;
1593 else
1594#endif
1595 new_rows += plines_win(wp, l, TRUE);
1596 ++j;
1597 if (new_rows > wp->w_height - row - 2)
1598 {
1599 /* it's getting too much, must redraw the rest */
1600 new_rows = 9999;
1601 break;
1602 }
1603 }
1604 xtra_rows = new_rows - old_rows;
1605 if (xtra_rows < 0)
1606 {
1607 /* May scroll text up. If there is not enough
1608 * remaining text or scrolling fails, must redraw the
1609 * rest. If scrolling works, must redraw the text
1610 * below the scrolled text. */
1611 if (row - xtra_rows >= wp->w_height - 2)
1612 mod_bot = MAXLNUM;
1613 else
1614 {
1615 check_for_delay(FALSE);
1616 if (win_del_lines(wp, row,
1617 -xtra_rows, FALSE, FALSE) == FAIL)
1618 mod_bot = MAXLNUM;
1619 else
1620 bot_start = wp->w_height + xtra_rows;
1621 }
1622 }
1623 else if (xtra_rows > 0)
1624 {
1625 /* May scroll text down. If there is not enough
1626 * remaining text of scrolling fails, must redraw the
1627 * rest. */
1628 if (row + xtra_rows >= wp->w_height - 2)
1629 mod_bot = MAXLNUM;
1630 else
1631 {
1632 check_for_delay(FALSE);
1633 if (win_ins_lines(wp, row + old_rows,
1634 xtra_rows, FALSE, FALSE) == FAIL)
1635 mod_bot = MAXLNUM;
1636 else if (top_end > row + old_rows)
1637 /* Scrolled the part at the top that requires
1638 * updating down. */
1639 top_end += xtra_rows;
1640 }
1641 }
1642
1643 /* When not updating the rest, may need to move w_lines[]
1644 * entries. */
1645 if (mod_bot != MAXLNUM && i != j)
1646 {
1647 if (j < i)
1648 {
1649 int x = row + new_rows;
1650
1651 /* move entries in w_lines[] upwards */
1652 for (;;)
1653 {
1654 /* stop at last valid entry in w_lines[] */
1655 if (i >= wp->w_lines_valid)
1656 {
1657 wp->w_lines_valid = j;
1658 break;
1659 }
1660 wp->w_lines[j] = wp->w_lines[i];
1661 /* stop at a line that won't fit */
1662 if (x + (int)wp->w_lines[j].wl_size
1663 > wp->w_height)
1664 {
1665 wp->w_lines_valid = j + 1;
1666 break;
1667 }
1668 x += wp->w_lines[j++].wl_size;
1669 ++i;
1670 }
1671 if (bot_start > x)
1672 bot_start = x;
1673 }
1674 else /* j > i */
1675 {
1676 /* move entries in w_lines[] downwards */
1677 j -= i;
1678 wp->w_lines_valid += j;
1679 if (wp->w_lines_valid > wp->w_height)
1680 wp->w_lines_valid = wp->w_height;
1681 for (i = wp->w_lines_valid; i - j >= idx; --i)
1682 wp->w_lines[i] = wp->w_lines[i - j];
1683
1684 /* The w_lines[] entries for inserted lines are
1685 * now invalid, but wl_size may be used above.
1686 * Reset to zero. */
1687 while (i >= idx)
1688 {
1689 wp->w_lines[i].wl_size = 0;
1690 wp->w_lines[i--].wl_valid = FALSE;
1691 }
1692 }
1693 }
1694 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 }
1696
1697#ifdef FEAT_FOLDING
1698 /*
1699 * When lines are folded, display one line for all of them.
1700 * Otherwise, display normally (can be several display lines when
1701 * 'wrap' is on).
1702 */
1703 fold_count = foldedCount(wp, lnum, &win_foldinfo);
1704 if (fold_count != 0)
1705 {
1706 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
1707 ++row;
1708 --fold_count;
1709 wp->w_lines[idx].wl_folded = TRUE;
1710 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
1711# ifdef FEAT_SYN_HL
1712 did_update = DID_FOLD;
1713# endif
1714 }
1715 else
1716#endif
1717 if (idx < wp->w_lines_valid
1718 && wp->w_lines[idx].wl_valid
1719 && wp->w_lines[idx].wl_lnum == lnum
1720 && lnum > wp->w_topline
1721 && !(dy_flags & DY_LASTLINE)
1722 && srow + wp->w_lines[idx].wl_size > wp->w_height
1723#ifdef FEAT_DIFF
1724 && diff_check_fill(wp, lnum) == 0
1725#endif
1726 )
1727 {
1728 /* This line is not going to fit. Don't draw anything here,
1729 * will draw "@ " lines below. */
1730 row = wp->w_height + 1;
1731 }
1732 else
1733 {
1734#ifdef FEAT_SEARCH_EXTRA
1735 prepare_search_hl(wp, lnum);
1736#endif
1737#ifdef FEAT_SYN_HL
1738 /* Let the syntax stuff know we skipped a few lines. */
1739 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
1740 && syntax_present(buf))
1741 syntax_end_parsing(syntax_last_parsed + 1);
1742#endif
1743
1744 /*
1745 * Display one line.
1746 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00001747 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748
1749#ifdef FEAT_FOLDING
1750 wp->w_lines[idx].wl_folded = FALSE;
1751 wp->w_lines[idx].wl_lastlnum = lnum;
1752#endif
1753#ifdef FEAT_SYN_HL
1754 did_update = DID_LINE;
1755 syntax_last_parsed = lnum;
1756#endif
1757 }
1758
1759 wp->w_lines[idx].wl_lnum = lnum;
1760 wp->w_lines[idx].wl_valid = TRUE;
1761 if (row > wp->w_height) /* past end of screen */
1762 {
1763 /* we may need the size of that too long line later on */
1764 if (dollar_vcol == 0)
1765 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
1766 ++idx;
1767 break;
1768 }
1769 if (dollar_vcol == 0)
1770 wp->w_lines[idx].wl_size = row - srow;
1771 ++idx;
1772#ifdef FEAT_FOLDING
1773 lnum += fold_count + 1;
1774#else
1775 ++lnum;
1776#endif
1777 }
1778 else
1779 {
1780 /* This line does not need updating, advance to the next one */
1781 row += wp->w_lines[idx++].wl_size;
1782 if (row > wp->w_height) /* past end of screen */
1783 break;
1784#ifdef FEAT_FOLDING
1785 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
1786#else
1787 ++lnum;
1788#endif
1789#ifdef FEAT_SYN_HL
1790 did_update = DID_NONE;
1791#endif
1792 }
1793
1794 if (lnum > buf->b_ml.ml_line_count)
1795 {
1796 eof = TRUE;
1797 break;
1798 }
1799 }
1800 /*
1801 * End of loop over all window lines.
1802 */
1803
1804
1805 if (idx > wp->w_lines_valid)
1806 wp->w_lines_valid = idx;
1807
1808#ifdef FEAT_SYN_HL
1809 /*
1810 * Let the syntax stuff know we stop parsing here.
1811 */
1812 if (syntax_last_parsed != 0 && syntax_present(buf))
1813 syntax_end_parsing(syntax_last_parsed + 1);
1814#endif
1815
1816 /*
1817 * If we didn't hit the end of the file, and we didn't finish the last
1818 * line we were working on, then the line didn't fit.
1819 */
1820 wp->w_empty_rows = 0;
1821#ifdef FEAT_DIFF
1822 wp->w_filler_rows = 0;
1823#endif
1824 if (!eof && !didline)
1825 {
1826 if (lnum == wp->w_topline)
1827 {
1828 /*
1829 * Single line that does not fit!
1830 * Don't overwrite it, it can be edited.
1831 */
1832 wp->w_botline = lnum + 1;
1833 }
1834#ifdef FEAT_DIFF
1835 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
1836 {
1837 /* Window ends in filler lines. */
1838 wp->w_botline = lnum;
1839 wp->w_filler_rows = wp->w_height - srow;
1840 }
1841#endif
1842 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
1843 {
1844 /*
1845 * Last line isn't finished: Display "@@@" at the end.
1846 */
1847 screen_fill(W_WINROW(wp) + wp->w_height - 1,
1848 W_WINROW(wp) + wp->w_height,
1849 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
1850 '@', '@', hl_attr(HLF_AT));
1851 set_empty_rows(wp, srow);
1852 wp->w_botline = lnum;
1853 }
1854 else
1855 {
1856 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
1857 wp->w_botline = lnum;
1858 }
1859 }
1860 else
1861 {
1862#ifdef FEAT_VERTSPLIT
1863 draw_vsep_win(wp, row);
1864#endif
1865 if (eof) /* we hit the end of the file */
1866 {
1867 wp->w_botline = buf->b_ml.ml_line_count + 1;
1868#ifdef FEAT_DIFF
1869 j = diff_check_fill(wp, wp->w_botline);
1870 if (j > 0 && !wp->w_botfill)
1871 {
1872 /*
1873 * Display filler lines at the end of the file
1874 */
1875 if (char2cells(fill_diff) > 1)
1876 i = '-';
1877 else
1878 i = fill_diff;
1879 if (row + j > wp->w_height)
1880 j = wp->w_height - row;
1881 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
1882 row += j;
1883 }
1884#endif
1885 }
1886 else if (dollar_vcol == 0)
1887 wp->w_botline = lnum;
1888
1889 /* make sure the rest of the screen is blank */
1890 /* put '~'s on rows that aren't part of the file. */
1891 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_AT);
1892 }
1893
1894 /* Reset the type of redrawing required, the window has been updated. */
1895 wp->w_redr_type = 0;
1896#ifdef FEAT_DIFF
1897 wp->w_old_topfill = wp->w_topfill;
1898 wp->w_old_botfill = wp->w_botfill;
1899#endif
1900
1901 if (dollar_vcol == 0)
1902 {
1903 /*
1904 * There is a trick with w_botline. If we invalidate it on each
1905 * change that might modify it, this will cause a lot of expensive
1906 * calls to plines() in update_topline() each time. Therefore the
1907 * value of w_botline is often approximated, and this value is used to
1908 * compute the value of w_topline. If the value of w_botline was
1909 * wrong, check that the value of w_topline is correct (cursor is on
1910 * the visible part of the text). If it's not, we need to redraw
1911 * again. Mostly this just means scrolling up a few lines, so it
1912 * doesn't look too bad. Only do this for the current window (where
1913 * changes are relevant).
1914 */
1915 wp->w_valid |= VALID_BOTLINE;
1916 if (wp == curwin && wp->w_botline != old_botline && !recursive)
1917 {
1918 recursive = TRUE;
1919 curwin->w_valid &= ~VALID_TOPLINE;
1920 update_topline(); /* may invalidate w_botline again */
1921 if (must_redraw != 0)
1922 {
1923 /* Don't update for changes in buffer again. */
1924 i = curbuf->b_mod_set;
1925 curbuf->b_mod_set = FALSE;
1926 win_update(curwin);
1927 must_redraw = 0;
1928 curbuf->b_mod_set = i;
1929 }
1930 recursive = FALSE;
1931 }
1932 }
1933
1934#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1935 /* restore got_int, unless CTRL-C was hit while redrawing */
1936 if (!got_int)
1937 got_int = save_got_int;
1938#endif
1939}
1940
1941#ifdef FEAT_SIGNS
1942static int draw_signcolumn __ARGS((win_T *wp));
1943
1944/*
1945 * Return TRUE when window "wp" has a column to draw signs in.
1946 */
1947 static int
1948draw_signcolumn(wp)
1949 win_T *wp;
1950{
1951 return (wp->w_buffer->b_signlist != NULL
1952# ifdef FEAT_NETBEANS_INTG
1953 || usingNetbeans
1954# endif
1955 );
1956}
1957#endif
1958
1959/*
1960 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
1961 * as the filler character.
1962 */
1963 static void
1964win_draw_end(wp, c1, c2, row, endrow, hl)
1965 win_T *wp;
1966 int c1;
1967 int c2;
1968 int row;
1969 int endrow;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001970 hlf_T hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971{
1972#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
1973 int n = 0;
1974# define FDC_OFF n
1975#else
1976# define FDC_OFF 0
1977#endif
1978
1979#ifdef FEAT_RIGHTLEFT
1980 if (wp->w_p_rl)
1981 {
1982 /* No check for cmdline window: should never be right-left. */
1983# ifdef FEAT_FOLDING
1984 n = wp->w_p_fdc;
1985
1986 if (n > 0)
1987 {
1988 /* draw the fold column at the right */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00001989 if (n > W_WIDTH(wp))
1990 n = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
1992 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
1993 ' ', ' ', hl_attr(HLF_FC));
1994 }
1995# endif
1996# ifdef FEAT_SIGNS
1997 if (draw_signcolumn(wp))
1998 {
1999 int nn = n + 2;
2000
2001 /* draw the sign column left of the fold column */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002002 if (nn > W_WIDTH(wp))
2003 nn = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2005 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
2006 ' ', ' ', hl_attr(HLF_SC));
2007 n = nn;
2008 }
2009# endif
2010 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2011 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
2012 c2, c2, hl_attr(hl));
2013 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2014 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
2015 c1, c2, hl_attr(hl));
2016 }
2017 else
2018#endif
2019 {
2020#ifdef FEAT_CMDWIN
2021 if (cmdwin_type != 0 && wp == curwin)
2022 {
2023 /* draw the cmdline character in the leftmost column */
2024 n = 1;
2025 if (n > wp->w_width)
2026 n = wp->w_width;
2027 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2028 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
2029 cmdwin_type, ' ', hl_attr(HLF_AT));
2030 }
2031#endif
2032#ifdef FEAT_FOLDING
2033 if (wp->w_p_fdc > 0)
2034 {
2035 int nn = n + wp->w_p_fdc;
2036
2037 /* draw the fold column at the left */
2038 if (nn > W_WIDTH(wp))
2039 nn = W_WIDTH(wp);
2040 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2041 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2042 ' ', ' ', hl_attr(HLF_FC));
2043 n = nn;
2044 }
2045#endif
2046#ifdef FEAT_SIGNS
2047 if (draw_signcolumn(wp))
2048 {
2049 int nn = n + 2;
2050
2051 /* draw the sign column after the fold column */
2052 if (nn > W_WIDTH(wp))
2053 nn = W_WIDTH(wp);
2054 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2055 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2056 ' ', ' ', hl_attr(HLF_SC));
2057 n = nn;
2058 }
2059#endif
2060 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2061 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
2062 c1, c2, hl_attr(hl));
2063 }
2064 set_empty_rows(wp, row);
2065}
2066
2067#ifdef FEAT_FOLDING
2068/*
2069 * Display one folded line.
2070 */
2071 static void
2072fold_line(wp, fold_count, foldinfo, lnum, row)
2073 win_T *wp;
2074 long fold_count;
2075 foldinfo_T *foldinfo;
2076 linenr_T lnum;
2077 int row;
2078{
2079 char_u buf[51];
2080 pos_T *top, *bot;
2081 linenr_T lnume = lnum + fold_count - 1;
2082 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002083 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 int col;
2086 int txtcol;
2087 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002088 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089
2090 /* Build the fold line:
2091 * 1. Add the cmdwin_type for the command-line window
2092 * 2. Add the 'foldcolumn'
2093 * 3. Add the 'number' column
2094 * 4. Compose the text
2095 * 5. Add the text
2096 * 6. set highlighting for the Visual area an other text
2097 */
2098 col = 0;
2099
2100 /*
2101 * 1. Add the cmdwin_type for the command-line window
2102 * Ignores 'rightleft', this window is never right-left.
2103 */
2104#ifdef FEAT_CMDWIN
2105 if (cmdwin_type != 0 && wp == curwin)
2106 {
2107 ScreenLines[off] = cmdwin_type;
2108 ScreenAttrs[off] = hl_attr(HLF_AT);
2109#ifdef FEAT_MBYTE
2110 if (enc_utf8)
2111 ScreenLinesUC[off] = 0;
2112#endif
2113 ++col;
2114 }
2115#endif
2116
2117 /*
2118 * 2. Add the 'foldcolumn'
2119 */
2120 fdc = wp->w_p_fdc;
2121 if (fdc > W_WIDTH(wp) - col)
2122 fdc = W_WIDTH(wp) - col;
2123 if (fdc > 0)
2124 {
2125 fill_foldcolumn(buf, wp, TRUE, lnum);
2126#ifdef FEAT_RIGHTLEFT
2127 if (wp->w_p_rl)
2128 {
2129 int i;
2130
2131 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
2132 hl_attr(HLF_FC));
2133 /* reverse the fold column */
2134 for (i = 0; i < fdc; ++i)
2135 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2136 }
2137 else
2138#endif
2139 copy_text_attr(off + col, buf, fdc, hl_attr(HLF_FC));
2140 col += fdc;
2141 }
2142
2143#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002144# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2145 for (ri = 0; ri < l; ++ri) \
2146 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2147 else \
2148 for (ri = 0; ri < l; ++ri) \
2149 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002151# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2152 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153#endif
2154
2155 /* Set all attributes of the 'number' column and the text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002156 RL_MEMSET(col, hl_attr(HLF_FL), W_WIDTH(wp) - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157
2158#ifdef FEAT_SIGNS
2159 /* If signs are being displayed, add two spaces. */
2160 if (draw_signcolumn(wp))
2161 {
2162 len = W_WIDTH(wp) - col;
2163 if (len > 0)
2164 {
2165 if (len > 2)
2166 len = 2;
2167# ifdef FEAT_RIGHTLEFT
2168 if (wp->w_p_rl)
2169 /* the line number isn't reversed */
2170 copy_text_attr(off + W_WIDTH(wp) - len - col,
2171 (char_u *)" ", len, hl_attr(HLF_FL));
2172 else
2173# endif
2174 copy_text_attr(off + col, (char_u *)" ", len, hl_attr(HLF_FL));
2175 col += len;
2176 }
2177 }
2178#endif
2179
2180 /*
2181 * 3. Add the 'number' column
2182 */
2183 if (wp->w_p_nu)
2184 {
2185 len = W_WIDTH(wp) - col;
2186 if (len > 0)
2187 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002188 int w = number_width(wp);
2189
2190 if (len > w + 1)
2191 len = w + 1;
2192 sprintf((char *)buf, "%*ld ", w, (long)lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193#ifdef FEAT_RIGHTLEFT
2194 if (wp->w_p_rl)
2195 /* the line number isn't reversed */
2196 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
2197 hl_attr(HLF_FL));
2198 else
2199#endif
2200 copy_text_attr(off + col, buf, len, hl_attr(HLF_FL));
2201 col += len;
2202 }
2203 }
2204
2205 /*
2206 * 4. Compose the folded-line string with 'foldtext', if set.
2207 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002208 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209
2210 txtcol = col; /* remember where text starts */
2211
2212 /*
2213 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2214 * Right-left text is put in columns 0 - number-col, normal text is put
2215 * in columns number-col - window-width.
2216 */
2217#ifdef FEAT_MBYTE
2218 if (has_mbyte)
2219 {
2220 int cells;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002221 int u8c, u8cc[MAX_MCO];
2222 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 int idx;
2224 int c_len;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002225 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226# ifdef FEAT_ARABIC
2227 int prev_c = 0; /* previous Arabic character */
2228 int prev_c1 = 0; /* first composing char for prev_c */
2229# endif
2230
2231# ifdef FEAT_RIGHTLEFT
2232 if (wp->w_p_rl)
2233 idx = off;
2234 else
2235# endif
2236 idx = off + col;
2237
2238 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2239 for (p = text; *p != NUL; )
2240 {
2241 cells = (*mb_ptr2cells)(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002242 c_len = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 if (col + cells > W_WIDTH(wp)
2244# ifdef FEAT_RIGHTLEFT
2245 - (wp->w_p_rl ? col : 0)
2246# endif
2247 )
2248 break;
2249 ScreenLines[idx] = *p;
2250 if (enc_utf8)
2251 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002252 u8c = utfc_ptr2char(p, u8cc);
2253 if (*p < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 {
2255 ScreenLinesUC[idx] = 0;
2256#ifdef FEAT_ARABIC
2257 prev_c = u8c;
2258#endif
2259 }
2260 else
2261 {
2262#ifdef FEAT_ARABIC
2263 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2264 {
2265 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002266 int pc, pc1, nc;
2267 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 int firstbyte = *p;
2269
2270 /* The idea of what is the previous and next
2271 * character depends on 'rightleft'. */
2272 if (wp->w_p_rl)
2273 {
2274 pc = prev_c;
2275 pc1 = prev_c1;
2276 nc = utf_ptr2char(p + c_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002277 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 }
2279 else
2280 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002281 pc = utfc_ptr2char(p + c_len, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002283 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 }
2285 prev_c = u8c;
2286
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002287 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 pc, pc1, nc);
2289 ScreenLines[idx] = firstbyte;
2290 }
2291 else
2292 prev_c = u8c;
2293#endif
2294 /* Non-BMP character: display as ? or fullwidth ?. */
2295 if (u8c >= 0x10000)
2296 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2297 else
2298 ScreenLinesUC[idx] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002299 for (i = 0; i < Screen_mco; ++i)
2300 {
2301 ScreenLinesC[i][idx] = u8cc[i];
2302 if (u8cc[i] == 0)
2303 break;
2304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 }
2306 if (cells > 1)
2307 ScreenLines[idx + 1] = 0;
2308 }
2309 else if (cells > 1) /* double-byte character */
2310 {
2311 if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2312 ScreenLines2[idx] = p[1];
2313 else
2314 ScreenLines[idx + 1] = p[1];
2315 }
2316 col += cells;
2317 idx += cells;
2318 p += c_len;
2319 }
2320 }
2321 else
2322#endif
2323 {
2324 len = (int)STRLEN(text);
2325 if (len > W_WIDTH(wp) - col)
2326 len = W_WIDTH(wp) - col;
2327 if (len > 0)
2328 {
2329#ifdef FEAT_RIGHTLEFT
2330 if (wp->w_p_rl)
2331 STRNCPY(current_ScreenLine, text, len);
2332 else
2333#endif
2334 STRNCPY(current_ScreenLine + col, text, len);
2335 col += len;
2336 }
2337 }
2338
2339 /* Fill the rest of the line with the fold filler */
2340#ifdef FEAT_RIGHTLEFT
2341 if (wp->w_p_rl)
2342 col -= txtcol;
2343#endif
2344 while (col < W_WIDTH(wp)
2345#ifdef FEAT_RIGHTLEFT
2346 - (wp->w_p_rl ? txtcol : 0)
2347#endif
2348 )
2349 {
2350#ifdef FEAT_MBYTE
2351 if (enc_utf8)
2352 {
2353 if (fill_fold >= 0x80)
2354 {
2355 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002356 ScreenLinesC[0][off + col] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 }
2358 else
2359 ScreenLinesUC[off + col] = 0;
2360 }
2361#endif
2362 ScreenLines[off + col++] = fill_fold;
2363 }
2364
2365 if (text != buf)
2366 vim_free(text);
2367
2368 /*
2369 * 6. set highlighting for the Visual area an other text.
2370 * If all folded lines are in the Visual area, highlight the line.
2371 */
2372#ifdef FEAT_VISUAL
2373 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2374 {
2375 if (ltoreq(curwin->w_cursor, VIsual))
2376 {
2377 /* Visual is after curwin->w_cursor */
2378 top = &curwin->w_cursor;
2379 bot = &VIsual;
2380 }
2381 else
2382 {
2383 /* Visual is before curwin->w_cursor */
2384 top = &VIsual;
2385 bot = &curwin->w_cursor;
2386 }
2387 if (lnum >= top->lnum
2388 && lnume <= bot->lnum
2389 && (VIsual_mode != 'v'
2390 || ((lnum > top->lnum
2391 || (lnum == top->lnum
2392 && top->col == 0))
2393 && (lnume < bot->lnum
2394 || (lnume == bot->lnum
2395 && (bot->col - (*p_sel == 'e'))
2396 >= STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
2397 {
2398 if (VIsual_mode == Ctrl_V)
2399 {
2400 /* Visual block mode: highlight the chars part of the block */
2401 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2402 {
2403 if (wp->w_old_cursor_lcol + txtcol < (colnr_T)W_WIDTH(wp))
2404 len = wp->w_old_cursor_lcol;
2405 else
2406 len = W_WIDTH(wp) - txtcol;
2407 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, hl_attr(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002408 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 }
2410 }
2411 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002412 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 /* Set all attributes of the text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002414 RL_MEMSET(txtcol, hl_attr(HLF_V), W_WIDTH(wp) - txtcol);
2415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 }
2417 }
2418#endif
2419
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002420#ifdef FEAT_SYN_HL
2421 /* Show 'cursorcolumn' in the fold line. */
2422 if (wp->w_p_cuc && (int)wp->w_virtcol + txtcol < W_WIDTH(wp))
2423 ScreenAttrs[off + wp->w_virtcol + txtcol] = hl_combine_attr(
2424 ScreenAttrs[off + wp->w_virtcol + txtcol], hl_attr(HLF_CUC));
2425#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426
2427 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2428 (int)W_WIDTH(wp), FALSE);
2429
2430 /*
2431 * Update w_cline_height and w_cline_folded if the cursor line was
2432 * updated (saves a call to plines() later).
2433 */
2434 if (wp == curwin
2435 && lnum <= curwin->w_cursor.lnum
2436 && lnume >= curwin->w_cursor.lnum)
2437 {
2438 curwin->w_cline_row = row;
2439 curwin->w_cline_height = 1;
2440 curwin->w_cline_folded = TRUE;
2441 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2442 }
2443}
2444
2445/*
2446 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2447 */
2448 static void
2449copy_text_attr(off, buf, len, attr)
2450 int off;
2451 char_u *buf;
2452 int len;
2453 int attr;
2454{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002455 int i;
2456
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 mch_memmove(ScreenLines + off, buf, (size_t)len);
2458# ifdef FEAT_MBYTE
2459 if (enc_utf8)
2460 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2461# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002462 for (i = 0; i < len; ++i)
2463 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464}
2465
2466/*
2467 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002468 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 */
2470 static void
2471fill_foldcolumn(p, wp, closed, lnum)
2472 char_u *p;
2473 win_T *wp;
2474 int closed; /* TRUE of FALSE */
2475 linenr_T lnum; /* current line number */
2476{
2477 int i = 0;
2478 int level;
2479 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002480 int empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481
2482 /* Init to all spaces. */
2483 copy_spaces(p, (size_t)wp->w_p_fdc);
2484
2485 level = win_foldinfo.fi_level;
2486 if (level > 0)
2487 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002488 /* If there is only one column put more info in it. */
2489 empty = (wp->w_p_fdc == 1) ? 0 : 1;
2490
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 /* If the column is too narrow, we start at the lowest level that
2492 * fits and use numbers to indicated the depth. */
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002493 first_level = level - wp->w_p_fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 if (first_level < 1)
2495 first_level = 1;
2496
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002497 for (i = 0; i + empty < wp->w_p_fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 {
2499 if (win_foldinfo.fi_lnum == lnum
2500 && first_level + i >= win_foldinfo.fi_low_level)
2501 p[i] = '-';
2502 else if (first_level == 1)
2503 p[i] = '|';
2504 else if (first_level + i <= 9)
2505 p[i] = '0' + first_level + i;
2506 else
2507 p[i] = '>';
2508 if (first_level + i == level)
2509 break;
2510 }
2511 }
2512 if (closed)
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002513 p[i >= wp->w_p_fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514}
2515#endif /* FEAT_FOLDING */
2516
2517/*
2518 * Display line "lnum" of window 'wp' on the screen.
2519 * Start at row "startrow", stop when "endrow" is reached.
2520 * wp->w_virtcol needs to be valid.
2521 *
2522 * Return the number of last row the line occupies.
2523 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002524/* ARGSUSED */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +00002526win_line(wp, lnum, startrow, endrow, nochange)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 win_T *wp;
2528 linenr_T lnum;
2529 int startrow;
2530 int endrow;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002531 int nochange; /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532{
2533 int col; /* visual column on screen */
2534 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2535 int c = 0; /* init for GCC */
2536 long vcol = 0; /* virtual column (for tabs) */
2537 long vcol_prev = -1; /* "vcol" of previous character */
2538 char_u *line; /* current line */
2539 char_u *ptr; /* current position in "line" */
2540 int row; /* row in the window, excl w_winrow */
2541 int screen_row; /* row on the screen, incl w_winrow */
2542
2543 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2544 int n_extra = 0; /* number of extra chars */
2545 char_u *p_extra = NULL; /* string of extra chars */
2546 int c_extra = NUL; /* extra chars, all the same */
2547 int extra_attr = 0; /* attributes when n_extra != 0 */
2548 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2549 displaying lcs_eol at end-of-line */
2550 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2551 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2552
2553 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2554 int saved_n_extra = 0;
2555 char_u *saved_p_extra = NULL;
2556 int saved_c_extra = 0;
2557 int saved_char_attr = 0;
2558
2559 int n_attr = 0; /* chars with special attr */
2560 int saved_attr2 = 0; /* char_attr saved for n_attr */
2561 int n_attr3 = 0; /* chars with overruling special attr */
2562 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2563
2564 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2565
2566 int fromcol, tocol; /* start/end of inverting */
2567 int fromcol_prev = -2; /* start of inverting after cursor */
2568 int noinvcur = FALSE; /* don't invert the cursor */
2569#ifdef FEAT_VISUAL
2570 pos_T *top, *bot;
2571#endif
2572 pos_T pos;
2573 long v;
2574
2575 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002576 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2578 in this line */
2579 int attr = 0; /* attributes for area highlighting */
2580 int area_attr = 0; /* attributes desired by highlighting */
2581 int search_attr = 0; /* attributes desired by 'hlsearch' */
2582#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002583 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 int syntax_attr = 0; /* attributes desired by syntax */
2585 int has_syntax = FALSE; /* this buffer has syntax highl. */
2586 int save_did_emsg;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002587#endif
2588#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002589 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002590# define SPWORDLEN 150
2591 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00002592 int nextlinecol = 0; /* column where nextline[] starts */
2593 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00002594 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00002595 int spell_attr = 0; /* attributes desired by spelling */
2596 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00002597 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2598 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00002599 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002600 static int cap_col = -1; /* column to check for Cap word */
2601 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002602 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603#endif
2604 int extra_check; /* has syntax or linebreak */
2605#ifdef FEAT_MBYTE
2606 int multi_attr = 0; /* attributes desired by multibyte */
2607 int mb_l = 1; /* multi-byte byte length */
2608 int mb_c = 0; /* decoded multi-byte character */
2609 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002610 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611#endif
2612#ifdef FEAT_DIFF
2613 int filler_lines; /* nr of filler lines to be drawn */
2614 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002615 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 int change_start = MAXCOL; /* first col of changed area */
2617 int change_end = -1; /* last col of changed area */
2618#endif
2619 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
2620#ifdef FEAT_LINEBREAK
2621 int need_showbreak = FALSE;
2622#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002623#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
2624 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625# define LINE_ATTR
2626 int line_attr = 0; /* atrribute for the whole line */
2627#endif
2628#ifdef FEAT_SEARCH_EXTRA
2629 match_T *shl; /* points to search_hl or match_hl */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002630#endif
2631#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_MBYTE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00002632 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633#endif
2634#ifdef FEAT_ARABIC
2635 int prev_c = 0; /* previous Arabic character */
2636 int prev_c1 = 0; /* first composing char for prev_c */
2637#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002638#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00002639 int did_line_attr = 0;
2640#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641
2642 /* draw_state: items that are drawn in sequence: */
2643#define WL_START 0 /* nothing done yet */
2644#ifdef FEAT_CMDWIN
2645# define WL_CMDLINE WL_START + 1 /* cmdline window column */
2646#else
2647# define WL_CMDLINE WL_START
2648#endif
2649#ifdef FEAT_FOLDING
2650# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
2651#else
2652# define WL_FOLD WL_CMDLINE
2653#endif
2654#ifdef FEAT_SIGNS
2655# define WL_SIGN WL_FOLD + 1 /* column for signs */
2656#else
2657# define WL_SIGN WL_FOLD /* column for signs */
2658#endif
2659#define WL_NR WL_SIGN + 1 /* line number */
2660#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
2661# define WL_SBR WL_NR + 1 /* 'showbreak' or 'diff' */
2662#else
2663# define WL_SBR WL_NR
2664#endif
2665#define WL_LINE WL_SBR + 1 /* text in the line */
2666 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00002667#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 int feedback_col = 0;
2669 int feedback_old_attr = -1;
2670#endif
2671
2672
2673 if (startrow > endrow) /* past the end already! */
2674 return startrow;
2675
2676 row = startrow;
2677 screen_row = row + W_WINROW(wp);
2678
2679 /*
2680 * To speed up the loop below, set extra_check when there is linebreak,
2681 * trailing white space and/or syntax processing to be done.
2682 */
2683#ifdef FEAT_LINEBREAK
2684 extra_check = wp->w_p_lbr;
2685#else
2686 extra_check = 0;
2687#endif
2688#ifdef FEAT_SYN_HL
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00002689 if (syntax_present(wp->w_buffer) && !wp->w_buffer->b_syn_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 {
2691 /* Prepare for syntax highlighting in this line. When there is an
2692 * error, stop syntax highlighting. */
2693 save_did_emsg = did_emsg;
2694 did_emsg = FALSE;
2695 syntax_start(wp, lnum);
2696 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00002697 wp->w_buffer->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 else
2699 {
2700 did_emsg = save_did_emsg;
2701 has_syntax = TRUE;
2702 extra_check = TRUE;
2703 }
2704 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002705#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00002706
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002707#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00002708 if (wp->w_p_spell
2709 && *wp->w_buffer->b_p_spl != NUL
2710 && wp->w_buffer->b_langp.ga_len > 0
2711 && *(char **)(wp->w_buffer->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00002712 {
2713 /* Prepare for spell checking. */
2714 has_spell = TRUE;
2715 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00002716
2717 /* Get the start of the next line, so that words that wrap to the next
2718 * line are found too: "et<line-break>al.".
2719 * Trick: skip a few chars for C/shell/Vim comments */
2720 nextline[SPWORDLEN] = NUL;
2721 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2722 {
2723 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
2724 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
2725 }
2726
2727 /* When a word wrapped from the previous line the start of the current
2728 * line is valid. */
2729 if (lnum == checked_lnum)
2730 cur_checked_col = checked_col;
2731 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002732
2733 /* When there was a sentence end in the previous line may require a
2734 * word starting with capital in this line. In line 1 always check
2735 * the first word. */
2736 if (lnum != capcol_lnum)
2737 cap_col = -1;
2738 if (lnum == 1)
2739 cap_col = 0;
2740 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00002741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742#endif
2743
2744 /*
2745 * handle visual active in this window
2746 */
2747 fromcol = -10;
2748 tocol = MAXCOL;
2749#ifdef FEAT_VISUAL
2750 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2751 {
2752 /* Visual is after curwin->w_cursor */
2753 if (ltoreq(curwin->w_cursor, VIsual))
2754 {
2755 top = &curwin->w_cursor;
2756 bot = &VIsual;
2757 }
2758 else /* Visual is before curwin->w_cursor */
2759 {
2760 top = &VIsual;
2761 bot = &curwin->w_cursor;
2762 }
2763 if (VIsual_mode == Ctrl_V) /* block mode */
2764 {
2765 if (lnum >= top->lnum && lnum <= bot->lnum)
2766 {
2767 fromcol = wp->w_old_cursor_fcol;
2768 tocol = wp->w_old_cursor_lcol;
2769 }
2770 }
2771 else /* non-block mode */
2772 {
2773 if (lnum > top->lnum && lnum <= bot->lnum)
2774 fromcol = 0;
2775 else if (lnum == top->lnum)
2776 {
2777 if (VIsual_mode == 'V') /* linewise */
2778 fromcol = 0;
2779 else
2780 {
2781 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
2782 if (gchar_pos(top) == NUL)
2783 tocol = fromcol + 1;
2784 }
2785 }
2786 if (VIsual_mode != 'V' && lnum == bot->lnum)
2787 {
2788 if (*p_sel == 'e' && bot->col == 0
2789#ifdef FEAT_VIRTUALEDIT
2790 && bot->coladd == 0
2791#endif
2792 )
2793 {
2794 fromcol = -10;
2795 tocol = MAXCOL;
2796 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002797 else if (bot->col == MAXCOL)
2798 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 else
2800 {
2801 pos = *bot;
2802 if (*p_sel == 'e')
2803 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
2804 else
2805 {
2806 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
2807 ++tocol;
2808 }
2809 }
2810 }
2811 }
2812
2813#ifndef MSDOS
2814 /* Check if the character under the cursor should not be inverted */
2815 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
2816# ifdef FEAT_GUI
2817 && !gui.in_use
2818# endif
2819 )
2820 noinvcur = TRUE;
2821#endif
2822
2823 /* if inverting in this line set area_highlighting */
2824 if (fromcol >= 0)
2825 {
2826 area_highlighting = TRUE;
2827 attr = hl_attr(HLF_V);
2828#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
2829 if (clip_star.available && !clip_star.owned && clip_isautosel())
2830 attr = hl_attr(HLF_VNC);
2831#endif
2832 }
2833 }
2834
2835 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002836 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 */
2838 else
2839#endif /* FEAT_VISUAL */
2840 if (highlight_match
2841 && wp == curwin
2842 && lnum >= curwin->w_cursor.lnum
2843 && lnum <= curwin->w_cursor.lnum + search_match_lines)
2844 {
2845 if (lnum == curwin->w_cursor.lnum)
2846 getvcol(curwin, &(curwin->w_cursor),
2847 (colnr_T *)&fromcol, NULL, NULL);
2848 else
2849 fromcol = 0;
2850 if (lnum == curwin->w_cursor.lnum + search_match_lines)
2851 {
2852 pos.lnum = lnum;
2853 pos.col = search_match_endcol;
2854 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
2855 }
2856 else
2857 tocol = MAXCOL;
2858 if (fromcol == tocol) /* do at least one character */
2859 tocol = fromcol + 1; /* happens when past end of line */
2860 area_highlighting = TRUE;
2861 attr = hl_attr(HLF_I);
2862 }
2863
2864#ifdef FEAT_DIFF
2865 filler_lines = diff_check(wp, lnum);
2866 if (filler_lines < 0)
2867 {
2868 if (filler_lines == -1)
2869 {
2870 if (diff_find_change(wp, lnum, &change_start, &change_end))
2871 diff_hlf = HLF_ADD; /* added line */
2872 else if (change_start == 0)
2873 diff_hlf = HLF_TXD; /* changed text */
2874 else
2875 diff_hlf = HLF_CHD; /* changed line */
2876 }
2877 else
2878 diff_hlf = HLF_ADD; /* added line */
2879 filler_lines = 0;
2880 area_highlighting = TRUE;
2881 }
2882 if (lnum == wp->w_topline)
2883 filler_lines = wp->w_topfill;
2884 filler_todo = filler_lines;
2885#endif
2886
2887#ifdef LINE_ATTR
2888# ifdef FEAT_SIGNS
2889 /* If this line has a sign with line highlighting set line_attr. */
2890 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
2891 if (v != 0)
2892 line_attr = sign_get_attr((int)v, TRUE);
2893# endif
2894# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
2895 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002896 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 line_attr = hl_attr(HLF_L);
2898# endif
2899 if (line_attr != 0)
2900 area_highlighting = TRUE;
2901#endif
2902
2903 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
2904 ptr = line;
2905
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002906#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00002907 if (has_spell)
2908 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002909 /* For checking first word with a capital skip white space. */
2910 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002911 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002912
Bram Moolenaar30abd282005-06-22 22:35:10 +00002913 /* To be able to spell-check over line boundaries copy the end of the
2914 * current line into nextline[]. Above the start of the next line was
2915 * copied to nextline[SPWORDLEN]. */
2916 if (nextline[SPWORDLEN] == NUL)
2917 {
2918 /* No next line or it is empty. */
2919 nextlinecol = MAXCOL;
2920 nextline_idx = 0;
2921 }
2922 else
2923 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002924 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00002925 if (v < SPWORDLEN)
2926 {
2927 /* Short line, use it completely and append the start of the
2928 * next line. */
2929 nextlinecol = 0;
2930 mch_memmove(nextline, line, (size_t)v);
2931 mch_memmove(nextline + v, nextline + SPWORDLEN,
2932 STRLEN(nextline + SPWORDLEN) + 1);
2933 nextline_idx = v + 1;
2934 }
2935 else
2936 {
2937 /* Long line, use only the last SPWORDLEN bytes. */
2938 nextlinecol = v - SPWORDLEN;
2939 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
2940 nextline_idx = SPWORDLEN + 1;
2941 }
2942 }
2943 }
2944#endif
2945
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 /* find start of trailing whitespace */
2947 if (wp->w_p_list && lcs_trail)
2948 {
2949 trailcol = (colnr_T)STRLEN(ptr);
2950 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
2951 --trailcol;
2952 trailcol += (colnr_T) (ptr - line);
2953 extra_check = TRUE;
2954 }
2955
2956 /*
2957 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
2958 * first character to be displayed.
2959 */
2960 if (wp->w_p_wrap)
2961 v = wp->w_skipcol;
2962 else
2963 v = wp->w_leftcol;
2964 if (v > 0)
2965 {
2966#ifdef FEAT_MBYTE
2967 char_u *prev_ptr = ptr;
2968#endif
2969 while (vcol < v && *ptr != NUL)
2970 {
2971 c = win_lbr_chartabsize(wp, ptr, (colnr_T)vcol, NULL);
2972 vcol += c;
2973#ifdef FEAT_MBYTE
2974 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002976 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 }
2978
2979#ifdef FEAT_VIRTUALEDIT
2980 /* When 'virtualedit' is set the end of the line may be before the
2981 * start of the displayed part. */
2982 if (vcol < v && *ptr == NUL && virtual_active())
2983 vcol = v;
2984#endif
2985
2986 /* Handle a character that's not completely on the screen: Put ptr at
2987 * that character but skip the first few screen characters. */
2988 if (vcol > v)
2989 {
2990 vcol -= c;
2991#ifdef FEAT_MBYTE
2992 ptr = prev_ptr;
2993#else
2994 --ptr;
2995#endif
2996 n_skip = v - vcol;
2997 }
2998
2999 /*
3000 * Adjust for when the inverted text is before the screen,
3001 * and when the start of the inverted text is before the screen.
3002 */
3003 if (tocol <= vcol)
3004 fromcol = 0;
3005 else if (fromcol >= 0 && fromcol < vcol)
3006 fromcol = vcol;
3007
3008#ifdef FEAT_LINEBREAK
3009 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3010 if (wp->w_p_wrap)
3011 need_showbreak = TRUE;
3012#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003013#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003014 /* When spell checking a word we need to figure out the start of the
3015 * word and if it's badly spelled or not. */
3016 if (has_spell)
3017 {
3018 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003019 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003020
3021 pos = wp->w_cursor;
3022 wp->w_cursor.lnum = lnum;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003023 wp->w_cursor.col = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003024 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003025 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003026 {
3027 /* no bad word found at line start, don't check until end of a
3028 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003029 spell_hlf = HLF_COUNT;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003030 word_end = (int)(spell_to_word_end(ptr, wp->w_buffer) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003031 }
3032 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003033 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003034 /* bad word found, use attributes until end of word */
3035 word_end = wp->w_cursor.col + len + 1;
3036
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003037 /* Turn index into actual attributes. */
3038 if (spell_hlf != HLF_COUNT)
3039 spell_attr = highlight_attr[spell_hlf];
3040 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003041 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003042
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003043# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003044 /* Need to restart syntax highlighting for this line. */
3045 if (has_syntax)
3046 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003047# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003048 }
3049#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 }
3051
3052 /*
3053 * Correct highlighting for cursor that can't be disabled.
3054 * Avoids having to check this for each character.
3055 */
3056 if (fromcol >= 0)
3057 {
3058 if (noinvcur)
3059 {
3060 if ((colnr_T)fromcol == wp->w_virtcol)
3061 {
3062 /* highlighting starts at cursor, let it start just after the
3063 * cursor */
3064 fromcol_prev = fromcol;
3065 fromcol = -1;
3066 }
3067 else if ((colnr_T)fromcol < wp->w_virtcol)
3068 /* restart highlighting after the cursor */
3069 fromcol_prev = wp->w_virtcol;
3070 }
3071 if (fromcol >= tocol)
3072 fromcol = -1;
3073 }
3074
3075#ifdef FEAT_SEARCH_EXTRA
3076 /*
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003077 * Handle highlighting the last used search pattern and ":match".
3078 * Do this for both search_hl and match_hl[3].
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003080 for (i = 3; i >= 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 {
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003082 shl = (i == 3) ? &search_hl : &match_hl[i];
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003083 shl->startcol = MAXCOL;
3084 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 shl->attr_cur = 0;
3086 if (shl->rm.regprog != NULL)
3087 {
3088 v = (long)(ptr - line);
3089 next_search_hl(wp, shl, lnum, (colnr_T)v);
3090
3091 /* Need to get the line again, a multi-line regexp may have made it
3092 * invalid. */
3093 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3094 ptr = line + v;
3095
3096 if (shl->lnum != 0 && shl->lnum <= lnum)
3097 {
3098 if (shl->lnum == lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003099 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003101 shl->startcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3103 - shl->rm.startpos[0].lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003104 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003106 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 /* Highlight one character for an empty match. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003108 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 {
3110#ifdef FEAT_MBYTE
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003111 if (has_mbyte && line[shl->endcol] != NUL)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003112 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 else
3114#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003115 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003117 if ((long)shl->startcol < v) /* match at leftcol */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 {
3119 shl->attr_cur = shl->attr;
3120 search_attr = shl->attr;
3121 }
3122 area_highlighting = TRUE;
3123 }
3124 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 }
3126#endif
3127
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003128#ifdef FEAT_SYN_HL
Bram Moolenaare2f98b92006-03-29 21:18:24 +00003129 /* Cursor line highlighting for 'cursorline'. Not when Visual mode is
3130 * active, because it's not clear what is selected then. */
3131 if (wp->w_p_cul && lnum == wp->w_cursor.lnum && !VIsual_active)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003132 {
3133 line_attr = hl_attr(HLF_CUL);
3134 area_highlighting = TRUE;
3135 }
3136#endif
3137
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003138 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 col = 0;
3140#ifdef FEAT_RIGHTLEFT
3141 if (wp->w_p_rl)
3142 {
3143 /* Rightleft window: process the text in the normal direction, but put
3144 * it in current_ScreenLine[] from right to left. Start at the
3145 * rightmost column of the window. */
3146 col = W_WIDTH(wp) - 1;
3147 off += col;
3148 }
3149#endif
3150
3151 /*
3152 * Repeat for the whole displayed line.
3153 */
3154 for (;;)
3155 {
3156 /* Skip this quickly when working on the text. */
3157 if (draw_state != WL_LINE)
3158 {
3159#ifdef FEAT_CMDWIN
3160 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3161 {
3162 draw_state = WL_CMDLINE;
3163 if (cmdwin_type != 0 && wp == curwin)
3164 {
3165 /* Draw the cmdline character. */
3166 *extra = cmdwin_type;
3167 n_extra = 1;
3168 p_extra = extra;
3169 c_extra = NUL;
3170 char_attr = hl_attr(HLF_AT);
3171 }
3172 }
3173#endif
3174
3175#ifdef FEAT_FOLDING
3176 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3177 {
3178 draw_state = WL_FOLD;
3179 if (wp->w_p_fdc > 0)
3180 {
3181 /* Draw the 'foldcolumn'. */
3182 fill_foldcolumn(extra, wp, FALSE, lnum);
3183 n_extra = wp->w_p_fdc;
3184 p_extra = extra;
3185 c_extra = NUL;
3186 char_attr = hl_attr(HLF_FC);
3187 }
3188 }
3189#endif
3190
3191#ifdef FEAT_SIGNS
3192 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3193 {
3194 draw_state = WL_SIGN;
3195 /* Show the sign column when there are any signs in this
3196 * buffer or when using Netbeans. */
3197 if (draw_signcolumn(wp)
3198# ifdef FEAT_DIFF
3199 && filler_todo <= 0
3200# endif
3201 )
3202 {
3203 int_u text_sign;
3204# ifdef FEAT_SIGN_ICONS
3205 int_u icon_sign;
3206# endif
3207
3208 /* Draw two cells with the sign value or blank. */
3209 c_extra = ' ';
3210 char_attr = hl_attr(HLF_SC);
3211 n_extra = 2;
3212
3213 if (row == startrow)
3214 {
3215 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3216 SIGN_TEXT);
3217# ifdef FEAT_SIGN_ICONS
3218 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3219 SIGN_ICON);
3220 if (gui.in_use && icon_sign != 0)
3221 {
3222 /* Use the image in this position. */
3223 c_extra = SIGN_BYTE;
3224# ifdef FEAT_NETBEANS_INTG
3225 if (buf_signcount(wp->w_buffer, lnum) > 1)
3226 c_extra = MULTISIGN_BYTE;
3227# endif
3228 char_attr = icon_sign;
3229 }
3230 else
3231# endif
3232 if (text_sign != 0)
3233 {
3234 p_extra = sign_get_text(text_sign);
3235 if (p_extra != NULL)
3236 {
3237 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003238 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 }
3240 char_attr = sign_get_attr(text_sign, FALSE);
3241 }
3242 }
3243 }
3244 }
3245#endif
3246
3247 if (draw_state == WL_NR - 1 && n_extra == 0)
3248 {
3249 draw_state = WL_NR;
3250 /* Display the line number. After the first fill with blanks
3251 * when the 'n' flag isn't in 'cpo' */
3252 if (wp->w_p_nu
3253 && (row == startrow
3254#ifdef FEAT_DIFF
3255 + filler_lines
3256#endif
3257 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3258 {
3259 /* Draw the line number (empty space after wrapping). */
3260 if (row == startrow
3261#ifdef FEAT_DIFF
3262 + filler_lines
3263#endif
3264 )
3265 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003266 sprintf((char *)extra, "%*ld ",
3267 number_width(wp), (long)lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 if (wp->w_skipcol > 0)
3269 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3270 *p_extra = '-';
3271#ifdef FEAT_RIGHTLEFT
3272 if (wp->w_p_rl) /* reverse line numbers */
3273 rl_mirror(extra);
3274#endif
3275 p_extra = extra;
3276 c_extra = NUL;
3277 }
3278 else
3279 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003280 n_extra = number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 char_attr = hl_attr(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003282#ifdef FEAT_SYN_HL
3283 /* When 'cursorline' is set highlight the line number of
3284 * the current line differently. */
3285 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3286 char_attr = hl_combine_attr(hl_attr(HLF_CUL), char_attr);
3287#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 }
3289 }
3290
3291#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3292 if (draw_state == WL_SBR - 1 && n_extra == 0)
3293 {
3294 draw_state = WL_SBR;
3295# ifdef FEAT_DIFF
3296 if (filler_todo > 0)
3297 {
3298 /* Draw "deleted" diff line(s). */
3299 if (char2cells(fill_diff) > 1)
3300 c_extra = '-';
3301 else
3302 c_extra = fill_diff;
3303# ifdef FEAT_RIGHTLEFT
3304 if (wp->w_p_rl)
3305 n_extra = col + 1;
3306 else
3307# endif
3308 n_extra = W_WIDTH(wp) - col;
3309 char_attr = hl_attr(HLF_DED);
3310 }
3311# endif
3312# ifdef FEAT_LINEBREAK
3313 if (*p_sbr != NUL && need_showbreak)
3314 {
3315 /* Draw 'showbreak' at the start of each broken line. */
3316 p_extra = p_sbr;
3317 c_extra = NUL;
3318 n_extra = (int)STRLEN(p_sbr);
3319 char_attr = hl_attr(HLF_AT);
3320 need_showbreak = FALSE;
3321 /* Correct end of highlighted area for 'showbreak',
3322 * required when 'linebreak' is also set. */
3323 if (tocol == vcol)
3324 tocol += n_extra;
3325 }
3326# endif
3327 }
3328#endif
3329
3330 if (draw_state == WL_LINE - 1 && n_extra == 0)
3331 {
3332 draw_state = WL_LINE;
3333 if (saved_n_extra)
3334 {
3335 /* Continue item from end of wrapped line. */
3336 n_extra = saved_n_extra;
3337 c_extra = saved_c_extra;
3338 p_extra = saved_p_extra;
3339 char_attr = saved_char_attr;
3340 }
3341 else
3342 char_attr = 0;
3343 }
3344 }
3345
3346 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003347 if (dollar_vcol != 0 && wp == curwin
3348 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349#ifdef FEAT_DIFF
3350 && filler_todo <= 0
3351#endif
3352 )
3353 {
3354 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3355 wp->w_p_rl);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003356 /* Pretend we have finished updating the window. Except when
3357 * 'cursorcolumn' is set. */
3358#ifdef FEAT_SYN_HL
3359 if (wp->w_p_cuc)
3360 row = wp->w_cline_row + wp->w_cline_height;
3361 else
3362#endif
3363 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 break;
3365 }
3366
3367 if (draw_state == WL_LINE && area_highlighting)
3368 {
3369 /* handle Visual or match highlighting in this line */
3370 if (vcol == fromcol
3371#ifdef FEAT_MBYTE
3372 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3373 && (*mb_ptr2cells)(ptr) > 1)
3374#endif
3375 || ((int)vcol_prev == fromcol_prev
3376 && vcol < tocol))
3377 area_attr = attr; /* start highlighting */
3378 else if (area_attr != 0
3379 && (vcol == tocol
3380 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382
3383#ifdef FEAT_SEARCH_EXTRA
3384 if (!n_extra)
3385 {
3386 /*
3387 * Check for start/end of search pattern match.
3388 * After end, check for start/end of next match.
3389 * When another match, have to check for start again.
3390 * Watch out for matching an empty string!
3391 * Do this first for search_hl, then for match_hl, so that
3392 * ":match" overrules 'hlsearch'.
3393 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003394 v = (long)(ptr - line);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003395 for (i = 3; i >= 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 {
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003397 shl = (i == 3) ? &search_hl : &match_hl[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 while (shl->rm.regprog != NULL)
3399 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003400 if (shl->startcol != MAXCOL
3401 && v >= (long)shl->startcol
3402 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 {
3404 shl->attr_cur = shl->attr;
3405 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003406 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 {
3408 shl->attr_cur = 0;
3409
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 next_search_hl(wp, shl, lnum, (colnr_T)v);
3411
3412 /* Need to get the line again, a multi-line regexp
3413 * may have made it invalid. */
3414 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3415 ptr = line + v;
3416
3417 if (shl->lnum == lnum)
3418 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003419 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003421 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003423 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003425 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 {
3427 /* highlight empty match, try again after
3428 * it */
3429#ifdef FEAT_MBYTE
3430 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003431 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003432 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 else
3434#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003435 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 }
3437
3438 /* Loop to check if the match starts at the
3439 * current position */
3440 continue;
3441 }
3442 }
3443 break;
3444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003446
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 /* ":match" highlighting overrules 'hlsearch' */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003448 for (i = 0; i <= 3; ++i)
3449 if (i == 3)
3450 search_attr = search_hl.attr_cur;
3451 else if (match_hl[i].attr_cur != 0)
3452 {
3453 search_attr = match_hl[i].attr_cur;
3454 break;
3455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 }
3457#endif
3458
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003460 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003462 if (diff_hlf == HLF_CHD && ptr - line >= change_start
3463 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003465 if (diff_hlf == HLF_TXD && ptr - line > change_end
3466 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003468 line_attr = hl_attr(diff_hlf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 }
3470#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003471
3472 /* Decide which of the highlight attributes to use. */
3473 attr_pri = TRUE;
3474 if (area_attr != 0)
3475 char_attr = area_attr;
3476 else if (search_attr != 0)
3477 char_attr = search_attr;
3478#ifdef LINE_ATTR
3479 /* Use line_attr when not in the Visual or 'incsearch' area
3480 * (area_attr may be 0 when "noinvcur" is set). */
3481 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
3482 || (vcol < fromcol || vcol >= tocol)))
3483 char_attr = line_attr;
3484#endif
3485 else
3486 {
3487 attr_pri = FALSE;
3488#ifdef FEAT_SYN_HL
3489 if (has_syntax)
3490 char_attr = syntax_attr;
3491 else
3492#endif
3493 char_attr = 0;
3494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 }
3496
3497 /*
3498 * Get the next character to put on the screen.
3499 */
3500 /*
3501 * The 'extra' array contains the extra stuff that is inserted to
3502 * represent special characters (non-printable stuff). When all
3503 * characters are the same, c_extra is used.
3504 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
3505 */
3506 if (n_extra > 0)
3507 {
3508 if (c_extra != NUL)
3509 {
3510 c = c_extra;
3511#ifdef FEAT_MBYTE
3512 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
3513 if (enc_utf8 && (*mb_char2len)(c) > 1)
3514 {
3515 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003516 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003517 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 }
3519 else
3520 mb_utf8 = FALSE;
3521#endif
3522 }
3523 else
3524 {
3525 c = *p_extra;
3526#ifdef FEAT_MBYTE
3527 if (has_mbyte)
3528 {
3529 mb_c = c;
3530 if (enc_utf8)
3531 {
3532 /* If the UTF-8 character is more than one byte:
3533 * Decode it into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003534 mb_l = (*mb_ptr2len)(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 mb_utf8 = FALSE;
3536 if (mb_l > n_extra)
3537 mb_l = 1;
3538 else if (mb_l > 1)
3539 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003540 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003542 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 }
3544 }
3545 else
3546 {
3547 /* if this is a DBCS character, put it in "mb_c" */
3548 mb_l = MB_BYTE2LEN(c);
3549 if (mb_l >= n_extra)
3550 mb_l = 1;
3551 else if (mb_l > 1)
3552 mb_c = (c << 8) + p_extra[1];
3553 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003554 if (mb_l == 0) /* at the NUL at end-of-line */
3555 mb_l = 1;
3556
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 /* If a double-width char doesn't fit display a '>' in the
3558 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003559 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560# ifdef FEAT_RIGHTLEFT
3561 wp->w_p_rl ? (col <= 0) :
3562# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003563 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 && (*mb_char2cells)(mb_c) == 2)
3565 {
3566 c = '>';
3567 mb_c = c;
3568 mb_l = 1;
3569 mb_utf8 = FALSE;
3570 multi_attr = hl_attr(HLF_AT);
3571 /* put the pointer back to output the double-width
3572 * character at the start of the next line. */
3573 ++n_extra;
3574 --p_extra;
3575 }
3576 else
3577 {
3578 n_extra -= mb_l - 1;
3579 p_extra += mb_l - 1;
3580 }
3581 }
3582#endif
3583 ++p_extra;
3584 }
3585 --n_extra;
3586 }
3587 else
3588 {
3589 /*
3590 * Get a character from the line itself.
3591 */
3592 c = *ptr;
3593#ifdef FEAT_MBYTE
3594 if (has_mbyte)
3595 {
3596 mb_c = c;
3597 if (enc_utf8)
3598 {
3599 /* If the UTF-8 character is more than one byte: Decode it
3600 * into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003601 mb_l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 mb_utf8 = FALSE;
3603 if (mb_l > 1)
3604 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003605 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 /* Overlong encoded ASCII or ASCII with composing char
3607 * is displayed normally, except a NUL. */
3608 if (mb_c < 0x80)
3609 c = mb_c;
3610 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003611
3612 /* At start of the line we can have a composing char.
3613 * Draw it as a space with a composing char. */
3614 if (utf_iscomposing(mb_c))
3615 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003616 for (i = Screen_mco - 1; i > 0; --i)
3617 u8cc[i] = u8cc[i - 1];
3618 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003619 mb_c = ' ';
3620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 }
3622
3623 if ((mb_l == 1 && c >= 0x80)
3624 || (mb_l >= 1 && mb_c == 0)
3625 || (mb_l > 1 && (!vim_isprintc(mb_c)
3626 || mb_c >= 0x10000)))
3627 {
3628 /*
3629 * Illegal UTF-8 byte: display as <xx>.
3630 * Non-BMP character : display as ? or fullwidth ?.
3631 */
3632 if (mb_c < 0x10000)
3633 {
3634 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003635# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 if (wp->w_p_rl) /* reverse */
3637 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003638# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 }
3640 else if (utf_char2cells(mb_c) != 2)
3641 STRCPY(extra, "?");
3642 else
3643 /* 0xff1f in UTF-8: full-width '?' */
3644 STRCPY(extra, "\357\274\237");
3645
3646 p_extra = extra;
3647 c = *p_extra;
3648 mb_c = mb_ptr2char_adv(&p_extra);
3649 mb_utf8 = (c >= 0x80);
3650 n_extra = (int)STRLEN(p_extra);
3651 c_extra = NUL;
3652 if (area_attr == 0 && search_attr == 0)
3653 {
3654 n_attr = n_extra + 1;
3655 extra_attr = hl_attr(HLF_8);
3656 saved_attr2 = char_attr; /* save current attr */
3657 }
3658 }
3659 else if (mb_l == 0) /* at the NUL at end-of-line */
3660 mb_l = 1;
3661#ifdef FEAT_ARABIC
3662 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
3663 {
3664 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003665 int pc, pc1, nc;
3666 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667
3668 /* The idea of what is the previous and next
3669 * character depends on 'rightleft'. */
3670 if (wp->w_p_rl)
3671 {
3672 pc = prev_c;
3673 pc1 = prev_c1;
3674 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003675 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 }
3677 else
3678 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003679 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003681 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 }
3683 prev_c = mb_c;
3684
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003685 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 }
3687 else
3688 prev_c = mb_c;
3689#endif
3690 }
3691 else /* enc_dbcs */
3692 {
3693 mb_l = MB_BYTE2LEN(c);
3694 if (mb_l == 0) /* at the NUL at end-of-line */
3695 mb_l = 1;
3696 else if (mb_l > 1)
3697 {
3698 /* We assume a second byte below 32 is illegal.
3699 * Hopefully this is OK for all double-byte encodings!
3700 */
3701 if (ptr[1] >= 32)
3702 mb_c = (c << 8) + ptr[1];
3703 else
3704 {
3705 if (ptr[1] == NUL)
3706 {
3707 /* head byte at end of line */
3708 mb_l = 1;
3709 transchar_nonprint(extra, c);
3710 }
3711 else
3712 {
3713 /* illegal tail byte */
3714 mb_l = 2;
3715 STRCPY(extra, "XX");
3716 }
3717 p_extra = extra;
3718 n_extra = (int)STRLEN(extra) - 1;
3719 c_extra = NUL;
3720 c = *p_extra++;
3721 if (area_attr == 0 && search_attr == 0)
3722 {
3723 n_attr = n_extra + 1;
3724 extra_attr = hl_attr(HLF_8);
3725 saved_attr2 = char_attr; /* save current attr */
3726 }
3727 mb_c = c;
3728 }
3729 }
3730 }
3731 /* If a double-width char doesn't fit display a '>' in the
3732 * last column; the character is displayed at the start of the
3733 * next line. */
3734 if ((
3735# ifdef FEAT_RIGHTLEFT
3736 wp->w_p_rl ? (col <= 0) :
3737# endif
3738 (col >= W_WIDTH(wp) - 1))
3739 && (*mb_char2cells)(mb_c) == 2)
3740 {
3741 c = '>';
3742 mb_c = c;
3743 mb_utf8 = FALSE;
3744 mb_l = 1;
3745 multi_attr = hl_attr(HLF_AT);
3746 /* Put pointer back so that the character will be
3747 * displayed at the start of the next line. */
3748 --ptr;
3749 }
3750 else if (*ptr != NUL)
3751 ptr += mb_l - 1;
3752
3753 /* If a double-width char doesn't fit at the left side display
3754 * a '<' in the first column. */
3755 if (n_skip > 0 && mb_l > 1)
3756 {
3757 extra[0] = '<';
3758 p_extra = extra;
3759 n_extra = 1;
3760 c_extra = NUL;
3761 c = ' ';
3762 if (area_attr == 0 && search_attr == 0)
3763 {
3764 n_attr = n_extra + 1;
3765 extra_attr = hl_attr(HLF_AT);
3766 saved_attr2 = char_attr; /* save current attr */
3767 }
3768 mb_c = c;
3769 mb_utf8 = FALSE;
3770 mb_l = 1;
3771 }
3772
3773 }
3774#endif
3775 ++ptr;
3776
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003777 /* 'list' : change char 160 to lcs_nbsp. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003778 if (wp->w_p_list && (c == 160
3779#ifdef FEAT_MBYTE
3780 || (mb_utf8 && mb_c == 160)
3781#endif
3782 ) && lcs_nbsp)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003783 {
3784 c = lcs_nbsp;
3785 if (area_attr == 0 && search_attr == 0)
3786 {
3787 n_attr = 1;
3788 extra_attr = hl_attr(HLF_8);
3789 saved_attr2 = char_attr; /* save current attr */
3790 }
3791#ifdef FEAT_MBYTE
3792 mb_c = c;
3793 if (enc_utf8 && (*mb_char2len)(c) > 1)
3794 {
3795 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003796 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003797 c = 0xc0;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003798 }
3799 else
3800 mb_utf8 = FALSE;
3801#endif
3802 }
3803
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 if (extra_check)
3805 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003806#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003807 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003808#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003809
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003810#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 /* Get syntax attribute, unless still at the start of the line
3812 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003813 v = (long)(ptr - line);
3814 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 {
3816 /* Get the syntax attribute for the character. If there
3817 * is an error, disable syntax highlighting. */
3818 save_did_emsg = did_emsg;
3819 did_emsg = FALSE;
3820
Bram Moolenaar217ad922005-03-20 22:37:15 +00003821 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003822# ifdef FEAT_SPELL
3823 has_spell ? &can_spell :
3824# endif
3825 NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826
3827 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00003828 {
3829 wp->w_buffer->b_syn_error = TRUE;
3830 has_syntax = FALSE;
3831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 else
3833 did_emsg = save_did_emsg;
3834
3835 /* Need to get the line again, a multi-line regexp may
3836 * have made it invalid. */
3837 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3838 ptr = line + v;
3839
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003840 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003842 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00003843 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003845#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003846
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003847#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003848 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00003849 * Only do this when there is no syntax highlighting, the
3850 * @Spell cluster is not used or the current syntax item
3851 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003852 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003853 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003854 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003855# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003856 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003857 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003858# endif
3859 if (c != 0 && (
3860# ifdef FEAT_SYN_HL
3861 !has_syntax ||
3862# endif
3863 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00003864 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00003865 char_u *prev_ptr, *p;
3866 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003867 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003868# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00003869 if (has_mbyte)
3870 {
3871 prev_ptr = ptr - mb_l;
3872 v -= mb_l - 1;
3873 }
3874 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00003875# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00003876 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003877
3878 /* Use nextline[] if possible, it has the start of the
3879 * next line concatenated. */
3880 if ((prev_ptr - line) - nextlinecol >= 0)
3881 p = nextline + (prev_ptr - line) - nextlinecol;
3882 else
3883 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003884 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00003885 len = spell_check(wp, p, &spell_hlf, &cap_col,
3886 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003887 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003888
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003889 /* In Insert mode only highlight a word that
3890 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003891 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003892 && (State & INSERT) != 0
3893 && wp->w_cursor.lnum == lnum
3894 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00003895 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003896 && wp->w_cursor.col < (colnr_T)word_end)
3897 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003898 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003899 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003900 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00003901
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003902 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00003903 && (p - nextline) + len > nextline_idx)
3904 {
3905 /* Remember that the good word continues at the
3906 * start of the next line. */
3907 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003908 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003909 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003910
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003911 /* Turn index into actual attributes. */
3912 if (spell_hlf != HLF_COUNT)
3913 spell_attr = highlight_attr[spell_hlf];
3914
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003915 if (cap_col > 0)
3916 {
3917 if (p != prev_ptr
3918 && (p - nextline) + cap_col >= nextline_idx)
3919 {
3920 /* Remember that the word in the next line
3921 * must start with a capital. */
3922 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003923 cap_col = (int)((p - nextline) + cap_col
3924 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003925 }
3926 else
3927 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003928 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003929 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00003930 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00003931 }
3932 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003933 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003934 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003935 char_attr = hl_combine_attr(char_attr, spell_attr);
3936 else
3937 char_attr = hl_combine_attr(spell_attr, char_attr);
3938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939#endif
3940#ifdef FEAT_LINEBREAK
3941 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00003942 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 */
3944 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003945 && !wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 {
3947 n_extra = win_lbr_chartabsize(wp, ptr - (
3948# ifdef FEAT_MBYTE
3949 has_mbyte ? mb_l :
3950# endif
3951 1), (colnr_T)vcol, NULL) - 1;
3952 c_extra = ' ';
3953 if (vim_iswhite(c))
3954 c = ' ';
3955 }
3956#endif
3957
3958 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
3959 {
3960 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003961 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 {
3963 n_attr = 1;
3964 extra_attr = hl_attr(HLF_8);
3965 saved_attr2 = char_attr; /* save current attr */
3966 }
3967#ifdef FEAT_MBYTE
3968 mb_c = c;
3969 if (enc_utf8 && (*mb_char2len)(c) > 1)
3970 {
3971 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003972 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003973 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 }
3975 else
3976 mb_utf8 = FALSE;
3977#endif
3978 }
3979 }
3980
3981 /*
3982 * Handling of non-printable characters.
3983 */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003984 if (!(chartab[c & 0xff] & CT_PRINT_CHAR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 {
3986 /*
3987 * when getting a character from the file, we may have to
3988 * turn it into something else on the way to putting it
3989 * into "ScreenLines".
3990 */
3991 if (c == TAB && (!wp->w_p_list || lcs_tab1))
3992 {
3993 /* tab amount depends on current column */
3994 n_extra = (int)wp->w_buffer->b_p_ts
3995 - vcol % (int)wp->w_buffer->b_p_ts - 1;
3996#ifdef FEAT_MBYTE
3997 mb_utf8 = FALSE; /* don't draw as UTF-8 */
3998#endif
3999 if (wp->w_p_list)
4000 {
4001 c = lcs_tab1;
4002 c_extra = lcs_tab2;
4003 n_attr = n_extra + 1;
4004 extra_attr = hl_attr(HLF_8);
4005 saved_attr2 = char_attr; /* save current attr */
4006#ifdef FEAT_MBYTE
4007 mb_c = c;
4008 if (enc_utf8 && (*mb_char2len)(c) > 1)
4009 {
4010 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004011 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004012 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 }
4014#endif
4015 }
4016 else
4017 {
4018 c_extra = ' ';
4019 c = ' ';
4020 }
4021 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004022 else if (c == NUL
4023 && ((wp->w_p_list && lcs_eol > 0)
4024 || ((fromcol >= 0 || fromcol_prev >= 0)
4025 && tocol > vcol
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004026#ifdef FEAT_VISUAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004027 && VIsual_mode != Ctrl_V
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004028#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004029 && (
4030# ifdef FEAT_RIGHTLEFT
4031 wp->w_p_rl ? (col >= 0) :
4032# endif
4033 (col < W_WIDTH(wp)))
4034 && !(noinvcur
4035 && (colnr_T)vcol == wp->w_virtcol)))
4036 && lcs_eol_one >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004038 /* Display a '$' after the line or highlight an extra
4039 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4041 /* For a diff line the highlighting continues after the
4042 * "$". */
4043 if (
4044# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004045 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046# ifdef LINE_ATTR
4047 &&
4048# endif
4049# endif
4050# ifdef LINE_ATTR
4051 line_attr == 0
4052# endif
4053 )
4054#endif
4055 {
4056#ifdef FEAT_VIRTUALEDIT
4057 /* In virtualedit, visual selections may extend
4058 * beyond end of line. */
4059 if (area_highlighting && virtual_active()
4060 && tocol != MAXCOL && vcol < tocol)
4061 n_extra = 0;
4062 else
4063#endif
4064 {
4065 p_extra = at_end_str;
4066 n_extra = 1;
4067 c_extra = NUL;
4068 }
4069 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004070 if (wp->w_p_list)
4071 c = lcs_eol;
4072 else
4073 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 lcs_eol_one = -1;
4075 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004076 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 {
4078 extra_attr = hl_attr(HLF_AT);
4079 n_attr = 1;
4080 }
4081#ifdef FEAT_MBYTE
4082 mb_c = c;
4083 if (enc_utf8 && (*mb_char2len)(c) > 1)
4084 {
4085 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004086 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004087 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 }
4089 else
4090 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4091#endif
4092 }
4093 else if (c != NUL)
4094 {
4095 p_extra = transchar(c);
4096#ifdef FEAT_RIGHTLEFT
4097 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4098 rl_mirror(p_extra); /* reverse "<12>" */
4099#endif
4100 n_extra = byte2cells(c) - 1;
4101 c_extra = NUL;
4102 c = *p_extra++;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004103 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 {
4105 n_attr = n_extra + 1;
4106 extra_attr = hl_attr(HLF_8);
4107 saved_attr2 = char_attr; /* save current attr */
4108 }
4109#ifdef FEAT_MBYTE
4110 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4111#endif
4112 }
4113#ifdef FEAT_VIRTUALEDIT
4114 else if (VIsual_active
4115 && (VIsual_mode == Ctrl_V
4116 || VIsual_mode == 'v')
4117 && virtual_active()
4118 && tocol != MAXCOL
4119 && vcol < tocol
4120 && (
4121# ifdef FEAT_RIGHTLEFT
4122 wp->w_p_rl ? (col >= 0) :
4123# endif
4124 (col < W_WIDTH(wp))))
4125 {
4126 c = ' ';
4127 --ptr; /* put it back at the NUL */
4128 }
4129#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004130#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 else if ((
4132# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004133 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 ) && (
4137# ifdef FEAT_RIGHTLEFT
4138 wp->w_p_rl ? (col >= 0) :
4139# endif
4140 (col < W_WIDTH(wp))))
4141 {
4142 /* Highlight until the right side of the window */
4143 c = ' ';
4144 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004145
4146 /* Remember we do the char for line highlighting. */
4147 ++did_line_attr;
4148
4149 /* don't do search HL for the rest of the line */
4150 if (line_attr != 0 && char_attr == search_attr && col > 0)
4151 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152# ifdef FEAT_DIFF
4153 if (diff_hlf == HLF_TXD)
4154 {
4155 diff_hlf = HLF_CHD;
4156 if (attr == 0 || char_attr != attr)
4157 char_attr = hl_attr(diff_hlf);
4158 }
4159# endif
4160 }
4161#endif
4162 }
4163 }
4164
4165 /* Don't override visual selection highlighting. */
4166 if (n_attr > 0
4167 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004168 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 char_attr = extra_attr;
4170
Bram Moolenaar81695252004-12-29 20:58:21 +00004171#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 /* XIM don't send preedit_start and preedit_end, but they send
4173 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
4174 * im_is_preediting() here. */
4175 if (xic != NULL
4176 && lnum == curwin->w_cursor.lnum
4177 && (State & INSERT)
4178 && !p_imdisable
4179 && im_is_preediting()
4180 && draw_state == WL_LINE)
4181 {
4182 colnr_T tcol;
4183
4184 if (preedit_end_col == MAXCOL)
4185 getvcol(curwin, &(curwin->w_cursor), &tcol, NULL, NULL);
4186 else
4187 tcol = preedit_end_col;
4188 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
4189 {
4190 if (feedback_old_attr < 0)
4191 {
4192 feedback_col = 0;
4193 feedback_old_attr = char_attr;
4194 }
4195 char_attr = im_get_feedback_attr(feedback_col);
4196 if (char_attr < 0)
4197 char_attr = feedback_old_attr;
4198 feedback_col++;
4199 }
4200 else if (feedback_old_attr >= 0)
4201 {
4202 char_attr = feedback_old_attr;
4203 feedback_old_attr = -1;
4204 feedback_col = 0;
4205 }
4206 }
4207#endif
4208 /*
4209 * Handle the case where we are in column 0 but not on the first
4210 * character of the line and the user wants us to show us a
4211 * special character (via 'listchars' option "precedes:<char>".
4212 */
4213 if (lcs_prec_todo != NUL
4214 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
4215#ifdef FEAT_DIFF
4216 && filler_todo <= 0
4217#endif
4218 && draw_state > WL_NR
4219 && c != NUL)
4220 {
4221 c = lcs_prec;
4222 lcs_prec_todo = NUL;
4223#ifdef FEAT_MBYTE
4224 mb_c = c;
4225 if (enc_utf8 && (*mb_char2len)(c) > 1)
4226 {
4227 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004228 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004229 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 }
4231 else
4232 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4233#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004234 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 {
4236 saved_attr3 = char_attr; /* save current attr */
4237 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
4238 n_attr3 = 1;
4239 }
4240 }
4241
4242 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00004243 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004245 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004246#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004247 || did_line_attr == 1
4248#endif
4249 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00004251#ifdef FEAT_SEARCH_EXTRA
4252 long prevcol = (long)(ptr - line) - (c == NUL);
4253#endif
4254
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 /* invert at least one char, used for Visual and empty line or
4256 * highlight match at end of line. If it's beyond the last
4257 * char on the screen, just overwrite that one (tricky!) Not
4258 * needed when a '$' was displayed for 'list'. */
4259 if (lcs_eol == lcs_eol_one
Bram Moolenaar91170f82006-05-05 21:15:17 +00004260 && ((area_attr != 0 && vcol == fromcol && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261#ifdef FEAT_SEARCH_EXTRA
4262 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004263 || ((prevcol == (long)search_hl.startcol
4264 || prevcol == (long)match_hl[0].startcol
4265 || prevcol == (long)match_hl[1].startcol
4266 || prevcol == (long)match_hl[2].startcol)
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004267# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004268 && did_line_attr <= 1
4269# endif
4270 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271#endif
4272 ))
4273 {
4274 int n = 0;
4275
4276#ifdef FEAT_RIGHTLEFT
4277 if (wp->w_p_rl)
4278 {
4279 if (col < 0)
4280 n = 1;
4281 }
4282 else
4283#endif
4284 {
4285 if (col >= W_WIDTH(wp))
4286 n = -1;
4287 }
4288 if (n != 0)
4289 {
4290 /* At the window boundary, highlight the last character
4291 * instead (better than nothing). */
4292 off += n;
4293 col += n;
4294 }
4295 else
4296 {
4297 /* Add a blank character to highlight. */
4298 ScreenLines[off] = ' ';
4299#ifdef FEAT_MBYTE
4300 if (enc_utf8)
4301 ScreenLinesUC[off] = 0;
4302#endif
4303 }
4304#ifdef FEAT_SEARCH_EXTRA
4305 if (area_attr == 0)
4306 {
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004307 for (i = 0; i <= 3; ++i)
4308 {
4309 if (i == 3)
4310 char_attr = search_hl.attr;
4311 else if ((ptr - line) - 1 == (long)match_hl[i].startcol)
4312 {
4313 char_attr = match_hl[i].attr;
4314 break;
4315 }
4316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 }
4318#endif
4319 ScreenAttrs[off] = char_attr;
4320#ifdef FEAT_RIGHTLEFT
4321 if (wp->w_p_rl)
4322 --col;
4323 else
4324#endif
4325 ++col;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004326 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00004328 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329
Bram Moolenaar91170f82006-05-05 21:15:17 +00004330 /*
4331 * At end of the text line.
4332 */
4333 if (c == NUL)
4334 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004335#ifdef FEAT_SYN_HL
4336 /* Highlight 'cursorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00004337 if (wp->w_p_wrap)
4338 v = wp->w_skipcol;
4339 else
4340 v = wp->w_leftcol;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004341 /* check if line ends before left margin */
4342 if (vcol < v + col - win_col_off(wp))
4343
4344 vcol = v + col - win_col_off(wp);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004345 if (wp->w_p_cuc
4346 && (int)wp->w_virtcol >= vcol
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004347 && (int)wp->w_virtcol < W_WIDTH(wp) * (row - startrow + 1)
4348 + v
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004349 && lnum != wp->w_cursor.lnum
4350# ifdef FEAT_RIGHTLEFT
4351 && !wp->w_p_rl
4352# endif
4353 )
4354 {
4355 while (col < W_WIDTH(wp))
4356 {
4357 ScreenLines[off] = ' ';
4358#ifdef FEAT_MBYTE
4359 if (enc_utf8)
4360 ScreenLinesUC[off] = 0;
4361#endif
4362 ++col;
Bram Moolenaarca003e12006-03-17 23:19:38 +00004363 if (vcol == (long)wp->w_virtcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004364 {
4365 ScreenAttrs[off] = hl_attr(HLF_CUC);
4366 break;
4367 }
4368 ScreenAttrs[off++] = 0;
4369 ++vcol;
4370 }
4371 }
4372#endif
4373
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 SCREEN_LINE(screen_row, W_WINCOL(wp), col, (int)W_WIDTH(wp),
4375 wp->w_p_rl);
4376 row++;
4377
4378 /*
4379 * Update w_cline_height and w_cline_folded if the cursor line was
4380 * updated (saves a call to plines() later).
4381 */
4382 if (wp == curwin && lnum == curwin->w_cursor.lnum)
4383 {
4384 curwin->w_cline_row = startrow;
4385 curwin->w_cline_height = row - startrow;
4386#ifdef FEAT_FOLDING
4387 curwin->w_cline_folded = FALSE;
4388#endif
4389 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
4390 }
4391
4392 break;
4393 }
4394
4395 /* line continues beyond line end */
4396 if (lcs_ext
4397 && !wp->w_p_wrap
4398#ifdef FEAT_DIFF
4399 && filler_todo <= 0
4400#endif
4401 && (
4402#ifdef FEAT_RIGHTLEFT
4403 wp->w_p_rl ? col == 0 :
4404#endif
4405 col == W_WIDTH(wp) - 1)
4406 && (*ptr != NUL
4407 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
4408 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
4409 {
4410 c = lcs_ext;
4411 char_attr = hl_attr(HLF_AT);
4412#ifdef FEAT_MBYTE
4413 mb_c = c;
4414 if (enc_utf8 && (*mb_char2len)(c) > 1)
4415 {
4416 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004417 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004418 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 }
4420 else
4421 mb_utf8 = FALSE;
4422#endif
4423 }
4424
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004425#ifdef FEAT_SYN_HL
4426 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
4427 * highlight the cursor position itself. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00004428 if (wp->w_p_cuc && vcol == (long)wp->w_virtcol
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004429 && lnum != wp->w_cursor.lnum
4430 && draw_state == WL_LINE)
4431 {
4432 vcol_save_attr = char_attr;
4433 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
4434 }
4435 else
4436 vcol_save_attr = -1;
4437#endif
4438
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 /*
4440 * Store character to be displayed.
4441 * Skip characters that are left of the screen for 'nowrap'.
4442 */
4443 vcol_prev = vcol;
4444 if (draw_state < WL_LINE || n_skip <= 0)
4445 {
4446 /*
4447 * Store the character.
4448 */
4449#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
4450 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
4451 {
4452 /* A double-wide character is: put first halve in left cell. */
4453 --off;
4454 --col;
4455 }
4456#endif
4457 ScreenLines[off] = c;
4458#ifdef FEAT_MBYTE
4459 if (enc_dbcs == DBCS_JPNU)
4460 ScreenLines2[off] = mb_c & 0xff;
4461 else if (enc_utf8)
4462 {
4463 if (mb_utf8)
4464 {
4465 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004466 if ((c & 0xff) == 0)
4467 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004468 for (i = 0; i < Screen_mco; ++i)
4469 {
4470 ScreenLinesC[i][off] = u8cc[i];
4471 if (u8cc[i] == 0)
4472 break;
4473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 }
4475 else
4476 ScreenLinesUC[off] = 0;
4477 }
4478 if (multi_attr)
4479 {
4480 ScreenAttrs[off] = multi_attr;
4481 multi_attr = 0;
4482 }
4483 else
4484#endif
4485 ScreenAttrs[off] = char_attr;
4486
4487#ifdef FEAT_MBYTE
4488 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
4489 {
4490 /* Need to fill two screen columns. */
4491 ++off;
4492 ++col;
4493 if (enc_utf8)
4494 /* UTF-8: Put a 0 in the second screen char. */
4495 ScreenLines[off] = 0;
4496 else
4497 /* DBCS: Put second byte in the second screen char. */
4498 ScreenLines[off] = mb_c & 0xff;
4499 ++vcol;
4500 /* When "tocol" is halfway a character, set it to the end of
4501 * the character, otherwise highlighting won't stop. */
4502 if (tocol == vcol)
4503 ++tocol;
4504#ifdef FEAT_RIGHTLEFT
4505 if (wp->w_p_rl)
4506 {
4507 /* now it's time to backup one cell */
4508 --off;
4509 --col;
4510 }
4511#endif
4512 }
4513#endif
4514#ifdef FEAT_RIGHTLEFT
4515 if (wp->w_p_rl)
4516 {
4517 --off;
4518 --col;
4519 }
4520 else
4521#endif
4522 {
4523 ++off;
4524 ++col;
4525 }
4526 }
4527 else
4528 --n_skip;
4529
4530 /* Only advance the "vcol" when after the 'number' column. */
4531 if (draw_state >= WL_SBR
4532#ifdef FEAT_DIFF
4533 && filler_todo <= 0
4534#endif
4535 )
4536 ++vcol;
4537
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004538#ifdef FEAT_SYN_HL
4539 if (vcol_save_attr >= 0)
4540 char_attr = vcol_save_attr;
4541#endif
4542
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 /* restore attributes after "predeces" in 'listchars' */
4544 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
4545 char_attr = saved_attr3;
4546
4547 /* restore attributes after last 'listchars' or 'number' char */
4548 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
4549 char_attr = saved_attr2;
4550
4551 /*
4552 * At end of screen line and there is more to come: Display the line
4553 * so far. If there is no more to display it is catched above.
4554 */
4555 if ((
4556#ifdef FEAT_RIGHTLEFT
4557 wp->w_p_rl ? (col < 0) :
4558#endif
4559 (col >= W_WIDTH(wp)))
4560 && (*ptr != NUL
4561#ifdef FEAT_DIFF
4562 || filler_todo > 0
4563#endif
4564 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
4565 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
4566 )
4567 {
4568 SCREEN_LINE(screen_row, W_WINCOL(wp), col, (int)W_WIDTH(wp),
4569 wp->w_p_rl);
4570 ++row;
4571 ++screen_row;
4572
4573 /* When not wrapping and finished diff lines, or when displayed
4574 * '$' and highlighting until last column, break here. */
4575 if ((!wp->w_p_wrap
4576#ifdef FEAT_DIFF
4577 && filler_todo <= 0
4578#endif
4579 ) || lcs_eol_one == -1)
4580 break;
4581
4582 /* When the window is too narrow draw all "@" lines. */
4583 if (draw_state != WL_LINE
4584#ifdef FEAT_DIFF
4585 && filler_todo <= 0
4586#endif
4587 )
4588 {
4589 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
4590#ifdef FEAT_VERTSPLIT
4591 draw_vsep_win(wp, row);
4592#endif
4593 row = endrow;
4594 }
4595
4596 /* When line got too long for screen break here. */
4597 if (row == endrow)
4598 {
4599 ++row;
4600 break;
4601 }
4602
4603 if (screen_cur_row == screen_row - 1
4604#ifdef FEAT_DIFF
4605 && filler_todo <= 0
4606#endif
4607 && W_WIDTH(wp) == Columns)
4608 {
4609 /* Remember that the line wraps, used for modeless copy. */
4610 LineWraps[screen_row - 1] = TRUE;
4611
4612 /*
4613 * Special trick to make copy/paste of wrapped lines work with
4614 * xterm/screen: write an extra character beyond the end of
4615 * the line. This will work with all terminal types
4616 * (regardless of the xn,am settings).
4617 * Only do this on a fast tty.
4618 * Only do this if the cursor is on the current line
4619 * (something has been written in it).
4620 * Don't do this for the GUI.
4621 * Don't do this for double-width characters.
4622 * Don't do this for a window not at the right screen border.
4623 */
4624 if (p_tf
4625#ifdef FEAT_GUI
4626 && !gui.in_use
4627#endif
4628#ifdef FEAT_MBYTE
4629 && !(has_mbyte
4630 && ((*mb_off2cells)(LineOffset[screen_row]) == 2
4631 || (*mb_off2cells)(LineOffset[screen_row - 1]
4632 + (int)Columns - 2) == 2))
4633#endif
4634 )
4635 {
4636 /* First make sure we are at the end of the screen line,
4637 * then output the same character again to let the
4638 * terminal know about the wrap. If the terminal doesn't
4639 * auto-wrap, we overwrite the character. */
4640 if (screen_cur_col != W_WIDTH(wp))
4641 screen_char(LineOffset[screen_row - 1]
4642 + (unsigned)Columns - 1,
4643 screen_row - 1, (int)(Columns - 1));
4644
4645#ifdef FEAT_MBYTE
4646 /* When there is a multi-byte character, just output a
4647 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00004648 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
4649 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 out_char(' ');
4651 else
4652#endif
4653 out_char(ScreenLines[LineOffset[screen_row - 1]
4654 + (Columns - 1)]);
4655 /* force a redraw of the first char on the next line */
4656 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
4657 screen_start(); /* don't know where cursor is now */
4658 }
4659 }
4660
4661 col = 0;
4662 off = (unsigned)(current_ScreenLine - ScreenLines);
4663#ifdef FEAT_RIGHTLEFT
4664 if (wp->w_p_rl)
4665 {
4666 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
4667 off += col;
4668 }
4669#endif
4670
4671 /* reset the drawing state for the start of a wrapped line */
4672 draw_state = WL_START;
4673 saved_n_extra = n_extra;
4674 saved_p_extra = p_extra;
4675 saved_c_extra = c_extra;
4676 saved_char_attr = char_attr;
4677 n_extra = 0;
4678 lcs_prec_todo = lcs_prec;
4679#ifdef FEAT_LINEBREAK
4680# ifdef FEAT_DIFF
4681 if (filler_todo <= 0)
4682# endif
4683 need_showbreak = TRUE;
4684#endif
4685#ifdef FEAT_DIFF
4686 --filler_todo;
4687 /* When the filler lines are actually below the last line of the
4688 * file, don't draw the line itself, break here. */
4689 if (filler_todo == 0 && wp->w_botfill)
4690 break;
4691#endif
4692 }
4693
4694 } /* for every character in the line */
4695
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004696#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004697 /* After an empty line check first word for capital. */
4698 if (*skipwhite(line) == NUL)
4699 {
4700 capcol_lnum = lnum + 1;
4701 cap_col = 0;
4702 }
4703#endif
4704
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 return row;
4706}
4707
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004708#ifdef FEAT_MBYTE
4709static int comp_char_differs __ARGS((int, int));
4710
4711/*
4712 * Return if the composing characters at "off_from" and "off_to" differ.
4713 */
4714 static int
4715comp_char_differs(off_from, off_to)
4716 int off_from;
4717 int off_to;
4718{
4719 int i;
4720
4721 for (i = 0; i < Screen_mco; ++i)
4722 {
4723 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
4724 return TRUE;
4725 if (ScreenLinesC[i][off_from] == 0)
4726 break;
4727 }
4728 return FALSE;
4729}
4730#endif
4731
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732/*
4733 * Check whether the given character needs redrawing:
4734 * - the (first byte of the) character is different
4735 * - the attributes are different
4736 * - the character is multi-byte and the next byte is different
4737 */
4738 static int
4739char_needs_redraw(off_from, off_to, cols)
4740 int off_from;
4741 int off_to;
4742 int cols;
4743{
4744 if (cols > 0
4745 && ((ScreenLines[off_from] != ScreenLines[off_to]
4746 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
4747
4748#ifdef FEAT_MBYTE
4749 || (enc_dbcs != 0
4750 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
4751 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
4752 ? ScreenLines2[off_from] != ScreenLines2[off_to]
4753 : (cols > 1 && ScreenLines[off_from + 1]
4754 != ScreenLines[off_to + 1])))
4755 || (enc_utf8
4756 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
4757 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004758 && comp_char_differs(off_from, off_to))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759#endif
4760 ))
4761 return TRUE;
4762 return FALSE;
4763}
4764
4765/*
4766 * Move one "cooked" screen line to the screen, but only the characters that
4767 * have actually changed. Handle insert/delete character.
4768 * "coloff" gives the first column on the screen for this line.
4769 * "endcol" gives the columns where valid characters are.
4770 * "clear_width" is the width of the window. It's > 0 if the rest of the line
4771 * needs to be cleared, negative otherwise.
4772 * "rlflag" is TRUE in a rightleft window:
4773 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
4774 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
4775 */
4776 static void
4777screen_line(row, coloff, endcol, clear_width
4778#ifdef FEAT_RIGHTLEFT
4779 , rlflag
4780#endif
4781 )
4782 int row;
4783 int coloff;
4784 int endcol;
4785 int clear_width;
4786#ifdef FEAT_RIGHTLEFT
4787 int rlflag;
4788#endif
4789{
4790 unsigned off_from;
4791 unsigned off_to;
4792 int col = 0;
4793#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_VERTSPLIT)
4794 int hl;
4795#endif
4796 int force = FALSE; /* force update rest of the line */
4797 int redraw_this /* bool: does character need redraw? */
4798#ifdef FEAT_GUI
4799 = TRUE /* For GUI when while-loop empty */
4800#endif
4801 ;
4802 int redraw_next; /* redraw_this for next character */
4803#ifdef FEAT_MBYTE
4804 int clear_next = FALSE;
4805 int char_cells; /* 1: normal char */
4806 /* 2: occupies two display cells */
4807# define CHAR_CELLS char_cells
4808#else
4809# define CHAR_CELLS 1
4810#endif
4811
4812# ifdef FEAT_CLIPBOARD
4813 clip_may_clear_selection(row, row);
4814# endif
4815
4816 off_from = (unsigned)(current_ScreenLine - ScreenLines);
4817 off_to = LineOffset[row] + coloff;
4818
4819#ifdef FEAT_RIGHTLEFT
4820 if (rlflag)
4821 {
4822 /* Clear rest first, because it's left of the text. */
4823 if (clear_width > 0)
4824 {
4825 while (col <= endcol && ScreenLines[off_to] == ' '
4826 && ScreenAttrs[off_to] == 0
4827# ifdef FEAT_MBYTE
4828 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
4829# endif
4830 )
4831 {
4832 ++off_to;
4833 ++col;
4834 }
4835 if (col <= endcol)
4836 screen_fill(row, row + 1, col + coloff,
4837 endcol + coloff + 1, ' ', ' ', 0);
4838 }
4839 col = endcol + 1;
4840 off_to = LineOffset[row] + col + coloff;
4841 off_from += col;
4842 endcol = (clear_width > 0 ? clear_width : -clear_width);
4843 }
4844#endif /* FEAT_RIGHTLEFT */
4845
4846 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
4847
4848 while (col < endcol)
4849 {
4850#ifdef FEAT_MBYTE
4851 if (has_mbyte && (col + 1 < endcol))
4852 char_cells = (*mb_off2cells)(off_from);
4853 else
4854 char_cells = 1;
4855#endif
4856
4857 redraw_this = redraw_next;
4858 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
4859 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
4860
4861#ifdef FEAT_GUI
4862 /* If the next character was bold, then redraw the current character to
4863 * remove any pixels that might have spilt over into us. This only
4864 * happens in the GUI.
4865 */
4866 if (redraw_next && gui.in_use)
4867 {
4868 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004869 if (hl > HL_ALL)
4870 hl = syn_attr2attr(hl);
4871 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 redraw_this = TRUE;
4873 }
4874#endif
4875
4876 if (redraw_this)
4877 {
4878 /*
4879 * Special handling when 'xs' termcap flag set (hpterm):
4880 * Attributes for characters are stored at the position where the
4881 * cursor is when writing the highlighting code. The
4882 * start-highlighting code must be written with the cursor on the
4883 * first highlighted character. The stop-highlighting code must
4884 * be written with the cursor just after the last highlighted
4885 * character.
4886 * Overwriting a character doesn't remove it's highlighting. Need
4887 * to clear the rest of the line, and force redrawing it
4888 * completely.
4889 */
4890 if ( p_wiv
4891 && !force
4892#ifdef FEAT_GUI
4893 && !gui.in_use
4894#endif
4895 && ScreenAttrs[off_to] != 0
4896 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
4897 {
4898 /*
4899 * Need to remove highlighting attributes here.
4900 */
4901 windgoto(row, col + coloff);
4902 out_str(T_CE); /* clear rest of this screen line */
4903 screen_start(); /* don't know where cursor is now */
4904 force = TRUE; /* force redraw of rest of the line */
4905 redraw_next = TRUE; /* or else next char would miss out */
4906
4907 /*
4908 * If the previous character was highlighted, need to stop
4909 * highlighting at this character.
4910 */
4911 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
4912 {
4913 screen_attr = ScreenAttrs[off_to - 1];
4914 term_windgoto(row, col + coloff);
4915 screen_stop_highlight();
4916 }
4917 else
4918 screen_attr = 0; /* highlighting has stopped */
4919 }
4920#ifdef FEAT_MBYTE
4921 if (enc_dbcs != 0)
4922 {
4923 /* Check if overwriting a double-byte with a single-byte or
4924 * the other way around requires another character to be
4925 * redrawn. For UTF-8 this isn't needed, because comparing
4926 * ScreenLinesUC[] is sufficient. */
4927 if (char_cells == 1
4928 && col + 1 < endcol
4929 && (*mb_off2cells)(off_to) > 1)
4930 {
4931 /* Writing a single-cell character over a double-cell
4932 * character: need to redraw the next cell. */
4933 ScreenLines[off_to + 1] = 0;
4934 redraw_next = TRUE;
4935 }
4936 else if (char_cells == 2
4937 && col + 2 < endcol
4938 && (*mb_off2cells)(off_to) == 1
4939 && (*mb_off2cells)(off_to + 1) > 1)
4940 {
4941 /* Writing the second half of a double-cell character over
4942 * a double-cell character: need to redraw the second
4943 * cell. */
4944 ScreenLines[off_to + 2] = 0;
4945 redraw_next = TRUE;
4946 }
4947
4948 if (enc_dbcs == DBCS_JPNU)
4949 ScreenLines2[off_to] = ScreenLines2[off_from];
4950 }
4951 /* When writing a single-width character over a double-width
4952 * character and at the end of the redrawn text, need to clear out
4953 * the right halve of the old character.
4954 * Also required when writing the right halve of a double-width
4955 * char over the left halve of an existing one. */
4956 if (has_mbyte && col + char_cells == endcol
4957 && ((char_cells == 1
4958 && (*mb_off2cells)(off_to) > 1)
4959 || (char_cells == 2
4960 && (*mb_off2cells)(off_to) == 1
4961 && (*mb_off2cells)(off_to + 1) > 1)))
4962 clear_next = TRUE;
4963#endif
4964
4965 ScreenLines[off_to] = ScreenLines[off_from];
4966#ifdef FEAT_MBYTE
4967 if (enc_utf8)
4968 {
4969 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
4970 if (ScreenLinesUC[off_from] != 0)
4971 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004972 int i;
4973
4974 for (i = 0; i < Screen_mco; ++i)
4975 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 }
4977 }
4978 if (char_cells == 2)
4979 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
4980#endif
4981
4982#if defined(FEAT_GUI) || defined(UNIX)
4983 /* The bold trick makes a single row of pixels appear in the next
4984 * character. When a bold character is removed, the next
4985 * character should be redrawn too. This happens for our own GUI
4986 * and for some xterms. */
4987 if (
4988# ifdef FEAT_GUI
4989 gui.in_use
4990# endif
4991# if defined(FEAT_GUI) && defined(UNIX)
4992 ||
4993# endif
4994# ifdef UNIX
4995 term_is_xterm
4996# endif
4997 )
4998 {
4999 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005000 if (hl > HL_ALL)
5001 hl = syn_attr2attr(hl);
5002 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 redraw_next = TRUE;
5004 }
5005#endif
5006 ScreenAttrs[off_to] = ScreenAttrs[off_from];
5007#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005008 /* For simplicity set the attributes of second half of a
5009 * double-wide character equal to the first half. */
5010 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005012
5013 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 else
5016#endif
5017 screen_char(off_to, row, col + coloff);
5018 }
5019 else if ( p_wiv
5020#ifdef FEAT_GUI
5021 && !gui.in_use
5022#endif
5023 && col + coloff > 0)
5024 {
5025 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
5026 {
5027 /*
5028 * Don't output stop-highlight when moving the cursor, it will
5029 * stop the highlighting when it should continue.
5030 */
5031 screen_attr = 0;
5032 }
5033 else if (screen_attr != 0)
5034 screen_stop_highlight();
5035 }
5036
5037 off_to += CHAR_CELLS;
5038 off_from += CHAR_CELLS;
5039 col += CHAR_CELLS;
5040 }
5041
5042#ifdef FEAT_MBYTE
5043 if (clear_next)
5044 {
5045 /* Clear the second half of a double-wide character of which the left
5046 * half was overwritten with a single-wide character. */
5047 ScreenLines[off_to] = ' ';
5048 if (enc_utf8)
5049 ScreenLinesUC[off_to] = 0;
5050 screen_char(off_to, row, col + coloff);
5051 }
5052#endif
5053
5054 if (clear_width > 0
5055#ifdef FEAT_RIGHTLEFT
5056 && !rlflag
5057#endif
5058 )
5059 {
5060#ifdef FEAT_GUI
5061 int startCol = col;
5062#endif
5063
5064 /* blank out the rest of the line */
5065 while (col < clear_width && ScreenLines[off_to] == ' '
5066 && ScreenAttrs[off_to] == 0
5067#ifdef FEAT_MBYTE
5068 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5069#endif
5070 )
5071 {
5072 ++off_to;
5073 ++col;
5074 }
5075 if (col < clear_width)
5076 {
5077#ifdef FEAT_GUI
5078 /*
5079 * In the GUI, clearing the rest of the line may leave pixels
5080 * behind if the first character cleared was bold. Some bold
5081 * fonts spill over the left. In this case we redraw the previous
5082 * character too. If we didn't skip any blanks above, then we
5083 * only redraw if the character wasn't already redrawn anyway.
5084 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00005085 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 {
5087 hl = ScreenAttrs[off_to];
5088 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00005089 {
5090 int prev_cells = 1;
5091# ifdef FEAT_MBYTE
5092 if (enc_utf8)
5093 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
5094 * that its width is 2. */
5095 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
5096 else if (enc_dbcs != 0)
5097 {
5098 /* find previous character by counting from first
5099 * column and get its width. */
5100 unsigned off = LineOffset[row];
5101
5102 while (off < off_to)
5103 {
5104 prev_cells = (*mb_off2cells)(off);
5105 off += prev_cells;
5106 }
5107 }
5108
5109 if (enc_dbcs != 0 && prev_cells > 1)
5110 screen_char_2(off_to - prev_cells, row,
5111 col + coloff - prev_cells);
5112 else
5113# endif
5114 screen_char(off_to - prev_cells, row,
5115 col + coloff - prev_cells);
5116 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 }
5118#endif
5119 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
5120 ' ', ' ', 0);
5121#ifdef FEAT_VERTSPLIT
5122 off_to += clear_width - col;
5123 col = clear_width;
5124#endif
5125 }
5126 }
5127
5128 if (clear_width > 0)
5129 {
5130#ifdef FEAT_VERTSPLIT
5131 /* For a window that's left of another, draw the separator char. */
5132 if (col + coloff < Columns)
5133 {
5134 int c;
5135
5136 c = fillchar_vsep(&hl);
5137 if (ScreenLines[off_to] != c
5138# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005139 || (enc_utf8 && (int)ScreenLinesUC[off_to]
5140 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141# endif
5142 || ScreenAttrs[off_to] != hl)
5143 {
5144 ScreenLines[off_to] = c;
5145 ScreenAttrs[off_to] = hl;
5146# ifdef FEAT_MBYTE
5147 if (enc_utf8)
5148 {
5149 if (c >= 0x80)
5150 {
5151 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005152 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 }
5154 else
5155 ScreenLinesUC[off_to] = 0;
5156 }
5157# endif
5158 screen_char(off_to, row, col + coloff);
5159 }
5160 }
5161 else
5162#endif
5163 LineWraps[row] = FALSE;
5164 }
5165}
5166
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005167#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005169 * Mirror text "str" for right-left displaying.
5170 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005172 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173rl_mirror(str)
5174 char_u *str;
5175{
5176 char_u *p1, *p2;
5177 int t;
5178
5179 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
5180 {
5181 t = *p1;
5182 *p1 = *p2;
5183 *p2 = t;
5184 }
5185}
5186#endif
5187
5188#if defined(FEAT_WINDOWS) || defined(PROTO)
5189/*
5190 * mark all status lines for redraw; used after first :cd
5191 */
5192 void
5193status_redraw_all()
5194{
5195 win_T *wp;
5196
5197 for (wp = firstwin; wp; wp = wp->w_next)
5198 if (wp->w_status_height)
5199 {
5200 wp->w_redr_status = TRUE;
5201 redraw_later(VALID);
5202 }
5203}
5204
5205/*
5206 * mark all status lines of the current buffer for redraw
5207 */
5208 void
5209status_redraw_curbuf()
5210{
5211 win_T *wp;
5212
5213 for (wp = firstwin; wp; wp = wp->w_next)
5214 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
5215 {
5216 wp->w_redr_status = TRUE;
5217 redraw_later(VALID);
5218 }
5219}
5220
5221/*
5222 * Redraw all status lines that need to be redrawn.
5223 */
5224 void
5225redraw_statuslines()
5226{
5227 win_T *wp;
5228
5229 for (wp = firstwin; wp; wp = wp->w_next)
5230 if (wp->w_redr_status)
5231 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00005232 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005233 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234}
5235#endif
5236
5237#if (defined(FEAT_WILDMENU) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
5238/*
5239 * Redraw all status lines at the bottom of frame "frp".
5240 */
5241 void
5242win_redraw_last_status(frp)
5243 frame_T *frp;
5244{
5245 if (frp->fr_layout == FR_LEAF)
5246 frp->fr_win->w_redr_status = TRUE;
5247 else if (frp->fr_layout == FR_ROW)
5248 {
5249 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
5250 win_redraw_last_status(frp);
5251 }
5252 else /* frp->fr_layout == FR_COL */
5253 {
5254 frp = frp->fr_child;
5255 while (frp->fr_next != NULL)
5256 frp = frp->fr_next;
5257 win_redraw_last_status(frp);
5258 }
5259}
5260#endif
5261
5262#ifdef FEAT_VERTSPLIT
5263/*
5264 * Draw the verticap separator right of window "wp" starting with line "row".
5265 */
5266 static void
5267draw_vsep_win(wp, row)
5268 win_T *wp;
5269 int row;
5270{
5271 int hl;
5272 int c;
5273
5274 if (wp->w_vsep_width)
5275 {
5276 /* draw the vertical separator right of this window */
5277 c = fillchar_vsep(&hl);
5278 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
5279 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
5280 c, ' ', hl);
5281 }
5282}
5283#endif
5284
5285#ifdef FEAT_WILDMENU
5286static int status_match_len __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005287static int skip_status_match_char __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288
5289/*
5290 * Get the lenght of an item as it will be shown in the status line.
5291 */
5292 static int
5293status_match_len(xp, s)
5294 expand_T *xp;
5295 char_u *s;
5296{
5297 int len = 0;
5298
5299#ifdef FEAT_MENU
5300 int emenu = (xp->xp_context == EXPAND_MENUS
5301 || xp->xp_context == EXPAND_MENUNAMES);
5302
5303 /* Check for menu separators - replace with '|'. */
5304 if (emenu && menu_is_separator(s))
5305 return 1;
5306#endif
5307
5308 while (*s != NUL)
5309 {
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005310 if (skip_status_match_char(xp, s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 ++s;
Bram Moolenaar81695252004-12-29 20:58:21 +00005312 len += ptr2cells(s);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005313 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 }
5315
5316 return len;
5317}
5318
5319/*
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005320 * Return TRUE for characters that are not displayed in a status match.
5321 * These are backslashes used for escaping. Do show backslashes in help tags.
5322 */
5323 static int
5324skip_status_match_char(xp, s)
5325 expand_T *xp;
5326 char_u *s;
5327{
5328 return ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
5329#ifdef FEAT_MENU
5330 || ((xp->xp_context == EXPAND_MENUS
5331 || xp->xp_context == EXPAND_MENUNAMES)
5332 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
5333#endif
5334 );
5335}
5336
5337/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 * Show wildchar matches in the status line.
5339 * Show at least the "match" item.
5340 * We start at item 'first_match' in the list and show all matches that fit.
5341 *
5342 * If inversion is possible we use it. Else '=' characters are used.
5343 */
5344 void
5345win_redr_status_matches(xp, num_matches, matches, match, showtail)
5346 expand_T *xp;
5347 int num_matches;
5348 char_u **matches; /* list of matches */
5349 int match;
5350 int showtail;
5351{
5352#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
5353 int row;
5354 char_u *buf;
5355 int len;
5356 int clen; /* lenght in screen cells */
5357 int fillchar;
5358 int attr;
5359 int i;
5360 int highlight = TRUE;
5361 char_u *selstart = NULL;
5362 int selstart_col = 0;
5363 char_u *selend = NULL;
5364 static int first_match = 0;
5365 int add_left = FALSE;
5366 char_u *s;
5367#ifdef FEAT_MENU
5368 int emenu;
5369#endif
5370#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
5371 int l;
5372#endif
5373
5374 if (matches == NULL) /* interrupted completion? */
5375 return;
5376
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005377#ifdef FEAT_MBYTE
5378 if (has_mbyte)
5379 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
5380 else
5381#endif
5382 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 if (buf == NULL)
5384 return;
5385
5386 if (match == -1) /* don't show match but original text */
5387 {
5388 match = 0;
5389 highlight = FALSE;
5390 }
5391 /* count 1 for the ending ">" */
5392 clen = status_match_len(xp, L_MATCH(match)) + 3;
5393 if (match == 0)
5394 first_match = 0;
5395 else if (match < first_match)
5396 {
5397 /* jumping left, as far as we can go */
5398 first_match = match;
5399 add_left = TRUE;
5400 }
5401 else
5402 {
5403 /* check if match fits on the screen */
5404 for (i = first_match; i < match; ++i)
5405 clen += status_match_len(xp, L_MATCH(i)) + 2;
5406 if (first_match > 0)
5407 clen += 2;
5408 /* jumping right, put match at the left */
5409 if ((long)clen > Columns)
5410 {
5411 first_match = match;
5412 /* if showing the last match, we can add some on the left */
5413 clen = 2;
5414 for (i = match; i < num_matches; ++i)
5415 {
5416 clen += status_match_len(xp, L_MATCH(i)) + 2;
5417 if ((long)clen >= Columns)
5418 break;
5419 }
5420 if (i == num_matches)
5421 add_left = TRUE;
5422 }
5423 }
5424 if (add_left)
5425 while (first_match > 0)
5426 {
5427 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
5428 if ((long)clen >= Columns)
5429 break;
5430 --first_match;
5431 }
5432
5433 fillchar = fillchar_status(&attr, TRUE);
5434
5435 if (first_match == 0)
5436 {
5437 *buf = NUL;
5438 len = 0;
5439 }
5440 else
5441 {
5442 STRCPY(buf, "< ");
5443 len = 2;
5444 }
5445 clen = len;
5446
5447 i = first_match;
5448 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
5449 {
5450 if (i == match)
5451 {
5452 selstart = buf + len;
5453 selstart_col = clen;
5454 }
5455
5456 s = L_MATCH(i);
5457 /* Check for menu separators - replace with '|' */
5458#ifdef FEAT_MENU
5459 emenu = (xp->xp_context == EXPAND_MENUS
5460 || xp->xp_context == EXPAND_MENUNAMES);
5461 if (emenu && menu_is_separator(s))
5462 {
5463 STRCPY(buf + len, transchar('|'));
5464 l = (int)STRLEN(buf + len);
5465 len += l;
5466 clen += l;
5467 }
5468 else
5469#endif
5470 for ( ; *s != NUL; ++s)
5471 {
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005472 if (skip_status_match_char(xp, s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 ++s;
5474 clen += ptr2cells(s);
5475#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005476 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 {
5478 STRNCPY(buf + len, s, l);
5479 s += l - 1;
5480 len += l;
5481 }
5482 else
5483#endif
5484 {
5485 STRCPY(buf + len, transchar_byte(*s));
5486 len += (int)STRLEN(buf + len);
5487 }
5488 }
5489 if (i == match)
5490 selend = buf + len;
5491
5492 *(buf + len++) = ' ';
5493 *(buf + len++) = ' ';
5494 clen += 2;
5495 if (++i == num_matches)
5496 break;
5497 }
5498
5499 if (i != num_matches)
5500 {
5501 *(buf + len++) = '>';
5502 ++clen;
5503 }
5504
5505 buf[len] = NUL;
5506
5507 row = cmdline_row - 1;
5508 if (row >= 0)
5509 {
5510 if (wild_menu_showing == 0)
5511 {
5512 if (msg_scrolled > 0)
5513 {
5514 /* Put the wildmenu just above the command line. If there is
5515 * no room, scroll the screen one line up. */
5516 if (cmdline_row == Rows - 1)
5517 {
5518 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
5519 ++msg_scrolled;
5520 }
5521 else
5522 {
5523 ++cmdline_row;
5524 ++row;
5525 }
5526 wild_menu_showing = WM_SCROLLED;
5527 }
5528 else
5529 {
5530 /* Create status line if needed by setting 'laststatus' to 2.
5531 * Set 'winminheight' to zero to avoid that the window is
5532 * resized. */
5533 if (lastwin->w_status_height == 0)
5534 {
5535 save_p_ls = p_ls;
5536 save_p_wmh = p_wmh;
5537 p_ls = 2;
5538 p_wmh = 0;
5539 last_status(FALSE);
5540 }
5541 wild_menu_showing = WM_SHOWN;
5542 }
5543 }
5544
5545 screen_puts(buf, row, 0, attr);
5546 if (selstart != NULL && highlight)
5547 {
5548 *selend = NUL;
5549 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
5550 }
5551
5552 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
5553 }
5554
5555#ifdef FEAT_VERTSPLIT
5556 win_redraw_last_status(topframe);
5557#else
5558 lastwin->w_redr_status = TRUE;
5559#endif
5560 vim_free(buf);
5561}
5562#endif
5563
5564#if defined(FEAT_WINDOWS) || defined(PROTO)
5565/*
5566 * Redraw the status line of window wp.
5567 *
5568 * If inversion is possible we use it. Else '=' characters are used.
5569 */
5570 void
5571win_redr_status(wp)
5572 win_T *wp;
5573{
5574 int row;
5575 char_u *p;
5576 int len;
5577 int fillchar;
5578 int attr;
5579 int this_ru_col;
5580
5581 wp->w_redr_status = FALSE;
5582 if (wp->w_status_height == 0)
5583 {
5584 /* no status line, can only be last window */
5585 redraw_cmdline = TRUE;
5586 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005587 else if (!redrawing()
5588#ifdef FEAT_INS_EXPAND
5589 /* don't update status line when popup menu is visible and may be
5590 * drawn over it */
5591 || pum_visible()
5592#endif
5593 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 {
5595 /* Don't redraw right now, do it later. */
5596 wp->w_redr_status = TRUE;
5597 }
5598#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005599 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 {
5601 /* redraw custom status line */
Bram Moolenaar238a5642006-02-21 22:12:05 +00005602 redraw_custum_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 }
5604#endif
5605 else
5606 {
5607 fillchar = fillchar_status(&attr, wp == curwin);
5608
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005609 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610 p = NameBuff;
5611 len = (int)STRLEN(p);
5612
5613 if (wp->w_buffer->b_help
5614#ifdef FEAT_QUICKFIX
5615 || wp->w_p_pvw
5616#endif
5617 || bufIsChanged(wp->w_buffer)
5618 || wp->w_buffer->b_p_ro)
5619 *(p + len++) = ' ';
5620 if (wp->w_buffer->b_help)
5621 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005622 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 len += (int)STRLEN(p + len);
5624 }
5625#ifdef FEAT_QUICKFIX
5626 if (wp->w_p_pvw)
5627 {
5628 STRCPY(p + len, _("[Preview]"));
5629 len += (int)STRLEN(p + len);
5630 }
5631#endif
5632 if (bufIsChanged(wp->w_buffer))
5633 {
5634 STRCPY(p + len, "[+]");
5635 len += 3;
5636 }
5637 if (wp->w_buffer->b_p_ro)
5638 {
5639 STRCPY(p + len, "[RO]");
5640 len += 4;
5641 }
5642
5643#ifndef FEAT_VERTSPLIT
5644 this_ru_col = ru_col;
5645 if (this_ru_col < (Columns + 1) / 2)
5646 this_ru_col = (Columns + 1) / 2;
5647#else
5648 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
5649 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
5650 this_ru_col = (W_WIDTH(wp) + 1) / 2;
5651 if (this_ru_col <= 1)
5652 {
5653 p = (char_u *)"<"; /* No room for file name! */
5654 len = 1;
5655 }
5656 else
5657#endif
5658#ifdef FEAT_MBYTE
5659 if (has_mbyte)
5660 {
5661 int clen = 0, i;
5662
5663 /* Count total number of display cells. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005664 for (i = 0; p[i] != NUL; i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 clen += (*mb_ptr2cells)(p + i);
5666 /* Find first character that will fit.
5667 * Going from start to end is much faster for DBCS. */
5668 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005669 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670 clen -= (*mb_ptr2cells)(p + i);
5671 len = clen;
5672 if (i > 0)
5673 {
5674 p = p + i - 1;
5675 *p = '<';
5676 ++len;
5677 }
5678
5679 }
5680 else
5681#endif
5682 if (len > this_ru_col - 1)
5683 {
5684 p += len - (this_ru_col - 1);
5685 *p = '<';
5686 len = this_ru_col - 1;
5687 }
5688
5689 row = W_WINROW(wp) + wp->w_height;
5690 screen_puts(p, row, W_WINCOL(wp), attr);
5691 screen_fill(row, row + 1, len + W_WINCOL(wp),
5692 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
5693
5694 if (get_keymap_str(wp, NameBuff, MAXPATHL)
5695 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
5696 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
5697 - 1 + W_WINCOL(wp)), attr);
5698
5699#ifdef FEAT_CMDL_INFO
5700 win_redr_ruler(wp, TRUE);
5701#endif
5702 }
5703
5704#ifdef FEAT_VERTSPLIT
5705 /*
5706 * May need to draw the character below the vertical separator.
5707 */
5708 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
5709 {
5710 if (stl_connected(wp))
5711 fillchar = fillchar_status(&attr, wp == curwin);
5712 else
5713 fillchar = fillchar_vsep(&attr);
5714 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
5715 attr);
5716 }
5717#endif
5718}
5719
Bram Moolenaar238a5642006-02-21 22:12:05 +00005720#ifdef FEAT_STL_OPT
5721/*
5722 * Redraw the status line according to 'statusline' and take care of any
5723 * errors encountered.
5724 */
5725 static void
5726redraw_custum_statusline(wp)
5727 win_T *wp;
5728{
5729 int save_called_emsg = called_emsg;
5730
5731 called_emsg = FALSE;
5732 win_redr_custom(wp, FALSE);
5733 if (called_emsg)
5734 set_string_option_direct((char_u *)"statusline", -1,
5735 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005736 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00005737 called_emsg |= save_called_emsg;
5738}
5739#endif
5740
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741# ifdef FEAT_VERTSPLIT
5742/*
5743 * Return TRUE if the status line of window "wp" is connected to the status
5744 * line of the window right of it. If not, then it's a vertical separator.
5745 * Only call if (wp->w_vsep_width != 0).
5746 */
5747 int
5748stl_connected(wp)
5749 win_T *wp;
5750{
5751 frame_T *fr;
5752
5753 fr = wp->w_frame;
5754 while (fr->fr_parent != NULL)
5755 {
5756 if (fr->fr_parent->fr_layout == FR_COL)
5757 {
5758 if (fr->fr_next != NULL)
5759 break;
5760 }
5761 else
5762 {
5763 if (fr->fr_next != NULL)
5764 return TRUE;
5765 }
5766 fr = fr->fr_parent;
5767 }
5768 return FALSE;
5769}
5770# endif
5771
5772#endif /* FEAT_WINDOWS */
5773
5774#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
5775/*
5776 * Get the value to show for the language mappings, active 'keymap'.
5777 */
5778 int
5779get_keymap_str(wp, buf, len)
5780 win_T *wp;
5781 char_u *buf; /* buffer for the result */
5782 int len; /* length of buffer */
5783{
5784 char_u *p;
5785
5786 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
5787 return FALSE;
5788
5789 {
5790#ifdef FEAT_EVAL
5791 buf_T *old_curbuf = curbuf;
5792 win_T *old_curwin = curwin;
5793 char_u *s;
5794
5795 curbuf = wp->w_buffer;
5796 curwin = wp;
5797 STRCPY(buf, "b:keymap_name"); /* must be writable */
5798 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005799 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 --emsg_skip;
5801 curbuf = old_curbuf;
5802 curwin = old_curwin;
5803 if (p == NULL || *p == NUL)
5804#endif
5805 {
5806#ifdef FEAT_KEYMAP
5807 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
5808 p = wp->w_buffer->b_p_keymap;
5809 else
5810#endif
5811 p = (char_u *)"lang";
5812 }
5813 if ((int)(STRLEN(p) + 3) < len)
5814 sprintf((char *)buf, "<%s>", p);
5815 else
5816 buf[0] = NUL;
5817#ifdef FEAT_EVAL
5818 vim_free(s);
5819#endif
5820 }
5821 return buf[0] != NUL;
5822}
5823#endif
5824
5825#if defined(FEAT_STL_OPT) || defined(PROTO)
5826/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005827 * Redraw the status line or ruler of window "wp".
5828 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 */
5830 static void
Bram Moolenaar9372a112005-12-06 19:59:18 +00005831win_redr_custom(wp, draw_ruler)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 win_T *wp;
Bram Moolenaar9372a112005-12-06 19:59:18 +00005833 int draw_ruler; /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834{
5835 int attr;
5836 int curattr;
5837 int row;
5838 int col = 0;
5839 int maxwidth;
5840 int width;
5841 int n;
5842 int len;
5843 int fillchar;
5844 char_u buf[MAXPATHL];
5845 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005846 struct stl_hlrec hltab[STL_MAX_ITEM];
5847 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005848 int use_sandbox = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849
5850 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005851 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005853 /* Use 'tabline'. Always at the first line of the screen. */
5854 p = p_tal;
5855 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00005856 fillchar = ' ';
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005857 attr = hl_attr(HLF_TPF);
5858 maxwidth = Columns;
5859# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005860 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005861# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005863 else
5864 {
5865 row = W_WINROW(wp) + wp->w_height;
5866 fillchar = fillchar_status(&attr, wp == curwin);
5867 maxwidth = W_WIDTH(wp);
5868
5869 if (draw_ruler)
5870 {
5871 p = p_ruf;
5872 /* advance past any leading group spec - implicit in ru_col */
5873 if (*p == '%')
5874 {
5875 if (*++p == '-')
5876 p++;
5877 if (atoi((char *) p))
5878 while (VIM_ISDIGIT(*p))
5879 p++;
5880 if (*p++ != '(')
5881 p = p_ruf;
5882 }
5883#ifdef FEAT_VERTSPLIT
5884 col = ru_col - (Columns - W_WIDTH(wp));
5885 if (col < (W_WIDTH(wp) + 1) / 2)
5886 col = (W_WIDTH(wp) + 1) / 2;
5887#else
5888 col = ru_col;
5889 if (col > (Columns + 1) / 2)
5890 col = (Columns + 1) / 2;
5891#endif
5892 maxwidth = W_WIDTH(wp) - col;
5893#ifdef FEAT_WINDOWS
5894 if (!wp->w_status_height)
5895#endif
5896 {
5897 row = Rows - 1;
5898 --maxwidth; /* writing in last column may cause scrolling */
5899 fillchar = ' ';
5900 attr = 0;
5901 }
5902
5903# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005904 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005905# endif
5906 }
5907 else
5908 {
5909 if (*wp->w_p_stl != NUL)
5910 p = wp->w_p_stl;
5911 else
5912 p = p_stl;
5913# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005914 use_sandbox = was_set_insecurely((char_u *)"statusline",
5915 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005916# endif
5917 }
5918
5919#ifdef FEAT_VERTSPLIT
5920 col += W_WINCOL(wp);
5921#endif
5922 }
5923
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924 if (maxwidth <= 0)
5925 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005927 width = build_stl_str_hl(wp == NULL ? curwin : wp,
5928 buf, sizeof(buf),
5929 p, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005930 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005931 len = (int)STRLEN(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932
5933 while (width < maxwidth && len < sizeof(buf) - 1)
5934 {
5935#ifdef FEAT_MBYTE
5936 len += (*mb_char2bytes)(fillchar, buf + len);
5937#else
5938 buf[len++] = fillchar;
5939#endif
5940 ++width;
5941 }
5942 buf[len] = NUL;
5943
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005944 /*
5945 * Draw each snippet with the specified highlighting.
5946 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947 curattr = attr;
5948 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005949 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005951 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 screen_puts_len(p, len, row, col, curattr);
5953 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005954 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005956 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005958 else if (hltab[n].userhl < 0)
5959 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00005961 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005962 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963#endif
5964 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005965 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966 }
5967 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005968
5969 if (wp == NULL)
5970 {
5971 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
5972 col = 0;
5973 len = 0;
5974 p = buf;
5975 fillchar = 0;
5976 for (n = 0; tabtab[n].start != NULL; n++)
5977 {
5978 len += vim_strnsize(p, (int)(tabtab[n].start - p));
5979 while (col < len)
5980 TabPageIdxs[col++] = fillchar;
5981 p = tabtab[n].start;
5982 fillchar = tabtab[n].userhl;
5983 }
5984 while (col < Columns)
5985 TabPageIdxs[col++] = fillchar;
5986 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987}
5988
5989#endif /* FEAT_STL_OPT */
5990
5991/*
5992 * Output a single character directly to the screen and update ScreenLines.
5993 */
5994 void
5995screen_putchar(c, row, col, attr)
5996 int c;
5997 int row, col;
5998 int attr;
5999{
6000#ifdef FEAT_MBYTE
6001 char_u buf[MB_MAXBYTES + 1];
6002
6003 buf[(*mb_char2bytes)(c, buf)] = NUL;
6004#else
6005 char_u buf[2];
6006
6007 buf[0] = c;
6008 buf[1] = NUL;
6009#endif
6010 screen_puts(buf, row, col, attr);
6011}
6012
6013/*
6014 * Get a single character directly from ScreenLines into "bytes[]".
6015 * Also return its attribute in *attrp;
6016 */
6017 void
6018screen_getbytes(row, col, bytes, attrp)
6019 int row, col;
6020 char_u *bytes;
6021 int *attrp;
6022{
6023 unsigned off;
6024
6025 /* safety check */
6026 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
6027 {
6028 off = LineOffset[row] + col;
6029 *attrp = ScreenAttrs[off];
6030 bytes[0] = ScreenLines[off];
6031 bytes[1] = NUL;
6032
6033#ifdef FEAT_MBYTE
6034 if (enc_utf8 && ScreenLinesUC[off] != 0)
6035 bytes[utfc_char2bytes(off, bytes)] = NUL;
6036 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6037 {
6038 bytes[0] = ScreenLines[off];
6039 bytes[1] = ScreenLines2[off];
6040 bytes[2] = NUL;
6041 }
6042 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
6043 {
6044 bytes[1] = ScreenLines[off + 1];
6045 bytes[2] = NUL;
6046 }
6047#endif
6048 }
6049}
6050
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006051#ifdef FEAT_MBYTE
6052static int screen_comp_differs __ARGS((int, int*));
6053
6054/*
6055 * Return TRUE if composing characters for screen posn "off" differs from
6056 * composing characters in "u8cc".
6057 */
6058 static int
6059screen_comp_differs(off, u8cc)
6060 int off;
6061 int *u8cc;
6062{
6063 int i;
6064
6065 for (i = 0; i < Screen_mco; ++i)
6066 {
6067 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
6068 return TRUE;
6069 if (u8cc[i] == 0)
6070 break;
6071 }
6072 return FALSE;
6073}
6074#endif
6075
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076/*
6077 * Put string '*text' on the screen at position 'row' and 'col', with
6078 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
6079 * Note: only outputs within one row, message is truncated at screen boundary!
6080 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
6081 */
6082 void
6083screen_puts(text, row, col, attr)
6084 char_u *text;
6085 int row;
6086 int col;
6087 int attr;
6088{
6089 screen_puts_len(text, -1, row, col, attr);
6090}
6091
6092/*
6093 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
6094 * a NUL.
6095 */
6096 void
6097screen_puts_len(text, len, row, col, attr)
6098 char_u *text;
6099 int len;
6100 int row;
6101 int col;
6102 int attr;
6103{
6104 unsigned off;
6105 char_u *ptr = text;
6106 int c;
6107#ifdef FEAT_MBYTE
6108 int mbyte_blen = 1;
6109 int mbyte_cells = 1;
6110 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006111 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 int clear_next_cell = FALSE;
6113# ifdef FEAT_ARABIC
6114 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006115 int pc, nc, nc1;
6116 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117# endif
6118#endif
6119
6120 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
6121 return;
6122
6123 off = LineOffset[row] + col;
6124 while (*ptr != NUL && col < screen_Columns
6125 && (len < 0 || (int)(ptr - text) < len))
6126 {
6127 c = *ptr;
6128#ifdef FEAT_MBYTE
6129 /* check if this is the first byte of a multibyte */
6130 if (has_mbyte)
6131 {
6132 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006133 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006135 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6137 mbyte_cells = 1;
6138 else if (enc_dbcs != 0)
6139 mbyte_cells = mbyte_blen;
6140 else /* enc_utf8 */
6141 {
6142 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006143 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 (int)((text + len) - ptr));
6145 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006146 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147 mbyte_cells = utf_char2cells(u8c);
6148 /* Non-BMP character: display as ? or fullwidth ?. */
6149 if (u8c >= 0x10000)
6150 {
6151 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
6152 if (attr == 0)
6153 attr = hl_attr(HLF_8);
6154 }
6155# ifdef FEAT_ARABIC
6156 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
6157 {
6158 /* Do Arabic shaping. */
6159 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
6160 {
6161 /* Past end of string to be displayed. */
6162 nc = NUL;
6163 nc1 = NUL;
6164 }
6165 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006166 {
6167 nc = utfc_ptr2char(ptr + mbyte_blen, pcc);
6168 nc1 = pcc[0];
6169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170 pc = prev_c;
6171 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006172 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 }
6174 else
6175 prev_c = u8c;
6176# endif
6177 }
6178 }
6179#endif
6180
6181 if (ScreenLines[off] != c
6182#ifdef FEAT_MBYTE
6183 || (mbyte_cells == 2
6184 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
6185 || (enc_dbcs == DBCS_JPNU
6186 && c == 0x8e
6187 && ScreenLines2[off] != ptr[1])
6188 || (enc_utf8
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006189 && (ScreenLinesUC[off] != (u8char_T)u8c
6190 || screen_comp_differs(off, u8cc)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191#endif
6192 || ScreenAttrs[off] != attr
6193 || exmode_active
6194 )
6195 {
6196#if defined(FEAT_GUI) || defined(UNIX)
6197 /* The bold trick makes a single row of pixels appear in the next
6198 * character. When a bold character is removed, the next
6199 * character should be redrawn too. This happens for our own GUI
6200 * and for some xterms.
6201 * Force the redraw by setting the attribute to a different value
6202 * than "attr", the contents of ScreenLines[] may be needed by
6203 * mb_off2cells() further on.
6204 * Don't do this for the last drawn character, because the next
6205 * character may not be redrawn. */
6206 if (
6207# ifdef FEAT_GUI
6208 gui.in_use
6209# endif
6210# if defined(FEAT_GUI) && defined(UNIX)
6211 ||
6212# endif
6213# ifdef UNIX
6214 term_is_xterm
6215# endif
6216 )
6217 {
6218 int n;
6219
6220 n = ScreenAttrs[off];
6221# ifdef FEAT_MBYTE
6222 if (col + mbyte_cells < screen_Columns
6223 && (n > HL_ALL || (n & HL_BOLD))
6224 && (len < 0 ? ptr[mbyte_blen] != NUL
6225 : ptr + mbyte_blen < text + len))
6226 ScreenAttrs[off + mbyte_cells] = attr + 1;
6227# else
6228 if (col + 1 < screen_Columns
6229 && (n > HL_ALL || (n & HL_BOLD))
6230 && (len < 0 ? ptr[1] != NUL : ptr + 1 < text + len))
6231 ScreenLines[off + 1] = 0;
6232# endif
6233 }
6234#endif
6235#ifdef FEAT_MBYTE
6236 /* When at the end of the text and overwriting a two-cell
6237 * character with a one-cell character, need to clear the next
6238 * cell. Also when overwriting the left halve of a two-cell char
6239 * with the right halve of a two-cell char. Do this only once
6240 * (mb_off2cells() may return 2 on the right halve). */
6241 if (clear_next_cell)
6242 clear_next_cell = FALSE;
6243 else if (has_mbyte
6244 && (len < 0 ? ptr[mbyte_blen] == NUL
6245 : ptr + mbyte_blen >= text + len)
6246 && ((mbyte_cells == 1 && (*mb_off2cells)(off) > 1)
6247 || (mbyte_cells == 2
6248 && (*mb_off2cells)(off) == 1
6249 && (*mb_off2cells)(off + 1) > 1)))
6250 clear_next_cell = TRUE;
6251
6252 /* Make sure we never leave a second byte of a double-byte behind,
6253 * it confuses mb_off2cells(). */
6254 if (enc_dbcs
6255 && ((mbyte_cells == 1 && (*mb_off2cells)(off) > 1)
6256 || (mbyte_cells == 2
6257 && (*mb_off2cells)(off) == 1
6258 && (*mb_off2cells)(off + 1) > 1)))
6259 ScreenLines[off + mbyte_blen] = 0;
6260#endif
6261 ScreenLines[off] = c;
6262 ScreenAttrs[off] = attr;
6263#ifdef FEAT_MBYTE
6264 if (enc_utf8)
6265 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006266 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 ScreenLinesUC[off] = 0;
6268 else
6269 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006270 int i;
6271
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006273 for (i = 0; i < Screen_mco; ++i)
6274 {
6275 ScreenLinesC[i][off] = u8cc[i];
6276 if (u8cc[i] == 0)
6277 break;
6278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 }
6280 if (mbyte_cells == 2)
6281 {
6282 ScreenLines[off + 1] = 0;
6283 ScreenAttrs[off + 1] = attr;
6284 }
6285 screen_char(off, row, col);
6286 }
6287 else if (mbyte_cells == 2)
6288 {
6289 ScreenLines[off + 1] = ptr[1];
6290 ScreenAttrs[off + 1] = attr;
6291 screen_char_2(off, row, col);
6292 }
6293 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6294 {
6295 ScreenLines2[off] = ptr[1];
6296 screen_char(off, row, col);
6297 }
6298 else
6299#endif
6300 screen_char(off, row, col);
6301 }
6302#ifdef FEAT_MBYTE
6303 if (has_mbyte)
6304 {
6305 off += mbyte_cells;
6306 col += mbyte_cells;
6307 ptr += mbyte_blen;
6308 if (clear_next_cell)
6309 ptr = (char_u *)" ";
6310 }
6311 else
6312#endif
6313 {
6314 ++off;
6315 ++col;
6316 ++ptr;
6317 }
6318 }
6319}
6320
6321#ifdef FEAT_SEARCH_EXTRA
6322/*
6323 * Prepare for 'searchhl' highlighting.
6324 */
6325 static void
6326start_search_hl()
6327{
6328 if (p_hls && !no_hlsearch)
6329 {
6330 last_pat_prog(&search_hl.rm);
6331 search_hl.attr = hl_attr(HLF_L);
6332 }
6333}
6334
6335/*
6336 * Clean up for 'searchhl' highlighting.
6337 */
6338 static void
6339end_search_hl()
6340{
6341 if (search_hl.rm.regprog != NULL)
6342 {
6343 vim_free(search_hl.rm.regprog);
6344 search_hl.rm.regprog = NULL;
6345 }
6346}
6347
6348/*
6349 * Advance to the match in window "wp" line "lnum" or past it.
6350 */
6351 static void
6352prepare_search_hl(wp, lnum)
6353 win_T *wp;
6354 linenr_T lnum;
6355{
6356 match_T *shl; /* points to search_hl or match_hl */
6357 int n;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00006358 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359
6360 /*
6361 * When using a multi-line pattern, start searching at the top
6362 * of the window or just after a closed fold.
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00006363 * Do this both for search_hl and match_hl[3].
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00006365 for (i = 3; i >= 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366 {
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00006367 shl = (i == 3) ? &search_hl : &match_hl[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368 if (shl->rm.regprog != NULL
6369 && shl->lnum == 0
6370 && re_multiline(shl->rm.regprog))
6371 {
6372 if (shl->first_lnum == 0)
6373 {
6374# ifdef FEAT_FOLDING
6375 for (shl->first_lnum = lnum;
6376 shl->first_lnum > wp->w_topline; --shl->first_lnum)
6377 if (hasFoldingWin(wp, shl->first_lnum - 1,
6378 NULL, NULL, TRUE, NULL))
6379 break;
6380# else
6381 shl->first_lnum = wp->w_topline;
6382# endif
6383 }
6384 n = 0;
6385 while (shl->first_lnum < lnum && shl->rm.regprog != NULL)
6386 {
6387 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n);
6388 if (shl->lnum != 0)
6389 {
6390 shl->first_lnum = shl->lnum
6391 + shl->rm.endpos[0].lnum
6392 - shl->rm.startpos[0].lnum;
6393 n = shl->rm.endpos[0].col;
6394 }
6395 else
6396 {
6397 ++shl->first_lnum;
6398 n = 0;
6399 }
6400 }
6401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402 }
6403}
6404
6405/*
6406 * Search for a next 'searchl' or ":match" match.
6407 * Uses shl->buf.
6408 * Sets shl->lnum and shl->rm contents.
6409 * Note: Assumes a previous match is always before "lnum", unless
6410 * shl->lnum is zero.
6411 * Careful: Any pointers for buffer lines will become invalid.
6412 */
6413 static void
6414next_search_hl(win, shl, lnum, mincol)
6415 win_T *win;
6416 match_T *shl; /* points to search_hl or match_hl */
6417 linenr_T lnum;
6418 colnr_T mincol; /* minimal column for a match */
6419{
6420 linenr_T l;
6421 colnr_T matchcol;
6422 long nmatched;
6423
6424 if (shl->lnum != 0)
6425 {
6426 /* Check for three situations:
6427 * 1. If the "lnum" is below a previous match, start a new search.
6428 * 2. If the previous match includes "mincol", use it.
6429 * 3. Continue after the previous match.
6430 */
6431 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
6432 if (lnum > l)
6433 shl->lnum = 0;
6434 else if (lnum < l || shl->rm.endpos[0].col > mincol)
6435 return;
6436 }
6437
6438 /*
6439 * Repeat searching for a match until one is found that includes "mincol"
6440 * or none is found in this line.
6441 */
6442 called_emsg = FALSE;
6443 for (;;)
6444 {
6445 /* Three situations:
6446 * 1. No useful previous match: search from start of line.
6447 * 2. Not Vi compatible or empty match: continue at next character.
6448 * Break the loop if this is beyond the end of the line.
6449 * 3. Vi compatible searching: continue at end of previous match.
6450 */
6451 if (shl->lnum == 0)
6452 matchcol = 0;
6453 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
6454 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006455 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006457 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006458
6459 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006460 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006461 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006463 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 shl->lnum = 0;
6465 break;
6466 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006467#ifdef FEAT_MBYTE
6468 if (has_mbyte)
6469 matchcol += mb_ptr2len(ml);
6470 else
6471#endif
6472 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473 }
6474 else
6475 matchcol = shl->rm.endpos[0].col;
6476
6477 shl->lnum = lnum;
6478 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum, matchcol);
6479 if (called_emsg)
6480 {
6481 /* Error while handling regexp: stop using this regexp. */
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00006482 if (shl == &search_hl)
6483 {
6484 /* don't free the regprog in match_hl[], it's a copy */
6485 vim_free(shl->rm.regprog);
6486 no_hlsearch = TRUE;
6487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488 shl->rm.regprog = NULL;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00006489 shl->lnum = 0;
6490 got_int = FALSE; /* avoid the "Type :quit to exit Vim" message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491 break;
6492 }
6493 if (nmatched == 0)
6494 {
6495 shl->lnum = 0; /* no match found */
6496 break;
6497 }
6498 if (shl->rm.startpos[0].lnum > 0
6499 || shl->rm.startpos[0].col >= mincol
6500 || nmatched > 1
6501 || shl->rm.endpos[0].col > mincol)
6502 {
6503 shl->lnum += shl->rm.startpos[0].lnum;
6504 break; /* useful match found */
6505 }
6506 }
6507}
6508#endif
6509
6510 static void
6511screen_start_highlight(attr)
6512 int attr;
6513{
6514 attrentry_T *aep = NULL;
6515
6516 screen_attr = attr;
6517 if (full_screen
6518#ifdef WIN3264
6519 && termcap_active
6520#endif
6521 )
6522 {
6523#ifdef FEAT_GUI
6524 if (gui.in_use)
6525 {
6526 char buf[20];
6527
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006528 /* The GUI handles this internally. */
6529 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530 OUT_STR(buf);
6531 }
6532 else
6533#endif
6534 {
6535 if (attr > HL_ALL) /* special HL attr. */
6536 {
6537 if (t_colors > 1)
6538 aep = syn_cterm_attr2entry(attr);
6539 else
6540 aep = syn_term_attr2entry(attr);
6541 if (aep == NULL) /* did ":syntax clear" */
6542 attr = 0;
6543 else
6544 attr = aep->ae_attr;
6545 }
6546 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
6547 out_str(T_MD);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006548 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
6549 && cterm_normal_fg_bold)
6550 /* If the Normal FG color has BOLD attribute and the new HL
6551 * has a FG color defined, clear BOLD. */
6552 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
6554 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006555 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
6556 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 out_str(T_US);
6558 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
6559 out_str(T_CZH);
6560 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
6561 out_str(T_MR);
6562
6563 /*
6564 * Output the color or start string after bold etc., in case the
6565 * bold etc. override the color setting.
6566 */
6567 if (aep != NULL)
6568 {
6569 if (t_colors > 1)
6570 {
6571 if (aep->ae_u.cterm.fg_color)
6572 term_fg_color(aep->ae_u.cterm.fg_color - 1);
6573 if (aep->ae_u.cterm.bg_color)
6574 term_bg_color(aep->ae_u.cterm.bg_color - 1);
6575 }
6576 else
6577 {
6578 if (aep->ae_u.term.start != NULL)
6579 out_str(aep->ae_u.term.start);
6580 }
6581 }
6582 }
6583 }
6584}
6585
6586 void
6587screen_stop_highlight()
6588{
6589 int do_ME = FALSE; /* output T_ME code */
6590
6591 if (screen_attr != 0
6592#ifdef WIN3264
6593 && termcap_active
6594#endif
6595 )
6596 {
6597#ifdef FEAT_GUI
6598 if (gui.in_use)
6599 {
6600 char buf[20];
6601
6602 /* use internal GUI code */
6603 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
6604 OUT_STR(buf);
6605 }
6606 else
6607#endif
6608 {
6609 if (screen_attr > HL_ALL) /* special HL attr. */
6610 {
6611 attrentry_T *aep;
6612
6613 if (t_colors > 1)
6614 {
6615 /*
6616 * Assume that t_me restores the original colors!
6617 */
6618 aep = syn_cterm_attr2entry(screen_attr);
6619 if (aep != NULL && (aep->ae_u.cterm.fg_color
6620 || aep->ae_u.cterm.bg_color))
6621 do_ME = TRUE;
6622 }
6623 else
6624 {
6625 aep = syn_term_attr2entry(screen_attr);
6626 if (aep != NULL && aep->ae_u.term.stop != NULL)
6627 {
6628 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
6629 do_ME = TRUE;
6630 else
6631 out_str(aep->ae_u.term.stop);
6632 }
6633 }
6634 if (aep == NULL) /* did ":syntax clear" */
6635 screen_attr = 0;
6636 else
6637 screen_attr = aep->ae_attr;
6638 }
6639
6640 /*
6641 * Often all ending-codes are equal to T_ME. Avoid outputting the
6642 * same sequence several times.
6643 */
6644 if (screen_attr & HL_STANDOUT)
6645 {
6646 if (STRCMP(T_SE, T_ME) == 0)
6647 do_ME = TRUE;
6648 else
6649 out_str(T_SE);
6650 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006651 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652 {
6653 if (STRCMP(T_UE, T_ME) == 0)
6654 do_ME = TRUE;
6655 else
6656 out_str(T_UE);
6657 }
6658 if (screen_attr & HL_ITALIC)
6659 {
6660 if (STRCMP(T_CZR, T_ME) == 0)
6661 do_ME = TRUE;
6662 else
6663 out_str(T_CZR);
6664 }
6665 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
6666 out_str(T_ME);
6667
6668 if (t_colors > 1)
6669 {
6670 /* set Normal cterm colors */
6671 if (cterm_normal_fg_color != 0)
6672 term_fg_color(cterm_normal_fg_color - 1);
6673 if (cterm_normal_bg_color != 0)
6674 term_bg_color(cterm_normal_bg_color - 1);
6675 if (cterm_normal_fg_bold)
6676 out_str(T_MD);
6677 }
6678 }
6679 }
6680 screen_attr = 0;
6681}
6682
6683/*
6684 * Reset the colors for a cterm. Used when leaving Vim.
6685 * The machine specific code may override this again.
6686 */
6687 void
6688reset_cterm_colors()
6689{
6690 if (t_colors > 1)
6691 {
6692 /* set Normal cterm colors */
6693 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
6694 {
6695 out_str(T_OP);
6696 screen_attr = -1;
6697 }
6698 if (cterm_normal_fg_bold)
6699 {
6700 out_str(T_ME);
6701 screen_attr = -1;
6702 }
6703 }
6704}
6705
6706/*
6707 * Put character ScreenLines["off"] on the screen at position "row" and "col",
6708 * using the attributes from ScreenAttrs["off"].
6709 */
6710 static void
6711screen_char(off, row, col)
6712 unsigned off;
6713 int row;
6714 int col;
6715{
6716 int attr;
6717
6718 /* Check for illegal values, just in case (could happen just after
6719 * resizing). */
6720 if (row >= screen_Rows || col >= screen_Columns)
6721 return;
6722
6723 /* Outputting the last character on the screen may scrollup the screen.
6724 * Don't to it! Mark the character invalid (update it when scrolled up) */
6725 if (row == screen_Rows - 1 && col == screen_Columns - 1
6726#ifdef FEAT_RIGHTLEFT
6727 /* account for first command-line character in rightleft mode */
6728 && !cmdmsg_rl
6729#endif
6730 )
6731 {
6732 ScreenAttrs[off] = (sattr_T)-1;
6733 return;
6734 }
6735
6736 /*
6737 * Stop highlighting first, so it's easier to move the cursor.
6738 */
6739#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
6740 if (screen_char_attr != 0)
6741 attr = screen_char_attr;
6742 else
6743#endif
6744 attr = ScreenAttrs[off];
6745 if (screen_attr != attr)
6746 screen_stop_highlight();
6747
6748 windgoto(row, col);
6749
6750 if (screen_attr != attr)
6751 screen_start_highlight(attr);
6752
6753#ifdef FEAT_MBYTE
6754 if (enc_utf8 && ScreenLinesUC[off] != 0)
6755 {
6756 char_u buf[MB_MAXBYTES + 1];
6757
6758 /* Convert UTF-8 character to bytes and write it. */
6759
6760 buf[utfc_char2bytes(off, buf)] = NUL;
6761
6762 out_str(buf);
6763 if (utf_char2cells(ScreenLinesUC[off]) > 1)
6764 ++screen_cur_col;
6765 }
6766 else
6767#endif
6768 {
6769#ifdef FEAT_MBYTE
6770 out_flush_check();
6771#endif
6772 out_char(ScreenLines[off]);
6773#ifdef FEAT_MBYTE
6774 /* double-byte character in single-width cell */
6775 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6776 out_char(ScreenLines2[off]);
6777#endif
6778 }
6779
6780 screen_cur_col++;
6781}
6782
6783#ifdef FEAT_MBYTE
6784
6785/*
6786 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
6787 * on the screen at position 'row' and 'col'.
6788 * The attributes of the first byte is used for all. This is required to
6789 * output the two bytes of a double-byte character with nothing in between.
6790 */
6791 static void
6792screen_char_2(off, row, col)
6793 unsigned off;
6794 int row;
6795 int col;
6796{
6797 /* Check for illegal values (could be wrong when screen was resized). */
6798 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
6799 return;
6800
6801 /* Outputting the last character on the screen may scrollup the screen.
6802 * Don't to it! Mark the character invalid (update it when scrolled up) */
6803 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
6804 {
6805 ScreenAttrs[off] = (sattr_T)-1;
6806 return;
6807 }
6808
6809 /* Output the first byte normally (positions the cursor), then write the
6810 * second byte directly. */
6811 screen_char(off, row, col);
6812 out_char(ScreenLines[off + 1]);
6813 ++screen_cur_col;
6814}
6815#endif
6816
6817#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
6818/*
6819 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
6820 * This uses the contents of ScreenLines[] and doesn't change it.
6821 */
6822 void
6823screen_draw_rectangle(row, col, height, width, invert)
6824 int row;
6825 int col;
6826 int height;
6827 int width;
6828 int invert;
6829{
6830 int r, c;
6831 int off;
6832
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006833 /* Can't use ScreenLines unless initialized */
6834 if (ScreenLines == NULL)
6835 return;
6836
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837 if (invert)
6838 screen_char_attr = HL_INVERSE;
6839 for (r = row; r < row + height; ++r)
6840 {
6841 off = LineOffset[r];
6842 for (c = col; c < col + width; ++c)
6843 {
6844#ifdef FEAT_MBYTE
6845 if (enc_dbcs != 0 && dbcs_off2cells(off + c) > 1)
6846 {
6847 screen_char_2(off + c, r, c);
6848 ++c;
6849 }
6850 else
6851#endif
6852 {
6853 screen_char(off + c, r, c);
6854#ifdef FEAT_MBYTE
6855 if (utf_off2cells(off + c) > 1)
6856 ++c;
6857#endif
6858 }
6859 }
6860 }
6861 screen_char_attr = 0;
6862}
6863#endif
6864
6865#ifdef FEAT_VERTSPLIT
6866/*
6867 * Redraw the characters for a vertically split window.
6868 */
6869 static void
6870redraw_block(row, end, wp)
6871 int row;
6872 int end;
6873 win_T *wp;
6874{
6875 int col;
6876 int width;
6877
6878# ifdef FEAT_CLIPBOARD
6879 clip_may_clear_selection(row, end - 1);
6880# endif
6881
6882 if (wp == NULL)
6883 {
6884 col = 0;
6885 width = Columns;
6886 }
6887 else
6888 {
6889 col = wp->w_wincol;
6890 width = wp->w_width;
6891 }
6892 screen_draw_rectangle(row, col, end - row, width, FALSE);
6893}
6894#endif
6895
6896/*
6897 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
6898 * with character 'c1' in first column followed by 'c2' in the other columns.
6899 * Use attributes 'attr'.
6900 */
6901 void
6902screen_fill(start_row, end_row, start_col, end_col, c1, c2, attr)
6903 int start_row, end_row;
6904 int start_col, end_col;
6905 int c1, c2;
6906 int attr;
6907{
6908 int row;
6909 int col;
6910 int off;
6911 int end_off;
6912 int did_delete;
6913 int c;
6914 int norm_term;
6915#if defined(FEAT_GUI) || defined(UNIX)
6916 int force_next = FALSE;
6917#endif
6918
6919 if (end_row > screen_Rows) /* safety check */
6920 end_row = screen_Rows;
6921 if (end_col > screen_Columns) /* safety check */
6922 end_col = screen_Columns;
6923 if (ScreenLines == NULL
6924 || start_row >= end_row
6925 || start_col >= end_col) /* nothing to do */
6926 return;
6927
6928 /* it's a "normal" terminal when not in a GUI or cterm */
6929 norm_term = (
6930#ifdef FEAT_GUI
6931 !gui.in_use &&
6932#endif
6933 t_colors <= 1);
6934 for (row = start_row; row < end_row; ++row)
6935 {
6936 /*
6937 * Try to use delete-line termcap code, when no attributes or in a
6938 * "normal" terminal, where a bold/italic space is just a
6939 * space.
6940 */
6941 did_delete = FALSE;
6942 if (c2 == ' '
6943 && end_col == Columns
6944 && can_clear(T_CE)
6945 && (attr == 0
6946 || (norm_term
6947 && attr <= HL_ALL
6948 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
6949 {
6950 /*
6951 * check if we really need to clear something
6952 */
6953 col = start_col;
6954 if (c1 != ' ') /* don't clear first char */
6955 ++col;
6956
6957 off = LineOffset[row] + col;
6958 end_off = LineOffset[row] + end_col;
6959
6960 /* skip blanks (used often, keep it fast!) */
6961#ifdef FEAT_MBYTE
6962 if (enc_utf8)
6963 while (off < end_off && ScreenLines[off] == ' '
6964 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
6965 ++off;
6966 else
6967#endif
6968 while (off < end_off && ScreenLines[off] == ' '
6969 && ScreenAttrs[off] == 0)
6970 ++off;
6971 if (off < end_off) /* something to be cleared */
6972 {
6973 col = off - LineOffset[row];
6974 screen_stop_highlight();
6975 term_windgoto(row, col);/* clear rest of this screen line */
6976 out_str(T_CE);
6977 screen_start(); /* don't know where cursor is now */
6978 col = end_col - col;
6979 while (col--) /* clear chars in ScreenLines */
6980 {
6981 ScreenLines[off] = ' ';
6982#ifdef FEAT_MBYTE
6983 if (enc_utf8)
6984 ScreenLinesUC[off] = 0;
6985#endif
6986 ScreenAttrs[off] = 0;
6987 ++off;
6988 }
6989 }
6990 did_delete = TRUE; /* the chars are cleared now */
6991 }
6992
6993 off = LineOffset[row] + start_col;
6994 c = c1;
6995 for (col = start_col; col < end_col; ++col)
6996 {
6997 if (ScreenLines[off] != c
6998#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006999 || (enc_utf8 && (int)ScreenLinesUC[off]
7000 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001#endif
7002 || ScreenAttrs[off] != attr
7003#if defined(FEAT_GUI) || defined(UNIX)
7004 || force_next
7005#endif
7006 )
7007 {
7008#if defined(FEAT_GUI) || defined(UNIX)
7009 /* The bold trick may make a single row of pixels appear in
7010 * the next character. When a bold character is removed, the
7011 * next character should be redrawn too. This happens for our
7012 * own GUI and for some xterms. */
7013 if (
7014# ifdef FEAT_GUI
7015 gui.in_use
7016# endif
7017# if defined(FEAT_GUI) && defined(UNIX)
7018 ||
7019# endif
7020# ifdef UNIX
7021 term_is_xterm
7022# endif
7023 )
7024 {
7025 if (ScreenLines[off] != ' '
7026 && (ScreenAttrs[off] > HL_ALL
7027 || ScreenAttrs[off] & HL_BOLD))
7028 force_next = TRUE;
7029 else
7030 force_next = FALSE;
7031 }
7032#endif
7033 ScreenLines[off] = c;
7034#ifdef FEAT_MBYTE
7035 if (enc_utf8)
7036 {
7037 if (c >= 0x80)
7038 {
7039 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007040 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041 }
7042 else
7043 ScreenLinesUC[off] = 0;
7044 }
7045#endif
7046 ScreenAttrs[off] = attr;
7047 if (!did_delete || c != ' ')
7048 screen_char(off, row, col);
7049 }
7050 ++off;
7051 if (col == start_col)
7052 {
7053 if (did_delete)
7054 break;
7055 c = c2;
7056 }
7057 }
7058 if (end_col == Columns)
7059 LineWraps[row] = FALSE;
7060 if (row == Rows - 1) /* overwritten the command line */
7061 {
7062 redraw_cmdline = TRUE;
7063 if (c1 == ' ' && c2 == ' ')
7064 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007065 if (start_col == 0)
7066 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 }
7068 }
7069}
7070
7071/*
7072 * Check if there should be a delay. Used before clearing or redrawing the
7073 * screen or the command line.
7074 */
7075 void
7076check_for_delay(check_msg_scroll)
7077 int check_msg_scroll;
7078{
7079 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
7080 && !did_wait_return
7081 && emsg_silent == 0)
7082 {
7083 out_flush();
7084 ui_delay(1000L, TRUE);
7085 emsg_on_display = FALSE;
7086 if (check_msg_scroll)
7087 msg_scroll = FALSE;
7088 }
7089}
7090
7091/*
7092 * screen_valid - allocate screen buffers if size changed
7093 * If "clear" is TRUE: clear screen if it has been resized.
7094 * Returns TRUE if there is a valid screen to write to.
7095 * Returns FALSE when starting up and screen not initialized yet.
7096 */
7097 int
7098screen_valid(clear)
7099 int clear;
7100{
7101 screenalloc(clear); /* allocate screen buffers if size changed */
7102 return (ScreenLines != NULL);
7103}
7104
7105/*
7106 * Resize the shell to Rows and Columns.
7107 * Allocate ScreenLines[] and associated items.
7108 *
7109 * There may be some time between setting Rows and Columns and (re)allocating
7110 * ScreenLines[]. This happens when starting up and when (manually) changing
7111 * the shell size. Always use screen_Rows and screen_Columns to access items
7112 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
7113 * final size of the shell is needed.
7114 */
7115 void
7116screenalloc(clear)
7117 int clear;
7118{
7119 int new_row, old_row;
7120#ifdef FEAT_GUI
7121 int old_Rows;
7122#endif
7123 win_T *wp;
7124 int outofmem = FALSE;
7125 int len;
7126 schar_T *new_ScreenLines;
7127#ifdef FEAT_MBYTE
7128 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007129 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007131 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132#endif
7133 sattr_T *new_ScreenAttrs;
7134 unsigned *new_LineOffset;
7135 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007136#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007137 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007138 tabpage_T *tp;
7139#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00007141 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142
7143 /*
7144 * Allocation of the screen buffers is done only when the size changes and
7145 * when Rows and Columns have been set and we have started doing full
7146 * screen stuff.
7147 */
7148 if ((ScreenLines != NULL
7149 && Rows == screen_Rows
7150 && Columns == screen_Columns
7151#ifdef FEAT_MBYTE
7152 && enc_utf8 == (ScreenLinesUC != NULL)
7153 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007154 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155#endif
7156 )
7157 || Rows == 0
7158 || Columns == 0
7159 || (!full_screen && ScreenLines == NULL))
7160 return;
7161
7162 /*
7163 * It's possible that we produce an out-of-memory message below, which
7164 * will cause this function to be called again. To break the loop, just
7165 * return here.
7166 */
7167 if (entered)
7168 return;
7169 entered = TRUE;
7170
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00007171 /*
7172 * Note that the window sizes are updated before reallocating the arrays,
7173 * thus we must not redraw here!
7174 */
7175 ++RedrawingDisabled;
7176
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 win_new_shellsize(); /* fit the windows in the new sized shell */
7178
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 comp_col(); /* recompute columns for shown command and ruler */
7180
7181 /*
7182 * We're changing the size of the screen.
7183 * - Allocate new arrays for ScreenLines and ScreenAttrs.
7184 * - Move lines from the old arrays into the new arrays, clear extra
7185 * lines (unless the screen is going to be cleared).
7186 * - Free the old arrays.
7187 *
7188 * If anything fails, make ScreenLines NULL, so we don't do anything!
7189 * Continuing with the old ScreenLines may result in a crash, because the
7190 * size is wrong.
7191 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007192 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 win_free_lsize(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194
7195 new_ScreenLines = (schar_T *)lalloc((long_u)(
7196 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7197#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007198 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 if (enc_utf8)
7200 {
7201 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
7202 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007203 for (i = 0; i < p_mco; ++i)
7204 new_ScreenLinesC[i] = (u8char_T *)lalloc((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
7206 }
7207 if (enc_dbcs == DBCS_JPNU)
7208 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
7209 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7210#endif
7211 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
7212 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
7213 new_LineOffset = (unsigned *)lalloc((long_u)(
7214 Rows * sizeof(unsigned)), FALSE);
7215 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007216#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007217 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007218#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007220 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 {
7222 if (win_alloc_lines(wp) == FAIL)
7223 {
7224 outofmem = TRUE;
7225#ifdef FEAT_WINDOWS
7226 break;
7227#endif
7228 }
7229 }
7230
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007231#ifdef FEAT_MBYTE
7232 for (i = 0; i < p_mco; ++i)
7233 if (new_ScreenLinesC[i] == NULL)
7234 break;
7235#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236 if (new_ScreenLines == NULL
7237#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007238 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
7240#endif
7241 || new_ScreenAttrs == NULL
7242 || new_LineOffset == NULL
7243 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00007244#ifdef FEAT_WINDOWS
7245 || new_TabPageIdxs == NULL
7246#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 || outofmem)
7248 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00007249 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007250 {
7251 /* guess the size */
7252 do_outofmem_msg((long_u)((Rows + 1) * Columns));
7253
7254 /* Remember we did this to avoid getting outofmem messages over
7255 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00007256 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 vim_free(new_ScreenLines);
7259 new_ScreenLines = NULL;
7260#ifdef FEAT_MBYTE
7261 vim_free(new_ScreenLinesUC);
7262 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007263 for (i = 0; i < p_mco; ++i)
7264 {
7265 vim_free(new_ScreenLinesC[i]);
7266 new_ScreenLinesC[i] = NULL;
7267 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268 vim_free(new_ScreenLines2);
7269 new_ScreenLines2 = NULL;
7270#endif
7271 vim_free(new_ScreenAttrs);
7272 new_ScreenAttrs = NULL;
7273 vim_free(new_LineOffset);
7274 new_LineOffset = NULL;
7275 vim_free(new_LineWraps);
7276 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007277#ifdef FEAT_WINDOWS
7278 vim_free(new_TabPageIdxs);
7279 new_TabPageIdxs = NULL;
7280#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 }
7282 else
7283 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00007284 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007285
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286 for (new_row = 0; new_row < Rows; ++new_row)
7287 {
7288 new_LineOffset[new_row] = new_row * Columns;
7289 new_LineWraps[new_row] = FALSE;
7290
7291 /*
7292 * If the screen is not going to be cleared, copy as much as
7293 * possible from the old screen to the new one and clear the rest
7294 * (used when resizing the window at the "--more--" prompt or when
7295 * executing an external command, for the GUI).
7296 */
7297 if (!clear)
7298 {
7299 (void)vim_memset(new_ScreenLines + new_row * Columns,
7300 ' ', (size_t)Columns * sizeof(schar_T));
7301#ifdef FEAT_MBYTE
7302 if (enc_utf8)
7303 {
7304 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
7305 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007306 for (i = 0; i < p_mco; ++i)
7307 (void)vim_memset(new_ScreenLinesC[i]
7308 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309 0, (size_t)Columns * sizeof(u8char_T));
7310 }
7311 if (enc_dbcs == DBCS_JPNU)
7312 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
7313 0, (size_t)Columns * sizeof(schar_T));
7314#endif
7315 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
7316 0, (size_t)Columns * sizeof(sattr_T));
7317 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007318 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319 {
7320 if (screen_Columns < Columns)
7321 len = screen_Columns;
7322 else
7323 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007324#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00007325 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007326 * may be invalid now. Also when p_mco changes. */
7327 if (!(enc_utf8 && ScreenLinesUC == NULL)
7328 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007329#endif
7330 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
7331 ScreenLines + LineOffset[old_row],
7332 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007334 if (enc_utf8 && ScreenLinesUC != NULL
7335 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336 {
7337 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
7338 ScreenLinesUC + LineOffset[old_row],
7339 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007340 for (i = 0; i < p_mco; ++i)
7341 mch_memmove(new_ScreenLinesC[i]
7342 + new_LineOffset[new_row],
7343 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344 (size_t)len * sizeof(u8char_T));
7345 }
7346 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
7347 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
7348 ScreenLines2 + LineOffset[old_row],
7349 (size_t)len * sizeof(schar_T));
7350#endif
7351 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
7352 ScreenAttrs + LineOffset[old_row],
7353 (size_t)len * sizeof(sattr_T));
7354 }
7355 }
7356 }
7357 /* Use the last line of the screen for the current line. */
7358 current_ScreenLine = new_ScreenLines + Rows * Columns;
7359 }
7360
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007361 free_screenlines();
7362
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363 ScreenLines = new_ScreenLines;
7364#ifdef FEAT_MBYTE
7365 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007366 for (i = 0; i < p_mco; ++i)
7367 ScreenLinesC[i] = new_ScreenLinesC[i];
7368 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369 ScreenLines2 = new_ScreenLines2;
7370#endif
7371 ScreenAttrs = new_ScreenAttrs;
7372 LineOffset = new_LineOffset;
7373 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007374#ifdef FEAT_WINDOWS
7375 TabPageIdxs = new_TabPageIdxs;
7376#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377
7378 /* It's important that screen_Rows and screen_Columns reflect the actual
7379 * size of ScreenLines[]. Set them before calling anything. */
7380#ifdef FEAT_GUI
7381 old_Rows = screen_Rows;
7382#endif
7383 screen_Rows = Rows;
7384 screen_Columns = Columns;
7385
7386 must_redraw = CLEAR; /* need to clear the screen later */
7387 if (clear)
7388 screenclear2();
7389
7390#ifdef FEAT_GUI
7391 else if (gui.in_use
7392 && !gui.starting
7393 && ScreenLines != NULL
7394 && old_Rows != Rows)
7395 {
7396 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
7397 /*
7398 * Adjust the position of the cursor, for when executing an external
7399 * command.
7400 */
7401 if (msg_row >= Rows) /* Rows got smaller */
7402 msg_row = Rows - 1; /* put cursor at last row */
7403 else if (Rows > old_Rows) /* Rows got bigger */
7404 msg_row += Rows - old_Rows; /* put cursor in same place */
7405 if (msg_col >= Columns) /* Columns got smaller */
7406 msg_col = Columns - 1; /* put cursor at last column */
7407 }
7408#endif
7409
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00007411 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00007412
7413#ifdef FEAT_AUTOCMD
7414 if (starting == 0)
7415 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
7416#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417}
7418
7419 void
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007420free_screenlines()
7421{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007422#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007423 int i;
7424
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007425 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007426 for (i = 0; i < Screen_mco; ++i)
7427 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007428 vim_free(ScreenLines2);
7429#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007430 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007431 vim_free(ScreenAttrs);
7432 vim_free(LineOffset);
7433 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007434#ifdef FEAT_WINDOWS
7435 vim_free(TabPageIdxs);
7436#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007437}
7438
7439 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440screenclear()
7441{
7442 check_for_delay(FALSE);
7443 screenalloc(FALSE); /* allocate screen buffers if size changed */
7444 screenclear2(); /* clear the screen */
7445}
7446
7447 static void
7448screenclear2()
7449{
7450 int i;
7451
7452 if (starting == NO_SCREEN || ScreenLines == NULL
7453#ifdef FEAT_GUI
7454 || (gui.in_use && gui.starting)
7455#endif
7456 )
7457 return;
7458
7459#ifdef FEAT_GUI
7460 if (!gui.in_use)
7461#endif
7462 screen_attr = -1; /* force setting the Normal colors */
7463 screen_stop_highlight(); /* don't want highlighting here */
7464
7465#ifdef FEAT_CLIPBOARD
7466 /* disable selection without redrawing it */
7467 clip_scroll_selection(9999);
7468#endif
7469
7470 /* blank out ScreenLines */
7471 for (i = 0; i < Rows; ++i)
7472 {
7473 lineclear(LineOffset[i], (int)Columns);
7474 LineWraps[i] = FALSE;
7475 }
7476
7477 if (can_clear(T_CL))
7478 {
7479 out_str(T_CL); /* clear the display */
7480 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007481 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482 }
7483 else
7484 {
7485 /* can't clear the screen, mark all chars with invalid attributes */
7486 for (i = 0; i < Rows; ++i)
7487 lineinvalid(LineOffset[i], (int)Columns);
7488 clear_cmdline = TRUE;
7489 }
7490
7491 screen_cleared = TRUE; /* can use contents of ScreenLines now */
7492
7493 win_rest_invalid(firstwin);
7494 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00007495#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00007496 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00007497#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 if (must_redraw == CLEAR) /* no need to clear again */
7499 must_redraw = NOT_VALID;
7500 compute_cmdrow();
7501 msg_row = cmdline_row; /* put cursor on last line for messages */
7502 msg_col = 0;
7503 screen_start(); /* don't know where cursor is now */
7504 msg_scrolled = 0; /* can't scroll back */
7505 msg_didany = FALSE;
7506 msg_didout = FALSE;
7507}
7508
7509/*
7510 * Clear one line in ScreenLines.
7511 */
7512 static void
7513lineclear(off, width)
7514 unsigned off;
7515 int width;
7516{
7517 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
7518#ifdef FEAT_MBYTE
7519 if (enc_utf8)
7520 (void)vim_memset(ScreenLinesUC + off, 0,
7521 (size_t)width * sizeof(u8char_T));
7522#endif
7523 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
7524}
7525
7526/*
7527 * Mark one line in ScreenLines invalid by setting the attributes to an
7528 * invalid value.
7529 */
7530 static void
7531lineinvalid(off, width)
7532 unsigned off;
7533 int width;
7534{
7535 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
7536}
7537
7538#ifdef FEAT_VERTSPLIT
7539/*
7540 * Copy part of a Screenline for vertically split window "wp".
7541 */
7542 static void
7543linecopy(to, from, wp)
7544 int to;
7545 int from;
7546 win_T *wp;
7547{
7548 unsigned off_to = LineOffset[to] + wp->w_wincol;
7549 unsigned off_from = LineOffset[from] + wp->w_wincol;
7550
7551 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
7552 wp->w_width * sizeof(schar_T));
7553# ifdef FEAT_MBYTE
7554 if (enc_utf8)
7555 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007556 int i;
7557
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
7559 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007560 for (i = 0; i < p_mco; ++i)
7561 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
7562 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563 }
7564 if (enc_dbcs == DBCS_JPNU)
7565 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
7566 wp->w_width * sizeof(schar_T));
7567# endif
7568 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
7569 wp->w_width * sizeof(sattr_T));
7570}
7571#endif
7572
7573/*
7574 * Return TRUE if clearing with term string "p" would work.
7575 * It can't work when the string is empty or it won't set the right background.
7576 */
7577 int
7578can_clear(p)
7579 char_u *p;
7580{
7581 return (*p != NUL && (t_colors <= 1
7582#ifdef FEAT_GUI
7583 || gui.in_use
7584#endif
7585 || cterm_normal_bg_color == 0 || *T_UT != NUL));
7586}
7587
7588/*
7589 * Reset cursor position. Use whenever cursor was moved because of outputting
7590 * something directly to the screen (shell commands) or a terminal control
7591 * code.
7592 */
7593 void
7594screen_start()
7595{
7596 screen_cur_row = screen_cur_col = 9999;
7597}
7598
7599/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600 * Move the cursor to position "row","col" in the screen.
7601 * This tries to find the most efficient way to move, minimizing the number of
7602 * characters sent to the terminal.
7603 */
7604 void
7605windgoto(row, col)
7606 int row;
7607 int col;
7608{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007609 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610 int i;
7611 int plan;
7612 int cost;
7613 int wouldbe_col;
7614 int noinvcurs;
7615 char_u *bs;
7616 int goto_cost;
7617 int attr;
7618
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007619#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
7621
7622#define PLAN_LE 1
7623#define PLAN_CR 2
7624#define PLAN_NL 3
7625#define PLAN_WRITE 4
7626 /* Can't use ScreenLines unless initialized */
7627 if (ScreenLines == NULL)
7628 return;
7629
7630 if (col != screen_cur_col || row != screen_cur_row)
7631 {
7632 /* Check for valid position. */
7633 if (row < 0) /* window without text lines? */
7634 row = 0;
7635 if (row >= screen_Rows)
7636 row = screen_Rows - 1;
7637 if (col >= screen_Columns)
7638 col = screen_Columns - 1;
7639
7640 /* check if no cursor movement is allowed in highlight mode */
7641 if (screen_attr && *T_MS == NUL)
7642 noinvcurs = HIGHL_COST;
7643 else
7644 noinvcurs = 0;
7645 goto_cost = GOTO_COST + noinvcurs;
7646
7647 /*
7648 * Plan how to do the positioning:
7649 * 1. Use CR to move it to column 0, same row.
7650 * 2. Use T_LE to move it a few columns to the left.
7651 * 3. Use NL to move a few lines down, column 0.
7652 * 4. Move a few columns to the right with T_ND or by writing chars.
7653 *
7654 * Don't do this if the cursor went beyond the last column, the cursor
7655 * position is unknown then (some terminals wrap, some don't )
7656 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007657 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 * characters to move the cursor to the right.
7659 */
7660 if (row >= screen_cur_row && screen_cur_col < Columns)
7661 {
7662 /*
7663 * If the cursor is in the same row, bigger col, we can use CR
7664 * or T_LE.
7665 */
7666 bs = NULL; /* init for GCC */
7667 attr = screen_attr;
7668 if (row == screen_cur_row && col < screen_cur_col)
7669 {
7670 /* "le" is preferred over "bc", because "bc" is obsolete */
7671 if (*T_LE)
7672 bs = T_LE; /* "cursor left" */
7673 else
7674 bs = T_BC; /* "backspace character (old) */
7675 if (*bs)
7676 cost = (screen_cur_col - col) * (int)STRLEN(bs);
7677 else
7678 cost = 999;
7679 if (col + 1 < cost) /* using CR is less characters */
7680 {
7681 plan = PLAN_CR;
7682 wouldbe_col = 0;
7683 cost = 1; /* CR is just one character */
7684 }
7685 else
7686 {
7687 plan = PLAN_LE;
7688 wouldbe_col = col;
7689 }
7690 if (noinvcurs) /* will stop highlighting */
7691 {
7692 cost += noinvcurs;
7693 attr = 0;
7694 }
7695 }
7696
7697 /*
7698 * If the cursor is above where we want to be, we can use CR LF.
7699 */
7700 else if (row > screen_cur_row)
7701 {
7702 plan = PLAN_NL;
7703 wouldbe_col = 0;
7704 cost = (row - screen_cur_row) * 2; /* CR LF */
7705 if (noinvcurs) /* will stop highlighting */
7706 {
7707 cost += noinvcurs;
7708 attr = 0;
7709 }
7710 }
7711
7712 /*
7713 * If the cursor is in the same row, smaller col, just use write.
7714 */
7715 else
7716 {
7717 plan = PLAN_WRITE;
7718 wouldbe_col = screen_cur_col;
7719 cost = 0;
7720 }
7721
7722 /*
7723 * Check if any characters that need to be written have the
7724 * correct attributes. Also avoid UTF-8 characters.
7725 */
7726 i = col - wouldbe_col;
7727 if (i > 0)
7728 cost += i;
7729 if (cost < goto_cost && i > 0)
7730 {
7731 /*
7732 * Check if the attributes are correct without additionally
7733 * stopping highlighting.
7734 */
7735 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
7736 while (i && *p++ == attr)
7737 --i;
7738 if (i != 0)
7739 {
7740 /*
7741 * Try if it works when highlighting is stopped here.
7742 */
7743 if (*--p == 0)
7744 {
7745 cost += noinvcurs;
7746 while (i && *p++ == 0)
7747 --i;
7748 }
7749 if (i != 0)
7750 cost = 999; /* different attributes, don't do it */
7751 }
7752#ifdef FEAT_MBYTE
7753 if (enc_utf8)
7754 {
7755 /* Don't use an UTF-8 char for positioning, it's slow. */
7756 for (i = wouldbe_col; i < col; ++i)
7757 if (ScreenLinesUC[LineOffset[row] + i] != 0)
7758 {
7759 cost = 999;
7760 break;
7761 }
7762 }
7763#endif
7764 }
7765
7766 /*
7767 * We can do it without term_windgoto()!
7768 */
7769 if (cost < goto_cost)
7770 {
7771 if (plan == PLAN_LE)
7772 {
7773 if (noinvcurs)
7774 screen_stop_highlight();
7775 while (screen_cur_col > col)
7776 {
7777 out_str(bs);
7778 --screen_cur_col;
7779 }
7780 }
7781 else if (plan == PLAN_CR)
7782 {
7783 if (noinvcurs)
7784 screen_stop_highlight();
7785 out_char('\r');
7786 screen_cur_col = 0;
7787 }
7788 else if (plan == PLAN_NL)
7789 {
7790 if (noinvcurs)
7791 screen_stop_highlight();
7792 while (screen_cur_row < row)
7793 {
7794 out_char('\n');
7795 ++screen_cur_row;
7796 }
7797 screen_cur_col = 0;
7798 }
7799
7800 i = col - screen_cur_col;
7801 if (i > 0)
7802 {
7803 /*
7804 * Use cursor-right if it's one character only. Avoids
7805 * removing a line of pixels from the last bold char, when
7806 * using the bold trick in the GUI.
7807 */
7808 if (T_ND[0] != NUL && T_ND[1] == NUL)
7809 {
7810 while (i-- > 0)
7811 out_char(*T_ND);
7812 }
7813 else
7814 {
7815 int off;
7816
7817 off = LineOffset[row] + screen_cur_col;
7818 while (i-- > 0)
7819 {
7820 if (ScreenAttrs[off] != screen_attr)
7821 screen_stop_highlight();
7822#ifdef FEAT_MBYTE
7823 out_flush_check();
7824#endif
7825 out_char(ScreenLines[off]);
7826#ifdef FEAT_MBYTE
7827 if (enc_dbcs == DBCS_JPNU
7828 && ScreenLines[off] == 0x8e)
7829 out_char(ScreenLines2[off]);
7830#endif
7831 ++off;
7832 }
7833 }
7834 }
7835 }
7836 }
7837 else
7838 cost = 999;
7839
7840 if (cost >= goto_cost)
7841 {
7842 if (noinvcurs)
7843 screen_stop_highlight();
7844 if (row == screen_cur_row && (col > screen_cur_col) &&
7845 *T_CRI != NUL)
7846 term_cursor_right(col - screen_cur_col);
7847 else
7848 term_windgoto(row, col);
7849 }
7850 screen_cur_row = row;
7851 screen_cur_col = col;
7852 }
7853}
7854
7855/*
7856 * Set cursor to its position in the current window.
7857 */
7858 void
7859setcursor()
7860{
7861 if (redrawing())
7862 {
7863 validate_cursor();
7864 windgoto(W_WINROW(curwin) + curwin->w_wrow,
7865 W_WINCOL(curwin) + (
7866#ifdef FEAT_RIGHTLEFT
7867 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
7868# ifdef FEAT_MBYTE
7869 has_mbyte ? (*mb_ptr2cells)(ml_get_cursor()) :
7870# endif
7871 1)) :
7872#endif
7873 curwin->w_wcol));
7874 }
7875}
7876
7877
7878/*
7879 * insert 'line_count' lines at 'row' in window 'wp'
7880 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
7881 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
7882 * scrolling.
7883 * Returns FAIL if the lines are not inserted, OK for success.
7884 */
7885 int
7886win_ins_lines(wp, row, line_count, invalid, mayclear)
7887 win_T *wp;
7888 int row;
7889 int line_count;
7890 int invalid;
7891 int mayclear;
7892{
7893 int did_delete;
7894 int nextrow;
7895 int lastrow;
7896 int retval;
7897
7898 if (invalid)
7899 wp->w_lines_valid = 0;
7900
7901 if (wp->w_height < 5)
7902 return FAIL;
7903
7904 if (line_count > wp->w_height - row)
7905 line_count = wp->w_height - row;
7906
7907 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
7908 if (retval != MAYBE)
7909 return retval;
7910
7911 /*
7912 * If there is a next window or a status line, we first try to delete the
7913 * lines at the bottom to avoid messing what is after the window.
7914 * If this fails and there are following windows, don't do anything to avoid
7915 * messing up those windows, better just redraw.
7916 */
7917 did_delete = FALSE;
7918#ifdef FEAT_WINDOWS
7919 if (wp->w_next != NULL || wp->w_status_height)
7920 {
7921 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
7922 line_count, (int)Rows, FALSE, NULL) == OK)
7923 did_delete = TRUE;
7924 else if (wp->w_next)
7925 return FAIL;
7926 }
7927#endif
7928 /*
7929 * if no lines deleted, blank the lines that will end up below the window
7930 */
7931 if (!did_delete)
7932 {
7933#ifdef FEAT_WINDOWS
7934 wp->w_redr_status = TRUE;
7935#endif
7936 redraw_cmdline = TRUE;
7937 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
7938 lastrow = nextrow + line_count;
7939 if (lastrow > Rows)
7940 lastrow = Rows;
7941 screen_fill(nextrow - line_count, lastrow - line_count,
7942 W_WINCOL(wp), (int)W_ENDCOL(wp),
7943 ' ', ' ', 0);
7944 }
7945
7946 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
7947 == FAIL)
7948 {
7949 /* deletion will have messed up other windows */
7950 if (did_delete)
7951 {
7952#ifdef FEAT_WINDOWS
7953 wp->w_redr_status = TRUE;
7954#endif
7955 win_rest_invalid(W_NEXT(wp));
7956 }
7957 return FAIL;
7958 }
7959
7960 return OK;
7961}
7962
7963/*
7964 * delete "line_count" window lines at "row" in window "wp"
7965 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
7966 * If "mayclear" is TRUE the screen will be cleared if it is faster than
7967 * scrolling
7968 * Return OK for success, FAIL if the lines are not deleted.
7969 */
7970 int
7971win_del_lines(wp, row, line_count, invalid, mayclear)
7972 win_T *wp;
7973 int row;
7974 int line_count;
7975 int invalid;
7976 int mayclear;
7977{
7978 int retval;
7979
7980 if (invalid)
7981 wp->w_lines_valid = 0;
7982
7983 if (line_count > wp->w_height - row)
7984 line_count = wp->w_height - row;
7985
7986 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
7987 if (retval != MAYBE)
7988 return retval;
7989
7990 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
7991 (int)Rows, FALSE, NULL) == FAIL)
7992 return FAIL;
7993
7994#ifdef FEAT_WINDOWS
7995 /*
7996 * If there are windows or status lines below, try to put them at the
7997 * correct place. If we can't do that, they have to be redrawn.
7998 */
7999 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
8000 {
8001 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8002 line_count, (int)Rows, NULL) == FAIL)
8003 {
8004 wp->w_redr_status = TRUE;
8005 win_rest_invalid(wp->w_next);
8006 }
8007 }
8008 /*
8009 * If this is the last window and there is no status line, redraw the
8010 * command line later.
8011 */
8012 else
8013#endif
8014 redraw_cmdline = TRUE;
8015 return OK;
8016}
8017
8018/*
8019 * Common code for win_ins_lines() and win_del_lines().
8020 * Returns OK or FAIL when the work has been done.
8021 * Returns MAYBE when not finished yet.
8022 */
8023 static int
8024win_do_lines(wp, row, line_count, mayclear, del)
8025 win_T *wp;
8026 int row;
8027 int line_count;
8028 int mayclear;
8029 int del;
8030{
8031 int retval;
8032
8033 if (!redrawing() || line_count <= 0)
8034 return FAIL;
8035
8036 /* only a few lines left: redraw is faster */
8037 if (mayclear && Rows - line_count < 5
8038#ifdef FEAT_VERTSPLIT
8039 && wp->w_width == Columns
8040#endif
8041 )
8042 {
8043 screenclear(); /* will set wp->w_lines_valid to 0 */
8044 return FAIL;
8045 }
8046
8047 /*
8048 * Delete all remaining lines
8049 */
8050 if (row + line_count >= wp->w_height)
8051 {
8052 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
8053 W_WINCOL(wp), (int)W_ENDCOL(wp),
8054 ' ', ' ', 0);
8055 return OK;
8056 }
8057
8058 /*
8059 * when scrolling, the message on the command line should be cleared,
8060 * otherwise it will stay there forever.
8061 */
8062 clear_cmdline = TRUE;
8063
8064 /*
8065 * If the terminal can set a scroll region, use that.
8066 * Always do this in a vertically split window. This will redraw from
8067 * ScreenLines[] when t_CV isn't defined. That's faster than using
8068 * win_line().
8069 * Don't use a scroll region when we are going to redraw the text, writing
8070 * a character in the lower right corner of the scroll region causes a
8071 * scroll-up in the DJGPP version.
8072 */
8073 if (scroll_region
8074#ifdef FEAT_VERTSPLIT
8075 || W_WIDTH(wp) != Columns
8076#endif
8077 )
8078 {
8079#ifdef FEAT_VERTSPLIT
8080 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8081#endif
8082 scroll_region_set(wp, row);
8083 if (del)
8084 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
8085 wp->w_height - row, FALSE, wp);
8086 else
8087 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
8088 wp->w_height - row, wp);
8089#ifdef FEAT_VERTSPLIT
8090 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8091#endif
8092 scroll_region_reset();
8093 return retval;
8094 }
8095
8096#ifdef FEAT_WINDOWS
8097 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
8098 return FAIL;
8099#endif
8100
8101 return MAYBE;
8102}
8103
8104/*
8105 * window 'wp' and everything after it is messed up, mark it for redraw
8106 */
8107 static void
8108win_rest_invalid(wp)
8109 win_T *wp;
8110{
8111#ifdef FEAT_WINDOWS
8112 while (wp != NULL)
8113#else
8114 if (wp != NULL)
8115#endif
8116 {
8117 redraw_win_later(wp, NOT_VALID);
8118#ifdef FEAT_WINDOWS
8119 wp->w_redr_status = TRUE;
8120 wp = wp->w_next;
8121#endif
8122 }
8123 redraw_cmdline = TRUE;
8124}
8125
8126/*
8127 * The rest of the routines in this file perform screen manipulations. The
8128 * given operation is performed physically on the screen. The corresponding
8129 * change is also made to the internal screen image. In this way, the editor
8130 * anticipates the effect of editing changes on the appearance of the screen.
8131 * That way, when we call screenupdate a complete redraw isn't usually
8132 * necessary. Another advantage is that we can keep adding code to anticipate
8133 * screen changes, and in the meantime, everything still works.
8134 */
8135
8136/*
8137 * types for inserting or deleting lines
8138 */
8139#define USE_T_CAL 1
8140#define USE_T_CDL 2
8141#define USE_T_AL 3
8142#define USE_T_CE 4
8143#define USE_T_DL 5
8144#define USE_T_SR 6
8145#define USE_NL 7
8146#define USE_T_CD 8
8147#define USE_REDRAW 9
8148
8149/*
8150 * insert lines on the screen and update ScreenLines[]
8151 * 'end' is the line after the scrolled part. Normally it is Rows.
8152 * When scrolling region used 'off' is the offset from the top for the region.
8153 * 'row' and 'end' are relative to the start of the region.
8154 *
8155 * return FAIL for failure, OK for success.
8156 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008157 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158screen_ins_lines(off, row, line_count, end, wp)
8159 int off;
8160 int row;
8161 int line_count;
8162 int end;
8163 win_T *wp; /* NULL or window to use width from */
8164{
8165 int i;
8166 int j;
8167 unsigned temp;
8168 int cursor_row;
8169 int type;
8170 int result_empty;
8171 int can_ce = can_clear(T_CE);
8172
8173 /*
8174 * FAIL if
8175 * - there is no valid screen
8176 * - the screen has to be redrawn completely
8177 * - the line count is less than one
8178 * - the line count is more than 'ttyscroll'
8179 */
8180 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
8181 return FAIL;
8182
8183 /*
8184 * There are seven ways to insert lines:
8185 * 0. When in a vertically split window and t_CV isn't set, redraw the
8186 * characters from ScreenLines[].
8187 * 1. Use T_CD (clear to end of display) if it exists and the result of
8188 * the insert is just empty lines
8189 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
8190 * present or line_count > 1. It looks better if we do all the inserts
8191 * at once.
8192 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
8193 * insert is just empty lines and T_CE is not present or line_count >
8194 * 1.
8195 * 4. Use T_AL (insert line) if it exists.
8196 * 5. Use T_CE (erase line) if it exists and the result of the insert is
8197 * just empty lines.
8198 * 6. Use T_DL (delete line) if it exists and the result of the insert is
8199 * just empty lines.
8200 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
8201 * the 'da' flag is not set or we have clear line capability.
8202 * 8. redraw the characters from ScreenLines[].
8203 *
8204 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
8205 * the scrollbar for the window. It does have insert line, use that if it
8206 * exists.
8207 */
8208 result_empty = (row + line_count >= end);
8209#ifdef FEAT_VERTSPLIT
8210 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
8211 type = USE_REDRAW;
8212 else
8213#endif
8214 if (can_clear(T_CD) && result_empty)
8215 type = USE_T_CD;
8216 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
8217 type = USE_T_CAL;
8218 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
8219 type = USE_T_CDL;
8220 else if (*T_AL != NUL)
8221 type = USE_T_AL;
8222 else if (can_ce && result_empty)
8223 type = USE_T_CE;
8224 else if (*T_DL != NUL && result_empty)
8225 type = USE_T_DL;
8226 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
8227 type = USE_T_SR;
8228 else
8229 return FAIL;
8230
8231 /*
8232 * For clearing the lines screen_del_lines() is used. This will also take
8233 * care of t_db if necessary.
8234 */
8235 if (type == USE_T_CD || type == USE_T_CDL ||
8236 type == USE_T_CE || type == USE_T_DL)
8237 return screen_del_lines(off, row, line_count, end, FALSE, wp);
8238
8239 /*
8240 * If text is retained below the screen, first clear or delete as many
8241 * lines at the bottom of the window as are about to be inserted so that
8242 * the deleted lines won't later surface during a screen_del_lines.
8243 */
8244 if (*T_DB)
8245 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
8246
8247#ifdef FEAT_CLIPBOARD
8248 /* Remove a modeless selection when inserting lines halfway the screen
8249 * or not the full width of the screen. */
8250 if (off + row > 0
8251# ifdef FEAT_VERTSPLIT
8252 || (wp != NULL && wp->w_width != Columns)
8253# endif
8254 )
8255 clip_clear_selection();
8256 else
8257 clip_scroll_selection(-line_count);
8258#endif
8259
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260#ifdef FEAT_GUI
8261 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
8262 * scrolling is actually carried out. */
8263 gui_dont_update_cursor();
8264#endif
8265
8266 if (*T_CCS != NUL) /* cursor relative to region */
8267 cursor_row = row;
8268 else
8269 cursor_row = row + off;
8270
8271 /*
8272 * Shift LineOffset[] line_count down to reflect the inserted lines.
8273 * Clear the inserted lines in ScreenLines[].
8274 */
8275 row += off;
8276 end += off;
8277 for (i = 0; i < line_count; ++i)
8278 {
8279#ifdef FEAT_VERTSPLIT
8280 if (wp != NULL && wp->w_width != Columns)
8281 {
8282 /* need to copy part of a line */
8283 j = end - 1 - i;
8284 while ((j -= line_count) >= row)
8285 linecopy(j + line_count, j, wp);
8286 j += line_count;
8287 if (can_clear((char_u *)" "))
8288 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
8289 else
8290 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
8291 LineWraps[j] = FALSE;
8292 }
8293 else
8294#endif
8295 {
8296 j = end - 1 - i;
8297 temp = LineOffset[j];
8298 while ((j -= line_count) >= row)
8299 {
8300 LineOffset[j + line_count] = LineOffset[j];
8301 LineWraps[j + line_count] = LineWraps[j];
8302 }
8303 LineOffset[j + line_count] = temp;
8304 LineWraps[j + line_count] = FALSE;
8305 if (can_clear((char_u *)" "))
8306 lineclear(temp, (int)Columns);
8307 else
8308 lineinvalid(temp, (int)Columns);
8309 }
8310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311
8312 screen_stop_highlight();
8313 windgoto(cursor_row, 0);
8314
8315#ifdef FEAT_VERTSPLIT
8316 /* redraw the characters */
8317 if (type == USE_REDRAW)
8318 redraw_block(row, end, wp);
8319 else
8320#endif
8321 if (type == USE_T_CAL)
8322 {
8323 term_append_lines(line_count);
8324 screen_start(); /* don't know where cursor is now */
8325 }
8326 else
8327 {
8328 for (i = 0; i < line_count; i++)
8329 {
8330 if (type == USE_T_AL)
8331 {
8332 if (i && cursor_row != 0)
8333 windgoto(cursor_row, 0);
8334 out_str(T_AL);
8335 }
8336 else /* type == USE_T_SR */
8337 out_str(T_SR);
8338 screen_start(); /* don't know where cursor is now */
8339 }
8340 }
8341
8342 /*
8343 * With scroll-reverse and 'da' flag set we need to clear the lines that
8344 * have been scrolled down into the region.
8345 */
8346 if (type == USE_T_SR && *T_DA)
8347 {
8348 for (i = 0; i < line_count; ++i)
8349 {
8350 windgoto(off + i, 0);
8351 out_str(T_CE);
8352 screen_start(); /* don't know where cursor is now */
8353 }
8354 }
8355
8356#ifdef FEAT_GUI
8357 gui_can_update_cursor();
8358 if (gui.in_use)
8359 out_flush(); /* always flush after a scroll */
8360#endif
8361 return OK;
8362}
8363
8364/*
8365 * delete lines on the screen and update ScreenLines[]
8366 * 'end' is the line after the scrolled part. Normally it is Rows.
8367 * When scrolling region used 'off' is the offset from the top for the region.
8368 * 'row' and 'end' are relative to the start of the region.
8369 *
8370 * Return OK for success, FAIL if the lines are not deleted.
8371 */
8372/*ARGSUSED*/
8373 int
8374screen_del_lines(off, row, line_count, end, force, wp)
8375 int off;
8376 int row;
8377 int line_count;
8378 int end;
8379 int force; /* even when line_count > p_ttyscroll */
8380 win_T *wp; /* NULL or window to use width from */
8381{
8382 int j;
8383 int i;
8384 unsigned temp;
8385 int cursor_row;
8386 int cursor_end;
8387 int result_empty; /* result is empty until end of region */
8388 int can_delete; /* deleting line codes can be used */
8389 int type;
8390
8391 /*
8392 * FAIL if
8393 * - there is no valid screen
8394 * - the screen has to be redrawn completely
8395 * - the line count is less than one
8396 * - the line count is more than 'ttyscroll'
8397 */
8398 if (!screen_valid(TRUE) || line_count <= 0 ||
8399 (!force && line_count > p_ttyscroll))
8400 return FAIL;
8401
8402 /*
8403 * Check if the rest of the current region will become empty.
8404 */
8405 result_empty = row + line_count >= end;
8406
8407 /*
8408 * We can delete lines only when 'db' flag not set or when 'ce' option
8409 * available.
8410 */
8411 can_delete = (*T_DB == NUL || can_clear(T_CE));
8412
8413 /*
8414 * There are six ways to delete lines:
8415 * 0. When in a vertically split window and t_CV isn't set, redraw the
8416 * characters from ScreenLines[].
8417 * 1. Use T_CD if it exists and the result is empty.
8418 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
8419 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
8420 * none of the other ways work.
8421 * 4. Use T_CE (erase line) if the result is empty.
8422 * 5. Use T_DL (delete line) if it exists.
8423 * 6. redraw the characters from ScreenLines[].
8424 */
8425#ifdef FEAT_VERTSPLIT
8426 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
8427 type = USE_REDRAW;
8428 else
8429#endif
8430 if (can_clear(T_CD) && result_empty)
8431 type = USE_T_CD;
8432#if defined(__BEOS__) && defined(BEOS_DR8)
8433 /*
8434 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
8435 * its internal termcap... this works okay for tests which test *T_DB !=
8436 * NUL. It has the disadvantage that the user cannot use any :set t_*
8437 * command to get T_DB (back) to empty_option, only :set term=... will do
8438 * the trick...
8439 * Anyway, this hack will hopefully go away with the next OS release.
8440 * (Olaf Seibert)
8441 */
8442 else if (row == 0 && T_DB == empty_option
8443 && (line_count == 1 || *T_CDL == NUL))
8444#else
8445 else if (row == 0 && (
8446#ifndef AMIGA
8447 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
8448 * up, so use delete-line command */
8449 line_count == 1 ||
8450#endif
8451 *T_CDL == NUL))
8452#endif
8453 type = USE_NL;
8454 else if (*T_CDL != NUL && line_count > 1 && can_delete)
8455 type = USE_T_CDL;
8456 else if (can_clear(T_CE) && result_empty
8457#ifdef FEAT_VERTSPLIT
8458 && (wp == NULL || wp->w_width == Columns)
8459#endif
8460 )
8461 type = USE_T_CE;
8462 else if (*T_DL != NUL && can_delete)
8463 type = USE_T_DL;
8464 else if (*T_CDL != NUL && can_delete)
8465 type = USE_T_CDL;
8466 else
8467 return FAIL;
8468
8469#ifdef FEAT_CLIPBOARD
8470 /* Remove a modeless selection when deleting lines halfway the screen or
8471 * not the full width of the screen. */
8472 if (off + row > 0
8473# ifdef FEAT_VERTSPLIT
8474 || (wp != NULL && wp->w_width != Columns)
8475# endif
8476 )
8477 clip_clear_selection();
8478 else
8479 clip_scroll_selection(line_count);
8480#endif
8481
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482#ifdef FEAT_GUI
8483 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
8484 * scrolling is actually carried out. */
8485 gui_dont_update_cursor();
8486#endif
8487
8488 if (*T_CCS != NUL) /* cursor relative to region */
8489 {
8490 cursor_row = row;
8491 cursor_end = end;
8492 }
8493 else
8494 {
8495 cursor_row = row + off;
8496 cursor_end = end + off;
8497 }
8498
8499 /*
8500 * Now shift LineOffset[] line_count up to reflect the deleted lines.
8501 * Clear the inserted lines in ScreenLines[].
8502 */
8503 row += off;
8504 end += off;
8505 for (i = 0; i < line_count; ++i)
8506 {
8507#ifdef FEAT_VERTSPLIT
8508 if (wp != NULL && wp->w_width != Columns)
8509 {
8510 /* need to copy part of a line */
8511 j = row + i;
8512 while ((j += line_count) <= end - 1)
8513 linecopy(j - line_count, j, wp);
8514 j -= line_count;
8515 if (can_clear((char_u *)" "))
8516 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
8517 else
8518 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
8519 LineWraps[j] = FALSE;
8520 }
8521 else
8522#endif
8523 {
8524 /* whole width, moving the line pointers is faster */
8525 j = row + i;
8526 temp = LineOffset[j];
8527 while ((j += line_count) <= end - 1)
8528 {
8529 LineOffset[j - line_count] = LineOffset[j];
8530 LineWraps[j - line_count] = LineWraps[j];
8531 }
8532 LineOffset[j - line_count] = temp;
8533 LineWraps[j - line_count] = FALSE;
8534 if (can_clear((char_u *)" "))
8535 lineclear(temp, (int)Columns);
8536 else
8537 lineinvalid(temp, (int)Columns);
8538 }
8539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540
8541 screen_stop_highlight();
8542
8543#ifdef FEAT_VERTSPLIT
8544 /* redraw the characters */
8545 if (type == USE_REDRAW)
8546 redraw_block(row, end, wp);
8547 else
8548#endif
8549 if (type == USE_T_CD) /* delete the lines */
8550 {
8551 windgoto(cursor_row, 0);
8552 out_str(T_CD);
8553 screen_start(); /* don't know where cursor is now */
8554 }
8555 else if (type == USE_T_CDL)
8556 {
8557 windgoto(cursor_row, 0);
8558 term_delete_lines(line_count);
8559 screen_start(); /* don't know where cursor is now */
8560 }
8561 /*
8562 * Deleting lines at top of the screen or scroll region: Just scroll
8563 * the whole screen (scroll region) up by outputting newlines on the
8564 * last line.
8565 */
8566 else if (type == USE_NL)
8567 {
8568 windgoto(cursor_end - 1, 0);
8569 for (i = line_count; --i >= 0; )
8570 out_char('\n'); /* cursor will remain on same line */
8571 }
8572 else
8573 {
8574 for (i = line_count; --i >= 0; )
8575 {
8576 if (type == USE_T_DL)
8577 {
8578 windgoto(cursor_row, 0);
8579 out_str(T_DL); /* delete a line */
8580 }
8581 else /* type == USE_T_CE */
8582 {
8583 windgoto(cursor_row + i, 0);
8584 out_str(T_CE); /* erase a line */
8585 }
8586 screen_start(); /* don't know where cursor is now */
8587 }
8588 }
8589
8590 /*
8591 * If the 'db' flag is set, we need to clear the lines that have been
8592 * scrolled up at the bottom of the region.
8593 */
8594 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
8595 {
8596 for (i = line_count; i > 0; --i)
8597 {
8598 windgoto(cursor_end - i, 0);
8599 out_str(T_CE); /* erase a line */
8600 screen_start(); /* don't know where cursor is now */
8601 }
8602 }
8603
8604#ifdef FEAT_GUI
8605 gui_can_update_cursor();
8606 if (gui.in_use)
8607 out_flush(); /* always flush after a scroll */
8608#endif
8609
8610 return OK;
8611}
8612
8613/*
8614 * show the current mode and ruler
8615 *
8616 * If clear_cmdline is TRUE, clear the rest of the cmdline.
8617 * If clear_cmdline is FALSE there may be a message there that needs to be
8618 * cleared only if a mode is shown.
8619 * Return the length of the message (0 if no message).
8620 */
8621 int
8622showmode()
8623{
8624 int need_clear;
8625 int length = 0;
8626 int do_mode;
8627 int attr;
8628 int nwr_save;
8629#ifdef FEAT_INS_EXPAND
8630 int sub_attr;
8631#endif
8632
Bram Moolenaar7df351e2006-01-23 22:30:28 +00008633 do_mode = ((p_smd && msg_silent == 0)
8634 && ((State & INSERT)
8635 || restart_edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636#ifdef FEAT_VISUAL
8637 || VIsual_active
8638#endif
8639 ));
8640 if (do_mode || Recording)
8641 {
8642 /*
8643 * Don't show mode right now, when not redrawing or inside a mapping.
8644 * Call char_avail() only when we are going to show something, because
8645 * it takes a bit of time.
8646 */
8647 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
8648 {
8649 redraw_cmdline = TRUE; /* show mode later */
8650 return 0;
8651 }
8652
8653 nwr_save = need_wait_return;
8654
8655 /* wait a bit before overwriting an important message */
8656 check_for_delay(FALSE);
8657
8658 /* if the cmdline is more than one line high, erase top lines */
8659 need_clear = clear_cmdline;
8660 if (clear_cmdline && cmdline_row < Rows - 1)
8661 msg_clr_cmdline(); /* will reset clear_cmdline */
8662
8663 /* Position on the last line in the window, column 0 */
8664 msg_pos_mode();
8665 cursor_off();
8666 attr = hl_attr(HLF_CM); /* Highlight mode */
8667 if (do_mode)
8668 {
8669 MSG_PUTS_ATTR("--", attr);
8670#if defined(FEAT_XIM)
8671 if (xic != NULL && im_get_status() && !p_imdisable
8672 && curbuf->b_p_iminsert == B_IMODE_IM)
8673# ifdef HAVE_GTK2 /* most of the time, it's not XIM being used */
8674 MSG_PUTS_ATTR(" IM", attr);
8675# else
8676 MSG_PUTS_ATTR(" XIM", attr);
8677# endif
8678#endif
8679#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
8680 if (gui.in_use)
8681 {
8682 if (hangul_input_state_get())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008683 MSG_PUTS_ATTR(" \307\321\261\333", attr); /* HANGUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684 }
8685#endif
8686#ifdef FEAT_INS_EXPAND
8687 if (edit_submode != NULL) /* CTRL-X in Insert mode */
8688 {
8689 /* These messages can get long, avoid a wrap in a narrow
8690 * window. Prefer showing edit_submode_extra. */
8691 length = (Rows - msg_row) * Columns - 3;
8692 if (edit_submode_extra != NULL)
8693 length -= vim_strsize(edit_submode_extra);
8694 if (length > 0)
8695 {
8696 if (edit_submode_pre != NULL)
8697 length -= vim_strsize(edit_submode_pre);
8698 if (length - vim_strsize(edit_submode) > 0)
8699 {
8700 if (edit_submode_pre != NULL)
8701 msg_puts_attr(edit_submode_pre, attr);
8702 msg_puts_attr(edit_submode, attr);
8703 }
8704 if (edit_submode_extra != NULL)
8705 {
8706 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
8707 if ((int)edit_submode_highl < (int)HLF_COUNT)
8708 sub_attr = hl_attr(edit_submode_highl);
8709 else
8710 sub_attr = attr;
8711 msg_puts_attr(edit_submode_extra, sub_attr);
8712 }
8713 }
8714 length = 0;
8715 }
8716 else
8717#endif
8718 {
8719#ifdef FEAT_VREPLACE
8720 if (State & VREPLACE_FLAG)
8721 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
8722 else
8723#endif
8724 if (State & REPLACE_FLAG)
8725 MSG_PUTS_ATTR(_(" REPLACE"), attr);
8726 else if (State & INSERT)
8727 {
8728#ifdef FEAT_RIGHTLEFT
8729 if (p_ri)
8730 MSG_PUTS_ATTR(_(" REVERSE"), attr);
8731#endif
8732 MSG_PUTS_ATTR(_(" INSERT"), attr);
8733 }
8734 else if (restart_edit == 'I')
8735 MSG_PUTS_ATTR(_(" (insert)"), attr);
8736 else if (restart_edit == 'R')
8737 MSG_PUTS_ATTR(_(" (replace)"), attr);
8738 else if (restart_edit == 'V')
8739 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
8740#ifdef FEAT_RIGHTLEFT
8741 if (p_hkmap)
8742 MSG_PUTS_ATTR(_(" Hebrew"), attr);
8743# ifdef FEAT_FKMAP
8744 if (p_fkmap)
8745 MSG_PUTS_ATTR(farsi_text_5, attr);
8746# endif
8747#endif
8748#ifdef FEAT_KEYMAP
8749 if (State & LANGMAP)
8750 {
8751# ifdef FEAT_ARABIC
8752 if (curwin->w_p_arab)
8753 MSG_PUTS_ATTR(_(" Arabic"), attr);
8754 else
8755# endif
8756 MSG_PUTS_ATTR(_(" (lang)"), attr);
8757 }
8758#endif
8759 if ((State & INSERT) && p_paste)
8760 MSG_PUTS_ATTR(_(" (paste)"), attr);
8761
8762#ifdef FEAT_VISUAL
8763 if (VIsual_active)
8764 {
8765 char *p;
8766
8767 /* Don't concatenate separate words to avoid translation
8768 * problems. */
8769 switch ((VIsual_select ? 4 : 0)
8770 + (VIsual_mode == Ctrl_V) * 2
8771 + (VIsual_mode == 'V'))
8772 {
8773 case 0: p = N_(" VISUAL"); break;
8774 case 1: p = N_(" VISUAL LINE"); break;
8775 case 2: p = N_(" VISUAL BLOCK"); break;
8776 case 4: p = N_(" SELECT"); break;
8777 case 5: p = N_(" SELECT LINE"); break;
8778 default: p = N_(" SELECT BLOCK"); break;
8779 }
8780 MSG_PUTS_ATTR(_(p), attr);
8781 }
8782#endif
8783 MSG_PUTS_ATTR(" --", attr);
8784 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008785
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786 need_clear = TRUE;
8787 }
8788 if (Recording
8789#ifdef FEAT_INS_EXPAND
8790 && edit_submode == NULL /* otherwise it gets too long */
8791#endif
8792 )
8793 {
8794 MSG_PUTS_ATTR(_("recording"), attr);
8795 need_clear = TRUE;
8796 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008797
8798 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008799 if (need_clear || clear_cmdline)
8800 msg_clr_eos();
8801 msg_didout = FALSE; /* overwrite this message */
8802 length = msg_col;
8803 msg_col = 0;
8804 need_wait_return = nwr_save; /* never ask for hit-return for this */
8805 }
8806 else if (clear_cmdline && msg_silent == 0)
8807 /* Clear the whole command line. Will reset "clear_cmdline". */
8808 msg_clr_cmdline();
8809
8810#ifdef FEAT_CMDL_INFO
8811# ifdef FEAT_VISUAL
8812 /* In Visual mode the size of the selected area must be redrawn. */
8813 if (VIsual_active)
8814 clear_showcmd();
8815# endif
8816
8817 /* If the last window has no status line, the ruler is after the mode
8818 * message and must be redrawn */
8819 if (redrawing()
8820# ifdef FEAT_WINDOWS
8821 && lastwin->w_status_height == 0
8822# endif
8823 )
8824 win_redr_ruler(lastwin, TRUE);
8825#endif
8826 redraw_cmdline = FALSE;
8827 clear_cmdline = FALSE;
8828
8829 return length;
8830}
8831
8832/*
8833 * Position for a mode message.
8834 */
8835 static void
8836msg_pos_mode()
8837{
8838 msg_col = 0;
8839 msg_row = Rows - 1;
8840}
8841
8842/*
8843 * Delete mode message. Used when ESC is typed which is expected to end
8844 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008845 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846 */
8847 void
8848unshowmode(force)
8849 int force;
8850{
8851 /*
8852 * Don't delete it right now, when not redrawing or insided a mapping.
8853 */
8854 if (!redrawing() || (!force && char_avail() && !KeyTyped))
8855 redraw_cmdline = TRUE; /* delete mode later */
8856 else
8857 {
8858 msg_pos_mode();
8859 if (Recording)
8860 MSG_PUTS_ATTR(_("recording"), hl_attr(HLF_CM));
8861 msg_clr_eos();
8862 }
8863}
8864
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008865#if defined(FEAT_WINDOWS)
8866/*
8867 * Draw the tab pages line at the top of the Vim window.
8868 */
8869 static void
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008870draw_tabline()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008871{
8872 int tabcount = 0;
8873 tabpage_T *tp;
8874 int tabwidth;
8875 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008876 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008877 int attr;
8878 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008879 win_T *cwp;
8880 int wincount;
8881 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008882 int c;
8883 int len;
8884 int attr_sel = hl_attr(HLF_TPS);
8885 int attr_nosel = hl_attr(HLF_TP);
8886 int attr_fill = hl_attr(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008887 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008888 int room;
8889 int use_sep_chars = (t_colors < 8
8890#ifdef FEAT_GUI
8891 && !gui.in_use
8892#endif
8893 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008894
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008895 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008896
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008897#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +00008898 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008899 if (gui_use_tabline())
8900 {
8901 gui_update_tabline();
8902 return;
8903 }
8904#endif
8905
8906 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008907 return;
8908
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008909#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008910
8911 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
8912 for (scol = 0; scol < Columns; ++scol)
8913 TabPageIdxs[scol] = 0;
8914
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008915 /* Use the 'tabline' option if it's set. */
8916 if (*p_tal != NUL)
8917 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00008918 int save_called_emsg = called_emsg;
8919
8920 /* Check for an error. If there is one we would loop in redrawing the
8921 * screen. Avoid that by making 'tabline' empty. */
8922 called_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008923 win_redr_custom(NULL, FALSE);
Bram Moolenaar238a5642006-02-21 22:12:05 +00008924 if (called_emsg)
8925 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00008926 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00008927 called_emsg |= save_called_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008928 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00008929 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008930#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008931 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00008932 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
8933 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008934
Bram Moolenaar238a5642006-02-21 22:12:05 +00008935 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
8936 if (tabwidth < 6)
8937 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008938
Bram Moolenaar238a5642006-02-21 22:12:05 +00008939 attr = attr_nosel;
8940 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008941 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00008942 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
8943 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00008944 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00008945 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008946
Bram Moolenaar238a5642006-02-21 22:12:05 +00008947 if (tp->tp_topframe == topframe)
8948 attr = attr_sel;
8949 if (use_sep_chars && col > 0)
8950 screen_putchar('|', 0, col++, attr);
8951
8952 if (tp->tp_topframe != topframe)
8953 attr = attr_nosel;
8954
8955 screen_putchar(' ', 0, col++, attr);
8956
8957 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00008958 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00008959 cwp = curwin;
8960 wp = firstwin;
8961 }
8962 else
8963 {
8964 cwp = tp->tp_curwin;
8965 wp = tp->tp_firstwin;
8966 }
8967
8968 modified = FALSE;
8969 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
8970 if (bufIsChanged(wp->w_buffer))
8971 modified = TRUE;
8972 if (modified || wincount > 1)
8973 {
8974 if (wincount > 1)
8975 {
8976 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008977 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00008978 if (col + len >= Columns - 3)
8979 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +00008980 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008981#if defined(FEAT_SYN_HL)
Bram Moolenaar238a5642006-02-21 22:12:05 +00008982 hl_combine_attr(attr, hl_attr(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008983#else
Bram Moolenaar238a5642006-02-21 22:12:05 +00008984 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008985#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00008986 );
8987 col += len;
8988 }
8989 if (modified)
8990 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
8991 screen_putchar(' ', 0, col++, attr);
8992 }
8993
8994 room = scol - col + tabwidth - 1;
8995 if (room > 0)
8996 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008997 /* Get buffer name in NameBuff[] */
8998 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008999 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009000 len = vim_strsize(NameBuff);
9001 p = NameBuff;
9002#ifdef FEAT_MBYTE
9003 if (has_mbyte)
9004 while (len > room)
9005 {
9006 len -= ptr2cells(p);
9007 mb_ptr_adv(p);
9008 }
9009 else
9010#endif
9011 if (len > room)
9012 {
9013 p += len - room;
9014 len = room;
9015 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009016 if (len > Columns - col - 1)
9017 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +00009018
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009019 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009020 col += len;
9021 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00009022 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009023
9024 /* Store the tab page number in TabPageIdxs[], so that
9025 * jump_to_mouse() knows where each one is. */
9026 ++tabcount;
9027 while (scol < col)
9028 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009029 }
9030
Bram Moolenaar238a5642006-02-21 22:12:05 +00009031 if (use_sep_chars)
9032 c = '_';
9033 else
9034 c = ' ';
9035 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009036
9037 /* Put an "X" for closing the current tab if there are several. */
9038 if (first_tabpage->tp_next != NULL)
9039 {
9040 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
9041 TabPageIdxs[Columns - 1] = -999;
9042 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009043 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +00009044
9045 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
9046 * set. */
9047 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009048}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009049
9050/*
9051 * Get buffer name for "buf" into NameBuff[].
9052 * Takes care of special buffer names and translates special characters.
9053 */
9054 void
9055get_trans_bufname(buf)
9056 buf_T *buf;
9057{
9058 if (buf_spname(buf) != NULL)
9059 STRCPY(NameBuff, buf_spname(buf));
9060 else
9061 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
9062 trans_characters(NameBuff, MAXPATHL);
9063}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009064#endif
9065
Bram Moolenaar071d4272004-06-13 20:20:40 +00009066#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
9067/*
9068 * Get the character to use in a status line. Get its attributes in "*attr".
9069 */
9070 static int
9071fillchar_status(attr, is_curwin)
9072 int *attr;
9073 int is_curwin;
9074{
9075 int fill;
9076 if (is_curwin)
9077 {
9078 *attr = hl_attr(HLF_S);
9079 fill = fill_stl;
9080 }
9081 else
9082 {
9083 *attr = hl_attr(HLF_SNC);
9084 fill = fill_stlnc;
9085 }
9086 /* Use fill when there is highlighting, and highlighting of current
9087 * window differs, or the fillchars differ, or this is not the
9088 * current window */
9089 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
9090 || !is_curwin || firstwin == lastwin)
9091 || (fill_stl != fill_stlnc)))
9092 return fill;
9093 if (is_curwin)
9094 return '^';
9095 return '=';
9096}
9097#endif
9098
9099#ifdef FEAT_VERTSPLIT
9100/*
9101 * Get the character to use in a separator between vertically split windows.
9102 * Get its attributes in "*attr".
9103 */
9104 static int
9105fillchar_vsep(attr)
9106 int *attr;
9107{
9108 *attr = hl_attr(HLF_C);
9109 if (*attr == 0 && fill_vert == ' ')
9110 return '|';
9111 else
9112 return fill_vert;
9113}
9114#endif
9115
9116/*
9117 * Return TRUE if redrawing should currently be done.
9118 */
9119 int
9120redrawing()
9121{
9122 return (!RedrawingDisabled
9123 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
9124}
9125
9126/*
9127 * Return TRUE if printing messages should currently be done.
9128 */
9129 int
9130messaging()
9131{
9132 return (!(p_lz && char_avail() && !KeyTyped));
9133}
9134
9135/*
9136 * Show current status info in ruler and various other places
9137 * If always is FALSE, only show ruler if position has changed.
9138 */
9139 void
9140showruler(always)
9141 int always;
9142{
9143 if (!always && !redrawing())
9144 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +00009145#ifdef FEAT_INS_EXPAND
9146 if (pum_visible())
9147 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00009148# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +00009149 /* Don't redraw right now, do it later. */
9150 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00009151# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +00009152 return;
9153 }
9154#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009155#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00009156 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +00009157 {
9158 redraw_custum_statusline(curwin);
9159 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160 else
9161#endif
9162#ifdef FEAT_CMDL_INFO
9163 win_redr_ruler(curwin, always);
9164#endif
9165
9166#ifdef FEAT_TITLE
9167 if (need_maketitle
9168# ifdef FEAT_STL_OPT
9169 || (p_icon && (stl_syntax & STL_IN_ICON))
9170 || (p_title && (stl_syntax & STL_IN_TITLE))
9171# endif
9172 )
9173 maketitle();
9174#endif
9175}
9176
9177#ifdef FEAT_CMDL_INFO
9178 static void
9179win_redr_ruler(wp, always)
9180 win_T *wp;
9181 int always;
9182{
9183 char_u buffer[70];
9184 int row;
9185 int fillchar;
9186 int attr;
9187 int empty_line = FALSE;
9188 colnr_T virtcol;
9189 int i;
9190 int o;
9191#ifdef FEAT_VERTSPLIT
9192 int this_ru_col;
9193 int off = 0;
9194 int width = Columns;
9195# define WITH_OFF(x) x
9196# define WITH_WIDTH(x) x
9197#else
9198# define WITH_OFF(x) 0
9199# define WITH_WIDTH(x) Columns
9200# define this_ru_col ru_col
9201#endif
9202
9203 /* If 'ruler' off or redrawing disabled, don't do anything */
9204 if (!p_ru)
9205 return;
9206
9207 /*
9208 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
9209 * after deleting lines, before cursor.lnum is corrected.
9210 */
9211 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
9212 return;
9213
9214#ifdef FEAT_INS_EXPAND
9215 /* Don't draw the ruler while doing insert-completion, it might overwrite
9216 * the (long) mode message. */
9217# ifdef FEAT_WINDOWS
9218 if (wp == lastwin && lastwin->w_status_height == 0)
9219# endif
9220 if (edit_submode != NULL)
9221 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00009222 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
9223 if (pum_visible())
9224 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009225#endif
9226
9227#ifdef FEAT_STL_OPT
9228 if (*p_ruf)
9229 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009230 int save_called_emsg = called_emsg;
9231
9232 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009233 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009234 if (called_emsg)
9235 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00009236 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009237 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009238 return;
9239 }
9240#endif
9241
9242 /*
9243 * Check if not in Insert mode and the line is empty (will show "0-1").
9244 */
9245 if (!(State & INSERT)
9246 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
9247 empty_line = TRUE;
9248
9249 /*
9250 * Only draw the ruler when something changed.
9251 */
9252 validate_virtcol_win(wp);
9253 if ( redraw_cmdline
9254 || always
9255 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
9256 || wp->w_cursor.col != wp->w_ru_cursor.col
9257 || wp->w_virtcol != wp->w_ru_virtcol
9258#ifdef FEAT_VIRTUALEDIT
9259 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
9260#endif
9261 || wp->w_topline != wp->w_ru_topline
9262 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
9263#ifdef FEAT_DIFF
9264 || wp->w_topfill != wp->w_ru_topfill
9265#endif
9266 || empty_line != wp->w_ru_empty)
9267 {
9268 cursor_off();
9269#ifdef FEAT_WINDOWS
9270 if (wp->w_status_height)
9271 {
9272 row = W_WINROW(wp) + wp->w_height;
9273 fillchar = fillchar_status(&attr, wp == curwin);
9274# ifdef FEAT_VERTSPLIT
9275 off = W_WINCOL(wp);
9276 width = W_WIDTH(wp);
9277# endif
9278 }
9279 else
9280#endif
9281 {
9282 row = Rows - 1;
9283 fillchar = ' ';
9284 attr = 0;
9285#ifdef FEAT_VERTSPLIT
9286 width = Columns;
9287 off = 0;
9288#endif
9289 }
9290
9291 /* In list mode virtcol needs to be recomputed */
9292 virtcol = wp->w_virtcol;
9293 if (wp->w_p_list && lcs_tab1 == NUL)
9294 {
9295 wp->w_p_list = FALSE;
9296 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
9297 wp->w_p_list = TRUE;
9298 }
9299
9300 /*
9301 * Some sprintfs return the length, some return a pointer.
9302 * To avoid portability problems we use strlen() here.
9303 */
9304 sprintf((char *)buffer, "%ld,",
9305 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
9306 ? 0L
9307 : (long)(wp->w_cursor.lnum));
9308 col_print(buffer + STRLEN(buffer),
9309 empty_line ? 0 : (int)wp->w_cursor.col + 1,
9310 (int)virtcol + 1);
9311
9312 /*
9313 * Add a "50%" if there is room for it.
9314 * On the last line, don't print in the last column (scrolls the
9315 * screen up on some terminals).
9316 */
9317 i = (int)STRLEN(buffer);
9318 get_rel_pos(wp, buffer + i + 1);
9319 o = i + vim_strsize(buffer + i + 1);
9320#ifdef FEAT_WINDOWS
9321 if (wp->w_status_height == 0) /* can't use last char of screen */
9322#endif
9323 ++o;
9324#ifdef FEAT_VERTSPLIT
9325 this_ru_col = ru_col - (Columns - width);
9326 if (this_ru_col < 0)
9327 this_ru_col = 0;
9328#endif
9329 /* Never use more than half the window/screen width, leave the other
9330 * half for the filename. */
9331 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
9332 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
9333 if (this_ru_col + o < WITH_WIDTH(width))
9334 {
9335 while (this_ru_col + o < WITH_WIDTH(width))
9336 {
9337#ifdef FEAT_MBYTE
9338 if (has_mbyte)
9339 i += (*mb_char2bytes)(fillchar, buffer + i);
9340 else
9341#endif
9342 buffer[i++] = fillchar;
9343 ++o;
9344 }
9345 get_rel_pos(wp, buffer + i);
9346 }
9347 /* Truncate at window boundary. */
9348#ifdef FEAT_MBYTE
9349 if (has_mbyte)
9350 {
9351 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009352 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009353 {
9354 o += (*mb_ptr2cells)(buffer + i);
9355 if (this_ru_col + o > WITH_WIDTH(width))
9356 {
9357 buffer[i] = NUL;
9358 break;
9359 }
9360 }
9361 }
9362 else
9363#endif
9364 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
9365 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
9366
9367 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
9368 i = redraw_cmdline;
9369 screen_fill(row, row + 1,
9370 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
9371 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
9372 fillchar, fillchar, attr);
9373 /* don't redraw the cmdline because of showing the ruler */
9374 redraw_cmdline = i;
9375 wp->w_ru_cursor = wp->w_cursor;
9376 wp->w_ru_virtcol = wp->w_virtcol;
9377 wp->w_ru_empty = empty_line;
9378 wp->w_ru_topline = wp->w_topline;
9379 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
9380#ifdef FEAT_DIFF
9381 wp->w_ru_topfill = wp->w_topfill;
9382#endif
9383 }
9384}
9385#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +00009386
9387#if defined(FEAT_LINEBREAK) || defined(PROTO)
9388/*
9389 * Return the width of the 'number' column.
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009390 * Caller may need to check if 'number' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +00009391 * Otherwise it depends on 'numberwidth' and the line count.
9392 */
9393 int
9394number_width(wp)
9395 win_T *wp;
9396{
9397 int n;
9398 linenr_T lnum;
9399
Bram Moolenaar592e0a22004-07-03 16:05:59 +00009400 lnum = wp->w_buffer->b_ml.ml_line_count;
9401 if (lnum == wp->w_nrwidth_line_count)
9402 return wp->w_nrwidth_width;
9403 wp->w_nrwidth_line_count = lnum;
9404
9405 n = 0;
9406 do
9407 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009408 lnum /= 10;
9409 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00009410 } while (lnum > 0);
9411
9412 /* 'numberwidth' gives the minimal width plus one */
9413 if (n < wp->w_p_nuw - 1)
9414 n = wp->w_p_nuw - 1;
9415
9416 wp->w_nrwidth_width = n;
9417 return n;
9418}
9419#endif