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