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