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