Resize view properly when showing toolbar
[MacVim.git] / src / screen.c
blobe8db7af1b0fc511eeff0797f20f366a43cc05ccd
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 (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2339 /* double-byte single width character */
2340 ScreenLines2[idx] = p[1];
2341 else if (cells > 1)
2342 /* double-width character */
2343 ScreenLines[idx + 1] = p[1];
2344 col += cells;
2345 idx += cells;
2346 p += c_len;
2349 else
2350 #endif
2352 len = (int)STRLEN(text);
2353 if (len > W_WIDTH(wp) - col)
2354 len = W_WIDTH(wp) - col;
2355 if (len > 0)
2357 #ifdef FEAT_RIGHTLEFT
2358 if (wp->w_p_rl)
2359 STRNCPY(current_ScreenLine, text, len);
2360 else
2361 #endif
2362 STRNCPY(current_ScreenLine + col, text, len);
2363 col += len;
2367 /* Fill the rest of the line with the fold filler */
2368 #ifdef FEAT_RIGHTLEFT
2369 if (wp->w_p_rl)
2370 col -= txtcol;
2371 #endif
2372 while (col < W_WIDTH(wp)
2373 #ifdef FEAT_RIGHTLEFT
2374 - (wp->w_p_rl ? txtcol : 0)
2375 #endif
2378 #ifdef FEAT_MBYTE
2379 if (enc_utf8)
2381 if (fill_fold >= 0x80)
2383 ScreenLinesUC[off + col] = fill_fold;
2384 ScreenLinesC[0][off + col] = 0;
2386 else
2387 ScreenLinesUC[off + col] = 0;
2389 #endif
2390 ScreenLines[off + col++] = fill_fold;
2393 if (text != buf)
2394 vim_free(text);
2397 * 6. set highlighting for the Visual area an other text.
2398 * If all folded lines are in the Visual area, highlight the line.
2400 #ifdef FEAT_VISUAL
2401 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2403 if (ltoreq(curwin->w_cursor, VIsual))
2405 /* Visual is after curwin->w_cursor */
2406 top = &curwin->w_cursor;
2407 bot = &VIsual;
2409 else
2411 /* Visual is before curwin->w_cursor */
2412 top = &VIsual;
2413 bot = &curwin->w_cursor;
2415 if (lnum >= top->lnum
2416 && lnume <= bot->lnum
2417 && (VIsual_mode != 'v'
2418 || ((lnum > top->lnum
2419 || (lnum == top->lnum
2420 && top->col == 0))
2421 && (lnume < bot->lnum
2422 || (lnume == bot->lnum
2423 && (bot->col - (*p_sel == 'e'))
2424 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
2426 if (VIsual_mode == Ctrl_V)
2428 /* Visual block mode: highlight the chars part of the block */
2429 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2431 if (wp->w_old_cursor_lcol + txtcol < (colnr_T)W_WIDTH(wp))
2432 len = wp->w_old_cursor_lcol;
2433 else
2434 len = W_WIDTH(wp) - txtcol;
2435 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, hl_attr(HLF_V),
2436 len - (int)wp->w_old_cursor_fcol);
2439 else
2441 /* Set all attributes of the text */
2442 RL_MEMSET(txtcol, hl_attr(HLF_V), W_WIDTH(wp) - txtcol);
2446 #endif
2448 #ifdef FEAT_SYN_HL
2449 /* Show 'cursorcolumn' in the fold line. */
2450 if (wp->w_p_cuc)
2452 txtcol += wp->w_virtcol;
2453 if (wp->w_p_wrap)
2454 txtcol -= wp->w_skipcol;
2455 else
2456 txtcol -= wp->w_leftcol;
2457 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2458 ScreenAttrs[off + txtcol] = hl_combine_attr(
2459 ScreenAttrs[off + txtcol], hl_attr(HLF_CUC));
2461 #endif
2463 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2464 (int)W_WIDTH(wp), FALSE);
2467 * Update w_cline_height and w_cline_folded if the cursor line was
2468 * updated (saves a call to plines() later).
2470 if (wp == curwin
2471 && lnum <= curwin->w_cursor.lnum
2472 && lnume >= curwin->w_cursor.lnum)
2474 curwin->w_cline_row = row;
2475 curwin->w_cline_height = 1;
2476 curwin->w_cline_folded = TRUE;
2477 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2482 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2484 static void
2485 copy_text_attr(off, buf, len, attr)
2486 int off;
2487 char_u *buf;
2488 int len;
2489 int attr;
2491 int i;
2493 mch_memmove(ScreenLines + off, buf, (size_t)len);
2494 # ifdef FEAT_MBYTE
2495 if (enc_utf8)
2496 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2497 # endif
2498 for (i = 0; i < len; ++i)
2499 ScreenAttrs[off + i] = attr;
2503 * Fill the foldcolumn at "p" for window "wp".
2504 * Only to be called when 'foldcolumn' > 0.
2506 static void
2507 fill_foldcolumn(p, wp, closed, lnum)
2508 char_u *p;
2509 win_T *wp;
2510 int closed; /* TRUE of FALSE */
2511 linenr_T lnum; /* current line number */
2513 int i = 0;
2514 int level;
2515 int first_level;
2516 int empty;
2518 /* Init to all spaces. */
2519 copy_spaces(p, (size_t)wp->w_p_fdc);
2521 level = win_foldinfo.fi_level;
2522 if (level > 0)
2524 /* If there is only one column put more info in it. */
2525 empty = (wp->w_p_fdc == 1) ? 0 : 1;
2527 /* If the column is too narrow, we start at the lowest level that
2528 * fits and use numbers to indicated the depth. */
2529 first_level = level - wp->w_p_fdc - closed + 1 + empty;
2530 if (first_level < 1)
2531 first_level = 1;
2533 for (i = 0; i + empty < wp->w_p_fdc; ++i)
2535 if (win_foldinfo.fi_lnum == lnum
2536 && first_level + i >= win_foldinfo.fi_low_level)
2537 p[i] = '-';
2538 else if (first_level == 1)
2539 p[i] = '|';
2540 else if (first_level + i <= 9)
2541 p[i] = '0' + first_level + i;
2542 else
2543 p[i] = '>';
2544 if (first_level + i == level)
2545 break;
2548 if (closed)
2549 p[i >= wp->w_p_fdc ? i - 1 : i] = '+';
2551 #endif /* FEAT_FOLDING */
2554 * Display line "lnum" of window 'wp' on the screen.
2555 * Start at row "startrow", stop when "endrow" is reached.
2556 * wp->w_virtcol needs to be valid.
2558 * Return the number of last row the line occupies.
2560 static int
2561 win_line(wp, lnum, startrow, endrow, nochange)
2562 win_T *wp;
2563 linenr_T lnum;
2564 int startrow;
2565 int endrow;
2566 int nochange UNUSED; /* not updating for changed text */
2568 int col; /* visual column on screen */
2569 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2570 int c = 0; /* init for GCC */
2571 long vcol = 0; /* virtual column (for tabs) */
2572 long vcol_prev = -1; /* "vcol" of previous character */
2573 char_u *line; /* current line */
2574 char_u *ptr; /* current position in "line" */
2575 int row; /* row in the window, excl w_winrow */
2576 int screen_row; /* row on the screen, incl w_winrow */
2578 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2579 int n_extra = 0; /* number of extra chars */
2580 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
2581 int c_extra = NUL; /* extra chars, all the same */
2582 int extra_attr = 0; /* attributes when n_extra != 0 */
2583 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2584 displaying lcs_eol at end-of-line */
2585 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2586 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2588 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2589 int saved_n_extra = 0;
2590 char_u *saved_p_extra = NULL;
2591 int saved_c_extra = 0;
2592 int saved_char_attr = 0;
2594 int n_attr = 0; /* chars with special attr */
2595 int saved_attr2 = 0; /* char_attr saved for n_attr */
2596 int n_attr3 = 0; /* chars with overruling special attr */
2597 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2599 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2601 int fromcol, tocol; /* start/end of inverting */
2602 int fromcol_prev = -2; /* start of inverting after cursor */
2603 int noinvcur = FALSE; /* don't invert the cursor */
2604 #ifdef FEAT_VISUAL
2605 pos_T *top, *bot;
2606 int lnum_in_visual_area = FALSE;
2607 #endif
2608 pos_T pos;
2609 long v;
2611 int char_attr = 0; /* attributes for next character */
2612 int attr_pri = FALSE; /* char_attr has priority */
2613 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2614 in this line */
2615 int attr = 0; /* attributes for area highlighting */
2616 int area_attr = 0; /* attributes desired by highlighting */
2617 int search_attr = 0; /* attributes desired by 'hlsearch' */
2618 #ifdef FEAT_SYN_HL
2619 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
2620 int syntax_attr = 0; /* attributes desired by syntax */
2621 int has_syntax = FALSE; /* this buffer has syntax highl. */
2622 int save_did_emsg;
2623 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
2624 #endif
2625 #ifdef FEAT_SPELL
2626 int has_spell = FALSE; /* this buffer has spell checking */
2627 # define SPWORDLEN 150
2628 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
2629 int nextlinecol = 0; /* column where nextline[] starts */
2630 int nextline_idx = 0; /* index in nextline[] where next line
2631 starts */
2632 int spell_attr = 0; /* attributes desired by spelling */
2633 int word_end = 0; /* last byte with same spell_attr */
2634 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2635 static int checked_col = 0; /* column in "checked_lnum" up to which
2636 * there are no spell errors */
2637 static int cap_col = -1; /* column to check for Cap word */
2638 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
2639 int cur_checked_col = 0; /* checked column for current line */
2640 #endif
2641 int extra_check; /* has syntax or linebreak */
2642 #ifdef FEAT_MBYTE
2643 int multi_attr = 0; /* attributes desired by multibyte */
2644 int mb_l = 1; /* multi-byte byte length */
2645 int mb_c = 0; /* decoded multi-byte character */
2646 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
2647 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
2648 #endif
2649 #ifdef FEAT_DIFF
2650 int filler_lines; /* nr of filler lines to be drawn */
2651 int filler_todo; /* nr of filler lines still to do + 1 */
2652 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
2653 int change_start = MAXCOL; /* first col of changed area */
2654 int change_end = -1; /* last col of changed area */
2655 #endif
2656 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
2657 #ifdef FEAT_LINEBREAK
2658 int need_showbreak = FALSE;
2659 #endif
2660 #if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
2661 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
2662 # define LINE_ATTR
2663 int line_attr = 0; /* attribute for the whole line */
2664 #endif
2665 #ifdef FEAT_SEARCH_EXTRA
2666 matchitem_T *cur; /* points to the match list */
2667 match_T *shl; /* points to search_hl or a match */
2668 int shl_flag; /* flag to indicate whether search_hl
2669 has been processed or not */
2670 int prevcol_hl_flag; /* flag to indicate whether prevcol
2671 equals startcol of search_hl or one
2672 of the matches */
2673 #endif
2674 #ifdef FEAT_ARABIC
2675 int prev_c = 0; /* previous Arabic character */
2676 int prev_c1 = 0; /* first composing char for prev_c */
2677 #endif
2678 #if defined(LINE_ATTR)
2679 int did_line_attr = 0;
2680 #endif
2682 /* draw_state: items that are drawn in sequence: */
2683 #define WL_START 0 /* nothing done yet */
2684 #ifdef FEAT_CMDWIN
2685 # define WL_CMDLINE WL_START + 1 /* cmdline window column */
2686 #else
2687 # define WL_CMDLINE WL_START
2688 #endif
2689 #ifdef FEAT_FOLDING
2690 # define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
2691 #else
2692 # define WL_FOLD WL_CMDLINE
2693 #endif
2694 #ifdef FEAT_SIGNS
2695 # define WL_SIGN WL_FOLD + 1 /* column for signs */
2696 #else
2697 # define WL_SIGN WL_FOLD /* column for signs */
2698 #endif
2699 #define WL_NR WL_SIGN + 1 /* line number */
2700 #if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
2701 # define WL_SBR WL_NR + 1 /* 'showbreak' or 'diff' */
2702 #else
2703 # define WL_SBR WL_NR
2704 #endif
2705 #define WL_LINE WL_SBR + 1 /* text in the line */
2706 int draw_state = WL_START; /* what to draw next */
2707 #if defined(FEAT_XIM) && (defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MACVIM))
2708 int feedback_col = 0;
2709 int feedback_old_attr = -1;
2710 #endif
2713 if (startrow > endrow) /* past the end already! */
2714 return startrow;
2716 row = startrow;
2717 screen_row = row + W_WINROW(wp);
2720 * To speed up the loop below, set extra_check when there is linebreak,
2721 * trailing white space and/or syntax processing to be done.
2723 #ifdef FEAT_LINEBREAK
2724 extra_check = wp->w_p_lbr;
2725 #else
2726 extra_check = 0;
2727 #endif
2728 #ifdef FEAT_SYN_HL
2729 if (syntax_present(wp->w_buffer) && !wp->w_buffer->b_syn_error)
2731 /* Prepare for syntax highlighting in this line. When there is an
2732 * error, stop syntax highlighting. */
2733 save_did_emsg = did_emsg;
2734 did_emsg = FALSE;
2735 syntax_start(wp, lnum);
2736 if (did_emsg)
2737 wp->w_buffer->b_syn_error = TRUE;
2738 else
2740 did_emsg = save_did_emsg;
2741 has_syntax = TRUE;
2742 extra_check = TRUE;
2745 #endif
2747 #ifdef FEAT_SPELL
2748 if (wp->w_p_spell
2749 && *wp->w_buffer->b_p_spl != NUL
2750 && wp->w_buffer->b_langp.ga_len > 0
2751 && *(char **)(wp->w_buffer->b_langp.ga_data) != NULL)
2753 /* Prepare for spell checking. */
2754 has_spell = TRUE;
2755 extra_check = TRUE;
2757 /* Get the start of the next line, so that words that wrap to the next
2758 * line are found too: "et<line-break>al.".
2759 * Trick: skip a few chars for C/shell/Vim comments */
2760 nextline[SPWORDLEN] = NUL;
2761 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2763 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
2764 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
2767 /* When a word wrapped from the previous line the start of the current
2768 * line is valid. */
2769 if (lnum == checked_lnum)
2770 cur_checked_col = checked_col;
2771 checked_lnum = 0;
2773 /* When there was a sentence end in the previous line may require a
2774 * word starting with capital in this line. In line 1 always check
2775 * the first word. */
2776 if (lnum != capcol_lnum)
2777 cap_col = -1;
2778 if (lnum == 1)
2779 cap_col = 0;
2780 capcol_lnum = 0;
2782 #endif
2785 * handle visual active in this window
2787 fromcol = -10;
2788 tocol = MAXCOL;
2789 #ifdef FEAT_VISUAL
2790 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2792 /* Visual is after curwin->w_cursor */
2793 if (ltoreq(curwin->w_cursor, VIsual))
2795 top = &curwin->w_cursor;
2796 bot = &VIsual;
2798 else /* Visual is before curwin->w_cursor */
2800 top = &VIsual;
2801 bot = &curwin->w_cursor;
2803 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
2804 if (VIsual_mode == Ctrl_V) /* block mode */
2806 if (lnum_in_visual_area)
2808 fromcol = wp->w_old_cursor_fcol;
2809 tocol = wp->w_old_cursor_lcol;
2812 else /* non-block mode */
2814 if (lnum > top->lnum && lnum <= bot->lnum)
2815 fromcol = 0;
2816 else if (lnum == top->lnum)
2818 if (VIsual_mode == 'V') /* linewise */
2819 fromcol = 0;
2820 else
2822 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
2823 if (gchar_pos(top) == NUL)
2824 tocol = fromcol + 1;
2827 if (VIsual_mode != 'V' && lnum == bot->lnum)
2829 if (*p_sel == 'e' && bot->col == 0
2830 #ifdef FEAT_VIRTUALEDIT
2831 && bot->coladd == 0
2832 #endif
2835 fromcol = -10;
2836 tocol = MAXCOL;
2838 else if (bot->col == MAXCOL)
2839 tocol = MAXCOL;
2840 else
2842 pos = *bot;
2843 if (*p_sel == 'e')
2844 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
2845 else
2847 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
2848 ++tocol;
2854 #ifndef MSDOS
2855 /* Check if the character under the cursor should not be inverted */
2856 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
2857 # ifdef FEAT_GUI
2858 && !gui.in_use
2859 # endif
2861 noinvcur = TRUE;
2862 #endif
2864 /* if inverting in this line set area_highlighting */
2865 if (fromcol >= 0)
2867 area_highlighting = TRUE;
2868 attr = hl_attr(HLF_V);
2869 #if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
2870 if (clip_star.available && !clip_star.owned && clip_isautosel())
2871 attr = hl_attr(HLF_VNC);
2872 #endif
2877 * handle 'incsearch' and ":s///c" highlighting
2879 else
2880 #endif /* FEAT_VISUAL */
2881 if (highlight_match
2882 && wp == curwin
2883 && lnum >= curwin->w_cursor.lnum
2884 && lnum <= curwin->w_cursor.lnum + search_match_lines)
2886 if (lnum == curwin->w_cursor.lnum)
2887 getvcol(curwin, &(curwin->w_cursor),
2888 (colnr_T *)&fromcol, NULL, NULL);
2889 else
2890 fromcol = 0;
2891 if (lnum == curwin->w_cursor.lnum + search_match_lines)
2893 pos.lnum = lnum;
2894 pos.col = search_match_endcol;
2895 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
2897 else
2898 tocol = MAXCOL;
2899 /* do at least one character; happens when past end of line */
2900 if (fromcol == tocol)
2901 tocol = fromcol + 1;
2902 area_highlighting = TRUE;
2903 attr = hl_attr(HLF_I);
2906 #ifdef FEAT_DIFF
2907 filler_lines = diff_check(wp, lnum);
2908 if (filler_lines < 0)
2910 if (filler_lines == -1)
2912 if (diff_find_change(wp, lnum, &change_start, &change_end))
2913 diff_hlf = HLF_ADD; /* added line */
2914 else if (change_start == 0)
2915 diff_hlf = HLF_TXD; /* changed text */
2916 else
2917 diff_hlf = HLF_CHD; /* changed line */
2919 else
2920 diff_hlf = HLF_ADD; /* added line */
2921 filler_lines = 0;
2922 area_highlighting = TRUE;
2924 if (lnum == wp->w_topline)
2925 filler_lines = wp->w_topfill;
2926 filler_todo = filler_lines;
2927 #endif
2929 #ifdef LINE_ATTR
2930 # ifdef FEAT_SIGNS
2931 /* If this line has a sign with line highlighting set line_attr. */
2932 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
2933 if (v != 0)
2934 line_attr = sign_get_attr((int)v, TRUE);
2935 # endif
2936 # if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
2937 /* Highlight the current line in the quickfix window. */
2938 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
2939 line_attr = hl_attr(HLF_L);
2940 # endif
2941 if (line_attr != 0)
2942 area_highlighting = TRUE;
2943 #endif
2945 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
2946 ptr = line;
2948 #ifdef FEAT_SPELL
2949 if (has_spell)
2951 /* For checking first word with a capital skip white space. */
2952 if (cap_col == 0)
2953 cap_col = (int)(skipwhite(line) - line);
2955 /* To be able to spell-check over line boundaries copy the end of the
2956 * current line into nextline[]. Above the start of the next line was
2957 * copied to nextline[SPWORDLEN]. */
2958 if (nextline[SPWORDLEN] == NUL)
2960 /* No next line or it is empty. */
2961 nextlinecol = MAXCOL;
2962 nextline_idx = 0;
2964 else
2966 v = (long)STRLEN(line);
2967 if (v < SPWORDLEN)
2969 /* Short line, use it completely and append the start of the
2970 * next line. */
2971 nextlinecol = 0;
2972 mch_memmove(nextline, line, (size_t)v);
2973 STRMOVE(nextline + v, nextline + SPWORDLEN);
2974 nextline_idx = v + 1;
2976 else
2978 /* Long line, use only the last SPWORDLEN bytes. */
2979 nextlinecol = v - SPWORDLEN;
2980 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
2981 nextline_idx = SPWORDLEN + 1;
2985 #endif
2987 /* find start of trailing whitespace */
2988 if (wp->w_p_list && lcs_trail)
2990 trailcol = (colnr_T)STRLEN(ptr);
2991 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
2992 --trailcol;
2993 trailcol += (colnr_T) (ptr - line);
2994 extra_check = TRUE;
2998 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
2999 * first character to be displayed.
3001 if (wp->w_p_wrap)
3002 v = wp->w_skipcol;
3003 else
3004 v = wp->w_leftcol;
3005 if (v > 0)
3007 #ifdef FEAT_MBYTE
3008 char_u *prev_ptr = ptr;
3009 #endif
3010 while (vcol < v && *ptr != NUL)
3012 c = win_lbr_chartabsize(wp, ptr, (colnr_T)vcol, NULL);
3013 vcol += c;
3014 #ifdef FEAT_MBYTE
3015 prev_ptr = ptr;
3016 #endif
3017 mb_ptr_adv(ptr);
3020 #if defined(FEAT_SYN_HL) || defined(FEAT_VIRTUALEDIT) || defined(FEAT_VISUAL)
3021 /* When:
3022 * - 'cuc' is set, or
3023 * - 'virtualedit' is set, or
3024 * - the visual mode is active,
3025 * the end of the line may be before the start of the displayed part.
3027 if (vcol < v && (
3028 # ifdef FEAT_SYN_HL
3029 wp->w_p_cuc
3030 # if defined(FEAT_VIRTUALEDIT) || defined(FEAT_VISUAL)
3032 # endif
3033 # endif
3034 # ifdef FEAT_VIRTUALEDIT
3035 virtual_active()
3036 # ifdef FEAT_VISUAL
3038 # endif
3039 # endif
3040 # ifdef FEAT_VISUAL
3041 (VIsual_active && wp->w_buffer == curwin->w_buffer)
3042 # endif
3045 vcol = v;
3047 #endif
3049 /* Handle a character that's not completely on the screen: Put ptr at
3050 * that character but skip the first few screen characters. */
3051 if (vcol > v)
3053 vcol -= c;
3054 #ifdef FEAT_MBYTE
3055 ptr = prev_ptr;
3056 #else
3057 --ptr;
3058 #endif
3059 n_skip = v - vcol;
3063 * Adjust for when the inverted text is before the screen,
3064 * and when the start of the inverted text is before the screen.
3066 if (tocol <= vcol)
3067 fromcol = 0;
3068 else if (fromcol >= 0 && fromcol < vcol)
3069 fromcol = vcol;
3071 #ifdef FEAT_LINEBREAK
3072 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3073 if (wp->w_p_wrap)
3074 need_showbreak = TRUE;
3075 #endif
3076 #ifdef FEAT_SPELL
3077 /* When spell checking a word we need to figure out the start of the
3078 * word and if it's badly spelled or not. */
3079 if (has_spell)
3081 int len;
3082 colnr_T linecol = (colnr_T)(ptr - line);
3083 hlf_T spell_hlf = HLF_COUNT;
3085 pos = wp->w_cursor;
3086 wp->w_cursor.lnum = lnum;
3087 wp->w_cursor.col = linecol;
3088 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
3090 /* spell_move_to() may call ml_get() and make "line" invalid */
3091 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3092 ptr = line + linecol;
3094 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
3096 /* no bad word found at line start, don't check until end of a
3097 * word */
3098 spell_hlf = HLF_COUNT;
3099 word_end = (int)(spell_to_word_end(ptr, wp->w_buffer)
3100 - line + 1);
3102 else
3104 /* bad word found, use attributes until end of word */
3105 word_end = wp->w_cursor.col + len + 1;
3107 /* Turn index into actual attributes. */
3108 if (spell_hlf != HLF_COUNT)
3109 spell_attr = highlight_attr[spell_hlf];
3111 wp->w_cursor = pos;
3113 # ifdef FEAT_SYN_HL
3114 /* Need to restart syntax highlighting for this line. */
3115 if (has_syntax)
3116 syntax_start(wp, lnum);
3117 # endif
3119 #endif
3123 * Correct highlighting for cursor that can't be disabled.
3124 * Avoids having to check this for each character.
3126 if (fromcol >= 0)
3128 if (noinvcur)
3130 if ((colnr_T)fromcol == wp->w_virtcol)
3132 /* highlighting starts at cursor, let it start just after the
3133 * cursor */
3134 fromcol_prev = fromcol;
3135 fromcol = -1;
3137 else if ((colnr_T)fromcol < wp->w_virtcol)
3138 /* restart highlighting after the cursor */
3139 fromcol_prev = wp->w_virtcol;
3141 if (fromcol >= tocol)
3142 fromcol = -1;
3145 #ifdef FEAT_SEARCH_EXTRA
3147 * Handle highlighting the last used search pattern and matches.
3148 * Do this for both search_hl and the match list.
3150 cur = wp->w_match_head;
3151 shl_flag = FALSE;
3152 while (cur != NULL || shl_flag == FALSE)
3154 if (shl_flag == FALSE)
3156 shl = &search_hl;
3157 shl_flag = TRUE;
3159 else
3160 shl = &cur->hl;
3161 shl->startcol = MAXCOL;
3162 shl->endcol = MAXCOL;
3163 shl->attr_cur = 0;
3164 if (shl->rm.regprog != NULL)
3166 v = (long)(ptr - line);
3167 next_search_hl(wp, shl, lnum, (colnr_T)v);
3169 /* Need to get the line again, a multi-line regexp may have made it
3170 * invalid. */
3171 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3172 ptr = line + v;
3174 if (shl->lnum != 0 && shl->lnum <= lnum)
3176 if (shl->lnum == lnum)
3177 shl->startcol = shl->rm.startpos[0].col;
3178 else
3179 shl->startcol = 0;
3180 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3181 - shl->rm.startpos[0].lnum)
3182 shl->endcol = shl->rm.endpos[0].col;
3183 else
3184 shl->endcol = MAXCOL;
3185 /* Highlight one character for an empty match. */
3186 if (shl->startcol == shl->endcol)
3188 #ifdef FEAT_MBYTE
3189 if (has_mbyte && line[shl->endcol] != NUL)
3190 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3191 else
3192 #endif
3193 ++shl->endcol;
3195 if ((long)shl->startcol < v) /* match at leftcol */
3197 shl->attr_cur = shl->attr;
3198 search_attr = shl->attr;
3200 area_highlighting = TRUE;
3203 if (shl != &search_hl && cur != NULL)
3204 cur = cur->next;
3206 #endif
3208 #ifdef FEAT_SYN_HL
3209 /* Cursor line highlighting for 'cursorline'. Not when Visual mode is
3210 * active, because it's not clear what is selected then. */
3211 if (wp->w_p_cul && lnum == wp->w_cursor.lnum && !VIsual_active)
3213 line_attr = hl_attr(HLF_CUL);
3214 area_highlighting = TRUE;
3216 #endif
3218 off = (unsigned)(current_ScreenLine - ScreenLines);
3219 col = 0;
3220 #ifdef FEAT_RIGHTLEFT
3221 if (wp->w_p_rl)
3223 /* Rightleft window: process the text in the normal direction, but put
3224 * it in current_ScreenLine[] from right to left. Start at the
3225 * rightmost column of the window. */
3226 col = W_WIDTH(wp) - 1;
3227 off += col;
3229 #endif
3232 * Repeat for the whole displayed line.
3234 for (;;)
3236 /* Skip this quickly when working on the text. */
3237 if (draw_state != WL_LINE)
3239 #ifdef FEAT_CMDWIN
3240 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3242 draw_state = WL_CMDLINE;
3243 if (cmdwin_type != 0 && wp == curwin)
3245 /* Draw the cmdline character. */
3246 n_extra = 1;
3247 c_extra = cmdwin_type;
3248 char_attr = hl_attr(HLF_AT);
3251 #endif
3253 #ifdef FEAT_FOLDING
3254 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3256 draw_state = WL_FOLD;
3257 if (wp->w_p_fdc > 0)
3259 /* Draw the 'foldcolumn'. */
3260 fill_foldcolumn(extra, wp, FALSE, lnum);
3261 n_extra = wp->w_p_fdc;
3262 p_extra = extra;
3263 p_extra[n_extra] = NUL;
3264 c_extra = NUL;
3265 char_attr = hl_attr(HLF_FC);
3268 #endif
3270 #ifdef FEAT_SIGNS
3271 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3273 draw_state = WL_SIGN;
3274 /* Show the sign column when there are any signs in this
3275 * buffer or when using Netbeans. */
3276 if (draw_signcolumn(wp)
3277 # ifdef FEAT_DIFF
3278 && filler_todo <= 0
3279 # endif
3282 int_u text_sign;
3283 # ifdef FEAT_SIGN_ICONS
3284 int_u icon_sign;
3285 # endif
3287 /* Draw two cells with the sign value or blank. */
3288 c_extra = ' ';
3289 char_attr = hl_attr(HLF_SC);
3290 n_extra = 2;
3292 if (row == startrow)
3294 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3295 SIGN_TEXT);
3296 # ifdef FEAT_SIGN_ICONS
3297 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3298 SIGN_ICON);
3299 if (gui.in_use && icon_sign != 0)
3301 /* Use the image in this position. */
3302 c_extra = SIGN_BYTE;
3303 # ifdef FEAT_NETBEANS_INTG
3304 if (buf_signcount(wp->w_buffer, lnum) > 1)
3305 c_extra = MULTISIGN_BYTE;
3306 # endif
3307 char_attr = icon_sign;
3309 else
3310 # endif
3311 if (text_sign != 0)
3313 p_extra = sign_get_text(text_sign);
3314 if (p_extra != NULL)
3316 c_extra = NUL;
3317 n_extra = (int)STRLEN(p_extra);
3319 char_attr = sign_get_attr(text_sign, FALSE);
3324 #endif
3326 if (draw_state == WL_NR - 1 && n_extra == 0)
3328 draw_state = WL_NR;
3329 /* Display the line number. After the first fill with blanks
3330 * when the 'n' flag isn't in 'cpo' */
3331 if (wp->w_p_nu
3332 && (row == startrow
3333 #ifdef FEAT_DIFF
3334 + filler_lines
3335 #endif
3336 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3338 /* Draw the line number (empty space after wrapping). */
3339 if (row == startrow
3340 #ifdef FEAT_DIFF
3341 + filler_lines
3342 #endif
3345 sprintf((char *)extra, "%*ld ",
3346 number_width(wp), (long)lnum);
3347 if (wp->w_skipcol > 0)
3348 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3349 *p_extra = '-';
3350 #ifdef FEAT_RIGHTLEFT
3351 if (wp->w_p_rl) /* reverse line numbers */
3352 rl_mirror(extra);
3353 #endif
3354 p_extra = extra;
3355 c_extra = NUL;
3357 else
3358 c_extra = ' ';
3359 n_extra = number_width(wp) + 1;
3360 char_attr = hl_attr(HLF_N);
3361 #ifdef FEAT_SYN_HL
3362 /* When 'cursorline' is set highlight the line number of
3363 * the current line differently. */
3364 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3365 char_attr = hl_combine_attr(hl_attr(HLF_CUL), char_attr);
3366 #endif
3370 #if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3371 if (draw_state == WL_SBR - 1 && n_extra == 0)
3373 draw_state = WL_SBR;
3374 # ifdef FEAT_DIFF
3375 if (filler_todo > 0)
3377 /* Draw "deleted" diff line(s). */
3378 if (char2cells(fill_diff) > 1)
3379 c_extra = '-';
3380 else
3381 c_extra = fill_diff;
3382 # ifdef FEAT_RIGHTLEFT
3383 if (wp->w_p_rl)
3384 n_extra = col + 1;
3385 else
3386 # endif
3387 n_extra = W_WIDTH(wp) - col;
3388 char_attr = hl_attr(HLF_DED);
3390 # endif
3391 # ifdef FEAT_LINEBREAK
3392 if (*p_sbr != NUL && need_showbreak)
3394 /* Draw 'showbreak' at the start of each broken line. */
3395 p_extra = p_sbr;
3396 c_extra = NUL;
3397 n_extra = (int)STRLEN(p_sbr);
3398 char_attr = hl_attr(HLF_AT);
3399 need_showbreak = FALSE;
3400 /* Correct end of highlighted area for 'showbreak',
3401 * required when 'linebreak' is also set. */
3402 if (tocol == vcol)
3403 tocol += n_extra;
3405 # endif
3407 #endif
3409 if (draw_state == WL_LINE - 1 && n_extra == 0)
3411 draw_state = WL_LINE;
3412 if (saved_n_extra)
3414 /* Continue item from end of wrapped line. */
3415 n_extra = saved_n_extra;
3416 c_extra = saved_c_extra;
3417 p_extra = saved_p_extra;
3418 char_attr = saved_char_attr;
3420 else
3421 char_attr = 0;
3425 /* When still displaying '$' of change command, stop at cursor */
3426 if (dollar_vcol != 0 && wp == curwin
3427 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
3428 #ifdef FEAT_DIFF
3429 && filler_todo <= 0
3430 #endif
3433 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3434 wp->w_p_rl);
3435 /* Pretend we have finished updating the window. Except when
3436 * 'cursorcolumn' is set. */
3437 #ifdef FEAT_SYN_HL
3438 if (wp->w_p_cuc)
3439 row = wp->w_cline_row + wp->w_cline_height;
3440 else
3441 #endif
3442 row = wp->w_height;
3443 break;
3446 if (draw_state == WL_LINE && area_highlighting)
3448 /* handle Visual or match highlighting in this line */
3449 if (vcol == fromcol
3450 #ifdef FEAT_MBYTE
3451 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3452 && (*mb_ptr2cells)(ptr) > 1)
3453 #endif
3454 || ((int)vcol_prev == fromcol_prev
3455 && vcol_prev < vcol /* not at margin */
3456 && vcol < tocol))
3457 area_attr = attr; /* start highlighting */
3458 else if (area_attr != 0
3459 && (vcol == tocol
3460 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
3461 area_attr = 0; /* stop highlighting */
3463 #ifdef FEAT_SEARCH_EXTRA
3464 if (!n_extra)
3467 * Check for start/end of search pattern match.
3468 * After end, check for start/end of next match.
3469 * When another match, have to check for start again.
3470 * Watch out for matching an empty string!
3471 * Do this for 'search_hl' and the match list (ordered by
3472 * priority).
3474 v = (long)(ptr - line);
3475 cur = wp->w_match_head;
3476 shl_flag = FALSE;
3477 while (cur != NULL || shl_flag == FALSE)
3479 if (shl_flag == FALSE
3480 && ((cur != NULL
3481 && cur->priority > SEARCH_HL_PRIORITY)
3482 || cur == NULL))
3484 shl = &search_hl;
3485 shl_flag = TRUE;
3487 else
3488 shl = &cur->hl;
3489 while (shl->rm.regprog != NULL)
3491 if (shl->startcol != MAXCOL
3492 && v >= (long)shl->startcol
3493 && v < (long)shl->endcol)
3495 shl->attr_cur = shl->attr;
3497 else if (v == (long)shl->endcol)
3499 shl->attr_cur = 0;
3501 next_search_hl(wp, shl, lnum, (colnr_T)v);
3503 /* Need to get the line again, a multi-line regexp
3504 * may have made it invalid. */
3505 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3506 ptr = line + v;
3508 if (shl->lnum == lnum)
3510 shl->startcol = shl->rm.startpos[0].col;
3511 if (shl->rm.endpos[0].lnum == 0)
3512 shl->endcol = shl->rm.endpos[0].col;
3513 else
3514 shl->endcol = MAXCOL;
3516 if (shl->startcol == shl->endcol)
3518 /* highlight empty match, try again after
3519 * it */
3520 #ifdef FEAT_MBYTE
3521 if (has_mbyte)
3522 shl->endcol += (*mb_ptr2len)(line
3523 + shl->endcol);
3524 else
3525 #endif
3526 ++shl->endcol;
3529 /* Loop to check if the match starts at the
3530 * current position */
3531 continue;
3534 break;
3536 if (shl != &search_hl && cur != NULL)
3537 cur = cur->next;
3540 /* Use attributes from match with highest priority among
3541 * 'search_hl' and the match list. */
3542 search_attr = search_hl.attr_cur;
3543 cur = wp->w_match_head;
3544 shl_flag = FALSE;
3545 while (cur != NULL || shl_flag == FALSE)
3547 if (shl_flag == FALSE
3548 && ((cur != NULL
3549 && cur->priority > SEARCH_HL_PRIORITY)
3550 || cur == NULL))
3552 shl = &search_hl;
3553 shl_flag = TRUE;
3555 else
3556 shl = &cur->hl;
3557 if (shl->attr_cur != 0)
3558 search_attr = shl->attr_cur;
3559 if (shl != &search_hl && cur != NULL)
3560 cur = cur->next;
3563 #endif
3565 #ifdef FEAT_DIFF
3566 if (diff_hlf != (hlf_T)0)
3568 if (diff_hlf == HLF_CHD && ptr - line >= change_start
3569 && n_extra == 0)
3570 diff_hlf = HLF_TXD; /* changed text */
3571 if (diff_hlf == HLF_TXD && ptr - line > change_end
3572 && n_extra == 0)
3573 diff_hlf = HLF_CHD; /* changed line */
3574 line_attr = hl_attr(diff_hlf);
3576 #endif
3578 /* Decide which of the highlight attributes to use. */
3579 attr_pri = TRUE;
3580 if (area_attr != 0)
3581 char_attr = area_attr;
3582 else if (search_attr != 0)
3583 char_attr = search_attr;
3584 #ifdef LINE_ATTR
3585 /* Use line_attr when not in the Visual or 'incsearch' area
3586 * (area_attr may be 0 when "noinvcur" is set). */
3587 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
3588 || vcol < fromcol || vcol_prev < fromcol_prev
3589 || vcol >= tocol))
3590 char_attr = line_attr;
3591 #endif
3592 else
3594 attr_pri = FALSE;
3595 #ifdef FEAT_SYN_HL
3596 if (has_syntax)
3597 char_attr = syntax_attr;
3598 else
3599 #endif
3600 char_attr = 0;
3605 * Get the next character to put on the screen.
3608 * The "p_extra" points to the extra stuff that is inserted to
3609 * represent special characters (non-printable stuff) and other
3610 * things. When all characters are the same, c_extra is used.
3611 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
3612 * "p_extra[n_extra]".
3613 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
3615 if (n_extra > 0)
3617 if (c_extra != NUL)
3619 c = c_extra;
3620 #ifdef FEAT_MBYTE
3621 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
3622 if (enc_utf8 && (*mb_char2len)(c) > 1)
3624 mb_utf8 = TRUE;
3625 u8cc[0] = 0;
3626 c = 0xc0;
3628 else
3629 mb_utf8 = FALSE;
3630 #endif
3632 else
3634 c = *p_extra;
3635 #ifdef FEAT_MBYTE
3636 if (has_mbyte)
3638 mb_c = c;
3639 if (enc_utf8)
3641 /* If the UTF-8 character is more than one byte:
3642 * Decode it into "mb_c". */
3643 mb_l = (*mb_ptr2len)(p_extra);
3644 mb_utf8 = FALSE;
3645 if (mb_l > n_extra)
3646 mb_l = 1;
3647 else if (mb_l > 1)
3649 mb_c = utfc_ptr2char(p_extra, u8cc);
3650 mb_utf8 = TRUE;
3651 c = 0xc0;
3654 else
3656 /* if this is a DBCS character, put it in "mb_c" */
3657 mb_l = MB_BYTE2LEN(c);
3658 if (mb_l >= n_extra)
3659 mb_l = 1;
3660 else if (mb_l > 1)
3661 mb_c = (c << 8) + p_extra[1];
3663 if (mb_l == 0) /* at the NUL at end-of-line */
3664 mb_l = 1;
3666 /* If a double-width char doesn't fit display a '>' in the
3667 * last column. */
3668 if ((
3669 # ifdef FEAT_RIGHTLEFT
3670 wp->w_p_rl ? (col <= 0) :
3671 # endif
3672 (col >= W_WIDTH(wp) - 1))
3673 && (*mb_char2cells)(mb_c) == 2)
3675 c = '>';
3676 mb_c = c;
3677 mb_l = 1;
3678 mb_utf8 = FALSE;
3679 multi_attr = hl_attr(HLF_AT);
3680 /* put the pointer back to output the double-width
3681 * character at the start of the next line. */
3682 ++n_extra;
3683 --p_extra;
3685 else
3687 n_extra -= mb_l - 1;
3688 p_extra += mb_l - 1;
3691 #endif
3692 ++p_extra;
3694 --n_extra;
3696 else
3699 * Get a character from the line itself.
3701 c = *ptr;
3702 #ifdef FEAT_MBYTE
3703 if (has_mbyte)
3705 mb_c = c;
3706 if (enc_utf8)
3708 /* If the UTF-8 character is more than one byte: Decode it
3709 * into "mb_c". */
3710 mb_l = (*mb_ptr2len)(ptr);
3711 mb_utf8 = FALSE;
3712 if (mb_l > 1)
3714 mb_c = utfc_ptr2char(ptr, u8cc);
3715 /* Overlong encoded ASCII or ASCII with composing char
3716 * is displayed normally, except a NUL. */
3717 if (mb_c < 0x80)
3718 c = mb_c;
3719 mb_utf8 = TRUE;
3721 /* At start of the line we can have a composing char.
3722 * Draw it as a space with a composing char. */
3723 if (utf_iscomposing(mb_c))
3725 int i;
3727 for (i = Screen_mco - 1; i > 0; --i)
3728 u8cc[i] = u8cc[i - 1];
3729 u8cc[0] = mb_c;
3730 mb_c = ' ';
3734 if ((mb_l == 1 && c >= 0x80)
3735 || (mb_l >= 1 && mb_c == 0)
3736 || (mb_l > 1 && (!vim_isprintc(mb_c)
3737 # ifdef UNICODE16
3738 || mb_c >= 0x10000
3739 # endif
3743 * Illegal UTF-8 byte: display as <xx>.
3744 * Non-BMP character : display as ? or fullwidth ?.
3746 # ifdef UNICODE16
3747 if (mb_c < 0x10000)
3748 # endif
3750 transchar_hex(extra, mb_c);
3751 # ifdef FEAT_RIGHTLEFT
3752 if (wp->w_p_rl) /* reverse */
3753 rl_mirror(extra);
3754 # endif
3756 # ifdef UNICODE16
3757 else if (utf_char2cells(mb_c) != 2)
3758 STRCPY(extra, "?");
3759 else
3760 /* 0xff1f in UTF-8: full-width '?' */
3761 STRCPY(extra, "\357\274\237");
3762 # endif
3764 p_extra = extra;
3765 c = *p_extra;
3766 mb_c = mb_ptr2char_adv(&p_extra);
3767 mb_utf8 = (c >= 0x80);
3768 n_extra = (int)STRLEN(p_extra);
3769 c_extra = NUL;
3770 if (area_attr == 0 && search_attr == 0)
3772 n_attr = n_extra + 1;
3773 extra_attr = hl_attr(HLF_8);
3774 saved_attr2 = char_attr; /* save current attr */
3777 else if (mb_l == 0) /* at the NUL at end-of-line */
3778 mb_l = 1;
3779 #ifdef FEAT_ARABIC
3780 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
3782 /* Do Arabic shaping. */
3783 int pc, pc1, nc;
3784 int pcc[MAX_MCO];
3786 /* The idea of what is the previous and next
3787 * character depends on 'rightleft'. */
3788 if (wp->w_p_rl)
3790 pc = prev_c;
3791 pc1 = prev_c1;
3792 nc = utf_ptr2char(ptr + mb_l);
3793 prev_c1 = u8cc[0];
3795 else
3797 pc = utfc_ptr2char(ptr + mb_l, pcc);
3798 nc = prev_c;
3799 pc1 = pcc[0];
3801 prev_c = mb_c;
3803 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
3805 else
3806 prev_c = mb_c;
3807 #endif
3809 else /* enc_dbcs */
3811 mb_l = MB_BYTE2LEN(c);
3812 if (mb_l == 0) /* at the NUL at end-of-line */
3813 mb_l = 1;
3814 else if (mb_l > 1)
3816 /* We assume a second byte below 32 is illegal.
3817 * Hopefully this is OK for all double-byte encodings!
3819 if (ptr[1] >= 32)
3820 mb_c = (c << 8) + ptr[1];
3821 else
3823 if (ptr[1] == NUL)
3825 /* head byte at end of line */
3826 mb_l = 1;
3827 transchar_nonprint(extra, c);
3829 else
3831 /* illegal tail byte */
3832 mb_l = 2;
3833 STRCPY(extra, "XX");
3835 p_extra = extra;
3836 n_extra = (int)STRLEN(extra) - 1;
3837 c_extra = NUL;
3838 c = *p_extra++;
3839 if (area_attr == 0 && search_attr == 0)
3841 n_attr = n_extra + 1;
3842 extra_attr = hl_attr(HLF_8);
3843 saved_attr2 = char_attr; /* save current attr */
3845 mb_c = c;
3849 /* If a double-width char doesn't fit display a '>' in the
3850 * last column; the character is displayed at the start of the
3851 * next line. */
3852 if ((
3853 # ifdef FEAT_RIGHTLEFT
3854 wp->w_p_rl ? (col <= 0) :
3855 # endif
3856 (col >= W_WIDTH(wp) - 1))
3857 && (*mb_char2cells)(mb_c) == 2)
3859 c = '>';
3860 mb_c = c;
3861 mb_utf8 = FALSE;
3862 mb_l = 1;
3863 multi_attr = hl_attr(HLF_AT);
3864 /* Put pointer back so that the character will be
3865 * displayed at the start of the next line. */
3866 --ptr;
3868 else if (*ptr != NUL)
3869 ptr += mb_l - 1;
3871 /* If a double-width char doesn't fit at the left side display
3872 * a '<' in the first column. */
3873 if (n_skip > 0 && mb_l > 1)
3875 n_extra = 1;
3876 c_extra = '<';
3877 c = ' ';
3878 if (area_attr == 0 && search_attr == 0)
3880 n_attr = n_extra + 1;
3881 extra_attr = hl_attr(HLF_AT);
3882 saved_attr2 = char_attr; /* save current attr */
3884 mb_c = c;
3885 mb_utf8 = FALSE;
3886 mb_l = 1;
3890 #endif
3891 ++ptr;
3893 /* 'list' : change char 160 to lcs_nbsp. */
3894 if (wp->w_p_list && (c == 160
3895 #ifdef FEAT_MBYTE
3896 || (mb_utf8 && mb_c == 160)
3897 #endif
3898 ) && lcs_nbsp)
3900 c = lcs_nbsp;
3901 if (area_attr == 0 && search_attr == 0)
3903 n_attr = 1;
3904 extra_attr = hl_attr(HLF_8);
3905 saved_attr2 = char_attr; /* save current attr */
3907 #ifdef FEAT_MBYTE
3908 mb_c = c;
3909 if (enc_utf8 && (*mb_char2len)(c) > 1)
3911 mb_utf8 = TRUE;
3912 u8cc[0] = 0;
3913 c = 0xc0;
3915 else
3916 mb_utf8 = FALSE;
3917 #endif
3920 if (extra_check)
3922 #ifdef FEAT_SPELL
3923 int can_spell = TRUE;
3924 #endif
3926 #ifdef FEAT_SYN_HL
3927 /* Get syntax attribute, unless still at the start of the line
3928 * (double-wide char that doesn't fit). */
3929 v = (long)(ptr - line);
3930 if (has_syntax && v > 0)
3932 /* Get the syntax attribute for the character. If there
3933 * is an error, disable syntax highlighting. */
3934 save_did_emsg = did_emsg;
3935 did_emsg = FALSE;
3937 syntax_attr = get_syntax_attr((colnr_T)v - 1,
3938 # ifdef FEAT_SPELL
3939 has_spell ? &can_spell :
3940 # endif
3941 NULL, FALSE);
3943 if (did_emsg)
3945 wp->w_buffer->b_syn_error = TRUE;
3946 has_syntax = FALSE;
3948 else
3949 did_emsg = save_did_emsg;
3951 /* Need to get the line again, a multi-line regexp may
3952 * have made it invalid. */
3953 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3954 ptr = line + v;
3956 if (!attr_pri)
3957 char_attr = syntax_attr;
3958 else
3959 char_attr = hl_combine_attr(syntax_attr, char_attr);
3961 #endif
3963 #ifdef FEAT_SPELL
3964 /* Check spelling (unless at the end of the line).
3965 * Only do this when there is no syntax highlighting, the
3966 * @Spell cluster is not used or the current syntax item
3967 * contains the @Spell cluster. */
3968 if (has_spell && v >= word_end && v > cur_checked_col)
3970 spell_attr = 0;
3971 # ifdef FEAT_SYN_HL
3972 if (!attr_pri)
3973 char_attr = syntax_attr;
3974 # endif
3975 if (c != 0 && (
3976 # ifdef FEAT_SYN_HL
3977 !has_syntax ||
3978 # endif
3979 can_spell))
3981 char_u *prev_ptr, *p;
3982 int len;
3983 hlf_T spell_hlf = HLF_COUNT;
3984 # ifdef FEAT_MBYTE
3985 if (has_mbyte)
3987 prev_ptr = ptr - mb_l;
3988 v -= mb_l - 1;
3990 else
3991 # endif
3992 prev_ptr = ptr - 1;
3994 /* Use nextline[] if possible, it has the start of the
3995 * next line concatenated. */
3996 if ((prev_ptr - line) - nextlinecol >= 0)
3997 p = nextline + (prev_ptr - line) - nextlinecol;
3998 else
3999 p = prev_ptr;
4000 cap_col -= (int)(prev_ptr - line);
4001 len = spell_check(wp, p, &spell_hlf, &cap_col,
4002 nochange);
4003 word_end = v + len;
4005 /* In Insert mode only highlight a word that
4006 * doesn't touch the cursor. */
4007 if (spell_hlf != HLF_COUNT
4008 && (State & INSERT) != 0
4009 && wp->w_cursor.lnum == lnum
4010 && wp->w_cursor.col >=
4011 (colnr_T)(prev_ptr - line)
4012 && wp->w_cursor.col < (colnr_T)word_end)
4014 spell_hlf = HLF_COUNT;
4015 spell_redraw_lnum = lnum;
4018 if (spell_hlf == HLF_COUNT && p != prev_ptr
4019 && (p - nextline) + len > nextline_idx)
4021 /* Remember that the good word continues at the
4022 * start of the next line. */
4023 checked_lnum = lnum + 1;
4024 checked_col = (int)((p - nextline) + len - nextline_idx);
4027 /* Turn index into actual attributes. */
4028 if (spell_hlf != HLF_COUNT)
4029 spell_attr = highlight_attr[spell_hlf];
4031 if (cap_col > 0)
4033 if (p != prev_ptr
4034 && (p - nextline) + cap_col >= nextline_idx)
4036 /* Remember that the word in the next line
4037 * must start with a capital. */
4038 capcol_lnum = lnum + 1;
4039 cap_col = (int)((p - nextline) + cap_col
4040 - nextline_idx);
4042 else
4043 /* Compute the actual column. */
4044 cap_col += (int)(prev_ptr - line);
4048 if (spell_attr != 0)
4050 if (!attr_pri)
4051 char_attr = hl_combine_attr(char_attr, spell_attr);
4052 else
4053 char_attr = hl_combine_attr(spell_attr, char_attr);
4055 #endif
4056 #ifdef FEAT_LINEBREAK
4058 * Found last space before word: check for line break.
4060 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr)
4061 && !wp->w_p_list)
4063 n_extra = win_lbr_chartabsize(wp, ptr - (
4064 # ifdef FEAT_MBYTE
4065 has_mbyte ? mb_l :
4066 # endif
4067 1), (colnr_T)vcol, NULL) - 1;
4068 c_extra = ' ';
4069 if (vim_iswhite(c))
4070 c = ' ';
4072 #endif
4074 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4076 c = lcs_trail;
4077 if (!attr_pri)
4079 n_attr = 1;
4080 extra_attr = hl_attr(HLF_8);
4081 saved_attr2 = char_attr; /* save current attr */
4083 #ifdef FEAT_MBYTE
4084 mb_c = c;
4085 if (enc_utf8 && (*mb_char2len)(c) > 1)
4087 mb_utf8 = TRUE;
4088 u8cc[0] = 0;
4089 c = 0xc0;
4091 else
4092 mb_utf8 = FALSE;
4093 #endif
4098 * Handling of non-printable characters.
4100 if (!(chartab[c & 0xff] & CT_PRINT_CHAR))
4103 * when getting a character from the file, we may have to
4104 * turn it into something else on the way to putting it
4105 * into "ScreenLines".
4107 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4109 /* tab amount depends on current column */
4110 n_extra = (int)wp->w_buffer->b_p_ts
4111 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4112 #ifdef FEAT_MBYTE
4113 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4114 #endif
4115 if (wp->w_p_list)
4117 c = lcs_tab1;
4118 c_extra = lcs_tab2;
4119 n_attr = n_extra + 1;
4120 extra_attr = hl_attr(HLF_8);
4121 saved_attr2 = char_attr; /* save current attr */
4122 #ifdef FEAT_MBYTE
4123 mb_c = c;
4124 if (enc_utf8 && (*mb_char2len)(c) > 1)
4126 mb_utf8 = TRUE;
4127 u8cc[0] = 0;
4128 c = 0xc0;
4130 #endif
4132 else
4134 c_extra = ' ';
4135 c = ' ';
4138 else if (c == NUL
4139 && ((wp->w_p_list && lcs_eol > 0)
4140 || ((fromcol >= 0 || fromcol_prev >= 0)
4141 && tocol > vcol
4142 #ifdef FEAT_VISUAL
4143 && VIsual_mode != Ctrl_V
4144 #endif
4145 && (
4146 # ifdef FEAT_RIGHTLEFT
4147 wp->w_p_rl ? (col >= 0) :
4148 # endif
4149 (col < W_WIDTH(wp)))
4150 && !(noinvcur
4151 && lnum == wp->w_cursor.lnum
4152 && (colnr_T)vcol == wp->w_virtcol)))
4153 && lcs_eol_one >= 0)
4155 /* Display a '$' after the line or highlight an extra
4156 * character if the line break is included. */
4157 #if defined(FEAT_DIFF) || defined(LINE_ATTR)
4158 /* For a diff line the highlighting continues after the
4159 * "$". */
4160 if (
4161 # ifdef FEAT_DIFF
4162 diff_hlf == (hlf_T)0
4163 # ifdef LINE_ATTR
4165 # endif
4166 # endif
4167 # ifdef LINE_ATTR
4168 line_attr == 0
4169 # endif
4171 #endif
4173 #ifdef FEAT_VIRTUALEDIT
4174 /* In virtualedit, visual selections may extend
4175 * beyond end of line. */
4176 if (area_highlighting && virtual_active()
4177 && tocol != MAXCOL && vcol < tocol)
4178 n_extra = 0;
4179 else
4180 #endif
4182 p_extra = at_end_str;
4183 n_extra = 1;
4184 c_extra = NUL;
4187 if (wp->w_p_list)
4188 c = lcs_eol;
4189 else
4190 c = ' ';
4191 lcs_eol_one = -1;
4192 --ptr; /* put it back at the NUL */
4193 if (!attr_pri)
4195 extra_attr = hl_attr(HLF_AT);
4196 n_attr = 1;
4198 #ifdef FEAT_MBYTE
4199 mb_c = c;
4200 if (enc_utf8 && (*mb_char2len)(c) > 1)
4202 mb_utf8 = TRUE;
4203 u8cc[0] = 0;
4204 c = 0xc0;
4206 else
4207 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4208 #endif
4210 else if (c != NUL)
4212 p_extra = transchar(c);
4213 #ifdef FEAT_RIGHTLEFT
4214 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4215 rl_mirror(p_extra); /* reverse "<12>" */
4216 #endif
4217 n_extra = byte2cells(c) - 1;
4218 c_extra = NUL;
4219 c = *p_extra++;
4220 if (!attr_pri)
4222 n_attr = n_extra + 1;
4223 extra_attr = hl_attr(HLF_8);
4224 saved_attr2 = char_attr; /* save current attr */
4226 #ifdef FEAT_MBYTE
4227 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4228 #endif
4230 #ifdef FEAT_VIRTUALEDIT
4231 else if (VIsual_active
4232 && (VIsual_mode == Ctrl_V
4233 || VIsual_mode == 'v')
4234 && virtual_active()
4235 && tocol != MAXCOL
4236 && vcol < tocol
4237 && (
4238 # ifdef FEAT_RIGHTLEFT
4239 wp->w_p_rl ? (col >= 0) :
4240 # endif
4241 (col < W_WIDTH(wp))))
4243 c = ' ';
4244 --ptr; /* put it back at the NUL */
4246 #endif
4247 #if defined(LINE_ATTR)
4248 else if ((
4249 # ifdef FEAT_DIFF
4250 diff_hlf != (hlf_T)0 ||
4251 # endif
4252 line_attr != 0
4253 ) && (
4254 # ifdef FEAT_RIGHTLEFT
4255 wp->w_p_rl ? (col >= 0) :
4256 # endif
4257 (col < W_WIDTH(wp))))
4259 /* Highlight until the right side of the window */
4260 c = ' ';
4261 --ptr; /* put it back at the NUL */
4263 /* Remember we do the char for line highlighting. */
4264 ++did_line_attr;
4266 /* don't do search HL for the rest of the line */
4267 if (line_attr != 0 && char_attr == search_attr && col > 0)
4268 char_attr = line_attr;
4269 # ifdef FEAT_DIFF
4270 if (diff_hlf == HLF_TXD)
4272 diff_hlf = HLF_CHD;
4273 if (attr == 0 || char_attr != attr)
4274 char_attr = hl_attr(diff_hlf);
4276 # endif
4278 #endif
4282 /* Don't override visual selection highlighting. */
4283 if (n_attr > 0
4284 && draw_state == WL_LINE
4285 && !attr_pri)
4286 char_attr = extra_attr;
4288 #if defined(FEAT_XIM) && (defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MACVIM))
4289 /* XIM don't send preedit_start and preedit_end, but they send
4290 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
4291 * im_is_preediting() here. */
4292 if (
4293 # ifndef FEAT_GUI_MACVIM
4294 xic != NULL &&
4295 # endif
4296 lnum == wp->w_cursor.lnum
4297 && (State & INSERT)
4298 && !p_imdisable
4299 && im_is_preediting()
4300 && draw_state == WL_LINE)
4302 colnr_T tcol;
4304 if (preedit_end_col == MAXCOL)
4305 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
4306 else
4307 tcol = preedit_end_col;
4308 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
4310 if (feedback_old_attr < 0)
4312 feedback_col = 0;
4313 feedback_old_attr = char_attr;
4315 char_attr = im_get_feedback_attr(feedback_col);
4316 if (char_attr < 0)
4317 char_attr = feedback_old_attr;
4318 feedback_col++;
4320 else if (feedback_old_attr >= 0)
4322 char_attr = feedback_old_attr;
4323 feedback_old_attr = -1;
4324 feedback_col = 0;
4327 #endif
4329 * Handle the case where we are in column 0 but not on the first
4330 * character of the line and the user wants us to show us a
4331 * special character (via 'listchars' option "precedes:<char>".
4333 if (lcs_prec_todo != NUL
4334 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
4335 #ifdef FEAT_DIFF
4336 && filler_todo <= 0
4337 #endif
4338 && draw_state > WL_NR
4339 && c != NUL)
4341 c = lcs_prec;
4342 lcs_prec_todo = NUL;
4343 #ifdef FEAT_MBYTE
4344 mb_c = c;
4345 if (enc_utf8 && (*mb_char2len)(c) > 1)
4347 mb_utf8 = TRUE;
4348 u8cc[0] = 0;
4349 c = 0xc0;
4351 else
4352 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4353 #endif
4354 if (!attr_pri)
4356 saved_attr3 = char_attr; /* save current attr */
4357 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
4358 n_attr3 = 1;
4363 * At end of the text line or just after the last character.
4365 if (c == NUL
4366 #if defined(LINE_ATTR)
4367 || did_line_attr == 1
4368 #endif
4371 #ifdef FEAT_SEARCH_EXTRA
4372 long prevcol = (long)(ptr - line) - (c == NUL);
4374 /* we're not really at that column when skipping some text */
4375 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
4376 ++prevcol;
4377 #endif
4379 /* invert at least one char, used for Visual and empty line or
4380 * highlight match at end of line. If it's beyond the last
4381 * char on the screen, just overwrite that one (tricky!) Not
4382 * needed when a '$' was displayed for 'list'. */
4383 #ifdef FEAT_SEARCH_EXTRA
4384 prevcol_hl_flag = FALSE;
4385 if (prevcol == (long)search_hl.startcol)
4386 prevcol_hl_flag = TRUE;
4387 else
4389 cur = wp->w_match_head;
4390 while (cur != NULL)
4392 if (prevcol == (long)cur->hl.startcol)
4394 prevcol_hl_flag = TRUE;
4395 break;
4397 cur = cur->next;
4400 #endif
4401 if (lcs_eol == lcs_eol_one
4402 && ((area_attr != 0 && vcol == fromcol
4403 #ifdef FEAT_VISUAL
4404 && (VIsual_mode != Ctrl_V
4405 || lnum == VIsual.lnum
4406 || lnum == curwin->w_cursor.lnum)
4407 #endif
4408 && c == NUL)
4409 #ifdef FEAT_SEARCH_EXTRA
4410 /* highlight 'hlsearch' match at end of line */
4411 || (prevcol_hl_flag == TRUE
4412 # if defined(LINE_ATTR)
4413 && did_line_attr <= 1
4414 # endif
4416 #endif
4419 int n = 0;
4421 #ifdef FEAT_RIGHTLEFT
4422 if (wp->w_p_rl)
4424 if (col < 0)
4425 n = 1;
4427 else
4428 #endif
4430 if (col >= W_WIDTH(wp))
4431 n = -1;
4433 if (n != 0)
4435 /* At the window boundary, highlight the last character
4436 * instead (better than nothing). */
4437 off += n;
4438 col += n;
4440 else
4442 /* Add a blank character to highlight. */
4443 ScreenLines[off] = ' ';
4444 #ifdef FEAT_MBYTE
4445 if (enc_utf8)
4446 ScreenLinesUC[off] = 0;
4447 #endif
4449 #ifdef FEAT_SEARCH_EXTRA
4450 if (area_attr == 0)
4452 /* Use attributes from match with highest priority among
4453 * 'search_hl' and the match list. */
4454 char_attr = search_hl.attr;
4455 cur = wp->w_match_head;
4456 shl_flag = FALSE;
4457 while (cur != NULL || shl_flag == FALSE)
4459 if (shl_flag == FALSE
4460 && ((cur != NULL
4461 && cur->priority > SEARCH_HL_PRIORITY)
4462 || cur == NULL))
4464 shl = &search_hl;
4465 shl_flag = TRUE;
4467 else
4468 shl = &cur->hl;
4469 if ((ptr - line) - 1 == (long)shl->startcol)
4470 char_attr = shl->attr;
4471 if (shl != &search_hl && cur != NULL)
4472 cur = cur->next;
4475 #endif
4476 ScreenAttrs[off] = char_attr;
4477 #ifdef FEAT_RIGHTLEFT
4478 if (wp->w_p_rl)
4480 --col;
4481 --off;
4483 else
4484 #endif
4486 ++col;
4487 ++off;
4489 ++vcol;
4490 #ifdef FEAT_SYN_HL
4491 eol_hl_off = 1;
4492 #endif
4497 * At end of the text line.
4499 if (c == NUL)
4501 #ifdef FEAT_SYN_HL
4502 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
4503 && lnum == wp->w_cursor.lnum)
4505 /* highlight last char after line */
4506 --col;
4507 --off;
4508 --vcol;
4511 /* Highlight 'cursorcolumn' past end of the line. */
4512 if (wp->w_p_wrap)
4513 v = wp->w_skipcol;
4514 else
4515 v = wp->w_leftcol;
4516 /* check if line ends before left margin */
4517 if (vcol < v + col - win_col_off(wp))
4519 vcol = v + col - win_col_off(wp);
4520 if (wp->w_p_cuc
4521 && (int)wp->w_virtcol >= vcol - eol_hl_off
4522 && (int)wp->w_virtcol < W_WIDTH(wp) * (row - startrow + 1)
4524 && lnum != wp->w_cursor.lnum
4525 # ifdef FEAT_RIGHTLEFT
4526 && !wp->w_p_rl
4527 # endif
4530 while (col < W_WIDTH(wp))
4532 ScreenLines[off] = ' ';
4533 #ifdef FEAT_MBYTE
4534 if (enc_utf8)
4535 ScreenLinesUC[off] = 0;
4536 #endif
4537 ++col;
4538 if (vcol == (long)wp->w_virtcol)
4540 ScreenAttrs[off] = hl_attr(HLF_CUC);
4541 break;
4543 ScreenAttrs[off++] = 0;
4544 ++vcol;
4547 #endif
4549 SCREEN_LINE(screen_row, W_WINCOL(wp), col, (int)W_WIDTH(wp),
4550 wp->w_p_rl);
4551 row++;
4554 * Update w_cline_height and w_cline_folded if the cursor line was
4555 * updated (saves a call to plines() later).
4557 if (wp == curwin && lnum == curwin->w_cursor.lnum)
4559 curwin->w_cline_row = startrow;
4560 curwin->w_cline_height = row - startrow;
4561 #ifdef FEAT_FOLDING
4562 curwin->w_cline_folded = FALSE;
4563 #endif
4564 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
4567 break;
4570 /* line continues beyond line end */
4571 if (lcs_ext
4572 && !wp->w_p_wrap
4573 #ifdef FEAT_DIFF
4574 && filler_todo <= 0
4575 #endif
4576 && (
4577 #ifdef FEAT_RIGHTLEFT
4578 wp->w_p_rl ? col == 0 :
4579 #endif
4580 col == W_WIDTH(wp) - 1)
4581 && (*ptr != NUL
4582 || (wp->w_p_list && lcs_eol_one > 0)
4583 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
4585 c = lcs_ext;
4586 char_attr = hl_attr(HLF_AT);
4587 #ifdef FEAT_MBYTE
4588 mb_c = c;
4589 if (enc_utf8 && (*mb_char2len)(c) > 1)
4591 mb_utf8 = TRUE;
4592 u8cc[0] = 0;
4593 c = 0xc0;
4595 else
4596 mb_utf8 = FALSE;
4597 #endif
4600 #ifdef FEAT_SYN_HL
4601 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
4602 * highlight the cursor position itself. */
4603 if (wp->w_p_cuc && vcol == (long)wp->w_virtcol
4604 && lnum != wp->w_cursor.lnum
4605 && draw_state == WL_LINE
4606 && !lnum_in_visual_area)
4608 vcol_save_attr = char_attr;
4609 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
4611 else
4612 vcol_save_attr = -1;
4613 #endif
4616 * Store character to be displayed.
4617 * Skip characters that are left of the screen for 'nowrap'.
4619 vcol_prev = vcol;
4620 if (draw_state < WL_LINE || n_skip <= 0)
4623 * Store the character.
4625 #if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
4626 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
4628 /* A double-wide character is: put first halve in left cell. */
4629 --off;
4630 --col;
4632 #endif
4633 ScreenLines[off] = c;
4634 #ifdef FEAT_MBYTE
4635 if (enc_dbcs == DBCS_JPNU)
4637 if ((mb_c & 0xff00) == 0x8e00)
4638 ScreenLines[off] = 0x8e;
4639 ScreenLines2[off] = mb_c & 0xff;
4641 else if (enc_utf8)
4643 if (mb_utf8)
4645 int i;
4647 ScreenLinesUC[off] = mb_c;
4648 if ((c & 0xff) == 0)
4649 ScreenLines[off] = 0x80; /* avoid storing zero */
4650 for (i = 0; i < Screen_mco; ++i)
4652 ScreenLinesC[i][off] = u8cc[i];
4653 if (u8cc[i] == 0)
4654 break;
4657 else
4658 ScreenLinesUC[off] = 0;
4660 if (multi_attr)
4662 ScreenAttrs[off] = multi_attr;
4663 multi_attr = 0;
4665 else
4666 #endif
4667 ScreenAttrs[off] = char_attr;
4669 #ifdef FEAT_MBYTE
4670 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
4672 /* Need to fill two screen columns. */
4673 ++off;
4674 ++col;
4675 if (enc_utf8)
4676 /* UTF-8: Put a 0 in the second screen char. */
4677 ScreenLines[off] = 0;
4678 else
4679 /* DBCS: Put second byte in the second screen char. */
4680 ScreenLines[off] = mb_c & 0xff;
4681 ++vcol;
4682 /* When "tocol" is halfway a character, set it to the end of
4683 * the character, otherwise highlighting won't stop. */
4684 if (tocol == vcol)
4685 ++tocol;
4686 #ifdef FEAT_RIGHTLEFT
4687 if (wp->w_p_rl)
4689 /* now it's time to backup one cell */
4690 --off;
4691 --col;
4693 #endif
4695 #endif
4696 #ifdef FEAT_RIGHTLEFT
4697 if (wp->w_p_rl)
4699 --off;
4700 --col;
4702 else
4703 #endif
4705 ++off;
4706 ++col;
4709 else
4710 --n_skip;
4712 /* Only advance the "vcol" when after the 'number' column. */
4713 if (draw_state > WL_NR
4714 #ifdef FEAT_DIFF
4715 && filler_todo <= 0
4716 #endif
4718 ++vcol;
4720 #ifdef FEAT_SYN_HL
4721 if (vcol_save_attr >= 0)
4722 char_attr = vcol_save_attr;
4723 #endif
4725 /* restore attributes after "predeces" in 'listchars' */
4726 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
4727 char_attr = saved_attr3;
4729 /* restore attributes after last 'listchars' or 'number' char */
4730 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
4731 char_attr = saved_attr2;
4734 * At end of screen line and there is more to come: Display the line
4735 * so far. If there is no more to display it is caught above.
4737 if ((
4738 #ifdef FEAT_RIGHTLEFT
4739 wp->w_p_rl ? (col < 0) :
4740 #endif
4741 (col >= W_WIDTH(wp)))
4742 && (*ptr != NUL
4743 #ifdef FEAT_DIFF
4744 || filler_todo > 0
4745 #endif
4746 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
4747 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
4750 SCREEN_LINE(screen_row, W_WINCOL(wp), col, (int)W_WIDTH(wp),
4751 wp->w_p_rl);
4752 ++row;
4753 ++screen_row;
4755 /* When not wrapping and finished diff lines, or when displayed
4756 * '$' and highlighting until last column, break here. */
4757 if ((!wp->w_p_wrap
4758 #ifdef FEAT_DIFF
4759 && filler_todo <= 0
4760 #endif
4761 ) || lcs_eol_one == -1)
4762 break;
4764 /* When the window is too narrow draw all "@" lines. */
4765 if (draw_state != WL_LINE
4766 #ifdef FEAT_DIFF
4767 && filler_todo <= 0
4768 #endif
4771 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
4772 #ifdef FEAT_VERTSPLIT
4773 draw_vsep_win(wp, row);
4774 #endif
4775 row = endrow;
4778 /* When line got too long for screen break here. */
4779 if (row == endrow)
4781 ++row;
4782 break;
4785 if (screen_cur_row == screen_row - 1
4786 #ifdef FEAT_DIFF
4787 && filler_todo <= 0
4788 #endif
4789 && W_WIDTH(wp) == Columns)
4791 /* Remember that the line wraps, used for modeless copy. */
4792 LineWraps[screen_row - 1] = TRUE;
4795 * Special trick to make copy/paste of wrapped lines work with
4796 * xterm/screen: write an extra character beyond the end of
4797 * the line. This will work with all terminal types
4798 * (regardless of the xn,am settings).
4799 * Only do this on a fast tty.
4800 * Only do this if the cursor is on the current line
4801 * (something has been written in it).
4802 * Don't do this for the GUI.
4803 * Don't do this for double-width characters.
4804 * Don't do this for a window not at the right screen border.
4806 if (p_tf
4807 #ifdef FEAT_GUI
4808 && !gui.in_use
4809 #endif
4810 #ifdef FEAT_MBYTE
4811 && !(has_mbyte
4812 && ((*mb_off2cells)(LineOffset[screen_row],
4813 LineOffset[screen_row] + screen_Columns)
4814 == 2
4815 || (*mb_off2cells)(LineOffset[screen_row - 1]
4816 + (int)Columns - 2,
4817 LineOffset[screen_row] + screen_Columns)
4818 == 2))
4819 #endif
4822 /* First make sure we are at the end of the screen line,
4823 * then output the same character again to let the
4824 * terminal know about the wrap. If the terminal doesn't
4825 * auto-wrap, we overwrite the character. */
4826 if (screen_cur_col != W_WIDTH(wp))
4827 screen_char(LineOffset[screen_row - 1]
4828 + (unsigned)Columns - 1,
4829 screen_row - 1, (int)(Columns - 1));
4831 #ifdef FEAT_MBYTE
4832 /* When there is a multi-byte character, just output a
4833 * space to keep it simple. */
4834 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
4835 screen_row - 1] + (Columns - 1)]) > 1)
4836 out_char(' ');
4837 else
4838 #endif
4839 out_char(ScreenLines[LineOffset[screen_row - 1]
4840 + (Columns - 1)]);
4841 /* force a redraw of the first char on the next line */
4842 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
4843 screen_start(); /* don't know where cursor is now */
4847 col = 0;
4848 off = (unsigned)(current_ScreenLine - ScreenLines);
4849 #ifdef FEAT_RIGHTLEFT
4850 if (wp->w_p_rl)
4852 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
4853 off += col;
4855 #endif
4857 /* reset the drawing state for the start of a wrapped line */
4858 draw_state = WL_START;
4859 saved_n_extra = n_extra;
4860 saved_p_extra = p_extra;
4861 saved_c_extra = c_extra;
4862 saved_char_attr = char_attr;
4863 n_extra = 0;
4864 lcs_prec_todo = lcs_prec;
4865 #ifdef FEAT_LINEBREAK
4866 # ifdef FEAT_DIFF
4867 if (filler_todo <= 0)
4868 # endif
4869 need_showbreak = TRUE;
4870 #endif
4871 #ifdef FEAT_DIFF
4872 --filler_todo;
4873 /* When the filler lines are actually below the last line of the
4874 * file, don't draw the line itself, break here. */
4875 if (filler_todo == 0 && wp->w_botfill)
4876 break;
4877 #endif
4880 } /* for every character in the line */
4882 #ifdef FEAT_SPELL
4883 /* After an empty line check first word for capital. */
4884 if (*skipwhite(line) == NUL)
4886 capcol_lnum = lnum + 1;
4887 cap_col = 0;
4889 #endif
4891 return row;
4894 #ifdef FEAT_MBYTE
4895 static int comp_char_differs __ARGS((int, int));
4898 * Return if the composing characters at "off_from" and "off_to" differ.
4900 static int
4901 comp_char_differs(off_from, off_to)
4902 int off_from;
4903 int off_to;
4905 int i;
4907 for (i = 0; i < Screen_mco; ++i)
4909 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
4910 return TRUE;
4911 if (ScreenLinesC[i][off_from] == 0)
4912 break;
4914 return FALSE;
4916 #endif
4919 * Check whether the given character needs redrawing:
4920 * - the (first byte of the) character is different
4921 * - the attributes are different
4922 * - the character is multi-byte and the next byte is different
4923 * - the character is two cells wide and the second cell differs.
4925 static int
4926 char_needs_redraw(off_from, off_to, cols)
4927 int off_from;
4928 int off_to;
4929 int cols;
4931 if (cols > 0
4932 && ((ScreenLines[off_from] != ScreenLines[off_to]
4933 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
4935 #ifdef FEAT_MBYTE
4936 || (enc_dbcs != 0
4937 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
4938 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
4939 ? ScreenLines2[off_from] != ScreenLines2[off_to]
4940 : (cols > 1 && ScreenLines[off_from + 1]
4941 != ScreenLines[off_to + 1])))
4942 || (enc_utf8
4943 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
4944 || (ScreenLinesUC[off_from] != 0
4945 && comp_char_differs(off_from, off_to))
4946 || (cols > 1 && ScreenLines[off_from + 1]
4947 != ScreenLines[off_to + 1])))
4948 #endif
4950 return TRUE;
4951 return FALSE;
4955 * Move one "cooked" screen line to the screen, but only the characters that
4956 * have actually changed. Handle insert/delete character.
4957 * "coloff" gives the first column on the screen for this line.
4958 * "endcol" gives the columns where valid characters are.
4959 * "clear_width" is the width of the window. It's > 0 if the rest of the line
4960 * needs to be cleared, negative otherwise.
4961 * "rlflag" is TRUE in a rightleft window:
4962 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
4963 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
4965 static void
4966 screen_line(row, coloff, endcol, clear_width
4967 #ifdef FEAT_RIGHTLEFT
4968 , rlflag
4969 #endif
4971 int row;
4972 int coloff;
4973 int endcol;
4974 int clear_width;
4975 #ifdef FEAT_RIGHTLEFT
4976 int rlflag;
4977 #endif
4979 unsigned off_from;
4980 unsigned off_to;
4981 #ifdef FEAT_MBYTE
4982 unsigned max_off_from;
4983 unsigned max_off_to;
4984 #endif
4985 int col = 0;
4986 #if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_VERTSPLIT)
4987 int hl;
4988 #endif
4989 int force = FALSE; /* force update rest of the line */
4990 int redraw_this /* bool: does character need redraw? */
4991 #ifdef FEAT_GUI
4992 = TRUE /* For GUI when while-loop empty */
4993 #endif
4995 int redraw_next; /* redraw_this for next character */
4996 #ifdef FEAT_MBYTE
4997 int clear_next = FALSE;
4998 int char_cells; /* 1: normal char */
4999 /* 2: occupies two display cells */
5000 # define CHAR_CELLS char_cells
5001 #else
5002 # define CHAR_CELLS 1
5003 #endif
5005 # ifdef FEAT_CLIPBOARD
5006 clip_may_clear_selection(row, row);
5007 # endif
5009 off_from = (unsigned)(current_ScreenLine - ScreenLines);
5010 off_to = LineOffset[row] + coloff;
5011 #ifdef FEAT_MBYTE
5012 max_off_from = off_from + screen_Columns;
5013 max_off_to = LineOffset[row] + screen_Columns;
5014 #endif
5016 #ifdef FEAT_RIGHTLEFT
5017 if (rlflag)
5019 /* Clear rest first, because it's left of the text. */
5020 if (clear_width > 0)
5022 while (col <= endcol && ScreenLines[off_to] == ' '
5023 && ScreenAttrs[off_to] == 0
5024 # ifdef FEAT_MBYTE
5025 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5026 # endif
5029 ++off_to;
5030 ++col;
5032 if (col <= endcol)
5033 screen_fill(row, row + 1, col + coloff,
5034 endcol + coloff + 1, ' ', ' ', 0);
5036 col = endcol + 1;
5037 off_to = LineOffset[row] + col + coloff;
5038 off_from += col;
5039 endcol = (clear_width > 0 ? clear_width : -clear_width);
5041 #endif /* FEAT_RIGHTLEFT */
5043 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
5045 while (col < endcol)
5047 #ifdef FEAT_MBYTE
5048 if (has_mbyte && (col + 1 < endcol))
5049 char_cells = (*mb_off2cells)(off_from, max_off_from);
5050 else
5051 char_cells = 1;
5052 #endif
5054 redraw_this = redraw_next;
5055 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
5056 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
5058 #ifdef FEAT_GUI
5059 /* If the next character was bold, then redraw the current character to
5060 * remove any pixels that might have spilt over into us. This only
5061 * happens in the GUI.
5063 if (redraw_next && gui.in_use)
5065 hl = ScreenAttrs[off_to + CHAR_CELLS];
5066 if (hl > HL_ALL)
5067 hl = syn_attr2attr(hl);
5068 if (hl & HL_BOLD)
5069 redraw_this = TRUE;
5071 #endif
5073 if (redraw_this)
5076 * Special handling when 'xs' termcap flag set (hpterm):
5077 * Attributes for characters are stored at the position where the
5078 * cursor is when writing the highlighting code. The
5079 * start-highlighting code must be written with the cursor on the
5080 * first highlighted character. The stop-highlighting code must
5081 * be written with the cursor just after the last highlighted
5082 * character.
5083 * Overwriting a character doesn't remove it's highlighting. Need
5084 * to clear the rest of the line, and force redrawing it
5085 * completely.
5087 if ( p_wiv
5088 && !force
5089 #ifdef FEAT_GUI
5090 && !gui.in_use
5091 #endif
5092 && ScreenAttrs[off_to] != 0
5093 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
5096 * Need to remove highlighting attributes here.
5098 windgoto(row, col + coloff);
5099 out_str(T_CE); /* clear rest of this screen line */
5100 screen_start(); /* don't know where cursor is now */
5101 force = TRUE; /* force redraw of rest of the line */
5102 redraw_next = TRUE; /* or else next char would miss out */
5105 * If the previous character was highlighted, need to stop
5106 * highlighting at this character.
5108 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
5110 screen_attr = ScreenAttrs[off_to - 1];
5111 term_windgoto(row, col + coloff);
5112 screen_stop_highlight();
5114 else
5115 screen_attr = 0; /* highlighting has stopped */
5117 #ifdef FEAT_MBYTE
5118 if (enc_dbcs != 0)
5120 /* Check if overwriting a double-byte with a single-byte or
5121 * the other way around requires another character to be
5122 * redrawn. For UTF-8 this isn't needed, because comparing
5123 * ScreenLinesUC[] is sufficient. */
5124 if (char_cells == 1
5125 && col + 1 < endcol
5126 && (*mb_off2cells)(off_to, max_off_to) > 1)
5128 /* Writing a single-cell character over a double-cell
5129 * character: need to redraw the next cell. */
5130 ScreenLines[off_to + 1] = 0;
5131 redraw_next = TRUE;
5133 else if (char_cells == 2
5134 && col + 2 < endcol
5135 && (*mb_off2cells)(off_to, max_off_to) == 1
5136 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
5138 /* Writing the second half of a double-cell character over
5139 * a double-cell character: need to redraw the second
5140 * cell. */
5141 ScreenLines[off_to + 2] = 0;
5142 redraw_next = TRUE;
5145 if (enc_dbcs == DBCS_JPNU)
5146 ScreenLines2[off_to] = ScreenLines2[off_from];
5148 /* When writing a single-width character over a double-width
5149 * character and at the end of the redrawn text, need to clear out
5150 * the right halve of the old character.
5151 * Also required when writing the right halve of a double-width
5152 * char over the left halve of an existing one. */
5153 if (has_mbyte && col + char_cells == endcol
5154 && ((char_cells == 1
5155 && (*mb_off2cells)(off_to, max_off_to) > 1)
5156 || (char_cells == 2
5157 && (*mb_off2cells)(off_to, max_off_to) == 1
5158 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
5159 clear_next = TRUE;
5160 #endif
5162 ScreenLines[off_to] = ScreenLines[off_from];
5163 #ifdef FEAT_MBYTE
5164 if (enc_utf8)
5166 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
5167 if (ScreenLinesUC[off_from] != 0)
5169 int i;
5171 for (i = 0; i < Screen_mco; ++i)
5172 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
5175 if (char_cells == 2)
5176 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
5177 #endif
5179 #if defined(FEAT_GUI) || defined(UNIX)
5180 /* The bold trick makes a single column of pixels appear in the
5181 * next character. When a bold character is removed, the next
5182 * character should be redrawn too. This happens for our own GUI
5183 * and for some xterms. */
5184 if (
5185 # ifdef FEAT_GUI
5186 gui.in_use
5187 # endif
5188 # if defined(FEAT_GUI) && defined(UNIX)
5190 # endif
5191 # ifdef UNIX
5192 term_is_xterm
5193 # endif
5196 hl = ScreenAttrs[off_to];
5197 if (hl > HL_ALL)
5198 hl = syn_attr2attr(hl);
5199 if (hl & HL_BOLD)
5200 redraw_next = TRUE;
5202 #endif
5203 ScreenAttrs[off_to] = ScreenAttrs[off_from];
5204 #ifdef FEAT_MBYTE
5205 /* For simplicity set the attributes of second half of a
5206 * double-wide character equal to the first half. */
5207 if (char_cells == 2)
5208 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
5210 if (enc_dbcs != 0 && char_cells == 2)
5211 screen_char_2(off_to, row, col + coloff);
5212 else
5213 #endif
5214 screen_char(off_to, row, col + coloff);
5216 else if ( p_wiv
5217 #ifdef FEAT_GUI
5218 && !gui.in_use
5219 #endif
5220 && col + coloff > 0)
5222 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
5225 * Don't output stop-highlight when moving the cursor, it will
5226 * stop the highlighting when it should continue.
5228 screen_attr = 0;
5230 else if (screen_attr != 0)
5231 screen_stop_highlight();
5234 off_to += CHAR_CELLS;
5235 off_from += CHAR_CELLS;
5236 col += CHAR_CELLS;
5239 #ifdef FEAT_MBYTE
5240 if (clear_next)
5242 /* Clear the second half of a double-wide character of which the left
5243 * half was overwritten with a single-wide character. */
5244 ScreenLines[off_to] = ' ';
5245 if (enc_utf8)
5246 ScreenLinesUC[off_to] = 0;
5247 screen_char(off_to, row, col + coloff);
5249 #endif
5251 if (clear_width > 0
5252 #ifdef FEAT_RIGHTLEFT
5253 && !rlflag
5254 #endif
5257 #ifdef FEAT_GUI
5258 int startCol = col;
5259 #endif
5261 /* blank out the rest of the line */
5262 while (col < clear_width && ScreenLines[off_to] == ' '
5263 && ScreenAttrs[off_to] == 0
5264 #ifdef FEAT_MBYTE
5265 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5266 #endif
5269 ++off_to;
5270 ++col;
5272 if (col < clear_width)
5274 #ifdef FEAT_GUI
5276 * In the GUI, clearing the rest of the line may leave pixels
5277 * behind if the first character cleared was bold. Some bold
5278 * fonts spill over the left. In this case we redraw the previous
5279 * character too. If we didn't skip any blanks above, then we
5280 * only redraw if the character wasn't already redrawn anyway.
5282 if (gui.in_use && (col > startCol || !redraw_this))
5284 hl = ScreenAttrs[off_to];
5285 if (hl > HL_ALL || (hl & HL_BOLD))
5287 int prev_cells = 1;
5288 # ifdef FEAT_MBYTE
5289 if (enc_utf8)
5290 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
5291 * that its width is 2. */
5292 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
5293 else if (enc_dbcs != 0)
5295 /* find previous character by counting from first
5296 * column and get its width. */
5297 unsigned off = LineOffset[row];
5298 unsigned max_off = LineOffset[row] + screen_Columns;
5300 while (off < off_to)
5302 prev_cells = (*mb_off2cells)(off, max_off);
5303 off += prev_cells;
5307 if (enc_dbcs != 0 && prev_cells > 1)
5308 screen_char_2(off_to - prev_cells, row,
5309 col + coloff - prev_cells);
5310 else
5311 # endif
5312 screen_char(off_to - prev_cells, row,
5313 col + coloff - prev_cells);
5316 #endif
5317 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
5318 ' ', ' ', 0);
5319 #ifdef FEAT_VERTSPLIT
5320 off_to += clear_width - col;
5321 col = clear_width;
5322 #endif
5326 if (clear_width > 0)
5328 #ifdef FEAT_VERTSPLIT
5329 /* For a window that's left of another, draw the separator char. */
5330 if (col + coloff < Columns)
5332 int c;
5334 c = fillchar_vsep(&hl);
5335 if (ScreenLines[off_to] != c
5336 # ifdef FEAT_MBYTE
5337 || (enc_utf8 && (int)ScreenLinesUC[off_to]
5338 != (c >= 0x80 ? c : 0))
5339 # endif
5340 || ScreenAttrs[off_to] != hl)
5342 ScreenLines[off_to] = c;
5343 ScreenAttrs[off_to] = hl;
5344 # ifdef FEAT_MBYTE
5345 if (enc_utf8)
5347 if (c >= 0x80)
5349 ScreenLinesUC[off_to] = c;
5350 ScreenLinesC[0][off_to] = 0;
5352 else
5353 ScreenLinesUC[off_to] = 0;
5355 # endif
5356 screen_char(off_to, row, col + coloff);
5359 else
5360 #endif
5361 LineWraps[row] = FALSE;
5365 #if defined(FEAT_RIGHTLEFT) || defined(PROTO)
5367 * Mirror text "str" for right-left displaying.
5368 * Only works for single-byte characters (e.g., numbers).
5370 void
5371 rl_mirror(str)
5372 char_u *str;
5374 char_u *p1, *p2;
5375 int t;
5377 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
5379 t = *p1;
5380 *p1 = *p2;
5381 *p2 = t;
5384 #endif
5386 #if defined(FEAT_WINDOWS) || defined(PROTO)
5388 * mark all status lines for redraw; used after first :cd
5390 void
5391 status_redraw_all()
5393 win_T *wp;
5395 for (wp = firstwin; wp; wp = wp->w_next)
5396 if (wp->w_status_height)
5398 wp->w_redr_status = TRUE;
5399 redraw_later(VALID);
5404 * mark all status lines of the current buffer for redraw
5406 void
5407 status_redraw_curbuf()
5409 win_T *wp;
5411 for (wp = firstwin; wp; wp = wp->w_next)
5412 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
5414 wp->w_redr_status = TRUE;
5415 redraw_later(VALID);
5420 * Redraw all status lines that need to be redrawn.
5422 void
5423 redraw_statuslines()
5425 win_T *wp;
5427 for (wp = firstwin; wp; wp = wp->w_next)
5428 if (wp->w_redr_status)
5429 win_redr_status(wp);
5430 if (redraw_tabline)
5431 draw_tabline();
5433 #endif
5435 #if (defined(FEAT_WILDMENU) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
5437 * Redraw all status lines at the bottom of frame "frp".
5439 void
5440 win_redraw_last_status(frp)
5441 frame_T *frp;
5443 if (frp->fr_layout == FR_LEAF)
5444 frp->fr_win->w_redr_status = TRUE;
5445 else if (frp->fr_layout == FR_ROW)
5447 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
5448 win_redraw_last_status(frp);
5450 else /* frp->fr_layout == FR_COL */
5452 frp = frp->fr_child;
5453 while (frp->fr_next != NULL)
5454 frp = frp->fr_next;
5455 win_redraw_last_status(frp);
5458 #endif
5460 #ifdef FEAT_VERTSPLIT
5462 * Draw the verticap separator right of window "wp" starting with line "row".
5464 static void
5465 draw_vsep_win(wp, row)
5466 win_T *wp;
5467 int row;
5469 int hl;
5470 int c;
5472 if (wp->w_vsep_width)
5474 /* draw the vertical separator right of this window */
5475 c = fillchar_vsep(&hl);
5476 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
5477 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
5478 c, ' ', hl);
5481 #endif
5483 #ifdef FEAT_WILDMENU
5484 static int status_match_len __ARGS((expand_T *xp, char_u *s));
5485 static int skip_status_match_char __ARGS((expand_T *xp, char_u *s));
5488 * Get the length of an item as it will be shown in the status line.
5490 static int
5491 status_match_len(xp, s)
5492 expand_T *xp;
5493 char_u *s;
5495 int len = 0;
5497 #ifdef FEAT_MENU
5498 int emenu = (xp->xp_context == EXPAND_MENUS
5499 || xp->xp_context == EXPAND_MENUNAMES);
5501 /* Check for menu separators - replace with '|'. */
5502 if (emenu && menu_is_separator(s))
5503 return 1;
5504 #endif
5506 while (*s != NUL)
5508 s += skip_status_match_char(xp, s);
5509 len += ptr2cells(s);
5510 mb_ptr_adv(s);
5513 return len;
5517 * Return the number of characters that should be skipped in a status match.
5518 * These are backslashes used for escaping. Do show backslashes in help tags.
5520 static int
5521 skip_status_match_char(xp, s)
5522 expand_T *xp;
5523 char_u *s;
5525 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
5526 #ifdef FEAT_MENU
5527 || ((xp->xp_context == EXPAND_MENUS
5528 || xp->xp_context == EXPAND_MENUNAMES)
5529 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
5530 #endif
5533 #ifndef BACKSLASH_IN_FILENAME
5534 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
5535 return 2;
5536 #endif
5537 return 1;
5539 return 0;
5543 * Show wildchar matches in the status line.
5544 * Show at least the "match" item.
5545 * We start at item 'first_match' in the list and show all matches that fit.
5547 * If inversion is possible we use it. Else '=' characters are used.
5549 void
5550 win_redr_status_matches(xp, num_matches, matches, match, showtail)
5551 expand_T *xp;
5552 int num_matches;
5553 char_u **matches; /* list of matches */
5554 int match;
5555 int showtail;
5557 #define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
5558 int row;
5559 char_u *buf;
5560 int len;
5561 int clen; /* length in screen cells */
5562 int fillchar;
5563 int attr;
5564 int i;
5565 int highlight = TRUE;
5566 char_u *selstart = NULL;
5567 int selstart_col = 0;
5568 char_u *selend = NULL;
5569 static int first_match = 0;
5570 int add_left = FALSE;
5571 char_u *s;
5572 #ifdef FEAT_MENU
5573 int emenu;
5574 #endif
5575 #if defined(FEAT_MBYTE) || defined(FEAT_MENU)
5576 int l;
5577 #endif
5579 if (matches == NULL) /* interrupted completion? */
5580 return;
5582 #ifdef FEAT_MBYTE
5583 if (has_mbyte)
5584 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
5585 else
5586 #endif
5587 buf = alloc((unsigned)Columns + 1);
5588 if (buf == NULL)
5589 return;
5591 if (match == -1) /* don't show match but original text */
5593 match = 0;
5594 highlight = FALSE;
5596 /* count 1 for the ending ">" */
5597 clen = status_match_len(xp, L_MATCH(match)) + 3;
5598 if (match == 0)
5599 first_match = 0;
5600 else if (match < first_match)
5602 /* jumping left, as far as we can go */
5603 first_match = match;
5604 add_left = TRUE;
5606 else
5608 /* check if match fits on the screen */
5609 for (i = first_match; i < match; ++i)
5610 clen += status_match_len(xp, L_MATCH(i)) + 2;
5611 if (first_match > 0)
5612 clen += 2;
5613 /* jumping right, put match at the left */
5614 if ((long)clen > Columns)
5616 first_match = match;
5617 /* if showing the last match, we can add some on the left */
5618 clen = 2;
5619 for (i = match; i < num_matches; ++i)
5621 clen += status_match_len(xp, L_MATCH(i)) + 2;
5622 if ((long)clen >= Columns)
5623 break;
5625 if (i == num_matches)
5626 add_left = TRUE;
5629 if (add_left)
5630 while (first_match > 0)
5632 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
5633 if ((long)clen >= Columns)
5634 break;
5635 --first_match;
5638 fillchar = fillchar_status(&attr, TRUE);
5640 if (first_match == 0)
5642 *buf = NUL;
5643 len = 0;
5645 else
5647 STRCPY(buf, "< ");
5648 len = 2;
5650 clen = len;
5652 i = first_match;
5653 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
5655 if (i == match)
5657 selstart = buf + len;
5658 selstart_col = clen;
5661 s = L_MATCH(i);
5662 /* Check for menu separators - replace with '|' */
5663 #ifdef FEAT_MENU
5664 emenu = (xp->xp_context == EXPAND_MENUS
5665 || xp->xp_context == EXPAND_MENUNAMES);
5666 if (emenu && menu_is_separator(s))
5668 STRCPY(buf + len, transchar('|'));
5669 l = (int)STRLEN(buf + len);
5670 len += l;
5671 clen += l;
5673 else
5674 #endif
5675 for ( ; *s != NUL; ++s)
5677 s += skip_status_match_char(xp, s);
5678 clen += ptr2cells(s);
5679 #ifdef FEAT_MBYTE
5680 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
5682 STRNCPY(buf + len, s, l);
5683 s += l - 1;
5684 len += l;
5686 else
5687 #endif
5689 STRCPY(buf + len, transchar_byte(*s));
5690 len += (int)STRLEN(buf + len);
5693 if (i == match)
5694 selend = buf + len;
5696 *(buf + len++) = ' ';
5697 *(buf + len++) = ' ';
5698 clen += 2;
5699 if (++i == num_matches)
5700 break;
5703 if (i != num_matches)
5705 *(buf + len++) = '>';
5706 ++clen;
5709 buf[len] = NUL;
5711 row = cmdline_row - 1;
5712 if (row >= 0)
5714 if (wild_menu_showing == 0)
5716 if (msg_scrolled > 0)
5718 /* Put the wildmenu just above the command line. If there is
5719 * no room, scroll the screen one line up. */
5720 if (cmdline_row == Rows - 1)
5722 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
5723 ++msg_scrolled;
5725 else
5727 ++cmdline_row;
5728 ++row;
5730 wild_menu_showing = WM_SCROLLED;
5732 else
5734 /* Create status line if needed by setting 'laststatus' to 2.
5735 * Set 'winminheight' to zero to avoid that the window is
5736 * resized. */
5737 if (lastwin->w_status_height == 0)
5739 save_p_ls = p_ls;
5740 save_p_wmh = p_wmh;
5741 p_ls = 2;
5742 p_wmh = 0;
5743 last_status(FALSE);
5745 wild_menu_showing = WM_SHOWN;
5749 screen_puts(buf, row, 0, attr);
5750 if (selstart != NULL && highlight)
5752 *selend = NUL;
5753 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
5756 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
5759 #ifdef FEAT_VERTSPLIT
5760 win_redraw_last_status(topframe);
5761 #else
5762 lastwin->w_redr_status = TRUE;
5763 #endif
5764 vim_free(buf);
5766 #endif
5768 #if defined(FEAT_WINDOWS) || defined(PROTO)
5770 * Redraw the status line of window wp.
5772 * If inversion is possible we use it. Else '=' characters are used.
5774 void
5775 win_redr_status(wp)
5776 win_T *wp;
5778 int row;
5779 char_u *p;
5780 int len;
5781 int fillchar;
5782 int attr;
5783 int this_ru_col;
5784 static int busy = FALSE;
5786 /* It's possible to get here recursively when 'statusline' (indirectly)
5787 * invokes ":redrawstatus". Simply ignore the call then. */
5788 if (busy)
5789 return;
5790 busy = TRUE;
5792 wp->w_redr_status = FALSE;
5793 if (wp->w_status_height == 0)
5795 /* no status line, can only be last window */
5796 redraw_cmdline = TRUE;
5798 else if (!redrawing()
5799 #ifdef FEAT_INS_EXPAND
5800 /* don't update status line when popup menu is visible and may be
5801 * drawn over it */
5802 || pum_visible()
5803 #endif
5806 /* Don't redraw right now, do it later. */
5807 wp->w_redr_status = TRUE;
5809 #ifdef FEAT_STL_OPT
5810 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
5812 /* redraw custom status line */
5813 redraw_custom_statusline(wp);
5815 #endif
5816 else
5818 fillchar = fillchar_status(&attr, wp == curwin);
5820 get_trans_bufname(wp->w_buffer);
5821 p = NameBuff;
5822 len = (int)STRLEN(p);
5824 if (wp->w_buffer->b_help
5825 #ifdef FEAT_QUICKFIX
5826 || wp->w_p_pvw
5827 #endif
5828 || bufIsChanged(wp->w_buffer)
5829 || wp->w_buffer->b_p_ro)
5830 *(p + len++) = ' ';
5831 if (wp->w_buffer->b_help)
5833 STRCPY(p + len, _("[Help]"));
5834 len += (int)STRLEN(p + len);
5836 #ifdef FEAT_QUICKFIX
5837 if (wp->w_p_pvw)
5839 STRCPY(p + len, _("[Preview]"));
5840 len += (int)STRLEN(p + len);
5842 #endif
5843 if (bufIsChanged(wp->w_buffer))
5845 STRCPY(p + len, "[+]");
5846 len += 3;
5848 if (wp->w_buffer->b_p_ro)
5850 STRCPY(p + len, "[RO]");
5851 len += 4;
5854 #ifndef FEAT_VERTSPLIT
5855 this_ru_col = ru_col;
5856 if (this_ru_col < (Columns + 1) / 2)
5857 this_ru_col = (Columns + 1) / 2;
5858 #else
5859 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
5860 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
5861 this_ru_col = (W_WIDTH(wp) + 1) / 2;
5862 if (this_ru_col <= 1)
5864 p = (char_u *)"<"; /* No room for file name! */
5865 len = 1;
5867 else
5868 #endif
5869 #ifdef FEAT_MBYTE
5870 if (has_mbyte)
5872 int clen = 0, i;
5874 /* Count total number of display cells. */
5875 for (i = 0; p[i] != NUL; i += (*mb_ptr2len)(p + i))
5876 clen += (*mb_ptr2cells)(p + i);
5877 /* Find first character that will fit.
5878 * Going from start to end is much faster for DBCS. */
5879 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
5880 i += (*mb_ptr2len)(p + i))
5881 clen -= (*mb_ptr2cells)(p + i);
5882 len = clen;
5883 if (i > 0)
5885 p = p + i - 1;
5886 *p = '<';
5887 ++len;
5891 else
5892 #endif
5893 if (len > this_ru_col - 1)
5895 p += len - (this_ru_col - 1);
5896 *p = '<';
5897 len = this_ru_col - 1;
5900 row = W_WINROW(wp) + wp->w_height;
5901 screen_puts(p, row, W_WINCOL(wp), attr);
5902 screen_fill(row, row + 1, len + W_WINCOL(wp),
5903 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
5905 if (get_keymap_str(wp, NameBuff, MAXPATHL)
5906 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
5907 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
5908 - 1 + W_WINCOL(wp)), attr);
5910 #ifdef FEAT_CMDL_INFO
5911 win_redr_ruler(wp, TRUE);
5912 #endif
5915 #ifdef FEAT_VERTSPLIT
5917 * May need to draw the character below the vertical separator.
5919 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
5921 if (stl_connected(wp))
5922 fillchar = fillchar_status(&attr, wp == curwin);
5923 else
5924 fillchar = fillchar_vsep(&attr);
5925 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
5926 attr);
5928 #endif
5929 busy = FALSE;
5932 #ifdef FEAT_STL_OPT
5934 * Redraw the status line according to 'statusline' and take care of any
5935 * errors encountered.
5937 static void
5938 redraw_custom_statusline(wp)
5939 win_T *wp;
5941 static int entered = FALSE;
5942 int save_called_emsg = called_emsg;
5944 /* When called recursively return. This can happen when the statusline
5945 * contains an expression that triggers a redraw. */
5946 if (entered)
5947 return;
5948 entered = TRUE;
5950 called_emsg = FALSE;
5951 win_redr_custom(wp, FALSE);
5952 if (called_emsg)
5954 /* When there is an error disable the statusline, otherwise the
5955 * display is messed up with errors and a redraw triggers the problem
5956 * again and again. */
5957 set_string_option_direct((char_u *)"statusline", -1,
5958 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
5959 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
5961 called_emsg |= save_called_emsg;
5962 entered = FALSE;
5964 #endif
5966 # ifdef FEAT_VERTSPLIT
5968 * Return TRUE if the status line of window "wp" is connected to the status
5969 * line of the window right of it. If not, then it's a vertical separator.
5970 * Only call if (wp->w_vsep_width != 0).
5973 stl_connected(wp)
5974 win_T *wp;
5976 frame_T *fr;
5978 fr = wp->w_frame;
5979 while (fr->fr_parent != NULL)
5981 if (fr->fr_parent->fr_layout == FR_COL)
5983 if (fr->fr_next != NULL)
5984 break;
5986 else
5988 if (fr->fr_next != NULL)
5989 return TRUE;
5991 fr = fr->fr_parent;
5993 return FALSE;
5995 # endif
5997 #endif /* FEAT_WINDOWS */
5999 #if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
6001 * Get the value to show for the language mappings, active 'keymap'.
6004 get_keymap_str(wp, buf, len)
6005 win_T *wp;
6006 char_u *buf; /* buffer for the result */
6007 int len; /* length of buffer */
6009 char_u *p;
6011 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
6012 return FALSE;
6015 #ifdef FEAT_EVAL
6016 buf_T *old_curbuf = curbuf;
6017 win_T *old_curwin = curwin;
6018 char_u *s;
6020 curbuf = wp->w_buffer;
6021 curwin = wp;
6022 STRCPY(buf, "b:keymap_name"); /* must be writable */
6023 ++emsg_skip;
6024 s = p = eval_to_string(buf, NULL, FALSE);
6025 --emsg_skip;
6026 curbuf = old_curbuf;
6027 curwin = old_curwin;
6028 if (p == NULL || *p == NUL)
6029 #endif
6031 #ifdef FEAT_KEYMAP
6032 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
6033 p = wp->w_buffer->b_p_keymap;
6034 else
6035 #endif
6036 p = (char_u *)"lang";
6038 if ((int)(STRLEN(p) + 3) < len)
6039 sprintf((char *)buf, "<%s>", p);
6040 else
6041 buf[0] = NUL;
6042 #ifdef FEAT_EVAL
6043 vim_free(s);
6044 #endif
6046 return buf[0] != NUL;
6048 #endif
6050 #if defined(FEAT_STL_OPT) || defined(PROTO)
6052 * Redraw the status line or ruler of window "wp".
6053 * When "wp" is NULL redraw the tab pages line from 'tabline'.
6055 static void
6056 win_redr_custom(wp, draw_ruler)
6057 win_T *wp;
6058 int draw_ruler; /* TRUE or FALSE */
6060 int attr;
6061 int curattr;
6062 int row;
6063 int col = 0;
6064 int maxwidth;
6065 int width;
6066 int n;
6067 int len;
6068 int fillchar;
6069 char_u buf[MAXPATHL];
6070 char_u *stl;
6071 char_u *p;
6072 struct stl_hlrec hltab[STL_MAX_ITEM];
6073 struct stl_hlrec tabtab[STL_MAX_ITEM];
6074 int use_sandbox = FALSE;
6076 /* setup environment for the task at hand */
6077 if (wp == NULL)
6079 /* Use 'tabline'. Always at the first line of the screen. */
6080 stl = p_tal;
6081 row = 0;
6082 fillchar = ' ';
6083 attr = hl_attr(HLF_TPF);
6084 maxwidth = Columns;
6085 # ifdef FEAT_EVAL
6086 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
6087 # endif
6089 else
6091 row = W_WINROW(wp) + wp->w_height;
6092 fillchar = fillchar_status(&attr, wp == curwin);
6093 maxwidth = W_WIDTH(wp);
6095 if (draw_ruler)
6097 stl = p_ruf;
6098 /* advance past any leading group spec - implicit in ru_col */
6099 if (*stl == '%')
6101 if (*++stl == '-')
6102 stl++;
6103 if (atoi((char *)stl))
6104 while (VIM_ISDIGIT(*stl))
6105 stl++;
6106 if (*stl++ != '(')
6107 stl = p_ruf;
6109 #ifdef FEAT_VERTSPLIT
6110 col = ru_col - (Columns - W_WIDTH(wp));
6111 if (col < (W_WIDTH(wp) + 1) / 2)
6112 col = (W_WIDTH(wp) + 1) / 2;
6113 #else
6114 col = ru_col;
6115 if (col > (Columns + 1) / 2)
6116 col = (Columns + 1) / 2;
6117 #endif
6118 maxwidth = W_WIDTH(wp) - col;
6119 #ifdef FEAT_WINDOWS
6120 if (!wp->w_status_height)
6121 #endif
6123 row = Rows - 1;
6124 --maxwidth; /* writing in last column may cause scrolling */
6125 fillchar = ' ';
6126 attr = 0;
6129 # ifdef FEAT_EVAL
6130 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
6131 # endif
6133 else
6135 if (*wp->w_p_stl != NUL)
6136 stl = wp->w_p_stl;
6137 else
6138 stl = p_stl;
6139 # ifdef FEAT_EVAL
6140 use_sandbox = was_set_insecurely((char_u *)"statusline",
6141 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
6142 # endif
6145 #ifdef FEAT_VERTSPLIT
6146 col += W_WINCOL(wp);
6147 #endif
6150 if (maxwidth <= 0)
6151 return;
6153 /* Make a copy, because the statusline may include a function call that
6154 * might change the option value and free the memory. */
6155 stl = vim_strsave(stl);
6156 width = build_stl_str_hl(wp == NULL ? curwin : wp,
6157 buf, sizeof(buf),
6158 stl, use_sandbox,
6159 fillchar, maxwidth, hltab, tabtab);
6160 vim_free(stl);
6161 len = (int)STRLEN(buf);
6163 while (width < maxwidth && len < (int)sizeof(buf) - 1)
6165 #ifdef FEAT_MBYTE
6166 len += (*mb_char2bytes)(fillchar, buf + len);
6167 #else
6168 buf[len++] = fillchar;
6169 #endif
6170 ++width;
6172 buf[len] = NUL;
6175 * Draw each snippet with the specified highlighting.
6177 curattr = attr;
6178 p = buf;
6179 for (n = 0; hltab[n].start != NULL; n++)
6181 len = (int)(hltab[n].start - p);
6182 screen_puts_len(p, len, row, col, curattr);
6183 col += vim_strnsize(p, len);
6184 p = hltab[n].start;
6186 if (hltab[n].userhl == 0)
6187 curattr = attr;
6188 else if (hltab[n].userhl < 0)
6189 curattr = syn_id2attr(-hltab[n].userhl);
6190 #ifdef FEAT_WINDOWS
6191 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
6192 curattr = highlight_stlnc[hltab[n].userhl - 1];
6193 #endif
6194 else
6195 curattr = highlight_user[hltab[n].userhl - 1];
6197 screen_puts(p, row, col, curattr);
6199 if (wp == NULL)
6201 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
6202 col = 0;
6203 len = 0;
6204 p = buf;
6205 fillchar = 0;
6206 for (n = 0; tabtab[n].start != NULL; n++)
6208 len += vim_strnsize(p, (int)(tabtab[n].start - p));
6209 while (col < len)
6210 TabPageIdxs[col++] = fillchar;
6211 p = tabtab[n].start;
6212 fillchar = tabtab[n].userhl;
6214 while (col < Columns)
6215 TabPageIdxs[col++] = fillchar;
6219 #endif /* FEAT_STL_OPT */
6222 * Output a single character directly to the screen and update ScreenLines.
6224 void
6225 screen_putchar(c, row, col, attr)
6226 int c;
6227 int row, col;
6228 int attr;
6230 #ifdef FEAT_MBYTE
6231 char_u buf[MB_MAXBYTES + 1];
6233 buf[(*mb_char2bytes)(c, buf)] = NUL;
6234 #else
6235 char_u buf[2];
6237 buf[0] = c;
6238 buf[1] = NUL;
6239 #endif
6240 screen_puts(buf, row, col, attr);
6244 * Get a single character directly from ScreenLines into "bytes[]".
6245 * Also return its attribute in *attrp;
6247 void
6248 screen_getbytes(row, col, bytes, attrp)
6249 int row, col;
6250 char_u *bytes;
6251 int *attrp;
6253 unsigned off;
6255 /* safety check */
6256 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
6258 off = LineOffset[row] + col;
6259 *attrp = ScreenAttrs[off];
6260 bytes[0] = ScreenLines[off];
6261 bytes[1] = NUL;
6263 #ifdef FEAT_MBYTE
6264 if (enc_utf8 && ScreenLinesUC[off] != 0)
6265 bytes[utfc_char2bytes(off, bytes)] = NUL;
6266 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6268 bytes[0] = ScreenLines[off];
6269 bytes[1] = ScreenLines2[off];
6270 bytes[2] = NUL;
6272 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
6274 bytes[1] = ScreenLines[off + 1];
6275 bytes[2] = NUL;
6277 #endif
6281 #ifdef FEAT_MBYTE
6282 static int screen_comp_differs __ARGS((int, int*));
6285 * Return TRUE if composing characters for screen posn "off" differs from
6286 * composing characters in "u8cc".
6288 static int
6289 screen_comp_differs(off, u8cc)
6290 int off;
6291 int *u8cc;
6293 int i;
6295 for (i = 0; i < Screen_mco; ++i)
6297 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
6298 return TRUE;
6299 if (u8cc[i] == 0)
6300 break;
6302 return FALSE;
6304 #endif
6307 * Put string '*text' on the screen at position 'row' and 'col', with
6308 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
6309 * Note: only outputs within one row, message is truncated at screen boundary!
6310 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
6312 void
6313 screen_puts(text, row, col, attr)
6314 char_u *text;
6315 int row;
6316 int col;
6317 int attr;
6319 screen_puts_len(text, -1, row, col, attr);
6323 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
6324 * a NUL.
6326 void
6327 screen_puts_len(text, len, row, col, attr)
6328 char_u *text;
6329 int len;
6330 int row;
6331 int col;
6332 int attr;
6334 unsigned off;
6335 char_u *ptr = text;
6336 int c;
6337 #ifdef FEAT_MBYTE
6338 unsigned max_off;
6339 int mbyte_blen = 1;
6340 int mbyte_cells = 1;
6341 int u8c = 0;
6342 int u8cc[MAX_MCO];
6343 int clear_next_cell = FALSE;
6344 # ifdef FEAT_ARABIC
6345 int prev_c = 0; /* previous Arabic character */
6346 int pc, nc, nc1;
6347 int pcc[MAX_MCO];
6348 # endif
6349 #endif
6350 #if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6351 int force_redraw_this;
6352 int force_redraw_next = FALSE;
6353 #endif
6354 int need_redraw;
6356 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
6357 return;
6358 off = LineOffset[row] + col;
6360 #ifdef FEAT_MBYTE
6361 /* When drawing over the right halve of a double-wide char clear out the
6362 * left halve. Only needed in a terminal. */
6363 if (has_mbyte && col > 0 && col < screen_Columns
6364 # ifdef FEAT_GUI
6365 && !gui.in_use
6366 # endif
6367 && mb_fix_col(col, row) != col)
6369 ScreenLines[off - 1] = ' ';
6370 ScreenAttrs[off - 1] = 0;
6371 if (enc_utf8)
6373 ScreenLinesUC[off - 1] = 0;
6374 ScreenLinesC[0][off - 1] = 0;
6376 /* redraw the previous cell, make it empty */
6377 screen_char(off - 1, row, col - 1);
6378 /* force the cell at "col" to be redrawn */
6379 force_redraw_next = TRUE;
6381 #endif
6383 #ifdef FEAT_MBYTE
6384 max_off = LineOffset[row] + screen_Columns;
6385 #endif
6386 while (col < screen_Columns
6387 && (len < 0 || (int)(ptr - text) < len)
6388 && *ptr != NUL)
6390 c = *ptr;
6391 #ifdef FEAT_MBYTE
6392 /* check if this is the first byte of a multibyte */
6393 if (has_mbyte)
6395 if (enc_utf8 && len > 0)
6396 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
6397 else
6398 mbyte_blen = (*mb_ptr2len)(ptr);
6399 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6400 mbyte_cells = 1;
6401 else if (enc_dbcs != 0)
6402 mbyte_cells = mbyte_blen;
6403 else /* enc_utf8 */
6405 if (len >= 0)
6406 u8c = utfc_ptr2char_len(ptr, u8cc,
6407 (int)((text + len) - ptr));
6408 else
6409 u8c = utfc_ptr2char(ptr, u8cc);
6410 mbyte_cells = utf_char2cells(u8c);
6411 # ifdef UNICODE16
6412 /* Non-BMP character: display as ? or fullwidth ?. */
6413 if (u8c >= 0x10000)
6415 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
6416 if (attr == 0)
6417 attr = hl_attr(HLF_8);
6419 # endif
6420 # ifdef FEAT_ARABIC
6421 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
6423 /* Do Arabic shaping. */
6424 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
6426 /* Past end of string to be displayed. */
6427 nc = NUL;
6428 nc1 = NUL;
6430 else
6432 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
6433 (int)((text + len) - ptr - mbyte_blen));
6434 nc1 = pcc[0];
6436 pc = prev_c;
6437 prev_c = u8c;
6438 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
6440 else
6441 prev_c = u8c;
6442 # endif
6443 if (col + mbyte_cells > screen_Columns)
6445 /* Only 1 cell left, but character requires 2 cells:
6446 * display a '>' in the last column to avoid wrapping. */
6447 c = '>';
6448 mbyte_cells = 1;
6452 #endif
6454 #if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6455 force_redraw_this = force_redraw_next;
6456 force_redraw_next = FALSE;
6457 #endif
6459 need_redraw = ScreenLines[off] != c
6460 #ifdef FEAT_MBYTE
6461 || (mbyte_cells == 2
6462 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
6463 || (enc_dbcs == DBCS_JPNU
6464 && c == 0x8e
6465 && ScreenLines2[off] != ptr[1])
6466 || (enc_utf8
6467 && (ScreenLinesUC[off] != (u8char_T)(c >= 0x80 ? u8c : 0)
6468 || screen_comp_differs(off, u8cc)))
6469 #endif
6470 || ScreenAttrs[off] != attr
6471 || exmode_active;
6473 if (need_redraw
6474 #if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6475 || force_redraw_this
6476 #endif
6479 #if defined(FEAT_GUI) || defined(UNIX)
6480 /* The bold trick makes a single row of pixels appear in the next
6481 * character. When a bold character is removed, the next
6482 * character should be redrawn too. This happens for our own GUI
6483 * and for some xterms. */
6484 if (need_redraw && ScreenLines[off] != ' ' && (
6485 # ifdef FEAT_GUI
6486 gui.in_use
6487 # endif
6488 # if defined(FEAT_GUI) && defined(UNIX)
6490 # endif
6491 # ifdef UNIX
6492 term_is_xterm
6493 # endif
6496 int n = ScreenAttrs[off];
6498 if (n > HL_ALL)
6499 n = syn_attr2attr(n);
6500 if (n & HL_BOLD)
6501 force_redraw_next = TRUE;
6503 #endif
6504 #ifdef FEAT_MBYTE
6505 /* When at the end of the text and overwriting a two-cell
6506 * character with a one-cell character, need to clear the next
6507 * cell. Also when overwriting the left halve of a two-cell char
6508 * with the right halve of a two-cell char. Do this only once
6509 * (mb_off2cells() may return 2 on the right halve). */
6510 if (clear_next_cell)
6511 clear_next_cell = FALSE;
6512 else if (has_mbyte
6513 && (len < 0 ? ptr[mbyte_blen] == NUL
6514 : ptr + mbyte_blen >= text + len)
6515 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
6516 || (mbyte_cells == 2
6517 && (*mb_off2cells)(off, max_off) == 1
6518 && (*mb_off2cells)(off + 1, max_off) > 1)))
6519 clear_next_cell = TRUE;
6521 /* Make sure we never leave a second byte of a double-byte behind,
6522 * it confuses mb_off2cells(). */
6523 if (enc_dbcs
6524 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
6525 || (mbyte_cells == 2
6526 && (*mb_off2cells)(off, max_off) == 1
6527 && (*mb_off2cells)(off + 1, max_off) > 1)))
6528 ScreenLines[off + mbyte_blen] = 0;
6529 #endif
6530 ScreenLines[off] = c;
6531 ScreenAttrs[off] = attr;
6532 #ifdef FEAT_MBYTE
6533 if (enc_utf8)
6535 if (c < 0x80 && u8cc[0] == 0)
6536 ScreenLinesUC[off] = 0;
6537 else
6539 int i;
6541 ScreenLinesUC[off] = u8c;
6542 for (i = 0; i < Screen_mco; ++i)
6544 ScreenLinesC[i][off] = u8cc[i];
6545 if (u8cc[i] == 0)
6546 break;
6549 if (mbyte_cells == 2)
6551 ScreenLines[off + 1] = 0;
6552 ScreenAttrs[off + 1] = attr;
6554 screen_char(off, row, col);
6556 else if (mbyte_cells == 2)
6558 ScreenLines[off + 1] = ptr[1];
6559 ScreenAttrs[off + 1] = attr;
6560 screen_char_2(off, row, col);
6562 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6564 ScreenLines2[off] = ptr[1];
6565 screen_char(off, row, col);
6567 else
6568 #endif
6569 screen_char(off, row, col);
6571 #ifdef FEAT_MBYTE
6572 if (has_mbyte)
6574 off += mbyte_cells;
6575 col += mbyte_cells;
6576 ptr += mbyte_blen;
6577 if (clear_next_cell)
6578 ptr = (char_u *)" ";
6580 else
6581 #endif
6583 ++off;
6584 ++col;
6585 ++ptr;
6589 #if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6590 /* If we detected the next character needs to be redrawn, but the text
6591 * doesn't extend up to there, update the character here. */
6592 if (force_redraw_next && col < screen_Columns)
6594 # ifdef FEAT_MBYTE
6595 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
6596 screen_char_2(off, row, col);
6597 else
6598 # endif
6599 screen_char(off, row, col);
6601 #endif
6604 #ifdef FEAT_SEARCH_EXTRA
6606 * Prepare for 'hlsearch' highlighting.
6608 static void
6609 start_search_hl()
6611 if (p_hls && !no_hlsearch)
6613 last_pat_prog(&search_hl.rm);
6614 search_hl.attr = hl_attr(HLF_L);
6615 # ifdef FEAT_RELTIME
6616 /* Set the time limit to 'redrawtime'. */
6617 profile_setlimit(p_rdt, &search_hl.tm);
6618 # endif
6623 * Clean up for 'hlsearch' highlighting.
6625 static void
6626 end_search_hl()
6628 if (search_hl.rm.regprog != NULL)
6630 vim_free(search_hl.rm.regprog);
6631 search_hl.rm.regprog = NULL;
6636 * Advance to the match in window "wp" line "lnum" or past it.
6638 static void
6639 prepare_search_hl(wp, lnum)
6640 win_T *wp;
6641 linenr_T lnum;
6643 matchitem_T *cur; /* points to the match list */
6644 match_T *shl; /* points to search_hl or a match */
6645 int shl_flag; /* flag to indicate whether search_hl
6646 has been processed or not */
6647 int n;
6650 * When using a multi-line pattern, start searching at the top
6651 * of the window or just after a closed fold.
6652 * Do this both for search_hl and the match list.
6654 cur = wp->w_match_head;
6655 shl_flag = FALSE;
6656 while (cur != NULL || shl_flag == FALSE)
6658 if (shl_flag == FALSE)
6660 shl = &search_hl;
6661 shl_flag = TRUE;
6663 else
6664 shl = &cur->hl;
6665 if (shl->rm.regprog != NULL
6666 && shl->lnum == 0
6667 && re_multiline(shl->rm.regprog))
6669 if (shl->first_lnum == 0)
6671 # ifdef FEAT_FOLDING
6672 for (shl->first_lnum = lnum;
6673 shl->first_lnum > wp->w_topline; --shl->first_lnum)
6674 if (hasFoldingWin(wp, shl->first_lnum - 1,
6675 NULL, NULL, TRUE, NULL))
6676 break;
6677 # else
6678 shl->first_lnum = wp->w_topline;
6679 # endif
6681 n = 0;
6682 while (shl->first_lnum < lnum && shl->rm.regprog != NULL)
6684 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n);
6685 if (shl->lnum != 0)
6687 shl->first_lnum = shl->lnum
6688 + shl->rm.endpos[0].lnum
6689 - shl->rm.startpos[0].lnum;
6690 n = shl->rm.endpos[0].col;
6692 else
6694 ++shl->first_lnum;
6695 n = 0;
6699 if (shl != &search_hl && cur != NULL)
6700 cur = cur->next;
6705 * Search for a next 'hlsearch' or match.
6706 * Uses shl->buf.
6707 * Sets shl->lnum and shl->rm contents.
6708 * Note: Assumes a previous match is always before "lnum", unless
6709 * shl->lnum is zero.
6710 * Careful: Any pointers for buffer lines will become invalid.
6712 static void
6713 next_search_hl(win, shl, lnum, mincol)
6714 win_T *win;
6715 match_T *shl; /* points to search_hl or a match */
6716 linenr_T lnum;
6717 colnr_T mincol; /* minimal column for a match */
6719 linenr_T l;
6720 colnr_T matchcol;
6721 long nmatched;
6723 if (shl->lnum != 0)
6725 /* Check for three situations:
6726 * 1. If the "lnum" is below a previous match, start a new search.
6727 * 2. If the previous match includes "mincol", use it.
6728 * 3. Continue after the previous match.
6730 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
6731 if (lnum > l)
6732 shl->lnum = 0;
6733 else if (lnum < l || shl->rm.endpos[0].col > mincol)
6734 return;
6738 * Repeat searching for a match until one is found that includes "mincol"
6739 * or none is found in this line.
6741 called_emsg = FALSE;
6742 for (;;)
6744 #ifdef FEAT_RELTIME
6745 /* Stop searching after passing the time limit. */
6746 if (profile_passed_limit(&(shl->tm)))
6748 shl->lnum = 0; /* no match found in time */
6749 break;
6751 #endif
6752 /* Three situations:
6753 * 1. No useful previous match: search from start of line.
6754 * 2. Not Vi compatible or empty match: continue at next character.
6755 * Break the loop if this is beyond the end of the line.
6756 * 3. Vi compatible searching: continue at end of previous match.
6758 if (shl->lnum == 0)
6759 matchcol = 0;
6760 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
6761 || (shl->rm.endpos[0].lnum == 0
6762 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
6764 char_u *ml;
6766 matchcol = shl->rm.startpos[0].col;
6767 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
6768 if (*ml == NUL)
6770 ++matchcol;
6771 shl->lnum = 0;
6772 break;
6774 #ifdef FEAT_MBYTE
6775 if (has_mbyte)
6776 matchcol += mb_ptr2len(ml);
6777 else
6778 #endif
6779 ++matchcol;
6781 else
6782 matchcol = shl->rm.endpos[0].col;
6784 shl->lnum = lnum;
6785 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum, matchcol,
6786 #ifdef FEAT_RELTIME
6787 &(shl->tm)
6788 #else
6789 NULL
6790 #endif
6792 if (called_emsg)
6794 /* Error while handling regexp: stop using this regexp. */
6795 if (shl == &search_hl)
6797 /* don't free regprog in the match list, it's a copy */
6798 vim_free(shl->rm.regprog);
6799 no_hlsearch = TRUE;
6801 shl->rm.regprog = NULL;
6802 shl->lnum = 0;
6803 got_int = FALSE; /* avoid the "Type :quit to exit Vim" message */
6804 break;
6806 if (nmatched == 0)
6808 shl->lnum = 0; /* no match found */
6809 break;
6811 if (shl->rm.startpos[0].lnum > 0
6812 || shl->rm.startpos[0].col >= mincol
6813 || nmatched > 1
6814 || shl->rm.endpos[0].col > mincol)
6816 shl->lnum += shl->rm.startpos[0].lnum;
6817 break; /* useful match found */
6821 #endif
6823 static void
6824 screen_start_highlight(attr)
6825 int attr;
6827 attrentry_T *aep = NULL;
6829 screen_attr = attr;
6830 if (full_screen
6831 #ifdef WIN3264
6832 && termcap_active
6833 #endif
6836 #ifdef FEAT_GUI
6837 if (gui.in_use)
6839 char buf[20];
6841 /* The GUI handles this internally. */
6842 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
6843 OUT_STR(buf);
6845 else
6846 #endif
6848 if (attr > HL_ALL) /* special HL attr. */
6850 if (t_colors > 1)
6851 aep = syn_cterm_attr2entry(attr);
6852 else
6853 aep = syn_term_attr2entry(attr);
6854 if (aep == NULL) /* did ":syntax clear" */
6855 attr = 0;
6856 else
6857 attr = aep->ae_attr;
6859 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
6860 out_str(T_MD);
6861 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
6862 && cterm_normal_fg_bold)
6863 /* If the Normal FG color has BOLD attribute and the new HL
6864 * has a FG color defined, clear BOLD. */
6865 out_str(T_ME);
6866 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
6867 out_str(T_SO);
6868 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
6869 /* underline or undercurl */
6870 out_str(T_US);
6871 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
6872 out_str(T_CZH);
6873 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
6874 out_str(T_MR);
6877 * Output the color or start string after bold etc., in case the
6878 * bold etc. override the color setting.
6880 if (aep != NULL)
6882 if (t_colors > 1)
6884 if (aep->ae_u.cterm.fg_color)
6885 term_fg_color(aep->ae_u.cterm.fg_color - 1);
6886 if (aep->ae_u.cterm.bg_color)
6887 term_bg_color(aep->ae_u.cterm.bg_color - 1);
6889 else
6891 if (aep->ae_u.term.start != NULL)
6892 out_str(aep->ae_u.term.start);
6899 void
6900 screen_stop_highlight()
6902 int do_ME = FALSE; /* output T_ME code */
6904 if (screen_attr != 0
6905 #ifdef WIN3264
6906 && termcap_active
6907 #endif
6910 #ifdef FEAT_GUI
6911 if (gui.in_use)
6913 char buf[20];
6915 /* use internal GUI code */
6916 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
6917 OUT_STR(buf);
6919 else
6920 #endif
6922 if (screen_attr > HL_ALL) /* special HL attr. */
6924 attrentry_T *aep;
6926 if (t_colors > 1)
6929 * Assume that t_me restores the original colors!
6931 aep = syn_cterm_attr2entry(screen_attr);
6932 if (aep != NULL && (aep->ae_u.cterm.fg_color
6933 || aep->ae_u.cterm.bg_color))
6934 do_ME = TRUE;
6936 else
6938 aep = syn_term_attr2entry(screen_attr);
6939 if (aep != NULL && aep->ae_u.term.stop != NULL)
6941 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
6942 do_ME = TRUE;
6943 else
6944 out_str(aep->ae_u.term.stop);
6947 if (aep == NULL) /* did ":syntax clear" */
6948 screen_attr = 0;
6949 else
6950 screen_attr = aep->ae_attr;
6954 * Often all ending-codes are equal to T_ME. Avoid outputting the
6955 * same sequence several times.
6957 if (screen_attr & HL_STANDOUT)
6959 if (STRCMP(T_SE, T_ME) == 0)
6960 do_ME = TRUE;
6961 else
6962 out_str(T_SE);
6964 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
6966 if (STRCMP(T_UE, T_ME) == 0)
6967 do_ME = TRUE;
6968 else
6969 out_str(T_UE);
6971 if (screen_attr & HL_ITALIC)
6973 if (STRCMP(T_CZR, T_ME) == 0)
6974 do_ME = TRUE;
6975 else
6976 out_str(T_CZR);
6978 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
6979 out_str(T_ME);
6981 if (t_colors > 1)
6983 /* set Normal cterm colors */
6984 if (cterm_normal_fg_color != 0)
6985 term_fg_color(cterm_normal_fg_color - 1);
6986 if (cterm_normal_bg_color != 0)
6987 term_bg_color(cterm_normal_bg_color - 1);
6988 if (cterm_normal_fg_bold)
6989 out_str(T_MD);
6993 screen_attr = 0;
6997 * Reset the colors for a cterm. Used when leaving Vim.
6998 * The machine specific code may override this again.
7000 void
7001 reset_cterm_colors()
7003 if (t_colors > 1)
7005 /* set Normal cterm colors */
7006 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
7008 out_str(T_OP);
7009 screen_attr = -1;
7011 if (cterm_normal_fg_bold)
7013 out_str(T_ME);
7014 screen_attr = -1;
7020 * Put character ScreenLines["off"] on the screen at position "row" and "col",
7021 * using the attributes from ScreenAttrs["off"].
7023 static void
7024 screen_char(off, row, col)
7025 unsigned off;
7026 int row;
7027 int col;
7029 int attr;
7031 /* Check for illegal values, just in case (could happen just after
7032 * resizing). */
7033 if (row >= screen_Rows || col >= screen_Columns)
7034 return;
7036 /* Outputting the last character on the screen may scrollup the screen.
7037 * Don't to it! Mark the character invalid (update it when scrolled up) */
7038 if (row == screen_Rows - 1 && col == screen_Columns - 1
7039 #ifdef FEAT_RIGHTLEFT
7040 /* account for first command-line character in rightleft mode */
7041 && !cmdmsg_rl
7042 #endif
7045 ScreenAttrs[off] = (sattr_T)-1;
7046 return;
7050 * Stop highlighting first, so it's easier to move the cursor.
7052 #if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
7053 if (screen_char_attr != 0)
7054 attr = screen_char_attr;
7055 else
7056 #endif
7057 attr = ScreenAttrs[off];
7058 if (screen_attr != attr)
7059 screen_stop_highlight();
7061 windgoto(row, col);
7063 if (screen_attr != attr)
7064 screen_start_highlight(attr);
7066 #ifdef FEAT_MBYTE
7067 if (enc_utf8 && ScreenLinesUC[off] != 0)
7069 char_u buf[MB_MAXBYTES + 1];
7071 /* Convert UTF-8 character to bytes and write it. */
7073 buf[utfc_char2bytes(off, buf)] = NUL;
7075 out_str(buf);
7076 if (utf_char2cells(ScreenLinesUC[off]) > 1)
7077 ++screen_cur_col;
7079 else
7080 #endif
7082 #ifdef FEAT_MBYTE
7083 out_flush_check();
7084 #endif
7085 out_char(ScreenLines[off]);
7086 #ifdef FEAT_MBYTE
7087 /* double-byte character in single-width cell */
7088 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7089 out_char(ScreenLines2[off]);
7090 #endif
7093 screen_cur_col++;
7096 #ifdef FEAT_MBYTE
7099 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
7100 * on the screen at position 'row' and 'col'.
7101 * The attributes of the first byte is used for all. This is required to
7102 * output the two bytes of a double-byte character with nothing in between.
7104 static void
7105 screen_char_2(off, row, col)
7106 unsigned off;
7107 int row;
7108 int col;
7110 /* Check for illegal values (could be wrong when screen was resized). */
7111 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
7112 return;
7114 /* Outputting the last character on the screen may scrollup the screen.
7115 * Don't to it! Mark the character invalid (update it when scrolled up) */
7116 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
7118 ScreenAttrs[off] = (sattr_T)-1;
7119 return;
7122 /* Output the first byte normally (positions the cursor), then write the
7123 * second byte directly. */
7124 screen_char(off, row, col);
7125 out_char(ScreenLines[off + 1]);
7126 ++screen_cur_col;
7128 #endif
7130 #if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
7132 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
7133 * This uses the contents of ScreenLines[] and doesn't change it.
7135 void
7136 screen_draw_rectangle(row, col, height, width, invert)
7137 int row;
7138 int col;
7139 int height;
7140 int width;
7141 int invert;
7143 int r, c;
7144 int off;
7145 #ifdef FEAT_MBYTE
7146 int max_off;
7147 #endif
7149 /* Can't use ScreenLines unless initialized */
7150 if (ScreenLines == NULL)
7151 return;
7153 if (invert)
7154 screen_char_attr = HL_INVERSE;
7155 for (r = row; r < row + height; ++r)
7157 off = LineOffset[r];
7158 #ifdef FEAT_MBYTE
7159 max_off = off + screen_Columns;
7160 #endif
7161 for (c = col; c < col + width; ++c)
7163 #ifdef FEAT_MBYTE
7164 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
7166 screen_char_2(off + c, r, c);
7167 ++c;
7169 else
7170 #endif
7172 screen_char(off + c, r, c);
7173 #ifdef FEAT_MBYTE
7174 if (utf_off2cells(off + c, max_off) > 1)
7175 ++c;
7176 #endif
7180 screen_char_attr = 0;
7182 #endif
7184 #ifdef FEAT_VERTSPLIT
7186 * Redraw the characters for a vertically split window.
7188 static void
7189 redraw_block(row, end, wp)
7190 int row;
7191 int end;
7192 win_T *wp;
7194 int col;
7195 int width;
7197 # ifdef FEAT_CLIPBOARD
7198 clip_may_clear_selection(row, end - 1);
7199 # endif
7201 if (wp == NULL)
7203 col = 0;
7204 width = Columns;
7206 else
7208 col = wp->w_wincol;
7209 width = wp->w_width;
7211 screen_draw_rectangle(row, col, end - row, width, FALSE);
7213 #endif
7216 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
7217 * with character 'c1' in first column followed by 'c2' in the other columns.
7218 * Use attributes 'attr'.
7220 void
7221 screen_fill(start_row, end_row, start_col, end_col, c1, c2, attr)
7222 int start_row, end_row;
7223 int start_col, end_col;
7224 int c1, c2;
7225 int attr;
7227 int row;
7228 int col;
7229 int off;
7230 int end_off;
7231 int did_delete;
7232 int c;
7233 int norm_term;
7234 #if defined(FEAT_GUI) || defined(UNIX)
7235 int force_next = FALSE;
7236 #endif
7238 if (end_row > screen_Rows) /* safety check */
7239 end_row = screen_Rows;
7240 if (end_col > screen_Columns) /* safety check */
7241 end_col = screen_Columns;
7242 if (ScreenLines == NULL
7243 || start_row >= end_row
7244 || start_col >= end_col) /* nothing to do */
7245 return;
7247 /* it's a "normal" terminal when not in a GUI or cterm */
7248 norm_term = (
7249 #ifdef FEAT_GUI
7250 !gui.in_use &&
7251 #endif
7252 t_colors <= 1);
7253 for (row = start_row; row < end_row; ++row)
7255 #ifdef FEAT_MBYTE
7256 if (has_mbyte
7257 # ifdef FEAT_GUI
7258 && !gui.in_use
7259 # endif
7262 /* When drawing over the right halve of a double-wide char clear
7263 * out the left halve. When drawing over the left halve of a
7264 * double wide-char clear out the right halve. Only needed in a
7265 * terminal. */
7266 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
7267 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
7268 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
7269 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
7271 #endif
7273 * Try to use delete-line termcap code, when no attributes or in a
7274 * "normal" terminal, where a bold/italic space is just a
7275 * space.
7277 did_delete = FALSE;
7278 if (c2 == ' '
7279 && end_col == Columns
7280 && can_clear(T_CE)
7281 && (attr == 0
7282 || (norm_term
7283 && attr <= HL_ALL
7284 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
7287 * check if we really need to clear something
7289 col = start_col;
7290 if (c1 != ' ') /* don't clear first char */
7291 ++col;
7293 off = LineOffset[row] + col;
7294 end_off = LineOffset[row] + end_col;
7296 /* skip blanks (used often, keep it fast!) */
7297 #ifdef FEAT_MBYTE
7298 if (enc_utf8)
7299 while (off < end_off && ScreenLines[off] == ' '
7300 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
7301 ++off;
7302 else
7303 #endif
7304 while (off < end_off && ScreenLines[off] == ' '
7305 && ScreenAttrs[off] == 0)
7306 ++off;
7307 if (off < end_off) /* something to be cleared */
7309 col = off - LineOffset[row];
7310 screen_stop_highlight();
7311 term_windgoto(row, col);/* clear rest of this screen line */
7312 out_str(T_CE);
7313 screen_start(); /* don't know where cursor is now */
7314 col = end_col - col;
7315 while (col--) /* clear chars in ScreenLines */
7317 ScreenLines[off] = ' ';
7318 #ifdef FEAT_MBYTE
7319 if (enc_utf8)
7320 ScreenLinesUC[off] = 0;
7321 #endif
7322 ScreenAttrs[off] = 0;
7323 ++off;
7326 did_delete = TRUE; /* the chars are cleared now */
7329 off = LineOffset[row] + start_col;
7330 c = c1;
7331 for (col = start_col; col < end_col; ++col)
7333 if (ScreenLines[off] != c
7334 #ifdef FEAT_MBYTE
7335 || (enc_utf8 && (int)ScreenLinesUC[off]
7336 != (c >= 0x80 ? c : 0))
7337 #endif
7338 || ScreenAttrs[off] != attr
7339 #if defined(FEAT_GUI) || defined(UNIX)
7340 || force_next
7341 #endif
7344 #if defined(FEAT_GUI) || defined(UNIX)
7345 /* The bold trick may make a single row of pixels appear in
7346 * the next character. When a bold character is removed, the
7347 * next character should be redrawn too. This happens for our
7348 * own GUI and for some xterms. */
7349 if (
7350 # ifdef FEAT_GUI
7351 gui.in_use
7352 # endif
7353 # if defined(FEAT_GUI) && defined(UNIX)
7355 # endif
7356 # ifdef UNIX
7357 term_is_xterm
7358 # endif
7361 if (ScreenLines[off] != ' '
7362 && (ScreenAttrs[off] > HL_ALL
7363 || ScreenAttrs[off] & HL_BOLD))
7364 force_next = TRUE;
7365 else
7366 force_next = FALSE;
7368 #endif
7369 ScreenLines[off] = c;
7370 #ifdef FEAT_MBYTE
7371 if (enc_utf8)
7373 if (c >= 0x80)
7375 ScreenLinesUC[off] = c;
7376 ScreenLinesC[0][off] = 0;
7378 else
7379 ScreenLinesUC[off] = 0;
7381 #endif
7382 ScreenAttrs[off] = attr;
7383 if (!did_delete || c != ' ')
7384 screen_char(off, row, col);
7386 ++off;
7387 if (col == start_col)
7389 if (did_delete)
7390 break;
7391 c = c2;
7394 if (end_col == Columns)
7395 LineWraps[row] = FALSE;
7396 if (row == Rows - 1) /* overwritten the command line */
7398 redraw_cmdline = TRUE;
7399 if (c1 == ' ' && c2 == ' ')
7400 clear_cmdline = FALSE; /* command line has been cleared */
7401 if (start_col == 0)
7402 mode_displayed = FALSE; /* mode cleared or overwritten */
7408 * Check if there should be a delay. Used before clearing or redrawing the
7409 * screen or the command line.
7411 void
7412 check_for_delay(check_msg_scroll)
7413 int check_msg_scroll;
7415 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
7416 && !did_wait_return
7417 && emsg_silent == 0)
7419 out_flush();
7420 ui_delay(1000L, TRUE);
7421 emsg_on_display = FALSE;
7422 if (check_msg_scroll)
7423 msg_scroll = FALSE;
7428 * screen_valid - allocate screen buffers if size changed
7429 * If "clear" is TRUE: clear screen if it has been resized.
7430 * Returns TRUE if there is a valid screen to write to.
7431 * Returns FALSE when starting up and screen not initialized yet.
7434 screen_valid(clear)
7435 int clear;
7437 screenalloc(clear); /* allocate screen buffers if size changed */
7438 return (ScreenLines != NULL);
7442 * Resize the shell to Rows and Columns.
7443 * Allocate ScreenLines[] and associated items.
7445 * There may be some time between setting Rows and Columns and (re)allocating
7446 * ScreenLines[]. This happens when starting up and when (manually) changing
7447 * the shell size. Always use screen_Rows and screen_Columns to access items
7448 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
7449 * final size of the shell is needed.
7451 void
7452 screenalloc(clear)
7453 int clear;
7455 int new_row, old_row;
7456 #ifdef FEAT_GUI
7457 int old_Rows;
7458 #endif
7459 win_T *wp;
7460 int outofmem = FALSE;
7461 int len;
7462 schar_T *new_ScreenLines;
7463 #ifdef FEAT_MBYTE
7464 u8char_T *new_ScreenLinesUC = NULL;
7465 u8char_T *new_ScreenLinesC[MAX_MCO];
7466 schar_T *new_ScreenLines2 = NULL;
7467 int i;
7468 #endif
7469 sattr_T *new_ScreenAttrs;
7470 unsigned *new_LineOffset;
7471 char_u *new_LineWraps;
7472 #ifdef FEAT_WINDOWS
7473 short *new_TabPageIdxs;
7474 tabpage_T *tp;
7475 #endif
7476 static int entered = FALSE; /* avoid recursiveness */
7477 static int done_outofmem_msg = FALSE; /* did outofmem message */
7478 #ifdef FEAT_AUTOCMD
7479 int retry_count = 0;
7481 retry:
7482 #endif
7484 * Allocation of the screen buffers is done only when the size changes and
7485 * when Rows and Columns have been set and we have started doing full
7486 * screen stuff.
7488 if ((ScreenLines != NULL
7489 && Rows == screen_Rows
7490 && Columns == screen_Columns
7491 #ifdef FEAT_MBYTE
7492 && enc_utf8 == (ScreenLinesUC != NULL)
7493 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
7494 && p_mco == Screen_mco
7495 #endif
7497 || Rows == 0
7498 || Columns == 0
7499 || (!full_screen && ScreenLines == NULL))
7500 return;
7503 * It's possible that we produce an out-of-memory message below, which
7504 * will cause this function to be called again. To break the loop, just
7505 * return here.
7507 if (entered)
7508 return;
7509 entered = TRUE;
7512 * Note that the window sizes are updated before reallocating the arrays,
7513 * thus we must not redraw here!
7515 ++RedrawingDisabled;
7517 win_new_shellsize(); /* fit the windows in the new sized shell */
7519 comp_col(); /* recompute columns for shown command and ruler */
7522 * We're changing the size of the screen.
7523 * - Allocate new arrays for ScreenLines and ScreenAttrs.
7524 * - Move lines from the old arrays into the new arrays, clear extra
7525 * lines (unless the screen is going to be cleared).
7526 * - Free the old arrays.
7528 * If anything fails, make ScreenLines NULL, so we don't do anything!
7529 * Continuing with the old ScreenLines may result in a crash, because the
7530 * size is wrong.
7532 FOR_ALL_TAB_WINDOWS(tp, wp)
7533 win_free_lsize(wp);
7534 #ifdef FEAT_AUTOCMD
7535 if (aucmd_win != NULL)
7536 win_free_lsize(aucmd_win);
7537 #endif
7539 new_ScreenLines = (schar_T *)lalloc((long_u)(
7540 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7541 #ifdef FEAT_MBYTE
7542 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T) * MAX_MCO);
7543 if (enc_utf8)
7545 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
7546 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
7547 for (i = 0; i < p_mco; ++i)
7548 new_ScreenLinesC[i] = (u8char_T *)lalloc((long_u)(
7549 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
7551 if (enc_dbcs == DBCS_JPNU)
7552 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
7553 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7554 #endif
7555 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
7556 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
7557 new_LineOffset = (unsigned *)lalloc((long_u)(
7558 Rows * sizeof(unsigned)), FALSE);
7559 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
7560 #ifdef FEAT_WINDOWS
7561 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
7562 #endif
7564 FOR_ALL_TAB_WINDOWS(tp, wp)
7566 if (win_alloc_lines(wp) == FAIL)
7568 outofmem = TRUE;
7569 #ifdef FEAT_WINDOWS
7570 goto give_up;
7571 #endif
7574 #ifdef FEAT_AUTOCMD
7575 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
7576 && win_alloc_lines(aucmd_win) == FAIL)
7577 outofmem = TRUE;
7578 #endif
7579 #ifdef FEAT_WINDOWS
7580 give_up:
7581 #endif
7583 #ifdef FEAT_MBYTE
7584 for (i = 0; i < p_mco; ++i)
7585 if (new_ScreenLinesC[i] == NULL)
7586 break;
7587 #endif
7588 if (new_ScreenLines == NULL
7589 #ifdef FEAT_MBYTE
7590 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
7591 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
7592 #endif
7593 || new_ScreenAttrs == NULL
7594 || new_LineOffset == NULL
7595 || new_LineWraps == NULL
7596 #ifdef FEAT_WINDOWS
7597 || new_TabPageIdxs == NULL
7598 #endif
7599 || outofmem)
7601 if (ScreenLines != NULL || !done_outofmem_msg)
7603 /* guess the size */
7604 do_outofmem_msg((long_u)((Rows + 1) * Columns));
7606 /* Remember we did this to avoid getting outofmem messages over
7607 * and over again. */
7608 done_outofmem_msg = TRUE;
7610 vim_free(new_ScreenLines);
7611 new_ScreenLines = NULL;
7612 #ifdef FEAT_MBYTE
7613 vim_free(new_ScreenLinesUC);
7614 new_ScreenLinesUC = NULL;
7615 for (i = 0; i < p_mco; ++i)
7617 vim_free(new_ScreenLinesC[i]);
7618 new_ScreenLinesC[i] = NULL;
7620 vim_free(new_ScreenLines2);
7621 new_ScreenLines2 = NULL;
7622 #endif
7623 vim_free(new_ScreenAttrs);
7624 new_ScreenAttrs = NULL;
7625 vim_free(new_LineOffset);
7626 new_LineOffset = NULL;
7627 vim_free(new_LineWraps);
7628 new_LineWraps = NULL;
7629 #ifdef FEAT_WINDOWS
7630 vim_free(new_TabPageIdxs);
7631 new_TabPageIdxs = NULL;
7632 #endif
7634 else
7636 done_outofmem_msg = FALSE;
7638 for (new_row = 0; new_row < Rows; ++new_row)
7640 new_LineOffset[new_row] = new_row * Columns;
7641 new_LineWraps[new_row] = FALSE;
7644 * If the screen is not going to be cleared, copy as much as
7645 * possible from the old screen to the new one and clear the rest
7646 * (used when resizing the window at the "--more--" prompt or when
7647 * executing an external command, for the GUI).
7649 if (!clear)
7651 (void)vim_memset(new_ScreenLines + new_row * Columns,
7652 ' ', (size_t)Columns * sizeof(schar_T));
7653 #ifdef FEAT_MBYTE
7654 if (enc_utf8)
7656 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
7657 0, (size_t)Columns * sizeof(u8char_T));
7658 for (i = 0; i < p_mco; ++i)
7659 (void)vim_memset(new_ScreenLinesC[i]
7660 + new_row * Columns,
7661 0, (size_t)Columns * sizeof(u8char_T));
7663 if (enc_dbcs == DBCS_JPNU)
7664 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
7665 0, (size_t)Columns * sizeof(schar_T));
7666 #endif
7667 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
7668 0, (size_t)Columns * sizeof(sattr_T));
7669 old_row = new_row + (screen_Rows - Rows);
7670 if (old_row >= 0 && ScreenLines != NULL)
7672 if (screen_Columns < Columns)
7673 len = screen_Columns;
7674 else
7675 len = Columns;
7676 #ifdef FEAT_MBYTE
7677 /* When switching to utf-8 don't copy characters, they
7678 * may be invalid now. Also when p_mco changes. */
7679 if (!(enc_utf8 && ScreenLinesUC == NULL)
7680 && p_mco == Screen_mco)
7681 #endif
7682 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
7683 ScreenLines + LineOffset[old_row],
7684 (size_t)len * sizeof(schar_T));
7685 #ifdef FEAT_MBYTE
7686 if (enc_utf8 && ScreenLinesUC != NULL
7687 && p_mco == Screen_mco)
7689 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
7690 ScreenLinesUC + LineOffset[old_row],
7691 (size_t)len * sizeof(u8char_T));
7692 for (i = 0; i < p_mco; ++i)
7693 mch_memmove(new_ScreenLinesC[i]
7694 + new_LineOffset[new_row],
7695 ScreenLinesC[i] + LineOffset[old_row],
7696 (size_t)len * sizeof(u8char_T));
7698 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
7699 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
7700 ScreenLines2 + LineOffset[old_row],
7701 (size_t)len * sizeof(schar_T));
7702 #endif
7703 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
7704 ScreenAttrs + LineOffset[old_row],
7705 (size_t)len * sizeof(sattr_T));
7709 /* Use the last line of the screen for the current line. */
7710 current_ScreenLine = new_ScreenLines + Rows * Columns;
7713 free_screenlines();
7715 ScreenLines = new_ScreenLines;
7716 #ifdef FEAT_MBYTE
7717 ScreenLinesUC = new_ScreenLinesUC;
7718 for (i = 0; i < p_mco; ++i)
7719 ScreenLinesC[i] = new_ScreenLinesC[i];
7720 Screen_mco = p_mco;
7721 ScreenLines2 = new_ScreenLines2;
7722 #endif
7723 ScreenAttrs = new_ScreenAttrs;
7724 LineOffset = new_LineOffset;
7725 LineWraps = new_LineWraps;
7726 #ifdef FEAT_WINDOWS
7727 TabPageIdxs = new_TabPageIdxs;
7728 #endif
7730 /* It's important that screen_Rows and screen_Columns reflect the actual
7731 * size of ScreenLines[]. Set them before calling anything. */
7732 #ifdef FEAT_GUI
7733 old_Rows = screen_Rows;
7734 #endif
7735 screen_Rows = Rows;
7736 screen_Columns = Columns;
7738 must_redraw = CLEAR; /* need to clear the screen later */
7739 if (clear)
7740 screenclear2();
7742 #ifdef FEAT_GUI
7743 else if (gui.in_use
7744 && !gui.starting
7745 && ScreenLines != NULL
7746 && old_Rows != Rows)
7748 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
7750 * Adjust the position of the cursor, for when executing an external
7751 * command.
7753 if (msg_row >= Rows) /* Rows got smaller */
7754 msg_row = Rows - 1; /* put cursor at last row */
7755 else if (Rows > old_Rows) /* Rows got bigger */
7756 msg_row += Rows - old_Rows; /* put cursor in same place */
7757 if (msg_col >= Columns) /* Columns got smaller */
7758 msg_col = Columns - 1; /* put cursor at last column */
7760 #endif
7762 entered = FALSE;
7763 --RedrawingDisabled;
7765 #ifdef FEAT_AUTOCMD
7767 * Do not apply autocommands more than 3 times to avoid an endless loop
7768 * in case applying autocommands always changes Rows or Columns.
7770 if (starting == 0 && ++retry_count <= 3)
7772 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
7773 /* In rare cases, autocommands may have altered Rows or Columns,
7774 * jump back to check if we need to allocate the screen again. */
7775 goto retry;
7777 #endif
7780 void
7781 free_screenlines()
7783 #ifdef FEAT_MBYTE
7784 int i;
7786 vim_free(ScreenLinesUC);
7787 for (i = 0; i < Screen_mco; ++i)
7788 vim_free(ScreenLinesC[i]);
7789 vim_free(ScreenLines2);
7790 #endif
7791 vim_free(ScreenLines);
7792 vim_free(ScreenAttrs);
7793 vim_free(LineOffset);
7794 vim_free(LineWraps);
7795 #ifdef FEAT_WINDOWS
7796 vim_free(TabPageIdxs);
7797 #endif
7800 void
7801 screenclear()
7803 check_for_delay(FALSE);
7804 screenalloc(FALSE); /* allocate screen buffers if size changed */
7805 screenclear2(); /* clear the screen */
7808 static void
7809 screenclear2()
7811 int i;
7813 if (starting == NO_SCREEN || ScreenLines == NULL
7814 #ifdef FEAT_GUI
7815 || (gui.in_use && gui.starting)
7816 #endif
7818 return;
7820 #ifdef FEAT_GUI
7821 if (!gui.in_use)
7822 #endif
7823 screen_attr = -1; /* force setting the Normal colors */
7824 screen_stop_highlight(); /* don't want highlighting here */
7826 #ifdef FEAT_CLIPBOARD
7827 /* disable selection without redrawing it */
7828 clip_scroll_selection(9999);
7829 #endif
7831 /* blank out ScreenLines */
7832 for (i = 0; i < Rows; ++i)
7834 lineclear(LineOffset[i], (int)Columns);
7835 LineWraps[i] = FALSE;
7838 if (can_clear(T_CL))
7840 out_str(T_CL); /* clear the display */
7841 clear_cmdline = FALSE;
7842 mode_displayed = FALSE;
7844 else
7846 /* can't clear the screen, mark all chars with invalid attributes */
7847 for (i = 0; i < Rows; ++i)
7848 lineinvalid(LineOffset[i], (int)Columns);
7849 clear_cmdline = TRUE;
7852 screen_cleared = TRUE; /* can use contents of ScreenLines now */
7854 win_rest_invalid(firstwin);
7855 redraw_cmdline = TRUE;
7856 #ifdef FEAT_WINDOWS
7857 redraw_tabline = TRUE;
7858 #endif
7859 if (must_redraw == CLEAR) /* no need to clear again */
7860 must_redraw = NOT_VALID;
7861 compute_cmdrow();
7862 msg_row = cmdline_row; /* put cursor on last line for messages */
7863 msg_col = 0;
7864 screen_start(); /* don't know where cursor is now */
7865 msg_scrolled = 0; /* can't scroll back */
7866 msg_didany = FALSE;
7867 msg_didout = FALSE;
7871 * Clear one line in ScreenLines.
7873 static void
7874 lineclear(off, width)
7875 unsigned off;
7876 int width;
7878 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
7879 #ifdef FEAT_MBYTE
7880 if (enc_utf8)
7881 (void)vim_memset(ScreenLinesUC + off, 0,
7882 (size_t)width * sizeof(u8char_T));
7883 #endif
7884 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
7888 * Mark one line in ScreenLines invalid by setting the attributes to an
7889 * invalid value.
7891 static void
7892 lineinvalid(off, width)
7893 unsigned off;
7894 int width;
7896 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
7899 #ifdef FEAT_VERTSPLIT
7901 * Copy part of a Screenline for vertically split window "wp".
7903 static void
7904 linecopy(to, from, wp)
7905 int to;
7906 int from;
7907 win_T *wp;
7909 unsigned off_to = LineOffset[to] + wp->w_wincol;
7910 unsigned off_from = LineOffset[from] + wp->w_wincol;
7912 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
7913 wp->w_width * sizeof(schar_T));
7914 # ifdef FEAT_MBYTE
7915 if (enc_utf8)
7917 int i;
7919 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
7920 wp->w_width * sizeof(u8char_T));
7921 for (i = 0; i < p_mco; ++i)
7922 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
7923 wp->w_width * sizeof(u8char_T));
7925 if (enc_dbcs == DBCS_JPNU)
7926 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
7927 wp->w_width * sizeof(schar_T));
7928 # endif
7929 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
7930 wp->w_width * sizeof(sattr_T));
7932 #endif
7935 * Return TRUE if clearing with term string "p" would work.
7936 * It can't work when the string is empty or it won't set the right background.
7939 can_clear(p)
7940 char_u *p;
7942 return (*p != NUL && (t_colors <= 1
7943 #ifdef FEAT_GUI
7944 || gui.in_use
7945 #endif
7946 || cterm_normal_bg_color == 0 || *T_UT != NUL));
7950 * Reset cursor position. Use whenever cursor was moved because of outputting
7951 * something directly to the screen (shell commands) or a terminal control
7952 * code.
7954 void
7955 screen_start()
7957 screen_cur_row = screen_cur_col = 9999;
7961 * Move the cursor to position "row","col" in the screen.
7962 * This tries to find the most efficient way to move, minimizing the number of
7963 * characters sent to the terminal.
7965 void
7966 windgoto(row, col)
7967 int row;
7968 int col;
7970 sattr_T *p;
7971 int i;
7972 int plan;
7973 int cost;
7974 int wouldbe_col;
7975 int noinvcurs;
7976 char_u *bs;
7977 int goto_cost;
7978 int attr;
7980 #define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
7981 #define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
7983 #define PLAN_LE 1
7984 #define PLAN_CR 2
7985 #define PLAN_NL 3
7986 #define PLAN_WRITE 4
7987 /* Can't use ScreenLines unless initialized */
7988 if (ScreenLines == NULL)
7989 return;
7991 if (col != screen_cur_col || row != screen_cur_row)
7993 /* Check for valid position. */
7994 if (row < 0) /* window without text lines? */
7995 row = 0;
7996 if (row >= screen_Rows)
7997 row = screen_Rows - 1;
7998 if (col >= screen_Columns)
7999 col = screen_Columns - 1;
8001 /* check if no cursor movement is allowed in highlight mode */
8002 if (screen_attr && *T_MS == NUL)
8003 noinvcurs = HIGHL_COST;
8004 else
8005 noinvcurs = 0;
8006 goto_cost = GOTO_COST + noinvcurs;
8009 * Plan how to do the positioning:
8010 * 1. Use CR to move it to column 0, same row.
8011 * 2. Use T_LE to move it a few columns to the left.
8012 * 3. Use NL to move a few lines down, column 0.
8013 * 4. Move a few columns to the right with T_ND or by writing chars.
8015 * Don't do this if the cursor went beyond the last column, the cursor
8016 * position is unknown then (some terminals wrap, some don't )
8018 * First check if the highlighting attributes allow us to write
8019 * characters to move the cursor to the right.
8021 if (row >= screen_cur_row && screen_cur_col < Columns)
8024 * If the cursor is in the same row, bigger col, we can use CR
8025 * or T_LE.
8027 bs = NULL; /* init for GCC */
8028 attr = screen_attr;
8029 if (row == screen_cur_row && col < screen_cur_col)
8031 /* "le" is preferred over "bc", because "bc" is obsolete */
8032 if (*T_LE)
8033 bs = T_LE; /* "cursor left" */
8034 else
8035 bs = T_BC; /* "backspace character (old) */
8036 if (*bs)
8037 cost = (screen_cur_col - col) * (int)STRLEN(bs);
8038 else
8039 cost = 999;
8040 if (col + 1 < cost) /* using CR is less characters */
8042 plan = PLAN_CR;
8043 wouldbe_col = 0;
8044 cost = 1; /* CR is just one character */
8046 else
8048 plan = PLAN_LE;
8049 wouldbe_col = col;
8051 if (noinvcurs) /* will stop highlighting */
8053 cost += noinvcurs;
8054 attr = 0;
8059 * If the cursor is above where we want to be, we can use CR LF.
8061 else if (row > screen_cur_row)
8063 plan = PLAN_NL;
8064 wouldbe_col = 0;
8065 cost = (row - screen_cur_row) * 2; /* CR LF */
8066 if (noinvcurs) /* will stop highlighting */
8068 cost += noinvcurs;
8069 attr = 0;
8074 * If the cursor is in the same row, smaller col, just use write.
8076 else
8078 plan = PLAN_WRITE;
8079 wouldbe_col = screen_cur_col;
8080 cost = 0;
8084 * Check if any characters that need to be written have the
8085 * correct attributes. Also avoid UTF-8 characters.
8087 i = col - wouldbe_col;
8088 if (i > 0)
8089 cost += i;
8090 if (cost < goto_cost && i > 0)
8093 * Check if the attributes are correct without additionally
8094 * stopping highlighting.
8096 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
8097 while (i && *p++ == attr)
8098 --i;
8099 if (i != 0)
8102 * Try if it works when highlighting is stopped here.
8104 if (*--p == 0)
8106 cost += noinvcurs;
8107 while (i && *p++ == 0)
8108 --i;
8110 if (i != 0)
8111 cost = 999; /* different attributes, don't do it */
8113 #ifdef FEAT_MBYTE
8114 if (enc_utf8)
8116 /* Don't use an UTF-8 char for positioning, it's slow. */
8117 for (i = wouldbe_col; i < col; ++i)
8118 if (ScreenLinesUC[LineOffset[row] + i] != 0)
8120 cost = 999;
8121 break;
8124 #endif
8128 * We can do it without term_windgoto()!
8130 if (cost < goto_cost)
8132 if (plan == PLAN_LE)
8134 if (noinvcurs)
8135 screen_stop_highlight();
8136 while (screen_cur_col > col)
8138 out_str(bs);
8139 --screen_cur_col;
8142 else if (plan == PLAN_CR)
8144 if (noinvcurs)
8145 screen_stop_highlight();
8146 out_char('\r');
8147 screen_cur_col = 0;
8149 else if (plan == PLAN_NL)
8151 if (noinvcurs)
8152 screen_stop_highlight();
8153 while (screen_cur_row < row)
8155 out_char('\n');
8156 ++screen_cur_row;
8158 screen_cur_col = 0;
8161 i = col - screen_cur_col;
8162 if (i > 0)
8165 * Use cursor-right if it's one character only. Avoids
8166 * removing a line of pixels from the last bold char, when
8167 * using the bold trick in the GUI.
8169 if (T_ND[0] != NUL && T_ND[1] == NUL)
8171 while (i-- > 0)
8172 out_char(*T_ND);
8174 else
8176 int off;
8178 off = LineOffset[row] + screen_cur_col;
8179 while (i-- > 0)
8181 if (ScreenAttrs[off] != screen_attr)
8182 screen_stop_highlight();
8183 #ifdef FEAT_MBYTE
8184 out_flush_check();
8185 #endif
8186 out_char(ScreenLines[off]);
8187 #ifdef FEAT_MBYTE
8188 if (enc_dbcs == DBCS_JPNU
8189 && ScreenLines[off] == 0x8e)
8190 out_char(ScreenLines2[off]);
8191 #endif
8192 ++off;
8198 else
8199 cost = 999;
8201 if (cost >= goto_cost)
8203 if (noinvcurs)
8204 screen_stop_highlight();
8205 if (row == screen_cur_row && (col > screen_cur_col) &&
8206 *T_CRI != NUL)
8207 term_cursor_right(col - screen_cur_col);
8208 else
8209 term_windgoto(row, col);
8211 screen_cur_row = row;
8212 screen_cur_col = col;
8217 * Set cursor to its position in the current window.
8219 void
8220 setcursor()
8222 if (redrawing())
8224 validate_cursor();
8225 windgoto(W_WINROW(curwin) + curwin->w_wrow,
8226 W_WINCOL(curwin) + (
8227 #ifdef FEAT_RIGHTLEFT
8228 /* With 'rightleft' set and the cursor on a double-wide
8229 * character, position it on the leftmost column. */
8230 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
8231 # ifdef FEAT_MBYTE
8232 (has_mbyte
8233 && (*mb_ptr2cells)(ml_get_cursor()) == 2
8234 && vim_isprintc(gchar_cursor())) ? 2 :
8235 # endif
8236 1)) :
8237 #endif
8238 curwin->w_wcol));
8244 * insert 'line_count' lines at 'row' in window 'wp'
8245 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
8246 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
8247 * scrolling.
8248 * Returns FAIL if the lines are not inserted, OK for success.
8251 win_ins_lines(wp, row, line_count, invalid, mayclear)
8252 win_T *wp;
8253 int row;
8254 int line_count;
8255 int invalid;
8256 int mayclear;
8258 int did_delete;
8259 int nextrow;
8260 int lastrow;
8261 int retval;
8263 if (invalid)
8264 wp->w_lines_valid = 0;
8266 if (wp->w_height < 5)
8267 return FAIL;
8269 if (line_count > wp->w_height - row)
8270 line_count = wp->w_height - row;
8272 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
8273 if (retval != MAYBE)
8274 return retval;
8277 * If there is a next window or a status line, we first try to delete the
8278 * lines at the bottom to avoid messing what is after the window.
8279 * If this fails and there are following windows, don't do anything to avoid
8280 * messing up those windows, better just redraw.
8282 did_delete = FALSE;
8283 #ifdef FEAT_WINDOWS
8284 if (wp->w_next != NULL || wp->w_status_height)
8286 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8287 line_count, (int)Rows, FALSE, NULL) == OK)
8288 did_delete = TRUE;
8289 else if (wp->w_next)
8290 return FAIL;
8292 #endif
8294 * if no lines deleted, blank the lines that will end up below the window
8296 if (!did_delete)
8298 #ifdef FEAT_WINDOWS
8299 wp->w_redr_status = TRUE;
8300 #endif
8301 redraw_cmdline = TRUE;
8302 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
8303 lastrow = nextrow + line_count;
8304 if (lastrow > Rows)
8305 lastrow = Rows;
8306 screen_fill(nextrow - line_count, lastrow - line_count,
8307 W_WINCOL(wp), (int)W_ENDCOL(wp),
8308 ' ', ' ', 0);
8311 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
8312 == FAIL)
8314 /* deletion will have messed up other windows */
8315 if (did_delete)
8317 #ifdef FEAT_WINDOWS
8318 wp->w_redr_status = TRUE;
8319 #endif
8320 win_rest_invalid(W_NEXT(wp));
8322 return FAIL;
8325 return OK;
8329 * delete "line_count" window lines at "row" in window "wp"
8330 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
8331 * If "mayclear" is TRUE the screen will be cleared if it is faster than
8332 * scrolling
8333 * Return OK for success, FAIL if the lines are not deleted.
8336 win_del_lines(wp, row, line_count, invalid, mayclear)
8337 win_T *wp;
8338 int row;
8339 int line_count;
8340 int invalid;
8341 int mayclear;
8343 int retval;
8345 if (invalid)
8346 wp->w_lines_valid = 0;
8348 if (line_count > wp->w_height - row)
8349 line_count = wp->w_height - row;
8351 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
8352 if (retval != MAYBE)
8353 return retval;
8355 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
8356 (int)Rows, FALSE, NULL) == FAIL)
8357 return FAIL;
8359 #ifdef FEAT_WINDOWS
8361 * If there are windows or status lines below, try to put them at the
8362 * correct place. If we can't do that, they have to be redrawn.
8364 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
8366 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8367 line_count, (int)Rows, NULL) == FAIL)
8369 wp->w_redr_status = TRUE;
8370 win_rest_invalid(wp->w_next);
8374 * If this is the last window and there is no status line, redraw the
8375 * command line later.
8377 else
8378 #endif
8379 redraw_cmdline = TRUE;
8380 return OK;
8384 * Common code for win_ins_lines() and win_del_lines().
8385 * Returns OK or FAIL when the work has been done.
8386 * Returns MAYBE when not finished yet.
8388 static int
8389 win_do_lines(wp, row, line_count, mayclear, del)
8390 win_T *wp;
8391 int row;
8392 int line_count;
8393 int mayclear;
8394 int del;
8396 int retval;
8398 if (!redrawing() || line_count <= 0)
8399 return FAIL;
8401 /* only a few lines left: redraw is faster */
8402 if (mayclear && Rows - line_count < 5
8403 #ifdef FEAT_VERTSPLIT
8404 && wp->w_width == Columns
8405 #endif
8408 screenclear(); /* will set wp->w_lines_valid to 0 */
8409 return FAIL;
8413 * Delete all remaining lines
8415 if (row + line_count >= wp->w_height)
8417 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
8418 W_WINCOL(wp), (int)W_ENDCOL(wp),
8419 ' ', ' ', 0);
8420 return OK;
8424 * when scrolling, the message on the command line should be cleared,
8425 * otherwise it will stay there forever.
8427 clear_cmdline = TRUE;
8430 * If the terminal can set a scroll region, use that.
8431 * Always do this in a vertically split window. This will redraw from
8432 * ScreenLines[] when t_CV isn't defined. That's faster than using
8433 * win_line().
8434 * Don't use a scroll region when we are going to redraw the text, writing
8435 * a character in the lower right corner of the scroll region causes a
8436 * scroll-up in the DJGPP version.
8438 if (scroll_region
8439 #ifdef FEAT_VERTSPLIT
8440 || W_WIDTH(wp) != Columns
8441 #endif
8444 #ifdef FEAT_VERTSPLIT
8445 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8446 #endif
8447 scroll_region_set(wp, row);
8448 if (del)
8449 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
8450 wp->w_height - row, FALSE, wp);
8451 else
8452 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
8453 wp->w_height - row, wp);
8454 #ifdef FEAT_VERTSPLIT
8455 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8456 #endif
8457 scroll_region_reset();
8458 return retval;
8461 #ifdef FEAT_WINDOWS
8462 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
8463 return FAIL;
8464 #endif
8466 return MAYBE;
8470 * window 'wp' and everything after it is messed up, mark it for redraw
8472 static void
8473 win_rest_invalid(wp)
8474 win_T *wp;
8476 #ifdef FEAT_WINDOWS
8477 while (wp != NULL)
8478 #else
8479 if (wp != NULL)
8480 #endif
8482 redraw_win_later(wp, NOT_VALID);
8483 #ifdef FEAT_WINDOWS
8484 wp->w_redr_status = TRUE;
8485 wp = wp->w_next;
8486 #endif
8488 redraw_cmdline = TRUE;
8492 * The rest of the routines in this file perform screen manipulations. The
8493 * given operation is performed physically on the screen. The corresponding
8494 * change is also made to the internal screen image. In this way, the editor
8495 * anticipates the effect of editing changes on the appearance of the screen.
8496 * That way, when we call screenupdate a complete redraw isn't usually
8497 * necessary. Another advantage is that we can keep adding code to anticipate
8498 * screen changes, and in the meantime, everything still works.
8502 * types for inserting or deleting lines
8504 #define USE_T_CAL 1
8505 #define USE_T_CDL 2
8506 #define USE_T_AL 3
8507 #define USE_T_CE 4
8508 #define USE_T_DL 5
8509 #define USE_T_SR 6
8510 #define USE_NL 7
8511 #define USE_T_CD 8
8512 #define USE_REDRAW 9
8515 * insert lines on the screen and update ScreenLines[]
8516 * 'end' is the line after the scrolled part. Normally it is Rows.
8517 * When scrolling region used 'off' is the offset from the top for the region.
8518 * 'row' and 'end' are relative to the start of the region.
8520 * return FAIL for failure, OK for success.
8523 screen_ins_lines(off, row, line_count, end, wp)
8524 int off;
8525 int row;
8526 int line_count;
8527 int end;
8528 win_T *wp; /* NULL or window to use width from */
8530 int i;
8531 int j;
8532 unsigned temp;
8533 int cursor_row;
8534 int type;
8535 int result_empty;
8536 int can_ce = can_clear(T_CE);
8539 * FAIL if
8540 * - there is no valid screen
8541 * - the screen has to be redrawn completely
8542 * - the line count is less than one
8543 * - the line count is more than 'ttyscroll'
8545 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
8546 return FAIL;
8549 * There are seven ways to insert lines:
8550 * 0. When in a vertically split window and t_CV isn't set, redraw the
8551 * characters from ScreenLines[].
8552 * 1. Use T_CD (clear to end of display) if it exists and the result of
8553 * the insert is just empty lines
8554 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
8555 * present or line_count > 1. It looks better if we do all the inserts
8556 * at once.
8557 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
8558 * insert is just empty lines and T_CE is not present or line_count >
8559 * 1.
8560 * 4. Use T_AL (insert line) if it exists.
8561 * 5. Use T_CE (erase line) if it exists and the result of the insert is
8562 * just empty lines.
8563 * 6. Use T_DL (delete line) if it exists and the result of the insert is
8564 * just empty lines.
8565 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
8566 * the 'da' flag is not set or we have clear line capability.
8567 * 8. redraw the characters from ScreenLines[].
8569 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
8570 * the scrollbar for the window. It does have insert line, use that if it
8571 * exists.
8573 result_empty = (row + line_count >= end);
8574 #ifdef FEAT_VERTSPLIT
8575 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
8576 type = USE_REDRAW;
8577 else
8578 #endif
8579 if (can_clear(T_CD) && result_empty)
8580 type = USE_T_CD;
8581 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
8582 type = USE_T_CAL;
8583 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
8584 type = USE_T_CDL;
8585 else if (*T_AL != NUL)
8586 type = USE_T_AL;
8587 else if (can_ce && result_empty)
8588 type = USE_T_CE;
8589 else if (*T_DL != NUL && result_empty)
8590 type = USE_T_DL;
8591 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
8592 type = USE_T_SR;
8593 else
8594 return FAIL;
8597 * For clearing the lines screen_del_lines() is used. This will also take
8598 * care of t_db if necessary.
8600 if (type == USE_T_CD || type == USE_T_CDL ||
8601 type == USE_T_CE || type == USE_T_DL)
8602 return screen_del_lines(off, row, line_count, end, FALSE, wp);
8605 * If text is retained below the screen, first clear or delete as many
8606 * lines at the bottom of the window as are about to be inserted so that
8607 * the deleted lines won't later surface during a screen_del_lines.
8609 if (*T_DB)
8610 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
8612 #ifdef FEAT_CLIPBOARD
8613 /* Remove a modeless selection when inserting lines halfway the screen
8614 * or not the full width of the screen. */
8615 if (off + row > 0
8616 # ifdef FEAT_VERTSPLIT
8617 || (wp != NULL && wp->w_width != Columns)
8618 # endif
8620 clip_clear_selection();
8621 else
8622 clip_scroll_selection(-line_count);
8623 #endif
8625 #ifdef FEAT_GUI
8626 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
8627 * scrolling is actually carried out. */
8628 gui_dont_update_cursor();
8629 #endif
8631 if (*T_CCS != NUL) /* cursor relative to region */
8632 cursor_row = row;
8633 else
8634 cursor_row = row + off;
8637 * Shift LineOffset[] line_count down to reflect the inserted lines.
8638 * Clear the inserted lines in ScreenLines[].
8640 row += off;
8641 end += off;
8642 for (i = 0; i < line_count; ++i)
8644 #ifdef FEAT_VERTSPLIT
8645 if (wp != NULL && wp->w_width != Columns)
8647 /* need to copy part of a line */
8648 j = end - 1 - i;
8649 while ((j -= line_count) >= row)
8650 linecopy(j + line_count, j, wp);
8651 j += line_count;
8652 if (can_clear((char_u *)" "))
8653 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
8654 else
8655 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
8656 LineWraps[j] = FALSE;
8658 else
8659 #endif
8661 j = end - 1 - i;
8662 temp = LineOffset[j];
8663 while ((j -= line_count) >= row)
8665 LineOffset[j + line_count] = LineOffset[j];
8666 LineWraps[j + line_count] = LineWraps[j];
8668 LineOffset[j + line_count] = temp;
8669 LineWraps[j + line_count] = FALSE;
8670 if (can_clear((char_u *)" "))
8671 lineclear(temp, (int)Columns);
8672 else
8673 lineinvalid(temp, (int)Columns);
8677 screen_stop_highlight();
8678 windgoto(cursor_row, 0);
8680 #ifdef FEAT_VERTSPLIT
8681 /* redraw the characters */
8682 if (type == USE_REDRAW)
8683 redraw_block(row, end, wp);
8684 else
8685 #endif
8686 if (type == USE_T_CAL)
8688 term_append_lines(line_count);
8689 screen_start(); /* don't know where cursor is now */
8691 else
8693 for (i = 0; i < line_count; i++)
8695 if (type == USE_T_AL)
8697 if (i && cursor_row != 0)
8698 windgoto(cursor_row, 0);
8699 out_str(T_AL);
8701 else /* type == USE_T_SR */
8702 out_str(T_SR);
8703 screen_start(); /* don't know where cursor is now */
8708 * With scroll-reverse and 'da' flag set we need to clear the lines that
8709 * have been scrolled down into the region.
8711 if (type == USE_T_SR && *T_DA)
8713 for (i = 0; i < line_count; ++i)
8715 windgoto(off + i, 0);
8716 out_str(T_CE);
8717 screen_start(); /* don't know where cursor is now */
8721 #ifdef FEAT_GUI
8722 gui_can_update_cursor();
8723 if (gui.in_use)
8724 out_flush(); /* always flush after a scroll */
8725 #endif
8726 return OK;
8730 * delete lines on the screen and update ScreenLines[]
8731 * 'end' is the line after the scrolled part. Normally it is Rows.
8732 * When scrolling region used 'off' is the offset from the top for the region.
8733 * 'row' and 'end' are relative to the start of the region.
8735 * Return OK for success, FAIL if the lines are not deleted.
8738 screen_del_lines(off, row, line_count, end, force, wp)
8739 int off;
8740 int row;
8741 int line_count;
8742 int end;
8743 int force; /* even when line_count > p_ttyscroll */
8744 win_T *wp UNUSED; /* NULL or window to use width from */
8746 int j;
8747 int i;
8748 unsigned temp;
8749 int cursor_row;
8750 int cursor_end;
8751 int result_empty; /* result is empty until end of region */
8752 int can_delete; /* deleting line codes can be used */
8753 int type;
8756 * FAIL if
8757 * - there is no valid screen
8758 * - the screen has to be redrawn completely
8759 * - the line count is less than one
8760 * - the line count is more than 'ttyscroll'
8762 if (!screen_valid(TRUE) || line_count <= 0 ||
8763 (!force && line_count > p_ttyscroll))
8764 return FAIL;
8767 * Check if the rest of the current region will become empty.
8769 result_empty = row + line_count >= end;
8772 * We can delete lines only when 'db' flag not set or when 'ce' option
8773 * available.
8775 can_delete = (*T_DB == NUL || can_clear(T_CE));
8778 * There are six ways to delete lines:
8779 * 0. When in a vertically split window and t_CV isn't set, redraw the
8780 * characters from ScreenLines[].
8781 * 1. Use T_CD if it exists and the result is empty.
8782 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
8783 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
8784 * none of the other ways work.
8785 * 4. Use T_CE (erase line) if the result is empty.
8786 * 5. Use T_DL (delete line) if it exists.
8787 * 6. redraw the characters from ScreenLines[].
8789 #ifdef FEAT_VERTSPLIT
8790 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
8791 type = USE_REDRAW;
8792 else
8793 #endif
8794 if (can_clear(T_CD) && result_empty)
8795 type = USE_T_CD;
8796 #if defined(__BEOS__) && defined(BEOS_DR8)
8798 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
8799 * its internal termcap... this works okay for tests which test *T_DB !=
8800 * NUL. It has the disadvantage that the user cannot use any :set t_*
8801 * command to get T_DB (back) to empty_option, only :set term=... will do
8802 * the trick...
8803 * Anyway, this hack will hopefully go away with the next OS release.
8804 * (Olaf Seibert)
8806 else if (row == 0 && T_DB == empty_option
8807 && (line_count == 1 || *T_CDL == NUL))
8808 #else
8809 else if (row == 0 && (
8810 #ifndef AMIGA
8811 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
8812 * up, so use delete-line command */
8813 line_count == 1 ||
8814 #endif
8815 *T_CDL == NUL))
8816 #endif
8817 type = USE_NL;
8818 else if (*T_CDL != NUL && line_count > 1 && can_delete)
8819 type = USE_T_CDL;
8820 else if (can_clear(T_CE) && result_empty
8821 #ifdef FEAT_VERTSPLIT
8822 && (wp == NULL || wp->w_width == Columns)
8823 #endif
8825 type = USE_T_CE;
8826 else if (*T_DL != NUL && can_delete)
8827 type = USE_T_DL;
8828 else if (*T_CDL != NUL && can_delete)
8829 type = USE_T_CDL;
8830 else
8831 return FAIL;
8833 #ifdef FEAT_CLIPBOARD
8834 /* Remove a modeless selection when deleting lines halfway the screen or
8835 * not the full width of the screen. */
8836 if (off + row > 0
8837 # ifdef FEAT_VERTSPLIT
8838 || (wp != NULL && wp->w_width != Columns)
8839 # endif
8841 clip_clear_selection();
8842 else
8843 clip_scroll_selection(line_count);
8844 #endif
8846 #ifdef FEAT_GUI
8847 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
8848 * scrolling is actually carried out. */
8849 gui_dont_update_cursor();
8850 #endif
8852 if (*T_CCS != NUL) /* cursor relative to region */
8854 cursor_row = row;
8855 cursor_end = end;
8857 else
8859 cursor_row = row + off;
8860 cursor_end = end + off;
8864 * Now shift LineOffset[] line_count up to reflect the deleted lines.
8865 * Clear the inserted lines in ScreenLines[].
8867 row += off;
8868 end += off;
8869 for (i = 0; i < line_count; ++i)
8871 #ifdef FEAT_VERTSPLIT
8872 if (wp != NULL && wp->w_width != Columns)
8874 /* need to copy part of a line */
8875 j = row + i;
8876 while ((j += line_count) <= end - 1)
8877 linecopy(j - line_count, j, wp);
8878 j -= line_count;
8879 if (can_clear((char_u *)" "))
8880 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
8881 else
8882 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
8883 LineWraps[j] = FALSE;
8885 else
8886 #endif
8888 /* whole width, moving the line pointers is faster */
8889 j = row + i;
8890 temp = LineOffset[j];
8891 while ((j += line_count) <= end - 1)
8893 LineOffset[j - line_count] = LineOffset[j];
8894 LineWraps[j - line_count] = LineWraps[j];
8896 LineOffset[j - line_count] = temp;
8897 LineWraps[j - line_count] = FALSE;
8898 if (can_clear((char_u *)" "))
8899 lineclear(temp, (int)Columns);
8900 else
8901 lineinvalid(temp, (int)Columns);
8905 screen_stop_highlight();
8907 #ifdef FEAT_VERTSPLIT
8908 /* redraw the characters */
8909 if (type == USE_REDRAW)
8910 redraw_block(row, end, wp);
8911 else
8912 #endif
8913 if (type == USE_T_CD) /* delete the lines */
8915 windgoto(cursor_row, 0);
8916 out_str(T_CD);
8917 screen_start(); /* don't know where cursor is now */
8919 else if (type == USE_T_CDL)
8921 windgoto(cursor_row, 0);
8922 term_delete_lines(line_count);
8923 screen_start(); /* don't know where cursor is now */
8926 * Deleting lines at top of the screen or scroll region: Just scroll
8927 * the whole screen (scroll region) up by outputting newlines on the
8928 * last line.
8930 else if (type == USE_NL)
8932 windgoto(cursor_end - 1, 0);
8933 for (i = line_count; --i >= 0; )
8934 out_char('\n'); /* cursor will remain on same line */
8936 else
8938 for (i = line_count; --i >= 0; )
8940 if (type == USE_T_DL)
8942 windgoto(cursor_row, 0);
8943 out_str(T_DL); /* delete a line */
8945 else /* type == USE_T_CE */
8947 windgoto(cursor_row + i, 0);
8948 out_str(T_CE); /* erase a line */
8950 screen_start(); /* don't know where cursor is now */
8955 * If the 'db' flag is set, we need to clear the lines that have been
8956 * scrolled up at the bottom of the region.
8958 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
8960 for (i = line_count; i > 0; --i)
8962 windgoto(cursor_end - i, 0);
8963 out_str(T_CE); /* erase a line */
8964 screen_start(); /* don't know where cursor is now */
8968 #ifdef FEAT_GUI
8969 gui_can_update_cursor();
8970 if (gui.in_use)
8971 out_flush(); /* always flush after a scroll */
8972 #endif
8974 return OK;
8978 * show the current mode and ruler
8980 * If clear_cmdline is TRUE, clear the rest of the cmdline.
8981 * If clear_cmdline is FALSE there may be a message there that needs to be
8982 * cleared only if a mode is shown.
8983 * Return the length of the message (0 if no message).
8986 showmode()
8988 int need_clear;
8989 int length = 0;
8990 int do_mode;
8991 int attr;
8992 int nwr_save;
8993 #ifdef FEAT_INS_EXPAND
8994 int sub_attr;
8995 #endif
8997 do_mode = ((p_smd && msg_silent == 0)
8998 && ((State & INSERT)
8999 || restart_edit
9000 #ifdef FEAT_VISUAL
9001 || VIsual_active
9002 #endif
9004 if (do_mode || Recording)
9007 * Don't show mode right now, when not redrawing or inside a mapping.
9008 * Call char_avail() only when we are going to show something, because
9009 * it takes a bit of time.
9011 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
9013 redraw_cmdline = TRUE; /* show mode later */
9014 return 0;
9017 nwr_save = need_wait_return;
9019 /* wait a bit before overwriting an important message */
9020 check_for_delay(FALSE);
9022 /* if the cmdline is more than one line high, erase top lines */
9023 need_clear = clear_cmdline;
9024 if (clear_cmdline && cmdline_row < Rows - 1)
9025 msg_clr_cmdline(); /* will reset clear_cmdline */
9027 /* Position on the last line in the window, column 0 */
9028 msg_pos_mode();
9029 cursor_off();
9030 attr = hl_attr(HLF_CM); /* Highlight mode */
9031 if (do_mode)
9033 MSG_PUTS_ATTR("--", attr);
9034 #if defined(FEAT_XIM)
9035 # if 0 /* old version, changed by SungHyun Nam July 2008 */
9036 if (xic != NULL && im_get_status() && !p_imdisable
9037 && curbuf->b_p_iminsert == B_IMODE_IM)
9038 # else
9039 if (
9040 # if defined(HAVE_GTK2) || defined(FEAT_GUI_MACVIM)
9041 preedit_get_status()
9042 # else
9043 im_get_status()
9044 # endif
9046 # endif
9047 # if defined(HAVE_GTK2) || defined(FEAT_GUI_MACVIM)
9048 /* most of the time, it's not XIM being used */
9049 MSG_PUTS_ATTR(" IM", attr);
9050 # else
9051 MSG_PUTS_ATTR(" XIM", attr);
9052 # endif
9053 #endif
9054 #if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
9055 if (gui.in_use)
9057 if (hangul_input_state_get())
9058 MSG_PUTS_ATTR(" \307\321\261\333", attr); /* HANGUL */
9060 #endif
9061 #ifdef FEAT_INS_EXPAND
9062 if (edit_submode != NULL) /* CTRL-X in Insert mode */
9064 /* These messages can get long, avoid a wrap in a narrow
9065 * window. Prefer showing edit_submode_extra. */
9066 length = (Rows - msg_row) * Columns - 3;
9067 if (edit_submode_extra != NULL)
9068 length -= vim_strsize(edit_submode_extra);
9069 if (length > 0)
9071 if (edit_submode_pre != NULL)
9072 length -= vim_strsize(edit_submode_pre);
9073 if (length - vim_strsize(edit_submode) > 0)
9075 if (edit_submode_pre != NULL)
9076 msg_puts_attr(edit_submode_pre, attr);
9077 msg_puts_attr(edit_submode, attr);
9079 if (edit_submode_extra != NULL)
9081 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
9082 if ((int)edit_submode_highl < (int)HLF_COUNT)
9083 sub_attr = hl_attr(edit_submode_highl);
9084 else
9085 sub_attr = attr;
9086 msg_puts_attr(edit_submode_extra, sub_attr);
9089 length = 0;
9091 else
9092 #endif
9094 #ifdef FEAT_VREPLACE
9095 if (State & VREPLACE_FLAG)
9096 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
9097 else
9098 #endif
9099 if (State & REPLACE_FLAG)
9100 MSG_PUTS_ATTR(_(" REPLACE"), attr);
9101 else if (State & INSERT)
9103 #ifdef FEAT_RIGHTLEFT
9104 if (p_ri)
9105 MSG_PUTS_ATTR(_(" REVERSE"), attr);
9106 #endif
9107 MSG_PUTS_ATTR(_(" INSERT"), attr);
9109 else if (restart_edit == 'I')
9110 MSG_PUTS_ATTR(_(" (insert)"), attr);
9111 else if (restart_edit == 'R')
9112 MSG_PUTS_ATTR(_(" (replace)"), attr);
9113 else if (restart_edit == 'V')
9114 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
9115 #ifdef FEAT_RIGHTLEFT
9116 if (p_hkmap)
9117 MSG_PUTS_ATTR(_(" Hebrew"), attr);
9118 # ifdef FEAT_FKMAP
9119 if (p_fkmap)
9120 MSG_PUTS_ATTR(farsi_text_5, attr);
9121 # endif
9122 #endif
9123 #ifdef FEAT_KEYMAP
9124 if (State & LANGMAP)
9126 # ifdef FEAT_ARABIC
9127 if (curwin->w_p_arab)
9128 MSG_PUTS_ATTR(_(" Arabic"), attr);
9129 else
9130 # endif
9131 MSG_PUTS_ATTR(_(" (lang)"), attr);
9133 #endif
9134 if ((State & INSERT) && p_paste)
9135 MSG_PUTS_ATTR(_(" (paste)"), attr);
9137 #ifdef FEAT_VISUAL
9138 if (VIsual_active)
9140 char *p;
9142 /* Don't concatenate separate words to avoid translation
9143 * problems. */
9144 switch ((VIsual_select ? 4 : 0)
9145 + (VIsual_mode == Ctrl_V) * 2
9146 + (VIsual_mode == 'V'))
9148 case 0: p = N_(" VISUAL"); break;
9149 case 1: p = N_(" VISUAL LINE"); break;
9150 case 2: p = N_(" VISUAL BLOCK"); break;
9151 case 4: p = N_(" SELECT"); break;
9152 case 5: p = N_(" SELECT LINE"); break;
9153 default: p = N_(" SELECT BLOCK"); break;
9155 MSG_PUTS_ATTR(_(p), attr);
9157 #endif
9158 MSG_PUTS_ATTR(" --", attr);
9161 need_clear = TRUE;
9163 if (Recording
9164 #ifdef FEAT_INS_EXPAND
9165 && edit_submode == NULL /* otherwise it gets too long */
9166 #endif
9169 MSG_PUTS_ATTR(_("recording"), attr);
9170 need_clear = TRUE;
9173 mode_displayed = TRUE;
9174 if (need_clear || clear_cmdline)
9175 msg_clr_eos();
9176 msg_didout = FALSE; /* overwrite this message */
9177 length = msg_col;
9178 msg_col = 0;
9179 need_wait_return = nwr_save; /* never ask for hit-return for this */
9181 else if (clear_cmdline && msg_silent == 0)
9182 /* Clear the whole command line. Will reset "clear_cmdline". */
9183 msg_clr_cmdline();
9185 #ifdef FEAT_CMDL_INFO
9186 # ifdef FEAT_VISUAL
9187 /* In Visual mode the size of the selected area must be redrawn. */
9188 if (VIsual_active)
9189 clear_showcmd();
9190 # endif
9192 /* If the last window has no status line, the ruler is after the mode
9193 * message and must be redrawn */
9194 if (redrawing()
9195 # ifdef FEAT_WINDOWS
9196 && lastwin->w_status_height == 0
9197 # endif
9199 win_redr_ruler(lastwin, TRUE);
9200 #endif
9201 redraw_cmdline = FALSE;
9202 clear_cmdline = FALSE;
9204 return length;
9208 * Position for a mode message.
9210 static void
9211 msg_pos_mode()
9213 msg_col = 0;
9214 msg_row = Rows - 1;
9218 * Delete mode message. Used when ESC is typed which is expected to end
9219 * Insert mode (but Insert mode didn't end yet!).
9220 * Caller should check "mode_displayed".
9222 void
9223 unshowmode(force)
9224 int force;
9227 * Don't delete it right now, when not redrawing or inside a mapping.
9229 if (!redrawing() || (!force && char_avail() && !KeyTyped))
9230 redraw_cmdline = TRUE; /* delete mode later */
9231 else
9233 msg_pos_mode();
9234 if (Recording)
9235 MSG_PUTS_ATTR(_("recording"), hl_attr(HLF_CM));
9236 msg_clr_eos();
9240 #if defined(FEAT_WINDOWS)
9242 * Draw the tab pages line at the top of the Vim window.
9244 static void
9245 draw_tabline()
9247 int tabcount = 0;
9248 tabpage_T *tp;
9249 int tabwidth;
9250 int col = 0;
9251 int scol = 0;
9252 int attr;
9253 win_T *wp;
9254 win_T *cwp;
9255 int wincount;
9256 int modified;
9257 int c;
9258 int len;
9259 int attr_sel = hl_attr(HLF_TPS);
9260 int attr_nosel = hl_attr(HLF_TP);
9261 int attr_fill = hl_attr(HLF_TPF);
9262 char_u *p;
9263 int room;
9264 int use_sep_chars = (t_colors < 8
9265 #ifdef FEAT_GUI
9266 && !gui.in_use
9267 #endif
9270 redraw_tabline = FALSE;
9272 #ifdef FEAT_GUI_TABLINE
9273 /* Take care of a GUI tabline. */
9274 if (gui_use_tabline())
9276 gui_update_tabline();
9277 return;
9279 #endif
9281 if (tabline_height() < 1)
9282 return;
9284 #if defined(FEAT_STL_OPT)
9286 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
9287 for (scol = 0; scol < Columns; ++scol)
9288 TabPageIdxs[scol] = 0;
9290 /* Use the 'tabline' option if it's set. */
9291 if (*p_tal != NUL)
9293 int save_called_emsg = called_emsg;
9295 /* Check for an error. If there is one we would loop in redrawing the
9296 * screen. Avoid that by making 'tabline' empty. */
9297 called_emsg = FALSE;
9298 win_redr_custom(NULL, FALSE);
9299 if (called_emsg)
9300 set_string_option_direct((char_u *)"tabline", -1,
9301 (char_u *)"", OPT_FREE, SID_ERROR);
9302 called_emsg |= save_called_emsg;
9304 else
9305 #endif
9307 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
9308 ++tabcount;
9310 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
9311 if (tabwidth < 6)
9312 tabwidth = 6;
9314 attr = attr_nosel;
9315 tabcount = 0;
9316 scol = 0;
9317 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
9318 tp = tp->tp_next)
9320 scol = col;
9322 if (tp->tp_topframe == topframe)
9323 attr = attr_sel;
9324 if (use_sep_chars && col > 0)
9325 screen_putchar('|', 0, col++, attr);
9327 if (tp->tp_topframe != topframe)
9328 attr = attr_nosel;
9330 screen_putchar(' ', 0, col++, attr);
9332 if (tp == curtab)
9334 cwp = curwin;
9335 wp = firstwin;
9337 else
9339 cwp = tp->tp_curwin;
9340 wp = tp->tp_firstwin;
9343 modified = FALSE;
9344 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
9345 if (bufIsChanged(wp->w_buffer))
9346 modified = TRUE;
9347 if (modified || wincount > 1)
9349 if (wincount > 1)
9351 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
9352 len = (int)STRLEN(NameBuff);
9353 if (col + len >= Columns - 3)
9354 break;
9355 screen_puts_len(NameBuff, len, 0, col,
9356 #if defined(FEAT_SYN_HL)
9357 hl_combine_attr(attr, hl_attr(HLF_T))
9358 #else
9359 attr
9360 #endif
9362 col += len;
9364 if (modified)
9365 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
9366 screen_putchar(' ', 0, col++, attr);
9369 room = scol - col + tabwidth - 1;
9370 if (room > 0)
9372 /* Get buffer name in NameBuff[] */
9373 get_trans_bufname(cwp->w_buffer);
9374 shorten_dir(NameBuff);
9375 len = vim_strsize(NameBuff);
9376 p = NameBuff;
9377 #ifdef FEAT_MBYTE
9378 if (has_mbyte)
9379 while (len > room)
9381 len -= ptr2cells(p);
9382 mb_ptr_adv(p);
9384 else
9385 #endif
9386 if (len > room)
9388 p += len - room;
9389 len = room;
9391 if (len > Columns - col - 1)
9392 len = Columns - col - 1;
9394 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
9395 col += len;
9397 screen_putchar(' ', 0, col++, attr);
9399 /* Store the tab page number in TabPageIdxs[], so that
9400 * jump_to_mouse() knows where each one is. */
9401 ++tabcount;
9402 while (scol < col)
9403 TabPageIdxs[scol++] = tabcount;
9406 if (use_sep_chars)
9407 c = '_';
9408 else
9409 c = ' ';
9410 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
9412 /* Put an "X" for closing the current tab if there are several. */
9413 if (first_tabpage->tp_next != NULL)
9415 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
9416 TabPageIdxs[Columns - 1] = -999;
9420 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
9421 * set. */
9422 redraw_tabline = FALSE;
9426 * Get buffer name for "buf" into NameBuff[].
9427 * Takes care of special buffer names and translates special characters.
9429 void
9430 get_trans_bufname(buf)
9431 buf_T *buf;
9433 if (buf_spname(buf) != NULL)
9434 STRCPY(NameBuff, buf_spname(buf));
9435 else
9436 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
9437 trans_characters(NameBuff, MAXPATHL);
9439 #endif
9441 #if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
9443 * Get the character to use in a status line. Get its attributes in "*attr".
9445 static int
9446 fillchar_status(attr, is_curwin)
9447 int *attr;
9448 int is_curwin;
9450 int fill;
9451 if (is_curwin)
9453 *attr = hl_attr(HLF_S);
9454 fill = fill_stl;
9456 else
9458 *attr = hl_attr(HLF_SNC);
9459 fill = fill_stlnc;
9461 /* Use fill when there is highlighting, and highlighting of current
9462 * window differs, or the fillchars differ, or this is not the
9463 * current window */
9464 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
9465 || !is_curwin || firstwin == lastwin)
9466 || (fill_stl != fill_stlnc)))
9467 return fill;
9468 if (is_curwin)
9469 return '^';
9470 return '=';
9472 #endif
9474 #ifdef FEAT_VERTSPLIT
9476 * Get the character to use in a separator between vertically split windows.
9477 * Get its attributes in "*attr".
9479 static int
9480 fillchar_vsep(attr)
9481 int *attr;
9483 *attr = hl_attr(HLF_C);
9484 if (*attr == 0 && fill_vert == ' ')
9485 return '|';
9486 else
9487 return fill_vert;
9489 #endif
9492 * Return TRUE if redrawing should currently be done.
9495 redrawing()
9497 return (!RedrawingDisabled
9498 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
9502 * Return TRUE if printing messages should currently be done.
9505 messaging()
9507 return (!(p_lz && char_avail() && !KeyTyped));
9511 * Show current status info in ruler and various other places
9512 * If always is FALSE, only show ruler if position has changed.
9514 void
9515 showruler(always)
9516 int always;
9518 if (!always && !redrawing())
9519 return;
9520 #ifdef FEAT_INS_EXPAND
9521 if (pum_visible())
9523 # ifdef FEAT_WINDOWS
9524 /* Don't redraw right now, do it later. */
9525 curwin->w_redr_status = TRUE;
9526 # endif
9527 return;
9529 #endif
9530 #if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
9531 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
9533 redraw_custom_statusline(curwin);
9535 else
9536 #endif
9537 #ifdef FEAT_CMDL_INFO
9538 win_redr_ruler(curwin, always);
9539 #endif
9541 #ifdef FEAT_TITLE
9542 if (need_maketitle
9543 # ifdef FEAT_STL_OPT
9544 || (p_icon && (stl_syntax & STL_IN_ICON))
9545 || (p_title && (stl_syntax & STL_IN_TITLE))
9546 # endif
9548 maketitle();
9549 #endif
9550 #ifdef FEAT_WINDOWS
9551 /* Redraw the tab pages line if needed. */
9552 if (redraw_tabline)
9553 draw_tabline();
9554 #endif
9557 #ifdef FEAT_CMDL_INFO
9558 static void
9559 win_redr_ruler(wp, always)
9560 win_T *wp;
9561 int always;
9563 #define RULER_BUF_LEN 70
9564 char_u buffer[RULER_BUF_LEN];
9565 int row;
9566 int fillchar;
9567 int attr;
9568 int empty_line = FALSE;
9569 colnr_T virtcol;
9570 int i;
9571 size_t len;
9572 int o;
9573 #ifdef FEAT_VERTSPLIT
9574 int this_ru_col;
9575 int off = 0;
9576 int width = Columns;
9577 # define WITH_OFF(x) x
9578 # define WITH_WIDTH(x) x
9579 #else
9580 # define WITH_OFF(x) 0
9581 # define WITH_WIDTH(x) Columns
9582 # define this_ru_col ru_col
9583 #endif
9585 /* If 'ruler' off or redrawing disabled, don't do anything */
9586 if (!p_ru)
9587 return;
9590 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
9591 * after deleting lines, before cursor.lnum is corrected.
9593 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
9594 return;
9596 #ifdef FEAT_INS_EXPAND
9597 /* Don't draw the ruler while doing insert-completion, it might overwrite
9598 * the (long) mode message. */
9599 # ifdef FEAT_WINDOWS
9600 if (wp == lastwin && lastwin->w_status_height == 0)
9601 # endif
9602 if (edit_submode != NULL)
9603 return;
9604 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
9605 if (pum_visible())
9606 return;
9607 #endif
9609 #ifdef FEAT_STL_OPT
9610 if (*p_ruf)
9612 int save_called_emsg = called_emsg;
9614 called_emsg = FALSE;
9615 win_redr_custom(wp, TRUE);
9616 if (called_emsg)
9617 set_string_option_direct((char_u *)"rulerformat", -1,
9618 (char_u *)"", OPT_FREE, SID_ERROR);
9619 called_emsg |= save_called_emsg;
9620 return;
9622 #endif
9625 * Check if not in Insert mode and the line is empty (will show "0-1").
9627 if (!(State & INSERT)
9628 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
9629 empty_line = TRUE;
9632 * Only draw the ruler when something changed.
9634 validate_virtcol_win(wp);
9635 if ( redraw_cmdline
9636 || always
9637 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
9638 || wp->w_cursor.col != wp->w_ru_cursor.col
9639 || wp->w_virtcol != wp->w_ru_virtcol
9640 #ifdef FEAT_VIRTUALEDIT
9641 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
9642 #endif
9643 || wp->w_topline != wp->w_ru_topline
9644 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
9645 #ifdef FEAT_DIFF
9646 || wp->w_topfill != wp->w_ru_topfill
9647 #endif
9648 || empty_line != wp->w_ru_empty)
9650 cursor_off();
9651 #ifdef FEAT_WINDOWS
9652 if (wp->w_status_height)
9654 row = W_WINROW(wp) + wp->w_height;
9655 fillchar = fillchar_status(&attr, wp == curwin);
9656 # ifdef FEAT_VERTSPLIT
9657 off = W_WINCOL(wp);
9658 width = W_WIDTH(wp);
9659 # endif
9661 else
9662 #endif
9664 row = Rows - 1;
9665 fillchar = ' ';
9666 attr = 0;
9667 #ifdef FEAT_VERTSPLIT
9668 width = Columns;
9669 off = 0;
9670 #endif
9673 /* In list mode virtcol needs to be recomputed */
9674 virtcol = wp->w_virtcol;
9675 if (wp->w_p_list && lcs_tab1 == NUL)
9677 wp->w_p_list = FALSE;
9678 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
9679 wp->w_p_list = TRUE;
9683 * Some sprintfs return the length, some return a pointer.
9684 * To avoid portability problems we use strlen() here.
9686 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
9687 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
9688 ? 0L
9689 : (long)(wp->w_cursor.lnum));
9690 len = STRLEN(buffer);
9691 col_print(buffer + len, RULER_BUF_LEN - len,
9692 empty_line ? 0 : (int)wp->w_cursor.col + 1,
9693 (int)virtcol + 1);
9696 * Add a "50%" if there is room for it.
9697 * On the last line, don't print in the last column (scrolls the
9698 * screen up on some terminals).
9700 i = (int)STRLEN(buffer);
9701 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
9702 o = i + vim_strsize(buffer + i + 1);
9703 #ifdef FEAT_WINDOWS
9704 if (wp->w_status_height == 0) /* can't use last char of screen */
9705 #endif
9706 ++o;
9707 #ifdef FEAT_VERTSPLIT
9708 this_ru_col = ru_col - (Columns - width);
9709 if (this_ru_col < 0)
9710 this_ru_col = 0;
9711 #endif
9712 /* Never use more than half the window/screen width, leave the other
9713 * half for the filename. */
9714 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
9715 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
9716 if (this_ru_col + o < WITH_WIDTH(width))
9718 while (this_ru_col + o < WITH_WIDTH(width))
9720 #ifdef FEAT_MBYTE
9721 if (has_mbyte)
9722 i += (*mb_char2bytes)(fillchar, buffer + i);
9723 else
9724 #endif
9725 buffer[i++] = fillchar;
9726 ++o;
9728 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
9730 /* Truncate at window boundary. */
9731 #ifdef FEAT_MBYTE
9732 if (has_mbyte)
9734 o = 0;
9735 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
9737 o += (*mb_ptr2cells)(buffer + i);
9738 if (this_ru_col + o > WITH_WIDTH(width))
9740 buffer[i] = NUL;
9741 break;
9745 else
9746 #endif
9747 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
9748 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
9750 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
9751 i = redraw_cmdline;
9752 screen_fill(row, row + 1,
9753 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
9754 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
9755 fillchar, fillchar, attr);
9756 /* don't redraw the cmdline because of showing the ruler */
9757 redraw_cmdline = i;
9758 wp->w_ru_cursor = wp->w_cursor;
9759 wp->w_ru_virtcol = wp->w_virtcol;
9760 wp->w_ru_empty = empty_line;
9761 wp->w_ru_topline = wp->w_topline;
9762 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
9763 #ifdef FEAT_DIFF
9764 wp->w_ru_topfill = wp->w_topfill;
9765 #endif
9768 #endif
9770 #if defined(FEAT_LINEBREAK) || defined(PROTO)
9772 * Return the width of the 'number' column.
9773 * Caller may need to check if 'number' is set.
9774 * Otherwise it depends on 'numberwidth' and the line count.
9777 number_width(wp)
9778 win_T *wp;
9780 int n;
9781 linenr_T lnum;
9783 lnum = wp->w_buffer->b_ml.ml_line_count;
9784 if (lnum == wp->w_nrwidth_line_count)
9785 return wp->w_nrwidth_width;
9786 wp->w_nrwidth_line_count = lnum;
9788 n = 0;
9791 lnum /= 10;
9792 ++n;
9793 } while (lnum > 0);
9795 /* 'numberwidth' gives the minimal width plus one */
9796 if (n < wp->w_p_nuw - 1)
9797 n = wp->w_p_nuw - 1;
9799 wp->w_nrwidth_width = n;
9800 return n;
9802 #endif