Patch 7.0.070
[MacVim/jjgod.git] / src / screen.c
blob6c6403e0bac6ac4b2b045c3f822a7ff0ad7a508f
1 /* vi:set ts=8 sts=4 sw=4:
3 * VIM - Vi IMproved by Bram Moolenaar
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 */
11 * screen.c: code for displaying on the screen
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.
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.
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 * ScreenLinesC[][] contain up to 'maxcombine' 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).
35 * The screen_*() functions write to the screen and handle updating
36 * ScreenLines[].
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.
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)
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.
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.
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.
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.
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.
73 * Commands that change highlighting and possibly cause a scroll too must call
74 * redraw_later(SOME_VALID) to update the whole window but still use scrolling
75 * to avoid redrawing everything. But the length of displayed lines must not
76 * change, use NOT_VALID then.
78 * Commands that move the window position must call redraw_later(NOT_VALID).
79 * TODO: should minimize redrawing by scrolling when possible.
81 * Commands that change everything (e.g., resizing the screen) must call
82 * redraw_all_later(NOT_VALID) or redraw_all_later(CLEAR).
84 * Things that are handled indirectly:
85 * - When messages scroll the screen up, msg_scrolled will be set and
86 * update_screen() called to redraw.
89 #include "vim.h"
92 * The attributes that are actually active for writing to the screen.
94 static int screen_attr = 0;
97 * Positioning the cursor is reduced by remembering the last position.
98 * Mostly used by windgoto() and screen_char().
100 static int screen_cur_row, screen_cur_col; /* last known cursor position */
102 #ifdef FEAT_SEARCH_EXTRA
104 * Struct used for highlighting 'hlsearch' matches for the last use search
105 * pattern or a ":match" item.
106 * For 'hlsearch' there is one pattern for all windows. For ":match" there is
107 * a different pattern for each window.
109 typedef struct
111 regmmatch_T rm; /* points to the regexp program; contains last found
112 match (may continue in next line) */
113 buf_T *buf; /* the buffer to search for a match */
114 linenr_T lnum; /* the line to search for a match */
115 int attr; /* attributes to be used for a match */
116 int attr_cur; /* attributes currently active in win_line() */
117 linenr_T first_lnum; /* first lnum to search for multi-line pat */
118 colnr_T startcol; /* in win_line() points to char where HL starts */
119 colnr_T endcol; /* in win_line() points to char where HL ends */
120 } match_T;
122 static match_T search_hl; /* used for 'hlsearch' highlight matching */
123 static match_T match_hl[3]; /* used for ":match" highlight matching */
124 #endif
126 #ifdef FEAT_FOLDING
127 static foldinfo_T win_foldinfo; /* info for 'foldcolumn' */
128 #endif
131 * Buffer for one screen line (characters and attributes).
133 static schar_T *current_ScreenLine;
135 static void win_update __ARGS((win_T *wp));
136 static void win_draw_end __ARGS((win_T *wp, int c1, int c2, int row, int endrow, hlf_T hl));
137 #ifdef FEAT_FOLDING
138 static void fold_line __ARGS((win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row));
139 static void fill_foldcolumn __ARGS((char_u *p, win_T *wp, int closed, linenr_T lnum));
140 static void copy_text_attr __ARGS((int off, char_u *buf, int len, int attr));
141 #endif
142 static int win_line __ARGS((win_T *, linenr_T, int, int, int nochange));
143 static int char_needs_redraw __ARGS((int off_from, int off_to, int cols));
144 #ifdef FEAT_RIGHTLEFT
145 static void screen_line __ARGS((int row, int coloff, int endcol, int clear_width, int rlflag));
146 # define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c), (rl))
147 #else
148 static void screen_line __ARGS((int row, int coloff, int endcol, int clear_width));
149 # define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c))
150 #endif
151 #ifdef FEAT_VERTSPLIT
152 static void draw_vsep_win __ARGS((win_T *wp, int row));
153 #endif
154 #ifdef FEAT_STL_OPT
155 static void redraw_custum_statusline __ARGS((win_T *wp));
156 #endif
157 #ifdef FEAT_SEARCH_EXTRA
158 static void start_search_hl __ARGS((void));
159 static void end_search_hl __ARGS((void));
160 static void prepare_search_hl __ARGS((win_T *wp, linenr_T lnum));
161 static void next_search_hl __ARGS((win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol));
162 #endif
163 static void screen_start_highlight __ARGS((int attr));
164 static void screen_char __ARGS((unsigned off, int row, int col));
165 #ifdef FEAT_MBYTE
166 static void screen_char_2 __ARGS((unsigned off, int row, int col));
167 #endif
168 static void screenclear2 __ARGS((void));
169 static void lineclear __ARGS((unsigned off, int width));
170 static void lineinvalid __ARGS((unsigned off, int width));
171 #ifdef FEAT_VERTSPLIT
172 static void linecopy __ARGS((int to, int from, win_T *wp));
173 static void redraw_block __ARGS((int row, int end, win_T *wp));
174 #endif
175 static int win_do_lines __ARGS((win_T *wp, int row, int line_count, int mayclear, int del));
176 static void win_rest_invalid __ARGS((win_T *wp));
177 static void msg_pos_mode __ARGS((void));
178 #if defined(FEAT_WINDOWS)
179 static void draw_tabline __ARGS((void));
180 #endif
181 #if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
182 static int fillchar_status __ARGS((int *attr, int is_curwin));
183 #endif
184 #ifdef FEAT_VERTSPLIT
185 static int fillchar_vsep __ARGS((int *attr));
186 #endif
187 #ifdef FEAT_STL_OPT
188 static void win_redr_custom __ARGS((win_T *wp, int draw_ruler));
189 #endif
190 #ifdef FEAT_CMDL_INFO
191 static void win_redr_ruler __ARGS((win_T *wp, int always));
192 #endif
194 #if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
195 /* Ugly global: overrule attribute used by screen_char() */
196 static int screen_char_attr = 0;
197 #endif
200 * Redraw the current window later, with update_screen(type).
201 * Set must_redraw only if not already set to a higher value.
202 * e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
204 void
205 redraw_later(type)
206 int type;
208 redraw_win_later(curwin, type);
211 void
212 redraw_win_later(wp, type)
213 win_T *wp;
214 int type;
216 if (wp->w_redr_type < type)
218 wp->w_redr_type = type;
219 if (type >= NOT_VALID)
220 wp->w_lines_valid = 0;
221 if (must_redraw < type) /* must_redraw is the maximum of all windows */
222 must_redraw = type;
227 * Force a complete redraw later. Also resets the highlighting. To be used
228 * after executing a shell command that messes up the screen.
230 void
231 redraw_later_clear()
233 redraw_all_later(CLEAR);
234 #ifdef FEAT_GUI
235 if (gui.in_use)
236 /* Use a code that will reset gui.highlight_mask in
237 * gui_stop_highlight(). */
238 screen_attr = HL_ALL + 1;
239 else
240 #endif
241 /* Use attributes that is very unlikely to appear in text. */
242 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE;
246 * Mark all windows to be redrawn later.
248 void
249 redraw_all_later(type)
250 int type;
252 win_T *wp;
254 FOR_ALL_WINDOWS(wp)
256 redraw_win_later(wp, type);
261 * Mark all windows that are editing the current buffer to be updated later.
263 void
264 redraw_curbuf_later(type)
265 int type;
267 redraw_buf_later(curbuf, type);
270 void
271 redraw_buf_later(buf, type)
272 buf_T *buf;
273 int type;
275 win_T *wp;
277 FOR_ALL_WINDOWS(wp)
279 if (wp->w_buffer == buf)
280 redraw_win_later(wp, type);
285 * Changed something in the current window, at buffer line "lnum", that
286 * requires that line and possibly other lines to be redrawn.
287 * Used when entering/leaving Insert mode with the cursor on a folded line.
288 * Used to remove the "$" from a change command.
289 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
290 * may become invalid and the whole window will have to be redrawn.
292 /*ARGSUSED*/
293 void
294 redrawWinline(lnum, invalid)
295 linenr_T lnum;
296 int invalid; /* window line height is invalid now */
298 #ifdef FEAT_FOLDING
299 int i;
300 #endif
302 if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
303 curwin->w_redraw_top = lnum;
304 if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
305 curwin->w_redraw_bot = lnum;
306 redraw_later(VALID);
308 #ifdef FEAT_FOLDING
309 if (invalid)
311 /* A w_lines[] entry for this lnum has become invalid. */
312 i = find_wl_entry(curwin, lnum);
313 if (i >= 0)
314 curwin->w_lines[i].wl_valid = FALSE;
316 #endif
320 * update all windows that are editing the current buffer
322 void
323 update_curbuf(type)
324 int type;
326 redraw_curbuf_later(type);
327 update_screen(type);
331 * update_screen()
333 * Based on the current value of curwin->w_topline, transfer a screenfull
334 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
336 void
337 update_screen(type)
338 int type;
340 win_T *wp;
341 static int did_intro = FALSE;
342 #if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
343 int did_one;
344 #endif
346 if (!screen_valid(TRUE))
347 return;
349 if (must_redraw)
351 if (type < must_redraw) /* use maximal type */
352 type = must_redraw;
353 must_redraw = 0;
356 /* Need to update w_lines[]. */
357 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
358 type = NOT_VALID;
360 if (!redrawing())
362 redraw_later(type); /* remember type for next time */
363 must_redraw = type;
364 if (type > INVERTED_ALL)
365 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
366 return;
369 updating_screen = TRUE;
370 #ifdef FEAT_SYN_HL
371 ++display_tick; /* let syntax code know we're in a next round of
372 * display updating */
373 #endif
376 * if the screen was scrolled up when displaying a message, scroll it down
378 if (msg_scrolled)
380 clear_cmdline = TRUE;
381 if (msg_scrolled > Rows - 5) /* clearing is faster */
382 type = CLEAR;
383 else if (type != CLEAR)
385 check_for_delay(FALSE);
386 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, NULL) == FAIL)
387 type = CLEAR;
388 FOR_ALL_WINDOWS(wp)
390 if (W_WINROW(wp) < msg_scrolled)
392 if (W_WINROW(wp) + wp->w_height > msg_scrolled
393 && wp->w_redr_type < REDRAW_TOP
394 && wp->w_lines_valid > 0
395 && wp->w_topline == wp->w_lines[0].wl_lnum)
397 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
398 wp->w_redr_type = REDRAW_TOP;
400 else
402 wp->w_redr_type = NOT_VALID;
403 #ifdef FEAT_WINDOWS
404 if (W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp)
405 <= msg_scrolled)
406 wp->w_redr_status = TRUE;
407 #endif
411 redraw_cmdline = TRUE;
412 #ifdef FEAT_WINDOWS
413 redraw_tabline = TRUE;
414 #endif
416 msg_scrolled = 0;
417 need_wait_return = FALSE;
420 /* reset cmdline_row now (may have been changed temporarily) */
421 compute_cmdrow();
423 /* Check for changed highlighting */
424 if (need_highlight_changed)
425 highlight_changed();
427 if (type == CLEAR) /* first clear screen */
429 screenclear(); /* will reset clear_cmdline */
430 type = NOT_VALID;
433 if (clear_cmdline) /* going to clear cmdline (done below) */
434 check_for_delay(FALSE);
436 #ifdef FEAT_LINEBREAK
437 /* Force redraw when width of 'number' column changes. */
438 if (curwin->w_redr_type < NOT_VALID
439 && curwin->w_nrwidth != (curwin->w_p_nu ? number_width(curwin) : 0))
440 curwin->w_redr_type = NOT_VALID;
441 #endif
444 * Only start redrawing if there is really something to do.
446 if (type == INVERTED)
447 update_curswant();
448 if (curwin->w_redr_type < type
449 && !((type == VALID
450 && curwin->w_lines[0].wl_valid
451 #ifdef FEAT_DIFF
452 && curwin->w_topfill == curwin->w_old_topfill
453 && curwin->w_botfill == curwin->w_old_botfill
454 #endif
455 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
456 #ifdef FEAT_VISUAL
457 || (type == INVERTED
458 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
459 && curwin->w_old_visual_mode == VIsual_mode
460 && (curwin->w_valid & VALID_VIRTCOL)
461 && curwin->w_old_curswant == curwin->w_curswant)
462 #endif
464 curwin->w_redr_type = type;
466 #ifdef FEAT_WINDOWS
467 /* Redraw the tab pages line if needed. */
468 if (redraw_tabline || type >= NOT_VALID)
469 draw_tabline();
470 #endif
472 #ifdef FEAT_SYN_HL
474 * Correct stored syntax highlighting info for changes in each displayed
475 * buffer. Each buffer must only be done once.
477 FOR_ALL_WINDOWS(wp)
479 if (wp->w_buffer->b_mod_set)
481 # ifdef FEAT_WINDOWS
482 win_T *wwp;
484 /* Check if we already did this buffer. */
485 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
486 if (wwp->w_buffer == wp->w_buffer)
487 break;
488 # endif
489 if (
490 # ifdef FEAT_WINDOWS
491 wwp == wp &&
492 # endif
493 syntax_present(wp->w_buffer))
494 syn_stack_apply_changes(wp->w_buffer);
497 #endif
500 * Go from top to bottom through the windows, redrawing the ones that need
501 * it.
503 #if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
504 did_one = FALSE;
505 #endif
506 #ifdef FEAT_SEARCH_EXTRA
507 search_hl.rm.regprog = NULL;
508 #endif
509 FOR_ALL_WINDOWS(wp)
511 if (wp->w_redr_type != 0)
513 cursor_off();
514 #if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
515 if (!did_one)
517 did_one = TRUE;
518 # ifdef FEAT_SEARCH_EXTRA
519 start_search_hl();
520 # endif
521 # ifdef FEAT_CLIPBOARD
522 /* When Visual area changed, may have to update selection. */
523 if (clip_star.available && clip_isautosel())
524 clip_update_selection();
525 # endif
526 #ifdef FEAT_GUI
527 /* Remove the cursor before starting to do anything, because
528 * scrolling may make it difficult to redraw the text under
529 * it. */
530 if (gui.in_use)
531 gui_undraw_cursor();
532 #endif
534 #endif
535 win_update(wp);
538 #ifdef FEAT_WINDOWS
539 /* redraw status line after the window to minimize cursor movement */
540 if (wp->w_redr_status)
542 cursor_off();
543 win_redr_status(wp);
545 #endif
547 #if defined(FEAT_SEARCH_EXTRA)
548 end_search_hl();
549 #endif
551 #ifdef FEAT_WINDOWS
552 /* Reset b_mod_set flags. Going through all windows is probably faster
553 * than going through all buffers (there could be many buffers). */
554 for (wp = firstwin; wp != NULL; wp = wp->w_next)
555 wp->w_buffer->b_mod_set = FALSE;
556 #else
557 curbuf->b_mod_set = FALSE;
558 #endif
560 updating_screen = FALSE;
561 #ifdef FEAT_GUI
562 gui_may_resize_shell();
563 #endif
565 /* Clear or redraw the command line. Done last, because scrolling may
566 * mess up the command line. */
567 if (clear_cmdline || redraw_cmdline)
568 showmode();
570 /* May put up an introductory message when not editing a file */
571 if (!did_intro && bufempty()
572 && curbuf->b_fname == NULL
573 #ifdef FEAT_WINDOWS
574 && firstwin->w_next == NULL
575 #endif
576 && vim_strchr(p_shm, SHM_INTRO) == NULL)
577 intro_message(FALSE);
578 did_intro = TRUE;
580 #ifdef FEAT_GUI
581 /* Redraw the cursor and update the scrollbars when all screen updating is
582 * done. */
583 if (gui.in_use)
585 out_flush(); /* required before updating the cursor */
586 if (did_one)
587 gui_update_cursor(FALSE, FALSE);
588 gui_update_scrollbars(FALSE);
590 #endif
593 #if defined(FEAT_SIGNS) || defined(FEAT_GUI)
594 static void update_prepare __ARGS((void));
595 static void update_finish __ARGS((void));
598 * Prepare for updating one or more windows.
600 static void
601 update_prepare()
603 cursor_off();
604 updating_screen = TRUE;
605 #ifdef FEAT_GUI
606 /* Remove the cursor before starting to do anything, because scrolling may
607 * make it difficult to redraw the text under it. */
608 if (gui.in_use)
609 gui_undraw_cursor();
610 #endif
611 #ifdef FEAT_SEARCH_EXTRA
612 start_search_hl();
613 #endif
617 * Finish updating one or more windows.
619 static void
620 update_finish()
622 if (redraw_cmdline)
623 showmode();
625 # ifdef FEAT_SEARCH_EXTRA
626 end_search_hl();
627 # endif
629 updating_screen = FALSE;
631 # ifdef FEAT_GUI
632 gui_may_resize_shell();
634 /* Redraw the cursor and update the scrollbars when all screen updating is
635 * done. */
636 if (gui.in_use)
638 out_flush(); /* required before updating the cursor */
639 gui_update_cursor(FALSE, FALSE);
640 gui_update_scrollbars(FALSE);
642 # endif
644 #endif
646 #if defined(FEAT_SIGNS) || defined(PROTO)
647 void
648 update_debug_sign(buf, lnum)
649 buf_T *buf;
650 linenr_T lnum;
652 win_T *wp;
653 int doit = FALSE;
655 # ifdef FEAT_FOLDING
656 win_foldinfo.fi_level = 0;
657 # endif
659 /* update/delete a specific mark */
660 FOR_ALL_WINDOWS(wp)
662 if (buf != NULL && lnum > 0)
664 if (wp->w_buffer == buf && lnum >= wp->w_topline
665 && lnum < wp->w_botline)
667 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
668 wp->w_redraw_top = lnum;
669 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
670 wp->w_redraw_bot = lnum;
671 redraw_win_later(wp, VALID);
674 else
675 redraw_win_later(wp, VALID);
676 if (wp->w_redr_type != 0)
677 doit = TRUE;
680 if (!doit)
681 return;
683 /* update all windows that need updating */
684 update_prepare();
686 # ifdef FEAT_WINDOWS
687 for (wp = firstwin; wp; wp = wp->w_next)
689 if (wp->w_redr_type != 0)
690 win_update(wp);
691 if (wp->w_redr_status)
692 win_redr_status(wp);
694 # else
695 if (curwin->w_redr_type != 0)
696 win_update(curwin);
697 # endif
699 update_finish();
701 #endif
704 #if defined(FEAT_GUI) || defined(PROTO)
706 * Update a single window, its status line and maybe the command line msg.
707 * Used for the GUI scrollbar.
709 void
710 updateWindow(wp)
711 win_T *wp;
713 update_prepare();
715 #ifdef FEAT_CLIPBOARD
716 /* When Visual area changed, may have to update selection. */
717 if (clip_star.available && clip_isautosel())
718 clip_update_selection();
719 #endif
721 win_update(wp);
723 #ifdef FEAT_WINDOWS
724 /* When the screen was cleared redraw the tab pages line. */
725 if (redraw_tabline)
726 draw_tabline();
728 if (wp->w_redr_status
729 # ifdef FEAT_CMDL_INFO
730 || p_ru
731 # endif
732 # ifdef FEAT_STL_OPT
733 || *p_stl != NUL || *wp->w_p_stl != NUL
734 # endif
736 win_redr_status(wp);
737 #endif
739 update_finish();
741 #endif
744 * Update a single window.
746 * This may cause the windows below it also to be redrawn (when clearing the
747 * screen or scrolling lines).
749 * How the window is redrawn depends on wp->w_redr_type. Each type also
750 * implies the one below it.
751 * NOT_VALID redraw the whole window
752 * SOME_VALID redraw the whole window but do scroll when possible
753 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
754 * INVERTED redraw the changed part of the Visual area
755 * INVERTED_ALL redraw the whole Visual area
756 * VALID 1. scroll up/down to adjust for a changed w_topline
757 * 2. update lines at the top when scrolled down
758 * 3. redraw changed text:
759 * - if wp->w_buffer->b_mod_set set, update lines between
760 * b_mod_top and b_mod_bot.
761 * - if wp->w_redraw_top non-zero, redraw lines between
762 * wp->w_redraw_top and wp->w_redr_bot.
763 * - continue redrawing when syntax status is invalid.
764 * 4. if scrolled up, update lines at the bottom.
765 * This results in three areas that may need updating:
766 * top: from first row to top_end (when scrolled down)
767 * mid: from mid_start to mid_end (update inversion or changed text)
768 * bot: from bot_start to last row (when scrolled up)
770 static void
771 win_update(wp)
772 win_T *wp;
774 buf_T *buf = wp->w_buffer;
775 int type;
776 int top_end = 0; /* Below last row of the top area that needs
777 updating. 0 when no top area updating. */
778 int mid_start = 999;/* first row of the mid area that needs
779 updating. 999 when no mid area updating. */
780 int mid_end = 0; /* Below last row of the mid area that needs
781 updating. 0 when no mid area updating. */
782 int bot_start = 999;/* first row of the bot area that needs
783 updating. 999 when no bot area updating */
784 #ifdef FEAT_VISUAL
785 int scrolled_down = FALSE; /* TRUE when scrolled down when
786 w_topline got smaller a bit */
787 #endif
788 #ifdef FEAT_SEARCH_EXTRA
789 int top_to_mod = FALSE; /* redraw above mod_top */
790 #endif
792 int row; /* current window row to display */
793 linenr_T lnum; /* current buffer lnum to display */
794 int idx; /* current index in w_lines[] */
795 int srow; /* starting row of the current line */
797 int eof = FALSE; /* if TRUE, we hit the end of the file */
798 int didline = FALSE; /* if TRUE, we finished the last line */
799 int i;
800 long j;
801 static int recursive = FALSE; /* being called recursively */
802 int old_botline = wp->w_botline;
803 #ifdef FEAT_FOLDING
804 long fold_count;
805 #endif
806 #ifdef FEAT_SYN_HL
807 /* remember what happened to the previous line, to know if
808 * check_visual_highlight() can be used */
809 #define DID_NONE 1 /* didn't update a line */
810 #define DID_LINE 2 /* updated a normal line */
811 #define DID_FOLD 3 /* updated a folded line */
812 int did_update = DID_NONE;
813 linenr_T syntax_last_parsed = 0; /* last parsed text line */
814 #endif
815 linenr_T mod_top = 0;
816 linenr_T mod_bot = 0;
817 #if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
818 int save_got_int;
819 #endif
821 type = wp->w_redr_type;
823 if (type == NOT_VALID)
825 #ifdef FEAT_WINDOWS
826 wp->w_redr_status = TRUE;
827 #endif
828 wp->w_lines_valid = 0;
831 /* Window is zero-height: nothing to draw. */
832 if (wp->w_height == 0)
834 wp->w_redr_type = 0;
835 return;
838 #ifdef FEAT_VERTSPLIT
839 /* Window is zero-width: Only need to draw the separator. */
840 if (wp->w_width == 0)
842 /* draw the vertical separator right of this window */
843 draw_vsep_win(wp, 0);
844 wp->w_redr_type = 0;
845 return;
847 #endif
849 #ifdef FEAT_SEARCH_EXTRA
850 /* Setup for ":match" and 'hlsearch' highlighting. Disable any previous
851 * match */
852 for (i = 0; i < 3; ++i)
854 match_hl[i].rm = wp->w_match[i];
855 if (wp->w_match_id[i] == 0)
856 match_hl[i].attr = 0;
857 else
858 match_hl[i].attr = syn_id2attr(wp->w_match_id[i]);
859 match_hl[i].buf = buf;
860 match_hl[i].lnum = 0;
861 match_hl[i].first_lnum = 0;
863 search_hl.buf = buf;
864 search_hl.lnum = 0;
865 search_hl.first_lnum = 0;
866 #endif
868 #ifdef FEAT_LINEBREAK
869 /* Force redraw when width of 'number' column changes. */
870 i = wp->w_p_nu ? number_width(wp) : 0;
871 if (wp->w_nrwidth != i)
873 type = NOT_VALID;
874 wp->w_nrwidth = i;
876 else
877 #endif
879 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
882 * When there are both inserted/deleted lines and specific lines to be
883 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
884 * everything (only happens when redrawing is off for while).
886 type = NOT_VALID;
888 else
891 * Set mod_top to the first line that needs displaying because of
892 * changes. Set mod_bot to the first line after the changes.
894 mod_top = wp->w_redraw_top;
895 if (wp->w_redraw_bot != 0)
896 mod_bot = wp->w_redraw_bot + 1;
897 else
898 mod_bot = 0;
899 wp->w_redraw_top = 0; /* reset for next time */
900 wp->w_redraw_bot = 0;
901 if (buf->b_mod_set)
903 if (mod_top == 0 || mod_top > buf->b_mod_top)
905 mod_top = buf->b_mod_top;
906 #ifdef FEAT_SYN_HL
907 /* Need to redraw lines above the change that may be included
908 * in a pattern match. */
909 if (syntax_present(buf))
911 mod_top -= buf->b_syn_sync_linebreaks;
912 if (mod_top < 1)
913 mod_top = 1;
915 #endif
917 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
918 mod_bot = buf->b_mod_bot;
920 #ifdef FEAT_SEARCH_EXTRA
921 /* When 'hlsearch' is on and using a multi-line search pattern, a
922 * change in one line may make the Search highlighting in a
923 * previous line invalid. Simple solution: redraw all visible
924 * lines above the change.
925 * Same for a ":match" pattern.
927 if (search_hl.rm.regprog != NULL
928 && re_multiline(search_hl.rm.regprog))
929 top_to_mod = TRUE;
930 else
931 for (i = 0; i < 3; ++i)
932 if (match_hl[i].rm.regprog != NULL
933 && re_multiline(match_hl[i].rm.regprog))
935 top_to_mod = TRUE;
936 break;
938 #endif
940 #ifdef FEAT_FOLDING
941 if (mod_top != 0 && hasAnyFolding(wp))
943 linenr_T lnumt, lnumb;
946 * A change in a line can cause lines above it to become folded or
947 * unfolded. Find the top most buffer line that may be affected.
948 * If the line was previously folded and displayed, get the first
949 * line of that fold. If the line is folded now, get the first
950 * folded line. Use the minimum of these two.
953 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
954 * the line below it. If there is no valid entry, use w_topline.
955 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
956 * to this line. If there is no valid entry, use MAXLNUM. */
957 lnumt = wp->w_topline;
958 lnumb = MAXLNUM;
959 for (i = 0; i < wp->w_lines_valid; ++i)
960 if (wp->w_lines[i].wl_valid)
962 if (wp->w_lines[i].wl_lastlnum < mod_top)
963 lnumt = wp->w_lines[i].wl_lastlnum + 1;
964 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
966 lnumb = wp->w_lines[i].wl_lnum;
967 /* When there is a fold column it might need updating
968 * in the next line ("J" just above an open fold). */
969 if (wp->w_p_fdc > 0)
970 ++lnumb;
974 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
975 if (mod_top > lnumt)
976 mod_top = lnumt;
978 /* Now do the same for the bottom line (one above mod_bot). */
979 --mod_bot;
980 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
981 ++mod_bot;
982 if (mod_bot < lnumb)
983 mod_bot = lnumb;
985 #endif
987 /* When a change starts above w_topline and the end is below
988 * w_topline, start redrawing at w_topline.
989 * If the end of the change is above w_topline: do like no change was
990 * made, but redraw the first line to find changes in syntax. */
991 if (mod_top != 0 && mod_top < wp->w_topline)
993 if (mod_bot > wp->w_topline)
994 mod_top = wp->w_topline;
995 #ifdef FEAT_SYN_HL
996 else if (syntax_present(buf))
997 top_end = 1;
998 #endif
1001 /* When line numbers are displayed need to redraw all lines below
1002 * inserted/deleted lines. */
1003 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1004 mod_bot = MAXLNUM;
1008 * When only displaying the lines at the top, set top_end. Used when
1009 * window has scrolled down for msg_scrolled.
1011 if (type == REDRAW_TOP)
1013 j = 0;
1014 for (i = 0; i < wp->w_lines_valid; ++i)
1016 j += wp->w_lines[i].wl_size;
1017 if (j >= wp->w_upd_rows)
1019 top_end = j;
1020 break;
1023 if (top_end == 0)
1024 /* not found (cannot happen?): redraw everything */
1025 type = NOT_VALID;
1026 else
1027 /* top area defined, the rest is VALID */
1028 type = VALID;
1032 * If there are no changes on the screen that require a complete redraw,
1033 * handle three cases:
1034 * 1: we are off the top of the screen by a few lines: scroll down
1035 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1036 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1037 * w_lines[] that needs updating.
1039 if ((type == VALID || type == SOME_VALID
1040 || type == INVERTED || type == INVERTED_ALL)
1041 #ifdef FEAT_DIFF
1042 && !wp->w_botfill && !wp->w_old_botfill
1043 #endif
1046 if (mod_top != 0 && wp->w_topline == mod_top)
1049 * w_topline is the first changed line, the scrolling will be done
1050 * further down.
1053 else if (wp->w_lines[0].wl_valid
1054 && (wp->w_topline < wp->w_lines[0].wl_lnum
1055 #ifdef FEAT_DIFF
1056 || (wp->w_topline == wp->w_lines[0].wl_lnum
1057 && wp->w_topfill > wp->w_old_topfill)
1058 #endif
1062 * New topline is above old topline: May scroll down.
1064 #ifdef FEAT_FOLDING
1065 if (hasAnyFolding(wp))
1067 linenr_T ln;
1069 /* count the number of lines we are off, counting a sequence
1070 * of folded lines as one */
1071 j = 0;
1072 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1074 ++j;
1075 if (j >= wp->w_height - 2)
1076 break;
1077 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1080 else
1081 #endif
1082 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1083 if (j < wp->w_height - 2) /* not too far off */
1085 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1086 #ifdef FEAT_DIFF
1087 /* insert extra lines for previously invisible filler lines */
1088 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1089 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1090 - wp->w_old_topfill;
1091 #endif
1092 if (i < wp->w_height - 2) /* less than a screen off */
1095 * Try to insert the correct number of lines.
1096 * If not the last window, delete the lines at the bottom.
1097 * win_ins_lines may fail when the terminal can't do it.
1099 if (i > 0)
1100 check_for_delay(FALSE);
1101 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1103 if (wp->w_lines_valid != 0)
1105 /* Need to update rows that are new, stop at the
1106 * first one that scrolled down. */
1107 top_end = i;
1108 #ifdef FEAT_VISUAL
1109 scrolled_down = TRUE;
1110 #endif
1112 /* Move the entries that were scrolled, disable
1113 * the entries for the lines to be redrawn. */
1114 if ((wp->w_lines_valid += j) > wp->w_height)
1115 wp->w_lines_valid = wp->w_height;
1116 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1117 wp->w_lines[idx] = wp->w_lines[idx - j];
1118 while (idx >= 0)
1119 wp->w_lines[idx--].wl_valid = FALSE;
1122 else
1123 mid_start = 0; /* redraw all lines */
1125 else
1126 mid_start = 0; /* redraw all lines */
1128 else
1129 mid_start = 0; /* redraw all lines */
1131 else
1134 * New topline is at or below old topline: May scroll up.
1135 * When topline didn't change, find first entry in w_lines[] that
1136 * needs updating.
1139 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1140 j = -1;
1141 row = 0;
1142 for (i = 0; i < wp->w_lines_valid; i++)
1144 if (wp->w_lines[i].wl_valid
1145 && wp->w_lines[i].wl_lnum == wp->w_topline)
1147 j = i;
1148 break;
1150 row += wp->w_lines[i].wl_size;
1152 if (j == -1)
1154 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1155 * lines */
1156 mid_start = 0;
1158 else
1161 * Try to delete the correct number of lines.
1162 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1164 #ifdef FEAT_DIFF
1165 /* If the topline didn't change, delete old filler lines,
1166 * otherwise delete filler lines of the new topline... */
1167 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1168 row += wp->w_old_topfill;
1169 else
1170 row += diff_check_fill(wp, wp->w_topline);
1171 /* ... but don't delete new filler lines. */
1172 row -= wp->w_topfill;
1173 #endif
1174 if (row > 0)
1176 check_for_delay(FALSE);
1177 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1178 bot_start = wp->w_height - row;
1179 else
1180 mid_start = 0; /* redraw all lines */
1182 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1185 * Skip the lines (below the deleted lines) that are still
1186 * valid and don't need redrawing. Copy their info
1187 * upwards, to compensate for the deleted lines. Set
1188 * bot_start to the first row that needs redrawing.
1190 bot_start = 0;
1191 idx = 0;
1192 for (;;)
1194 wp->w_lines[idx] = wp->w_lines[j];
1195 /* stop at line that didn't fit, unless it is still
1196 * valid (no lines deleted) */
1197 if (row > 0 && bot_start + row
1198 + (int)wp->w_lines[j].wl_size > wp->w_height)
1200 wp->w_lines_valid = idx + 1;
1201 break;
1203 bot_start += wp->w_lines[idx++].wl_size;
1205 /* stop at the last valid entry in w_lines[].wl_size */
1206 if (++j >= wp->w_lines_valid)
1208 wp->w_lines_valid = idx;
1209 break;
1212 #ifdef FEAT_DIFF
1213 /* Correct the first entry for filler lines at the top
1214 * when it won't get updated below. */
1215 if (wp->w_p_diff && bot_start > 0)
1216 wp->w_lines[0].wl_size =
1217 plines_win_nofill(wp, wp->w_topline, TRUE)
1218 + wp->w_topfill;
1219 #endif
1224 /* When starting redraw in the first line, redraw all lines. When
1225 * there is only one window it's probably faster to clear the screen
1226 * first. */
1227 if (mid_start == 0)
1229 mid_end = wp->w_height;
1230 if (lastwin == firstwin)
1231 screenclear();
1234 else
1236 /* Not VALID or INVERTED: redraw all lines. */
1237 mid_start = 0;
1238 mid_end = wp->w_height;
1241 if (type == SOME_VALID)
1243 /* SOME_VALID: redraw all lines. */
1244 mid_start = 0;
1245 mid_end = wp->w_height;
1246 type = NOT_VALID;
1249 #ifdef FEAT_VISUAL
1250 /* check if we are updating or removing the inverted part */
1251 if ((VIsual_active && buf == curwin->w_buffer)
1252 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1254 linenr_T from, to;
1256 if (VIsual_active)
1258 if (VIsual_active
1259 && (VIsual_mode != wp->w_old_visual_mode
1260 || type == INVERTED_ALL))
1263 * If the type of Visual selection changed, redraw the whole
1264 * selection. Also when the ownership of the X selection is
1265 * gained or lost.
1267 if (curwin->w_cursor.lnum < VIsual.lnum)
1269 from = curwin->w_cursor.lnum;
1270 to = VIsual.lnum;
1272 else
1274 from = VIsual.lnum;
1275 to = curwin->w_cursor.lnum;
1277 /* redraw more when the cursor moved as well */
1278 if (wp->w_old_cursor_lnum < from)
1279 from = wp->w_old_cursor_lnum;
1280 if (wp->w_old_cursor_lnum > to)
1281 to = wp->w_old_cursor_lnum;
1282 if (wp->w_old_visual_lnum < from)
1283 from = wp->w_old_visual_lnum;
1284 if (wp->w_old_visual_lnum > to)
1285 to = wp->w_old_visual_lnum;
1287 else
1290 * Find the line numbers that need to be updated: The lines
1291 * between the old cursor position and the current cursor
1292 * position. Also check if the Visual position changed.
1294 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1296 from = curwin->w_cursor.lnum;
1297 to = wp->w_old_cursor_lnum;
1299 else
1301 from = wp->w_old_cursor_lnum;
1302 to = curwin->w_cursor.lnum;
1303 if (from == 0) /* Visual mode just started */
1304 from = to;
1307 if (VIsual.lnum != wp->w_old_visual_lnum
1308 || VIsual.col != wp->w_old_visual_col)
1310 if (wp->w_old_visual_lnum < from
1311 && wp->w_old_visual_lnum != 0)
1312 from = wp->w_old_visual_lnum;
1313 if (wp->w_old_visual_lnum > to)
1314 to = wp->w_old_visual_lnum;
1315 if (VIsual.lnum < from)
1316 from = VIsual.lnum;
1317 if (VIsual.lnum > to)
1318 to = VIsual.lnum;
1323 * If in block mode and changed column or curwin->w_curswant:
1324 * update all lines.
1325 * First compute the actual start and end column.
1327 if (VIsual_mode == Ctrl_V)
1329 colnr_T fromc, toc;
1331 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
1332 ++toc;
1333 if (curwin->w_curswant == MAXCOL)
1334 toc = MAXCOL;
1336 if (fromc != wp->w_old_cursor_fcol
1337 || toc != wp->w_old_cursor_lcol)
1339 if (from > VIsual.lnum)
1340 from = VIsual.lnum;
1341 if (to < VIsual.lnum)
1342 to = VIsual.lnum;
1344 wp->w_old_cursor_fcol = fromc;
1345 wp->w_old_cursor_lcol = toc;
1348 else
1350 /* Use the line numbers of the old Visual area. */
1351 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1353 from = wp->w_old_cursor_lnum;
1354 to = wp->w_old_visual_lnum;
1356 else
1358 from = wp->w_old_visual_lnum;
1359 to = wp->w_old_cursor_lnum;
1364 * There is no need to update lines above the top of the window.
1366 if (from < wp->w_topline)
1367 from = wp->w_topline;
1370 * If we know the value of w_botline, use it to restrict the update to
1371 * the lines that are visible in the window.
1373 if (wp->w_valid & VALID_BOTLINE)
1375 if (from >= wp->w_botline)
1376 from = wp->w_botline - 1;
1377 if (to >= wp->w_botline)
1378 to = wp->w_botline - 1;
1382 * Find the minimal part to be updated.
1383 * Watch out for scrolling that made entries in w_lines[] invalid.
1384 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1385 * top_end; need to redraw from top_end to the "to" line.
1386 * A middle mouse click with a Visual selection may change the text
1387 * above the Visual area and reset wl_valid, do count these for
1388 * mid_end (in srow).
1390 if (mid_start > 0)
1392 lnum = wp->w_topline;
1393 idx = 0;
1394 srow = 0;
1395 if (scrolled_down)
1396 mid_start = top_end;
1397 else
1398 mid_start = 0;
1399 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1401 if (wp->w_lines[idx].wl_valid)
1402 mid_start += wp->w_lines[idx].wl_size;
1403 else if (!scrolled_down)
1404 srow += wp->w_lines[idx].wl_size;
1405 ++idx;
1406 # ifdef FEAT_FOLDING
1407 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1408 lnum = wp->w_lines[idx].wl_lnum;
1409 else
1410 # endif
1411 ++lnum;
1413 srow += mid_start;
1414 mid_end = wp->w_height;
1415 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1417 if (wp->w_lines[idx].wl_valid
1418 && wp->w_lines[idx].wl_lnum >= to + 1)
1420 /* Only update until first row of this line */
1421 mid_end = srow;
1422 break;
1424 srow += wp->w_lines[idx].wl_size;
1429 if (VIsual_active && buf == curwin->w_buffer)
1431 wp->w_old_visual_mode = VIsual_mode;
1432 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1433 wp->w_old_visual_lnum = VIsual.lnum;
1434 wp->w_old_visual_col = VIsual.col;
1435 wp->w_old_curswant = curwin->w_curswant;
1437 else
1439 wp->w_old_visual_mode = 0;
1440 wp->w_old_cursor_lnum = 0;
1441 wp->w_old_visual_lnum = 0;
1442 wp->w_old_visual_col = 0;
1444 #endif /* FEAT_VISUAL */
1446 #if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1447 /* reset got_int, otherwise regexp won't work */
1448 save_got_int = got_int;
1449 got_int = 0;
1450 #endif
1451 #ifdef FEAT_FOLDING
1452 win_foldinfo.fi_level = 0;
1453 #endif
1456 * Update all the window rows.
1458 idx = 0; /* first entry in w_lines[].wl_size */
1459 row = 0;
1460 srow = 0;
1461 lnum = wp->w_topline; /* first line shown in window */
1462 for (;;)
1464 /* stop updating when reached the end of the window (check for _past_
1465 * the end of the window is at the end of the loop) */
1466 if (row == wp->w_height)
1468 didline = TRUE;
1469 break;
1472 /* stop updating when hit the end of the file */
1473 if (lnum > buf->b_ml.ml_line_count)
1475 eof = TRUE;
1476 break;
1479 /* Remember the starting row of the line that is going to be dealt
1480 * with. It is used further down when the line doesn't fit. */
1481 srow = row;
1484 * Update a line when it is in an area that needs updating, when it
1485 * has changes or w_lines[idx] is invalid.
1486 * bot_start may be halfway a wrapped line after using
1487 * win_del_lines(), check if the current line includes it.
1488 * When syntax folding is being used, the saved syntax states will
1489 * already have been updated, we can't see where the syntax state is
1490 * the same again, just update until the end of the window.
1492 if (row < top_end
1493 || (row >= mid_start && row < mid_end)
1494 #ifdef FEAT_SEARCH_EXTRA
1495 || top_to_mod
1496 #endif
1497 || idx >= wp->w_lines_valid
1498 || (row + wp->w_lines[idx].wl_size > bot_start)
1499 || (mod_top != 0
1500 && (lnum == mod_top
1501 || (lnum >= mod_top
1502 && (lnum < mod_bot
1503 #ifdef FEAT_SYN_HL
1504 || did_update == DID_FOLD
1505 || (did_update == DID_LINE
1506 && syntax_present(buf)
1507 && (
1508 # ifdef FEAT_FOLDING
1509 (foldmethodIsSyntax(wp)
1510 && hasAnyFolding(wp)) ||
1511 # endif
1512 syntax_check_changed(lnum)))
1513 #endif
1514 )))))
1516 #ifdef FEAT_SEARCH_EXTRA
1517 if (lnum == mod_top)
1518 top_to_mod = FALSE;
1519 #endif
1522 * When at start of changed lines: May scroll following lines
1523 * up or down to minimize redrawing.
1524 * Don't do this when the change continues until the end.
1525 * Don't scroll when dollar_vcol is non-zero, keep the "$".
1527 if (lnum == mod_top
1528 && mod_bot != MAXLNUM
1529 && !(dollar_vcol != 0 && mod_bot == mod_top + 1))
1531 int old_rows = 0;
1532 int new_rows = 0;
1533 int xtra_rows;
1534 linenr_T l;
1536 /* Count the old number of window rows, using w_lines[], which
1537 * should still contain the sizes for the lines as they are
1538 * currently displayed. */
1539 for (i = idx; i < wp->w_lines_valid; ++i)
1541 /* Only valid lines have a meaningful wl_lnum. Invalid
1542 * lines are part of the changed area. */
1543 if (wp->w_lines[i].wl_valid
1544 && wp->w_lines[i].wl_lnum == mod_bot)
1545 break;
1546 old_rows += wp->w_lines[i].wl_size;
1547 #ifdef FEAT_FOLDING
1548 if (wp->w_lines[i].wl_valid
1549 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1551 /* Must have found the last valid entry above mod_bot.
1552 * Add following invalid entries. */
1553 ++i;
1554 while (i < wp->w_lines_valid
1555 && !wp->w_lines[i].wl_valid)
1556 old_rows += wp->w_lines[i++].wl_size;
1557 break;
1559 #endif
1562 if (i >= wp->w_lines_valid)
1564 /* We can't find a valid line below the changed lines,
1565 * need to redraw until the end of the window.
1566 * Inserting/deleting lines has no use. */
1567 bot_start = 0;
1569 else
1571 /* Able to count old number of rows: Count new window
1572 * rows, and may insert/delete lines */
1573 j = idx;
1574 for (l = lnum; l < mod_bot; ++l)
1576 #ifdef FEAT_FOLDING
1577 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1578 ++new_rows;
1579 else
1580 #endif
1581 #ifdef FEAT_DIFF
1582 if (l == wp->w_topline)
1583 new_rows += plines_win_nofill(wp, l, TRUE)
1584 + wp->w_topfill;
1585 else
1586 #endif
1587 new_rows += plines_win(wp, l, TRUE);
1588 ++j;
1589 if (new_rows > wp->w_height - row - 2)
1591 /* it's getting too much, must redraw the rest */
1592 new_rows = 9999;
1593 break;
1596 xtra_rows = new_rows - old_rows;
1597 if (xtra_rows < 0)
1599 /* May scroll text up. If there is not enough
1600 * remaining text or scrolling fails, must redraw the
1601 * rest. If scrolling works, must redraw the text
1602 * below the scrolled text. */
1603 if (row - xtra_rows >= wp->w_height - 2)
1604 mod_bot = MAXLNUM;
1605 else
1607 check_for_delay(FALSE);
1608 if (win_del_lines(wp, row,
1609 -xtra_rows, FALSE, FALSE) == FAIL)
1610 mod_bot = MAXLNUM;
1611 else
1612 bot_start = wp->w_height + xtra_rows;
1615 else if (xtra_rows > 0)
1617 /* May scroll text down. If there is not enough
1618 * remaining text of scrolling fails, must redraw the
1619 * rest. */
1620 if (row + xtra_rows >= wp->w_height - 2)
1621 mod_bot = MAXLNUM;
1622 else
1624 check_for_delay(FALSE);
1625 if (win_ins_lines(wp, row + old_rows,
1626 xtra_rows, FALSE, FALSE) == FAIL)
1627 mod_bot = MAXLNUM;
1628 else if (top_end > row + old_rows)
1629 /* Scrolled the part at the top that requires
1630 * updating down. */
1631 top_end += xtra_rows;
1635 /* When not updating the rest, may need to move w_lines[]
1636 * entries. */
1637 if (mod_bot != MAXLNUM && i != j)
1639 if (j < i)
1641 int x = row + new_rows;
1643 /* move entries in w_lines[] upwards */
1644 for (;;)
1646 /* stop at last valid entry in w_lines[] */
1647 if (i >= wp->w_lines_valid)
1649 wp->w_lines_valid = j;
1650 break;
1652 wp->w_lines[j] = wp->w_lines[i];
1653 /* stop at a line that won't fit */
1654 if (x + (int)wp->w_lines[j].wl_size
1655 > wp->w_height)
1657 wp->w_lines_valid = j + 1;
1658 break;
1660 x += wp->w_lines[j++].wl_size;
1661 ++i;
1663 if (bot_start > x)
1664 bot_start = x;
1666 else /* j > i */
1668 /* move entries in w_lines[] downwards */
1669 j -= i;
1670 wp->w_lines_valid += j;
1671 if (wp->w_lines_valid > wp->w_height)
1672 wp->w_lines_valid = wp->w_height;
1673 for (i = wp->w_lines_valid; i - j >= idx; --i)
1674 wp->w_lines[i] = wp->w_lines[i - j];
1676 /* The w_lines[] entries for inserted lines are
1677 * now invalid, but wl_size may be used above.
1678 * Reset to zero. */
1679 while (i >= idx)
1681 wp->w_lines[i].wl_size = 0;
1682 wp->w_lines[i--].wl_valid = FALSE;
1689 #ifdef FEAT_FOLDING
1691 * When lines are folded, display one line for all of them.
1692 * Otherwise, display normally (can be several display lines when
1693 * 'wrap' is on).
1695 fold_count = foldedCount(wp, lnum, &win_foldinfo);
1696 if (fold_count != 0)
1698 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
1699 ++row;
1700 --fold_count;
1701 wp->w_lines[idx].wl_folded = TRUE;
1702 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
1703 # ifdef FEAT_SYN_HL
1704 did_update = DID_FOLD;
1705 # endif
1707 else
1708 #endif
1709 if (idx < wp->w_lines_valid
1710 && wp->w_lines[idx].wl_valid
1711 && wp->w_lines[idx].wl_lnum == lnum
1712 && lnum > wp->w_topline
1713 && !(dy_flags & DY_LASTLINE)
1714 && srow + wp->w_lines[idx].wl_size > wp->w_height
1715 #ifdef FEAT_DIFF
1716 && diff_check_fill(wp, lnum) == 0
1717 #endif
1720 /* This line is not going to fit. Don't draw anything here,
1721 * will draw "@ " lines below. */
1722 row = wp->w_height + 1;
1724 else
1726 #ifdef FEAT_SEARCH_EXTRA
1727 prepare_search_hl(wp, lnum);
1728 #endif
1729 #ifdef FEAT_SYN_HL
1730 /* Let the syntax stuff know we skipped a few lines. */
1731 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
1732 && syntax_present(buf))
1733 syntax_end_parsing(syntax_last_parsed + 1);
1734 #endif
1737 * Display one line.
1739 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
1741 #ifdef FEAT_FOLDING
1742 wp->w_lines[idx].wl_folded = FALSE;
1743 wp->w_lines[idx].wl_lastlnum = lnum;
1744 #endif
1745 #ifdef FEAT_SYN_HL
1746 did_update = DID_LINE;
1747 syntax_last_parsed = lnum;
1748 #endif
1751 wp->w_lines[idx].wl_lnum = lnum;
1752 wp->w_lines[idx].wl_valid = TRUE;
1753 if (row > wp->w_height) /* past end of screen */
1755 /* we may need the size of that too long line later on */
1756 if (dollar_vcol == 0)
1757 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
1758 ++idx;
1759 break;
1761 if (dollar_vcol == 0)
1762 wp->w_lines[idx].wl_size = row - srow;
1763 ++idx;
1764 #ifdef FEAT_FOLDING
1765 lnum += fold_count + 1;
1766 #else
1767 ++lnum;
1768 #endif
1770 else
1772 /* This line does not need updating, advance to the next one */
1773 row += wp->w_lines[idx++].wl_size;
1774 if (row > wp->w_height) /* past end of screen */
1775 break;
1776 #ifdef FEAT_FOLDING
1777 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
1778 #else
1779 ++lnum;
1780 #endif
1781 #ifdef FEAT_SYN_HL
1782 did_update = DID_NONE;
1783 #endif
1786 if (lnum > buf->b_ml.ml_line_count)
1788 eof = TRUE;
1789 break;
1793 * End of loop over all window lines.
1797 if (idx > wp->w_lines_valid)
1798 wp->w_lines_valid = idx;
1800 #ifdef FEAT_SYN_HL
1802 * Let the syntax stuff know we stop parsing here.
1804 if (syntax_last_parsed != 0 && syntax_present(buf))
1805 syntax_end_parsing(syntax_last_parsed + 1);
1806 #endif
1809 * If we didn't hit the end of the file, and we didn't finish the last
1810 * line we were working on, then the line didn't fit.
1812 wp->w_empty_rows = 0;
1813 #ifdef FEAT_DIFF
1814 wp->w_filler_rows = 0;
1815 #endif
1816 if (!eof && !didline)
1818 if (lnum == wp->w_topline)
1821 * Single line that does not fit!
1822 * Don't overwrite it, it can be edited.
1824 wp->w_botline = lnum + 1;
1826 #ifdef FEAT_DIFF
1827 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
1829 /* Window ends in filler lines. */
1830 wp->w_botline = lnum;
1831 wp->w_filler_rows = wp->w_height - srow;
1833 #endif
1834 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
1837 * Last line isn't finished: Display "@@@" at the end.
1839 screen_fill(W_WINROW(wp) + wp->w_height - 1,
1840 W_WINROW(wp) + wp->w_height,
1841 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
1842 '@', '@', hl_attr(HLF_AT));
1843 set_empty_rows(wp, srow);
1844 wp->w_botline = lnum;
1846 else
1848 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
1849 wp->w_botline = lnum;
1852 else
1854 #ifdef FEAT_VERTSPLIT
1855 draw_vsep_win(wp, row);
1856 #endif
1857 if (eof) /* we hit the end of the file */
1859 wp->w_botline = buf->b_ml.ml_line_count + 1;
1860 #ifdef FEAT_DIFF
1861 j = diff_check_fill(wp, wp->w_botline);
1862 if (j > 0 && !wp->w_botfill)
1865 * Display filler lines at the end of the file
1867 if (char2cells(fill_diff) > 1)
1868 i = '-';
1869 else
1870 i = fill_diff;
1871 if (row + j > wp->w_height)
1872 j = wp->w_height - row;
1873 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
1874 row += j;
1876 #endif
1878 else if (dollar_vcol == 0)
1879 wp->w_botline = lnum;
1881 /* make sure the rest of the screen is blank */
1882 /* put '~'s on rows that aren't part of the file. */
1883 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_AT);
1886 /* Reset the type of redrawing required, the window has been updated. */
1887 wp->w_redr_type = 0;
1888 #ifdef FEAT_DIFF
1889 wp->w_old_topfill = wp->w_topfill;
1890 wp->w_old_botfill = wp->w_botfill;
1891 #endif
1893 if (dollar_vcol == 0)
1896 * There is a trick with w_botline. If we invalidate it on each
1897 * change that might modify it, this will cause a lot of expensive
1898 * calls to plines() in update_topline() each time. Therefore the
1899 * value of w_botline is often approximated, and this value is used to
1900 * compute the value of w_topline. If the value of w_botline was
1901 * wrong, check that the value of w_topline is correct (cursor is on
1902 * the visible part of the text). If it's not, we need to redraw
1903 * again. Mostly this just means scrolling up a few lines, so it
1904 * doesn't look too bad. Only do this for the current window (where
1905 * changes are relevant).
1907 wp->w_valid |= VALID_BOTLINE;
1908 if (wp == curwin && wp->w_botline != old_botline && !recursive)
1910 recursive = TRUE;
1911 curwin->w_valid &= ~VALID_TOPLINE;
1912 update_topline(); /* may invalidate w_botline again */
1913 if (must_redraw != 0)
1915 /* Don't update for changes in buffer again. */
1916 i = curbuf->b_mod_set;
1917 curbuf->b_mod_set = FALSE;
1918 win_update(curwin);
1919 must_redraw = 0;
1920 curbuf->b_mod_set = i;
1922 recursive = FALSE;
1926 #if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1927 /* restore got_int, unless CTRL-C was hit while redrawing */
1928 if (!got_int)
1929 got_int = save_got_int;
1930 #endif
1933 #ifdef FEAT_SIGNS
1934 static int draw_signcolumn __ARGS((win_T *wp));
1937 * Return TRUE when window "wp" has a column to draw signs in.
1939 static int
1940 draw_signcolumn(wp)
1941 win_T *wp;
1943 return (wp->w_buffer->b_signlist != NULL
1944 # ifdef FEAT_NETBEANS_INTG
1945 || usingNetbeans
1946 # endif
1949 #endif
1952 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
1953 * as the filler character.
1955 static void
1956 win_draw_end(wp, c1, c2, row, endrow, hl)
1957 win_T *wp;
1958 int c1;
1959 int c2;
1960 int row;
1961 int endrow;
1962 hlf_T hl;
1964 #if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
1965 int n = 0;
1966 # define FDC_OFF n
1967 #else
1968 # define FDC_OFF 0
1969 #endif
1971 #ifdef FEAT_RIGHTLEFT
1972 if (wp->w_p_rl)
1974 /* No check for cmdline window: should never be right-left. */
1975 # ifdef FEAT_FOLDING
1976 n = wp->w_p_fdc;
1978 if (n > 0)
1980 /* draw the fold column at the right */
1981 if (n > W_WIDTH(wp))
1982 n = W_WIDTH(wp);
1983 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
1984 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
1985 ' ', ' ', hl_attr(HLF_FC));
1987 # endif
1988 # ifdef FEAT_SIGNS
1989 if (draw_signcolumn(wp))
1991 int nn = n + 2;
1993 /* draw the sign column left of the fold column */
1994 if (nn > W_WIDTH(wp))
1995 nn = W_WIDTH(wp);
1996 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
1997 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
1998 ' ', ' ', hl_attr(HLF_SC));
1999 n = nn;
2001 # endif
2002 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2003 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
2004 c2, c2, hl_attr(hl));
2005 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2006 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
2007 c1, c2, hl_attr(hl));
2009 else
2010 #endif
2012 #ifdef FEAT_CMDWIN
2013 if (cmdwin_type != 0 && wp == curwin)
2015 /* draw the cmdline character in the leftmost column */
2016 n = 1;
2017 if (n > wp->w_width)
2018 n = wp->w_width;
2019 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2020 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
2021 cmdwin_type, ' ', hl_attr(HLF_AT));
2023 #endif
2024 #ifdef FEAT_FOLDING
2025 if (wp->w_p_fdc > 0)
2027 int nn = n + wp->w_p_fdc;
2029 /* draw the fold column at the left */
2030 if (nn > W_WIDTH(wp))
2031 nn = W_WIDTH(wp);
2032 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2033 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2034 ' ', ' ', hl_attr(HLF_FC));
2035 n = nn;
2037 #endif
2038 #ifdef FEAT_SIGNS
2039 if (draw_signcolumn(wp))
2041 int nn = n + 2;
2043 /* draw the sign column after the fold column */
2044 if (nn > W_WIDTH(wp))
2045 nn = W_WIDTH(wp);
2046 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2047 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2048 ' ', ' ', hl_attr(HLF_SC));
2049 n = nn;
2051 #endif
2052 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2053 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
2054 c1, c2, hl_attr(hl));
2056 set_empty_rows(wp, row);
2059 #ifdef FEAT_FOLDING
2061 * Display one folded line.
2063 static void
2064 fold_line(wp, fold_count, foldinfo, lnum, row)
2065 win_T *wp;
2066 long fold_count;
2067 foldinfo_T *foldinfo;
2068 linenr_T lnum;
2069 int row;
2071 char_u buf[51];
2072 pos_T *top, *bot;
2073 linenr_T lnume = lnum + fold_count - 1;
2074 int len;
2075 char_u *text;
2076 int fdc;
2077 int col;
2078 int txtcol;
2079 int off = (int)(current_ScreenLine - ScreenLines);
2080 int ri;
2082 /* Build the fold line:
2083 * 1. Add the cmdwin_type for the command-line window
2084 * 2. Add the 'foldcolumn'
2085 * 3. Add the 'number' column
2086 * 4. Compose the text
2087 * 5. Add the text
2088 * 6. set highlighting for the Visual area an other text
2090 col = 0;
2093 * 1. Add the cmdwin_type for the command-line window
2094 * Ignores 'rightleft', this window is never right-left.
2096 #ifdef FEAT_CMDWIN
2097 if (cmdwin_type != 0 && wp == curwin)
2099 ScreenLines[off] = cmdwin_type;
2100 ScreenAttrs[off] = hl_attr(HLF_AT);
2101 #ifdef FEAT_MBYTE
2102 if (enc_utf8)
2103 ScreenLinesUC[off] = 0;
2104 #endif
2105 ++col;
2107 #endif
2110 * 2. Add the 'foldcolumn'
2112 fdc = wp->w_p_fdc;
2113 if (fdc > W_WIDTH(wp) - col)
2114 fdc = W_WIDTH(wp) - col;
2115 if (fdc > 0)
2117 fill_foldcolumn(buf, wp, TRUE, lnum);
2118 #ifdef FEAT_RIGHTLEFT
2119 if (wp->w_p_rl)
2121 int i;
2123 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
2124 hl_attr(HLF_FC));
2125 /* reverse the fold column */
2126 for (i = 0; i < fdc; ++i)
2127 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2129 else
2130 #endif
2131 copy_text_attr(off + col, buf, fdc, hl_attr(HLF_FC));
2132 col += fdc;
2135 #ifdef FEAT_RIGHTLEFT
2136 # define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2137 for (ri = 0; ri < l; ++ri) \
2138 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2139 else \
2140 for (ri = 0; ri < l; ++ri) \
2141 ScreenAttrs[off + (p) + ri] = v
2142 #else
2143 # define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2144 ScreenAttrs[off + (p) + ri] = v
2145 #endif
2147 /* Set all attributes of the 'number' column and the text */
2148 RL_MEMSET(col, hl_attr(HLF_FL), W_WIDTH(wp) - col);
2150 #ifdef FEAT_SIGNS
2151 /* If signs are being displayed, add two spaces. */
2152 if (draw_signcolumn(wp))
2154 len = W_WIDTH(wp) - col;
2155 if (len > 0)
2157 if (len > 2)
2158 len = 2;
2159 # ifdef FEAT_RIGHTLEFT
2160 if (wp->w_p_rl)
2161 /* the line number isn't reversed */
2162 copy_text_attr(off + W_WIDTH(wp) - len - col,
2163 (char_u *)" ", len, hl_attr(HLF_FL));
2164 else
2165 # endif
2166 copy_text_attr(off + col, (char_u *)" ", len, hl_attr(HLF_FL));
2167 col += len;
2170 #endif
2173 * 3. Add the 'number' column
2175 if (wp->w_p_nu)
2177 len = W_WIDTH(wp) - col;
2178 if (len > 0)
2180 int w = number_width(wp);
2182 if (len > w + 1)
2183 len = w + 1;
2184 sprintf((char *)buf, "%*ld ", w, (long)lnum);
2185 #ifdef FEAT_RIGHTLEFT
2186 if (wp->w_p_rl)
2187 /* the line number isn't reversed */
2188 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
2189 hl_attr(HLF_FL));
2190 else
2191 #endif
2192 copy_text_attr(off + col, buf, len, hl_attr(HLF_FL));
2193 col += len;
2198 * 4. Compose the folded-line string with 'foldtext', if set.
2200 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
2202 txtcol = col; /* remember where text starts */
2205 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2206 * Right-left text is put in columns 0 - number-col, normal text is put
2207 * in columns number-col - window-width.
2209 #ifdef FEAT_MBYTE
2210 if (has_mbyte)
2212 int cells;
2213 int u8c, u8cc[MAX_MCO];
2214 int i;
2215 int idx;
2216 int c_len;
2217 char_u *p;
2218 # ifdef FEAT_ARABIC
2219 int prev_c = 0; /* previous Arabic character */
2220 int prev_c1 = 0; /* first composing char for prev_c */
2221 # endif
2223 # ifdef FEAT_RIGHTLEFT
2224 if (wp->w_p_rl)
2225 idx = off;
2226 else
2227 # endif
2228 idx = off + col;
2230 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2231 for (p = text; *p != NUL; )
2233 cells = (*mb_ptr2cells)(p);
2234 c_len = (*mb_ptr2len)(p);
2235 if (col + cells > W_WIDTH(wp)
2236 # ifdef FEAT_RIGHTLEFT
2237 - (wp->w_p_rl ? col : 0)
2238 # endif
2240 break;
2241 ScreenLines[idx] = *p;
2242 if (enc_utf8)
2244 u8c = utfc_ptr2char(p, u8cc);
2245 if (*p < 0x80 && u8cc[0] == 0)
2247 ScreenLinesUC[idx] = 0;
2248 #ifdef FEAT_ARABIC
2249 prev_c = u8c;
2250 #endif
2252 else
2254 #ifdef FEAT_ARABIC
2255 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2257 /* Do Arabic shaping. */
2258 int pc, pc1, nc;
2259 int pcc[MAX_MCO];
2260 int firstbyte = *p;
2262 /* The idea of what is the previous and next
2263 * character depends on 'rightleft'. */
2264 if (wp->w_p_rl)
2266 pc = prev_c;
2267 pc1 = prev_c1;
2268 nc = utf_ptr2char(p + c_len);
2269 prev_c1 = u8cc[0];
2271 else
2273 pc = utfc_ptr2char(p + c_len, pcc);
2274 nc = prev_c;
2275 pc1 = pcc[0];
2277 prev_c = u8c;
2279 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2280 pc, pc1, nc);
2281 ScreenLines[idx] = firstbyte;
2283 else
2284 prev_c = u8c;
2285 #endif
2286 /* Non-BMP character: display as ? or fullwidth ?. */
2287 if (u8c >= 0x10000)
2288 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2289 else
2290 ScreenLinesUC[idx] = u8c;
2291 for (i = 0; i < Screen_mco; ++i)
2293 ScreenLinesC[i][idx] = u8cc[i];
2294 if (u8cc[i] == 0)
2295 break;
2298 if (cells > 1)
2299 ScreenLines[idx + 1] = 0;
2301 else if (cells > 1) /* double-byte character */
2303 if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2304 ScreenLines2[idx] = p[1];
2305 else
2306 ScreenLines[idx + 1] = p[1];
2308 col += cells;
2309 idx += cells;
2310 p += c_len;
2313 else
2314 #endif
2316 len = (int)STRLEN(text);
2317 if (len > W_WIDTH(wp) - col)
2318 len = W_WIDTH(wp) - col;
2319 if (len > 0)
2321 #ifdef FEAT_RIGHTLEFT
2322 if (wp->w_p_rl)
2323 STRNCPY(current_ScreenLine, text, len);
2324 else
2325 #endif
2326 STRNCPY(current_ScreenLine + col, text, len);
2327 col += len;
2331 /* Fill the rest of the line with the fold filler */
2332 #ifdef FEAT_RIGHTLEFT
2333 if (wp->w_p_rl)
2334 col -= txtcol;
2335 #endif
2336 while (col < W_WIDTH(wp)
2337 #ifdef FEAT_RIGHTLEFT
2338 - (wp->w_p_rl ? txtcol : 0)
2339 #endif
2342 #ifdef FEAT_MBYTE
2343 if (enc_utf8)
2345 if (fill_fold >= 0x80)
2347 ScreenLinesUC[off + col] = fill_fold;
2348 ScreenLinesC[0][off + col] = 0;
2350 else
2351 ScreenLinesUC[off + col] = 0;
2353 #endif
2354 ScreenLines[off + col++] = fill_fold;
2357 if (text != buf)
2358 vim_free(text);
2361 * 6. set highlighting for the Visual area an other text.
2362 * If all folded lines are in the Visual area, highlight the line.
2364 #ifdef FEAT_VISUAL
2365 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2367 if (ltoreq(curwin->w_cursor, VIsual))
2369 /* Visual is after curwin->w_cursor */
2370 top = &curwin->w_cursor;
2371 bot = &VIsual;
2373 else
2375 /* Visual is before curwin->w_cursor */
2376 top = &VIsual;
2377 bot = &curwin->w_cursor;
2379 if (lnum >= top->lnum
2380 && lnume <= bot->lnum
2381 && (VIsual_mode != 'v'
2382 || ((lnum > top->lnum
2383 || (lnum == top->lnum
2384 && top->col == 0))
2385 && (lnume < bot->lnum
2386 || (lnume == bot->lnum
2387 && (bot->col - (*p_sel == 'e'))
2388 >= STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
2390 if (VIsual_mode == Ctrl_V)
2392 /* Visual block mode: highlight the chars part of the block */
2393 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2395 if (wp->w_old_cursor_lcol + txtcol < (colnr_T)W_WIDTH(wp))
2396 len = wp->w_old_cursor_lcol;
2397 else
2398 len = W_WIDTH(wp) - txtcol;
2399 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, hl_attr(HLF_V),
2400 len - (int)wp->w_old_cursor_fcol);
2403 else
2405 /* Set all attributes of the text */
2406 RL_MEMSET(txtcol, hl_attr(HLF_V), W_WIDTH(wp) - txtcol);
2410 #endif
2412 #ifdef FEAT_SYN_HL
2413 /* Show 'cursorcolumn' in the fold line. */
2414 if (wp->w_p_cuc && (int)wp->w_virtcol + txtcol < W_WIDTH(wp))
2415 ScreenAttrs[off + wp->w_virtcol + txtcol] = hl_combine_attr(
2416 ScreenAttrs[off + wp->w_virtcol + txtcol], hl_attr(HLF_CUC));
2417 #endif
2419 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2420 (int)W_WIDTH(wp), FALSE);
2423 * Update w_cline_height and w_cline_folded if the cursor line was
2424 * updated (saves a call to plines() later).
2426 if (wp == curwin
2427 && lnum <= curwin->w_cursor.lnum
2428 && lnume >= curwin->w_cursor.lnum)
2430 curwin->w_cline_row = row;
2431 curwin->w_cline_height = 1;
2432 curwin->w_cline_folded = TRUE;
2433 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2438 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2440 static void
2441 copy_text_attr(off, buf, len, attr)
2442 int off;
2443 char_u *buf;
2444 int len;
2445 int attr;
2447 int i;
2449 mch_memmove(ScreenLines + off, buf, (size_t)len);
2450 # ifdef FEAT_MBYTE
2451 if (enc_utf8)
2452 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2453 # endif
2454 for (i = 0; i < len; ++i)
2455 ScreenAttrs[off + i] = attr;
2459 * Fill the foldcolumn at "p" for window "wp".
2460 * Only to be called when 'foldcolumn' > 0.
2462 static void
2463 fill_foldcolumn(p, wp, closed, lnum)
2464 char_u *p;
2465 win_T *wp;
2466 int closed; /* TRUE of FALSE */
2467 linenr_T lnum; /* current line number */
2469 int i = 0;
2470 int level;
2471 int first_level;
2472 int empty;
2474 /* Init to all spaces. */
2475 copy_spaces(p, (size_t)wp->w_p_fdc);
2477 level = win_foldinfo.fi_level;
2478 if (level > 0)
2480 /* If there is only one column put more info in it. */
2481 empty = (wp->w_p_fdc == 1) ? 0 : 1;
2483 /* If the column is too narrow, we start at the lowest level that
2484 * fits and use numbers to indicated the depth. */
2485 first_level = level - wp->w_p_fdc - closed + 1 + empty;
2486 if (first_level < 1)
2487 first_level = 1;
2489 for (i = 0; i + empty < wp->w_p_fdc; ++i)
2491 if (win_foldinfo.fi_lnum == lnum
2492 && first_level + i >= win_foldinfo.fi_low_level)
2493 p[i] = '-';
2494 else if (first_level == 1)
2495 p[i] = '|';
2496 else if (first_level + i <= 9)
2497 p[i] = '0' + first_level + i;
2498 else
2499 p[i] = '>';
2500 if (first_level + i == level)
2501 break;
2504 if (closed)
2505 p[i >= wp->w_p_fdc ? i - 1 : i] = '+';
2507 #endif /* FEAT_FOLDING */
2510 * Display line "lnum" of window 'wp' on the screen.
2511 * Start at row "startrow", stop when "endrow" is reached.
2512 * wp->w_virtcol needs to be valid.
2514 * Return the number of last row the line occupies.
2516 /* ARGSUSED */
2517 static int
2518 win_line(wp, lnum, startrow, endrow, nochange)
2519 win_T *wp;
2520 linenr_T lnum;
2521 int startrow;
2522 int endrow;
2523 int nochange; /* not updating for changed text */
2525 int col; /* visual column on screen */
2526 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2527 int c = 0; /* init for GCC */
2528 long vcol = 0; /* virtual column (for tabs) */
2529 long vcol_prev = -1; /* "vcol" of previous character */
2530 char_u *line; /* current line */
2531 char_u *ptr; /* current position in "line" */
2532 int row; /* row in the window, excl w_winrow */
2533 int screen_row; /* row on the screen, incl w_winrow */
2535 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2536 int n_extra = 0; /* number of extra chars */
2537 char_u *p_extra = NULL; /* string of extra chars */
2538 int c_extra = NUL; /* extra chars, all the same */
2539 int extra_attr = 0; /* attributes when n_extra != 0 */
2540 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2541 displaying lcs_eol at end-of-line */
2542 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2543 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2545 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2546 int saved_n_extra = 0;
2547 char_u *saved_p_extra = NULL;
2548 int saved_c_extra = 0;
2549 int saved_char_attr = 0;
2551 int n_attr = 0; /* chars with special attr */
2552 int saved_attr2 = 0; /* char_attr saved for n_attr */
2553 int n_attr3 = 0; /* chars with overruling special attr */
2554 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2556 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2558 int fromcol, tocol; /* start/end of inverting */
2559 int fromcol_prev = -2; /* start of inverting after cursor */
2560 int noinvcur = FALSE; /* don't invert the cursor */
2561 #ifdef FEAT_VISUAL
2562 pos_T *top, *bot;
2563 #endif
2564 pos_T pos;
2565 long v;
2567 int char_attr = 0; /* attributes for next character */
2568 int attr_pri = FALSE; /* char_attr has priority */
2569 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2570 in this line */
2571 int attr = 0; /* attributes for area highlighting */
2572 int area_attr = 0; /* attributes desired by highlighting */
2573 int search_attr = 0; /* attributes desired by 'hlsearch' */
2574 #ifdef FEAT_SYN_HL
2575 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
2576 int syntax_attr = 0; /* attributes desired by syntax */
2577 int has_syntax = FALSE; /* this buffer has syntax highl. */
2578 int save_did_emsg;
2579 #endif
2580 #ifdef FEAT_SPELL
2581 int has_spell = FALSE; /* this buffer has spell checking */
2582 # define SPWORDLEN 150
2583 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
2584 int nextlinecol = 0; /* column where nextline[] starts */
2585 int nextline_idx = 0; /* index in nextline[] where next line
2586 starts */
2587 int spell_attr = 0; /* attributes desired by spelling */
2588 int word_end = 0; /* last byte with same spell_attr */
2589 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2590 static int checked_col = 0; /* column in "checked_lnum" up to which
2591 * there are no spell errors */
2592 static int cap_col = -1; /* column to check for Cap word */
2593 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
2594 int cur_checked_col = 0; /* checked column for current line */
2595 #endif
2596 int extra_check; /* has syntax or linebreak */
2597 #ifdef FEAT_MBYTE
2598 int multi_attr = 0; /* attributes desired by multibyte */
2599 int mb_l = 1; /* multi-byte byte length */
2600 int mb_c = 0; /* decoded multi-byte character */
2601 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
2602 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
2603 #endif
2604 #ifdef FEAT_DIFF
2605 int filler_lines; /* nr of filler lines to be drawn */
2606 int filler_todo; /* nr of filler lines still to do + 1 */
2607 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
2608 int change_start = MAXCOL; /* first col of changed area */
2609 int change_end = -1; /* last col of changed area */
2610 #endif
2611 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
2612 #ifdef FEAT_LINEBREAK
2613 int need_showbreak = FALSE;
2614 #endif
2615 #if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
2616 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
2617 # define LINE_ATTR
2618 int line_attr = 0; /* atrribute for the whole line */
2619 #endif
2620 #ifdef FEAT_SEARCH_EXTRA
2621 match_T *shl; /* points to search_hl or match_hl */
2622 #endif
2623 #if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_MBYTE)
2624 int i;
2625 #endif
2626 #ifdef FEAT_ARABIC
2627 int prev_c = 0; /* previous Arabic character */
2628 int prev_c1 = 0; /* first composing char for prev_c */
2629 #endif
2630 #if defined(LINE_ATTR)
2631 int did_line_attr = 0;
2632 #endif
2634 /* draw_state: items that are drawn in sequence: */
2635 #define WL_START 0 /* nothing done yet */
2636 #ifdef FEAT_CMDWIN
2637 # define WL_CMDLINE WL_START + 1 /* cmdline window column */
2638 #else
2639 # define WL_CMDLINE WL_START
2640 #endif
2641 #ifdef FEAT_FOLDING
2642 # define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
2643 #else
2644 # define WL_FOLD WL_CMDLINE
2645 #endif
2646 #ifdef FEAT_SIGNS
2647 # define WL_SIGN WL_FOLD + 1 /* column for signs */
2648 #else
2649 # define WL_SIGN WL_FOLD /* column for signs */
2650 #endif
2651 #define WL_NR WL_SIGN + 1 /* line number */
2652 #if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
2653 # define WL_SBR WL_NR + 1 /* 'showbreak' or 'diff' */
2654 #else
2655 # define WL_SBR WL_NR
2656 #endif
2657 #define WL_LINE WL_SBR + 1 /* text in the line */
2658 int draw_state = WL_START; /* what to draw next */
2659 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2660 int feedback_col = 0;
2661 int feedback_old_attr = -1;
2662 #endif
2665 if (startrow > endrow) /* past the end already! */
2666 return startrow;
2668 row = startrow;
2669 screen_row = row + W_WINROW(wp);
2672 * To speed up the loop below, set extra_check when there is linebreak,
2673 * trailing white space and/or syntax processing to be done.
2675 #ifdef FEAT_LINEBREAK
2676 extra_check = wp->w_p_lbr;
2677 #else
2678 extra_check = 0;
2679 #endif
2680 #ifdef FEAT_SYN_HL
2681 if (syntax_present(wp->w_buffer) && !wp->w_buffer->b_syn_error)
2683 /* Prepare for syntax highlighting in this line. When there is an
2684 * error, stop syntax highlighting. */
2685 save_did_emsg = did_emsg;
2686 did_emsg = FALSE;
2687 syntax_start(wp, lnum);
2688 if (did_emsg)
2689 wp->w_buffer->b_syn_error = TRUE;
2690 else
2692 did_emsg = save_did_emsg;
2693 has_syntax = TRUE;
2694 extra_check = TRUE;
2697 #endif
2699 #ifdef FEAT_SPELL
2700 if (wp->w_p_spell
2701 && *wp->w_buffer->b_p_spl != NUL
2702 && wp->w_buffer->b_langp.ga_len > 0
2703 && *(char **)(wp->w_buffer->b_langp.ga_data) != NULL)
2705 /* Prepare for spell checking. */
2706 has_spell = TRUE;
2707 extra_check = TRUE;
2709 /* Get the start of the next line, so that words that wrap to the next
2710 * line are found too: "et<line-break>al.".
2711 * Trick: skip a few chars for C/shell/Vim comments */
2712 nextline[SPWORDLEN] = NUL;
2713 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2715 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
2716 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
2719 /* When a word wrapped from the previous line the start of the current
2720 * line is valid. */
2721 if (lnum == checked_lnum)
2722 cur_checked_col = checked_col;
2723 checked_lnum = 0;
2725 /* When there was a sentence end in the previous line may require a
2726 * word starting with capital in this line. In line 1 always check
2727 * the first word. */
2728 if (lnum != capcol_lnum)
2729 cap_col = -1;
2730 if (lnum == 1)
2731 cap_col = 0;
2732 capcol_lnum = 0;
2734 #endif
2737 * handle visual active in this window
2739 fromcol = -10;
2740 tocol = MAXCOL;
2741 #ifdef FEAT_VISUAL
2742 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2744 /* Visual is after curwin->w_cursor */
2745 if (ltoreq(curwin->w_cursor, VIsual))
2747 top = &curwin->w_cursor;
2748 bot = &VIsual;
2750 else /* Visual is before curwin->w_cursor */
2752 top = &VIsual;
2753 bot = &curwin->w_cursor;
2755 if (VIsual_mode == Ctrl_V) /* block mode */
2757 if (lnum >= top->lnum && lnum <= bot->lnum)
2759 fromcol = wp->w_old_cursor_fcol;
2760 tocol = wp->w_old_cursor_lcol;
2763 else /* non-block mode */
2765 if (lnum > top->lnum && lnum <= bot->lnum)
2766 fromcol = 0;
2767 else if (lnum == top->lnum)
2769 if (VIsual_mode == 'V') /* linewise */
2770 fromcol = 0;
2771 else
2773 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
2774 if (gchar_pos(top) == NUL)
2775 tocol = fromcol + 1;
2778 if (VIsual_mode != 'V' && lnum == bot->lnum)
2780 if (*p_sel == 'e' && bot->col == 0
2781 #ifdef FEAT_VIRTUALEDIT
2782 && bot->coladd == 0
2783 #endif
2786 fromcol = -10;
2787 tocol = MAXCOL;
2789 else if (bot->col == MAXCOL)
2790 tocol = MAXCOL;
2791 else
2793 pos = *bot;
2794 if (*p_sel == 'e')
2795 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
2796 else
2798 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
2799 ++tocol;
2805 #ifndef MSDOS
2806 /* Check if the character under the cursor should not be inverted */
2807 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
2808 # ifdef FEAT_GUI
2809 && !gui.in_use
2810 # endif
2812 noinvcur = TRUE;
2813 #endif
2815 /* if inverting in this line set area_highlighting */
2816 if (fromcol >= 0)
2818 area_highlighting = TRUE;
2819 attr = hl_attr(HLF_V);
2820 #if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
2821 if (clip_star.available && !clip_star.owned && clip_isautosel())
2822 attr = hl_attr(HLF_VNC);
2823 #endif
2828 * handle 'incsearch' and ":s///c" highlighting
2830 else
2831 #endif /* FEAT_VISUAL */
2832 if (highlight_match
2833 && wp == curwin
2834 && lnum >= curwin->w_cursor.lnum
2835 && lnum <= curwin->w_cursor.lnum + search_match_lines)
2837 if (lnum == curwin->w_cursor.lnum)
2838 getvcol(curwin, &(curwin->w_cursor),
2839 (colnr_T *)&fromcol, NULL, NULL);
2840 else
2841 fromcol = 0;
2842 if (lnum == curwin->w_cursor.lnum + search_match_lines)
2844 pos.lnum = lnum;
2845 pos.col = search_match_endcol;
2846 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
2848 else
2849 tocol = MAXCOL;
2850 if (fromcol == tocol) /* do at least one character */
2851 tocol = fromcol + 1; /* happens when past end of line */
2852 area_highlighting = TRUE;
2853 attr = hl_attr(HLF_I);
2856 #ifdef FEAT_DIFF
2857 filler_lines = diff_check(wp, lnum);
2858 if (filler_lines < 0)
2860 if (filler_lines == -1)
2862 if (diff_find_change(wp, lnum, &change_start, &change_end))
2863 diff_hlf = HLF_ADD; /* added line */
2864 else if (change_start == 0)
2865 diff_hlf = HLF_TXD; /* changed text */
2866 else
2867 diff_hlf = HLF_CHD; /* changed line */
2869 else
2870 diff_hlf = HLF_ADD; /* added line */
2871 filler_lines = 0;
2872 area_highlighting = TRUE;
2874 if (lnum == wp->w_topline)
2875 filler_lines = wp->w_topfill;
2876 filler_todo = filler_lines;
2877 #endif
2879 #ifdef LINE_ATTR
2880 # ifdef FEAT_SIGNS
2881 /* If this line has a sign with line highlighting set line_attr. */
2882 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
2883 if (v != 0)
2884 line_attr = sign_get_attr((int)v, TRUE);
2885 # endif
2886 # if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
2887 /* Highlight the current line in the quickfix window. */
2888 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
2889 line_attr = hl_attr(HLF_L);
2890 # endif
2891 if (line_attr != 0)
2892 area_highlighting = TRUE;
2893 #endif
2895 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
2896 ptr = line;
2898 #ifdef FEAT_SPELL
2899 if (has_spell)
2901 /* For checking first word with a capital skip white space. */
2902 if (cap_col == 0)
2903 cap_col = (int)(skipwhite(line) - line);
2905 /* To be able to spell-check over line boundaries copy the end of the
2906 * current line into nextline[]. Above the start of the next line was
2907 * copied to nextline[SPWORDLEN]. */
2908 if (nextline[SPWORDLEN] == NUL)
2910 /* No next line or it is empty. */
2911 nextlinecol = MAXCOL;
2912 nextline_idx = 0;
2914 else
2916 v = (long)STRLEN(line);
2917 if (v < SPWORDLEN)
2919 /* Short line, use it completely and append the start of the
2920 * next line. */
2921 nextlinecol = 0;
2922 mch_memmove(nextline, line, (size_t)v);
2923 mch_memmove(nextline + v, nextline + SPWORDLEN,
2924 STRLEN(nextline + SPWORDLEN) + 1);
2925 nextline_idx = v + 1;
2927 else
2929 /* Long line, use only the last SPWORDLEN bytes. */
2930 nextlinecol = v - SPWORDLEN;
2931 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
2932 nextline_idx = SPWORDLEN + 1;
2936 #endif
2938 /* find start of trailing whitespace */
2939 if (wp->w_p_list && lcs_trail)
2941 trailcol = (colnr_T)STRLEN(ptr);
2942 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
2943 --trailcol;
2944 trailcol += (colnr_T) (ptr - line);
2945 extra_check = TRUE;
2949 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
2950 * first character to be displayed.
2952 if (wp->w_p_wrap)
2953 v = wp->w_skipcol;
2954 else
2955 v = wp->w_leftcol;
2956 if (v > 0)
2958 #ifdef FEAT_MBYTE
2959 char_u *prev_ptr = ptr;
2960 #endif
2961 while (vcol < v && *ptr != NUL)
2963 c = win_lbr_chartabsize(wp, ptr, (colnr_T)vcol, NULL);
2964 vcol += c;
2965 #ifdef FEAT_MBYTE
2966 prev_ptr = ptr;
2967 #endif
2968 mb_ptr_adv(ptr);
2971 #ifdef FEAT_VIRTUALEDIT
2972 /* When 'virtualedit' is set the end of the line may be before the
2973 * start of the displayed part. */
2974 if (vcol < v && *ptr == NUL && virtual_active())
2975 vcol = v;
2976 #endif
2978 /* Handle a character that's not completely on the screen: Put ptr at
2979 * that character but skip the first few screen characters. */
2980 if (vcol > v)
2982 vcol -= c;
2983 #ifdef FEAT_MBYTE
2984 ptr = prev_ptr;
2985 #else
2986 --ptr;
2987 #endif
2988 n_skip = v - vcol;
2992 * Adjust for when the inverted text is before the screen,
2993 * and when the start of the inverted text is before the screen.
2995 if (tocol <= vcol)
2996 fromcol = 0;
2997 else if (fromcol >= 0 && fromcol < vcol)
2998 fromcol = vcol;
3000 #ifdef FEAT_LINEBREAK
3001 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3002 if (wp->w_p_wrap)
3003 need_showbreak = TRUE;
3004 #endif
3005 #ifdef FEAT_SPELL
3006 /* When spell checking a word we need to figure out the start of the
3007 * word and if it's badly spelled or not. */
3008 if (has_spell)
3010 int len;
3011 hlf_T spell_hlf = HLF_COUNT;
3013 pos = wp->w_cursor;
3014 wp->w_cursor.lnum = lnum;
3015 wp->w_cursor.col = (colnr_T)(ptr - line);
3016 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
3017 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
3019 /* no bad word found at line start, don't check until end of a
3020 * word */
3021 spell_hlf = HLF_COUNT;
3022 word_end = (int)(spell_to_word_end(ptr, wp->w_buffer) - line + 1);
3024 else
3026 /* bad word found, use attributes until end of word */
3027 word_end = wp->w_cursor.col + len + 1;
3029 /* Turn index into actual attributes. */
3030 if (spell_hlf != HLF_COUNT)
3031 spell_attr = highlight_attr[spell_hlf];
3033 wp->w_cursor = pos;
3035 # ifdef FEAT_SYN_HL
3036 /* Need to restart syntax highlighting for this line. */
3037 if (has_syntax)
3038 syntax_start(wp, lnum);
3039 # endif
3041 #endif
3045 * Correct highlighting for cursor that can't be disabled.
3046 * Avoids having to check this for each character.
3048 if (fromcol >= 0)
3050 if (noinvcur)
3052 if ((colnr_T)fromcol == wp->w_virtcol)
3054 /* highlighting starts at cursor, let it start just after the
3055 * cursor */
3056 fromcol_prev = fromcol;
3057 fromcol = -1;
3059 else if ((colnr_T)fromcol < wp->w_virtcol)
3060 /* restart highlighting after the cursor */
3061 fromcol_prev = wp->w_virtcol;
3063 if (fromcol >= tocol)
3064 fromcol = -1;
3067 #ifdef FEAT_SEARCH_EXTRA
3069 * Handle highlighting the last used search pattern and ":match".
3070 * Do this for both search_hl and match_hl[3].
3072 for (i = 3; i >= 0; --i)
3074 shl = (i == 3) ? &search_hl : &match_hl[i];
3075 shl->startcol = MAXCOL;
3076 shl->endcol = MAXCOL;
3077 shl->attr_cur = 0;
3078 if (shl->rm.regprog != NULL)
3080 v = (long)(ptr - line);
3081 next_search_hl(wp, shl, lnum, (colnr_T)v);
3083 /* Need to get the line again, a multi-line regexp may have made it
3084 * invalid. */
3085 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3086 ptr = line + v;
3088 if (shl->lnum != 0 && shl->lnum <= lnum)
3090 if (shl->lnum == lnum)
3091 shl->startcol = shl->rm.startpos[0].col;
3092 else
3093 shl->startcol = 0;
3094 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3095 - shl->rm.startpos[0].lnum)
3096 shl->endcol = shl->rm.endpos[0].col;
3097 else
3098 shl->endcol = MAXCOL;
3099 /* Highlight one character for an empty match. */
3100 if (shl->startcol == shl->endcol)
3102 #ifdef FEAT_MBYTE
3103 if (has_mbyte && line[shl->endcol] != NUL)
3104 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3105 else
3106 #endif
3107 ++shl->endcol;
3109 if ((long)shl->startcol < v) /* match at leftcol */
3111 shl->attr_cur = shl->attr;
3112 search_attr = shl->attr;
3114 area_highlighting = TRUE;
3118 #endif
3120 #ifdef FEAT_SYN_HL
3121 /* Cursor line highlighting for 'cursorline'. Not when Visual mode is
3122 * active, because it's not clear what is selected then. */
3123 if (wp->w_p_cul && lnum == wp->w_cursor.lnum && !VIsual_active)
3125 line_attr = hl_attr(HLF_CUL);
3126 area_highlighting = TRUE;
3128 #endif
3130 off = (unsigned)(current_ScreenLine - ScreenLines);
3131 col = 0;
3132 #ifdef FEAT_RIGHTLEFT
3133 if (wp->w_p_rl)
3135 /* Rightleft window: process the text in the normal direction, but put
3136 * it in current_ScreenLine[] from right to left. Start at the
3137 * rightmost column of the window. */
3138 col = W_WIDTH(wp) - 1;
3139 off += col;
3141 #endif
3144 * Repeat for the whole displayed line.
3146 for (;;)
3148 /* Skip this quickly when working on the text. */
3149 if (draw_state != WL_LINE)
3151 #ifdef FEAT_CMDWIN
3152 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3154 draw_state = WL_CMDLINE;
3155 if (cmdwin_type != 0 && wp == curwin)
3157 /* Draw the cmdline character. */
3158 *extra = cmdwin_type;
3159 n_extra = 1;
3160 p_extra = extra;
3161 c_extra = NUL;
3162 char_attr = hl_attr(HLF_AT);
3165 #endif
3167 #ifdef FEAT_FOLDING
3168 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3170 draw_state = WL_FOLD;
3171 if (wp->w_p_fdc > 0)
3173 /* Draw the 'foldcolumn'. */
3174 fill_foldcolumn(extra, wp, FALSE, lnum);
3175 n_extra = wp->w_p_fdc;
3176 p_extra = extra;
3177 c_extra = NUL;
3178 char_attr = hl_attr(HLF_FC);
3181 #endif
3183 #ifdef FEAT_SIGNS
3184 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3186 draw_state = WL_SIGN;
3187 /* Show the sign column when there are any signs in this
3188 * buffer or when using Netbeans. */
3189 if (draw_signcolumn(wp)
3190 # ifdef FEAT_DIFF
3191 && filler_todo <= 0
3192 # endif
3195 int_u text_sign;
3196 # ifdef FEAT_SIGN_ICONS
3197 int_u icon_sign;
3198 # endif
3200 /* Draw two cells with the sign value or blank. */
3201 c_extra = ' ';
3202 char_attr = hl_attr(HLF_SC);
3203 n_extra = 2;
3205 if (row == startrow)
3207 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3208 SIGN_TEXT);
3209 # ifdef FEAT_SIGN_ICONS
3210 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3211 SIGN_ICON);
3212 if (gui.in_use && icon_sign != 0)
3214 /* Use the image in this position. */
3215 c_extra = SIGN_BYTE;
3216 # ifdef FEAT_NETBEANS_INTG
3217 if (buf_signcount(wp->w_buffer, lnum) > 1)
3218 c_extra = MULTISIGN_BYTE;
3219 # endif
3220 char_attr = icon_sign;
3222 else
3223 # endif
3224 if (text_sign != 0)
3226 p_extra = sign_get_text(text_sign);
3227 if (p_extra != NULL)
3229 c_extra = NUL;
3230 n_extra = (int)STRLEN(p_extra);
3232 char_attr = sign_get_attr(text_sign, FALSE);
3237 #endif
3239 if (draw_state == WL_NR - 1 && n_extra == 0)
3241 draw_state = WL_NR;
3242 /* Display the line number. After the first fill with blanks
3243 * when the 'n' flag isn't in 'cpo' */
3244 if (wp->w_p_nu
3245 && (row == startrow
3246 #ifdef FEAT_DIFF
3247 + filler_lines
3248 #endif
3249 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3251 /* Draw the line number (empty space after wrapping). */
3252 if (row == startrow
3253 #ifdef FEAT_DIFF
3254 + filler_lines
3255 #endif
3258 sprintf((char *)extra, "%*ld ",
3259 number_width(wp), (long)lnum);
3260 if (wp->w_skipcol > 0)
3261 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3262 *p_extra = '-';
3263 #ifdef FEAT_RIGHTLEFT
3264 if (wp->w_p_rl) /* reverse line numbers */
3265 rl_mirror(extra);
3266 #endif
3267 p_extra = extra;
3268 c_extra = NUL;
3270 else
3271 c_extra = ' ';
3272 n_extra = number_width(wp) + 1;
3273 char_attr = hl_attr(HLF_N);
3274 #ifdef FEAT_SYN_HL
3275 /* When 'cursorline' is set highlight the line number of
3276 * the current line differently. */
3277 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3278 char_attr = hl_combine_attr(hl_attr(HLF_CUL), char_attr);
3279 #endif
3283 #if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3284 if (draw_state == WL_SBR - 1 && n_extra == 0)
3286 draw_state = WL_SBR;
3287 # ifdef FEAT_DIFF
3288 if (filler_todo > 0)
3290 /* Draw "deleted" diff line(s). */
3291 if (char2cells(fill_diff) > 1)
3292 c_extra = '-';
3293 else
3294 c_extra = fill_diff;
3295 # ifdef FEAT_RIGHTLEFT
3296 if (wp->w_p_rl)
3297 n_extra = col + 1;
3298 else
3299 # endif
3300 n_extra = W_WIDTH(wp) - col;
3301 char_attr = hl_attr(HLF_DED);
3303 # endif
3304 # ifdef FEAT_LINEBREAK
3305 if (*p_sbr != NUL && need_showbreak)
3307 /* Draw 'showbreak' at the start of each broken line. */
3308 p_extra = p_sbr;
3309 c_extra = NUL;
3310 n_extra = (int)STRLEN(p_sbr);
3311 char_attr = hl_attr(HLF_AT);
3312 need_showbreak = FALSE;
3313 /* Correct end of highlighted area for 'showbreak',
3314 * required when 'linebreak' is also set. */
3315 if (tocol == vcol)
3316 tocol += n_extra;
3318 # endif
3320 #endif
3322 if (draw_state == WL_LINE - 1 && n_extra == 0)
3324 draw_state = WL_LINE;
3325 if (saved_n_extra)
3327 /* Continue item from end of wrapped line. */
3328 n_extra = saved_n_extra;
3329 c_extra = saved_c_extra;
3330 p_extra = saved_p_extra;
3331 char_attr = saved_char_attr;
3333 else
3334 char_attr = 0;
3338 /* When still displaying '$' of change command, stop at cursor */
3339 if (dollar_vcol != 0 && wp == curwin
3340 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
3341 #ifdef FEAT_DIFF
3342 && filler_todo <= 0
3343 #endif
3346 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3347 wp->w_p_rl);
3348 /* Pretend we have finished updating the window. Except when
3349 * 'cursorcolumn' is set. */
3350 #ifdef FEAT_SYN_HL
3351 if (wp->w_p_cuc)
3352 row = wp->w_cline_row + wp->w_cline_height;
3353 else
3354 #endif
3355 row = wp->w_height;
3356 break;
3359 if (draw_state == WL_LINE && area_highlighting)
3361 /* handle Visual or match highlighting in this line */
3362 if (vcol == fromcol
3363 #ifdef FEAT_MBYTE
3364 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3365 && (*mb_ptr2cells)(ptr) > 1)
3366 #endif
3367 || ((int)vcol_prev == fromcol_prev
3368 && vcol < tocol))
3369 area_attr = attr; /* start highlighting */
3370 else if (area_attr != 0
3371 && (vcol == tocol
3372 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
3373 area_attr = 0; /* stop highlighting */
3375 #ifdef FEAT_SEARCH_EXTRA
3376 if (!n_extra)
3379 * Check for start/end of search pattern match.
3380 * After end, check for start/end of next match.
3381 * When another match, have to check for start again.
3382 * Watch out for matching an empty string!
3383 * Do this first for search_hl, then for match_hl, so that
3384 * ":match" overrules 'hlsearch'.
3386 v = (long)(ptr - line);
3387 for (i = 3; i >= 0; --i)
3389 shl = (i == 3) ? &search_hl : &match_hl[i];
3390 while (shl->rm.regprog != NULL)
3392 if (shl->startcol != MAXCOL
3393 && v >= (long)shl->startcol
3394 && v < (long)shl->endcol)
3396 shl->attr_cur = shl->attr;
3398 else if (v == (long)shl->endcol)
3400 shl->attr_cur = 0;
3402 next_search_hl(wp, shl, lnum, (colnr_T)v);
3404 /* Need to get the line again, a multi-line regexp
3405 * may have made it invalid. */
3406 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3407 ptr = line + v;
3409 if (shl->lnum == lnum)
3411 shl->startcol = shl->rm.startpos[0].col;
3412 if (shl->rm.endpos[0].lnum == 0)
3413 shl->endcol = shl->rm.endpos[0].col;
3414 else
3415 shl->endcol = MAXCOL;
3417 if (shl->startcol == shl->endcol)
3419 /* highlight empty match, try again after
3420 * it */
3421 #ifdef FEAT_MBYTE
3422 if (has_mbyte)
3423 shl->endcol += (*mb_ptr2len)(line
3424 + shl->endcol);
3425 else
3426 #endif
3427 ++shl->endcol;
3430 /* Loop to check if the match starts at the
3431 * current position */
3432 continue;
3435 break;
3439 /* ":match" highlighting overrules 'hlsearch' */
3440 for (i = 0; i <= 3; ++i)
3441 if (i == 3)
3442 search_attr = search_hl.attr_cur;
3443 else if (match_hl[i].attr_cur != 0)
3445 search_attr = match_hl[i].attr_cur;
3446 break;
3449 #endif
3451 #ifdef FEAT_DIFF
3452 if (diff_hlf != (hlf_T)0)
3454 if (diff_hlf == HLF_CHD && ptr - line >= change_start)
3455 diff_hlf = HLF_TXD; /* changed text */
3456 if (diff_hlf == HLF_TXD && ptr - line > change_end)
3457 diff_hlf = HLF_CHD; /* changed line */
3458 line_attr = hl_attr(diff_hlf);
3460 #endif
3462 /* Decide which of the highlight attributes to use. */
3463 attr_pri = TRUE;
3464 if (area_attr != 0)
3465 char_attr = area_attr;
3466 else if (search_attr != 0)
3467 char_attr = search_attr;
3468 #ifdef LINE_ATTR
3469 /* Use line_attr when not in the Visual or 'incsearch' area
3470 * (area_attr may be 0 when "noinvcur" is set). */
3471 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
3472 || (vcol < fromcol || vcol >= tocol)))
3473 char_attr = line_attr;
3474 #endif
3475 else
3477 attr_pri = FALSE;
3478 #ifdef FEAT_SYN_HL
3479 if (has_syntax)
3480 char_attr = syntax_attr;
3481 else
3482 #endif
3483 char_attr = 0;
3488 * Get the next character to put on the screen.
3491 * The 'extra' array contains the extra stuff that is inserted to
3492 * represent special characters (non-printable stuff). When all
3493 * characters are the same, c_extra is used.
3494 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
3496 if (n_extra > 0)
3498 if (c_extra != NUL)
3500 c = c_extra;
3501 #ifdef FEAT_MBYTE
3502 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
3503 if (enc_utf8 && (*mb_char2len)(c) > 1)
3505 mb_utf8 = TRUE;
3506 u8cc[0] = 0;
3507 c = 0xc0;
3509 else
3510 mb_utf8 = FALSE;
3511 #endif
3513 else
3515 c = *p_extra;
3516 #ifdef FEAT_MBYTE
3517 if (has_mbyte)
3519 mb_c = c;
3520 if (enc_utf8)
3522 /* If the UTF-8 character is more than one byte:
3523 * Decode it into "mb_c". */
3524 mb_l = (*mb_ptr2len)(p_extra);
3525 mb_utf8 = FALSE;
3526 if (mb_l > n_extra)
3527 mb_l = 1;
3528 else if (mb_l > 1)
3530 mb_c = utfc_ptr2char(p_extra, u8cc);
3531 mb_utf8 = TRUE;
3532 c = 0xc0;
3535 else
3537 /* if this is a DBCS character, put it in "mb_c" */
3538 mb_l = MB_BYTE2LEN(c);
3539 if (mb_l >= n_extra)
3540 mb_l = 1;
3541 else if (mb_l > 1)
3542 mb_c = (c << 8) + p_extra[1];
3544 if (mb_l == 0) /* at the NUL at end-of-line */
3545 mb_l = 1;
3547 /* If a double-width char doesn't fit display a '>' in the
3548 * last column. */
3549 if ((
3550 # ifdef FEAT_RIGHTLEFT
3551 wp->w_p_rl ? (col <= 0) :
3552 # endif
3553 (col >= W_WIDTH(wp) - 1))
3554 && (*mb_char2cells)(mb_c) == 2)
3556 c = '>';
3557 mb_c = c;
3558 mb_l = 1;
3559 mb_utf8 = FALSE;
3560 multi_attr = hl_attr(HLF_AT);
3561 /* put the pointer back to output the double-width
3562 * character at the start of the next line. */
3563 ++n_extra;
3564 --p_extra;
3566 else
3568 n_extra -= mb_l - 1;
3569 p_extra += mb_l - 1;
3572 #endif
3573 ++p_extra;
3575 --n_extra;
3577 else
3580 * Get a character from the line itself.
3582 c = *ptr;
3583 #ifdef FEAT_MBYTE
3584 if (has_mbyte)
3586 mb_c = c;
3587 if (enc_utf8)
3589 /* If the UTF-8 character is more than one byte: Decode it
3590 * into "mb_c". */
3591 mb_l = (*mb_ptr2len)(ptr);
3592 mb_utf8 = FALSE;
3593 if (mb_l > 1)
3595 mb_c = utfc_ptr2char(ptr, u8cc);
3596 /* Overlong encoded ASCII or ASCII with composing char
3597 * is displayed normally, except a NUL. */
3598 if (mb_c < 0x80)
3599 c = mb_c;
3600 mb_utf8 = TRUE;
3602 /* At start of the line we can have a composing char.
3603 * Draw it as a space with a composing char. */
3604 if (utf_iscomposing(mb_c))
3606 for (i = Screen_mco - 1; i > 0; --i)
3607 u8cc[i] = u8cc[i - 1];
3608 u8cc[0] = mb_c;
3609 mb_c = ' ';
3613 if ((mb_l == 1 && c >= 0x80)
3614 || (mb_l >= 1 && mb_c == 0)
3615 || (mb_l > 1 && (!vim_isprintc(mb_c)
3616 || mb_c >= 0x10000)))
3619 * Illegal UTF-8 byte: display as <xx>.
3620 * Non-BMP character : display as ? or fullwidth ?.
3622 if (mb_c < 0x10000)
3624 transchar_hex(extra, mb_c);
3625 # ifdef FEAT_RIGHTLEFT
3626 if (wp->w_p_rl) /* reverse */
3627 rl_mirror(extra);
3628 # endif
3630 else if (utf_char2cells(mb_c) != 2)
3631 STRCPY(extra, "?");
3632 else
3633 /* 0xff1f in UTF-8: full-width '?' */
3634 STRCPY(extra, "\357\274\237");
3636 p_extra = extra;
3637 c = *p_extra;
3638 mb_c = mb_ptr2char_adv(&p_extra);
3639 mb_utf8 = (c >= 0x80);
3640 n_extra = (int)STRLEN(p_extra);
3641 c_extra = NUL;
3642 if (area_attr == 0 && search_attr == 0)
3644 n_attr = n_extra + 1;
3645 extra_attr = hl_attr(HLF_8);
3646 saved_attr2 = char_attr; /* save current attr */
3649 else if (mb_l == 0) /* at the NUL at end-of-line */
3650 mb_l = 1;
3651 #ifdef FEAT_ARABIC
3652 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
3654 /* Do Arabic shaping. */
3655 int pc, pc1, nc;
3656 int pcc[MAX_MCO];
3658 /* The idea of what is the previous and next
3659 * character depends on 'rightleft'. */
3660 if (wp->w_p_rl)
3662 pc = prev_c;
3663 pc1 = prev_c1;
3664 nc = utf_ptr2char(ptr + mb_l);
3665 prev_c1 = u8cc[0];
3667 else
3669 pc = utfc_ptr2char(ptr + mb_l, pcc);
3670 nc = prev_c;
3671 pc1 = pcc[0];
3673 prev_c = mb_c;
3675 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
3677 else
3678 prev_c = mb_c;
3679 #endif
3681 else /* enc_dbcs */
3683 mb_l = MB_BYTE2LEN(c);
3684 if (mb_l == 0) /* at the NUL at end-of-line */
3685 mb_l = 1;
3686 else if (mb_l > 1)
3688 /* We assume a second byte below 32 is illegal.
3689 * Hopefully this is OK for all double-byte encodings!
3691 if (ptr[1] >= 32)
3692 mb_c = (c << 8) + ptr[1];
3693 else
3695 if (ptr[1] == NUL)
3697 /* head byte at end of line */
3698 mb_l = 1;
3699 transchar_nonprint(extra, c);
3701 else
3703 /* illegal tail byte */
3704 mb_l = 2;
3705 STRCPY(extra, "XX");
3707 p_extra = extra;
3708 n_extra = (int)STRLEN(extra) - 1;
3709 c_extra = NUL;
3710 c = *p_extra++;
3711 if (area_attr == 0 && search_attr == 0)
3713 n_attr = n_extra + 1;
3714 extra_attr = hl_attr(HLF_8);
3715 saved_attr2 = char_attr; /* save current attr */
3717 mb_c = c;
3721 /* If a double-width char doesn't fit display a '>' in the
3722 * last column; the character is displayed at the start of the
3723 * next line. */
3724 if ((
3725 # ifdef FEAT_RIGHTLEFT
3726 wp->w_p_rl ? (col <= 0) :
3727 # endif
3728 (col >= W_WIDTH(wp) - 1))
3729 && (*mb_char2cells)(mb_c) == 2)
3731 c = '>';
3732 mb_c = c;
3733 mb_utf8 = FALSE;
3734 mb_l = 1;
3735 multi_attr = hl_attr(HLF_AT);
3736 /* Put pointer back so that the character will be
3737 * displayed at the start of the next line. */
3738 --ptr;
3740 else if (*ptr != NUL)
3741 ptr += mb_l - 1;
3743 /* If a double-width char doesn't fit at the left side display
3744 * a '<' in the first column. */
3745 if (n_skip > 0 && mb_l > 1)
3747 extra[0] = '<';
3748 p_extra = extra;
3749 n_extra = 1;
3750 c_extra = NUL;
3751 c = ' ';
3752 if (area_attr == 0 && search_attr == 0)
3754 n_attr = n_extra + 1;
3755 extra_attr = hl_attr(HLF_AT);
3756 saved_attr2 = char_attr; /* save current attr */
3758 mb_c = c;
3759 mb_utf8 = FALSE;
3760 mb_l = 1;
3764 #endif
3765 ++ptr;
3767 /* 'list' : change char 160 to lcs_nbsp. */
3768 if (wp->w_p_list && (c == 160
3769 #ifdef FEAT_MBYTE
3770 || (mb_utf8 && mb_c == 160)
3771 #endif
3772 ) && lcs_nbsp)
3774 c = lcs_nbsp;
3775 if (area_attr == 0 && search_attr == 0)
3777 n_attr = 1;
3778 extra_attr = hl_attr(HLF_8);
3779 saved_attr2 = char_attr; /* save current attr */
3781 #ifdef FEAT_MBYTE
3782 mb_c = c;
3783 if (enc_utf8 && (*mb_char2len)(c) > 1)
3785 mb_utf8 = TRUE;
3786 u8cc[0] = 0;
3787 c = 0xc0;
3789 else
3790 mb_utf8 = FALSE;
3791 #endif
3794 if (extra_check)
3796 #ifdef FEAT_SPELL
3797 int can_spell = TRUE;
3798 #endif
3800 #ifdef FEAT_SYN_HL
3801 /* Get syntax attribute, unless still at the start of the line
3802 * (double-wide char that doesn't fit). */
3803 v = (long)(ptr - line);
3804 if (has_syntax && v > 0)
3806 /* Get the syntax attribute for the character. If there
3807 * is an error, disable syntax highlighting. */
3808 save_did_emsg = did_emsg;
3809 did_emsg = FALSE;
3811 syntax_attr = get_syntax_attr((colnr_T)v - 1,
3812 # ifdef FEAT_SPELL
3813 has_spell ? &can_spell :
3814 # endif
3815 NULL);
3817 if (did_emsg)
3819 wp->w_buffer->b_syn_error = TRUE;
3820 has_syntax = FALSE;
3822 else
3823 did_emsg = save_did_emsg;
3825 /* Need to get the line again, a multi-line regexp may
3826 * have made it invalid. */
3827 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3828 ptr = line + v;
3830 if (!attr_pri)
3831 char_attr = syntax_attr;
3832 else
3833 char_attr = hl_combine_attr(syntax_attr, char_attr);
3835 #endif
3837 #ifdef FEAT_SPELL
3838 /* Check spelling (unless at the end of the line).
3839 * Only do this when there is no syntax highlighting, the
3840 * @Spell cluster is not used or the current syntax item
3841 * contains the @Spell cluster. */
3842 if (has_spell && v >= word_end && v > cur_checked_col)
3844 spell_attr = 0;
3845 # ifdef FEAT_SYN_HL
3846 if (!attr_pri)
3847 char_attr = syntax_attr;
3848 # endif
3849 if (c != 0 && (
3850 # ifdef FEAT_SYN_HL
3851 !has_syntax ||
3852 # endif
3853 can_spell))
3855 char_u *prev_ptr, *p;
3856 int len;
3857 hlf_T spell_hlf = HLF_COUNT;
3858 # ifdef FEAT_MBYTE
3859 if (has_mbyte)
3861 prev_ptr = ptr - mb_l;
3862 v -= mb_l - 1;
3864 else
3865 # endif
3866 prev_ptr = ptr - 1;
3868 /* Use nextline[] if possible, it has the start of the
3869 * next line concatenated. */
3870 if ((prev_ptr - line) - nextlinecol >= 0)
3871 p = nextline + (prev_ptr - line) - nextlinecol;
3872 else
3873 p = prev_ptr;
3874 cap_col -= (int)(prev_ptr - line);
3875 len = spell_check(wp, p, &spell_hlf, &cap_col,
3876 nochange);
3877 word_end = v + len;
3879 /* In Insert mode only highlight a word that
3880 * doesn't touch the cursor. */
3881 if (spell_hlf != HLF_COUNT
3882 && (State & INSERT) != 0
3883 && wp->w_cursor.lnum == lnum
3884 && wp->w_cursor.col >=
3885 (colnr_T)(prev_ptr - line)
3886 && wp->w_cursor.col < (colnr_T)word_end)
3888 spell_hlf = HLF_COUNT;
3889 spell_redraw_lnum = lnum;
3892 if (spell_hlf == HLF_COUNT && p != prev_ptr
3893 && (p - nextline) + len > nextline_idx)
3895 /* Remember that the good word continues at the
3896 * start of the next line. */
3897 checked_lnum = lnum + 1;
3898 checked_col = (int)((p - nextline) + len - nextline_idx);
3901 /* Turn index into actual attributes. */
3902 if (spell_hlf != HLF_COUNT)
3903 spell_attr = highlight_attr[spell_hlf];
3905 if (cap_col > 0)
3907 if (p != prev_ptr
3908 && (p - nextline) + cap_col >= nextline_idx)
3910 /* Remember that the word in the next line
3911 * must start with a capital. */
3912 capcol_lnum = lnum + 1;
3913 cap_col = (int)((p - nextline) + cap_col
3914 - nextline_idx);
3916 else
3917 /* Compute the actual column. */
3918 cap_col += (int)(prev_ptr - line);
3922 if (spell_attr != 0)
3924 if (!attr_pri)
3925 char_attr = hl_combine_attr(char_attr, spell_attr);
3926 else
3927 char_attr = hl_combine_attr(spell_attr, char_attr);
3929 #endif
3930 #ifdef FEAT_LINEBREAK
3932 * Found last space before word: check for line break.
3934 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr)
3935 && !wp->w_p_list)
3937 n_extra = win_lbr_chartabsize(wp, ptr - (
3938 # ifdef FEAT_MBYTE
3939 has_mbyte ? mb_l :
3940 # endif
3941 1), (colnr_T)vcol, NULL) - 1;
3942 c_extra = ' ';
3943 if (vim_iswhite(c))
3944 c = ' ';
3946 #endif
3948 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
3950 c = lcs_trail;
3951 if (!attr_pri)
3953 n_attr = 1;
3954 extra_attr = hl_attr(HLF_8);
3955 saved_attr2 = char_attr; /* save current attr */
3957 #ifdef FEAT_MBYTE
3958 mb_c = c;
3959 if (enc_utf8 && (*mb_char2len)(c) > 1)
3961 mb_utf8 = TRUE;
3962 u8cc[0] = 0;
3963 c = 0xc0;
3965 else
3966 mb_utf8 = FALSE;
3967 #endif
3972 * Handling of non-printable characters.
3974 if (!(chartab[c & 0xff] & CT_PRINT_CHAR))
3977 * when getting a character from the file, we may have to
3978 * turn it into something else on the way to putting it
3979 * into "ScreenLines".
3981 if (c == TAB && (!wp->w_p_list || lcs_tab1))
3983 /* tab amount depends on current column */
3984 n_extra = (int)wp->w_buffer->b_p_ts
3985 - vcol % (int)wp->w_buffer->b_p_ts - 1;
3986 #ifdef FEAT_MBYTE
3987 mb_utf8 = FALSE; /* don't draw as UTF-8 */
3988 #endif
3989 if (wp->w_p_list)
3991 c = lcs_tab1;
3992 c_extra = lcs_tab2;
3993 n_attr = n_extra + 1;
3994 extra_attr = hl_attr(HLF_8);
3995 saved_attr2 = char_attr; /* save current attr */
3996 #ifdef FEAT_MBYTE
3997 mb_c = c;
3998 if (enc_utf8 && (*mb_char2len)(c) > 1)
4000 mb_utf8 = TRUE;
4001 u8cc[0] = 0;
4002 c = 0xc0;
4004 #endif
4006 else
4008 c_extra = ' ';
4009 c = ' ';
4012 else if (c == NUL
4013 && ((wp->w_p_list && lcs_eol > 0)
4014 || ((fromcol >= 0 || fromcol_prev >= 0)
4015 && tocol > vcol
4016 #ifdef FEAT_VISUAL
4017 && VIsual_mode != Ctrl_V
4018 #endif
4019 && (
4020 # ifdef FEAT_RIGHTLEFT
4021 wp->w_p_rl ? (col >= 0) :
4022 # endif
4023 (col < W_WIDTH(wp)))
4024 && !(noinvcur
4025 && (colnr_T)vcol == wp->w_virtcol)))
4026 && lcs_eol_one >= 0)
4028 /* Display a '$' after the line or highlight an extra
4029 * character if the line break is included. */
4030 #if defined(FEAT_DIFF) || defined(LINE_ATTR)
4031 /* For a diff line the highlighting continues after the
4032 * "$". */
4033 if (
4034 # ifdef FEAT_DIFF
4035 diff_hlf == (hlf_T)0
4036 # ifdef LINE_ATTR
4038 # endif
4039 # endif
4040 # ifdef LINE_ATTR
4041 line_attr == 0
4042 # endif
4044 #endif
4046 #ifdef FEAT_VIRTUALEDIT
4047 /* In virtualedit, visual selections may extend
4048 * beyond end of line. */
4049 if (area_highlighting && virtual_active()
4050 && tocol != MAXCOL && vcol < tocol)
4051 n_extra = 0;
4052 else
4053 #endif
4055 p_extra = at_end_str;
4056 n_extra = 1;
4057 c_extra = NUL;
4060 if (wp->w_p_list)
4061 c = lcs_eol;
4062 else
4063 c = ' ';
4064 lcs_eol_one = -1;
4065 --ptr; /* put it back at the NUL */
4066 if (!attr_pri)
4068 extra_attr = hl_attr(HLF_AT);
4069 n_attr = 1;
4071 #ifdef FEAT_MBYTE
4072 mb_c = c;
4073 if (enc_utf8 && (*mb_char2len)(c) > 1)
4075 mb_utf8 = TRUE;
4076 u8cc[0] = 0;
4077 c = 0xc0;
4079 else
4080 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4081 #endif
4083 else if (c != NUL)
4085 p_extra = transchar(c);
4086 #ifdef FEAT_RIGHTLEFT
4087 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4088 rl_mirror(p_extra); /* reverse "<12>" */
4089 #endif
4090 n_extra = byte2cells(c) - 1;
4091 c_extra = NUL;
4092 c = *p_extra++;
4093 if (!attr_pri)
4095 n_attr = n_extra + 1;
4096 extra_attr = hl_attr(HLF_8);
4097 saved_attr2 = char_attr; /* save current attr */
4099 #ifdef FEAT_MBYTE
4100 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4101 #endif
4103 #ifdef FEAT_VIRTUALEDIT
4104 else if (VIsual_active
4105 && (VIsual_mode == Ctrl_V
4106 || VIsual_mode == 'v')
4107 && virtual_active()
4108 && tocol != MAXCOL
4109 && vcol < tocol
4110 && (
4111 # ifdef FEAT_RIGHTLEFT
4112 wp->w_p_rl ? (col >= 0) :
4113 # endif
4114 (col < W_WIDTH(wp))))
4116 c = ' ';
4117 --ptr; /* put it back at the NUL */
4119 #endif
4120 #if defined(LINE_ATTR)
4121 else if ((
4122 # ifdef FEAT_DIFF
4123 diff_hlf != (hlf_T)0 ||
4124 # endif
4125 line_attr != 0
4126 ) && (
4127 # ifdef FEAT_RIGHTLEFT
4128 wp->w_p_rl ? (col >= 0) :
4129 # endif
4130 (col < W_WIDTH(wp))))
4132 /* Highlight until the right side of the window */
4133 c = ' ';
4134 --ptr; /* put it back at the NUL */
4136 /* Remember we do the char for line highlighting. */
4137 ++did_line_attr;
4139 /* don't do search HL for the rest of the line */
4140 if (line_attr != 0 && char_attr == search_attr && col > 0)
4141 char_attr = line_attr;
4142 # ifdef FEAT_DIFF
4143 if (diff_hlf == HLF_TXD)
4145 diff_hlf = HLF_CHD;
4146 if (attr == 0 || char_attr != attr)
4147 char_attr = hl_attr(diff_hlf);
4149 # endif
4151 #endif
4155 /* Don't override visual selection highlighting. */
4156 if (n_attr > 0
4157 && draw_state == WL_LINE
4158 && !attr_pri)
4159 char_attr = extra_attr;
4161 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
4162 /* XIM don't send preedit_start and preedit_end, but they send
4163 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
4164 * im_is_preediting() here. */
4165 if (xic != NULL
4166 && lnum == curwin->w_cursor.lnum
4167 && (State & INSERT)
4168 && !p_imdisable
4169 && im_is_preediting()
4170 && draw_state == WL_LINE)
4172 colnr_T tcol;
4174 if (preedit_end_col == MAXCOL)
4175 getvcol(curwin, &(curwin->w_cursor), &tcol, NULL, NULL);
4176 else
4177 tcol = preedit_end_col;
4178 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
4180 if (feedback_old_attr < 0)
4182 feedback_col = 0;
4183 feedback_old_attr = char_attr;
4185 char_attr = im_get_feedback_attr(feedback_col);
4186 if (char_attr < 0)
4187 char_attr = feedback_old_attr;
4188 feedback_col++;
4190 else if (feedback_old_attr >= 0)
4192 char_attr = feedback_old_attr;
4193 feedback_old_attr = -1;
4194 feedback_col = 0;
4197 #endif
4199 * Handle the case where we are in column 0 but not on the first
4200 * character of the line and the user wants us to show us a
4201 * special character (via 'listchars' option "precedes:<char>".
4203 if (lcs_prec_todo != NUL
4204 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
4205 #ifdef FEAT_DIFF
4206 && filler_todo <= 0
4207 #endif
4208 && draw_state > WL_NR
4209 && c != NUL)
4211 c = lcs_prec;
4212 lcs_prec_todo = NUL;
4213 #ifdef FEAT_MBYTE
4214 mb_c = c;
4215 if (enc_utf8 && (*mb_char2len)(c) > 1)
4217 mb_utf8 = TRUE;
4218 u8cc[0] = 0;
4219 c = 0xc0;
4221 else
4222 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4223 #endif
4224 if (!attr_pri)
4226 saved_attr3 = char_attr; /* save current attr */
4227 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
4228 n_attr3 = 1;
4233 * At end of the text line or just after the last character.
4235 if (c == NUL
4236 #if defined(LINE_ATTR)
4237 || did_line_attr == 1
4238 #endif
4241 #ifdef FEAT_SEARCH_EXTRA
4242 long prevcol = (long)(ptr - line) - (c == NUL);
4243 #endif
4245 /* invert at least one char, used for Visual and empty line or
4246 * highlight match at end of line. If it's beyond the last
4247 * char on the screen, just overwrite that one (tricky!) Not
4248 * needed when a '$' was displayed for 'list'. */
4249 if (lcs_eol == lcs_eol_one
4250 && ((area_attr != 0 && vcol == fromcol && c == NUL)
4251 #ifdef FEAT_SEARCH_EXTRA
4252 /* highlight 'hlsearch' match at end of line */
4253 || ((prevcol == (long)search_hl.startcol
4254 || prevcol == (long)match_hl[0].startcol
4255 || prevcol == (long)match_hl[1].startcol
4256 || prevcol == (long)match_hl[2].startcol)
4257 # if defined(LINE_ATTR)
4258 && did_line_attr <= 1
4259 # endif
4261 #endif
4264 int n = 0;
4266 #ifdef FEAT_RIGHTLEFT
4267 if (wp->w_p_rl)
4269 if (col < 0)
4270 n = 1;
4272 else
4273 #endif
4275 if (col >= W_WIDTH(wp))
4276 n = -1;
4278 if (n != 0)
4280 /* At the window boundary, highlight the last character
4281 * instead (better than nothing). */
4282 off += n;
4283 col += n;
4285 else
4287 /* Add a blank character to highlight. */
4288 ScreenLines[off] = ' ';
4289 #ifdef FEAT_MBYTE
4290 if (enc_utf8)
4291 ScreenLinesUC[off] = 0;
4292 #endif
4294 #ifdef FEAT_SEARCH_EXTRA
4295 if (area_attr == 0)
4297 for (i = 0; i <= 3; ++i)
4299 if (i == 3)
4300 char_attr = search_hl.attr;
4301 else if ((ptr - line) - 1 == (long)match_hl[i].startcol)
4303 char_attr = match_hl[i].attr;
4304 break;
4308 #endif
4309 ScreenAttrs[off] = char_attr;
4310 #ifdef FEAT_RIGHTLEFT
4311 if (wp->w_p_rl)
4312 --col;
4313 else
4314 #endif
4315 ++col;
4316 ++vcol;
4321 * At end of the text line.
4323 if (c == NUL)
4325 #ifdef FEAT_SYN_HL
4326 /* Highlight 'cursorcolumn' past end of the line. */
4327 if (wp->w_p_wrap)
4328 v = wp->w_skipcol;
4329 else
4330 v = wp->w_leftcol;
4331 /* check if line ends before left margin */
4332 if (vcol < v + col - win_col_off(wp))
4334 vcol = v + col - win_col_off(wp);
4335 if (wp->w_p_cuc
4336 && (int)wp->w_virtcol >= vcol
4337 && (int)wp->w_virtcol < W_WIDTH(wp) * (row - startrow + 1)
4339 && lnum != wp->w_cursor.lnum
4340 # ifdef FEAT_RIGHTLEFT
4341 && !wp->w_p_rl
4342 # endif
4345 while (col < W_WIDTH(wp))
4347 ScreenLines[off] = ' ';
4348 #ifdef FEAT_MBYTE
4349 if (enc_utf8)
4350 ScreenLinesUC[off] = 0;
4351 #endif
4352 ++col;
4353 if (vcol == (long)wp->w_virtcol)
4355 ScreenAttrs[off] = hl_attr(HLF_CUC);
4356 break;
4358 ScreenAttrs[off++] = 0;
4359 ++vcol;
4362 #endif
4364 SCREEN_LINE(screen_row, W_WINCOL(wp), col, (int)W_WIDTH(wp),
4365 wp->w_p_rl);
4366 row++;
4369 * Update w_cline_height and w_cline_folded if the cursor line was
4370 * updated (saves a call to plines() later).
4372 if (wp == curwin && lnum == curwin->w_cursor.lnum)
4374 curwin->w_cline_row = startrow;
4375 curwin->w_cline_height = row - startrow;
4376 #ifdef FEAT_FOLDING
4377 curwin->w_cline_folded = FALSE;
4378 #endif
4379 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
4382 break;
4385 /* line continues beyond line end */
4386 if (lcs_ext
4387 && !wp->w_p_wrap
4388 #ifdef FEAT_DIFF
4389 && filler_todo <= 0
4390 #endif
4391 && (
4392 #ifdef FEAT_RIGHTLEFT
4393 wp->w_p_rl ? col == 0 :
4394 #endif
4395 col == W_WIDTH(wp) - 1)
4396 && (*ptr != NUL
4397 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
4398 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
4400 c = lcs_ext;
4401 char_attr = hl_attr(HLF_AT);
4402 #ifdef FEAT_MBYTE
4403 mb_c = c;
4404 if (enc_utf8 && (*mb_char2len)(c) > 1)
4406 mb_utf8 = TRUE;
4407 u8cc[0] = 0;
4408 c = 0xc0;
4410 else
4411 mb_utf8 = FALSE;
4412 #endif
4415 #ifdef FEAT_SYN_HL
4416 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
4417 * highlight the cursor position itself. */
4418 if (wp->w_p_cuc && vcol == (long)wp->w_virtcol
4419 && lnum != wp->w_cursor.lnum
4420 && draw_state == WL_LINE)
4422 vcol_save_attr = char_attr;
4423 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
4425 else
4426 vcol_save_attr = -1;
4427 #endif
4430 * Store character to be displayed.
4431 * Skip characters that are left of the screen for 'nowrap'.
4433 vcol_prev = vcol;
4434 if (draw_state < WL_LINE || n_skip <= 0)
4437 * Store the character.
4439 #if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
4440 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
4442 /* A double-wide character is: put first halve in left cell. */
4443 --off;
4444 --col;
4446 #endif
4447 ScreenLines[off] = c;
4448 #ifdef FEAT_MBYTE
4449 if (enc_dbcs == DBCS_JPNU)
4450 ScreenLines2[off] = mb_c & 0xff;
4451 else if (enc_utf8)
4453 if (mb_utf8)
4455 ScreenLinesUC[off] = mb_c;
4456 if ((c & 0xff) == 0)
4457 ScreenLines[off] = 0x80; /* avoid storing zero */
4458 for (i = 0; i < Screen_mco; ++i)
4460 ScreenLinesC[i][off] = u8cc[i];
4461 if (u8cc[i] == 0)
4462 break;
4465 else
4466 ScreenLinesUC[off] = 0;
4468 if (multi_attr)
4470 ScreenAttrs[off] = multi_attr;
4471 multi_attr = 0;
4473 else
4474 #endif
4475 ScreenAttrs[off] = char_attr;
4477 #ifdef FEAT_MBYTE
4478 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
4480 /* Need to fill two screen columns. */
4481 ++off;
4482 ++col;
4483 if (enc_utf8)
4484 /* UTF-8: Put a 0 in the second screen char. */
4485 ScreenLines[off] = 0;
4486 else
4487 /* DBCS: Put second byte in the second screen char. */
4488 ScreenLines[off] = mb_c & 0xff;
4489 ++vcol;
4490 /* When "tocol" is halfway a character, set it to the end of
4491 * the character, otherwise highlighting won't stop. */
4492 if (tocol == vcol)
4493 ++tocol;
4494 #ifdef FEAT_RIGHTLEFT
4495 if (wp->w_p_rl)
4497 /* now it's time to backup one cell */
4498 --off;
4499 --col;
4501 #endif
4503 #endif
4504 #ifdef FEAT_RIGHTLEFT
4505 if (wp->w_p_rl)
4507 --off;
4508 --col;
4510 else
4511 #endif
4513 ++off;
4514 ++col;
4517 else
4518 --n_skip;
4520 /* Only advance the "vcol" when after the 'number' column. */
4521 if (draw_state >= WL_SBR
4522 #ifdef FEAT_DIFF
4523 && filler_todo <= 0
4524 #endif
4526 ++vcol;
4528 #ifdef FEAT_SYN_HL
4529 if (vcol_save_attr >= 0)
4530 char_attr = vcol_save_attr;
4531 #endif
4533 /* restore attributes after "predeces" in 'listchars' */
4534 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
4535 char_attr = saved_attr3;
4537 /* restore attributes after last 'listchars' or 'number' char */
4538 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
4539 char_attr = saved_attr2;
4542 * At end of screen line and there is more to come: Display the line
4543 * so far. If there is no more to display it is catched above.
4545 if ((
4546 #ifdef FEAT_RIGHTLEFT
4547 wp->w_p_rl ? (col < 0) :
4548 #endif
4549 (col >= W_WIDTH(wp)))
4550 && (*ptr != NUL
4551 #ifdef FEAT_DIFF
4552 || filler_todo > 0
4553 #endif
4554 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
4555 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
4558 SCREEN_LINE(screen_row, W_WINCOL(wp), col, (int)W_WIDTH(wp),
4559 wp->w_p_rl);
4560 ++row;
4561 ++screen_row;
4563 /* When not wrapping and finished diff lines, or when displayed
4564 * '$' and highlighting until last column, break here. */
4565 if ((!wp->w_p_wrap
4566 #ifdef FEAT_DIFF
4567 && filler_todo <= 0
4568 #endif
4569 ) || lcs_eol_one == -1)
4570 break;
4572 /* When the window is too narrow draw all "@" lines. */
4573 if (draw_state != WL_LINE
4574 #ifdef FEAT_DIFF
4575 && filler_todo <= 0
4576 #endif
4579 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
4580 #ifdef FEAT_VERTSPLIT
4581 draw_vsep_win(wp, row);
4582 #endif
4583 row = endrow;
4586 /* When line got too long for screen break here. */
4587 if (row == endrow)
4589 ++row;
4590 break;
4593 if (screen_cur_row == screen_row - 1
4594 #ifdef FEAT_DIFF
4595 && filler_todo <= 0
4596 #endif
4597 && W_WIDTH(wp) == Columns)
4599 /* Remember that the line wraps, used for modeless copy. */
4600 LineWraps[screen_row - 1] = TRUE;
4603 * Special trick to make copy/paste of wrapped lines work with
4604 * xterm/screen: write an extra character beyond the end of
4605 * the line. This will work with all terminal types
4606 * (regardless of the xn,am settings).
4607 * Only do this on a fast tty.
4608 * Only do this if the cursor is on the current line
4609 * (something has been written in it).
4610 * Don't do this for the GUI.
4611 * Don't do this for double-width characters.
4612 * Don't do this for a window not at the right screen border.
4614 if (p_tf
4615 #ifdef FEAT_GUI
4616 && !gui.in_use
4617 #endif
4618 #ifdef FEAT_MBYTE
4619 && !(has_mbyte
4620 && ((*mb_off2cells)(LineOffset[screen_row]) == 2
4621 || (*mb_off2cells)(LineOffset[screen_row - 1]
4622 + (int)Columns - 2) == 2))
4623 #endif
4626 /* First make sure we are at the end of the screen line,
4627 * then output the same character again to let the
4628 * terminal know about the wrap. If the terminal doesn't
4629 * auto-wrap, we overwrite the character. */
4630 if (screen_cur_col != W_WIDTH(wp))
4631 screen_char(LineOffset[screen_row - 1]
4632 + (unsigned)Columns - 1,
4633 screen_row - 1, (int)(Columns - 1));
4635 #ifdef FEAT_MBYTE
4636 /* When there is a multi-byte character, just output a
4637 * space to keep it simple. */
4638 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
4639 screen_row - 1] + (Columns - 1)]) > 1)
4640 out_char(' ');
4641 else
4642 #endif
4643 out_char(ScreenLines[LineOffset[screen_row - 1]
4644 + (Columns - 1)]);
4645 /* force a redraw of the first char on the next line */
4646 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
4647 screen_start(); /* don't know where cursor is now */
4651 col = 0;
4652 off = (unsigned)(current_ScreenLine - ScreenLines);
4653 #ifdef FEAT_RIGHTLEFT
4654 if (wp->w_p_rl)
4656 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
4657 off += col;
4659 #endif
4661 /* reset the drawing state for the start of a wrapped line */
4662 draw_state = WL_START;
4663 saved_n_extra = n_extra;
4664 saved_p_extra = p_extra;
4665 saved_c_extra = c_extra;
4666 saved_char_attr = char_attr;
4667 n_extra = 0;
4668 lcs_prec_todo = lcs_prec;
4669 #ifdef FEAT_LINEBREAK
4670 # ifdef FEAT_DIFF
4671 if (filler_todo <= 0)
4672 # endif
4673 need_showbreak = TRUE;
4674 #endif
4675 #ifdef FEAT_DIFF
4676 --filler_todo;
4677 /* When the filler lines are actually below the last line of the
4678 * file, don't draw the line itself, break here. */
4679 if (filler_todo == 0 && wp->w_botfill)
4680 break;
4681 #endif
4684 } /* for every character in the line */
4686 #ifdef FEAT_SPELL
4687 /* After an empty line check first word for capital. */
4688 if (*skipwhite(line) == NUL)
4690 capcol_lnum = lnum + 1;
4691 cap_col = 0;
4693 #endif
4695 return row;
4698 #ifdef FEAT_MBYTE
4699 static int comp_char_differs __ARGS((int, int));
4702 * Return if the composing characters at "off_from" and "off_to" differ.
4704 static int
4705 comp_char_differs(off_from, off_to)
4706 int off_from;
4707 int off_to;
4709 int i;
4711 for (i = 0; i < Screen_mco; ++i)
4713 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
4714 return TRUE;
4715 if (ScreenLinesC[i][off_from] == 0)
4716 break;
4718 return FALSE;
4720 #endif
4723 * Check whether the given character needs redrawing:
4724 * - the (first byte of the) character is different
4725 * - the attributes are different
4726 * - the character is multi-byte and the next byte is different
4728 static int
4729 char_needs_redraw(off_from, off_to, cols)
4730 int off_from;
4731 int off_to;
4732 int cols;
4734 if (cols > 0
4735 && ((ScreenLines[off_from] != ScreenLines[off_to]
4736 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
4738 #ifdef FEAT_MBYTE
4739 || (enc_dbcs != 0
4740 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
4741 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
4742 ? ScreenLines2[off_from] != ScreenLines2[off_to]
4743 : (cols > 1 && ScreenLines[off_from + 1]
4744 != ScreenLines[off_to + 1])))
4745 || (enc_utf8
4746 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
4747 || (ScreenLinesUC[off_from] != 0
4748 && comp_char_differs(off_from, off_to))))
4749 #endif
4751 return TRUE;
4752 return FALSE;
4756 * Move one "cooked" screen line to the screen, but only the characters that
4757 * have actually changed. Handle insert/delete character.
4758 * "coloff" gives the first column on the screen for this line.
4759 * "endcol" gives the columns where valid characters are.
4760 * "clear_width" is the width of the window. It's > 0 if the rest of the line
4761 * needs to be cleared, negative otherwise.
4762 * "rlflag" is TRUE in a rightleft window:
4763 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
4764 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
4766 static void
4767 screen_line(row, coloff, endcol, clear_width
4768 #ifdef FEAT_RIGHTLEFT
4769 , rlflag
4770 #endif
4772 int row;
4773 int coloff;
4774 int endcol;
4775 int clear_width;
4776 #ifdef FEAT_RIGHTLEFT
4777 int rlflag;
4778 #endif
4780 unsigned off_from;
4781 unsigned off_to;
4782 int col = 0;
4783 #if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_VERTSPLIT)
4784 int hl;
4785 #endif
4786 int force = FALSE; /* force update rest of the line */
4787 int redraw_this /* bool: does character need redraw? */
4788 #ifdef FEAT_GUI
4789 = TRUE /* For GUI when while-loop empty */
4790 #endif
4792 int redraw_next; /* redraw_this for next character */
4793 #ifdef FEAT_MBYTE
4794 int clear_next = FALSE;
4795 int char_cells; /* 1: normal char */
4796 /* 2: occupies two display cells */
4797 # define CHAR_CELLS char_cells
4798 #else
4799 # define CHAR_CELLS 1
4800 #endif
4802 # ifdef FEAT_CLIPBOARD
4803 clip_may_clear_selection(row, row);
4804 # endif
4806 off_from = (unsigned)(current_ScreenLine - ScreenLines);
4807 off_to = LineOffset[row] + coloff;
4809 #ifdef FEAT_RIGHTLEFT
4810 if (rlflag)
4812 /* Clear rest first, because it's left of the text. */
4813 if (clear_width > 0)
4815 while (col <= endcol && ScreenLines[off_to] == ' '
4816 && ScreenAttrs[off_to] == 0
4817 # ifdef FEAT_MBYTE
4818 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
4819 # endif
4822 ++off_to;
4823 ++col;
4825 if (col <= endcol)
4826 screen_fill(row, row + 1, col + coloff,
4827 endcol + coloff + 1, ' ', ' ', 0);
4829 col = endcol + 1;
4830 off_to = LineOffset[row] + col + coloff;
4831 off_from += col;
4832 endcol = (clear_width > 0 ? clear_width : -clear_width);
4834 #endif /* FEAT_RIGHTLEFT */
4836 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
4838 while (col < endcol)
4840 #ifdef FEAT_MBYTE
4841 if (has_mbyte && (col + 1 < endcol))
4842 char_cells = (*mb_off2cells)(off_from);
4843 else
4844 char_cells = 1;
4845 #endif
4847 redraw_this = redraw_next;
4848 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
4849 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
4851 #ifdef FEAT_GUI
4852 /* If the next character was bold, then redraw the current character to
4853 * remove any pixels that might have spilt over into us. This only
4854 * happens in the GUI.
4856 if (redraw_next && gui.in_use)
4858 hl = ScreenAttrs[off_to + CHAR_CELLS];
4859 if (hl > HL_ALL)
4860 hl = syn_attr2attr(hl);
4861 if (hl & HL_BOLD)
4862 redraw_this = TRUE;
4864 #endif
4866 if (redraw_this)
4869 * Special handling when 'xs' termcap flag set (hpterm):
4870 * Attributes for characters are stored at the position where the
4871 * cursor is when writing the highlighting code. The
4872 * start-highlighting code must be written with the cursor on the
4873 * first highlighted character. The stop-highlighting code must
4874 * be written with the cursor just after the last highlighted
4875 * character.
4876 * Overwriting a character doesn't remove it's highlighting. Need
4877 * to clear the rest of the line, and force redrawing it
4878 * completely.
4880 if ( p_wiv
4881 && !force
4882 #ifdef FEAT_GUI
4883 && !gui.in_use
4884 #endif
4885 && ScreenAttrs[off_to] != 0
4886 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
4889 * Need to remove highlighting attributes here.
4891 windgoto(row, col + coloff);
4892 out_str(T_CE); /* clear rest of this screen line */
4893 screen_start(); /* don't know where cursor is now */
4894 force = TRUE; /* force redraw of rest of the line */
4895 redraw_next = TRUE; /* or else next char would miss out */
4898 * If the previous character was highlighted, need to stop
4899 * highlighting at this character.
4901 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
4903 screen_attr = ScreenAttrs[off_to - 1];
4904 term_windgoto(row, col + coloff);
4905 screen_stop_highlight();
4907 else
4908 screen_attr = 0; /* highlighting has stopped */
4910 #ifdef FEAT_MBYTE
4911 if (enc_dbcs != 0)
4913 /* Check if overwriting a double-byte with a single-byte or
4914 * the other way around requires another character to be
4915 * redrawn. For UTF-8 this isn't needed, because comparing
4916 * ScreenLinesUC[] is sufficient. */
4917 if (char_cells == 1
4918 && col + 1 < endcol
4919 && (*mb_off2cells)(off_to) > 1)
4921 /* Writing a single-cell character over a double-cell
4922 * character: need to redraw the next cell. */
4923 ScreenLines[off_to + 1] = 0;
4924 redraw_next = TRUE;
4926 else if (char_cells == 2
4927 && col + 2 < endcol
4928 && (*mb_off2cells)(off_to) == 1
4929 && (*mb_off2cells)(off_to + 1) > 1)
4931 /* Writing the second half of a double-cell character over
4932 * a double-cell character: need to redraw the second
4933 * cell. */
4934 ScreenLines[off_to + 2] = 0;
4935 redraw_next = TRUE;
4938 if (enc_dbcs == DBCS_JPNU)
4939 ScreenLines2[off_to] = ScreenLines2[off_from];
4941 /* When writing a single-width character over a double-width
4942 * character and at the end of the redrawn text, need to clear out
4943 * the right halve of the old character.
4944 * Also required when writing the right halve of a double-width
4945 * char over the left halve of an existing one. */
4946 if (has_mbyte && col + char_cells == endcol
4947 && ((char_cells == 1
4948 && (*mb_off2cells)(off_to) > 1)
4949 || (char_cells == 2
4950 && (*mb_off2cells)(off_to) == 1
4951 && (*mb_off2cells)(off_to + 1) > 1)))
4952 clear_next = TRUE;
4953 #endif
4955 ScreenLines[off_to] = ScreenLines[off_from];
4956 #ifdef FEAT_MBYTE
4957 if (enc_utf8)
4959 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
4960 if (ScreenLinesUC[off_from] != 0)
4962 int i;
4964 for (i = 0; i < Screen_mco; ++i)
4965 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
4968 if (char_cells == 2)
4969 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
4970 #endif
4972 #if defined(FEAT_GUI) || defined(UNIX)
4973 /* The bold trick makes a single row of pixels appear in the next
4974 * character. When a bold character is removed, the next
4975 * character should be redrawn too. This happens for our own GUI
4976 * and for some xterms. */
4977 if (
4978 # ifdef FEAT_GUI
4979 gui.in_use
4980 # endif
4981 # if defined(FEAT_GUI) && defined(UNIX)
4983 # endif
4984 # ifdef UNIX
4985 term_is_xterm
4986 # endif
4989 hl = ScreenAttrs[off_to];
4990 if (hl > HL_ALL)
4991 hl = syn_attr2attr(hl);
4992 if (hl & HL_BOLD)
4993 redraw_next = TRUE;
4995 #endif
4996 ScreenAttrs[off_to] = ScreenAttrs[off_from];
4997 #ifdef FEAT_MBYTE
4998 /* For simplicity set the attributes of second half of a
4999 * double-wide character equal to the first half. */
5000 if (char_cells == 2)
5001 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
5003 if (enc_dbcs != 0 && char_cells == 2)
5004 screen_char_2(off_to, row, col + coloff);
5005 else
5006 #endif
5007 screen_char(off_to, row, col + coloff);
5009 else if ( p_wiv
5010 #ifdef FEAT_GUI
5011 && !gui.in_use
5012 #endif
5013 && col + coloff > 0)
5015 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
5018 * Don't output stop-highlight when moving the cursor, it will
5019 * stop the highlighting when it should continue.
5021 screen_attr = 0;
5023 else if (screen_attr != 0)
5024 screen_stop_highlight();
5027 off_to += CHAR_CELLS;
5028 off_from += CHAR_CELLS;
5029 col += CHAR_CELLS;
5032 #ifdef FEAT_MBYTE
5033 if (clear_next)
5035 /* Clear the second half of a double-wide character of which the left
5036 * half was overwritten with a single-wide character. */
5037 ScreenLines[off_to] = ' ';
5038 if (enc_utf8)
5039 ScreenLinesUC[off_to] = 0;
5040 screen_char(off_to, row, col + coloff);
5042 #endif
5044 if (clear_width > 0
5045 #ifdef FEAT_RIGHTLEFT
5046 && !rlflag
5047 #endif
5050 #ifdef FEAT_GUI
5051 int startCol = col;
5052 #endif
5054 /* blank out the rest of the line */
5055 while (col < clear_width && ScreenLines[off_to] == ' '
5056 && ScreenAttrs[off_to] == 0
5057 #ifdef FEAT_MBYTE
5058 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5059 #endif
5062 ++off_to;
5063 ++col;
5065 if (col < clear_width)
5067 #ifdef FEAT_GUI
5069 * In the GUI, clearing the rest of the line may leave pixels
5070 * behind if the first character cleared was bold. Some bold
5071 * fonts spill over the left. In this case we redraw the previous
5072 * character too. If we didn't skip any blanks above, then we
5073 * only redraw if the character wasn't already redrawn anyway.
5075 if (gui.in_use && (col > startCol || !redraw_this)
5076 # ifdef FEAT_MBYTE
5077 && enc_dbcs == 0
5078 # endif
5081 hl = ScreenAttrs[off_to];
5082 if (hl > HL_ALL || (hl & HL_BOLD))
5083 screen_char(off_to - 1, row, col + coloff - 1);
5085 #endif
5086 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
5087 ' ', ' ', 0);
5088 #ifdef FEAT_VERTSPLIT
5089 off_to += clear_width - col;
5090 col = clear_width;
5091 #endif
5095 if (clear_width > 0)
5097 #ifdef FEAT_VERTSPLIT
5098 /* For a window that's left of another, draw the separator char. */
5099 if (col + coloff < Columns)
5101 int c;
5103 c = fillchar_vsep(&hl);
5104 if (ScreenLines[off_to] != c
5105 # ifdef FEAT_MBYTE
5106 || (enc_utf8 && (int)ScreenLinesUC[off_to]
5107 != (c >= 0x80 ? c : 0))
5108 # endif
5109 || ScreenAttrs[off_to] != hl)
5111 ScreenLines[off_to] = c;
5112 ScreenAttrs[off_to] = hl;
5113 # ifdef FEAT_MBYTE
5114 if (enc_utf8)
5116 if (c >= 0x80)
5118 ScreenLinesUC[off_to] = c;
5119 ScreenLinesC[0][off_to] = 0;
5121 else
5122 ScreenLinesUC[off_to] = 0;
5124 # endif
5125 screen_char(off_to, row, col + coloff);
5128 else
5129 #endif
5130 LineWraps[row] = FALSE;
5134 #if defined(FEAT_RIGHTLEFT) || defined(PROTO)
5136 * Mirror text "str" for right-left displaying.
5137 * Only works for single-byte characters (e.g., numbers).
5139 void
5140 rl_mirror(str)
5141 char_u *str;
5143 char_u *p1, *p2;
5144 int t;
5146 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
5148 t = *p1;
5149 *p1 = *p2;
5150 *p2 = t;
5153 #endif
5155 #if defined(FEAT_WINDOWS) || defined(PROTO)
5157 * mark all status lines for redraw; used after first :cd
5159 void
5160 status_redraw_all()
5162 win_T *wp;
5164 for (wp = firstwin; wp; wp = wp->w_next)
5165 if (wp->w_status_height)
5167 wp->w_redr_status = TRUE;
5168 redraw_later(VALID);
5173 * mark all status lines of the current buffer for redraw
5175 void
5176 status_redraw_curbuf()
5178 win_T *wp;
5180 for (wp = firstwin; wp; wp = wp->w_next)
5181 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
5183 wp->w_redr_status = TRUE;
5184 redraw_later(VALID);
5189 * Redraw all status lines that need to be redrawn.
5191 void
5192 redraw_statuslines()
5194 win_T *wp;
5196 for (wp = firstwin; wp; wp = wp->w_next)
5197 if (wp->w_redr_status)
5198 win_redr_status(wp);
5199 if (redraw_tabline)
5200 draw_tabline();
5202 #endif
5204 #if (defined(FEAT_WILDMENU) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
5206 * Redraw all status lines at the bottom of frame "frp".
5208 void
5209 win_redraw_last_status(frp)
5210 frame_T *frp;
5212 if (frp->fr_layout == FR_LEAF)
5213 frp->fr_win->w_redr_status = TRUE;
5214 else if (frp->fr_layout == FR_ROW)
5216 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
5217 win_redraw_last_status(frp);
5219 else /* frp->fr_layout == FR_COL */
5221 frp = frp->fr_child;
5222 while (frp->fr_next != NULL)
5223 frp = frp->fr_next;
5224 win_redraw_last_status(frp);
5227 #endif
5229 #ifdef FEAT_VERTSPLIT
5231 * Draw the verticap separator right of window "wp" starting with line "row".
5233 static void
5234 draw_vsep_win(wp, row)
5235 win_T *wp;
5236 int row;
5238 int hl;
5239 int c;
5241 if (wp->w_vsep_width)
5243 /* draw the vertical separator right of this window */
5244 c = fillchar_vsep(&hl);
5245 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
5246 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
5247 c, ' ', hl);
5250 #endif
5252 #ifdef FEAT_WILDMENU
5253 static int status_match_len __ARGS((expand_T *xp, char_u *s));
5254 static int skip_status_match_char __ARGS((expand_T *xp, char_u *s));
5257 * Get the lenght of an item as it will be shown in the status line.
5259 static int
5260 status_match_len(xp, s)
5261 expand_T *xp;
5262 char_u *s;
5264 int len = 0;
5266 #ifdef FEAT_MENU
5267 int emenu = (xp->xp_context == EXPAND_MENUS
5268 || xp->xp_context == EXPAND_MENUNAMES);
5270 /* Check for menu separators - replace with '|'. */
5271 if (emenu && menu_is_separator(s))
5272 return 1;
5273 #endif
5275 while (*s != NUL)
5277 if (skip_status_match_char(xp, s))
5278 ++s;
5279 len += ptr2cells(s);
5280 mb_ptr_adv(s);
5283 return len;
5287 * Return TRUE for characters that are not displayed in a status match.
5288 * These are backslashes used for escaping. Do show backslashes in help tags.
5290 static int
5291 skip_status_match_char(xp, s)
5292 expand_T *xp;
5293 char_u *s;
5295 return ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
5296 #ifdef FEAT_MENU
5297 || ((xp->xp_context == EXPAND_MENUS
5298 || xp->xp_context == EXPAND_MENUNAMES)
5299 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
5300 #endif
5305 * Show wildchar matches in the status line.
5306 * Show at least the "match" item.
5307 * We start at item 'first_match' in the list and show all matches that fit.
5309 * If inversion is possible we use it. Else '=' characters are used.
5311 void
5312 win_redr_status_matches(xp, num_matches, matches, match, showtail)
5313 expand_T *xp;
5314 int num_matches;
5315 char_u **matches; /* list of matches */
5316 int match;
5317 int showtail;
5319 #define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
5320 int row;
5321 char_u *buf;
5322 int len;
5323 int clen; /* lenght in screen cells */
5324 int fillchar;
5325 int attr;
5326 int i;
5327 int highlight = TRUE;
5328 char_u *selstart = NULL;
5329 int selstart_col = 0;
5330 char_u *selend = NULL;
5331 static int first_match = 0;
5332 int add_left = FALSE;
5333 char_u *s;
5334 #ifdef FEAT_MENU
5335 int emenu;
5336 #endif
5337 #if defined(FEAT_MBYTE) || defined(FEAT_MENU)
5338 int l;
5339 #endif
5341 if (matches == NULL) /* interrupted completion? */
5342 return;
5344 #ifdef FEAT_MBYTE
5345 if (has_mbyte)
5346 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
5347 else
5348 #endif
5349 buf = alloc((unsigned)Columns + 1);
5350 if (buf == NULL)
5351 return;
5353 if (match == -1) /* don't show match but original text */
5355 match = 0;
5356 highlight = FALSE;
5358 /* count 1 for the ending ">" */
5359 clen = status_match_len(xp, L_MATCH(match)) + 3;
5360 if (match == 0)
5361 first_match = 0;
5362 else if (match < first_match)
5364 /* jumping left, as far as we can go */
5365 first_match = match;
5366 add_left = TRUE;
5368 else
5370 /* check if match fits on the screen */
5371 for (i = first_match; i < match; ++i)
5372 clen += status_match_len(xp, L_MATCH(i)) + 2;
5373 if (first_match > 0)
5374 clen += 2;
5375 /* jumping right, put match at the left */
5376 if ((long)clen > Columns)
5378 first_match = match;
5379 /* if showing the last match, we can add some on the left */
5380 clen = 2;
5381 for (i = match; i < num_matches; ++i)
5383 clen += status_match_len(xp, L_MATCH(i)) + 2;
5384 if ((long)clen >= Columns)
5385 break;
5387 if (i == num_matches)
5388 add_left = TRUE;
5391 if (add_left)
5392 while (first_match > 0)
5394 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
5395 if ((long)clen >= Columns)
5396 break;
5397 --first_match;
5400 fillchar = fillchar_status(&attr, TRUE);
5402 if (first_match == 0)
5404 *buf = NUL;
5405 len = 0;
5407 else
5409 STRCPY(buf, "< ");
5410 len = 2;
5412 clen = len;
5414 i = first_match;
5415 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
5417 if (i == match)
5419 selstart = buf + len;
5420 selstart_col = clen;
5423 s = L_MATCH(i);
5424 /* Check for menu separators - replace with '|' */
5425 #ifdef FEAT_MENU
5426 emenu = (xp->xp_context == EXPAND_MENUS
5427 || xp->xp_context == EXPAND_MENUNAMES);
5428 if (emenu && menu_is_separator(s))
5430 STRCPY(buf + len, transchar('|'));
5431 l = (int)STRLEN(buf + len);
5432 len += l;
5433 clen += l;
5435 else
5436 #endif
5437 for ( ; *s != NUL; ++s)
5439 if (skip_status_match_char(xp, s))
5440 ++s;
5441 clen += ptr2cells(s);
5442 #ifdef FEAT_MBYTE
5443 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
5445 STRNCPY(buf + len, s, l);
5446 s += l - 1;
5447 len += l;
5449 else
5450 #endif
5452 STRCPY(buf + len, transchar_byte(*s));
5453 len += (int)STRLEN(buf + len);
5456 if (i == match)
5457 selend = buf + len;
5459 *(buf + len++) = ' ';
5460 *(buf + len++) = ' ';
5461 clen += 2;
5462 if (++i == num_matches)
5463 break;
5466 if (i != num_matches)
5468 *(buf + len++) = '>';
5469 ++clen;
5472 buf[len] = NUL;
5474 row = cmdline_row - 1;
5475 if (row >= 0)
5477 if (wild_menu_showing == 0)
5479 if (msg_scrolled > 0)
5481 /* Put the wildmenu just above the command line. If there is
5482 * no room, scroll the screen one line up. */
5483 if (cmdline_row == Rows - 1)
5485 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
5486 ++msg_scrolled;
5488 else
5490 ++cmdline_row;
5491 ++row;
5493 wild_menu_showing = WM_SCROLLED;
5495 else
5497 /* Create status line if needed by setting 'laststatus' to 2.
5498 * Set 'winminheight' to zero to avoid that the window is
5499 * resized. */
5500 if (lastwin->w_status_height == 0)
5502 save_p_ls = p_ls;
5503 save_p_wmh = p_wmh;
5504 p_ls = 2;
5505 p_wmh = 0;
5506 last_status(FALSE);
5508 wild_menu_showing = WM_SHOWN;
5512 screen_puts(buf, row, 0, attr);
5513 if (selstart != NULL && highlight)
5515 *selend = NUL;
5516 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
5519 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
5522 #ifdef FEAT_VERTSPLIT
5523 win_redraw_last_status(topframe);
5524 #else
5525 lastwin->w_redr_status = TRUE;
5526 #endif
5527 vim_free(buf);
5529 #endif
5531 #if defined(FEAT_WINDOWS) || defined(PROTO)
5533 * Redraw the status line of window wp.
5535 * If inversion is possible we use it. Else '=' characters are used.
5537 void
5538 win_redr_status(wp)
5539 win_T *wp;
5541 int row;
5542 char_u *p;
5543 int len;
5544 int fillchar;
5545 int attr;
5546 int this_ru_col;
5548 wp->w_redr_status = FALSE;
5549 if (wp->w_status_height == 0)
5551 /* no status line, can only be last window */
5552 redraw_cmdline = TRUE;
5554 else if (!redrawing()
5555 #ifdef FEAT_INS_EXPAND
5556 /* don't update status line when popup menu is visible and may be
5557 * drawn over it */
5558 || pum_visible()
5559 #endif
5562 /* Don't redraw right now, do it later. */
5563 wp->w_redr_status = TRUE;
5565 #ifdef FEAT_STL_OPT
5566 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
5568 /* redraw custom status line */
5569 redraw_custum_statusline(wp);
5571 #endif
5572 else
5574 fillchar = fillchar_status(&attr, wp == curwin);
5576 get_trans_bufname(wp->w_buffer);
5577 p = NameBuff;
5578 len = (int)STRLEN(p);
5580 if (wp->w_buffer->b_help
5581 #ifdef FEAT_QUICKFIX
5582 || wp->w_p_pvw
5583 #endif
5584 || bufIsChanged(wp->w_buffer)
5585 || wp->w_buffer->b_p_ro)
5586 *(p + len++) = ' ';
5587 if (wp->w_buffer->b_help)
5589 STRCPY(p + len, _("[Help]"));
5590 len += (int)STRLEN(p + len);
5592 #ifdef FEAT_QUICKFIX
5593 if (wp->w_p_pvw)
5595 STRCPY(p + len, _("[Preview]"));
5596 len += (int)STRLEN(p + len);
5598 #endif
5599 if (bufIsChanged(wp->w_buffer))
5601 STRCPY(p + len, "[+]");
5602 len += 3;
5604 if (wp->w_buffer->b_p_ro)
5606 STRCPY(p + len, "[RO]");
5607 len += 4;
5610 #ifndef FEAT_VERTSPLIT
5611 this_ru_col = ru_col;
5612 if (this_ru_col < (Columns + 1) / 2)
5613 this_ru_col = (Columns + 1) / 2;
5614 #else
5615 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
5616 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
5617 this_ru_col = (W_WIDTH(wp) + 1) / 2;
5618 if (this_ru_col <= 1)
5620 p = (char_u *)"<"; /* No room for file name! */
5621 len = 1;
5623 else
5624 #endif
5625 #ifdef FEAT_MBYTE
5626 if (has_mbyte)
5628 int clen = 0, i;
5630 /* Count total number of display cells. */
5631 for (i = 0; p[i] != NUL; i += (*mb_ptr2len)(p + i))
5632 clen += (*mb_ptr2cells)(p + i);
5633 /* Find first character that will fit.
5634 * Going from start to end is much faster for DBCS. */
5635 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
5636 i += (*mb_ptr2len)(p + i))
5637 clen -= (*mb_ptr2cells)(p + i);
5638 len = clen;
5639 if (i > 0)
5641 p = p + i - 1;
5642 *p = '<';
5643 ++len;
5647 else
5648 #endif
5649 if (len > this_ru_col - 1)
5651 p += len - (this_ru_col - 1);
5652 *p = '<';
5653 len = this_ru_col - 1;
5656 row = W_WINROW(wp) + wp->w_height;
5657 screen_puts(p, row, W_WINCOL(wp), attr);
5658 screen_fill(row, row + 1, len + W_WINCOL(wp),
5659 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
5661 if (get_keymap_str(wp, NameBuff, MAXPATHL)
5662 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
5663 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
5664 - 1 + W_WINCOL(wp)), attr);
5666 #ifdef FEAT_CMDL_INFO
5667 win_redr_ruler(wp, TRUE);
5668 #endif
5671 #ifdef FEAT_VERTSPLIT
5673 * May need to draw the character below the vertical separator.
5675 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
5677 if (stl_connected(wp))
5678 fillchar = fillchar_status(&attr, wp == curwin);
5679 else
5680 fillchar = fillchar_vsep(&attr);
5681 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
5682 attr);
5684 #endif
5687 #ifdef FEAT_STL_OPT
5689 * Redraw the status line according to 'statusline' and take care of any
5690 * errors encountered.
5692 static void
5693 redraw_custum_statusline(wp)
5694 win_T *wp;
5696 int save_called_emsg = called_emsg;
5698 called_emsg = FALSE;
5699 win_redr_custom(wp, FALSE);
5700 if (called_emsg)
5701 set_string_option_direct((char_u *)"statusline", -1,
5702 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
5703 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
5704 called_emsg |= save_called_emsg;
5706 #endif
5708 # ifdef FEAT_VERTSPLIT
5710 * Return TRUE if the status line of window "wp" is connected to the status
5711 * line of the window right of it. If not, then it's a vertical separator.
5712 * Only call if (wp->w_vsep_width != 0).
5715 stl_connected(wp)
5716 win_T *wp;
5718 frame_T *fr;
5720 fr = wp->w_frame;
5721 while (fr->fr_parent != NULL)
5723 if (fr->fr_parent->fr_layout == FR_COL)
5725 if (fr->fr_next != NULL)
5726 break;
5728 else
5730 if (fr->fr_next != NULL)
5731 return TRUE;
5733 fr = fr->fr_parent;
5735 return FALSE;
5737 # endif
5739 #endif /* FEAT_WINDOWS */
5741 #if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
5743 * Get the value to show for the language mappings, active 'keymap'.
5746 get_keymap_str(wp, buf, len)
5747 win_T *wp;
5748 char_u *buf; /* buffer for the result */
5749 int len; /* length of buffer */
5751 char_u *p;
5753 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
5754 return FALSE;
5757 #ifdef FEAT_EVAL
5758 buf_T *old_curbuf = curbuf;
5759 win_T *old_curwin = curwin;
5760 char_u *s;
5762 curbuf = wp->w_buffer;
5763 curwin = wp;
5764 STRCPY(buf, "b:keymap_name"); /* must be writable */
5765 ++emsg_skip;
5766 s = p = eval_to_string(buf, NULL, FALSE);
5767 --emsg_skip;
5768 curbuf = old_curbuf;
5769 curwin = old_curwin;
5770 if (p == NULL || *p == NUL)
5771 #endif
5773 #ifdef FEAT_KEYMAP
5774 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
5775 p = wp->w_buffer->b_p_keymap;
5776 else
5777 #endif
5778 p = (char_u *)"lang";
5780 if ((int)(STRLEN(p) + 3) < len)
5781 sprintf((char *)buf, "<%s>", p);
5782 else
5783 buf[0] = NUL;
5784 #ifdef FEAT_EVAL
5785 vim_free(s);
5786 #endif
5788 return buf[0] != NUL;
5790 #endif
5792 #if defined(FEAT_STL_OPT) || defined(PROTO)
5794 * Redraw the status line or ruler of window "wp".
5795 * When "wp" is NULL redraw the tab pages line from 'tabline'.
5797 static void
5798 win_redr_custom(wp, draw_ruler)
5799 win_T *wp;
5800 int draw_ruler; /* TRUE or FALSE */
5802 int attr;
5803 int curattr;
5804 int row;
5805 int col = 0;
5806 int maxwidth;
5807 int width;
5808 int n;
5809 int len;
5810 int fillchar;
5811 char_u buf[MAXPATHL];
5812 char_u *p;
5813 struct stl_hlrec hltab[STL_MAX_ITEM];
5814 struct stl_hlrec tabtab[STL_MAX_ITEM];
5815 int use_sandbox = FALSE;
5817 /* setup environment for the task at hand */
5818 if (wp == NULL)
5820 /* Use 'tabline'. Always at the first line of the screen. */
5821 p = p_tal;
5822 row = 0;
5823 fillchar = ' ';
5824 attr = hl_attr(HLF_TPF);
5825 maxwidth = Columns;
5826 # ifdef FEAT_EVAL
5827 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
5828 # endif
5830 else
5832 row = W_WINROW(wp) + wp->w_height;
5833 fillchar = fillchar_status(&attr, wp == curwin);
5834 maxwidth = W_WIDTH(wp);
5836 if (draw_ruler)
5838 p = p_ruf;
5839 /* advance past any leading group spec - implicit in ru_col */
5840 if (*p == '%')
5842 if (*++p == '-')
5843 p++;
5844 if (atoi((char *) p))
5845 while (VIM_ISDIGIT(*p))
5846 p++;
5847 if (*p++ != '(')
5848 p = p_ruf;
5850 #ifdef FEAT_VERTSPLIT
5851 col = ru_col - (Columns - W_WIDTH(wp));
5852 if (col < (W_WIDTH(wp) + 1) / 2)
5853 col = (W_WIDTH(wp) + 1) / 2;
5854 #else
5855 col = ru_col;
5856 if (col > (Columns + 1) / 2)
5857 col = (Columns + 1) / 2;
5858 #endif
5859 maxwidth = W_WIDTH(wp) - col;
5860 #ifdef FEAT_WINDOWS
5861 if (!wp->w_status_height)
5862 #endif
5864 row = Rows - 1;
5865 --maxwidth; /* writing in last column may cause scrolling */
5866 fillchar = ' ';
5867 attr = 0;
5870 # ifdef FEAT_EVAL
5871 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
5872 # endif
5874 else
5876 if (*wp->w_p_stl != NUL)
5877 p = wp->w_p_stl;
5878 else
5879 p = p_stl;
5880 # ifdef FEAT_EVAL
5881 use_sandbox = was_set_insecurely((char_u *)"statusline",
5882 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
5883 # endif
5886 #ifdef FEAT_VERTSPLIT
5887 col += W_WINCOL(wp);
5888 #endif
5891 if (maxwidth <= 0)
5892 return;
5894 width = build_stl_str_hl(wp == NULL ? curwin : wp,
5895 buf, sizeof(buf),
5896 p, use_sandbox,
5897 fillchar, maxwidth, hltab, tabtab);
5898 len = (int)STRLEN(buf);
5900 while (width < maxwidth && len < sizeof(buf) - 1)
5902 #ifdef FEAT_MBYTE
5903 len += (*mb_char2bytes)(fillchar, buf + len);
5904 #else
5905 buf[len++] = fillchar;
5906 #endif
5907 ++width;
5909 buf[len] = NUL;
5912 * Draw each snippet with the specified highlighting.
5914 curattr = attr;
5915 p = buf;
5916 for (n = 0; hltab[n].start != NULL; n++)
5918 len = (int)(hltab[n].start - p);
5919 screen_puts_len(p, len, row, col, curattr);
5920 col += vim_strnsize(p, len);
5921 p = hltab[n].start;
5923 if (hltab[n].userhl == 0)
5924 curattr = attr;
5925 else if (hltab[n].userhl < 0)
5926 curattr = syn_id2attr(-hltab[n].userhl);
5927 #ifdef FEAT_WINDOWS
5928 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
5929 curattr = highlight_stlnc[hltab[n].userhl - 1];
5930 #endif
5931 else
5932 curattr = highlight_user[hltab[n].userhl - 1];
5934 screen_puts(p, row, col, curattr);
5936 if (wp == NULL)
5938 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
5939 col = 0;
5940 len = 0;
5941 p = buf;
5942 fillchar = 0;
5943 for (n = 0; tabtab[n].start != NULL; n++)
5945 len += vim_strnsize(p, (int)(tabtab[n].start - p));
5946 while (col < len)
5947 TabPageIdxs[col++] = fillchar;
5948 p = tabtab[n].start;
5949 fillchar = tabtab[n].userhl;
5951 while (col < Columns)
5952 TabPageIdxs[col++] = fillchar;
5956 #endif /* FEAT_STL_OPT */
5959 * Output a single character directly to the screen and update ScreenLines.
5961 void
5962 screen_putchar(c, row, col, attr)
5963 int c;
5964 int row, col;
5965 int attr;
5967 #ifdef FEAT_MBYTE
5968 char_u buf[MB_MAXBYTES + 1];
5970 buf[(*mb_char2bytes)(c, buf)] = NUL;
5971 #else
5972 char_u buf[2];
5974 buf[0] = c;
5975 buf[1] = NUL;
5976 #endif
5977 screen_puts(buf, row, col, attr);
5981 * Get a single character directly from ScreenLines into "bytes[]".
5982 * Also return its attribute in *attrp;
5984 void
5985 screen_getbytes(row, col, bytes, attrp)
5986 int row, col;
5987 char_u *bytes;
5988 int *attrp;
5990 unsigned off;
5992 /* safety check */
5993 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
5995 off = LineOffset[row] + col;
5996 *attrp = ScreenAttrs[off];
5997 bytes[0] = ScreenLines[off];
5998 bytes[1] = NUL;
6000 #ifdef FEAT_MBYTE
6001 if (enc_utf8 && ScreenLinesUC[off] != 0)
6002 bytes[utfc_char2bytes(off, bytes)] = NUL;
6003 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6005 bytes[0] = ScreenLines[off];
6006 bytes[1] = ScreenLines2[off];
6007 bytes[2] = NUL;
6009 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
6011 bytes[1] = ScreenLines[off + 1];
6012 bytes[2] = NUL;
6014 #endif
6018 #ifdef FEAT_MBYTE
6019 static int screen_comp_differs __ARGS((int, int*));
6022 * Return TRUE if composing characters for screen posn "off" differs from
6023 * composing characters in "u8cc".
6025 static int
6026 screen_comp_differs(off, u8cc)
6027 int off;
6028 int *u8cc;
6030 int i;
6032 for (i = 0; i < Screen_mco; ++i)
6034 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
6035 return TRUE;
6036 if (u8cc[i] == 0)
6037 break;
6039 return FALSE;
6041 #endif
6044 * Put string '*text' on the screen at position 'row' and 'col', with
6045 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
6046 * Note: only outputs within one row, message is truncated at screen boundary!
6047 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
6049 void
6050 screen_puts(text, row, col, attr)
6051 char_u *text;
6052 int row;
6053 int col;
6054 int attr;
6056 screen_puts_len(text, -1, row, col, attr);
6060 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
6061 * a NUL.
6063 void
6064 screen_puts_len(text, len, row, col, attr)
6065 char_u *text;
6066 int len;
6067 int row;
6068 int col;
6069 int attr;
6071 unsigned off;
6072 char_u *ptr = text;
6073 int c;
6074 #ifdef FEAT_MBYTE
6075 int mbyte_blen = 1;
6076 int mbyte_cells = 1;
6077 int u8c = 0;
6078 int u8cc[MAX_MCO];
6079 int clear_next_cell = FALSE;
6080 # ifdef FEAT_ARABIC
6081 int prev_c = 0; /* previous Arabic character */
6082 int pc, nc, nc1;
6083 int pcc[MAX_MCO];
6084 # endif
6085 #endif
6087 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
6088 return;
6090 off = LineOffset[row] + col;
6091 while (*ptr != NUL && col < screen_Columns
6092 && (len < 0 || (int)(ptr - text) < len))
6094 c = *ptr;
6095 #ifdef FEAT_MBYTE
6096 /* check if this is the first byte of a multibyte */
6097 if (has_mbyte)
6099 if (enc_utf8 && len > 0)
6100 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
6101 else
6102 mbyte_blen = (*mb_ptr2len)(ptr);
6103 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6104 mbyte_cells = 1;
6105 else if (enc_dbcs != 0)
6106 mbyte_cells = mbyte_blen;
6107 else /* enc_utf8 */
6109 if (len >= 0)
6110 u8c = utfc_ptr2char_len(ptr, u8cc,
6111 (int)((text + len) - ptr));
6112 else
6113 u8c = utfc_ptr2char(ptr, u8cc);
6114 mbyte_cells = utf_char2cells(u8c);
6115 /* Non-BMP character: display as ? or fullwidth ?. */
6116 if (u8c >= 0x10000)
6118 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
6119 if (attr == 0)
6120 attr = hl_attr(HLF_8);
6122 # ifdef FEAT_ARABIC
6123 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
6125 /* Do Arabic shaping. */
6126 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
6128 /* Past end of string to be displayed. */
6129 nc = NUL;
6130 nc1 = NUL;
6132 else
6134 nc = utfc_ptr2char(ptr + mbyte_blen, pcc);
6135 nc1 = pcc[0];
6137 pc = prev_c;
6138 prev_c = u8c;
6139 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
6141 else
6142 prev_c = u8c;
6143 # endif
6146 #endif
6148 if (ScreenLines[off] != c
6149 #ifdef FEAT_MBYTE
6150 || (mbyte_cells == 2
6151 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
6152 || (enc_dbcs == DBCS_JPNU
6153 && c == 0x8e
6154 && ScreenLines2[off] != ptr[1])
6155 || (enc_utf8
6156 && (ScreenLinesUC[off] != (u8char_T)u8c
6157 || screen_comp_differs(off, u8cc)))
6158 #endif
6159 || ScreenAttrs[off] != attr
6160 || exmode_active
6163 #if defined(FEAT_GUI) || defined(UNIX)
6164 /* The bold trick makes a single row of pixels appear in the next
6165 * character. When a bold character is removed, the next
6166 * character should be redrawn too. This happens for our own GUI
6167 * and for some xterms.
6168 * Force the redraw by setting the attribute to a different value
6169 * than "attr", the contents of ScreenLines[] may be needed by
6170 * mb_off2cells() further on.
6171 * Don't do this for the last drawn character, because the next
6172 * character may not be redrawn. */
6173 if (
6174 # ifdef FEAT_GUI
6175 gui.in_use
6176 # endif
6177 # if defined(FEAT_GUI) && defined(UNIX)
6179 # endif
6180 # ifdef UNIX
6181 term_is_xterm
6182 # endif
6185 int n;
6187 n = ScreenAttrs[off];
6188 # ifdef FEAT_MBYTE
6189 if (col + mbyte_cells < screen_Columns
6190 && (n > HL_ALL || (n & HL_BOLD))
6191 && (len < 0 ? ptr[mbyte_blen] != NUL
6192 : ptr + mbyte_blen < text + len))
6193 ScreenAttrs[off + mbyte_cells] = attr + 1;
6194 # else
6195 if (col + 1 < screen_Columns
6196 && (n > HL_ALL || (n & HL_BOLD))
6197 && (len < 0 ? ptr[1] != NUL : ptr + 1 < text + len))
6198 ScreenLines[off + 1] = 0;
6199 # endif
6201 #endif
6202 #ifdef FEAT_MBYTE
6203 /* When at the end of the text and overwriting a two-cell
6204 * character with a one-cell character, need to clear the next
6205 * cell. Also when overwriting the left halve of a two-cell char
6206 * with the right halve of a two-cell char. Do this only once
6207 * (mb_off2cells() may return 2 on the right halve). */
6208 if (clear_next_cell)
6209 clear_next_cell = FALSE;
6210 else if (has_mbyte
6211 && (len < 0 ? ptr[mbyte_blen] == NUL
6212 : ptr + mbyte_blen >= text + len)
6213 && ((mbyte_cells == 1 && (*mb_off2cells)(off) > 1)
6214 || (mbyte_cells == 2
6215 && (*mb_off2cells)(off) == 1
6216 && (*mb_off2cells)(off + 1) > 1)))
6217 clear_next_cell = TRUE;
6219 /* Make sure we never leave a second byte of a double-byte behind,
6220 * it confuses mb_off2cells(). */
6221 if (enc_dbcs
6222 && ((mbyte_cells == 1 && (*mb_off2cells)(off) > 1)
6223 || (mbyte_cells == 2
6224 && (*mb_off2cells)(off) == 1
6225 && (*mb_off2cells)(off + 1) > 1)))
6226 ScreenLines[off + mbyte_blen] = 0;
6227 #endif
6228 ScreenLines[off] = c;
6229 ScreenAttrs[off] = attr;
6230 #ifdef FEAT_MBYTE
6231 if (enc_utf8)
6233 if (c < 0x80 && u8cc[0] == 0)
6234 ScreenLinesUC[off] = 0;
6235 else
6237 int i;
6239 ScreenLinesUC[off] = u8c;
6240 for (i = 0; i < Screen_mco; ++i)
6242 ScreenLinesC[i][off] = u8cc[i];
6243 if (u8cc[i] == 0)
6244 break;
6247 if (mbyte_cells == 2)
6249 ScreenLines[off + 1] = 0;
6250 ScreenAttrs[off + 1] = attr;
6252 screen_char(off, row, col);
6254 else if (mbyte_cells == 2)
6256 ScreenLines[off + 1] = ptr[1];
6257 ScreenAttrs[off + 1] = attr;
6258 screen_char_2(off, row, col);
6260 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6262 ScreenLines2[off] = ptr[1];
6263 screen_char(off, row, col);
6265 else
6266 #endif
6267 screen_char(off, row, col);
6269 #ifdef FEAT_MBYTE
6270 if (has_mbyte)
6272 off += mbyte_cells;
6273 col += mbyte_cells;
6274 ptr += mbyte_blen;
6275 if (clear_next_cell)
6276 ptr = (char_u *)" ";
6278 else
6279 #endif
6281 ++off;
6282 ++col;
6283 ++ptr;
6288 #ifdef FEAT_SEARCH_EXTRA
6290 * Prepare for 'searchhl' highlighting.
6292 static void
6293 start_search_hl()
6295 if (p_hls && !no_hlsearch)
6297 last_pat_prog(&search_hl.rm);
6298 search_hl.attr = hl_attr(HLF_L);
6303 * Clean up for 'searchhl' highlighting.
6305 static void
6306 end_search_hl()
6308 if (search_hl.rm.regprog != NULL)
6310 vim_free(search_hl.rm.regprog);
6311 search_hl.rm.regprog = NULL;
6316 * Advance to the match in window "wp" line "lnum" or past it.
6318 static void
6319 prepare_search_hl(wp, lnum)
6320 win_T *wp;
6321 linenr_T lnum;
6323 match_T *shl; /* points to search_hl or match_hl */
6324 int n;
6325 int i;
6328 * When using a multi-line pattern, start searching at the top
6329 * of the window or just after a closed fold.
6330 * Do this both for search_hl and match_hl[3].
6332 for (i = 3; i >= 0; --i)
6334 shl = (i == 3) ? &search_hl : &match_hl[i];
6335 if (shl->rm.regprog != NULL
6336 && shl->lnum == 0
6337 && re_multiline(shl->rm.regprog))
6339 if (shl->first_lnum == 0)
6341 # ifdef FEAT_FOLDING
6342 for (shl->first_lnum = lnum;
6343 shl->first_lnum > wp->w_topline; --shl->first_lnum)
6344 if (hasFoldingWin(wp, shl->first_lnum - 1,
6345 NULL, NULL, TRUE, NULL))
6346 break;
6347 # else
6348 shl->first_lnum = wp->w_topline;
6349 # endif
6351 n = 0;
6352 while (shl->first_lnum < lnum && shl->rm.regprog != NULL)
6354 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n);
6355 if (shl->lnum != 0)
6357 shl->first_lnum = shl->lnum
6358 + shl->rm.endpos[0].lnum
6359 - shl->rm.startpos[0].lnum;
6360 n = shl->rm.endpos[0].col;
6362 else
6364 ++shl->first_lnum;
6365 n = 0;
6373 * Search for a next 'searchl' or ":match" match.
6374 * Uses shl->buf.
6375 * Sets shl->lnum and shl->rm contents.
6376 * Note: Assumes a previous match is always before "lnum", unless
6377 * shl->lnum is zero.
6378 * Careful: Any pointers for buffer lines will become invalid.
6380 static void
6381 next_search_hl(win, shl, lnum, mincol)
6382 win_T *win;
6383 match_T *shl; /* points to search_hl or match_hl */
6384 linenr_T lnum;
6385 colnr_T mincol; /* minimal column for a match */
6387 linenr_T l;
6388 colnr_T matchcol;
6389 long nmatched;
6391 if (shl->lnum != 0)
6393 /* Check for three situations:
6394 * 1. If the "lnum" is below a previous match, start a new search.
6395 * 2. If the previous match includes "mincol", use it.
6396 * 3. Continue after the previous match.
6398 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
6399 if (lnum > l)
6400 shl->lnum = 0;
6401 else if (lnum < l || shl->rm.endpos[0].col > mincol)
6402 return;
6406 * Repeat searching for a match until one is found that includes "mincol"
6407 * or none is found in this line.
6409 called_emsg = FALSE;
6410 for (;;)
6412 /* Three situations:
6413 * 1. No useful previous match: search from start of line.
6414 * 2. Not Vi compatible or empty match: continue at next character.
6415 * Break the loop if this is beyond the end of the line.
6416 * 3. Vi compatible searching: continue at end of previous match.
6418 if (shl->lnum == 0)
6419 matchcol = 0;
6420 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
6421 || (shl->rm.endpos[0].lnum == 0
6422 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
6424 char_u *ml;
6426 matchcol = shl->rm.startpos[0].col;
6427 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
6428 if (*ml == NUL)
6430 ++matchcol;
6431 shl->lnum = 0;
6432 break;
6434 #ifdef FEAT_MBYTE
6435 if (has_mbyte)
6436 matchcol += mb_ptr2len(ml);
6437 else
6438 #endif
6439 ++matchcol;
6441 else
6442 matchcol = shl->rm.endpos[0].col;
6444 shl->lnum = lnum;
6445 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum, matchcol);
6446 if (called_emsg)
6448 /* Error while handling regexp: stop using this regexp. */
6449 vim_free(shl->rm.regprog);
6450 shl->rm.regprog = NULL;
6451 no_hlsearch = TRUE;
6452 break;
6454 if (nmatched == 0)
6456 shl->lnum = 0; /* no match found */
6457 break;
6459 if (shl->rm.startpos[0].lnum > 0
6460 || shl->rm.startpos[0].col >= mincol
6461 || nmatched > 1
6462 || shl->rm.endpos[0].col > mincol)
6464 shl->lnum += shl->rm.startpos[0].lnum;
6465 break; /* useful match found */
6469 #endif
6471 static void
6472 screen_start_highlight(attr)
6473 int attr;
6475 attrentry_T *aep = NULL;
6477 screen_attr = attr;
6478 if (full_screen
6479 #ifdef WIN3264
6480 && termcap_active
6481 #endif
6484 #ifdef FEAT_GUI
6485 if (gui.in_use)
6487 char buf[20];
6489 /* The GUI handles this internally. */
6490 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
6491 OUT_STR(buf);
6493 else
6494 #endif
6496 if (attr > HL_ALL) /* special HL attr. */
6498 if (t_colors > 1)
6499 aep = syn_cterm_attr2entry(attr);
6500 else
6501 aep = syn_term_attr2entry(attr);
6502 if (aep == NULL) /* did ":syntax clear" */
6503 attr = 0;
6504 else
6505 attr = aep->ae_attr;
6507 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
6508 out_str(T_MD);
6509 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
6510 && cterm_normal_fg_bold)
6511 /* If the Normal FG color has BOLD attribute and the new HL
6512 * has a FG color defined, clear BOLD. */
6513 out_str(T_ME);
6514 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
6515 out_str(T_SO);
6516 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
6517 /* underline or undercurl */
6518 out_str(T_US);
6519 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
6520 out_str(T_CZH);
6521 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
6522 out_str(T_MR);
6525 * Output the color or start string after bold etc., in case the
6526 * bold etc. override the color setting.
6528 if (aep != NULL)
6530 if (t_colors > 1)
6532 if (aep->ae_u.cterm.fg_color)
6533 term_fg_color(aep->ae_u.cterm.fg_color - 1);
6534 if (aep->ae_u.cterm.bg_color)
6535 term_bg_color(aep->ae_u.cterm.bg_color - 1);
6537 else
6539 if (aep->ae_u.term.start != NULL)
6540 out_str(aep->ae_u.term.start);
6547 void
6548 screen_stop_highlight()
6550 int do_ME = FALSE; /* output T_ME code */
6552 if (screen_attr != 0
6553 #ifdef WIN3264
6554 && termcap_active
6555 #endif
6558 #ifdef FEAT_GUI
6559 if (gui.in_use)
6561 char buf[20];
6563 /* use internal GUI code */
6564 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
6565 OUT_STR(buf);
6567 else
6568 #endif
6570 if (screen_attr > HL_ALL) /* special HL attr. */
6572 attrentry_T *aep;
6574 if (t_colors > 1)
6577 * Assume that t_me restores the original colors!
6579 aep = syn_cterm_attr2entry(screen_attr);
6580 if (aep != NULL && (aep->ae_u.cterm.fg_color
6581 || aep->ae_u.cterm.bg_color))
6582 do_ME = TRUE;
6584 else
6586 aep = syn_term_attr2entry(screen_attr);
6587 if (aep != NULL && aep->ae_u.term.stop != NULL)
6589 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
6590 do_ME = TRUE;
6591 else
6592 out_str(aep->ae_u.term.stop);
6595 if (aep == NULL) /* did ":syntax clear" */
6596 screen_attr = 0;
6597 else
6598 screen_attr = aep->ae_attr;
6602 * Often all ending-codes are equal to T_ME. Avoid outputting the
6603 * same sequence several times.
6605 if (screen_attr & HL_STANDOUT)
6607 if (STRCMP(T_SE, T_ME) == 0)
6608 do_ME = TRUE;
6609 else
6610 out_str(T_SE);
6612 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
6614 if (STRCMP(T_UE, T_ME) == 0)
6615 do_ME = TRUE;
6616 else
6617 out_str(T_UE);
6619 if (screen_attr & HL_ITALIC)
6621 if (STRCMP(T_CZR, T_ME) == 0)
6622 do_ME = TRUE;
6623 else
6624 out_str(T_CZR);
6626 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
6627 out_str(T_ME);
6629 if (t_colors > 1)
6631 /* set Normal cterm colors */
6632 if (cterm_normal_fg_color != 0)
6633 term_fg_color(cterm_normal_fg_color - 1);
6634 if (cterm_normal_bg_color != 0)
6635 term_bg_color(cterm_normal_bg_color - 1);
6636 if (cterm_normal_fg_bold)
6637 out_str(T_MD);
6641 screen_attr = 0;
6645 * Reset the colors for a cterm. Used when leaving Vim.
6646 * The machine specific code may override this again.
6648 void
6649 reset_cterm_colors()
6651 if (t_colors > 1)
6653 /* set Normal cterm colors */
6654 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
6656 out_str(T_OP);
6657 screen_attr = -1;
6659 if (cterm_normal_fg_bold)
6661 out_str(T_ME);
6662 screen_attr = -1;
6668 * Put character ScreenLines["off"] on the screen at position "row" and "col",
6669 * using the attributes from ScreenAttrs["off"].
6671 static void
6672 screen_char(off, row, col)
6673 unsigned off;
6674 int row;
6675 int col;
6677 int attr;
6679 /* Check for illegal values, just in case (could happen just after
6680 * resizing). */
6681 if (row >= screen_Rows || col >= screen_Columns)
6682 return;
6684 /* Outputting the last character on the screen may scrollup the screen.
6685 * Don't to it! Mark the character invalid (update it when scrolled up) */
6686 if (row == screen_Rows - 1 && col == screen_Columns - 1
6687 #ifdef FEAT_RIGHTLEFT
6688 /* account for first command-line character in rightleft mode */
6689 && !cmdmsg_rl
6690 #endif
6693 ScreenAttrs[off] = (sattr_T)-1;
6694 return;
6698 * Stop highlighting first, so it's easier to move the cursor.
6700 #if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
6701 if (screen_char_attr != 0)
6702 attr = screen_char_attr;
6703 else
6704 #endif
6705 attr = ScreenAttrs[off];
6706 if (screen_attr != attr)
6707 screen_stop_highlight();
6709 windgoto(row, col);
6711 if (screen_attr != attr)
6712 screen_start_highlight(attr);
6714 #ifdef FEAT_MBYTE
6715 if (enc_utf8 && ScreenLinesUC[off] != 0)
6717 char_u buf[MB_MAXBYTES + 1];
6719 /* Convert UTF-8 character to bytes and write it. */
6721 buf[utfc_char2bytes(off, buf)] = NUL;
6723 out_str(buf);
6724 if (utf_char2cells(ScreenLinesUC[off]) > 1)
6725 ++screen_cur_col;
6727 else
6728 #endif
6730 #ifdef FEAT_MBYTE
6731 out_flush_check();
6732 #endif
6733 out_char(ScreenLines[off]);
6734 #ifdef FEAT_MBYTE
6735 /* double-byte character in single-width cell */
6736 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6737 out_char(ScreenLines2[off]);
6738 #endif
6741 screen_cur_col++;
6744 #ifdef FEAT_MBYTE
6747 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
6748 * on the screen at position 'row' and 'col'.
6749 * The attributes of the first byte is used for all. This is required to
6750 * output the two bytes of a double-byte character with nothing in between.
6752 static void
6753 screen_char_2(off, row, col)
6754 unsigned off;
6755 int row;
6756 int col;
6758 /* Check for illegal values (could be wrong when screen was resized). */
6759 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
6760 return;
6762 /* Outputting the last character on the screen may scrollup the screen.
6763 * Don't to it! Mark the character invalid (update it when scrolled up) */
6764 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
6766 ScreenAttrs[off] = (sattr_T)-1;
6767 return;
6770 /* Output the first byte normally (positions the cursor), then write the
6771 * second byte directly. */
6772 screen_char(off, row, col);
6773 out_char(ScreenLines[off + 1]);
6774 ++screen_cur_col;
6776 #endif
6778 #if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
6780 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
6781 * This uses the contents of ScreenLines[] and doesn't change it.
6783 void
6784 screen_draw_rectangle(row, col, height, width, invert)
6785 int row;
6786 int col;
6787 int height;
6788 int width;
6789 int invert;
6791 int r, c;
6792 int off;
6794 /* Can't use ScreenLines unless initialized */
6795 if (ScreenLines == NULL)
6796 return;
6798 if (invert)
6799 screen_char_attr = HL_INVERSE;
6800 for (r = row; r < row + height; ++r)
6802 off = LineOffset[r];
6803 for (c = col; c < col + width; ++c)
6805 #ifdef FEAT_MBYTE
6806 if (enc_dbcs != 0 && dbcs_off2cells(off + c) > 1)
6808 screen_char_2(off + c, r, c);
6809 ++c;
6811 else
6812 #endif
6814 screen_char(off + c, r, c);
6815 #ifdef FEAT_MBYTE
6816 if (utf_off2cells(off + c) > 1)
6817 ++c;
6818 #endif
6822 screen_char_attr = 0;
6824 #endif
6826 #ifdef FEAT_VERTSPLIT
6828 * Redraw the characters for a vertically split window.
6830 static void
6831 redraw_block(row, end, wp)
6832 int row;
6833 int end;
6834 win_T *wp;
6836 int col;
6837 int width;
6839 # ifdef FEAT_CLIPBOARD
6840 clip_may_clear_selection(row, end - 1);
6841 # endif
6843 if (wp == NULL)
6845 col = 0;
6846 width = Columns;
6848 else
6850 col = wp->w_wincol;
6851 width = wp->w_width;
6853 screen_draw_rectangle(row, col, end - row, width, FALSE);
6855 #endif
6858 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
6859 * with character 'c1' in first column followed by 'c2' in the other columns.
6860 * Use attributes 'attr'.
6862 void
6863 screen_fill(start_row, end_row, start_col, end_col, c1, c2, attr)
6864 int start_row, end_row;
6865 int start_col, end_col;
6866 int c1, c2;
6867 int attr;
6869 int row;
6870 int col;
6871 int off;
6872 int end_off;
6873 int did_delete;
6874 int c;
6875 int norm_term;
6876 #if defined(FEAT_GUI) || defined(UNIX)
6877 int force_next = FALSE;
6878 #endif
6880 if (end_row > screen_Rows) /* safety check */
6881 end_row = screen_Rows;
6882 if (end_col > screen_Columns) /* safety check */
6883 end_col = screen_Columns;
6884 if (ScreenLines == NULL
6885 || start_row >= end_row
6886 || start_col >= end_col) /* nothing to do */
6887 return;
6889 /* it's a "normal" terminal when not in a GUI or cterm */
6890 norm_term = (
6891 #ifdef FEAT_GUI
6892 !gui.in_use &&
6893 #endif
6894 t_colors <= 1);
6895 for (row = start_row; row < end_row; ++row)
6898 * Try to use delete-line termcap code, when no attributes or in a
6899 * "normal" terminal, where a bold/italic space is just a
6900 * space.
6902 did_delete = FALSE;
6903 if (c2 == ' '
6904 && end_col == Columns
6905 && can_clear(T_CE)
6906 && (attr == 0
6907 || (norm_term
6908 && attr <= HL_ALL
6909 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
6912 * check if we really need to clear something
6914 col = start_col;
6915 if (c1 != ' ') /* don't clear first char */
6916 ++col;
6918 off = LineOffset[row] + col;
6919 end_off = LineOffset[row] + end_col;
6921 /* skip blanks (used often, keep it fast!) */
6922 #ifdef FEAT_MBYTE
6923 if (enc_utf8)
6924 while (off < end_off && ScreenLines[off] == ' '
6925 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
6926 ++off;
6927 else
6928 #endif
6929 while (off < end_off && ScreenLines[off] == ' '
6930 && ScreenAttrs[off] == 0)
6931 ++off;
6932 if (off < end_off) /* something to be cleared */
6934 col = off - LineOffset[row];
6935 screen_stop_highlight();
6936 term_windgoto(row, col);/* clear rest of this screen line */
6937 out_str(T_CE);
6938 screen_start(); /* don't know where cursor is now */
6939 col = end_col - col;
6940 while (col--) /* clear chars in ScreenLines */
6942 ScreenLines[off] = ' ';
6943 #ifdef FEAT_MBYTE
6944 if (enc_utf8)
6945 ScreenLinesUC[off] = 0;
6946 #endif
6947 ScreenAttrs[off] = 0;
6948 ++off;
6951 did_delete = TRUE; /* the chars are cleared now */
6954 off = LineOffset[row] + start_col;
6955 c = c1;
6956 for (col = start_col; col < end_col; ++col)
6958 if (ScreenLines[off] != c
6959 #ifdef FEAT_MBYTE
6960 || (enc_utf8 && (int)ScreenLinesUC[off]
6961 != (c >= 0x80 ? c : 0))
6962 #endif
6963 || ScreenAttrs[off] != attr
6964 #if defined(FEAT_GUI) || defined(UNIX)
6965 || force_next
6966 #endif
6969 #if defined(FEAT_GUI) || defined(UNIX)
6970 /* The bold trick may make a single row of pixels appear in
6971 * the next character. When a bold character is removed, the
6972 * next character should be redrawn too. This happens for our
6973 * own GUI and for some xterms. */
6974 if (
6975 # ifdef FEAT_GUI
6976 gui.in_use
6977 # endif
6978 # if defined(FEAT_GUI) && defined(UNIX)
6980 # endif
6981 # ifdef UNIX
6982 term_is_xterm
6983 # endif
6986 if (ScreenLines[off] != ' '
6987 && (ScreenAttrs[off] > HL_ALL
6988 || ScreenAttrs[off] & HL_BOLD))
6989 force_next = TRUE;
6990 else
6991 force_next = FALSE;
6993 #endif
6994 ScreenLines[off] = c;
6995 #ifdef FEAT_MBYTE
6996 if (enc_utf8)
6998 if (c >= 0x80)
7000 ScreenLinesUC[off] = c;
7001 ScreenLinesC[0][off] = 0;
7003 else
7004 ScreenLinesUC[off] = 0;
7006 #endif
7007 ScreenAttrs[off] = attr;
7008 if (!did_delete || c != ' ')
7009 screen_char(off, row, col);
7011 ++off;
7012 if (col == start_col)
7014 if (did_delete)
7015 break;
7016 c = c2;
7019 if (end_col == Columns)
7020 LineWraps[row] = FALSE;
7021 if (row == Rows - 1) /* overwritten the command line */
7023 redraw_cmdline = TRUE;
7024 if (c1 == ' ' && c2 == ' ')
7025 clear_cmdline = FALSE; /* command line has been cleared */
7026 if (start_col == 0)
7027 mode_displayed = FALSE; /* mode cleared or overwritten */
7033 * Check if there should be a delay. Used before clearing or redrawing the
7034 * screen or the command line.
7036 void
7037 check_for_delay(check_msg_scroll)
7038 int check_msg_scroll;
7040 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
7041 && !did_wait_return
7042 && emsg_silent == 0)
7044 out_flush();
7045 ui_delay(1000L, TRUE);
7046 emsg_on_display = FALSE;
7047 if (check_msg_scroll)
7048 msg_scroll = FALSE;
7053 * screen_valid - allocate screen buffers if size changed
7054 * If "clear" is TRUE: clear screen if it has been resized.
7055 * Returns TRUE if there is a valid screen to write to.
7056 * Returns FALSE when starting up and screen not initialized yet.
7059 screen_valid(clear)
7060 int clear;
7062 screenalloc(clear); /* allocate screen buffers if size changed */
7063 return (ScreenLines != NULL);
7067 * Resize the shell to Rows and Columns.
7068 * Allocate ScreenLines[] and associated items.
7070 * There may be some time between setting Rows and Columns and (re)allocating
7071 * ScreenLines[]. This happens when starting up and when (manually) changing
7072 * the shell size. Always use screen_Rows and screen_Columns to access items
7073 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
7074 * final size of the shell is needed.
7076 void
7077 screenalloc(clear)
7078 int clear;
7080 int new_row, old_row;
7081 #ifdef FEAT_GUI
7082 int old_Rows;
7083 #endif
7084 win_T *wp;
7085 int outofmem = FALSE;
7086 int len;
7087 schar_T *new_ScreenLines;
7088 #ifdef FEAT_MBYTE
7089 u8char_T *new_ScreenLinesUC = NULL;
7090 u8char_T *new_ScreenLinesC[MAX_MCO];
7091 schar_T *new_ScreenLines2 = NULL;
7092 int i;
7093 #endif
7094 sattr_T *new_ScreenAttrs;
7095 unsigned *new_LineOffset;
7096 char_u *new_LineWraps;
7097 #ifdef FEAT_WINDOWS
7098 short *new_TabPageIdxs;
7099 tabpage_T *tp;
7100 #endif
7101 static int entered = FALSE; /* avoid recursiveness */
7102 static int done_outofmem_msg = FALSE; /* did outofmem message */
7105 * Allocation of the screen buffers is done only when the size changes and
7106 * when Rows and Columns have been set and we have started doing full
7107 * screen stuff.
7109 if ((ScreenLines != NULL
7110 && Rows == screen_Rows
7111 && Columns == screen_Columns
7112 #ifdef FEAT_MBYTE
7113 && enc_utf8 == (ScreenLinesUC != NULL)
7114 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
7115 && p_mco == Screen_mco
7116 #endif
7118 || Rows == 0
7119 || Columns == 0
7120 || (!full_screen && ScreenLines == NULL))
7121 return;
7124 * It's possible that we produce an out-of-memory message below, which
7125 * will cause this function to be called again. To break the loop, just
7126 * return here.
7128 if (entered)
7129 return;
7130 entered = TRUE;
7133 * Note that the window sizes are updated before reallocating the arrays,
7134 * thus we must not redraw here!
7136 ++RedrawingDisabled;
7138 win_new_shellsize(); /* fit the windows in the new sized shell */
7140 comp_col(); /* recompute columns for shown command and ruler */
7143 * We're changing the size of the screen.
7144 * - Allocate new arrays for ScreenLines and ScreenAttrs.
7145 * - Move lines from the old arrays into the new arrays, clear extra
7146 * lines (unless the screen is going to be cleared).
7147 * - Free the old arrays.
7149 * If anything fails, make ScreenLines NULL, so we don't do anything!
7150 * Continuing with the old ScreenLines may result in a crash, because the
7151 * size is wrong.
7153 FOR_ALL_TAB_WINDOWS(tp, wp)
7154 win_free_lsize(wp);
7156 new_ScreenLines = (schar_T *)lalloc((long_u)(
7157 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7158 #ifdef FEAT_MBYTE
7159 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T) * MAX_MCO);
7160 if (enc_utf8)
7162 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
7163 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
7164 for (i = 0; i < p_mco; ++i)
7165 new_ScreenLinesC[i] = (u8char_T *)lalloc((long_u)(
7166 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
7168 if (enc_dbcs == DBCS_JPNU)
7169 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
7170 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7171 #endif
7172 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
7173 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
7174 new_LineOffset = (unsigned *)lalloc((long_u)(
7175 Rows * sizeof(unsigned)), FALSE);
7176 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
7177 #ifdef FEAT_WINDOWS
7178 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
7179 #endif
7181 FOR_ALL_TAB_WINDOWS(tp, wp)
7183 if (win_alloc_lines(wp) == FAIL)
7185 outofmem = TRUE;
7186 #ifdef FEAT_WINDOWS
7187 break;
7188 #endif
7192 #ifdef FEAT_MBYTE
7193 for (i = 0; i < p_mco; ++i)
7194 if (new_ScreenLinesC[i] == NULL)
7195 break;
7196 #endif
7197 if (new_ScreenLines == NULL
7198 #ifdef FEAT_MBYTE
7199 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
7200 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
7201 #endif
7202 || new_ScreenAttrs == NULL
7203 || new_LineOffset == NULL
7204 || new_LineWraps == NULL
7205 #ifdef FEAT_WINDOWS
7206 || new_TabPageIdxs == NULL
7207 #endif
7208 || outofmem)
7210 if (ScreenLines != NULL || !done_outofmem_msg)
7212 /* guess the size */
7213 do_outofmem_msg((long_u)((Rows + 1) * Columns));
7215 /* Remember we did this to avoid getting outofmem messages over
7216 * and over again. */
7217 done_outofmem_msg = TRUE;
7219 vim_free(new_ScreenLines);
7220 new_ScreenLines = NULL;
7221 #ifdef FEAT_MBYTE
7222 vim_free(new_ScreenLinesUC);
7223 new_ScreenLinesUC = NULL;
7224 for (i = 0; i < p_mco; ++i)
7226 vim_free(new_ScreenLinesC[i]);
7227 new_ScreenLinesC[i] = NULL;
7229 vim_free(new_ScreenLines2);
7230 new_ScreenLines2 = NULL;
7231 #endif
7232 vim_free(new_ScreenAttrs);
7233 new_ScreenAttrs = NULL;
7234 vim_free(new_LineOffset);
7235 new_LineOffset = NULL;
7236 vim_free(new_LineWraps);
7237 new_LineWraps = NULL;
7238 #ifdef FEAT_WINDOWS
7239 vim_free(new_TabPageIdxs);
7240 new_TabPageIdxs = NULL;
7241 #endif
7243 else
7245 done_outofmem_msg = FALSE;
7247 for (new_row = 0; new_row < Rows; ++new_row)
7249 new_LineOffset[new_row] = new_row * Columns;
7250 new_LineWraps[new_row] = FALSE;
7253 * If the screen is not going to be cleared, copy as much as
7254 * possible from the old screen to the new one and clear the rest
7255 * (used when resizing the window at the "--more--" prompt or when
7256 * executing an external command, for the GUI).
7258 if (!clear)
7260 (void)vim_memset(new_ScreenLines + new_row * Columns,
7261 ' ', (size_t)Columns * sizeof(schar_T));
7262 #ifdef FEAT_MBYTE
7263 if (enc_utf8)
7265 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
7266 0, (size_t)Columns * sizeof(u8char_T));
7267 for (i = 0; i < p_mco; ++i)
7268 (void)vim_memset(new_ScreenLinesC[i]
7269 + new_row * Columns,
7270 0, (size_t)Columns * sizeof(u8char_T));
7272 if (enc_dbcs == DBCS_JPNU)
7273 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
7274 0, (size_t)Columns * sizeof(schar_T));
7275 #endif
7276 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
7277 0, (size_t)Columns * sizeof(sattr_T));
7278 old_row = new_row + (screen_Rows - Rows);
7279 if (old_row >= 0 && ScreenLines != NULL)
7281 if (screen_Columns < Columns)
7282 len = screen_Columns;
7283 else
7284 len = Columns;
7285 #ifdef FEAT_MBYTE
7286 /* When switching to utf-8 don't copy characters, they
7287 * may be invalid now. Also when p_mco changes. */
7288 if (!(enc_utf8 && ScreenLinesUC == NULL)
7289 && p_mco == Screen_mco)
7290 #endif
7291 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
7292 ScreenLines + LineOffset[old_row],
7293 (size_t)len * sizeof(schar_T));
7294 #ifdef FEAT_MBYTE
7295 if (enc_utf8 && ScreenLinesUC != NULL
7296 && p_mco == Screen_mco)
7298 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
7299 ScreenLinesUC + LineOffset[old_row],
7300 (size_t)len * sizeof(u8char_T));
7301 for (i = 0; i < p_mco; ++i)
7302 mch_memmove(new_ScreenLinesC[i]
7303 + new_LineOffset[new_row],
7304 ScreenLinesC[i] + LineOffset[old_row],
7305 (size_t)len * sizeof(u8char_T));
7307 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
7308 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
7309 ScreenLines2 + LineOffset[old_row],
7310 (size_t)len * sizeof(schar_T));
7311 #endif
7312 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
7313 ScreenAttrs + LineOffset[old_row],
7314 (size_t)len * sizeof(sattr_T));
7318 /* Use the last line of the screen for the current line. */
7319 current_ScreenLine = new_ScreenLines + Rows * Columns;
7322 free_screenlines();
7324 ScreenLines = new_ScreenLines;
7325 #ifdef FEAT_MBYTE
7326 ScreenLinesUC = new_ScreenLinesUC;
7327 for (i = 0; i < p_mco; ++i)
7328 ScreenLinesC[i] = new_ScreenLinesC[i];
7329 Screen_mco = p_mco;
7330 ScreenLines2 = new_ScreenLines2;
7331 #endif
7332 ScreenAttrs = new_ScreenAttrs;
7333 LineOffset = new_LineOffset;
7334 LineWraps = new_LineWraps;
7335 #ifdef FEAT_WINDOWS
7336 TabPageIdxs = new_TabPageIdxs;
7337 #endif
7339 /* It's important that screen_Rows and screen_Columns reflect the actual
7340 * size of ScreenLines[]. Set them before calling anything. */
7341 #ifdef FEAT_GUI
7342 old_Rows = screen_Rows;
7343 #endif
7344 screen_Rows = Rows;
7345 screen_Columns = Columns;
7347 must_redraw = CLEAR; /* need to clear the screen later */
7348 if (clear)
7349 screenclear2();
7351 #ifdef FEAT_GUI
7352 else if (gui.in_use
7353 && !gui.starting
7354 && ScreenLines != NULL
7355 && old_Rows != Rows)
7357 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
7359 * Adjust the position of the cursor, for when executing an external
7360 * command.
7362 if (msg_row >= Rows) /* Rows got smaller */
7363 msg_row = Rows - 1; /* put cursor at last row */
7364 else if (Rows > old_Rows) /* Rows got bigger */
7365 msg_row += Rows - old_Rows; /* put cursor in same place */
7366 if (msg_col >= Columns) /* Columns got smaller */
7367 msg_col = Columns - 1; /* put cursor at last column */
7369 #endif
7371 entered = FALSE;
7372 --RedrawingDisabled;
7374 #ifdef FEAT_AUTOCMD
7375 if (starting == 0)
7376 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
7377 #endif
7380 void
7381 free_screenlines()
7383 #ifdef FEAT_MBYTE
7384 int i;
7386 vim_free(ScreenLinesUC);
7387 for (i = 0; i < Screen_mco; ++i)
7388 vim_free(ScreenLinesC[i]);
7389 vim_free(ScreenLines2);
7390 #endif
7391 vim_free(ScreenLines);
7392 vim_free(ScreenAttrs);
7393 vim_free(LineOffset);
7394 vim_free(LineWraps);
7395 #ifdef FEAT_WINDOWS
7396 vim_free(TabPageIdxs);
7397 #endif
7400 void
7401 screenclear()
7403 check_for_delay(FALSE);
7404 screenalloc(FALSE); /* allocate screen buffers if size changed */
7405 screenclear2(); /* clear the screen */
7408 static void
7409 screenclear2()
7411 int i;
7413 if (starting == NO_SCREEN || ScreenLines == NULL
7414 #ifdef FEAT_GUI
7415 || (gui.in_use && gui.starting)
7416 #endif
7418 return;
7420 #ifdef FEAT_GUI
7421 if (!gui.in_use)
7422 #endif
7423 screen_attr = -1; /* force setting the Normal colors */
7424 screen_stop_highlight(); /* don't want highlighting here */
7426 #ifdef FEAT_CLIPBOARD
7427 /* disable selection without redrawing it */
7428 clip_scroll_selection(9999);
7429 #endif
7431 /* blank out ScreenLines */
7432 for (i = 0; i < Rows; ++i)
7434 lineclear(LineOffset[i], (int)Columns);
7435 LineWraps[i] = FALSE;
7438 if (can_clear(T_CL))
7440 out_str(T_CL); /* clear the display */
7441 clear_cmdline = FALSE;
7442 mode_displayed = FALSE;
7444 else
7446 /* can't clear the screen, mark all chars with invalid attributes */
7447 for (i = 0; i < Rows; ++i)
7448 lineinvalid(LineOffset[i], (int)Columns);
7449 clear_cmdline = TRUE;
7452 screen_cleared = TRUE; /* can use contents of ScreenLines now */
7454 win_rest_invalid(firstwin);
7455 redraw_cmdline = TRUE;
7456 #ifdef FEAT_WINDOWS
7457 redraw_tabline = TRUE;
7458 #endif
7459 if (must_redraw == CLEAR) /* no need to clear again */
7460 must_redraw = NOT_VALID;
7461 compute_cmdrow();
7462 msg_row = cmdline_row; /* put cursor on last line for messages */
7463 msg_col = 0;
7464 screen_start(); /* don't know where cursor is now */
7465 msg_scrolled = 0; /* can't scroll back */
7466 msg_didany = FALSE;
7467 msg_didout = FALSE;
7471 * Clear one line in ScreenLines.
7473 static void
7474 lineclear(off, width)
7475 unsigned off;
7476 int width;
7478 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
7479 #ifdef FEAT_MBYTE
7480 if (enc_utf8)
7481 (void)vim_memset(ScreenLinesUC + off, 0,
7482 (size_t)width * sizeof(u8char_T));
7483 #endif
7484 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
7488 * Mark one line in ScreenLines invalid by setting the attributes to an
7489 * invalid value.
7491 static void
7492 lineinvalid(off, width)
7493 unsigned off;
7494 int width;
7496 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
7499 #ifdef FEAT_VERTSPLIT
7501 * Copy part of a Screenline for vertically split window "wp".
7503 static void
7504 linecopy(to, from, wp)
7505 int to;
7506 int from;
7507 win_T *wp;
7509 unsigned off_to = LineOffset[to] + wp->w_wincol;
7510 unsigned off_from = LineOffset[from] + wp->w_wincol;
7512 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
7513 wp->w_width * sizeof(schar_T));
7514 # ifdef FEAT_MBYTE
7515 if (enc_utf8)
7517 int i;
7519 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
7520 wp->w_width * sizeof(u8char_T));
7521 for (i = 0; i < p_mco; ++i)
7522 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
7523 wp->w_width * sizeof(u8char_T));
7525 if (enc_dbcs == DBCS_JPNU)
7526 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
7527 wp->w_width * sizeof(schar_T));
7528 # endif
7529 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
7530 wp->w_width * sizeof(sattr_T));
7532 #endif
7535 * Return TRUE if clearing with term string "p" would work.
7536 * It can't work when the string is empty or it won't set the right background.
7539 can_clear(p)
7540 char_u *p;
7542 return (*p != NUL && (t_colors <= 1
7543 #ifdef FEAT_GUI
7544 || gui.in_use
7545 #endif
7546 || cterm_normal_bg_color == 0 || *T_UT != NUL));
7550 * Reset cursor position. Use whenever cursor was moved because of outputting
7551 * something directly to the screen (shell commands) or a terminal control
7552 * code.
7554 void
7555 screen_start()
7557 screen_cur_row = screen_cur_col = 9999;
7561 * Move the cursor to position "row","col" in the screen.
7562 * This tries to find the most efficient way to move, minimizing the number of
7563 * characters sent to the terminal.
7565 void
7566 windgoto(row, col)
7567 int row;
7568 int col;
7570 sattr_T *p;
7571 int i;
7572 int plan;
7573 int cost;
7574 int wouldbe_col;
7575 int noinvcurs;
7576 char_u *bs;
7577 int goto_cost;
7578 int attr;
7580 #define GOTO_COST 7 /* asssume a term_windgoto() takes about 7 chars */
7581 #define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
7583 #define PLAN_LE 1
7584 #define PLAN_CR 2
7585 #define PLAN_NL 3
7586 #define PLAN_WRITE 4
7587 /* Can't use ScreenLines unless initialized */
7588 if (ScreenLines == NULL)
7589 return;
7591 if (col != screen_cur_col || row != screen_cur_row)
7593 /* Check for valid position. */
7594 if (row < 0) /* window without text lines? */
7595 row = 0;
7596 if (row >= screen_Rows)
7597 row = screen_Rows - 1;
7598 if (col >= screen_Columns)
7599 col = screen_Columns - 1;
7601 /* check if no cursor movement is allowed in highlight mode */
7602 if (screen_attr && *T_MS == NUL)
7603 noinvcurs = HIGHL_COST;
7604 else
7605 noinvcurs = 0;
7606 goto_cost = GOTO_COST + noinvcurs;
7609 * Plan how to do the positioning:
7610 * 1. Use CR to move it to column 0, same row.
7611 * 2. Use T_LE to move it a few columns to the left.
7612 * 3. Use NL to move a few lines down, column 0.
7613 * 4. Move a few columns to the right with T_ND or by writing chars.
7615 * Don't do this if the cursor went beyond the last column, the cursor
7616 * position is unknown then (some terminals wrap, some don't )
7618 * First check if the highlighting attibutes allow us to write
7619 * characters to move the cursor to the right.
7621 if (row >= screen_cur_row && screen_cur_col < Columns)
7624 * If the cursor is in the same row, bigger col, we can use CR
7625 * or T_LE.
7627 bs = NULL; /* init for GCC */
7628 attr = screen_attr;
7629 if (row == screen_cur_row && col < screen_cur_col)
7631 /* "le" is preferred over "bc", because "bc" is obsolete */
7632 if (*T_LE)
7633 bs = T_LE; /* "cursor left" */
7634 else
7635 bs = T_BC; /* "backspace character (old) */
7636 if (*bs)
7637 cost = (screen_cur_col - col) * (int)STRLEN(bs);
7638 else
7639 cost = 999;
7640 if (col + 1 < cost) /* using CR is less characters */
7642 plan = PLAN_CR;
7643 wouldbe_col = 0;
7644 cost = 1; /* CR is just one character */
7646 else
7648 plan = PLAN_LE;
7649 wouldbe_col = col;
7651 if (noinvcurs) /* will stop highlighting */
7653 cost += noinvcurs;
7654 attr = 0;
7659 * If the cursor is above where we want to be, we can use CR LF.
7661 else if (row > screen_cur_row)
7663 plan = PLAN_NL;
7664 wouldbe_col = 0;
7665 cost = (row - screen_cur_row) * 2; /* CR LF */
7666 if (noinvcurs) /* will stop highlighting */
7668 cost += noinvcurs;
7669 attr = 0;
7674 * If the cursor is in the same row, smaller col, just use write.
7676 else
7678 plan = PLAN_WRITE;
7679 wouldbe_col = screen_cur_col;
7680 cost = 0;
7684 * Check if any characters that need to be written have the
7685 * correct attributes. Also avoid UTF-8 characters.
7687 i = col - wouldbe_col;
7688 if (i > 0)
7689 cost += i;
7690 if (cost < goto_cost && i > 0)
7693 * Check if the attributes are correct without additionally
7694 * stopping highlighting.
7696 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
7697 while (i && *p++ == attr)
7698 --i;
7699 if (i != 0)
7702 * Try if it works when highlighting is stopped here.
7704 if (*--p == 0)
7706 cost += noinvcurs;
7707 while (i && *p++ == 0)
7708 --i;
7710 if (i != 0)
7711 cost = 999; /* different attributes, don't do it */
7713 #ifdef FEAT_MBYTE
7714 if (enc_utf8)
7716 /* Don't use an UTF-8 char for positioning, it's slow. */
7717 for (i = wouldbe_col; i < col; ++i)
7718 if (ScreenLinesUC[LineOffset[row] + i] != 0)
7720 cost = 999;
7721 break;
7724 #endif
7728 * We can do it without term_windgoto()!
7730 if (cost < goto_cost)
7732 if (plan == PLAN_LE)
7734 if (noinvcurs)
7735 screen_stop_highlight();
7736 while (screen_cur_col > col)
7738 out_str(bs);
7739 --screen_cur_col;
7742 else if (plan == PLAN_CR)
7744 if (noinvcurs)
7745 screen_stop_highlight();
7746 out_char('\r');
7747 screen_cur_col = 0;
7749 else if (plan == PLAN_NL)
7751 if (noinvcurs)
7752 screen_stop_highlight();
7753 while (screen_cur_row < row)
7755 out_char('\n');
7756 ++screen_cur_row;
7758 screen_cur_col = 0;
7761 i = col - screen_cur_col;
7762 if (i > 0)
7765 * Use cursor-right if it's one character only. Avoids
7766 * removing a line of pixels from the last bold char, when
7767 * using the bold trick in the GUI.
7769 if (T_ND[0] != NUL && T_ND[1] == NUL)
7771 while (i-- > 0)
7772 out_char(*T_ND);
7774 else
7776 int off;
7778 off = LineOffset[row] + screen_cur_col;
7779 while (i-- > 0)
7781 if (ScreenAttrs[off] != screen_attr)
7782 screen_stop_highlight();
7783 #ifdef FEAT_MBYTE
7784 out_flush_check();
7785 #endif
7786 out_char(ScreenLines[off]);
7787 #ifdef FEAT_MBYTE
7788 if (enc_dbcs == DBCS_JPNU
7789 && ScreenLines[off] == 0x8e)
7790 out_char(ScreenLines2[off]);
7791 #endif
7792 ++off;
7798 else
7799 cost = 999;
7801 if (cost >= goto_cost)
7803 if (noinvcurs)
7804 screen_stop_highlight();
7805 if (row == screen_cur_row && (col > screen_cur_col) &&
7806 *T_CRI != NUL)
7807 term_cursor_right(col - screen_cur_col);
7808 else
7809 term_windgoto(row, col);
7811 screen_cur_row = row;
7812 screen_cur_col = col;
7817 * Set cursor to its position in the current window.
7819 void
7820 setcursor()
7822 if (redrawing())
7824 validate_cursor();
7825 windgoto(W_WINROW(curwin) + curwin->w_wrow,
7826 W_WINCOL(curwin) + (
7827 #ifdef FEAT_RIGHTLEFT
7828 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
7829 # ifdef FEAT_MBYTE
7830 has_mbyte ? (*mb_ptr2cells)(ml_get_cursor()) :
7831 # endif
7832 1)) :
7833 #endif
7834 curwin->w_wcol));
7840 * insert 'line_count' lines at 'row' in window 'wp'
7841 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
7842 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
7843 * scrolling.
7844 * Returns FAIL if the lines are not inserted, OK for success.
7847 win_ins_lines(wp, row, line_count, invalid, mayclear)
7848 win_T *wp;
7849 int row;
7850 int line_count;
7851 int invalid;
7852 int mayclear;
7854 int did_delete;
7855 int nextrow;
7856 int lastrow;
7857 int retval;
7859 if (invalid)
7860 wp->w_lines_valid = 0;
7862 if (wp->w_height < 5)
7863 return FAIL;
7865 if (line_count > wp->w_height - row)
7866 line_count = wp->w_height - row;
7868 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
7869 if (retval != MAYBE)
7870 return retval;
7873 * If there is a next window or a status line, we first try to delete the
7874 * lines at the bottom to avoid messing what is after the window.
7875 * If this fails and there are following windows, don't do anything to avoid
7876 * messing up those windows, better just redraw.
7878 did_delete = FALSE;
7879 #ifdef FEAT_WINDOWS
7880 if (wp->w_next != NULL || wp->w_status_height)
7882 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
7883 line_count, (int)Rows, FALSE, NULL) == OK)
7884 did_delete = TRUE;
7885 else if (wp->w_next)
7886 return FAIL;
7888 #endif
7890 * if no lines deleted, blank the lines that will end up below the window
7892 if (!did_delete)
7894 #ifdef FEAT_WINDOWS
7895 wp->w_redr_status = TRUE;
7896 #endif
7897 redraw_cmdline = TRUE;
7898 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
7899 lastrow = nextrow + line_count;
7900 if (lastrow > Rows)
7901 lastrow = Rows;
7902 screen_fill(nextrow - line_count, lastrow - line_count,
7903 W_WINCOL(wp), (int)W_ENDCOL(wp),
7904 ' ', ' ', 0);
7907 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
7908 == FAIL)
7910 /* deletion will have messed up other windows */
7911 if (did_delete)
7913 #ifdef FEAT_WINDOWS
7914 wp->w_redr_status = TRUE;
7915 #endif
7916 win_rest_invalid(W_NEXT(wp));
7918 return FAIL;
7921 return OK;
7925 * delete "line_count" window lines at "row" in window "wp"
7926 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
7927 * If "mayclear" is TRUE the screen will be cleared if it is faster than
7928 * scrolling
7929 * Return OK for success, FAIL if the lines are not deleted.
7932 win_del_lines(wp, row, line_count, invalid, mayclear)
7933 win_T *wp;
7934 int row;
7935 int line_count;
7936 int invalid;
7937 int mayclear;
7939 int retval;
7941 if (invalid)
7942 wp->w_lines_valid = 0;
7944 if (line_count > wp->w_height - row)
7945 line_count = wp->w_height - row;
7947 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
7948 if (retval != MAYBE)
7949 return retval;
7951 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
7952 (int)Rows, FALSE, NULL) == FAIL)
7953 return FAIL;
7955 #ifdef FEAT_WINDOWS
7957 * If there are windows or status lines below, try to put them at the
7958 * correct place. If we can't do that, they have to be redrawn.
7960 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
7962 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
7963 line_count, (int)Rows, NULL) == FAIL)
7965 wp->w_redr_status = TRUE;
7966 win_rest_invalid(wp->w_next);
7970 * If this is the last window and there is no status line, redraw the
7971 * command line later.
7973 else
7974 #endif
7975 redraw_cmdline = TRUE;
7976 return OK;
7980 * Common code for win_ins_lines() and win_del_lines().
7981 * Returns OK or FAIL when the work has been done.
7982 * Returns MAYBE when not finished yet.
7984 static int
7985 win_do_lines(wp, row, line_count, mayclear, del)
7986 win_T *wp;
7987 int row;
7988 int line_count;
7989 int mayclear;
7990 int del;
7992 int retval;
7994 if (!redrawing() || line_count <= 0)
7995 return FAIL;
7997 /* only a few lines left: redraw is faster */
7998 if (mayclear && Rows - line_count < 5
7999 #ifdef FEAT_VERTSPLIT
8000 && wp->w_width == Columns
8001 #endif
8004 screenclear(); /* will set wp->w_lines_valid to 0 */
8005 return FAIL;
8009 * Delete all remaining lines
8011 if (row + line_count >= wp->w_height)
8013 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
8014 W_WINCOL(wp), (int)W_ENDCOL(wp),
8015 ' ', ' ', 0);
8016 return OK;
8020 * when scrolling, the message on the command line should be cleared,
8021 * otherwise it will stay there forever.
8023 clear_cmdline = TRUE;
8026 * If the terminal can set a scroll region, use that.
8027 * Always do this in a vertically split window. This will redraw from
8028 * ScreenLines[] when t_CV isn't defined. That's faster than using
8029 * win_line().
8030 * Don't use a scroll region when we are going to redraw the text, writing
8031 * a character in the lower right corner of the scroll region causes a
8032 * scroll-up in the DJGPP version.
8034 if (scroll_region
8035 #ifdef FEAT_VERTSPLIT
8036 || W_WIDTH(wp) != Columns
8037 #endif
8040 #ifdef FEAT_VERTSPLIT
8041 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8042 #endif
8043 scroll_region_set(wp, row);
8044 if (del)
8045 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
8046 wp->w_height - row, FALSE, wp);
8047 else
8048 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
8049 wp->w_height - row, wp);
8050 #ifdef FEAT_VERTSPLIT
8051 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8052 #endif
8053 scroll_region_reset();
8054 return retval;
8057 #ifdef FEAT_WINDOWS
8058 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
8059 return FAIL;
8060 #endif
8062 return MAYBE;
8066 * window 'wp' and everything after it is messed up, mark it for redraw
8068 static void
8069 win_rest_invalid(wp)
8070 win_T *wp;
8072 #ifdef FEAT_WINDOWS
8073 while (wp != NULL)
8074 #else
8075 if (wp != NULL)
8076 #endif
8078 redraw_win_later(wp, NOT_VALID);
8079 #ifdef FEAT_WINDOWS
8080 wp->w_redr_status = TRUE;
8081 wp = wp->w_next;
8082 #endif
8084 redraw_cmdline = TRUE;
8088 * The rest of the routines in this file perform screen manipulations. The
8089 * given operation is performed physically on the screen. The corresponding
8090 * change is also made to the internal screen image. In this way, the editor
8091 * anticipates the effect of editing changes on the appearance of the screen.
8092 * That way, when we call screenupdate a complete redraw isn't usually
8093 * necessary. Another advantage is that we can keep adding code to anticipate
8094 * screen changes, and in the meantime, everything still works.
8098 * types for inserting or deleting lines
8100 #define USE_T_CAL 1
8101 #define USE_T_CDL 2
8102 #define USE_T_AL 3
8103 #define USE_T_CE 4
8104 #define USE_T_DL 5
8105 #define USE_T_SR 6
8106 #define USE_NL 7
8107 #define USE_T_CD 8
8108 #define USE_REDRAW 9
8111 * insert lines on the screen and update ScreenLines[]
8112 * 'end' is the line after the scrolled part. Normally it is Rows.
8113 * When scrolling region used 'off' is the offset from the top for the region.
8114 * 'row' and 'end' are relative to the start of the region.
8116 * return FAIL for failure, OK for success.
8119 screen_ins_lines(off, row, line_count, end, wp)
8120 int off;
8121 int row;
8122 int line_count;
8123 int end;
8124 win_T *wp; /* NULL or window to use width from */
8126 int i;
8127 int j;
8128 unsigned temp;
8129 int cursor_row;
8130 int type;
8131 int result_empty;
8132 int can_ce = can_clear(T_CE);
8135 * FAIL if
8136 * - there is no valid screen
8137 * - the screen has to be redrawn completely
8138 * - the line count is less than one
8139 * - the line count is more than 'ttyscroll'
8141 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
8142 return FAIL;
8145 * There are seven ways to insert lines:
8146 * 0. When in a vertically split window and t_CV isn't set, redraw the
8147 * characters from ScreenLines[].
8148 * 1. Use T_CD (clear to end of display) if it exists and the result of
8149 * the insert is just empty lines
8150 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
8151 * present or line_count > 1. It looks better if we do all the inserts
8152 * at once.
8153 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
8154 * insert is just empty lines and T_CE is not present or line_count >
8155 * 1.
8156 * 4. Use T_AL (insert line) if it exists.
8157 * 5. Use T_CE (erase line) if it exists and the result of the insert is
8158 * just empty lines.
8159 * 6. Use T_DL (delete line) if it exists and the result of the insert is
8160 * just empty lines.
8161 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
8162 * the 'da' flag is not set or we have clear line capability.
8163 * 8. redraw the characters from ScreenLines[].
8165 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
8166 * the scrollbar for the window. It does have insert line, use that if it
8167 * exists.
8169 result_empty = (row + line_count >= end);
8170 #ifdef FEAT_VERTSPLIT
8171 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
8172 type = USE_REDRAW;
8173 else
8174 #endif
8175 if (can_clear(T_CD) && result_empty)
8176 type = USE_T_CD;
8177 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
8178 type = USE_T_CAL;
8179 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
8180 type = USE_T_CDL;
8181 else if (*T_AL != NUL)
8182 type = USE_T_AL;
8183 else if (can_ce && result_empty)
8184 type = USE_T_CE;
8185 else if (*T_DL != NUL && result_empty)
8186 type = USE_T_DL;
8187 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
8188 type = USE_T_SR;
8189 else
8190 return FAIL;
8193 * For clearing the lines screen_del_lines() is used. This will also take
8194 * care of t_db if necessary.
8196 if (type == USE_T_CD || type == USE_T_CDL ||
8197 type == USE_T_CE || type == USE_T_DL)
8198 return screen_del_lines(off, row, line_count, end, FALSE, wp);
8201 * If text is retained below the screen, first clear or delete as many
8202 * lines at the bottom of the window as are about to be inserted so that
8203 * the deleted lines won't later surface during a screen_del_lines.
8205 if (*T_DB)
8206 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
8208 #ifdef FEAT_CLIPBOARD
8209 /* Remove a modeless selection when inserting lines halfway the screen
8210 * or not the full width of the screen. */
8211 if (off + row > 0
8212 # ifdef FEAT_VERTSPLIT
8213 || (wp != NULL && wp->w_width != Columns)
8214 # endif
8216 clip_clear_selection();
8217 else
8218 clip_scroll_selection(-line_count);
8219 #endif
8221 #ifdef FEAT_GUI
8222 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
8223 * scrolling is actually carried out. */
8224 gui_dont_update_cursor();
8225 #endif
8227 if (*T_CCS != NUL) /* cursor relative to region */
8228 cursor_row = row;
8229 else
8230 cursor_row = row + off;
8233 * Shift LineOffset[] line_count down to reflect the inserted lines.
8234 * Clear the inserted lines in ScreenLines[].
8236 row += off;
8237 end += off;
8238 for (i = 0; i < line_count; ++i)
8240 #ifdef FEAT_VERTSPLIT
8241 if (wp != NULL && wp->w_width != Columns)
8243 /* need to copy part of a line */
8244 j = end - 1 - i;
8245 while ((j -= line_count) >= row)
8246 linecopy(j + line_count, j, wp);
8247 j += line_count;
8248 if (can_clear((char_u *)" "))
8249 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
8250 else
8251 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
8252 LineWraps[j] = FALSE;
8254 else
8255 #endif
8257 j = end - 1 - i;
8258 temp = LineOffset[j];
8259 while ((j -= line_count) >= row)
8261 LineOffset[j + line_count] = LineOffset[j];
8262 LineWraps[j + line_count] = LineWraps[j];
8264 LineOffset[j + line_count] = temp;
8265 LineWraps[j + line_count] = FALSE;
8266 if (can_clear((char_u *)" "))
8267 lineclear(temp, (int)Columns);
8268 else
8269 lineinvalid(temp, (int)Columns);
8273 screen_stop_highlight();
8274 windgoto(cursor_row, 0);
8276 #ifdef FEAT_VERTSPLIT
8277 /* redraw the characters */
8278 if (type == USE_REDRAW)
8279 redraw_block(row, end, wp);
8280 else
8281 #endif
8282 if (type == USE_T_CAL)
8284 term_append_lines(line_count);
8285 screen_start(); /* don't know where cursor is now */
8287 else
8289 for (i = 0; i < line_count; i++)
8291 if (type == USE_T_AL)
8293 if (i && cursor_row != 0)
8294 windgoto(cursor_row, 0);
8295 out_str(T_AL);
8297 else /* type == USE_T_SR */
8298 out_str(T_SR);
8299 screen_start(); /* don't know where cursor is now */
8304 * With scroll-reverse and 'da' flag set we need to clear the lines that
8305 * have been scrolled down into the region.
8307 if (type == USE_T_SR && *T_DA)
8309 for (i = 0; i < line_count; ++i)
8311 windgoto(off + i, 0);
8312 out_str(T_CE);
8313 screen_start(); /* don't know where cursor is now */
8317 #ifdef FEAT_GUI
8318 gui_can_update_cursor();
8319 if (gui.in_use)
8320 out_flush(); /* always flush after a scroll */
8321 #endif
8322 return OK;
8326 * delete lines on the screen and update ScreenLines[]
8327 * 'end' is the line after the scrolled part. Normally it is Rows.
8328 * When scrolling region used 'off' is the offset from the top for the region.
8329 * 'row' and 'end' are relative to the start of the region.
8331 * Return OK for success, FAIL if the lines are not deleted.
8333 /*ARGSUSED*/
8335 screen_del_lines(off, row, line_count, end, force, wp)
8336 int off;
8337 int row;
8338 int line_count;
8339 int end;
8340 int force; /* even when line_count > p_ttyscroll */
8341 win_T *wp; /* NULL or window to use width from */
8343 int j;
8344 int i;
8345 unsigned temp;
8346 int cursor_row;
8347 int cursor_end;
8348 int result_empty; /* result is empty until end of region */
8349 int can_delete; /* deleting line codes can be used */
8350 int type;
8353 * FAIL if
8354 * - there is no valid screen
8355 * - the screen has to be redrawn completely
8356 * - the line count is less than one
8357 * - the line count is more than 'ttyscroll'
8359 if (!screen_valid(TRUE) || line_count <= 0 ||
8360 (!force && line_count > p_ttyscroll))
8361 return FAIL;
8364 * Check if the rest of the current region will become empty.
8366 result_empty = row + line_count >= end;
8369 * We can delete lines only when 'db' flag not set or when 'ce' option
8370 * available.
8372 can_delete = (*T_DB == NUL || can_clear(T_CE));
8375 * There are six ways to delete lines:
8376 * 0. When in a vertically split window and t_CV isn't set, redraw the
8377 * characters from ScreenLines[].
8378 * 1. Use T_CD if it exists and the result is empty.
8379 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
8380 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
8381 * none of the other ways work.
8382 * 4. Use T_CE (erase line) if the result is empty.
8383 * 5. Use T_DL (delete line) if it exists.
8384 * 6. redraw the characters from ScreenLines[].
8386 #ifdef FEAT_VERTSPLIT
8387 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
8388 type = USE_REDRAW;
8389 else
8390 #endif
8391 if (can_clear(T_CD) && result_empty)
8392 type = USE_T_CD;
8393 #if defined(__BEOS__) && defined(BEOS_DR8)
8395 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
8396 * its internal termcap... this works okay for tests which test *T_DB !=
8397 * NUL. It has the disadvantage that the user cannot use any :set t_*
8398 * command to get T_DB (back) to empty_option, only :set term=... will do
8399 * the trick...
8400 * Anyway, this hack will hopefully go away with the next OS release.
8401 * (Olaf Seibert)
8403 else if (row == 0 && T_DB == empty_option
8404 && (line_count == 1 || *T_CDL == NUL))
8405 #else
8406 else if (row == 0 && (
8407 #ifndef AMIGA
8408 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
8409 * up, so use delete-line command */
8410 line_count == 1 ||
8411 #endif
8412 *T_CDL == NUL))
8413 #endif
8414 type = USE_NL;
8415 else if (*T_CDL != NUL && line_count > 1 && can_delete)
8416 type = USE_T_CDL;
8417 else if (can_clear(T_CE) && result_empty
8418 #ifdef FEAT_VERTSPLIT
8419 && (wp == NULL || wp->w_width == Columns)
8420 #endif
8422 type = USE_T_CE;
8423 else if (*T_DL != NUL && can_delete)
8424 type = USE_T_DL;
8425 else if (*T_CDL != NUL && can_delete)
8426 type = USE_T_CDL;
8427 else
8428 return FAIL;
8430 #ifdef FEAT_CLIPBOARD
8431 /* Remove a modeless selection when deleting lines halfway the screen or
8432 * not the full width of the screen. */
8433 if (off + row > 0
8434 # ifdef FEAT_VERTSPLIT
8435 || (wp != NULL && wp->w_width != Columns)
8436 # endif
8438 clip_clear_selection();
8439 else
8440 clip_scroll_selection(line_count);
8441 #endif
8443 #ifdef FEAT_GUI
8444 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
8445 * scrolling is actually carried out. */
8446 gui_dont_update_cursor();
8447 #endif
8449 if (*T_CCS != NUL) /* cursor relative to region */
8451 cursor_row = row;
8452 cursor_end = end;
8454 else
8456 cursor_row = row + off;
8457 cursor_end = end + off;
8461 * Now shift LineOffset[] line_count up to reflect the deleted lines.
8462 * Clear the inserted lines in ScreenLines[].
8464 row += off;
8465 end += off;
8466 for (i = 0; i < line_count; ++i)
8468 #ifdef FEAT_VERTSPLIT
8469 if (wp != NULL && wp->w_width != Columns)
8471 /* need to copy part of a line */
8472 j = row + i;
8473 while ((j += line_count) <= end - 1)
8474 linecopy(j - line_count, j, wp);
8475 j -= line_count;
8476 if (can_clear((char_u *)" "))
8477 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
8478 else
8479 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
8480 LineWraps[j] = FALSE;
8482 else
8483 #endif
8485 /* whole width, moving the line pointers is faster */
8486 j = row + i;
8487 temp = LineOffset[j];
8488 while ((j += line_count) <= end - 1)
8490 LineOffset[j - line_count] = LineOffset[j];
8491 LineWraps[j - line_count] = LineWraps[j];
8493 LineOffset[j - line_count] = temp;
8494 LineWraps[j - line_count] = FALSE;
8495 if (can_clear((char_u *)" "))
8496 lineclear(temp, (int)Columns);
8497 else
8498 lineinvalid(temp, (int)Columns);
8502 screen_stop_highlight();
8504 #ifdef FEAT_VERTSPLIT
8505 /* redraw the characters */
8506 if (type == USE_REDRAW)
8507 redraw_block(row, end, wp);
8508 else
8509 #endif
8510 if (type == USE_T_CD) /* delete the lines */
8512 windgoto(cursor_row, 0);
8513 out_str(T_CD);
8514 screen_start(); /* don't know where cursor is now */
8516 else if (type == USE_T_CDL)
8518 windgoto(cursor_row, 0);
8519 term_delete_lines(line_count);
8520 screen_start(); /* don't know where cursor is now */
8523 * Deleting lines at top of the screen or scroll region: Just scroll
8524 * the whole screen (scroll region) up by outputting newlines on the
8525 * last line.
8527 else if (type == USE_NL)
8529 windgoto(cursor_end - 1, 0);
8530 for (i = line_count; --i >= 0; )
8531 out_char('\n'); /* cursor will remain on same line */
8533 else
8535 for (i = line_count; --i >= 0; )
8537 if (type == USE_T_DL)
8539 windgoto(cursor_row, 0);
8540 out_str(T_DL); /* delete a line */
8542 else /* type == USE_T_CE */
8544 windgoto(cursor_row + i, 0);
8545 out_str(T_CE); /* erase a line */
8547 screen_start(); /* don't know where cursor is now */
8552 * If the 'db' flag is set, we need to clear the lines that have been
8553 * scrolled up at the bottom of the region.
8555 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
8557 for (i = line_count; i > 0; --i)
8559 windgoto(cursor_end - i, 0);
8560 out_str(T_CE); /* erase a line */
8561 screen_start(); /* don't know where cursor is now */
8565 #ifdef FEAT_GUI
8566 gui_can_update_cursor();
8567 if (gui.in_use)
8568 out_flush(); /* always flush after a scroll */
8569 #endif
8571 return OK;
8575 * show the current mode and ruler
8577 * If clear_cmdline is TRUE, clear the rest of the cmdline.
8578 * If clear_cmdline is FALSE there may be a message there that needs to be
8579 * cleared only if a mode is shown.
8580 * Return the length of the message (0 if no message).
8583 showmode()
8585 int need_clear;
8586 int length = 0;
8587 int do_mode;
8588 int attr;
8589 int nwr_save;
8590 #ifdef FEAT_INS_EXPAND
8591 int sub_attr;
8592 #endif
8594 do_mode = ((p_smd && msg_silent == 0)
8595 && ((State & INSERT)
8596 || restart_edit
8597 #ifdef FEAT_VISUAL
8598 || VIsual_active
8599 #endif
8601 if (do_mode || Recording)
8604 * Don't show mode right now, when not redrawing or inside a mapping.
8605 * Call char_avail() only when we are going to show something, because
8606 * it takes a bit of time.
8608 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
8610 redraw_cmdline = TRUE; /* show mode later */
8611 return 0;
8614 nwr_save = need_wait_return;
8616 /* wait a bit before overwriting an important message */
8617 check_for_delay(FALSE);
8619 /* if the cmdline is more than one line high, erase top lines */
8620 need_clear = clear_cmdline;
8621 if (clear_cmdline && cmdline_row < Rows - 1)
8622 msg_clr_cmdline(); /* will reset clear_cmdline */
8624 /* Position on the last line in the window, column 0 */
8625 msg_pos_mode();
8626 cursor_off();
8627 attr = hl_attr(HLF_CM); /* Highlight mode */
8628 if (do_mode)
8630 MSG_PUTS_ATTR("--", attr);
8631 #if defined(FEAT_XIM)
8632 if (xic != NULL && im_get_status() && !p_imdisable
8633 && curbuf->b_p_iminsert == B_IMODE_IM)
8634 # ifdef HAVE_GTK2 /* most of the time, it's not XIM being used */
8635 MSG_PUTS_ATTR(" IM", attr);
8636 # else
8637 MSG_PUTS_ATTR(" XIM", attr);
8638 # endif
8639 #endif
8640 #if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
8641 if (gui.in_use)
8643 if (hangul_input_state_get())
8644 MSG_PUTS_ATTR(" \307\321\261\333", attr); /* HANGUL */
8646 #endif
8647 #ifdef FEAT_INS_EXPAND
8648 if (edit_submode != NULL) /* CTRL-X in Insert mode */
8650 /* These messages can get long, avoid a wrap in a narrow
8651 * window. Prefer showing edit_submode_extra. */
8652 length = (Rows - msg_row) * Columns - 3;
8653 if (edit_submode_extra != NULL)
8654 length -= vim_strsize(edit_submode_extra);
8655 if (length > 0)
8657 if (edit_submode_pre != NULL)
8658 length -= vim_strsize(edit_submode_pre);
8659 if (length - vim_strsize(edit_submode) > 0)
8661 if (edit_submode_pre != NULL)
8662 msg_puts_attr(edit_submode_pre, attr);
8663 msg_puts_attr(edit_submode, attr);
8665 if (edit_submode_extra != NULL)
8667 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
8668 if ((int)edit_submode_highl < (int)HLF_COUNT)
8669 sub_attr = hl_attr(edit_submode_highl);
8670 else
8671 sub_attr = attr;
8672 msg_puts_attr(edit_submode_extra, sub_attr);
8675 length = 0;
8677 else
8678 #endif
8680 #ifdef FEAT_VREPLACE
8681 if (State & VREPLACE_FLAG)
8682 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
8683 else
8684 #endif
8685 if (State & REPLACE_FLAG)
8686 MSG_PUTS_ATTR(_(" REPLACE"), attr);
8687 else if (State & INSERT)
8689 #ifdef FEAT_RIGHTLEFT
8690 if (p_ri)
8691 MSG_PUTS_ATTR(_(" REVERSE"), attr);
8692 #endif
8693 MSG_PUTS_ATTR(_(" INSERT"), attr);
8695 else if (restart_edit == 'I')
8696 MSG_PUTS_ATTR(_(" (insert)"), attr);
8697 else if (restart_edit == 'R')
8698 MSG_PUTS_ATTR(_(" (replace)"), attr);
8699 else if (restart_edit == 'V')
8700 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
8701 #ifdef FEAT_RIGHTLEFT
8702 if (p_hkmap)
8703 MSG_PUTS_ATTR(_(" Hebrew"), attr);
8704 # ifdef FEAT_FKMAP
8705 if (p_fkmap)
8706 MSG_PUTS_ATTR(farsi_text_5, attr);
8707 # endif
8708 #endif
8709 #ifdef FEAT_KEYMAP
8710 if (State & LANGMAP)
8712 # ifdef FEAT_ARABIC
8713 if (curwin->w_p_arab)
8714 MSG_PUTS_ATTR(_(" Arabic"), attr);
8715 else
8716 # endif
8717 MSG_PUTS_ATTR(_(" (lang)"), attr);
8719 #endif
8720 if ((State & INSERT) && p_paste)
8721 MSG_PUTS_ATTR(_(" (paste)"), attr);
8723 #ifdef FEAT_VISUAL
8724 if (VIsual_active)
8726 char *p;
8728 /* Don't concatenate separate words to avoid translation
8729 * problems. */
8730 switch ((VIsual_select ? 4 : 0)
8731 + (VIsual_mode == Ctrl_V) * 2
8732 + (VIsual_mode == 'V'))
8734 case 0: p = N_(" VISUAL"); break;
8735 case 1: p = N_(" VISUAL LINE"); break;
8736 case 2: p = N_(" VISUAL BLOCK"); break;
8737 case 4: p = N_(" SELECT"); break;
8738 case 5: p = N_(" SELECT LINE"); break;
8739 default: p = N_(" SELECT BLOCK"); break;
8741 MSG_PUTS_ATTR(_(p), attr);
8743 #endif
8744 MSG_PUTS_ATTR(" --", attr);
8747 need_clear = TRUE;
8749 if (Recording
8750 #ifdef FEAT_INS_EXPAND
8751 && edit_submode == NULL /* otherwise it gets too long */
8752 #endif
8755 MSG_PUTS_ATTR(_("recording"), attr);
8756 need_clear = TRUE;
8759 mode_displayed = TRUE;
8760 if (need_clear || clear_cmdline)
8761 msg_clr_eos();
8762 msg_didout = FALSE; /* overwrite this message */
8763 length = msg_col;
8764 msg_col = 0;
8765 need_wait_return = nwr_save; /* never ask for hit-return for this */
8767 else if (clear_cmdline && msg_silent == 0)
8768 /* Clear the whole command line. Will reset "clear_cmdline". */
8769 msg_clr_cmdline();
8771 #ifdef FEAT_CMDL_INFO
8772 # ifdef FEAT_VISUAL
8773 /* In Visual mode the size of the selected area must be redrawn. */
8774 if (VIsual_active)
8775 clear_showcmd();
8776 # endif
8778 /* If the last window has no status line, the ruler is after the mode
8779 * message and must be redrawn */
8780 if (redrawing()
8781 # ifdef FEAT_WINDOWS
8782 && lastwin->w_status_height == 0
8783 # endif
8785 win_redr_ruler(lastwin, TRUE);
8786 #endif
8787 redraw_cmdline = FALSE;
8788 clear_cmdline = FALSE;
8790 return length;
8794 * Position for a mode message.
8796 static void
8797 msg_pos_mode()
8799 msg_col = 0;
8800 msg_row = Rows - 1;
8804 * Delete mode message. Used when ESC is typed which is expected to end
8805 * Insert mode (but Insert mode didn't end yet!).
8806 * Caller should check "mode_displayed".
8808 void
8809 unshowmode(force)
8810 int force;
8813 * Don't delete it right now, when not redrawing or insided a mapping.
8815 if (!redrawing() || (!force && char_avail() && !KeyTyped))
8816 redraw_cmdline = TRUE; /* delete mode later */
8817 else
8819 msg_pos_mode();
8820 if (Recording)
8821 MSG_PUTS_ATTR(_("recording"), hl_attr(HLF_CM));
8822 msg_clr_eos();
8826 #if defined(FEAT_WINDOWS)
8828 * Draw the tab pages line at the top of the Vim window.
8830 static void
8831 draw_tabline()
8833 int tabcount = 0;
8834 tabpage_T *tp;
8835 int tabwidth;
8836 int col = 0;
8837 int scol = 0;
8838 int attr;
8839 win_T *wp;
8840 win_T *cwp;
8841 int wincount;
8842 int modified;
8843 int c;
8844 int len;
8845 int attr_sel = hl_attr(HLF_TPS);
8846 int attr_nosel = hl_attr(HLF_TP);
8847 int attr_fill = hl_attr(HLF_TPF);
8848 char_u *p;
8849 int room;
8850 int use_sep_chars = (t_colors < 8
8851 #ifdef FEAT_GUI
8852 && !gui.in_use
8853 #endif
8856 redraw_tabline = FALSE;
8858 #ifdef FEAT_GUI_TABLINE
8859 /* Take care of a GUI tabline. */
8860 if (gui_use_tabline())
8862 gui_update_tabline();
8863 return;
8865 #endif
8867 if (tabline_height() < 1)
8868 return;
8870 #if defined(FEAT_STL_OPT)
8872 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
8873 for (scol = 0; scol < Columns; ++scol)
8874 TabPageIdxs[scol] = 0;
8876 /* Use the 'tabline' option if it's set. */
8877 if (*p_tal != NUL)
8879 int save_called_emsg = called_emsg;
8881 /* Check for an error. If there is one we would loop in redrawing the
8882 * screen. Avoid that by making 'tabline' empty. */
8883 called_emsg = FALSE;
8884 win_redr_custom(NULL, FALSE);
8885 if (called_emsg)
8886 set_string_option_direct((char_u *)"tabline", -1,
8887 (char_u *)"", OPT_FREE, SID_ERROR);
8888 called_emsg |= save_called_emsg;
8890 else
8891 #endif
8893 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
8894 ++tabcount;
8896 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
8897 if (tabwidth < 6)
8898 tabwidth = 6;
8900 attr = attr_nosel;
8901 tabcount = 0;
8902 scol = 0;
8903 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
8904 tp = tp->tp_next)
8906 scol = col;
8908 if (tp->tp_topframe == topframe)
8909 attr = attr_sel;
8910 if (use_sep_chars && col > 0)
8911 screen_putchar('|', 0, col++, attr);
8913 if (tp->tp_topframe != topframe)
8914 attr = attr_nosel;
8916 screen_putchar(' ', 0, col++, attr);
8918 if (tp == curtab)
8920 cwp = curwin;
8921 wp = firstwin;
8923 else
8925 cwp = tp->tp_curwin;
8926 wp = tp->tp_firstwin;
8929 modified = FALSE;
8930 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
8931 if (bufIsChanged(wp->w_buffer))
8932 modified = TRUE;
8933 if (modified || wincount > 1)
8935 if (wincount > 1)
8937 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
8938 len = (int)STRLEN(NameBuff);
8939 if (col + len >= Columns - 3)
8940 break;
8941 screen_puts_len(NameBuff, len, 0, col,
8942 #if defined(FEAT_SYN_HL)
8943 hl_combine_attr(attr, hl_attr(HLF_T))
8944 #else
8945 attr
8946 #endif
8948 col += len;
8950 if (modified)
8951 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
8952 screen_putchar(' ', 0, col++, attr);
8955 room = scol - col + tabwidth - 1;
8956 if (room > 0)
8958 /* Get buffer name in NameBuff[] */
8959 get_trans_bufname(cwp->w_buffer);
8960 shorten_dir(NameBuff);
8961 len = vim_strsize(NameBuff);
8962 p = NameBuff;
8963 #ifdef FEAT_MBYTE
8964 if (has_mbyte)
8965 while (len > room)
8967 len -= ptr2cells(p);
8968 mb_ptr_adv(p);
8970 else
8971 #endif
8972 if (len > room)
8974 p += len - room;
8975 len = room;
8977 if (len > Columns - col - 1)
8978 len = Columns - col - 1;
8980 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
8981 col += len;
8983 screen_putchar(' ', 0, col++, attr);
8985 /* Store the tab page number in TabPageIdxs[], so that
8986 * jump_to_mouse() knows where each one is. */
8987 ++tabcount;
8988 while (scol < col)
8989 TabPageIdxs[scol++] = tabcount;
8992 if (use_sep_chars)
8993 c = '_';
8994 else
8995 c = ' ';
8996 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
8998 /* Put an "X" for closing the current tab if there are several. */
8999 if (first_tabpage->tp_next != NULL)
9001 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
9002 TabPageIdxs[Columns - 1] = -999;
9006 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
9007 * set. */
9008 redraw_tabline = FALSE;
9012 * Get buffer name for "buf" into NameBuff[].
9013 * Takes care of special buffer names and translates special characters.
9015 void
9016 get_trans_bufname(buf)
9017 buf_T *buf;
9019 if (buf_spname(buf) != NULL)
9020 STRCPY(NameBuff, buf_spname(buf));
9021 else
9022 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
9023 trans_characters(NameBuff, MAXPATHL);
9025 #endif
9027 #if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
9029 * Get the character to use in a status line. Get its attributes in "*attr".
9031 static int
9032 fillchar_status(attr, is_curwin)
9033 int *attr;
9034 int is_curwin;
9036 int fill;
9037 if (is_curwin)
9039 *attr = hl_attr(HLF_S);
9040 fill = fill_stl;
9042 else
9044 *attr = hl_attr(HLF_SNC);
9045 fill = fill_stlnc;
9047 /* Use fill when there is highlighting, and highlighting of current
9048 * window differs, or the fillchars differ, or this is not the
9049 * current window */
9050 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
9051 || !is_curwin || firstwin == lastwin)
9052 || (fill_stl != fill_stlnc)))
9053 return fill;
9054 if (is_curwin)
9055 return '^';
9056 return '=';
9058 #endif
9060 #ifdef FEAT_VERTSPLIT
9062 * Get the character to use in a separator between vertically split windows.
9063 * Get its attributes in "*attr".
9065 static int
9066 fillchar_vsep(attr)
9067 int *attr;
9069 *attr = hl_attr(HLF_C);
9070 if (*attr == 0 && fill_vert == ' ')
9071 return '|';
9072 else
9073 return fill_vert;
9075 #endif
9078 * Return TRUE if redrawing should currently be done.
9081 redrawing()
9083 return (!RedrawingDisabled
9084 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
9088 * Return TRUE if printing messages should currently be done.
9091 messaging()
9093 return (!(p_lz && char_avail() && !KeyTyped));
9097 * Show current status info in ruler and various other places
9098 * If always is FALSE, only show ruler if position has changed.
9100 void
9101 showruler(always)
9102 int always;
9104 if (!always && !redrawing())
9105 return;
9106 #ifdef FEAT_INS_EXPAND
9107 if (pum_visible())
9109 # ifdef FEAT_WINDOWS
9110 /* Don't redraw right now, do it later. */
9111 curwin->w_redr_status = TRUE;
9112 # endif
9113 return;
9115 #endif
9116 #if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
9117 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
9119 redraw_custum_statusline(curwin);
9121 else
9122 #endif
9123 #ifdef FEAT_CMDL_INFO
9124 win_redr_ruler(curwin, always);
9125 #endif
9127 #ifdef FEAT_TITLE
9128 if (need_maketitle
9129 # ifdef FEAT_STL_OPT
9130 || (p_icon && (stl_syntax & STL_IN_ICON))
9131 || (p_title && (stl_syntax & STL_IN_TITLE))
9132 # endif
9134 maketitle();
9135 #endif
9138 #ifdef FEAT_CMDL_INFO
9139 static void
9140 win_redr_ruler(wp, always)
9141 win_T *wp;
9142 int always;
9144 char_u buffer[70];
9145 int row;
9146 int fillchar;
9147 int attr;
9148 int empty_line = FALSE;
9149 colnr_T virtcol;
9150 int i;
9151 int o;
9152 #ifdef FEAT_VERTSPLIT
9153 int this_ru_col;
9154 int off = 0;
9155 int width = Columns;
9156 # define WITH_OFF(x) x
9157 # define WITH_WIDTH(x) x
9158 #else
9159 # define WITH_OFF(x) 0
9160 # define WITH_WIDTH(x) Columns
9161 # define this_ru_col ru_col
9162 #endif
9164 /* If 'ruler' off or redrawing disabled, don't do anything */
9165 if (!p_ru)
9166 return;
9169 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
9170 * after deleting lines, before cursor.lnum is corrected.
9172 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
9173 return;
9175 #ifdef FEAT_INS_EXPAND
9176 /* Don't draw the ruler while doing insert-completion, it might overwrite
9177 * the (long) mode message. */
9178 # ifdef FEAT_WINDOWS
9179 if (wp == lastwin && lastwin->w_status_height == 0)
9180 # endif
9181 if (edit_submode != NULL)
9182 return;
9183 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
9184 if (pum_visible())
9185 return;
9186 #endif
9188 #ifdef FEAT_STL_OPT
9189 if (*p_ruf)
9191 int save_called_emsg = called_emsg;
9193 called_emsg = FALSE;
9194 win_redr_custom(wp, TRUE);
9195 if (called_emsg)
9196 set_string_option_direct((char_u *)"rulerformat", -1,
9197 (char_u *)"", OPT_FREE, SID_ERROR);
9198 called_emsg |= save_called_emsg;
9199 return;
9201 #endif
9204 * Check if not in Insert mode and the line is empty (will show "0-1").
9206 if (!(State & INSERT)
9207 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
9208 empty_line = TRUE;
9211 * Only draw the ruler when something changed.
9213 validate_virtcol_win(wp);
9214 if ( redraw_cmdline
9215 || always
9216 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
9217 || wp->w_cursor.col != wp->w_ru_cursor.col
9218 || wp->w_virtcol != wp->w_ru_virtcol
9219 #ifdef FEAT_VIRTUALEDIT
9220 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
9221 #endif
9222 || wp->w_topline != wp->w_ru_topline
9223 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
9224 #ifdef FEAT_DIFF
9225 || wp->w_topfill != wp->w_ru_topfill
9226 #endif
9227 || empty_line != wp->w_ru_empty)
9229 cursor_off();
9230 #ifdef FEAT_WINDOWS
9231 if (wp->w_status_height)
9233 row = W_WINROW(wp) + wp->w_height;
9234 fillchar = fillchar_status(&attr, wp == curwin);
9235 # ifdef FEAT_VERTSPLIT
9236 off = W_WINCOL(wp);
9237 width = W_WIDTH(wp);
9238 # endif
9240 else
9241 #endif
9243 row = Rows - 1;
9244 fillchar = ' ';
9245 attr = 0;
9246 #ifdef FEAT_VERTSPLIT
9247 width = Columns;
9248 off = 0;
9249 #endif
9252 /* In list mode virtcol needs to be recomputed */
9253 virtcol = wp->w_virtcol;
9254 if (wp->w_p_list && lcs_tab1 == NUL)
9256 wp->w_p_list = FALSE;
9257 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
9258 wp->w_p_list = TRUE;
9262 * Some sprintfs return the length, some return a pointer.
9263 * To avoid portability problems we use strlen() here.
9265 sprintf((char *)buffer, "%ld,",
9266 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
9267 ? 0L
9268 : (long)(wp->w_cursor.lnum));
9269 col_print(buffer + STRLEN(buffer),
9270 empty_line ? 0 : (int)wp->w_cursor.col + 1,
9271 (int)virtcol + 1);
9274 * Add a "50%" if there is room for it.
9275 * On the last line, don't print in the last column (scrolls the
9276 * screen up on some terminals).
9278 i = (int)STRLEN(buffer);
9279 get_rel_pos(wp, buffer + i + 1);
9280 o = i + vim_strsize(buffer + i + 1);
9281 #ifdef FEAT_WINDOWS
9282 if (wp->w_status_height == 0) /* can't use last char of screen */
9283 #endif
9284 ++o;
9285 #ifdef FEAT_VERTSPLIT
9286 this_ru_col = ru_col - (Columns - width);
9287 if (this_ru_col < 0)
9288 this_ru_col = 0;
9289 #endif
9290 /* Never use more than half the window/screen width, leave the other
9291 * half for the filename. */
9292 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
9293 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
9294 if (this_ru_col + o < WITH_WIDTH(width))
9296 while (this_ru_col + o < WITH_WIDTH(width))
9298 #ifdef FEAT_MBYTE
9299 if (has_mbyte)
9300 i += (*mb_char2bytes)(fillchar, buffer + i);
9301 else
9302 #endif
9303 buffer[i++] = fillchar;
9304 ++o;
9306 get_rel_pos(wp, buffer + i);
9308 /* Truncate at window boundary. */
9309 #ifdef FEAT_MBYTE
9310 if (has_mbyte)
9312 o = 0;
9313 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
9315 o += (*mb_ptr2cells)(buffer + i);
9316 if (this_ru_col + o > WITH_WIDTH(width))
9318 buffer[i] = NUL;
9319 break;
9323 else
9324 #endif
9325 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
9326 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
9328 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
9329 i = redraw_cmdline;
9330 screen_fill(row, row + 1,
9331 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
9332 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
9333 fillchar, fillchar, attr);
9334 /* don't redraw the cmdline because of showing the ruler */
9335 redraw_cmdline = i;
9336 wp->w_ru_cursor = wp->w_cursor;
9337 wp->w_ru_virtcol = wp->w_virtcol;
9338 wp->w_ru_empty = empty_line;
9339 wp->w_ru_topline = wp->w_topline;
9340 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
9341 #ifdef FEAT_DIFF
9342 wp->w_ru_topfill = wp->w_topfill;
9343 #endif
9346 #endif
9348 #if defined(FEAT_LINEBREAK) || defined(PROTO)
9350 * Return the width of the 'number' column.
9351 * Caller may need to check if 'number' is set.
9352 * Otherwise it depends on 'numberwidth' and the line count.
9355 number_width(wp)
9356 win_T *wp;
9358 int n;
9359 linenr_T lnum;
9361 lnum = wp->w_buffer->b_ml.ml_line_count;
9362 if (lnum == wp->w_nrwidth_line_count)
9363 return wp->w_nrwidth_width;
9364 wp->w_nrwidth_line_count = lnum;
9366 n = 0;
9369 lnum /= 10;
9370 ++n;
9371 } while (lnum > 0);
9373 /* 'numberwidth' gives the minimal width plus one */
9374 if (n < wp->w_p_nuw - 1)
9375 n = wp->w_p_nuw - 1;
9377 wp->w_nrwidth_width = n;
9378 return n;
9380 #endif