Merge branch 'vim' into feat/var-tabstops
[vim_extended.git] / src / screen.c
blobd686e4022e70218f74995019f2c94ed8af169722
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 immediate 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
103 static match_T search_hl; /* used for 'hlsearch' highlight matching */
104 #endif
106 #ifdef FEAT_FOLDING
107 static foldinfo_T win_foldinfo; /* info for 'foldcolumn' */
108 #endif
111 * Buffer for one screen line (characters and attributes).
113 static schar_T *current_ScreenLine;
115 static void win_update __ARGS((win_T *wp));
116 static void win_draw_end __ARGS((win_T *wp, int c1, int c2, int row, int endrow, hlf_T hl));
117 #ifdef FEAT_FOLDING
118 static void fold_line __ARGS((win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row));
119 static void fill_foldcolumn __ARGS((char_u *p, win_T *wp, int closed, linenr_T lnum));
120 static void copy_text_attr __ARGS((int off, char_u *buf, int len, int attr));
121 #endif
122 static int win_line __ARGS((win_T *, linenr_T, int, int, int nochange));
123 static int char_needs_redraw __ARGS((int off_from, int off_to, int cols));
124 #ifdef FEAT_RIGHTLEFT
125 static void screen_line __ARGS((int row, int coloff, int endcol, int clear_width, int rlflag));
126 # define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c), (rl))
127 #else
128 static void screen_line __ARGS((int row, int coloff, int endcol, int clear_width));
129 # define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c))
130 #endif
131 #ifdef FEAT_VERTSPLIT
132 static void draw_vsep_win __ARGS((win_T *wp, int row));
133 #endif
134 #ifdef FEAT_STL_OPT
135 static void redraw_custom_statusline __ARGS((win_T *wp));
136 #endif
137 #ifdef FEAT_SEARCH_EXTRA
138 #define SEARCH_HL_PRIORITY 0
139 static void start_search_hl __ARGS((void));
140 static void end_search_hl __ARGS((void));
141 static void prepare_search_hl __ARGS((win_T *wp, linenr_T lnum));
142 static void next_search_hl __ARGS((win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol));
143 #endif
144 static void screen_start_highlight __ARGS((int attr));
145 static void screen_char __ARGS((unsigned off, int row, int col));
146 #ifdef FEAT_MBYTE
147 static void screen_char_2 __ARGS((unsigned off, int row, int col));
148 #endif
149 static void screenclear2 __ARGS((void));
150 static void lineclear __ARGS((unsigned off, int width));
151 static void lineinvalid __ARGS((unsigned off, int width));
152 #ifdef FEAT_VERTSPLIT
153 static void linecopy __ARGS((int to, int from, win_T *wp));
154 static void redraw_block __ARGS((int row, int end, win_T *wp));
155 #endif
156 static int win_do_lines __ARGS((win_T *wp, int row, int line_count, int mayclear, int del));
157 static void win_rest_invalid __ARGS((win_T *wp));
158 static void msg_pos_mode __ARGS((void));
159 #if defined(FEAT_WINDOWS)
160 static void draw_tabline __ARGS((void));
161 #endif
162 #if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
163 static int fillchar_status __ARGS((int *attr, int is_curwin));
164 #endif
165 #ifdef FEAT_VERTSPLIT
166 static int fillchar_vsep __ARGS((int *attr));
167 #endif
168 #ifdef FEAT_STL_OPT
169 static void win_redr_custom __ARGS((win_T *wp, int draw_ruler));
170 #endif
171 #ifdef FEAT_CMDL_INFO
172 static void win_redr_ruler __ARGS((win_T *wp, int always));
173 #endif
175 #if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
176 /* Ugly global: overrule attribute used by screen_char() */
177 static int screen_char_attr = 0;
178 #endif
181 * Redraw the current window later, with update_screen(type).
182 * Set must_redraw only if not already set to a higher value.
183 * e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
185 void
186 redraw_later(type)
187 int type;
189 redraw_win_later(curwin, type);
192 void
193 redraw_win_later(wp, type)
194 win_T *wp;
195 int type;
197 if (wp->w_redr_type < type)
199 wp->w_redr_type = type;
200 if (type >= NOT_VALID)
201 wp->w_lines_valid = 0;
202 if (must_redraw < type) /* must_redraw is the maximum of all windows */
203 must_redraw = type;
208 * Force a complete redraw later. Also resets the highlighting. To be used
209 * after executing a shell command that messes up the screen.
211 void
212 redraw_later_clear()
214 redraw_all_later(CLEAR);
215 #ifdef FEAT_GUI
216 if (gui.in_use)
217 /* Use a code that will reset gui.highlight_mask in
218 * gui_stop_highlight(). */
219 screen_attr = HL_ALL + 1;
220 else
221 #endif
222 /* Use attributes that is very unlikely to appear in text. */
223 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE;
227 * Mark all windows to be redrawn later.
229 void
230 redraw_all_later(type)
231 int type;
233 win_T *wp;
235 FOR_ALL_WINDOWS(wp)
237 redraw_win_later(wp, type);
242 * Mark all windows that are editing the current buffer to be updated later.
244 void
245 redraw_curbuf_later(type)
246 int type;
248 redraw_buf_later(curbuf, type);
251 void
252 redraw_buf_later(buf, type)
253 buf_T *buf;
254 int type;
256 win_T *wp;
258 FOR_ALL_WINDOWS(wp)
260 if (wp->w_buffer == buf)
261 redraw_win_later(wp, type);
266 * Changed something in the current window, at buffer line "lnum", that
267 * requires that line and possibly other lines to be redrawn.
268 * Used when entering/leaving Insert mode with the cursor on a folded line.
269 * Used to remove the "$" from a change command.
270 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
271 * may become invalid and the whole window will have to be redrawn.
273 void
274 redrawWinline(lnum, invalid)
275 linenr_T lnum;
276 int invalid UNUSED; /* window line height is invalid now */
278 #ifdef FEAT_FOLDING
279 int i;
280 #endif
282 if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
283 curwin->w_redraw_top = lnum;
284 if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
285 curwin->w_redraw_bot = lnum;
286 redraw_later(VALID);
288 #ifdef FEAT_FOLDING
289 if (invalid)
291 /* A w_lines[] entry for this lnum has become invalid. */
292 i = find_wl_entry(curwin, lnum);
293 if (i >= 0)
294 curwin->w_lines[i].wl_valid = FALSE;
296 #endif
300 * update all windows that are editing the current buffer
302 void
303 update_curbuf(type)
304 int type;
306 redraw_curbuf_later(type);
307 update_screen(type);
311 * update_screen()
313 * Based on the current value of curwin->w_topline, transfer a screenfull
314 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
316 void
317 update_screen(type)
318 int type;
320 win_T *wp;
321 static int did_intro = FALSE;
322 #if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
323 int did_one;
324 #endif
326 /* Don't do anything if the screen structures are (not yet) valid. */
327 if (!screen_valid(TRUE))
328 return;
330 if (must_redraw)
332 if (type < must_redraw) /* use maximal type */
333 type = must_redraw;
335 /* must_redraw is reset here, so that when we run into some weird
336 * reason to redraw while busy redrawing (e.g., asynchronous
337 * scrolling), or update_topline() in win_update() will cause a
338 * scroll, the screen will be redrawn later or in win_update(). */
339 must_redraw = 0;
342 /* Need to update w_lines[]. */
343 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
344 type = NOT_VALID;
346 /* Postpone the redrawing when it's not needed and when being called
347 * recursively. */
348 if (!redrawing() || updating_screen)
350 redraw_later(type); /* remember type for next time */
351 must_redraw = type;
352 if (type > INVERTED_ALL)
353 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
354 return;
357 updating_screen = TRUE;
358 #ifdef FEAT_SYN_HL
359 ++display_tick; /* let syntax code know we're in a next round of
360 * display updating */
361 #endif
364 * if the screen was scrolled up when displaying a message, scroll it down
366 if (msg_scrolled)
368 clear_cmdline = TRUE;
369 if (msg_scrolled > Rows - 5) /* clearing is faster */
370 type = CLEAR;
371 else if (type != CLEAR)
373 check_for_delay(FALSE);
374 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, NULL) == FAIL)
375 type = CLEAR;
376 FOR_ALL_WINDOWS(wp)
378 if (W_WINROW(wp) < msg_scrolled)
380 if (W_WINROW(wp) + wp->w_height > msg_scrolled
381 && wp->w_redr_type < REDRAW_TOP
382 && wp->w_lines_valid > 0
383 && wp->w_topline == wp->w_lines[0].wl_lnum)
385 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
386 wp->w_redr_type = REDRAW_TOP;
388 else
390 wp->w_redr_type = NOT_VALID;
391 #ifdef FEAT_WINDOWS
392 if (W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp)
393 <= msg_scrolled)
394 wp->w_redr_status = TRUE;
395 #endif
399 redraw_cmdline = TRUE;
400 #ifdef FEAT_WINDOWS
401 redraw_tabline = TRUE;
402 #endif
404 msg_scrolled = 0;
405 need_wait_return = FALSE;
408 /* reset cmdline_row now (may have been changed temporarily) */
409 compute_cmdrow();
411 /* Check for changed highlighting */
412 if (need_highlight_changed)
413 highlight_changed();
415 if (type == CLEAR) /* first clear screen */
417 screenclear(); /* will reset clear_cmdline */
418 type = NOT_VALID;
421 if (clear_cmdline) /* going to clear cmdline (done below) */
422 check_for_delay(FALSE);
424 #ifdef FEAT_LINEBREAK
425 /* Force redraw when width of 'number' column changes. */
426 if (curwin->w_redr_type < NOT_VALID
427 && curwin->w_nrwidth != (curwin->w_p_nu ? number_width(curwin) : 0))
428 curwin->w_redr_type = NOT_VALID;
429 #endif
432 * Only start redrawing if there is really something to do.
434 if (type == INVERTED)
435 update_curswant();
436 if (curwin->w_redr_type < type
437 && !((type == VALID
438 && curwin->w_lines[0].wl_valid
439 #ifdef FEAT_DIFF
440 && curwin->w_topfill == curwin->w_old_topfill
441 && curwin->w_botfill == curwin->w_old_botfill
442 #endif
443 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
444 #ifdef FEAT_VISUAL
445 || (type == INVERTED
446 && VIsual_active
447 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
448 && curwin->w_old_visual_mode == VIsual_mode
449 && (curwin->w_valid & VALID_VIRTCOL)
450 && curwin->w_old_curswant == curwin->w_curswant)
451 #endif
453 curwin->w_redr_type = type;
455 #ifdef FEAT_WINDOWS
456 /* Redraw the tab pages line if needed. */
457 if (redraw_tabline || type >= NOT_VALID)
458 draw_tabline();
459 #endif
461 #ifdef FEAT_SYN_HL
463 * Correct stored syntax highlighting info for changes in each displayed
464 * buffer. Each buffer must only be done once.
466 FOR_ALL_WINDOWS(wp)
468 if (wp->w_buffer->b_mod_set)
470 # ifdef FEAT_WINDOWS
471 win_T *wwp;
473 /* Check if we already did this buffer. */
474 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
475 if (wwp->w_buffer == wp->w_buffer)
476 break;
477 # endif
478 if (
479 # ifdef FEAT_WINDOWS
480 wwp == wp &&
481 # endif
482 syntax_present(wp->w_buffer))
483 syn_stack_apply_changes(wp->w_buffer);
486 #endif
489 * Go from top to bottom through the windows, redrawing the ones that need
490 * it.
492 #if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
493 did_one = FALSE;
494 #endif
495 #ifdef FEAT_SEARCH_EXTRA
496 search_hl.rm.regprog = NULL;
497 #endif
498 FOR_ALL_WINDOWS(wp)
500 if (wp->w_redr_type != 0)
502 cursor_off();
503 #if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
504 if (!did_one)
506 did_one = TRUE;
507 # ifdef FEAT_SEARCH_EXTRA
508 start_search_hl();
509 # endif
510 # ifdef FEAT_CLIPBOARD
511 /* When Visual area changed, may have to update selection. */
512 if (clip_star.available && clip_isautosel())
513 clip_update_selection();
514 # endif
515 #ifdef FEAT_GUI
516 /* Remove the cursor before starting to do anything, because
517 * scrolling may make it difficult to redraw the text under
518 * it. */
519 if (gui.in_use)
520 gui_undraw_cursor();
521 #endif
523 #endif
524 win_update(wp);
527 #ifdef FEAT_WINDOWS
528 /* redraw status line after the window to minimize cursor movement */
529 if (wp->w_redr_status)
531 cursor_off();
532 win_redr_status(wp);
534 #endif
536 #if defined(FEAT_SEARCH_EXTRA)
537 end_search_hl();
538 #endif
540 #ifdef FEAT_WINDOWS
541 /* Reset b_mod_set flags. Going through all windows is probably faster
542 * than going through all buffers (there could be many buffers). */
543 for (wp = firstwin; wp != NULL; wp = wp->w_next)
544 wp->w_buffer->b_mod_set = FALSE;
545 #else
546 curbuf->b_mod_set = FALSE;
547 #endif
549 updating_screen = FALSE;
550 #ifdef FEAT_GUI
551 gui_may_resize_shell();
552 #endif
554 /* Clear or redraw the command line. Done last, because scrolling may
555 * mess up the command line. */
556 if (clear_cmdline || redraw_cmdline)
557 showmode();
559 /* May put up an introductory message when not editing a file */
560 if (!did_intro && bufempty()
561 && curbuf->b_fname == NULL
562 #ifdef FEAT_WINDOWS
563 && firstwin->w_next == NULL
564 #endif
565 && vim_strchr(p_shm, SHM_INTRO) == NULL)
566 intro_message(FALSE);
567 did_intro = TRUE;
569 #ifdef FEAT_GUI
570 /* Redraw the cursor and update the scrollbars when all screen updating is
571 * done. */
572 if (gui.in_use)
574 out_flush(); /* required before updating the cursor */
575 if (did_one)
576 gui_update_cursor(FALSE, FALSE);
577 gui_update_scrollbars(FALSE);
579 #endif
582 #if defined(FEAT_SIGNS) || defined(FEAT_GUI)
583 static void update_prepare __ARGS((void));
584 static void update_finish __ARGS((void));
587 * Prepare for updating one or more windows.
588 * Caller must check for "updating_screen" already set to avoid recursiveness.
590 static void
591 update_prepare()
593 cursor_off();
594 updating_screen = TRUE;
595 #ifdef FEAT_GUI
596 /* Remove the cursor before starting to do anything, because scrolling may
597 * make it difficult to redraw the text under it. */
598 if (gui.in_use)
599 gui_undraw_cursor();
600 #endif
601 #ifdef FEAT_SEARCH_EXTRA
602 start_search_hl();
603 #endif
607 * Finish updating one or more windows.
609 static void
610 update_finish()
612 if (redraw_cmdline)
613 showmode();
615 # ifdef FEAT_SEARCH_EXTRA
616 end_search_hl();
617 # endif
619 updating_screen = FALSE;
621 # ifdef FEAT_GUI
622 gui_may_resize_shell();
624 /* Redraw the cursor and update the scrollbars when all screen updating is
625 * done. */
626 if (gui.in_use)
628 out_flush(); /* required before updating the cursor */
629 gui_update_cursor(FALSE, FALSE);
630 gui_update_scrollbars(FALSE);
632 # endif
634 #endif
636 #if defined(FEAT_SIGNS) || defined(PROTO)
637 void
638 update_debug_sign(buf, lnum)
639 buf_T *buf;
640 linenr_T lnum;
642 win_T *wp;
643 int doit = FALSE;
645 # ifdef FEAT_FOLDING
646 win_foldinfo.fi_level = 0;
647 # endif
649 /* update/delete a specific mark */
650 FOR_ALL_WINDOWS(wp)
652 if (buf != NULL && lnum > 0)
654 if (wp->w_buffer == buf && lnum >= wp->w_topline
655 && lnum < wp->w_botline)
657 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
658 wp->w_redraw_top = lnum;
659 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
660 wp->w_redraw_bot = lnum;
661 redraw_win_later(wp, VALID);
664 else
665 redraw_win_later(wp, VALID);
666 if (wp->w_redr_type != 0)
667 doit = TRUE;
670 /* Return when there is nothing to do or screen updating already
671 * happening. */
672 if (!doit || updating_screen)
673 return;
675 /* update all windows that need updating */
676 update_prepare();
678 # ifdef FEAT_WINDOWS
679 for (wp = firstwin; wp; wp = wp->w_next)
681 if (wp->w_redr_type != 0)
682 win_update(wp);
683 if (wp->w_redr_status)
684 win_redr_status(wp);
686 # else
687 if (curwin->w_redr_type != 0)
688 win_update(curwin);
689 # endif
691 update_finish();
693 #endif
696 #if defined(FEAT_GUI) || defined(PROTO)
698 * Update a single window, its status line and maybe the command line msg.
699 * Used for the GUI scrollbar.
701 void
702 updateWindow(wp)
703 win_T *wp;
705 /* return if already busy updating */
706 if (updating_screen)
707 return;
709 update_prepare();
711 #ifdef FEAT_CLIPBOARD
712 /* When Visual area changed, may have to update selection. */
713 if (clip_star.available && clip_isautosel())
714 clip_update_selection();
715 #endif
717 win_update(wp);
719 #ifdef FEAT_WINDOWS
720 /* When the screen was cleared redraw the tab pages line. */
721 if (redraw_tabline)
722 draw_tabline();
724 if (wp->w_redr_status
725 # ifdef FEAT_CMDL_INFO
726 || p_ru
727 # endif
728 # ifdef FEAT_STL_OPT
729 || *p_stl != NUL || *wp->w_p_stl != NUL
730 # endif
732 win_redr_status(wp);
733 #endif
735 update_finish();
737 #endif
740 * Update a single window.
742 * This may cause the windows below it also to be redrawn (when clearing the
743 * screen or scrolling lines).
745 * How the window is redrawn depends on wp->w_redr_type. Each type also
746 * implies the one below it.
747 * NOT_VALID redraw the whole window
748 * SOME_VALID redraw the whole window but do scroll when possible
749 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
750 * INVERTED redraw the changed part of the Visual area
751 * INVERTED_ALL redraw the whole Visual area
752 * VALID 1. scroll up/down to adjust for a changed w_topline
753 * 2. update lines at the top when scrolled down
754 * 3. redraw changed text:
755 * - if wp->w_buffer->b_mod_set set, update lines between
756 * b_mod_top and b_mod_bot.
757 * - if wp->w_redraw_top non-zero, redraw lines between
758 * wp->w_redraw_top and wp->w_redr_bot.
759 * - continue redrawing when syntax status is invalid.
760 * 4. if scrolled up, update lines at the bottom.
761 * This results in three areas that may need updating:
762 * top: from first row to top_end (when scrolled down)
763 * mid: from mid_start to mid_end (update inversion or changed text)
764 * bot: from bot_start to last row (when scrolled up)
766 static void
767 win_update(wp)
768 win_T *wp;
770 buf_T *buf = wp->w_buffer;
771 int type;
772 int top_end = 0; /* Below last row of the top area that needs
773 updating. 0 when no top area updating. */
774 int mid_start = 999;/* first row of the mid area that needs
775 updating. 999 when no mid area updating. */
776 int mid_end = 0; /* Below last row of the mid area that needs
777 updating. 0 when no mid area updating. */
778 int bot_start = 999;/* first row of the bot area that needs
779 updating. 999 when no bot area updating */
780 #ifdef FEAT_VISUAL
781 int scrolled_down = FALSE; /* TRUE when scrolled down when
782 w_topline got smaller a bit */
783 #endif
784 #ifdef FEAT_SEARCH_EXTRA
785 matchitem_T *cur; /* points to the match list */
786 int top_to_mod = FALSE; /* redraw above mod_top */
787 #endif
789 int row; /* current window row to display */
790 linenr_T lnum; /* current buffer lnum to display */
791 int idx; /* current index in w_lines[] */
792 int srow; /* starting row of the current line */
794 int eof = FALSE; /* if TRUE, we hit the end of the file */
795 int didline = FALSE; /* if TRUE, we finished the last line */
796 int i;
797 long j;
798 static int recursive = FALSE; /* being called recursively */
799 int old_botline = wp->w_botline;
800 #ifdef FEAT_FOLDING
801 long fold_count;
802 #endif
803 #ifdef FEAT_SYN_HL
804 /* remember what happened to the previous line, to know if
805 * check_visual_highlight() can be used */
806 #define DID_NONE 1 /* didn't update a line */
807 #define DID_LINE 2 /* updated a normal line */
808 #define DID_FOLD 3 /* updated a folded line */
809 int did_update = DID_NONE;
810 linenr_T syntax_last_parsed = 0; /* last parsed text line */
811 #endif
812 linenr_T mod_top = 0;
813 linenr_T mod_bot = 0;
814 #if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
815 int save_got_int;
816 #endif
818 type = wp->w_redr_type;
820 if (type == NOT_VALID)
822 #ifdef FEAT_WINDOWS
823 wp->w_redr_status = TRUE;
824 #endif
825 wp->w_lines_valid = 0;
828 /* Window is zero-height: nothing to draw. */
829 if (wp->w_height == 0)
831 wp->w_redr_type = 0;
832 return;
835 #ifdef FEAT_VERTSPLIT
836 /* Window is zero-width: Only need to draw the separator. */
837 if (wp->w_width == 0)
839 /* draw the vertical separator right of this window */
840 draw_vsep_win(wp, 0);
841 wp->w_redr_type = 0;
842 return;
844 #endif
846 #ifdef FEAT_SEARCH_EXTRA
847 /* Setup for match and 'hlsearch' highlighting. Disable any previous
848 * match */
849 cur = wp->w_match_head;
850 while (cur != NULL)
852 cur->hl.rm = cur->match;
853 if (cur->hlg_id == 0)
854 cur->hl.attr = 0;
855 else
856 cur->hl.attr = syn_id2attr(cur->hlg_id);
857 cur->hl.buf = buf;
858 cur->hl.lnum = 0;
859 cur->hl.first_lnum = 0;
860 # ifdef FEAT_RELTIME
861 /* Set the time limit to 'redrawtime'. */
862 profile_setlimit(p_rdt, &(cur->hl.tm));
863 # endif
864 cur = cur->next;
866 search_hl.buf = buf;
867 search_hl.lnum = 0;
868 search_hl.first_lnum = 0;
869 /* time limit is set at the toplevel, for all windows */
870 #endif
872 #ifdef FEAT_LINEBREAK
873 /* Force redraw when width of 'number' column changes. */
874 i = wp->w_p_nu ? number_width(wp) : 0;
875 if (wp->w_nrwidth != i)
877 type = NOT_VALID;
878 wp->w_nrwidth = i;
880 else
881 #endif
883 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
886 * When there are both inserted/deleted lines and specific lines to be
887 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
888 * everything (only happens when redrawing is off for while).
890 type = NOT_VALID;
892 else
895 * Set mod_top to the first line that needs displaying because of
896 * changes. Set mod_bot to the first line after the changes.
898 mod_top = wp->w_redraw_top;
899 if (wp->w_redraw_bot != 0)
900 mod_bot = wp->w_redraw_bot + 1;
901 else
902 mod_bot = 0;
903 wp->w_redraw_top = 0; /* reset for next time */
904 wp->w_redraw_bot = 0;
905 if (buf->b_mod_set)
907 if (mod_top == 0 || mod_top > buf->b_mod_top)
909 mod_top = buf->b_mod_top;
910 #ifdef FEAT_SYN_HL
911 /* Need to redraw lines above the change that may be included
912 * in a pattern match. */
913 if (syntax_present(buf))
915 mod_top -= buf->b_syn_sync_linebreaks;
916 if (mod_top < 1)
917 mod_top = 1;
919 #endif
921 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
922 mod_bot = buf->b_mod_bot;
924 #ifdef FEAT_SEARCH_EXTRA
925 /* When 'hlsearch' is on and using a multi-line search pattern, a
926 * change in one line may make the Search highlighting in a
927 * previous line invalid. Simple solution: redraw all visible
928 * lines above the change.
929 * Same for a match pattern.
931 if (search_hl.rm.regprog != NULL
932 && re_multiline(search_hl.rm.regprog))
933 top_to_mod = TRUE;
934 else
936 cur = wp->w_match_head;
937 while (cur != NULL)
939 if (cur->match.regprog != NULL
940 && re_multiline(cur->match.regprog))
942 top_to_mod = TRUE;
943 break;
945 cur = cur->next;
948 #endif
950 #ifdef FEAT_FOLDING
951 if (mod_top != 0 && hasAnyFolding(wp))
953 linenr_T lnumt, lnumb;
956 * A change in a line can cause lines above it to become folded or
957 * unfolded. Find the top most buffer line that may be affected.
958 * If the line was previously folded and displayed, get the first
959 * line of that fold. If the line is folded now, get the first
960 * folded line. Use the minimum of these two.
963 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
964 * the line below it. If there is no valid entry, use w_topline.
965 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
966 * to this line. If there is no valid entry, use MAXLNUM. */
967 lnumt = wp->w_topline;
968 lnumb = MAXLNUM;
969 for (i = 0; i < wp->w_lines_valid; ++i)
970 if (wp->w_lines[i].wl_valid)
972 if (wp->w_lines[i].wl_lastlnum < mod_top)
973 lnumt = wp->w_lines[i].wl_lastlnum + 1;
974 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
976 lnumb = wp->w_lines[i].wl_lnum;
977 /* When there is a fold column it might need updating
978 * in the next line ("J" just above an open fold). */
979 if (wp->w_p_fdc > 0)
980 ++lnumb;
984 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
985 if (mod_top > lnumt)
986 mod_top = lnumt;
988 /* Now do the same for the bottom line (one above mod_bot). */
989 --mod_bot;
990 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
991 ++mod_bot;
992 if (mod_bot < lnumb)
993 mod_bot = lnumb;
995 #endif
997 /* When a change starts above w_topline and the end is below
998 * w_topline, start redrawing at w_topline.
999 * If the end of the change is above w_topline: do like no change was
1000 * made, but redraw the first line to find changes in syntax. */
1001 if (mod_top != 0 && mod_top < wp->w_topline)
1003 if (mod_bot > wp->w_topline)
1004 mod_top = wp->w_topline;
1005 #ifdef FEAT_SYN_HL
1006 else if (syntax_present(buf))
1007 top_end = 1;
1008 #endif
1011 /* When line numbers are displayed need to redraw all lines below
1012 * inserted/deleted lines. */
1013 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1014 mod_bot = MAXLNUM;
1018 * When only displaying the lines at the top, set top_end. Used when
1019 * window has scrolled down for msg_scrolled.
1021 if (type == REDRAW_TOP)
1023 j = 0;
1024 for (i = 0; i < wp->w_lines_valid; ++i)
1026 j += wp->w_lines[i].wl_size;
1027 if (j >= wp->w_upd_rows)
1029 top_end = j;
1030 break;
1033 if (top_end == 0)
1034 /* not found (cannot happen?): redraw everything */
1035 type = NOT_VALID;
1036 else
1037 /* top area defined, the rest is VALID */
1038 type = VALID;
1041 /* Trick: we want to avoid clearing the screen twice. screenclear() will
1042 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1043 * non-zero and thus not FALSE) will indicate that screenclear() was not
1044 * called. */
1045 if (screen_cleared)
1046 screen_cleared = MAYBE;
1049 * If there are no changes on the screen that require a complete redraw,
1050 * handle three cases:
1051 * 1: we are off the top of the screen by a few lines: scroll down
1052 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1053 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1054 * w_lines[] that needs updating.
1056 if ((type == VALID || type == SOME_VALID
1057 || type == INVERTED || type == INVERTED_ALL)
1058 #ifdef FEAT_DIFF
1059 && !wp->w_botfill && !wp->w_old_botfill
1060 #endif
1063 if (mod_top != 0 && wp->w_topline == mod_top)
1066 * w_topline is the first changed line, the scrolling will be done
1067 * further down.
1070 else if (wp->w_lines[0].wl_valid
1071 && (wp->w_topline < wp->w_lines[0].wl_lnum
1072 #ifdef FEAT_DIFF
1073 || (wp->w_topline == wp->w_lines[0].wl_lnum
1074 && wp->w_topfill > wp->w_old_topfill)
1075 #endif
1079 * New topline is above old topline: May scroll down.
1081 #ifdef FEAT_FOLDING
1082 if (hasAnyFolding(wp))
1084 linenr_T ln;
1086 /* count the number of lines we are off, counting a sequence
1087 * of folded lines as one */
1088 j = 0;
1089 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1091 ++j;
1092 if (j >= wp->w_height - 2)
1093 break;
1094 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1097 else
1098 #endif
1099 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1100 if (j < wp->w_height - 2) /* not too far off */
1102 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1103 #ifdef FEAT_DIFF
1104 /* insert extra lines for previously invisible filler lines */
1105 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1106 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1107 - wp->w_old_topfill;
1108 #endif
1109 if (i < wp->w_height - 2) /* less than a screen off */
1112 * Try to insert the correct number of lines.
1113 * If not the last window, delete the lines at the bottom.
1114 * win_ins_lines may fail when the terminal can't do it.
1116 if (i > 0)
1117 check_for_delay(FALSE);
1118 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1120 if (wp->w_lines_valid != 0)
1122 /* Need to update rows that are new, stop at the
1123 * first one that scrolled down. */
1124 top_end = i;
1125 #ifdef FEAT_VISUAL
1126 scrolled_down = TRUE;
1127 #endif
1129 /* Move the entries that were scrolled, disable
1130 * the entries for the lines to be redrawn. */
1131 if ((wp->w_lines_valid += j) > wp->w_height)
1132 wp->w_lines_valid = wp->w_height;
1133 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1134 wp->w_lines[idx] = wp->w_lines[idx - j];
1135 while (idx >= 0)
1136 wp->w_lines[idx--].wl_valid = FALSE;
1139 else
1140 mid_start = 0; /* redraw all lines */
1142 else
1143 mid_start = 0; /* redraw all lines */
1145 else
1146 mid_start = 0; /* redraw all lines */
1148 else
1151 * New topline is at or below old topline: May scroll up.
1152 * When topline didn't change, find first entry in w_lines[] that
1153 * needs updating.
1156 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1157 j = -1;
1158 row = 0;
1159 for (i = 0; i < wp->w_lines_valid; i++)
1161 if (wp->w_lines[i].wl_valid
1162 && wp->w_lines[i].wl_lnum == wp->w_topline)
1164 j = i;
1165 break;
1167 row += wp->w_lines[i].wl_size;
1169 if (j == -1)
1171 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1172 * lines */
1173 mid_start = 0;
1175 else
1178 * Try to delete the correct number of lines.
1179 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1181 #ifdef FEAT_DIFF
1182 /* If the topline didn't change, delete old filler lines,
1183 * otherwise delete filler lines of the new topline... */
1184 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1185 row += wp->w_old_topfill;
1186 else
1187 row += diff_check_fill(wp, wp->w_topline);
1188 /* ... but don't delete new filler lines. */
1189 row -= wp->w_topfill;
1190 #endif
1191 if (row > 0)
1193 check_for_delay(FALSE);
1194 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1195 bot_start = wp->w_height - row;
1196 else
1197 mid_start = 0; /* redraw all lines */
1199 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1202 * Skip the lines (below the deleted lines) that are still
1203 * valid and don't need redrawing. Copy their info
1204 * upwards, to compensate for the deleted lines. Set
1205 * bot_start to the first row that needs redrawing.
1207 bot_start = 0;
1208 idx = 0;
1209 for (;;)
1211 wp->w_lines[idx] = wp->w_lines[j];
1212 /* stop at line that didn't fit, unless it is still
1213 * valid (no lines deleted) */
1214 if (row > 0 && bot_start + row
1215 + (int)wp->w_lines[j].wl_size > wp->w_height)
1217 wp->w_lines_valid = idx + 1;
1218 break;
1220 bot_start += wp->w_lines[idx++].wl_size;
1222 /* stop at the last valid entry in w_lines[].wl_size */
1223 if (++j >= wp->w_lines_valid)
1225 wp->w_lines_valid = idx;
1226 break;
1229 #ifdef FEAT_DIFF
1230 /* Correct the first entry for filler lines at the top
1231 * when it won't get updated below. */
1232 if (wp->w_p_diff && bot_start > 0)
1233 wp->w_lines[0].wl_size =
1234 plines_win_nofill(wp, wp->w_topline, TRUE)
1235 + wp->w_topfill;
1236 #endif
1241 /* When starting redraw in the first line, redraw all lines. When
1242 * there is only one window it's probably faster to clear the screen
1243 * first. */
1244 if (mid_start == 0)
1246 mid_end = wp->w_height;
1247 if (lastwin == firstwin)
1249 /* Clear the screen when it was not done by win_del_lines() or
1250 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1251 * then. */
1252 if (screen_cleared != TRUE)
1253 screenclear();
1254 #ifdef FEAT_WINDOWS
1255 /* The screen was cleared, redraw the tab pages line. */
1256 if (redraw_tabline)
1257 draw_tabline();
1258 #endif
1262 /* When win_del_lines() or win_ins_lines() caused the screen to be
1263 * cleared (only happens for the first window) or when screenclear()
1264 * was called directly above, "must_redraw" will have been set to
1265 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1266 if (screen_cleared == TRUE)
1267 must_redraw = 0;
1269 else
1271 /* Not VALID or INVERTED: redraw all lines. */
1272 mid_start = 0;
1273 mid_end = wp->w_height;
1276 if (type == SOME_VALID)
1278 /* SOME_VALID: redraw all lines. */
1279 mid_start = 0;
1280 mid_end = wp->w_height;
1281 type = NOT_VALID;
1284 #ifdef FEAT_VISUAL
1285 /* check if we are updating or removing the inverted part */
1286 if ((VIsual_active && buf == curwin->w_buffer)
1287 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1289 linenr_T from, to;
1291 if (VIsual_active)
1293 if (VIsual_active
1294 && (VIsual_mode != wp->w_old_visual_mode
1295 || type == INVERTED_ALL))
1298 * If the type of Visual selection changed, redraw the whole
1299 * selection. Also when the ownership of the X selection is
1300 * gained or lost.
1302 if (curwin->w_cursor.lnum < VIsual.lnum)
1304 from = curwin->w_cursor.lnum;
1305 to = VIsual.lnum;
1307 else
1309 from = VIsual.lnum;
1310 to = curwin->w_cursor.lnum;
1312 /* redraw more when the cursor moved as well */
1313 if (wp->w_old_cursor_lnum < from)
1314 from = wp->w_old_cursor_lnum;
1315 if (wp->w_old_cursor_lnum > to)
1316 to = wp->w_old_cursor_lnum;
1317 if (wp->w_old_visual_lnum < from)
1318 from = wp->w_old_visual_lnum;
1319 if (wp->w_old_visual_lnum > to)
1320 to = wp->w_old_visual_lnum;
1322 else
1325 * Find the line numbers that need to be updated: The lines
1326 * between the old cursor position and the current cursor
1327 * position. Also check if the Visual position changed.
1329 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1331 from = curwin->w_cursor.lnum;
1332 to = wp->w_old_cursor_lnum;
1334 else
1336 from = wp->w_old_cursor_lnum;
1337 to = curwin->w_cursor.lnum;
1338 if (from == 0) /* Visual mode just started */
1339 from = to;
1342 if (VIsual.lnum != wp->w_old_visual_lnum
1343 || VIsual.col != wp->w_old_visual_col)
1345 if (wp->w_old_visual_lnum < from
1346 && wp->w_old_visual_lnum != 0)
1347 from = wp->w_old_visual_lnum;
1348 if (wp->w_old_visual_lnum > to)
1349 to = wp->w_old_visual_lnum;
1350 if (VIsual.lnum < from)
1351 from = VIsual.lnum;
1352 if (VIsual.lnum > to)
1353 to = VIsual.lnum;
1358 * If in block mode and changed column or curwin->w_curswant:
1359 * update all lines.
1360 * First compute the actual start and end column.
1362 if (VIsual_mode == Ctrl_V)
1364 colnr_T fromc, toc;
1366 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
1367 ++toc;
1368 if (curwin->w_curswant == MAXCOL)
1369 toc = MAXCOL;
1371 if (fromc != wp->w_old_cursor_fcol
1372 || toc != wp->w_old_cursor_lcol)
1374 if (from > VIsual.lnum)
1375 from = VIsual.lnum;
1376 if (to < VIsual.lnum)
1377 to = VIsual.lnum;
1379 wp->w_old_cursor_fcol = fromc;
1380 wp->w_old_cursor_lcol = toc;
1383 else
1385 /* Use the line numbers of the old Visual area. */
1386 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1388 from = wp->w_old_cursor_lnum;
1389 to = wp->w_old_visual_lnum;
1391 else
1393 from = wp->w_old_visual_lnum;
1394 to = wp->w_old_cursor_lnum;
1399 * There is no need to update lines above the top of the window.
1401 if (from < wp->w_topline)
1402 from = wp->w_topline;
1405 * If we know the value of w_botline, use it to restrict the update to
1406 * the lines that are visible in the window.
1408 if (wp->w_valid & VALID_BOTLINE)
1410 if (from >= wp->w_botline)
1411 from = wp->w_botline - 1;
1412 if (to >= wp->w_botline)
1413 to = wp->w_botline - 1;
1417 * Find the minimal part to be updated.
1418 * Watch out for scrolling that made entries in w_lines[] invalid.
1419 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1420 * top_end; need to redraw from top_end to the "to" line.
1421 * A middle mouse click with a Visual selection may change the text
1422 * above the Visual area and reset wl_valid, do count these for
1423 * mid_end (in srow).
1425 if (mid_start > 0)
1427 lnum = wp->w_topline;
1428 idx = 0;
1429 srow = 0;
1430 if (scrolled_down)
1431 mid_start = top_end;
1432 else
1433 mid_start = 0;
1434 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1436 if (wp->w_lines[idx].wl_valid)
1437 mid_start += wp->w_lines[idx].wl_size;
1438 else if (!scrolled_down)
1439 srow += wp->w_lines[idx].wl_size;
1440 ++idx;
1441 # ifdef FEAT_FOLDING
1442 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1443 lnum = wp->w_lines[idx].wl_lnum;
1444 else
1445 # endif
1446 ++lnum;
1448 srow += mid_start;
1449 mid_end = wp->w_height;
1450 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1452 if (wp->w_lines[idx].wl_valid
1453 && wp->w_lines[idx].wl_lnum >= to + 1)
1455 /* Only update until first row of this line */
1456 mid_end = srow;
1457 break;
1459 srow += wp->w_lines[idx].wl_size;
1464 if (VIsual_active && buf == curwin->w_buffer)
1466 wp->w_old_visual_mode = VIsual_mode;
1467 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1468 wp->w_old_visual_lnum = VIsual.lnum;
1469 wp->w_old_visual_col = VIsual.col;
1470 wp->w_old_curswant = curwin->w_curswant;
1472 else
1474 wp->w_old_visual_mode = 0;
1475 wp->w_old_cursor_lnum = 0;
1476 wp->w_old_visual_lnum = 0;
1477 wp->w_old_visual_col = 0;
1479 #endif /* FEAT_VISUAL */
1481 #if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1482 /* reset got_int, otherwise regexp won't work */
1483 save_got_int = got_int;
1484 got_int = 0;
1485 #endif
1486 #ifdef FEAT_FOLDING
1487 win_foldinfo.fi_level = 0;
1488 #endif
1491 * Update all the window rows.
1493 idx = 0; /* first entry in w_lines[].wl_size */
1494 row = 0;
1495 srow = 0;
1496 lnum = wp->w_topline; /* first line shown in window */
1497 for (;;)
1499 /* stop updating when reached the end of the window (check for _past_
1500 * the end of the window is at the end of the loop) */
1501 if (row == wp->w_height)
1503 didline = TRUE;
1504 break;
1507 /* stop updating when hit the end of the file */
1508 if (lnum > buf->b_ml.ml_line_count)
1510 eof = TRUE;
1511 break;
1514 /* Remember the starting row of the line that is going to be dealt
1515 * with. It is used further down when the line doesn't fit. */
1516 srow = row;
1519 * Update a line when it is in an area that needs updating, when it
1520 * has changes or w_lines[idx] is invalid.
1521 * bot_start may be halfway a wrapped line after using
1522 * win_del_lines(), check if the current line includes it.
1523 * When syntax folding is being used, the saved syntax states will
1524 * already have been updated, we can't see where the syntax state is
1525 * the same again, just update until the end of the window.
1527 if (row < top_end
1528 || (row >= mid_start && row < mid_end)
1529 #ifdef FEAT_SEARCH_EXTRA
1530 || top_to_mod
1531 #endif
1532 || idx >= wp->w_lines_valid
1533 || (row + wp->w_lines[idx].wl_size > bot_start)
1534 || (mod_top != 0
1535 && (lnum == mod_top
1536 || (lnum >= mod_top
1537 && (lnum < mod_bot
1538 #ifdef FEAT_SYN_HL
1539 || did_update == DID_FOLD
1540 || (did_update == DID_LINE
1541 && syntax_present(buf)
1542 && (
1543 # ifdef FEAT_FOLDING
1544 (foldmethodIsSyntax(wp)
1545 && hasAnyFolding(wp)) ||
1546 # endif
1547 syntax_check_changed(lnum)))
1548 #endif
1549 )))))
1551 #ifdef FEAT_SEARCH_EXTRA
1552 if (lnum == mod_top)
1553 top_to_mod = FALSE;
1554 #endif
1557 * When at start of changed lines: May scroll following lines
1558 * up or down to minimize redrawing.
1559 * Don't do this when the change continues until the end.
1560 * Don't scroll when dollar_vcol is non-zero, keep the "$".
1562 if (lnum == mod_top
1563 && mod_bot != MAXLNUM
1564 && !(dollar_vcol != 0 && mod_bot == mod_top + 1))
1566 int old_rows = 0;
1567 int new_rows = 0;
1568 int xtra_rows;
1569 linenr_T l;
1571 /* Count the old number of window rows, using w_lines[], which
1572 * should still contain the sizes for the lines as they are
1573 * currently displayed. */
1574 for (i = idx; i < wp->w_lines_valid; ++i)
1576 /* Only valid lines have a meaningful wl_lnum. Invalid
1577 * lines are part of the changed area. */
1578 if (wp->w_lines[i].wl_valid
1579 && wp->w_lines[i].wl_lnum == mod_bot)
1580 break;
1581 old_rows += wp->w_lines[i].wl_size;
1582 #ifdef FEAT_FOLDING
1583 if (wp->w_lines[i].wl_valid
1584 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1586 /* Must have found the last valid entry above mod_bot.
1587 * Add following invalid entries. */
1588 ++i;
1589 while (i < wp->w_lines_valid
1590 && !wp->w_lines[i].wl_valid)
1591 old_rows += wp->w_lines[i++].wl_size;
1592 break;
1594 #endif
1597 if (i >= wp->w_lines_valid)
1599 /* We can't find a valid line below the changed lines,
1600 * need to redraw until the end of the window.
1601 * Inserting/deleting lines has no use. */
1602 bot_start = 0;
1604 else
1606 /* Able to count old number of rows: Count new window
1607 * rows, and may insert/delete lines */
1608 j = idx;
1609 for (l = lnum; l < mod_bot; ++l)
1611 #ifdef FEAT_FOLDING
1612 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1613 ++new_rows;
1614 else
1615 #endif
1616 #ifdef FEAT_DIFF
1617 if (l == wp->w_topline)
1618 new_rows += plines_win_nofill(wp, l, TRUE)
1619 + wp->w_topfill;
1620 else
1621 #endif
1622 new_rows += plines_win(wp, l, TRUE);
1623 ++j;
1624 if (new_rows > wp->w_height - row - 2)
1626 /* it's getting too much, must redraw the rest */
1627 new_rows = 9999;
1628 break;
1631 xtra_rows = new_rows - old_rows;
1632 if (xtra_rows < 0)
1634 /* May scroll text up. If there is not enough
1635 * remaining text or scrolling fails, must redraw the
1636 * rest. If scrolling works, must redraw the text
1637 * below the scrolled text. */
1638 if (row - xtra_rows >= wp->w_height - 2)
1639 mod_bot = MAXLNUM;
1640 else
1642 check_for_delay(FALSE);
1643 if (win_del_lines(wp, row,
1644 -xtra_rows, FALSE, FALSE) == FAIL)
1645 mod_bot = MAXLNUM;
1646 else
1647 bot_start = wp->w_height + xtra_rows;
1650 else if (xtra_rows > 0)
1652 /* May scroll text down. If there is not enough
1653 * remaining text of scrolling fails, must redraw the
1654 * rest. */
1655 if (row + xtra_rows >= wp->w_height - 2)
1656 mod_bot = MAXLNUM;
1657 else
1659 check_for_delay(FALSE);
1660 if (win_ins_lines(wp, row + old_rows,
1661 xtra_rows, FALSE, FALSE) == FAIL)
1662 mod_bot = MAXLNUM;
1663 else if (top_end > row + old_rows)
1664 /* Scrolled the part at the top that requires
1665 * updating down. */
1666 top_end += xtra_rows;
1670 /* When not updating the rest, may need to move w_lines[]
1671 * entries. */
1672 if (mod_bot != MAXLNUM && i != j)
1674 if (j < i)
1676 int x = row + new_rows;
1678 /* move entries in w_lines[] upwards */
1679 for (;;)
1681 /* stop at last valid entry in w_lines[] */
1682 if (i >= wp->w_lines_valid)
1684 wp->w_lines_valid = j;
1685 break;
1687 wp->w_lines[j] = wp->w_lines[i];
1688 /* stop at a line that won't fit */
1689 if (x + (int)wp->w_lines[j].wl_size
1690 > wp->w_height)
1692 wp->w_lines_valid = j + 1;
1693 break;
1695 x += wp->w_lines[j++].wl_size;
1696 ++i;
1698 if (bot_start > x)
1699 bot_start = x;
1701 else /* j > i */
1703 /* move entries in w_lines[] downwards */
1704 j -= i;
1705 wp->w_lines_valid += j;
1706 if (wp->w_lines_valid > wp->w_height)
1707 wp->w_lines_valid = wp->w_height;
1708 for (i = wp->w_lines_valid; i - j >= idx; --i)
1709 wp->w_lines[i] = wp->w_lines[i - j];
1711 /* The w_lines[] entries for inserted lines are
1712 * now invalid, but wl_size may be used above.
1713 * Reset to zero. */
1714 while (i >= idx)
1716 wp->w_lines[i].wl_size = 0;
1717 wp->w_lines[i--].wl_valid = FALSE;
1724 #ifdef FEAT_FOLDING
1726 * When lines are folded, display one line for all of them.
1727 * Otherwise, display normally (can be several display lines when
1728 * 'wrap' is on).
1730 fold_count = foldedCount(wp, lnum, &win_foldinfo);
1731 if (fold_count != 0)
1733 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
1734 ++row;
1735 --fold_count;
1736 wp->w_lines[idx].wl_folded = TRUE;
1737 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
1738 # ifdef FEAT_SYN_HL
1739 did_update = DID_FOLD;
1740 # endif
1742 else
1743 #endif
1744 if (idx < wp->w_lines_valid
1745 && wp->w_lines[idx].wl_valid
1746 && wp->w_lines[idx].wl_lnum == lnum
1747 && lnum > wp->w_topline
1748 && !(dy_flags & DY_LASTLINE)
1749 && srow + wp->w_lines[idx].wl_size > wp->w_height
1750 #ifdef FEAT_DIFF
1751 && diff_check_fill(wp, lnum) == 0
1752 #endif
1755 /* This line is not going to fit. Don't draw anything here,
1756 * will draw "@ " lines below. */
1757 row = wp->w_height + 1;
1759 else
1761 #ifdef FEAT_SEARCH_EXTRA
1762 prepare_search_hl(wp, lnum);
1763 #endif
1764 #ifdef FEAT_SYN_HL
1765 /* Let the syntax stuff know we skipped a few lines. */
1766 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
1767 && syntax_present(buf))
1768 syntax_end_parsing(syntax_last_parsed + 1);
1769 #endif
1772 * Display one line.
1774 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
1776 #ifdef FEAT_FOLDING
1777 wp->w_lines[idx].wl_folded = FALSE;
1778 wp->w_lines[idx].wl_lastlnum = lnum;
1779 #endif
1780 #ifdef FEAT_SYN_HL
1781 did_update = DID_LINE;
1782 syntax_last_parsed = lnum;
1783 #endif
1786 wp->w_lines[idx].wl_lnum = lnum;
1787 wp->w_lines[idx].wl_valid = TRUE;
1788 if (row > wp->w_height) /* past end of screen */
1790 /* we may need the size of that too long line later on */
1791 if (dollar_vcol == 0)
1792 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
1793 ++idx;
1794 break;
1796 if (dollar_vcol == 0)
1797 wp->w_lines[idx].wl_size = row - srow;
1798 ++idx;
1799 #ifdef FEAT_FOLDING
1800 lnum += fold_count + 1;
1801 #else
1802 ++lnum;
1803 #endif
1805 else
1807 /* This line does not need updating, advance to the next one */
1808 row += wp->w_lines[idx++].wl_size;
1809 if (row > wp->w_height) /* past end of screen */
1810 break;
1811 #ifdef FEAT_FOLDING
1812 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
1813 #else
1814 ++lnum;
1815 #endif
1816 #ifdef FEAT_SYN_HL
1817 did_update = DID_NONE;
1818 #endif
1821 if (lnum > buf->b_ml.ml_line_count)
1823 eof = TRUE;
1824 break;
1828 * End of loop over all window lines.
1832 if (idx > wp->w_lines_valid)
1833 wp->w_lines_valid = idx;
1835 #ifdef FEAT_SYN_HL
1837 * Let the syntax stuff know we stop parsing here.
1839 if (syntax_last_parsed != 0 && syntax_present(buf))
1840 syntax_end_parsing(syntax_last_parsed + 1);
1841 #endif
1844 * If we didn't hit the end of the file, and we didn't finish the last
1845 * line we were working on, then the line didn't fit.
1847 wp->w_empty_rows = 0;
1848 #ifdef FEAT_DIFF
1849 wp->w_filler_rows = 0;
1850 #endif
1851 if (!eof && !didline)
1853 if (lnum == wp->w_topline)
1856 * Single line that does not fit!
1857 * Don't overwrite it, it can be edited.
1859 wp->w_botline = lnum + 1;
1861 #ifdef FEAT_DIFF
1862 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
1864 /* Window ends in filler lines. */
1865 wp->w_botline = lnum;
1866 wp->w_filler_rows = wp->w_height - srow;
1868 #endif
1869 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
1872 * Last line isn't finished: Display "@@@" at the end.
1874 screen_fill(W_WINROW(wp) + wp->w_height - 1,
1875 W_WINROW(wp) + wp->w_height,
1876 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
1877 '@', '@', hl_attr(HLF_AT));
1878 set_empty_rows(wp, srow);
1879 wp->w_botline = lnum;
1881 else
1883 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
1884 wp->w_botline = lnum;
1887 else
1889 #ifdef FEAT_VERTSPLIT
1890 draw_vsep_win(wp, row);
1891 #endif
1892 if (eof) /* we hit the end of the file */
1894 wp->w_botline = buf->b_ml.ml_line_count + 1;
1895 #ifdef FEAT_DIFF
1896 j = diff_check_fill(wp, wp->w_botline);
1897 if (j > 0 && !wp->w_botfill)
1900 * Display filler lines at the end of the file
1902 if (char2cells(fill_diff) > 1)
1903 i = '-';
1904 else
1905 i = fill_diff;
1906 if (row + j > wp->w_height)
1907 j = wp->w_height - row;
1908 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
1909 row += j;
1911 #endif
1913 else if (dollar_vcol == 0)
1914 wp->w_botline = lnum;
1916 /* make sure the rest of the screen is blank */
1917 /* put '~'s on rows that aren't part of the file. */
1918 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_AT);
1921 /* Reset the type of redrawing required, the window has been updated. */
1922 wp->w_redr_type = 0;
1923 #ifdef FEAT_DIFF
1924 wp->w_old_topfill = wp->w_topfill;
1925 wp->w_old_botfill = wp->w_botfill;
1926 #endif
1928 if (dollar_vcol == 0)
1931 * There is a trick with w_botline. If we invalidate it on each
1932 * change that might modify it, this will cause a lot of expensive
1933 * calls to plines() in update_topline() each time. Therefore the
1934 * value of w_botline is often approximated, and this value is used to
1935 * compute the value of w_topline. If the value of w_botline was
1936 * wrong, check that the value of w_topline is correct (cursor is on
1937 * the visible part of the text). If it's not, we need to redraw
1938 * again. Mostly this just means scrolling up a few lines, so it
1939 * doesn't look too bad. Only do this for the current window (where
1940 * changes are relevant).
1942 wp->w_valid |= VALID_BOTLINE;
1943 if (wp == curwin && wp->w_botline != old_botline && !recursive)
1945 recursive = TRUE;
1946 curwin->w_valid &= ~VALID_TOPLINE;
1947 update_topline(); /* may invalidate w_botline again */
1948 if (must_redraw != 0)
1950 /* Don't update for changes in buffer again. */
1951 i = curbuf->b_mod_set;
1952 curbuf->b_mod_set = FALSE;
1953 win_update(curwin);
1954 must_redraw = 0;
1955 curbuf->b_mod_set = i;
1957 recursive = FALSE;
1961 #if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1962 /* restore got_int, unless CTRL-C was hit while redrawing */
1963 if (!got_int)
1964 got_int = save_got_int;
1965 #endif
1968 #ifdef FEAT_SIGNS
1969 static int draw_signcolumn __ARGS((win_T *wp));
1972 * Return TRUE when window "wp" has a column to draw signs in.
1974 static int
1975 draw_signcolumn(wp)
1976 win_T *wp;
1978 return (wp->w_buffer->b_signlist != NULL
1979 # ifdef FEAT_NETBEANS_INTG
1980 || usingNetbeans
1981 # endif
1984 #endif
1987 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
1988 * as the filler character.
1990 static void
1991 win_draw_end(wp, c1, c2, row, endrow, hl)
1992 win_T *wp;
1993 int c1;
1994 int c2;
1995 int row;
1996 int endrow;
1997 hlf_T hl;
1999 #if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2000 int n = 0;
2001 # define FDC_OFF n
2002 #else
2003 # define FDC_OFF 0
2004 #endif
2006 #ifdef FEAT_RIGHTLEFT
2007 if (wp->w_p_rl)
2009 /* No check for cmdline window: should never be right-left. */
2010 # ifdef FEAT_FOLDING
2011 n = wp->w_p_fdc;
2013 if (n > 0)
2015 /* draw the fold column at the right */
2016 if (n > W_WIDTH(wp))
2017 n = W_WIDTH(wp);
2018 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2019 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
2020 ' ', ' ', hl_attr(HLF_FC));
2022 # endif
2023 # ifdef FEAT_SIGNS
2024 if (draw_signcolumn(wp))
2026 int nn = n + 2;
2028 /* draw the sign column left of the fold column */
2029 if (nn > W_WIDTH(wp))
2030 nn = W_WIDTH(wp);
2031 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2032 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
2033 ' ', ' ', hl_attr(HLF_SC));
2034 n = nn;
2036 # endif
2037 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2038 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
2039 c2, c2, hl_attr(hl));
2040 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2041 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
2042 c1, c2, hl_attr(hl));
2044 else
2045 #endif
2047 #ifdef FEAT_CMDWIN
2048 if (cmdwin_type != 0 && wp == curwin)
2050 /* draw the cmdline character in the leftmost column */
2051 n = 1;
2052 if (n > wp->w_width)
2053 n = wp->w_width;
2054 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2055 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
2056 cmdwin_type, ' ', hl_attr(HLF_AT));
2058 #endif
2059 #ifdef FEAT_FOLDING
2060 if (wp->w_p_fdc > 0)
2062 int nn = n + wp->w_p_fdc;
2064 /* draw the fold column at the left */
2065 if (nn > W_WIDTH(wp))
2066 nn = W_WIDTH(wp);
2067 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2068 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2069 ' ', ' ', hl_attr(HLF_FC));
2070 n = nn;
2072 #endif
2073 #ifdef FEAT_SIGNS
2074 if (draw_signcolumn(wp))
2076 int nn = n + 2;
2078 /* draw the sign column after the fold column */
2079 if (nn > W_WIDTH(wp))
2080 nn = W_WIDTH(wp);
2081 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2082 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2083 ' ', ' ', hl_attr(HLF_SC));
2084 n = nn;
2086 #endif
2087 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2088 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
2089 c1, c2, hl_attr(hl));
2091 set_empty_rows(wp, row);
2094 #ifdef FEAT_FOLDING
2096 * Display one folded line.
2098 static void
2099 fold_line(wp, fold_count, foldinfo, lnum, row)
2100 win_T *wp;
2101 long fold_count;
2102 foldinfo_T *foldinfo;
2103 linenr_T lnum;
2104 int row;
2106 char_u buf[51];
2107 pos_T *top, *bot;
2108 linenr_T lnume = lnum + fold_count - 1;
2109 int len;
2110 char_u *text;
2111 int fdc;
2112 int col;
2113 int txtcol;
2114 int off = (int)(current_ScreenLine - ScreenLines);
2115 int ri;
2117 /* Build the fold line:
2118 * 1. Add the cmdwin_type for the command-line window
2119 * 2. Add the 'foldcolumn'
2120 * 3. Add the 'number' column
2121 * 4. Compose the text
2122 * 5. Add the text
2123 * 6. set highlighting for the Visual area an other text
2125 col = 0;
2128 * 1. Add the cmdwin_type for the command-line window
2129 * Ignores 'rightleft', this window is never right-left.
2131 #ifdef FEAT_CMDWIN
2132 if (cmdwin_type != 0 && wp == curwin)
2134 ScreenLines[off] = cmdwin_type;
2135 ScreenAttrs[off] = hl_attr(HLF_AT);
2136 #ifdef FEAT_MBYTE
2137 if (enc_utf8)
2138 ScreenLinesUC[off] = 0;
2139 #endif
2140 ++col;
2142 #endif
2145 * 2. Add the 'foldcolumn'
2147 fdc = wp->w_p_fdc;
2148 if (fdc > W_WIDTH(wp) - col)
2149 fdc = W_WIDTH(wp) - col;
2150 if (fdc > 0)
2152 fill_foldcolumn(buf, wp, TRUE, lnum);
2153 #ifdef FEAT_RIGHTLEFT
2154 if (wp->w_p_rl)
2156 int i;
2158 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
2159 hl_attr(HLF_FC));
2160 /* reverse the fold column */
2161 for (i = 0; i < fdc; ++i)
2162 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2164 else
2165 #endif
2166 copy_text_attr(off + col, buf, fdc, hl_attr(HLF_FC));
2167 col += fdc;
2170 #ifdef FEAT_RIGHTLEFT
2171 # define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2172 for (ri = 0; ri < l; ++ri) \
2173 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2174 else \
2175 for (ri = 0; ri < l; ++ri) \
2176 ScreenAttrs[off + (p) + ri] = v
2177 #else
2178 # define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2179 ScreenAttrs[off + (p) + ri] = v
2180 #endif
2182 /* Set all attributes of the 'number' column and the text */
2183 RL_MEMSET(col, hl_attr(HLF_FL), W_WIDTH(wp) - col);
2185 #ifdef FEAT_SIGNS
2186 /* If signs are being displayed, add two spaces. */
2187 if (draw_signcolumn(wp))
2189 len = W_WIDTH(wp) - col;
2190 if (len > 0)
2192 if (len > 2)
2193 len = 2;
2194 # ifdef FEAT_RIGHTLEFT
2195 if (wp->w_p_rl)
2196 /* the line number isn't reversed */
2197 copy_text_attr(off + W_WIDTH(wp) - len - col,
2198 (char_u *)" ", len, hl_attr(HLF_FL));
2199 else
2200 # endif
2201 copy_text_attr(off + col, (char_u *)" ", len, hl_attr(HLF_FL));
2202 col += len;
2205 #endif
2208 * 3. Add the 'number' column
2210 if (wp->w_p_nu)
2212 len = W_WIDTH(wp) - col;
2213 if (len > 0)
2215 int w = number_width(wp);
2217 if (len > w + 1)
2218 len = w + 1;
2219 sprintf((char *)buf, "%*ld ", w, (long)lnum);
2220 #ifdef FEAT_RIGHTLEFT
2221 if (wp->w_p_rl)
2222 /* the line number isn't reversed */
2223 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
2224 hl_attr(HLF_FL));
2225 else
2226 #endif
2227 copy_text_attr(off + col, buf, len, hl_attr(HLF_FL));
2228 col += len;
2233 * 4. Compose the folded-line string with 'foldtext', if set.
2235 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
2237 txtcol = col; /* remember where text starts */
2240 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2241 * Right-left text is put in columns 0 - number-col, normal text is put
2242 * in columns number-col - window-width.
2244 #ifdef FEAT_MBYTE
2245 if (has_mbyte)
2247 int cells;
2248 int u8c, u8cc[MAX_MCO];
2249 int i;
2250 int idx;
2251 int c_len;
2252 char_u *p;
2253 # ifdef FEAT_ARABIC
2254 int prev_c = 0; /* previous Arabic character */
2255 int prev_c1 = 0; /* first composing char for prev_c */
2256 # endif
2258 # ifdef FEAT_RIGHTLEFT
2259 if (wp->w_p_rl)
2260 idx = off;
2261 else
2262 # endif
2263 idx = off + col;
2265 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2266 for (p = text; *p != NUL; )
2268 cells = (*mb_ptr2cells)(p);
2269 c_len = (*mb_ptr2len)(p);
2270 if (col + cells > W_WIDTH(wp)
2271 # ifdef FEAT_RIGHTLEFT
2272 - (wp->w_p_rl ? col : 0)
2273 # endif
2275 break;
2276 ScreenLines[idx] = *p;
2277 if (enc_utf8)
2279 u8c = utfc_ptr2char(p, u8cc);
2280 if (*p < 0x80 && u8cc[0] == 0)
2282 ScreenLinesUC[idx] = 0;
2283 #ifdef FEAT_ARABIC
2284 prev_c = u8c;
2285 #endif
2287 else
2289 #ifdef FEAT_ARABIC
2290 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2292 /* Do Arabic shaping. */
2293 int pc, pc1, nc;
2294 int pcc[MAX_MCO];
2295 int firstbyte = *p;
2297 /* The idea of what is the previous and next
2298 * character depends on 'rightleft'. */
2299 if (wp->w_p_rl)
2301 pc = prev_c;
2302 pc1 = prev_c1;
2303 nc = utf_ptr2char(p + c_len);
2304 prev_c1 = u8cc[0];
2306 else
2308 pc = utfc_ptr2char(p + c_len, pcc);
2309 nc = prev_c;
2310 pc1 = pcc[0];
2312 prev_c = u8c;
2314 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2315 pc, pc1, nc);
2316 ScreenLines[idx] = firstbyte;
2318 else
2319 prev_c = u8c;
2320 #endif
2321 /* Non-BMP character: display as ? or fullwidth ?. */
2322 #ifdef UNICODE16
2323 if (u8c >= 0x10000)
2324 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2325 else
2326 #endif
2327 ScreenLinesUC[idx] = u8c;
2328 for (i = 0; i < Screen_mco; ++i)
2330 ScreenLinesC[i][idx] = u8cc[i];
2331 if (u8cc[i] == 0)
2332 break;
2335 if (cells > 1)
2336 ScreenLines[idx + 1] = 0;
2338 else if (cells > 1) /* double-byte character */
2340 if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2341 ScreenLines2[idx] = p[1];
2342 else
2343 ScreenLines[idx + 1] = p[1];
2345 col += cells;
2346 idx += cells;
2347 p += c_len;
2350 else
2351 #endif
2353 len = (int)STRLEN(text);
2354 if (len > W_WIDTH(wp) - col)
2355 len = W_WIDTH(wp) - col;
2356 if (len > 0)
2358 #ifdef FEAT_RIGHTLEFT
2359 if (wp->w_p_rl)
2360 STRNCPY(current_ScreenLine, text, len);
2361 else
2362 #endif
2363 STRNCPY(current_ScreenLine + col, text, len);
2364 col += len;
2368 /* Fill the rest of the line with the fold filler */
2369 #ifdef FEAT_RIGHTLEFT
2370 if (wp->w_p_rl)
2371 col -= txtcol;
2372 #endif
2373 while (col < W_WIDTH(wp)
2374 #ifdef FEAT_RIGHTLEFT
2375 - (wp->w_p_rl ? txtcol : 0)
2376 #endif
2379 #ifdef FEAT_MBYTE
2380 if (enc_utf8)
2382 if (fill_fold >= 0x80)
2384 ScreenLinesUC[off + col] = fill_fold;
2385 ScreenLinesC[0][off + col] = 0;
2387 else
2388 ScreenLinesUC[off + col] = 0;
2390 #endif
2391 ScreenLines[off + col++] = fill_fold;
2394 if (text != buf)
2395 vim_free(text);
2398 * 6. set highlighting for the Visual area an other text.
2399 * If all folded lines are in the Visual area, highlight the line.
2401 #ifdef FEAT_VISUAL
2402 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2404 if (ltoreq(curwin->w_cursor, VIsual))
2406 /* Visual is after curwin->w_cursor */
2407 top = &curwin->w_cursor;
2408 bot = &VIsual;
2410 else
2412 /* Visual is before curwin->w_cursor */
2413 top = &VIsual;
2414 bot = &curwin->w_cursor;
2416 if (lnum >= top->lnum
2417 && lnume <= bot->lnum
2418 && (VIsual_mode != 'v'
2419 || ((lnum > top->lnum
2420 || (lnum == top->lnum
2421 && top->col == 0))
2422 && (lnume < bot->lnum
2423 || (lnume == bot->lnum
2424 && (bot->col - (*p_sel == 'e'))
2425 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
2427 if (VIsual_mode == Ctrl_V)
2429 /* Visual block mode: highlight the chars part of the block */
2430 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2432 if (wp->w_old_cursor_lcol + txtcol < (colnr_T)W_WIDTH(wp))
2433 len = wp->w_old_cursor_lcol;
2434 else
2435 len = W_WIDTH(wp) - txtcol;
2436 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, hl_attr(HLF_V),
2437 len - (int)wp->w_old_cursor_fcol);
2440 else
2442 /* Set all attributes of the text */
2443 RL_MEMSET(txtcol, hl_attr(HLF_V), W_WIDTH(wp) - txtcol);
2447 #endif
2449 #ifdef FEAT_SYN_HL
2450 /* Show 'cursorcolumn' in the fold line. */
2451 if (wp->w_p_cuc)
2453 txtcol += wp->w_virtcol;
2454 if (wp->w_p_wrap)
2455 txtcol -= wp->w_skipcol;
2456 else
2457 txtcol -= wp->w_leftcol;
2458 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2459 ScreenAttrs[off + txtcol] = hl_combine_attr(
2460 ScreenAttrs[off + txtcol], hl_attr(HLF_CUC));
2462 #endif
2464 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2465 (int)W_WIDTH(wp), FALSE);
2468 * Update w_cline_height and w_cline_folded if the cursor line was
2469 * updated (saves a call to plines() later).
2471 if (wp == curwin
2472 && lnum <= curwin->w_cursor.lnum
2473 && lnume >= curwin->w_cursor.lnum)
2475 curwin->w_cline_row = row;
2476 curwin->w_cline_height = 1;
2477 curwin->w_cline_folded = TRUE;
2478 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2483 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2485 static void
2486 copy_text_attr(off, buf, len, attr)
2487 int off;
2488 char_u *buf;
2489 int len;
2490 int attr;
2492 int i;
2494 mch_memmove(ScreenLines + off, buf, (size_t)len);
2495 # ifdef FEAT_MBYTE
2496 if (enc_utf8)
2497 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2498 # endif
2499 for (i = 0; i < len; ++i)
2500 ScreenAttrs[off + i] = attr;
2504 * Fill the foldcolumn at "p" for window "wp".
2505 * Only to be called when 'foldcolumn' > 0.
2507 static void
2508 fill_foldcolumn(p, wp, closed, lnum)
2509 char_u *p;
2510 win_T *wp;
2511 int closed; /* TRUE of FALSE */
2512 linenr_T lnum; /* current line number */
2514 int i = 0;
2515 int level;
2516 int first_level;
2517 int empty;
2519 /* Init to all spaces. */
2520 copy_spaces(p, (size_t)wp->w_p_fdc);
2522 level = win_foldinfo.fi_level;
2523 if (level > 0)
2525 /* If there is only one column put more info in it. */
2526 empty = (wp->w_p_fdc == 1) ? 0 : 1;
2528 /* If the column is too narrow, we start at the lowest level that
2529 * fits and use numbers to indicated the depth. */
2530 first_level = level - wp->w_p_fdc - closed + 1 + empty;
2531 if (first_level < 1)
2532 first_level = 1;
2534 for (i = 0; i + empty < wp->w_p_fdc; ++i)
2536 if (win_foldinfo.fi_lnum == lnum
2537 && first_level + i >= win_foldinfo.fi_low_level)
2538 p[i] = '-';
2539 else if (first_level == 1)
2540 p[i] = '|';
2541 else if (first_level + i <= 9)
2542 p[i] = '0' + first_level + i;
2543 else
2544 p[i] = '>';
2545 if (first_level + i == level)
2546 break;
2549 if (closed)
2550 p[i >= wp->w_p_fdc ? i - 1 : i] = '+';
2552 #endif /* FEAT_FOLDING */
2555 * Display line "lnum" of window 'wp' on the screen.
2556 * Start at row "startrow", stop when "endrow" is reached.
2557 * wp->w_virtcol needs to be valid.
2559 * Return the number of last row the line occupies.
2561 static int
2562 win_line(wp, lnum, startrow, endrow, nochange)
2563 win_T *wp;
2564 linenr_T lnum;
2565 int startrow;
2566 int endrow;
2567 int nochange UNUSED; /* not updating for changed text */
2569 int col; /* visual column on screen */
2570 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2571 int c = 0; /* init for GCC */
2572 long vcol = 0; /* virtual column (for tabs) */
2573 long vcol_prev = -1; /* "vcol" of previous character */
2574 char_u *line; /* current line */
2575 char_u *ptr; /* current position in "line" */
2576 int row; /* row in the window, excl w_winrow */
2577 int screen_row; /* row on the screen, incl w_winrow */
2579 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2580 int n_extra = 0; /* number of extra chars */
2581 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
2582 int c_extra = NUL; /* extra chars, all the same */
2583 int extra_attr = 0; /* attributes when n_extra != 0 */
2584 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2585 displaying lcs_eol at end-of-line */
2586 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2587 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2589 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2590 int saved_n_extra = 0;
2591 char_u *saved_p_extra = NULL;
2592 int saved_c_extra = 0;
2593 int saved_char_attr = 0;
2595 int n_attr = 0; /* chars with special attr */
2596 int saved_attr2 = 0; /* char_attr saved for n_attr */
2597 int n_attr3 = 0; /* chars with overruling special attr */
2598 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2600 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2602 int fromcol, tocol; /* start/end of inverting */
2603 int fromcol_prev = -2; /* start of inverting after cursor */
2604 int noinvcur = FALSE; /* don't invert the cursor */
2605 #ifdef FEAT_VISUAL
2606 pos_T *top, *bot;
2607 int lnum_in_visual_area = FALSE;
2608 #endif
2609 pos_T pos;
2610 long v;
2612 int char_attr = 0; /* attributes for next character */
2613 int attr_pri = FALSE; /* char_attr has priority */
2614 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2615 in this line */
2616 int attr = 0; /* attributes for area highlighting */
2617 int area_attr = 0; /* attributes desired by highlighting */
2618 int search_attr = 0; /* attributes desired by 'hlsearch' */
2619 #ifdef FEAT_SYN_HL
2620 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
2621 int syntax_attr = 0; /* attributes desired by syntax */
2622 int has_syntax = FALSE; /* this buffer has syntax highl. */
2623 int save_did_emsg;
2624 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
2625 #endif
2626 #ifdef FEAT_SPELL
2627 int has_spell = FALSE; /* this buffer has spell checking */
2628 # define SPWORDLEN 150
2629 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
2630 int nextlinecol = 0; /* column where nextline[] starts */
2631 int nextline_idx = 0; /* index in nextline[] where next line
2632 starts */
2633 int spell_attr = 0; /* attributes desired by spelling */
2634 int word_end = 0; /* last byte with same spell_attr */
2635 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2636 static int checked_col = 0; /* column in "checked_lnum" up to which
2637 * there are no spell errors */
2638 static int cap_col = -1; /* column to check for Cap word */
2639 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
2640 int cur_checked_col = 0; /* checked column for current line */
2641 #endif
2642 int extra_check; /* has syntax or linebreak */
2643 #ifdef FEAT_MBYTE
2644 int multi_attr = 0; /* attributes desired by multibyte */
2645 int mb_l = 1; /* multi-byte byte length */
2646 int mb_c = 0; /* decoded multi-byte character */
2647 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
2648 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
2649 #endif
2650 #ifdef FEAT_DIFF
2651 int filler_lines; /* nr of filler lines to be drawn */
2652 int filler_todo; /* nr of filler lines still to do + 1 */
2653 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
2654 int change_start = MAXCOL; /* first col of changed area */
2655 int change_end = -1; /* last col of changed area */
2656 #endif
2657 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
2658 #ifdef FEAT_LINEBREAK
2659 int need_showbreak = FALSE;
2660 #endif
2661 #if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
2662 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
2663 # define LINE_ATTR
2664 int line_attr = 0; /* attribute for the whole line */
2665 #endif
2666 #ifdef FEAT_SEARCH_EXTRA
2667 matchitem_T *cur; /* points to the match list */
2668 match_T *shl; /* points to search_hl or a match */
2669 int shl_flag; /* flag to indicate whether search_hl
2670 has been processed or not */
2671 int prevcol_hl_flag; /* flag to indicate whether prevcol
2672 equals startcol of search_hl or one
2673 of the matches */
2674 #endif
2675 #ifdef FEAT_ARABIC
2676 int prev_c = 0; /* previous Arabic character */
2677 int prev_c1 = 0; /* first composing char for prev_c */
2678 #endif
2679 #if defined(LINE_ATTR)
2680 int did_line_attr = 0;
2681 #endif
2683 /* draw_state: items that are drawn in sequence: */
2684 #define WL_START 0 /* nothing done yet */
2685 #ifdef FEAT_CMDWIN
2686 # define WL_CMDLINE WL_START + 1 /* cmdline window column */
2687 #else
2688 # define WL_CMDLINE WL_START
2689 #endif
2690 #ifdef FEAT_FOLDING
2691 # define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
2692 #else
2693 # define WL_FOLD WL_CMDLINE
2694 #endif
2695 #ifdef FEAT_SIGNS
2696 # define WL_SIGN WL_FOLD + 1 /* column for signs */
2697 #else
2698 # define WL_SIGN WL_FOLD /* column for signs */
2699 #endif
2700 #define WL_NR WL_SIGN + 1 /* line number */
2701 #if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
2702 # define WL_SBR WL_NR + 1 /* 'showbreak' or 'diff' */
2703 #else
2704 # define WL_SBR WL_NR
2705 #endif
2706 #define WL_LINE WL_SBR + 1 /* text in the line */
2707 int draw_state = WL_START; /* what to draw next */
2708 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2709 int feedback_col = 0;
2710 int feedback_old_attr = -1;
2711 #endif
2714 if (startrow > endrow) /* past the end already! */
2715 return startrow;
2717 row = startrow;
2718 screen_row = row + W_WINROW(wp);
2721 * To speed up the loop below, set extra_check when there is linebreak,
2722 * trailing white space and/or syntax processing to be done.
2724 #ifdef FEAT_LINEBREAK
2725 extra_check = wp->w_p_lbr;
2726 #else
2727 extra_check = 0;
2728 #endif
2729 #ifdef FEAT_SYN_HL
2730 if (syntax_present(wp->w_buffer) && !wp->w_buffer->b_syn_error)
2732 /* Prepare for syntax highlighting in this line. When there is an
2733 * error, stop syntax highlighting. */
2734 save_did_emsg = did_emsg;
2735 did_emsg = FALSE;
2736 syntax_start(wp, lnum);
2737 if (did_emsg)
2738 wp->w_buffer->b_syn_error = TRUE;
2739 else
2741 did_emsg = save_did_emsg;
2742 has_syntax = TRUE;
2743 extra_check = TRUE;
2746 #endif
2748 #ifdef FEAT_SPELL
2749 if (wp->w_p_spell
2750 && *wp->w_buffer->b_p_spl != NUL
2751 && wp->w_buffer->b_langp.ga_len > 0
2752 && *(char **)(wp->w_buffer->b_langp.ga_data) != NULL)
2754 /* Prepare for spell checking. */
2755 has_spell = TRUE;
2756 extra_check = TRUE;
2758 /* Get the start of the next line, so that words that wrap to the next
2759 * line are found too: "et<line-break>al.".
2760 * Trick: skip a few chars for C/shell/Vim comments */
2761 nextline[SPWORDLEN] = NUL;
2762 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2764 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
2765 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
2768 /* When a word wrapped from the previous line the start of the current
2769 * line is valid. */
2770 if (lnum == checked_lnum)
2771 cur_checked_col = checked_col;
2772 checked_lnum = 0;
2774 /* When there was a sentence end in the previous line may require a
2775 * word starting with capital in this line. In line 1 always check
2776 * the first word. */
2777 if (lnum != capcol_lnum)
2778 cap_col = -1;
2779 if (lnum == 1)
2780 cap_col = 0;
2781 capcol_lnum = 0;
2783 #endif
2786 * handle visual active in this window
2788 fromcol = -10;
2789 tocol = MAXCOL;
2790 #ifdef FEAT_VISUAL
2791 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2793 /* Visual is after curwin->w_cursor */
2794 if (ltoreq(curwin->w_cursor, VIsual))
2796 top = &curwin->w_cursor;
2797 bot = &VIsual;
2799 else /* Visual is before curwin->w_cursor */
2801 top = &VIsual;
2802 bot = &curwin->w_cursor;
2804 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
2805 if (VIsual_mode == Ctrl_V) /* block mode */
2807 if (lnum_in_visual_area)
2809 fromcol = wp->w_old_cursor_fcol;
2810 tocol = wp->w_old_cursor_lcol;
2813 else /* non-block mode */
2815 if (lnum > top->lnum && lnum <= bot->lnum)
2816 fromcol = 0;
2817 else if (lnum == top->lnum)
2819 if (VIsual_mode == 'V') /* linewise */
2820 fromcol = 0;
2821 else
2823 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
2824 if (gchar_pos(top) == NUL)
2825 tocol = fromcol + 1;
2828 if (VIsual_mode != 'V' && lnum == bot->lnum)
2830 if (*p_sel == 'e' && bot->col == 0
2831 #ifdef FEAT_VIRTUALEDIT
2832 && bot->coladd == 0
2833 #endif
2836 fromcol = -10;
2837 tocol = MAXCOL;
2839 else if (bot->col == MAXCOL)
2840 tocol = MAXCOL;
2841 else
2843 pos = *bot;
2844 if (*p_sel == 'e')
2845 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
2846 else
2848 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
2849 ++tocol;
2855 #ifndef MSDOS
2856 /* Check if the character under the cursor should not be inverted */
2857 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
2858 # ifdef FEAT_GUI
2859 && !gui.in_use
2860 # endif
2862 noinvcur = TRUE;
2863 #endif
2865 /* if inverting in this line set area_highlighting */
2866 if (fromcol >= 0)
2868 area_highlighting = TRUE;
2869 attr = hl_attr(HLF_V);
2870 #if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
2871 if (clip_star.available && !clip_star.owned && clip_isautosel())
2872 attr = hl_attr(HLF_VNC);
2873 #endif
2878 * handle 'incsearch' and ":s///c" highlighting
2880 else
2881 #endif /* FEAT_VISUAL */
2882 if (highlight_match
2883 && wp == curwin
2884 && lnum >= curwin->w_cursor.lnum
2885 && lnum <= curwin->w_cursor.lnum + search_match_lines)
2887 if (lnum == curwin->w_cursor.lnum)
2888 getvcol(curwin, &(curwin->w_cursor),
2889 (colnr_T *)&fromcol, NULL, NULL);
2890 else
2891 fromcol = 0;
2892 if (lnum == curwin->w_cursor.lnum + search_match_lines)
2894 pos.lnum = lnum;
2895 pos.col = search_match_endcol;
2896 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
2898 else
2899 tocol = MAXCOL;
2900 /* do at least one character; happens when past end of line */
2901 if (fromcol == tocol)
2902 tocol = fromcol + 1;
2903 area_highlighting = TRUE;
2904 attr = hl_attr(HLF_I);
2907 #ifdef FEAT_DIFF
2908 filler_lines = diff_check(wp, lnum);
2909 if (filler_lines < 0)
2911 if (filler_lines == -1)
2913 if (diff_find_change(wp, lnum, &change_start, &change_end))
2914 diff_hlf = HLF_ADD; /* added line */
2915 else if (change_start == 0)
2916 diff_hlf = HLF_TXD; /* changed text */
2917 else
2918 diff_hlf = HLF_CHD; /* changed line */
2920 else
2921 diff_hlf = HLF_ADD; /* added line */
2922 filler_lines = 0;
2923 area_highlighting = TRUE;
2925 if (lnum == wp->w_topline)
2926 filler_lines = wp->w_topfill;
2927 filler_todo = filler_lines;
2928 #endif
2930 #ifdef LINE_ATTR
2931 # ifdef FEAT_SIGNS
2932 /* If this line has a sign with line highlighting set line_attr. */
2933 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
2934 if (v != 0)
2935 line_attr = sign_get_attr((int)v, TRUE);
2936 # endif
2937 # if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
2938 /* Highlight the current line in the quickfix window. */
2939 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
2940 line_attr = hl_attr(HLF_L);
2941 # endif
2942 if (line_attr != 0)
2943 area_highlighting = TRUE;
2944 #endif
2946 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
2947 ptr = line;
2949 #ifdef FEAT_SPELL
2950 if (has_spell)
2952 /* For checking first word with a capital skip white space. */
2953 if (cap_col == 0)
2954 cap_col = (int)(skipwhite(line) - line);
2956 /* To be able to spell-check over line boundaries copy the end of the
2957 * current line into nextline[]. Above the start of the next line was
2958 * copied to nextline[SPWORDLEN]. */
2959 if (nextline[SPWORDLEN] == NUL)
2961 /* No next line or it is empty. */
2962 nextlinecol = MAXCOL;
2963 nextline_idx = 0;
2965 else
2967 v = (long)STRLEN(line);
2968 if (v < SPWORDLEN)
2970 /* Short line, use it completely and append the start of the
2971 * next line. */
2972 nextlinecol = 0;
2973 mch_memmove(nextline, line, (size_t)v);
2974 STRMOVE(nextline + v, nextline + SPWORDLEN);
2975 nextline_idx = v + 1;
2977 else
2979 /* Long line, use only the last SPWORDLEN bytes. */
2980 nextlinecol = v - SPWORDLEN;
2981 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
2982 nextline_idx = SPWORDLEN + 1;
2986 #endif
2988 /* find start of trailing whitespace */
2989 if (wp->w_p_list && lcs_trail)
2991 trailcol = (colnr_T)STRLEN(ptr);
2992 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
2993 --trailcol;
2994 trailcol += (colnr_T) (ptr - line);
2995 extra_check = TRUE;
2999 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3000 * first character to be displayed.
3002 if (wp->w_p_wrap)
3003 v = wp->w_skipcol;
3004 else
3005 v = wp->w_leftcol;
3006 if (v > 0)
3008 #ifdef FEAT_MBYTE
3009 char_u *prev_ptr = ptr;
3010 #endif
3011 while (vcol < v && *ptr != NUL)
3013 c = win_lbr_chartabsize(wp, ptr, (colnr_T)vcol, NULL);
3014 vcol += c;
3015 #ifdef FEAT_MBYTE
3016 prev_ptr = ptr;
3017 #endif
3018 mb_ptr_adv(ptr);
3021 #if defined(FEAT_SYN_HL) || defined(FEAT_VIRTUALEDIT) || defined(FEAT_VISUAL)
3022 /* When:
3023 * - 'cuc' is set, or
3024 * - 'virtualedit' is set, or
3025 * - the visual mode is active,
3026 * the end of the line may be before the start of the displayed part.
3028 if (vcol < v && (
3029 # ifdef FEAT_SYN_HL
3030 wp->w_p_cuc
3031 # if defined(FEAT_VIRTUALEDIT) || defined(FEAT_VISUAL)
3033 # endif
3034 # endif
3035 # ifdef FEAT_VIRTUALEDIT
3036 virtual_active()
3037 # ifdef FEAT_VISUAL
3039 # endif
3040 # endif
3041 # ifdef FEAT_VISUAL
3042 (VIsual_active && wp->w_buffer == curwin->w_buffer)
3043 # endif
3046 vcol = v;
3048 #endif
3050 /* Handle a character that's not completely on the screen: Put ptr at
3051 * that character but skip the first few screen characters. */
3052 if (vcol > v)
3054 vcol -= c;
3055 #ifdef FEAT_MBYTE
3056 ptr = prev_ptr;
3057 #else
3058 --ptr;
3059 #endif
3060 n_skip = v - vcol;
3064 * Adjust for when the inverted text is before the screen,
3065 * and when the start of the inverted text is before the screen.
3067 if (tocol <= vcol)
3068 fromcol = 0;
3069 else if (fromcol >= 0 && fromcol < vcol)
3070 fromcol = vcol;
3072 #ifdef FEAT_LINEBREAK
3073 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3074 if (wp->w_p_wrap)
3075 need_showbreak = TRUE;
3076 #endif
3077 #ifdef FEAT_SPELL
3078 /* When spell checking a word we need to figure out the start of the
3079 * word and if it's badly spelled or not. */
3080 if (has_spell)
3082 int len;
3083 colnr_T linecol = (colnr_T)(ptr - line);
3084 hlf_T spell_hlf = HLF_COUNT;
3086 pos = wp->w_cursor;
3087 wp->w_cursor.lnum = lnum;
3088 wp->w_cursor.col = linecol;
3089 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
3091 /* spell_move_to() may call ml_get() and make "line" invalid */
3092 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3093 ptr = line + linecol;
3095 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
3097 /* no bad word found at line start, don't check until end of a
3098 * word */
3099 spell_hlf = HLF_COUNT;
3100 word_end = (int)(spell_to_word_end(ptr, wp->w_buffer)
3101 - line + 1);
3103 else
3105 /* bad word found, use attributes until end of word */
3106 word_end = wp->w_cursor.col + len + 1;
3108 /* Turn index into actual attributes. */
3109 if (spell_hlf != HLF_COUNT)
3110 spell_attr = highlight_attr[spell_hlf];
3112 wp->w_cursor = pos;
3114 # ifdef FEAT_SYN_HL
3115 /* Need to restart syntax highlighting for this line. */
3116 if (has_syntax)
3117 syntax_start(wp, lnum);
3118 # endif
3120 #endif
3124 * Correct highlighting for cursor that can't be disabled.
3125 * Avoids having to check this for each character.
3127 if (fromcol >= 0)
3129 if (noinvcur)
3131 if ((colnr_T)fromcol == wp->w_virtcol)
3133 /* highlighting starts at cursor, let it start just after the
3134 * cursor */
3135 fromcol_prev = fromcol;
3136 fromcol = -1;
3138 else if ((colnr_T)fromcol < wp->w_virtcol)
3139 /* restart highlighting after the cursor */
3140 fromcol_prev = wp->w_virtcol;
3142 if (fromcol >= tocol)
3143 fromcol = -1;
3146 #ifdef FEAT_SEARCH_EXTRA
3148 * Handle highlighting the last used search pattern and matches.
3149 * Do this for both search_hl and the match list.
3151 cur = wp->w_match_head;
3152 shl_flag = FALSE;
3153 while (cur != NULL || shl_flag == FALSE)
3155 if (shl_flag == FALSE)
3157 shl = &search_hl;
3158 shl_flag = TRUE;
3160 else
3161 shl = &cur->hl;
3162 shl->startcol = MAXCOL;
3163 shl->endcol = MAXCOL;
3164 shl->attr_cur = 0;
3165 if (shl->rm.regprog != NULL)
3167 v = (long)(ptr - line);
3168 next_search_hl(wp, shl, lnum, (colnr_T)v);
3170 /* Need to get the line again, a multi-line regexp may have made it
3171 * invalid. */
3172 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3173 ptr = line + v;
3175 if (shl->lnum != 0 && shl->lnum <= lnum)
3177 if (shl->lnum == lnum)
3178 shl->startcol = shl->rm.startpos[0].col;
3179 else
3180 shl->startcol = 0;
3181 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3182 - shl->rm.startpos[0].lnum)
3183 shl->endcol = shl->rm.endpos[0].col;
3184 else
3185 shl->endcol = MAXCOL;
3186 /* Highlight one character for an empty match. */
3187 if (shl->startcol == shl->endcol)
3189 #ifdef FEAT_MBYTE
3190 if (has_mbyte && line[shl->endcol] != NUL)
3191 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3192 else
3193 #endif
3194 ++shl->endcol;
3196 if ((long)shl->startcol < v) /* match at leftcol */
3198 shl->attr_cur = shl->attr;
3199 search_attr = shl->attr;
3201 area_highlighting = TRUE;
3204 if (shl != &search_hl && cur != NULL)
3205 cur = cur->next;
3207 #endif
3209 #ifdef FEAT_SYN_HL
3210 /* Cursor line highlighting for 'cursorline'. Not when Visual mode is
3211 * active, because it's not clear what is selected then. */
3212 if (wp->w_p_cul && lnum == wp->w_cursor.lnum && !VIsual_active)
3214 line_attr = hl_attr(HLF_CUL);
3215 area_highlighting = TRUE;
3217 #endif
3219 off = (unsigned)(current_ScreenLine - ScreenLines);
3220 col = 0;
3221 #ifdef FEAT_RIGHTLEFT
3222 if (wp->w_p_rl)
3224 /* Rightleft window: process the text in the normal direction, but put
3225 * it in current_ScreenLine[] from right to left. Start at the
3226 * rightmost column of the window. */
3227 col = W_WIDTH(wp) - 1;
3228 off += col;
3230 #endif
3233 * Repeat for the whole displayed line.
3235 for (;;)
3237 /* Skip this quickly when working on the text. */
3238 if (draw_state != WL_LINE)
3240 #ifdef FEAT_CMDWIN
3241 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3243 draw_state = WL_CMDLINE;
3244 if (cmdwin_type != 0 && wp == curwin)
3246 /* Draw the cmdline character. */
3247 n_extra = 1;
3248 c_extra = cmdwin_type;
3249 char_attr = hl_attr(HLF_AT);
3252 #endif
3254 #ifdef FEAT_FOLDING
3255 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3257 draw_state = WL_FOLD;
3258 if (wp->w_p_fdc > 0)
3260 /* Draw the 'foldcolumn'. */
3261 fill_foldcolumn(extra, wp, FALSE, lnum);
3262 n_extra = wp->w_p_fdc;
3263 p_extra = extra;
3264 p_extra[n_extra] = NUL;
3265 c_extra = NUL;
3266 char_attr = hl_attr(HLF_FC);
3269 #endif
3271 #ifdef FEAT_SIGNS
3272 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3274 draw_state = WL_SIGN;
3275 /* Show the sign column when there are any signs in this
3276 * buffer or when using Netbeans. */
3277 if (draw_signcolumn(wp)
3278 # ifdef FEAT_DIFF
3279 && filler_todo <= 0
3280 # endif
3283 int_u text_sign;
3284 # ifdef FEAT_SIGN_ICONS
3285 int_u icon_sign;
3286 # endif
3288 /* Draw two cells with the sign value or blank. */
3289 c_extra = ' ';
3290 char_attr = hl_attr(HLF_SC);
3291 n_extra = 2;
3293 if (row == startrow)
3295 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3296 SIGN_TEXT);
3297 # ifdef FEAT_SIGN_ICONS
3298 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3299 SIGN_ICON);
3300 if (gui.in_use && icon_sign != 0)
3302 /* Use the image in this position. */
3303 c_extra = SIGN_BYTE;
3304 # ifdef FEAT_NETBEANS_INTG
3305 if (buf_signcount(wp->w_buffer, lnum) > 1)
3306 c_extra = MULTISIGN_BYTE;
3307 # endif
3308 char_attr = icon_sign;
3310 else
3311 # endif
3312 if (text_sign != 0)
3314 p_extra = sign_get_text(text_sign);
3315 if (p_extra != NULL)
3317 c_extra = NUL;
3318 n_extra = (int)STRLEN(p_extra);
3320 char_attr = sign_get_attr(text_sign, FALSE);
3325 #endif
3327 if (draw_state == WL_NR - 1 && n_extra == 0)
3329 draw_state = WL_NR;
3330 /* Display the line number. After the first fill with blanks
3331 * when the 'n' flag isn't in 'cpo' */
3332 if (wp->w_p_nu
3333 && (row == startrow
3334 #ifdef FEAT_DIFF
3335 + filler_lines
3336 #endif
3337 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3339 /* Draw the line number (empty space after wrapping). */
3340 if (row == startrow
3341 #ifdef FEAT_DIFF
3342 + filler_lines
3343 #endif
3346 sprintf((char *)extra, "%*ld ",
3347 number_width(wp), (long)lnum);
3348 if (wp->w_skipcol > 0)
3349 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3350 *p_extra = '-';
3351 #ifdef FEAT_RIGHTLEFT
3352 if (wp->w_p_rl) /* reverse line numbers */
3353 rl_mirror(extra);
3354 #endif
3355 p_extra = extra;
3356 c_extra = NUL;
3358 else
3359 c_extra = ' ';
3360 n_extra = number_width(wp) + 1;
3361 char_attr = hl_attr(HLF_N);
3362 #ifdef FEAT_SYN_HL
3363 /* When 'cursorline' is set highlight the line number of
3364 * the current line differently. */
3365 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3366 char_attr = hl_combine_attr(hl_attr(HLF_CUL), char_attr);
3367 #endif
3371 #if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3372 if (draw_state == WL_SBR - 1 && n_extra == 0)
3374 draw_state = WL_SBR;
3375 # ifdef FEAT_DIFF
3376 if (filler_todo > 0)
3378 /* Draw "deleted" diff line(s). */
3379 if (char2cells(fill_diff) > 1)
3380 c_extra = '-';
3381 else
3382 c_extra = fill_diff;
3383 # ifdef FEAT_RIGHTLEFT
3384 if (wp->w_p_rl)
3385 n_extra = col + 1;
3386 else
3387 # endif
3388 n_extra = W_WIDTH(wp) - col;
3389 char_attr = hl_attr(HLF_DED);
3391 # endif
3392 # ifdef FEAT_LINEBREAK
3393 if (*p_sbr != NUL && need_showbreak)
3395 /* Draw 'showbreak' at the start of each broken line. */
3396 p_extra = p_sbr;
3397 c_extra = NUL;
3398 n_extra = (int)STRLEN(p_sbr);
3399 char_attr = hl_attr(HLF_AT);
3400 need_showbreak = FALSE;
3401 /* Correct end of highlighted area for 'showbreak',
3402 * required when 'linebreak' is also set. */
3403 if (tocol == vcol)
3404 tocol += n_extra;
3406 # endif
3408 #endif
3410 if (draw_state == WL_LINE - 1 && n_extra == 0)
3412 draw_state = WL_LINE;
3413 if (saved_n_extra)
3415 /* Continue item from end of wrapped line. */
3416 n_extra = saved_n_extra;
3417 c_extra = saved_c_extra;
3418 p_extra = saved_p_extra;
3419 char_attr = saved_char_attr;
3421 else
3422 char_attr = 0;
3426 /* When still displaying '$' of change command, stop at cursor */
3427 if (dollar_vcol != 0 && wp == curwin
3428 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
3429 #ifdef FEAT_DIFF
3430 && filler_todo <= 0
3431 #endif
3434 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3435 wp->w_p_rl);
3436 /* Pretend we have finished updating the window. Except when
3437 * 'cursorcolumn' is set. */
3438 #ifdef FEAT_SYN_HL
3439 if (wp->w_p_cuc)
3440 row = wp->w_cline_row + wp->w_cline_height;
3441 else
3442 #endif
3443 row = wp->w_height;
3444 break;
3447 if (draw_state == WL_LINE && area_highlighting)
3449 /* handle Visual or match highlighting in this line */
3450 if (vcol == fromcol
3451 #ifdef FEAT_MBYTE
3452 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3453 && (*mb_ptr2cells)(ptr) > 1)
3454 #endif
3455 || ((int)vcol_prev == fromcol_prev
3456 && vcol_prev < vcol /* not at margin */
3457 && vcol < tocol))
3458 area_attr = attr; /* start highlighting */
3459 else if (area_attr != 0
3460 && (vcol == tocol
3461 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
3462 area_attr = 0; /* stop highlighting */
3464 #ifdef FEAT_SEARCH_EXTRA
3465 if (!n_extra)
3468 * Check for start/end of search pattern match.
3469 * After end, check for start/end of next match.
3470 * When another match, have to check for start again.
3471 * Watch out for matching an empty string!
3472 * Do this for 'search_hl' and the match list (ordered by
3473 * priority).
3475 v = (long)(ptr - line);
3476 cur = wp->w_match_head;
3477 shl_flag = FALSE;
3478 while (cur != NULL || shl_flag == FALSE)
3480 if (shl_flag == FALSE
3481 && ((cur != NULL
3482 && cur->priority > SEARCH_HL_PRIORITY)
3483 || cur == NULL))
3485 shl = &search_hl;
3486 shl_flag = TRUE;
3488 else
3489 shl = &cur->hl;
3490 while (shl->rm.regprog != NULL)
3492 if (shl->startcol != MAXCOL
3493 && v >= (long)shl->startcol
3494 && v < (long)shl->endcol)
3496 shl->attr_cur = shl->attr;
3498 else if (v == (long)shl->endcol)
3500 shl->attr_cur = 0;
3502 next_search_hl(wp, shl, lnum, (colnr_T)v);
3504 /* Need to get the line again, a multi-line regexp
3505 * may have made it invalid. */
3506 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3507 ptr = line + v;
3509 if (shl->lnum == lnum)
3511 shl->startcol = shl->rm.startpos[0].col;
3512 if (shl->rm.endpos[0].lnum == 0)
3513 shl->endcol = shl->rm.endpos[0].col;
3514 else
3515 shl->endcol = MAXCOL;
3517 if (shl->startcol == shl->endcol)
3519 /* highlight empty match, try again after
3520 * it */
3521 #ifdef FEAT_MBYTE
3522 if (has_mbyte)
3523 shl->endcol += (*mb_ptr2len)(line
3524 + shl->endcol);
3525 else
3526 #endif
3527 ++shl->endcol;
3530 /* Loop to check if the match starts at the
3531 * current position */
3532 continue;
3535 break;
3537 if (shl != &search_hl && cur != NULL)
3538 cur = cur->next;
3541 /* Use attributes from match with highest priority among
3542 * 'search_hl' and the match list. */
3543 search_attr = search_hl.attr_cur;
3544 cur = wp->w_match_head;
3545 shl_flag = FALSE;
3546 while (cur != NULL || shl_flag == FALSE)
3548 if (shl_flag == FALSE
3549 && ((cur != NULL
3550 && cur->priority > SEARCH_HL_PRIORITY)
3551 || cur == NULL))
3553 shl = &search_hl;
3554 shl_flag = TRUE;
3556 else
3557 shl = &cur->hl;
3558 if (shl->attr_cur != 0)
3559 search_attr = shl->attr_cur;
3560 if (shl != &search_hl && cur != NULL)
3561 cur = cur->next;
3564 #endif
3566 #ifdef FEAT_DIFF
3567 if (diff_hlf != (hlf_T)0)
3569 if (diff_hlf == HLF_CHD && ptr - line >= change_start
3570 && n_extra == 0)
3571 diff_hlf = HLF_TXD; /* changed text */
3572 if (diff_hlf == HLF_TXD && ptr - line > change_end
3573 && n_extra == 0)
3574 diff_hlf = HLF_CHD; /* changed line */
3575 line_attr = hl_attr(diff_hlf);
3577 #endif
3579 /* Decide which of the highlight attributes to use. */
3580 attr_pri = TRUE;
3581 if (area_attr != 0)
3582 char_attr = area_attr;
3583 else if (search_attr != 0)
3584 char_attr = search_attr;
3585 #ifdef LINE_ATTR
3586 /* Use line_attr when not in the Visual or 'incsearch' area
3587 * (area_attr may be 0 when "noinvcur" is set). */
3588 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
3589 || vcol < fromcol || vcol_prev < fromcol_prev
3590 || vcol >= tocol))
3591 char_attr = line_attr;
3592 #endif
3593 else
3595 attr_pri = FALSE;
3596 #ifdef FEAT_SYN_HL
3597 if (has_syntax)
3598 char_attr = syntax_attr;
3599 else
3600 #endif
3601 char_attr = 0;
3606 * Get the next character to put on the screen.
3609 * The "p_extra" points to the extra stuff that is inserted to
3610 * represent special characters (non-printable stuff) and other
3611 * things. When all characters are the same, c_extra is used.
3612 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
3613 * "p_extra[n_extra]".
3614 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
3616 if (n_extra > 0)
3618 if (c_extra != NUL)
3620 c = c_extra;
3621 #ifdef FEAT_MBYTE
3622 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
3623 if (enc_utf8 && (*mb_char2len)(c) > 1)
3625 mb_utf8 = TRUE;
3626 u8cc[0] = 0;
3627 c = 0xc0;
3629 else
3630 mb_utf8 = FALSE;
3631 #endif
3633 else
3635 c = *p_extra;
3636 #ifdef FEAT_MBYTE
3637 if (has_mbyte)
3639 mb_c = c;
3640 if (enc_utf8)
3642 /* If the UTF-8 character is more than one byte:
3643 * Decode it into "mb_c". */
3644 mb_l = (*mb_ptr2len)(p_extra);
3645 mb_utf8 = FALSE;
3646 if (mb_l > n_extra)
3647 mb_l = 1;
3648 else if (mb_l > 1)
3650 mb_c = utfc_ptr2char(p_extra, u8cc);
3651 mb_utf8 = TRUE;
3652 c = 0xc0;
3655 else
3657 /* if this is a DBCS character, put it in "mb_c" */
3658 mb_l = MB_BYTE2LEN(c);
3659 if (mb_l >= n_extra)
3660 mb_l = 1;
3661 else if (mb_l > 1)
3662 mb_c = (c << 8) + p_extra[1];
3664 if (mb_l == 0) /* at the NUL at end-of-line */
3665 mb_l = 1;
3667 /* If a double-width char doesn't fit display a '>' in the
3668 * last column. */
3669 if ((
3670 # ifdef FEAT_RIGHTLEFT
3671 wp->w_p_rl ? (col <= 0) :
3672 # endif
3673 (col >= W_WIDTH(wp) - 1))
3674 && (*mb_char2cells)(mb_c) == 2)
3676 c = '>';
3677 mb_c = c;
3678 mb_l = 1;
3679 mb_utf8 = FALSE;
3680 multi_attr = hl_attr(HLF_AT);
3681 /* put the pointer back to output the double-width
3682 * character at the start of the next line. */
3683 ++n_extra;
3684 --p_extra;
3686 else
3688 n_extra -= mb_l - 1;
3689 p_extra += mb_l - 1;
3692 #endif
3693 ++p_extra;
3695 --n_extra;
3697 else
3700 * Get a character from the line itself.
3702 c = *ptr;
3703 #ifdef FEAT_MBYTE
3704 if (has_mbyte)
3706 mb_c = c;
3707 if (enc_utf8)
3709 /* If the UTF-8 character is more than one byte: Decode it
3710 * into "mb_c". */
3711 mb_l = (*mb_ptr2len)(ptr);
3712 mb_utf8 = FALSE;
3713 if (mb_l > 1)
3715 mb_c = utfc_ptr2char(ptr, u8cc);
3716 /* Overlong encoded ASCII or ASCII with composing char
3717 * is displayed normally, except a NUL. */
3718 if (mb_c < 0x80)
3719 c = mb_c;
3720 mb_utf8 = TRUE;
3722 /* At start of the line we can have a composing char.
3723 * Draw it as a space with a composing char. */
3724 if (utf_iscomposing(mb_c))
3726 int i;
3728 for (i = Screen_mco - 1; i > 0; --i)
3729 u8cc[i] = u8cc[i - 1];
3730 u8cc[0] = mb_c;
3731 mb_c = ' ';
3735 if ((mb_l == 1 && c >= 0x80)
3736 || (mb_l >= 1 && mb_c == 0)
3737 || (mb_l > 1 && (!vim_isprintc(mb_c)
3738 # ifdef UNICODE16
3739 || mb_c >= 0x10000
3740 # endif
3744 * Illegal UTF-8 byte: display as <xx>.
3745 * Non-BMP character : display as ? or fullwidth ?.
3747 # ifdef UNICODE16
3748 if (mb_c < 0x10000)
3749 # endif
3751 transchar_hex(extra, mb_c);
3752 # ifdef FEAT_RIGHTLEFT
3753 if (wp->w_p_rl) /* reverse */
3754 rl_mirror(extra);
3755 # endif
3757 # ifdef UNICODE16
3758 else if (utf_char2cells(mb_c) != 2)
3759 STRCPY(extra, "?");
3760 else
3761 /* 0xff1f in UTF-8: full-width '?' */
3762 STRCPY(extra, "\357\274\237");
3763 # endif
3765 p_extra = extra;
3766 c = *p_extra;
3767 mb_c = mb_ptr2char_adv(&p_extra);
3768 mb_utf8 = (c >= 0x80);
3769 n_extra = (int)STRLEN(p_extra);
3770 c_extra = NUL;
3771 if (area_attr == 0 && search_attr == 0)
3773 n_attr = n_extra + 1;
3774 extra_attr = hl_attr(HLF_8);
3775 saved_attr2 = char_attr; /* save current attr */
3778 else if (mb_l == 0) /* at the NUL at end-of-line */
3779 mb_l = 1;
3780 #ifdef FEAT_ARABIC
3781 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
3783 /* Do Arabic shaping. */
3784 int pc, pc1, nc;
3785 int pcc[MAX_MCO];
3787 /* The idea of what is the previous and next
3788 * character depends on 'rightleft'. */
3789 if (wp->w_p_rl)
3791 pc = prev_c;
3792 pc1 = prev_c1;
3793 nc = utf_ptr2char(ptr + mb_l);
3794 prev_c1 = u8cc[0];
3796 else
3798 pc = utfc_ptr2char(ptr + mb_l, pcc);
3799 nc = prev_c;
3800 pc1 = pcc[0];
3802 prev_c = mb_c;
3804 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
3806 else
3807 prev_c = mb_c;
3808 #endif
3810 else /* enc_dbcs */
3812 mb_l = MB_BYTE2LEN(c);
3813 if (mb_l == 0) /* at the NUL at end-of-line */
3814 mb_l = 1;
3815 else if (mb_l > 1)
3817 /* We assume a second byte below 32 is illegal.
3818 * Hopefully this is OK for all double-byte encodings!
3820 if (ptr[1] >= 32)
3821 mb_c = (c << 8) + ptr[1];
3822 else
3824 if (ptr[1] == NUL)
3826 /* head byte at end of line */
3827 mb_l = 1;
3828 transchar_nonprint(extra, c);
3830 else
3832 /* illegal tail byte */
3833 mb_l = 2;
3834 STRCPY(extra, "XX");
3836 p_extra = extra;
3837 n_extra = (int)STRLEN(extra) - 1;
3838 c_extra = NUL;
3839 c = *p_extra++;
3840 if (area_attr == 0 && search_attr == 0)
3842 n_attr = n_extra + 1;
3843 extra_attr = hl_attr(HLF_8);
3844 saved_attr2 = char_attr; /* save current attr */
3846 mb_c = c;
3850 /* If a double-width char doesn't fit display a '>' in the
3851 * last column; the character is displayed at the start of the
3852 * next line. */
3853 if ((
3854 # ifdef FEAT_RIGHTLEFT
3855 wp->w_p_rl ? (col <= 0) :
3856 # endif
3857 (col >= W_WIDTH(wp) - 1))
3858 && (*mb_char2cells)(mb_c) == 2)
3860 c = '>';
3861 mb_c = c;
3862 mb_utf8 = FALSE;
3863 mb_l = 1;
3864 multi_attr = hl_attr(HLF_AT);
3865 /* Put pointer back so that the character will be
3866 * displayed at the start of the next line. */
3867 --ptr;
3869 else if (*ptr != NUL)
3870 ptr += mb_l - 1;
3872 /* If a double-width char doesn't fit at the left side display
3873 * a '<' in the first column. */
3874 if (n_skip > 0 && mb_l > 1)
3876 n_extra = 1;
3877 c_extra = '<';
3878 c = ' ';
3879 if (area_attr == 0 && search_attr == 0)
3881 n_attr = n_extra + 1;
3882 extra_attr = hl_attr(HLF_AT);
3883 saved_attr2 = char_attr; /* save current attr */
3885 mb_c = c;
3886 mb_utf8 = FALSE;
3887 mb_l = 1;
3891 #endif
3892 ++ptr;
3894 /* 'list' : change char 160 to lcs_nbsp. */
3895 if (wp->w_p_list && (c == 160
3896 #ifdef FEAT_MBYTE
3897 || (mb_utf8 && mb_c == 160)
3898 #endif
3899 ) && lcs_nbsp)
3901 c = lcs_nbsp;
3902 if (area_attr == 0 && search_attr == 0)
3904 n_attr = 1;
3905 extra_attr = hl_attr(HLF_8);
3906 saved_attr2 = char_attr; /* save current attr */
3908 #ifdef FEAT_MBYTE
3909 mb_c = c;
3910 if (enc_utf8 && (*mb_char2len)(c) > 1)
3912 mb_utf8 = TRUE;
3913 u8cc[0] = 0;
3914 c = 0xc0;
3916 else
3917 mb_utf8 = FALSE;
3918 #endif
3921 if (extra_check)
3923 #ifdef FEAT_SPELL
3924 int can_spell = TRUE;
3925 #endif
3927 #ifdef FEAT_SYN_HL
3928 /* Get syntax attribute, unless still at the start of the line
3929 * (double-wide char that doesn't fit). */
3930 v = (long)(ptr - line);
3931 if (has_syntax && v > 0)
3933 /* Get the syntax attribute for the character. If there
3934 * is an error, disable syntax highlighting. */
3935 save_did_emsg = did_emsg;
3936 did_emsg = FALSE;
3938 syntax_attr = get_syntax_attr((colnr_T)v - 1,
3939 # ifdef FEAT_SPELL
3940 has_spell ? &can_spell :
3941 # endif
3942 NULL, FALSE);
3944 if (did_emsg)
3946 wp->w_buffer->b_syn_error = TRUE;
3947 has_syntax = FALSE;
3949 else
3950 did_emsg = save_did_emsg;
3952 /* Need to get the line again, a multi-line regexp may
3953 * have made it invalid. */
3954 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3955 ptr = line + v;
3957 if (!attr_pri)
3958 char_attr = syntax_attr;
3959 else
3960 char_attr = hl_combine_attr(syntax_attr, char_attr);
3962 #endif
3964 #ifdef FEAT_SPELL
3965 /* Check spelling (unless at the end of the line).
3966 * Only do this when there is no syntax highlighting, the
3967 * @Spell cluster is not used or the current syntax item
3968 * contains the @Spell cluster. */
3969 if (has_spell && v >= word_end && v > cur_checked_col)
3971 spell_attr = 0;
3972 # ifdef FEAT_SYN_HL
3973 if (!attr_pri)
3974 char_attr = syntax_attr;
3975 # endif
3976 if (c != 0 && (
3977 # ifdef FEAT_SYN_HL
3978 !has_syntax ||
3979 # endif
3980 can_spell))
3982 char_u *prev_ptr, *p;
3983 int len;
3984 hlf_T spell_hlf = HLF_COUNT;
3985 # ifdef FEAT_MBYTE
3986 if (has_mbyte)
3988 prev_ptr = ptr - mb_l;
3989 v -= mb_l - 1;
3991 else
3992 # endif
3993 prev_ptr = ptr - 1;
3995 /* Use nextline[] if possible, it has the start of the
3996 * next line concatenated. */
3997 if ((prev_ptr - line) - nextlinecol >= 0)
3998 p = nextline + (prev_ptr - line) - nextlinecol;
3999 else
4000 p = prev_ptr;
4001 cap_col -= (int)(prev_ptr - line);
4002 len = spell_check(wp, p, &spell_hlf, &cap_col,
4003 nochange);
4004 word_end = v + len;
4006 /* In Insert mode only highlight a word that
4007 * doesn't touch the cursor. */
4008 if (spell_hlf != HLF_COUNT
4009 && (State & INSERT) != 0
4010 && wp->w_cursor.lnum == lnum
4011 && wp->w_cursor.col >=
4012 (colnr_T)(prev_ptr - line)
4013 && wp->w_cursor.col < (colnr_T)word_end)
4015 spell_hlf = HLF_COUNT;
4016 spell_redraw_lnum = lnum;
4019 if (spell_hlf == HLF_COUNT && p != prev_ptr
4020 && (p - nextline) + len > nextline_idx)
4022 /* Remember that the good word continues at the
4023 * start of the next line. */
4024 checked_lnum = lnum + 1;
4025 checked_col = (int)((p - nextline) + len - nextline_idx);
4028 /* Turn index into actual attributes. */
4029 if (spell_hlf != HLF_COUNT)
4030 spell_attr = highlight_attr[spell_hlf];
4032 if (cap_col > 0)
4034 if (p != prev_ptr
4035 && (p - nextline) + cap_col >= nextline_idx)
4037 /* Remember that the word in the next line
4038 * must start with a capital. */
4039 capcol_lnum = lnum + 1;
4040 cap_col = (int)((p - nextline) + cap_col
4041 - nextline_idx);
4043 else
4044 /* Compute the actual column. */
4045 cap_col += (int)(prev_ptr - line);
4049 if (spell_attr != 0)
4051 if (!attr_pri)
4052 char_attr = hl_combine_attr(char_attr, spell_attr);
4053 else
4054 char_attr = hl_combine_attr(spell_attr, char_attr);
4056 #endif
4057 #ifdef FEAT_LINEBREAK
4059 * Found last space before word: check for line break.
4061 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr)
4062 && !wp->w_p_list)
4064 n_extra = win_lbr_chartabsize(wp, ptr - (
4065 # ifdef FEAT_MBYTE
4066 has_mbyte ? mb_l :
4067 # endif
4068 1), (colnr_T)vcol, NULL) - 1;
4069 c_extra = ' ';
4070 if (vim_iswhite(c))
4071 c = ' ';
4073 #endif
4075 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4077 c = lcs_trail;
4078 if (!attr_pri)
4080 n_attr = 1;
4081 extra_attr = hl_attr(HLF_8);
4082 saved_attr2 = char_attr; /* save current attr */
4084 #ifdef FEAT_MBYTE
4085 mb_c = c;
4086 if (enc_utf8 && (*mb_char2len)(c) > 1)
4088 mb_utf8 = TRUE;
4089 u8cc[0] = 0;
4090 c = 0xc0;
4092 else
4093 mb_utf8 = FALSE;
4094 #endif
4099 * Handling of non-printable characters.
4101 if (!(chartab[c & 0xff] & CT_PRINT_CHAR))
4104 * when getting a character from the file, we may have to
4105 * turn it into something else on the way to putting it
4106 * into "ScreenLines".
4108 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4110 /* tab amount depends on current column */
4111 #ifdef FEAT_VARTABS
4112 n_extra = tabstop_padding(vcol, wp->w_buffer->b_p_ts,
4113 wp->w_buffer->b_p_vts_ary) - 1;
4114 #else
4115 n_extra = (int)wp->w_buffer->b_p_ts
4116 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4117 #endif
4118 #ifdef FEAT_MBYTE
4119 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4120 #endif
4121 if (wp->w_p_list)
4123 c = lcs_tab1;
4124 c_extra = lcs_tab2;
4125 n_attr = n_extra + 1;
4126 extra_attr = hl_attr(HLF_8);
4127 saved_attr2 = char_attr; /* save current attr */
4128 #ifdef FEAT_MBYTE
4129 mb_c = c;
4130 if (enc_utf8 && (*mb_char2len)(c) > 1)
4132 mb_utf8 = TRUE;
4133 u8cc[0] = 0;
4134 c = 0xc0;
4136 #endif
4138 else
4140 c_extra = ' ';
4141 c = ' ';
4144 else if (c == NUL
4145 && ((wp->w_p_list && lcs_eol > 0)
4146 || ((fromcol >= 0 || fromcol_prev >= 0)
4147 && tocol > vcol
4148 #ifdef FEAT_VISUAL
4149 && VIsual_mode != Ctrl_V
4150 #endif
4151 && (
4152 # ifdef FEAT_RIGHTLEFT
4153 wp->w_p_rl ? (col >= 0) :
4154 # endif
4155 (col < W_WIDTH(wp)))
4156 && !(noinvcur
4157 && lnum == wp->w_cursor.lnum
4158 && (colnr_T)vcol == wp->w_virtcol)))
4159 && lcs_eol_one >= 0)
4161 /* Display a '$' after the line or highlight an extra
4162 * character if the line break is included. */
4163 #if defined(FEAT_DIFF) || defined(LINE_ATTR)
4164 /* For a diff line the highlighting continues after the
4165 * "$". */
4166 if (
4167 # ifdef FEAT_DIFF
4168 diff_hlf == (hlf_T)0
4169 # ifdef LINE_ATTR
4171 # endif
4172 # endif
4173 # ifdef LINE_ATTR
4174 line_attr == 0
4175 # endif
4177 #endif
4179 #ifdef FEAT_VIRTUALEDIT
4180 /* In virtualedit, visual selections may extend
4181 * beyond end of line. */
4182 if (area_highlighting && virtual_active()
4183 && tocol != MAXCOL && vcol < tocol)
4184 n_extra = 0;
4185 else
4186 #endif
4188 p_extra = at_end_str;
4189 n_extra = 1;
4190 c_extra = NUL;
4193 if (wp->w_p_list)
4194 c = lcs_eol;
4195 else
4196 c = ' ';
4197 lcs_eol_one = -1;
4198 --ptr; /* put it back at the NUL */
4199 if (!attr_pri)
4201 extra_attr = hl_attr(HLF_AT);
4202 n_attr = 1;
4204 #ifdef FEAT_MBYTE
4205 mb_c = c;
4206 if (enc_utf8 && (*mb_char2len)(c) > 1)
4208 mb_utf8 = TRUE;
4209 u8cc[0] = 0;
4210 c = 0xc0;
4212 else
4213 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4214 #endif
4216 else if (c != NUL)
4218 p_extra = transchar(c);
4219 #ifdef FEAT_RIGHTLEFT
4220 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4221 rl_mirror(p_extra); /* reverse "<12>" */
4222 #endif
4223 n_extra = byte2cells(c) - 1;
4224 c_extra = NUL;
4225 c = *p_extra++;
4226 if (!attr_pri)
4228 n_attr = n_extra + 1;
4229 extra_attr = hl_attr(HLF_8);
4230 saved_attr2 = char_attr; /* save current attr */
4232 #ifdef FEAT_MBYTE
4233 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4234 #endif
4236 #ifdef FEAT_VIRTUALEDIT
4237 else if (VIsual_active
4238 && (VIsual_mode == Ctrl_V
4239 || VIsual_mode == 'v')
4240 && virtual_active()
4241 && tocol != MAXCOL
4242 && vcol < tocol
4243 && (
4244 # ifdef FEAT_RIGHTLEFT
4245 wp->w_p_rl ? (col >= 0) :
4246 # endif
4247 (col < W_WIDTH(wp))))
4249 c = ' ';
4250 --ptr; /* put it back at the NUL */
4252 #endif
4253 #if defined(LINE_ATTR)
4254 else if ((
4255 # ifdef FEAT_DIFF
4256 diff_hlf != (hlf_T)0 ||
4257 # endif
4258 line_attr != 0
4259 ) && (
4260 # ifdef FEAT_RIGHTLEFT
4261 wp->w_p_rl ? (col >= 0) :
4262 # endif
4263 (col < W_WIDTH(wp))))
4265 /* Highlight until the right side of the window */
4266 c = ' ';
4267 --ptr; /* put it back at the NUL */
4269 /* Remember we do the char for line highlighting. */
4270 ++did_line_attr;
4272 /* don't do search HL for the rest of the line */
4273 if (line_attr != 0 && char_attr == search_attr && col > 0)
4274 char_attr = line_attr;
4275 # ifdef FEAT_DIFF
4276 if (diff_hlf == HLF_TXD)
4278 diff_hlf = HLF_CHD;
4279 if (attr == 0 || char_attr != attr)
4280 char_attr = hl_attr(diff_hlf);
4282 # endif
4284 #endif
4288 /* Don't override visual selection highlighting. */
4289 if (n_attr > 0
4290 && draw_state == WL_LINE
4291 && !attr_pri)
4292 char_attr = extra_attr;
4294 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
4295 /* XIM don't send preedit_start and preedit_end, but they send
4296 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
4297 * im_is_preediting() here. */
4298 if (xic != NULL
4299 && lnum == wp->w_cursor.lnum
4300 && (State & INSERT)
4301 && !p_imdisable
4302 && im_is_preediting()
4303 && draw_state == WL_LINE)
4305 colnr_T tcol;
4307 if (preedit_end_col == MAXCOL)
4308 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
4309 else
4310 tcol = preedit_end_col;
4311 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
4313 if (feedback_old_attr < 0)
4315 feedback_col = 0;
4316 feedback_old_attr = char_attr;
4318 char_attr = im_get_feedback_attr(feedback_col);
4319 if (char_attr < 0)
4320 char_attr = feedback_old_attr;
4321 feedback_col++;
4323 else if (feedback_old_attr >= 0)
4325 char_attr = feedback_old_attr;
4326 feedback_old_attr = -1;
4327 feedback_col = 0;
4330 #endif
4332 * Handle the case where we are in column 0 but not on the first
4333 * character of the line and the user wants us to show us a
4334 * special character (via 'listchars' option "precedes:<char>".
4336 if (lcs_prec_todo != NUL
4337 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
4338 #ifdef FEAT_DIFF
4339 && filler_todo <= 0
4340 #endif
4341 && draw_state > WL_NR
4342 && c != NUL)
4344 c = lcs_prec;
4345 lcs_prec_todo = NUL;
4346 #ifdef FEAT_MBYTE
4347 mb_c = c;
4348 if (enc_utf8 && (*mb_char2len)(c) > 1)
4350 mb_utf8 = TRUE;
4351 u8cc[0] = 0;
4352 c = 0xc0;
4354 else
4355 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4356 #endif
4357 if (!attr_pri)
4359 saved_attr3 = char_attr; /* save current attr */
4360 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
4361 n_attr3 = 1;
4366 * At end of the text line or just after the last character.
4368 if (c == NUL
4369 #if defined(LINE_ATTR)
4370 || did_line_attr == 1
4371 #endif
4374 #ifdef FEAT_SEARCH_EXTRA
4375 long prevcol = (long)(ptr - line) - (c == NUL);
4377 /* we're not really at that column when skipping some text */
4378 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
4379 ++prevcol;
4380 #endif
4382 /* invert at least one char, used for Visual and empty line or
4383 * highlight match at end of line. If it's beyond the last
4384 * char on the screen, just overwrite that one (tricky!) Not
4385 * needed when a '$' was displayed for 'list'. */
4386 #ifdef FEAT_SEARCH_EXTRA
4387 prevcol_hl_flag = FALSE;
4388 if (prevcol == (long)search_hl.startcol)
4389 prevcol_hl_flag = TRUE;
4390 else
4392 cur = wp->w_match_head;
4393 while (cur != NULL)
4395 if (prevcol == (long)cur->hl.startcol)
4397 prevcol_hl_flag = TRUE;
4398 break;
4400 cur = cur->next;
4403 #endif
4404 if (lcs_eol == lcs_eol_one
4405 && ((area_attr != 0 && vcol == fromcol
4406 #ifdef FEAT_VISUAL
4407 && (VIsual_mode != Ctrl_V
4408 || lnum == VIsual.lnum
4409 || lnum == curwin->w_cursor.lnum)
4410 #endif
4411 && c == NUL)
4412 #ifdef FEAT_SEARCH_EXTRA
4413 /* highlight 'hlsearch' match at end of line */
4414 || (prevcol_hl_flag == TRUE
4415 # if defined(LINE_ATTR)
4416 && did_line_attr <= 1
4417 # endif
4419 #endif
4422 int n = 0;
4424 #ifdef FEAT_RIGHTLEFT
4425 if (wp->w_p_rl)
4427 if (col < 0)
4428 n = 1;
4430 else
4431 #endif
4433 if (col >= W_WIDTH(wp))
4434 n = -1;
4436 if (n != 0)
4438 /* At the window boundary, highlight the last character
4439 * instead (better than nothing). */
4440 off += n;
4441 col += n;
4443 else
4445 /* Add a blank character to highlight. */
4446 ScreenLines[off] = ' ';
4447 #ifdef FEAT_MBYTE
4448 if (enc_utf8)
4449 ScreenLinesUC[off] = 0;
4450 #endif
4452 #ifdef FEAT_SEARCH_EXTRA
4453 if (area_attr == 0)
4455 /* Use attributes from match with highest priority among
4456 * 'search_hl' and the match list. */
4457 char_attr = search_hl.attr;
4458 cur = wp->w_match_head;
4459 shl_flag = FALSE;
4460 while (cur != NULL || shl_flag == FALSE)
4462 if (shl_flag == FALSE
4463 && ((cur != NULL
4464 && cur->priority > SEARCH_HL_PRIORITY)
4465 || cur == NULL))
4467 shl = &search_hl;
4468 shl_flag = TRUE;
4470 else
4471 shl = &cur->hl;
4472 if ((ptr - line) - 1 == (long)shl->startcol)
4473 char_attr = shl->attr;
4474 if (shl != &search_hl && cur != NULL)
4475 cur = cur->next;
4478 #endif
4479 ScreenAttrs[off] = char_attr;
4480 #ifdef FEAT_RIGHTLEFT
4481 if (wp->w_p_rl)
4483 --col;
4484 --off;
4486 else
4487 #endif
4489 ++col;
4490 ++off;
4492 ++vcol;
4493 #ifdef FEAT_SYN_HL
4494 eol_hl_off = 1;
4495 #endif
4500 * At end of the text line.
4502 if (c == NUL)
4504 #ifdef FEAT_SYN_HL
4505 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
4506 && lnum == wp->w_cursor.lnum)
4508 /* highlight last char after line */
4509 --col;
4510 --off;
4511 --vcol;
4514 /* Highlight 'cursorcolumn' past end of the line. */
4515 if (wp->w_p_wrap)
4516 v = wp->w_skipcol;
4517 else
4518 v = wp->w_leftcol;
4519 /* check if line ends before left margin */
4520 if (vcol < v + col - win_col_off(wp))
4522 vcol = v + col - win_col_off(wp);
4523 if (wp->w_p_cuc
4524 && (int)wp->w_virtcol >= vcol - eol_hl_off
4525 && (int)wp->w_virtcol < W_WIDTH(wp) * (row - startrow + 1)
4527 && lnum != wp->w_cursor.lnum
4528 # ifdef FEAT_RIGHTLEFT
4529 && !wp->w_p_rl
4530 # endif
4533 while (col < W_WIDTH(wp))
4535 ScreenLines[off] = ' ';
4536 #ifdef FEAT_MBYTE
4537 if (enc_utf8)
4538 ScreenLinesUC[off] = 0;
4539 #endif
4540 ++col;
4541 if (vcol == (long)wp->w_virtcol)
4543 ScreenAttrs[off] = hl_attr(HLF_CUC);
4544 break;
4546 ScreenAttrs[off++] = 0;
4547 ++vcol;
4550 #endif
4552 SCREEN_LINE(screen_row, W_WINCOL(wp), col, (int)W_WIDTH(wp),
4553 wp->w_p_rl);
4554 row++;
4557 * Update w_cline_height and w_cline_folded if the cursor line was
4558 * updated (saves a call to plines() later).
4560 if (wp == curwin && lnum == curwin->w_cursor.lnum)
4562 curwin->w_cline_row = startrow;
4563 curwin->w_cline_height = row - startrow;
4564 #ifdef FEAT_FOLDING
4565 curwin->w_cline_folded = FALSE;
4566 #endif
4567 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
4570 break;
4573 /* line continues beyond line end */
4574 if (lcs_ext
4575 && !wp->w_p_wrap
4576 #ifdef FEAT_DIFF
4577 && filler_todo <= 0
4578 #endif
4579 && (
4580 #ifdef FEAT_RIGHTLEFT
4581 wp->w_p_rl ? col == 0 :
4582 #endif
4583 col == W_WIDTH(wp) - 1)
4584 && (*ptr != NUL
4585 || (wp->w_p_list && lcs_eol_one > 0)
4586 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
4588 c = lcs_ext;
4589 char_attr = hl_attr(HLF_AT);
4590 #ifdef FEAT_MBYTE
4591 mb_c = c;
4592 if (enc_utf8 && (*mb_char2len)(c) > 1)
4594 mb_utf8 = TRUE;
4595 u8cc[0] = 0;
4596 c = 0xc0;
4598 else
4599 mb_utf8 = FALSE;
4600 #endif
4603 #ifdef FEAT_SYN_HL
4604 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
4605 * highlight the cursor position itself. */
4606 if (wp->w_p_cuc && vcol == (long)wp->w_virtcol
4607 && lnum != wp->w_cursor.lnum
4608 && draw_state == WL_LINE
4609 && !lnum_in_visual_area)
4611 vcol_save_attr = char_attr;
4612 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
4614 else
4615 vcol_save_attr = -1;
4616 #endif
4619 * Store character to be displayed.
4620 * Skip characters that are left of the screen for 'nowrap'.
4622 vcol_prev = vcol;
4623 if (draw_state < WL_LINE || n_skip <= 0)
4626 * Store the character.
4628 #if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
4629 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
4631 /* A double-wide character is: put first halve in left cell. */
4632 --off;
4633 --col;
4635 #endif
4636 ScreenLines[off] = c;
4637 #ifdef FEAT_MBYTE
4638 if (enc_dbcs == DBCS_JPNU)
4639 ScreenLines2[off] = mb_c & 0xff;
4640 else if (enc_utf8)
4642 if (mb_utf8)
4644 int i;
4646 ScreenLinesUC[off] = mb_c;
4647 if ((c & 0xff) == 0)
4648 ScreenLines[off] = 0x80; /* avoid storing zero */
4649 for (i = 0; i < Screen_mco; ++i)
4651 ScreenLinesC[i][off] = u8cc[i];
4652 if (u8cc[i] == 0)
4653 break;
4656 else
4657 ScreenLinesUC[off] = 0;
4659 if (multi_attr)
4661 ScreenAttrs[off] = multi_attr;
4662 multi_attr = 0;
4664 else
4665 #endif
4666 ScreenAttrs[off] = char_attr;
4668 #ifdef FEAT_MBYTE
4669 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
4671 /* Need to fill two screen columns. */
4672 ++off;
4673 ++col;
4674 if (enc_utf8)
4675 /* UTF-8: Put a 0 in the second screen char. */
4676 ScreenLines[off] = 0;
4677 else
4678 /* DBCS: Put second byte in the second screen char. */
4679 ScreenLines[off] = mb_c & 0xff;
4680 ++vcol;
4681 /* When "tocol" is halfway a character, set it to the end of
4682 * the character, otherwise highlighting won't stop. */
4683 if (tocol == vcol)
4684 ++tocol;
4685 #ifdef FEAT_RIGHTLEFT
4686 if (wp->w_p_rl)
4688 /* now it's time to backup one cell */
4689 --off;
4690 --col;
4692 #endif
4694 #endif
4695 #ifdef FEAT_RIGHTLEFT
4696 if (wp->w_p_rl)
4698 --off;
4699 --col;
4701 else
4702 #endif
4704 ++off;
4705 ++col;
4708 else
4709 --n_skip;
4711 /* Only advance the "vcol" when after the 'number' column. */
4712 if (draw_state > WL_NR
4713 #ifdef FEAT_DIFF
4714 && filler_todo <= 0
4715 #endif
4717 ++vcol;
4719 #ifdef FEAT_SYN_HL
4720 if (vcol_save_attr >= 0)
4721 char_attr = vcol_save_attr;
4722 #endif
4724 /* restore attributes after "predeces" in 'listchars' */
4725 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
4726 char_attr = saved_attr3;
4728 /* restore attributes after last 'listchars' or 'number' char */
4729 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
4730 char_attr = saved_attr2;
4733 * At end of screen line and there is more to come: Display the line
4734 * so far. If there is no more to display it is caught above.
4736 if ((
4737 #ifdef FEAT_RIGHTLEFT
4738 wp->w_p_rl ? (col < 0) :
4739 #endif
4740 (col >= W_WIDTH(wp)))
4741 && (*ptr != NUL
4742 #ifdef FEAT_DIFF
4743 || filler_todo > 0
4744 #endif
4745 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
4746 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
4749 SCREEN_LINE(screen_row, W_WINCOL(wp), col, (int)W_WIDTH(wp),
4750 wp->w_p_rl);
4751 ++row;
4752 ++screen_row;
4754 /* When not wrapping and finished diff lines, or when displayed
4755 * '$' and highlighting until last column, break here. */
4756 if ((!wp->w_p_wrap
4757 #ifdef FEAT_DIFF
4758 && filler_todo <= 0
4759 #endif
4760 ) || lcs_eol_one == -1)
4761 break;
4763 /* When the window is too narrow draw all "@" lines. */
4764 if (draw_state != WL_LINE
4765 #ifdef FEAT_DIFF
4766 && filler_todo <= 0
4767 #endif
4770 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
4771 #ifdef FEAT_VERTSPLIT
4772 draw_vsep_win(wp, row);
4773 #endif
4774 row = endrow;
4777 /* When line got too long for screen break here. */
4778 if (row == endrow)
4780 ++row;
4781 break;
4784 if (screen_cur_row == screen_row - 1
4785 #ifdef FEAT_DIFF
4786 && filler_todo <= 0
4787 #endif
4788 && W_WIDTH(wp) == Columns)
4790 /* Remember that the line wraps, used for modeless copy. */
4791 LineWraps[screen_row - 1] = TRUE;
4794 * Special trick to make copy/paste of wrapped lines work with
4795 * xterm/screen: write an extra character beyond the end of
4796 * the line. This will work with all terminal types
4797 * (regardless of the xn,am settings).
4798 * Only do this on a fast tty.
4799 * Only do this if the cursor is on the current line
4800 * (something has been written in it).
4801 * Don't do this for the GUI.
4802 * Don't do this for double-width characters.
4803 * Don't do this for a window not at the right screen border.
4805 if (p_tf
4806 #ifdef FEAT_GUI
4807 && !gui.in_use
4808 #endif
4809 #ifdef FEAT_MBYTE
4810 && !(has_mbyte
4811 && ((*mb_off2cells)(LineOffset[screen_row],
4812 LineOffset[screen_row] + screen_Columns)
4813 == 2
4814 || (*mb_off2cells)(LineOffset[screen_row - 1]
4815 + (int)Columns - 2,
4816 LineOffset[screen_row] + screen_Columns)
4817 == 2))
4818 #endif
4821 /* First make sure we are at the end of the screen line,
4822 * then output the same character again to let the
4823 * terminal know about the wrap. If the terminal doesn't
4824 * auto-wrap, we overwrite the character. */
4825 if (screen_cur_col != W_WIDTH(wp))
4826 screen_char(LineOffset[screen_row - 1]
4827 + (unsigned)Columns - 1,
4828 screen_row - 1, (int)(Columns - 1));
4830 #ifdef FEAT_MBYTE
4831 /* When there is a multi-byte character, just output a
4832 * space to keep it simple. */
4833 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
4834 screen_row - 1] + (Columns - 1)]) > 1)
4835 out_char(' ');
4836 else
4837 #endif
4838 out_char(ScreenLines[LineOffset[screen_row - 1]
4839 + (Columns - 1)]);
4840 /* force a redraw of the first char on the next line */
4841 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
4842 screen_start(); /* don't know where cursor is now */
4846 col = 0;
4847 off = (unsigned)(current_ScreenLine - ScreenLines);
4848 #ifdef FEAT_RIGHTLEFT
4849 if (wp->w_p_rl)
4851 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
4852 off += col;
4854 #endif
4856 /* reset the drawing state for the start of a wrapped line */
4857 draw_state = WL_START;
4858 saved_n_extra = n_extra;
4859 saved_p_extra = p_extra;
4860 saved_c_extra = c_extra;
4861 saved_char_attr = char_attr;
4862 n_extra = 0;
4863 lcs_prec_todo = lcs_prec;
4864 #ifdef FEAT_LINEBREAK
4865 # ifdef FEAT_DIFF
4866 if (filler_todo <= 0)
4867 # endif
4868 need_showbreak = TRUE;
4869 #endif
4870 #ifdef FEAT_DIFF
4871 --filler_todo;
4872 /* When the filler lines are actually below the last line of the
4873 * file, don't draw the line itself, break here. */
4874 if (filler_todo == 0 && wp->w_botfill)
4875 break;
4876 #endif
4879 } /* for every character in the line */
4881 #ifdef FEAT_SPELL
4882 /* After an empty line check first word for capital. */
4883 if (*skipwhite(line) == NUL)
4885 capcol_lnum = lnum + 1;
4886 cap_col = 0;
4888 #endif
4890 return row;
4893 #ifdef FEAT_MBYTE
4894 static int comp_char_differs __ARGS((int, int));
4897 * Return if the composing characters at "off_from" and "off_to" differ.
4899 static int
4900 comp_char_differs(off_from, off_to)
4901 int off_from;
4902 int off_to;
4904 int i;
4906 for (i = 0; i < Screen_mco; ++i)
4908 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
4909 return TRUE;
4910 if (ScreenLinesC[i][off_from] == 0)
4911 break;
4913 return FALSE;
4915 #endif
4918 * Check whether the given character needs redrawing:
4919 * - the (first byte of the) character is different
4920 * - the attributes are different
4921 * - the character is multi-byte and the next byte is different
4922 * - the character is two cells wide and the second cell differs.
4924 static int
4925 char_needs_redraw(off_from, off_to, cols)
4926 int off_from;
4927 int off_to;
4928 int cols;
4930 if (cols > 0
4931 && ((ScreenLines[off_from] != ScreenLines[off_to]
4932 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
4934 #ifdef FEAT_MBYTE
4935 || (enc_dbcs != 0
4936 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
4937 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
4938 ? ScreenLines2[off_from] != ScreenLines2[off_to]
4939 : (cols > 1 && ScreenLines[off_from + 1]
4940 != ScreenLines[off_to + 1])))
4941 || (enc_utf8
4942 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
4943 || (ScreenLinesUC[off_from] != 0
4944 && comp_char_differs(off_from, off_to))
4945 || (cols > 1 && ScreenLines[off_from + 1]
4946 != ScreenLines[off_to + 1])))
4947 #endif
4949 return TRUE;
4950 return FALSE;
4954 * Move one "cooked" screen line to the screen, but only the characters that
4955 * have actually changed. Handle insert/delete character.
4956 * "coloff" gives the first column on the screen for this line.
4957 * "endcol" gives the columns where valid characters are.
4958 * "clear_width" is the width of the window. It's > 0 if the rest of the line
4959 * needs to be cleared, negative otherwise.
4960 * "rlflag" is TRUE in a rightleft window:
4961 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
4962 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
4964 static void
4965 screen_line(row, coloff, endcol, clear_width
4966 #ifdef FEAT_RIGHTLEFT
4967 , rlflag
4968 #endif
4970 int row;
4971 int coloff;
4972 int endcol;
4973 int clear_width;
4974 #ifdef FEAT_RIGHTLEFT
4975 int rlflag;
4976 #endif
4978 unsigned off_from;
4979 unsigned off_to;
4980 #ifdef FEAT_MBYTE
4981 unsigned max_off_from;
4982 unsigned max_off_to;
4983 #endif
4984 int col = 0;
4985 #if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_VERTSPLIT)
4986 int hl;
4987 #endif
4988 int force = FALSE; /* force update rest of the line */
4989 int redraw_this /* bool: does character need redraw? */
4990 #ifdef FEAT_GUI
4991 = TRUE /* For GUI when while-loop empty */
4992 #endif
4994 int redraw_next; /* redraw_this for next character */
4995 #ifdef FEAT_MBYTE
4996 int clear_next = FALSE;
4997 int char_cells; /* 1: normal char */
4998 /* 2: occupies two display cells */
4999 # define CHAR_CELLS char_cells
5000 #else
5001 # define CHAR_CELLS 1
5002 #endif
5004 # ifdef FEAT_CLIPBOARD
5005 clip_may_clear_selection(row, row);
5006 # endif
5008 off_from = (unsigned)(current_ScreenLine - ScreenLines);
5009 off_to = LineOffset[row] + coloff;
5010 #ifdef FEAT_MBYTE
5011 max_off_from = off_from + screen_Columns;
5012 max_off_to = LineOffset[row] + screen_Columns;
5013 #endif
5015 #ifdef FEAT_RIGHTLEFT
5016 if (rlflag)
5018 /* Clear rest first, because it's left of the text. */
5019 if (clear_width > 0)
5021 while (col <= endcol && ScreenLines[off_to] == ' '
5022 && ScreenAttrs[off_to] == 0
5023 # ifdef FEAT_MBYTE
5024 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5025 # endif
5028 ++off_to;
5029 ++col;
5031 if (col <= endcol)
5032 screen_fill(row, row + 1, col + coloff,
5033 endcol + coloff + 1, ' ', ' ', 0);
5035 col = endcol + 1;
5036 off_to = LineOffset[row] + col + coloff;
5037 off_from += col;
5038 endcol = (clear_width > 0 ? clear_width : -clear_width);
5040 #endif /* FEAT_RIGHTLEFT */
5042 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
5044 while (col < endcol)
5046 #ifdef FEAT_MBYTE
5047 if (has_mbyte && (col + 1 < endcol))
5048 char_cells = (*mb_off2cells)(off_from, max_off_from);
5049 else
5050 char_cells = 1;
5051 #endif
5053 redraw_this = redraw_next;
5054 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
5055 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
5057 #ifdef FEAT_GUI
5058 /* If the next character was bold, then redraw the current character to
5059 * remove any pixels that might have spilt over into us. This only
5060 * happens in the GUI.
5062 if (redraw_next && gui.in_use)
5064 hl = ScreenAttrs[off_to + CHAR_CELLS];
5065 if (hl > HL_ALL)
5066 hl = syn_attr2attr(hl);
5067 if (hl & HL_BOLD)
5068 redraw_this = TRUE;
5070 #endif
5072 if (redraw_this)
5075 * Special handling when 'xs' termcap flag set (hpterm):
5076 * Attributes for characters are stored at the position where the
5077 * cursor is when writing the highlighting code. The
5078 * start-highlighting code must be written with the cursor on the
5079 * first highlighted character. The stop-highlighting code must
5080 * be written with the cursor just after the last highlighted
5081 * character.
5082 * Overwriting a character doesn't remove it's highlighting. Need
5083 * to clear the rest of the line, and force redrawing it
5084 * completely.
5086 if ( p_wiv
5087 && !force
5088 #ifdef FEAT_GUI
5089 && !gui.in_use
5090 #endif
5091 && ScreenAttrs[off_to] != 0
5092 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
5095 * Need to remove highlighting attributes here.
5097 windgoto(row, col + coloff);
5098 out_str(T_CE); /* clear rest of this screen line */
5099 screen_start(); /* don't know where cursor is now */
5100 force = TRUE; /* force redraw of rest of the line */
5101 redraw_next = TRUE; /* or else next char would miss out */
5104 * If the previous character was highlighted, need to stop
5105 * highlighting at this character.
5107 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
5109 screen_attr = ScreenAttrs[off_to - 1];
5110 term_windgoto(row, col + coloff);
5111 screen_stop_highlight();
5113 else
5114 screen_attr = 0; /* highlighting has stopped */
5116 #ifdef FEAT_MBYTE
5117 if (enc_dbcs != 0)
5119 /* Check if overwriting a double-byte with a single-byte or
5120 * the other way around requires another character to be
5121 * redrawn. For UTF-8 this isn't needed, because comparing
5122 * ScreenLinesUC[] is sufficient. */
5123 if (char_cells == 1
5124 && col + 1 < endcol
5125 && (*mb_off2cells)(off_to, max_off_to) > 1)
5127 /* Writing a single-cell character over a double-cell
5128 * character: need to redraw the next cell. */
5129 ScreenLines[off_to + 1] = 0;
5130 redraw_next = TRUE;
5132 else if (char_cells == 2
5133 && col + 2 < endcol
5134 && (*mb_off2cells)(off_to, max_off_to) == 1
5135 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
5137 /* Writing the second half of a double-cell character over
5138 * a double-cell character: need to redraw the second
5139 * cell. */
5140 ScreenLines[off_to + 2] = 0;
5141 redraw_next = TRUE;
5144 if (enc_dbcs == DBCS_JPNU)
5145 ScreenLines2[off_to] = ScreenLines2[off_from];
5147 /* When writing a single-width character over a double-width
5148 * character and at the end of the redrawn text, need to clear out
5149 * the right halve of the old character.
5150 * Also required when writing the right halve of a double-width
5151 * char over the left halve of an existing one. */
5152 if (has_mbyte && col + char_cells == endcol
5153 && ((char_cells == 1
5154 && (*mb_off2cells)(off_to, max_off_to) > 1)
5155 || (char_cells == 2
5156 && (*mb_off2cells)(off_to, max_off_to) == 1
5157 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
5158 clear_next = TRUE;
5159 #endif
5161 ScreenLines[off_to] = ScreenLines[off_from];
5162 #ifdef FEAT_MBYTE
5163 if (enc_utf8)
5165 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
5166 if (ScreenLinesUC[off_from] != 0)
5168 int i;
5170 for (i = 0; i < Screen_mco; ++i)
5171 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
5174 if (char_cells == 2)
5175 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
5176 #endif
5178 #if defined(FEAT_GUI) || defined(UNIX)
5179 /* The bold trick makes a single column of pixels appear in the
5180 * next character. When a bold character is removed, the next
5181 * character should be redrawn too. This happens for our own GUI
5182 * and for some xterms. */
5183 if (
5184 # ifdef FEAT_GUI
5185 gui.in_use
5186 # endif
5187 # if defined(FEAT_GUI) && defined(UNIX)
5189 # endif
5190 # ifdef UNIX
5191 term_is_xterm
5192 # endif
5195 hl = ScreenAttrs[off_to];
5196 if (hl > HL_ALL)
5197 hl = syn_attr2attr(hl);
5198 if (hl & HL_BOLD)
5199 redraw_next = TRUE;
5201 #endif
5202 ScreenAttrs[off_to] = ScreenAttrs[off_from];
5203 #ifdef FEAT_MBYTE
5204 /* For simplicity set the attributes of second half of a
5205 * double-wide character equal to the first half. */
5206 if (char_cells == 2)
5207 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
5209 if (enc_dbcs != 0 && char_cells == 2)
5210 screen_char_2(off_to, row, col + coloff);
5211 else
5212 #endif
5213 screen_char(off_to, row, col + coloff);
5215 else if ( p_wiv
5216 #ifdef FEAT_GUI
5217 && !gui.in_use
5218 #endif
5219 && col + coloff > 0)
5221 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
5224 * Don't output stop-highlight when moving the cursor, it will
5225 * stop the highlighting when it should continue.
5227 screen_attr = 0;
5229 else if (screen_attr != 0)
5230 screen_stop_highlight();
5233 off_to += CHAR_CELLS;
5234 off_from += CHAR_CELLS;
5235 col += CHAR_CELLS;
5238 #ifdef FEAT_MBYTE
5239 if (clear_next)
5241 /* Clear the second half of a double-wide character of which the left
5242 * half was overwritten with a single-wide character. */
5243 ScreenLines[off_to] = ' ';
5244 if (enc_utf8)
5245 ScreenLinesUC[off_to] = 0;
5246 screen_char(off_to, row, col + coloff);
5248 #endif
5250 if (clear_width > 0
5251 #ifdef FEAT_RIGHTLEFT
5252 && !rlflag
5253 #endif
5256 #ifdef FEAT_GUI
5257 int startCol = col;
5258 #endif
5260 /* blank out the rest of the line */
5261 while (col < clear_width && ScreenLines[off_to] == ' '
5262 && ScreenAttrs[off_to] == 0
5263 #ifdef FEAT_MBYTE
5264 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5265 #endif
5268 ++off_to;
5269 ++col;
5271 if (col < clear_width)
5273 #ifdef FEAT_GUI
5275 * In the GUI, clearing the rest of the line may leave pixels
5276 * behind if the first character cleared was bold. Some bold
5277 * fonts spill over the left. In this case we redraw the previous
5278 * character too. If we didn't skip any blanks above, then we
5279 * only redraw if the character wasn't already redrawn anyway.
5281 if (gui.in_use && (col > startCol || !redraw_this))
5283 hl = ScreenAttrs[off_to];
5284 if (hl > HL_ALL || (hl & HL_BOLD))
5286 int prev_cells = 1;
5287 # ifdef FEAT_MBYTE
5288 if (enc_utf8)
5289 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
5290 * that its width is 2. */
5291 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
5292 else if (enc_dbcs != 0)
5294 /* find previous character by counting from first
5295 * column and get its width. */
5296 unsigned off = LineOffset[row];
5297 unsigned max_off = LineOffset[row] + screen_Columns;
5299 while (off < off_to)
5301 prev_cells = (*mb_off2cells)(off, max_off);
5302 off += prev_cells;
5306 if (enc_dbcs != 0 && prev_cells > 1)
5307 screen_char_2(off_to - prev_cells, row,
5308 col + coloff - prev_cells);
5309 else
5310 # endif
5311 screen_char(off_to - prev_cells, row,
5312 col + coloff - prev_cells);
5315 #endif
5316 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
5317 ' ', ' ', 0);
5318 #ifdef FEAT_VERTSPLIT
5319 off_to += clear_width - col;
5320 col = clear_width;
5321 #endif
5325 if (clear_width > 0)
5327 #ifdef FEAT_VERTSPLIT
5328 /* For a window that's left of another, draw the separator char. */
5329 if (col + coloff < Columns)
5331 int c;
5333 c = fillchar_vsep(&hl);
5334 if (ScreenLines[off_to] != c
5335 # ifdef FEAT_MBYTE
5336 || (enc_utf8 && (int)ScreenLinesUC[off_to]
5337 != (c >= 0x80 ? c : 0))
5338 # endif
5339 || ScreenAttrs[off_to] != hl)
5341 ScreenLines[off_to] = c;
5342 ScreenAttrs[off_to] = hl;
5343 # ifdef FEAT_MBYTE
5344 if (enc_utf8)
5346 if (c >= 0x80)
5348 ScreenLinesUC[off_to] = c;
5349 ScreenLinesC[0][off_to] = 0;
5351 else
5352 ScreenLinesUC[off_to] = 0;
5354 # endif
5355 screen_char(off_to, row, col + coloff);
5358 else
5359 #endif
5360 LineWraps[row] = FALSE;
5364 #if defined(FEAT_RIGHTLEFT) || defined(PROTO)
5366 * Mirror text "str" for right-left displaying.
5367 * Only works for single-byte characters (e.g., numbers).
5369 void
5370 rl_mirror(str)
5371 char_u *str;
5373 char_u *p1, *p2;
5374 int t;
5376 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
5378 t = *p1;
5379 *p1 = *p2;
5380 *p2 = t;
5383 #endif
5385 #if defined(FEAT_WINDOWS) || defined(PROTO)
5387 * mark all status lines for redraw; used after first :cd
5389 void
5390 status_redraw_all()
5392 win_T *wp;
5394 for (wp = firstwin; wp; wp = wp->w_next)
5395 if (wp->w_status_height)
5397 wp->w_redr_status = TRUE;
5398 redraw_later(VALID);
5403 * mark all status lines of the current buffer for redraw
5405 void
5406 status_redraw_curbuf()
5408 win_T *wp;
5410 for (wp = firstwin; wp; wp = wp->w_next)
5411 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
5413 wp->w_redr_status = TRUE;
5414 redraw_later(VALID);
5419 * Redraw all status lines that need to be redrawn.
5421 void
5422 redraw_statuslines()
5424 win_T *wp;
5426 for (wp = firstwin; wp; wp = wp->w_next)
5427 if (wp->w_redr_status)
5428 win_redr_status(wp);
5429 if (redraw_tabline)
5430 draw_tabline();
5432 #endif
5434 #if (defined(FEAT_WILDMENU) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
5436 * Redraw all status lines at the bottom of frame "frp".
5438 void
5439 win_redraw_last_status(frp)
5440 frame_T *frp;
5442 if (frp->fr_layout == FR_LEAF)
5443 frp->fr_win->w_redr_status = TRUE;
5444 else if (frp->fr_layout == FR_ROW)
5446 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
5447 win_redraw_last_status(frp);
5449 else /* frp->fr_layout == FR_COL */
5451 frp = frp->fr_child;
5452 while (frp->fr_next != NULL)
5453 frp = frp->fr_next;
5454 win_redraw_last_status(frp);
5457 #endif
5459 #ifdef FEAT_VERTSPLIT
5461 * Draw the verticap separator right of window "wp" starting with line "row".
5463 static void
5464 draw_vsep_win(wp, row)
5465 win_T *wp;
5466 int row;
5468 int hl;
5469 int c;
5471 if (wp->w_vsep_width)
5473 /* draw the vertical separator right of this window */
5474 c = fillchar_vsep(&hl);
5475 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
5476 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
5477 c, ' ', hl);
5480 #endif
5482 #ifdef FEAT_WILDMENU
5483 static int status_match_len __ARGS((expand_T *xp, char_u *s));
5484 static int skip_status_match_char __ARGS((expand_T *xp, char_u *s));
5487 * Get the length of an item as it will be shown in the status line.
5489 static int
5490 status_match_len(xp, s)
5491 expand_T *xp;
5492 char_u *s;
5494 int len = 0;
5496 #ifdef FEAT_MENU
5497 int emenu = (xp->xp_context == EXPAND_MENUS
5498 || xp->xp_context == EXPAND_MENUNAMES);
5500 /* Check for menu separators - replace with '|'. */
5501 if (emenu && menu_is_separator(s))
5502 return 1;
5503 #endif
5505 while (*s != NUL)
5507 s += skip_status_match_char(xp, s);
5508 len += ptr2cells(s);
5509 mb_ptr_adv(s);
5512 return len;
5516 * Return the number of characters that should be skipped in a status match.
5517 * These are backslashes used for escaping. Do show backslashes in help tags.
5519 static int
5520 skip_status_match_char(xp, s)
5521 expand_T *xp;
5522 char_u *s;
5524 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
5525 #ifdef FEAT_MENU
5526 || ((xp->xp_context == EXPAND_MENUS
5527 || xp->xp_context == EXPAND_MENUNAMES)
5528 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
5529 #endif
5532 #ifndef BACKSLASH_IN_FILENAME
5533 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
5534 return 2;
5535 #endif
5536 return 1;
5538 return 0;
5542 * Show wildchar matches in the status line.
5543 * Show at least the "match" item.
5544 * We start at item 'first_match' in the list and show all matches that fit.
5546 * If inversion is possible we use it. Else '=' characters are used.
5548 void
5549 win_redr_status_matches(xp, num_matches, matches, match, showtail)
5550 expand_T *xp;
5551 int num_matches;
5552 char_u **matches; /* list of matches */
5553 int match;
5554 int showtail;
5556 #define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
5557 int row;
5558 char_u *buf;
5559 int len;
5560 int clen; /* length in screen cells */
5561 int fillchar;
5562 int attr;
5563 int i;
5564 int highlight = TRUE;
5565 char_u *selstart = NULL;
5566 int selstart_col = 0;
5567 char_u *selend = NULL;
5568 static int first_match = 0;
5569 int add_left = FALSE;
5570 char_u *s;
5571 #ifdef FEAT_MENU
5572 int emenu;
5573 #endif
5574 #if defined(FEAT_MBYTE) || defined(FEAT_MENU)
5575 int l;
5576 #endif
5578 if (matches == NULL) /* interrupted completion? */
5579 return;
5581 #ifdef FEAT_MBYTE
5582 if (has_mbyte)
5583 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
5584 else
5585 #endif
5586 buf = alloc((unsigned)Columns + 1);
5587 if (buf == NULL)
5588 return;
5590 if (match == -1) /* don't show match but original text */
5592 match = 0;
5593 highlight = FALSE;
5595 /* count 1 for the ending ">" */
5596 clen = status_match_len(xp, L_MATCH(match)) + 3;
5597 if (match == 0)
5598 first_match = 0;
5599 else if (match < first_match)
5601 /* jumping left, as far as we can go */
5602 first_match = match;
5603 add_left = TRUE;
5605 else
5607 /* check if match fits on the screen */
5608 for (i = first_match; i < match; ++i)
5609 clen += status_match_len(xp, L_MATCH(i)) + 2;
5610 if (first_match > 0)
5611 clen += 2;
5612 /* jumping right, put match at the left */
5613 if ((long)clen > Columns)
5615 first_match = match;
5616 /* if showing the last match, we can add some on the left */
5617 clen = 2;
5618 for (i = match; i < num_matches; ++i)
5620 clen += status_match_len(xp, L_MATCH(i)) + 2;
5621 if ((long)clen >= Columns)
5622 break;
5624 if (i == num_matches)
5625 add_left = TRUE;
5628 if (add_left)
5629 while (first_match > 0)
5631 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
5632 if ((long)clen >= Columns)
5633 break;
5634 --first_match;
5637 fillchar = fillchar_status(&attr, TRUE);
5639 if (first_match == 0)
5641 *buf = NUL;
5642 len = 0;
5644 else
5646 STRCPY(buf, "< ");
5647 len = 2;
5649 clen = len;
5651 i = first_match;
5652 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
5654 if (i == match)
5656 selstart = buf + len;
5657 selstart_col = clen;
5660 s = L_MATCH(i);
5661 /* Check for menu separators - replace with '|' */
5662 #ifdef FEAT_MENU
5663 emenu = (xp->xp_context == EXPAND_MENUS
5664 || xp->xp_context == EXPAND_MENUNAMES);
5665 if (emenu && menu_is_separator(s))
5667 STRCPY(buf + len, transchar('|'));
5668 l = (int)STRLEN(buf + len);
5669 len += l;
5670 clen += l;
5672 else
5673 #endif
5674 for ( ; *s != NUL; ++s)
5676 s += skip_status_match_char(xp, s);
5677 clen += ptr2cells(s);
5678 #ifdef FEAT_MBYTE
5679 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
5681 STRNCPY(buf + len, s, l);
5682 s += l - 1;
5683 len += l;
5685 else
5686 #endif
5688 STRCPY(buf + len, transchar_byte(*s));
5689 len += (int)STRLEN(buf + len);
5692 if (i == match)
5693 selend = buf + len;
5695 *(buf + len++) = ' ';
5696 *(buf + len++) = ' ';
5697 clen += 2;
5698 if (++i == num_matches)
5699 break;
5702 if (i != num_matches)
5704 *(buf + len++) = '>';
5705 ++clen;
5708 buf[len] = NUL;
5710 row = cmdline_row - 1;
5711 if (row >= 0)
5713 if (wild_menu_showing == 0)
5715 if (msg_scrolled > 0)
5717 /* Put the wildmenu just above the command line. If there is
5718 * no room, scroll the screen one line up. */
5719 if (cmdline_row == Rows - 1)
5721 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
5722 ++msg_scrolled;
5724 else
5726 ++cmdline_row;
5727 ++row;
5729 wild_menu_showing = WM_SCROLLED;
5731 else
5733 /* Create status line if needed by setting 'laststatus' to 2.
5734 * Set 'winminheight' to zero to avoid that the window is
5735 * resized. */
5736 if (lastwin->w_status_height == 0)
5738 save_p_ls = p_ls;
5739 save_p_wmh = p_wmh;
5740 p_ls = 2;
5741 p_wmh = 0;
5742 last_status(FALSE);
5744 wild_menu_showing = WM_SHOWN;
5748 screen_puts(buf, row, 0, attr);
5749 if (selstart != NULL && highlight)
5751 *selend = NUL;
5752 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
5755 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
5758 #ifdef FEAT_VERTSPLIT
5759 win_redraw_last_status(topframe);
5760 #else
5761 lastwin->w_redr_status = TRUE;
5762 #endif
5763 vim_free(buf);
5765 #endif
5767 #if defined(FEAT_WINDOWS) || defined(PROTO)
5769 * Redraw the status line of window wp.
5771 * If inversion is possible we use it. Else '=' characters are used.
5773 void
5774 win_redr_status(wp)
5775 win_T *wp;
5777 int row;
5778 char_u *p;
5779 int len;
5780 int fillchar;
5781 int attr;
5782 int this_ru_col;
5783 static int busy = FALSE;
5785 /* It's possible to get here recursively when 'statusline' (indirectly)
5786 * invokes ":redrawstatus". Simply ignore the call then. */
5787 if (busy)
5788 return;
5789 busy = TRUE;
5791 wp->w_redr_status = FALSE;
5792 if (wp->w_status_height == 0)
5794 /* no status line, can only be last window */
5795 redraw_cmdline = TRUE;
5797 else if (!redrawing()
5798 #ifdef FEAT_INS_EXPAND
5799 /* don't update status line when popup menu is visible and may be
5800 * drawn over it */
5801 || pum_visible()
5802 #endif
5805 /* Don't redraw right now, do it later. */
5806 wp->w_redr_status = TRUE;
5808 #ifdef FEAT_STL_OPT
5809 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
5811 /* redraw custom status line */
5812 redraw_custom_statusline(wp);
5814 #endif
5815 else
5817 fillchar = fillchar_status(&attr, wp == curwin);
5819 get_trans_bufname(wp->w_buffer);
5820 p = NameBuff;
5821 len = (int)STRLEN(p);
5823 if (wp->w_buffer->b_help
5824 #ifdef FEAT_QUICKFIX
5825 || wp->w_p_pvw
5826 #endif
5827 || bufIsChanged(wp->w_buffer)
5828 || wp->w_buffer->b_p_ro)
5829 *(p + len++) = ' ';
5830 if (wp->w_buffer->b_help)
5832 STRCPY(p + len, _("[Help]"));
5833 len += (int)STRLEN(p + len);
5835 #ifdef FEAT_QUICKFIX
5836 if (wp->w_p_pvw)
5838 STRCPY(p + len, _("[Preview]"));
5839 len += (int)STRLEN(p + len);
5841 #endif
5842 if (bufIsChanged(wp->w_buffer))
5844 STRCPY(p + len, "[+]");
5845 len += 3;
5847 if (wp->w_buffer->b_p_ro)
5849 STRCPY(p + len, "[RO]");
5850 len += 4;
5853 #ifndef FEAT_VERTSPLIT
5854 this_ru_col = ru_col;
5855 if (this_ru_col < (Columns + 1) / 2)
5856 this_ru_col = (Columns + 1) / 2;
5857 #else
5858 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
5859 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
5860 this_ru_col = (W_WIDTH(wp) + 1) / 2;
5861 if (this_ru_col <= 1)
5863 p = (char_u *)"<"; /* No room for file name! */
5864 len = 1;
5866 else
5867 #endif
5868 #ifdef FEAT_MBYTE
5869 if (has_mbyte)
5871 int clen = 0, i;
5873 /* Count total number of display cells. */
5874 for (i = 0; p[i] != NUL; i += (*mb_ptr2len)(p + i))
5875 clen += (*mb_ptr2cells)(p + i);
5876 /* Find first character that will fit.
5877 * Going from start to end is much faster for DBCS. */
5878 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
5879 i += (*mb_ptr2len)(p + i))
5880 clen -= (*mb_ptr2cells)(p + i);
5881 len = clen;
5882 if (i > 0)
5884 p = p + i - 1;
5885 *p = '<';
5886 ++len;
5890 else
5891 #endif
5892 if (len > this_ru_col - 1)
5894 p += len - (this_ru_col - 1);
5895 *p = '<';
5896 len = this_ru_col - 1;
5899 row = W_WINROW(wp) + wp->w_height;
5900 screen_puts(p, row, W_WINCOL(wp), attr);
5901 screen_fill(row, row + 1, len + W_WINCOL(wp),
5902 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
5904 if (get_keymap_str(wp, NameBuff, MAXPATHL)
5905 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
5906 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
5907 - 1 + W_WINCOL(wp)), attr);
5909 #ifdef FEAT_CMDL_INFO
5910 win_redr_ruler(wp, TRUE);
5911 #endif
5914 #ifdef FEAT_VERTSPLIT
5916 * May need to draw the character below the vertical separator.
5918 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
5920 if (stl_connected(wp))
5921 fillchar = fillchar_status(&attr, wp == curwin);
5922 else
5923 fillchar = fillchar_vsep(&attr);
5924 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
5925 attr);
5927 #endif
5928 busy = FALSE;
5931 #ifdef FEAT_STL_OPT
5933 * Redraw the status line according to 'statusline' and take care of any
5934 * errors encountered.
5936 static void
5937 redraw_custom_statusline(wp)
5938 win_T *wp;
5940 static int entered = FALSE;
5941 int save_called_emsg = called_emsg;
5943 /* When called recursively return. This can happen when the statusline
5944 * contains an expression that triggers a redraw. */
5945 if (entered)
5946 return;
5947 entered = TRUE;
5949 called_emsg = FALSE;
5950 win_redr_custom(wp, FALSE);
5951 if (called_emsg)
5953 /* When there is an error disable the statusline, otherwise the
5954 * display is messed up with errors and a redraw triggers the problem
5955 * again and again. */
5956 set_string_option_direct((char_u *)"statusline", -1,
5957 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
5958 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
5960 called_emsg |= save_called_emsg;
5961 entered = FALSE;
5963 #endif
5965 # ifdef FEAT_VERTSPLIT
5967 * Return TRUE if the status line of window "wp" is connected to the status
5968 * line of the window right of it. If not, then it's a vertical separator.
5969 * Only call if (wp->w_vsep_width != 0).
5972 stl_connected(wp)
5973 win_T *wp;
5975 frame_T *fr;
5977 fr = wp->w_frame;
5978 while (fr->fr_parent != NULL)
5980 if (fr->fr_parent->fr_layout == FR_COL)
5982 if (fr->fr_next != NULL)
5983 break;
5985 else
5987 if (fr->fr_next != NULL)
5988 return TRUE;
5990 fr = fr->fr_parent;
5992 return FALSE;
5994 # endif
5996 #endif /* FEAT_WINDOWS */
5998 #if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
6000 * Get the value to show for the language mappings, active 'keymap'.
6003 get_keymap_str(wp, buf, len)
6004 win_T *wp;
6005 char_u *buf; /* buffer for the result */
6006 int len; /* length of buffer */
6008 char_u *p;
6010 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
6011 return FALSE;
6014 #ifdef FEAT_EVAL
6015 buf_T *old_curbuf = curbuf;
6016 win_T *old_curwin = curwin;
6017 char_u *s;
6019 curbuf = wp->w_buffer;
6020 curwin = wp;
6021 STRCPY(buf, "b:keymap_name"); /* must be writable */
6022 ++emsg_skip;
6023 s = p = eval_to_string(buf, NULL, FALSE);
6024 --emsg_skip;
6025 curbuf = old_curbuf;
6026 curwin = old_curwin;
6027 if (p == NULL || *p == NUL)
6028 #endif
6030 #ifdef FEAT_KEYMAP
6031 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
6032 p = wp->w_buffer->b_p_keymap;
6033 else
6034 #endif
6035 p = (char_u *)"lang";
6037 if ((int)(STRLEN(p) + 3) < len)
6038 sprintf((char *)buf, "<%s>", p);
6039 else
6040 buf[0] = NUL;
6041 #ifdef FEAT_EVAL
6042 vim_free(s);
6043 #endif
6045 return buf[0] != NUL;
6047 #endif
6049 #if defined(FEAT_STL_OPT) || defined(PROTO)
6051 * Redraw the status line or ruler of window "wp".
6052 * When "wp" is NULL redraw the tab pages line from 'tabline'.
6054 static void
6055 win_redr_custom(wp, draw_ruler)
6056 win_T *wp;
6057 int draw_ruler; /* TRUE or FALSE */
6059 int attr;
6060 int curattr;
6061 int row;
6062 int col = 0;
6063 int maxwidth;
6064 int width;
6065 int n;
6066 int len;
6067 int fillchar;
6068 char_u buf[MAXPATHL];
6069 char_u *stl;
6070 char_u *p;
6071 struct stl_hlrec hltab[STL_MAX_ITEM];
6072 struct stl_hlrec tabtab[STL_MAX_ITEM];
6073 int use_sandbox = FALSE;
6075 /* setup environment for the task at hand */
6076 if (wp == NULL)
6078 /* Use 'tabline'. Always at the first line of the screen. */
6079 stl = p_tal;
6080 row = 0;
6081 fillchar = ' ';
6082 attr = hl_attr(HLF_TPF);
6083 maxwidth = Columns;
6084 # ifdef FEAT_EVAL
6085 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
6086 # endif
6088 else
6090 row = W_WINROW(wp) + wp->w_height;
6091 fillchar = fillchar_status(&attr, wp == curwin);
6092 maxwidth = W_WIDTH(wp);
6094 if (draw_ruler)
6096 stl = p_ruf;
6097 /* advance past any leading group spec - implicit in ru_col */
6098 if (*stl == '%')
6100 if (*++stl == '-')
6101 stl++;
6102 if (atoi((char *)stl))
6103 while (VIM_ISDIGIT(*stl))
6104 stl++;
6105 if (*stl++ != '(')
6106 stl = p_ruf;
6108 #ifdef FEAT_VERTSPLIT
6109 col = ru_col - (Columns - W_WIDTH(wp));
6110 if (col < (W_WIDTH(wp) + 1) / 2)
6111 col = (W_WIDTH(wp) + 1) / 2;
6112 #else
6113 col = ru_col;
6114 if (col > (Columns + 1) / 2)
6115 col = (Columns + 1) / 2;
6116 #endif
6117 maxwidth = W_WIDTH(wp) - col;
6118 #ifdef FEAT_WINDOWS
6119 if (!wp->w_status_height)
6120 #endif
6122 row = Rows - 1;
6123 --maxwidth; /* writing in last column may cause scrolling */
6124 fillchar = ' ';
6125 attr = 0;
6128 # ifdef FEAT_EVAL
6129 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
6130 # endif
6132 else
6134 if (*wp->w_p_stl != NUL)
6135 stl = wp->w_p_stl;
6136 else
6137 stl = p_stl;
6138 # ifdef FEAT_EVAL
6139 use_sandbox = was_set_insecurely((char_u *)"statusline",
6140 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
6141 # endif
6144 #ifdef FEAT_VERTSPLIT
6145 col += W_WINCOL(wp);
6146 #endif
6149 if (maxwidth <= 0)
6150 return;
6152 /* Make a copy, because the statusline may include a function call that
6153 * might change the option value and free the memory. */
6154 stl = vim_strsave(stl);
6155 width = build_stl_str_hl(wp == NULL ? curwin : wp,
6156 buf, sizeof(buf),
6157 stl, use_sandbox,
6158 fillchar, maxwidth, hltab, tabtab);
6159 vim_free(stl);
6160 len = (int)STRLEN(buf);
6162 while (width < maxwidth && len < (int)sizeof(buf) - 1)
6164 #ifdef FEAT_MBYTE
6165 len += (*mb_char2bytes)(fillchar, buf + len);
6166 #else
6167 buf[len++] = fillchar;
6168 #endif
6169 ++width;
6171 buf[len] = NUL;
6174 * Draw each snippet with the specified highlighting.
6176 curattr = attr;
6177 p = buf;
6178 for (n = 0; hltab[n].start != NULL; n++)
6180 len = (int)(hltab[n].start - p);
6181 screen_puts_len(p, len, row, col, curattr);
6182 col += vim_strnsize(p, len);
6183 p = hltab[n].start;
6185 if (hltab[n].userhl == 0)
6186 curattr = attr;
6187 else if (hltab[n].userhl < 0)
6188 curattr = syn_id2attr(-hltab[n].userhl);
6189 #ifdef FEAT_WINDOWS
6190 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
6191 curattr = highlight_stlnc[hltab[n].userhl - 1];
6192 #endif
6193 else
6194 curattr = highlight_user[hltab[n].userhl - 1];
6196 screen_puts(p, row, col, curattr);
6198 if (wp == NULL)
6200 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
6201 col = 0;
6202 len = 0;
6203 p = buf;
6204 fillchar = 0;
6205 for (n = 0; tabtab[n].start != NULL; n++)
6207 len += vim_strnsize(p, (int)(tabtab[n].start - p));
6208 while (col < len)
6209 TabPageIdxs[col++] = fillchar;
6210 p = tabtab[n].start;
6211 fillchar = tabtab[n].userhl;
6213 while (col < Columns)
6214 TabPageIdxs[col++] = fillchar;
6218 #endif /* FEAT_STL_OPT */
6221 * Output a single character directly to the screen and update ScreenLines.
6223 void
6224 screen_putchar(c, row, col, attr)
6225 int c;
6226 int row, col;
6227 int attr;
6229 #ifdef FEAT_MBYTE
6230 char_u buf[MB_MAXBYTES + 1];
6232 buf[(*mb_char2bytes)(c, buf)] = NUL;
6233 #else
6234 char_u buf[2];
6236 buf[0] = c;
6237 buf[1] = NUL;
6238 #endif
6239 screen_puts(buf, row, col, attr);
6243 * Get a single character directly from ScreenLines into "bytes[]".
6244 * Also return its attribute in *attrp;
6246 void
6247 screen_getbytes(row, col, bytes, attrp)
6248 int row, col;
6249 char_u *bytes;
6250 int *attrp;
6252 unsigned off;
6254 /* safety check */
6255 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
6257 off = LineOffset[row] + col;
6258 *attrp = ScreenAttrs[off];
6259 bytes[0] = ScreenLines[off];
6260 bytes[1] = NUL;
6262 #ifdef FEAT_MBYTE
6263 if (enc_utf8 && ScreenLinesUC[off] != 0)
6264 bytes[utfc_char2bytes(off, bytes)] = NUL;
6265 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6267 bytes[0] = ScreenLines[off];
6268 bytes[1] = ScreenLines2[off];
6269 bytes[2] = NUL;
6271 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
6273 bytes[1] = ScreenLines[off + 1];
6274 bytes[2] = NUL;
6276 #endif
6280 #ifdef FEAT_MBYTE
6281 static int screen_comp_differs __ARGS((int, int*));
6284 * Return TRUE if composing characters for screen posn "off" differs from
6285 * composing characters in "u8cc".
6287 static int
6288 screen_comp_differs(off, u8cc)
6289 int off;
6290 int *u8cc;
6292 int i;
6294 for (i = 0; i < Screen_mco; ++i)
6296 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
6297 return TRUE;
6298 if (u8cc[i] == 0)
6299 break;
6301 return FALSE;
6303 #endif
6306 * Put string '*text' on the screen at position 'row' and 'col', with
6307 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
6308 * Note: only outputs within one row, message is truncated at screen boundary!
6309 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
6311 void
6312 screen_puts(text, row, col, attr)
6313 char_u *text;
6314 int row;
6315 int col;
6316 int attr;
6318 screen_puts_len(text, -1, row, col, attr);
6322 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
6323 * a NUL.
6325 void
6326 screen_puts_len(text, len, row, col, attr)
6327 char_u *text;
6328 int len;
6329 int row;
6330 int col;
6331 int attr;
6333 unsigned off;
6334 char_u *ptr = text;
6335 int c;
6336 #ifdef FEAT_MBYTE
6337 unsigned max_off;
6338 int mbyte_blen = 1;
6339 int mbyte_cells = 1;
6340 int u8c = 0;
6341 int u8cc[MAX_MCO];
6342 int clear_next_cell = FALSE;
6343 # ifdef FEAT_ARABIC
6344 int prev_c = 0; /* previous Arabic character */
6345 int pc, nc, nc1;
6346 int pcc[MAX_MCO];
6347 # endif
6348 #endif
6349 #if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6350 int force_redraw_this;
6351 int force_redraw_next = FALSE;
6352 #endif
6353 int need_redraw;
6355 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
6356 return;
6357 off = LineOffset[row] + col;
6359 #ifdef FEAT_MBYTE
6360 /* When drawing over the right halve of a double-wide char clear out the
6361 * left halve. Only needed in a terminal. */
6362 if (has_mbyte && col > 0 && col < screen_Columns
6363 # ifdef FEAT_GUI
6364 && !gui.in_use
6365 # endif
6366 && mb_fix_col(col, row) != col)
6368 ScreenLines[off - 1] = ' ';
6369 ScreenAttrs[off - 1] = 0;
6370 if (enc_utf8)
6372 ScreenLinesUC[off - 1] = 0;
6373 ScreenLinesC[0][off - 1] = 0;
6375 /* redraw the previous cell, make it empty */
6376 screen_char(off - 1, row, col - 1);
6377 /* force the cell at "col" to be redrawn */
6378 force_redraw_next = TRUE;
6380 #endif
6382 #ifdef FEAT_MBYTE
6383 max_off = LineOffset[row] + screen_Columns;
6384 #endif
6385 while (col < screen_Columns
6386 && (len < 0 || (int)(ptr - text) < len)
6387 && *ptr != NUL)
6389 c = *ptr;
6390 #ifdef FEAT_MBYTE
6391 /* check if this is the first byte of a multibyte */
6392 if (has_mbyte)
6394 if (enc_utf8 && len > 0)
6395 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
6396 else
6397 mbyte_blen = (*mb_ptr2len)(ptr);
6398 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6399 mbyte_cells = 1;
6400 else if (enc_dbcs != 0)
6401 mbyte_cells = mbyte_blen;
6402 else /* enc_utf8 */
6404 if (len >= 0)
6405 u8c = utfc_ptr2char_len(ptr, u8cc,
6406 (int)((text + len) - ptr));
6407 else
6408 u8c = utfc_ptr2char(ptr, u8cc);
6409 mbyte_cells = utf_char2cells(u8c);
6410 # ifdef UNICODE16
6411 /* Non-BMP character: display as ? or fullwidth ?. */
6412 if (u8c >= 0x10000)
6414 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
6415 if (attr == 0)
6416 attr = hl_attr(HLF_8);
6418 # endif
6419 # ifdef FEAT_ARABIC
6420 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
6422 /* Do Arabic shaping. */
6423 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
6425 /* Past end of string to be displayed. */
6426 nc = NUL;
6427 nc1 = NUL;
6429 else
6431 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
6432 (int)((text + len) - ptr - mbyte_blen));
6433 nc1 = pcc[0];
6435 pc = prev_c;
6436 prev_c = u8c;
6437 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
6439 else
6440 prev_c = u8c;
6441 # endif
6442 if (col + mbyte_cells > screen_Columns)
6444 /* Only 1 cell left, but character requires 2 cells:
6445 * display a '>' in the last column to avoid wrapping. */
6446 c = '>';
6447 mbyte_cells = 1;
6451 #endif
6453 #if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6454 force_redraw_this = force_redraw_next;
6455 force_redraw_next = FALSE;
6456 #endif
6458 need_redraw = ScreenLines[off] != c
6459 #ifdef FEAT_MBYTE
6460 || (mbyte_cells == 2
6461 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
6462 || (enc_dbcs == DBCS_JPNU
6463 && c == 0x8e
6464 && ScreenLines2[off] != ptr[1])
6465 || (enc_utf8
6466 && (ScreenLinesUC[off] != (u8char_T)(c >= 0x80 ? u8c : 0)
6467 || screen_comp_differs(off, u8cc)))
6468 #endif
6469 || ScreenAttrs[off] != attr
6470 || exmode_active;
6472 if (need_redraw
6473 #if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6474 || force_redraw_this
6475 #endif
6478 #if defined(FEAT_GUI) || defined(UNIX)
6479 /* The bold trick makes a single row of pixels appear in the next
6480 * character. When a bold character is removed, the next
6481 * character should be redrawn too. This happens for our own GUI
6482 * and for some xterms. */
6483 if (need_redraw && ScreenLines[off] != ' ' && (
6484 # ifdef FEAT_GUI
6485 gui.in_use
6486 # endif
6487 # if defined(FEAT_GUI) && defined(UNIX)
6489 # endif
6490 # ifdef UNIX
6491 term_is_xterm
6492 # endif
6495 int n = ScreenAttrs[off];
6497 if (n > HL_ALL)
6498 n = syn_attr2attr(n);
6499 if (n & HL_BOLD)
6500 force_redraw_next = TRUE;
6502 #endif
6503 #ifdef FEAT_MBYTE
6504 /* When at the end of the text and overwriting a two-cell
6505 * character with a one-cell character, need to clear the next
6506 * cell. Also when overwriting the left halve of a two-cell char
6507 * with the right halve of a two-cell char. Do this only once
6508 * (mb_off2cells() may return 2 on the right halve). */
6509 if (clear_next_cell)
6510 clear_next_cell = FALSE;
6511 else if (has_mbyte
6512 && (len < 0 ? ptr[mbyte_blen] == NUL
6513 : ptr + mbyte_blen >= text + len)
6514 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
6515 || (mbyte_cells == 2
6516 && (*mb_off2cells)(off, max_off) == 1
6517 && (*mb_off2cells)(off + 1, max_off) > 1)))
6518 clear_next_cell = TRUE;
6520 /* Make sure we never leave a second byte of a double-byte behind,
6521 * it confuses mb_off2cells(). */
6522 if (enc_dbcs
6523 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
6524 || (mbyte_cells == 2
6525 && (*mb_off2cells)(off, max_off) == 1
6526 && (*mb_off2cells)(off + 1, max_off) > 1)))
6527 ScreenLines[off + mbyte_blen] = 0;
6528 #endif
6529 ScreenLines[off] = c;
6530 ScreenAttrs[off] = attr;
6531 #ifdef FEAT_MBYTE
6532 if (enc_utf8)
6534 if (c < 0x80 && u8cc[0] == 0)
6535 ScreenLinesUC[off] = 0;
6536 else
6538 int i;
6540 ScreenLinesUC[off] = u8c;
6541 for (i = 0; i < Screen_mco; ++i)
6543 ScreenLinesC[i][off] = u8cc[i];
6544 if (u8cc[i] == 0)
6545 break;
6548 if (mbyte_cells == 2)
6550 ScreenLines[off + 1] = 0;
6551 ScreenAttrs[off + 1] = attr;
6553 screen_char(off, row, col);
6555 else if (mbyte_cells == 2)
6557 ScreenLines[off + 1] = ptr[1];
6558 ScreenAttrs[off + 1] = attr;
6559 screen_char_2(off, row, col);
6561 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6563 ScreenLines2[off] = ptr[1];
6564 screen_char(off, row, col);
6566 else
6567 #endif
6568 screen_char(off, row, col);
6570 #ifdef FEAT_MBYTE
6571 if (has_mbyte)
6573 off += mbyte_cells;
6574 col += mbyte_cells;
6575 ptr += mbyte_blen;
6576 if (clear_next_cell)
6577 ptr = (char_u *)" ";
6579 else
6580 #endif
6582 ++off;
6583 ++col;
6584 ++ptr;
6588 #if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6589 /* If we detected the next character needs to be redrawn, but the text
6590 * doesn't extend up to there, update the character here. */
6591 if (force_redraw_next && col < screen_Columns)
6593 # ifdef FEAT_MBYTE
6594 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
6595 screen_char_2(off, row, col);
6596 else
6597 # endif
6598 screen_char(off, row, col);
6600 #endif
6603 #ifdef FEAT_SEARCH_EXTRA
6605 * Prepare for 'hlsearch' highlighting.
6607 static void
6608 start_search_hl()
6610 if (p_hls && !no_hlsearch)
6612 last_pat_prog(&search_hl.rm);
6613 search_hl.attr = hl_attr(HLF_L);
6614 # ifdef FEAT_RELTIME
6615 /* Set the time limit to 'redrawtime'. */
6616 profile_setlimit(p_rdt, &search_hl.tm);
6617 # endif
6622 * Clean up for 'hlsearch' highlighting.
6624 static void
6625 end_search_hl()
6627 if (search_hl.rm.regprog != NULL)
6629 vim_free(search_hl.rm.regprog);
6630 search_hl.rm.regprog = NULL;
6635 * Advance to the match in window "wp" line "lnum" or past it.
6637 static void
6638 prepare_search_hl(wp, lnum)
6639 win_T *wp;
6640 linenr_T lnum;
6642 matchitem_T *cur; /* points to the match list */
6643 match_T *shl; /* points to search_hl or a match */
6644 int shl_flag; /* flag to indicate whether search_hl
6645 has been processed or not */
6646 int n;
6649 * When using a multi-line pattern, start searching at the top
6650 * of the window or just after a closed fold.
6651 * Do this both for search_hl and the match list.
6653 cur = wp->w_match_head;
6654 shl_flag = FALSE;
6655 while (cur != NULL || shl_flag == FALSE)
6657 if (shl_flag == FALSE)
6659 shl = &search_hl;
6660 shl_flag = TRUE;
6662 else
6663 shl = &cur->hl;
6664 if (shl->rm.regprog != NULL
6665 && shl->lnum == 0
6666 && re_multiline(shl->rm.regprog))
6668 if (shl->first_lnum == 0)
6670 # ifdef FEAT_FOLDING
6671 for (shl->first_lnum = lnum;
6672 shl->first_lnum > wp->w_topline; --shl->first_lnum)
6673 if (hasFoldingWin(wp, shl->first_lnum - 1,
6674 NULL, NULL, TRUE, NULL))
6675 break;
6676 # else
6677 shl->first_lnum = wp->w_topline;
6678 # endif
6680 n = 0;
6681 while (shl->first_lnum < lnum && shl->rm.regprog != NULL)
6683 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n);
6684 if (shl->lnum != 0)
6686 shl->first_lnum = shl->lnum
6687 + shl->rm.endpos[0].lnum
6688 - shl->rm.startpos[0].lnum;
6689 n = shl->rm.endpos[0].col;
6691 else
6693 ++shl->first_lnum;
6694 n = 0;
6698 if (shl != &search_hl && cur != NULL)
6699 cur = cur->next;
6704 * Search for a next 'hlsearch' or match.
6705 * Uses shl->buf.
6706 * Sets shl->lnum and shl->rm contents.
6707 * Note: Assumes a previous match is always before "lnum", unless
6708 * shl->lnum is zero.
6709 * Careful: Any pointers for buffer lines will become invalid.
6711 static void
6712 next_search_hl(win, shl, lnum, mincol)
6713 win_T *win;
6714 match_T *shl; /* points to search_hl or a match */
6715 linenr_T lnum;
6716 colnr_T mincol; /* minimal column for a match */
6718 linenr_T l;
6719 colnr_T matchcol;
6720 long nmatched;
6722 if (shl->lnum != 0)
6724 /* Check for three situations:
6725 * 1. If the "lnum" is below a previous match, start a new search.
6726 * 2. If the previous match includes "mincol", use it.
6727 * 3. Continue after the previous match.
6729 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
6730 if (lnum > l)
6731 shl->lnum = 0;
6732 else if (lnum < l || shl->rm.endpos[0].col > mincol)
6733 return;
6737 * Repeat searching for a match until one is found that includes "mincol"
6738 * or none is found in this line.
6740 called_emsg = FALSE;
6741 for (;;)
6743 #ifdef FEAT_RELTIME
6744 /* Stop searching after passing the time limit. */
6745 if (profile_passed_limit(&(shl->tm)))
6747 shl->lnum = 0; /* no match found in time */
6748 break;
6750 #endif
6751 /* Three situations:
6752 * 1. No useful previous match: search from start of line.
6753 * 2. Not Vi compatible or empty match: continue at next character.
6754 * Break the loop if this is beyond the end of the line.
6755 * 3. Vi compatible searching: continue at end of previous match.
6757 if (shl->lnum == 0)
6758 matchcol = 0;
6759 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
6760 || (shl->rm.endpos[0].lnum == 0
6761 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
6763 char_u *ml;
6765 matchcol = shl->rm.startpos[0].col;
6766 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
6767 if (*ml == NUL)
6769 ++matchcol;
6770 shl->lnum = 0;
6771 break;
6773 #ifdef FEAT_MBYTE
6774 if (has_mbyte)
6775 matchcol += mb_ptr2len(ml);
6776 else
6777 #endif
6778 ++matchcol;
6780 else
6781 matchcol = shl->rm.endpos[0].col;
6783 shl->lnum = lnum;
6784 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum, matchcol,
6785 #ifdef FEAT_RELTIME
6786 &(shl->tm)
6787 #else
6788 NULL
6789 #endif
6791 if (called_emsg)
6793 /* Error while handling regexp: stop using this regexp. */
6794 if (shl == &search_hl)
6796 /* don't free regprog in the match list, it's a copy */
6797 vim_free(shl->rm.regprog);
6798 no_hlsearch = TRUE;
6800 shl->rm.regprog = NULL;
6801 shl->lnum = 0;
6802 got_int = FALSE; /* avoid the "Type :quit to exit Vim" message */
6803 break;
6805 if (nmatched == 0)
6807 shl->lnum = 0; /* no match found */
6808 break;
6810 if (shl->rm.startpos[0].lnum > 0
6811 || shl->rm.startpos[0].col >= mincol
6812 || nmatched > 1
6813 || shl->rm.endpos[0].col > mincol)
6815 shl->lnum += shl->rm.startpos[0].lnum;
6816 break; /* useful match found */
6820 #endif
6822 static void
6823 screen_start_highlight(attr)
6824 int attr;
6826 attrentry_T *aep = NULL;
6828 screen_attr = attr;
6829 if (full_screen
6830 #ifdef WIN3264
6831 && termcap_active
6832 #endif
6835 #ifdef FEAT_GUI
6836 if (gui.in_use)
6838 char buf[20];
6840 /* The GUI handles this internally. */
6841 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
6842 OUT_STR(buf);
6844 else
6845 #endif
6847 if (attr > HL_ALL) /* special HL attr. */
6849 if (t_colors > 1)
6850 aep = syn_cterm_attr2entry(attr);
6851 else
6852 aep = syn_term_attr2entry(attr);
6853 if (aep == NULL) /* did ":syntax clear" */
6854 attr = 0;
6855 else
6856 attr = aep->ae_attr;
6858 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
6859 out_str(T_MD);
6860 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
6861 && cterm_normal_fg_bold)
6862 /* If the Normal FG color has BOLD attribute and the new HL
6863 * has a FG color defined, clear BOLD. */
6864 out_str(T_ME);
6865 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
6866 out_str(T_SO);
6867 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
6868 /* underline or undercurl */
6869 out_str(T_US);
6870 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
6871 out_str(T_CZH);
6872 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
6873 out_str(T_MR);
6876 * Output the color or start string after bold etc., in case the
6877 * bold etc. override the color setting.
6879 if (aep != NULL)
6881 if (t_colors > 1)
6883 if (aep->ae_u.cterm.fg_color)
6884 term_fg_color(aep->ae_u.cterm.fg_color - 1);
6885 if (aep->ae_u.cterm.bg_color)
6886 term_bg_color(aep->ae_u.cterm.bg_color - 1);
6888 else
6890 if (aep->ae_u.term.start != NULL)
6891 out_str(aep->ae_u.term.start);
6898 void
6899 screen_stop_highlight()
6901 int do_ME = FALSE; /* output T_ME code */
6903 if (screen_attr != 0
6904 #ifdef WIN3264
6905 && termcap_active
6906 #endif
6909 #ifdef FEAT_GUI
6910 if (gui.in_use)
6912 char buf[20];
6914 /* use internal GUI code */
6915 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
6916 OUT_STR(buf);
6918 else
6919 #endif
6921 if (screen_attr > HL_ALL) /* special HL attr. */
6923 attrentry_T *aep;
6925 if (t_colors > 1)
6928 * Assume that t_me restores the original colors!
6930 aep = syn_cterm_attr2entry(screen_attr);
6931 if (aep != NULL && (aep->ae_u.cterm.fg_color
6932 || aep->ae_u.cterm.bg_color))
6933 do_ME = TRUE;
6935 else
6937 aep = syn_term_attr2entry(screen_attr);
6938 if (aep != NULL && aep->ae_u.term.stop != NULL)
6940 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
6941 do_ME = TRUE;
6942 else
6943 out_str(aep->ae_u.term.stop);
6946 if (aep == NULL) /* did ":syntax clear" */
6947 screen_attr = 0;
6948 else
6949 screen_attr = aep->ae_attr;
6953 * Often all ending-codes are equal to T_ME. Avoid outputting the
6954 * same sequence several times.
6956 if (screen_attr & HL_STANDOUT)
6958 if (STRCMP(T_SE, T_ME) == 0)
6959 do_ME = TRUE;
6960 else
6961 out_str(T_SE);
6963 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
6965 if (STRCMP(T_UE, T_ME) == 0)
6966 do_ME = TRUE;
6967 else
6968 out_str(T_UE);
6970 if (screen_attr & HL_ITALIC)
6972 if (STRCMP(T_CZR, T_ME) == 0)
6973 do_ME = TRUE;
6974 else
6975 out_str(T_CZR);
6977 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
6978 out_str(T_ME);
6980 if (t_colors > 1)
6982 /* set Normal cterm colors */
6983 if (cterm_normal_fg_color != 0)
6984 term_fg_color(cterm_normal_fg_color - 1);
6985 if (cterm_normal_bg_color != 0)
6986 term_bg_color(cterm_normal_bg_color - 1);
6987 if (cterm_normal_fg_bold)
6988 out_str(T_MD);
6992 screen_attr = 0;
6996 * Reset the colors for a cterm. Used when leaving Vim.
6997 * The machine specific code may override this again.
6999 void
7000 reset_cterm_colors()
7002 if (t_colors > 1)
7004 /* set Normal cterm colors */
7005 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
7007 out_str(T_OP);
7008 screen_attr = -1;
7010 if (cterm_normal_fg_bold)
7012 out_str(T_ME);
7013 screen_attr = -1;
7019 * Put character ScreenLines["off"] on the screen at position "row" and "col",
7020 * using the attributes from ScreenAttrs["off"].
7022 static void
7023 screen_char(off, row, col)
7024 unsigned off;
7025 int row;
7026 int col;
7028 int attr;
7030 /* Check for illegal values, just in case (could happen just after
7031 * resizing). */
7032 if (row >= screen_Rows || col >= screen_Columns)
7033 return;
7035 /* Outputting the last character on the screen may scrollup the screen.
7036 * Don't to it! Mark the character invalid (update it when scrolled up) */
7037 if (row == screen_Rows - 1 && col == screen_Columns - 1
7038 #ifdef FEAT_RIGHTLEFT
7039 /* account for first command-line character in rightleft mode */
7040 && !cmdmsg_rl
7041 #endif
7044 ScreenAttrs[off] = (sattr_T)-1;
7045 return;
7049 * Stop highlighting first, so it's easier to move the cursor.
7051 #if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
7052 if (screen_char_attr != 0)
7053 attr = screen_char_attr;
7054 else
7055 #endif
7056 attr = ScreenAttrs[off];
7057 if (screen_attr != attr)
7058 screen_stop_highlight();
7060 windgoto(row, col);
7062 if (screen_attr != attr)
7063 screen_start_highlight(attr);
7065 #ifdef FEAT_MBYTE
7066 if (enc_utf8 && ScreenLinesUC[off] != 0)
7068 char_u buf[MB_MAXBYTES + 1];
7070 /* Convert UTF-8 character to bytes and write it. */
7072 buf[utfc_char2bytes(off, buf)] = NUL;
7074 out_str(buf);
7075 if (utf_char2cells(ScreenLinesUC[off]) > 1)
7076 ++screen_cur_col;
7078 else
7079 #endif
7081 #ifdef FEAT_MBYTE
7082 out_flush_check();
7083 #endif
7084 out_char(ScreenLines[off]);
7085 #ifdef FEAT_MBYTE
7086 /* double-byte character in single-width cell */
7087 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7088 out_char(ScreenLines2[off]);
7089 #endif
7092 screen_cur_col++;
7095 #ifdef FEAT_MBYTE
7098 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
7099 * on the screen at position 'row' and 'col'.
7100 * The attributes of the first byte is used for all. This is required to
7101 * output the two bytes of a double-byte character with nothing in between.
7103 static void
7104 screen_char_2(off, row, col)
7105 unsigned off;
7106 int row;
7107 int col;
7109 /* Check for illegal values (could be wrong when screen was resized). */
7110 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
7111 return;
7113 /* Outputting the last character on the screen may scrollup the screen.
7114 * Don't to it! Mark the character invalid (update it when scrolled up) */
7115 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
7117 ScreenAttrs[off] = (sattr_T)-1;
7118 return;
7121 /* Output the first byte normally (positions the cursor), then write the
7122 * second byte directly. */
7123 screen_char(off, row, col);
7124 out_char(ScreenLines[off + 1]);
7125 ++screen_cur_col;
7127 #endif
7129 #if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
7131 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
7132 * This uses the contents of ScreenLines[] and doesn't change it.
7134 void
7135 screen_draw_rectangle(row, col, height, width, invert)
7136 int row;
7137 int col;
7138 int height;
7139 int width;
7140 int invert;
7142 int r, c;
7143 int off;
7144 #ifdef FEAT_MBYTE
7145 int max_off;
7146 #endif
7148 /* Can't use ScreenLines unless initialized */
7149 if (ScreenLines == NULL)
7150 return;
7152 if (invert)
7153 screen_char_attr = HL_INVERSE;
7154 for (r = row; r < row + height; ++r)
7156 off = LineOffset[r];
7157 #ifdef FEAT_MBYTE
7158 max_off = off + screen_Columns;
7159 #endif
7160 for (c = col; c < col + width; ++c)
7162 #ifdef FEAT_MBYTE
7163 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
7165 screen_char_2(off + c, r, c);
7166 ++c;
7168 else
7169 #endif
7171 screen_char(off + c, r, c);
7172 #ifdef FEAT_MBYTE
7173 if (utf_off2cells(off + c, max_off) > 1)
7174 ++c;
7175 #endif
7179 screen_char_attr = 0;
7181 #endif
7183 #ifdef FEAT_VERTSPLIT
7185 * Redraw the characters for a vertically split window.
7187 static void
7188 redraw_block(row, end, wp)
7189 int row;
7190 int end;
7191 win_T *wp;
7193 int col;
7194 int width;
7196 # ifdef FEAT_CLIPBOARD
7197 clip_may_clear_selection(row, end - 1);
7198 # endif
7200 if (wp == NULL)
7202 col = 0;
7203 width = Columns;
7205 else
7207 col = wp->w_wincol;
7208 width = wp->w_width;
7210 screen_draw_rectangle(row, col, end - row, width, FALSE);
7212 #endif
7215 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
7216 * with character 'c1' in first column followed by 'c2' in the other columns.
7217 * Use attributes 'attr'.
7219 void
7220 screen_fill(start_row, end_row, start_col, end_col, c1, c2, attr)
7221 int start_row, end_row;
7222 int start_col, end_col;
7223 int c1, c2;
7224 int attr;
7226 int row;
7227 int col;
7228 int off;
7229 int end_off;
7230 int did_delete;
7231 int c;
7232 int norm_term;
7233 #if defined(FEAT_GUI) || defined(UNIX)
7234 int force_next = FALSE;
7235 #endif
7237 if (end_row > screen_Rows) /* safety check */
7238 end_row = screen_Rows;
7239 if (end_col > screen_Columns) /* safety check */
7240 end_col = screen_Columns;
7241 if (ScreenLines == NULL
7242 || start_row >= end_row
7243 || start_col >= end_col) /* nothing to do */
7244 return;
7246 /* it's a "normal" terminal when not in a GUI or cterm */
7247 norm_term = (
7248 #ifdef FEAT_GUI
7249 !gui.in_use &&
7250 #endif
7251 t_colors <= 1);
7252 for (row = start_row; row < end_row; ++row)
7254 #ifdef FEAT_MBYTE
7255 if (has_mbyte
7256 # ifdef FEAT_GUI
7257 && !gui.in_use
7258 # endif
7261 /* When drawing over the right halve of a double-wide char clear
7262 * out the left halve. When drawing over the left halve of a
7263 * double wide-char clear out the right halve. Only needed in a
7264 * terminal. */
7265 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
7266 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
7267 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
7268 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
7270 #endif
7272 * Try to use delete-line termcap code, when no attributes or in a
7273 * "normal" terminal, where a bold/italic space is just a
7274 * space.
7276 did_delete = FALSE;
7277 if (c2 == ' '
7278 && end_col == Columns
7279 && can_clear(T_CE)
7280 && (attr == 0
7281 || (norm_term
7282 && attr <= HL_ALL
7283 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
7286 * check if we really need to clear something
7288 col = start_col;
7289 if (c1 != ' ') /* don't clear first char */
7290 ++col;
7292 off = LineOffset[row] + col;
7293 end_off = LineOffset[row] + end_col;
7295 /* skip blanks (used often, keep it fast!) */
7296 #ifdef FEAT_MBYTE
7297 if (enc_utf8)
7298 while (off < end_off && ScreenLines[off] == ' '
7299 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
7300 ++off;
7301 else
7302 #endif
7303 while (off < end_off && ScreenLines[off] == ' '
7304 && ScreenAttrs[off] == 0)
7305 ++off;
7306 if (off < end_off) /* something to be cleared */
7308 col = off - LineOffset[row];
7309 screen_stop_highlight();
7310 term_windgoto(row, col);/* clear rest of this screen line */
7311 out_str(T_CE);
7312 screen_start(); /* don't know where cursor is now */
7313 col = end_col - col;
7314 while (col--) /* clear chars in ScreenLines */
7316 ScreenLines[off] = ' ';
7317 #ifdef FEAT_MBYTE
7318 if (enc_utf8)
7319 ScreenLinesUC[off] = 0;
7320 #endif
7321 ScreenAttrs[off] = 0;
7322 ++off;
7325 did_delete = TRUE; /* the chars are cleared now */
7328 off = LineOffset[row] + start_col;
7329 c = c1;
7330 for (col = start_col; col < end_col; ++col)
7332 if (ScreenLines[off] != c
7333 #ifdef FEAT_MBYTE
7334 || (enc_utf8 && (int)ScreenLinesUC[off]
7335 != (c >= 0x80 ? c : 0))
7336 #endif
7337 || ScreenAttrs[off] != attr
7338 #if defined(FEAT_GUI) || defined(UNIX)
7339 || force_next
7340 #endif
7343 #if defined(FEAT_GUI) || defined(UNIX)
7344 /* The bold trick may make a single row of pixels appear in
7345 * the next character. When a bold character is removed, the
7346 * next character should be redrawn too. This happens for our
7347 * own GUI and for some xterms. */
7348 if (
7349 # ifdef FEAT_GUI
7350 gui.in_use
7351 # endif
7352 # if defined(FEAT_GUI) && defined(UNIX)
7354 # endif
7355 # ifdef UNIX
7356 term_is_xterm
7357 # endif
7360 if (ScreenLines[off] != ' '
7361 && (ScreenAttrs[off] > HL_ALL
7362 || ScreenAttrs[off] & HL_BOLD))
7363 force_next = TRUE;
7364 else
7365 force_next = FALSE;
7367 #endif
7368 ScreenLines[off] = c;
7369 #ifdef FEAT_MBYTE
7370 if (enc_utf8)
7372 if (c >= 0x80)
7374 ScreenLinesUC[off] = c;
7375 ScreenLinesC[0][off] = 0;
7377 else
7378 ScreenLinesUC[off] = 0;
7380 #endif
7381 ScreenAttrs[off] = attr;
7382 if (!did_delete || c != ' ')
7383 screen_char(off, row, col);
7385 ++off;
7386 if (col == start_col)
7388 if (did_delete)
7389 break;
7390 c = c2;
7393 if (end_col == Columns)
7394 LineWraps[row] = FALSE;
7395 if (row == Rows - 1) /* overwritten the command line */
7397 redraw_cmdline = TRUE;
7398 if (c1 == ' ' && c2 == ' ')
7399 clear_cmdline = FALSE; /* command line has been cleared */
7400 if (start_col == 0)
7401 mode_displayed = FALSE; /* mode cleared or overwritten */
7407 * Check if there should be a delay. Used before clearing or redrawing the
7408 * screen or the command line.
7410 void
7411 check_for_delay(check_msg_scroll)
7412 int check_msg_scroll;
7414 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
7415 && !did_wait_return
7416 && emsg_silent == 0)
7418 out_flush();
7419 ui_delay(1000L, TRUE);
7420 emsg_on_display = FALSE;
7421 if (check_msg_scroll)
7422 msg_scroll = FALSE;
7427 * screen_valid - allocate screen buffers if size changed
7428 * If "clear" is TRUE: clear screen if it has been resized.
7429 * Returns TRUE if there is a valid screen to write to.
7430 * Returns FALSE when starting up and screen not initialized yet.
7433 screen_valid(clear)
7434 int clear;
7436 screenalloc(clear); /* allocate screen buffers if size changed */
7437 return (ScreenLines != NULL);
7441 * Resize the shell to Rows and Columns.
7442 * Allocate ScreenLines[] and associated items.
7444 * There may be some time between setting Rows and Columns and (re)allocating
7445 * ScreenLines[]. This happens when starting up and when (manually) changing
7446 * the shell size. Always use screen_Rows and screen_Columns to access items
7447 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
7448 * final size of the shell is needed.
7450 void
7451 screenalloc(clear)
7452 int clear;
7454 int new_row, old_row;
7455 #ifdef FEAT_GUI
7456 int old_Rows;
7457 #endif
7458 win_T *wp;
7459 int outofmem = FALSE;
7460 int len;
7461 schar_T *new_ScreenLines;
7462 #ifdef FEAT_MBYTE
7463 u8char_T *new_ScreenLinesUC = NULL;
7464 u8char_T *new_ScreenLinesC[MAX_MCO];
7465 schar_T *new_ScreenLines2 = NULL;
7466 int i;
7467 #endif
7468 sattr_T *new_ScreenAttrs;
7469 unsigned *new_LineOffset;
7470 char_u *new_LineWraps;
7471 #ifdef FEAT_WINDOWS
7472 short *new_TabPageIdxs;
7473 tabpage_T *tp;
7474 #endif
7475 static int entered = FALSE; /* avoid recursiveness */
7476 static int done_outofmem_msg = FALSE; /* did outofmem message */
7477 #ifdef FEAT_AUTOCMD
7478 int retry_count = 0;
7480 retry:
7481 #endif
7483 * Allocation of the screen buffers is done only when the size changes and
7484 * when Rows and Columns have been set and we have started doing full
7485 * screen stuff.
7487 if ((ScreenLines != NULL
7488 && Rows == screen_Rows
7489 && Columns == screen_Columns
7490 #ifdef FEAT_MBYTE
7491 && enc_utf8 == (ScreenLinesUC != NULL)
7492 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
7493 && p_mco == Screen_mco
7494 #endif
7496 || Rows == 0
7497 || Columns == 0
7498 || (!full_screen && ScreenLines == NULL))
7499 return;
7502 * It's possible that we produce an out-of-memory message below, which
7503 * will cause this function to be called again. To break the loop, just
7504 * return here.
7506 if (entered)
7507 return;
7508 entered = TRUE;
7511 * Note that the window sizes are updated before reallocating the arrays,
7512 * thus we must not redraw here!
7514 ++RedrawingDisabled;
7516 win_new_shellsize(); /* fit the windows in the new sized shell */
7518 comp_col(); /* recompute columns for shown command and ruler */
7521 * We're changing the size of the screen.
7522 * - Allocate new arrays for ScreenLines and ScreenAttrs.
7523 * - Move lines from the old arrays into the new arrays, clear extra
7524 * lines (unless the screen is going to be cleared).
7525 * - Free the old arrays.
7527 * If anything fails, make ScreenLines NULL, so we don't do anything!
7528 * Continuing with the old ScreenLines may result in a crash, because the
7529 * size is wrong.
7531 FOR_ALL_TAB_WINDOWS(tp, wp)
7532 win_free_lsize(wp);
7533 #ifdef FEAT_AUTOCMD
7534 if (aucmd_win != NULL)
7535 win_free_lsize(aucmd_win);
7536 #endif
7538 new_ScreenLines = (schar_T *)lalloc((long_u)(
7539 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7540 #ifdef FEAT_MBYTE
7541 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T) * MAX_MCO);
7542 if (enc_utf8)
7544 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
7545 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
7546 for (i = 0; i < p_mco; ++i)
7547 new_ScreenLinesC[i] = (u8char_T *)lalloc((long_u)(
7548 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
7550 if (enc_dbcs == DBCS_JPNU)
7551 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
7552 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7553 #endif
7554 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
7555 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
7556 new_LineOffset = (unsigned *)lalloc((long_u)(
7557 Rows * sizeof(unsigned)), FALSE);
7558 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
7559 #ifdef FEAT_WINDOWS
7560 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
7561 #endif
7563 FOR_ALL_TAB_WINDOWS(tp, wp)
7565 if (win_alloc_lines(wp) == FAIL)
7567 outofmem = TRUE;
7568 #ifdef FEAT_WINDOWS
7569 goto give_up;
7570 #endif
7573 #ifdef FEAT_AUTOCMD
7574 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
7575 && win_alloc_lines(aucmd_win) == FAIL)
7576 outofmem = TRUE;
7577 #endif
7578 #ifdef FEAT_WINDOWS
7579 give_up:
7580 #endif
7582 #ifdef FEAT_MBYTE
7583 for (i = 0; i < p_mco; ++i)
7584 if (new_ScreenLinesC[i] == NULL)
7585 break;
7586 #endif
7587 if (new_ScreenLines == NULL
7588 #ifdef FEAT_MBYTE
7589 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
7590 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
7591 #endif
7592 || new_ScreenAttrs == NULL
7593 || new_LineOffset == NULL
7594 || new_LineWraps == NULL
7595 #ifdef FEAT_WINDOWS
7596 || new_TabPageIdxs == NULL
7597 #endif
7598 || outofmem)
7600 if (ScreenLines != NULL || !done_outofmem_msg)
7602 /* guess the size */
7603 do_outofmem_msg((long_u)((Rows + 1) * Columns));
7605 /* Remember we did this to avoid getting outofmem messages over
7606 * and over again. */
7607 done_outofmem_msg = TRUE;
7609 vim_free(new_ScreenLines);
7610 new_ScreenLines = NULL;
7611 #ifdef FEAT_MBYTE
7612 vim_free(new_ScreenLinesUC);
7613 new_ScreenLinesUC = NULL;
7614 for (i = 0; i < p_mco; ++i)
7616 vim_free(new_ScreenLinesC[i]);
7617 new_ScreenLinesC[i] = NULL;
7619 vim_free(new_ScreenLines2);
7620 new_ScreenLines2 = NULL;
7621 #endif
7622 vim_free(new_ScreenAttrs);
7623 new_ScreenAttrs = NULL;
7624 vim_free(new_LineOffset);
7625 new_LineOffset = NULL;
7626 vim_free(new_LineWraps);
7627 new_LineWraps = NULL;
7628 #ifdef FEAT_WINDOWS
7629 vim_free(new_TabPageIdxs);
7630 new_TabPageIdxs = NULL;
7631 #endif
7633 else
7635 done_outofmem_msg = FALSE;
7637 for (new_row = 0; new_row < Rows; ++new_row)
7639 new_LineOffset[new_row] = new_row * Columns;
7640 new_LineWraps[new_row] = FALSE;
7643 * If the screen is not going to be cleared, copy as much as
7644 * possible from the old screen to the new one and clear the rest
7645 * (used when resizing the window at the "--more--" prompt or when
7646 * executing an external command, for the GUI).
7648 if (!clear)
7650 (void)vim_memset(new_ScreenLines + new_row * Columns,
7651 ' ', (size_t)Columns * sizeof(schar_T));
7652 #ifdef FEAT_MBYTE
7653 if (enc_utf8)
7655 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
7656 0, (size_t)Columns * sizeof(u8char_T));
7657 for (i = 0; i < p_mco; ++i)
7658 (void)vim_memset(new_ScreenLinesC[i]
7659 + new_row * Columns,
7660 0, (size_t)Columns * sizeof(u8char_T));
7662 if (enc_dbcs == DBCS_JPNU)
7663 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
7664 0, (size_t)Columns * sizeof(schar_T));
7665 #endif
7666 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
7667 0, (size_t)Columns * sizeof(sattr_T));
7668 old_row = new_row + (screen_Rows - Rows);
7669 if (old_row >= 0 && ScreenLines != NULL)
7671 if (screen_Columns < Columns)
7672 len = screen_Columns;
7673 else
7674 len = Columns;
7675 #ifdef FEAT_MBYTE
7676 /* When switching to utf-8 don't copy characters, they
7677 * may be invalid now. Also when p_mco changes. */
7678 if (!(enc_utf8 && ScreenLinesUC == NULL)
7679 && p_mco == Screen_mco)
7680 #endif
7681 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
7682 ScreenLines + LineOffset[old_row],
7683 (size_t)len * sizeof(schar_T));
7684 #ifdef FEAT_MBYTE
7685 if (enc_utf8 && ScreenLinesUC != NULL
7686 && p_mco == Screen_mco)
7688 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
7689 ScreenLinesUC + LineOffset[old_row],
7690 (size_t)len * sizeof(u8char_T));
7691 for (i = 0; i < p_mco; ++i)
7692 mch_memmove(new_ScreenLinesC[i]
7693 + new_LineOffset[new_row],
7694 ScreenLinesC[i] + LineOffset[old_row],
7695 (size_t)len * sizeof(u8char_T));
7697 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
7698 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
7699 ScreenLines2 + LineOffset[old_row],
7700 (size_t)len * sizeof(schar_T));
7701 #endif
7702 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
7703 ScreenAttrs + LineOffset[old_row],
7704 (size_t)len * sizeof(sattr_T));
7708 /* Use the last line of the screen for the current line. */
7709 current_ScreenLine = new_ScreenLines + Rows * Columns;
7712 free_screenlines();
7714 ScreenLines = new_ScreenLines;
7715 #ifdef FEAT_MBYTE
7716 ScreenLinesUC = new_ScreenLinesUC;
7717 for (i = 0; i < p_mco; ++i)
7718 ScreenLinesC[i] = new_ScreenLinesC[i];
7719 Screen_mco = p_mco;
7720 ScreenLines2 = new_ScreenLines2;
7721 #endif
7722 ScreenAttrs = new_ScreenAttrs;
7723 LineOffset = new_LineOffset;
7724 LineWraps = new_LineWraps;
7725 #ifdef FEAT_WINDOWS
7726 TabPageIdxs = new_TabPageIdxs;
7727 #endif
7729 /* It's important that screen_Rows and screen_Columns reflect the actual
7730 * size of ScreenLines[]. Set them before calling anything. */
7731 #ifdef FEAT_GUI
7732 old_Rows = screen_Rows;
7733 #endif
7734 screen_Rows = Rows;
7735 screen_Columns = Columns;
7737 must_redraw = CLEAR; /* need to clear the screen later */
7738 if (clear)
7739 screenclear2();
7741 #ifdef FEAT_GUI
7742 else if (gui.in_use
7743 && !gui.starting
7744 && ScreenLines != NULL
7745 && old_Rows != Rows)
7747 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
7749 * Adjust the position of the cursor, for when executing an external
7750 * command.
7752 if (msg_row >= Rows) /* Rows got smaller */
7753 msg_row = Rows - 1; /* put cursor at last row */
7754 else if (Rows > old_Rows) /* Rows got bigger */
7755 msg_row += Rows - old_Rows; /* put cursor in same place */
7756 if (msg_col >= Columns) /* Columns got smaller */
7757 msg_col = Columns - 1; /* put cursor at last column */
7759 #endif
7761 entered = FALSE;
7762 --RedrawingDisabled;
7764 #ifdef FEAT_AUTOCMD
7766 * Do not apply autocommands more than 3 times to avoid an endless loop
7767 * in case applying autocommands always changes Rows or Columns.
7769 if (starting == 0 && ++retry_count <= 3)
7771 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
7772 /* In rare cases, autocommands may have altered Rows or Columns,
7773 * jump back to check if we need to allocate the screen again. */
7774 goto retry;
7776 #endif
7779 void
7780 free_screenlines()
7782 #ifdef FEAT_MBYTE
7783 int i;
7785 vim_free(ScreenLinesUC);
7786 for (i = 0; i < Screen_mco; ++i)
7787 vim_free(ScreenLinesC[i]);
7788 vim_free(ScreenLines2);
7789 #endif
7790 vim_free(ScreenLines);
7791 vim_free(ScreenAttrs);
7792 vim_free(LineOffset);
7793 vim_free(LineWraps);
7794 #ifdef FEAT_WINDOWS
7795 vim_free(TabPageIdxs);
7796 #endif
7799 void
7800 screenclear()
7802 check_for_delay(FALSE);
7803 screenalloc(FALSE); /* allocate screen buffers if size changed */
7804 screenclear2(); /* clear the screen */
7807 static void
7808 screenclear2()
7810 int i;
7812 if (starting == NO_SCREEN || ScreenLines == NULL
7813 #ifdef FEAT_GUI
7814 || (gui.in_use && gui.starting)
7815 #endif
7817 return;
7819 #ifdef FEAT_GUI
7820 if (!gui.in_use)
7821 #endif
7822 screen_attr = -1; /* force setting the Normal colors */
7823 screen_stop_highlight(); /* don't want highlighting here */
7825 #ifdef FEAT_CLIPBOARD
7826 /* disable selection without redrawing it */
7827 clip_scroll_selection(9999);
7828 #endif
7830 /* blank out ScreenLines */
7831 for (i = 0; i < Rows; ++i)
7833 lineclear(LineOffset[i], (int)Columns);
7834 LineWraps[i] = FALSE;
7837 if (can_clear(T_CL))
7839 out_str(T_CL); /* clear the display */
7840 clear_cmdline = FALSE;
7841 mode_displayed = FALSE;
7843 else
7845 /* can't clear the screen, mark all chars with invalid attributes */
7846 for (i = 0; i < Rows; ++i)
7847 lineinvalid(LineOffset[i], (int)Columns);
7848 clear_cmdline = TRUE;
7851 screen_cleared = TRUE; /* can use contents of ScreenLines now */
7853 win_rest_invalid(firstwin);
7854 redraw_cmdline = TRUE;
7855 #ifdef FEAT_WINDOWS
7856 redraw_tabline = TRUE;
7857 #endif
7858 if (must_redraw == CLEAR) /* no need to clear again */
7859 must_redraw = NOT_VALID;
7860 compute_cmdrow();
7861 msg_row = cmdline_row; /* put cursor on last line for messages */
7862 msg_col = 0;
7863 screen_start(); /* don't know where cursor is now */
7864 msg_scrolled = 0; /* can't scroll back */
7865 msg_didany = FALSE;
7866 msg_didout = FALSE;
7870 * Clear one line in ScreenLines.
7872 static void
7873 lineclear(off, width)
7874 unsigned off;
7875 int width;
7877 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
7878 #ifdef FEAT_MBYTE
7879 if (enc_utf8)
7880 (void)vim_memset(ScreenLinesUC + off, 0,
7881 (size_t)width * sizeof(u8char_T));
7882 #endif
7883 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
7887 * Mark one line in ScreenLines invalid by setting the attributes to an
7888 * invalid value.
7890 static void
7891 lineinvalid(off, width)
7892 unsigned off;
7893 int width;
7895 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
7898 #ifdef FEAT_VERTSPLIT
7900 * Copy part of a Screenline for vertically split window "wp".
7902 static void
7903 linecopy(to, from, wp)
7904 int to;
7905 int from;
7906 win_T *wp;
7908 unsigned off_to = LineOffset[to] + wp->w_wincol;
7909 unsigned off_from = LineOffset[from] + wp->w_wincol;
7911 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
7912 wp->w_width * sizeof(schar_T));
7913 # ifdef FEAT_MBYTE
7914 if (enc_utf8)
7916 int i;
7918 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
7919 wp->w_width * sizeof(u8char_T));
7920 for (i = 0; i < p_mco; ++i)
7921 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
7922 wp->w_width * sizeof(u8char_T));
7924 if (enc_dbcs == DBCS_JPNU)
7925 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
7926 wp->w_width * sizeof(schar_T));
7927 # endif
7928 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
7929 wp->w_width * sizeof(sattr_T));
7931 #endif
7934 * Return TRUE if clearing with term string "p" would work.
7935 * It can't work when the string is empty or it won't set the right background.
7938 can_clear(p)
7939 char_u *p;
7941 return (*p != NUL && (t_colors <= 1
7942 #ifdef FEAT_GUI
7943 || gui.in_use
7944 #endif
7945 || cterm_normal_bg_color == 0 || *T_UT != NUL));
7949 * Reset cursor position. Use whenever cursor was moved because of outputting
7950 * something directly to the screen (shell commands) or a terminal control
7951 * code.
7953 void
7954 screen_start()
7956 screen_cur_row = screen_cur_col = 9999;
7960 * Move the cursor to position "row","col" in the screen.
7961 * This tries to find the most efficient way to move, minimizing the number of
7962 * characters sent to the terminal.
7964 void
7965 windgoto(row, col)
7966 int row;
7967 int col;
7969 sattr_T *p;
7970 int i;
7971 int plan;
7972 int cost;
7973 int wouldbe_col;
7974 int noinvcurs;
7975 char_u *bs;
7976 int goto_cost;
7977 int attr;
7979 #define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
7980 #define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
7982 #define PLAN_LE 1
7983 #define PLAN_CR 2
7984 #define PLAN_NL 3
7985 #define PLAN_WRITE 4
7986 /* Can't use ScreenLines unless initialized */
7987 if (ScreenLines == NULL)
7988 return;
7990 if (col != screen_cur_col || row != screen_cur_row)
7992 /* Check for valid position. */
7993 if (row < 0) /* window without text lines? */
7994 row = 0;
7995 if (row >= screen_Rows)
7996 row = screen_Rows - 1;
7997 if (col >= screen_Columns)
7998 col = screen_Columns - 1;
8000 /* check if no cursor movement is allowed in highlight mode */
8001 if (screen_attr && *T_MS == NUL)
8002 noinvcurs = HIGHL_COST;
8003 else
8004 noinvcurs = 0;
8005 goto_cost = GOTO_COST + noinvcurs;
8008 * Plan how to do the positioning:
8009 * 1. Use CR to move it to column 0, same row.
8010 * 2. Use T_LE to move it a few columns to the left.
8011 * 3. Use NL to move a few lines down, column 0.
8012 * 4. Move a few columns to the right with T_ND or by writing chars.
8014 * Don't do this if the cursor went beyond the last column, the cursor
8015 * position is unknown then (some terminals wrap, some don't )
8017 * First check if the highlighting attributes allow us to write
8018 * characters to move the cursor to the right.
8020 if (row >= screen_cur_row && screen_cur_col < Columns)
8023 * If the cursor is in the same row, bigger col, we can use CR
8024 * or T_LE.
8026 bs = NULL; /* init for GCC */
8027 attr = screen_attr;
8028 if (row == screen_cur_row && col < screen_cur_col)
8030 /* "le" is preferred over "bc", because "bc" is obsolete */
8031 if (*T_LE)
8032 bs = T_LE; /* "cursor left" */
8033 else
8034 bs = T_BC; /* "backspace character (old) */
8035 if (*bs)
8036 cost = (screen_cur_col - col) * (int)STRLEN(bs);
8037 else
8038 cost = 999;
8039 if (col + 1 < cost) /* using CR is less characters */
8041 plan = PLAN_CR;
8042 wouldbe_col = 0;
8043 cost = 1; /* CR is just one character */
8045 else
8047 plan = PLAN_LE;
8048 wouldbe_col = col;
8050 if (noinvcurs) /* will stop highlighting */
8052 cost += noinvcurs;
8053 attr = 0;
8058 * If the cursor is above where we want to be, we can use CR LF.
8060 else if (row > screen_cur_row)
8062 plan = PLAN_NL;
8063 wouldbe_col = 0;
8064 cost = (row - screen_cur_row) * 2; /* CR LF */
8065 if (noinvcurs) /* will stop highlighting */
8067 cost += noinvcurs;
8068 attr = 0;
8073 * If the cursor is in the same row, smaller col, just use write.
8075 else
8077 plan = PLAN_WRITE;
8078 wouldbe_col = screen_cur_col;
8079 cost = 0;
8083 * Check if any characters that need to be written have the
8084 * correct attributes. Also avoid UTF-8 characters.
8086 i = col - wouldbe_col;
8087 if (i > 0)
8088 cost += i;
8089 if (cost < goto_cost && i > 0)
8092 * Check if the attributes are correct without additionally
8093 * stopping highlighting.
8095 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
8096 while (i && *p++ == attr)
8097 --i;
8098 if (i != 0)
8101 * Try if it works when highlighting is stopped here.
8103 if (*--p == 0)
8105 cost += noinvcurs;
8106 while (i && *p++ == 0)
8107 --i;
8109 if (i != 0)
8110 cost = 999; /* different attributes, don't do it */
8112 #ifdef FEAT_MBYTE
8113 if (enc_utf8)
8115 /* Don't use an UTF-8 char for positioning, it's slow. */
8116 for (i = wouldbe_col; i < col; ++i)
8117 if (ScreenLinesUC[LineOffset[row] + i] != 0)
8119 cost = 999;
8120 break;
8123 #endif
8127 * We can do it without term_windgoto()!
8129 if (cost < goto_cost)
8131 if (plan == PLAN_LE)
8133 if (noinvcurs)
8134 screen_stop_highlight();
8135 while (screen_cur_col > col)
8137 out_str(bs);
8138 --screen_cur_col;
8141 else if (plan == PLAN_CR)
8143 if (noinvcurs)
8144 screen_stop_highlight();
8145 out_char('\r');
8146 screen_cur_col = 0;
8148 else if (plan == PLAN_NL)
8150 if (noinvcurs)
8151 screen_stop_highlight();
8152 while (screen_cur_row < row)
8154 out_char('\n');
8155 ++screen_cur_row;
8157 screen_cur_col = 0;
8160 i = col - screen_cur_col;
8161 if (i > 0)
8164 * Use cursor-right if it's one character only. Avoids
8165 * removing a line of pixels from the last bold char, when
8166 * using the bold trick in the GUI.
8168 if (T_ND[0] != NUL && T_ND[1] == NUL)
8170 while (i-- > 0)
8171 out_char(*T_ND);
8173 else
8175 int off;
8177 off = LineOffset[row] + screen_cur_col;
8178 while (i-- > 0)
8180 if (ScreenAttrs[off] != screen_attr)
8181 screen_stop_highlight();
8182 #ifdef FEAT_MBYTE
8183 out_flush_check();
8184 #endif
8185 out_char(ScreenLines[off]);
8186 #ifdef FEAT_MBYTE
8187 if (enc_dbcs == DBCS_JPNU
8188 && ScreenLines[off] == 0x8e)
8189 out_char(ScreenLines2[off]);
8190 #endif
8191 ++off;
8197 else
8198 cost = 999;
8200 if (cost >= goto_cost)
8202 if (noinvcurs)
8203 screen_stop_highlight();
8204 if (row == screen_cur_row && (col > screen_cur_col) &&
8205 *T_CRI != NUL)
8206 term_cursor_right(col - screen_cur_col);
8207 else
8208 term_windgoto(row, col);
8210 screen_cur_row = row;
8211 screen_cur_col = col;
8216 * Set cursor to its position in the current window.
8218 void
8219 setcursor()
8221 if (redrawing())
8223 validate_cursor();
8224 windgoto(W_WINROW(curwin) + curwin->w_wrow,
8225 W_WINCOL(curwin) + (
8226 #ifdef FEAT_RIGHTLEFT
8227 /* With 'rightleft' set and the cursor on a double-wide
8228 * character, position it on the leftmost column. */
8229 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
8230 # ifdef FEAT_MBYTE
8231 (has_mbyte
8232 && (*mb_ptr2cells)(ml_get_cursor()) == 2
8233 && vim_isprintc(gchar_cursor())) ? 2 :
8234 # endif
8235 1)) :
8236 #endif
8237 curwin->w_wcol));
8243 * insert 'line_count' lines at 'row' in window 'wp'
8244 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
8245 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
8246 * scrolling.
8247 * Returns FAIL if the lines are not inserted, OK for success.
8250 win_ins_lines(wp, row, line_count, invalid, mayclear)
8251 win_T *wp;
8252 int row;
8253 int line_count;
8254 int invalid;
8255 int mayclear;
8257 int did_delete;
8258 int nextrow;
8259 int lastrow;
8260 int retval;
8262 if (invalid)
8263 wp->w_lines_valid = 0;
8265 if (wp->w_height < 5)
8266 return FAIL;
8268 if (line_count > wp->w_height - row)
8269 line_count = wp->w_height - row;
8271 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
8272 if (retval != MAYBE)
8273 return retval;
8276 * If there is a next window or a status line, we first try to delete the
8277 * lines at the bottom to avoid messing what is after the window.
8278 * If this fails and there are following windows, don't do anything to avoid
8279 * messing up those windows, better just redraw.
8281 did_delete = FALSE;
8282 #ifdef FEAT_WINDOWS
8283 if (wp->w_next != NULL || wp->w_status_height)
8285 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8286 line_count, (int)Rows, FALSE, NULL) == OK)
8287 did_delete = TRUE;
8288 else if (wp->w_next)
8289 return FAIL;
8291 #endif
8293 * if no lines deleted, blank the lines that will end up below the window
8295 if (!did_delete)
8297 #ifdef FEAT_WINDOWS
8298 wp->w_redr_status = TRUE;
8299 #endif
8300 redraw_cmdline = TRUE;
8301 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
8302 lastrow = nextrow + line_count;
8303 if (lastrow > Rows)
8304 lastrow = Rows;
8305 screen_fill(nextrow - line_count, lastrow - line_count,
8306 W_WINCOL(wp), (int)W_ENDCOL(wp),
8307 ' ', ' ', 0);
8310 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
8311 == FAIL)
8313 /* deletion will have messed up other windows */
8314 if (did_delete)
8316 #ifdef FEAT_WINDOWS
8317 wp->w_redr_status = TRUE;
8318 #endif
8319 win_rest_invalid(W_NEXT(wp));
8321 return FAIL;
8324 return OK;
8328 * delete "line_count" window lines at "row" in window "wp"
8329 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
8330 * If "mayclear" is TRUE the screen will be cleared if it is faster than
8331 * scrolling
8332 * Return OK for success, FAIL if the lines are not deleted.
8335 win_del_lines(wp, row, line_count, invalid, mayclear)
8336 win_T *wp;
8337 int row;
8338 int line_count;
8339 int invalid;
8340 int mayclear;
8342 int retval;
8344 if (invalid)
8345 wp->w_lines_valid = 0;
8347 if (line_count > wp->w_height - row)
8348 line_count = wp->w_height - row;
8350 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
8351 if (retval != MAYBE)
8352 return retval;
8354 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
8355 (int)Rows, FALSE, NULL) == FAIL)
8356 return FAIL;
8358 #ifdef FEAT_WINDOWS
8360 * If there are windows or status lines below, try to put them at the
8361 * correct place. If we can't do that, they have to be redrawn.
8363 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
8365 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8366 line_count, (int)Rows, NULL) == FAIL)
8368 wp->w_redr_status = TRUE;
8369 win_rest_invalid(wp->w_next);
8373 * If this is the last window and there is no status line, redraw the
8374 * command line later.
8376 else
8377 #endif
8378 redraw_cmdline = TRUE;
8379 return OK;
8383 * Common code for win_ins_lines() and win_del_lines().
8384 * Returns OK or FAIL when the work has been done.
8385 * Returns MAYBE when not finished yet.
8387 static int
8388 win_do_lines(wp, row, line_count, mayclear, del)
8389 win_T *wp;
8390 int row;
8391 int line_count;
8392 int mayclear;
8393 int del;
8395 int retval;
8397 if (!redrawing() || line_count <= 0)
8398 return FAIL;
8400 /* only a few lines left: redraw is faster */
8401 if (mayclear && Rows - line_count < 5
8402 #ifdef FEAT_VERTSPLIT
8403 && wp->w_width == Columns
8404 #endif
8407 screenclear(); /* will set wp->w_lines_valid to 0 */
8408 return FAIL;
8412 * Delete all remaining lines
8414 if (row + line_count >= wp->w_height)
8416 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
8417 W_WINCOL(wp), (int)W_ENDCOL(wp),
8418 ' ', ' ', 0);
8419 return OK;
8423 * when scrolling, the message on the command line should be cleared,
8424 * otherwise it will stay there forever.
8426 clear_cmdline = TRUE;
8429 * If the terminal can set a scroll region, use that.
8430 * Always do this in a vertically split window. This will redraw from
8431 * ScreenLines[] when t_CV isn't defined. That's faster than using
8432 * win_line().
8433 * Don't use a scroll region when we are going to redraw the text, writing
8434 * a character in the lower right corner of the scroll region causes a
8435 * scroll-up in the DJGPP version.
8437 if (scroll_region
8438 #ifdef FEAT_VERTSPLIT
8439 || W_WIDTH(wp) != Columns
8440 #endif
8443 #ifdef FEAT_VERTSPLIT
8444 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8445 #endif
8446 scroll_region_set(wp, row);
8447 if (del)
8448 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
8449 wp->w_height - row, FALSE, wp);
8450 else
8451 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
8452 wp->w_height - row, wp);
8453 #ifdef FEAT_VERTSPLIT
8454 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8455 #endif
8456 scroll_region_reset();
8457 return retval;
8460 #ifdef FEAT_WINDOWS
8461 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
8462 return FAIL;
8463 #endif
8465 return MAYBE;
8469 * window 'wp' and everything after it is messed up, mark it for redraw
8471 static void
8472 win_rest_invalid(wp)
8473 win_T *wp;
8475 #ifdef FEAT_WINDOWS
8476 while (wp != NULL)
8477 #else
8478 if (wp != NULL)
8479 #endif
8481 redraw_win_later(wp, NOT_VALID);
8482 #ifdef FEAT_WINDOWS
8483 wp->w_redr_status = TRUE;
8484 wp = wp->w_next;
8485 #endif
8487 redraw_cmdline = TRUE;
8491 * The rest of the routines in this file perform screen manipulations. The
8492 * given operation is performed physically on the screen. The corresponding
8493 * change is also made to the internal screen image. In this way, the editor
8494 * anticipates the effect of editing changes on the appearance of the screen.
8495 * That way, when we call screenupdate a complete redraw isn't usually
8496 * necessary. Another advantage is that we can keep adding code to anticipate
8497 * screen changes, and in the meantime, everything still works.
8501 * types for inserting or deleting lines
8503 #define USE_T_CAL 1
8504 #define USE_T_CDL 2
8505 #define USE_T_AL 3
8506 #define USE_T_CE 4
8507 #define USE_T_DL 5
8508 #define USE_T_SR 6
8509 #define USE_NL 7
8510 #define USE_T_CD 8
8511 #define USE_REDRAW 9
8514 * insert lines on the screen and update ScreenLines[]
8515 * 'end' is the line after the scrolled part. Normally it is Rows.
8516 * When scrolling region used 'off' is the offset from the top for the region.
8517 * 'row' and 'end' are relative to the start of the region.
8519 * return FAIL for failure, OK for success.
8522 screen_ins_lines(off, row, line_count, end, wp)
8523 int off;
8524 int row;
8525 int line_count;
8526 int end;
8527 win_T *wp; /* NULL or window to use width from */
8529 int i;
8530 int j;
8531 unsigned temp;
8532 int cursor_row;
8533 int type;
8534 int result_empty;
8535 int can_ce = can_clear(T_CE);
8538 * FAIL if
8539 * - there is no valid screen
8540 * - the screen has to be redrawn completely
8541 * - the line count is less than one
8542 * - the line count is more than 'ttyscroll'
8544 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
8545 return FAIL;
8548 * There are seven ways to insert lines:
8549 * 0. When in a vertically split window and t_CV isn't set, redraw the
8550 * characters from ScreenLines[].
8551 * 1. Use T_CD (clear to end of display) if it exists and the result of
8552 * the insert is just empty lines
8553 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
8554 * present or line_count > 1. It looks better if we do all the inserts
8555 * at once.
8556 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
8557 * insert is just empty lines and T_CE is not present or line_count >
8558 * 1.
8559 * 4. Use T_AL (insert line) if it exists.
8560 * 5. Use T_CE (erase line) if it exists and the result of the insert is
8561 * just empty lines.
8562 * 6. Use T_DL (delete line) if it exists and the result of the insert is
8563 * just empty lines.
8564 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
8565 * the 'da' flag is not set or we have clear line capability.
8566 * 8. redraw the characters from ScreenLines[].
8568 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
8569 * the scrollbar for the window. It does have insert line, use that if it
8570 * exists.
8572 result_empty = (row + line_count >= end);
8573 #ifdef FEAT_VERTSPLIT
8574 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
8575 type = USE_REDRAW;
8576 else
8577 #endif
8578 if (can_clear(T_CD) && result_empty)
8579 type = USE_T_CD;
8580 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
8581 type = USE_T_CAL;
8582 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
8583 type = USE_T_CDL;
8584 else if (*T_AL != NUL)
8585 type = USE_T_AL;
8586 else if (can_ce && result_empty)
8587 type = USE_T_CE;
8588 else if (*T_DL != NUL && result_empty)
8589 type = USE_T_DL;
8590 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
8591 type = USE_T_SR;
8592 else
8593 return FAIL;
8596 * For clearing the lines screen_del_lines() is used. This will also take
8597 * care of t_db if necessary.
8599 if (type == USE_T_CD || type == USE_T_CDL ||
8600 type == USE_T_CE || type == USE_T_DL)
8601 return screen_del_lines(off, row, line_count, end, FALSE, wp);
8604 * If text is retained below the screen, first clear or delete as many
8605 * lines at the bottom of the window as are about to be inserted so that
8606 * the deleted lines won't later surface during a screen_del_lines.
8608 if (*T_DB)
8609 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
8611 #ifdef FEAT_CLIPBOARD
8612 /* Remove a modeless selection when inserting lines halfway the screen
8613 * or not the full width of the screen. */
8614 if (off + row > 0
8615 # ifdef FEAT_VERTSPLIT
8616 || (wp != NULL && wp->w_width != Columns)
8617 # endif
8619 clip_clear_selection();
8620 else
8621 clip_scroll_selection(-line_count);
8622 #endif
8624 #ifdef FEAT_GUI
8625 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
8626 * scrolling is actually carried out. */
8627 gui_dont_update_cursor();
8628 #endif
8630 if (*T_CCS != NUL) /* cursor relative to region */
8631 cursor_row = row;
8632 else
8633 cursor_row = row + off;
8636 * Shift LineOffset[] line_count down to reflect the inserted lines.
8637 * Clear the inserted lines in ScreenLines[].
8639 row += off;
8640 end += off;
8641 for (i = 0; i < line_count; ++i)
8643 #ifdef FEAT_VERTSPLIT
8644 if (wp != NULL && wp->w_width != Columns)
8646 /* need to copy part of a line */
8647 j = end - 1 - i;
8648 while ((j -= line_count) >= row)
8649 linecopy(j + line_count, j, wp);
8650 j += line_count;
8651 if (can_clear((char_u *)" "))
8652 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
8653 else
8654 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
8655 LineWraps[j] = FALSE;
8657 else
8658 #endif
8660 j = end - 1 - i;
8661 temp = LineOffset[j];
8662 while ((j -= line_count) >= row)
8664 LineOffset[j + line_count] = LineOffset[j];
8665 LineWraps[j + line_count] = LineWraps[j];
8667 LineOffset[j + line_count] = temp;
8668 LineWraps[j + line_count] = FALSE;
8669 if (can_clear((char_u *)" "))
8670 lineclear(temp, (int)Columns);
8671 else
8672 lineinvalid(temp, (int)Columns);
8676 screen_stop_highlight();
8677 windgoto(cursor_row, 0);
8679 #ifdef FEAT_VERTSPLIT
8680 /* redraw the characters */
8681 if (type == USE_REDRAW)
8682 redraw_block(row, end, wp);
8683 else
8684 #endif
8685 if (type == USE_T_CAL)
8687 term_append_lines(line_count);
8688 screen_start(); /* don't know where cursor is now */
8690 else
8692 for (i = 0; i < line_count; i++)
8694 if (type == USE_T_AL)
8696 if (i && cursor_row != 0)
8697 windgoto(cursor_row, 0);
8698 out_str(T_AL);
8700 else /* type == USE_T_SR */
8701 out_str(T_SR);
8702 screen_start(); /* don't know where cursor is now */
8707 * With scroll-reverse and 'da' flag set we need to clear the lines that
8708 * have been scrolled down into the region.
8710 if (type == USE_T_SR && *T_DA)
8712 for (i = 0; i < line_count; ++i)
8714 windgoto(off + i, 0);
8715 out_str(T_CE);
8716 screen_start(); /* don't know where cursor is now */
8720 #ifdef FEAT_GUI
8721 gui_can_update_cursor();
8722 if (gui.in_use)
8723 out_flush(); /* always flush after a scroll */
8724 #endif
8725 return OK;
8729 * delete lines on the screen and update ScreenLines[]
8730 * 'end' is the line after the scrolled part. Normally it is Rows.
8731 * When scrolling region used 'off' is the offset from the top for the region.
8732 * 'row' and 'end' are relative to the start of the region.
8734 * Return OK for success, FAIL if the lines are not deleted.
8737 screen_del_lines(off, row, line_count, end, force, wp)
8738 int off;
8739 int row;
8740 int line_count;
8741 int end;
8742 int force; /* even when line_count > p_ttyscroll */
8743 win_T *wp UNUSED; /* NULL or window to use width from */
8745 int j;
8746 int i;
8747 unsigned temp;
8748 int cursor_row;
8749 int cursor_end;
8750 int result_empty; /* result is empty until end of region */
8751 int can_delete; /* deleting line codes can be used */
8752 int type;
8755 * FAIL if
8756 * - there is no valid screen
8757 * - the screen has to be redrawn completely
8758 * - the line count is less than one
8759 * - the line count is more than 'ttyscroll'
8761 if (!screen_valid(TRUE) || line_count <= 0 ||
8762 (!force && line_count > p_ttyscroll))
8763 return FAIL;
8766 * Check if the rest of the current region will become empty.
8768 result_empty = row + line_count >= end;
8771 * We can delete lines only when 'db' flag not set or when 'ce' option
8772 * available.
8774 can_delete = (*T_DB == NUL || can_clear(T_CE));
8777 * There are six ways to delete lines:
8778 * 0. When in a vertically split window and t_CV isn't set, redraw the
8779 * characters from ScreenLines[].
8780 * 1. Use T_CD if it exists and the result is empty.
8781 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
8782 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
8783 * none of the other ways work.
8784 * 4. Use T_CE (erase line) if the result is empty.
8785 * 5. Use T_DL (delete line) if it exists.
8786 * 6. redraw the characters from ScreenLines[].
8788 #ifdef FEAT_VERTSPLIT
8789 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
8790 type = USE_REDRAW;
8791 else
8792 #endif
8793 if (can_clear(T_CD) && result_empty)
8794 type = USE_T_CD;
8795 #if defined(__BEOS__) && defined(BEOS_DR8)
8797 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
8798 * its internal termcap... this works okay for tests which test *T_DB !=
8799 * NUL. It has the disadvantage that the user cannot use any :set t_*
8800 * command to get T_DB (back) to empty_option, only :set term=... will do
8801 * the trick...
8802 * Anyway, this hack will hopefully go away with the next OS release.
8803 * (Olaf Seibert)
8805 else if (row == 0 && T_DB == empty_option
8806 && (line_count == 1 || *T_CDL == NUL))
8807 #else
8808 else if (row == 0 && (
8809 #ifndef AMIGA
8810 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
8811 * up, so use delete-line command */
8812 line_count == 1 ||
8813 #endif
8814 *T_CDL == NUL))
8815 #endif
8816 type = USE_NL;
8817 else if (*T_CDL != NUL && line_count > 1 && can_delete)
8818 type = USE_T_CDL;
8819 else if (can_clear(T_CE) && result_empty
8820 #ifdef FEAT_VERTSPLIT
8821 && (wp == NULL || wp->w_width == Columns)
8822 #endif
8824 type = USE_T_CE;
8825 else if (*T_DL != NUL && can_delete)
8826 type = USE_T_DL;
8827 else if (*T_CDL != NUL && can_delete)
8828 type = USE_T_CDL;
8829 else
8830 return FAIL;
8832 #ifdef FEAT_CLIPBOARD
8833 /* Remove a modeless selection when deleting lines halfway the screen or
8834 * not the full width of the screen. */
8835 if (off + row > 0
8836 # ifdef FEAT_VERTSPLIT
8837 || (wp != NULL && wp->w_width != Columns)
8838 # endif
8840 clip_clear_selection();
8841 else
8842 clip_scroll_selection(line_count);
8843 #endif
8845 #ifdef FEAT_GUI
8846 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
8847 * scrolling is actually carried out. */
8848 gui_dont_update_cursor();
8849 #endif
8851 if (*T_CCS != NUL) /* cursor relative to region */
8853 cursor_row = row;
8854 cursor_end = end;
8856 else
8858 cursor_row = row + off;
8859 cursor_end = end + off;
8863 * Now shift LineOffset[] line_count up to reflect the deleted lines.
8864 * Clear the inserted lines in ScreenLines[].
8866 row += off;
8867 end += off;
8868 for (i = 0; i < line_count; ++i)
8870 #ifdef FEAT_VERTSPLIT
8871 if (wp != NULL && wp->w_width != Columns)
8873 /* need to copy part of a line */
8874 j = row + i;
8875 while ((j += line_count) <= end - 1)
8876 linecopy(j - line_count, j, wp);
8877 j -= line_count;
8878 if (can_clear((char_u *)" "))
8879 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
8880 else
8881 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
8882 LineWraps[j] = FALSE;
8884 else
8885 #endif
8887 /* whole width, moving the line pointers is faster */
8888 j = row + i;
8889 temp = LineOffset[j];
8890 while ((j += line_count) <= end - 1)
8892 LineOffset[j - line_count] = LineOffset[j];
8893 LineWraps[j - line_count] = LineWraps[j];
8895 LineOffset[j - line_count] = temp;
8896 LineWraps[j - line_count] = FALSE;
8897 if (can_clear((char_u *)" "))
8898 lineclear(temp, (int)Columns);
8899 else
8900 lineinvalid(temp, (int)Columns);
8904 screen_stop_highlight();
8906 #ifdef FEAT_VERTSPLIT
8907 /* redraw the characters */
8908 if (type == USE_REDRAW)
8909 redraw_block(row, end, wp);
8910 else
8911 #endif
8912 if (type == USE_T_CD) /* delete the lines */
8914 windgoto(cursor_row, 0);
8915 out_str(T_CD);
8916 screen_start(); /* don't know where cursor is now */
8918 else if (type == USE_T_CDL)
8920 windgoto(cursor_row, 0);
8921 term_delete_lines(line_count);
8922 screen_start(); /* don't know where cursor is now */
8925 * Deleting lines at top of the screen or scroll region: Just scroll
8926 * the whole screen (scroll region) up by outputting newlines on the
8927 * last line.
8929 else if (type == USE_NL)
8931 windgoto(cursor_end - 1, 0);
8932 for (i = line_count; --i >= 0; )
8933 out_char('\n'); /* cursor will remain on same line */
8935 else
8937 for (i = line_count; --i >= 0; )
8939 if (type == USE_T_DL)
8941 windgoto(cursor_row, 0);
8942 out_str(T_DL); /* delete a line */
8944 else /* type == USE_T_CE */
8946 windgoto(cursor_row + i, 0);
8947 out_str(T_CE); /* erase a line */
8949 screen_start(); /* don't know where cursor is now */
8954 * If the 'db' flag is set, we need to clear the lines that have been
8955 * scrolled up at the bottom of the region.
8957 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
8959 for (i = line_count; i > 0; --i)
8961 windgoto(cursor_end - i, 0);
8962 out_str(T_CE); /* erase a line */
8963 screen_start(); /* don't know where cursor is now */
8967 #ifdef FEAT_GUI
8968 gui_can_update_cursor();
8969 if (gui.in_use)
8970 out_flush(); /* always flush after a scroll */
8971 #endif
8973 return OK;
8977 * show the current mode and ruler
8979 * If clear_cmdline is TRUE, clear the rest of the cmdline.
8980 * If clear_cmdline is FALSE there may be a message there that needs to be
8981 * cleared only if a mode is shown.
8982 * Return the length of the message (0 if no message).
8985 showmode()
8987 int need_clear;
8988 int length = 0;
8989 int do_mode;
8990 int attr;
8991 int nwr_save;
8992 #ifdef FEAT_INS_EXPAND
8993 int sub_attr;
8994 #endif
8996 do_mode = ((p_smd && msg_silent == 0)
8997 && ((State & INSERT)
8998 || restart_edit
8999 #ifdef FEAT_VISUAL
9000 || VIsual_active
9001 #endif
9003 if (do_mode || Recording)
9006 * Don't show mode right now, when not redrawing or inside a mapping.
9007 * Call char_avail() only when we are going to show something, because
9008 * it takes a bit of time.
9010 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
9012 redraw_cmdline = TRUE; /* show mode later */
9013 return 0;
9016 nwr_save = need_wait_return;
9018 /* wait a bit before overwriting an important message */
9019 check_for_delay(FALSE);
9021 /* if the cmdline is more than one line high, erase top lines */
9022 need_clear = clear_cmdline;
9023 if (clear_cmdline && cmdline_row < Rows - 1)
9024 msg_clr_cmdline(); /* will reset clear_cmdline */
9026 /* Position on the last line in the window, column 0 */
9027 msg_pos_mode();
9028 cursor_off();
9029 attr = hl_attr(HLF_CM); /* Highlight mode */
9030 if (do_mode)
9032 MSG_PUTS_ATTR("--", attr);
9033 #if defined(FEAT_XIM)
9034 # if 0 /* old version, changed by SungHyun Nam July 2008 */
9035 if (xic != NULL && im_get_status() && !p_imdisable
9036 && curbuf->b_p_iminsert == B_IMODE_IM)
9037 # else
9038 if (
9039 # ifdef HAVE_GTK2
9040 preedit_get_status()
9041 # else
9042 im_get_status()
9043 # endif
9045 # endif
9046 # ifdef HAVE_GTK2 /* most of the time, it's not XIM being used */
9047 MSG_PUTS_ATTR(" IM", attr);
9048 # else
9049 MSG_PUTS_ATTR(" XIM", attr);
9050 # endif
9051 #endif
9052 #if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
9053 if (gui.in_use)
9055 if (hangul_input_state_get())
9056 MSG_PUTS_ATTR(" \307\321\261\333", attr); /* HANGUL */
9058 #endif
9059 #ifdef FEAT_INS_EXPAND
9060 if (edit_submode != NULL) /* CTRL-X in Insert mode */
9062 /* These messages can get long, avoid a wrap in a narrow
9063 * window. Prefer showing edit_submode_extra. */
9064 length = (Rows - msg_row) * Columns - 3;
9065 if (edit_submode_extra != NULL)
9066 length -= vim_strsize(edit_submode_extra);
9067 if (length > 0)
9069 if (edit_submode_pre != NULL)
9070 length -= vim_strsize(edit_submode_pre);
9071 if (length - vim_strsize(edit_submode) > 0)
9073 if (edit_submode_pre != NULL)
9074 msg_puts_attr(edit_submode_pre, attr);
9075 msg_puts_attr(edit_submode, attr);
9077 if (edit_submode_extra != NULL)
9079 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
9080 if ((int)edit_submode_highl < (int)HLF_COUNT)
9081 sub_attr = hl_attr(edit_submode_highl);
9082 else
9083 sub_attr = attr;
9084 msg_puts_attr(edit_submode_extra, sub_attr);
9087 length = 0;
9089 else
9090 #endif
9092 #ifdef FEAT_VREPLACE
9093 if (State & VREPLACE_FLAG)
9094 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
9095 else
9096 #endif
9097 if (State & REPLACE_FLAG)
9098 MSG_PUTS_ATTR(_(" REPLACE"), attr);
9099 else if (State & INSERT)
9101 #ifdef FEAT_RIGHTLEFT
9102 if (p_ri)
9103 MSG_PUTS_ATTR(_(" REVERSE"), attr);
9104 #endif
9105 MSG_PUTS_ATTR(_(" INSERT"), attr);
9107 else if (restart_edit == 'I')
9108 MSG_PUTS_ATTR(_(" (insert)"), attr);
9109 else if (restart_edit == 'R')
9110 MSG_PUTS_ATTR(_(" (replace)"), attr);
9111 else if (restart_edit == 'V')
9112 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
9113 #ifdef FEAT_RIGHTLEFT
9114 if (p_hkmap)
9115 MSG_PUTS_ATTR(_(" Hebrew"), attr);
9116 # ifdef FEAT_FKMAP
9117 if (p_fkmap)
9118 MSG_PUTS_ATTR(farsi_text_5, attr);
9119 # endif
9120 #endif
9121 #ifdef FEAT_KEYMAP
9122 if (State & LANGMAP)
9124 # ifdef FEAT_ARABIC
9125 if (curwin->w_p_arab)
9126 MSG_PUTS_ATTR(_(" Arabic"), attr);
9127 else
9128 # endif
9129 MSG_PUTS_ATTR(_(" (lang)"), attr);
9131 #endif
9132 if ((State & INSERT) && p_paste)
9133 MSG_PUTS_ATTR(_(" (paste)"), attr);
9135 #ifdef FEAT_VISUAL
9136 if (VIsual_active)
9138 char *p;
9140 /* Don't concatenate separate words to avoid translation
9141 * problems. */
9142 switch ((VIsual_select ? 4 : 0)
9143 + (VIsual_mode == Ctrl_V) * 2
9144 + (VIsual_mode == 'V'))
9146 case 0: p = N_(" VISUAL"); break;
9147 case 1: p = N_(" VISUAL LINE"); break;
9148 case 2: p = N_(" VISUAL BLOCK"); break;
9149 case 4: p = N_(" SELECT"); break;
9150 case 5: p = N_(" SELECT LINE"); break;
9151 default: p = N_(" SELECT BLOCK"); break;
9153 MSG_PUTS_ATTR(_(p), attr);
9155 #endif
9156 MSG_PUTS_ATTR(" --", attr);
9159 need_clear = TRUE;
9161 if (Recording
9162 #ifdef FEAT_INS_EXPAND
9163 && edit_submode == NULL /* otherwise it gets too long */
9164 #endif
9167 MSG_PUTS_ATTR(_("recording"), attr);
9168 need_clear = TRUE;
9171 mode_displayed = TRUE;
9172 if (need_clear || clear_cmdline)
9173 msg_clr_eos();
9174 msg_didout = FALSE; /* overwrite this message */
9175 length = msg_col;
9176 msg_col = 0;
9177 need_wait_return = nwr_save; /* never ask for hit-return for this */
9179 else if (clear_cmdline && msg_silent == 0)
9180 /* Clear the whole command line. Will reset "clear_cmdline". */
9181 msg_clr_cmdline();
9183 #ifdef FEAT_CMDL_INFO
9184 # ifdef FEAT_VISUAL
9185 /* In Visual mode the size of the selected area must be redrawn. */
9186 if (VIsual_active)
9187 clear_showcmd();
9188 # endif
9190 /* If the last window has no status line, the ruler is after the mode
9191 * message and must be redrawn */
9192 if (redrawing()
9193 # ifdef FEAT_WINDOWS
9194 && lastwin->w_status_height == 0
9195 # endif
9197 win_redr_ruler(lastwin, TRUE);
9198 #endif
9199 redraw_cmdline = FALSE;
9200 clear_cmdline = FALSE;
9202 return length;
9206 * Position for a mode message.
9208 static void
9209 msg_pos_mode()
9211 msg_col = 0;
9212 msg_row = Rows - 1;
9216 * Delete mode message. Used when ESC is typed which is expected to end
9217 * Insert mode (but Insert mode didn't end yet!).
9218 * Caller should check "mode_displayed".
9220 void
9221 unshowmode(force)
9222 int force;
9225 * Don't delete it right now, when not redrawing or inside a mapping.
9227 if (!redrawing() || (!force && char_avail() && !KeyTyped))
9228 redraw_cmdline = TRUE; /* delete mode later */
9229 else
9231 msg_pos_mode();
9232 if (Recording)
9233 MSG_PUTS_ATTR(_("recording"), hl_attr(HLF_CM));
9234 msg_clr_eos();
9238 #if defined(FEAT_WINDOWS)
9240 * Draw the tab pages line at the top of the Vim window.
9242 static void
9243 draw_tabline()
9245 int tabcount = 0;
9246 tabpage_T *tp;
9247 int tabwidth;
9248 int col = 0;
9249 int scol = 0;
9250 int attr;
9251 win_T *wp;
9252 win_T *cwp;
9253 int wincount;
9254 int modified;
9255 int c;
9256 int len;
9257 int attr_sel = hl_attr(HLF_TPS);
9258 int attr_nosel = hl_attr(HLF_TP);
9259 int attr_fill = hl_attr(HLF_TPF);
9260 char_u *p;
9261 int room;
9262 int use_sep_chars = (t_colors < 8
9263 #ifdef FEAT_GUI
9264 && !gui.in_use
9265 #endif
9268 redraw_tabline = FALSE;
9270 #ifdef FEAT_GUI_TABLINE
9271 /* Take care of a GUI tabline. */
9272 if (gui_use_tabline())
9274 gui_update_tabline();
9275 return;
9277 #endif
9279 if (tabline_height() < 1)
9280 return;
9282 #if defined(FEAT_STL_OPT)
9284 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
9285 for (scol = 0; scol < Columns; ++scol)
9286 TabPageIdxs[scol] = 0;
9288 /* Use the 'tabline' option if it's set. */
9289 if (*p_tal != NUL)
9291 int save_called_emsg = called_emsg;
9293 /* Check for an error. If there is one we would loop in redrawing the
9294 * screen. Avoid that by making 'tabline' empty. */
9295 called_emsg = FALSE;
9296 win_redr_custom(NULL, FALSE);
9297 if (called_emsg)
9298 set_string_option_direct((char_u *)"tabline", -1,
9299 (char_u *)"", OPT_FREE, SID_ERROR);
9300 called_emsg |= save_called_emsg;
9302 else
9303 #endif
9305 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
9306 ++tabcount;
9308 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
9309 if (tabwidth < 6)
9310 tabwidth = 6;
9312 attr = attr_nosel;
9313 tabcount = 0;
9314 scol = 0;
9315 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
9316 tp = tp->tp_next)
9318 scol = col;
9320 if (tp->tp_topframe == topframe)
9321 attr = attr_sel;
9322 if (use_sep_chars && col > 0)
9323 screen_putchar('|', 0, col++, attr);
9325 if (tp->tp_topframe != topframe)
9326 attr = attr_nosel;
9328 screen_putchar(' ', 0, col++, attr);
9330 if (tp == curtab)
9332 cwp = curwin;
9333 wp = firstwin;
9335 else
9337 cwp = tp->tp_curwin;
9338 wp = tp->tp_firstwin;
9341 modified = FALSE;
9342 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
9343 if (bufIsChanged(wp->w_buffer))
9344 modified = TRUE;
9345 if (modified || wincount > 1)
9347 if (wincount > 1)
9349 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
9350 len = (int)STRLEN(NameBuff);
9351 if (col + len >= Columns - 3)
9352 break;
9353 screen_puts_len(NameBuff, len, 0, col,
9354 #if defined(FEAT_SYN_HL)
9355 hl_combine_attr(attr, hl_attr(HLF_T))
9356 #else
9357 attr
9358 #endif
9360 col += len;
9362 if (modified)
9363 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
9364 screen_putchar(' ', 0, col++, attr);
9367 room = scol - col + tabwidth - 1;
9368 if (room > 0)
9370 /* Get buffer name in NameBuff[] */
9371 get_trans_bufname(cwp->w_buffer);
9372 shorten_dir(NameBuff);
9373 len = vim_strsize(NameBuff);
9374 p = NameBuff;
9375 #ifdef FEAT_MBYTE
9376 if (has_mbyte)
9377 while (len > room)
9379 len -= ptr2cells(p);
9380 mb_ptr_adv(p);
9382 else
9383 #endif
9384 if (len > room)
9386 p += len - room;
9387 len = room;
9389 if (len > Columns - col - 1)
9390 len = Columns - col - 1;
9392 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
9393 col += len;
9395 screen_putchar(' ', 0, col++, attr);
9397 /* Store the tab page number in TabPageIdxs[], so that
9398 * jump_to_mouse() knows where each one is. */
9399 ++tabcount;
9400 while (scol < col)
9401 TabPageIdxs[scol++] = tabcount;
9404 if (use_sep_chars)
9405 c = '_';
9406 else
9407 c = ' ';
9408 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
9410 /* Put an "X" for closing the current tab if there are several. */
9411 if (first_tabpage->tp_next != NULL)
9413 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
9414 TabPageIdxs[Columns - 1] = -999;
9418 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
9419 * set. */
9420 redraw_tabline = FALSE;
9424 * Get buffer name for "buf" into NameBuff[].
9425 * Takes care of special buffer names and translates special characters.
9427 void
9428 get_trans_bufname(buf)
9429 buf_T *buf;
9431 if (buf_spname(buf) != NULL)
9432 STRCPY(NameBuff, buf_spname(buf));
9433 else
9434 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
9435 trans_characters(NameBuff, MAXPATHL);
9437 #endif
9439 #if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
9441 * Get the character to use in a status line. Get its attributes in "*attr".
9443 static int
9444 fillchar_status(attr, is_curwin)
9445 int *attr;
9446 int is_curwin;
9448 int fill;
9449 if (is_curwin)
9451 *attr = hl_attr(HLF_S);
9452 fill = fill_stl;
9454 else
9456 *attr = hl_attr(HLF_SNC);
9457 fill = fill_stlnc;
9459 /* Use fill when there is highlighting, and highlighting of current
9460 * window differs, or the fillchars differ, or this is not the
9461 * current window */
9462 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
9463 || !is_curwin || firstwin == lastwin)
9464 || (fill_stl != fill_stlnc)))
9465 return fill;
9466 if (is_curwin)
9467 return '^';
9468 return '=';
9470 #endif
9472 #ifdef FEAT_VERTSPLIT
9474 * Get the character to use in a separator between vertically split windows.
9475 * Get its attributes in "*attr".
9477 static int
9478 fillchar_vsep(attr)
9479 int *attr;
9481 *attr = hl_attr(HLF_C);
9482 if (*attr == 0 && fill_vert == ' ')
9483 return '|';
9484 else
9485 return fill_vert;
9487 #endif
9490 * Return TRUE if redrawing should currently be done.
9493 redrawing()
9495 return (!RedrawingDisabled
9496 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
9500 * Return TRUE if printing messages should currently be done.
9503 messaging()
9505 return (!(p_lz && char_avail() && !KeyTyped));
9509 * Show current status info in ruler and various other places
9510 * If always is FALSE, only show ruler if position has changed.
9512 void
9513 showruler(always)
9514 int always;
9516 if (!always && !redrawing())
9517 return;
9518 #ifdef FEAT_INS_EXPAND
9519 if (pum_visible())
9521 # ifdef FEAT_WINDOWS
9522 /* Don't redraw right now, do it later. */
9523 curwin->w_redr_status = TRUE;
9524 # endif
9525 return;
9527 #endif
9528 #if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
9529 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
9531 redraw_custom_statusline(curwin);
9533 else
9534 #endif
9535 #ifdef FEAT_CMDL_INFO
9536 win_redr_ruler(curwin, always);
9537 #endif
9539 #ifdef FEAT_TITLE
9540 if (need_maketitle
9541 # ifdef FEAT_STL_OPT
9542 || (p_icon && (stl_syntax & STL_IN_ICON))
9543 || (p_title && (stl_syntax & STL_IN_TITLE))
9544 # endif
9546 maketitle();
9547 #endif
9548 #ifdef FEAT_WINDOWS
9549 /* Redraw the tab pages line if needed. */
9550 if (redraw_tabline)
9551 draw_tabline();
9552 #endif
9555 #ifdef FEAT_CMDL_INFO
9556 static void
9557 win_redr_ruler(wp, always)
9558 win_T *wp;
9559 int always;
9561 #define RULER_BUF_LEN 70
9562 char_u buffer[RULER_BUF_LEN];
9563 int row;
9564 int fillchar;
9565 int attr;
9566 int empty_line = FALSE;
9567 colnr_T virtcol;
9568 int i;
9569 size_t len;
9570 int o;
9571 #ifdef FEAT_VERTSPLIT
9572 int this_ru_col;
9573 int off = 0;
9574 int width = Columns;
9575 # define WITH_OFF(x) x
9576 # define WITH_WIDTH(x) x
9577 #else
9578 # define WITH_OFF(x) 0
9579 # define WITH_WIDTH(x) Columns
9580 # define this_ru_col ru_col
9581 #endif
9583 /* If 'ruler' off or redrawing disabled, don't do anything */
9584 if (!p_ru)
9585 return;
9588 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
9589 * after deleting lines, before cursor.lnum is corrected.
9591 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
9592 return;
9594 #ifdef FEAT_INS_EXPAND
9595 /* Don't draw the ruler while doing insert-completion, it might overwrite
9596 * the (long) mode message. */
9597 # ifdef FEAT_WINDOWS
9598 if (wp == lastwin && lastwin->w_status_height == 0)
9599 # endif
9600 if (edit_submode != NULL)
9601 return;
9602 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
9603 if (pum_visible())
9604 return;
9605 #endif
9607 #ifdef FEAT_STL_OPT
9608 if (*p_ruf)
9610 int save_called_emsg = called_emsg;
9612 called_emsg = FALSE;
9613 win_redr_custom(wp, TRUE);
9614 if (called_emsg)
9615 set_string_option_direct((char_u *)"rulerformat", -1,
9616 (char_u *)"", OPT_FREE, SID_ERROR);
9617 called_emsg |= save_called_emsg;
9618 return;
9620 #endif
9623 * Check if not in Insert mode and the line is empty (will show "0-1").
9625 if (!(State & INSERT)
9626 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
9627 empty_line = TRUE;
9630 * Only draw the ruler when something changed.
9632 validate_virtcol_win(wp);
9633 if ( redraw_cmdline
9634 || always
9635 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
9636 || wp->w_cursor.col != wp->w_ru_cursor.col
9637 || wp->w_virtcol != wp->w_ru_virtcol
9638 #ifdef FEAT_VIRTUALEDIT
9639 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
9640 #endif
9641 || wp->w_topline != wp->w_ru_topline
9642 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
9643 #ifdef FEAT_DIFF
9644 || wp->w_topfill != wp->w_ru_topfill
9645 #endif
9646 || empty_line != wp->w_ru_empty)
9648 cursor_off();
9649 #ifdef FEAT_WINDOWS
9650 if (wp->w_status_height)
9652 row = W_WINROW(wp) + wp->w_height;
9653 fillchar = fillchar_status(&attr, wp == curwin);
9654 # ifdef FEAT_VERTSPLIT
9655 off = W_WINCOL(wp);
9656 width = W_WIDTH(wp);
9657 # endif
9659 else
9660 #endif
9662 row = Rows - 1;
9663 fillchar = ' ';
9664 attr = 0;
9665 #ifdef FEAT_VERTSPLIT
9666 width = Columns;
9667 off = 0;
9668 #endif
9671 /* In list mode virtcol needs to be recomputed */
9672 virtcol = wp->w_virtcol;
9673 if (wp->w_p_list && lcs_tab1 == NUL)
9675 wp->w_p_list = FALSE;
9676 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
9677 wp->w_p_list = TRUE;
9681 * Some sprintfs return the length, some return a pointer.
9682 * To avoid portability problems we use strlen() here.
9684 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
9685 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
9686 ? 0L
9687 : (long)(wp->w_cursor.lnum));
9688 len = STRLEN(buffer);
9689 col_print(buffer + len, RULER_BUF_LEN - len,
9690 empty_line ? 0 : (int)wp->w_cursor.col + 1,
9691 (int)virtcol + 1);
9694 * Add a "50%" if there is room for it.
9695 * On the last line, don't print in the last column (scrolls the
9696 * screen up on some terminals).
9698 i = (int)STRLEN(buffer);
9699 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
9700 o = i + vim_strsize(buffer + i + 1);
9701 #ifdef FEAT_WINDOWS
9702 if (wp->w_status_height == 0) /* can't use last char of screen */
9703 #endif
9704 ++o;
9705 #ifdef FEAT_VERTSPLIT
9706 this_ru_col = ru_col - (Columns - width);
9707 if (this_ru_col < 0)
9708 this_ru_col = 0;
9709 #endif
9710 /* Never use more than half the window/screen width, leave the other
9711 * half for the filename. */
9712 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
9713 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
9714 if (this_ru_col + o < WITH_WIDTH(width))
9716 while (this_ru_col + o < WITH_WIDTH(width))
9718 #ifdef FEAT_MBYTE
9719 if (has_mbyte)
9720 i += (*mb_char2bytes)(fillchar, buffer + i);
9721 else
9722 #endif
9723 buffer[i++] = fillchar;
9724 ++o;
9726 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
9728 /* Truncate at window boundary. */
9729 #ifdef FEAT_MBYTE
9730 if (has_mbyte)
9732 o = 0;
9733 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
9735 o += (*mb_ptr2cells)(buffer + i);
9736 if (this_ru_col + o > WITH_WIDTH(width))
9738 buffer[i] = NUL;
9739 break;
9743 else
9744 #endif
9745 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
9746 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
9748 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
9749 i = redraw_cmdline;
9750 screen_fill(row, row + 1,
9751 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
9752 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
9753 fillchar, fillchar, attr);
9754 /* don't redraw the cmdline because of showing the ruler */
9755 redraw_cmdline = i;
9756 wp->w_ru_cursor = wp->w_cursor;
9757 wp->w_ru_virtcol = wp->w_virtcol;
9758 wp->w_ru_empty = empty_line;
9759 wp->w_ru_topline = wp->w_topline;
9760 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
9761 #ifdef FEAT_DIFF
9762 wp->w_ru_topfill = wp->w_topfill;
9763 #endif
9766 #endif
9768 #if defined(FEAT_LINEBREAK) || defined(PROTO)
9770 * Return the width of the 'number' column.
9771 * Caller may need to check if 'number' is set.
9772 * Otherwise it depends on 'numberwidth' and the line count.
9775 number_width(wp)
9776 win_T *wp;
9778 int n;
9779 linenr_T lnum;
9781 lnum = wp->w_buffer->b_ml.ml_line_count;
9782 if (lnum == wp->w_nrwidth_line_count)
9783 return wp->w_nrwidth_width;
9784 wp->w_nrwidth_line_count = lnum;
9786 n = 0;
9789 lnum /= 10;
9790 ++n;
9791 } while (lnum > 0);
9793 /* 'numberwidth' gives the minimal width plus one */
9794 if (n < wp->w_p_nuw - 1)
9795 n = wp->w_p_nuw - 1;
9797 wp->w_nrwidth_width = n;
9798 return n;
9800 #endif