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