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