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