xref: /vim-8.2.3635/src/screen.c (revision cf2d8dee)
1 /* 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 and
29  * ScreenLinesC[][] is not used.  When the character occupies two display
30  * cells the next byte in ScreenLines[] is 0.
31  * ScreenLinesC[][] contain up to 'maxcombine' composing characters
32  * (drawn on top of the first character).  There is 0 after the last one used.
33  * ScreenLines2[] is only used for euc-jp to store the second byte if the
34  * first byte is 0x8e (single-width character).
35  *
36  * The screen_*() functions write to the screen and handle updating
37  * ScreenLines[].
38  *
39  * update_screen() is the function that updates all windows and status lines.
40  * It is called form the main loop when must_redraw is non-zero.  It may be
41  * called from other places when an immediate screen update is needed.
42  *
43  * The part of the buffer that is displayed in a window is set with:
44  * - w_topline (first buffer line in window)
45  * - w_topfill (filler lines above the first line)
46  * - w_leftcol (leftmost window cell in window),
47  * - w_skipcol (skipped window cells of first line)
48  *
49  * Commands that only move the cursor around in a window, do not need to take
50  * action to update the display.  The main loop will check if w_topline is
51  * valid and update it (scroll the window) when needed.
52  *
53  * Commands that scroll a window change w_topline and must call
54  * check_cursor() to move the cursor into the visible part of the window, and
55  * call redraw_later(VALID) to have the window displayed by update_screen()
56  * later.
57  *
58  * Commands that change text in the buffer must call changed_bytes() or
59  * changed_lines() to mark the area that changed and will require updating
60  * later.  The main loop will call update_screen(), which will update each
61  * window that shows the changed buffer.  This assumes text above the change
62  * can remain displayed as it is.  Text after the change may need updating for
63  * scrolling, folding and syntax highlighting.
64  *
65  * Commands that change how a window is displayed (e.g., setting 'list') or
66  * invalidate the contents of a window in another way (e.g., change fold
67  * settings), must call redraw_later(NOT_VALID) to have the whole window
68  * redisplayed by update_screen() later.
69  *
70  * Commands that change how a buffer is displayed (e.g., setting 'tabstop')
71  * must call redraw_curbuf_later(NOT_VALID) to have all the windows for the
72  * buffer redisplayed by update_screen() later.
73  *
74  * Commands that change highlighting and possibly cause a scroll too must call
75  * redraw_later(SOME_VALID) to update the whole window but still use scrolling
76  * to avoid redrawing everything.  But the length of displayed lines must not
77  * change, use NOT_VALID then.
78  *
79  * Commands that move the window position must call redraw_later(NOT_VALID).
80  * TODO: should minimize redrawing by scrolling when possible.
81  *
82  * Commands that change everything (e.g., resizing the screen) must call
83  * redraw_all_later(NOT_VALID) or redraw_all_later(CLEAR).
84  *
85  * Things that are handled indirectly:
86  * - When messages scroll the screen up, msg_scrolled will be set and
87  *   update_screen() called to redraw.
88  */
89 
90 #include "vim.h"
91 
92 #define MB_FILLER_CHAR '<'  /* character used when a double-width character
93 			     * doesn't fit. */
94 
95 /*
96  * The attributes that are actually active for writing to the screen.
97  */
98 static int	screen_attr = 0;
99 
100 /*
101  * Positioning the cursor is reduced by remembering the last position.
102  * Mostly used by windgoto() and screen_char().
103  */
104 static int	screen_cur_row, screen_cur_col;	/* last known cursor position */
105 
106 #ifdef FEAT_SEARCH_EXTRA
107 static match_T search_hl;	/* used for 'hlsearch' highlight matching */
108 #endif
109 
110 #ifdef FEAT_FOLDING
111 static foldinfo_T win_foldinfo;	/* info for 'foldcolumn' */
112 static int compute_foldcolumn(win_T *wp, int col);
113 #endif
114 
115 /*
116  * Buffer for one screen line (characters and attributes).
117  */
118 static schar_T	*current_ScreenLine;
119 
120 static void win_update(win_T *wp);
121 static void win_draw_end(win_T *wp, int c1, int c2, int row, int endrow, hlf_T hl);
122 #ifdef FEAT_FOLDING
123 static void fold_line(win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row);
124 static void fill_foldcolumn(char_u *p, win_T *wp, int closed, linenr_T lnum);
125 static void copy_text_attr(int off, char_u *buf, int len, int attr);
126 #endif
127 static int win_line(win_T *, linenr_T, int, int, int nochange);
128 static int char_needs_redraw(int off_from, int off_to, int cols);
129 #ifdef FEAT_RIGHTLEFT
130 static void screen_line(int row, int coloff, int endcol, int clear_width, int rlflag);
131 # define SCREEN_LINE(r, o, e, c, rl)    screen_line((r), (o), (e), (c), (rl))
132 #else
133 static void screen_line(int row, int coloff, int endcol, int clear_width);
134 # define SCREEN_LINE(r, o, e, c, rl)    screen_line((r), (o), (e), (c))
135 #endif
136 #ifdef FEAT_WINDOWS
137 static void draw_vsep_win(win_T *wp, int row);
138 #endif
139 #ifdef FEAT_STL_OPT
140 static void redraw_custom_statusline(win_T *wp);
141 #endif
142 #ifdef FEAT_SEARCH_EXTRA
143 # define SEARCH_HL_PRIORITY 0
144 static void start_search_hl(void);
145 static void end_search_hl(void);
146 static void init_search_hl(win_T *wp);
147 static void prepare_search_hl(win_T *wp, linenr_T lnum);
148 static void next_search_hl(win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol, matchitem_T *cur);
149 static int next_search_hl_pos(match_T *shl, linenr_T lnum, posmatch_T *pos, colnr_T mincol);
150 #endif
151 static void screen_start_highlight(int attr);
152 static void screen_char(unsigned off, int row, int col);
153 #ifdef FEAT_MBYTE
154 static void screen_char_2(unsigned off, int row, int col);
155 #endif
156 static void screenclear2(void);
157 static void lineclear(unsigned off, int width);
158 static void lineinvalid(unsigned off, int width);
159 #ifdef FEAT_WINDOWS
160 static void linecopy(int to, int from, win_T *wp);
161 static void redraw_block(int row, int end, win_T *wp);
162 #endif
163 static int win_do_lines(win_T *wp, int row, int line_count, int mayclear, int del);
164 static void win_rest_invalid(win_T *wp);
165 static void msg_pos_mode(void);
166 static void recording_mode(int attr);
167 #if defined(FEAT_WINDOWS)
168 static void draw_tabline(void);
169 #endif
170 #if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
171 static int fillchar_status(int *attr, int is_curwin);
172 #endif
173 #ifdef FEAT_WINDOWS
174 static int fillchar_vsep(int *attr);
175 #endif
176 #ifdef FEAT_STL_OPT
177 static void win_redr_custom(win_T *wp, int draw_ruler);
178 #endif
179 #ifdef FEAT_CMDL_INFO
180 static void win_redr_ruler(win_T *wp, int always);
181 #endif
182 
183 #if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS)
184 /* Ugly global: overrule attribute used by screen_char() */
185 static int screen_char_attr = 0;
186 #endif
187 
188 /*
189  * Redraw the current window later, with update_screen(type).
190  * Set must_redraw only if not already set to a higher value.
191  * e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
192  */
193     void
194 redraw_later(int type)
195 {
196     redraw_win_later(curwin, type);
197 }
198 
199     void
200 redraw_win_later(
201     win_T	*wp,
202     int		type)
203 {
204     if (wp->w_redr_type < type)
205     {
206 	wp->w_redr_type = type;
207 	if (type >= NOT_VALID)
208 	    wp->w_lines_valid = 0;
209 	if (must_redraw < type)	/* must_redraw is the maximum of all windows */
210 	    must_redraw = type;
211     }
212 }
213 
214 /*
215  * Force a complete redraw later.  Also resets the highlighting.  To be used
216  * after executing a shell command that messes up the screen.
217  */
218     void
219 redraw_later_clear(void)
220 {
221     redraw_all_later(CLEAR);
222 #ifdef FEAT_GUI
223     if (gui.in_use)
224 	/* Use a code that will reset gui.highlight_mask in
225 	 * gui_stop_highlight(). */
226 	screen_attr = HL_ALL + 1;
227     else
228 #endif
229 	/* Use attributes that is very unlikely to appear in text. */
230 	screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE;
231 }
232 
233 /*
234  * Mark all windows to be redrawn later.
235  */
236     void
237 redraw_all_later(int type)
238 {
239     win_T	*wp;
240 
241     FOR_ALL_WINDOWS(wp)
242     {
243 	redraw_win_later(wp, type);
244     }
245 }
246 
247 /*
248  * Mark all windows that are editing the current buffer to be updated later.
249  */
250     void
251 redraw_curbuf_later(int type)
252 {
253     redraw_buf_later(curbuf, type);
254 }
255 
256     void
257 redraw_buf_later(buf_T *buf, int type)
258 {
259     win_T	*wp;
260 
261     FOR_ALL_WINDOWS(wp)
262     {
263 	if (wp->w_buffer == buf)
264 	    redraw_win_later(wp, type);
265     }
266 }
267 
268 /*
269  * Redraw as soon as possible.  When the command line is not scrolled redraw
270  * right away and restore what was on the command line.
271  * Return a code indicating what happened.
272  */
273     int
274 redraw_asap(int type)
275 {
276     int		rows;
277     int		cols = screen_Columns;
278     int		r;
279     int		ret = 0;
280     schar_T	*screenline;	/* copy from ScreenLines[] */
281     sattr_T	*screenattr;	/* copy from ScreenAttrs[] */
282 #ifdef FEAT_MBYTE
283     int		i;
284     u8char_T	*screenlineUC = NULL;	/* copy from ScreenLinesUC[] */
285     u8char_T	*screenlineC[MAX_MCO];	/* copy from ScreenLinesC[][] */
286     schar_T	*screenline2 = NULL;	/* copy from ScreenLines2[] */
287 #endif
288 
289     redraw_later(type);
290     if (msg_scrolled || (State != NORMAL && State != NORMAL_BUSY) || exiting)
291 	return ret;
292 
293     /* Allocate space to save the text displayed in the command line area. */
294     rows = screen_Rows - cmdline_row;
295     screenline = (schar_T *)lalloc(
296 			   (long_u)(rows * cols * sizeof(schar_T)), FALSE);
297     screenattr = (sattr_T *)lalloc(
298 			   (long_u)(rows * cols * sizeof(sattr_T)), FALSE);
299     if (screenline == NULL || screenattr == NULL)
300 	ret = 2;
301 #ifdef FEAT_MBYTE
302     if (enc_utf8)
303     {
304 	screenlineUC = (u8char_T *)lalloc(
305 			  (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
306 	if (screenlineUC == NULL)
307 	    ret = 2;
308 	for (i = 0; i < p_mco; ++i)
309 	{
310 	    screenlineC[i] = (u8char_T *)lalloc(
311 			  (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
312 	    if (screenlineC[i] == NULL)
313 		ret = 2;
314 	}
315     }
316     if (enc_dbcs == DBCS_JPNU)
317     {
318 	screenline2 = (schar_T *)lalloc(
319 			   (long_u)(rows * cols * sizeof(schar_T)), FALSE);
320 	if (screenline2 == NULL)
321 	    ret = 2;
322     }
323 #endif
324 
325     if (ret != 2)
326     {
327 	/* Save the text displayed in the command line area. */
328 	for (r = 0; r < rows; ++r)
329 	{
330 	    mch_memmove(screenline + r * cols,
331 			ScreenLines + LineOffset[cmdline_row + r],
332 			(size_t)cols * sizeof(schar_T));
333 	    mch_memmove(screenattr + r * cols,
334 			ScreenAttrs + LineOffset[cmdline_row + r],
335 			(size_t)cols * sizeof(sattr_T));
336 #ifdef FEAT_MBYTE
337 	    if (enc_utf8)
338 	    {
339 		mch_memmove(screenlineUC + r * cols,
340 			    ScreenLinesUC + LineOffset[cmdline_row + r],
341 			    (size_t)cols * sizeof(u8char_T));
342 		for (i = 0; i < p_mco; ++i)
343 		    mch_memmove(screenlineC[i] + r * cols,
344 				ScreenLinesC[i] + LineOffset[cmdline_row + r],
345 				(size_t)cols * sizeof(u8char_T));
346 	    }
347 	    if (enc_dbcs == DBCS_JPNU)
348 		mch_memmove(screenline2 + r * cols,
349 			    ScreenLines2 + LineOffset[cmdline_row + r],
350 			    (size_t)cols * sizeof(schar_T));
351 #endif
352 	}
353 
354 	update_screen(0);
355 	ret = 3;
356 
357 	if (must_redraw == 0)
358 	{
359 	    int	off = (int)(current_ScreenLine - ScreenLines);
360 
361 	    /* Restore the text displayed in the command line area. */
362 	    for (r = 0; r < rows; ++r)
363 	    {
364 		mch_memmove(current_ScreenLine,
365 			    screenline + r * cols,
366 			    (size_t)cols * sizeof(schar_T));
367 		mch_memmove(ScreenAttrs + off,
368 			    screenattr + r * cols,
369 			    (size_t)cols * sizeof(sattr_T));
370 #ifdef FEAT_MBYTE
371 		if (enc_utf8)
372 		{
373 		    mch_memmove(ScreenLinesUC + off,
374 				screenlineUC + r * cols,
375 				(size_t)cols * sizeof(u8char_T));
376 		    for (i = 0; i < p_mco; ++i)
377 			mch_memmove(ScreenLinesC[i] + off,
378 				    screenlineC[i] + r * cols,
379 				    (size_t)cols * sizeof(u8char_T));
380 		}
381 		if (enc_dbcs == DBCS_JPNU)
382 		    mch_memmove(ScreenLines2 + off,
383 				screenline2 + r * cols,
384 				(size_t)cols * sizeof(schar_T));
385 #endif
386 		SCREEN_LINE(cmdline_row + r, 0, cols, cols, FALSE);
387 	    }
388 	    ret = 4;
389 	}
390     }
391 
392     vim_free(screenline);
393     vim_free(screenattr);
394 #ifdef FEAT_MBYTE
395     if (enc_utf8)
396     {
397 	vim_free(screenlineUC);
398 	for (i = 0; i < p_mco; ++i)
399 	    vim_free(screenlineC[i]);
400     }
401     if (enc_dbcs == DBCS_JPNU)
402 	vim_free(screenline2);
403 #endif
404 
405     /* Show the intro message when appropriate. */
406     maybe_intro_message();
407 
408     setcursor();
409 
410     return ret;
411 }
412 
413 /*
414  * Invoked after an asynchronous callback is called.
415  * If an echo command was used the cursor needs to be put back where
416  * it belongs. If highlighting was changed a redraw is needed.
417  */
418     void
419 redraw_after_callback()
420 {
421     if (State == HITRETURN || State == ASKMORE)
422 	; /* do nothing */
423     else if (State & CMDLINE)
424 	redrawcmdline();
425     else if ((State & NORMAL) || (State & INSERT))
426     {
427 	update_screen(0);
428 	setcursor();
429     }
430     cursor_on();
431     out_flush();
432 #ifdef FEAT_GUI
433     if (gui.in_use)
434     {
435 	gui_update_cursor(TRUE, FALSE);
436 	gui_mch_flush();
437     }
438 #endif
439 }
440 
441 /*
442  * Changed something in the current window, at buffer line "lnum", that
443  * requires that line and possibly other lines to be redrawn.
444  * Used when entering/leaving Insert mode with the cursor on a folded line.
445  * Used to remove the "$" from a change command.
446  * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
447  * may become invalid and the whole window will have to be redrawn.
448  */
449     void
450 redrawWinline(
451     linenr_T	lnum,
452     int		invalid UNUSED)	/* window line height is invalid now */
453 {
454 #ifdef FEAT_FOLDING
455     int		i;
456 #endif
457 
458     if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
459 	curwin->w_redraw_top = lnum;
460     if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
461 	curwin->w_redraw_bot = lnum;
462     redraw_later(VALID);
463 
464 #ifdef FEAT_FOLDING
465     if (invalid)
466     {
467 	/* A w_lines[] entry for this lnum has become invalid. */
468 	i = find_wl_entry(curwin, lnum);
469 	if (i >= 0)
470 	    curwin->w_lines[i].wl_valid = FALSE;
471     }
472 #endif
473 }
474 
475 /*
476  * update all windows that are editing the current buffer
477  */
478     void
479 update_curbuf(int type)
480 {
481     redraw_curbuf_later(type);
482     update_screen(type);
483 }
484 
485 /*
486  * update_screen()
487  *
488  * Based on the current value of curwin->w_topline, transfer a screenfull
489  * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
490  */
491     void
492 update_screen(int type)
493 {
494     win_T	*wp;
495     static int	did_intro = FALSE;
496 #if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
497     int		did_one;
498 #endif
499 
500     /* Don't do anything if the screen structures are (not yet) valid. */
501     if (!screen_valid(TRUE))
502 	return;
503 
504     if (must_redraw)
505     {
506 	if (type < must_redraw)	    /* use maximal type */
507 	    type = must_redraw;
508 
509 	/* must_redraw is reset here, so that when we run into some weird
510 	 * reason to redraw while busy redrawing (e.g., asynchronous
511 	 * scrolling), or update_topline() in win_update() will cause a
512 	 * scroll, the screen will be redrawn later or in win_update(). */
513 	must_redraw = 0;
514     }
515 
516     /* Need to update w_lines[]. */
517     if (curwin->w_lines_valid == 0 && type < NOT_VALID)
518 	type = NOT_VALID;
519 
520     /* Postpone the redrawing when it's not needed and when being called
521      * recursively. */
522     if (!redrawing() || updating_screen)
523     {
524 	redraw_later(type);		/* remember type for next time */
525 	must_redraw = type;
526 	if (type > INVERTED_ALL)
527 	    curwin->w_lines_valid = 0;	/* don't use w_lines[].wl_size now */
528 	return;
529     }
530 
531     updating_screen = TRUE;
532 #ifdef FEAT_SYN_HL
533     ++display_tick;	    /* let syntax code know we're in a next round of
534 			     * display updating */
535 #endif
536 
537     /*
538      * if the screen was scrolled up when displaying a message, scroll it down
539      */
540     if (msg_scrolled)
541     {
542 	clear_cmdline = TRUE;
543 	if (msg_scrolled > Rows - 5)	    /* clearing is faster */
544 	    type = CLEAR;
545 	else if (type != CLEAR)
546 	{
547 	    check_for_delay(FALSE);
548 	    if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, NULL) == FAIL)
549 		type = CLEAR;
550 	    FOR_ALL_WINDOWS(wp)
551 	    {
552 		if (W_WINROW(wp) < msg_scrolled)
553 		{
554 		    if (W_WINROW(wp) + wp->w_height > msg_scrolled
555 			    && wp->w_redr_type < REDRAW_TOP
556 			    && wp->w_lines_valid > 0
557 			    && wp->w_topline == wp->w_lines[0].wl_lnum)
558 		    {
559 			wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
560 			wp->w_redr_type = REDRAW_TOP;
561 		    }
562 		    else
563 		    {
564 			wp->w_redr_type = NOT_VALID;
565 #ifdef FEAT_WINDOWS
566 			if (W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp)
567 				<= msg_scrolled)
568 			    wp->w_redr_status = TRUE;
569 #endif
570 		    }
571 		}
572 	    }
573 	    redraw_cmdline = TRUE;
574 #ifdef FEAT_WINDOWS
575 	    redraw_tabline = TRUE;
576 #endif
577 	}
578 	msg_scrolled = 0;
579 	need_wait_return = FALSE;
580     }
581 
582     /* reset cmdline_row now (may have been changed temporarily) */
583     compute_cmdrow();
584 
585     /* Check for changed highlighting */
586     if (need_highlight_changed)
587 	highlight_changed();
588 
589     if (type == CLEAR)		/* first clear screen */
590     {
591 	screenclear();		/* will reset clear_cmdline */
592 	type = NOT_VALID;
593     }
594 
595     if (clear_cmdline)		/* going to clear cmdline (done below) */
596 	check_for_delay(FALSE);
597 
598 #ifdef FEAT_LINEBREAK
599     /* Force redraw when width of 'number' or 'relativenumber' column
600      * changes. */
601     if (curwin->w_redr_type < NOT_VALID
602 	   && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
603 				    ? number_width(curwin) : 0))
604 	curwin->w_redr_type = NOT_VALID;
605 #endif
606 
607     /*
608      * Only start redrawing if there is really something to do.
609      */
610     if (type == INVERTED)
611 	update_curswant();
612     if (curwin->w_redr_type < type
613 	    && !((type == VALID
614 		    && curwin->w_lines[0].wl_valid
615 #ifdef FEAT_DIFF
616 		    && curwin->w_topfill == curwin->w_old_topfill
617 		    && curwin->w_botfill == curwin->w_old_botfill
618 #endif
619 		    && curwin->w_topline == curwin->w_lines[0].wl_lnum)
620 		|| (type == INVERTED
621 		    && VIsual_active
622 		    && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
623 		    && curwin->w_old_visual_mode == VIsual_mode
624 		    && (curwin->w_valid & VALID_VIRTCOL)
625 		    && curwin->w_old_curswant == curwin->w_curswant)
626 		))
627 	curwin->w_redr_type = type;
628 
629 #ifdef FEAT_WINDOWS
630     /* Redraw the tab pages line if needed. */
631     if (redraw_tabline || type >= NOT_VALID)
632 	draw_tabline();
633 #endif
634 
635 #ifdef FEAT_SYN_HL
636     /*
637      * Correct stored syntax highlighting info for changes in each displayed
638      * buffer.  Each buffer must only be done once.
639      */
640     FOR_ALL_WINDOWS(wp)
641     {
642 	if (wp->w_buffer->b_mod_set)
643 	{
644 # ifdef FEAT_WINDOWS
645 	    win_T	*wwp;
646 
647 	    /* Check if we already did this buffer. */
648 	    for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
649 		if (wwp->w_buffer == wp->w_buffer)
650 		    break;
651 # endif
652 	    if (
653 # ifdef FEAT_WINDOWS
654 		    wwp == wp &&
655 # endif
656 		    syntax_present(wp))
657 		syn_stack_apply_changes(wp->w_buffer);
658 	}
659     }
660 #endif
661 
662     /*
663      * Go from top to bottom through the windows, redrawing the ones that need
664      * it.
665      */
666 #if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
667     did_one = FALSE;
668 #endif
669 #ifdef FEAT_SEARCH_EXTRA
670     search_hl.rm.regprog = NULL;
671 #endif
672     FOR_ALL_WINDOWS(wp)
673     {
674 	if (wp->w_redr_type != 0)
675 	{
676 	    cursor_off();
677 #if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
678 	    if (!did_one)
679 	    {
680 		did_one = TRUE;
681 # ifdef FEAT_SEARCH_EXTRA
682 		start_search_hl();
683 # endif
684 # ifdef FEAT_CLIPBOARD
685 		/* When Visual area changed, may have to update selection. */
686 		if (clip_star.available && clip_isautosel_star())
687 		    clip_update_selection(&clip_star);
688 		if (clip_plus.available && clip_isautosel_plus())
689 		    clip_update_selection(&clip_plus);
690 # endif
691 #ifdef FEAT_GUI
692 		/* Remove the cursor before starting to do anything, because
693 		 * scrolling may make it difficult to redraw the text under
694 		 * it. */
695 		if (gui.in_use)
696 		    gui_undraw_cursor();
697 #endif
698 	    }
699 #endif
700 	    win_update(wp);
701 	}
702 
703 #ifdef FEAT_WINDOWS
704 	/* redraw status line after the window to minimize cursor movement */
705 	if (wp->w_redr_status)
706 	{
707 	    cursor_off();
708 	    win_redr_status(wp);
709 	}
710 #endif
711     }
712 #if defined(FEAT_SEARCH_EXTRA)
713     end_search_hl();
714 #endif
715 #ifdef FEAT_INS_EXPAND
716     /* May need to redraw the popup menu. */
717     if (pum_visible())
718 	pum_redraw();
719 #endif
720 
721 #ifdef FEAT_WINDOWS
722     /* Reset b_mod_set flags.  Going through all windows is probably faster
723      * than going through all buffers (there could be many buffers). */
724     for (wp = firstwin; wp != NULL; wp = wp->w_next)
725 	wp->w_buffer->b_mod_set = FALSE;
726 #else
727 	curbuf->b_mod_set = FALSE;
728 #endif
729 
730     updating_screen = FALSE;
731 #ifdef FEAT_GUI
732     gui_may_resize_shell();
733 #endif
734 
735     /* Clear or redraw the command line.  Done last, because scrolling may
736      * mess up the command line. */
737     if (clear_cmdline || redraw_cmdline)
738 	showmode();
739 
740     /* May put up an introductory message when not editing a file */
741     if (!did_intro)
742 	maybe_intro_message();
743     did_intro = TRUE;
744 
745 #ifdef FEAT_GUI
746     /* Redraw the cursor and update the scrollbars when all screen updating is
747      * done. */
748     if (gui.in_use)
749     {
750 	out_flush();	/* required before updating the cursor */
751 	if (did_one)
752 	    gui_update_cursor(FALSE, FALSE);
753 	gui_update_scrollbars(FALSE);
754     }
755 #endif
756 }
757 
758 #if defined(FEAT_CONCEAL) || defined(PROTO)
759 /*
760  * Return TRUE if the cursor line in window "wp" may be concealed, according
761  * to the 'concealcursor' option.
762  */
763     int
764 conceal_cursor_line(win_T *wp)
765 {
766     int		c;
767 
768     if (*wp->w_p_cocu == NUL)
769 	return FALSE;
770     if (get_real_state() & VISUAL)
771 	c = 'v';
772     else if (State & INSERT)
773 	c = 'i';
774     else if (State & NORMAL)
775 	c = 'n';
776     else if (State & CMDLINE)
777 	c = 'c';
778     else
779 	return FALSE;
780     return vim_strchr(wp->w_p_cocu, c) != NULL;
781 }
782 
783 /*
784  * Check if the cursor line needs to be redrawn because of 'concealcursor'.
785  */
786     void
787 conceal_check_cursur_line(void)
788 {
789     if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
790     {
791 	need_cursor_line_redraw = TRUE;
792 	/* Need to recompute cursor column, e.g., when starting Visual mode
793 	 * without concealing. */
794 	curs_columns(TRUE);
795     }
796 }
797 
798     void
799 update_single_line(win_T *wp, linenr_T lnum)
800 {
801     int		row;
802     int		j;
803 
804     if (lnum >= wp->w_topline && lnum < wp->w_botline
805 				 && foldedCount(wp, lnum, &win_foldinfo) == 0)
806     {
807 # ifdef FEAT_GUI
808 	/* Remove the cursor before starting to do anything, because scrolling
809 	 * may make it difficult to redraw the text under it. */
810 	if (gui.in_use)
811 	    gui_undraw_cursor();
812 # endif
813 	row = 0;
814 	for (j = 0; j < wp->w_lines_valid; ++j)
815 	{
816 	    if (lnum == wp->w_lines[j].wl_lnum)
817 	    {
818 		screen_start();	/* not sure of screen cursor */
819 # ifdef FEAT_SEARCH_EXTRA
820 		init_search_hl(wp);
821 		start_search_hl();
822 		prepare_search_hl(wp, lnum);
823 # endif
824 		win_line(wp, lnum, row, row + wp->w_lines[j].wl_size, FALSE);
825 # if defined(FEAT_SEARCH_EXTRA)
826 		end_search_hl();
827 # endif
828 		break;
829 	    }
830 	    row += wp->w_lines[j].wl_size;
831 	}
832 # ifdef FEAT_GUI
833 	/* Redraw the cursor */
834 	if (gui.in_use)
835 	{
836 	    out_flush();	/* required before updating the cursor */
837 	    gui_update_cursor(FALSE, FALSE);
838 	}
839 # endif
840     }
841     need_cursor_line_redraw = FALSE;
842 }
843 #endif
844 
845 #if defined(FEAT_SIGNS) || defined(FEAT_GUI)
846 static void update_prepare(void);
847 static void update_finish(void);
848 
849 /*
850  * Prepare for updating one or more windows.
851  * Caller must check for "updating_screen" already set to avoid recursiveness.
852  */
853     static void
854 update_prepare(void)
855 {
856     cursor_off();
857     updating_screen = TRUE;
858 #ifdef FEAT_GUI
859     /* Remove the cursor before starting to do anything, because scrolling may
860      * make it difficult to redraw the text under it. */
861     if (gui.in_use)
862 	gui_undraw_cursor();
863 #endif
864 #ifdef FEAT_SEARCH_EXTRA
865     start_search_hl();
866 #endif
867 }
868 
869 /*
870  * Finish updating one or more windows.
871  */
872     static void
873 update_finish(void)
874 {
875     if (redraw_cmdline)
876 	showmode();
877 
878 # ifdef FEAT_SEARCH_EXTRA
879     end_search_hl();
880 # endif
881 
882     updating_screen = FALSE;
883 
884 # ifdef FEAT_GUI
885     gui_may_resize_shell();
886 
887     /* Redraw the cursor and update the scrollbars when all screen updating is
888      * done. */
889     if (gui.in_use)
890     {
891 	out_flush();	/* required before updating the cursor */
892 	gui_update_cursor(FALSE, FALSE);
893 	gui_update_scrollbars(FALSE);
894     }
895 # endif
896 }
897 #endif
898 
899 #if defined(FEAT_SIGNS) || defined(PROTO)
900     void
901 update_debug_sign(buf_T *buf, linenr_T lnum)
902 {
903     win_T	*wp;
904     int		doit = FALSE;
905 
906 # ifdef FEAT_FOLDING
907     win_foldinfo.fi_level = 0;
908 # endif
909 
910     /* update/delete a specific mark */
911     FOR_ALL_WINDOWS(wp)
912     {
913 	if (buf != NULL && lnum > 0)
914 	{
915 	    if (wp->w_buffer == buf && lnum >= wp->w_topline
916 						      && lnum < wp->w_botline)
917 	    {
918 		if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
919 		    wp->w_redraw_top = lnum;
920 		if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
921 		    wp->w_redraw_bot = lnum;
922 		redraw_win_later(wp, VALID);
923 	    }
924 	}
925 	else
926 	    redraw_win_later(wp, VALID);
927 	if (wp->w_redr_type != 0)
928 	    doit = TRUE;
929     }
930 
931     /* Return when there is nothing to do, screen updating is already
932      * happening (recursive call) or still starting up. */
933     if (!doit || updating_screen
934 #ifdef FEAT_GUI
935 	    || gui.starting
936 #endif
937 	    || starting)
938 	return;
939 
940     /* update all windows that need updating */
941     update_prepare();
942 
943 # ifdef FEAT_WINDOWS
944     for (wp = firstwin; wp; wp = wp->w_next)
945     {
946 	if (wp->w_redr_type != 0)
947 	    win_update(wp);
948 	if (wp->w_redr_status)
949 	    win_redr_status(wp);
950     }
951 # else
952     if (curwin->w_redr_type != 0)
953 	win_update(curwin);
954 # endif
955 
956     update_finish();
957 }
958 #endif
959 
960 
961 #if defined(FEAT_GUI) || defined(PROTO)
962 /*
963  * Update a single window, its status line and maybe the command line msg.
964  * Used for the GUI scrollbar.
965  */
966     void
967 updateWindow(win_T *wp)
968 {
969     /* return if already busy updating */
970     if (updating_screen)
971 	return;
972 
973     update_prepare();
974 
975 #ifdef FEAT_CLIPBOARD
976     /* When Visual area changed, may have to update selection. */
977     if (clip_star.available && clip_isautosel_star())
978 	clip_update_selection(&clip_star);
979     if (clip_plus.available && clip_isautosel_plus())
980 	clip_update_selection(&clip_plus);
981 #endif
982 
983     win_update(wp);
984 
985 #ifdef FEAT_WINDOWS
986     /* When the screen was cleared redraw the tab pages line. */
987     if (redraw_tabline)
988 	draw_tabline();
989 
990     if (wp->w_redr_status
991 # ifdef FEAT_CMDL_INFO
992 	    || p_ru
993 # endif
994 # ifdef FEAT_STL_OPT
995 	    || *p_stl != NUL || *wp->w_p_stl != NUL
996 # endif
997 	    )
998 	win_redr_status(wp);
999 #endif
1000 
1001     update_finish();
1002 }
1003 #endif
1004 
1005 /*
1006  * Update a single window.
1007  *
1008  * This may cause the windows below it also to be redrawn (when clearing the
1009  * screen or scrolling lines).
1010  *
1011  * How the window is redrawn depends on wp->w_redr_type.  Each type also
1012  * implies the one below it.
1013  * NOT_VALID	redraw the whole window
1014  * SOME_VALID	redraw the whole window but do scroll when possible
1015  * REDRAW_TOP	redraw the top w_upd_rows window lines, otherwise like VALID
1016  * INVERTED	redraw the changed part of the Visual area
1017  * INVERTED_ALL	redraw the whole Visual area
1018  * VALID	1. scroll up/down to adjust for a changed w_topline
1019  *		2. update lines at the top when scrolled down
1020  *		3. redraw changed text:
1021  *		   - if wp->w_buffer->b_mod_set set, update lines between
1022  *		     b_mod_top and b_mod_bot.
1023  *		   - if wp->w_redraw_top non-zero, redraw lines between
1024  *		     wp->w_redraw_top and wp->w_redr_bot.
1025  *		   - continue redrawing when syntax status is invalid.
1026  *		4. if scrolled up, update lines at the bottom.
1027  * This results in three areas that may need updating:
1028  * top:	from first row to top_end (when scrolled down)
1029  * mid: from mid_start to mid_end (update inversion or changed text)
1030  * bot: from bot_start to last row (when scrolled up)
1031  */
1032     static void
1033 win_update(win_T *wp)
1034 {
1035     buf_T	*buf = wp->w_buffer;
1036     int		type;
1037     int		top_end = 0;	/* Below last row of the top area that needs
1038 				   updating.  0 when no top area updating. */
1039     int		mid_start = 999;/* first row of the mid area that needs
1040 				   updating.  999 when no mid area updating. */
1041     int		mid_end = 0;	/* Below last row of the mid area that needs
1042 				   updating.  0 when no mid area updating. */
1043     int		bot_start = 999;/* first row of the bot area that needs
1044 				   updating.  999 when no bot area updating */
1045     int		scrolled_down = FALSE;	/* TRUE when scrolled down when
1046 					   w_topline got smaller a bit */
1047 #ifdef FEAT_SEARCH_EXTRA
1048     matchitem_T *cur;		/* points to the match list */
1049     int		top_to_mod = FALSE;    /* redraw above mod_top */
1050 #endif
1051 
1052     int		row;		/* current window row to display */
1053     linenr_T	lnum;		/* current buffer lnum to display */
1054     int		idx;		/* current index in w_lines[] */
1055     int		srow;		/* starting row of the current line */
1056 
1057     int		eof = FALSE;	/* if TRUE, we hit the end of the file */
1058     int		didline = FALSE; /* if TRUE, we finished the last line */
1059     int		i;
1060     long	j;
1061     static int	recursive = FALSE;	/* being called recursively */
1062     int		old_botline = wp->w_botline;
1063 #ifdef FEAT_FOLDING
1064     long	fold_count;
1065 #endif
1066 #ifdef FEAT_SYN_HL
1067     /* remember what happened to the previous line, to know if
1068      * check_visual_highlight() can be used */
1069 #define DID_NONE 1	/* didn't update a line */
1070 #define DID_LINE 2	/* updated a normal line */
1071 #define DID_FOLD 3	/* updated a folded line */
1072     int		did_update = DID_NONE;
1073     linenr_T	syntax_last_parsed = 0;		/* last parsed text line */
1074 #endif
1075     linenr_T	mod_top = 0;
1076     linenr_T	mod_bot = 0;
1077 #if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1078     int		save_got_int;
1079 #endif
1080 
1081     type = wp->w_redr_type;
1082 
1083     if (type == NOT_VALID)
1084     {
1085 #ifdef FEAT_WINDOWS
1086 	wp->w_redr_status = TRUE;
1087 #endif
1088 	wp->w_lines_valid = 0;
1089     }
1090 
1091     /* Window is zero-height: nothing to draw. */
1092     if (wp->w_height == 0)
1093     {
1094 	wp->w_redr_type = 0;
1095 	return;
1096     }
1097 
1098 #ifdef FEAT_WINDOWS
1099     /* Window is zero-width: Only need to draw the separator. */
1100     if (wp->w_width == 0)
1101     {
1102 	/* draw the vertical separator right of this window */
1103 	draw_vsep_win(wp, 0);
1104 	wp->w_redr_type = 0;
1105 	return;
1106     }
1107 #endif
1108 
1109 #ifdef FEAT_SEARCH_EXTRA
1110     init_search_hl(wp);
1111 #endif
1112 
1113 #ifdef FEAT_LINEBREAK
1114     /* Force redraw when width of 'number' or 'relativenumber' column
1115      * changes. */
1116     i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
1117     if (wp->w_nrwidth != i)
1118     {
1119 	type = NOT_VALID;
1120 	wp->w_nrwidth = i;
1121     }
1122     else
1123 #endif
1124 
1125     if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1126     {
1127 	/*
1128 	 * When there are both inserted/deleted lines and specific lines to be
1129 	 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1130 	 * everything (only happens when redrawing is off for while).
1131 	 */
1132 	type = NOT_VALID;
1133     }
1134     else
1135     {
1136 	/*
1137 	 * Set mod_top to the first line that needs displaying because of
1138 	 * changes.  Set mod_bot to the first line after the changes.
1139 	 */
1140 	mod_top = wp->w_redraw_top;
1141 	if (wp->w_redraw_bot != 0)
1142 	    mod_bot = wp->w_redraw_bot + 1;
1143 	else
1144 	    mod_bot = 0;
1145 	wp->w_redraw_top = 0;	/* reset for next time */
1146 	wp->w_redraw_bot = 0;
1147 	if (buf->b_mod_set)
1148 	{
1149 	    if (mod_top == 0 || mod_top > buf->b_mod_top)
1150 	    {
1151 		mod_top = buf->b_mod_top;
1152 #ifdef FEAT_SYN_HL
1153 		/* Need to redraw lines above the change that may be included
1154 		 * in a pattern match. */
1155 		if (syntax_present(wp))
1156 		{
1157 		    mod_top -= buf->b_s.b_syn_sync_linebreaks;
1158 		    if (mod_top < 1)
1159 			mod_top = 1;
1160 		}
1161 #endif
1162 	    }
1163 	    if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1164 		mod_bot = buf->b_mod_bot;
1165 
1166 #ifdef FEAT_SEARCH_EXTRA
1167 	    /* When 'hlsearch' is on and using a multi-line search pattern, a
1168 	     * change in one line may make the Search highlighting in a
1169 	     * previous line invalid.  Simple solution: redraw all visible
1170 	     * lines above the change.
1171 	     * Same for a match pattern.
1172 	     */
1173 	    if (search_hl.rm.regprog != NULL
1174 					&& re_multiline(search_hl.rm.regprog))
1175 		top_to_mod = TRUE;
1176 	    else
1177 	    {
1178 		cur = wp->w_match_head;
1179 		while (cur != NULL)
1180 		{
1181 		    if (cur->match.regprog != NULL
1182 					   && re_multiline(cur->match.regprog))
1183 		    {
1184 			top_to_mod = TRUE;
1185 			break;
1186 		    }
1187 		    cur = cur->next;
1188 		}
1189 	    }
1190 #endif
1191 	}
1192 #ifdef FEAT_FOLDING
1193 	if (mod_top != 0 && hasAnyFolding(wp))
1194 	{
1195 	    linenr_T	lnumt, lnumb;
1196 
1197 	    /*
1198 	     * A change in a line can cause lines above it to become folded or
1199 	     * unfolded.  Find the top most buffer line that may be affected.
1200 	     * If the line was previously folded and displayed, get the first
1201 	     * line of that fold.  If the line is folded now, get the first
1202 	     * folded line.  Use the minimum of these two.
1203 	     */
1204 
1205 	    /* Find last valid w_lines[] entry above mod_top.  Set lnumt to
1206 	     * the line below it.  If there is no valid entry, use w_topline.
1207 	     * Find the first valid w_lines[] entry below mod_bot.  Set lnumb
1208 	     * to this line.  If there is no valid entry, use MAXLNUM. */
1209 	    lnumt = wp->w_topline;
1210 	    lnumb = MAXLNUM;
1211 	    for (i = 0; i < wp->w_lines_valid; ++i)
1212 		if (wp->w_lines[i].wl_valid)
1213 		{
1214 		    if (wp->w_lines[i].wl_lastlnum < mod_top)
1215 			lnumt = wp->w_lines[i].wl_lastlnum + 1;
1216 		    if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1217 		    {
1218 			lnumb = wp->w_lines[i].wl_lnum;
1219 			/* When there is a fold column it might need updating
1220 			 * in the next line ("J" just above an open fold). */
1221 			if (compute_foldcolumn(wp, 0) > 0)
1222 			    ++lnumb;
1223 		    }
1224 		}
1225 
1226 	    (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1227 	    if (mod_top > lnumt)
1228 		mod_top = lnumt;
1229 
1230 	    /* Now do the same for the bottom line (one above mod_bot). */
1231 	    --mod_bot;
1232 	    (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1233 	    ++mod_bot;
1234 	    if (mod_bot < lnumb)
1235 		mod_bot = lnumb;
1236 	}
1237 #endif
1238 
1239 	/* When a change starts above w_topline and the end is below
1240 	 * w_topline, start redrawing at w_topline.
1241 	 * If the end of the change is above w_topline: do like no change was
1242 	 * made, but redraw the first line to find changes in syntax. */
1243 	if (mod_top != 0 && mod_top < wp->w_topline)
1244 	{
1245 	    if (mod_bot > wp->w_topline)
1246 		mod_top = wp->w_topline;
1247 #ifdef FEAT_SYN_HL
1248 	    else if (syntax_present(wp))
1249 		top_end = 1;
1250 #endif
1251 	}
1252 
1253 	/* When line numbers are displayed need to redraw all lines below
1254 	 * inserted/deleted lines. */
1255 	if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1256 	    mod_bot = MAXLNUM;
1257     }
1258 
1259     /*
1260      * When only displaying the lines at the top, set top_end.  Used when
1261      * window has scrolled down for msg_scrolled.
1262      */
1263     if (type == REDRAW_TOP)
1264     {
1265 	j = 0;
1266 	for (i = 0; i < wp->w_lines_valid; ++i)
1267 	{
1268 	    j += wp->w_lines[i].wl_size;
1269 	    if (j >= wp->w_upd_rows)
1270 	    {
1271 		top_end = j;
1272 		break;
1273 	    }
1274 	}
1275 	if (top_end == 0)
1276 	    /* not found (cannot happen?): redraw everything */
1277 	    type = NOT_VALID;
1278 	else
1279 	    /* top area defined, the rest is VALID */
1280 	    type = VALID;
1281     }
1282 
1283     /* Trick: we want to avoid clearing the screen twice.  screenclear() will
1284      * set "screen_cleared" to TRUE.  The special value MAYBE (which is still
1285      * non-zero and thus not FALSE) will indicate that screenclear() was not
1286      * called. */
1287     if (screen_cleared)
1288 	screen_cleared = MAYBE;
1289 
1290     /*
1291      * If there are no changes on the screen that require a complete redraw,
1292      * handle three cases:
1293      * 1: we are off the top of the screen by a few lines: scroll down
1294      * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1295      * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1296      *    w_lines[] that needs updating.
1297      */
1298     if ((type == VALID || type == SOME_VALID
1299 				  || type == INVERTED || type == INVERTED_ALL)
1300 #ifdef FEAT_DIFF
1301 	    && !wp->w_botfill && !wp->w_old_botfill
1302 #endif
1303 	    )
1304     {
1305 	if (mod_top != 0 && wp->w_topline == mod_top)
1306 	{
1307 	    /*
1308 	     * w_topline is the first changed line, the scrolling will be done
1309 	     * further down.
1310 	     */
1311 	}
1312 	else if (wp->w_lines[0].wl_valid
1313 		&& (wp->w_topline < wp->w_lines[0].wl_lnum
1314 #ifdef FEAT_DIFF
1315 		    || (wp->w_topline == wp->w_lines[0].wl_lnum
1316 			&& wp->w_topfill > wp->w_old_topfill)
1317 #endif
1318 		   ))
1319 	{
1320 	    /*
1321 	     * New topline is above old topline: May scroll down.
1322 	     */
1323 #ifdef FEAT_FOLDING
1324 	    if (hasAnyFolding(wp))
1325 	    {
1326 		linenr_T ln;
1327 
1328 		/* count the number of lines we are off, counting a sequence
1329 		 * of folded lines as one */
1330 		j = 0;
1331 		for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1332 		{
1333 		    ++j;
1334 		    if (j >= wp->w_height - 2)
1335 			break;
1336 		    (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1337 		}
1338 	    }
1339 	    else
1340 #endif
1341 		j = wp->w_lines[0].wl_lnum - wp->w_topline;
1342 	    if (j < wp->w_height - 2)		/* not too far off */
1343 	    {
1344 		i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1345 #ifdef FEAT_DIFF
1346 		/* insert extra lines for previously invisible filler lines */
1347 		if (wp->w_lines[0].wl_lnum != wp->w_topline)
1348 		    i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1349 							  - wp->w_old_topfill;
1350 #endif
1351 		if (i < wp->w_height - 2)	/* less than a screen off */
1352 		{
1353 		    /*
1354 		     * Try to insert the correct number of lines.
1355 		     * If not the last window, delete the lines at the bottom.
1356 		     * win_ins_lines may fail when the terminal can't do it.
1357 		     */
1358 		    if (i > 0)
1359 			check_for_delay(FALSE);
1360 		    if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1361 		    {
1362 			if (wp->w_lines_valid != 0)
1363 			{
1364 			    /* Need to update rows that are new, stop at the
1365 			     * first one that scrolled down. */
1366 			    top_end = i;
1367 			    scrolled_down = TRUE;
1368 
1369 			    /* Move the entries that were scrolled, disable
1370 			     * the entries for the lines to be redrawn. */
1371 			    if ((wp->w_lines_valid += j) > wp->w_height)
1372 				wp->w_lines_valid = wp->w_height;
1373 			    for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1374 				wp->w_lines[idx] = wp->w_lines[idx - j];
1375 			    while (idx >= 0)
1376 				wp->w_lines[idx--].wl_valid = FALSE;
1377 			}
1378 		    }
1379 		    else
1380 			mid_start = 0;		/* redraw all lines */
1381 		}
1382 		else
1383 		    mid_start = 0;		/* redraw all lines */
1384 	    }
1385 	    else
1386 		mid_start = 0;		/* redraw all lines */
1387 	}
1388 	else
1389 	{
1390 	    /*
1391 	     * New topline is at or below old topline: May scroll up.
1392 	     * When topline didn't change, find first entry in w_lines[] that
1393 	     * needs updating.
1394 	     */
1395 
1396 	    /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1397 	    j = -1;
1398 	    row = 0;
1399 	    for (i = 0; i < wp->w_lines_valid; i++)
1400 	    {
1401 		if (wp->w_lines[i].wl_valid
1402 			&& wp->w_lines[i].wl_lnum == wp->w_topline)
1403 		{
1404 		    j = i;
1405 		    break;
1406 		}
1407 		row += wp->w_lines[i].wl_size;
1408 	    }
1409 	    if (j == -1)
1410 	    {
1411 		/* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1412 		 * lines */
1413 		mid_start = 0;
1414 	    }
1415 	    else
1416 	    {
1417 		/*
1418 		 * Try to delete the correct number of lines.
1419 		 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1420 		 */
1421 #ifdef FEAT_DIFF
1422 		/* If the topline didn't change, delete old filler lines,
1423 		 * otherwise delete filler lines of the new topline... */
1424 		if (wp->w_lines[0].wl_lnum == wp->w_topline)
1425 		    row += wp->w_old_topfill;
1426 		else
1427 		    row += diff_check_fill(wp, wp->w_topline);
1428 		/* ... but don't delete new filler lines. */
1429 		row -= wp->w_topfill;
1430 #endif
1431 		if (row > 0)
1432 		{
1433 		    check_for_delay(FALSE);
1434 		    if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1435 			bot_start = wp->w_height - row;
1436 		    else
1437 			mid_start = 0;		/* redraw all lines */
1438 		}
1439 		if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1440 		{
1441 		    /*
1442 		     * Skip the lines (below the deleted lines) that are still
1443 		     * valid and don't need redrawing.	Copy their info
1444 		     * upwards, to compensate for the deleted lines.  Set
1445 		     * bot_start to the first row that needs redrawing.
1446 		     */
1447 		    bot_start = 0;
1448 		    idx = 0;
1449 		    for (;;)
1450 		    {
1451 			wp->w_lines[idx] = wp->w_lines[j];
1452 			/* stop at line that didn't fit, unless it is still
1453 			 * valid (no lines deleted) */
1454 			if (row > 0 && bot_start + row
1455 				 + (int)wp->w_lines[j].wl_size > wp->w_height)
1456 			{
1457 			    wp->w_lines_valid = idx + 1;
1458 			    break;
1459 			}
1460 			bot_start += wp->w_lines[idx++].wl_size;
1461 
1462 			/* stop at the last valid entry in w_lines[].wl_size */
1463 			if (++j >= wp->w_lines_valid)
1464 			{
1465 			    wp->w_lines_valid = idx;
1466 			    break;
1467 			}
1468 		    }
1469 #ifdef FEAT_DIFF
1470 		    /* Correct the first entry for filler lines at the top
1471 		     * when it won't get updated below. */
1472 		    if (wp->w_p_diff && bot_start > 0)
1473 			wp->w_lines[0].wl_size =
1474 			    plines_win_nofill(wp, wp->w_topline, TRUE)
1475 							      + wp->w_topfill;
1476 #endif
1477 		}
1478 	    }
1479 	}
1480 
1481 	/* When starting redraw in the first line, redraw all lines.  When
1482 	 * there is only one window it's probably faster to clear the screen
1483 	 * first. */
1484 	if (mid_start == 0)
1485 	{
1486 	    mid_end = wp->w_height;
1487 	    if (lastwin == firstwin)
1488 	    {
1489 		/* Clear the screen when it was not done by win_del_lines() or
1490 		 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1491 		 * then. */
1492 		if (screen_cleared != TRUE)
1493 		    screenclear();
1494 #ifdef FEAT_WINDOWS
1495 		/* The screen was cleared, redraw the tab pages line. */
1496 		if (redraw_tabline)
1497 		    draw_tabline();
1498 #endif
1499 	    }
1500 	}
1501 
1502 	/* When win_del_lines() or win_ins_lines() caused the screen to be
1503 	 * cleared (only happens for the first window) or when screenclear()
1504 	 * was called directly above, "must_redraw" will have been set to
1505 	 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1506 	if (screen_cleared == TRUE)
1507 	    must_redraw = 0;
1508     }
1509     else
1510     {
1511 	/* Not VALID or INVERTED: redraw all lines. */
1512 	mid_start = 0;
1513 	mid_end = wp->w_height;
1514     }
1515 
1516     if (type == SOME_VALID)
1517     {
1518 	/* SOME_VALID: redraw all lines. */
1519 	mid_start = 0;
1520 	mid_end = wp->w_height;
1521 	type = NOT_VALID;
1522     }
1523 
1524     /* check if we are updating or removing the inverted part */
1525     if ((VIsual_active && buf == curwin->w_buffer)
1526 	    || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1527     {
1528 	linenr_T    from, to;
1529 
1530 	if (VIsual_active)
1531 	{
1532 	    if (VIsual_active
1533 		    && (VIsual_mode != wp->w_old_visual_mode
1534 			|| type == INVERTED_ALL))
1535 	    {
1536 		/*
1537 		 * If the type of Visual selection changed, redraw the whole
1538 		 * selection.  Also when the ownership of the X selection is
1539 		 * gained or lost.
1540 		 */
1541 		if (curwin->w_cursor.lnum < VIsual.lnum)
1542 		{
1543 		    from = curwin->w_cursor.lnum;
1544 		    to = VIsual.lnum;
1545 		}
1546 		else
1547 		{
1548 		    from = VIsual.lnum;
1549 		    to = curwin->w_cursor.lnum;
1550 		}
1551 		/* redraw more when the cursor moved as well */
1552 		if (wp->w_old_cursor_lnum < from)
1553 		    from = wp->w_old_cursor_lnum;
1554 		if (wp->w_old_cursor_lnum > to)
1555 		    to = wp->w_old_cursor_lnum;
1556 		if (wp->w_old_visual_lnum < from)
1557 		    from = wp->w_old_visual_lnum;
1558 		if (wp->w_old_visual_lnum > to)
1559 		    to = wp->w_old_visual_lnum;
1560 	    }
1561 	    else
1562 	    {
1563 		/*
1564 		 * Find the line numbers that need to be updated: The lines
1565 		 * between the old cursor position and the current cursor
1566 		 * position.  Also check if the Visual position changed.
1567 		 */
1568 		if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1569 		{
1570 		    from = curwin->w_cursor.lnum;
1571 		    to = wp->w_old_cursor_lnum;
1572 		}
1573 		else
1574 		{
1575 		    from = wp->w_old_cursor_lnum;
1576 		    to = curwin->w_cursor.lnum;
1577 		    if (from == 0)	/* Visual mode just started */
1578 			from = to;
1579 		}
1580 
1581 		if (VIsual.lnum != wp->w_old_visual_lnum
1582 					|| VIsual.col != wp->w_old_visual_col)
1583 		{
1584 		    if (wp->w_old_visual_lnum < from
1585 						&& wp->w_old_visual_lnum != 0)
1586 			from = wp->w_old_visual_lnum;
1587 		    if (wp->w_old_visual_lnum > to)
1588 			to = wp->w_old_visual_lnum;
1589 		    if (VIsual.lnum < from)
1590 			from = VIsual.lnum;
1591 		    if (VIsual.lnum > to)
1592 			to = VIsual.lnum;
1593 		}
1594 	    }
1595 
1596 	    /*
1597 	     * If in block mode and changed column or curwin->w_curswant:
1598 	     * update all lines.
1599 	     * First compute the actual start and end column.
1600 	     */
1601 	    if (VIsual_mode == Ctrl_V)
1602 	    {
1603 		colnr_T	    fromc, toc;
1604 #if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1605 		int	    save_ve_flags = ve_flags;
1606 
1607 		if (curwin->w_p_lbr)
1608 		    ve_flags = VE_ALL;
1609 #endif
1610 		getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
1611 #if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1612 		ve_flags = save_ve_flags;
1613 #endif
1614 		++toc;
1615 		if (curwin->w_curswant == MAXCOL)
1616 		    toc = MAXCOL;
1617 
1618 		if (fromc != wp->w_old_cursor_fcol
1619 			|| toc != wp->w_old_cursor_lcol)
1620 		{
1621 		    if (from > VIsual.lnum)
1622 			from = VIsual.lnum;
1623 		    if (to < VIsual.lnum)
1624 			to = VIsual.lnum;
1625 		}
1626 		wp->w_old_cursor_fcol = fromc;
1627 		wp->w_old_cursor_lcol = toc;
1628 	    }
1629 	}
1630 	else
1631 	{
1632 	    /* Use the line numbers of the old Visual area. */
1633 	    if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1634 	    {
1635 		from = wp->w_old_cursor_lnum;
1636 		to = wp->w_old_visual_lnum;
1637 	    }
1638 	    else
1639 	    {
1640 		from = wp->w_old_visual_lnum;
1641 		to = wp->w_old_cursor_lnum;
1642 	    }
1643 	}
1644 
1645 	/*
1646 	 * There is no need to update lines above the top of the window.
1647 	 */
1648 	if (from < wp->w_topline)
1649 	    from = wp->w_topline;
1650 
1651 	/*
1652 	 * If we know the value of w_botline, use it to restrict the update to
1653 	 * the lines that are visible in the window.
1654 	 */
1655 	if (wp->w_valid & VALID_BOTLINE)
1656 	{
1657 	    if (from >= wp->w_botline)
1658 		from = wp->w_botline - 1;
1659 	    if (to >= wp->w_botline)
1660 		to = wp->w_botline - 1;
1661 	}
1662 
1663 	/*
1664 	 * Find the minimal part to be updated.
1665 	 * Watch out for scrolling that made entries in w_lines[] invalid.
1666 	 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1667 	 * top_end; need to redraw from top_end to the "to" line.
1668 	 * A middle mouse click with a Visual selection may change the text
1669 	 * above the Visual area and reset wl_valid, do count these for
1670 	 * mid_end (in srow).
1671 	 */
1672 	if (mid_start > 0)
1673 	{
1674 	    lnum = wp->w_topline;
1675 	    idx = 0;
1676 	    srow = 0;
1677 	    if (scrolled_down)
1678 		mid_start = top_end;
1679 	    else
1680 		mid_start = 0;
1681 	    while (lnum < from && idx < wp->w_lines_valid)	/* find start */
1682 	    {
1683 		if (wp->w_lines[idx].wl_valid)
1684 		    mid_start += wp->w_lines[idx].wl_size;
1685 		else if (!scrolled_down)
1686 		    srow += wp->w_lines[idx].wl_size;
1687 		++idx;
1688 # ifdef FEAT_FOLDING
1689 		if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1690 		    lnum = wp->w_lines[idx].wl_lnum;
1691 		else
1692 # endif
1693 		    ++lnum;
1694 	    }
1695 	    srow += mid_start;
1696 	    mid_end = wp->w_height;
1697 	    for ( ; idx < wp->w_lines_valid; ++idx)		/* find end */
1698 	    {
1699 		if (wp->w_lines[idx].wl_valid
1700 			&& wp->w_lines[idx].wl_lnum >= to + 1)
1701 		{
1702 		    /* Only update until first row of this line */
1703 		    mid_end = srow;
1704 		    break;
1705 		}
1706 		srow += wp->w_lines[idx].wl_size;
1707 	    }
1708 	}
1709     }
1710 
1711     if (VIsual_active && buf == curwin->w_buffer)
1712     {
1713 	wp->w_old_visual_mode = VIsual_mode;
1714 	wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1715 	wp->w_old_visual_lnum = VIsual.lnum;
1716 	wp->w_old_visual_col = VIsual.col;
1717 	wp->w_old_curswant = curwin->w_curswant;
1718     }
1719     else
1720     {
1721 	wp->w_old_visual_mode = 0;
1722 	wp->w_old_cursor_lnum = 0;
1723 	wp->w_old_visual_lnum = 0;
1724 	wp->w_old_visual_col = 0;
1725     }
1726 
1727 #if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1728     /* reset got_int, otherwise regexp won't work */
1729     save_got_int = got_int;
1730     got_int = 0;
1731 #endif
1732 #ifdef FEAT_FOLDING
1733     win_foldinfo.fi_level = 0;
1734 #endif
1735 
1736     /*
1737      * Update all the window rows.
1738      */
1739     idx = 0;		/* first entry in w_lines[].wl_size */
1740     row = 0;
1741     srow = 0;
1742     lnum = wp->w_topline;	/* first line shown in window */
1743     for (;;)
1744     {
1745 	/* stop updating when reached the end of the window (check for _past_
1746 	 * the end of the window is at the end of the loop) */
1747 	if (row == wp->w_height)
1748 	{
1749 	    didline = TRUE;
1750 	    break;
1751 	}
1752 
1753 	/* stop updating when hit the end of the file */
1754 	if (lnum > buf->b_ml.ml_line_count)
1755 	{
1756 	    eof = TRUE;
1757 	    break;
1758 	}
1759 
1760 	/* Remember the starting row of the line that is going to be dealt
1761 	 * with.  It is used further down when the line doesn't fit. */
1762 	srow = row;
1763 
1764 	/*
1765 	 * Update a line when it is in an area that needs updating, when it
1766 	 * has changes or w_lines[idx] is invalid.
1767 	 * bot_start may be halfway a wrapped line after using
1768 	 * win_del_lines(), check if the current line includes it.
1769 	 * When syntax folding is being used, the saved syntax states will
1770 	 * already have been updated, we can't see where the syntax state is
1771 	 * the same again, just update until the end of the window.
1772 	 */
1773 	if (row < top_end
1774 		|| (row >= mid_start && row < mid_end)
1775 #ifdef FEAT_SEARCH_EXTRA
1776 		|| top_to_mod
1777 #endif
1778 		|| idx >= wp->w_lines_valid
1779 		|| (row + wp->w_lines[idx].wl_size > bot_start)
1780 		|| (mod_top != 0
1781 		    && (lnum == mod_top
1782 			|| (lnum >= mod_top
1783 			    && (lnum < mod_bot
1784 #ifdef FEAT_SYN_HL
1785 				|| did_update == DID_FOLD
1786 				|| (did_update == DID_LINE
1787 				    && syntax_present(wp)
1788 				    && (
1789 # ifdef FEAT_FOLDING
1790 					(foldmethodIsSyntax(wp)
1791 						      && hasAnyFolding(wp)) ||
1792 # endif
1793 					syntax_check_changed(lnum)))
1794 #endif
1795 #ifdef FEAT_SEARCH_EXTRA
1796 				/* match in fixed position might need redraw
1797 				 * if lines were inserted or deleted */
1798 				|| (wp->w_match_head != NULL
1799 						    && buf->b_mod_xlines != 0)
1800 #endif
1801 				)))))
1802 	{
1803 #ifdef FEAT_SEARCH_EXTRA
1804 	    if (lnum == mod_top)
1805 		top_to_mod = FALSE;
1806 #endif
1807 
1808 	    /*
1809 	     * When at start of changed lines: May scroll following lines
1810 	     * up or down to minimize redrawing.
1811 	     * Don't do this when the change continues until the end.
1812 	     * Don't scroll when dollar_vcol >= 0, keep the "$".
1813 	     */
1814 	    if (lnum == mod_top
1815 		    && mod_bot != MAXLNUM
1816 		    && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
1817 	    {
1818 		int		old_rows = 0;
1819 		int		new_rows = 0;
1820 		int		xtra_rows;
1821 		linenr_T	l;
1822 
1823 		/* Count the old number of window rows, using w_lines[], which
1824 		 * should still contain the sizes for the lines as they are
1825 		 * currently displayed. */
1826 		for (i = idx; i < wp->w_lines_valid; ++i)
1827 		{
1828 		    /* Only valid lines have a meaningful wl_lnum.  Invalid
1829 		     * lines are part of the changed area. */
1830 		    if (wp->w_lines[i].wl_valid
1831 			    && wp->w_lines[i].wl_lnum == mod_bot)
1832 			break;
1833 		    old_rows += wp->w_lines[i].wl_size;
1834 #ifdef FEAT_FOLDING
1835 		    if (wp->w_lines[i].wl_valid
1836 			    && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1837 		    {
1838 			/* Must have found the last valid entry above mod_bot.
1839 			 * Add following invalid entries. */
1840 			++i;
1841 			while (i < wp->w_lines_valid
1842 						  && !wp->w_lines[i].wl_valid)
1843 			    old_rows += wp->w_lines[i++].wl_size;
1844 			break;
1845 		    }
1846 #endif
1847 		}
1848 
1849 		if (i >= wp->w_lines_valid)
1850 		{
1851 		    /* We can't find a valid line below the changed lines,
1852 		     * need to redraw until the end of the window.
1853 		     * Inserting/deleting lines has no use. */
1854 		    bot_start = 0;
1855 		}
1856 		else
1857 		{
1858 		    /* Able to count old number of rows: Count new window
1859 		     * rows, and may insert/delete lines */
1860 		    j = idx;
1861 		    for (l = lnum; l < mod_bot; ++l)
1862 		    {
1863 #ifdef FEAT_FOLDING
1864 			if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1865 			    ++new_rows;
1866 			else
1867 #endif
1868 #ifdef FEAT_DIFF
1869 			    if (l == wp->w_topline)
1870 			    new_rows += plines_win_nofill(wp, l, TRUE)
1871 							      + wp->w_topfill;
1872 			else
1873 #endif
1874 			    new_rows += plines_win(wp, l, TRUE);
1875 			++j;
1876 			if (new_rows > wp->w_height - row - 2)
1877 			{
1878 			    /* it's getting too much, must redraw the rest */
1879 			    new_rows = 9999;
1880 			    break;
1881 			}
1882 		    }
1883 		    xtra_rows = new_rows - old_rows;
1884 		    if (xtra_rows < 0)
1885 		    {
1886 			/* May scroll text up.  If there is not enough
1887 			 * remaining text or scrolling fails, must redraw the
1888 			 * rest.  If scrolling works, must redraw the text
1889 			 * below the scrolled text. */
1890 			if (row - xtra_rows >= wp->w_height - 2)
1891 			    mod_bot = MAXLNUM;
1892 			else
1893 			{
1894 			    check_for_delay(FALSE);
1895 			    if (win_del_lines(wp, row,
1896 					    -xtra_rows, FALSE, FALSE) == FAIL)
1897 				mod_bot = MAXLNUM;
1898 			    else
1899 				bot_start = wp->w_height + xtra_rows;
1900 			}
1901 		    }
1902 		    else if (xtra_rows > 0)
1903 		    {
1904 			/* May scroll text down.  If there is not enough
1905 			 * remaining text of scrolling fails, must redraw the
1906 			 * rest. */
1907 			if (row + xtra_rows >= wp->w_height - 2)
1908 			    mod_bot = MAXLNUM;
1909 			else
1910 			{
1911 			    check_for_delay(FALSE);
1912 			    if (win_ins_lines(wp, row + old_rows,
1913 					     xtra_rows, FALSE, FALSE) == FAIL)
1914 				mod_bot = MAXLNUM;
1915 			    else if (top_end > row + old_rows)
1916 				/* Scrolled the part at the top that requires
1917 				 * updating down. */
1918 				top_end += xtra_rows;
1919 			}
1920 		    }
1921 
1922 		    /* When not updating the rest, may need to move w_lines[]
1923 		     * entries. */
1924 		    if (mod_bot != MAXLNUM && i != j)
1925 		    {
1926 			if (j < i)
1927 			{
1928 			    int x = row + new_rows;
1929 
1930 			    /* move entries in w_lines[] upwards */
1931 			    for (;;)
1932 			    {
1933 				/* stop at last valid entry in w_lines[] */
1934 				if (i >= wp->w_lines_valid)
1935 				{
1936 				    wp->w_lines_valid = j;
1937 				    break;
1938 				}
1939 				wp->w_lines[j] = wp->w_lines[i];
1940 				/* stop at a line that won't fit */
1941 				if (x + (int)wp->w_lines[j].wl_size
1942 							   > wp->w_height)
1943 				{
1944 				    wp->w_lines_valid = j + 1;
1945 				    break;
1946 				}
1947 				x += wp->w_lines[j++].wl_size;
1948 				++i;
1949 			    }
1950 			    if (bot_start > x)
1951 				bot_start = x;
1952 			}
1953 			else /* j > i */
1954 			{
1955 			    /* move entries in w_lines[] downwards */
1956 			    j -= i;
1957 			    wp->w_lines_valid += j;
1958 			    if (wp->w_lines_valid > wp->w_height)
1959 				wp->w_lines_valid = wp->w_height;
1960 			    for (i = wp->w_lines_valid; i - j >= idx; --i)
1961 				wp->w_lines[i] = wp->w_lines[i - j];
1962 
1963 			    /* The w_lines[] entries for inserted lines are
1964 			     * now invalid, but wl_size may be used above.
1965 			     * Reset to zero. */
1966 			    while (i >= idx)
1967 			    {
1968 				wp->w_lines[i].wl_size = 0;
1969 				wp->w_lines[i--].wl_valid = FALSE;
1970 			    }
1971 			}
1972 		    }
1973 		}
1974 	    }
1975 
1976 #ifdef FEAT_FOLDING
1977 	    /*
1978 	     * When lines are folded, display one line for all of them.
1979 	     * Otherwise, display normally (can be several display lines when
1980 	     * 'wrap' is on).
1981 	     */
1982 	    fold_count = foldedCount(wp, lnum, &win_foldinfo);
1983 	    if (fold_count != 0)
1984 	    {
1985 		fold_line(wp, fold_count, &win_foldinfo, lnum, row);
1986 		++row;
1987 		--fold_count;
1988 		wp->w_lines[idx].wl_folded = TRUE;
1989 		wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
1990 # ifdef FEAT_SYN_HL
1991 		did_update = DID_FOLD;
1992 # endif
1993 	    }
1994 	    else
1995 #endif
1996 	    if (idx < wp->w_lines_valid
1997 		    && wp->w_lines[idx].wl_valid
1998 		    && wp->w_lines[idx].wl_lnum == lnum
1999 		    && lnum > wp->w_topline
2000 		    && !(dy_flags & DY_LASTLINE)
2001 		    && srow + wp->w_lines[idx].wl_size > wp->w_height
2002 #ifdef FEAT_DIFF
2003 		    && diff_check_fill(wp, lnum) == 0
2004 #endif
2005 		    )
2006 	    {
2007 		/* This line is not going to fit.  Don't draw anything here,
2008 		 * will draw "@  " lines below. */
2009 		row = wp->w_height + 1;
2010 	    }
2011 	    else
2012 	    {
2013 #ifdef FEAT_SEARCH_EXTRA
2014 		prepare_search_hl(wp, lnum);
2015 #endif
2016 #ifdef FEAT_SYN_HL
2017 		/* Let the syntax stuff know we skipped a few lines. */
2018 		if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
2019 						       && syntax_present(wp))
2020 		    syntax_end_parsing(syntax_last_parsed + 1);
2021 #endif
2022 
2023 		/*
2024 		 * Display one line.
2025 		 */
2026 		row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
2027 
2028 #ifdef FEAT_FOLDING
2029 		wp->w_lines[idx].wl_folded = FALSE;
2030 		wp->w_lines[idx].wl_lastlnum = lnum;
2031 #endif
2032 #ifdef FEAT_SYN_HL
2033 		did_update = DID_LINE;
2034 		syntax_last_parsed = lnum;
2035 #endif
2036 	    }
2037 
2038 	    wp->w_lines[idx].wl_lnum = lnum;
2039 	    wp->w_lines[idx].wl_valid = TRUE;
2040 	    if (row > wp->w_height)	/* past end of screen */
2041 	    {
2042 		/* we may need the size of that too long line later on */
2043 		if (dollar_vcol == -1)
2044 		    wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2045 		++idx;
2046 		break;
2047 	    }
2048 	    if (dollar_vcol == -1)
2049 		wp->w_lines[idx].wl_size = row - srow;
2050 	    ++idx;
2051 #ifdef FEAT_FOLDING
2052 	    lnum += fold_count + 1;
2053 #else
2054 	    ++lnum;
2055 #endif
2056 	}
2057 	else
2058 	{
2059 	    /* This line does not need updating, advance to the next one */
2060 	    row += wp->w_lines[idx++].wl_size;
2061 	    if (row > wp->w_height)	/* past end of screen */
2062 		break;
2063 #ifdef FEAT_FOLDING
2064 	    lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2065 #else
2066 	    ++lnum;
2067 #endif
2068 #ifdef FEAT_SYN_HL
2069 	    did_update = DID_NONE;
2070 #endif
2071 	}
2072 
2073 	if (lnum > buf->b_ml.ml_line_count)
2074 	{
2075 	    eof = TRUE;
2076 	    break;
2077 	}
2078     }
2079     /*
2080      * End of loop over all window lines.
2081      */
2082 
2083 
2084     if (idx > wp->w_lines_valid)
2085 	wp->w_lines_valid = idx;
2086 
2087 #ifdef FEAT_SYN_HL
2088     /*
2089      * Let the syntax stuff know we stop parsing here.
2090      */
2091     if (syntax_last_parsed != 0 && syntax_present(wp))
2092 	syntax_end_parsing(syntax_last_parsed + 1);
2093 #endif
2094 
2095     /*
2096      * If we didn't hit the end of the file, and we didn't finish the last
2097      * line we were working on, then the line didn't fit.
2098      */
2099     wp->w_empty_rows = 0;
2100 #ifdef FEAT_DIFF
2101     wp->w_filler_rows = 0;
2102 #endif
2103     if (!eof && !didline)
2104     {
2105 	if (lnum == wp->w_topline)
2106 	{
2107 	    /*
2108 	     * Single line that does not fit!
2109 	     * Don't overwrite it, it can be edited.
2110 	     */
2111 	    wp->w_botline = lnum + 1;
2112 	}
2113 #ifdef FEAT_DIFF
2114 	else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2115 	{
2116 	    /* Window ends in filler lines. */
2117 	    wp->w_botline = lnum;
2118 	    wp->w_filler_rows = wp->w_height - srow;
2119 	}
2120 #endif
2121 	else if (dy_flags & DY_LASTLINE)	/* 'display' has "lastline" */
2122 	{
2123 	    /*
2124 	     * Last line isn't finished: Display "@@@" at the end.
2125 	     */
2126 	    screen_fill(W_WINROW(wp) + wp->w_height - 1,
2127 		    W_WINROW(wp) + wp->w_height,
2128 		    (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
2129 		    '@', '@', hl_attr(HLF_AT));
2130 	    set_empty_rows(wp, srow);
2131 	    wp->w_botline = lnum;
2132 	}
2133 	else
2134 	{
2135 	    win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
2136 	    wp->w_botline = lnum;
2137 	}
2138     }
2139     else
2140     {
2141 #ifdef FEAT_WINDOWS
2142 	draw_vsep_win(wp, row);
2143 #endif
2144 	if (eof)		/* we hit the end of the file */
2145 	{
2146 	    wp->w_botline = buf->b_ml.ml_line_count + 1;
2147 #ifdef FEAT_DIFF
2148 	    j = diff_check_fill(wp, wp->w_botline);
2149 	    if (j > 0 && !wp->w_botfill)
2150 	    {
2151 		/*
2152 		 * Display filler lines at the end of the file
2153 		 */
2154 		if (char2cells(fill_diff) > 1)
2155 		    i = '-';
2156 		else
2157 		    i = fill_diff;
2158 		if (row + j > wp->w_height)
2159 		    j = wp->w_height - row;
2160 		win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2161 		row += j;
2162 	    }
2163 #endif
2164 	}
2165 	else if (dollar_vcol == -1)
2166 	    wp->w_botline = lnum;
2167 
2168 	/* make sure the rest of the screen is blank */
2169 	/* put '~'s on rows that aren't part of the file. */
2170 	win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_AT);
2171     }
2172 
2173     /* Reset the type of redrawing required, the window has been updated. */
2174     wp->w_redr_type = 0;
2175 #ifdef FEAT_DIFF
2176     wp->w_old_topfill = wp->w_topfill;
2177     wp->w_old_botfill = wp->w_botfill;
2178 #endif
2179 
2180     if (dollar_vcol == -1)
2181     {
2182 	/*
2183 	 * There is a trick with w_botline.  If we invalidate it on each
2184 	 * change that might modify it, this will cause a lot of expensive
2185 	 * calls to plines() in update_topline() each time.  Therefore the
2186 	 * value of w_botline is often approximated, and this value is used to
2187 	 * compute the value of w_topline.  If the value of w_botline was
2188 	 * wrong, check that the value of w_topline is correct (cursor is on
2189 	 * the visible part of the text).  If it's not, we need to redraw
2190 	 * again.  Mostly this just means scrolling up a few lines, so it
2191 	 * doesn't look too bad.  Only do this for the current window (where
2192 	 * changes are relevant).
2193 	 */
2194 	wp->w_valid |= VALID_BOTLINE;
2195 	if (wp == curwin && wp->w_botline != old_botline && !recursive)
2196 	{
2197 	    recursive = TRUE;
2198 	    curwin->w_valid &= ~VALID_TOPLINE;
2199 	    update_topline();	/* may invalidate w_botline again */
2200 	    if (must_redraw != 0)
2201 	    {
2202 		/* Don't update for changes in buffer again. */
2203 		i = curbuf->b_mod_set;
2204 		curbuf->b_mod_set = FALSE;
2205 		win_update(curwin);
2206 		must_redraw = 0;
2207 		curbuf->b_mod_set = i;
2208 	    }
2209 	    recursive = FALSE;
2210 	}
2211     }
2212 
2213 #if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2214     /* restore got_int, unless CTRL-C was hit while redrawing */
2215     if (!got_int)
2216 	got_int = save_got_int;
2217 #endif
2218 }
2219 
2220 #ifdef FEAT_SIGNS
2221 static int draw_signcolumn(win_T *wp);
2222 
2223 /*
2224  * Return TRUE when window "wp" has a column to draw signs in.
2225  */
2226     static int
2227 draw_signcolumn(win_T *wp)
2228 {
2229     return (wp->w_buffer->b_signlist != NULL
2230 # ifdef FEAT_NETBEANS_INTG
2231 				|| wp->w_buffer->b_has_sign_column
2232 # endif
2233 		    );
2234 }
2235 #endif
2236 
2237 /*
2238  * Clear the rest of the window and mark the unused lines with "c1".  use "c2"
2239  * as the filler character.
2240  */
2241     static void
2242 win_draw_end(
2243     win_T	*wp,
2244     int		c1,
2245     int		c2,
2246     int		row,
2247     int		endrow,
2248     hlf_T	hl)
2249 {
2250 #if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2251     int		n = 0;
2252 # define FDC_OFF n
2253 #else
2254 # define FDC_OFF 0
2255 #endif
2256 #ifdef FEAT_FOLDING
2257     int		fdc = compute_foldcolumn(wp, 0);
2258 #endif
2259 
2260 #ifdef FEAT_RIGHTLEFT
2261     if (wp->w_p_rl)
2262     {
2263 	/* No check for cmdline window: should never be right-left. */
2264 # ifdef FEAT_FOLDING
2265 	n = fdc;
2266 
2267 	if (n > 0)
2268 	{
2269 	    /* draw the fold column at the right */
2270 	    if (n > W_WIDTH(wp))
2271 		n = W_WIDTH(wp);
2272 	    screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2273 		    W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
2274 		    ' ', ' ', hl_attr(HLF_FC));
2275 	}
2276 # endif
2277 # ifdef FEAT_SIGNS
2278 	if (draw_signcolumn(wp))
2279 	{
2280 	    int nn = n + 2;
2281 
2282 	    /* draw the sign column left of the fold column */
2283 	    if (nn > W_WIDTH(wp))
2284 		nn = W_WIDTH(wp);
2285 	    screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2286 		    W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
2287 		    ' ', ' ', hl_attr(HLF_SC));
2288 	    n = nn;
2289 	}
2290 # endif
2291 	screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2292 		W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
2293 		c2, c2, hl_attr(hl));
2294 	screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2295 		W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
2296 		c1, c2, hl_attr(hl));
2297     }
2298     else
2299 #endif
2300     {
2301 #ifdef FEAT_CMDWIN
2302 	if (cmdwin_type != 0 && wp == curwin)
2303 	{
2304 	    /* draw the cmdline character in the leftmost column */
2305 	    n = 1;
2306 	    if (n > wp->w_width)
2307 		n = wp->w_width;
2308 	    screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2309 		    W_WINCOL(wp), (int)W_WINCOL(wp) + n,
2310 		    cmdwin_type, ' ', hl_attr(HLF_AT));
2311 	}
2312 #endif
2313 #ifdef FEAT_FOLDING
2314 	if (fdc > 0)
2315 	{
2316 	    int	    nn = n + fdc;
2317 
2318 	    /* draw the fold column at the left */
2319 	    if (nn > W_WIDTH(wp))
2320 		nn = W_WIDTH(wp);
2321 	    screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2322 		    W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2323 		    ' ', ' ', hl_attr(HLF_FC));
2324 	    n = nn;
2325 	}
2326 #endif
2327 #ifdef FEAT_SIGNS
2328 	if (draw_signcolumn(wp))
2329 	{
2330 	    int	    nn = n + 2;
2331 
2332 	    /* draw the sign column after the fold column */
2333 	    if (nn > W_WIDTH(wp))
2334 		nn = W_WIDTH(wp);
2335 	    screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2336 		    W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2337 		    ' ', ' ', hl_attr(HLF_SC));
2338 	    n = nn;
2339 	}
2340 #endif
2341 	screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2342 		W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
2343 		c1, c2, hl_attr(hl));
2344     }
2345     set_empty_rows(wp, row);
2346 }
2347 
2348 #ifdef FEAT_SYN_HL
2349 static int advance_color_col(int vcol, int **color_cols);
2350 
2351 /*
2352  * Advance **color_cols and return TRUE when there are columns to draw.
2353  */
2354     static int
2355 advance_color_col(int vcol, int **color_cols)
2356 {
2357     while (**color_cols >= 0 && vcol > **color_cols)
2358 	++*color_cols;
2359     return (**color_cols >= 0);
2360 }
2361 #endif
2362 
2363 #ifdef FEAT_FOLDING
2364 /*
2365  * Compute the width of the foldcolumn.  Based on 'foldcolumn' and how much
2366  * space is available for window "wp", minus "col".
2367  */
2368     static int
2369 compute_foldcolumn(win_T *wp, int col)
2370 {
2371     int fdc = wp->w_p_fdc;
2372     int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
2373     int wwidth = W_WIDTH(wp);
2374 
2375     if (fdc > wwidth - (col + wmw))
2376 	fdc = wwidth - (col + wmw);
2377     return fdc;
2378 }
2379 
2380 /*
2381  * Display one folded line.
2382  */
2383     static void
2384 fold_line(
2385     win_T	*wp,
2386     long	fold_count,
2387     foldinfo_T	*foldinfo,
2388     linenr_T	lnum,
2389     int		row)
2390 {
2391     char_u	buf[51];
2392     pos_T	*top, *bot;
2393     linenr_T	lnume = lnum + fold_count - 1;
2394     int		len;
2395     char_u	*text;
2396     int		fdc;
2397     int		col;
2398     int		txtcol;
2399     int		off = (int)(current_ScreenLine - ScreenLines);
2400     int		ri;
2401 
2402     /* Build the fold line:
2403      * 1. Add the cmdwin_type for the command-line window
2404      * 2. Add the 'foldcolumn'
2405      * 3. Add the 'number' or 'relativenumber' column
2406      * 4. Compose the text
2407      * 5. Add the text
2408      * 6. set highlighting for the Visual area an other text
2409      */
2410     col = 0;
2411 
2412     /*
2413      * 1. Add the cmdwin_type for the command-line window
2414      * Ignores 'rightleft', this window is never right-left.
2415      */
2416 #ifdef FEAT_CMDWIN
2417     if (cmdwin_type != 0 && wp == curwin)
2418     {
2419 	ScreenLines[off] = cmdwin_type;
2420 	ScreenAttrs[off] = hl_attr(HLF_AT);
2421 #ifdef FEAT_MBYTE
2422 	if (enc_utf8)
2423 	    ScreenLinesUC[off] = 0;
2424 #endif
2425 	++col;
2426     }
2427 #endif
2428 
2429     /*
2430      * 2. Add the 'foldcolumn'
2431      *    Reduce the width when there is not enough space.
2432      */
2433     fdc = compute_foldcolumn(wp, col);
2434     if (fdc > 0)
2435     {
2436 	fill_foldcolumn(buf, wp, TRUE, lnum);
2437 #ifdef FEAT_RIGHTLEFT
2438 	if (wp->w_p_rl)
2439 	{
2440 	    int		i;
2441 
2442 	    copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
2443 							     hl_attr(HLF_FC));
2444 	    /* reverse the fold column */
2445 	    for (i = 0; i < fdc; ++i)
2446 		ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2447 	}
2448 	else
2449 #endif
2450 	    copy_text_attr(off + col, buf, fdc, hl_attr(HLF_FC));
2451 	col += fdc;
2452     }
2453 
2454 #ifdef FEAT_RIGHTLEFT
2455 # define RL_MEMSET(p, v, l)  if (wp->w_p_rl) \
2456 				for (ri = 0; ri < l; ++ri) \
2457 				   ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2458 			     else \
2459 				for (ri = 0; ri < l; ++ri) \
2460 				   ScreenAttrs[off + (p) + ri] = v
2461 #else
2462 # define RL_MEMSET(p, v, l)   for (ri = 0; ri < l; ++ri) \
2463 				 ScreenAttrs[off + (p) + ri] = v
2464 #endif
2465 
2466     /* Set all attributes of the 'number' or 'relativenumber' column and the
2467      * text */
2468     RL_MEMSET(col, hl_attr(HLF_FL), W_WIDTH(wp) - col);
2469 
2470 #ifdef FEAT_SIGNS
2471     /* If signs are being displayed, add two spaces. */
2472     if (draw_signcolumn(wp))
2473     {
2474 	len = W_WIDTH(wp) - col;
2475 	if (len > 0)
2476 	{
2477 	    if (len > 2)
2478 		len = 2;
2479 # ifdef FEAT_RIGHTLEFT
2480 	    if (wp->w_p_rl)
2481 		/* the line number isn't reversed */
2482 		copy_text_attr(off + W_WIDTH(wp) - len - col,
2483 					(char_u *)"  ", len, hl_attr(HLF_FL));
2484 	    else
2485 # endif
2486 		copy_text_attr(off + col, (char_u *)"  ", len, hl_attr(HLF_FL));
2487 	    col += len;
2488 	}
2489     }
2490 #endif
2491 
2492     /*
2493      * 3. Add the 'number' or 'relativenumber' column
2494      */
2495     if (wp->w_p_nu || wp->w_p_rnu)
2496     {
2497 	len = W_WIDTH(wp) - col;
2498 	if (len > 0)
2499 	{
2500 	    int	    w = number_width(wp);
2501 	    long    num;
2502 	    char    *fmt = "%*ld ";
2503 
2504 	    if (len > w + 1)
2505 		len = w + 1;
2506 
2507 	    if (wp->w_p_nu && !wp->w_p_rnu)
2508 		/* 'number' + 'norelativenumber' */
2509 		num = (long)lnum;
2510 	    else
2511 	    {
2512 		/* 'relativenumber', don't use negative numbers */
2513 		num = labs((long)get_cursor_rel_lnum(wp, lnum));
2514 		if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
2515 		{
2516 		    /* 'number' + 'relativenumber': cursor line shows absolute
2517 		     * line number */
2518 		    num = lnum;
2519 		    fmt = "%-*ld ";
2520 		}
2521 	    }
2522 
2523 	    sprintf((char *)buf, fmt, w, num);
2524 #ifdef FEAT_RIGHTLEFT
2525 	    if (wp->w_p_rl)
2526 		/* the line number isn't reversed */
2527 		copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
2528 							     hl_attr(HLF_FL));
2529 	    else
2530 #endif
2531 		copy_text_attr(off + col, buf, len, hl_attr(HLF_FL));
2532 	    col += len;
2533 	}
2534     }
2535 
2536     /*
2537      * 4. Compose the folded-line string with 'foldtext', if set.
2538      */
2539     text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
2540 
2541     txtcol = col;	/* remember where text starts */
2542 
2543     /*
2544      * 5. move the text to current_ScreenLine.  Fill up with "fill_fold".
2545      *    Right-left text is put in columns 0 - number-col, normal text is put
2546      *    in columns number-col - window-width.
2547      */
2548 #ifdef FEAT_MBYTE
2549     if (has_mbyte)
2550     {
2551 	int	cells;
2552 	int	u8c, u8cc[MAX_MCO];
2553 	int	i;
2554 	int	idx;
2555 	int	c_len;
2556 	char_u	*p;
2557 # ifdef FEAT_ARABIC
2558 	int	prev_c = 0;		/* previous Arabic character */
2559 	int	prev_c1 = 0;		/* first composing char for prev_c */
2560 # endif
2561 
2562 # ifdef FEAT_RIGHTLEFT
2563 	if (wp->w_p_rl)
2564 	    idx = off;
2565 	else
2566 # endif
2567 	    idx = off + col;
2568 
2569 	/* Store multibyte characters in ScreenLines[] et al. correctly. */
2570 	for (p = text; *p != NUL; )
2571 	{
2572 	    cells = (*mb_ptr2cells)(p);
2573 	    c_len = (*mb_ptr2len)(p);
2574 	    if (col + cells > W_WIDTH(wp)
2575 # ifdef FEAT_RIGHTLEFT
2576 		    - (wp->w_p_rl ? col : 0)
2577 # endif
2578 		    )
2579 		break;
2580 	    ScreenLines[idx] = *p;
2581 	    if (enc_utf8)
2582 	    {
2583 		u8c = utfc_ptr2char(p, u8cc);
2584 		if (*p < 0x80 && u8cc[0] == 0)
2585 		{
2586 		    ScreenLinesUC[idx] = 0;
2587 #ifdef FEAT_ARABIC
2588 		    prev_c = u8c;
2589 #endif
2590 		}
2591 		else
2592 		{
2593 #ifdef FEAT_ARABIC
2594 		    if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2595 		    {
2596 			/* Do Arabic shaping. */
2597 			int	pc, pc1, nc;
2598 			int	pcc[MAX_MCO];
2599 			int	firstbyte = *p;
2600 
2601 			/* The idea of what is the previous and next
2602 			 * character depends on 'rightleft'. */
2603 			if (wp->w_p_rl)
2604 			{
2605 			    pc = prev_c;
2606 			    pc1 = prev_c1;
2607 			    nc = utf_ptr2char(p + c_len);
2608 			    prev_c1 = u8cc[0];
2609 			}
2610 			else
2611 			{
2612 			    pc = utfc_ptr2char(p + c_len, pcc);
2613 			    nc = prev_c;
2614 			    pc1 = pcc[0];
2615 			}
2616 			prev_c = u8c;
2617 
2618 			u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2619 								 pc, pc1, nc);
2620 			ScreenLines[idx] = firstbyte;
2621 		    }
2622 		    else
2623 			prev_c = u8c;
2624 #endif
2625 		    /* Non-BMP character: display as ? or fullwidth ?. */
2626 #ifdef UNICODE16
2627 		    if (u8c >= 0x10000)
2628 			ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2629 		    else
2630 #endif
2631 			ScreenLinesUC[idx] = u8c;
2632 		    for (i = 0; i < Screen_mco; ++i)
2633 		    {
2634 			ScreenLinesC[i][idx] = u8cc[i];
2635 			if (u8cc[i] == 0)
2636 			    break;
2637 		    }
2638 		}
2639 		if (cells > 1)
2640 		    ScreenLines[idx + 1] = 0;
2641 	    }
2642 	    else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2643 		/* double-byte single width character */
2644 		ScreenLines2[idx] = p[1];
2645 	    else if (cells > 1)
2646 		/* double-width character */
2647 		ScreenLines[idx + 1] = p[1];
2648 	    col += cells;
2649 	    idx += cells;
2650 	    p += c_len;
2651 	}
2652     }
2653     else
2654 #endif
2655     {
2656 	len = (int)STRLEN(text);
2657 	if (len > W_WIDTH(wp) - col)
2658 	    len = W_WIDTH(wp) - col;
2659 	if (len > 0)
2660 	{
2661 #ifdef FEAT_RIGHTLEFT
2662 	    if (wp->w_p_rl)
2663 		STRNCPY(current_ScreenLine, text, len);
2664 	    else
2665 #endif
2666 		STRNCPY(current_ScreenLine + col, text, len);
2667 	    col += len;
2668 	}
2669     }
2670 
2671     /* Fill the rest of the line with the fold filler */
2672 #ifdef FEAT_RIGHTLEFT
2673     if (wp->w_p_rl)
2674 	col -= txtcol;
2675 #endif
2676     while (col < W_WIDTH(wp)
2677 #ifdef FEAT_RIGHTLEFT
2678 		    - (wp->w_p_rl ? txtcol : 0)
2679 #endif
2680 	    )
2681     {
2682 #ifdef FEAT_MBYTE
2683 	if (enc_utf8)
2684 	{
2685 	    if (fill_fold >= 0x80)
2686 	    {
2687 		ScreenLinesUC[off + col] = fill_fold;
2688 		ScreenLinesC[0][off + col] = 0;
2689 	    }
2690 	    else
2691 		ScreenLinesUC[off + col] = 0;
2692 	}
2693 #endif
2694 	ScreenLines[off + col++] = fill_fold;
2695     }
2696 
2697     if (text != buf)
2698 	vim_free(text);
2699 
2700     /*
2701      * 6. set highlighting for the Visual area an other text.
2702      * If all folded lines are in the Visual area, highlight the line.
2703      */
2704     if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2705     {
2706 	if (ltoreq(curwin->w_cursor, VIsual))
2707 	{
2708 	    /* Visual is after curwin->w_cursor */
2709 	    top = &curwin->w_cursor;
2710 	    bot = &VIsual;
2711 	}
2712 	else
2713 	{
2714 	    /* Visual is before curwin->w_cursor */
2715 	    top = &VIsual;
2716 	    bot = &curwin->w_cursor;
2717 	}
2718 	if (lnum >= top->lnum
2719 		&& lnume <= bot->lnum
2720 		&& (VIsual_mode != 'v'
2721 		    || ((lnum > top->lnum
2722 			    || (lnum == top->lnum
2723 				&& top->col == 0))
2724 			&& (lnume < bot->lnum
2725 			    || (lnume == bot->lnum
2726 				&& (bot->col - (*p_sel == 'e'))
2727 		>= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
2728 	{
2729 	    if (VIsual_mode == Ctrl_V)
2730 	    {
2731 		/* Visual block mode: highlight the chars part of the block */
2732 		if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2733 		{
2734 		    if (wp->w_old_cursor_lcol != MAXCOL
2735 			     && wp->w_old_cursor_lcol + txtcol
2736 						       < (colnr_T)W_WIDTH(wp))
2737 			len = wp->w_old_cursor_lcol;
2738 		    else
2739 			len = W_WIDTH(wp) - txtcol;
2740 		    RL_MEMSET(wp->w_old_cursor_fcol + txtcol, hl_attr(HLF_V),
2741 					    len - (int)wp->w_old_cursor_fcol);
2742 		}
2743 	    }
2744 	    else
2745 	    {
2746 		/* Set all attributes of the text */
2747 		RL_MEMSET(txtcol, hl_attr(HLF_V), W_WIDTH(wp) - txtcol);
2748 	    }
2749 	}
2750     }
2751 
2752 #ifdef FEAT_SYN_HL
2753     /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2754     if (wp->w_p_cc_cols)
2755     {
2756 	int i = 0;
2757 	int j = wp->w_p_cc_cols[i];
2758 	int old_txtcol = txtcol;
2759 
2760 	while (j > -1)
2761 	{
2762 	    txtcol += j;
2763 	    if (wp->w_p_wrap)
2764 		txtcol -= wp->w_skipcol;
2765 	    else
2766 		txtcol -= wp->w_leftcol;
2767 	    if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2768 		ScreenAttrs[off + txtcol] = hl_combine_attr(
2769 				    ScreenAttrs[off + txtcol], hl_attr(HLF_MC));
2770 	    txtcol = old_txtcol;
2771 	    j = wp->w_p_cc_cols[++i];
2772 	}
2773     }
2774 
2775     /* Show 'cursorcolumn' in the fold line. */
2776     if (wp->w_p_cuc)
2777     {
2778 	txtcol += wp->w_virtcol;
2779 	if (wp->w_p_wrap)
2780 	    txtcol -= wp->w_skipcol;
2781 	else
2782 	    txtcol -= wp->w_leftcol;
2783 	if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2784 	    ScreenAttrs[off + txtcol] = hl_combine_attr(
2785 				 ScreenAttrs[off + txtcol], hl_attr(HLF_CUC));
2786     }
2787 #endif
2788 
2789     SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2790 						     (int)W_WIDTH(wp), FALSE);
2791 
2792     /*
2793      * Update w_cline_height and w_cline_folded if the cursor line was
2794      * updated (saves a call to plines() later).
2795      */
2796     if (wp == curwin
2797 	    && lnum <= curwin->w_cursor.lnum
2798 	    && lnume >= curwin->w_cursor.lnum)
2799     {
2800 	curwin->w_cline_row = row;
2801 	curwin->w_cline_height = 1;
2802 	curwin->w_cline_folded = TRUE;
2803 	curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2804     }
2805 }
2806 
2807 /*
2808  * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2809  */
2810     static void
2811 copy_text_attr(
2812     int		off,
2813     char_u	*buf,
2814     int		len,
2815     int		attr)
2816 {
2817     int		i;
2818 
2819     mch_memmove(ScreenLines + off, buf, (size_t)len);
2820 # ifdef FEAT_MBYTE
2821     if (enc_utf8)
2822 	vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2823 # endif
2824     for (i = 0; i < len; ++i)
2825 	ScreenAttrs[off + i] = attr;
2826 }
2827 
2828 /*
2829  * Fill the foldcolumn at "p" for window "wp".
2830  * Only to be called when 'foldcolumn' > 0.
2831  */
2832     static void
2833 fill_foldcolumn(
2834     char_u	*p,
2835     win_T	*wp,
2836     int		closed,		/* TRUE of FALSE */
2837     linenr_T	lnum)		/* current line number */
2838 {
2839     int		i = 0;
2840     int		level;
2841     int		first_level;
2842     int		empty;
2843     int		fdc = compute_foldcolumn(wp, 0);
2844 
2845     /* Init to all spaces. */
2846     vim_memset(p, ' ', (size_t)fdc);
2847 
2848     level = win_foldinfo.fi_level;
2849     if (level > 0)
2850     {
2851 	/* If there is only one column put more info in it. */
2852 	empty = (fdc == 1) ? 0 : 1;
2853 
2854 	/* If the column is too narrow, we start at the lowest level that
2855 	 * fits and use numbers to indicated the depth. */
2856 	first_level = level - fdc - closed + 1 + empty;
2857 	if (first_level < 1)
2858 	    first_level = 1;
2859 
2860 	for (i = 0; i + empty < fdc; ++i)
2861 	{
2862 	    if (win_foldinfo.fi_lnum == lnum
2863 			      && first_level + i >= win_foldinfo.fi_low_level)
2864 		p[i] = '-';
2865 	    else if (first_level == 1)
2866 		p[i] = '|';
2867 	    else if (first_level + i <= 9)
2868 		p[i] = '0' + first_level + i;
2869 	    else
2870 		p[i] = '>';
2871 	    if (first_level + i == level)
2872 		break;
2873 	}
2874     }
2875     if (closed)
2876 	p[i >= fdc ? i - 1 : i] = '+';
2877 }
2878 #endif /* FEAT_FOLDING */
2879 
2880 /*
2881  * Display line "lnum" of window 'wp' on the screen.
2882  * Start at row "startrow", stop when "endrow" is reached.
2883  * wp->w_virtcol needs to be valid.
2884  *
2885  * Return the number of last row the line occupies.
2886  */
2887     static int
2888 win_line(
2889     win_T	*wp,
2890     linenr_T	lnum,
2891     int		startrow,
2892     int		endrow,
2893     int		nochange UNUSED)	/* not updating for changed text */
2894 {
2895     int		col;			/* visual column on screen */
2896     unsigned	off;			/* offset in ScreenLines/ScreenAttrs */
2897     int		c = 0;			/* init for GCC */
2898     long	vcol = 0;		/* virtual column (for tabs) */
2899 #ifdef FEAT_LINEBREAK
2900     long	vcol_sbr = -1;		/* virtual column after showbreak */
2901 #endif
2902     long	vcol_prev = -1;		/* "vcol" of previous character */
2903     char_u	*line;			/* current line */
2904     char_u	*ptr;			/* current position in "line" */
2905     int		row;			/* row in the window, excl w_winrow */
2906     int		screen_row;		/* row on the screen, incl w_winrow */
2907 
2908     char_u	extra[18];		/* "%ld" and 'fdc' must fit in here */
2909     int		n_extra = 0;		/* number of extra chars */
2910     char_u	*p_extra = NULL;	/* string of extra chars, plus NUL */
2911     char_u	*p_extra_free = NULL;   /* p_extra needs to be freed */
2912     int		c_extra = NUL;		/* extra chars, all the same */
2913     int		extra_attr = 0;		/* attributes when n_extra != 0 */
2914     static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2915 					   displaying lcs_eol at end-of-line */
2916     int		lcs_eol_one = lcs_eol;	/* lcs_eol until it's been used */
2917     int		lcs_prec_todo = lcs_prec;   /* lcs_prec until it's been used */
2918 
2919     /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2920     int		saved_n_extra = 0;
2921     char_u	*saved_p_extra = NULL;
2922     int		saved_c_extra = 0;
2923     int		saved_char_attr = 0;
2924 
2925     int		n_attr = 0;		/* chars with special attr */
2926     int		saved_attr2 = 0;	/* char_attr saved for n_attr */
2927     int		n_attr3 = 0;		/* chars with overruling special attr */
2928     int		saved_attr3 = 0;	/* char_attr saved for n_attr3 */
2929 
2930     int		n_skip = 0;		/* nr of chars to skip for 'nowrap' */
2931 
2932     int		fromcol, tocol;		/* start/end of inverting */
2933     int		fromcol_prev = -2;	/* start of inverting after cursor */
2934     int		noinvcur = FALSE;	/* don't invert the cursor */
2935     pos_T	*top, *bot;
2936     int		lnum_in_visual_area = FALSE;
2937     pos_T	pos;
2938     long	v;
2939 
2940     int		char_attr = 0;		/* attributes for next character */
2941     int		attr_pri = FALSE;	/* char_attr has priority */
2942     int		area_highlighting = FALSE; /* Visual or incsearch highlighting
2943 					      in this line */
2944     int		attr = 0;		/* attributes for area highlighting */
2945     int		area_attr = 0;		/* attributes desired by highlighting */
2946     int		search_attr = 0;	/* attributes desired by 'hlsearch' */
2947 #ifdef FEAT_SYN_HL
2948     int		vcol_save_attr = 0;	/* saved attr for 'cursorcolumn' */
2949     int		syntax_attr = 0;	/* attributes desired by syntax */
2950     int		has_syntax = FALSE;	/* this buffer has syntax highl. */
2951     int		save_did_emsg;
2952     int		eol_hl_off = 0;		/* 1 if highlighted char after EOL */
2953     int		draw_color_col = FALSE;	/* highlight colorcolumn */
2954     int		*color_cols = NULL;	/* pointer to according columns array */
2955 #endif
2956 #ifdef FEAT_SPELL
2957     int		has_spell = FALSE;	/* this buffer has spell checking */
2958 # define SPWORDLEN 150
2959     char_u	nextline[SPWORDLEN * 2];/* text with start of the next line */
2960     int		nextlinecol = 0;	/* column where nextline[] starts */
2961     int		nextline_idx = 0;	/* index in nextline[] where next line
2962 					   starts */
2963     int		spell_attr = 0;		/* attributes desired by spelling */
2964     int		word_end = 0;		/* last byte with same spell_attr */
2965     static linenr_T  checked_lnum = 0;	/* line number for "checked_col" */
2966     static int	checked_col = 0;	/* column in "checked_lnum" up to which
2967 					 * there are no spell errors */
2968     static int	cap_col = -1;		/* column to check for Cap word */
2969     static linenr_T capcol_lnum = 0;	/* line number where "cap_col" used */
2970     int		cur_checked_col = 0;	/* checked column for current line */
2971 #endif
2972     int		extra_check;		/* has syntax or linebreak */
2973 #ifdef FEAT_MBYTE
2974     int		multi_attr = 0;		/* attributes desired by multibyte */
2975     int		mb_l = 1;		/* multi-byte byte length */
2976     int		mb_c = 0;		/* decoded multi-byte character */
2977     int		mb_utf8 = FALSE;	/* screen char is UTF-8 char */
2978     int		u8cc[MAX_MCO];		/* composing UTF-8 chars */
2979 #endif
2980 #ifdef FEAT_DIFF
2981     int		filler_lines;		/* nr of filler lines to be drawn */
2982     int		filler_todo;		/* nr of filler lines still to do + 1 */
2983     hlf_T	diff_hlf = (hlf_T)0;	/* type of diff highlighting */
2984     int		change_start = MAXCOL;	/* first col of changed area */
2985     int		change_end = -1;	/* last col of changed area */
2986 #endif
2987     colnr_T	trailcol = MAXCOL;	/* start of trailing spaces */
2988 #ifdef FEAT_LINEBREAK
2989     int		need_showbreak = FALSE;
2990 #endif
2991 #if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
2992 	|| defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
2993 # define LINE_ATTR
2994     int		line_attr = 0;		/* attribute for the whole line */
2995 #endif
2996 #ifdef FEAT_SEARCH_EXTRA
2997     matchitem_T *cur;			/* points to the match list */
2998     match_T	*shl;			/* points to search_hl or a match */
2999     int		shl_flag;		/* flag to indicate whether search_hl
3000 					   has been processed or not */
3001     int		pos_inprogress;		/* marks that position match search is
3002 					   in progress */
3003     int		prevcol_hl_flag;	/* flag to indicate whether prevcol
3004 					   equals startcol of search_hl or one
3005 					   of the matches */
3006 #endif
3007 #ifdef FEAT_ARABIC
3008     int		prev_c = 0;		/* previous Arabic character */
3009     int		prev_c1 = 0;		/* first composing char for prev_c */
3010 #endif
3011 #if defined(LINE_ATTR)
3012     int		did_line_attr = 0;
3013 #endif
3014 
3015     /* draw_state: items that are drawn in sequence: */
3016 #define WL_START	0		/* nothing done yet */
3017 #ifdef FEAT_CMDWIN
3018 # define WL_CMDLINE	WL_START + 1	/* cmdline window column */
3019 #else
3020 # define WL_CMDLINE	WL_START
3021 #endif
3022 #ifdef FEAT_FOLDING
3023 # define WL_FOLD	WL_CMDLINE + 1	/* 'foldcolumn' */
3024 #else
3025 # define WL_FOLD	WL_CMDLINE
3026 #endif
3027 #ifdef FEAT_SIGNS
3028 # define WL_SIGN	WL_FOLD + 1	/* column for signs */
3029 #else
3030 # define WL_SIGN	WL_FOLD		/* column for signs */
3031 #endif
3032 #define WL_NR		WL_SIGN + 1	/* line number */
3033 #ifdef FEAT_LINEBREAK
3034 # define WL_BRI		WL_NR + 1	/* 'breakindent' */
3035 #else
3036 # define WL_BRI		WL_NR
3037 #endif
3038 #if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3039 # define WL_SBR		WL_BRI + 1	/* 'showbreak' or 'diff' */
3040 #else
3041 # define WL_SBR		WL_BRI
3042 #endif
3043 #define WL_LINE		WL_SBR + 1	/* text in the line */
3044     int		draw_state = WL_START;	/* what to draw next */
3045 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3046     int		feedback_col = 0;
3047     int		feedback_old_attr = -1;
3048 #endif
3049 
3050 #ifdef FEAT_CONCEAL
3051     int		syntax_flags	= 0;
3052     int		syntax_seqnr	= 0;
3053     int		prev_syntax_id	= 0;
3054     int		conceal_attr	= hl_attr(HLF_CONCEAL);
3055     int		is_concealing	= FALSE;
3056     int		boguscols	= 0;	/* nonexistent columns added to force
3057 					   wrapping */
3058     int		vcol_off	= 0;	/* offset for concealed characters */
3059     int		did_wcol	= FALSE;
3060     int		match_conc	= FALSE; /* cchar for match functions */
3061     int		has_match_conc  = FALSE; /* match wants to conceal */
3062     int		old_boguscols   = 0;
3063 # define VCOL_HLC (vcol - vcol_off)
3064 # define FIX_FOR_BOGUSCOLS \
3065     { \
3066 	n_extra += vcol_off; \
3067 	vcol -= vcol_off; \
3068 	vcol_off = 0; \
3069 	col -= boguscols; \
3070 	old_boguscols = boguscols; \
3071 	boguscols = 0; \
3072     }
3073 #else
3074 # define VCOL_HLC (vcol)
3075 #endif
3076 
3077     if (startrow > endrow)		/* past the end already! */
3078 	return startrow;
3079 
3080     row = startrow;
3081     screen_row = row + W_WINROW(wp);
3082 
3083     /*
3084      * To speed up the loop below, set extra_check when there is linebreak,
3085      * trailing white space and/or syntax processing to be done.
3086      */
3087 #ifdef FEAT_LINEBREAK
3088     extra_check = wp->w_p_lbr;
3089 #else
3090     extra_check = 0;
3091 #endif
3092 #ifdef FEAT_SYN_HL
3093     if (syntax_present(wp) && !wp->w_s->b_syn_error)
3094     {
3095 	/* Prepare for syntax highlighting in this line.  When there is an
3096 	 * error, stop syntax highlighting. */
3097 	save_did_emsg = did_emsg;
3098 	did_emsg = FALSE;
3099 	syntax_start(wp, lnum);
3100 	if (did_emsg)
3101 	    wp->w_s->b_syn_error = TRUE;
3102 	else
3103 	{
3104 	    did_emsg = save_did_emsg;
3105 	    has_syntax = TRUE;
3106 	    extra_check = TRUE;
3107 	}
3108     }
3109 
3110     /* Check for columns to display for 'colorcolumn'. */
3111     color_cols = wp->w_p_cc_cols;
3112     if (color_cols != NULL)
3113 	draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
3114 #endif
3115 
3116 #ifdef FEAT_SPELL
3117     if (wp->w_p_spell
3118 	    && *wp->w_s->b_p_spl != NUL
3119 	    && wp->w_s->b_langp.ga_len > 0
3120 	    && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
3121     {
3122 	/* Prepare for spell checking. */
3123 	has_spell = TRUE;
3124 	extra_check = TRUE;
3125 
3126 	/* Get the start of the next line, so that words that wrap to the next
3127 	 * line are found too: "et<line-break>al.".
3128 	 * Trick: skip a few chars for C/shell/Vim comments */
3129 	nextline[SPWORDLEN] = NUL;
3130 	if (lnum < wp->w_buffer->b_ml.ml_line_count)
3131 	{
3132 	    line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3133 	    spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3134 	}
3135 
3136 	/* When a word wrapped from the previous line the start of the current
3137 	 * line is valid. */
3138 	if (lnum == checked_lnum)
3139 	    cur_checked_col = checked_col;
3140 	checked_lnum = 0;
3141 
3142 	/* When there was a sentence end in the previous line may require a
3143 	 * word starting with capital in this line.  In line 1 always check
3144 	 * the first word. */
3145 	if (lnum != capcol_lnum)
3146 	    cap_col = -1;
3147 	if (lnum == 1)
3148 	    cap_col = 0;
3149 	capcol_lnum = 0;
3150     }
3151 #endif
3152 
3153     /*
3154      * handle visual active in this window
3155      */
3156     fromcol = -10;
3157     tocol = MAXCOL;
3158     if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3159     {
3160 					/* Visual is after curwin->w_cursor */
3161 	if (ltoreq(curwin->w_cursor, VIsual))
3162 	{
3163 	    top = &curwin->w_cursor;
3164 	    bot = &VIsual;
3165 	}
3166 	else				/* Visual is before curwin->w_cursor */
3167 	{
3168 	    top = &VIsual;
3169 	    bot = &curwin->w_cursor;
3170 	}
3171 	lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
3172 	if (VIsual_mode == Ctrl_V)	/* block mode */
3173 	{
3174 	    if (lnum_in_visual_area)
3175 	    {
3176 		fromcol = wp->w_old_cursor_fcol;
3177 		tocol = wp->w_old_cursor_lcol;
3178 	    }
3179 	}
3180 	else				/* non-block mode */
3181 	{
3182 	    if (lnum > top->lnum && lnum <= bot->lnum)
3183 		fromcol = 0;
3184 	    else if (lnum == top->lnum)
3185 	    {
3186 		if (VIsual_mode == 'V')	/* linewise */
3187 		    fromcol = 0;
3188 		else
3189 		{
3190 		    getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3191 		    if (gchar_pos(top) == NUL)
3192 			tocol = fromcol + 1;
3193 		}
3194 	    }
3195 	    if (VIsual_mode != 'V' && lnum == bot->lnum)
3196 	    {
3197 		if (*p_sel == 'e' && bot->col == 0
3198 #ifdef FEAT_VIRTUALEDIT
3199 			&& bot->coladd == 0
3200 #endif
3201 		   )
3202 		{
3203 		    fromcol = -10;
3204 		    tocol = MAXCOL;
3205 		}
3206 		else if (bot->col == MAXCOL)
3207 		    tocol = MAXCOL;
3208 		else
3209 		{
3210 		    pos = *bot;
3211 		    if (*p_sel == 'e')
3212 			getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3213 		    else
3214 		    {
3215 			getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3216 			++tocol;
3217 		    }
3218 		}
3219 	    }
3220 	}
3221 
3222 	/* Check if the character under the cursor should not be inverted */
3223 	if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
3224 #ifdef FEAT_GUI
3225 		&& !gui.in_use
3226 #endif
3227 		)
3228 	    noinvcur = TRUE;
3229 
3230 	/* if inverting in this line set area_highlighting */
3231 	if (fromcol >= 0)
3232 	{
3233 	    area_highlighting = TRUE;
3234 	    attr = hl_attr(HLF_V);
3235 #if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
3236 	    if ((clip_star.available && !clip_star.owned
3237 						     && clip_isautosel_star())
3238 		    || (clip_plus.available && !clip_plus.owned
3239 						    && clip_isautosel_plus()))
3240 		attr = hl_attr(HLF_VNC);
3241 #endif
3242 	}
3243     }
3244 
3245     /*
3246      * handle 'incsearch' and ":s///c" highlighting
3247      */
3248     else if (highlight_match
3249 	    && wp == curwin
3250 	    && lnum >= curwin->w_cursor.lnum
3251 	    && lnum <= curwin->w_cursor.lnum + search_match_lines)
3252     {
3253 	if (lnum == curwin->w_cursor.lnum)
3254 	    getvcol(curwin, &(curwin->w_cursor),
3255 					     (colnr_T *)&fromcol, NULL, NULL);
3256 	else
3257 	    fromcol = 0;
3258 	if (lnum == curwin->w_cursor.lnum + search_match_lines)
3259 	{
3260 	    pos.lnum = lnum;
3261 	    pos.col = search_match_endcol;
3262 	    getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3263 	}
3264 	else
3265 	    tocol = MAXCOL;
3266 	/* do at least one character; happens when past end of line */
3267 	if (fromcol == tocol)
3268 	    tocol = fromcol + 1;
3269 	area_highlighting = TRUE;
3270 	attr = hl_attr(HLF_I);
3271     }
3272 
3273 #ifdef FEAT_DIFF
3274     filler_lines = diff_check(wp, lnum);
3275     if (filler_lines < 0)
3276     {
3277 	if (filler_lines == -1)
3278 	{
3279 	    if (diff_find_change(wp, lnum, &change_start, &change_end))
3280 		diff_hlf = HLF_ADD;	/* added line */
3281 	    else if (change_start == 0)
3282 		diff_hlf = HLF_TXD;	/* changed text */
3283 	    else
3284 		diff_hlf = HLF_CHD;	/* changed line */
3285 	}
3286 	else
3287 	    diff_hlf = HLF_ADD;		/* added line */
3288 	filler_lines = 0;
3289 	area_highlighting = TRUE;
3290     }
3291     if (lnum == wp->w_topline)
3292 	filler_lines = wp->w_topfill;
3293     filler_todo = filler_lines;
3294 #endif
3295 
3296 #ifdef LINE_ATTR
3297 # ifdef FEAT_SIGNS
3298     /* If this line has a sign with line highlighting set line_attr. */
3299     v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3300     if (v != 0)
3301 	line_attr = sign_get_attr((int)v, TRUE);
3302 # endif
3303 # if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
3304     /* Highlight the current line in the quickfix window. */
3305     if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
3306 	line_attr = hl_attr(HLF_L);
3307 # endif
3308     if (line_attr != 0)
3309 	area_highlighting = TRUE;
3310 #endif
3311 
3312     line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3313     ptr = line;
3314 
3315 #ifdef FEAT_SPELL
3316     if (has_spell)
3317     {
3318 	/* For checking first word with a capital skip white space. */
3319 	if (cap_col == 0)
3320 	    cap_col = (int)(skipwhite(line) - line);
3321 
3322 	/* To be able to spell-check over line boundaries copy the end of the
3323 	 * current line into nextline[].  Above the start of the next line was
3324 	 * copied to nextline[SPWORDLEN]. */
3325 	if (nextline[SPWORDLEN] == NUL)
3326 	{
3327 	    /* No next line or it is empty. */
3328 	    nextlinecol = MAXCOL;
3329 	    nextline_idx = 0;
3330 	}
3331 	else
3332 	{
3333 	    v = (long)STRLEN(line);
3334 	    if (v < SPWORDLEN)
3335 	    {
3336 		/* Short line, use it completely and append the start of the
3337 		 * next line. */
3338 		nextlinecol = 0;
3339 		mch_memmove(nextline, line, (size_t)v);
3340 		STRMOVE(nextline + v, nextline + SPWORDLEN);
3341 		nextline_idx = v + 1;
3342 	    }
3343 	    else
3344 	    {
3345 		/* Long line, use only the last SPWORDLEN bytes. */
3346 		nextlinecol = v - SPWORDLEN;
3347 		mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3348 		nextline_idx = SPWORDLEN + 1;
3349 	    }
3350 	}
3351     }
3352 #endif
3353 
3354     if (wp->w_p_list)
3355     {
3356 	if (lcs_space || lcs_trail)
3357 	    extra_check = TRUE;
3358 	/* find start of trailing whitespace */
3359 	if (lcs_trail)
3360 	{
3361 	    trailcol = (colnr_T)STRLEN(ptr);
3362 	    while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
3363 		--trailcol;
3364 	    trailcol += (colnr_T) (ptr - line);
3365 	}
3366     }
3367 
3368     /*
3369      * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3370      * first character to be displayed.
3371      */
3372     if (wp->w_p_wrap)
3373 	v = wp->w_skipcol;
3374     else
3375 	v = wp->w_leftcol;
3376     if (v > 0)
3377     {
3378 #ifdef FEAT_MBYTE
3379 	char_u	*prev_ptr = ptr;
3380 #endif
3381 	while (vcol < v && *ptr != NUL)
3382 	{
3383 	    c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
3384 	    vcol += c;
3385 #ifdef FEAT_MBYTE
3386 	    prev_ptr = ptr;
3387 #endif
3388 	    mb_ptr_adv(ptr);
3389 	}
3390 
3391 	/* When:
3392 	 * - 'cuc' is set, or
3393 	 * - 'colorcolumn' is set, or
3394 	 * - 'virtualedit' is set, or
3395 	 * - the visual mode is active,
3396 	 * the end of the line may be before the start of the displayed part.
3397 	 */
3398 	if (vcol < v && (
3399 #ifdef FEAT_SYN_HL
3400 	     wp->w_p_cuc || draw_color_col ||
3401 #endif
3402 #ifdef FEAT_VIRTUALEDIT
3403 	     virtual_active() ||
3404 #endif
3405 	     (VIsual_active && wp->w_buffer == curwin->w_buffer)))
3406 	{
3407 	    vcol = v;
3408 	}
3409 
3410 	/* Handle a character that's not completely on the screen: Put ptr at
3411 	 * that character but skip the first few screen characters. */
3412 	if (vcol > v)
3413 	{
3414 	    vcol -= c;
3415 #ifdef FEAT_MBYTE
3416 	    ptr = prev_ptr;
3417 #else
3418 	    --ptr;
3419 #endif
3420 	    n_skip = v - vcol;
3421 	}
3422 
3423 	/*
3424 	 * Adjust for when the inverted text is before the screen,
3425 	 * and when the start of the inverted text is before the screen.
3426 	 */
3427 	if (tocol <= vcol)
3428 	    fromcol = 0;
3429 	else if (fromcol >= 0 && fromcol < vcol)
3430 	    fromcol = vcol;
3431 
3432 #ifdef FEAT_LINEBREAK
3433 	/* When w_skipcol is non-zero, first line needs 'showbreak' */
3434 	if (wp->w_p_wrap)
3435 	    need_showbreak = TRUE;
3436 #endif
3437 #ifdef FEAT_SPELL
3438 	/* When spell checking a word we need to figure out the start of the
3439 	 * word and if it's badly spelled or not. */
3440 	if (has_spell)
3441 	{
3442 	    int		len;
3443 	    colnr_T	linecol = (colnr_T)(ptr - line);
3444 	    hlf_T	spell_hlf = HLF_COUNT;
3445 
3446 	    pos = wp->w_cursor;
3447 	    wp->w_cursor.lnum = lnum;
3448 	    wp->w_cursor.col = linecol;
3449 	    len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
3450 
3451 	    /* spell_move_to() may call ml_get() and make "line" invalid */
3452 	    line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3453 	    ptr = line + linecol;
3454 
3455 	    if (len == 0 || (int)wp->w_cursor.col > ptr - line)
3456 	    {
3457 		/* no bad word found at line start, don't check until end of a
3458 		 * word */
3459 		spell_hlf = HLF_COUNT;
3460 		word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
3461 	    }
3462 	    else
3463 	    {
3464 		/* bad word found, use attributes until end of word */
3465 		word_end = wp->w_cursor.col + len + 1;
3466 
3467 		/* Turn index into actual attributes. */
3468 		if (spell_hlf != HLF_COUNT)
3469 		    spell_attr = highlight_attr[spell_hlf];
3470 	    }
3471 	    wp->w_cursor = pos;
3472 
3473 # ifdef FEAT_SYN_HL
3474 	    /* Need to restart syntax highlighting for this line. */
3475 	    if (has_syntax)
3476 		syntax_start(wp, lnum);
3477 # endif
3478 	}
3479 #endif
3480     }
3481 
3482     /*
3483      * Correct highlighting for cursor that can't be disabled.
3484      * Avoids having to check this for each character.
3485      */
3486     if (fromcol >= 0)
3487     {
3488 	if (noinvcur)
3489 	{
3490 	    if ((colnr_T)fromcol == wp->w_virtcol)
3491 	    {
3492 		/* highlighting starts at cursor, let it start just after the
3493 		 * cursor */
3494 		fromcol_prev = fromcol;
3495 		fromcol = -1;
3496 	    }
3497 	    else if ((colnr_T)fromcol < wp->w_virtcol)
3498 		/* restart highlighting after the cursor */
3499 		fromcol_prev = wp->w_virtcol;
3500 	}
3501 	if (fromcol >= tocol)
3502 	    fromcol = -1;
3503     }
3504 
3505 #ifdef FEAT_SEARCH_EXTRA
3506     /*
3507      * Handle highlighting the last used search pattern and matches.
3508      * Do this for both search_hl and the match list.
3509      */
3510     cur = wp->w_match_head;
3511     shl_flag = FALSE;
3512     while (cur != NULL || shl_flag == FALSE)
3513     {
3514 	if (shl_flag == FALSE)
3515 	{
3516 	    shl = &search_hl;
3517 	    shl_flag = TRUE;
3518 	}
3519 	else
3520 	    shl = &cur->hl;
3521 	shl->startcol = MAXCOL;
3522 	shl->endcol = MAXCOL;
3523 	shl->attr_cur = 0;
3524 	v = (long)(ptr - line);
3525 	if (cur != NULL)
3526 	    cur->pos.cur = 0;
3527 	next_search_hl(wp, shl, lnum, (colnr_T)v, cur);
3528 
3529 	/* Need to get the line again, a multi-line regexp may have made it
3530 	 * invalid. */
3531 	line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3532 	ptr = line + v;
3533 
3534 	if (shl->lnum != 0 && shl->lnum <= lnum)
3535 	{
3536 	    if (shl->lnum == lnum)
3537 		shl->startcol = shl->rm.startpos[0].col;
3538 	    else
3539 		shl->startcol = 0;
3540 	    if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3541 						- shl->rm.startpos[0].lnum)
3542 		shl->endcol = shl->rm.endpos[0].col;
3543 	    else
3544 		shl->endcol = MAXCOL;
3545 	    /* Highlight one character for an empty match. */
3546 	    if (shl->startcol == shl->endcol)
3547 	    {
3548 #ifdef FEAT_MBYTE
3549 		if (has_mbyte && line[shl->endcol] != NUL)
3550 		    shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3551 		else
3552 #endif
3553 		    ++shl->endcol;
3554 	    }
3555 	    if ((long)shl->startcol < v)  /* match at leftcol */
3556 	    {
3557 		shl->attr_cur = shl->attr;
3558 		search_attr = shl->attr;
3559 	    }
3560 	    area_highlighting = TRUE;
3561 	}
3562 	if (shl != &search_hl && cur != NULL)
3563 	    cur = cur->next;
3564     }
3565 #endif
3566 
3567 #ifdef FEAT_SYN_HL
3568     /* Cursor line highlighting for 'cursorline' in the current window.  Not
3569      * when Visual mode is active, because it's not clear what is selected
3570      * then. */
3571     if (wp->w_p_cul && lnum == wp->w_cursor.lnum
3572 					 && !(wp == curwin && VIsual_active))
3573     {
3574 	line_attr = hl_attr(HLF_CUL);
3575 	area_highlighting = TRUE;
3576     }
3577 #endif
3578 
3579     off = (unsigned)(current_ScreenLine - ScreenLines);
3580     col = 0;
3581 #ifdef FEAT_RIGHTLEFT
3582     if (wp->w_p_rl)
3583     {
3584 	/* Rightleft window: process the text in the normal direction, but put
3585 	 * it in current_ScreenLine[] from right to left.  Start at the
3586 	 * rightmost column of the window. */
3587 	col = W_WIDTH(wp) - 1;
3588 	off += col;
3589     }
3590 #endif
3591 
3592     /*
3593      * Repeat for the whole displayed line.
3594      */
3595     for (;;)
3596     {
3597 #ifdef FEAT_CONCEAL
3598 	has_match_conc = FALSE;
3599 #endif
3600 	/* Skip this quickly when working on the text. */
3601 	if (draw_state != WL_LINE)
3602 	{
3603 #ifdef FEAT_CMDWIN
3604 	    if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3605 	    {
3606 		draw_state = WL_CMDLINE;
3607 		if (cmdwin_type != 0 && wp == curwin)
3608 		{
3609 		    /* Draw the cmdline character. */
3610 		    n_extra = 1;
3611 		    c_extra = cmdwin_type;
3612 		    char_attr = hl_attr(HLF_AT);
3613 		}
3614 	    }
3615 #endif
3616 
3617 #ifdef FEAT_FOLDING
3618 	    if (draw_state == WL_FOLD - 1 && n_extra == 0)
3619 	    {
3620 		int fdc = compute_foldcolumn(wp, 0);
3621 
3622 		draw_state = WL_FOLD;
3623 		if (fdc > 0)
3624 		{
3625 		    /* Draw the 'foldcolumn'. */
3626 		    fill_foldcolumn(extra, wp, FALSE, lnum);
3627 		    n_extra = fdc;
3628 		    p_extra = extra;
3629 		    p_extra[n_extra] = NUL;
3630 		    c_extra = NUL;
3631 		    char_attr = hl_attr(HLF_FC);
3632 		}
3633 	    }
3634 #endif
3635 
3636 #ifdef FEAT_SIGNS
3637 	    if (draw_state == WL_SIGN - 1 && n_extra == 0)
3638 	    {
3639 		draw_state = WL_SIGN;
3640 		/* Show the sign column when there are any signs in this
3641 		 * buffer or when using Netbeans. */
3642 		if (draw_signcolumn(wp))
3643 		{
3644 		    int	text_sign;
3645 # ifdef FEAT_SIGN_ICONS
3646 		    int	icon_sign;
3647 # endif
3648 
3649 		    /* Draw two cells with the sign value or blank. */
3650 		    c_extra = ' ';
3651 		    char_attr = hl_attr(HLF_SC);
3652 		    n_extra = 2;
3653 
3654 		    if (row == startrow
3655 #ifdef FEAT_DIFF
3656 			    + filler_lines && filler_todo <= 0
3657 #endif
3658 			    )
3659 		    {
3660 			text_sign = buf_getsigntype(wp->w_buffer, lnum,
3661 								   SIGN_TEXT);
3662 # ifdef FEAT_SIGN_ICONS
3663 			icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3664 								   SIGN_ICON);
3665 			if (gui.in_use && icon_sign != 0)
3666 			{
3667 			    /* Use the image in this position. */
3668 			    c_extra = SIGN_BYTE;
3669 #  ifdef FEAT_NETBEANS_INTG
3670 			    if (buf_signcount(wp->w_buffer, lnum) > 1)
3671 				c_extra = MULTISIGN_BYTE;
3672 #  endif
3673 			    char_attr = icon_sign;
3674 			}
3675 			else
3676 # endif
3677 			    if (text_sign != 0)
3678 			{
3679 			    p_extra = sign_get_text(text_sign);
3680 			    if (p_extra != NULL)
3681 			    {
3682 				c_extra = NUL;
3683 				n_extra = (int)STRLEN(p_extra);
3684 			    }
3685 			    char_attr = sign_get_attr(text_sign, FALSE);
3686 			}
3687 		    }
3688 		}
3689 	    }
3690 #endif
3691 
3692 	    if (draw_state == WL_NR - 1 && n_extra == 0)
3693 	    {
3694 		draw_state = WL_NR;
3695 		/* Display the absolute or relative line number. After the
3696 		 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3697 		if ((wp->w_p_nu || wp->w_p_rnu)
3698 			&& (row == startrow
3699 #ifdef FEAT_DIFF
3700 			    + filler_lines
3701 #endif
3702 			    || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3703 		{
3704 		    /* Draw the line number (empty space after wrapping). */
3705 		    if (row == startrow
3706 #ifdef FEAT_DIFF
3707 			    + filler_lines
3708 #endif
3709 			    )
3710 		    {
3711 			long num;
3712 			char *fmt = "%*ld ";
3713 
3714 			if (wp->w_p_nu && !wp->w_p_rnu)
3715 			    /* 'number' + 'norelativenumber' */
3716 			    num = (long)lnum;
3717 			else
3718 			{
3719 			    /* 'relativenumber', don't use negative numbers */
3720 			    num = labs((long)get_cursor_rel_lnum(wp, lnum));
3721 			    if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
3722 			    {
3723 				/* 'number' + 'relativenumber' */
3724 				num = lnum;
3725 				fmt = "%-*ld ";
3726 			    }
3727 			}
3728 
3729 			sprintf((char *)extra, fmt,
3730 						number_width(wp), num);
3731 			if (wp->w_skipcol > 0)
3732 			    for (p_extra = extra; *p_extra == ' '; ++p_extra)
3733 				*p_extra = '-';
3734 #ifdef FEAT_RIGHTLEFT
3735 			if (wp->w_p_rl)		    /* reverse line numbers */
3736 			    rl_mirror(extra);
3737 #endif
3738 			p_extra = extra;
3739 			c_extra = NUL;
3740 		    }
3741 		    else
3742 			c_extra = ' ';
3743 		    n_extra = number_width(wp) + 1;
3744 		    char_attr = hl_attr(HLF_N);
3745 #ifdef FEAT_SYN_HL
3746 		    /* When 'cursorline' is set highlight the line number of
3747 		     * the current line differently.
3748 		     * TODO: Can we use CursorLine instead of CursorLineNr
3749 		     * when CursorLineNr isn't set? */
3750 		    if ((wp->w_p_cul || wp->w_p_rnu)
3751 						 && lnum == wp->w_cursor.lnum)
3752 			char_attr = hl_attr(HLF_CLN);
3753 #endif
3754 		}
3755 	    }
3756 
3757 #ifdef FEAT_LINEBREAK
3758 	    if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3759 					     && n_extra == 0 && *p_sbr != NUL)
3760 		/* draw indent after showbreak value */
3761 		draw_state = WL_BRI;
3762 	    else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3763 		/* After the showbreak, draw the breakindent */
3764 		draw_state = WL_BRI - 1;
3765 
3766 	    /* draw 'breakindent': indent wrapped text accordingly */
3767 	    if (draw_state == WL_BRI - 1 && n_extra == 0)
3768 	    {
3769 		draw_state = WL_BRI;
3770 		if (wp->w_p_bri && n_extra == 0 && row != startrow
3771 # ifdef FEAT_DIFF
3772 			&& filler_lines == 0
3773 # endif
3774 		   )
3775 		{
3776 		    char_attr = 0; /* was: hl_attr(HLF_AT); */
3777 # ifdef FEAT_DIFF
3778 		    if (diff_hlf != (hlf_T)0)
3779 		    {
3780 			char_attr = hl_attr(diff_hlf);
3781 #  ifdef FEAT_SYN_HL
3782 			if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3783 			    char_attr = hl_combine_attr(char_attr,
3784 							    hl_attr(HLF_CUL));
3785 #  endif
3786 		    }
3787 # endif
3788 		    p_extra = NULL;
3789 		    c_extra = ' ';
3790 		    n_extra = get_breakindent_win(wp,
3791 				       ml_get_buf(wp->w_buffer, lnum, FALSE));
3792 		    /* Correct end of highlighted area for 'breakindent',
3793 		     * required when 'linebreak' is also set. */
3794 		    if (tocol == vcol)
3795 			tocol += n_extra;
3796 		}
3797 	    }
3798 #endif
3799 
3800 #if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3801 	    if (draw_state == WL_SBR - 1 && n_extra == 0)
3802 	    {
3803 		draw_state = WL_SBR;
3804 # ifdef FEAT_DIFF
3805 		if (filler_todo > 0)
3806 		{
3807 		    /* Draw "deleted" diff line(s). */
3808 		    if (char2cells(fill_diff) > 1)
3809 			c_extra = '-';
3810 		    else
3811 			c_extra = fill_diff;
3812 #  ifdef FEAT_RIGHTLEFT
3813 		    if (wp->w_p_rl)
3814 			n_extra = col + 1;
3815 		    else
3816 #  endif
3817 			n_extra = W_WIDTH(wp) - col;
3818 		    char_attr = hl_attr(HLF_DED);
3819 		}
3820 # endif
3821 # ifdef FEAT_LINEBREAK
3822 		if (*p_sbr != NUL && need_showbreak)
3823 		{
3824 		    /* Draw 'showbreak' at the start of each broken line. */
3825 		    p_extra = p_sbr;
3826 		    c_extra = NUL;
3827 		    n_extra = (int)STRLEN(p_sbr);
3828 		    char_attr = hl_attr(HLF_AT);
3829 		    need_showbreak = FALSE;
3830 		    vcol_sbr = vcol + MB_CHARLEN(p_sbr);
3831 		    /* Correct end of highlighted area for 'showbreak',
3832 		     * required when 'linebreak' is also set. */
3833 		    if (tocol == vcol)
3834 			tocol += n_extra;
3835 #ifdef FEAT_SYN_HL
3836 		    /* combine 'showbreak' with 'cursorline' */
3837 		    if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3838 			char_attr = hl_combine_attr(char_attr,
3839 							    hl_attr(HLF_CUL));
3840 #endif
3841 		}
3842 # endif
3843 	    }
3844 #endif
3845 
3846 	    if (draw_state == WL_LINE - 1 && n_extra == 0)
3847 	    {
3848 		draw_state = WL_LINE;
3849 		if (saved_n_extra)
3850 		{
3851 		    /* Continue item from end of wrapped line. */
3852 		    n_extra = saved_n_extra;
3853 		    c_extra = saved_c_extra;
3854 		    p_extra = saved_p_extra;
3855 		    char_attr = saved_char_attr;
3856 		}
3857 		else
3858 		    char_attr = 0;
3859 	    }
3860 	}
3861 
3862 	/* When still displaying '$' of change command, stop at cursor */
3863 	if (dollar_vcol >= 0 && wp == curwin
3864 		   && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
3865 #ifdef FEAT_DIFF
3866 				   && filler_todo <= 0
3867 #endif
3868 		)
3869 	{
3870 	    SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3871 								  wp->w_p_rl);
3872 	    /* Pretend we have finished updating the window.  Except when
3873 	     * 'cursorcolumn' is set. */
3874 #ifdef FEAT_SYN_HL
3875 	    if (wp->w_p_cuc)
3876 		row = wp->w_cline_row + wp->w_cline_height;
3877 	    else
3878 #endif
3879 		row = wp->w_height;
3880 	    break;
3881 	}
3882 
3883 	if (draw_state == WL_LINE && area_highlighting)
3884 	{
3885 	    /* handle Visual or match highlighting in this line */
3886 	    if (vcol == fromcol
3887 #ifdef FEAT_MBYTE
3888 		    || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3889 			&& (*mb_ptr2cells)(ptr) > 1)
3890 #endif
3891 		    || ((int)vcol_prev == fromcol_prev
3892 			&& vcol_prev < vcol	/* not at margin */
3893 			&& vcol < tocol))
3894 		area_attr = attr;		/* start highlighting */
3895 	    else if (area_attr != 0
3896 		    && (vcol == tocol
3897 			|| (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
3898 		area_attr = 0;			/* stop highlighting */
3899 
3900 #ifdef FEAT_SEARCH_EXTRA
3901 	    if (!n_extra)
3902 	    {
3903 		/*
3904 		 * Check for start/end of search pattern match.
3905 		 * After end, check for start/end of next match.
3906 		 * When another match, have to check for start again.
3907 		 * Watch out for matching an empty string!
3908 		 * Do this for 'search_hl' and the match list (ordered by
3909 		 * priority).
3910 		 */
3911 		v = (long)(ptr - line);
3912 		cur = wp->w_match_head;
3913 		shl_flag = FALSE;
3914 		while (cur != NULL || shl_flag == FALSE)
3915 		{
3916 		    if (shl_flag == FALSE
3917 			    && ((cur != NULL
3918 				    && cur->priority > SEARCH_HL_PRIORITY)
3919 				|| cur == NULL))
3920 		    {
3921 			shl = &search_hl;
3922 			shl_flag = TRUE;
3923 		    }
3924 		    else
3925 			shl = &cur->hl;
3926 		    if (cur != NULL)
3927 			cur->pos.cur = 0;
3928 		    pos_inprogress = TRUE;
3929 		    while (shl->rm.regprog != NULL
3930 					   || (cur != NULL && pos_inprogress))
3931 		    {
3932 			if (shl->startcol != MAXCOL
3933 				&& v >= (long)shl->startcol
3934 				&& v < (long)shl->endcol)
3935 			{
3936 #ifdef FEAT_MBYTE
3937 			    int tmp_col = v + MB_PTR2LEN(ptr);
3938 
3939 			    if (shl->endcol < tmp_col)
3940 				shl->endcol = tmp_col;
3941 #endif
3942 			    shl->attr_cur = shl->attr;
3943 #ifdef FEAT_CONCEAL
3944 			    if (cur != NULL && syn_name2id((char_u *)"Conceal")
3945 							       == cur->hlg_id)
3946 			    {
3947 				has_match_conc = TRUE;
3948 				match_conc = cur->conceal_char;
3949 			    }
3950 			    else
3951 				has_match_conc = match_conc = FALSE;
3952 #endif
3953 			}
3954 			else if (v == (long)shl->endcol)
3955 			{
3956 			    shl->attr_cur = 0;
3957 #ifdef FEAT_CONCEAL
3958 			    prev_syntax_id = 0;
3959 #endif
3960 			    next_search_hl(wp, shl, lnum, (colnr_T)v, cur);
3961 			    pos_inprogress = cur == NULL || cur->pos.cur == 0
3962 							       ? FALSE : TRUE;
3963 
3964 			    /* Need to get the line again, a multi-line regexp
3965 			     * may have made it invalid. */
3966 			    line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3967 			    ptr = line + v;
3968 
3969 			    if (shl->lnum == lnum)
3970 			    {
3971 				shl->startcol = shl->rm.startpos[0].col;
3972 				if (shl->rm.endpos[0].lnum == 0)
3973 				    shl->endcol = shl->rm.endpos[0].col;
3974 				else
3975 				    shl->endcol = MAXCOL;
3976 
3977 				if (shl->startcol == shl->endcol)
3978 				{
3979 				    /* highlight empty match, try again after
3980 				     * it */
3981 #ifdef FEAT_MBYTE
3982 				    if (has_mbyte)
3983 					shl->endcol += (*mb_ptr2len)(line
3984 							       + shl->endcol);
3985 				    else
3986 #endif
3987 					++shl->endcol;
3988 				}
3989 
3990 				/* Loop to check if the match starts at the
3991 				 * current position */
3992 				continue;
3993 			    }
3994 			}
3995 			break;
3996 		    }
3997 		    if (shl != &search_hl && cur != NULL)
3998 			cur = cur->next;
3999 		}
4000 
4001 		/* Use attributes from match with highest priority among
4002 		 * 'search_hl' and the match list. */
4003 		search_attr = search_hl.attr_cur;
4004 		cur = wp->w_match_head;
4005 		shl_flag = FALSE;
4006 		while (cur != NULL || shl_flag == FALSE)
4007 		{
4008 		    if (shl_flag == FALSE
4009 			    && ((cur != NULL
4010 				    && cur->priority > SEARCH_HL_PRIORITY)
4011 				|| cur == NULL))
4012 		    {
4013 			shl = &search_hl;
4014 			shl_flag = TRUE;
4015 		    }
4016 		    else
4017 			shl = &cur->hl;
4018 		    if (shl->attr_cur != 0)
4019 			search_attr = shl->attr_cur;
4020 		    if (shl != &search_hl && cur != NULL)
4021 			cur = cur->next;
4022 		}
4023 	    }
4024 #endif
4025 
4026 #ifdef FEAT_DIFF
4027 	    if (diff_hlf != (hlf_T)0)
4028 	    {
4029 		if (diff_hlf == HLF_CHD && ptr - line >= change_start
4030 							      && n_extra == 0)
4031 		    diff_hlf = HLF_TXD;		/* changed text */
4032 		if (diff_hlf == HLF_TXD && ptr - line > change_end
4033 							      && n_extra == 0)
4034 		    diff_hlf = HLF_CHD;		/* changed line */
4035 		line_attr = hl_attr(diff_hlf);
4036 		if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4037 		    line_attr = hl_combine_attr(line_attr, hl_attr(HLF_CUL));
4038 	    }
4039 #endif
4040 
4041 	    /* Decide which of the highlight attributes to use. */
4042 	    attr_pri = TRUE;
4043 #ifdef LINE_ATTR
4044 	    if (area_attr != 0)
4045 		char_attr = hl_combine_attr(line_attr, area_attr);
4046 	    else if (search_attr != 0)
4047 		char_attr = hl_combine_attr(line_attr, search_attr);
4048 		/* Use line_attr when not in the Visual or 'incsearch' area
4049 		 * (area_attr may be 0 when "noinvcur" is set). */
4050 	    else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
4051 				|| vcol < fromcol || vcol_prev < fromcol_prev
4052 				|| vcol >= tocol))
4053 		char_attr = line_attr;
4054 #else
4055 	    if (area_attr != 0)
4056 		char_attr = area_attr;
4057 	    else if (search_attr != 0)
4058 		char_attr = search_attr;
4059 #endif
4060 	    else
4061 	    {
4062 		attr_pri = FALSE;
4063 #ifdef FEAT_SYN_HL
4064 		if (has_syntax)
4065 		    char_attr = syntax_attr;
4066 		else
4067 #endif
4068 		    char_attr = 0;
4069 	    }
4070 	}
4071 
4072 	/*
4073 	 * Get the next character to put on the screen.
4074 	 */
4075 	/*
4076 	 * The "p_extra" points to the extra stuff that is inserted to
4077 	 * represent special characters (non-printable stuff) and other
4078 	 * things.  When all characters are the same, c_extra is used.
4079 	 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4080 	 * "p_extra[n_extra]".
4081 	 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4082 	 */
4083 	if (n_extra > 0)
4084 	{
4085 	    if (c_extra != NUL)
4086 	    {
4087 		c = c_extra;
4088 #ifdef FEAT_MBYTE
4089 		mb_c = c;	/* doesn't handle non-utf-8 multi-byte! */
4090 		if (enc_utf8 && (*mb_char2len)(c) > 1)
4091 		{
4092 		    mb_utf8 = TRUE;
4093 		    u8cc[0] = 0;
4094 		    c = 0xc0;
4095 		}
4096 		else
4097 		    mb_utf8 = FALSE;
4098 #endif
4099 	    }
4100 	    else
4101 	    {
4102 		c = *p_extra;
4103 #ifdef FEAT_MBYTE
4104 		if (has_mbyte)
4105 		{
4106 		    mb_c = c;
4107 		    if (enc_utf8)
4108 		    {
4109 			/* If the UTF-8 character is more than one byte:
4110 			 * Decode it into "mb_c". */
4111 			mb_l = (*mb_ptr2len)(p_extra);
4112 			mb_utf8 = FALSE;
4113 			if (mb_l > n_extra)
4114 			    mb_l = 1;
4115 			else if (mb_l > 1)
4116 			{
4117 			    mb_c = utfc_ptr2char(p_extra, u8cc);
4118 			    mb_utf8 = TRUE;
4119 			    c = 0xc0;
4120 			}
4121 		    }
4122 		    else
4123 		    {
4124 			/* if this is a DBCS character, put it in "mb_c" */
4125 			mb_l = MB_BYTE2LEN(c);
4126 			if (mb_l >= n_extra)
4127 			    mb_l = 1;
4128 			else if (mb_l > 1)
4129 			    mb_c = (c << 8) + p_extra[1];
4130 		    }
4131 		    if (mb_l == 0)  /* at the NUL at end-of-line */
4132 			mb_l = 1;
4133 
4134 		    /* If a double-width char doesn't fit display a '>' in the
4135 		     * last column. */
4136 		    if ((
4137 # ifdef FEAT_RIGHTLEFT
4138 			    wp->w_p_rl ? (col <= 0) :
4139 # endif
4140 				    (col >= W_WIDTH(wp) - 1))
4141 			    && (*mb_char2cells)(mb_c) == 2)
4142 		    {
4143 			c = '>';
4144 			mb_c = c;
4145 			mb_l = 1;
4146 			mb_utf8 = FALSE;
4147 			multi_attr = hl_attr(HLF_AT);
4148 			/* put the pointer back to output the double-width
4149 			 * character at the start of the next line. */
4150 			++n_extra;
4151 			--p_extra;
4152 		    }
4153 		    else
4154 		    {
4155 			n_extra -= mb_l - 1;
4156 			p_extra += mb_l - 1;
4157 		    }
4158 		}
4159 #endif
4160 		++p_extra;
4161 	    }
4162 	    --n_extra;
4163 	}
4164 	else
4165 	{
4166 	    if (p_extra_free != NULL)
4167 	    {
4168 		vim_free(p_extra_free);
4169 		p_extra_free = NULL;
4170 	    }
4171 	    /*
4172 	     * Get a character from the line itself.
4173 	     */
4174 	    c = *ptr;
4175 #ifdef FEAT_MBYTE
4176 	    if (has_mbyte)
4177 	    {
4178 		mb_c = c;
4179 		if (enc_utf8)
4180 		{
4181 		    /* If the UTF-8 character is more than one byte: Decode it
4182 		     * into "mb_c". */
4183 		    mb_l = (*mb_ptr2len)(ptr);
4184 		    mb_utf8 = FALSE;
4185 		    if (mb_l > 1)
4186 		    {
4187 			mb_c = utfc_ptr2char(ptr, u8cc);
4188 			/* Overlong encoded ASCII or ASCII with composing char
4189 			 * is displayed normally, except a NUL. */
4190 			if (mb_c < 0x80)
4191 			    c = mb_c;
4192 			mb_utf8 = TRUE;
4193 
4194 			/* At start of the line we can have a composing char.
4195 			 * Draw it as a space with a composing char. */
4196 			if (utf_iscomposing(mb_c))
4197 			{
4198 			    int i;
4199 
4200 			    for (i = Screen_mco - 1; i > 0; --i)
4201 				u8cc[i] = u8cc[i - 1];
4202 			    u8cc[0] = mb_c;
4203 			    mb_c = ' ';
4204 			}
4205 		    }
4206 
4207 		    if ((mb_l == 1 && c >= 0x80)
4208 			    || (mb_l >= 1 && mb_c == 0)
4209 			    || (mb_l > 1 && (!vim_isprintc(mb_c)
4210 # ifdef UNICODE16
4211 							 || mb_c >= 0x10000
4212 # endif
4213 							 )))
4214 		    {
4215 			/*
4216 			 * Illegal UTF-8 byte: display as <xx>.
4217 			 * Non-BMP character : display as ? or fullwidth ?.
4218 			 */
4219 # ifdef UNICODE16
4220 			if (mb_c < 0x10000)
4221 # endif
4222 			{
4223 			    transchar_hex(extra, mb_c);
4224 # ifdef FEAT_RIGHTLEFT
4225 			    if (wp->w_p_rl)		/* reverse */
4226 				rl_mirror(extra);
4227 # endif
4228 			}
4229 # ifdef UNICODE16
4230 			else if (utf_char2cells(mb_c) != 2)
4231 			    STRCPY(extra, "?");
4232 			else
4233 			    /* 0xff1f in UTF-8: full-width '?' */
4234 			    STRCPY(extra, "\357\274\237");
4235 # endif
4236 
4237 			p_extra = extra;
4238 			c = *p_extra;
4239 			mb_c = mb_ptr2char_adv(&p_extra);
4240 			mb_utf8 = (c >= 0x80);
4241 			n_extra = (int)STRLEN(p_extra);
4242 			c_extra = NUL;
4243 			if (area_attr == 0 && search_attr == 0)
4244 			{
4245 			    n_attr = n_extra + 1;
4246 			    extra_attr = hl_attr(HLF_8);
4247 			    saved_attr2 = char_attr; /* save current attr */
4248 			}
4249 		    }
4250 		    else if (mb_l == 0)  /* at the NUL at end-of-line */
4251 			mb_l = 1;
4252 #ifdef FEAT_ARABIC
4253 		    else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4254 		    {
4255 			/* Do Arabic shaping. */
4256 			int	pc, pc1, nc;
4257 			int	pcc[MAX_MCO];
4258 
4259 			/* The idea of what is the previous and next
4260 			 * character depends on 'rightleft'. */
4261 			if (wp->w_p_rl)
4262 			{
4263 			    pc = prev_c;
4264 			    pc1 = prev_c1;
4265 			    nc = utf_ptr2char(ptr + mb_l);
4266 			    prev_c1 = u8cc[0];
4267 			}
4268 			else
4269 			{
4270 			    pc = utfc_ptr2char(ptr + mb_l, pcc);
4271 			    nc = prev_c;
4272 			    pc1 = pcc[0];
4273 			}
4274 			prev_c = mb_c;
4275 
4276 			mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
4277 		    }
4278 		    else
4279 			prev_c = mb_c;
4280 #endif
4281 		}
4282 		else	/* enc_dbcs */
4283 		{
4284 		    mb_l = MB_BYTE2LEN(c);
4285 		    if (mb_l == 0)  /* at the NUL at end-of-line */
4286 			mb_l = 1;
4287 		    else if (mb_l > 1)
4288 		    {
4289 			/* We assume a second byte below 32 is illegal.
4290 			 * Hopefully this is OK for all double-byte encodings!
4291 			 */
4292 			if (ptr[1] >= 32)
4293 			    mb_c = (c << 8) + ptr[1];
4294 			else
4295 			{
4296 			    if (ptr[1] == NUL)
4297 			    {
4298 				/* head byte at end of line */
4299 				mb_l = 1;
4300 				transchar_nonprint(extra, c);
4301 			    }
4302 			    else
4303 			    {
4304 				/* illegal tail byte */
4305 				mb_l = 2;
4306 				STRCPY(extra, "XX");
4307 			    }
4308 			    p_extra = extra;
4309 			    n_extra = (int)STRLEN(extra) - 1;
4310 			    c_extra = NUL;
4311 			    c = *p_extra++;
4312 			    if (area_attr == 0 && search_attr == 0)
4313 			    {
4314 				n_attr = n_extra + 1;
4315 				extra_attr = hl_attr(HLF_8);
4316 				saved_attr2 = char_attr; /* save current attr */
4317 			    }
4318 			    mb_c = c;
4319 			}
4320 		    }
4321 		}
4322 		/* If a double-width char doesn't fit display a '>' in the
4323 		 * last column; the character is displayed at the start of the
4324 		 * next line. */
4325 		if ((
4326 # ifdef FEAT_RIGHTLEFT
4327 			    wp->w_p_rl ? (col <= 0) :
4328 # endif
4329 				(col >= W_WIDTH(wp) - 1))
4330 			&& (*mb_char2cells)(mb_c) == 2)
4331 		{
4332 		    c = '>';
4333 		    mb_c = c;
4334 		    mb_utf8 = FALSE;
4335 		    mb_l = 1;
4336 		    multi_attr = hl_attr(HLF_AT);
4337 		    /* Put pointer back so that the character will be
4338 		     * displayed at the start of the next line. */
4339 		    --ptr;
4340 		}
4341 		else if (*ptr != NUL)
4342 		    ptr += mb_l - 1;
4343 
4344 		/* If a double-width char doesn't fit at the left side display
4345 		 * a '<' in the first column.  Don't do this for unprintable
4346 		 * characters. */
4347 		if (n_skip > 0 && mb_l > 1 && n_extra == 0)
4348 		{
4349 		    n_extra = 1;
4350 		    c_extra = MB_FILLER_CHAR;
4351 		    c = ' ';
4352 		    if (area_attr == 0 && search_attr == 0)
4353 		    {
4354 			n_attr = n_extra + 1;
4355 			extra_attr = hl_attr(HLF_AT);
4356 			saved_attr2 = char_attr; /* save current attr */
4357 		    }
4358 		    mb_c = c;
4359 		    mb_utf8 = FALSE;
4360 		    mb_l = 1;
4361 		}
4362 
4363 	    }
4364 #endif
4365 	    ++ptr;
4366 
4367 	    if (extra_check)
4368 	    {
4369 #ifdef FEAT_SPELL
4370 		int	can_spell = TRUE;
4371 #endif
4372 
4373 #ifdef FEAT_SYN_HL
4374 		/* Get syntax attribute, unless still at the start of the line
4375 		 * (double-wide char that doesn't fit). */
4376 		v = (long)(ptr - line);
4377 		if (has_syntax && v > 0)
4378 		{
4379 		    /* Get the syntax attribute for the character.  If there
4380 		     * is an error, disable syntax highlighting. */
4381 		    save_did_emsg = did_emsg;
4382 		    did_emsg = FALSE;
4383 
4384 		    syntax_attr = get_syntax_attr((colnr_T)v - 1,
4385 # ifdef FEAT_SPELL
4386 						has_spell ? &can_spell :
4387 # endif
4388 						NULL, FALSE);
4389 
4390 		    if (did_emsg)
4391 		    {
4392 			wp->w_s->b_syn_error = TRUE;
4393 			has_syntax = FALSE;
4394 		    }
4395 		    else
4396 			did_emsg = save_did_emsg;
4397 
4398 		    /* Need to get the line again, a multi-line regexp may
4399 		     * have made it invalid. */
4400 		    line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4401 		    ptr = line + v;
4402 
4403 		    if (!attr_pri)
4404 			char_attr = syntax_attr;
4405 		    else
4406 			char_attr = hl_combine_attr(syntax_attr, char_attr);
4407 # ifdef FEAT_CONCEAL
4408 		    /* no concealing past the end of the line, it interferes
4409 		     * with line highlighting */
4410 		    if (c == NUL)
4411 			syntax_flags = 0;
4412 		    else
4413 			syntax_flags = get_syntax_info(&syntax_seqnr);
4414 # endif
4415 		}
4416 #endif
4417 
4418 #ifdef FEAT_SPELL
4419 		/* Check spelling (unless at the end of the line).
4420 		 * Only do this when there is no syntax highlighting, the
4421 		 * @Spell cluster is not used or the current syntax item
4422 		 * contains the @Spell cluster. */
4423 		if (has_spell && v >= word_end && v > cur_checked_col)
4424 		{
4425 		    spell_attr = 0;
4426 # ifdef FEAT_SYN_HL
4427 		    if (!attr_pri)
4428 			char_attr = syntax_attr;
4429 # endif
4430 		    if (c != 0 && (
4431 # ifdef FEAT_SYN_HL
4432 				!has_syntax ||
4433 # endif
4434 				can_spell))
4435 		    {
4436 			char_u	*prev_ptr, *p;
4437 			int	len;
4438 			hlf_T	spell_hlf = HLF_COUNT;
4439 # ifdef FEAT_MBYTE
4440 			if (has_mbyte)
4441 			{
4442 			    prev_ptr = ptr - mb_l;
4443 			    v -= mb_l - 1;
4444 			}
4445 			else
4446 # endif
4447 			    prev_ptr = ptr - 1;
4448 
4449 			/* Use nextline[] if possible, it has the start of the
4450 			 * next line concatenated. */
4451 			if ((prev_ptr - line) - nextlinecol >= 0)
4452 			    p = nextline + (prev_ptr - line) - nextlinecol;
4453 			else
4454 			    p = prev_ptr;
4455 			cap_col -= (int)(prev_ptr - line);
4456 			len = spell_check(wp, p, &spell_hlf, &cap_col,
4457 								    nochange);
4458 			word_end = v + len;
4459 
4460 			/* In Insert mode only highlight a word that
4461 			 * doesn't touch the cursor. */
4462 			if (spell_hlf != HLF_COUNT
4463 				&& (State & INSERT) != 0
4464 				&& wp->w_cursor.lnum == lnum
4465 				&& wp->w_cursor.col >=
4466 						    (colnr_T)(prev_ptr - line)
4467 				&& wp->w_cursor.col < (colnr_T)word_end)
4468 			{
4469 			    spell_hlf = HLF_COUNT;
4470 			    spell_redraw_lnum = lnum;
4471 			}
4472 
4473 			if (spell_hlf == HLF_COUNT && p != prev_ptr
4474 				       && (p - nextline) + len > nextline_idx)
4475 			{
4476 			    /* Remember that the good word continues at the
4477 			     * start of the next line. */
4478 			    checked_lnum = lnum + 1;
4479 			    checked_col = (int)((p - nextline) + len - nextline_idx);
4480 			}
4481 
4482 			/* Turn index into actual attributes. */
4483 			if (spell_hlf != HLF_COUNT)
4484 			    spell_attr = highlight_attr[spell_hlf];
4485 
4486 			if (cap_col > 0)
4487 			{
4488 			    if (p != prev_ptr
4489 				   && (p - nextline) + cap_col >= nextline_idx)
4490 			    {
4491 				/* Remember that the word in the next line
4492 				 * must start with a capital. */
4493 				capcol_lnum = lnum + 1;
4494 				cap_col = (int)((p - nextline) + cap_col
4495 							       - nextline_idx);
4496 			    }
4497 			    else
4498 				/* Compute the actual column. */
4499 				cap_col += (int)(prev_ptr - line);
4500 			}
4501 		    }
4502 		}
4503 		if (spell_attr != 0)
4504 		{
4505 		    if (!attr_pri)
4506 			char_attr = hl_combine_attr(char_attr, spell_attr);
4507 		    else
4508 			char_attr = hl_combine_attr(spell_attr, char_attr);
4509 		}
4510 #endif
4511 #ifdef FEAT_LINEBREAK
4512 		/*
4513 		 * Found last space before word: check for line break.
4514 		 */
4515 		if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr))
4516 		{
4517 # ifdef FEAT_MBYTE
4518 		    int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
4519 # endif
4520 		    char_u *p = ptr - (
4521 # ifdef FEAT_MBYTE
4522 				mb_off +
4523 # endif
4524 				1);
4525 
4526 		    /* TODO: is passing p for start of the line OK? */
4527 		    n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
4528 								    NULL) - 1;
4529 		    if (c == TAB && n_extra + col > W_WIDTH(wp))
4530 			n_extra = (int)wp->w_buffer->b_p_ts
4531 				       - vcol % (int)wp->w_buffer->b_p_ts - 1;
4532 
4533 # ifdef FEAT_MBYTE
4534 		    c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
4535 # else
4536 		    c_extra = ' ';
4537 # endif
4538 		    if (vim_iswhite(c))
4539 		    {
4540 #ifdef FEAT_CONCEAL
4541 			if (c == TAB)
4542 			    /* See "Tab alignment" below. */
4543 			    FIX_FOR_BOGUSCOLS;
4544 #endif
4545 			if (!wp->w_p_list)
4546 			    c = ' ';
4547 		    }
4548 		}
4549 #endif
4550 
4551 		/* 'list': change char 160 to lcs_nbsp and space to lcs_space.
4552 		 */
4553 		if (wp->w_p_list
4554 			&& (((c == 160
4555 #ifdef FEAT_MBYTE
4556 			      || (mb_utf8 && (mb_c == 160 || mb_c == 0x202f))
4557 #endif
4558 			     ) && lcs_nbsp)
4559 			|| (c == ' ' && lcs_space && ptr - line <= trailcol)))
4560 		{
4561 		    c = (c == ' ') ? lcs_space : lcs_nbsp;
4562 		    if (area_attr == 0 && search_attr == 0)
4563 		    {
4564 			n_attr = 1;
4565 			extra_attr = hl_attr(HLF_8);
4566 			saved_attr2 = char_attr; /* save current attr */
4567 		    }
4568 #ifdef FEAT_MBYTE
4569 		    mb_c = c;
4570 		    if (enc_utf8 && (*mb_char2len)(c) > 1)
4571 		    {
4572 			mb_utf8 = TRUE;
4573 			u8cc[0] = 0;
4574 			c = 0xc0;
4575 		    }
4576 		    else
4577 			mb_utf8 = FALSE;
4578 #endif
4579 		}
4580 
4581 		if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4582 		{
4583 		    c = lcs_trail;
4584 		    if (!attr_pri)
4585 		    {
4586 			n_attr = 1;
4587 			extra_attr = hl_attr(HLF_8);
4588 			saved_attr2 = char_attr; /* save current attr */
4589 		    }
4590 #ifdef FEAT_MBYTE
4591 		    mb_c = c;
4592 		    if (enc_utf8 && (*mb_char2len)(c) > 1)
4593 		    {
4594 			mb_utf8 = TRUE;
4595 			u8cc[0] = 0;
4596 			c = 0xc0;
4597 		    }
4598 		    else
4599 			mb_utf8 = FALSE;
4600 #endif
4601 		}
4602 	    }
4603 
4604 	    /*
4605 	     * Handling of non-printable characters.
4606 	     */
4607 	    if (!vim_isprintc(c))
4608 	    {
4609 		/*
4610 		 * when getting a character from the file, we may have to
4611 		 * turn it into something else on the way to putting it
4612 		 * into "ScreenLines".
4613 		 */
4614 		if (c == TAB && (!wp->w_p_list || lcs_tab1))
4615 		{
4616 		    int tab_len = 0;
4617 		    long vcol_adjusted = vcol; /* removed showbreak length */
4618 #ifdef FEAT_LINEBREAK
4619 		    /* only adjust the tab_len, when at the first column
4620 		     * after the showbreak value was drawn */
4621 		    if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4622 			vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4623 #endif
4624 		    /* tab amount depends on current column */
4625 		    tab_len = (int)wp->w_buffer->b_p_ts
4626 					- vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4627 
4628 #ifdef FEAT_LINEBREAK
4629 		    if (!wp->w_p_lbr || !wp->w_p_list)
4630 #endif
4631 		    /* tab amount depends on current column */
4632 			n_extra = tab_len;
4633 #ifdef FEAT_LINEBREAK
4634 		    else
4635 		    {
4636 			char_u *p;
4637 			int	len = n_extra;
4638 			int	i;
4639 			int	saved_nextra = n_extra;
4640 
4641 #ifdef FEAT_CONCEAL
4642 			if (vcol_off > 0)
4643 			    /* there are characters to conceal */
4644 			    tab_len += vcol_off;
4645 			/* boguscols before FIX_FOR_BOGUSCOLS macro from above
4646 			 */
4647 			if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4648 							 && n_extra > tab_len)
4649 			    tab_len += n_extra - tab_len;
4650 #endif
4651 
4652 			/* if n_extra > 0, it gives the number of chars, to
4653 			 * use for a tab, else we need to calculate the width
4654 			 * for a tab */
4655 #ifdef FEAT_MBYTE
4656 			len = (tab_len * mb_char2len(lcs_tab2));
4657 			if (n_extra > 0)
4658 			    len += n_extra - tab_len;
4659 #endif
4660 			c = lcs_tab1;
4661 			p = alloc((unsigned)(len + 1));
4662 			vim_memset(p, ' ', len);
4663 			p[len] = NUL;
4664 			p_extra_free = p;
4665 			for (i = 0; i < tab_len; i++)
4666 			{
4667 #ifdef FEAT_MBYTE
4668 			    mb_char2bytes(lcs_tab2, p);
4669 			    p += mb_char2len(lcs_tab2);
4670 			    n_extra += mb_char2len(lcs_tab2)
4671 						 - (saved_nextra > 0 ? 1 : 0);
4672 #else
4673 			    p[i] = lcs_tab2;
4674 #endif
4675 			}
4676 			p_extra = p_extra_free;
4677 #ifdef FEAT_CONCEAL
4678 			/* n_extra will be increased by FIX_FOX_BOGUSCOLS
4679 			 * macro below, so need to adjust for that here */
4680 			if (vcol_off > 0)
4681 			    n_extra -= vcol_off;
4682 #endif
4683 		    }
4684 #endif
4685 #ifdef FEAT_CONCEAL
4686 		    {
4687 			int vc_saved = vcol_off;
4688 
4689 			/* Tab alignment should be identical regardless of
4690 			 * 'conceallevel' value. So tab compensates of all
4691 			 * previous concealed characters, and thus resets
4692 			 * vcol_off and boguscols accumulated so far in the
4693 			 * line. Note that the tab can be longer than
4694 			 * 'tabstop' when there are concealed characters. */
4695 			FIX_FOR_BOGUSCOLS;
4696 
4697 			/* Make sure, the highlighting for the tab char will be
4698 			 * correctly set further below (effectively reverts the
4699 			 * FIX_FOR_BOGSUCOLS macro */
4700 			if (n_extra == tab_len + vc_saved && wp->w_p_list
4701 								  && lcs_tab1)
4702 			    tab_len += vc_saved;
4703 		    }
4704 #endif
4705 #ifdef FEAT_MBYTE
4706 		    mb_utf8 = FALSE;	/* don't draw as UTF-8 */
4707 #endif
4708 		    if (wp->w_p_list)
4709 		    {
4710 			c = lcs_tab1;
4711 #ifdef FEAT_LINEBREAK
4712 			if (wp->w_p_lbr)
4713 			    c_extra = NUL; /* using p_extra from above */
4714 			else
4715 #endif
4716 			    c_extra = lcs_tab2;
4717 			n_attr = tab_len + 1;
4718 			extra_attr = hl_attr(HLF_8);
4719 			saved_attr2 = char_attr; /* save current attr */
4720 #ifdef FEAT_MBYTE
4721 			mb_c = c;
4722 			if (enc_utf8 && (*mb_char2len)(c) > 1)
4723 			{
4724 			    mb_utf8 = TRUE;
4725 			    u8cc[0] = 0;
4726 			    c = 0xc0;
4727 			}
4728 #endif
4729 		    }
4730 		    else
4731 		    {
4732 			c_extra = ' ';
4733 			c = ' ';
4734 		    }
4735 		}
4736 		else if (c == NUL
4737 			&& (wp->w_p_list
4738 			    || ((fromcol >= 0 || fromcol_prev >= 0)
4739 				&& tocol > vcol
4740 				&& VIsual_mode != Ctrl_V
4741 				&& (
4742 # ifdef FEAT_RIGHTLEFT
4743 				    wp->w_p_rl ? (col >= 0) :
4744 # endif
4745 				    (col < W_WIDTH(wp)))
4746 				&& !(noinvcur
4747 				    && lnum == wp->w_cursor.lnum
4748 				    && (colnr_T)vcol == wp->w_virtcol)))
4749 			&& lcs_eol_one > 0)
4750 		{
4751 		    /* Display a '$' after the line or highlight an extra
4752 		     * character if the line break is included. */
4753 #if defined(FEAT_DIFF) || defined(LINE_ATTR)
4754 		    /* For a diff line the highlighting continues after the
4755 		     * "$". */
4756 		    if (
4757 # ifdef FEAT_DIFF
4758 			    diff_hlf == (hlf_T)0
4759 #  ifdef LINE_ATTR
4760 			    &&
4761 #  endif
4762 # endif
4763 # ifdef LINE_ATTR
4764 			    line_attr == 0
4765 # endif
4766 		       )
4767 #endif
4768 		    {
4769 #ifdef FEAT_VIRTUALEDIT
4770 			/* In virtualedit, visual selections may extend
4771 			 * beyond end of line. */
4772 			if (area_highlighting && virtual_active()
4773 				&& tocol != MAXCOL && vcol < tocol)
4774 			    n_extra = 0;
4775 			else
4776 #endif
4777 			{
4778 			    p_extra = at_end_str;
4779 			    n_extra = 1;
4780 			    c_extra = NUL;
4781 			}
4782 		    }
4783 		    if (wp->w_p_list && lcs_eol > 0)
4784 			c = lcs_eol;
4785 		    else
4786 			c = ' ';
4787 		    lcs_eol_one = -1;
4788 		    --ptr;	    /* put it back at the NUL */
4789 		    if (!attr_pri)
4790 		    {
4791 			extra_attr = hl_attr(HLF_AT);
4792 			n_attr = 1;
4793 		    }
4794 #ifdef FEAT_MBYTE
4795 		    mb_c = c;
4796 		    if (enc_utf8 && (*mb_char2len)(c) > 1)
4797 		    {
4798 			mb_utf8 = TRUE;
4799 			u8cc[0] = 0;
4800 			c = 0xc0;
4801 		    }
4802 		    else
4803 			mb_utf8 = FALSE;	/* don't draw as UTF-8 */
4804 #endif
4805 		}
4806 		else if (c != NUL)
4807 		{
4808 		    p_extra = transchar(c);
4809 		    if (n_extra == 0)
4810 			n_extra = byte2cells(c) - 1;
4811 #ifdef FEAT_RIGHTLEFT
4812 		    if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4813 			rl_mirror(p_extra);	/* reverse "<12>" */
4814 #endif
4815 		    c_extra = NUL;
4816 #ifdef FEAT_LINEBREAK
4817 		    if (wp->w_p_lbr)
4818 		    {
4819 			char_u *p;
4820 
4821 			c = *p_extra;
4822 			p = alloc((unsigned)n_extra + 1);
4823 			vim_memset(p, ' ', n_extra);
4824 			STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
4825 			p[n_extra] = NUL;
4826 			p_extra_free = p_extra = p;
4827 		    }
4828 		    else
4829 #endif
4830 		    {
4831 			n_extra = byte2cells(c) - 1;
4832 			c = *p_extra++;
4833 		    }
4834 		    if (!attr_pri)
4835 		    {
4836 			n_attr = n_extra + 1;
4837 			extra_attr = hl_attr(HLF_8);
4838 			saved_attr2 = char_attr; /* save current attr */
4839 		    }
4840 #ifdef FEAT_MBYTE
4841 		    mb_utf8 = FALSE;	/* don't draw as UTF-8 */
4842 #endif
4843 		}
4844 #ifdef FEAT_VIRTUALEDIT
4845 		else if (VIsual_active
4846 			 && (VIsual_mode == Ctrl_V
4847 			     || VIsual_mode == 'v')
4848 			 && virtual_active()
4849 			 && tocol != MAXCOL
4850 			 && vcol < tocol
4851 			 && (
4852 # ifdef FEAT_RIGHTLEFT
4853 			    wp->w_p_rl ? (col >= 0) :
4854 # endif
4855 			    (col < W_WIDTH(wp))))
4856 		{
4857 		    c = ' ';
4858 		    --ptr;	    /* put it back at the NUL */
4859 		}
4860 #endif
4861 #if defined(LINE_ATTR)
4862 		else if ((
4863 # ifdef FEAT_DIFF
4864 			    diff_hlf != (hlf_T)0 ||
4865 # endif
4866 			    line_attr != 0
4867 			) && (
4868 # ifdef FEAT_RIGHTLEFT
4869 			    wp->w_p_rl ? (col >= 0) :
4870 # endif
4871 			    (col
4872 # ifdef FEAT_CONCEAL
4873 				- boguscols
4874 # endif
4875 					    < W_WIDTH(wp))))
4876 		{
4877 		    /* Highlight until the right side of the window */
4878 		    c = ' ';
4879 		    --ptr;	    /* put it back at the NUL */
4880 
4881 		    /* Remember we do the char for line highlighting. */
4882 		    ++did_line_attr;
4883 
4884 		    /* don't do search HL for the rest of the line */
4885 		    if (line_attr != 0 && char_attr == search_attr && col > 0)
4886 			char_attr = line_attr;
4887 # ifdef FEAT_DIFF
4888 		    if (diff_hlf == HLF_TXD)
4889 		    {
4890 			diff_hlf = HLF_CHD;
4891 			if (attr == 0 || char_attr != attr)
4892 			{
4893 			    char_attr = hl_attr(diff_hlf);
4894 			    if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4895 				char_attr = hl_combine_attr(char_attr,
4896 							    hl_attr(HLF_CUL));
4897 			}
4898 		    }
4899 # endif
4900 		}
4901 #endif
4902 	    }
4903 
4904 #ifdef FEAT_CONCEAL
4905 	    if (   wp->w_p_cole > 0
4906 		&& (wp != curwin || lnum != wp->w_cursor.lnum ||
4907 							conceal_cursor_line(wp) )
4908 		&& ( (syntax_flags & HL_CONCEAL) != 0 || has_match_conc)
4909 		&& !(lnum_in_visual_area
4910 				    && vim_strchr(wp->w_p_cocu, 'v') == NULL))
4911 	    {
4912 		char_attr = conceal_attr;
4913 		if (prev_syntax_id != syntax_seqnr
4914 			&& (syn_get_sub_char() != NUL || match_conc
4915 							 || wp->w_p_cole == 1)
4916 			&& wp->w_p_cole != 3)
4917 		{
4918 		    /* First time at this concealed item: display one
4919 		     * character. */
4920 		    if (match_conc)
4921 			c = match_conc;
4922 		    else if (syn_get_sub_char() != NUL)
4923 			c = syn_get_sub_char();
4924 		    else if (lcs_conceal != NUL)
4925 			c = lcs_conceal;
4926 		    else
4927 			c = ' ';
4928 
4929 		    prev_syntax_id = syntax_seqnr;
4930 
4931 		    if (n_extra > 0)
4932 			vcol_off += n_extra;
4933 		    vcol += n_extra;
4934 		    if (wp->w_p_wrap && n_extra > 0)
4935 		    {
4936 # ifdef FEAT_RIGHTLEFT
4937 			if (wp->w_p_rl)
4938 			{
4939 			    col -= n_extra;
4940 			    boguscols -= n_extra;
4941 			}
4942 			else
4943 # endif
4944 			{
4945 			    boguscols += n_extra;
4946 			    col += n_extra;
4947 			}
4948 		    }
4949 		    n_extra = 0;
4950 		    n_attr = 0;
4951 		}
4952 		else if (n_skip == 0)
4953 		{
4954 		    is_concealing = TRUE;
4955 		    n_skip = 1;
4956 		}
4957 # ifdef FEAT_MBYTE
4958 		mb_c = c;
4959 		if (enc_utf8 && (*mb_char2len)(c) > 1)
4960 		{
4961 		    mb_utf8 = TRUE;
4962 		    u8cc[0] = 0;
4963 		    c = 0xc0;
4964 		}
4965 		else
4966 		    mb_utf8 = FALSE;	/* don't draw as UTF-8 */
4967 # endif
4968 	    }
4969 	    else
4970 	    {
4971 		prev_syntax_id = 0;
4972 		is_concealing = FALSE;
4973 	    }
4974 #endif /* FEAT_CONCEAL */
4975 	}
4976 
4977 #ifdef FEAT_CONCEAL
4978 	/* In the cursor line and we may be concealing characters: correct
4979 	 * the cursor column when we reach its position. */
4980 	if (!did_wcol && draw_state == WL_LINE
4981 		&& wp == curwin && lnum == wp->w_cursor.lnum
4982 		&& conceal_cursor_line(wp)
4983 		&& (int)wp->w_virtcol <= vcol + n_skip)
4984 	{
4985 #  ifdef FEAT_RIGHTLEFT
4986 	    if (wp->w_p_rl)
4987 		wp->w_wcol = W_WIDTH(wp) - col + boguscols - 1;
4988 	    else
4989 #  endif
4990 		wp->w_wcol = col - boguscols;
4991 	    wp->w_wrow = row;
4992 	    did_wcol = TRUE;
4993 	}
4994 #endif
4995 
4996 	/* Don't override visual selection highlighting. */
4997 	if (n_attr > 0
4998 		&& draw_state == WL_LINE
4999 		&& !attr_pri)
5000 	    char_attr = extra_attr;
5001 
5002 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
5003 	/* XIM don't send preedit_start and preedit_end, but they send
5004 	 * preedit_changed and commit.  Thus Vim can't set "im_is_active", use
5005 	 * im_is_preediting() here. */
5006 	if (xic != NULL
5007 		&& lnum == wp->w_cursor.lnum
5008 		&& (State & INSERT)
5009 		&& !p_imdisable
5010 		&& im_is_preediting()
5011 		&& draw_state == WL_LINE)
5012 	{
5013 	    colnr_T tcol;
5014 
5015 	    if (preedit_end_col == MAXCOL)
5016 		getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
5017 	    else
5018 		tcol = preedit_end_col;
5019 	    if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5020 	    {
5021 		if (feedback_old_attr < 0)
5022 		{
5023 		    feedback_col = 0;
5024 		    feedback_old_attr = char_attr;
5025 		}
5026 		char_attr = im_get_feedback_attr(feedback_col);
5027 		if (char_attr < 0)
5028 		    char_attr = feedback_old_attr;
5029 		feedback_col++;
5030 	    }
5031 	    else if (feedback_old_attr >= 0)
5032 	    {
5033 		char_attr = feedback_old_attr;
5034 		feedback_old_attr = -1;
5035 		feedback_col = 0;
5036 	    }
5037 	}
5038 #endif
5039 	/*
5040 	 * Handle the case where we are in column 0 but not on the first
5041 	 * character of the line and the user wants us to show us a
5042 	 * special character (via 'listchars' option "precedes:<char>".
5043 	 */
5044 	if (lcs_prec_todo != NUL
5045 		&& wp->w_p_list
5046 		&& (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5047 #ifdef FEAT_DIFF
5048 		&& filler_todo <= 0
5049 #endif
5050 		&& draw_state > WL_NR
5051 		&& c != NUL)
5052 	{
5053 	    c = lcs_prec;
5054 	    lcs_prec_todo = NUL;
5055 #ifdef FEAT_MBYTE
5056 	    if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5057 	    {
5058 		/* Double-width character being overwritten by the "precedes"
5059 		 * character, need to fill up half the character. */
5060 		c_extra = MB_FILLER_CHAR;
5061 		n_extra = 1;
5062 		n_attr = 2;
5063 		extra_attr = hl_attr(HLF_AT);
5064 	    }
5065 	    mb_c = c;
5066 	    if (enc_utf8 && (*mb_char2len)(c) > 1)
5067 	    {
5068 		mb_utf8 = TRUE;
5069 		u8cc[0] = 0;
5070 		c = 0xc0;
5071 	    }
5072 	    else
5073 		mb_utf8 = FALSE;	/* don't draw as UTF-8 */
5074 #endif
5075 	    if (!attr_pri)
5076 	    {
5077 		saved_attr3 = char_attr; /* save current attr */
5078 		char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
5079 		n_attr3 = 1;
5080 	    }
5081 	}
5082 
5083 	/*
5084 	 * At end of the text line or just after the last character.
5085 	 */
5086 	if (c == NUL
5087 #if defined(LINE_ATTR)
5088 		|| did_line_attr == 1
5089 #endif
5090 		)
5091 	{
5092 #ifdef FEAT_SEARCH_EXTRA
5093 	    long prevcol = (long)(ptr - line) - (c == NUL);
5094 
5095 	    /* we're not really at that column when skipping some text */
5096 	    if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
5097 		++prevcol;
5098 #endif
5099 
5100 	    /* Invert at least one char, used for Visual and empty line or
5101 	     * highlight match at end of line. If it's beyond the last
5102 	     * char on the screen, just overwrite that one (tricky!)  Not
5103 	     * needed when a '$' was displayed for 'list'. */
5104 #ifdef FEAT_SEARCH_EXTRA
5105 	    prevcol_hl_flag = FALSE;
5106 	    if (prevcol == (long)search_hl.startcol)
5107 		prevcol_hl_flag = TRUE;
5108 	    else
5109 	    {
5110 		cur = wp->w_match_head;
5111 		while (cur != NULL)
5112 		{
5113 		    if (prevcol == (long)cur->hl.startcol)
5114 		    {
5115 			prevcol_hl_flag = TRUE;
5116 			break;
5117 		    }
5118 		    cur = cur->next;
5119 		}
5120 	    }
5121 #endif
5122 	    if (lcs_eol == lcs_eol_one
5123 		    && ((area_attr != 0 && vcol == fromcol
5124 			    && (VIsual_mode != Ctrl_V
5125 				|| lnum == VIsual.lnum
5126 				|| lnum == curwin->w_cursor.lnum)
5127 			    && c == NUL)
5128 #ifdef FEAT_SEARCH_EXTRA
5129 			/* highlight 'hlsearch' match at end of line */
5130 			|| (prevcol_hl_flag == TRUE
5131 # if defined(LINE_ATTR)
5132 			    && did_line_attr <= 1
5133 # endif
5134 			   )
5135 #endif
5136 		       ))
5137 	    {
5138 		int n = 0;
5139 
5140 #ifdef FEAT_RIGHTLEFT
5141 		if (wp->w_p_rl)
5142 		{
5143 		    if (col < 0)
5144 			n = 1;
5145 		}
5146 		else
5147 #endif
5148 		{
5149 		    if (col >= W_WIDTH(wp))
5150 			n = -1;
5151 		}
5152 		if (n != 0)
5153 		{
5154 		    /* At the window boundary, highlight the last character
5155 		     * instead (better than nothing). */
5156 		    off += n;
5157 		    col += n;
5158 		}
5159 		else
5160 		{
5161 		    /* Add a blank character to highlight. */
5162 		    ScreenLines[off] = ' ';
5163 #ifdef FEAT_MBYTE
5164 		    if (enc_utf8)
5165 			ScreenLinesUC[off] = 0;
5166 #endif
5167 		}
5168 #ifdef FEAT_SEARCH_EXTRA
5169 		if (area_attr == 0)
5170 		{
5171 		    /* Use attributes from match with highest priority among
5172 		     * 'search_hl' and the match list. */
5173 		    char_attr = search_hl.attr;
5174 		    cur = wp->w_match_head;
5175 		    shl_flag = FALSE;
5176 		    while (cur != NULL || shl_flag == FALSE)
5177 		    {
5178 			if (shl_flag == FALSE
5179 				&& ((cur != NULL
5180 					&& cur->priority > SEARCH_HL_PRIORITY)
5181 				    || cur == NULL))
5182 			{
5183 			    shl = &search_hl;
5184 			    shl_flag = TRUE;
5185 			}
5186 			else
5187 			    shl = &cur->hl;
5188 			if ((ptr - line) - 1 == (long)shl->startcol)
5189 			    char_attr = shl->attr;
5190 			if (shl != &search_hl && cur != NULL)
5191 			    cur = cur->next;
5192 		    }
5193 		}
5194 #endif
5195 		ScreenAttrs[off] = char_attr;
5196 #ifdef FEAT_RIGHTLEFT
5197 		if (wp->w_p_rl)
5198 		{
5199 		    --col;
5200 		    --off;
5201 		}
5202 		else
5203 #endif
5204 		{
5205 		    ++col;
5206 		    ++off;
5207 		}
5208 		++vcol;
5209 #ifdef FEAT_SYN_HL
5210 		eol_hl_off = 1;
5211 #endif
5212 	    }
5213 	}
5214 
5215 	/*
5216 	 * At end of the text line.
5217 	 */
5218 	if (c == NUL)
5219 	{
5220 #ifdef FEAT_SYN_HL
5221 	    if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
5222 		    && lnum == wp->w_cursor.lnum)
5223 	    {
5224 		/* highlight last char after line */
5225 		--col;
5226 		--off;
5227 		--vcol;
5228 	    }
5229 
5230 	    /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
5231 	    if (wp->w_p_wrap)
5232 		v = wp->w_skipcol;
5233 	    else
5234 		v = wp->w_leftcol;
5235 
5236 	    /* check if line ends before left margin */
5237 	    if (vcol < v + col - win_col_off(wp))
5238 		vcol = v + col - win_col_off(wp);
5239 #ifdef FEAT_CONCEAL
5240 	    /* Get rid of the boguscols now, we want to draw until the right
5241 	     * edge for 'cursorcolumn'. */
5242 	    col -= boguscols;
5243 	    boguscols = 0;
5244 #endif
5245 
5246 	    if (draw_color_col)
5247 		draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
5248 
5249 	    if (((wp->w_p_cuc
5250 		      && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5251 		      && (int)wp->w_virtcol <
5252 					W_WIDTH(wp) * (row - startrow + 1) + v
5253 		      && lnum != wp->w_cursor.lnum)
5254 		    || draw_color_col)
5255 # ifdef FEAT_RIGHTLEFT
5256 		    && !wp->w_p_rl
5257 # endif
5258 		    )
5259 	    {
5260 		int	rightmost_vcol = 0;
5261 		int	i;
5262 
5263 		if (wp->w_p_cuc)
5264 		    rightmost_vcol = wp->w_virtcol;
5265 		if (draw_color_col)
5266 		    /* determine rightmost colorcolumn to possibly draw */
5267 		    for (i = 0; color_cols[i] >= 0; ++i)
5268 			if (rightmost_vcol < color_cols[i])
5269 			    rightmost_vcol = color_cols[i];
5270 
5271 		while (col < W_WIDTH(wp))
5272 		{
5273 		    ScreenLines[off] = ' ';
5274 #ifdef FEAT_MBYTE
5275 		    if (enc_utf8)
5276 			ScreenLinesUC[off] = 0;
5277 #endif
5278 		    ++col;
5279 		    if (draw_color_col)
5280 			draw_color_col = advance_color_col(VCOL_HLC,
5281 								 &color_cols);
5282 
5283 		    if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
5284 			ScreenAttrs[off++] = hl_attr(HLF_CUC);
5285 		    else if (draw_color_col && VCOL_HLC == *color_cols)
5286 			ScreenAttrs[off++] = hl_attr(HLF_MC);
5287 		    else
5288 			ScreenAttrs[off++] = 0;
5289 
5290 		    if (VCOL_HLC >= rightmost_vcol)
5291 			break;
5292 
5293 		    ++vcol;
5294 		}
5295 	    }
5296 #endif
5297 
5298 	    SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5299 						(int)W_WIDTH(wp), wp->w_p_rl);
5300 	    row++;
5301 
5302 	    /*
5303 	     * Update w_cline_height and w_cline_folded if the cursor line was
5304 	     * updated (saves a call to plines() later).
5305 	     */
5306 	    if (wp == curwin && lnum == curwin->w_cursor.lnum)
5307 	    {
5308 		curwin->w_cline_row = startrow;
5309 		curwin->w_cline_height = row - startrow;
5310 #ifdef FEAT_FOLDING
5311 		curwin->w_cline_folded = FALSE;
5312 #endif
5313 		curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5314 	    }
5315 
5316 	    break;
5317 	}
5318 
5319 	/* line continues beyond line end */
5320 	if (lcs_ext
5321 		&& !wp->w_p_wrap
5322 #ifdef FEAT_DIFF
5323 		&& filler_todo <= 0
5324 #endif
5325 		&& (
5326 #ifdef FEAT_RIGHTLEFT
5327 		    wp->w_p_rl ? col == 0 :
5328 #endif
5329 		    col == W_WIDTH(wp) - 1)
5330 		&& (*ptr != NUL
5331 		    || (wp->w_p_list && lcs_eol_one > 0)
5332 		    || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5333 	{
5334 	    c = lcs_ext;
5335 	    char_attr = hl_attr(HLF_AT);
5336 #ifdef FEAT_MBYTE
5337 	    mb_c = c;
5338 	    if (enc_utf8 && (*mb_char2len)(c) > 1)
5339 	    {
5340 		mb_utf8 = TRUE;
5341 		u8cc[0] = 0;
5342 		c = 0xc0;
5343 	    }
5344 	    else
5345 		mb_utf8 = FALSE;
5346 #endif
5347 	}
5348 
5349 #ifdef FEAT_SYN_HL
5350 	/* advance to the next 'colorcolumn' */
5351 	if (draw_color_col)
5352 	    draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
5353 
5354 	/* Highlight the cursor column if 'cursorcolumn' is set.  But don't
5355 	 * highlight the cursor position itself.
5356 	 * Also highlight the 'colorcolumn' if it is different than
5357 	 * 'cursorcolumn' */
5358 	vcol_save_attr = -1;
5359 	if (draw_state == WL_LINE && !lnum_in_visual_area)
5360 	{
5361 	    if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
5362 						 && lnum != wp->w_cursor.lnum)
5363 	    {
5364 		vcol_save_attr = char_attr;
5365 		char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
5366 	    }
5367 	    else if (draw_color_col && VCOL_HLC == *color_cols)
5368 	    {
5369 		vcol_save_attr = char_attr;
5370 		char_attr = hl_combine_attr(char_attr, hl_attr(HLF_MC));
5371 	    }
5372 	}
5373 #endif
5374 
5375 	/*
5376 	 * Store character to be displayed.
5377 	 * Skip characters that are left of the screen for 'nowrap'.
5378 	 */
5379 	vcol_prev = vcol;
5380 	if (draw_state < WL_LINE || n_skip <= 0)
5381 	{
5382 	    /*
5383 	     * Store the character.
5384 	     */
5385 #if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5386 	    if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5387 	    {
5388 		/* A double-wide character is: put first halve in left cell. */
5389 		--off;
5390 		--col;
5391 	    }
5392 #endif
5393 	    ScreenLines[off] = c;
5394 #ifdef FEAT_MBYTE
5395 	    if (enc_dbcs == DBCS_JPNU)
5396 	    {
5397 		if ((mb_c & 0xff00) == 0x8e00)
5398 		    ScreenLines[off] = 0x8e;
5399 		ScreenLines2[off] = mb_c & 0xff;
5400 	    }
5401 	    else if (enc_utf8)
5402 	    {
5403 		if (mb_utf8)
5404 		{
5405 		    int i;
5406 
5407 		    ScreenLinesUC[off] = mb_c;
5408 		    if ((c & 0xff) == 0)
5409 			ScreenLines[off] = 0x80;   /* avoid storing zero */
5410 		    for (i = 0; i < Screen_mco; ++i)
5411 		    {
5412 			ScreenLinesC[i][off] = u8cc[i];
5413 			if (u8cc[i] == 0)
5414 			    break;
5415 		    }
5416 		}
5417 		else
5418 		    ScreenLinesUC[off] = 0;
5419 	    }
5420 	    if (multi_attr)
5421 	    {
5422 		ScreenAttrs[off] = multi_attr;
5423 		multi_attr = 0;
5424 	    }
5425 	    else
5426 #endif
5427 		ScreenAttrs[off] = char_attr;
5428 
5429 #ifdef FEAT_MBYTE
5430 	    if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5431 	    {
5432 		/* Need to fill two screen columns. */
5433 		++off;
5434 		++col;
5435 		if (enc_utf8)
5436 		    /* UTF-8: Put a 0 in the second screen char. */
5437 		    ScreenLines[off] = 0;
5438 		else
5439 		    /* DBCS: Put second byte in the second screen char. */
5440 		    ScreenLines[off] = mb_c & 0xff;
5441 		if (draw_state > WL_NR
5442 #ifdef FEAT_DIFF
5443 			&& filler_todo <= 0
5444 #endif
5445 			)
5446 		    ++vcol;
5447 		/* When "tocol" is halfway a character, set it to the end of
5448 		 * the character, otherwise highlighting won't stop. */
5449 		if (tocol == vcol)
5450 		    ++tocol;
5451 #ifdef FEAT_RIGHTLEFT
5452 		if (wp->w_p_rl)
5453 		{
5454 		    /* now it's time to backup one cell */
5455 		    --off;
5456 		    --col;
5457 		}
5458 #endif
5459 	    }
5460 #endif
5461 #ifdef FEAT_RIGHTLEFT
5462 	    if (wp->w_p_rl)
5463 	    {
5464 		--off;
5465 		--col;
5466 	    }
5467 	    else
5468 #endif
5469 	    {
5470 		++off;
5471 		++col;
5472 	    }
5473 	}
5474 #ifdef FEAT_CONCEAL
5475 	else if (wp->w_p_cole > 0 && is_concealing)
5476 	{
5477 	    --n_skip;
5478 	    ++vcol_off;
5479 	    if (n_extra > 0)
5480 		vcol_off += n_extra;
5481 	    if (wp->w_p_wrap)
5482 	    {
5483 		/*
5484 		 * Special voodoo required if 'wrap' is on.
5485 		 *
5486 		 * Advance the column indicator to force the line
5487 		 * drawing to wrap early. This will make the line
5488 		 * take up the same screen space when parts are concealed,
5489 		 * so that cursor line computations aren't messed up.
5490 		 *
5491 		 * To avoid the fictitious advance of 'col' causing
5492 		 * trailing junk to be written out of the screen line
5493 		 * we are building, 'boguscols' keeps track of the number
5494 		 * of bad columns we have advanced.
5495 		 */
5496 		if (n_extra > 0)
5497 		{
5498 		    vcol += n_extra;
5499 # ifdef FEAT_RIGHTLEFT
5500 		    if (wp->w_p_rl)
5501 		    {
5502 			col -= n_extra;
5503 			boguscols -= n_extra;
5504 		    }
5505 		    else
5506 # endif
5507 		    {
5508 			col += n_extra;
5509 			boguscols += n_extra;
5510 		    }
5511 		    n_extra = 0;
5512 		    n_attr = 0;
5513 		}
5514 
5515 
5516 # ifdef FEAT_MBYTE
5517 		if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5518 		{
5519 		    /* Need to fill two screen columns. */
5520 #  ifdef FEAT_RIGHTLEFT
5521 		    if (wp->w_p_rl)
5522 		    {
5523 			--boguscols;
5524 			--col;
5525 		    }
5526 		    else
5527 #  endif
5528 		    {
5529 			++boguscols;
5530 			++col;
5531 		    }
5532 		}
5533 # endif
5534 
5535 # ifdef FEAT_RIGHTLEFT
5536 		if (wp->w_p_rl)
5537 		{
5538 		    --boguscols;
5539 		    --col;
5540 		}
5541 		else
5542 # endif
5543 		{
5544 		    ++boguscols;
5545 		    ++col;
5546 		}
5547 	    }
5548 	    else
5549 	    {
5550 		if (n_extra > 0)
5551 		{
5552 		    vcol += n_extra;
5553 		    n_extra = 0;
5554 		    n_attr = 0;
5555 		}
5556 	    }
5557 
5558 	}
5559 #endif /* FEAT_CONCEAL */
5560 	else
5561 	    --n_skip;
5562 
5563 	/* Only advance the "vcol" when after the 'number' or 'relativenumber'
5564 	 * column. */
5565 	if (draw_state > WL_NR
5566 #ifdef FEAT_DIFF
5567 		&& filler_todo <= 0
5568 #endif
5569 		)
5570 	    ++vcol;
5571 
5572 #ifdef FEAT_SYN_HL
5573 	if (vcol_save_attr >= 0)
5574 	    char_attr = vcol_save_attr;
5575 #endif
5576 
5577 	/* restore attributes after "predeces" in 'listchars' */
5578 	if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5579 	    char_attr = saved_attr3;
5580 
5581 	/* restore attributes after last 'listchars' or 'number' char */
5582 	if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5583 	    char_attr = saved_attr2;
5584 
5585 	/*
5586 	 * At end of screen line and there is more to come: Display the line
5587 	 * so far.  If there is no more to display it is caught above.
5588 	 */
5589 	if ((
5590 #ifdef FEAT_RIGHTLEFT
5591 	    wp->w_p_rl ? (col < 0) :
5592 #endif
5593 				    (col >= W_WIDTH(wp)))
5594 		&& (*ptr != NUL
5595 #ifdef FEAT_DIFF
5596 		    || filler_todo > 0
5597 #endif
5598 		    || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
5599 		    || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5600 		)
5601 	{
5602 #ifdef FEAT_CONCEAL
5603 	    SCREEN_LINE(screen_row, W_WINCOL(wp), col - boguscols,
5604 						(int)W_WIDTH(wp), wp->w_p_rl);
5605 	    boguscols = 0;
5606 #else
5607 	    SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5608 						(int)W_WIDTH(wp), wp->w_p_rl);
5609 #endif
5610 	    ++row;
5611 	    ++screen_row;
5612 
5613 	    /* When not wrapping and finished diff lines, or when displayed
5614 	     * '$' and highlighting until last column, break here. */
5615 	    if ((!wp->w_p_wrap
5616 #ifdef FEAT_DIFF
5617 		    && filler_todo <= 0
5618 #endif
5619 		    ) || lcs_eol_one == -1)
5620 		break;
5621 
5622 	    /* When the window is too narrow draw all "@" lines. */
5623 	    if (draw_state != WL_LINE
5624 #ifdef FEAT_DIFF
5625 		    && filler_todo <= 0
5626 #endif
5627 		    )
5628 	    {
5629 		win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
5630 #ifdef FEAT_WINDOWS
5631 		draw_vsep_win(wp, row);
5632 #endif
5633 		row = endrow;
5634 	    }
5635 
5636 	    /* When line got too long for screen break here. */
5637 	    if (row == endrow)
5638 	    {
5639 		++row;
5640 		break;
5641 	    }
5642 
5643 	    if (screen_cur_row == screen_row - 1
5644 #ifdef FEAT_DIFF
5645 		     && filler_todo <= 0
5646 #endif
5647 		     && W_WIDTH(wp) == Columns)
5648 	    {
5649 		/* Remember that the line wraps, used for modeless copy. */
5650 		LineWraps[screen_row - 1] = TRUE;
5651 
5652 		/*
5653 		 * Special trick to make copy/paste of wrapped lines work with
5654 		 * xterm/screen: write an extra character beyond the end of
5655 		 * the line. This will work with all terminal types
5656 		 * (regardless of the xn,am settings).
5657 		 * Only do this on a fast tty.
5658 		 * Only do this if the cursor is on the current line
5659 		 * (something has been written in it).
5660 		 * Don't do this for the GUI.
5661 		 * Don't do this for double-width characters.
5662 		 * Don't do this for a window not at the right screen border.
5663 		 */
5664 		if (p_tf
5665 #ifdef FEAT_GUI
5666 			 && !gui.in_use
5667 #endif
5668 #ifdef FEAT_MBYTE
5669 			 && !(has_mbyte
5670 			     && ((*mb_off2cells)(LineOffset[screen_row],
5671 				     LineOffset[screen_row] + screen_Columns)
5672 									  == 2
5673 				 || (*mb_off2cells)(LineOffset[screen_row - 1]
5674 							+ (int)Columns - 2,
5675 				     LineOffset[screen_row] + screen_Columns)
5676 									== 2))
5677 #endif
5678 		   )
5679 		{
5680 		    /* First make sure we are at the end of the screen line,
5681 		     * then output the same character again to let the
5682 		     * terminal know about the wrap.  If the terminal doesn't
5683 		     * auto-wrap, we overwrite the character. */
5684 		    if (screen_cur_col != W_WIDTH(wp))
5685 			screen_char(LineOffset[screen_row - 1]
5686 						      + (unsigned)Columns - 1,
5687 					  screen_row - 1, (int)(Columns - 1));
5688 
5689 #ifdef FEAT_MBYTE
5690 		    /* When there is a multi-byte character, just output a
5691 		     * space to keep it simple. */
5692 		    if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5693 					screen_row - 1] + (Columns - 1)]) > 1)
5694 			out_char(' ');
5695 		    else
5696 #endif
5697 			out_char(ScreenLines[LineOffset[screen_row - 1]
5698 							    + (Columns - 1)]);
5699 		    /* force a redraw of the first char on the next line */
5700 		    ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5701 		    screen_start();	/* don't know where cursor is now */
5702 		}
5703 	    }
5704 
5705 	    col = 0;
5706 	    off = (unsigned)(current_ScreenLine - ScreenLines);
5707 #ifdef FEAT_RIGHTLEFT
5708 	    if (wp->w_p_rl)
5709 	    {
5710 		col = W_WIDTH(wp) - 1;	/* col is not used if breaking! */
5711 		off += col;
5712 	    }
5713 #endif
5714 
5715 	    /* reset the drawing state for the start of a wrapped line */
5716 	    draw_state = WL_START;
5717 	    saved_n_extra = n_extra;
5718 	    saved_p_extra = p_extra;
5719 	    saved_c_extra = c_extra;
5720 	    saved_char_attr = char_attr;
5721 	    n_extra = 0;
5722 	    lcs_prec_todo = lcs_prec;
5723 #ifdef FEAT_LINEBREAK
5724 # ifdef FEAT_DIFF
5725 	    if (filler_todo <= 0)
5726 # endif
5727 		need_showbreak = TRUE;
5728 #endif
5729 #ifdef FEAT_DIFF
5730 	    --filler_todo;
5731 	    /* When the filler lines are actually below the last line of the
5732 	     * file, don't draw the line itself, break here. */
5733 	    if (filler_todo == 0 && wp->w_botfill)
5734 		break;
5735 #endif
5736 	}
5737 
5738     }	/* for every character in the line */
5739 
5740 #ifdef FEAT_SPELL
5741     /* After an empty line check first word for capital. */
5742     if (*skipwhite(line) == NUL)
5743     {
5744 	capcol_lnum = lnum + 1;
5745 	cap_col = 0;
5746     }
5747 #endif
5748 
5749     return row;
5750 }
5751 
5752 #ifdef FEAT_MBYTE
5753 static int comp_char_differs(int, int);
5754 
5755 /*
5756  * Return if the composing characters at "off_from" and "off_to" differ.
5757  * Only to be used when ScreenLinesUC[off_from] != 0.
5758  */
5759     static int
5760 comp_char_differs(int off_from, int off_to)
5761 {
5762     int	    i;
5763 
5764     for (i = 0; i < Screen_mco; ++i)
5765     {
5766 	if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5767 	    return TRUE;
5768 	if (ScreenLinesC[i][off_from] == 0)
5769 	    break;
5770     }
5771     return FALSE;
5772 }
5773 #endif
5774 
5775 /*
5776  * Check whether the given character needs redrawing:
5777  * - the (first byte of the) character is different
5778  * - the attributes are different
5779  * - the character is multi-byte and the next byte is different
5780  * - the character is two cells wide and the second cell differs.
5781  */
5782     static int
5783 char_needs_redraw(int off_from, int off_to, int cols)
5784 {
5785     if (cols > 0
5786 	    && ((ScreenLines[off_from] != ScreenLines[off_to]
5787 		    || ScreenAttrs[off_from] != ScreenAttrs[off_to])
5788 
5789 #ifdef FEAT_MBYTE
5790 		|| (enc_dbcs != 0
5791 		    && MB_BYTE2LEN(ScreenLines[off_from]) > 1
5792 		    && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
5793 			? ScreenLines2[off_from] != ScreenLines2[off_to]
5794 			: (cols > 1 && ScreenLines[off_from + 1]
5795 						 != ScreenLines[off_to + 1])))
5796 		|| (enc_utf8
5797 		    && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
5798 			|| (ScreenLinesUC[off_from] != 0
5799 			    && comp_char_differs(off_from, off_to))
5800 			|| ((*mb_off2cells)(off_from, off_from + cols) > 1
5801 			    && ScreenLines[off_from + 1]
5802 						  != ScreenLines[off_to + 1])))
5803 #endif
5804 	       ))
5805 	return TRUE;
5806     return FALSE;
5807 }
5808 
5809 /*
5810  * Move one "cooked" screen line to the screen, but only the characters that
5811  * have actually changed.  Handle insert/delete character.
5812  * "coloff" gives the first column on the screen for this line.
5813  * "endcol" gives the columns where valid characters are.
5814  * "clear_width" is the width of the window.  It's > 0 if the rest of the line
5815  * needs to be cleared, negative otherwise.
5816  * "rlflag" is TRUE in a rightleft window:
5817  *    When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
5818  *    When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
5819  */
5820     static void
5821 screen_line(
5822     int	    row,
5823     int	    coloff,
5824     int	    endcol,
5825     int	    clear_width
5826 #ifdef FEAT_RIGHTLEFT
5827     , int   rlflag
5828 #endif
5829     )
5830 {
5831     unsigned	    off_from;
5832     unsigned	    off_to;
5833 #ifdef FEAT_MBYTE
5834     unsigned	    max_off_from;
5835     unsigned	    max_off_to;
5836 #endif
5837     int		    col = 0;
5838 #if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_WINDOWS)
5839     int		    hl;
5840 #endif
5841     int		    force = FALSE;	/* force update rest of the line */
5842     int		    redraw_this		/* bool: does character need redraw? */
5843 #ifdef FEAT_GUI
5844 				= TRUE	/* For GUI when while-loop empty */
5845 #endif
5846 				;
5847     int		    redraw_next;	/* redraw_this for next character */
5848 #ifdef FEAT_MBYTE
5849     int		    clear_next = FALSE;
5850     int		    char_cells;		/* 1: normal char */
5851 					/* 2: occupies two display cells */
5852 # define CHAR_CELLS char_cells
5853 #else
5854 # define CHAR_CELLS 1
5855 #endif
5856 
5857     /* Check for illegal row and col, just in case. */
5858     if (row >= Rows)
5859 	row = Rows - 1;
5860     if (endcol > Columns)
5861 	endcol = Columns;
5862 
5863 # ifdef FEAT_CLIPBOARD
5864     clip_may_clear_selection(row, row);
5865 # endif
5866 
5867     off_from = (unsigned)(current_ScreenLine - ScreenLines);
5868     off_to = LineOffset[row] + coloff;
5869 #ifdef FEAT_MBYTE
5870     max_off_from = off_from + screen_Columns;
5871     max_off_to = LineOffset[row] + screen_Columns;
5872 #endif
5873 
5874 #ifdef FEAT_RIGHTLEFT
5875     if (rlflag)
5876     {
5877 	/* Clear rest first, because it's left of the text. */
5878 	if (clear_width > 0)
5879 	{
5880 	    while (col <= endcol && ScreenLines[off_to] == ' '
5881 		    && ScreenAttrs[off_to] == 0
5882 # ifdef FEAT_MBYTE
5883 				  && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5884 # endif
5885 						  )
5886 	    {
5887 		++off_to;
5888 		++col;
5889 	    }
5890 	    if (col <= endcol)
5891 		screen_fill(row, row + 1, col + coloff,
5892 					    endcol + coloff + 1, ' ', ' ', 0);
5893 	}
5894 	col = endcol + 1;
5895 	off_to = LineOffset[row] + col + coloff;
5896 	off_from += col;
5897 	endcol = (clear_width > 0 ? clear_width : -clear_width);
5898     }
5899 #endif /* FEAT_RIGHTLEFT */
5900 
5901     redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
5902 
5903     while (col < endcol)
5904     {
5905 #ifdef FEAT_MBYTE
5906 	if (has_mbyte && (col + 1 < endcol))
5907 	    char_cells = (*mb_off2cells)(off_from, max_off_from);
5908 	else
5909 	    char_cells = 1;
5910 #endif
5911 
5912 	redraw_this = redraw_next;
5913 	redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
5914 			      off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
5915 
5916 #ifdef FEAT_GUI
5917 	/* If the next character was bold, then redraw the current character to
5918 	 * remove any pixels that might have spilt over into us.  This only
5919 	 * happens in the GUI.
5920 	 */
5921 	if (redraw_next && gui.in_use)
5922 	{
5923 	    hl = ScreenAttrs[off_to + CHAR_CELLS];
5924 	    if (hl > HL_ALL)
5925 		hl = syn_attr2attr(hl);
5926 	    if (hl & HL_BOLD)
5927 		redraw_this = TRUE;
5928 	}
5929 #endif
5930 
5931 	if (redraw_this)
5932 	{
5933 	    /*
5934 	     * Special handling when 'xs' termcap flag set (hpterm):
5935 	     * Attributes for characters are stored at the position where the
5936 	     * cursor is when writing the highlighting code.  The
5937 	     * start-highlighting code must be written with the cursor on the
5938 	     * first highlighted character.  The stop-highlighting code must
5939 	     * be written with the cursor just after the last highlighted
5940 	     * character.
5941 	     * Overwriting a character doesn't remove it's highlighting.  Need
5942 	     * to clear the rest of the line, and force redrawing it
5943 	     * completely.
5944 	     */
5945 	    if (       p_wiv
5946 		    && !force
5947 #ifdef FEAT_GUI
5948 		    && !gui.in_use
5949 #endif
5950 		    && ScreenAttrs[off_to] != 0
5951 		    && ScreenAttrs[off_from] != ScreenAttrs[off_to])
5952 	    {
5953 		/*
5954 		 * Need to remove highlighting attributes here.
5955 		 */
5956 		windgoto(row, col + coloff);
5957 		out_str(T_CE);		/* clear rest of this screen line */
5958 		screen_start();		/* don't know where cursor is now */
5959 		force = TRUE;		/* force redraw of rest of the line */
5960 		redraw_next = TRUE;	/* or else next char would miss out */
5961 
5962 		/*
5963 		 * If the previous character was highlighted, need to stop
5964 		 * highlighting at this character.
5965 		 */
5966 		if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
5967 		{
5968 		    screen_attr = ScreenAttrs[off_to - 1];
5969 		    term_windgoto(row, col + coloff);
5970 		    screen_stop_highlight();
5971 		}
5972 		else
5973 		    screen_attr = 0;	    /* highlighting has stopped */
5974 	    }
5975 #ifdef FEAT_MBYTE
5976 	    if (enc_dbcs != 0)
5977 	    {
5978 		/* Check if overwriting a double-byte with a single-byte or
5979 		 * the other way around requires another character to be
5980 		 * redrawn.  For UTF-8 this isn't needed, because comparing
5981 		 * ScreenLinesUC[] is sufficient. */
5982 		if (char_cells == 1
5983 			&& col + 1 < endcol
5984 			&& (*mb_off2cells)(off_to, max_off_to) > 1)
5985 		{
5986 		    /* Writing a single-cell character over a double-cell
5987 		     * character: need to redraw the next cell. */
5988 		    ScreenLines[off_to + 1] = 0;
5989 		    redraw_next = TRUE;
5990 		}
5991 		else if (char_cells == 2
5992 			&& col + 2 < endcol
5993 			&& (*mb_off2cells)(off_to, max_off_to) == 1
5994 			&& (*mb_off2cells)(off_to + 1, max_off_to) > 1)
5995 		{
5996 		    /* Writing the second half of a double-cell character over
5997 		     * a double-cell character: need to redraw the second
5998 		     * cell. */
5999 		    ScreenLines[off_to + 2] = 0;
6000 		    redraw_next = TRUE;
6001 		}
6002 
6003 		if (enc_dbcs == DBCS_JPNU)
6004 		    ScreenLines2[off_to] = ScreenLines2[off_from];
6005 	    }
6006 	    /* When writing a single-width character over a double-width
6007 	     * character and at the end of the redrawn text, need to clear out
6008 	     * the right halve of the old character.
6009 	     * Also required when writing the right halve of a double-width
6010 	     * char over the left halve of an existing one. */
6011 	    if (has_mbyte && col + char_cells == endcol
6012 		    && ((char_cells == 1
6013 			    && (*mb_off2cells)(off_to, max_off_to) > 1)
6014 			|| (char_cells == 2
6015 			    && (*mb_off2cells)(off_to, max_off_to) == 1
6016 			    && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
6017 		clear_next = TRUE;
6018 #endif
6019 
6020 	    ScreenLines[off_to] = ScreenLines[off_from];
6021 #ifdef FEAT_MBYTE
6022 	    if (enc_utf8)
6023 	    {
6024 		ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6025 		if (ScreenLinesUC[off_from] != 0)
6026 		{
6027 		    int	    i;
6028 
6029 		    for (i = 0; i < Screen_mco; ++i)
6030 			ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
6031 		}
6032 	    }
6033 	    if (char_cells == 2)
6034 		ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
6035 #endif
6036 
6037 #if defined(FEAT_GUI) || defined(UNIX)
6038 	    /* The bold trick makes a single column of pixels appear in the
6039 	     * next character.  When a bold character is removed, the next
6040 	     * character should be redrawn too.  This happens for our own GUI
6041 	     * and for some xterms. */
6042 	    if (
6043 # ifdef FEAT_GUI
6044 		    gui.in_use
6045 # endif
6046 # if defined(FEAT_GUI) && defined(UNIX)
6047 		    ||
6048 # endif
6049 # ifdef UNIX
6050 		    term_is_xterm
6051 # endif
6052 		    )
6053 	    {
6054 		hl = ScreenAttrs[off_to];
6055 		if (hl > HL_ALL)
6056 		    hl = syn_attr2attr(hl);
6057 		if (hl & HL_BOLD)
6058 		    redraw_next = TRUE;
6059 	    }
6060 #endif
6061 	    ScreenAttrs[off_to] = ScreenAttrs[off_from];
6062 #ifdef FEAT_MBYTE
6063 	    /* For simplicity set the attributes of second half of a
6064 	     * double-wide character equal to the first half. */
6065 	    if (char_cells == 2)
6066 		ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
6067 
6068 	    if (enc_dbcs != 0 && char_cells == 2)
6069 		screen_char_2(off_to, row, col + coloff);
6070 	    else
6071 #endif
6072 		screen_char(off_to, row, col + coloff);
6073 	}
6074 	else if (  p_wiv
6075 #ifdef FEAT_GUI
6076 		&& !gui.in_use
6077 #endif
6078 		&& col + coloff > 0)
6079 	{
6080 	    if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6081 	    {
6082 		/*
6083 		 * Don't output stop-highlight when moving the cursor, it will
6084 		 * stop the highlighting when it should continue.
6085 		 */
6086 		screen_attr = 0;
6087 	    }
6088 	    else if (screen_attr != 0)
6089 		screen_stop_highlight();
6090 	}
6091 
6092 	off_to += CHAR_CELLS;
6093 	off_from += CHAR_CELLS;
6094 	col += CHAR_CELLS;
6095     }
6096 
6097 #ifdef FEAT_MBYTE
6098     if (clear_next)
6099     {
6100 	/* Clear the second half of a double-wide character of which the left
6101 	 * half was overwritten with a single-wide character. */
6102 	ScreenLines[off_to] = ' ';
6103 	if (enc_utf8)
6104 	    ScreenLinesUC[off_to] = 0;
6105 	screen_char(off_to, row, col + coloff);
6106     }
6107 #endif
6108 
6109     if (clear_width > 0
6110 #ifdef FEAT_RIGHTLEFT
6111 		    && !rlflag
6112 #endif
6113 				   )
6114     {
6115 #ifdef FEAT_GUI
6116 	int startCol = col;
6117 #endif
6118 
6119 	/* blank out the rest of the line */
6120 	while (col < clear_width && ScreenLines[off_to] == ' '
6121 						  && ScreenAttrs[off_to] == 0
6122 #ifdef FEAT_MBYTE
6123 				  && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6124 #endif
6125 						  )
6126 	{
6127 	    ++off_to;
6128 	    ++col;
6129 	}
6130 	if (col < clear_width)
6131 	{
6132 #ifdef FEAT_GUI
6133 	    /*
6134 	     * In the GUI, clearing the rest of the line may leave pixels
6135 	     * behind if the first character cleared was bold.  Some bold
6136 	     * fonts spill over the left.  In this case we redraw the previous
6137 	     * character too.  If we didn't skip any blanks above, then we
6138 	     * only redraw if the character wasn't already redrawn anyway.
6139 	     */
6140 	    if (gui.in_use && (col > startCol || !redraw_this))
6141 	    {
6142 		hl = ScreenAttrs[off_to];
6143 		if (hl > HL_ALL || (hl & HL_BOLD))
6144 		{
6145 		    int prev_cells = 1;
6146 # ifdef FEAT_MBYTE
6147 		    if (enc_utf8)
6148 			/* for utf-8, ScreenLines[char_offset + 1] == 0 means
6149 			 * that its width is 2. */
6150 			prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6151 		    else if (enc_dbcs != 0)
6152 		    {
6153 			/* find previous character by counting from first
6154 			 * column and get its width. */
6155 			unsigned off = LineOffset[row];
6156 			unsigned max_off = LineOffset[row] + screen_Columns;
6157 
6158 			while (off < off_to)
6159 			{
6160 			    prev_cells = (*mb_off2cells)(off, max_off);
6161 			    off += prev_cells;
6162 			}
6163 		    }
6164 
6165 		    if (enc_dbcs != 0 && prev_cells > 1)
6166 			screen_char_2(off_to - prev_cells, row,
6167 						   col + coloff - prev_cells);
6168 		    else
6169 # endif
6170 			screen_char(off_to - prev_cells, row,
6171 						   col + coloff - prev_cells);
6172 		}
6173 	    }
6174 #endif
6175 	    screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6176 								 ' ', ' ', 0);
6177 #ifdef FEAT_WINDOWS
6178 	    off_to += clear_width - col;
6179 	    col = clear_width;
6180 #endif
6181 	}
6182     }
6183 
6184     if (clear_width > 0)
6185     {
6186 #ifdef FEAT_WINDOWS
6187 	/* For a window that's left of another, draw the separator char. */
6188 	if (col + coloff < Columns)
6189 	{
6190 	    int c;
6191 
6192 	    c = fillchar_vsep(&hl);
6193 	    if (ScreenLines[off_to] != (schar_T)c
6194 # ifdef FEAT_MBYTE
6195 		    || (enc_utf8 && (int)ScreenLinesUC[off_to]
6196 						       != (c >= 0x80 ? c : 0))
6197 # endif
6198 		    || ScreenAttrs[off_to] != hl)
6199 	    {
6200 		ScreenLines[off_to] = c;
6201 		ScreenAttrs[off_to] = hl;
6202 # ifdef FEAT_MBYTE
6203 		if (enc_utf8)
6204 		{
6205 		    if (c >= 0x80)
6206 		    {
6207 			ScreenLinesUC[off_to] = c;
6208 			ScreenLinesC[0][off_to] = 0;
6209 		    }
6210 		    else
6211 			ScreenLinesUC[off_to] = 0;
6212 		}
6213 # endif
6214 		screen_char(off_to, row, col + coloff);
6215 	    }
6216 	}
6217 	else
6218 #endif
6219 	    LineWraps[row] = FALSE;
6220     }
6221 }
6222 
6223 #if defined(FEAT_RIGHTLEFT) || defined(PROTO)
6224 /*
6225  * Mirror text "str" for right-left displaying.
6226  * Only works for single-byte characters (e.g., numbers).
6227  */
6228     void
6229 rl_mirror(char_u *str)
6230 {
6231     char_u	*p1, *p2;
6232     int		t;
6233 
6234     for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6235     {
6236 	t = *p1;
6237 	*p1 = *p2;
6238 	*p2 = t;
6239     }
6240 }
6241 #endif
6242 
6243 #if defined(FEAT_WINDOWS) || defined(PROTO)
6244 /*
6245  * mark all status lines for redraw; used after first :cd
6246  */
6247     void
6248 status_redraw_all(void)
6249 {
6250     win_T	*wp;
6251 
6252     for (wp = firstwin; wp; wp = wp->w_next)
6253 	if (wp->w_status_height)
6254 	{
6255 	    wp->w_redr_status = TRUE;
6256 	    redraw_later(VALID);
6257 	}
6258 }
6259 
6260 /*
6261  * mark all status lines of the current buffer for redraw
6262  */
6263     void
6264 status_redraw_curbuf(void)
6265 {
6266     win_T	*wp;
6267 
6268     for (wp = firstwin; wp; wp = wp->w_next)
6269 	if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6270 	{
6271 	    wp->w_redr_status = TRUE;
6272 	    redraw_later(VALID);
6273 	}
6274 }
6275 
6276 /*
6277  * Redraw all status lines that need to be redrawn.
6278  */
6279     void
6280 redraw_statuslines(void)
6281 {
6282     win_T	*wp;
6283 
6284     for (wp = firstwin; wp; wp = wp->w_next)
6285 	if (wp->w_redr_status)
6286 	    win_redr_status(wp);
6287     if (redraw_tabline)
6288 	draw_tabline();
6289 }
6290 #endif
6291 
6292 #if (defined(FEAT_WILDMENU) && defined(FEAT_WINDOWS)) || defined(PROTO)
6293 /*
6294  * Redraw all status lines at the bottom of frame "frp".
6295  */
6296     void
6297 win_redraw_last_status(frame_T *frp)
6298 {
6299     if (frp->fr_layout == FR_LEAF)
6300 	frp->fr_win->w_redr_status = TRUE;
6301     else if (frp->fr_layout == FR_ROW)
6302     {
6303 	for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6304 	    win_redraw_last_status(frp);
6305     }
6306     else /* frp->fr_layout == FR_COL */
6307     {
6308 	frp = frp->fr_child;
6309 	while (frp->fr_next != NULL)
6310 	    frp = frp->fr_next;
6311 	win_redraw_last_status(frp);
6312     }
6313 }
6314 #endif
6315 
6316 #ifdef FEAT_WINDOWS
6317 /*
6318  * Draw the verticap separator right of window "wp" starting with line "row".
6319  */
6320     static void
6321 draw_vsep_win(win_T *wp, int row)
6322 {
6323     int		hl;
6324     int		c;
6325 
6326     if (wp->w_vsep_width)
6327     {
6328 	/* draw the vertical separator right of this window */
6329 	c = fillchar_vsep(&hl);
6330 	screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6331 		W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6332 		c, ' ', hl);
6333     }
6334 }
6335 #endif
6336 
6337 #ifdef FEAT_WILDMENU
6338 static int status_match_len(expand_T *xp, char_u *s);
6339 static int skip_status_match_char(expand_T *xp, char_u *s);
6340 
6341 /*
6342  * Get the length of an item as it will be shown in the status line.
6343  */
6344     static int
6345 status_match_len(expand_T *xp, char_u *s)
6346 {
6347     int	len = 0;
6348 
6349 #ifdef FEAT_MENU
6350     int emenu = (xp->xp_context == EXPAND_MENUS
6351 	    || xp->xp_context == EXPAND_MENUNAMES);
6352 
6353     /* Check for menu separators - replace with '|'. */
6354     if (emenu && menu_is_separator(s))
6355 	return 1;
6356 #endif
6357 
6358     while (*s != NUL)
6359     {
6360 	s += skip_status_match_char(xp, s);
6361 	len += ptr2cells(s);
6362 	mb_ptr_adv(s);
6363     }
6364 
6365     return len;
6366 }
6367 
6368 /*
6369  * Return the number of characters that should be skipped in a status match.
6370  * These are backslashes used for escaping.  Do show backslashes in help tags.
6371  */
6372     static int
6373 skip_status_match_char(expand_T *xp, char_u *s)
6374 {
6375     if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
6376 #ifdef FEAT_MENU
6377 	    || ((xp->xp_context == EXPAND_MENUS
6378 		    || xp->xp_context == EXPAND_MENUNAMES)
6379 			  && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6380 #endif
6381 	   )
6382     {
6383 #ifndef BACKSLASH_IN_FILENAME
6384 	if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6385 	    return 2;
6386 #endif
6387 	return 1;
6388     }
6389     return 0;
6390 }
6391 
6392 /*
6393  * Show wildchar matches in the status line.
6394  * Show at least the "match" item.
6395  * We start at item 'first_match' in the list and show all matches that fit.
6396  *
6397  * If inversion is possible we use it. Else '=' characters are used.
6398  */
6399     void
6400 win_redr_status_matches(
6401     expand_T	*xp,
6402     int		num_matches,
6403     char_u	**matches,	/* list of matches */
6404     int		match,
6405     int		showtail)
6406 {
6407 #define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6408     int		row;
6409     char_u	*buf;
6410     int		len;
6411     int		clen;		/* length in screen cells */
6412     int		fillchar;
6413     int		attr;
6414     int		i;
6415     int		highlight = TRUE;
6416     char_u	*selstart = NULL;
6417     int		selstart_col = 0;
6418     char_u	*selend = NULL;
6419     static int	first_match = 0;
6420     int		add_left = FALSE;
6421     char_u	*s;
6422 #ifdef FEAT_MENU
6423     int		emenu;
6424 #endif
6425 #if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6426     int		l;
6427 #endif
6428 
6429     if (matches == NULL)	/* interrupted completion? */
6430 	return;
6431 
6432 #ifdef FEAT_MBYTE
6433     if (has_mbyte)
6434 	buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6435     else
6436 #endif
6437 	buf = alloc((unsigned)Columns + 1);
6438     if (buf == NULL)
6439 	return;
6440 
6441     if (match == -1)	/* don't show match but original text */
6442     {
6443 	match = 0;
6444 	highlight = FALSE;
6445     }
6446     /* count 1 for the ending ">" */
6447     clen = status_match_len(xp, L_MATCH(match)) + 3;
6448     if (match == 0)
6449 	first_match = 0;
6450     else if (match < first_match)
6451     {
6452 	/* jumping left, as far as we can go */
6453 	first_match = match;
6454 	add_left = TRUE;
6455     }
6456     else
6457     {
6458 	/* check if match fits on the screen */
6459 	for (i = first_match; i < match; ++i)
6460 	    clen += status_match_len(xp, L_MATCH(i)) + 2;
6461 	if (first_match > 0)
6462 	    clen += 2;
6463 	/* jumping right, put match at the left */
6464 	if ((long)clen > Columns)
6465 	{
6466 	    first_match = match;
6467 	    /* if showing the last match, we can add some on the left */
6468 	    clen = 2;
6469 	    for (i = match; i < num_matches; ++i)
6470 	    {
6471 		clen += status_match_len(xp, L_MATCH(i)) + 2;
6472 		if ((long)clen >= Columns)
6473 		    break;
6474 	    }
6475 	    if (i == num_matches)
6476 		add_left = TRUE;
6477 	}
6478     }
6479     if (add_left)
6480 	while (first_match > 0)
6481 	{
6482 	    clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6483 	    if ((long)clen >= Columns)
6484 		break;
6485 	    --first_match;
6486 	}
6487 
6488     fillchar = fillchar_status(&attr, TRUE);
6489 
6490     if (first_match == 0)
6491     {
6492 	*buf = NUL;
6493 	len = 0;
6494     }
6495     else
6496     {
6497 	STRCPY(buf, "< ");
6498 	len = 2;
6499     }
6500     clen = len;
6501 
6502     i = first_match;
6503     while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6504     {
6505 	if (i == match)
6506 	{
6507 	    selstart = buf + len;
6508 	    selstart_col = clen;
6509 	}
6510 
6511 	s = L_MATCH(i);
6512 	/* Check for menu separators - replace with '|' */
6513 #ifdef FEAT_MENU
6514 	emenu = (xp->xp_context == EXPAND_MENUS
6515 		|| xp->xp_context == EXPAND_MENUNAMES);
6516 	if (emenu && menu_is_separator(s))
6517 	{
6518 	    STRCPY(buf + len, transchar('|'));
6519 	    l = (int)STRLEN(buf + len);
6520 	    len += l;
6521 	    clen += l;
6522 	}
6523 	else
6524 #endif
6525 	    for ( ; *s != NUL; ++s)
6526 	{
6527 	    s += skip_status_match_char(xp, s);
6528 	    clen += ptr2cells(s);
6529 #ifdef FEAT_MBYTE
6530 	    if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
6531 	    {
6532 		STRNCPY(buf + len, s, l);
6533 		s += l - 1;
6534 		len += l;
6535 	    }
6536 	    else
6537 #endif
6538 	    {
6539 		STRCPY(buf + len, transchar_byte(*s));
6540 		len += (int)STRLEN(buf + len);
6541 	    }
6542 	}
6543 	if (i == match)
6544 	    selend = buf + len;
6545 
6546 	*(buf + len++) = ' ';
6547 	*(buf + len++) = ' ';
6548 	clen += 2;
6549 	if (++i == num_matches)
6550 		break;
6551     }
6552 
6553     if (i != num_matches)
6554     {
6555 	*(buf + len++) = '>';
6556 	++clen;
6557     }
6558 
6559     buf[len] = NUL;
6560 
6561     row = cmdline_row - 1;
6562     if (row >= 0)
6563     {
6564 	if (wild_menu_showing == 0)
6565 	{
6566 	    if (msg_scrolled > 0)
6567 	    {
6568 		/* Put the wildmenu just above the command line.  If there is
6569 		 * no room, scroll the screen one line up. */
6570 		if (cmdline_row == Rows - 1)
6571 		{
6572 		    screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
6573 		    ++msg_scrolled;
6574 		}
6575 		else
6576 		{
6577 		    ++cmdline_row;
6578 		    ++row;
6579 		}
6580 		wild_menu_showing = WM_SCROLLED;
6581 	    }
6582 	    else
6583 	    {
6584 		/* Create status line if needed by setting 'laststatus' to 2.
6585 		 * Set 'winminheight' to zero to avoid that the window is
6586 		 * resized. */
6587 		if (lastwin->w_status_height == 0)
6588 		{
6589 		    save_p_ls = p_ls;
6590 		    save_p_wmh = p_wmh;
6591 		    p_ls = 2;
6592 		    p_wmh = 0;
6593 		    last_status(FALSE);
6594 		}
6595 		wild_menu_showing = WM_SHOWN;
6596 	    }
6597 	}
6598 
6599 	screen_puts(buf, row, 0, attr);
6600 	if (selstart != NULL && highlight)
6601 	{
6602 	    *selend = NUL;
6603 	    screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
6604 	}
6605 
6606 	screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6607     }
6608 
6609 #ifdef FEAT_WINDOWS
6610     win_redraw_last_status(topframe);
6611 #else
6612     lastwin->w_redr_status = TRUE;
6613 #endif
6614     vim_free(buf);
6615 }
6616 #endif
6617 
6618 #if defined(FEAT_WINDOWS) || defined(PROTO)
6619 /*
6620  * Redraw the status line of window wp.
6621  *
6622  * If inversion is possible we use it. Else '=' characters are used.
6623  */
6624     void
6625 win_redr_status(win_T *wp)
6626 {
6627     int		row;
6628     char_u	*p;
6629     int		len;
6630     int		fillchar;
6631     int		attr;
6632     int		this_ru_col;
6633     static int  busy = FALSE;
6634 
6635     /* It's possible to get here recursively when 'statusline' (indirectly)
6636      * invokes ":redrawstatus".  Simply ignore the call then. */
6637     if (busy)
6638 	return;
6639     busy = TRUE;
6640 
6641     wp->w_redr_status = FALSE;
6642     if (wp->w_status_height == 0)
6643     {
6644 	/* no status line, can only be last window */
6645 	redraw_cmdline = TRUE;
6646     }
6647     else if (!redrawing()
6648 #ifdef FEAT_INS_EXPAND
6649 	    /* don't update status line when popup menu is visible and may be
6650 	     * drawn over it */
6651 	    || pum_visible()
6652 #endif
6653 	    )
6654     {
6655 	/* Don't redraw right now, do it later. */
6656 	wp->w_redr_status = TRUE;
6657     }
6658 #ifdef FEAT_STL_OPT
6659     else if (*p_stl != NUL || *wp->w_p_stl != NUL)
6660     {
6661 	/* redraw custom status line */
6662 	redraw_custom_statusline(wp);
6663     }
6664 #endif
6665     else
6666     {
6667 	fillchar = fillchar_status(&attr, wp == curwin);
6668 
6669 	get_trans_bufname(wp->w_buffer);
6670 	p = NameBuff;
6671 	len = (int)STRLEN(p);
6672 
6673 	if (wp->w_buffer->b_help
6674 #ifdef FEAT_QUICKFIX
6675 		|| wp->w_p_pvw
6676 #endif
6677 		|| bufIsChanged(wp->w_buffer)
6678 		|| wp->w_buffer->b_p_ro)
6679 	    *(p + len++) = ' ';
6680 	if (wp->w_buffer->b_help)
6681 	{
6682 	    STRCPY(p + len, _("[Help]"));
6683 	    len += (int)STRLEN(p + len);
6684 	}
6685 #ifdef FEAT_QUICKFIX
6686 	if (wp->w_p_pvw)
6687 	{
6688 	    STRCPY(p + len, _("[Preview]"));
6689 	    len += (int)STRLEN(p + len);
6690 	}
6691 #endif
6692 	if (bufIsChanged(wp->w_buffer))
6693 	{
6694 	    STRCPY(p + len, "[+]");
6695 	    len += 3;
6696 	}
6697 	if (wp->w_buffer->b_p_ro)
6698 	{
6699 	    STRCPY(p + len, _("[RO]"));
6700 	    len += 4;
6701 	}
6702 
6703 	this_ru_col = ru_col - (Columns - W_WIDTH(wp));
6704 	if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
6705 	    this_ru_col = (W_WIDTH(wp) + 1) / 2;
6706 	if (this_ru_col <= 1)
6707 	{
6708 	    p = (char_u *)"<";		/* No room for file name! */
6709 	    len = 1;
6710 	}
6711 	else
6712 #ifdef FEAT_MBYTE
6713 	    if (has_mbyte)
6714 	    {
6715 		int	clen = 0, i;
6716 
6717 		/* Count total number of display cells. */
6718 		clen = mb_string2cells(p, -1);
6719 
6720 		/* Find first character that will fit.
6721 		 * Going from start to end is much faster for DBCS. */
6722 		for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
6723 					      i += (*mb_ptr2len)(p + i))
6724 		    clen -= (*mb_ptr2cells)(p + i);
6725 		len = clen;
6726 		if (i > 0)
6727 		{
6728 		    p = p + i - 1;
6729 		    *p = '<';
6730 		    ++len;
6731 		}
6732 
6733 	    }
6734 	    else
6735 #endif
6736 	    if (len > this_ru_col - 1)
6737 	    {
6738 		p += len - (this_ru_col - 1);
6739 		*p = '<';
6740 		len = this_ru_col - 1;
6741 	    }
6742 
6743 	row = W_WINROW(wp) + wp->w_height;
6744 	screen_puts(p, row, W_WINCOL(wp), attr);
6745 	screen_fill(row, row + 1, len + W_WINCOL(wp),
6746 			this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
6747 
6748 	if (get_keymap_str(wp, NameBuff, MAXPATHL)
6749 		&& (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6750 	    screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
6751 						   - 1 + W_WINCOL(wp)), attr);
6752 
6753 #ifdef FEAT_CMDL_INFO
6754 	win_redr_ruler(wp, TRUE);
6755 #endif
6756     }
6757 
6758     /*
6759      * May need to draw the character below the vertical separator.
6760      */
6761     if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6762     {
6763 	if (stl_connected(wp))
6764 	    fillchar = fillchar_status(&attr, wp == curwin);
6765 	else
6766 	    fillchar = fillchar_vsep(&attr);
6767 	screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6768 									attr);
6769     }
6770     busy = FALSE;
6771 }
6772 
6773 #ifdef FEAT_STL_OPT
6774 /*
6775  * Redraw the status line according to 'statusline' and take care of any
6776  * errors encountered.
6777  */
6778     static void
6779 redraw_custom_statusline(win_T *wp)
6780 {
6781     static int	    entered = FALSE;
6782     int		    save_called_emsg = called_emsg;
6783 
6784     /* When called recursively return.  This can happen when the statusline
6785      * contains an expression that triggers a redraw. */
6786     if (entered)
6787 	return;
6788     entered = TRUE;
6789 
6790     called_emsg = FALSE;
6791     win_redr_custom(wp, FALSE);
6792     if (called_emsg)
6793     {
6794 	/* When there is an error disable the statusline, otherwise the
6795 	 * display is messed up with errors and a redraw triggers the problem
6796 	 * again and again. */
6797 	set_string_option_direct((char_u *)"statusline", -1,
6798 		(char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
6799 					? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
6800     }
6801     called_emsg |= save_called_emsg;
6802     entered = FALSE;
6803 }
6804 #endif
6805 
6806 /*
6807  * Return TRUE if the status line of window "wp" is connected to the status
6808  * line of the window right of it.  If not, then it's a vertical separator.
6809  * Only call if (wp->w_vsep_width != 0).
6810  */
6811     int
6812 stl_connected(win_T *wp)
6813 {
6814     frame_T	*fr;
6815 
6816     fr = wp->w_frame;
6817     while (fr->fr_parent != NULL)
6818     {
6819 	if (fr->fr_parent->fr_layout == FR_COL)
6820 	{
6821 	    if (fr->fr_next != NULL)
6822 		break;
6823 	}
6824 	else
6825 	{
6826 	    if (fr->fr_next != NULL)
6827 		return TRUE;
6828 	}
6829 	fr = fr->fr_parent;
6830     }
6831     return FALSE;
6832 }
6833 
6834 #endif /* FEAT_WINDOWS */
6835 
6836 #if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
6837 /*
6838  * Get the value to show for the language mappings, active 'keymap'.
6839  */
6840     int
6841 get_keymap_str(
6842     win_T	*wp,
6843     char_u	*buf,	    /* buffer for the result */
6844     int		len)	    /* length of buffer */
6845 {
6846     char_u	*p;
6847 
6848     if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
6849 	return FALSE;
6850 
6851     {
6852 #ifdef FEAT_EVAL
6853 	buf_T	*old_curbuf = curbuf;
6854 	win_T	*old_curwin = curwin;
6855 	char_u	*s;
6856 
6857 	curbuf = wp->w_buffer;
6858 	curwin = wp;
6859 	STRCPY(buf, "b:keymap_name");	/* must be writable */
6860 	++emsg_skip;
6861 	s = p = eval_to_string(buf, NULL, FALSE);
6862 	--emsg_skip;
6863 	curbuf = old_curbuf;
6864 	curwin = old_curwin;
6865 	if (p == NULL || *p == NUL)
6866 #endif
6867 	{
6868 #ifdef FEAT_KEYMAP
6869 	    if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
6870 		p = wp->w_buffer->b_p_keymap;
6871 	    else
6872 #endif
6873 		p = (char_u *)"lang";
6874 	}
6875 	if ((int)(STRLEN(p) + 3) < len)
6876 	    sprintf((char *)buf, "<%s>", p);
6877 	else
6878 	    buf[0] = NUL;
6879 #ifdef FEAT_EVAL
6880 	vim_free(s);
6881 #endif
6882     }
6883     return buf[0] != NUL;
6884 }
6885 #endif
6886 
6887 #if defined(FEAT_STL_OPT) || defined(PROTO)
6888 /*
6889  * Redraw the status line or ruler of window "wp".
6890  * When "wp" is NULL redraw the tab pages line from 'tabline'.
6891  */
6892     static void
6893 win_redr_custom(
6894     win_T	*wp,
6895     int		draw_ruler)	/* TRUE or FALSE */
6896 {
6897     static int	entered = FALSE;
6898     int		attr;
6899     int		curattr;
6900     int		row;
6901     int		col = 0;
6902     int		maxwidth;
6903     int		width;
6904     int		n;
6905     int		len;
6906     int		fillchar;
6907     char_u	buf[MAXPATHL];
6908     char_u	*stl;
6909     char_u	*p;
6910     struct	stl_hlrec hltab[STL_MAX_ITEM];
6911     struct	stl_hlrec tabtab[STL_MAX_ITEM];
6912     int		use_sandbox = FALSE;
6913     win_T	*ewp;
6914     int		p_crb_save;
6915 
6916     /* There is a tiny chance that this gets called recursively: When
6917      * redrawing a status line triggers redrawing the ruler or tabline.
6918      * Avoid trouble by not allowing recursion. */
6919     if (entered)
6920 	return;
6921     entered = TRUE;
6922 
6923     /* setup environment for the task at hand */
6924     if (wp == NULL)
6925     {
6926 	/* Use 'tabline'.  Always at the first line of the screen. */
6927 	stl = p_tal;
6928 	row = 0;
6929 	fillchar = ' ';
6930 	attr = hl_attr(HLF_TPF);
6931 	maxwidth = Columns;
6932 # ifdef FEAT_EVAL
6933 	use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
6934 # endif
6935     }
6936     else
6937     {
6938 	row = W_WINROW(wp) + wp->w_height;
6939 	fillchar = fillchar_status(&attr, wp == curwin);
6940 	maxwidth = W_WIDTH(wp);
6941 
6942 	if (draw_ruler)
6943 	{
6944 	    stl = p_ruf;
6945 	    /* advance past any leading group spec - implicit in ru_col */
6946 	    if (*stl == '%')
6947 	    {
6948 		if (*++stl == '-')
6949 		    stl++;
6950 		if (atoi((char *)stl))
6951 		    while (VIM_ISDIGIT(*stl))
6952 			stl++;
6953 		if (*stl++ != '(')
6954 		    stl = p_ruf;
6955 	    }
6956 #ifdef FEAT_WINDOWS
6957 	    col = ru_col - (Columns - W_WIDTH(wp));
6958 	    if (col < (W_WIDTH(wp) + 1) / 2)
6959 		col = (W_WIDTH(wp) + 1) / 2;
6960 #else
6961 	    col = ru_col;
6962 	    if (col > (Columns + 1) / 2)
6963 		col = (Columns + 1) / 2;
6964 #endif
6965 	    maxwidth = W_WIDTH(wp) - col;
6966 #ifdef FEAT_WINDOWS
6967 	    if (!wp->w_status_height)
6968 #endif
6969 	    {
6970 		row = Rows - 1;
6971 		--maxwidth;	/* writing in last column may cause scrolling */
6972 		fillchar = ' ';
6973 		attr = 0;
6974 	    }
6975 
6976 # ifdef FEAT_EVAL
6977 	    use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
6978 # endif
6979 	}
6980 	else
6981 	{
6982 	    if (*wp->w_p_stl != NUL)
6983 		stl = wp->w_p_stl;
6984 	    else
6985 		stl = p_stl;
6986 # ifdef FEAT_EVAL
6987 	    use_sandbox = was_set_insecurely((char_u *)"statusline",
6988 					 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
6989 # endif
6990 	}
6991 
6992 #ifdef FEAT_WINDOWS
6993 	col += W_WINCOL(wp);
6994 #endif
6995     }
6996 
6997     if (maxwidth <= 0)
6998 	goto theend;
6999 
7000     /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7001      * the cursor away and back. */
7002     ewp = wp == NULL ? curwin : wp;
7003     p_crb_save = ewp->w_p_crb;
7004     ewp->w_p_crb = FALSE;
7005 
7006     /* Make a copy, because the statusline may include a function call that
7007      * might change the option value and free the memory. */
7008     stl = vim_strsave(stl);
7009     width = build_stl_str_hl(ewp, buf, sizeof(buf),
7010 				stl, use_sandbox,
7011 				fillchar, maxwidth, hltab, tabtab);
7012     vim_free(stl);
7013     ewp->w_p_crb = p_crb_save;
7014 
7015     /* Make all characters printable. */
7016     p = transstr(buf);
7017     if (p != NULL)
7018     {
7019 	vim_strncpy(buf, p, sizeof(buf) - 1);
7020 	vim_free(p);
7021     }
7022 
7023     /* fill up with "fillchar" */
7024     len = (int)STRLEN(buf);
7025     while (width < maxwidth && len < (int)sizeof(buf) - 1)
7026     {
7027 #ifdef FEAT_MBYTE
7028 	len += (*mb_char2bytes)(fillchar, buf + len);
7029 #else
7030 	buf[len++] = fillchar;
7031 #endif
7032 	++width;
7033     }
7034     buf[len] = NUL;
7035 
7036     /*
7037      * Draw each snippet with the specified highlighting.
7038      */
7039     curattr = attr;
7040     p = buf;
7041     for (n = 0; hltab[n].start != NULL; n++)
7042     {
7043 	len = (int)(hltab[n].start - p);
7044 	screen_puts_len(p, len, row, col, curattr);
7045 	col += vim_strnsize(p, len);
7046 	p = hltab[n].start;
7047 
7048 	if (hltab[n].userhl == 0)
7049 	    curattr = attr;
7050 	else if (hltab[n].userhl < 0)
7051 	    curattr = syn_id2attr(-hltab[n].userhl);
7052 #ifdef FEAT_WINDOWS
7053 	else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
7054 	    curattr = highlight_stlnc[hltab[n].userhl - 1];
7055 #endif
7056 	else
7057 	    curattr = highlight_user[hltab[n].userhl - 1];
7058     }
7059     screen_puts(p, row, col, curattr);
7060 
7061     if (wp == NULL)
7062     {
7063 	/* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7064 	col = 0;
7065 	len = 0;
7066 	p = buf;
7067 	fillchar = 0;
7068 	for (n = 0; tabtab[n].start != NULL; n++)
7069 	{
7070 	    len += vim_strnsize(p, (int)(tabtab[n].start - p));
7071 	    while (col < len)
7072 		TabPageIdxs[col++] = fillchar;
7073 	    p = tabtab[n].start;
7074 	    fillchar = tabtab[n].userhl;
7075 	}
7076 	while (col < Columns)
7077 	    TabPageIdxs[col++] = fillchar;
7078     }
7079 
7080 theend:
7081     entered = FALSE;
7082 }
7083 
7084 #endif /* FEAT_STL_OPT */
7085 
7086 /*
7087  * Output a single character directly to the screen and update ScreenLines.
7088  */
7089     void
7090 screen_putchar(int c, int row, int col, int attr)
7091 {
7092     char_u	buf[MB_MAXBYTES + 1];
7093 
7094 #ifdef FEAT_MBYTE
7095     if (has_mbyte)
7096 	buf[(*mb_char2bytes)(c, buf)] = NUL;
7097     else
7098 #endif
7099     {
7100 	buf[0] = c;
7101 	buf[1] = NUL;
7102     }
7103     screen_puts(buf, row, col, attr);
7104 }
7105 
7106 /*
7107  * Get a single character directly from ScreenLines into "bytes[]".
7108  * Also return its attribute in *attrp;
7109  */
7110     void
7111 screen_getbytes(int row, int col, char_u *bytes, int *attrp)
7112 {
7113     unsigned off;
7114 
7115     /* safety check */
7116     if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7117     {
7118 	off = LineOffset[row] + col;
7119 	*attrp = ScreenAttrs[off];
7120 	bytes[0] = ScreenLines[off];
7121 	bytes[1] = NUL;
7122 
7123 #ifdef FEAT_MBYTE
7124 	if (enc_utf8 && ScreenLinesUC[off] != 0)
7125 	    bytes[utfc_char2bytes(off, bytes)] = NUL;
7126 	else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7127 	{
7128 	    bytes[0] = ScreenLines[off];
7129 	    bytes[1] = ScreenLines2[off];
7130 	    bytes[2] = NUL;
7131 	}
7132 	else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7133 	{
7134 	    bytes[1] = ScreenLines[off + 1];
7135 	    bytes[2] = NUL;
7136 	}
7137 #endif
7138     }
7139 }
7140 
7141 #ifdef FEAT_MBYTE
7142 static int screen_comp_differs(int, int*);
7143 
7144 /*
7145  * Return TRUE if composing characters for screen posn "off" differs from
7146  * composing characters in "u8cc".
7147  * Only to be used when ScreenLinesUC[off] != 0.
7148  */
7149     static int
7150 screen_comp_differs(int off, int *u8cc)
7151 {
7152     int	    i;
7153 
7154     for (i = 0; i < Screen_mco; ++i)
7155     {
7156 	if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7157 	    return TRUE;
7158 	if (u8cc[i] == 0)
7159 	    break;
7160     }
7161     return FALSE;
7162 }
7163 #endif
7164 
7165 /*
7166  * Put string '*text' on the screen at position 'row' and 'col', with
7167  * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7168  * Note: only outputs within one row, message is truncated at screen boundary!
7169  * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7170  */
7171     void
7172 screen_puts(
7173     char_u	*text,
7174     int		row,
7175     int		col,
7176     int		attr)
7177 {
7178     screen_puts_len(text, -1, row, col, attr);
7179 }
7180 
7181 /*
7182  * Like screen_puts(), but output "text[len]".  When "len" is -1 output up to
7183  * a NUL.
7184  */
7185     void
7186 screen_puts_len(
7187     char_u	*text,
7188     int		textlen,
7189     int		row,
7190     int		col,
7191     int		attr)
7192 {
7193     unsigned	off;
7194     char_u	*ptr = text;
7195     int		len = textlen;
7196     int		c;
7197 #ifdef FEAT_MBYTE
7198     unsigned	max_off;
7199     int		mbyte_blen = 1;
7200     int		mbyte_cells = 1;
7201     int		u8c = 0;
7202     int		u8cc[MAX_MCO];
7203     int		clear_next_cell = FALSE;
7204 # ifdef FEAT_ARABIC
7205     int		prev_c = 0;		/* previous Arabic character */
7206     int		pc, nc, nc1;
7207     int		pcc[MAX_MCO];
7208 # endif
7209 #endif
7210 #if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7211     int		force_redraw_this;
7212     int		force_redraw_next = FALSE;
7213 #endif
7214     int		need_redraw;
7215 
7216     if (ScreenLines == NULL || row >= screen_Rows)	/* safety check */
7217 	return;
7218     off = LineOffset[row] + col;
7219 
7220 #ifdef FEAT_MBYTE
7221     /* When drawing over the right halve of a double-wide char clear out the
7222      * left halve.  Only needed in a terminal. */
7223     if (has_mbyte && col > 0 && col < screen_Columns
7224 # ifdef FEAT_GUI
7225 	    && !gui.in_use
7226 # endif
7227 	    && mb_fix_col(col, row) != col)
7228     {
7229 	ScreenLines[off - 1] = ' ';
7230 	ScreenAttrs[off - 1] = 0;
7231 	if (enc_utf8)
7232 	{
7233 	    ScreenLinesUC[off - 1] = 0;
7234 	    ScreenLinesC[0][off - 1] = 0;
7235 	}
7236 	/* redraw the previous cell, make it empty */
7237 	screen_char(off - 1, row, col - 1);
7238 	/* force the cell at "col" to be redrawn */
7239 	force_redraw_next = TRUE;
7240     }
7241 #endif
7242 
7243 #ifdef FEAT_MBYTE
7244     max_off = LineOffset[row] + screen_Columns;
7245 #endif
7246     while (col < screen_Columns
7247 	    && (len < 0 || (int)(ptr - text) < len)
7248 	    && *ptr != NUL)
7249     {
7250 	c = *ptr;
7251 #ifdef FEAT_MBYTE
7252 	/* check if this is the first byte of a multibyte */
7253 	if (has_mbyte)
7254 	{
7255 	    if (enc_utf8 && len > 0)
7256 		mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
7257 	    else
7258 		mbyte_blen = (*mb_ptr2len)(ptr);
7259 	    if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7260 		mbyte_cells = 1;
7261 	    else if (enc_dbcs != 0)
7262 		mbyte_cells = mbyte_blen;
7263 	    else	/* enc_utf8 */
7264 	    {
7265 		if (len >= 0)
7266 		    u8c = utfc_ptr2char_len(ptr, u8cc,
7267 						   (int)((text + len) - ptr));
7268 		else
7269 		    u8c = utfc_ptr2char(ptr, u8cc);
7270 		mbyte_cells = utf_char2cells(u8c);
7271 # ifdef UNICODE16
7272 		/* Non-BMP character: display as ? or fullwidth ?. */
7273 		if (u8c >= 0x10000)
7274 		{
7275 		    u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7276 		    if (attr == 0)
7277 			attr = hl_attr(HLF_8);
7278 		}
7279 # endif
7280 # ifdef FEAT_ARABIC
7281 		if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7282 		{
7283 		    /* Do Arabic shaping. */
7284 		    if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7285 		    {
7286 			/* Past end of string to be displayed. */
7287 			nc = NUL;
7288 			nc1 = NUL;
7289 		    }
7290 		    else
7291 		    {
7292 			nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7293 				      (int)((text + len) - ptr - mbyte_blen));
7294 			nc1 = pcc[0];
7295 		    }
7296 		    pc = prev_c;
7297 		    prev_c = u8c;
7298 		    u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
7299 		}
7300 		else
7301 		    prev_c = u8c;
7302 # endif
7303 		if (col + mbyte_cells > screen_Columns)
7304 		{
7305 		    /* Only 1 cell left, but character requires 2 cells:
7306 		     * display a '>' in the last column to avoid wrapping. */
7307 		    c = '>';
7308 		    mbyte_cells = 1;
7309 		}
7310 	    }
7311 	}
7312 #endif
7313 
7314 #if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7315 	force_redraw_this = force_redraw_next;
7316 	force_redraw_next = FALSE;
7317 #endif
7318 
7319 	need_redraw = ScreenLines[off] != c
7320 #ifdef FEAT_MBYTE
7321 		|| (mbyte_cells == 2
7322 		    && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7323 		|| (enc_dbcs == DBCS_JPNU
7324 		    && c == 0x8e
7325 		    && ScreenLines2[off] != ptr[1])
7326 		|| (enc_utf8
7327 		    && (ScreenLinesUC[off] !=
7328 				(u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7329 			|| (ScreenLinesUC[off] != 0
7330 					  && screen_comp_differs(off, u8cc))))
7331 #endif
7332 		|| ScreenAttrs[off] != attr
7333 		|| exmode_active;
7334 
7335 	if (need_redraw
7336 #if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7337 		|| force_redraw_this
7338 #endif
7339 		)
7340 	{
7341 #if defined(FEAT_GUI) || defined(UNIX)
7342 	    /* The bold trick makes a single row of pixels appear in the next
7343 	     * character.  When a bold character is removed, the next
7344 	     * character should be redrawn too.  This happens for our own GUI
7345 	     * and for some xterms. */
7346 	    if (need_redraw && ScreenLines[off] != ' ' && (
7347 # ifdef FEAT_GUI
7348 		    gui.in_use
7349 # endif
7350 # if defined(FEAT_GUI) && defined(UNIX)
7351 		    ||
7352 # endif
7353 # ifdef UNIX
7354 		    term_is_xterm
7355 # endif
7356 		    ))
7357 	    {
7358 		int	n = ScreenAttrs[off];
7359 
7360 		if (n > HL_ALL)
7361 		    n = syn_attr2attr(n);
7362 		if (n & HL_BOLD)
7363 		    force_redraw_next = TRUE;
7364 	    }
7365 #endif
7366 #ifdef FEAT_MBYTE
7367 	    /* When at the end of the text and overwriting a two-cell
7368 	     * character with a one-cell character, need to clear the next
7369 	     * cell.  Also when overwriting the left halve of a two-cell char
7370 	     * with the right halve of a two-cell char.  Do this only once
7371 	     * (mb_off2cells() may return 2 on the right halve). */
7372 	    if (clear_next_cell)
7373 		clear_next_cell = FALSE;
7374 	    else if (has_mbyte
7375 		    && (len < 0 ? ptr[mbyte_blen] == NUL
7376 					     : ptr + mbyte_blen >= text + len)
7377 		    && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
7378 			|| (mbyte_cells == 2
7379 			    && (*mb_off2cells)(off, max_off) == 1
7380 			    && (*mb_off2cells)(off + 1, max_off) > 1)))
7381 		clear_next_cell = TRUE;
7382 
7383 	    /* Make sure we never leave a second byte of a double-byte behind,
7384 	     * it confuses mb_off2cells(). */
7385 	    if (enc_dbcs
7386 		    && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
7387 			|| (mbyte_cells == 2
7388 			    && (*mb_off2cells)(off, max_off) == 1
7389 			    && (*mb_off2cells)(off + 1, max_off) > 1)))
7390 		ScreenLines[off + mbyte_blen] = 0;
7391 #endif
7392 	    ScreenLines[off] = c;
7393 	    ScreenAttrs[off] = attr;
7394 #ifdef FEAT_MBYTE
7395 	    if (enc_utf8)
7396 	    {
7397 		if (c < 0x80 && u8cc[0] == 0)
7398 		    ScreenLinesUC[off] = 0;
7399 		else
7400 		{
7401 		    int	    i;
7402 
7403 		    ScreenLinesUC[off] = u8c;
7404 		    for (i = 0; i < Screen_mco; ++i)
7405 		    {
7406 			ScreenLinesC[i][off] = u8cc[i];
7407 			if (u8cc[i] == 0)
7408 			    break;
7409 		    }
7410 		}
7411 		if (mbyte_cells == 2)
7412 		{
7413 		    ScreenLines[off + 1] = 0;
7414 		    ScreenAttrs[off + 1] = attr;
7415 		}
7416 		screen_char(off, row, col);
7417 	    }
7418 	    else if (mbyte_cells == 2)
7419 	    {
7420 		ScreenLines[off + 1] = ptr[1];
7421 		ScreenAttrs[off + 1] = attr;
7422 		screen_char_2(off, row, col);
7423 	    }
7424 	    else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7425 	    {
7426 		ScreenLines2[off] = ptr[1];
7427 		screen_char(off, row, col);
7428 	    }
7429 	    else
7430 #endif
7431 		screen_char(off, row, col);
7432 	}
7433 #ifdef FEAT_MBYTE
7434 	if (has_mbyte)
7435 	{
7436 	    off += mbyte_cells;
7437 	    col += mbyte_cells;
7438 	    ptr += mbyte_blen;
7439 	    if (clear_next_cell)
7440 	    {
7441 		/* This only happens at the end, display one space next. */
7442 		ptr = (char_u *)" ";
7443 		len = -1;
7444 	    }
7445 	}
7446 	else
7447 #endif
7448 	{
7449 	    ++off;
7450 	    ++col;
7451 	    ++ptr;
7452 	}
7453     }
7454 
7455 #if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7456     /* If we detected the next character needs to be redrawn, but the text
7457      * doesn't extend up to there, update the character here. */
7458     if (force_redraw_next && col < screen_Columns)
7459     {
7460 # ifdef FEAT_MBYTE
7461 	if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7462 	    screen_char_2(off, row, col);
7463 	else
7464 # endif
7465 	    screen_char(off, row, col);
7466     }
7467 #endif
7468 }
7469 
7470 #ifdef FEAT_SEARCH_EXTRA
7471 /*
7472  * Prepare for 'hlsearch' highlighting.
7473  */
7474     static void
7475 start_search_hl(void)
7476 {
7477     if (p_hls && !no_hlsearch)
7478     {
7479 	last_pat_prog(&search_hl.rm);
7480 	search_hl.attr = hl_attr(HLF_L);
7481 # ifdef FEAT_RELTIME
7482 	/* Set the time limit to 'redrawtime'. */
7483 	profile_setlimit(p_rdt, &search_hl.tm);
7484 # endif
7485     }
7486 }
7487 
7488 /*
7489  * Clean up for 'hlsearch' highlighting.
7490  */
7491     static void
7492 end_search_hl(void)
7493 {
7494     if (search_hl.rm.regprog != NULL)
7495     {
7496 	vim_regfree(search_hl.rm.regprog);
7497 	search_hl.rm.regprog = NULL;
7498     }
7499 }
7500 
7501 /*
7502  * Init for calling prepare_search_hl().
7503  */
7504     static void
7505 init_search_hl(win_T *wp)
7506 {
7507     matchitem_T *cur;
7508 
7509     /* Setup for match and 'hlsearch' highlighting.  Disable any previous
7510      * match */
7511     cur = wp->w_match_head;
7512     while (cur != NULL)
7513     {
7514 	cur->hl.rm = cur->match;
7515 	if (cur->hlg_id == 0)
7516 	    cur->hl.attr = 0;
7517 	else
7518 	    cur->hl.attr = syn_id2attr(cur->hlg_id);
7519 	cur->hl.buf = wp->w_buffer;
7520 	cur->hl.lnum = 0;
7521 	cur->hl.first_lnum = 0;
7522 # ifdef FEAT_RELTIME
7523 	/* Set the time limit to 'redrawtime'. */
7524 	profile_setlimit(p_rdt, &(cur->hl.tm));
7525 # endif
7526 	cur = cur->next;
7527     }
7528     search_hl.buf = wp->w_buffer;
7529     search_hl.lnum = 0;
7530     search_hl.first_lnum = 0;
7531     /* time limit is set at the toplevel, for all windows */
7532 }
7533 
7534 /*
7535  * Advance to the match in window "wp" line "lnum" or past it.
7536  */
7537     static void
7538 prepare_search_hl(win_T *wp, linenr_T lnum)
7539 {
7540     matchitem_T *cur;		/* points to the match list */
7541     match_T	*shl;		/* points to search_hl or a match */
7542     int		shl_flag;	/* flag to indicate whether search_hl
7543 				   has been processed or not */
7544     int		pos_inprogress;	/* marks that position match search is
7545 				   in progress */
7546     int		n;
7547 
7548     /*
7549      * When using a multi-line pattern, start searching at the top
7550      * of the window or just after a closed fold.
7551      * Do this both for search_hl and the match list.
7552      */
7553     cur = wp->w_match_head;
7554     shl_flag = FALSE;
7555     while (cur != NULL || shl_flag == FALSE)
7556     {
7557 	if (shl_flag == FALSE)
7558 	{
7559 	    shl = &search_hl;
7560 	    shl_flag = TRUE;
7561 	}
7562 	else
7563 	    shl = &cur->hl;
7564 	if (shl->rm.regprog != NULL
7565 		&& shl->lnum == 0
7566 		&& re_multiline(shl->rm.regprog))
7567 	{
7568 	    if (shl->first_lnum == 0)
7569 	    {
7570 # ifdef FEAT_FOLDING
7571 		for (shl->first_lnum = lnum;
7572 			   shl->first_lnum > wp->w_topline; --shl->first_lnum)
7573 		    if (hasFoldingWin(wp, shl->first_lnum - 1,
7574 						      NULL, NULL, TRUE, NULL))
7575 			break;
7576 # else
7577 		shl->first_lnum = wp->w_topline;
7578 # endif
7579 	    }
7580 	    if (cur != NULL)
7581 		cur->pos.cur = 0;
7582 	    pos_inprogress = TRUE;
7583 	    n = 0;
7584 	    while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7585 					  || (cur != NULL && pos_inprogress)))
7586 	    {
7587 		next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n, cur);
7588 		pos_inprogress = cur == NULL || cur->pos.cur == 0
7589 							      ? FALSE : TRUE;
7590 		if (shl->lnum != 0)
7591 		{
7592 		    shl->first_lnum = shl->lnum
7593 				    + shl->rm.endpos[0].lnum
7594 				    - shl->rm.startpos[0].lnum;
7595 		    n = shl->rm.endpos[0].col;
7596 		}
7597 		else
7598 		{
7599 		    ++shl->first_lnum;
7600 		    n = 0;
7601 		}
7602 	    }
7603 	}
7604 	if (shl != &search_hl && cur != NULL)
7605 	    cur = cur->next;
7606     }
7607 }
7608 
7609 /*
7610  * Search for a next 'hlsearch' or match.
7611  * Uses shl->buf.
7612  * Sets shl->lnum and shl->rm contents.
7613  * Note: Assumes a previous match is always before "lnum", unless
7614  * shl->lnum is zero.
7615  * Careful: Any pointers for buffer lines will become invalid.
7616  */
7617     static void
7618 next_search_hl(
7619     win_T	    *win,
7620     match_T	    *shl,	/* points to search_hl or a match */
7621     linenr_T	    lnum,
7622     colnr_T	    mincol,	/* minimal column for a match */
7623     matchitem_T	    *cur)	/* to retrieve match positions if any */
7624 {
7625     linenr_T	l;
7626     colnr_T	matchcol;
7627     long	nmatched;
7628 
7629     if (shl->lnum != 0)
7630     {
7631 	/* Check for three situations:
7632 	 * 1. If the "lnum" is below a previous match, start a new search.
7633 	 * 2. If the previous match includes "mincol", use it.
7634 	 * 3. Continue after the previous match.
7635 	 */
7636 	l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7637 	if (lnum > l)
7638 	    shl->lnum = 0;
7639 	else if (lnum < l || shl->rm.endpos[0].col > mincol)
7640 	    return;
7641     }
7642 
7643     /*
7644      * Repeat searching for a match until one is found that includes "mincol"
7645      * or none is found in this line.
7646      */
7647     called_emsg = FALSE;
7648     for (;;)
7649     {
7650 #ifdef FEAT_RELTIME
7651 	/* Stop searching after passing the time limit. */
7652 	if (profile_passed_limit(&(shl->tm)))
7653 	{
7654 	    shl->lnum = 0;		/* no match found in time */
7655 	    break;
7656 	}
7657 #endif
7658 	/* Three situations:
7659 	 * 1. No useful previous match: search from start of line.
7660 	 * 2. Not Vi compatible or empty match: continue at next character.
7661 	 *    Break the loop if this is beyond the end of the line.
7662 	 * 3. Vi compatible searching: continue at end of previous match.
7663 	 */
7664 	if (shl->lnum == 0)
7665 	    matchcol = 0;
7666 	else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7667 		|| (shl->rm.endpos[0].lnum == 0
7668 		    && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
7669 	{
7670 	    char_u	*ml;
7671 
7672 	    matchcol = shl->rm.startpos[0].col;
7673 	    ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
7674 	    if (*ml == NUL)
7675 	    {
7676 		++matchcol;
7677 		shl->lnum = 0;
7678 		break;
7679 	    }
7680 #ifdef FEAT_MBYTE
7681 	    if (has_mbyte)
7682 		matchcol += mb_ptr2len(ml);
7683 	    else
7684 #endif
7685 		++matchcol;
7686 	}
7687 	else
7688 	    matchcol = shl->rm.endpos[0].col;
7689 
7690 	shl->lnum = lnum;
7691 	if (shl->rm.regprog != NULL)
7692 	{
7693 	    /* Remember whether shl->rm is using a copy of the regprog in
7694 	     * cur->match. */
7695 	    int regprog_is_copy = (shl != &search_hl && cur != NULL
7696 				&& shl == &cur->hl
7697 				&& cur->match.regprog == cur->hl.rm.regprog);
7698 
7699 	    nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7700 		    matchcol,
7701 #ifdef FEAT_RELTIME
7702 		    &(shl->tm)
7703 #else
7704 		    NULL
7705 #endif
7706 		    );
7707 	    /* Copy the regprog, in case it got freed and recompiled. */
7708 	    if (regprog_is_copy)
7709 		cur->match.regprog = cur->hl.rm.regprog;
7710 
7711 	    if (called_emsg || got_int)
7712 	    {
7713 		/* Error while handling regexp: stop using this regexp. */
7714 		if (shl == &search_hl)
7715 		{
7716 		    /* don't free regprog in the match list, it's a copy */
7717 		    vim_regfree(shl->rm.regprog);
7718 		    SET_NO_HLSEARCH(TRUE);
7719 		}
7720 		shl->rm.regprog = NULL;
7721 		shl->lnum = 0;
7722 		got_int = FALSE;  /* avoid the "Type :quit to exit Vim"
7723 				     message */
7724 		break;
7725 	    }
7726 	}
7727 	else if (cur != NULL)
7728 	    nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
7729 	else
7730 	    nmatched = 0;
7731 	if (nmatched == 0)
7732 	{
7733 	    shl->lnum = 0;		/* no match found */
7734 	    break;
7735 	}
7736 	if (shl->rm.startpos[0].lnum > 0
7737 		|| shl->rm.startpos[0].col >= mincol
7738 		|| nmatched > 1
7739 		|| shl->rm.endpos[0].col > mincol)
7740 	{
7741 	    shl->lnum += shl->rm.startpos[0].lnum;
7742 	    break;			/* useful match found */
7743 	}
7744     }
7745 }
7746 
7747     static int
7748 next_search_hl_pos(
7749     match_T	    *shl,	/* points to a match */
7750     linenr_T	    lnum,
7751     posmatch_T	    *posmatch,	/* match positions */
7752     colnr_T	    mincol)	/* minimal column for a match */
7753 {
7754     int	    i;
7755     int	    bot = -1;
7756 
7757     shl->lnum = 0;
7758     for (i = posmatch->cur; i < MAXPOSMATCH; i++)
7759     {
7760 	if (posmatch->pos[i].lnum == 0)
7761 	    break;
7762 	if (posmatch->pos[i].col < mincol)
7763 	    continue;
7764 	if (posmatch->pos[i].lnum == lnum)
7765 	{
7766 	    if (shl->lnum == lnum)
7767 	    {
7768 		/* partially sort positions by column numbers
7769 		 * on the same line */
7770 		if (posmatch->pos[i].col < posmatch->pos[bot].col)
7771 		{
7772 		    llpos_T	tmp = posmatch->pos[i];
7773 
7774 		    posmatch->pos[i] = posmatch->pos[bot];
7775 		    posmatch->pos[bot] = tmp;
7776 		}
7777 	    }
7778 	    else
7779 	    {
7780 		bot = i;
7781 		shl->lnum = lnum;
7782 	    }
7783 	}
7784     }
7785     posmatch->cur = 0;
7786     if (shl->lnum == lnum && bot >= 0)
7787     {
7788 	colnr_T	start = posmatch->pos[bot].col == 0
7789 					     ? 0 : posmatch->pos[bot].col - 1;
7790 	colnr_T	end = posmatch->pos[bot].col == 0
7791 				    ? MAXCOL : start + posmatch->pos[bot].len;
7792 
7793 	shl->rm.startpos[0].lnum = 0;
7794 	shl->rm.startpos[0].col = start;
7795 	shl->rm.endpos[0].lnum = 0;
7796 	shl->rm.endpos[0].col = end;
7797 	posmatch->cur = bot + 1;
7798 	return TRUE;
7799     }
7800     return FALSE;
7801 }
7802 #endif
7803 
7804       static void
7805 screen_start_highlight(int attr)
7806 {
7807     attrentry_T *aep = NULL;
7808 
7809     screen_attr = attr;
7810     if (full_screen
7811 #ifdef WIN3264
7812 		    && termcap_active
7813 #endif
7814 				       )
7815     {
7816 #ifdef FEAT_GUI
7817 	if (gui.in_use)
7818 	{
7819 	    char	buf[20];
7820 
7821 	    /* The GUI handles this internally. */
7822 	    sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
7823 	    OUT_STR(buf);
7824 	}
7825 	else
7826 #endif
7827 	{
7828 	    if (attr > HL_ALL)				/* special HL attr. */
7829 	    {
7830 		if (t_colors > 1)
7831 		    aep = syn_cterm_attr2entry(attr);
7832 		else
7833 		    aep = syn_term_attr2entry(attr);
7834 		if (aep == NULL)	    /* did ":syntax clear" */
7835 		    attr = 0;
7836 		else
7837 		    attr = aep->ae_attr;
7838 	    }
7839 	    if ((attr & HL_BOLD) && T_MD != NULL)	/* bold */
7840 		out_str(T_MD);
7841 	    else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
7842 						      && cterm_normal_fg_bold)
7843 		/* If the Normal FG color has BOLD attribute and the new HL
7844 		 * has a FG color defined, clear BOLD. */
7845 		out_str(T_ME);
7846 	    if ((attr & HL_STANDOUT) && T_SO != NULL)	/* standout */
7847 		out_str(T_SO);
7848 	    if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
7849 						   /* underline or undercurl */
7850 		out_str(T_US);
7851 	    if ((attr & HL_ITALIC) && T_CZH != NULL)	/* italic */
7852 		out_str(T_CZH);
7853 	    if ((attr & HL_INVERSE) && T_MR != NULL)	/* inverse (reverse) */
7854 		out_str(T_MR);
7855 
7856 	    /*
7857 	     * Output the color or start string after bold etc., in case the
7858 	     * bold etc. override the color setting.
7859 	     */
7860 	    if (aep != NULL)
7861 	    {
7862 		if (t_colors > 1)
7863 		{
7864 		    if (aep->ae_u.cterm.fg_color)
7865 			term_fg_color(aep->ae_u.cterm.fg_color - 1);
7866 		    if (aep->ae_u.cterm.bg_color)
7867 			term_bg_color(aep->ae_u.cterm.bg_color - 1);
7868 		}
7869 		else
7870 		{
7871 		    if (aep->ae_u.term.start != NULL)
7872 			out_str(aep->ae_u.term.start);
7873 		}
7874 	    }
7875 	}
7876     }
7877 }
7878 
7879       void
7880 screen_stop_highlight(void)
7881 {
7882     int	    do_ME = FALSE;	    /* output T_ME code */
7883 
7884     if (screen_attr != 0
7885 #ifdef WIN3264
7886 			&& termcap_active
7887 #endif
7888 					   )
7889     {
7890 #ifdef FEAT_GUI
7891 	if (gui.in_use)
7892 	{
7893 	    char	buf[20];
7894 
7895 	    /* use internal GUI code */
7896 	    sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
7897 	    OUT_STR(buf);
7898 	}
7899 	else
7900 #endif
7901 	{
7902 	    if (screen_attr > HL_ALL)			/* special HL attr. */
7903 	    {
7904 		attrentry_T *aep;
7905 
7906 		if (t_colors > 1)
7907 		{
7908 		    /*
7909 		     * Assume that t_me restores the original colors!
7910 		     */
7911 		    aep = syn_cterm_attr2entry(screen_attr);
7912 		    if (aep != NULL && (aep->ae_u.cterm.fg_color
7913 						 || aep->ae_u.cterm.bg_color))
7914 			do_ME = TRUE;
7915 		}
7916 		else
7917 		{
7918 		    aep = syn_term_attr2entry(screen_attr);
7919 		    if (aep != NULL && aep->ae_u.term.stop != NULL)
7920 		    {
7921 			if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
7922 			    do_ME = TRUE;
7923 			else
7924 			    out_str(aep->ae_u.term.stop);
7925 		    }
7926 		}
7927 		if (aep == NULL)	    /* did ":syntax clear" */
7928 		    screen_attr = 0;
7929 		else
7930 		    screen_attr = aep->ae_attr;
7931 	    }
7932 
7933 	    /*
7934 	     * Often all ending-codes are equal to T_ME.  Avoid outputting the
7935 	     * same sequence several times.
7936 	     */
7937 	    if (screen_attr & HL_STANDOUT)
7938 	    {
7939 		if (STRCMP(T_SE, T_ME) == 0)
7940 		    do_ME = TRUE;
7941 		else
7942 		    out_str(T_SE);
7943 	    }
7944 	    if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
7945 	    {
7946 		if (STRCMP(T_UE, T_ME) == 0)
7947 		    do_ME = TRUE;
7948 		else
7949 		    out_str(T_UE);
7950 	    }
7951 	    if (screen_attr & HL_ITALIC)
7952 	    {
7953 		if (STRCMP(T_CZR, T_ME) == 0)
7954 		    do_ME = TRUE;
7955 		else
7956 		    out_str(T_CZR);
7957 	    }
7958 	    if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
7959 		out_str(T_ME);
7960 
7961 	    if (t_colors > 1)
7962 	    {
7963 		/* set Normal cterm colors */
7964 		if (cterm_normal_fg_color != 0)
7965 		    term_fg_color(cterm_normal_fg_color - 1);
7966 		if (cterm_normal_bg_color != 0)
7967 		    term_bg_color(cterm_normal_bg_color - 1);
7968 		if (cterm_normal_fg_bold)
7969 		    out_str(T_MD);
7970 	    }
7971 	}
7972     }
7973     screen_attr = 0;
7974 }
7975 
7976 /*
7977  * Reset the colors for a cterm.  Used when leaving Vim.
7978  * The machine specific code may override this again.
7979  */
7980     void
7981 reset_cterm_colors(void)
7982 {
7983     if (t_colors > 1)
7984     {
7985 	/* set Normal cterm colors */
7986 	if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
7987 	{
7988 	    out_str(T_OP);
7989 	    screen_attr = -1;
7990 	}
7991 	if (cterm_normal_fg_bold)
7992 	{
7993 	    out_str(T_ME);
7994 	    screen_attr = -1;
7995 	}
7996     }
7997 }
7998 
7999 /*
8000  * Put character ScreenLines["off"] on the screen at position "row" and "col",
8001  * using the attributes from ScreenAttrs["off"].
8002  */
8003     static void
8004 screen_char(unsigned off, int row, int col)
8005 {
8006     int		attr;
8007 
8008     /* Check for illegal values, just in case (could happen just after
8009      * resizing). */
8010     if (row >= screen_Rows || col >= screen_Columns)
8011 	return;
8012 
8013     /* Outputting a character in the last cell on the screen may scroll the
8014      * screen up.  Only do it when the "xn" termcap property is set, otherwise
8015      * mark the character invalid (update it when scrolled up). */
8016     if (*T_XN == NUL
8017 	    && row == screen_Rows - 1 && col == screen_Columns - 1
8018 #ifdef FEAT_RIGHTLEFT
8019 	    /* account for first command-line character in rightleft mode */
8020 	    && !cmdmsg_rl
8021 #endif
8022        )
8023     {
8024 	ScreenAttrs[off] = (sattr_T)-1;
8025 	return;
8026     }
8027 
8028     /*
8029      * Stop highlighting first, so it's easier to move the cursor.
8030      */
8031 #if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS)
8032     if (screen_char_attr != 0)
8033 	attr = screen_char_attr;
8034     else
8035 #endif
8036 	attr = ScreenAttrs[off];
8037     if (screen_attr != attr)
8038 	screen_stop_highlight();
8039 
8040     windgoto(row, col);
8041 
8042     if (screen_attr != attr)
8043 	screen_start_highlight(attr);
8044 
8045 #ifdef FEAT_MBYTE
8046     if (enc_utf8 && ScreenLinesUC[off] != 0)
8047     {
8048 	char_u	    buf[MB_MAXBYTES + 1];
8049 
8050 	/* Convert UTF-8 character to bytes and write it. */
8051 
8052 	buf[utfc_char2bytes(off, buf)] = NUL;
8053 
8054 	out_str(buf);
8055 	if (utf_char2cells(ScreenLinesUC[off]) > 1)
8056 	    ++screen_cur_col;
8057     }
8058     else
8059 #endif
8060     {
8061 #ifdef FEAT_MBYTE
8062 	out_flush_check();
8063 #endif
8064 	out_char(ScreenLines[off]);
8065 #ifdef FEAT_MBYTE
8066 	/* double-byte character in single-width cell */
8067 	if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8068 	    out_char(ScreenLines2[off]);
8069 #endif
8070     }
8071 
8072     screen_cur_col++;
8073 }
8074 
8075 #ifdef FEAT_MBYTE
8076 
8077 /*
8078  * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8079  * on the screen at position 'row' and 'col'.
8080  * The attributes of the first byte is used for all.  This is required to
8081  * output the two bytes of a double-byte character with nothing in between.
8082  */
8083     static void
8084 screen_char_2(unsigned off, int row, int col)
8085 {
8086     /* Check for illegal values (could be wrong when screen was resized). */
8087     if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8088 	return;
8089 
8090     /* Outputting the last character on the screen may scrollup the screen.
8091      * Don't to it!  Mark the character invalid (update it when scrolled up) */
8092     if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8093     {
8094 	ScreenAttrs[off] = (sattr_T)-1;
8095 	return;
8096     }
8097 
8098     /* Output the first byte normally (positions the cursor), then write the
8099      * second byte directly. */
8100     screen_char(off, row, col);
8101     out_char(ScreenLines[off + 1]);
8102     ++screen_cur_col;
8103 }
8104 #endif
8105 
8106 #if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS) || defined(PROTO)
8107 /*
8108  * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8109  * This uses the contents of ScreenLines[] and doesn't change it.
8110  */
8111     void
8112 screen_draw_rectangle(
8113     int		row,
8114     int		col,
8115     int		height,
8116     int		width,
8117     int		invert)
8118 {
8119     int		r, c;
8120     int		off;
8121 #ifdef FEAT_MBYTE
8122     int		max_off;
8123 #endif
8124 
8125     /* Can't use ScreenLines unless initialized */
8126     if (ScreenLines == NULL)
8127 	return;
8128 
8129     if (invert)
8130 	screen_char_attr = HL_INVERSE;
8131     for (r = row; r < row + height; ++r)
8132     {
8133 	off = LineOffset[r];
8134 #ifdef FEAT_MBYTE
8135 	max_off = off + screen_Columns;
8136 #endif
8137 	for (c = col; c < col + width; ++c)
8138 	{
8139 #ifdef FEAT_MBYTE
8140 	    if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
8141 	    {
8142 		screen_char_2(off + c, r, c);
8143 		++c;
8144 	    }
8145 	    else
8146 #endif
8147 	    {
8148 		screen_char(off + c, r, c);
8149 #ifdef FEAT_MBYTE
8150 		if (utf_off2cells(off + c, max_off) > 1)
8151 		    ++c;
8152 #endif
8153 	    }
8154 	}
8155     }
8156     screen_char_attr = 0;
8157 }
8158 #endif
8159 
8160 #ifdef FEAT_WINDOWS
8161 /*
8162  * Redraw the characters for a vertically split window.
8163  */
8164     static void
8165 redraw_block(int row, int end, win_T *wp)
8166 {
8167     int		col;
8168     int		width;
8169 
8170 # ifdef FEAT_CLIPBOARD
8171     clip_may_clear_selection(row, end - 1);
8172 # endif
8173 
8174     if (wp == NULL)
8175     {
8176 	col = 0;
8177 	width = Columns;
8178     }
8179     else
8180     {
8181 	col = wp->w_wincol;
8182 	width = wp->w_width;
8183     }
8184     screen_draw_rectangle(row, col, end - row, width, FALSE);
8185 }
8186 #endif
8187 
8188 /*
8189  * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8190  * with character 'c1' in first column followed by 'c2' in the other columns.
8191  * Use attributes 'attr'.
8192  */
8193     void
8194 screen_fill(
8195     int	    start_row,
8196     int	    end_row,
8197     int	    start_col,
8198     int	    end_col,
8199     int	    c1,
8200     int	    c2,
8201     int	    attr)
8202 {
8203     int		    row;
8204     int		    col;
8205     int		    off;
8206     int		    end_off;
8207     int		    did_delete;
8208     int		    c;
8209     int		    norm_term;
8210 #if defined(FEAT_GUI) || defined(UNIX)
8211     int		    force_next = FALSE;
8212 #endif
8213 
8214     if (end_row > screen_Rows)		/* safety check */
8215 	end_row = screen_Rows;
8216     if (end_col > screen_Columns)	/* safety check */
8217 	end_col = screen_Columns;
8218     if (ScreenLines == NULL
8219 	    || start_row >= end_row
8220 	    || start_col >= end_col)	/* nothing to do */
8221 	return;
8222 
8223     /* it's a "normal" terminal when not in a GUI or cterm */
8224     norm_term = (
8225 #ifdef FEAT_GUI
8226 	    !gui.in_use &&
8227 #endif
8228 			    t_colors <= 1);
8229     for (row = start_row; row < end_row; ++row)
8230     {
8231 #ifdef FEAT_MBYTE
8232 	if (has_mbyte
8233 # ifdef FEAT_GUI
8234 		&& !gui.in_use
8235 # endif
8236 	   )
8237 	{
8238 	    /* When drawing over the right halve of a double-wide char clear
8239 	     * out the left halve.  When drawing over the left halve of a
8240 	     * double wide-char clear out the right halve.  Only needed in a
8241 	     * terminal. */
8242 	    if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
8243 		screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
8244 	    if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
8245 		screen_puts_len((char_u *)" ", 1, row, end_col, 0);
8246 	}
8247 #endif
8248 	/*
8249 	 * Try to use delete-line termcap code, when no attributes or in a
8250 	 * "normal" terminal, where a bold/italic space is just a
8251 	 * space.
8252 	 */
8253 	did_delete = FALSE;
8254 	if (c2 == ' '
8255 		&& end_col == Columns
8256 		&& can_clear(T_CE)
8257 		&& (attr == 0
8258 		    || (norm_term
8259 			&& attr <= HL_ALL
8260 			&& ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8261 	{
8262 	    /*
8263 	     * check if we really need to clear something
8264 	     */
8265 	    col = start_col;
8266 	    if (c1 != ' ')			/* don't clear first char */
8267 		++col;
8268 
8269 	    off = LineOffset[row] + col;
8270 	    end_off = LineOffset[row] + end_col;
8271 
8272 	    /* skip blanks (used often, keep it fast!) */
8273 #ifdef FEAT_MBYTE
8274 	    if (enc_utf8)
8275 		while (off < end_off && ScreenLines[off] == ' '
8276 			  && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8277 		    ++off;
8278 	    else
8279 #endif
8280 		while (off < end_off && ScreenLines[off] == ' '
8281 						     && ScreenAttrs[off] == 0)
8282 		    ++off;
8283 	    if (off < end_off)		/* something to be cleared */
8284 	    {
8285 		col = off - LineOffset[row];
8286 		screen_stop_highlight();
8287 		term_windgoto(row, col);/* clear rest of this screen line */
8288 		out_str(T_CE);
8289 		screen_start();		/* don't know where cursor is now */
8290 		col = end_col - col;
8291 		while (col--)		/* clear chars in ScreenLines */
8292 		{
8293 		    ScreenLines[off] = ' ';
8294 #ifdef FEAT_MBYTE
8295 		    if (enc_utf8)
8296 			ScreenLinesUC[off] = 0;
8297 #endif
8298 		    ScreenAttrs[off] = 0;
8299 		    ++off;
8300 		}
8301 	    }
8302 	    did_delete = TRUE;		/* the chars are cleared now */
8303 	}
8304 
8305 	off = LineOffset[row] + start_col;
8306 	c = c1;
8307 	for (col = start_col; col < end_col; ++col)
8308 	{
8309 	    if (ScreenLines[off] != c
8310 #ifdef FEAT_MBYTE
8311 		    || (enc_utf8 && (int)ScreenLinesUC[off]
8312 						       != (c >= 0x80 ? c : 0))
8313 #endif
8314 		    || ScreenAttrs[off] != attr
8315 #if defined(FEAT_GUI) || defined(UNIX)
8316 		    || force_next
8317 #endif
8318 		    )
8319 	    {
8320 #if defined(FEAT_GUI) || defined(UNIX)
8321 		/* The bold trick may make a single row of pixels appear in
8322 		 * the next character.  When a bold character is removed, the
8323 		 * next character should be redrawn too.  This happens for our
8324 		 * own GUI and for some xterms.  */
8325 		if (
8326 # ifdef FEAT_GUI
8327 			gui.in_use
8328 # endif
8329 # if defined(FEAT_GUI) && defined(UNIX)
8330 			||
8331 # endif
8332 # ifdef UNIX
8333 			term_is_xterm
8334 # endif
8335 		   )
8336 		{
8337 		    if (ScreenLines[off] != ' '
8338 			    && (ScreenAttrs[off] > HL_ALL
8339 				|| ScreenAttrs[off] & HL_BOLD))
8340 			force_next = TRUE;
8341 		    else
8342 			force_next = FALSE;
8343 		}
8344 #endif
8345 		ScreenLines[off] = c;
8346 #ifdef FEAT_MBYTE
8347 		if (enc_utf8)
8348 		{
8349 		    if (c >= 0x80)
8350 		    {
8351 			ScreenLinesUC[off] = c;
8352 			ScreenLinesC[0][off] = 0;
8353 		    }
8354 		    else
8355 			ScreenLinesUC[off] = 0;
8356 		}
8357 #endif
8358 		ScreenAttrs[off] = attr;
8359 		if (!did_delete || c != ' ')
8360 		    screen_char(off, row, col);
8361 	    }
8362 	    ++off;
8363 	    if (col == start_col)
8364 	    {
8365 		if (did_delete)
8366 		    break;
8367 		c = c2;
8368 	    }
8369 	}
8370 	if (end_col == Columns)
8371 	    LineWraps[row] = FALSE;
8372 	if (row == Rows - 1)		/* overwritten the command line */
8373 	{
8374 	    redraw_cmdline = TRUE;
8375 	    if (c1 == ' ' && c2 == ' ')
8376 		clear_cmdline = FALSE;	/* command line has been cleared */
8377 	    if (start_col == 0)
8378 		mode_displayed = FALSE; /* mode cleared or overwritten */
8379 	}
8380     }
8381 }
8382 
8383 /*
8384  * Check if there should be a delay.  Used before clearing or redrawing the
8385  * screen or the command line.
8386  */
8387     void
8388 check_for_delay(int check_msg_scroll)
8389 {
8390     if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8391 	    && !did_wait_return
8392 	    && emsg_silent == 0)
8393     {
8394 	out_flush();
8395 	ui_delay(1000L, TRUE);
8396 	emsg_on_display = FALSE;
8397 	if (check_msg_scroll)
8398 	    msg_scroll = FALSE;
8399     }
8400 }
8401 
8402 /*
8403  * screen_valid -  allocate screen buffers if size changed
8404  *   If "doclear" is TRUE: clear screen if it has been resized.
8405  *	Returns TRUE if there is a valid screen to write to.
8406  *	Returns FALSE when starting up and screen not initialized yet.
8407  */
8408     int
8409 screen_valid(int doclear)
8410 {
8411     screenalloc(doclear);	   /* allocate screen buffers if size changed */
8412     return (ScreenLines != NULL);
8413 }
8414 
8415 /*
8416  * Resize the shell to Rows and Columns.
8417  * Allocate ScreenLines[] and associated items.
8418  *
8419  * There may be some time between setting Rows and Columns and (re)allocating
8420  * ScreenLines[].  This happens when starting up and when (manually) changing
8421  * the shell size.  Always use screen_Rows and screen_Columns to access items
8422  * in ScreenLines[].  Use Rows and Columns for positioning text etc. where the
8423  * final size of the shell is needed.
8424  */
8425     void
8426 screenalloc(int doclear)
8427 {
8428     int		    new_row, old_row;
8429 #ifdef FEAT_GUI
8430     int		    old_Rows;
8431 #endif
8432     win_T	    *wp;
8433     int		    outofmem = FALSE;
8434     int		    len;
8435     schar_T	    *new_ScreenLines;
8436 #ifdef FEAT_MBYTE
8437     u8char_T	    *new_ScreenLinesUC = NULL;
8438     u8char_T	    *new_ScreenLinesC[MAX_MCO];
8439     schar_T	    *new_ScreenLines2 = NULL;
8440     int		    i;
8441 #endif
8442     sattr_T	    *new_ScreenAttrs;
8443     unsigned	    *new_LineOffset;
8444     char_u	    *new_LineWraps;
8445 #ifdef FEAT_WINDOWS
8446     short	    *new_TabPageIdxs;
8447     tabpage_T	    *tp;
8448 #endif
8449     static int	    entered = FALSE;		/* avoid recursiveness */
8450     static int	    done_outofmem_msg = FALSE;	/* did outofmem message */
8451 #ifdef FEAT_AUTOCMD
8452     int		    retry_count = 0;
8453 
8454 retry:
8455 #endif
8456     /*
8457      * Allocation of the screen buffers is done only when the size changes and
8458      * when Rows and Columns have been set and we have started doing full
8459      * screen stuff.
8460      */
8461     if ((ScreenLines != NULL
8462 		&& Rows == screen_Rows
8463 		&& Columns == screen_Columns
8464 #ifdef FEAT_MBYTE
8465 		&& enc_utf8 == (ScreenLinesUC != NULL)
8466 		&& (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
8467 		&& p_mco == Screen_mco
8468 #endif
8469 		)
8470 	    || Rows == 0
8471 	    || Columns == 0
8472 	    || (!full_screen && ScreenLines == NULL))
8473 	return;
8474 
8475     /*
8476      * It's possible that we produce an out-of-memory message below, which
8477      * will cause this function to be called again.  To break the loop, just
8478      * return here.
8479      */
8480     if (entered)
8481 	return;
8482     entered = TRUE;
8483 
8484     /*
8485      * Note that the window sizes are updated before reallocating the arrays,
8486      * thus we must not redraw here!
8487      */
8488     ++RedrawingDisabled;
8489 
8490     win_new_shellsize();    /* fit the windows in the new sized shell */
8491 
8492     comp_col();		/* recompute columns for shown command and ruler */
8493 
8494     /*
8495      * We're changing the size of the screen.
8496      * - Allocate new arrays for ScreenLines and ScreenAttrs.
8497      * - Move lines from the old arrays into the new arrays, clear extra
8498      *	 lines (unless the screen is going to be cleared).
8499      * - Free the old arrays.
8500      *
8501      * If anything fails, make ScreenLines NULL, so we don't do anything!
8502      * Continuing with the old ScreenLines may result in a crash, because the
8503      * size is wrong.
8504      */
8505     FOR_ALL_TAB_WINDOWS(tp, wp)
8506 	win_free_lsize(wp);
8507 #ifdef FEAT_AUTOCMD
8508     if (aucmd_win != NULL)
8509 	win_free_lsize(aucmd_win);
8510 #endif
8511 
8512     new_ScreenLines = (schar_T *)lalloc((long_u)(
8513 			      (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8514 #ifdef FEAT_MBYTE
8515     vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
8516     if (enc_utf8)
8517     {
8518 	new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8519 			     (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8520 	for (i = 0; i < p_mco; ++i)
8521 	    new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
8522 			     (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8523     }
8524     if (enc_dbcs == DBCS_JPNU)
8525 	new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8526 			     (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8527 #endif
8528     new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8529 			      (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8530     new_LineOffset = (unsigned *)lalloc((long_u)(
8531 					 Rows * sizeof(unsigned)), FALSE);
8532     new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
8533 #ifdef FEAT_WINDOWS
8534     new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
8535 #endif
8536 
8537     FOR_ALL_TAB_WINDOWS(tp, wp)
8538     {
8539 	if (win_alloc_lines(wp) == FAIL)
8540 	{
8541 	    outofmem = TRUE;
8542 #ifdef FEAT_WINDOWS
8543 	    goto give_up;
8544 #endif
8545 	}
8546     }
8547 #ifdef FEAT_AUTOCMD
8548     if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8549 					&& win_alloc_lines(aucmd_win) == FAIL)
8550 	outofmem = TRUE;
8551 #endif
8552 #ifdef FEAT_WINDOWS
8553 give_up:
8554 #endif
8555 
8556 #ifdef FEAT_MBYTE
8557     for (i = 0; i < p_mco; ++i)
8558 	if (new_ScreenLinesC[i] == NULL)
8559 	    break;
8560 #endif
8561     if (new_ScreenLines == NULL
8562 #ifdef FEAT_MBYTE
8563 	    || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
8564 	    || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8565 #endif
8566 	    || new_ScreenAttrs == NULL
8567 	    || new_LineOffset == NULL
8568 	    || new_LineWraps == NULL
8569 #ifdef FEAT_WINDOWS
8570 	    || new_TabPageIdxs == NULL
8571 #endif
8572 	    || outofmem)
8573     {
8574 	if (ScreenLines != NULL || !done_outofmem_msg)
8575 	{
8576 	    /* guess the size */
8577 	    do_outofmem_msg((long_u)((Rows + 1) * Columns));
8578 
8579 	    /* Remember we did this to avoid getting outofmem messages over
8580 	     * and over again. */
8581 	    done_outofmem_msg = TRUE;
8582 	}
8583 	vim_free(new_ScreenLines);
8584 	new_ScreenLines = NULL;
8585 #ifdef FEAT_MBYTE
8586 	vim_free(new_ScreenLinesUC);
8587 	new_ScreenLinesUC = NULL;
8588 	for (i = 0; i < p_mco; ++i)
8589 	{
8590 	    vim_free(new_ScreenLinesC[i]);
8591 	    new_ScreenLinesC[i] = NULL;
8592 	}
8593 	vim_free(new_ScreenLines2);
8594 	new_ScreenLines2 = NULL;
8595 #endif
8596 	vim_free(new_ScreenAttrs);
8597 	new_ScreenAttrs = NULL;
8598 	vim_free(new_LineOffset);
8599 	new_LineOffset = NULL;
8600 	vim_free(new_LineWraps);
8601 	new_LineWraps = NULL;
8602 #ifdef FEAT_WINDOWS
8603 	vim_free(new_TabPageIdxs);
8604 	new_TabPageIdxs = NULL;
8605 #endif
8606     }
8607     else
8608     {
8609 	done_outofmem_msg = FALSE;
8610 
8611 	for (new_row = 0; new_row < Rows; ++new_row)
8612 	{
8613 	    new_LineOffset[new_row] = new_row * Columns;
8614 	    new_LineWraps[new_row] = FALSE;
8615 
8616 	    /*
8617 	     * If the screen is not going to be cleared, copy as much as
8618 	     * possible from the old screen to the new one and clear the rest
8619 	     * (used when resizing the window at the "--more--" prompt or when
8620 	     * executing an external command, for the GUI).
8621 	     */
8622 	    if (!doclear)
8623 	    {
8624 		(void)vim_memset(new_ScreenLines + new_row * Columns,
8625 				      ' ', (size_t)Columns * sizeof(schar_T));
8626 #ifdef FEAT_MBYTE
8627 		if (enc_utf8)
8628 		{
8629 		    (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8630 				       0, (size_t)Columns * sizeof(u8char_T));
8631 		    for (i = 0; i < p_mco; ++i)
8632 			(void)vim_memset(new_ScreenLinesC[i]
8633 							  + new_row * Columns,
8634 				       0, (size_t)Columns * sizeof(u8char_T));
8635 		}
8636 		if (enc_dbcs == DBCS_JPNU)
8637 		    (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8638 				       0, (size_t)Columns * sizeof(schar_T));
8639 #endif
8640 		(void)vim_memset(new_ScreenAttrs + new_row * Columns,
8641 					0, (size_t)Columns * sizeof(sattr_T));
8642 		old_row = new_row + (screen_Rows - Rows);
8643 		if (old_row >= 0 && ScreenLines != NULL)
8644 		{
8645 		    if (screen_Columns < Columns)
8646 			len = screen_Columns;
8647 		    else
8648 			len = Columns;
8649 #ifdef FEAT_MBYTE
8650 		    /* When switching to utf-8 don't copy characters, they
8651 		     * may be invalid now.  Also when p_mco changes. */
8652 		    if (!(enc_utf8 && ScreenLinesUC == NULL)
8653 						       && p_mco == Screen_mco)
8654 #endif
8655 			mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8656 				ScreenLines + LineOffset[old_row],
8657 				(size_t)len * sizeof(schar_T));
8658 #ifdef FEAT_MBYTE
8659 		    if (enc_utf8 && ScreenLinesUC != NULL
8660 						       && p_mco == Screen_mco)
8661 		    {
8662 			mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8663 				ScreenLinesUC + LineOffset[old_row],
8664 				(size_t)len * sizeof(u8char_T));
8665 			for (i = 0; i < p_mco; ++i)
8666 			    mch_memmove(new_ScreenLinesC[i]
8667 						    + new_LineOffset[new_row],
8668 				ScreenLinesC[i] + LineOffset[old_row],
8669 				(size_t)len * sizeof(u8char_T));
8670 		    }
8671 		    if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8672 			mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8673 				ScreenLines2 + LineOffset[old_row],
8674 				(size_t)len * sizeof(schar_T));
8675 #endif
8676 		    mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8677 			    ScreenAttrs + LineOffset[old_row],
8678 			    (size_t)len * sizeof(sattr_T));
8679 		}
8680 	    }
8681 	}
8682 	/* Use the last line of the screen for the current line. */
8683 	current_ScreenLine = new_ScreenLines + Rows * Columns;
8684     }
8685 
8686     free_screenlines();
8687 
8688     ScreenLines = new_ScreenLines;
8689 #ifdef FEAT_MBYTE
8690     ScreenLinesUC = new_ScreenLinesUC;
8691     for (i = 0; i < p_mco; ++i)
8692 	ScreenLinesC[i] = new_ScreenLinesC[i];
8693     Screen_mco = p_mco;
8694     ScreenLines2 = new_ScreenLines2;
8695 #endif
8696     ScreenAttrs = new_ScreenAttrs;
8697     LineOffset = new_LineOffset;
8698     LineWraps = new_LineWraps;
8699 #ifdef FEAT_WINDOWS
8700     TabPageIdxs = new_TabPageIdxs;
8701 #endif
8702 
8703     /* It's important that screen_Rows and screen_Columns reflect the actual
8704      * size of ScreenLines[].  Set them before calling anything. */
8705 #ifdef FEAT_GUI
8706     old_Rows = screen_Rows;
8707 #endif
8708     screen_Rows = Rows;
8709     screen_Columns = Columns;
8710 
8711     must_redraw = CLEAR;	/* need to clear the screen later */
8712     if (doclear)
8713 	screenclear2();
8714 
8715 #ifdef FEAT_GUI
8716     else if (gui.in_use
8717 	    && !gui.starting
8718 	    && ScreenLines != NULL
8719 	    && old_Rows != Rows)
8720     {
8721 	(void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8722 	/*
8723 	 * Adjust the position of the cursor, for when executing an external
8724 	 * command.
8725 	 */
8726 	if (msg_row >= Rows)		/* Rows got smaller */
8727 	    msg_row = Rows - 1;		/* put cursor at last row */
8728 	else if (Rows > old_Rows)	/* Rows got bigger */
8729 	    msg_row += Rows - old_Rows; /* put cursor in same place */
8730 	if (msg_col >= Columns)		/* Columns got smaller */
8731 	    msg_col = Columns - 1;	/* put cursor at last column */
8732     }
8733 #endif
8734 
8735     entered = FALSE;
8736     --RedrawingDisabled;
8737 
8738 #ifdef FEAT_AUTOCMD
8739     /*
8740      * Do not apply autocommands more than 3 times to avoid an endless loop
8741      * in case applying autocommands always changes Rows or Columns.
8742      */
8743     if (starting == 0 && ++retry_count <= 3)
8744     {
8745 	apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
8746 	/* In rare cases, autocommands may have altered Rows or Columns,
8747 	 * jump back to check if we need to allocate the screen again. */
8748 	goto retry;
8749     }
8750 #endif
8751 }
8752 
8753     void
8754 free_screenlines(void)
8755 {
8756 #ifdef FEAT_MBYTE
8757     int		i;
8758 
8759     vim_free(ScreenLinesUC);
8760     for (i = 0; i < Screen_mco; ++i)
8761 	vim_free(ScreenLinesC[i]);
8762     vim_free(ScreenLines2);
8763 #endif
8764     vim_free(ScreenLines);
8765     vim_free(ScreenAttrs);
8766     vim_free(LineOffset);
8767     vim_free(LineWraps);
8768 #ifdef FEAT_WINDOWS
8769     vim_free(TabPageIdxs);
8770 #endif
8771 }
8772 
8773     void
8774 screenclear(void)
8775 {
8776     check_for_delay(FALSE);
8777     screenalloc(FALSE);	    /* allocate screen buffers if size changed */
8778     screenclear2();	    /* clear the screen */
8779 }
8780 
8781     static void
8782 screenclear2(void)
8783 {
8784     int	    i;
8785 
8786     if (starting == NO_SCREEN || ScreenLines == NULL
8787 #ifdef FEAT_GUI
8788 	    || (gui.in_use && gui.starting)
8789 #endif
8790 	    )
8791 	return;
8792 
8793 #ifdef FEAT_GUI
8794     if (!gui.in_use)
8795 #endif
8796 	screen_attr = -1;	/* force setting the Normal colors */
8797     screen_stop_highlight();	/* don't want highlighting here */
8798 
8799 #ifdef FEAT_CLIPBOARD
8800     /* disable selection without redrawing it */
8801     clip_scroll_selection(9999);
8802 #endif
8803 
8804     /* blank out ScreenLines */
8805     for (i = 0; i < Rows; ++i)
8806     {
8807 	lineclear(LineOffset[i], (int)Columns);
8808 	LineWraps[i] = FALSE;
8809     }
8810 
8811     if (can_clear(T_CL))
8812     {
8813 	out_str(T_CL);		/* clear the display */
8814 	clear_cmdline = FALSE;
8815 	mode_displayed = FALSE;
8816     }
8817     else
8818     {
8819 	/* can't clear the screen, mark all chars with invalid attributes */
8820 	for (i = 0; i < Rows; ++i)
8821 	    lineinvalid(LineOffset[i], (int)Columns);
8822 	clear_cmdline = TRUE;
8823     }
8824 
8825     screen_cleared = TRUE;	/* can use contents of ScreenLines now */
8826 
8827     win_rest_invalid(firstwin);
8828     redraw_cmdline = TRUE;
8829 #ifdef FEAT_WINDOWS
8830     redraw_tabline = TRUE;
8831 #endif
8832     if (must_redraw == CLEAR)	/* no need to clear again */
8833 	must_redraw = NOT_VALID;
8834     compute_cmdrow();
8835     msg_row = cmdline_row;	/* put cursor on last line for messages */
8836     msg_col = 0;
8837     screen_start();		/* don't know where cursor is now */
8838     msg_scrolled = 0;		/* can't scroll back */
8839     msg_didany = FALSE;
8840     msg_didout = FALSE;
8841 }
8842 
8843 /*
8844  * Clear one line in ScreenLines.
8845  */
8846     static void
8847 lineclear(unsigned off, int width)
8848 {
8849     (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
8850 #ifdef FEAT_MBYTE
8851     if (enc_utf8)
8852 	(void)vim_memset(ScreenLinesUC + off, 0,
8853 					  (size_t)width * sizeof(u8char_T));
8854 #endif
8855     (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
8856 }
8857 
8858 /*
8859  * Mark one line in ScreenLines invalid by setting the attributes to an
8860  * invalid value.
8861  */
8862     static void
8863 lineinvalid(unsigned off, int width)
8864 {
8865     (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
8866 }
8867 
8868 #ifdef FEAT_WINDOWS
8869 /*
8870  * Copy part of a Screenline for vertically split window "wp".
8871  */
8872     static void
8873 linecopy(int to, int from, win_T *wp)
8874 {
8875     unsigned	off_to = LineOffset[to] + wp->w_wincol;
8876     unsigned	off_from = LineOffset[from] + wp->w_wincol;
8877 
8878     mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
8879 	    wp->w_width * sizeof(schar_T));
8880 # ifdef FEAT_MBYTE
8881     if (enc_utf8)
8882     {
8883 	int	i;
8884 
8885 	mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
8886 		wp->w_width * sizeof(u8char_T));
8887 	for (i = 0; i < p_mco; ++i)
8888 	    mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
8889 		    wp->w_width * sizeof(u8char_T));
8890     }
8891     if (enc_dbcs == DBCS_JPNU)
8892 	mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
8893 		wp->w_width * sizeof(schar_T));
8894 # endif
8895     mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
8896 	    wp->w_width * sizeof(sattr_T));
8897 }
8898 #endif
8899 
8900 /*
8901  * Return TRUE if clearing with term string "p" would work.
8902  * It can't work when the string is empty or it won't set the right background.
8903  */
8904     int
8905 can_clear(char_u *p)
8906 {
8907     return (*p != NUL && (t_colors <= 1
8908 #ifdef FEAT_GUI
8909 		|| gui.in_use
8910 #endif
8911 		|| cterm_normal_bg_color == 0 || *T_UT != NUL));
8912 }
8913 
8914 /*
8915  * Reset cursor position. Use whenever cursor was moved because of outputting
8916  * something directly to the screen (shell commands) or a terminal control
8917  * code.
8918  */
8919     void
8920 screen_start(void)
8921 {
8922     screen_cur_row = screen_cur_col = 9999;
8923 }
8924 
8925 /*
8926  * Move the cursor to position "row","col" in the screen.
8927  * This tries to find the most efficient way to move, minimizing the number of
8928  * characters sent to the terminal.
8929  */
8930     void
8931 windgoto(int row, int col)
8932 {
8933     sattr_T	    *p;
8934     int		    i;
8935     int		    plan;
8936     int		    cost;
8937     int		    wouldbe_col;
8938     int		    noinvcurs;
8939     char_u	    *bs;
8940     int		    goto_cost;
8941     int		    attr;
8942 
8943 #define GOTO_COST   7	/* assume a term_windgoto() takes about 7 chars */
8944 #define HIGHL_COST  5	/* assume unhighlight takes 5 chars */
8945 
8946 #define PLAN_LE	    1
8947 #define PLAN_CR	    2
8948 #define PLAN_NL	    3
8949 #define PLAN_WRITE  4
8950     /* Can't use ScreenLines unless initialized */
8951     if (ScreenLines == NULL)
8952 	return;
8953 
8954     if (col != screen_cur_col || row != screen_cur_row)
8955     {
8956 	/* Check for valid position. */
8957 	if (row < 0)	/* window without text lines? */
8958 	    row = 0;
8959 	if (row >= screen_Rows)
8960 	    row = screen_Rows - 1;
8961 	if (col >= screen_Columns)
8962 	    col = screen_Columns - 1;
8963 
8964 	/* check if no cursor movement is allowed in highlight mode */
8965 	if (screen_attr && *T_MS == NUL)
8966 	    noinvcurs = HIGHL_COST;
8967 	else
8968 	    noinvcurs = 0;
8969 	goto_cost = GOTO_COST + noinvcurs;
8970 
8971 	/*
8972 	 * Plan how to do the positioning:
8973 	 * 1. Use CR to move it to column 0, same row.
8974 	 * 2. Use T_LE to move it a few columns to the left.
8975 	 * 3. Use NL to move a few lines down, column 0.
8976 	 * 4. Move a few columns to the right with T_ND or by writing chars.
8977 	 *
8978 	 * Don't do this if the cursor went beyond the last column, the cursor
8979 	 * position is unknown then (some terminals wrap, some don't )
8980 	 *
8981 	 * First check if the highlighting attributes allow us to write
8982 	 * characters to move the cursor to the right.
8983 	 */
8984 	if (row >= screen_cur_row && screen_cur_col < Columns)
8985 	{
8986 	    /*
8987 	     * If the cursor is in the same row, bigger col, we can use CR
8988 	     * or T_LE.
8989 	     */
8990 	    bs = NULL;			    /* init for GCC */
8991 	    attr = screen_attr;
8992 	    if (row == screen_cur_row && col < screen_cur_col)
8993 	    {
8994 		/* "le" is preferred over "bc", because "bc" is obsolete */
8995 		if (*T_LE)
8996 		    bs = T_LE;		    /* "cursor left" */
8997 		else
8998 		    bs = T_BC;		    /* "backspace character (old) */
8999 		if (*bs)
9000 		    cost = (screen_cur_col - col) * (int)STRLEN(bs);
9001 		else
9002 		    cost = 999;
9003 		if (col + 1 < cost)	    /* using CR is less characters */
9004 		{
9005 		    plan = PLAN_CR;
9006 		    wouldbe_col = 0;
9007 		    cost = 1;		    /* CR is just one character */
9008 		}
9009 		else
9010 		{
9011 		    plan = PLAN_LE;
9012 		    wouldbe_col = col;
9013 		}
9014 		if (noinvcurs)		    /* will stop highlighting */
9015 		{
9016 		    cost += noinvcurs;
9017 		    attr = 0;
9018 		}
9019 	    }
9020 
9021 	    /*
9022 	     * If the cursor is above where we want to be, we can use CR LF.
9023 	     */
9024 	    else if (row > screen_cur_row)
9025 	    {
9026 		plan = PLAN_NL;
9027 		wouldbe_col = 0;
9028 		cost = (row - screen_cur_row) * 2;  /* CR LF */
9029 		if (noinvcurs)		    /* will stop highlighting */
9030 		{
9031 		    cost += noinvcurs;
9032 		    attr = 0;
9033 		}
9034 	    }
9035 
9036 	    /*
9037 	     * If the cursor is in the same row, smaller col, just use write.
9038 	     */
9039 	    else
9040 	    {
9041 		plan = PLAN_WRITE;
9042 		wouldbe_col = screen_cur_col;
9043 		cost = 0;
9044 	    }
9045 
9046 	    /*
9047 	     * Check if any characters that need to be written have the
9048 	     * correct attributes.  Also avoid UTF-8 characters.
9049 	     */
9050 	    i = col - wouldbe_col;
9051 	    if (i > 0)
9052 		cost += i;
9053 	    if (cost < goto_cost && i > 0)
9054 	    {
9055 		/*
9056 		 * Check if the attributes are correct without additionally
9057 		 * stopping highlighting.
9058 		 */
9059 		p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9060 		while (i && *p++ == attr)
9061 		    --i;
9062 		if (i != 0)
9063 		{
9064 		    /*
9065 		     * Try if it works when highlighting is stopped here.
9066 		     */
9067 		    if (*--p == 0)
9068 		    {
9069 			cost += noinvcurs;
9070 			while (i && *p++ == 0)
9071 			    --i;
9072 		    }
9073 		    if (i != 0)
9074 			cost = 999;	/* different attributes, don't do it */
9075 		}
9076 #ifdef FEAT_MBYTE
9077 		if (enc_utf8)
9078 		{
9079 		    /* Don't use an UTF-8 char for positioning, it's slow. */
9080 		    for (i = wouldbe_col; i < col; ++i)
9081 			if (ScreenLinesUC[LineOffset[row] + i] != 0)
9082 			{
9083 			    cost = 999;
9084 			    break;
9085 			}
9086 		}
9087 #endif
9088 	    }
9089 
9090 	    /*
9091 	     * We can do it without term_windgoto()!
9092 	     */
9093 	    if (cost < goto_cost)
9094 	    {
9095 		if (plan == PLAN_LE)
9096 		{
9097 		    if (noinvcurs)
9098 			screen_stop_highlight();
9099 		    while (screen_cur_col > col)
9100 		    {
9101 			out_str(bs);
9102 			--screen_cur_col;
9103 		    }
9104 		}
9105 		else if (plan == PLAN_CR)
9106 		{
9107 		    if (noinvcurs)
9108 			screen_stop_highlight();
9109 		    out_char('\r');
9110 		    screen_cur_col = 0;
9111 		}
9112 		else if (plan == PLAN_NL)
9113 		{
9114 		    if (noinvcurs)
9115 			screen_stop_highlight();
9116 		    while (screen_cur_row < row)
9117 		    {
9118 			out_char('\n');
9119 			++screen_cur_row;
9120 		    }
9121 		    screen_cur_col = 0;
9122 		}
9123 
9124 		i = col - screen_cur_col;
9125 		if (i > 0)
9126 		{
9127 		    /*
9128 		     * Use cursor-right if it's one character only.  Avoids
9129 		     * removing a line of pixels from the last bold char, when
9130 		     * using the bold trick in the GUI.
9131 		     */
9132 		    if (T_ND[0] != NUL && T_ND[1] == NUL)
9133 		    {
9134 			while (i-- > 0)
9135 			    out_char(*T_ND);
9136 		    }
9137 		    else
9138 		    {
9139 			int	off;
9140 
9141 			off = LineOffset[row] + screen_cur_col;
9142 			while (i-- > 0)
9143 			{
9144 			    if (ScreenAttrs[off] != screen_attr)
9145 				screen_stop_highlight();
9146 #ifdef FEAT_MBYTE
9147 			    out_flush_check();
9148 #endif
9149 			    out_char(ScreenLines[off]);
9150 #ifdef FEAT_MBYTE
9151 			    if (enc_dbcs == DBCS_JPNU
9152 						  && ScreenLines[off] == 0x8e)
9153 				out_char(ScreenLines2[off]);
9154 #endif
9155 			    ++off;
9156 			}
9157 		    }
9158 		}
9159 	    }
9160 	}
9161 	else
9162 	    cost = 999;
9163 
9164 	if (cost >= goto_cost)
9165 	{
9166 	    if (noinvcurs)
9167 		screen_stop_highlight();
9168 	    if (row == screen_cur_row && (col > screen_cur_col)
9169 							     && *T_CRI != NUL)
9170 		term_cursor_right(col - screen_cur_col);
9171 	    else
9172 		term_windgoto(row, col);
9173 	}
9174 	screen_cur_row = row;
9175 	screen_cur_col = col;
9176     }
9177 }
9178 
9179 /*
9180  * Set cursor to its position in the current window.
9181  */
9182     void
9183 setcursor(void)
9184 {
9185     if (redrawing())
9186     {
9187 	validate_cursor();
9188 	windgoto(W_WINROW(curwin) + curwin->w_wrow,
9189 		W_WINCOL(curwin) + (
9190 #ifdef FEAT_RIGHTLEFT
9191 		/* With 'rightleft' set and the cursor on a double-wide
9192 		 * character, position it on the leftmost column. */
9193 		curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
9194 # ifdef FEAT_MBYTE
9195 			(has_mbyte
9196 			   && (*mb_ptr2cells)(ml_get_cursor()) == 2
9197 			   && vim_isprintc(gchar_cursor())) ? 2 :
9198 # endif
9199 			1)) :
9200 #endif
9201 							    curwin->w_wcol));
9202     }
9203 }
9204 
9205 
9206 /*
9207  * insert 'line_count' lines at 'row' in window 'wp'
9208  * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9209  * if 'mayclear' is TRUE the screen will be cleared if it is faster than
9210  * scrolling.
9211  * Returns FAIL if the lines are not inserted, OK for success.
9212  */
9213     int
9214 win_ins_lines(
9215     win_T	*wp,
9216     int		row,
9217     int		line_count,
9218     int		invalid,
9219     int		mayclear)
9220 {
9221     int		did_delete;
9222     int		nextrow;
9223     int		lastrow;
9224     int		retval;
9225 
9226     if (invalid)
9227 	wp->w_lines_valid = 0;
9228 
9229     if (wp->w_height < 5)
9230 	return FAIL;
9231 
9232     if (line_count > wp->w_height - row)
9233 	line_count = wp->w_height - row;
9234 
9235     retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
9236     if (retval != MAYBE)
9237 	return retval;
9238 
9239     /*
9240      * If there is a next window or a status line, we first try to delete the
9241      * lines at the bottom to avoid messing what is after the window.
9242      * If this fails and there are following windows, don't do anything to avoid
9243      * messing up those windows, better just redraw.
9244      */
9245     did_delete = FALSE;
9246 #ifdef FEAT_WINDOWS
9247     if (wp->w_next != NULL || wp->w_status_height)
9248     {
9249 	if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9250 				    line_count, (int)Rows, FALSE, NULL) == OK)
9251 	    did_delete = TRUE;
9252 	else if (wp->w_next)
9253 	    return FAIL;
9254     }
9255 #endif
9256     /*
9257      * if no lines deleted, blank the lines that will end up below the window
9258      */
9259     if (!did_delete)
9260     {
9261 #ifdef FEAT_WINDOWS
9262 	wp->w_redr_status = TRUE;
9263 #endif
9264 	redraw_cmdline = TRUE;
9265 	nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
9266 	lastrow = nextrow + line_count;
9267 	if (lastrow > Rows)
9268 	    lastrow = Rows;
9269 	screen_fill(nextrow - line_count, lastrow - line_count,
9270 		  W_WINCOL(wp), (int)W_ENDCOL(wp),
9271 		  ' ', ' ', 0);
9272     }
9273 
9274     if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
9275 								      == FAIL)
9276     {
9277 	    /* deletion will have messed up other windows */
9278 	if (did_delete)
9279 	{
9280 #ifdef FEAT_WINDOWS
9281 	    wp->w_redr_status = TRUE;
9282 #endif
9283 	    win_rest_invalid(W_NEXT(wp));
9284 	}
9285 	return FAIL;
9286     }
9287 
9288     return OK;
9289 }
9290 
9291 /*
9292  * delete "line_count" window lines at "row" in window "wp"
9293  * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9294  * If "mayclear" is TRUE the screen will be cleared if it is faster than
9295  * scrolling
9296  * Return OK for success, FAIL if the lines are not deleted.
9297  */
9298     int
9299 win_del_lines(
9300     win_T	*wp,
9301     int		row,
9302     int		line_count,
9303     int		invalid,
9304     int		mayclear)
9305 {
9306     int		retval;
9307 
9308     if (invalid)
9309 	wp->w_lines_valid = 0;
9310 
9311     if (line_count > wp->w_height - row)
9312 	line_count = wp->w_height - row;
9313 
9314     retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
9315     if (retval != MAYBE)
9316 	return retval;
9317 
9318     if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
9319 					      (int)Rows, FALSE, NULL) == FAIL)
9320 	return FAIL;
9321 
9322 #ifdef FEAT_WINDOWS
9323     /*
9324      * If there are windows or status lines below, try to put them at the
9325      * correct place. If we can't do that, they have to be redrawn.
9326      */
9327     if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9328     {
9329 	if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9330 					 line_count, (int)Rows, NULL) == FAIL)
9331 	{
9332 	    wp->w_redr_status = TRUE;
9333 	    win_rest_invalid(wp->w_next);
9334 	}
9335     }
9336     /*
9337      * If this is the last window and there is no status line, redraw the
9338      * command line later.
9339      */
9340     else
9341 #endif
9342 	redraw_cmdline = TRUE;
9343     return OK;
9344 }
9345 
9346 /*
9347  * Common code for win_ins_lines() and win_del_lines().
9348  * Returns OK or FAIL when the work has been done.
9349  * Returns MAYBE when not finished yet.
9350  */
9351     static int
9352 win_do_lines(
9353     win_T	*wp,
9354     int		row,
9355     int		line_count,
9356     int		mayclear,
9357     int		del)
9358 {
9359     int		retval;
9360 
9361     if (!redrawing() || line_count <= 0)
9362 	return FAIL;
9363 
9364     /* only a few lines left: redraw is faster */
9365     if (mayclear && Rows - line_count < 5
9366 #ifdef FEAT_WINDOWS
9367 	    && wp->w_width == Columns
9368 #endif
9369 	    )
9370     {
9371 	screenclear();	    /* will set wp->w_lines_valid to 0 */
9372 	return FAIL;
9373     }
9374 
9375     /*
9376      * Delete all remaining lines
9377      */
9378     if (row + line_count >= wp->w_height)
9379     {
9380 	screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
9381 		W_WINCOL(wp), (int)W_ENDCOL(wp),
9382 		' ', ' ', 0);
9383 	return OK;
9384     }
9385 
9386     /*
9387      * when scrolling, the message on the command line should be cleared,
9388      * otherwise it will stay there forever.
9389      */
9390     clear_cmdline = TRUE;
9391 
9392     /*
9393      * If the terminal can set a scroll region, use that.
9394      * Always do this in a vertically split window.  This will redraw from
9395      * ScreenLines[] when t_CV isn't defined.  That's faster than using
9396      * win_line().
9397      * Don't use a scroll region when we are going to redraw the text, writing
9398      * a character in the lower right corner of the scroll region may cause a
9399      * scroll-up .
9400      */
9401     if (scroll_region
9402 #ifdef FEAT_WINDOWS
9403 	    || W_WIDTH(wp) != Columns
9404 #endif
9405 	    )
9406     {
9407 #ifdef FEAT_WINDOWS
9408 	if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9409 #endif
9410 	    scroll_region_set(wp, row);
9411 	if (del)
9412 	    retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
9413 					       wp->w_height - row, FALSE, wp);
9414 	else
9415 	    retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
9416 						      wp->w_height - row, wp);
9417 #ifdef FEAT_WINDOWS
9418 	if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9419 #endif
9420 	    scroll_region_reset();
9421 	return retval;
9422     }
9423 
9424 #ifdef FEAT_WINDOWS
9425     if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9426 	return FAIL;
9427 #endif
9428 
9429     return MAYBE;
9430 }
9431 
9432 /*
9433  * window 'wp' and everything after it is messed up, mark it for redraw
9434  */
9435     static void
9436 win_rest_invalid(win_T *wp)
9437 {
9438 #ifdef FEAT_WINDOWS
9439     while (wp != NULL)
9440 #else
9441     if (wp != NULL)
9442 #endif
9443     {
9444 	redraw_win_later(wp, NOT_VALID);
9445 #ifdef FEAT_WINDOWS
9446 	wp->w_redr_status = TRUE;
9447 	wp = wp->w_next;
9448 #endif
9449     }
9450     redraw_cmdline = TRUE;
9451 }
9452 
9453 /*
9454  * The rest of the routines in this file perform screen manipulations. The
9455  * given operation is performed physically on the screen. The corresponding
9456  * change is also made to the internal screen image. In this way, the editor
9457  * anticipates the effect of editing changes on the appearance of the screen.
9458  * That way, when we call screenupdate a complete redraw isn't usually
9459  * necessary. Another advantage is that we can keep adding code to anticipate
9460  * screen changes, and in the meantime, everything still works.
9461  */
9462 
9463 /*
9464  * types for inserting or deleting lines
9465  */
9466 #define USE_T_CAL   1
9467 #define USE_T_CDL   2
9468 #define USE_T_AL    3
9469 #define USE_T_CE    4
9470 #define USE_T_DL    5
9471 #define USE_T_SR    6
9472 #define USE_NL	    7
9473 #define USE_T_CD    8
9474 #define USE_REDRAW  9
9475 
9476 /*
9477  * insert lines on the screen and update ScreenLines[]
9478  * 'end' is the line after the scrolled part. Normally it is Rows.
9479  * When scrolling region used 'off' is the offset from the top for the region.
9480  * 'row' and 'end' are relative to the start of the region.
9481  *
9482  * return FAIL for failure, OK for success.
9483  */
9484     int
9485 screen_ins_lines(
9486     int		off,
9487     int		row,
9488     int		line_count,
9489     int		end,
9490     win_T	*wp)	    /* NULL or window to use width from */
9491 {
9492     int		i;
9493     int		j;
9494     unsigned	temp;
9495     int		cursor_row;
9496     int		type;
9497     int		result_empty;
9498     int		can_ce = can_clear(T_CE);
9499 
9500     /*
9501      * FAIL if
9502      * - there is no valid screen
9503      * - the screen has to be redrawn completely
9504      * - the line count is less than one
9505      * - the line count is more than 'ttyscroll'
9506      */
9507     if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
9508 	return FAIL;
9509 
9510     /*
9511      * There are seven ways to insert lines:
9512      * 0. When in a vertically split window and t_CV isn't set, redraw the
9513      *    characters from ScreenLines[].
9514      * 1. Use T_CD (clear to end of display) if it exists and the result of
9515      *	  the insert is just empty lines
9516      * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9517      *	  present or line_count > 1. It looks better if we do all the inserts
9518      *	  at once.
9519      * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9520      *	  insert is just empty lines and T_CE is not present or line_count >
9521      *	  1.
9522      * 4. Use T_AL (insert line) if it exists.
9523      * 5. Use T_CE (erase line) if it exists and the result of the insert is
9524      *	  just empty lines.
9525      * 6. Use T_DL (delete line) if it exists and the result of the insert is
9526      *	  just empty lines.
9527      * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9528      *	  the 'da' flag is not set or we have clear line capability.
9529      * 8. redraw the characters from ScreenLines[].
9530      *
9531      * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9532      * the scrollbar for the window. It does have insert line, use that if it
9533      * exists.
9534      */
9535     result_empty = (row + line_count >= end);
9536 #ifdef FEAT_WINDOWS
9537     if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9538 	type = USE_REDRAW;
9539     else
9540 #endif
9541     if (can_clear(T_CD) && result_empty)
9542 	type = USE_T_CD;
9543     else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9544 	type = USE_T_CAL;
9545     else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9546 	type = USE_T_CDL;
9547     else if (*T_AL != NUL)
9548 	type = USE_T_AL;
9549     else if (can_ce && result_empty)
9550 	type = USE_T_CE;
9551     else if (*T_DL != NUL && result_empty)
9552 	type = USE_T_DL;
9553     else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9554 	type = USE_T_SR;
9555     else
9556 	return FAIL;
9557 
9558     /*
9559      * For clearing the lines screen_del_lines() is used. This will also take
9560      * care of t_db if necessary.
9561      */
9562     if (type == USE_T_CD || type == USE_T_CDL ||
9563 					 type == USE_T_CE || type == USE_T_DL)
9564 	return screen_del_lines(off, row, line_count, end, FALSE, wp);
9565 
9566     /*
9567      * If text is retained below the screen, first clear or delete as many
9568      * lines at the bottom of the window as are about to be inserted so that
9569      * the deleted lines won't later surface during a screen_del_lines.
9570      */
9571     if (*T_DB)
9572 	screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
9573 
9574 #ifdef FEAT_CLIPBOARD
9575     /* Remove a modeless selection when inserting lines halfway the screen
9576      * or not the full width of the screen. */
9577     if (off + row > 0
9578 # ifdef FEAT_WINDOWS
9579 	    || (wp != NULL && wp->w_width != Columns)
9580 # endif
9581        )
9582 	clip_clear_selection(&clip_star);
9583     else
9584 	clip_scroll_selection(-line_count);
9585 #endif
9586 
9587 #ifdef FEAT_GUI
9588     /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9589      * scrolling is actually carried out. */
9590     gui_dont_update_cursor();
9591 #endif
9592 
9593     if (*T_CCS != NUL)	   /* cursor relative to region */
9594 	cursor_row = row;
9595     else
9596 	cursor_row = row + off;
9597 
9598     /*
9599      * Shift LineOffset[] line_count down to reflect the inserted lines.
9600      * Clear the inserted lines in ScreenLines[].
9601      */
9602     row += off;
9603     end += off;
9604     for (i = 0; i < line_count; ++i)
9605     {
9606 #ifdef FEAT_WINDOWS
9607 	if (wp != NULL && wp->w_width != Columns)
9608 	{
9609 	    /* need to copy part of a line */
9610 	    j = end - 1 - i;
9611 	    while ((j -= line_count) >= row)
9612 		linecopy(j + line_count, j, wp);
9613 	    j += line_count;
9614 	    if (can_clear((char_u *)" "))
9615 		lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9616 	    else
9617 		lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9618 	    LineWraps[j] = FALSE;
9619 	}
9620 	else
9621 #endif
9622 	{
9623 	    j = end - 1 - i;
9624 	    temp = LineOffset[j];
9625 	    while ((j -= line_count) >= row)
9626 	    {
9627 		LineOffset[j + line_count] = LineOffset[j];
9628 		LineWraps[j + line_count] = LineWraps[j];
9629 	    }
9630 	    LineOffset[j + line_count] = temp;
9631 	    LineWraps[j + line_count] = FALSE;
9632 	    if (can_clear((char_u *)" "))
9633 		lineclear(temp, (int)Columns);
9634 	    else
9635 		lineinvalid(temp, (int)Columns);
9636 	}
9637     }
9638 
9639     screen_stop_highlight();
9640     windgoto(cursor_row, 0);
9641 
9642 #ifdef FEAT_WINDOWS
9643     /* redraw the characters */
9644     if (type == USE_REDRAW)
9645 	redraw_block(row, end, wp);
9646     else
9647 #endif
9648 	if (type == USE_T_CAL)
9649     {
9650 	term_append_lines(line_count);
9651 	screen_start();		/* don't know where cursor is now */
9652     }
9653     else
9654     {
9655 	for (i = 0; i < line_count; i++)
9656 	{
9657 	    if (type == USE_T_AL)
9658 	    {
9659 		if (i && cursor_row != 0)
9660 		    windgoto(cursor_row, 0);
9661 		out_str(T_AL);
9662 	    }
9663 	    else  /* type == USE_T_SR */
9664 		out_str(T_SR);
9665 	    screen_start();	    /* don't know where cursor is now */
9666 	}
9667     }
9668 
9669     /*
9670      * With scroll-reverse and 'da' flag set we need to clear the lines that
9671      * have been scrolled down into the region.
9672      */
9673     if (type == USE_T_SR && *T_DA)
9674     {
9675 	for (i = 0; i < line_count; ++i)
9676 	{
9677 	    windgoto(off + i, 0);
9678 	    out_str(T_CE);
9679 	    screen_start();	    /* don't know where cursor is now */
9680 	}
9681     }
9682 
9683 #ifdef FEAT_GUI
9684     gui_can_update_cursor();
9685     if (gui.in_use)
9686 	out_flush();	/* always flush after a scroll */
9687 #endif
9688     return OK;
9689 }
9690 
9691 /*
9692  * delete lines on the screen and update ScreenLines[]
9693  * 'end' is the line after the scrolled part. Normally it is Rows.
9694  * When scrolling region used 'off' is the offset from the top for the region.
9695  * 'row' and 'end' are relative to the start of the region.
9696  *
9697  * Return OK for success, FAIL if the lines are not deleted.
9698  */
9699     int
9700 screen_del_lines(
9701     int		off,
9702     int		row,
9703     int		line_count,
9704     int		end,
9705     int		force,		/* even when line_count > p_ttyscroll */
9706     win_T	*wp UNUSED)	/* NULL or window to use width from */
9707 {
9708     int		j;
9709     int		i;
9710     unsigned	temp;
9711     int		cursor_row;
9712     int		cursor_end;
9713     int		result_empty;	/* result is empty until end of region */
9714     int		can_delete;	/* deleting line codes can be used */
9715     int		type;
9716 
9717     /*
9718      * FAIL if
9719      * - there is no valid screen
9720      * - the screen has to be redrawn completely
9721      * - the line count is less than one
9722      * - the line count is more than 'ttyscroll'
9723      */
9724     if (!screen_valid(TRUE) || line_count <= 0 ||
9725 					 (!force && line_count > p_ttyscroll))
9726 	return FAIL;
9727 
9728     /*
9729      * Check if the rest of the current region will become empty.
9730      */
9731     result_empty = row + line_count >= end;
9732 
9733     /*
9734      * We can delete lines only when 'db' flag not set or when 'ce' option
9735      * available.
9736      */
9737     can_delete = (*T_DB == NUL || can_clear(T_CE));
9738 
9739     /*
9740      * There are six ways to delete lines:
9741      * 0. When in a vertically split window and t_CV isn't set, redraw the
9742      *    characters from ScreenLines[].
9743      * 1. Use T_CD if it exists and the result is empty.
9744      * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9745      * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9746      *	  none of the other ways work.
9747      * 4. Use T_CE (erase line) if the result is empty.
9748      * 5. Use T_DL (delete line) if it exists.
9749      * 6. redraw the characters from ScreenLines[].
9750      */
9751 #ifdef FEAT_WINDOWS
9752     if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9753 	type = USE_REDRAW;
9754     else
9755 #endif
9756     if (can_clear(T_CD) && result_empty)
9757 	type = USE_T_CD;
9758 #if defined(__BEOS__) && defined(BEOS_DR8)
9759     /*
9760      * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9761      * its internal termcap... this works okay for tests which test *T_DB !=
9762      * NUL.  It has the disadvantage that the user cannot use any :set t_*
9763      * command to get T_DB (back) to empty_option, only :set term=... will do
9764      * the trick...
9765      * Anyway, this hack will hopefully go away with the next OS release.
9766      * (Olaf Seibert)
9767      */
9768     else if (row == 0 && T_DB == empty_option
9769 					&& (line_count == 1 || *T_CDL == NUL))
9770 #else
9771     else if (row == 0 && (
9772 #ifndef AMIGA
9773 	/* On the Amiga, somehow '\n' on the last line doesn't always scroll
9774 	 * up, so use delete-line command */
9775 			    line_count == 1 ||
9776 #endif
9777 						*T_CDL == NUL))
9778 #endif
9779 	type = USE_NL;
9780     else if (*T_CDL != NUL && line_count > 1 && can_delete)
9781 	type = USE_T_CDL;
9782     else if (can_clear(T_CE) && result_empty
9783 #ifdef FEAT_WINDOWS
9784 	    && (wp == NULL || wp->w_width == Columns)
9785 #endif
9786 	    )
9787 	type = USE_T_CE;
9788     else if (*T_DL != NUL && can_delete)
9789 	type = USE_T_DL;
9790     else if (*T_CDL != NUL && can_delete)
9791 	type = USE_T_CDL;
9792     else
9793 	return FAIL;
9794 
9795 #ifdef FEAT_CLIPBOARD
9796     /* Remove a modeless selection when deleting lines halfway the screen or
9797      * not the full width of the screen. */
9798     if (off + row > 0
9799 # ifdef FEAT_WINDOWS
9800 	    || (wp != NULL && wp->w_width != Columns)
9801 # endif
9802        )
9803 	clip_clear_selection(&clip_star);
9804     else
9805 	clip_scroll_selection(line_count);
9806 #endif
9807 
9808 #ifdef FEAT_GUI
9809     /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9810      * scrolling is actually carried out. */
9811     gui_dont_update_cursor();
9812 #endif
9813 
9814     if (*T_CCS != NUL)	    /* cursor relative to region */
9815     {
9816 	cursor_row = row;
9817 	cursor_end = end;
9818     }
9819     else
9820     {
9821 	cursor_row = row + off;
9822 	cursor_end = end + off;
9823     }
9824 
9825     /*
9826      * Now shift LineOffset[] line_count up to reflect the deleted lines.
9827      * Clear the inserted lines in ScreenLines[].
9828      */
9829     row += off;
9830     end += off;
9831     for (i = 0; i < line_count; ++i)
9832     {
9833 #ifdef FEAT_WINDOWS
9834 	if (wp != NULL && wp->w_width != Columns)
9835 	{
9836 	    /* need to copy part of a line */
9837 	    j = row + i;
9838 	    while ((j += line_count) <= end - 1)
9839 		linecopy(j - line_count, j, wp);
9840 	    j -= line_count;
9841 	    if (can_clear((char_u *)" "))
9842 		lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9843 	    else
9844 		lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9845 	    LineWraps[j] = FALSE;
9846 	}
9847 	else
9848 #endif
9849 	{
9850 	    /* whole width, moving the line pointers is faster */
9851 	    j = row + i;
9852 	    temp = LineOffset[j];
9853 	    while ((j += line_count) <= end - 1)
9854 	    {
9855 		LineOffset[j - line_count] = LineOffset[j];
9856 		LineWraps[j - line_count] = LineWraps[j];
9857 	    }
9858 	    LineOffset[j - line_count] = temp;
9859 	    LineWraps[j - line_count] = FALSE;
9860 	    if (can_clear((char_u *)" "))
9861 		lineclear(temp, (int)Columns);
9862 	    else
9863 		lineinvalid(temp, (int)Columns);
9864 	}
9865     }
9866 
9867     screen_stop_highlight();
9868 
9869 #ifdef FEAT_WINDOWS
9870     /* redraw the characters */
9871     if (type == USE_REDRAW)
9872 	redraw_block(row, end, wp);
9873     else
9874 #endif
9875 	if (type == USE_T_CD)	/* delete the lines */
9876     {
9877 	windgoto(cursor_row, 0);
9878 	out_str(T_CD);
9879 	screen_start();			/* don't know where cursor is now */
9880     }
9881     else if (type == USE_T_CDL)
9882     {
9883 	windgoto(cursor_row, 0);
9884 	term_delete_lines(line_count);
9885 	screen_start();			/* don't know where cursor is now */
9886     }
9887     /*
9888      * Deleting lines at top of the screen or scroll region: Just scroll
9889      * the whole screen (scroll region) up by outputting newlines on the
9890      * last line.
9891      */
9892     else if (type == USE_NL)
9893     {
9894 	windgoto(cursor_end - 1, 0);
9895 	for (i = line_count; --i >= 0; )
9896 	    out_char('\n');		/* cursor will remain on same line */
9897     }
9898     else
9899     {
9900 	for (i = line_count; --i >= 0; )
9901 	{
9902 	    if (type == USE_T_DL)
9903 	    {
9904 		windgoto(cursor_row, 0);
9905 		out_str(T_DL);		/* delete a line */
9906 	    }
9907 	    else /* type == USE_T_CE */
9908 	    {
9909 		windgoto(cursor_row + i, 0);
9910 		out_str(T_CE);		/* erase a line */
9911 	    }
9912 	    screen_start();		/* don't know where cursor is now */
9913 	}
9914     }
9915 
9916     /*
9917      * If the 'db' flag is set, we need to clear the lines that have been
9918      * scrolled up at the bottom of the region.
9919      */
9920     if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
9921     {
9922 	for (i = line_count; i > 0; --i)
9923 	{
9924 	    windgoto(cursor_end - i, 0);
9925 	    out_str(T_CE);		/* erase a line */
9926 	    screen_start();		/* don't know where cursor is now */
9927 	}
9928     }
9929 
9930 #ifdef FEAT_GUI
9931     gui_can_update_cursor();
9932     if (gui.in_use)
9933 	out_flush();	/* always flush after a scroll */
9934 #endif
9935 
9936     return OK;
9937 }
9938 
9939 /*
9940  * show the current mode and ruler
9941  *
9942  * If clear_cmdline is TRUE, clear the rest of the cmdline.
9943  * If clear_cmdline is FALSE there may be a message there that needs to be
9944  * cleared only if a mode is shown.
9945  * Return the length of the message (0 if no message).
9946  */
9947     int
9948 showmode(void)
9949 {
9950     int		need_clear;
9951     int		length = 0;
9952     int		do_mode;
9953     int		attr;
9954     int		nwr_save;
9955 #ifdef FEAT_INS_EXPAND
9956     int		sub_attr;
9957 #endif
9958 
9959     do_mode = ((p_smd && msg_silent == 0)
9960 	    && ((State & INSERT)
9961 		|| restart_edit
9962 		|| VIsual_active));
9963     if (do_mode || Recording)
9964     {
9965 	/*
9966 	 * Don't show mode right now, when not redrawing or inside a mapping.
9967 	 * Call char_avail() only when we are going to show something, because
9968 	 * it takes a bit of time.
9969 	 */
9970 	if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
9971 	{
9972 	    redraw_cmdline = TRUE;		/* show mode later */
9973 	    return 0;
9974 	}
9975 
9976 	nwr_save = need_wait_return;
9977 
9978 	/* wait a bit before overwriting an important message */
9979 	check_for_delay(FALSE);
9980 
9981 	/* if the cmdline is more than one line high, erase top lines */
9982 	need_clear = clear_cmdline;
9983 	if (clear_cmdline && cmdline_row < Rows - 1)
9984 	    msg_clr_cmdline();			/* will reset clear_cmdline */
9985 
9986 	/* Position on the last line in the window, column 0 */
9987 	msg_pos_mode();
9988 	cursor_off();
9989 	attr = hl_attr(HLF_CM);			/* Highlight mode */
9990 	if (do_mode)
9991 	{
9992 	    MSG_PUTS_ATTR("--", attr);
9993 #if defined(FEAT_XIM)
9994 	    if (
9995 # ifdef FEAT_GUI_GTK
9996 		    preedit_get_status()
9997 # else
9998 		    im_get_status()
9999 # endif
10000 	       )
10001 # ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
10002 		MSG_PUTS_ATTR(" IM", attr);
10003 # else
10004 		MSG_PUTS_ATTR(" XIM", attr);
10005 # endif
10006 #endif
10007 #if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10008 	    if (gui.in_use)
10009 	    {
10010 		if (hangul_input_state_get())
10011 		{
10012 		    /* HANGUL */
10013 		    if (enc_utf8)
10014 			MSG_PUTS_ATTR(" \355\225\234\352\270\200", attr);
10015 		    else
10016 			MSG_PUTS_ATTR(" \307\321\261\333", attr);
10017 		}
10018 	    }
10019 #endif
10020 #ifdef FEAT_INS_EXPAND
10021 	    /* CTRL-X in Insert mode */
10022 	    if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
10023 	    {
10024 		/* These messages can get long, avoid a wrap in a narrow
10025 		 * window.  Prefer showing edit_submode_extra. */
10026 		length = (Rows - msg_row) * Columns - 3;
10027 		if (edit_submode_extra != NULL)
10028 		    length -= vim_strsize(edit_submode_extra);
10029 		if (length > 0)
10030 		{
10031 		    if (edit_submode_pre != NULL)
10032 			length -= vim_strsize(edit_submode_pre);
10033 		    if (length - vim_strsize(edit_submode) > 0)
10034 		    {
10035 			if (edit_submode_pre != NULL)
10036 			    msg_puts_attr(edit_submode_pre, attr);
10037 			msg_puts_attr(edit_submode, attr);
10038 		    }
10039 		    if (edit_submode_extra != NULL)
10040 		    {
10041 			MSG_PUTS_ATTR(" ", attr);  /* add a space in between */
10042 			if ((int)edit_submode_highl < (int)HLF_COUNT)
10043 			    sub_attr = hl_attr(edit_submode_highl);
10044 			else
10045 			    sub_attr = attr;
10046 			msg_puts_attr(edit_submode_extra, sub_attr);
10047 		    }
10048 		}
10049 		length = 0;
10050 	    }
10051 	    else
10052 #endif
10053 	    {
10054 #ifdef FEAT_VREPLACE
10055 		if (State & VREPLACE_FLAG)
10056 		    MSG_PUTS_ATTR(_(" VREPLACE"), attr);
10057 		else
10058 #endif
10059 		    if (State & REPLACE_FLAG)
10060 		    MSG_PUTS_ATTR(_(" REPLACE"), attr);
10061 		else if (State & INSERT)
10062 		{
10063 #ifdef FEAT_RIGHTLEFT
10064 		    if (p_ri)
10065 			MSG_PUTS_ATTR(_(" REVERSE"), attr);
10066 #endif
10067 		    MSG_PUTS_ATTR(_(" INSERT"), attr);
10068 		}
10069 		else if (restart_edit == 'I')
10070 		    MSG_PUTS_ATTR(_(" (insert)"), attr);
10071 		else if (restart_edit == 'R')
10072 		    MSG_PUTS_ATTR(_(" (replace)"), attr);
10073 		else if (restart_edit == 'V')
10074 		    MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10075 #ifdef FEAT_RIGHTLEFT
10076 		if (p_hkmap)
10077 		    MSG_PUTS_ATTR(_(" Hebrew"), attr);
10078 # ifdef FEAT_FKMAP
10079 		if (p_fkmap)
10080 		    MSG_PUTS_ATTR(farsi_text_5, attr);
10081 # endif
10082 #endif
10083 #ifdef FEAT_KEYMAP
10084 		if (State & LANGMAP)
10085 		{
10086 # ifdef FEAT_ARABIC
10087 		    if (curwin->w_p_arab)
10088 			MSG_PUTS_ATTR(_(" Arabic"), attr);
10089 		    else
10090 # endif
10091 			MSG_PUTS_ATTR(_(" (lang)"), attr);
10092 		}
10093 #endif
10094 		if ((State & INSERT) && p_paste)
10095 		    MSG_PUTS_ATTR(_(" (paste)"), attr);
10096 
10097 		if (VIsual_active)
10098 		{
10099 		    char *p;
10100 
10101 		    /* Don't concatenate separate words to avoid translation
10102 		     * problems. */
10103 		    switch ((VIsual_select ? 4 : 0)
10104 			    + (VIsual_mode == Ctrl_V) * 2
10105 			    + (VIsual_mode == 'V'))
10106 		    {
10107 			case 0:	p = N_(" VISUAL"); break;
10108 			case 1: p = N_(" VISUAL LINE"); break;
10109 			case 2: p = N_(" VISUAL BLOCK"); break;
10110 			case 4: p = N_(" SELECT"); break;
10111 			case 5: p = N_(" SELECT LINE"); break;
10112 			default: p = N_(" SELECT BLOCK"); break;
10113 		    }
10114 		    MSG_PUTS_ATTR(_(p), attr);
10115 		}
10116 		MSG_PUTS_ATTR(" --", attr);
10117 	    }
10118 
10119 	    need_clear = TRUE;
10120 	}
10121 	if (Recording
10122 #ifdef FEAT_INS_EXPAND
10123 		&& edit_submode == NULL	    /* otherwise it gets too long */
10124 #endif
10125 		)
10126 	{
10127 	    recording_mode(attr);
10128 	    need_clear = TRUE;
10129 	}
10130 
10131 	mode_displayed = TRUE;
10132 	if (need_clear || clear_cmdline)
10133 	    msg_clr_eos();
10134 	msg_didout = FALSE;		/* overwrite this message */
10135 	length = msg_col;
10136 	msg_col = 0;
10137 	need_wait_return = nwr_save;	/* never ask for hit-return for this */
10138     }
10139     else if (clear_cmdline && msg_silent == 0)
10140 	/* Clear the whole command line.  Will reset "clear_cmdline". */
10141 	msg_clr_cmdline();
10142 
10143 #ifdef FEAT_CMDL_INFO
10144     /* In Visual mode the size of the selected area must be redrawn. */
10145     if (VIsual_active)
10146 	clear_showcmd();
10147 
10148     /* If the last window has no status line, the ruler is after the mode
10149      * message and must be redrawn */
10150     if (redrawing()
10151 # ifdef FEAT_WINDOWS
10152 	    && lastwin->w_status_height == 0
10153 # endif
10154        )
10155 	win_redr_ruler(lastwin, TRUE);
10156 #endif
10157     redraw_cmdline = FALSE;
10158     clear_cmdline = FALSE;
10159 
10160     return length;
10161 }
10162 
10163 /*
10164  * Position for a mode message.
10165  */
10166     static void
10167 msg_pos_mode(void)
10168 {
10169     msg_col = 0;
10170     msg_row = Rows - 1;
10171 }
10172 
10173 /*
10174  * Delete mode message.  Used when ESC is typed which is expected to end
10175  * Insert mode (but Insert mode didn't end yet!).
10176  * Caller should check "mode_displayed".
10177  */
10178     void
10179 unshowmode(int force)
10180 {
10181     /*
10182      * Don't delete it right now, when not redrawing or inside a mapping.
10183      */
10184     if (!redrawing() || (!force && char_avail() && !KeyTyped))
10185 	redraw_cmdline = TRUE;		/* delete mode later */
10186     else
10187     {
10188 	msg_pos_mode();
10189 	if (Recording)
10190 	    recording_mode(hl_attr(HLF_CM));
10191 	msg_clr_eos();
10192     }
10193 }
10194 
10195     static void
10196 recording_mode(int attr)
10197 {
10198     MSG_PUTS_ATTR(_("recording"), attr);
10199     if (!shortmess(SHM_RECORDING))
10200     {
10201 	char_u s[4];
10202 	sprintf((char *)s, " @%c", Recording);
10203 	MSG_PUTS_ATTR(s, attr);
10204     }
10205 }
10206 
10207 #if defined(FEAT_WINDOWS)
10208 /*
10209  * Draw the tab pages line at the top of the Vim window.
10210  */
10211     static void
10212 draw_tabline(void)
10213 {
10214     int		tabcount = 0;
10215     tabpage_T	*tp;
10216     int		tabwidth;
10217     int		col = 0;
10218     int		scol = 0;
10219     int		attr;
10220     win_T	*wp;
10221     win_T	*cwp;
10222     int		wincount;
10223     int		modified;
10224     int		c;
10225     int		len;
10226     int		attr_sel = hl_attr(HLF_TPS);
10227     int		attr_nosel = hl_attr(HLF_TP);
10228     int		attr_fill = hl_attr(HLF_TPF);
10229     char_u	*p;
10230     int		room;
10231     int		use_sep_chars = (t_colors < 8
10232 #ifdef FEAT_GUI
10233 					    && !gui.in_use
10234 #endif
10235 					    );
10236 
10237     redraw_tabline = FALSE;
10238 
10239 #ifdef FEAT_GUI_TABLINE
10240     /* Take care of a GUI tabline. */
10241     if (gui_use_tabline())
10242     {
10243 	gui_update_tabline();
10244 	return;
10245     }
10246 #endif
10247 
10248     if (tabline_height() < 1)
10249 	return;
10250 
10251 #if defined(FEAT_STL_OPT)
10252 
10253     /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10254     for (scol = 0; scol < Columns; ++scol)
10255 	TabPageIdxs[scol] = 0;
10256 
10257     /* Use the 'tabline' option if it's set. */
10258     if (*p_tal != NUL)
10259     {
10260 	int	save_called_emsg = called_emsg;
10261 
10262 	/* Check for an error.  If there is one we would loop in redrawing the
10263 	 * screen.  Avoid that by making 'tabline' empty. */
10264 	called_emsg = FALSE;
10265 	win_redr_custom(NULL, FALSE);
10266 	if (called_emsg)
10267 	    set_string_option_direct((char_u *)"tabline", -1,
10268 					   (char_u *)"", OPT_FREE, SID_ERROR);
10269 	called_emsg |= save_called_emsg;
10270     }
10271     else
10272 #endif
10273     {
10274 	for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
10275 	    ++tabcount;
10276 
10277 	tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10278 	if (tabwidth < 6)
10279 	    tabwidth = 6;
10280 
10281 	attr = attr_nosel;
10282 	tabcount = 0;
10283 	scol = 0;
10284 	for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10285 							     tp = tp->tp_next)
10286 	{
10287 	    scol = col;
10288 
10289 	    if (tp->tp_topframe == topframe)
10290 		attr = attr_sel;
10291 	    if (use_sep_chars && col > 0)
10292 		screen_putchar('|', 0, col++, attr);
10293 
10294 	    if (tp->tp_topframe != topframe)
10295 		attr = attr_nosel;
10296 
10297 	    screen_putchar(' ', 0, col++, attr);
10298 
10299 	    if (tp == curtab)
10300 	    {
10301 		cwp = curwin;
10302 		wp = firstwin;
10303 	    }
10304 	    else
10305 	    {
10306 		cwp = tp->tp_curwin;
10307 		wp = tp->tp_firstwin;
10308 	    }
10309 
10310 	    modified = FALSE;
10311 	    for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10312 		if (bufIsChanged(wp->w_buffer))
10313 		    modified = TRUE;
10314 	    if (modified || wincount > 1)
10315 	    {
10316 		if (wincount > 1)
10317 		{
10318 		    vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
10319 		    len = (int)STRLEN(NameBuff);
10320 		    if (col + len >= Columns - 3)
10321 			break;
10322 		    screen_puts_len(NameBuff, len, 0, col,
10323 #if defined(FEAT_SYN_HL)
10324 					 hl_combine_attr(attr, hl_attr(HLF_T))
10325 #else
10326 					 attr
10327 #endif
10328 					       );
10329 		    col += len;
10330 		}
10331 		if (modified)
10332 		    screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10333 		screen_putchar(' ', 0, col++, attr);
10334 	    }
10335 
10336 	    room = scol - col + tabwidth - 1;
10337 	    if (room > 0)
10338 	    {
10339 		/* Get buffer name in NameBuff[] */
10340 		get_trans_bufname(cwp->w_buffer);
10341 		shorten_dir(NameBuff);
10342 		len = vim_strsize(NameBuff);
10343 		p = NameBuff;
10344 #ifdef FEAT_MBYTE
10345 		if (has_mbyte)
10346 		    while (len > room)
10347 		    {
10348 			len -= ptr2cells(p);
10349 			mb_ptr_adv(p);
10350 		    }
10351 		else
10352 #endif
10353 		    if (len > room)
10354 		{
10355 		    p += len - room;
10356 		    len = room;
10357 		}
10358 		if (len > Columns - col - 1)
10359 		    len = Columns - col - 1;
10360 
10361 		screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
10362 		col += len;
10363 	    }
10364 	    screen_putchar(' ', 0, col++, attr);
10365 
10366 	    /* Store the tab page number in TabPageIdxs[], so that
10367 	     * jump_to_mouse() knows where each one is. */
10368 	    ++tabcount;
10369 	    while (scol < col)
10370 		TabPageIdxs[scol++] = tabcount;
10371 	}
10372 
10373 	if (use_sep_chars)
10374 	    c = '_';
10375 	else
10376 	    c = ' ';
10377 	screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
10378 
10379 	/* Put an "X" for closing the current tab if there are several. */
10380 	if (first_tabpage->tp_next != NULL)
10381 	{
10382 	    screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10383 	    TabPageIdxs[Columns - 1] = -999;
10384 	}
10385     }
10386 
10387     /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10388      * set. */
10389     redraw_tabline = FALSE;
10390 }
10391 
10392 /*
10393  * Get buffer name for "buf" into NameBuff[].
10394  * Takes care of special buffer names and translates special characters.
10395  */
10396     void
10397 get_trans_bufname(buf_T *buf)
10398 {
10399     if (buf_spname(buf) != NULL)
10400 	vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
10401     else
10402 	home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10403     trans_characters(NameBuff, MAXPATHL);
10404 }
10405 #endif
10406 
10407 #if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
10408 /*
10409  * Get the character to use in a status line.  Get its attributes in "*attr".
10410  */
10411     static int
10412 fillchar_status(int *attr, int is_curwin)
10413 {
10414     int fill;
10415     if (is_curwin)
10416     {
10417 	*attr = hl_attr(HLF_S);
10418 	fill = fill_stl;
10419     }
10420     else
10421     {
10422 	*attr = hl_attr(HLF_SNC);
10423 	fill = fill_stlnc;
10424     }
10425     /* Use fill when there is highlighting, and highlighting of current
10426      * window differs, or the fillchars differ, or this is not the
10427      * current window */
10428     if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
10429 			|| !is_curwin || firstwin == lastwin)
10430 		    || (fill_stl != fill_stlnc)))
10431 	return fill;
10432     if (is_curwin)
10433 	return '^';
10434     return '=';
10435 }
10436 #endif
10437 
10438 #ifdef FEAT_WINDOWS
10439 /*
10440  * Get the character to use in a separator between vertically split windows.
10441  * Get its attributes in "*attr".
10442  */
10443     static int
10444 fillchar_vsep(int *attr)
10445 {
10446     *attr = hl_attr(HLF_C);
10447     if (*attr == 0 && fill_vert == ' ')
10448 	return '|';
10449     else
10450 	return fill_vert;
10451 }
10452 #endif
10453 
10454 /*
10455  * Return TRUE if redrawing should currently be done.
10456  */
10457     int
10458 redrawing(void)
10459 {
10460     return (!RedrawingDisabled
10461 		       && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
10462 }
10463 
10464 /*
10465  * Return TRUE if printing messages should currently be done.
10466  */
10467     int
10468 messaging(void)
10469 {
10470     return (!(p_lz && char_avail() && !KeyTyped));
10471 }
10472 
10473 /*
10474  * Show current status info in ruler and various other places
10475  * If always is FALSE, only show ruler if position has changed.
10476  */
10477     void
10478 showruler(int always)
10479 {
10480     if (!always && !redrawing())
10481 	return;
10482 #ifdef FEAT_INS_EXPAND
10483     if (pum_visible())
10484     {
10485 # ifdef FEAT_WINDOWS
10486 	/* Don't redraw right now, do it later. */
10487 	curwin->w_redr_status = TRUE;
10488 # endif
10489 	return;
10490     }
10491 #endif
10492 #if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
10493     if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
10494     {
10495 	redraw_custom_statusline(curwin);
10496     }
10497     else
10498 #endif
10499 #ifdef FEAT_CMDL_INFO
10500 	win_redr_ruler(curwin, always);
10501 #endif
10502 
10503 #ifdef FEAT_TITLE
10504     if (need_maketitle
10505 # ifdef FEAT_STL_OPT
10506 	    || (p_icon && (stl_syntax & STL_IN_ICON))
10507 	    || (p_title && (stl_syntax & STL_IN_TITLE))
10508 # endif
10509        )
10510 	maketitle();
10511 #endif
10512 #ifdef FEAT_WINDOWS
10513     /* Redraw the tab pages line if needed. */
10514     if (redraw_tabline)
10515 	draw_tabline();
10516 #endif
10517 }
10518 
10519 #ifdef FEAT_CMDL_INFO
10520     static void
10521 win_redr_ruler(win_T *wp, int always)
10522 {
10523 #define RULER_BUF_LEN 70
10524     char_u	buffer[RULER_BUF_LEN];
10525     int		row;
10526     int		fillchar;
10527     int		attr;
10528     int		empty_line = FALSE;
10529     colnr_T	virtcol;
10530     int		i;
10531     size_t	len;
10532     int		o;
10533 #ifdef FEAT_WINDOWS
10534     int		this_ru_col;
10535     int		off = 0;
10536     int		width = Columns;
10537 # define WITH_OFF(x) x
10538 # define WITH_WIDTH(x) x
10539 #else
10540 # define WITH_OFF(x) 0
10541 # define WITH_WIDTH(x) Columns
10542 # define this_ru_col ru_col
10543 #endif
10544 
10545     /* If 'ruler' off or redrawing disabled, don't do anything */
10546     if (!p_ru)
10547 	return;
10548 
10549     /*
10550      * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10551      * after deleting lines, before cursor.lnum is corrected.
10552      */
10553     if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10554 	return;
10555 
10556 #ifdef FEAT_INS_EXPAND
10557     /* Don't draw the ruler while doing insert-completion, it might overwrite
10558      * the (long) mode message. */
10559 # ifdef FEAT_WINDOWS
10560     if (wp == lastwin && lastwin->w_status_height == 0)
10561 # endif
10562 	if (edit_submode != NULL)
10563 	    return;
10564     /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10565     if (pum_visible())
10566 	return;
10567 #endif
10568 
10569 #ifdef FEAT_STL_OPT
10570     if (*p_ruf)
10571     {
10572 	int	save_called_emsg = called_emsg;
10573 
10574 	called_emsg = FALSE;
10575 	win_redr_custom(wp, TRUE);
10576 	if (called_emsg)
10577 	    set_string_option_direct((char_u *)"rulerformat", -1,
10578 					   (char_u *)"", OPT_FREE, SID_ERROR);
10579 	called_emsg |= save_called_emsg;
10580 	return;
10581     }
10582 #endif
10583 
10584     /*
10585      * Check if not in Insert mode and the line is empty (will show "0-1").
10586      */
10587     if (!(State & INSERT)
10588 		&& *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10589 	empty_line = TRUE;
10590 
10591     /*
10592      * Only draw the ruler when something changed.
10593      */
10594     validate_virtcol_win(wp);
10595     if (       redraw_cmdline
10596 	    || always
10597 	    || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10598 	    || wp->w_cursor.col != wp->w_ru_cursor.col
10599 	    || wp->w_virtcol != wp->w_ru_virtcol
10600 #ifdef FEAT_VIRTUALEDIT
10601 	    || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10602 #endif
10603 	    || wp->w_topline != wp->w_ru_topline
10604 	    || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10605 #ifdef FEAT_DIFF
10606 	    || wp->w_topfill != wp->w_ru_topfill
10607 #endif
10608 	    || empty_line != wp->w_ru_empty)
10609     {
10610 	cursor_off();
10611 #ifdef FEAT_WINDOWS
10612 	if (wp->w_status_height)
10613 	{
10614 	    row = W_WINROW(wp) + wp->w_height;
10615 	    fillchar = fillchar_status(&attr, wp == curwin);
10616 	    off = W_WINCOL(wp);
10617 	    width = W_WIDTH(wp);
10618 	}
10619 	else
10620 #endif
10621 	{
10622 	    row = Rows - 1;
10623 	    fillchar = ' ';
10624 	    attr = 0;
10625 #ifdef FEAT_WINDOWS
10626 	    width = Columns;
10627 	    off = 0;
10628 #endif
10629 	}
10630 
10631 	/* In list mode virtcol needs to be recomputed */
10632 	virtcol = wp->w_virtcol;
10633 	if (wp->w_p_list && lcs_tab1 == NUL)
10634 	{
10635 	    wp->w_p_list = FALSE;
10636 	    getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10637 	    wp->w_p_list = TRUE;
10638 	}
10639 
10640 	/*
10641 	 * Some sprintfs return the length, some return a pointer.
10642 	 * To avoid portability problems we use strlen() here.
10643 	 */
10644 	vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
10645 		(wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10646 		    ? 0L
10647 		    : (long)(wp->w_cursor.lnum));
10648 	len = STRLEN(buffer);
10649 	col_print(buffer + len, RULER_BUF_LEN - len,
10650 			empty_line ? 0 : (int)wp->w_cursor.col + 1,
10651 			(int)virtcol + 1);
10652 
10653 	/*
10654 	 * Add a "50%" if there is room for it.
10655 	 * On the last line, don't print in the last column (scrolls the
10656 	 * screen up on some terminals).
10657 	 */
10658 	i = (int)STRLEN(buffer);
10659 	get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
10660 	o = i + vim_strsize(buffer + i + 1);
10661 #ifdef FEAT_WINDOWS
10662 	if (wp->w_status_height == 0)	/* can't use last char of screen */
10663 #endif
10664 	    ++o;
10665 #ifdef FEAT_WINDOWS
10666 	this_ru_col = ru_col - (Columns - width);
10667 	if (this_ru_col < 0)
10668 	    this_ru_col = 0;
10669 #endif
10670 	/* Never use more than half the window/screen width, leave the other
10671 	 * half for the filename. */
10672 	if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
10673 	    this_ru_col = (WITH_WIDTH(width) + 1) / 2;
10674 	if (this_ru_col + o < WITH_WIDTH(width))
10675 	{
10676 	    /* need at least 3 chars left for get_rel_pos() + NUL */
10677 	    while (this_ru_col + o < WITH_WIDTH(width) && RULER_BUF_LEN > i + 4)
10678 	    {
10679 #ifdef FEAT_MBYTE
10680 		if (has_mbyte)
10681 		    i += (*mb_char2bytes)(fillchar, buffer + i);
10682 		else
10683 #endif
10684 		    buffer[i++] = fillchar;
10685 		++o;
10686 	    }
10687 	    get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
10688 	}
10689 	/* Truncate at window boundary. */
10690 #ifdef FEAT_MBYTE
10691 	if (has_mbyte)
10692 	{
10693 	    o = 0;
10694 	    for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
10695 	    {
10696 		o += (*mb_ptr2cells)(buffer + i);
10697 		if (this_ru_col + o > WITH_WIDTH(width))
10698 		{
10699 		    buffer[i] = NUL;
10700 		    break;
10701 		}
10702 	    }
10703 	}
10704 	else
10705 #endif
10706 	if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
10707 	    buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
10708 
10709 	screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
10710 	i = redraw_cmdline;
10711 	screen_fill(row, row + 1,
10712 		this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
10713 		(int)(WITH_OFF(off) + WITH_WIDTH(width)),
10714 		fillchar, fillchar, attr);
10715 	/* don't redraw the cmdline because of showing the ruler */
10716 	redraw_cmdline = i;
10717 	wp->w_ru_cursor = wp->w_cursor;
10718 	wp->w_ru_virtcol = wp->w_virtcol;
10719 	wp->w_ru_empty = empty_line;
10720 	wp->w_ru_topline = wp->w_topline;
10721 	wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10722 #ifdef FEAT_DIFF
10723 	wp->w_ru_topfill = wp->w_topfill;
10724 #endif
10725     }
10726 }
10727 #endif
10728 
10729 #if defined(FEAT_LINEBREAK) || defined(PROTO)
10730 /*
10731  * Return the width of the 'number' and 'relativenumber' column.
10732  * Caller may need to check if 'number' or 'relativenumber' is set.
10733  * Otherwise it depends on 'numberwidth' and the line count.
10734  */
10735     int
10736 number_width(win_T *wp)
10737 {
10738     int		n;
10739     linenr_T	lnum;
10740 
10741     if (wp->w_p_rnu && !wp->w_p_nu)
10742 	/* cursor line shows "0" */
10743 	lnum = wp->w_height;
10744     else
10745 	/* cursor line shows absolute line number */
10746 	lnum = wp->w_buffer->b_ml.ml_line_count;
10747 
10748     if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
10749 	return wp->w_nrwidth_width;
10750     wp->w_nrwidth_line_count = lnum;
10751 
10752     n = 0;
10753     do
10754     {
10755 	lnum /= 10;
10756 	++n;
10757     } while (lnum > 0);
10758 
10759     /* 'numberwidth' gives the minimal width plus one */
10760     if (n < wp->w_p_nuw - 1)
10761 	n = wp->w_p_nuw - 1;
10762 
10763     wp->w_nrwidth_width = n;
10764     wp->w_nuw_cached = wp->w_p_nuw;
10765     return n;
10766 }
10767 #endif
10768 
10769 /*
10770  * Return the current cursor column. This is the actual position on the
10771  * screen. First column is 0.
10772  */
10773     int
10774 screen_screencol(void)
10775 {
10776     return screen_cur_col;
10777 }
10778 
10779 /*
10780  * Return the current cursor row. This is the actual position on the screen.
10781  * First row is 0.
10782  */
10783     int
10784 screen_screenrow(void)
10785 {
10786     return screen_cur_row;
10787 }
10788