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