Merge branch 'vim' into feat/indent-wrap-lines
[vim_extended.git] / src / screen.c
blobcae40680310e0fad27d0f8a1c9bcb996ae358fbd
1 /* vi:set ts=8 sts=4 sw=4:
3 * VIM - Vi IMproved by Bram Moolenaar
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
11 * screen.c: code for displaying on the screen
13 * Output to the screen (console, terminal emulator or GUI window) is minimized
14 * by remembering what is already on the screen, and only updating the parts
15 * that changed.
17 * ScreenLines[off] Contains a copy of the whole screen, as it is currently
18 * displayed (excluding text written by external commands).
19 * ScreenAttrs[off] Contains the associated attributes.
20 * LineOffset[row] Contains the offset into ScreenLines*[] and ScreenAttrs[]
21 * for each line.
22 * LineWraps[row] Flag for each line whether it wraps to the next line.
24 * For double-byte characters, two consecutive bytes in ScreenLines[] can form
25 * one character which occupies two display cells.
26 * For UTF-8 a multi-byte character is converted to Unicode and stored in
27 * ScreenLinesUC[]. ScreenLines[] contains the first byte only. For an ASCII
28 * character without composing chars ScreenLinesUC[] will be 0. When the
29 * character occupies two display cells the next byte in ScreenLines[] is 0.
30 * ScreenLinesC[][] contain up to 'maxcombine' composing characters
31 * (drawn on top of the first character). They are 0 when not used.
32 * ScreenLines2[] is only used for euc-jp to store the second byte if the
33 * first byte is 0x8e (single-width character).
35 * The screen_*() functions write to the screen and handle updating
36 * ScreenLines[].
38 * update_screen() is the function that updates all windows and status lines.
39 * It is called form the main loop when must_redraw is non-zero. It may be
40 * called from other places when an immediate screen update is needed.
42 * The part of the buffer that is displayed in a window is set with:
43 * - w_topline (first buffer line in window)
44 * - w_topfill (filler line above the first line)
45 * - w_leftcol (leftmost window cell in window),
46 * - w_skipcol (skipped window cells of first line)
48 * Commands that only move the cursor around in a window, do not need to take
49 * action to update the display. The main loop will check if w_topline is
50 * valid and update it (scroll the window) when needed.
52 * Commands that scroll a window change w_topline and must call
53 * check_cursor() to move the cursor into the visible part of the window, and
54 * call redraw_later(VALID) to have the window displayed by update_screen()
55 * later.
57 * Commands that change text in the buffer must call changed_bytes() or
58 * changed_lines() to mark the area that changed and will require updating
59 * later. The main loop will call update_screen(), which will update each
60 * window that shows the changed buffer. This assumes text above the change
61 * can remain displayed as it is. Text after the change may need updating for
62 * scrolling, folding and syntax highlighting.
64 * Commands that change how a window is displayed (e.g., setting 'list') or
65 * invalidate the contents of a window in another way (e.g., change fold
66 * settings), must call redraw_later(NOT_VALID) to have the whole window
67 * redisplayed by update_screen() later.
69 * Commands that change how a buffer is displayed (e.g., setting 'tabstop')
70 * must call redraw_curbuf_later(NOT_VALID) to have all the windows for the
71 * buffer redisplayed by update_screen() later.
73 * Commands that change highlighting and possibly cause a scroll too must call
74 * redraw_later(SOME_VALID) to update the whole window but still use scrolling
75 * to avoid redrawing everything. But the length of displayed lines must not
76 * change, use NOT_VALID then.
78 * Commands that move the window position must call redraw_later(NOT_VALID).
79 * TODO: should minimize redrawing by scrolling when possible.
81 * Commands that change everything (e.g., resizing the screen) must call
82 * redraw_all_later(NOT_VALID) or redraw_all_later(CLEAR).
84 * Things that are handled indirectly:
85 * - When messages scroll the screen up, msg_scrolled will be set and
86 * update_screen() called to redraw.
89 #include "vim.h"
92 * The attributes that are actually active for writing to the screen.
94 static int screen_attr = 0;
97 * Positioning the cursor is reduced by remembering the last position.
98 * Mostly used by windgoto() and screen_char().
100 static int screen_cur_row, screen_cur_col; /* last known cursor position */
102 #ifdef FEAT_SEARCH_EXTRA
103 static match_T search_hl; /* used for 'hlsearch' highlight matching */
104 #endif
106 #ifdef FEAT_FOLDING
107 static foldinfo_T win_foldinfo; /* info for 'foldcolumn' */
108 #endif
111 * Buffer for one screen line (characters and attributes).
113 static schar_T *current_ScreenLine;
115 static void win_update __ARGS((win_T *wp));
116 static void win_draw_end __ARGS((win_T *wp, int c1, int c2, int row, int endrow, hlf_T hl));
117 #ifdef FEAT_FOLDING
118 static void fold_line __ARGS((win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row));
119 static void fill_foldcolumn __ARGS((char_u *p, win_T *wp, int closed, linenr_T lnum));
120 static void copy_text_attr __ARGS((int off, char_u *buf, int len, int attr));
121 #endif
122 static int win_line __ARGS((win_T *, linenr_T, int, int, int nochange));
123 static int char_needs_redraw __ARGS((int off_from, int off_to, int cols));
124 #ifdef FEAT_RIGHTLEFT
125 static void screen_line __ARGS((int row, int coloff, int endcol, int clear_width, int rlflag));
126 # define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c), (rl))
127 #else
128 static void screen_line __ARGS((int row, int coloff, int endcol, int clear_width));
129 # define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c))
130 #endif
131 #ifdef FEAT_VERTSPLIT
132 static void draw_vsep_win __ARGS((win_T *wp, int row));
133 #endif
134 #ifdef FEAT_STL_OPT
135 static void redraw_custom_statusline __ARGS((win_T *wp));
136 #endif
137 #ifdef FEAT_SEARCH_EXTRA
138 #define SEARCH_HL_PRIORITY 0
139 static void start_search_hl __ARGS((void));
140 static void end_search_hl __ARGS((void));
141 static void prepare_search_hl __ARGS((win_T *wp, linenr_T lnum));
142 static void next_search_hl __ARGS((win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol));
143 #endif
144 static void screen_start_highlight __ARGS((int attr));
145 static void screen_char __ARGS((unsigned off, int row, int col));
146 #ifdef FEAT_MBYTE
147 static void screen_char_2 __ARGS((unsigned off, int row, int col));
148 #endif
149 static void screenclear2 __ARGS((void));
150 static void lineclear __ARGS((unsigned off, int width));
151 static void lineinvalid __ARGS((unsigned off, int width));
152 #ifdef FEAT_VERTSPLIT
153 static void linecopy __ARGS((int to, int from, win_T *wp));
154 static void redraw_block __ARGS((int row, int end, win_T *wp));
155 #endif
156 static int win_do_lines __ARGS((win_T *wp, int row, int line_count, int mayclear, int del));
157 static void win_rest_invalid __ARGS((win_T *wp));
158 static void msg_pos_mode __ARGS((void));
159 #if defined(FEAT_WINDOWS)
160 static void draw_tabline __ARGS((void));
161 #endif
162 #if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
163 static int fillchar_status __ARGS((int *attr, int is_curwin));
164 #endif
165 #ifdef FEAT_VERTSPLIT
166 static int fillchar_vsep __ARGS((int *attr));
167 #endif
168 #ifdef FEAT_STL_OPT
169 static void win_redr_custom __ARGS((win_T *wp, int draw_ruler));
170 #endif
171 #ifdef FEAT_CMDL_INFO
172 static void win_redr_ruler __ARGS((win_T *wp, int always));
173 #endif
175 #if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
176 /* Ugly global: overrule attribute used by screen_char() */
177 static int screen_char_attr = 0;
178 #endif
181 * Redraw the current window later, with update_screen(type).
182 * Set must_redraw only if not already set to a higher value.
183 * e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
185 void
186 redraw_later(type)
187 int type;
189 redraw_win_later(curwin, type);
192 void
193 redraw_win_later(wp, type)
194 win_T *wp;
195 int type;
197 if (wp->w_redr_type < type)
199 wp->w_redr_type = type;
200 if (type >= NOT_VALID)
201 wp->w_lines_valid = 0;
202 if (must_redraw < type) /* must_redraw is the maximum of all windows */
203 must_redraw = type;
208 * Force a complete redraw later. Also resets the highlighting. To be used
209 * after executing a shell command that messes up the screen.
211 void
212 redraw_later_clear()
214 redraw_all_later(CLEAR);
215 #ifdef FEAT_GUI
216 if (gui.in_use)
217 /* Use a code that will reset gui.highlight_mask in
218 * gui_stop_highlight(). */
219 screen_attr = HL_ALL + 1;
220 else
221 #endif
222 /* Use attributes that is very unlikely to appear in text. */
223 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE;
227 * Mark all windows to be redrawn later.
229 void
230 redraw_all_later(type)
231 int type;
233 win_T *wp;
235 FOR_ALL_WINDOWS(wp)
237 redraw_win_later(wp, type);
242 * Mark all windows that are editing the current buffer to be updated later.
244 void
245 redraw_curbuf_later(type)
246 int type;
248 redraw_buf_later(curbuf, type);
251 void
252 redraw_buf_later(buf, type)
253 buf_T *buf;
254 int type;
256 win_T *wp;
258 FOR_ALL_WINDOWS(wp)
260 if (wp->w_buffer == buf)
261 redraw_win_later(wp, type);
266 * Changed something in the current window, at buffer line "lnum", that
267 * requires that line and possibly other lines to be redrawn.
268 * Used when entering/leaving Insert mode with the cursor on a folded line.
269 * Used to remove the "$" from a change command.
270 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
271 * may become invalid and the whole window will have to be redrawn.
273 void
274 redrawWinline(lnum, invalid)
275 linenr_T lnum;
276 int invalid UNUSED; /* window line height is invalid now */
278 #ifdef FEAT_FOLDING
279 int i;
280 #endif
282 if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
283 curwin->w_redraw_top = lnum;
284 if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
285 curwin->w_redraw_bot = lnum;
286 redraw_later(VALID);
288 #ifdef FEAT_FOLDING
289 if (invalid)
291 /* A w_lines[] entry for this lnum has become invalid. */
292 i = find_wl_entry(curwin, lnum);
293 if (i >= 0)
294 curwin->w_lines[i].wl_valid = FALSE;
296 #endif
300 * update all windows that are editing the current buffer
302 void
303 update_curbuf(type)
304 int type;
306 redraw_curbuf_later(type);
307 update_screen(type);
311 * update_screen()
313 * Based on the current value of curwin->w_topline, transfer a screenfull
314 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
316 void
317 update_screen(type)
318 int type;
320 win_T *wp;
321 static int did_intro = FALSE;
322 #if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
323 int did_one;
324 #endif
326 /* Don't do anything if the screen structures are (not yet) valid. */
327 if (!screen_valid(TRUE))
328 return;
330 if (must_redraw)
332 if (type < must_redraw) /* use maximal type */
333 type = must_redraw;
335 /* must_redraw is reset here, so that when we run into some weird
336 * reason to redraw while busy redrawing (e.g., asynchronous
337 * scrolling), or update_topline() in win_update() will cause a
338 * scroll, the screen will be redrawn later or in win_update(). */
339 must_redraw = 0;
342 /* Need to update w_lines[]. */
343 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
344 type = NOT_VALID;
346 /* Postpone the redrawing when it's not needed and when being called
347 * recursively. */
348 if (!redrawing() || updating_screen)
350 redraw_later(type); /* remember type for next time */
351 must_redraw = type;
352 if (type > INVERTED_ALL)
353 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
354 return;
357 updating_screen = TRUE;
358 #ifdef FEAT_SYN_HL
359 ++display_tick; /* let syntax code know we're in a next round of
360 * display updating */
361 #endif
364 * if the screen was scrolled up when displaying a message, scroll it down
366 if (msg_scrolled)
368 clear_cmdline = TRUE;
369 if (msg_scrolled > Rows - 5) /* clearing is faster */
370 type = CLEAR;
371 else if (type != CLEAR)
373 check_for_delay(FALSE);
374 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, NULL) == FAIL)
375 type = CLEAR;
376 FOR_ALL_WINDOWS(wp)
378 if (W_WINROW(wp) < msg_scrolled)
380 if (W_WINROW(wp) + wp->w_height > msg_scrolled
381 && wp->w_redr_type < REDRAW_TOP
382 && wp->w_lines_valid > 0
383 && wp->w_topline == wp->w_lines[0].wl_lnum)
385 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
386 wp->w_redr_type = REDRAW_TOP;
388 else
390 wp->w_redr_type = NOT_VALID;
391 #ifdef FEAT_WINDOWS
392 if (W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp)
393 <= msg_scrolled)
394 wp->w_redr_status = TRUE;
395 #endif
399 redraw_cmdline = TRUE;
400 #ifdef FEAT_WINDOWS
401 redraw_tabline = TRUE;
402 #endif
404 msg_scrolled = 0;
405 need_wait_return = FALSE;
408 /* reset cmdline_row now (may have been changed temporarily) */
409 compute_cmdrow();
411 /* Check for changed highlighting */
412 if (need_highlight_changed)
413 highlight_changed();
415 if (type == CLEAR) /* first clear screen */
417 screenclear(); /* will reset clear_cmdline */
418 type = NOT_VALID;
421 if (clear_cmdline) /* going to clear cmdline (done below) */
422 check_for_delay(FALSE);
424 #ifdef FEAT_LINEBREAK
425 /* Force redraw when width of 'number' column changes. */
426 if (curwin->w_redr_type < NOT_VALID
427 && curwin->w_nrwidth != (curwin->w_p_nu ? number_width(curwin) : 0))
428 curwin->w_redr_type = NOT_VALID;
429 #endif
432 * Only start redrawing if there is really something to do.
434 if (type == INVERTED)
435 update_curswant();
436 if (curwin->w_redr_type < type
437 && !((type == VALID
438 && curwin->w_lines[0].wl_valid
439 #ifdef FEAT_DIFF
440 && curwin->w_topfill == curwin->w_old_topfill
441 && curwin->w_botfill == curwin->w_old_botfill
442 #endif
443 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
444 #ifdef FEAT_VISUAL
445 || (type == INVERTED
446 && VIsual_active
447 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
448 && curwin->w_old_visual_mode == VIsual_mode
449 && (curwin->w_valid & VALID_VIRTCOL)
450 && curwin->w_old_curswant == curwin->w_curswant)
451 #endif
453 curwin->w_redr_type = type;
455 #ifdef FEAT_WINDOWS
456 /* Redraw the tab pages line if needed. */
457 if (redraw_tabline || type >= NOT_VALID)
458 draw_tabline();
459 #endif
461 #ifdef FEAT_SYN_HL
463 * Correct stored syntax highlighting info for changes in each displayed
464 * buffer. Each buffer must only be done once.
466 FOR_ALL_WINDOWS(wp)
468 if (wp->w_buffer->b_mod_set)
470 # ifdef FEAT_WINDOWS
471 win_T *wwp;
473 /* Check if we already did this buffer. */
474 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
475 if (wwp->w_buffer == wp->w_buffer)
476 break;
477 # endif
478 if (
479 # ifdef FEAT_WINDOWS
480 wwp == wp &&
481 # endif
482 syntax_present(wp->w_buffer))
483 syn_stack_apply_changes(wp->w_buffer);
486 #endif
489 * Go from top to bottom through the windows, redrawing the ones that need
490 * it.
492 #if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
493 did_one = FALSE;
494 #endif
495 #ifdef FEAT_SEARCH_EXTRA
496 search_hl.rm.regprog = NULL;
497 #endif
498 FOR_ALL_WINDOWS(wp)
500 if (wp->w_redr_type != 0)
502 cursor_off();
503 #if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
504 if (!did_one)
506 did_one = TRUE;
507 # ifdef FEAT_SEARCH_EXTRA
508 start_search_hl();
509 # endif
510 # ifdef FEAT_CLIPBOARD
511 /* When Visual area changed, may have to update selection. */
512 if (clip_star.available && clip_isautosel())
513 clip_update_selection();
514 # endif
515 #ifdef FEAT_GUI
516 /* Remove the cursor before starting to do anything, because
517 * scrolling may make it difficult to redraw the text under
518 * it. */
519 if (gui.in_use)
520 gui_undraw_cursor();
521 #endif
523 #endif
524 win_update(wp);
527 #ifdef FEAT_WINDOWS
528 /* redraw status line after the window to minimize cursor movement */
529 if (wp->w_redr_status)
531 cursor_off();
532 win_redr_status(wp);
534 #endif
536 #if defined(FEAT_SEARCH_EXTRA)
537 end_search_hl();
538 #endif
540 #ifdef FEAT_WINDOWS
541 /* Reset b_mod_set flags. Going through all windows is probably faster
542 * than going through all buffers (there could be many buffers). */
543 for (wp = firstwin; wp != NULL; wp = wp->w_next)
544 wp->w_buffer->b_mod_set = FALSE;
545 #else
546 curbuf->b_mod_set = FALSE;
547 #endif
549 updating_screen = FALSE;
550 #ifdef FEAT_GUI
551 gui_may_resize_shell();
552 #endif
554 /* Clear or redraw the command line. Done last, because scrolling may
555 * mess up the command line. */
556 if (clear_cmdline || redraw_cmdline)
557 showmode();
559 /* May put up an introductory message when not editing a file */
560 if (!did_intro && bufempty()
561 && curbuf->b_fname == NULL
562 #ifdef FEAT_WINDOWS
563 && firstwin->w_next == NULL
564 #endif
565 && vim_strchr(p_shm, SHM_INTRO) == NULL)
566 intro_message(FALSE);
567 did_intro = TRUE;
569 #ifdef FEAT_GUI
570 /* Redraw the cursor and update the scrollbars when all screen updating is
571 * done. */
572 if (gui.in_use)
574 out_flush(); /* required before updating the cursor */
575 if (did_one)
576 gui_update_cursor(FALSE, FALSE);
577 gui_update_scrollbars(FALSE);
579 #endif
582 #if defined(FEAT_SIGNS) || defined(FEAT_GUI)
583 static void update_prepare __ARGS((void));
584 static void update_finish __ARGS((void));
587 * Prepare for updating one or more windows.
588 * Caller must check for "updating_screen" already set to avoid recursiveness.
590 static void
591 update_prepare()
593 cursor_off();
594 updating_screen = TRUE;
595 #ifdef FEAT_GUI
596 /* Remove the cursor before starting to do anything, because scrolling may
597 * make it difficult to redraw the text under it. */
598 if (gui.in_use)
599 gui_undraw_cursor();
600 #endif
601 #ifdef FEAT_SEARCH_EXTRA
602 start_search_hl();
603 #endif
607 * Finish updating one or more windows.
609 static void
610 update_finish()
612 if (redraw_cmdline)
613 showmode();
615 # ifdef FEAT_SEARCH_EXTRA
616 end_search_hl();
617 # endif
619 updating_screen = FALSE;
621 # ifdef FEAT_GUI
622 gui_may_resize_shell();
624 /* Redraw the cursor and update the scrollbars when all screen updating is
625 * done. */
626 if (gui.in_use)
628 out_flush(); /* required before updating the cursor */
629 gui_update_cursor(FALSE, FALSE);
630 gui_update_scrollbars(FALSE);
632 # endif
634 #endif
636 #if defined(FEAT_SIGNS) || defined(PROTO)
637 void
638 update_debug_sign(buf, lnum)
639 buf_T *buf;
640 linenr_T lnum;
642 win_T *wp;
643 int doit = FALSE;
645 # ifdef FEAT_FOLDING
646 win_foldinfo.fi_level = 0;
647 # endif
649 /* update/delete a specific mark */
650 FOR_ALL_WINDOWS(wp)
652 if (buf != NULL && lnum > 0)
654 if (wp->w_buffer == buf && lnum >= wp->w_topline
655 && lnum < wp->w_botline)
657 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
658 wp->w_redraw_top = lnum;
659 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
660 wp->w_redraw_bot = lnum;
661 redraw_win_later(wp, VALID);
664 else
665 redraw_win_later(wp, VALID);
666 if (wp->w_redr_type != 0)
667 doit = TRUE;
670 /* Return when there is nothing to do or screen updating already
671 * happening. */
672 if (!doit || updating_screen)
673 return;
675 /* update all windows that need updating */
676 update_prepare();
678 # ifdef FEAT_WINDOWS
679 for (wp = firstwin; wp; wp = wp->w_next)
681 if (wp->w_redr_type != 0)
682 win_update(wp);
683 if (wp->w_redr_status)
684 win_redr_status(wp);
686 # else
687 if (curwin->w_redr_type != 0)
688 win_update(curwin);
689 # endif
691 update_finish();
693 #endif
696 #if defined(FEAT_GUI) || defined(PROTO)
698 * Update a single window, its status line and maybe the command line msg.
699 * Used for the GUI scrollbar.
701 void
702 updateWindow(wp)
703 win_T *wp;
705 /* return if already busy updating */
706 if (updating_screen)
707 return;
709 update_prepare();
711 #ifdef FEAT_CLIPBOARD
712 /* When Visual area changed, may have to update selection. */
713 if (clip_star.available && clip_isautosel())
714 clip_update_selection();
715 #endif
717 win_update(wp);
719 #ifdef FEAT_WINDOWS
720 /* When the screen was cleared redraw the tab pages line. */
721 if (redraw_tabline)
722 draw_tabline();
724 if (wp->w_redr_status
725 # ifdef FEAT_CMDL_INFO
726 || p_ru
727 # endif
728 # ifdef FEAT_STL_OPT
729 || *p_stl != NUL || *wp->w_p_stl != NUL
730 # endif
732 win_redr_status(wp);
733 #endif
735 update_finish();
737 #endif
740 * Update a single window.
742 * This may cause the windows below it also to be redrawn (when clearing the
743 * screen or scrolling lines).
745 * How the window is redrawn depends on wp->w_redr_type. Each type also
746 * implies the one below it.
747 * NOT_VALID redraw the whole window
748 * SOME_VALID redraw the whole window but do scroll when possible
749 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
750 * INVERTED redraw the changed part of the Visual area
751 * INVERTED_ALL redraw the whole Visual area
752 * VALID 1. scroll up/down to adjust for a changed w_topline
753 * 2. update lines at the top when scrolled down
754 * 3. redraw changed text:
755 * - if wp->w_buffer->b_mod_set set, update lines between
756 * b_mod_top and b_mod_bot.
757 * - if wp->w_redraw_top non-zero, redraw lines between
758 * wp->w_redraw_top and wp->w_redr_bot.
759 * - continue redrawing when syntax status is invalid.
760 * 4. if scrolled up, update lines at the bottom.
761 * This results in three areas that may need updating:
762 * top: from first row to top_end (when scrolled down)
763 * mid: from mid_start to mid_end (update inversion or changed text)
764 * bot: from bot_start to last row (when scrolled up)
766 static void
767 win_update(wp)
768 win_T *wp;
770 buf_T *buf = wp->w_buffer;
771 int type;
772 int top_end = 0; /* Below last row of the top area that needs
773 updating. 0 when no top area updating. */
774 int mid_start = 999;/* first row of the mid area that needs
775 updating. 999 when no mid area updating. */
776 int mid_end = 0; /* Below last row of the mid area that needs
777 updating. 0 when no mid area updating. */
778 int bot_start = 999;/* first row of the bot area that needs
779 updating. 999 when no bot area updating */
780 #ifdef FEAT_VISUAL
781 int scrolled_down = FALSE; /* TRUE when scrolled down when
782 w_topline got smaller a bit */
783 #endif
784 #ifdef FEAT_SEARCH_EXTRA
785 matchitem_T *cur; /* points to the match list */
786 int top_to_mod = FALSE; /* redraw above mod_top */
787 #endif
789 int row; /* current window row to display */
790 linenr_T lnum; /* current buffer lnum to display */
791 int idx; /* current index in w_lines[] */
792 int srow; /* starting row of the current line */
794 int eof = FALSE; /* if TRUE, we hit the end of the file */
795 int didline = FALSE; /* if TRUE, we finished the last line */
796 int i;
797 long j;
798 static int recursive = FALSE; /* being called recursively */
799 int old_botline = wp->w_botline;
800 #ifdef FEAT_FOLDING
801 long fold_count;
802 #endif
803 #ifdef FEAT_SYN_HL
804 /* remember what happened to the previous line, to know if
805 * check_visual_highlight() can be used */
806 #define DID_NONE 1 /* didn't update a line */
807 #define DID_LINE 2 /* updated a normal line */
808 #define DID_FOLD 3 /* updated a folded line */
809 int did_update = DID_NONE;
810 linenr_T syntax_last_parsed = 0; /* last parsed text line */
811 #endif
812 linenr_T mod_top = 0;
813 linenr_T mod_bot = 0;
814 #if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
815 int save_got_int;
816 #endif
818 type = wp->w_redr_type;
820 if (type == NOT_VALID)
822 #ifdef FEAT_WINDOWS
823 wp->w_redr_status = TRUE;
824 #endif
825 wp->w_lines_valid = 0;
828 /* Window is zero-height: nothing to draw. */
829 if (wp->w_height == 0)
831 wp->w_redr_type = 0;
832 return;
835 #ifdef FEAT_VERTSPLIT
836 /* Window is zero-width: Only need to draw the separator. */
837 if (wp->w_width == 0)
839 /* draw the vertical separator right of this window */
840 draw_vsep_win(wp, 0);
841 wp->w_redr_type = 0;
842 return;
844 #endif
846 #ifdef FEAT_SEARCH_EXTRA
847 /* Setup for match and 'hlsearch' highlighting. Disable any previous
848 * match */
849 cur = wp->w_match_head;
850 while (cur != NULL)
852 cur->hl.rm = cur->match;
853 if (cur->hlg_id == 0)
854 cur->hl.attr = 0;
855 else
856 cur->hl.attr = syn_id2attr(cur->hlg_id);
857 cur->hl.buf = buf;
858 cur->hl.lnum = 0;
859 cur->hl.first_lnum = 0;
860 # ifdef FEAT_RELTIME
861 /* Set the time limit to 'redrawtime'. */
862 profile_setlimit(p_rdt, &(cur->hl.tm));
863 # endif
864 cur = cur->next;
866 search_hl.buf = buf;
867 search_hl.lnum = 0;
868 search_hl.first_lnum = 0;
869 /* time limit is set at the toplevel, for all windows */
870 #endif
872 #ifdef FEAT_LINEBREAK
873 /* Force redraw when width of 'number' column changes. */
874 i = wp->w_p_nu ? number_width(wp) : 0;
875 if (wp->w_nrwidth != i)
877 type = NOT_VALID;
878 wp->w_nrwidth = i;
880 else
881 #endif
883 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
886 * When there are both inserted/deleted lines and specific lines to be
887 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
888 * everything (only happens when redrawing is off for while).
890 type = NOT_VALID;
892 else
895 * Set mod_top to the first line that needs displaying because of
896 * changes. Set mod_bot to the first line after the changes.
898 mod_top = wp->w_redraw_top;
899 if (wp->w_redraw_bot != 0)
900 mod_bot = wp->w_redraw_bot + 1;
901 else
902 mod_bot = 0;
903 wp->w_redraw_top = 0; /* reset for next time */
904 wp->w_redraw_bot = 0;
905 if (buf->b_mod_set)
907 if (mod_top == 0 || mod_top > buf->b_mod_top)
909 mod_top = buf->b_mod_top;
910 #ifdef FEAT_SYN_HL
911 /* Need to redraw lines above the change that may be included
912 * in a pattern match. */
913 if (syntax_present(buf))
915 mod_top -= buf->b_syn_sync_linebreaks;
916 if (mod_top < 1)
917 mod_top = 1;
919 #endif
921 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
922 mod_bot = buf->b_mod_bot;
924 #ifdef FEAT_SEARCH_EXTRA
925 /* When 'hlsearch' is on and using a multi-line search pattern, a
926 * change in one line may make the Search highlighting in a
927 * previous line invalid. Simple solution: redraw all visible
928 * lines above the change.
929 * Same for a match pattern.
931 if (search_hl.rm.regprog != NULL
932 && re_multiline(search_hl.rm.regprog))
933 top_to_mod = TRUE;
934 else
936 cur = wp->w_match_head;
937 while (cur != NULL)
939 if (cur->match.regprog != NULL
940 && re_multiline(cur->match.regprog))
942 top_to_mod = TRUE;
943 break;
945 cur = cur->next;
948 #endif
950 #ifdef FEAT_FOLDING
951 if (mod_top != 0 && hasAnyFolding(wp))
953 linenr_T lnumt, lnumb;
956 * A change in a line can cause lines above it to become folded or
957 * unfolded. Find the top most buffer line that may be affected.
958 * If the line was previously folded and displayed, get the first
959 * line of that fold. If the line is folded now, get the first
960 * folded line. Use the minimum of these two.
963 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
964 * the line below it. If there is no valid entry, use w_topline.
965 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
966 * to this line. If there is no valid entry, use MAXLNUM. */
967 lnumt = wp->w_topline;
968 lnumb = MAXLNUM;
969 for (i = 0; i < wp->w_lines_valid; ++i)
970 if (wp->w_lines[i].wl_valid)
972 if (wp->w_lines[i].wl_lastlnum < mod_top)
973 lnumt = wp->w_lines[i].wl_lastlnum + 1;
974 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
976 lnumb = wp->w_lines[i].wl_lnum;
977 /* When there is a fold column it might need updating
978 * in the next line ("J" just above an open fold). */
979 if (wp->w_p_fdc > 0)
980 ++lnumb;
984 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
985 if (mod_top > lnumt)
986 mod_top = lnumt;
988 /* Now do the same for the bottom line (one above mod_bot). */
989 --mod_bot;
990 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
991 ++mod_bot;
992 if (mod_bot < lnumb)
993 mod_bot = lnumb;
995 #endif
997 /* When a change starts above w_topline and the end is below
998 * w_topline, start redrawing at w_topline.
999 * If the end of the change is above w_topline: do like no change was
1000 * made, but redraw the first line to find changes in syntax. */
1001 if (mod_top != 0 && mod_top < wp->w_topline)
1003 if (mod_bot > wp->w_topline)
1004 mod_top = wp->w_topline;
1005 #ifdef FEAT_SYN_HL
1006 else if (syntax_present(buf))
1007 top_end = 1;
1008 #endif
1011 /* When line numbers are displayed need to redraw all lines below
1012 * inserted/deleted lines. */
1013 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1014 mod_bot = MAXLNUM;
1018 * When only displaying the lines at the top, set top_end. Used when
1019 * window has scrolled down for msg_scrolled.
1021 if (type == REDRAW_TOP)
1023 j = 0;
1024 for (i = 0; i < wp->w_lines_valid; ++i)
1026 j += wp->w_lines[i].wl_size;
1027 if (j >= wp->w_upd_rows)
1029 top_end = j;
1030 break;
1033 if (top_end == 0)
1034 /* not found (cannot happen?): redraw everything */
1035 type = NOT_VALID;
1036 else
1037 /* top area defined, the rest is VALID */
1038 type = VALID;
1041 /* Trick: we want to avoid clearing the screen twice. screenclear() will
1042 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1043 * non-zero and thus not FALSE) will indicate that screenclear() was not
1044 * called. */
1045 if (screen_cleared)
1046 screen_cleared = MAYBE;
1049 * If there are no changes on the screen that require a complete redraw,
1050 * handle three cases:
1051 * 1: we are off the top of the screen by a few lines: scroll down
1052 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1053 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1054 * w_lines[] that needs updating.
1056 if ((type == VALID || type == SOME_VALID
1057 || type == INVERTED || type == INVERTED_ALL)
1058 #ifdef FEAT_DIFF
1059 && !wp->w_botfill && !wp->w_old_botfill
1060 #endif
1063 if (mod_top != 0 && wp->w_topline == mod_top)
1066 * w_topline is the first changed line, the scrolling will be done
1067 * further down.
1070 else if (wp->w_lines[0].wl_valid
1071 && (wp->w_topline < wp->w_lines[0].wl_lnum
1072 #ifdef FEAT_DIFF
1073 || (wp->w_topline == wp->w_lines[0].wl_lnum
1074 && wp->w_topfill > wp->w_old_topfill)
1075 #endif
1079 * New topline is above old topline: May scroll down.
1081 #ifdef FEAT_FOLDING
1082 if (hasAnyFolding(wp))
1084 linenr_T ln;
1086 /* count the number of lines we are off, counting a sequence
1087 * of folded lines as one */
1088 j = 0;
1089 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1091 ++j;
1092 if (j >= wp->w_height - 2)
1093 break;
1094 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1097 else
1098 #endif
1099 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1100 if (j < wp->w_height - 2) /* not too far off */
1102 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1103 #ifdef FEAT_DIFF
1104 /* insert extra lines for previously invisible filler lines */
1105 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1106 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1107 - wp->w_old_topfill;
1108 #endif
1109 if (i < wp->w_height - 2) /* less than a screen off */
1112 * Try to insert the correct number of lines.
1113 * If not the last window, delete the lines at the bottom.
1114 * win_ins_lines may fail when the terminal can't do it.
1116 if (i > 0)
1117 check_for_delay(FALSE);
1118 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1120 if (wp->w_lines_valid != 0)
1122 /* Need to update rows that are new, stop at the
1123 * first one that scrolled down. */
1124 top_end = i;
1125 #ifdef FEAT_VISUAL
1126 scrolled_down = TRUE;
1127 #endif
1129 /* Move the entries that were scrolled, disable
1130 * the entries for the lines to be redrawn. */
1131 if ((wp->w_lines_valid += j) > wp->w_height)
1132 wp->w_lines_valid = wp->w_height;
1133 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1134 wp->w_lines[idx] = wp->w_lines[idx - j];
1135 while (idx >= 0)
1136 wp->w_lines[idx--].wl_valid = FALSE;
1139 else
1140 mid_start = 0; /* redraw all lines */
1142 else
1143 mid_start = 0; /* redraw all lines */
1145 else
1146 mid_start = 0; /* redraw all lines */
1148 else
1151 * New topline is at or below old topline: May scroll up.
1152 * When topline didn't change, find first entry in w_lines[] that
1153 * needs updating.
1156 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1157 j = -1;
1158 row = 0;
1159 for (i = 0; i < wp->w_lines_valid; i++)
1161 if (wp->w_lines[i].wl_valid
1162 && wp->w_lines[i].wl_lnum == wp->w_topline)
1164 j = i;
1165 break;
1167 row += wp->w_lines[i].wl_size;
1169 if (j == -1)
1171 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1172 * lines */
1173 mid_start = 0;
1175 else
1178 * Try to delete the correct number of lines.
1179 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1181 #ifdef FEAT_DIFF
1182 /* If the topline didn't change, delete old filler lines,
1183 * otherwise delete filler lines of the new topline... */
1184 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1185 row += wp->w_old_topfill;
1186 else
1187 row += diff_check_fill(wp, wp->w_topline);
1188 /* ... but don't delete new filler lines. */
1189 row -= wp->w_topfill;
1190 #endif
1191 if (row > 0)
1193 check_for_delay(FALSE);
1194 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1195 bot_start = wp->w_height - row;
1196 else
1197 mid_start = 0; /* redraw all lines */
1199 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1202 * Skip the lines (below the deleted lines) that are still
1203 * valid and don't need redrawing. Copy their info
1204 * upwards, to compensate for the deleted lines. Set
1205 * bot_start to the first row that needs redrawing.
1207 bot_start = 0;
1208 idx = 0;
1209 for (;;)
1211 wp->w_lines[idx] = wp->w_lines[j];
1212 /* stop at line that didn't fit, unless it is still
1213 * valid (no lines deleted) */
1214 if (row > 0 && bot_start + row
1215 + (int)wp->w_lines[j].wl_size > wp->w_height)
1217 wp->w_lines_valid = idx + 1;
1218 break;
1220 bot_start += wp->w_lines[idx++].wl_size;
1222 /* stop at the last valid entry in w_lines[].wl_size */
1223 if (++j >= wp->w_lines_valid)
1225 wp->w_lines_valid = idx;
1226 break;
1229 #ifdef FEAT_DIFF
1230 /* Correct the first entry for filler lines at the top
1231 * when it won't get updated below. */
1232 if (wp->w_p_diff && bot_start > 0)
1233 wp->w_lines[0].wl_size =
1234 plines_win_nofill(wp, wp->w_topline, TRUE)
1235 + wp->w_topfill;
1236 #endif
1241 /* When starting redraw in the first line, redraw all lines. When
1242 * there is only one window it's probably faster to clear the screen
1243 * first. */
1244 if (mid_start == 0)
1246 mid_end = wp->w_height;
1247 if (lastwin == firstwin)
1249 /* Clear the screen when it was not done by win_del_lines() or
1250 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1251 * then. */
1252 if (screen_cleared != TRUE)
1253 screenclear();
1254 #ifdef FEAT_WINDOWS
1255 /* The screen was cleared, redraw the tab pages line. */
1256 if (redraw_tabline)
1257 draw_tabline();
1258 #endif
1262 /* When win_del_lines() or win_ins_lines() caused the screen to be
1263 * cleared (only happens for the first window) or when screenclear()
1264 * was called directly above, "must_redraw" will have been set to
1265 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1266 if (screen_cleared == TRUE)
1267 must_redraw = 0;
1269 else
1271 /* Not VALID or INVERTED: redraw all lines. */
1272 mid_start = 0;
1273 mid_end = wp->w_height;
1276 if (type == SOME_VALID)
1278 /* SOME_VALID: redraw all lines. */
1279 mid_start = 0;
1280 mid_end = wp->w_height;
1281 type = NOT_VALID;
1284 #ifdef FEAT_VISUAL
1285 /* check if we are updating or removing the inverted part */
1286 if ((VIsual_active && buf == curwin->w_buffer)
1287 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1289 linenr_T from, to;
1291 if (VIsual_active)
1293 if (VIsual_active
1294 && (VIsual_mode != wp->w_old_visual_mode
1295 || type == INVERTED_ALL))
1298 * If the type of Visual selection changed, redraw the whole
1299 * selection. Also when the ownership of the X selection is
1300 * gained or lost.
1302 if (curwin->w_cursor.lnum < VIsual.lnum)
1304 from = curwin->w_cursor.lnum;
1305 to = VIsual.lnum;
1307 else
1309 from = VIsual.lnum;
1310 to = curwin->w_cursor.lnum;
1312 /* redraw more when the cursor moved as well */
1313 if (wp->w_old_cursor_lnum < from)
1314 from = wp->w_old_cursor_lnum;
1315 if (wp->w_old_cursor_lnum > to)
1316 to = wp->w_old_cursor_lnum;
1317 if (wp->w_old_visual_lnum < from)
1318 from = wp->w_old_visual_lnum;
1319 if (wp->w_old_visual_lnum > to)
1320 to = wp->w_old_visual_lnum;
1322 else
1325 * Find the line numbers that need to be updated: The lines
1326 * between the old cursor position and the current cursor
1327 * position. Also check if the Visual position changed.
1329 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1331 from = curwin->w_cursor.lnum;
1332 to = wp->w_old_cursor_lnum;
1334 else
1336 from = wp->w_old_cursor_lnum;
1337 to = curwin->w_cursor.lnum;
1338 if (from == 0) /* Visual mode just started */
1339 from = to;
1342 if (VIsual.lnum != wp->w_old_visual_lnum
1343 || VIsual.col != wp->w_old_visual_col)
1345 if (wp->w_old_visual_lnum < from
1346 && wp->w_old_visual_lnum != 0)
1347 from = wp->w_old_visual_lnum;
1348 if (wp->w_old_visual_lnum > to)
1349 to = wp->w_old_visual_lnum;
1350 if (VIsual.lnum < from)
1351 from = VIsual.lnum;
1352 if (VIsual.lnum > to)
1353 to = VIsual.lnum;
1358 * If in block mode and changed column or curwin->w_curswant:
1359 * update all lines.
1360 * First compute the actual start and end column.
1362 if (VIsual_mode == Ctrl_V)
1364 colnr_T fromc, toc;
1366 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
1367 ++toc;
1368 if (curwin->w_curswant == MAXCOL)
1369 toc = MAXCOL;
1371 if (fromc != wp->w_old_cursor_fcol
1372 || toc != wp->w_old_cursor_lcol)
1374 if (from > VIsual.lnum)
1375 from = VIsual.lnum;
1376 if (to < VIsual.lnum)
1377 to = VIsual.lnum;
1379 wp->w_old_cursor_fcol = fromc;
1380 wp->w_old_cursor_lcol = toc;
1383 else
1385 /* Use the line numbers of the old Visual area. */
1386 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1388 from = wp->w_old_cursor_lnum;
1389 to = wp->w_old_visual_lnum;
1391 else
1393 from = wp->w_old_visual_lnum;
1394 to = wp->w_old_cursor_lnum;
1399 * There is no need to update lines above the top of the window.
1401 if (from < wp->w_topline)
1402 from = wp->w_topline;
1405 * If we know the value of w_botline, use it to restrict the update to
1406 * the lines that are visible in the window.
1408 if (wp->w_valid & VALID_BOTLINE)
1410 if (from >= wp->w_botline)
1411 from = wp->w_botline - 1;
1412 if (to >= wp->w_botline)
1413 to = wp->w_botline - 1;
1417 * Find the minimal part to be updated.
1418 * Watch out for scrolling that made entries in w_lines[] invalid.
1419 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1420 * top_end; need to redraw from top_end to the "to" line.
1421 * A middle mouse click with a Visual selection may change the text
1422 * above the Visual area and reset wl_valid, do count these for
1423 * mid_end (in srow).
1425 if (mid_start > 0)
1427 lnum = wp->w_topline;
1428 idx = 0;
1429 srow = 0;
1430 if (scrolled_down)
1431 mid_start = top_end;
1432 else
1433 mid_start = 0;
1434 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1436 if (wp->w_lines[idx].wl_valid)
1437 mid_start += wp->w_lines[idx].wl_size;
1438 else if (!scrolled_down)
1439 srow += wp->w_lines[idx].wl_size;
1440 ++idx;
1441 # ifdef FEAT_FOLDING
1442 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1443 lnum = wp->w_lines[idx].wl_lnum;
1444 else
1445 # endif
1446 ++lnum;
1448 srow += mid_start;
1449 mid_end = wp->w_height;
1450 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1452 if (wp->w_lines[idx].wl_valid
1453 && wp->w_lines[idx].wl_lnum >= to + 1)
1455 /* Only update until first row of this line */
1456 mid_end = srow;
1457 break;
1459 srow += wp->w_lines[idx].wl_size;
1464 if (VIsual_active && buf == curwin->w_buffer)
1466 wp->w_old_visual_mode = VIsual_mode;
1467 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1468 wp->w_old_visual_lnum = VIsual.lnum;
1469 wp->w_old_visual_col = VIsual.col;
1470 wp->w_old_curswant = curwin->w_curswant;
1472 else
1474 wp->w_old_visual_mode = 0;
1475 wp->w_old_cursor_lnum = 0;
1476 wp->w_old_visual_lnum = 0;
1477 wp->w_old_visual_col = 0;
1479 #endif /* FEAT_VISUAL */
1481 #if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1482 /* reset got_int, otherwise regexp won't work */
1483 save_got_int = got_int;
1484 got_int = 0;
1485 #endif
1486 #ifdef FEAT_FOLDING
1487 win_foldinfo.fi_level = 0;
1488 #endif
1491 * Update all the window rows.
1493 idx = 0; /* first entry in w_lines[].wl_size */
1494 row = 0;
1495 srow = 0;
1496 lnum = wp->w_topline; /* first line shown in window */
1497 for (;;)
1499 /* stop updating when reached the end of the window (check for _past_
1500 * the end of the window is at the end of the loop) */
1501 if (row == wp->w_height)
1503 didline = TRUE;
1504 break;
1507 /* stop updating when hit the end of the file */
1508 if (lnum > buf->b_ml.ml_line_count)
1510 eof = TRUE;
1511 break;
1514 /* Remember the starting row of the line that is going to be dealt
1515 * with. It is used further down when the line doesn't fit. */
1516 srow = row;
1519 * Update a line when it is in an area that needs updating, when it
1520 * has changes or w_lines[idx] is invalid.
1521 * bot_start may be halfway a wrapped line after using
1522 * win_del_lines(), check if the current line includes it.
1523 * When syntax folding is being used, the saved syntax states will
1524 * already have been updated, we can't see where the syntax state is
1525 * the same again, just update until the end of the window.
1527 if (row < top_end
1528 || (row >= mid_start && row < mid_end)
1529 #ifdef FEAT_SEARCH_EXTRA
1530 || top_to_mod
1531 #endif
1532 || idx >= wp->w_lines_valid
1533 || (row + wp->w_lines[idx].wl_size > bot_start)
1534 || (mod_top != 0
1535 && (lnum == mod_top
1536 || (lnum >= mod_top
1537 && (lnum < mod_bot
1538 #ifdef FEAT_SYN_HL
1539 || did_update == DID_FOLD
1540 || (did_update == DID_LINE
1541 && syntax_present(buf)
1542 && (
1543 # ifdef FEAT_FOLDING
1544 (foldmethodIsSyntax(wp)
1545 && hasAnyFolding(wp)) ||
1546 # endif
1547 syntax_check_changed(lnum)))
1548 #endif
1549 )))))
1551 #ifdef FEAT_SEARCH_EXTRA
1552 if (lnum == mod_top)
1553 top_to_mod = FALSE;
1554 #endif
1557 * When at start of changed lines: May scroll following lines
1558 * up or down to minimize redrawing.
1559 * Don't do this when the change continues until the end.
1560 * Don't scroll when dollar_vcol is non-zero, keep the "$".
1562 if (lnum == mod_top
1563 && mod_bot != MAXLNUM
1564 && !(dollar_vcol != 0 && mod_bot == mod_top + 1))
1566 int old_rows = 0;
1567 int new_rows = 0;
1568 int xtra_rows;
1569 linenr_T l;
1571 /* Count the old number of window rows, using w_lines[], which
1572 * should still contain the sizes for the lines as they are
1573 * currently displayed. */
1574 for (i = idx; i < wp->w_lines_valid; ++i)
1576 /* Only valid lines have a meaningful wl_lnum. Invalid
1577 * lines are part of the changed area. */
1578 if (wp->w_lines[i].wl_valid
1579 && wp->w_lines[i].wl_lnum == mod_bot)
1580 break;
1581 old_rows += wp->w_lines[i].wl_size;
1582 #ifdef FEAT_FOLDING
1583 if (wp->w_lines[i].wl_valid
1584 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1586 /* Must have found the last valid entry above mod_bot.
1587 * Add following invalid entries. */
1588 ++i;
1589 while (i < wp->w_lines_valid
1590 && !wp->w_lines[i].wl_valid)
1591 old_rows += wp->w_lines[i++].wl_size;
1592 break;
1594 #endif
1597 if (i >= wp->w_lines_valid)
1599 /* We can't find a valid line below the changed lines,
1600 * need to redraw until the end of the window.
1601 * Inserting/deleting lines has no use. */
1602 bot_start = 0;
1604 else
1606 /* Able to count old number of rows: Count new window
1607 * rows, and may insert/delete lines */
1608 j = idx;
1609 for (l = lnum; l < mod_bot; ++l)
1611 #ifdef FEAT_FOLDING
1612 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1613 ++new_rows;
1614 else
1615 #endif
1616 #ifdef FEAT_DIFF
1617 if (l == wp->w_topline)
1618 new_rows += plines_win_nofill(wp, l, TRUE)
1619 + wp->w_topfill;
1620 else
1621 #endif
1622 new_rows += plines_win(wp, l, TRUE);
1623 ++j;
1624 if (new_rows > wp->w_height - row - 2)
1626 /* it's getting too much, must redraw the rest */
1627 new_rows = 9999;
1628 break;
1631 xtra_rows = new_rows - old_rows;
1632 if (xtra_rows < 0)
1634 /* May scroll text up. If there is not enough
1635 * remaining text or scrolling fails, must redraw the
1636 * rest. If scrolling works, must redraw the text
1637 * below the scrolled text. */
1638 if (row - xtra_rows >= wp->w_height - 2)
1639 mod_bot = MAXLNUM;
1640 else
1642 check_for_delay(FALSE);
1643 if (win_del_lines(wp, row,
1644 -xtra_rows, FALSE, FALSE) == FAIL)
1645 mod_bot = MAXLNUM;
1646 else
1647 bot_start = wp->w_height + xtra_rows;
1650 else if (xtra_rows > 0)
1652 /* May scroll text down. If there is not enough
1653 * remaining text of scrolling fails, must redraw the
1654 * rest. */
1655 if (row + xtra_rows >= wp->w_height - 2)
1656 mod_bot = MAXLNUM;
1657 else
1659 check_for_delay(FALSE);
1660 if (win_ins_lines(wp, row + old_rows,
1661 xtra_rows, FALSE, FALSE) == FAIL)
1662 mod_bot = MAXLNUM;
1663 else if (top_end > row + old_rows)
1664 /* Scrolled the part at the top that requires
1665 * updating down. */
1666 top_end += xtra_rows;
1670 /* When not updating the rest, may need to move w_lines[]
1671 * entries. */
1672 if (mod_bot != MAXLNUM && i != j)
1674 if (j < i)
1676 int x = row + new_rows;
1678 /* move entries in w_lines[] upwards */
1679 for (;;)
1681 /* stop at last valid entry in w_lines[] */
1682 if (i >= wp->w_lines_valid)
1684 wp->w_lines_valid = j;
1685 break;
1687 wp->w_lines[j] = wp->w_lines[i];
1688 /* stop at a line that won't fit */
1689 if (x + (int)wp->w_lines[j].wl_size
1690 > wp->w_height)
1692 wp->w_lines_valid = j + 1;
1693 break;
1695 x += wp->w_lines[j++].wl_size;
1696 ++i;
1698 if (bot_start > x)
1699 bot_start = x;
1701 else /* j > i */
1703 /* move entries in w_lines[] downwards */
1704 j -= i;
1705 wp->w_lines_valid += j;
1706 if (wp->w_lines_valid > wp->w_height)
1707 wp->w_lines_valid = wp->w_height;
1708 for (i = wp->w_lines_valid; i - j >= idx; --i)
1709 wp->w_lines[i] = wp->w_lines[i - j];
1711 /* The w_lines[] entries for inserted lines are
1712 * now invalid, but wl_size may be used above.
1713 * Reset to zero. */
1714 while (i >= idx)
1716 wp->w_lines[i].wl_size = 0;
1717 wp->w_lines[i--].wl_valid = FALSE;
1724 #ifdef FEAT_FOLDING
1726 * When lines are folded, display one line for all of them.
1727 * Otherwise, display normally (can be several display lines when
1728 * 'wrap' is on).
1730 fold_count = foldedCount(wp, lnum, &win_foldinfo);
1731 if (fold_count != 0)
1733 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
1734 ++row;
1735 --fold_count;
1736 wp->w_lines[idx].wl_folded = TRUE;
1737 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
1738 # ifdef FEAT_SYN_HL
1739 did_update = DID_FOLD;
1740 # endif
1742 else
1743 #endif
1744 if (idx < wp->w_lines_valid
1745 && wp->w_lines[idx].wl_valid
1746 && wp->w_lines[idx].wl_lnum == lnum
1747 && lnum > wp->w_topline
1748 && !(dy_flags & DY_LASTLINE)
1749 && srow + wp->w_lines[idx].wl_size > wp->w_height
1750 #ifdef FEAT_DIFF
1751 && diff_check_fill(wp, lnum) == 0
1752 #endif
1755 /* This line is not going to fit. Don't draw anything here,
1756 * will draw "@ " lines below. */
1757 row = wp->w_height + 1;
1759 else
1761 #ifdef FEAT_SEARCH_EXTRA
1762 prepare_search_hl(wp, lnum);
1763 #endif
1764 #ifdef FEAT_SYN_HL
1765 /* Let the syntax stuff know we skipped a few lines. */
1766 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
1767 && syntax_present(buf))
1768 syntax_end_parsing(syntax_last_parsed + 1);
1769 #endif
1772 * Display one line.
1774 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
1776 #ifdef FEAT_FOLDING
1777 wp->w_lines[idx].wl_folded = FALSE;
1778 wp->w_lines[idx].wl_lastlnum = lnum;
1779 #endif
1780 #ifdef FEAT_SYN_HL
1781 did_update = DID_LINE;
1782 syntax_last_parsed = lnum;
1783 #endif
1786 wp->w_lines[idx].wl_lnum = lnum;
1787 wp->w_lines[idx].wl_valid = TRUE;
1788 if (row > wp->w_height) /* past end of screen */
1790 /* we may need the size of that too long line later on */
1791 if (dollar_vcol == 0)
1792 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
1793 ++idx;
1794 break;
1796 if (dollar_vcol == 0)
1797 wp->w_lines[idx].wl_size = row - srow;
1798 ++idx;
1799 #ifdef FEAT_FOLDING
1800 lnum += fold_count + 1;
1801 #else
1802 ++lnum;
1803 #endif
1805 else
1807 /* This line does not need updating, advance to the next one */
1808 row += wp->w_lines[idx++].wl_size;
1809 if (row > wp->w_height) /* past end of screen */
1810 break;
1811 #ifdef FEAT_FOLDING
1812 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
1813 #else
1814 ++lnum;
1815 #endif
1816 #ifdef FEAT_SYN_HL
1817 did_update = DID_NONE;
1818 #endif
1821 if (lnum > buf->b_ml.ml_line_count)
1823 eof = TRUE;
1824 break;
1828 * End of loop over all window lines.
1832 if (idx > wp->w_lines_valid)
1833 wp->w_lines_valid = idx;
1835 #ifdef FEAT_SYN_HL
1837 * Let the syntax stuff know we stop parsing here.
1839 if (syntax_last_parsed != 0 && syntax_present(buf))
1840 syntax_end_parsing(syntax_last_parsed + 1);
1841 #endif
1844 * If we didn't hit the end of the file, and we didn't finish the last
1845 * line we were working on, then the line didn't fit.
1847 wp->w_empty_rows = 0;
1848 #ifdef FEAT_DIFF
1849 wp->w_filler_rows = 0;
1850 #endif
1851 if (!eof && !didline)
1853 if (lnum == wp->w_topline)
1856 * Single line that does not fit!
1857 * Don't overwrite it, it can be edited.
1859 wp->w_botline = lnum + 1;
1861 #ifdef FEAT_DIFF
1862 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
1864 /* Window ends in filler lines. */
1865 wp->w_botline = lnum;
1866 wp->w_filler_rows = wp->w_height - srow;
1868 #endif
1869 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
1872 * Last line isn't finished: Display "@@@" at the end.
1874 screen_fill(W_WINROW(wp) + wp->w_height - 1,
1875 W_WINROW(wp) + wp->w_height,
1876 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
1877 '@', '@', hl_attr(HLF_AT));
1878 set_empty_rows(wp, srow);
1879 wp->w_botline = lnum;
1881 else
1883 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
1884 wp->w_botline = lnum;
1887 else
1889 #ifdef FEAT_VERTSPLIT
1890 draw_vsep_win(wp, row);
1891 #endif
1892 if (eof) /* we hit the end of the file */
1894 wp->w_botline = buf->b_ml.ml_line_count + 1;
1895 #ifdef FEAT_DIFF
1896 j = diff_check_fill(wp, wp->w_botline);
1897 if (j > 0 && !wp->w_botfill)
1900 * Display filler lines at the end of the file
1902 if (char2cells(fill_diff) > 1)
1903 i = '-';
1904 else
1905 i = fill_diff;
1906 if (row + j > wp->w_height)
1907 j = wp->w_height - row;
1908 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
1909 row += j;
1911 #endif
1913 else if (dollar_vcol == 0)
1914 wp->w_botline = lnum;
1916 /* make sure the rest of the screen is blank */
1917 /* put '~'s on rows that aren't part of the file. */
1918 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_AT);
1921 /* Reset the type of redrawing required, the window has been updated. */
1922 wp->w_redr_type = 0;
1923 #ifdef FEAT_DIFF
1924 wp->w_old_topfill = wp->w_topfill;
1925 wp->w_old_botfill = wp->w_botfill;
1926 #endif
1928 if (dollar_vcol == 0)
1931 * There is a trick with w_botline. If we invalidate it on each
1932 * change that might modify it, this will cause a lot of expensive
1933 * calls to plines() in update_topline() each time. Therefore the
1934 * value of w_botline is often approximated, and this value is used to
1935 * compute the value of w_topline. If the value of w_botline was
1936 * wrong, check that the value of w_topline is correct (cursor is on
1937 * the visible part of the text). If it's not, we need to redraw
1938 * again. Mostly this just means scrolling up a few lines, so it
1939 * doesn't look too bad. Only do this for the current window (where
1940 * changes are relevant).
1942 wp->w_valid |= VALID_BOTLINE;
1943 if (wp == curwin && wp->w_botline != old_botline && !recursive)
1945 recursive = TRUE;
1946 curwin->w_valid &= ~VALID_TOPLINE;
1947 update_topline(); /* may invalidate w_botline again */
1948 if (must_redraw != 0)
1950 /* Don't update for changes in buffer again. */
1951 i = curbuf->b_mod_set;
1952 curbuf->b_mod_set = FALSE;
1953 win_update(curwin);
1954 must_redraw = 0;
1955 curbuf->b_mod_set = i;
1957 recursive = FALSE;
1961 #if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1962 /* restore got_int, unless CTRL-C was hit while redrawing */
1963 if (!got_int)
1964 got_int = save_got_int;
1965 #endif
1968 #ifdef FEAT_SIGNS
1969 static int draw_signcolumn __ARGS((win_T *wp));
1972 * Return TRUE when window "wp" has a column to draw signs in.
1974 static int
1975 draw_signcolumn(wp)
1976 win_T *wp;
1978 return (wp->w_buffer->b_signlist != NULL
1979 # ifdef FEAT_NETBEANS_INTG
1980 || usingNetbeans
1981 # endif
1984 #endif
1987 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
1988 * as the filler character.
1990 static void
1991 win_draw_end(wp, c1, c2, row, endrow, hl)
1992 win_T *wp;
1993 int c1;
1994 int c2;
1995 int row;
1996 int endrow;
1997 hlf_T hl;
1999 #if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2000 int n = 0;
2001 # define FDC_OFF n
2002 #else
2003 # define FDC_OFF 0
2004 #endif
2006 #ifdef FEAT_RIGHTLEFT
2007 if (wp->w_p_rl)
2009 /* No check for cmdline window: should never be right-left. */
2010 # ifdef FEAT_FOLDING
2011 n = wp->w_p_fdc;
2013 if (n > 0)
2015 /* draw the fold column at the right */
2016 if (n > W_WIDTH(wp))
2017 n = W_WIDTH(wp);
2018 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2019 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
2020 ' ', ' ', hl_attr(HLF_FC));
2022 # endif
2023 # ifdef FEAT_SIGNS
2024 if (draw_signcolumn(wp))
2026 int nn = n + 2;
2028 /* draw the sign column left of the fold column */
2029 if (nn > W_WIDTH(wp))
2030 nn = W_WIDTH(wp);
2031 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2032 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
2033 ' ', ' ', hl_attr(HLF_SC));
2034 n = nn;
2036 # endif
2037 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2038 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
2039 c2, c2, hl_attr(hl));
2040 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2041 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
2042 c1, c2, hl_attr(hl));
2044 else
2045 #endif
2047 #ifdef FEAT_CMDWIN
2048 if (cmdwin_type != 0 && wp == curwin)
2050 /* draw the cmdline character in the leftmost column */
2051 n = 1;
2052 if (n > wp->w_width)
2053 n = wp->w_width;
2054 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2055 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
2056 cmdwin_type, ' ', hl_attr(HLF_AT));
2058 #endif
2059 #ifdef FEAT_FOLDING
2060 if (wp->w_p_fdc > 0)
2062 int nn = n + wp->w_p_fdc;
2064 /* draw the fold column at the left */
2065 if (nn > W_WIDTH(wp))
2066 nn = W_WIDTH(wp);
2067 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2068 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2069 ' ', ' ', hl_attr(HLF_FC));
2070 n = nn;
2072 #endif
2073 #ifdef FEAT_SIGNS
2074 if (draw_signcolumn(wp))
2076 int nn = n + 2;
2078 /* draw the sign column after the fold column */
2079 if (nn > W_WIDTH(wp))
2080 nn = W_WIDTH(wp);
2081 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2082 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2083 ' ', ' ', hl_attr(HLF_SC));
2084 n = nn;
2086 #endif
2087 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2088 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
2089 c1, c2, hl_attr(hl));
2091 set_empty_rows(wp, row);
2094 #ifdef FEAT_FOLDING
2096 * Display one folded line.
2098 static void
2099 fold_line(wp, fold_count, foldinfo, lnum, row)
2100 win_T *wp;
2101 long fold_count;
2102 foldinfo_T *foldinfo;
2103 linenr_T lnum;
2104 int row;
2106 char_u buf[51];
2107 pos_T *top, *bot;
2108 linenr_T lnume = lnum + fold_count - 1;
2109 int len;
2110 char_u *text;
2111 int fdc;
2112 int col;
2113 int txtcol;
2114 int off = (int)(current_ScreenLine - ScreenLines);
2115 int ri;
2117 /* Build the fold line:
2118 * 1. Add the cmdwin_type for the command-line window
2119 * 2. Add the 'foldcolumn'
2120 * 3. Add the 'number' column
2121 * 4. Compose the text
2122 * 5. Add the text
2123 * 6. set highlighting for the Visual area an other text
2125 col = 0;
2128 * 1. Add the cmdwin_type for the command-line window
2129 * Ignores 'rightleft', this window is never right-left.
2131 #ifdef FEAT_CMDWIN
2132 if (cmdwin_type != 0 && wp == curwin)
2134 ScreenLines[off] = cmdwin_type;
2135 ScreenAttrs[off] = hl_attr(HLF_AT);
2136 #ifdef FEAT_MBYTE
2137 if (enc_utf8)
2138 ScreenLinesUC[off] = 0;
2139 #endif
2140 ++col;
2142 #endif
2145 * 2. Add the 'foldcolumn'
2147 fdc = wp->w_p_fdc;
2148 if (fdc > W_WIDTH(wp) - col)
2149 fdc = W_WIDTH(wp) - col;
2150 if (fdc > 0)
2152 fill_foldcolumn(buf, wp, TRUE, lnum);
2153 #ifdef FEAT_RIGHTLEFT
2154 if (wp->w_p_rl)
2156 int i;
2158 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
2159 hl_attr(HLF_FC));
2160 /* reverse the fold column */
2161 for (i = 0; i < fdc; ++i)
2162 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2164 else
2165 #endif
2166 copy_text_attr(off + col, buf, fdc, hl_attr(HLF_FC));
2167 col += fdc;
2170 #ifdef FEAT_RIGHTLEFT
2171 # define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2172 for (ri = 0; ri < l; ++ri) \
2173 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2174 else \
2175 for (ri = 0; ri < l; ++ri) \
2176 ScreenAttrs[off + (p) + ri] = v
2177 #else
2178 # define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2179 ScreenAttrs[off + (p) + ri] = v
2180 #endif
2182 /* Set all attributes of the 'number' column and the text */
2183 RL_MEMSET(col, hl_attr(HLF_FL), W_WIDTH(wp) - col);
2185 #ifdef FEAT_SIGNS
2186 /* If signs are being displayed, add two spaces. */
2187 if (draw_signcolumn(wp))
2189 len = W_WIDTH(wp) - col;
2190 if (len > 0)
2192 if (len > 2)
2193 len = 2;
2194 # ifdef FEAT_RIGHTLEFT
2195 if (wp->w_p_rl)
2196 /* the line number isn't reversed */
2197 copy_text_attr(off + W_WIDTH(wp) - len - col,
2198 (char_u *)" ", len, hl_attr(HLF_FL));
2199 else
2200 # endif
2201 copy_text_attr(off + col, (char_u *)" ", len, hl_attr(HLF_FL));
2202 col += len;
2205 #endif
2208 * 3. Add the 'number' column
2210 if (wp->w_p_nu)
2212 len = W_WIDTH(wp) - col;
2213 if (len > 0)
2215 int w = number_width(wp);
2217 if (len > w + 1)
2218 len = w + 1;
2219 sprintf((char *)buf, "%*ld ", w, (long)lnum);
2220 #ifdef FEAT_RIGHTLEFT
2221 if (wp->w_p_rl)
2222 /* the line number isn't reversed */
2223 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
2224 hl_attr(HLF_FL));
2225 else
2226 #endif
2227 copy_text_attr(off + col, buf, len, hl_attr(HLF_FL));
2228 col += len;
2233 * 4. Compose the folded-line string with 'foldtext', if set.
2235 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
2237 txtcol = col; /* remember where text starts */
2240 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2241 * Right-left text is put in columns 0 - number-col, normal text is put
2242 * in columns number-col - window-width.
2244 #ifdef FEAT_MBYTE
2245 if (has_mbyte)
2247 int cells;
2248 int u8c, u8cc[MAX_MCO];
2249 int i;
2250 int idx;
2251 int c_len;
2252 char_u *p;
2253 # ifdef FEAT_ARABIC
2254 int prev_c = 0; /* previous Arabic character */
2255 int prev_c1 = 0; /* first composing char for prev_c */
2256 # endif
2258 # ifdef FEAT_RIGHTLEFT
2259 if (wp->w_p_rl)
2260 idx = off;
2261 else
2262 # endif
2263 idx = off + col;
2265 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2266 for (p = text; *p != NUL; )
2268 cells = (*mb_ptr2cells)(p);
2269 c_len = (*mb_ptr2len)(p);
2270 if (col + cells > W_WIDTH(wp)
2271 # ifdef FEAT_RIGHTLEFT
2272 - (wp->w_p_rl ? col : 0)
2273 # endif
2275 break;
2276 ScreenLines[idx] = *p;
2277 if (enc_utf8)
2279 u8c = utfc_ptr2char(p, u8cc);
2280 if (*p < 0x80 && u8cc[0] == 0)
2282 ScreenLinesUC[idx] = 0;
2283 #ifdef FEAT_ARABIC
2284 prev_c = u8c;
2285 #endif
2287 else
2289 #ifdef FEAT_ARABIC
2290 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2292 /* Do Arabic shaping. */
2293 int pc, pc1, nc;
2294 int pcc[MAX_MCO];
2295 int firstbyte = *p;
2297 /* The idea of what is the previous and next
2298 * character depends on 'rightleft'. */
2299 if (wp->w_p_rl)
2301 pc = prev_c;
2302 pc1 = prev_c1;
2303 nc = utf_ptr2char(p + c_len);
2304 prev_c1 = u8cc[0];
2306 else
2308 pc = utfc_ptr2char(p + c_len, pcc);
2309 nc = prev_c;
2310 pc1 = pcc[0];
2312 prev_c = u8c;
2314 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2315 pc, pc1, nc);
2316 ScreenLines[idx] = firstbyte;
2318 else
2319 prev_c = u8c;
2320 #endif
2321 /* Non-BMP character: display as ? or fullwidth ?. */
2322 #ifdef UNICODE16
2323 if (u8c >= 0x10000)
2324 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2325 else
2326 #endif
2327 ScreenLinesUC[idx] = u8c;
2328 for (i = 0; i < Screen_mco; ++i)
2330 ScreenLinesC[i][idx] = u8cc[i];
2331 if (u8cc[i] == 0)
2332 break;
2335 if (cells > 1)
2336 ScreenLines[idx + 1] = 0;
2338 else if (cells > 1) /* double-byte character */
2340 if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2341 ScreenLines2[idx] = p[1];
2342 else
2343 ScreenLines[idx + 1] = p[1];
2345 col += cells;
2346 idx += cells;
2347 p += c_len;
2350 else
2351 #endif
2353 len = (int)STRLEN(text);
2354 if (len > W_WIDTH(wp) - col)
2355 len = W_WIDTH(wp) - col;
2356 if (len > 0)
2358 #ifdef FEAT_RIGHTLEFT
2359 if (wp->w_p_rl)
2360 STRNCPY(current_ScreenLine, text, len);
2361 else
2362 #endif
2363 STRNCPY(current_ScreenLine + col, text, len);
2364 col += len;
2368 /* Fill the rest of the line with the fold filler */
2369 #ifdef FEAT_RIGHTLEFT
2370 if (wp->w_p_rl)
2371 col -= txtcol;
2372 #endif
2373 while (col < W_WIDTH(wp)
2374 #ifdef FEAT_RIGHTLEFT
2375 - (wp->w_p_rl ? txtcol : 0)
2376 #endif
2379 #ifdef FEAT_MBYTE
2380 if (enc_utf8)
2382 if (fill_fold >= 0x80)
2384 ScreenLinesUC[off + col] = fill_fold;
2385 ScreenLinesC[0][off + col] = 0;
2387 else
2388 ScreenLinesUC[off + col] = 0;
2390 #endif
2391 ScreenLines[off + col++] = fill_fold;
2394 if (text != buf)
2395 vim_free(text);
2398 * 6. set highlighting for the Visual area an other text.
2399 * If all folded lines are in the Visual area, highlight the line.
2401 #ifdef FEAT_VISUAL
2402 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2404 if (ltoreq(curwin->w_cursor, VIsual))
2406 /* Visual is after curwin->w_cursor */
2407 top = &curwin->w_cursor;
2408 bot = &VIsual;
2410 else
2412 /* Visual is before curwin->w_cursor */
2413 top = &VIsual;
2414 bot = &curwin->w_cursor;
2416 if (lnum >= top->lnum
2417 && lnume <= bot->lnum
2418 && (VIsual_mode != 'v'
2419 || ((lnum > top->lnum
2420 || (lnum == top->lnum
2421 && top->col == 0))
2422 && (lnume < bot->lnum
2423 || (lnume == bot->lnum
2424 && (bot->col - (*p_sel == 'e'))
2425 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
2427 if (VIsual_mode == Ctrl_V)
2429 /* Visual block mode: highlight the chars part of the block */
2430 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2432 if (wp->w_old_cursor_lcol + txtcol < (colnr_T)W_WIDTH(wp))
2433 len = wp->w_old_cursor_lcol;
2434 else
2435 len = W_WIDTH(wp) - txtcol;
2436 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, hl_attr(HLF_V),
2437 len - (int)wp->w_old_cursor_fcol);
2440 else
2442 /* Set all attributes of the text */
2443 RL_MEMSET(txtcol, hl_attr(HLF_V), W_WIDTH(wp) - txtcol);
2447 #endif
2449 #ifdef FEAT_SYN_HL
2450 /* Show 'cursorcolumn' in the fold line. */
2451 if (wp->w_p_cuc)
2453 txtcol += wp->w_virtcol;
2454 if (wp->w_p_wrap)
2455 txtcol -= wp->w_skipcol;
2456 else
2457 txtcol -= wp->w_leftcol;
2458 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2459 ScreenAttrs[off + txtcol] = hl_combine_attr(
2460 ScreenAttrs[off + txtcol], hl_attr(HLF_CUC));
2462 #endif
2464 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2465 (int)W_WIDTH(wp), FALSE);
2468 * Update w_cline_height and w_cline_folded if the cursor line was
2469 * updated (saves a call to plines() later).
2471 if (wp == curwin
2472 && lnum <= curwin->w_cursor.lnum
2473 && lnume >= curwin->w_cursor.lnum)
2475 curwin->w_cline_row = row;
2476 curwin->w_cline_height = 1;
2477 curwin->w_cline_folded = TRUE;
2478 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2483 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2485 static void
2486 copy_text_attr(off, buf, len, attr)
2487 int off;
2488 char_u *buf;
2489 int len;
2490 int attr;
2492 int i;
2494 mch_memmove(ScreenLines + off, buf, (size_t)len);
2495 # ifdef FEAT_MBYTE
2496 if (enc_utf8)
2497 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2498 # endif
2499 for (i = 0; i < len; ++i)
2500 ScreenAttrs[off + i] = attr;
2504 * Fill the foldcolumn at "p" for window "wp".
2505 * Only to be called when 'foldcolumn' > 0.
2507 static void
2508 fill_foldcolumn(p, wp, closed, lnum)
2509 char_u *p;
2510 win_T *wp;
2511 int closed; /* TRUE of FALSE */
2512 linenr_T lnum; /* current line number */
2514 int i = 0;
2515 int level;
2516 int first_level;
2517 int empty;
2519 /* Init to all spaces. */
2520 copy_spaces(p, (size_t)wp->w_p_fdc);
2522 level = win_foldinfo.fi_level;
2523 if (level > 0)
2525 /* If there is only one column put more info in it. */
2526 empty = (wp->w_p_fdc == 1) ? 0 : 1;
2528 /* If the column is too narrow, we start at the lowest level that
2529 * fits and use numbers to indicated the depth. */
2530 first_level = level - wp->w_p_fdc - closed + 1 + empty;
2531 if (first_level < 1)
2532 first_level = 1;
2534 for (i = 0; i + empty < wp->w_p_fdc; ++i)
2536 if (win_foldinfo.fi_lnum == lnum
2537 && first_level + i >= win_foldinfo.fi_low_level)
2538 p[i] = '-';
2539 else if (first_level == 1)
2540 p[i] = '|';
2541 else if (first_level + i <= 9)
2542 p[i] = '0' + first_level + i;
2543 else
2544 p[i] = '>';
2545 if (first_level + i == level)
2546 break;
2549 if (closed)
2550 p[i >= wp->w_p_fdc ? i - 1 : i] = '+';
2552 #endif /* FEAT_FOLDING */
2555 * Display line "lnum" of window 'wp' on the screen.
2556 * Start at row "startrow", stop when "endrow" is reached.
2557 * wp->w_virtcol needs to be valid.
2559 * Return the number of last row the line occupies.
2561 static int
2562 win_line(wp, lnum, startrow, endrow, nochange)
2563 win_T *wp;
2564 linenr_T lnum;
2565 int startrow;
2566 int endrow;
2567 int nochange UNUSED; /* not updating for changed text */
2569 int col; /* visual column on screen */
2570 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2571 int c = 0; /* init for GCC */
2572 long vcol = 0; /* virtual column (for tabs) */
2573 long vcol_prev = -1; /* "vcol" of previous character */
2574 char_u *line; /* current line */
2575 char_u *ptr; /* current position in "line" */
2576 int row; /* row in the window, excl w_winrow */
2577 int screen_row; /* row on the screen, incl w_winrow */
2579 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2580 int n_extra = 0; /* number of extra chars */
2581 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
2582 int c_extra = NUL; /* extra chars, all the same */
2583 int extra_attr = 0; /* attributes when n_extra != 0 */
2584 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2585 displaying lcs_eol at end-of-line */
2586 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2587 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2589 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2590 int saved_n_extra = 0;
2591 char_u *saved_p_extra = NULL;
2592 int saved_c_extra = 0;
2593 int saved_char_attr = 0;
2595 int n_attr = 0; /* chars with special attr */
2596 int saved_attr2 = 0; /* char_attr saved for n_attr */
2597 int n_attr3 = 0; /* chars with overruling special attr */
2598 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2600 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2602 int fromcol, tocol; /* start/end of inverting */
2603 int fromcol_prev = -2; /* start of inverting after cursor */
2604 int noinvcur = FALSE; /* don't invert the cursor */
2605 #ifdef FEAT_VISUAL
2606 pos_T *top, *bot;
2607 int lnum_in_visual_area = FALSE;
2608 #endif
2609 pos_T pos;
2610 long v;
2612 int char_attr = 0; /* attributes for next character */
2613 int attr_pri = FALSE; /* char_attr has priority */
2614 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2615 in this line */
2616 int attr = 0; /* attributes for area highlighting */
2617 int area_attr = 0; /* attributes desired by highlighting */
2618 int search_attr = 0; /* attributes desired by 'hlsearch' */
2619 #ifdef FEAT_SYN_HL
2620 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
2621 int syntax_attr = 0; /* attributes desired by syntax */
2622 int has_syntax = FALSE; /* this buffer has syntax highl. */
2623 int save_did_emsg;
2624 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
2625 #endif
2626 #ifdef FEAT_SPELL
2627 int has_spell = FALSE; /* this buffer has spell checking */
2628 # define SPWORDLEN 150
2629 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
2630 int nextlinecol = 0; /* column where nextline[] starts */
2631 int nextline_idx = 0; /* index in nextline[] where next line
2632 starts */
2633 int spell_attr = 0; /* attributes desired by spelling */
2634 int word_end = 0; /* last byte with same spell_attr */
2635 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2636 static int checked_col = 0; /* column in "checked_lnum" up to which
2637 * there are no spell errors */
2638 static int cap_col = -1; /* column to check for Cap word */
2639 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
2640 int cur_checked_col = 0; /* checked column for current line */
2641 #endif
2642 int extra_check; /* has syntax or linebreak */
2643 #ifdef FEAT_MBYTE
2644 int multi_attr = 0; /* attributes desired by multibyte */
2645 int mb_l = 1; /* multi-byte byte length */
2646 int mb_c = 0; /* decoded multi-byte character */
2647 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
2648 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
2649 #endif
2650 #ifdef FEAT_DIFF
2651 int filler_lines; /* nr of filler lines to be drawn */
2652 int filler_todo; /* nr of filler lines still to do + 1 */
2653 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
2654 int change_start = MAXCOL; /* first col of changed area */
2655 int change_end = -1; /* last col of changed area */
2656 #endif
2657 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
2658 #ifdef FEAT_LINEBREAK
2659 int need_showbreak = FALSE;
2660 #endif
2661 #if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
2662 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
2663 # define LINE_ATTR
2664 int line_attr = 0; /* attribute for the whole line */
2665 #endif
2666 #ifdef FEAT_SEARCH_EXTRA
2667 matchitem_T *cur; /* points to the match list */
2668 match_T *shl; /* points to search_hl or a match */
2669 int shl_flag; /* flag to indicate whether search_hl
2670 has been processed or not */
2671 int prevcol_hl_flag; /* flag to indicate whether prevcol
2672 equals startcol of search_hl or one
2673 of the matches */
2674 #endif
2675 #ifdef FEAT_ARABIC
2676 int prev_c = 0; /* previous Arabic character */
2677 int prev_c1 = 0; /* first composing char for prev_c */
2678 #endif
2679 #if defined(LINE_ATTR)
2680 int did_line_attr = 0;
2681 #endif
2683 /* draw_state: items that are drawn in sequence: */
2684 #define WL_START 0 /* nothing done yet */
2685 #ifdef FEAT_CMDWIN
2686 # define WL_CMDLINE WL_START + 1 /* cmdline window column */
2687 #else
2688 # define WL_CMDLINE WL_START
2689 #endif
2690 #ifdef FEAT_FOLDING
2691 # define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
2692 #else
2693 # define WL_FOLD WL_CMDLINE
2694 #endif
2695 #ifdef FEAT_SIGNS
2696 # define WL_SIGN WL_FOLD + 1 /* column for signs */
2697 #else
2698 # define WL_SIGN WL_FOLD /* column for signs */
2699 #endif
2700 #define WL_NR WL_SIGN + 1 /* line number */
2701 #ifdef FEAT_LINEBREAK
2702 # define WL_BRI WL_NR + 1 /* 'breakindent' */
2703 #else
2704 # define WL_BRI WL_NR
2705 #endif
2706 #if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
2707 # define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
2708 #else
2709 # define WL_SBR WL_BRI
2710 #endif
2711 #define WL_LINE WL_SBR + 1 /* text in the line */
2712 int draw_state = WL_START; /* what to draw next */
2713 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2714 int feedback_col = 0;
2715 int feedback_old_attr = -1;
2716 #endif
2719 if (startrow > endrow) /* past the end already! */
2720 return startrow;
2722 row = startrow;
2723 screen_row = row + W_WINROW(wp);
2726 * To speed up the loop below, set extra_check when there is linebreak,
2727 * trailing white space and/or syntax processing to be done.
2729 #ifdef FEAT_LINEBREAK
2730 extra_check = wp->w_p_lbr;
2731 #else
2732 extra_check = 0;
2733 #endif
2734 #ifdef FEAT_SYN_HL
2735 if (syntax_present(wp->w_buffer) && !wp->w_buffer->b_syn_error)
2737 /* Prepare for syntax highlighting in this line. When there is an
2738 * error, stop syntax highlighting. */
2739 save_did_emsg = did_emsg;
2740 did_emsg = FALSE;
2741 syntax_start(wp, lnum);
2742 if (did_emsg)
2743 wp->w_buffer->b_syn_error = TRUE;
2744 else
2746 did_emsg = save_did_emsg;
2747 has_syntax = TRUE;
2748 extra_check = TRUE;
2751 #endif
2753 #ifdef FEAT_SPELL
2754 if (wp->w_p_spell
2755 && *wp->w_buffer->b_p_spl != NUL
2756 && wp->w_buffer->b_langp.ga_len > 0
2757 && *(char **)(wp->w_buffer->b_langp.ga_data) != NULL)
2759 /* Prepare for spell checking. */
2760 has_spell = TRUE;
2761 extra_check = TRUE;
2763 /* Get the start of the next line, so that words that wrap to the next
2764 * line are found too: "et<line-break>al.".
2765 * Trick: skip a few chars for C/shell/Vim comments */
2766 nextline[SPWORDLEN] = NUL;
2767 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2769 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
2770 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
2773 /* When a word wrapped from the previous line the start of the current
2774 * line is valid. */
2775 if (lnum == checked_lnum)
2776 cur_checked_col = checked_col;
2777 checked_lnum = 0;
2779 /* When there was a sentence end in the previous line may require a
2780 * word starting with capital in this line. In line 1 always check
2781 * the first word. */
2782 if (lnum != capcol_lnum)
2783 cap_col = -1;
2784 if (lnum == 1)
2785 cap_col = 0;
2786 capcol_lnum = 0;
2788 #endif
2791 * handle visual active in this window
2793 fromcol = -10;
2794 tocol = MAXCOL;
2795 #ifdef FEAT_VISUAL
2796 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2798 /* Visual is after curwin->w_cursor */
2799 if (ltoreq(curwin->w_cursor, VIsual))
2801 top = &curwin->w_cursor;
2802 bot = &VIsual;
2804 else /* Visual is before curwin->w_cursor */
2806 top = &VIsual;
2807 bot = &curwin->w_cursor;
2809 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
2810 if (VIsual_mode == Ctrl_V) /* block mode */
2812 if (lnum_in_visual_area)
2814 fromcol = wp->w_old_cursor_fcol;
2815 tocol = wp->w_old_cursor_lcol;
2818 else /* non-block mode */
2820 if (lnum > top->lnum && lnum <= bot->lnum)
2821 fromcol = 0;
2822 else if (lnum == top->lnum)
2824 if (VIsual_mode == 'V') /* linewise */
2825 fromcol = 0;
2826 else
2828 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
2829 if (gchar_pos(top) == NUL)
2830 tocol = fromcol + 1;
2833 if (VIsual_mode != 'V' && lnum == bot->lnum)
2835 if (*p_sel == 'e' && bot->col == 0
2836 #ifdef FEAT_VIRTUALEDIT
2837 && bot->coladd == 0
2838 #endif
2841 fromcol = -10;
2842 tocol = MAXCOL;
2844 else if (bot->col == MAXCOL)
2845 tocol = MAXCOL;
2846 else
2848 pos = *bot;
2849 if (*p_sel == 'e')
2850 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
2851 else
2853 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
2854 ++tocol;
2860 #ifndef MSDOS
2861 /* Check if the character under the cursor should not be inverted */
2862 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
2863 # ifdef FEAT_GUI
2864 && !gui.in_use
2865 # endif
2867 noinvcur = TRUE;
2868 #endif
2870 /* if inverting in this line set area_highlighting */
2871 if (fromcol >= 0)
2873 area_highlighting = TRUE;
2874 attr = hl_attr(HLF_V);
2875 #if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
2876 if (clip_star.available && !clip_star.owned && clip_isautosel())
2877 attr = hl_attr(HLF_VNC);
2878 #endif
2883 * handle 'incsearch' and ":s///c" highlighting
2885 else
2886 #endif /* FEAT_VISUAL */
2887 if (highlight_match
2888 && wp == curwin
2889 && lnum >= curwin->w_cursor.lnum
2890 && lnum <= curwin->w_cursor.lnum + search_match_lines)
2892 if (lnum == curwin->w_cursor.lnum)
2893 getvcol(curwin, &(curwin->w_cursor),
2894 (colnr_T *)&fromcol, NULL, NULL);
2895 else
2896 fromcol = 0;
2897 if (lnum == curwin->w_cursor.lnum + search_match_lines)
2899 pos.lnum = lnum;
2900 pos.col = search_match_endcol;
2901 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
2903 else
2904 tocol = MAXCOL;
2905 /* do at least one character; happens when past end of line */
2906 if (fromcol == tocol)
2907 tocol = fromcol + 1;
2908 area_highlighting = TRUE;
2909 attr = hl_attr(HLF_I);
2912 #ifdef FEAT_DIFF
2913 filler_lines = diff_check(wp, lnum);
2914 if (filler_lines < 0)
2916 if (filler_lines == -1)
2918 if (diff_find_change(wp, lnum, &change_start, &change_end))
2919 diff_hlf = HLF_ADD; /* added line */
2920 else if (change_start == 0)
2921 diff_hlf = HLF_TXD; /* changed text */
2922 else
2923 diff_hlf = HLF_CHD; /* changed line */
2925 else
2926 diff_hlf = HLF_ADD; /* added line */
2927 filler_lines = 0;
2928 area_highlighting = TRUE;
2930 if (lnum == wp->w_topline)
2931 filler_lines = wp->w_topfill;
2932 filler_todo = filler_lines;
2933 #endif
2935 #ifdef LINE_ATTR
2936 # ifdef FEAT_SIGNS
2937 /* If this line has a sign with line highlighting set line_attr. */
2938 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
2939 if (v != 0)
2940 line_attr = sign_get_attr((int)v, TRUE);
2941 # endif
2942 # if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
2943 /* Highlight the current line in the quickfix window. */
2944 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
2945 line_attr = hl_attr(HLF_L);
2946 # endif
2947 if (line_attr != 0)
2948 area_highlighting = TRUE;
2949 #endif
2951 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
2952 ptr = line;
2954 #ifdef FEAT_SPELL
2955 if (has_spell)
2957 /* For checking first word with a capital skip white space. */
2958 if (cap_col == 0)
2959 cap_col = (int)(skipwhite(line) - line);
2961 /* To be able to spell-check over line boundaries copy the end of the
2962 * current line into nextline[]. Above the start of the next line was
2963 * copied to nextline[SPWORDLEN]. */
2964 if (nextline[SPWORDLEN] == NUL)
2966 /* No next line or it is empty. */
2967 nextlinecol = MAXCOL;
2968 nextline_idx = 0;
2970 else
2972 v = (long)STRLEN(line);
2973 if (v < SPWORDLEN)
2975 /* Short line, use it completely and append the start of the
2976 * next line. */
2977 nextlinecol = 0;
2978 mch_memmove(nextline, line, (size_t)v);
2979 STRMOVE(nextline + v, nextline + SPWORDLEN);
2980 nextline_idx = v + 1;
2982 else
2984 /* Long line, use only the last SPWORDLEN bytes. */
2985 nextlinecol = v - SPWORDLEN;
2986 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
2987 nextline_idx = SPWORDLEN + 1;
2991 #endif
2993 /* find start of trailing whitespace */
2994 if (wp->w_p_list && lcs_trail)
2996 trailcol = (colnr_T)STRLEN(ptr);
2997 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
2998 --trailcol;
2999 trailcol += (colnr_T) (ptr - line);
3000 extra_check = TRUE;
3004 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3005 * first character to be displayed.
3007 if (wp->w_p_wrap)
3008 v = wp->w_skipcol;
3009 else
3010 v = wp->w_leftcol;
3011 if (v > 0)
3013 #ifdef FEAT_MBYTE
3014 char_u *prev_ptr = ptr;
3015 #endif
3016 while (vcol < v && *ptr != NUL)
3018 c = win_lbr_chartabsize(wp, ptr, (colnr_T)vcol, NULL, lnum);
3019 vcol += c;
3020 #ifdef FEAT_MBYTE
3021 prev_ptr = ptr;
3022 #endif
3023 mb_ptr_adv(ptr);
3026 #if defined(FEAT_SYN_HL) || defined(FEAT_VIRTUALEDIT) || defined(FEAT_VISUAL)
3027 /* When:
3028 * - 'cuc' is set, or
3029 * - 'virtualedit' is set, or
3030 * - the visual mode is active,
3031 * the end of the line may be before the start of the displayed part.
3033 if (vcol < v && (
3034 # ifdef FEAT_SYN_HL
3035 wp->w_p_cuc
3036 # if defined(FEAT_VIRTUALEDIT) || defined(FEAT_VISUAL)
3038 # endif
3039 # endif
3040 # ifdef FEAT_VIRTUALEDIT
3041 virtual_active()
3042 # ifdef FEAT_VISUAL
3044 # endif
3045 # endif
3046 # ifdef FEAT_VISUAL
3047 (VIsual_active && wp->w_buffer == curwin->w_buffer)
3048 # endif
3051 vcol = v;
3053 #endif
3055 /* Handle a character that's not completely on the screen: Put ptr at
3056 * that character but skip the first few screen characters. */
3057 if (vcol > v)
3059 vcol -= c;
3060 #ifdef FEAT_MBYTE
3061 ptr = prev_ptr;
3062 #else
3063 --ptr;
3064 #endif
3065 n_skip = v - vcol;
3069 * Adjust for when the inverted text is before the screen,
3070 * and when the start of the inverted text is before the screen.
3072 if (tocol <= vcol)
3073 fromcol = 0;
3074 else if (fromcol >= 0 && fromcol < vcol)
3075 fromcol = vcol;
3077 #ifdef FEAT_LINEBREAK
3078 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3079 if (wp->w_p_wrap)
3080 need_showbreak = TRUE;
3081 #endif
3082 #ifdef FEAT_SPELL
3083 /* When spell checking a word we need to figure out the start of the
3084 * word and if it's badly spelled or not. */
3085 if (has_spell)
3087 int len;
3088 colnr_T linecol = (colnr_T)(ptr - line);
3089 hlf_T spell_hlf = HLF_COUNT;
3091 pos = wp->w_cursor;
3092 wp->w_cursor.lnum = lnum;
3093 wp->w_cursor.col = linecol;
3094 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
3096 /* spell_move_to() may call ml_get() and make "line" invalid */
3097 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3098 ptr = line + linecol;
3100 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
3102 /* no bad word found at line start, don't check until end of a
3103 * word */
3104 spell_hlf = HLF_COUNT;
3105 word_end = (int)(spell_to_word_end(ptr, wp->w_buffer)
3106 - line + 1);
3108 else
3110 /* bad word found, use attributes until end of word */
3111 word_end = wp->w_cursor.col + len + 1;
3113 /* Turn index into actual attributes. */
3114 if (spell_hlf != HLF_COUNT)
3115 spell_attr = highlight_attr[spell_hlf];
3117 wp->w_cursor = pos;
3119 # ifdef FEAT_SYN_HL
3120 /* Need to restart syntax highlighting for this line. */
3121 if (has_syntax)
3122 syntax_start(wp, lnum);
3123 # endif
3125 #endif
3129 * Correct highlighting for cursor that can't be disabled.
3130 * Avoids having to check this for each character.
3132 if (fromcol >= 0)
3134 if (noinvcur)
3136 if ((colnr_T)fromcol == wp->w_virtcol)
3138 /* highlighting starts at cursor, let it start just after the
3139 * cursor */
3140 fromcol_prev = fromcol;
3141 fromcol = -1;
3143 else if ((colnr_T)fromcol < wp->w_virtcol)
3144 /* restart highlighting after the cursor */
3145 fromcol_prev = wp->w_virtcol;
3147 if (fromcol >= tocol)
3148 fromcol = -1;
3151 #ifdef FEAT_SEARCH_EXTRA
3153 * Handle highlighting the last used search pattern and matches.
3154 * Do this for both search_hl and the match list.
3156 cur = wp->w_match_head;
3157 shl_flag = FALSE;
3158 while (cur != NULL || shl_flag == FALSE)
3160 if (shl_flag == FALSE)
3162 shl = &search_hl;
3163 shl_flag = TRUE;
3165 else
3166 shl = &cur->hl;
3167 shl->startcol = MAXCOL;
3168 shl->endcol = MAXCOL;
3169 shl->attr_cur = 0;
3170 if (shl->rm.regprog != NULL)
3172 v = (long)(ptr - line);
3173 next_search_hl(wp, shl, lnum, (colnr_T)v);
3175 /* Need to get the line again, a multi-line regexp may have made it
3176 * invalid. */
3177 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3178 ptr = line + v;
3180 if (shl->lnum != 0 && shl->lnum <= lnum)
3182 if (shl->lnum == lnum)
3183 shl->startcol = shl->rm.startpos[0].col;
3184 else
3185 shl->startcol = 0;
3186 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3187 - shl->rm.startpos[0].lnum)
3188 shl->endcol = shl->rm.endpos[0].col;
3189 else
3190 shl->endcol = MAXCOL;
3191 /* Highlight one character for an empty match. */
3192 if (shl->startcol == shl->endcol)
3194 #ifdef FEAT_MBYTE
3195 if (has_mbyte && line[shl->endcol] != NUL)
3196 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3197 else
3198 #endif
3199 ++shl->endcol;
3201 if ((long)shl->startcol < v) /* match at leftcol */
3203 shl->attr_cur = shl->attr;
3204 search_attr = shl->attr;
3206 area_highlighting = TRUE;
3209 if (shl != &search_hl && cur != NULL)
3210 cur = cur->next;
3212 #endif
3214 #ifdef FEAT_SYN_HL
3215 /* Cursor line highlighting for 'cursorline'. Not when Visual mode is
3216 * active, because it's not clear what is selected then. */
3217 if (wp->w_p_cul && lnum == wp->w_cursor.lnum && !VIsual_active)
3219 line_attr = hl_attr(HLF_CUL);
3220 area_highlighting = TRUE;
3222 #endif
3224 off = (unsigned)(current_ScreenLine - ScreenLines);
3225 col = 0;
3226 #ifdef FEAT_RIGHTLEFT
3227 if (wp->w_p_rl)
3229 /* Rightleft window: process the text in the normal direction, but put
3230 * it in current_ScreenLine[] from right to left. Start at the
3231 * rightmost column of the window. */
3232 col = W_WIDTH(wp) - 1;
3233 off += col;
3235 #endif
3238 * Repeat for the whole displayed line.
3240 for (;;)
3242 /* Skip this quickly when working on the text. */
3243 if (draw_state != WL_LINE)
3245 #ifdef FEAT_CMDWIN
3246 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3248 draw_state = WL_CMDLINE;
3249 if (cmdwin_type != 0 && wp == curwin)
3251 /* Draw the cmdline character. */
3252 n_extra = 1;
3253 c_extra = cmdwin_type;
3254 char_attr = hl_attr(HLF_AT);
3257 #endif
3259 #ifdef FEAT_FOLDING
3260 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3262 draw_state = WL_FOLD;
3263 if (wp->w_p_fdc > 0)
3265 /* Draw the 'foldcolumn'. */
3266 fill_foldcolumn(extra, wp, FALSE, lnum);
3267 n_extra = wp->w_p_fdc;
3268 p_extra = extra;
3269 p_extra[n_extra] = NUL;
3270 c_extra = NUL;
3271 char_attr = hl_attr(HLF_FC);
3274 #endif
3276 #ifdef FEAT_SIGNS
3277 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3279 draw_state = WL_SIGN;
3280 /* Show the sign column when there are any signs in this
3281 * buffer or when using Netbeans. */
3282 if (draw_signcolumn(wp)
3283 # ifdef FEAT_DIFF
3284 && filler_todo <= 0
3285 # endif
3288 int_u text_sign;
3289 # ifdef FEAT_SIGN_ICONS
3290 int_u icon_sign;
3291 # endif
3293 /* Draw two cells with the sign value or blank. */
3294 c_extra = ' ';
3295 char_attr = hl_attr(HLF_SC);
3296 n_extra = 2;
3298 if (row == startrow)
3300 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3301 SIGN_TEXT);
3302 # ifdef FEAT_SIGN_ICONS
3303 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3304 SIGN_ICON);
3305 if (gui.in_use && icon_sign != 0)
3307 /* Use the image in this position. */
3308 c_extra = SIGN_BYTE;
3309 # ifdef FEAT_NETBEANS_INTG
3310 if (buf_signcount(wp->w_buffer, lnum) > 1)
3311 c_extra = MULTISIGN_BYTE;
3312 # endif
3313 char_attr = icon_sign;
3315 else
3316 # endif
3317 if (text_sign != 0)
3319 p_extra = sign_get_text(text_sign);
3320 if (p_extra != NULL)
3322 c_extra = NUL;
3323 n_extra = (int)STRLEN(p_extra);
3325 char_attr = sign_get_attr(text_sign, FALSE);
3330 #endif
3332 if (draw_state == WL_NR - 1 && n_extra == 0)
3334 draw_state = WL_NR;
3335 /* Display the line number. After the first fill with blanks
3336 * when the 'n' flag isn't in 'cpo' */
3337 if (wp->w_p_nu
3338 && (row == startrow
3339 #ifdef FEAT_DIFF
3340 + filler_lines
3341 #endif
3342 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3344 /* Draw the line number (empty space after wrapping). */
3345 if (row == startrow
3346 #ifdef FEAT_DIFF
3347 + filler_lines
3348 #endif
3351 sprintf((char *)extra, "%*ld ",
3352 number_width(wp), (long)lnum);
3353 if (wp->w_skipcol > 0)
3354 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3355 *p_extra = '-';
3356 #ifdef FEAT_RIGHTLEFT
3357 if (wp->w_p_rl) /* reverse line numbers */
3358 rl_mirror(extra);
3359 #endif
3360 p_extra = extra;
3361 c_extra = NUL;
3363 else
3364 c_extra = ' ';
3365 n_extra = number_width(wp) + 1;
3366 char_attr = hl_attr(HLF_N);
3367 #ifdef FEAT_SYN_HL
3368 /* When 'cursorline' is set highlight the line number of
3369 * the current line differently. */
3370 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3371 char_attr = hl_combine_attr(hl_attr(HLF_CUL), char_attr);
3372 #endif
3376 #ifdef FEAT_LINEBREAK
3377 /* draw 'breakindent': indent wrapped text accordingly */
3378 if (draw_state == WL_BRI -1 && n_extra == 0){
3379 draw_state = WL_BRI;
3380 # ifdef FEAT_DIFF
3381 /* FIXME: handle (filler_todo > 0): or modify showbreak so that ---- lines are shorter by the amount needed? */
3382 # endif
3383 if (wp->w_p_bri && row != startrow){ /* FIXME: what is startrow? Don't we need it as well?? */
3384 p_extra = NUL;
3385 c_extra = ' ';
3386 n_extra = get_breakindent_win(wp,lnum);
3387 char_attr = 0; /* was: hl_attr(HLF_AT); */
3388 /* FIXME: why do we need to adjust vcol if showbreak does not?? */
3389 vcol += n_extra;
3390 /* FIXME: is this relevant here? copied shamelessly from showbreak */
3391 /* Correct end of highlighted area for 'breakindent',
3392 * required when 'linebreak' is also set. */
3393 if (tocol == vcol)
3394 tocol += n_extra;
3397 #endif
3400 #if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3401 if (draw_state == WL_SBR - 1 && n_extra == 0)
3403 draw_state = WL_SBR;
3404 # ifdef FEAT_DIFF
3405 if (filler_todo > 0)
3407 /* Draw "deleted" diff line(s). */
3408 if (char2cells(fill_diff) > 1)
3409 c_extra = '-';
3410 else
3411 c_extra = fill_diff;
3412 # ifdef FEAT_RIGHTLEFT
3413 if (wp->w_p_rl)
3414 n_extra = col + 1;
3415 else
3416 # endif
3417 n_extra = W_WIDTH(wp) - col;
3418 char_attr = hl_attr(HLF_DED);
3420 # endif
3421 # ifdef FEAT_LINEBREAK
3422 if (*p_sbr != NUL && need_showbreak)
3424 /* Draw 'showbreak' at the start of each broken line. */
3425 p_extra = p_sbr;
3426 c_extra = NUL;
3427 n_extra = (int)STRLEN(p_sbr);
3428 char_attr = hl_attr(HLF_AT);
3429 need_showbreak = FALSE;
3430 /* Correct end of highlighted area for 'showbreak',
3431 * required when 'linebreak' is also set. */
3432 if (tocol == vcol)
3433 tocol += n_extra;
3435 # endif
3437 #endif
3439 if (draw_state == WL_LINE - 1 && n_extra == 0)
3441 draw_state = WL_LINE;
3442 if (saved_n_extra)
3444 /* Continue item from end of wrapped line. */
3445 n_extra = saved_n_extra;
3446 c_extra = saved_c_extra;
3447 p_extra = saved_p_extra;
3448 char_attr = saved_char_attr;
3450 else
3451 char_attr = 0;
3455 /* When still displaying '$' of change command, stop at cursor */
3456 if (dollar_vcol != 0 && wp == curwin
3457 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
3458 #ifdef FEAT_DIFF
3459 && filler_todo <= 0
3460 #endif
3463 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3464 wp->w_p_rl);
3465 /* Pretend we have finished updating the window. Except when
3466 * 'cursorcolumn' is set. */
3467 #ifdef FEAT_SYN_HL
3468 if (wp->w_p_cuc)
3469 row = wp->w_cline_row + wp->w_cline_height;
3470 else
3471 #endif
3472 row = wp->w_height;
3473 break;
3476 if (draw_state == WL_LINE && area_highlighting)
3478 /* handle Visual or match highlighting in this line */
3479 if (vcol == fromcol
3480 #ifdef FEAT_MBYTE
3481 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3482 && (*mb_ptr2cells)(ptr) > 1)
3483 #endif
3484 || ((int)vcol_prev == fromcol_prev
3485 && vcol_prev < vcol /* not at margin */
3486 && vcol < tocol))
3487 area_attr = attr; /* start highlighting */
3488 else if (area_attr != 0
3489 && (vcol == tocol
3490 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
3491 area_attr = 0; /* stop highlighting */
3493 #ifdef FEAT_SEARCH_EXTRA
3494 if (!n_extra)
3497 * Check for start/end of search pattern match.
3498 * After end, check for start/end of next match.
3499 * When another match, have to check for start again.
3500 * Watch out for matching an empty string!
3501 * Do this for 'search_hl' and the match list (ordered by
3502 * priority).
3504 v = (long)(ptr - line);
3505 cur = wp->w_match_head;
3506 shl_flag = FALSE;
3507 while (cur != NULL || shl_flag == FALSE)
3509 if (shl_flag == FALSE
3510 && ((cur != NULL
3511 && cur->priority > SEARCH_HL_PRIORITY)
3512 || cur == NULL))
3514 shl = &search_hl;
3515 shl_flag = TRUE;
3517 else
3518 shl = &cur->hl;
3519 while (shl->rm.regprog != NULL)
3521 if (shl->startcol != MAXCOL
3522 && v >= (long)shl->startcol
3523 && v < (long)shl->endcol)
3525 shl->attr_cur = shl->attr;
3527 else if (v == (long)shl->endcol)
3529 shl->attr_cur = 0;
3531 next_search_hl(wp, shl, lnum, (colnr_T)v);
3533 /* Need to get the line again, a multi-line regexp
3534 * may have made it invalid. */
3535 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3536 ptr = line + v;
3538 if (shl->lnum == lnum)
3540 shl->startcol = shl->rm.startpos[0].col;
3541 if (shl->rm.endpos[0].lnum == 0)
3542 shl->endcol = shl->rm.endpos[0].col;
3543 else
3544 shl->endcol = MAXCOL;
3546 if (shl->startcol == shl->endcol)
3548 /* highlight empty match, try again after
3549 * it */
3550 #ifdef FEAT_MBYTE
3551 if (has_mbyte)
3552 shl->endcol += (*mb_ptr2len)(line
3553 + shl->endcol);
3554 else
3555 #endif
3556 ++shl->endcol;
3559 /* Loop to check if the match starts at the
3560 * current position */
3561 continue;
3564 break;
3566 if (shl != &search_hl && cur != NULL)
3567 cur = cur->next;
3570 /* Use attributes from match with highest priority among
3571 * 'search_hl' and the match list. */
3572 search_attr = search_hl.attr_cur;
3573 cur = wp->w_match_head;
3574 shl_flag = FALSE;
3575 while (cur != NULL || shl_flag == FALSE)
3577 if (shl_flag == FALSE
3578 && ((cur != NULL
3579 && cur->priority > SEARCH_HL_PRIORITY)
3580 || cur == NULL))
3582 shl = &search_hl;
3583 shl_flag = TRUE;
3585 else
3586 shl = &cur->hl;
3587 if (shl->attr_cur != 0)
3588 search_attr = shl->attr_cur;
3589 if (shl != &search_hl && cur != NULL)
3590 cur = cur->next;
3593 #endif
3595 #ifdef FEAT_DIFF
3596 if (diff_hlf != (hlf_T)0)
3598 if (diff_hlf == HLF_CHD && ptr - line >= change_start
3599 && n_extra == 0)
3600 diff_hlf = HLF_TXD; /* changed text */
3601 if (diff_hlf == HLF_TXD && ptr - line > change_end
3602 && n_extra == 0)
3603 diff_hlf = HLF_CHD; /* changed line */
3604 line_attr = hl_attr(diff_hlf);
3606 #endif
3608 /* Decide which of the highlight attributes to use. */
3609 attr_pri = TRUE;
3610 if (area_attr != 0)
3611 char_attr = area_attr;
3612 else if (search_attr != 0)
3613 char_attr = search_attr;
3614 #ifdef LINE_ATTR
3615 /* Use line_attr when not in the Visual or 'incsearch' area
3616 * (area_attr may be 0 when "noinvcur" is set). */
3617 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
3618 || vcol < fromcol || vcol_prev < fromcol_prev
3619 || vcol >= tocol))
3620 char_attr = line_attr;
3621 #endif
3622 else
3624 attr_pri = FALSE;
3625 #ifdef FEAT_SYN_HL
3626 if (has_syntax)
3627 char_attr = syntax_attr;
3628 else
3629 #endif
3630 char_attr = 0;
3635 * Get the next character to put on the screen.
3638 * The "p_extra" points to the extra stuff that is inserted to
3639 * represent special characters (non-printable stuff) and other
3640 * things. When all characters are the same, c_extra is used.
3641 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
3642 * "p_extra[n_extra]".
3643 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
3645 if (n_extra > 0)
3647 if (c_extra != NUL)
3649 c = c_extra;
3650 #ifdef FEAT_MBYTE
3651 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
3652 if (enc_utf8 && (*mb_char2len)(c) > 1)
3654 mb_utf8 = TRUE;
3655 u8cc[0] = 0;
3656 c = 0xc0;
3658 else
3659 mb_utf8 = FALSE;
3660 #endif
3662 else
3664 c = *p_extra;
3665 #ifdef FEAT_MBYTE
3666 if (has_mbyte)
3668 mb_c = c;
3669 if (enc_utf8)
3671 /* If the UTF-8 character is more than one byte:
3672 * Decode it into "mb_c". */
3673 mb_l = (*mb_ptr2len)(p_extra);
3674 mb_utf8 = FALSE;
3675 if (mb_l > n_extra)
3676 mb_l = 1;
3677 else if (mb_l > 1)
3679 mb_c = utfc_ptr2char(p_extra, u8cc);
3680 mb_utf8 = TRUE;
3681 c = 0xc0;
3684 else
3686 /* if this is a DBCS character, put it in "mb_c" */
3687 mb_l = MB_BYTE2LEN(c);
3688 if (mb_l >= n_extra)
3689 mb_l = 1;
3690 else if (mb_l > 1)
3691 mb_c = (c << 8) + p_extra[1];
3693 if (mb_l == 0) /* at the NUL at end-of-line */
3694 mb_l = 1;
3696 /* If a double-width char doesn't fit display a '>' in the
3697 * last column. */
3698 if ((
3699 # ifdef FEAT_RIGHTLEFT
3700 wp->w_p_rl ? (col <= 0) :
3701 # endif
3702 (col >= W_WIDTH(wp) - 1))
3703 && (*mb_char2cells)(mb_c) == 2)
3705 c = '>';
3706 mb_c = c;
3707 mb_l = 1;
3708 mb_utf8 = FALSE;
3709 multi_attr = hl_attr(HLF_AT);
3710 /* put the pointer back to output the double-width
3711 * character at the start of the next line. */
3712 ++n_extra;
3713 --p_extra;
3715 else
3717 n_extra -= mb_l - 1;
3718 p_extra += mb_l - 1;
3721 #endif
3722 ++p_extra;
3724 --n_extra;
3726 else
3729 * Get a character from the line itself.
3731 c = *ptr;
3732 #ifdef FEAT_MBYTE
3733 if (has_mbyte)
3735 mb_c = c;
3736 if (enc_utf8)
3738 /* If the UTF-8 character is more than one byte: Decode it
3739 * into "mb_c". */
3740 mb_l = (*mb_ptr2len)(ptr);
3741 mb_utf8 = FALSE;
3742 if (mb_l > 1)
3744 mb_c = utfc_ptr2char(ptr, u8cc);
3745 /* Overlong encoded ASCII or ASCII with composing char
3746 * is displayed normally, except a NUL. */
3747 if (mb_c < 0x80)
3748 c = mb_c;
3749 mb_utf8 = TRUE;
3751 /* At start of the line we can have a composing char.
3752 * Draw it as a space with a composing char. */
3753 if (utf_iscomposing(mb_c))
3755 int i;
3757 for (i = Screen_mco - 1; i > 0; --i)
3758 u8cc[i] = u8cc[i - 1];
3759 u8cc[0] = mb_c;
3760 mb_c = ' ';
3764 if ((mb_l == 1 && c >= 0x80)
3765 || (mb_l >= 1 && mb_c == 0)
3766 || (mb_l > 1 && (!vim_isprintc(mb_c)
3767 # ifdef UNICODE16
3768 || mb_c >= 0x10000
3769 # endif
3773 * Illegal UTF-8 byte: display as <xx>.
3774 * Non-BMP character : display as ? or fullwidth ?.
3776 # ifdef UNICODE16
3777 if (mb_c < 0x10000)
3778 # endif
3780 transchar_hex(extra, mb_c);
3781 # ifdef FEAT_RIGHTLEFT
3782 if (wp->w_p_rl) /* reverse */
3783 rl_mirror(extra);
3784 # endif
3786 # ifdef UNICODE16
3787 else if (utf_char2cells(mb_c) != 2)
3788 STRCPY(extra, "?");
3789 else
3790 /* 0xff1f in UTF-8: full-width '?' */
3791 STRCPY(extra, "\357\274\237");
3792 # endif
3794 p_extra = extra;
3795 c = *p_extra;
3796 mb_c = mb_ptr2char_adv(&p_extra);
3797 mb_utf8 = (c >= 0x80);
3798 n_extra = (int)STRLEN(p_extra);
3799 c_extra = NUL;
3800 if (area_attr == 0 && search_attr == 0)
3802 n_attr = n_extra + 1;
3803 extra_attr = hl_attr(HLF_8);
3804 saved_attr2 = char_attr; /* save current attr */
3807 else if (mb_l == 0) /* at the NUL at end-of-line */
3808 mb_l = 1;
3809 #ifdef FEAT_ARABIC
3810 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
3812 /* Do Arabic shaping. */
3813 int pc, pc1, nc;
3814 int pcc[MAX_MCO];
3816 /* The idea of what is the previous and next
3817 * character depends on 'rightleft'. */
3818 if (wp->w_p_rl)
3820 pc = prev_c;
3821 pc1 = prev_c1;
3822 nc = utf_ptr2char(ptr + mb_l);
3823 prev_c1 = u8cc[0];
3825 else
3827 pc = utfc_ptr2char(ptr + mb_l, pcc);
3828 nc = prev_c;
3829 pc1 = pcc[0];
3831 prev_c = mb_c;
3833 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
3835 else
3836 prev_c = mb_c;
3837 #endif
3839 else /* enc_dbcs */
3841 mb_l = MB_BYTE2LEN(c);
3842 if (mb_l == 0) /* at the NUL at end-of-line */
3843 mb_l = 1;
3844 else if (mb_l > 1)
3846 /* We assume a second byte below 32 is illegal.
3847 * Hopefully this is OK for all double-byte encodings!
3849 if (ptr[1] >= 32)
3850 mb_c = (c << 8) + ptr[1];
3851 else
3853 if (ptr[1] == NUL)
3855 /* head byte at end of line */
3856 mb_l = 1;
3857 transchar_nonprint(extra, c);
3859 else
3861 /* illegal tail byte */
3862 mb_l = 2;
3863 STRCPY(extra, "XX");
3865 p_extra = extra;
3866 n_extra = (int)STRLEN(extra) - 1;
3867 c_extra = NUL;
3868 c = *p_extra++;
3869 if (area_attr == 0 && search_attr == 0)
3871 n_attr = n_extra + 1;
3872 extra_attr = hl_attr(HLF_8);
3873 saved_attr2 = char_attr; /* save current attr */
3875 mb_c = c;
3879 /* If a double-width char doesn't fit display a '>' in the
3880 * last column; the character is displayed at the start of the
3881 * next line. */
3882 if ((
3883 # ifdef FEAT_RIGHTLEFT
3884 wp->w_p_rl ? (col <= 0) :
3885 # endif
3886 (col >= W_WIDTH(wp) - 1))
3887 && (*mb_char2cells)(mb_c) == 2)
3889 c = '>';
3890 mb_c = c;
3891 mb_utf8 = FALSE;
3892 mb_l = 1;
3893 multi_attr = hl_attr(HLF_AT);
3894 /* Put pointer back so that the character will be
3895 * displayed at the start of the next line. */
3896 --ptr;
3898 else if (*ptr != NUL)
3899 ptr += mb_l - 1;
3901 /* If a double-width char doesn't fit at the left side display
3902 * a '<' in the first column. */
3903 if (n_skip > 0 && mb_l > 1)
3905 n_extra = 1;
3906 c_extra = '<';
3907 c = ' ';
3908 if (area_attr == 0 && search_attr == 0)
3910 n_attr = n_extra + 1;
3911 extra_attr = hl_attr(HLF_AT);
3912 saved_attr2 = char_attr; /* save current attr */
3914 mb_c = c;
3915 mb_utf8 = FALSE;
3916 mb_l = 1;
3920 #endif
3921 ++ptr;
3923 /* 'list' : change char 160 to lcs_nbsp. */
3924 if (wp->w_p_list && (c == 160
3925 #ifdef FEAT_MBYTE
3926 || (mb_utf8 && mb_c == 160)
3927 #endif
3928 ) && lcs_nbsp)
3930 c = lcs_nbsp;
3931 if (area_attr == 0 && search_attr == 0)
3933 n_attr = 1;
3934 extra_attr = hl_attr(HLF_8);
3935 saved_attr2 = char_attr; /* save current attr */
3937 #ifdef FEAT_MBYTE
3938 mb_c = c;
3939 if (enc_utf8 && (*mb_char2len)(c) > 1)
3941 mb_utf8 = TRUE;
3942 u8cc[0] = 0;
3943 c = 0xc0;
3945 else
3946 mb_utf8 = FALSE;
3947 #endif
3950 if (extra_check)
3952 #ifdef FEAT_SPELL
3953 int can_spell = TRUE;
3954 #endif
3956 #ifdef FEAT_SYN_HL
3957 /* Get syntax attribute, unless still at the start of the line
3958 * (double-wide char that doesn't fit). */
3959 v = (long)(ptr - line);
3960 if (has_syntax && v > 0)
3962 /* Get the syntax attribute for the character. If there
3963 * is an error, disable syntax highlighting. */
3964 save_did_emsg = did_emsg;
3965 did_emsg = FALSE;
3967 syntax_attr = get_syntax_attr((colnr_T)v - 1,
3968 # ifdef FEAT_SPELL
3969 has_spell ? &can_spell :
3970 # endif
3971 NULL, FALSE);
3973 if (did_emsg)
3975 wp->w_buffer->b_syn_error = TRUE;
3976 has_syntax = FALSE;
3978 else
3979 did_emsg = save_did_emsg;
3981 /* Need to get the line again, a multi-line regexp may
3982 * have made it invalid. */
3983 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3984 ptr = line + v;
3986 if (!attr_pri)
3987 char_attr = syntax_attr;
3988 else
3989 char_attr = hl_combine_attr(syntax_attr, char_attr);
3991 #endif
3993 #ifdef FEAT_SPELL
3994 /* Check spelling (unless at the end of the line).
3995 * Only do this when there is no syntax highlighting, the
3996 * @Spell cluster is not used or the current syntax item
3997 * contains the @Spell cluster. */
3998 if (has_spell && v >= word_end && v > cur_checked_col)
4000 spell_attr = 0;
4001 # ifdef FEAT_SYN_HL
4002 if (!attr_pri)
4003 char_attr = syntax_attr;
4004 # endif
4005 if (c != 0 && (
4006 # ifdef FEAT_SYN_HL
4007 !has_syntax ||
4008 # endif
4009 can_spell))
4011 char_u *prev_ptr, *p;
4012 int len;
4013 hlf_T spell_hlf = HLF_COUNT;
4014 # ifdef FEAT_MBYTE
4015 if (has_mbyte)
4017 prev_ptr = ptr - mb_l;
4018 v -= mb_l - 1;
4020 else
4021 # endif
4022 prev_ptr = ptr - 1;
4024 /* Use nextline[] if possible, it has the start of the
4025 * next line concatenated. */
4026 if ((prev_ptr - line) - nextlinecol >= 0)
4027 p = nextline + (prev_ptr - line) - nextlinecol;
4028 else
4029 p = prev_ptr;
4030 cap_col -= (int)(prev_ptr - line);
4031 len = spell_check(wp, p, &spell_hlf, &cap_col,
4032 nochange);
4033 word_end = v + len;
4035 /* In Insert mode only highlight a word that
4036 * doesn't touch the cursor. */
4037 if (spell_hlf != HLF_COUNT
4038 && (State & INSERT) != 0
4039 && wp->w_cursor.lnum == lnum
4040 && wp->w_cursor.col >=
4041 (colnr_T)(prev_ptr - line)
4042 && wp->w_cursor.col < (colnr_T)word_end)
4044 spell_hlf = HLF_COUNT;
4045 spell_redraw_lnum = lnum;
4048 if (spell_hlf == HLF_COUNT && p != prev_ptr
4049 && (p - nextline) + len > nextline_idx)
4051 /* Remember that the good word continues at the
4052 * start of the next line. */
4053 checked_lnum = lnum + 1;
4054 checked_col = (int)((p - nextline) + len - nextline_idx);
4057 /* Turn index into actual attributes. */
4058 if (spell_hlf != HLF_COUNT)
4059 spell_attr = highlight_attr[spell_hlf];
4061 if (cap_col > 0)
4063 if (p != prev_ptr
4064 && (p - nextline) + cap_col >= nextline_idx)
4066 /* Remember that the word in the next line
4067 * must start with a capital. */
4068 capcol_lnum = lnum + 1;
4069 cap_col = (int)((p - nextline) + cap_col
4070 - nextline_idx);
4072 else
4073 /* Compute the actual column. */
4074 cap_col += (int)(prev_ptr - line);
4078 if (spell_attr != 0)
4080 if (!attr_pri)
4081 char_attr = hl_combine_attr(char_attr, spell_attr);
4082 else
4083 char_attr = hl_combine_attr(spell_attr, char_attr);
4085 #endif
4086 #ifdef FEAT_LINEBREAK
4088 * Found last space before word: check for line break.
4090 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr)
4091 && !wp->w_p_list)
4093 n_extra = win_lbr_chartabsize(wp, ptr - (
4094 # ifdef FEAT_MBYTE
4095 has_mbyte ? mb_l :
4096 # endif
4097 1), (colnr_T)vcol, NULL, lnum) - 1;
4098 c_extra = ' ';
4099 if (vim_iswhite(c))
4100 c = ' ';
4102 #endif
4104 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4106 c = lcs_trail;
4107 if (!attr_pri)
4109 n_attr = 1;
4110 extra_attr = hl_attr(HLF_8);
4111 saved_attr2 = char_attr; /* save current attr */
4113 #ifdef FEAT_MBYTE
4114 mb_c = c;
4115 if (enc_utf8 && (*mb_char2len)(c) > 1)
4117 mb_utf8 = TRUE;
4118 u8cc[0] = 0;
4119 c = 0xc0;
4121 else
4122 mb_utf8 = FALSE;
4123 #endif
4128 * Handling of non-printable characters.
4130 if (!(chartab[c & 0xff] & CT_PRINT_CHAR))
4133 * when getting a character from the file, we may have to
4134 * turn it into something else on the way to putting it
4135 * into "ScreenLines".
4137 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4139 /* tab amount depends on current column */
4140 n_extra = (int)wp->w_buffer->b_p_ts
4141 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4142 #ifdef FEAT_MBYTE
4143 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4144 #endif
4145 if (wp->w_p_list)
4147 c = lcs_tab1;
4148 c_extra = lcs_tab2;
4149 n_attr = n_extra + 1;
4150 extra_attr = hl_attr(HLF_8);
4151 saved_attr2 = char_attr; /* save current attr */
4152 #ifdef FEAT_MBYTE
4153 mb_c = c;
4154 if (enc_utf8 && (*mb_char2len)(c) > 1)
4156 mb_utf8 = TRUE;
4157 u8cc[0] = 0;
4158 c = 0xc0;
4160 #endif
4162 else
4164 c_extra = ' ';
4165 c = ' ';
4168 else if (c == NUL
4169 && ((wp->w_p_list && lcs_eol > 0)
4170 || ((fromcol >= 0 || fromcol_prev >= 0)
4171 && tocol > vcol
4172 #ifdef FEAT_VISUAL
4173 && VIsual_mode != Ctrl_V
4174 #endif
4175 && (
4176 # ifdef FEAT_RIGHTLEFT
4177 wp->w_p_rl ? (col >= 0) :
4178 # endif
4179 (col < W_WIDTH(wp)))
4180 && !(noinvcur
4181 && lnum == wp->w_cursor.lnum
4182 && (colnr_T)vcol == wp->w_virtcol)))
4183 && lcs_eol_one >= 0)
4185 /* Display a '$' after the line or highlight an extra
4186 * character if the line break is included. */
4187 #if defined(FEAT_DIFF) || defined(LINE_ATTR)
4188 /* For a diff line the highlighting continues after the
4189 * "$". */
4190 if (
4191 # ifdef FEAT_DIFF
4192 diff_hlf == (hlf_T)0
4193 # ifdef LINE_ATTR
4195 # endif
4196 # endif
4197 # ifdef LINE_ATTR
4198 line_attr == 0
4199 # endif
4201 #endif
4203 #ifdef FEAT_VIRTUALEDIT
4204 /* In virtualedit, visual selections may extend
4205 * beyond end of line. */
4206 if (area_highlighting && virtual_active()
4207 && tocol != MAXCOL && vcol < tocol)
4208 n_extra = 0;
4209 else
4210 #endif
4212 p_extra = at_end_str;
4213 n_extra = 1;
4214 c_extra = NUL;
4217 if (wp->w_p_list)
4218 c = lcs_eol;
4219 else
4220 c = ' ';
4221 lcs_eol_one = -1;
4222 --ptr; /* put it back at the NUL */
4223 if (!attr_pri)
4225 extra_attr = hl_attr(HLF_AT);
4226 n_attr = 1;
4228 #ifdef FEAT_MBYTE
4229 mb_c = c;
4230 if (enc_utf8 && (*mb_char2len)(c) > 1)
4232 mb_utf8 = TRUE;
4233 u8cc[0] = 0;
4234 c = 0xc0;
4236 else
4237 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4238 #endif
4240 else if (c != NUL)
4242 p_extra = transchar(c);
4243 #ifdef FEAT_RIGHTLEFT
4244 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4245 rl_mirror(p_extra); /* reverse "<12>" */
4246 #endif
4247 n_extra = byte2cells(c) - 1;
4248 c_extra = NUL;
4249 c = *p_extra++;
4250 if (!attr_pri)
4252 n_attr = n_extra + 1;
4253 extra_attr = hl_attr(HLF_8);
4254 saved_attr2 = char_attr; /* save current attr */
4256 #ifdef FEAT_MBYTE
4257 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4258 #endif
4260 #ifdef FEAT_VIRTUALEDIT
4261 else if (VIsual_active
4262 && (VIsual_mode == Ctrl_V
4263 || VIsual_mode == 'v')
4264 && virtual_active()
4265 && tocol != MAXCOL
4266 && vcol < tocol
4267 && (
4268 # ifdef FEAT_RIGHTLEFT
4269 wp->w_p_rl ? (col >= 0) :
4270 # endif
4271 (col < W_WIDTH(wp))))
4273 c = ' ';
4274 --ptr; /* put it back at the NUL */
4276 #endif
4277 #if defined(LINE_ATTR)
4278 else if ((
4279 # ifdef FEAT_DIFF
4280 diff_hlf != (hlf_T)0 ||
4281 # endif
4282 line_attr != 0
4283 ) && (
4284 # ifdef FEAT_RIGHTLEFT
4285 wp->w_p_rl ? (col >= 0) :
4286 # endif
4287 (col < W_WIDTH(wp))))
4289 /* Highlight until the right side of the window */
4290 c = ' ';
4291 --ptr; /* put it back at the NUL */
4293 /* Remember we do the char for line highlighting. */
4294 ++did_line_attr;
4296 /* don't do search HL for the rest of the line */
4297 if (line_attr != 0 && char_attr == search_attr && col > 0)
4298 char_attr = line_attr;
4299 # ifdef FEAT_DIFF
4300 if (diff_hlf == HLF_TXD)
4302 diff_hlf = HLF_CHD;
4303 if (attr == 0 || char_attr != attr)
4304 char_attr = hl_attr(diff_hlf);
4306 # endif
4308 #endif
4312 /* Don't override visual selection highlighting. */
4313 if (n_attr > 0
4314 && draw_state == WL_LINE
4315 && !attr_pri)
4316 char_attr = extra_attr;
4318 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
4319 /* XIM don't send preedit_start and preedit_end, but they send
4320 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
4321 * im_is_preediting() here. */
4322 if (xic != NULL
4323 && lnum == wp->w_cursor.lnum
4324 && (State & INSERT)
4325 && !p_imdisable
4326 && im_is_preediting()
4327 && draw_state == WL_LINE)
4329 colnr_T tcol;
4331 if (preedit_end_col == MAXCOL)
4332 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
4333 else
4334 tcol = preedit_end_col;
4335 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
4337 if (feedback_old_attr < 0)
4339 feedback_col = 0;
4340 feedback_old_attr = char_attr;
4342 char_attr = im_get_feedback_attr(feedback_col);
4343 if (char_attr < 0)
4344 char_attr = feedback_old_attr;
4345 feedback_col++;
4347 else if (feedback_old_attr >= 0)
4349 char_attr = feedback_old_attr;
4350 feedback_old_attr = -1;
4351 feedback_col = 0;
4354 #endif
4356 * Handle the case where we are in column 0 but not on the first
4357 * character of the line and the user wants us to show us a
4358 * special character (via 'listchars' option "precedes:<char>".
4360 if (lcs_prec_todo != NUL
4361 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
4362 #ifdef FEAT_DIFF
4363 && filler_todo <= 0
4364 #endif
4365 && draw_state > WL_NR
4366 && c != NUL)
4368 c = lcs_prec;
4369 lcs_prec_todo = NUL;
4370 #ifdef FEAT_MBYTE
4371 mb_c = c;
4372 if (enc_utf8 && (*mb_char2len)(c) > 1)
4374 mb_utf8 = TRUE;
4375 u8cc[0] = 0;
4376 c = 0xc0;
4378 else
4379 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4380 #endif
4381 if (!attr_pri)
4383 saved_attr3 = char_attr; /* save current attr */
4384 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
4385 n_attr3 = 1;
4390 * At end of the text line or just after the last character.
4392 if (c == NUL
4393 #if defined(LINE_ATTR)
4394 || did_line_attr == 1
4395 #endif
4398 #ifdef FEAT_SEARCH_EXTRA
4399 long prevcol = (long)(ptr - line) - (c == NUL);
4401 /* we're not really at that column when skipping some text */
4402 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
4403 ++prevcol;
4404 #endif
4406 /* invert at least one char, used for Visual and empty line or
4407 * highlight match at end of line. If it's beyond the last
4408 * char on the screen, just overwrite that one (tricky!) Not
4409 * needed when a '$' was displayed for 'list'. */
4410 #ifdef FEAT_SEARCH_EXTRA
4411 prevcol_hl_flag = FALSE;
4412 if (prevcol == (long)search_hl.startcol)
4413 prevcol_hl_flag = TRUE;
4414 else
4416 cur = wp->w_match_head;
4417 while (cur != NULL)
4419 if (prevcol == (long)cur->hl.startcol)
4421 prevcol_hl_flag = TRUE;
4422 break;
4424 cur = cur->next;
4427 #endif
4428 if (lcs_eol == lcs_eol_one
4429 && ((area_attr != 0 && vcol == fromcol
4430 #ifdef FEAT_VISUAL
4431 && (VIsual_mode != Ctrl_V
4432 || lnum == VIsual.lnum
4433 || lnum == curwin->w_cursor.lnum)
4434 #endif
4435 && c == NUL)
4436 #ifdef FEAT_SEARCH_EXTRA
4437 /* highlight 'hlsearch' match at end of line */
4438 || (prevcol_hl_flag == TRUE
4439 # if defined(LINE_ATTR)
4440 && did_line_attr <= 1
4441 # endif
4443 #endif
4446 int n = 0;
4448 #ifdef FEAT_RIGHTLEFT
4449 if (wp->w_p_rl)
4451 if (col < 0)
4452 n = 1;
4454 else
4455 #endif
4457 if (col >= W_WIDTH(wp))
4458 n = -1;
4460 if (n != 0)
4462 /* At the window boundary, highlight the last character
4463 * instead (better than nothing). */
4464 off += n;
4465 col += n;
4467 else
4469 /* Add a blank character to highlight. */
4470 ScreenLines[off] = ' ';
4471 #ifdef FEAT_MBYTE
4472 if (enc_utf8)
4473 ScreenLinesUC[off] = 0;
4474 #endif
4476 #ifdef FEAT_SEARCH_EXTRA
4477 if (area_attr == 0)
4479 /* Use attributes from match with highest priority among
4480 * 'search_hl' and the match list. */
4481 char_attr = search_hl.attr;
4482 cur = wp->w_match_head;
4483 shl_flag = FALSE;
4484 while (cur != NULL || shl_flag == FALSE)
4486 if (shl_flag == FALSE
4487 && ((cur != NULL
4488 && cur->priority > SEARCH_HL_PRIORITY)
4489 || cur == NULL))
4491 shl = &search_hl;
4492 shl_flag = TRUE;
4494 else
4495 shl = &cur->hl;
4496 if ((ptr - line) - 1 == (long)shl->startcol)
4497 char_attr = shl->attr;
4498 if (shl != &search_hl && cur != NULL)
4499 cur = cur->next;
4502 #endif
4503 ScreenAttrs[off] = char_attr;
4504 #ifdef FEAT_RIGHTLEFT
4505 if (wp->w_p_rl)
4507 --col;
4508 --off;
4510 else
4511 #endif
4513 ++col;
4514 ++off;
4516 ++vcol;
4517 #ifdef FEAT_SYN_HL
4518 eol_hl_off = 1;
4519 #endif
4524 * At end of the text line.
4526 if (c == NUL)
4528 #ifdef FEAT_SYN_HL
4529 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
4530 && lnum == wp->w_cursor.lnum)
4532 /* highlight last char after line */
4533 --col;
4534 --off;
4535 --vcol;
4538 /* Highlight 'cursorcolumn' past end of the line. */
4539 if (wp->w_p_wrap)
4540 v = wp->w_skipcol;
4541 else
4542 v = wp->w_leftcol;
4543 /* check if line ends before left margin */
4544 if (vcol < v + col - win_col_off(wp))
4546 vcol = v + col - win_col_off(wp);
4547 if (wp->w_p_cuc
4548 && (int)wp->w_virtcol >= vcol - eol_hl_off
4549 && (int)wp->w_virtcol < W_WIDTH(wp) * (row - startrow + 1)
4551 && lnum != wp->w_cursor.lnum
4552 # ifdef FEAT_RIGHTLEFT
4553 && !wp->w_p_rl
4554 # endif
4557 while (col < W_WIDTH(wp))
4559 ScreenLines[off] = ' ';
4560 #ifdef FEAT_MBYTE
4561 if (enc_utf8)
4562 ScreenLinesUC[off] = 0;
4563 #endif
4564 ++col;
4565 if (vcol == (long)wp->w_virtcol)
4567 ScreenAttrs[off] = hl_attr(HLF_CUC);
4568 break;
4570 ScreenAttrs[off++] = 0;
4571 ++vcol;
4574 #endif
4576 SCREEN_LINE(screen_row, W_WINCOL(wp), col, (int)W_WIDTH(wp),
4577 wp->w_p_rl);
4578 row++;
4581 * Update w_cline_height and w_cline_folded if the cursor line was
4582 * updated (saves a call to plines() later).
4584 if (wp == curwin && lnum == curwin->w_cursor.lnum)
4586 curwin->w_cline_row = startrow;
4587 curwin->w_cline_height = row - startrow;
4588 #ifdef FEAT_FOLDING
4589 curwin->w_cline_folded = FALSE;
4590 #endif
4591 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
4594 break;
4597 /* line continues beyond line end */
4598 if (lcs_ext
4599 && !wp->w_p_wrap
4600 #ifdef FEAT_DIFF
4601 && filler_todo <= 0
4602 #endif
4603 && (
4604 #ifdef FEAT_RIGHTLEFT
4605 wp->w_p_rl ? col == 0 :
4606 #endif
4607 col == W_WIDTH(wp) - 1)
4608 && (*ptr != NUL
4609 || (wp->w_p_list && lcs_eol_one > 0)
4610 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
4612 c = lcs_ext;
4613 char_attr = hl_attr(HLF_AT);
4614 #ifdef FEAT_MBYTE
4615 mb_c = c;
4616 if (enc_utf8 && (*mb_char2len)(c) > 1)
4618 mb_utf8 = TRUE;
4619 u8cc[0] = 0;
4620 c = 0xc0;
4622 else
4623 mb_utf8 = FALSE;
4624 #endif
4627 #ifdef FEAT_SYN_HL
4628 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
4629 * highlight the cursor position itself. */
4630 if (wp->w_p_cuc && vcol == (long)wp->w_virtcol
4631 && lnum != wp->w_cursor.lnum
4632 && draw_state == WL_LINE
4633 && !lnum_in_visual_area)
4635 vcol_save_attr = char_attr;
4636 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
4638 else
4639 vcol_save_attr = -1;
4640 #endif
4643 * Store character to be displayed.
4644 * Skip characters that are left of the screen for 'nowrap'.
4646 vcol_prev = vcol;
4647 if (draw_state < WL_LINE || n_skip <= 0)
4650 * Store the character.
4652 #if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
4653 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
4655 /* A double-wide character is: put first halve in left cell. */
4656 --off;
4657 --col;
4659 #endif
4660 ScreenLines[off] = c;
4661 #ifdef FEAT_MBYTE
4662 if (enc_dbcs == DBCS_JPNU)
4663 ScreenLines2[off] = mb_c & 0xff;
4664 else if (enc_utf8)
4666 if (mb_utf8)
4668 int i;
4670 ScreenLinesUC[off] = mb_c;
4671 if ((c & 0xff) == 0)
4672 ScreenLines[off] = 0x80; /* avoid storing zero */
4673 for (i = 0; i < Screen_mco; ++i)
4675 ScreenLinesC[i][off] = u8cc[i];
4676 if (u8cc[i] == 0)
4677 break;
4680 else
4681 ScreenLinesUC[off] = 0;
4683 if (multi_attr)
4685 ScreenAttrs[off] = multi_attr;
4686 multi_attr = 0;
4688 else
4689 #endif
4690 ScreenAttrs[off] = char_attr;
4692 #ifdef FEAT_MBYTE
4693 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
4695 /* Need to fill two screen columns. */
4696 ++off;
4697 ++col;
4698 if (enc_utf8)
4699 /* UTF-8: Put a 0 in the second screen char. */
4700 ScreenLines[off] = 0;
4701 else
4702 /* DBCS: Put second byte in the second screen char. */
4703 ScreenLines[off] = mb_c & 0xff;
4704 ++vcol;
4705 /* When "tocol" is halfway a character, set it to the end of
4706 * the character, otherwise highlighting won't stop. */
4707 if (tocol == vcol)
4708 ++tocol;
4709 #ifdef FEAT_RIGHTLEFT
4710 if (wp->w_p_rl)
4712 /* now it's time to backup one cell */
4713 --off;
4714 --col;
4716 #endif
4718 #endif
4719 #ifdef FEAT_RIGHTLEFT
4720 if (wp->w_p_rl)
4722 --off;
4723 --col;
4725 else
4726 #endif
4728 ++off;
4729 ++col;
4732 else
4733 --n_skip;
4735 /* Only advance the "vcol" when after the 'number' column. */
4736 if (draw_state > WL_NR
4737 #ifdef FEAT_DIFF
4738 && filler_todo <= 0
4739 #endif
4741 ++vcol;
4743 #ifdef FEAT_SYN_HL
4744 if (vcol_save_attr >= 0)
4745 char_attr = vcol_save_attr;
4746 #endif
4748 /* restore attributes after "predeces" in 'listchars' */
4749 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
4750 char_attr = saved_attr3;
4752 /* restore attributes after last 'listchars' or 'number' char */
4753 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
4754 char_attr = saved_attr2;
4757 * At end of screen line and there is more to come: Display the line
4758 * so far. If there is no more to display it is caught above.
4760 if ((
4761 #ifdef FEAT_RIGHTLEFT
4762 wp->w_p_rl ? (col < 0) :
4763 #endif
4764 (col >= W_WIDTH(wp)))
4765 && (*ptr != NUL
4766 #ifdef FEAT_DIFF
4767 || filler_todo > 0
4768 #endif
4769 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
4770 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
4773 SCREEN_LINE(screen_row, W_WINCOL(wp), col, (int)W_WIDTH(wp),
4774 wp->w_p_rl);
4775 ++row;
4776 ++screen_row;
4778 /* When not wrapping and finished diff lines, or when displayed
4779 * '$' and highlighting until last column, break here. */
4780 if ((!wp->w_p_wrap
4781 #ifdef FEAT_DIFF
4782 && filler_todo <= 0
4783 #endif
4784 ) || lcs_eol_one == -1)
4785 break;
4787 /* When the window is too narrow draw all "@" lines. */
4788 if (draw_state != WL_LINE
4789 #ifdef FEAT_DIFF
4790 && filler_todo <= 0
4791 #endif
4794 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
4795 #ifdef FEAT_VERTSPLIT
4796 draw_vsep_win(wp, row);
4797 #endif
4798 row = endrow;
4801 /* When line got too long for screen break here. */
4802 if (row == endrow)
4804 ++row;
4805 break;
4808 if (screen_cur_row == screen_row - 1
4809 #ifdef FEAT_DIFF
4810 && filler_todo <= 0
4811 #endif
4812 && W_WIDTH(wp) == Columns)
4814 /* Remember that the line wraps, used for modeless copy. */
4815 LineWraps[screen_row - 1] = TRUE;
4818 * Special trick to make copy/paste of wrapped lines work with
4819 * xterm/screen: write an extra character beyond the end of
4820 * the line. This will work with all terminal types
4821 * (regardless of the xn,am settings).
4822 * Only do this on a fast tty.
4823 * Only do this if the cursor is on the current line
4824 * (something has been written in it).
4825 * Don't do this for the GUI.
4826 * Don't do this for double-width characters.
4827 * Don't do this for a window not at the right screen border.
4829 if (p_tf
4830 #ifdef FEAT_GUI
4831 && !gui.in_use
4832 #endif
4833 #ifdef FEAT_MBYTE
4834 && !(has_mbyte
4835 && ((*mb_off2cells)(LineOffset[screen_row],
4836 LineOffset[screen_row] + screen_Columns)
4837 == 2
4838 || (*mb_off2cells)(LineOffset[screen_row - 1]
4839 + (int)Columns - 2,
4840 LineOffset[screen_row] + screen_Columns)
4841 == 2))
4842 #endif
4845 /* First make sure we are at the end of the screen line,
4846 * then output the same character again to let the
4847 * terminal know about the wrap. If the terminal doesn't
4848 * auto-wrap, we overwrite the character. */
4849 if (screen_cur_col != W_WIDTH(wp))
4850 screen_char(LineOffset[screen_row - 1]
4851 + (unsigned)Columns - 1,
4852 screen_row - 1, (int)(Columns - 1));
4854 #ifdef FEAT_MBYTE
4855 /* When there is a multi-byte character, just output a
4856 * space to keep it simple. */
4857 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
4858 screen_row - 1] + (Columns - 1)]) > 1)
4859 out_char(' ');
4860 else
4861 #endif
4862 out_char(ScreenLines[LineOffset[screen_row - 1]
4863 + (Columns - 1)]);
4864 /* force a redraw of the first char on the next line */
4865 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
4866 screen_start(); /* don't know where cursor is now */
4870 col = 0;
4871 off = (unsigned)(current_ScreenLine - ScreenLines);
4872 #ifdef FEAT_RIGHTLEFT
4873 if (wp->w_p_rl)
4875 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
4876 off += col;
4878 #endif
4880 /* reset the drawing state for the start of a wrapped line */
4881 draw_state = WL_START;
4882 saved_n_extra = n_extra;
4883 saved_p_extra = p_extra;
4884 saved_c_extra = c_extra;
4885 saved_char_attr = char_attr;
4886 n_extra = 0;
4887 lcs_prec_todo = lcs_prec;
4888 #ifdef FEAT_LINEBREAK
4889 # ifdef FEAT_DIFF
4890 if (filler_todo <= 0)
4891 # endif
4892 need_showbreak = TRUE;
4893 #endif
4894 #ifdef FEAT_DIFF
4895 --filler_todo;
4896 /* When the filler lines are actually below the last line of the
4897 * file, don't draw the line itself, break here. */
4898 if (filler_todo == 0 && wp->w_botfill)
4899 break;
4900 #endif
4903 } /* for every character in the line */
4905 #ifdef FEAT_SPELL
4906 /* After an empty line check first word for capital. */
4907 if (*skipwhite(line) == NUL)
4909 capcol_lnum = lnum + 1;
4910 cap_col = 0;
4912 #endif
4914 return row;
4917 #ifdef FEAT_MBYTE
4918 static int comp_char_differs __ARGS((int, int));
4921 * Return if the composing characters at "off_from" and "off_to" differ.
4923 static int
4924 comp_char_differs(off_from, off_to)
4925 int off_from;
4926 int off_to;
4928 int i;
4930 for (i = 0; i < Screen_mco; ++i)
4932 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
4933 return TRUE;
4934 if (ScreenLinesC[i][off_from] == 0)
4935 break;
4937 return FALSE;
4939 #endif
4942 * Check whether the given character needs redrawing:
4943 * - the (first byte of the) character is different
4944 * - the attributes are different
4945 * - the character is multi-byte and the next byte is different
4946 * - the character is two cells wide and the second cell differs.
4948 static int
4949 char_needs_redraw(off_from, off_to, cols)
4950 int off_from;
4951 int off_to;
4952 int cols;
4954 if (cols > 0
4955 && ((ScreenLines[off_from] != ScreenLines[off_to]
4956 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
4958 #ifdef FEAT_MBYTE
4959 || (enc_dbcs != 0
4960 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
4961 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
4962 ? ScreenLines2[off_from] != ScreenLines2[off_to]
4963 : (cols > 1 && ScreenLines[off_from + 1]
4964 != ScreenLines[off_to + 1])))
4965 || (enc_utf8
4966 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
4967 || (ScreenLinesUC[off_from] != 0
4968 && comp_char_differs(off_from, off_to))
4969 || (cols > 1 && ScreenLines[off_from + 1]
4970 != ScreenLines[off_to + 1])))
4971 #endif
4973 return TRUE;
4974 return FALSE;
4978 * Move one "cooked" screen line to the screen, but only the characters that
4979 * have actually changed. Handle insert/delete character.
4980 * "coloff" gives the first column on the screen for this line.
4981 * "endcol" gives the columns where valid characters are.
4982 * "clear_width" is the width of the window. It's > 0 if the rest of the line
4983 * needs to be cleared, negative otherwise.
4984 * "rlflag" is TRUE in a rightleft window:
4985 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
4986 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
4988 static void
4989 screen_line(row, coloff, endcol, clear_width
4990 #ifdef FEAT_RIGHTLEFT
4991 , rlflag
4992 #endif
4994 int row;
4995 int coloff;
4996 int endcol;
4997 int clear_width;
4998 #ifdef FEAT_RIGHTLEFT
4999 int rlflag;
5000 #endif
5002 unsigned off_from;
5003 unsigned off_to;
5004 #ifdef FEAT_MBYTE
5005 unsigned max_off_from;
5006 unsigned max_off_to;
5007 #endif
5008 int col = 0;
5009 #if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_VERTSPLIT)
5010 int hl;
5011 #endif
5012 int force = FALSE; /* force update rest of the line */
5013 int redraw_this /* bool: does character need redraw? */
5014 #ifdef FEAT_GUI
5015 = TRUE /* For GUI when while-loop empty */
5016 #endif
5018 int redraw_next; /* redraw_this for next character */
5019 #ifdef FEAT_MBYTE
5020 int clear_next = FALSE;
5021 int char_cells; /* 1: normal char */
5022 /* 2: occupies two display cells */
5023 # define CHAR_CELLS char_cells
5024 #else
5025 # define CHAR_CELLS 1
5026 #endif
5028 # ifdef FEAT_CLIPBOARD
5029 clip_may_clear_selection(row, row);
5030 # endif
5032 off_from = (unsigned)(current_ScreenLine - ScreenLines);
5033 off_to = LineOffset[row] + coloff;
5034 #ifdef FEAT_MBYTE
5035 max_off_from = off_from + screen_Columns;
5036 max_off_to = LineOffset[row] + screen_Columns;
5037 #endif
5039 #ifdef FEAT_RIGHTLEFT
5040 if (rlflag)
5042 /* Clear rest first, because it's left of the text. */
5043 if (clear_width > 0)
5045 while (col <= endcol && ScreenLines[off_to] == ' '
5046 && ScreenAttrs[off_to] == 0
5047 # ifdef FEAT_MBYTE
5048 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5049 # endif
5052 ++off_to;
5053 ++col;
5055 if (col <= endcol)
5056 screen_fill(row, row + 1, col + coloff,
5057 endcol + coloff + 1, ' ', ' ', 0);
5059 col = endcol + 1;
5060 off_to = LineOffset[row] + col + coloff;
5061 off_from += col;
5062 endcol = (clear_width > 0 ? clear_width : -clear_width);
5064 #endif /* FEAT_RIGHTLEFT */
5066 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
5068 while (col < endcol)
5070 #ifdef FEAT_MBYTE
5071 if (has_mbyte && (col + 1 < endcol))
5072 char_cells = (*mb_off2cells)(off_from, max_off_from);
5073 else
5074 char_cells = 1;
5075 #endif
5077 redraw_this = redraw_next;
5078 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
5079 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
5081 #ifdef FEAT_GUI
5082 /* If the next character was bold, then redraw the current character to
5083 * remove any pixels that might have spilt over into us. This only
5084 * happens in the GUI.
5086 if (redraw_next && gui.in_use)
5088 hl = ScreenAttrs[off_to + CHAR_CELLS];
5089 if (hl > HL_ALL)
5090 hl = syn_attr2attr(hl);
5091 if (hl & HL_BOLD)
5092 redraw_this = TRUE;
5094 #endif
5096 if (redraw_this)
5099 * Special handling when 'xs' termcap flag set (hpterm):
5100 * Attributes for characters are stored at the position where the
5101 * cursor is when writing the highlighting code. The
5102 * start-highlighting code must be written with the cursor on the
5103 * first highlighted character. The stop-highlighting code must
5104 * be written with the cursor just after the last highlighted
5105 * character.
5106 * Overwriting a character doesn't remove it's highlighting. Need
5107 * to clear the rest of the line, and force redrawing it
5108 * completely.
5110 if ( p_wiv
5111 && !force
5112 #ifdef FEAT_GUI
5113 && !gui.in_use
5114 #endif
5115 && ScreenAttrs[off_to] != 0
5116 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
5119 * Need to remove highlighting attributes here.
5121 windgoto(row, col + coloff);
5122 out_str(T_CE); /* clear rest of this screen line */
5123 screen_start(); /* don't know where cursor is now */
5124 force = TRUE; /* force redraw of rest of the line */
5125 redraw_next = TRUE; /* or else next char would miss out */
5128 * If the previous character was highlighted, need to stop
5129 * highlighting at this character.
5131 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
5133 screen_attr = ScreenAttrs[off_to - 1];
5134 term_windgoto(row, col + coloff);
5135 screen_stop_highlight();
5137 else
5138 screen_attr = 0; /* highlighting has stopped */
5140 #ifdef FEAT_MBYTE
5141 if (enc_dbcs != 0)
5143 /* Check if overwriting a double-byte with a single-byte or
5144 * the other way around requires another character to be
5145 * redrawn. For UTF-8 this isn't needed, because comparing
5146 * ScreenLinesUC[] is sufficient. */
5147 if (char_cells == 1
5148 && col + 1 < endcol
5149 && (*mb_off2cells)(off_to, max_off_to) > 1)
5151 /* Writing a single-cell character over a double-cell
5152 * character: need to redraw the next cell. */
5153 ScreenLines[off_to + 1] = 0;
5154 redraw_next = TRUE;
5156 else if (char_cells == 2
5157 && col + 2 < endcol
5158 && (*mb_off2cells)(off_to, max_off_to) == 1
5159 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
5161 /* Writing the second half of a double-cell character over
5162 * a double-cell character: need to redraw the second
5163 * cell. */
5164 ScreenLines[off_to + 2] = 0;
5165 redraw_next = TRUE;
5168 if (enc_dbcs == DBCS_JPNU)
5169 ScreenLines2[off_to] = ScreenLines2[off_from];
5171 /* When writing a single-width character over a double-width
5172 * character and at the end of the redrawn text, need to clear out
5173 * the right halve of the old character.
5174 * Also required when writing the right halve of a double-width
5175 * char over the left halve of an existing one. */
5176 if (has_mbyte && col + char_cells == endcol
5177 && ((char_cells == 1
5178 && (*mb_off2cells)(off_to, max_off_to) > 1)
5179 || (char_cells == 2
5180 && (*mb_off2cells)(off_to, max_off_to) == 1
5181 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
5182 clear_next = TRUE;
5183 #endif
5185 ScreenLines[off_to] = ScreenLines[off_from];
5186 #ifdef FEAT_MBYTE
5187 if (enc_utf8)
5189 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
5190 if (ScreenLinesUC[off_from] != 0)
5192 int i;
5194 for (i = 0; i < Screen_mco; ++i)
5195 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
5198 if (char_cells == 2)
5199 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
5200 #endif
5202 #if defined(FEAT_GUI) || defined(UNIX)
5203 /* The bold trick makes a single column of pixels appear in the
5204 * next character. When a bold character is removed, the next
5205 * character should be redrawn too. This happens for our own GUI
5206 * and for some xterms. */
5207 if (
5208 # ifdef FEAT_GUI
5209 gui.in_use
5210 # endif
5211 # if defined(FEAT_GUI) && defined(UNIX)
5213 # endif
5214 # ifdef UNIX
5215 term_is_xterm
5216 # endif
5219 hl = ScreenAttrs[off_to];
5220 if (hl > HL_ALL)
5221 hl = syn_attr2attr(hl);
5222 if (hl & HL_BOLD)
5223 redraw_next = TRUE;
5225 #endif
5226 ScreenAttrs[off_to] = ScreenAttrs[off_from];
5227 #ifdef FEAT_MBYTE
5228 /* For simplicity set the attributes of second half of a
5229 * double-wide character equal to the first half. */
5230 if (char_cells == 2)
5231 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
5233 if (enc_dbcs != 0 && char_cells == 2)
5234 screen_char_2(off_to, row, col + coloff);
5235 else
5236 #endif
5237 screen_char(off_to, row, col + coloff);
5239 else if ( p_wiv
5240 #ifdef FEAT_GUI
5241 && !gui.in_use
5242 #endif
5243 && col + coloff > 0)
5245 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
5248 * Don't output stop-highlight when moving the cursor, it will
5249 * stop the highlighting when it should continue.
5251 screen_attr = 0;
5253 else if (screen_attr != 0)
5254 screen_stop_highlight();
5257 off_to += CHAR_CELLS;
5258 off_from += CHAR_CELLS;
5259 col += CHAR_CELLS;
5262 #ifdef FEAT_MBYTE
5263 if (clear_next)
5265 /* Clear the second half of a double-wide character of which the left
5266 * half was overwritten with a single-wide character. */
5267 ScreenLines[off_to] = ' ';
5268 if (enc_utf8)
5269 ScreenLinesUC[off_to] = 0;
5270 screen_char(off_to, row, col + coloff);
5272 #endif
5274 if (clear_width > 0
5275 #ifdef FEAT_RIGHTLEFT
5276 && !rlflag
5277 #endif
5280 #ifdef FEAT_GUI
5281 int startCol = col;
5282 #endif
5284 /* blank out the rest of the line */
5285 while (col < clear_width && ScreenLines[off_to] == ' '
5286 && ScreenAttrs[off_to] == 0
5287 #ifdef FEAT_MBYTE
5288 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5289 #endif
5292 ++off_to;
5293 ++col;
5295 if (col < clear_width)
5297 #ifdef FEAT_GUI
5299 * In the GUI, clearing the rest of the line may leave pixels
5300 * behind if the first character cleared was bold. Some bold
5301 * fonts spill over the left. In this case we redraw the previous
5302 * character too. If we didn't skip any blanks above, then we
5303 * only redraw if the character wasn't already redrawn anyway.
5305 if (gui.in_use && (col > startCol || !redraw_this))
5307 hl = ScreenAttrs[off_to];
5308 if (hl > HL_ALL || (hl & HL_BOLD))
5310 int prev_cells = 1;
5311 # ifdef FEAT_MBYTE
5312 if (enc_utf8)
5313 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
5314 * that its width is 2. */
5315 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
5316 else if (enc_dbcs != 0)
5318 /* find previous character by counting from first
5319 * column and get its width. */
5320 unsigned off = LineOffset[row];
5321 unsigned max_off = LineOffset[row] + screen_Columns;
5323 while (off < off_to)
5325 prev_cells = (*mb_off2cells)(off, max_off);
5326 off += prev_cells;
5330 if (enc_dbcs != 0 && prev_cells > 1)
5331 screen_char_2(off_to - prev_cells, row,
5332 col + coloff - prev_cells);
5333 else
5334 # endif
5335 screen_char(off_to - prev_cells, row,
5336 col + coloff - prev_cells);
5339 #endif
5340 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
5341 ' ', ' ', 0);
5342 #ifdef FEAT_VERTSPLIT
5343 off_to += clear_width - col;
5344 col = clear_width;
5345 #endif
5349 if (clear_width > 0)
5351 #ifdef FEAT_VERTSPLIT
5352 /* For a window that's left of another, draw the separator char. */
5353 if (col + coloff < Columns)
5355 int c;
5357 c = fillchar_vsep(&hl);
5358 if (ScreenLines[off_to] != c
5359 # ifdef FEAT_MBYTE
5360 || (enc_utf8 && (int)ScreenLinesUC[off_to]
5361 != (c >= 0x80 ? c : 0))
5362 # endif
5363 || ScreenAttrs[off_to] != hl)
5365 ScreenLines[off_to] = c;
5366 ScreenAttrs[off_to] = hl;
5367 # ifdef FEAT_MBYTE
5368 if (enc_utf8)
5370 if (c >= 0x80)
5372 ScreenLinesUC[off_to] = c;
5373 ScreenLinesC[0][off_to] = 0;
5375 else
5376 ScreenLinesUC[off_to] = 0;
5378 # endif
5379 screen_char(off_to, row, col + coloff);
5382 else
5383 #endif
5384 LineWraps[row] = FALSE;
5388 #if defined(FEAT_RIGHTLEFT) || defined(PROTO)
5390 * Mirror text "str" for right-left displaying.
5391 * Only works for single-byte characters (e.g., numbers).
5393 void
5394 rl_mirror(str)
5395 char_u *str;
5397 char_u *p1, *p2;
5398 int t;
5400 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
5402 t = *p1;
5403 *p1 = *p2;
5404 *p2 = t;
5407 #endif
5409 #if defined(FEAT_WINDOWS) || defined(PROTO)
5411 * mark all status lines for redraw; used after first :cd
5413 void
5414 status_redraw_all()
5416 win_T *wp;
5418 for (wp = firstwin; wp; wp = wp->w_next)
5419 if (wp->w_status_height)
5421 wp->w_redr_status = TRUE;
5422 redraw_later(VALID);
5427 * mark all status lines of the current buffer for redraw
5429 void
5430 status_redraw_curbuf()
5432 win_T *wp;
5434 for (wp = firstwin; wp; wp = wp->w_next)
5435 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
5437 wp->w_redr_status = TRUE;
5438 redraw_later(VALID);
5443 * Redraw all status lines that need to be redrawn.
5445 void
5446 redraw_statuslines()
5448 win_T *wp;
5450 for (wp = firstwin; wp; wp = wp->w_next)
5451 if (wp->w_redr_status)
5452 win_redr_status(wp);
5453 if (redraw_tabline)
5454 draw_tabline();
5456 #endif
5458 #if (defined(FEAT_WILDMENU) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
5460 * Redraw all status lines at the bottom of frame "frp".
5462 void
5463 win_redraw_last_status(frp)
5464 frame_T *frp;
5466 if (frp->fr_layout == FR_LEAF)
5467 frp->fr_win->w_redr_status = TRUE;
5468 else if (frp->fr_layout == FR_ROW)
5470 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
5471 win_redraw_last_status(frp);
5473 else /* frp->fr_layout == FR_COL */
5475 frp = frp->fr_child;
5476 while (frp->fr_next != NULL)
5477 frp = frp->fr_next;
5478 win_redraw_last_status(frp);
5481 #endif
5483 #ifdef FEAT_VERTSPLIT
5485 * Draw the verticap separator right of window "wp" starting with line "row".
5487 static void
5488 draw_vsep_win(wp, row)
5489 win_T *wp;
5490 int row;
5492 int hl;
5493 int c;
5495 if (wp->w_vsep_width)
5497 /* draw the vertical separator right of this window */
5498 c = fillchar_vsep(&hl);
5499 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
5500 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
5501 c, ' ', hl);
5504 #endif
5506 #ifdef FEAT_WILDMENU
5507 static int status_match_len __ARGS((expand_T *xp, char_u *s));
5508 static int skip_status_match_char __ARGS((expand_T *xp, char_u *s));
5511 * Get the length of an item as it will be shown in the status line.
5513 static int
5514 status_match_len(xp, s)
5515 expand_T *xp;
5516 char_u *s;
5518 int len = 0;
5520 #ifdef FEAT_MENU
5521 int emenu = (xp->xp_context == EXPAND_MENUS
5522 || xp->xp_context == EXPAND_MENUNAMES);
5524 /* Check for menu separators - replace with '|'. */
5525 if (emenu && menu_is_separator(s))
5526 return 1;
5527 #endif
5529 while (*s != NUL)
5531 s += skip_status_match_char(xp, s);
5532 len += ptr2cells(s);
5533 mb_ptr_adv(s);
5536 return len;
5540 * Return the number of characters that should be skipped in a status match.
5541 * These are backslashes used for escaping. Do show backslashes in help tags.
5543 static int
5544 skip_status_match_char(xp, s)
5545 expand_T *xp;
5546 char_u *s;
5548 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
5549 #ifdef FEAT_MENU
5550 || ((xp->xp_context == EXPAND_MENUS
5551 || xp->xp_context == EXPAND_MENUNAMES)
5552 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
5553 #endif
5556 #ifndef BACKSLASH_IN_FILENAME
5557 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
5558 return 2;
5559 #endif
5560 return 1;
5562 return 0;
5566 * Show wildchar matches in the status line.
5567 * Show at least the "match" item.
5568 * We start at item 'first_match' in the list and show all matches that fit.
5570 * If inversion is possible we use it. Else '=' characters are used.
5572 void
5573 win_redr_status_matches(xp, num_matches, matches, match, showtail)
5574 expand_T *xp;
5575 int num_matches;
5576 char_u **matches; /* list of matches */
5577 int match;
5578 int showtail;
5580 #define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
5581 int row;
5582 char_u *buf;
5583 int len;
5584 int clen; /* length in screen cells */
5585 int fillchar;
5586 int attr;
5587 int i;
5588 int highlight = TRUE;
5589 char_u *selstart = NULL;
5590 int selstart_col = 0;
5591 char_u *selend = NULL;
5592 static int first_match = 0;
5593 int add_left = FALSE;
5594 char_u *s;
5595 #ifdef FEAT_MENU
5596 int emenu;
5597 #endif
5598 #if defined(FEAT_MBYTE) || defined(FEAT_MENU)
5599 int l;
5600 #endif
5602 if (matches == NULL) /* interrupted completion? */
5603 return;
5605 #ifdef FEAT_MBYTE
5606 if (has_mbyte)
5607 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
5608 else
5609 #endif
5610 buf = alloc((unsigned)Columns + 1);
5611 if (buf == NULL)
5612 return;
5614 if (match == -1) /* don't show match but original text */
5616 match = 0;
5617 highlight = FALSE;
5619 /* count 1 for the ending ">" */
5620 clen = status_match_len(xp, L_MATCH(match)) + 3;
5621 if (match == 0)
5622 first_match = 0;
5623 else if (match < first_match)
5625 /* jumping left, as far as we can go */
5626 first_match = match;
5627 add_left = TRUE;
5629 else
5631 /* check if match fits on the screen */
5632 for (i = first_match; i < match; ++i)
5633 clen += status_match_len(xp, L_MATCH(i)) + 2;
5634 if (first_match > 0)
5635 clen += 2;
5636 /* jumping right, put match at the left */
5637 if ((long)clen > Columns)
5639 first_match = match;
5640 /* if showing the last match, we can add some on the left */
5641 clen = 2;
5642 for (i = match; i < num_matches; ++i)
5644 clen += status_match_len(xp, L_MATCH(i)) + 2;
5645 if ((long)clen >= Columns)
5646 break;
5648 if (i == num_matches)
5649 add_left = TRUE;
5652 if (add_left)
5653 while (first_match > 0)
5655 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
5656 if ((long)clen >= Columns)
5657 break;
5658 --first_match;
5661 fillchar = fillchar_status(&attr, TRUE);
5663 if (first_match == 0)
5665 *buf = NUL;
5666 len = 0;
5668 else
5670 STRCPY(buf, "< ");
5671 len = 2;
5673 clen = len;
5675 i = first_match;
5676 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
5678 if (i == match)
5680 selstart = buf + len;
5681 selstart_col = clen;
5684 s = L_MATCH(i);
5685 /* Check for menu separators - replace with '|' */
5686 #ifdef FEAT_MENU
5687 emenu = (xp->xp_context == EXPAND_MENUS
5688 || xp->xp_context == EXPAND_MENUNAMES);
5689 if (emenu && menu_is_separator(s))
5691 STRCPY(buf + len, transchar('|'));
5692 l = (int)STRLEN(buf + len);
5693 len += l;
5694 clen += l;
5696 else
5697 #endif
5698 for ( ; *s != NUL; ++s)
5700 s += skip_status_match_char(xp, s);
5701 clen += ptr2cells(s);
5702 #ifdef FEAT_MBYTE
5703 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
5705 STRNCPY(buf + len, s, l);
5706 s += l - 1;
5707 len += l;
5709 else
5710 #endif
5712 STRCPY(buf + len, transchar_byte(*s));
5713 len += (int)STRLEN(buf + len);
5716 if (i == match)
5717 selend = buf + len;
5719 *(buf + len++) = ' ';
5720 *(buf + len++) = ' ';
5721 clen += 2;
5722 if (++i == num_matches)
5723 break;
5726 if (i != num_matches)
5728 *(buf + len++) = '>';
5729 ++clen;
5732 buf[len] = NUL;
5734 row = cmdline_row - 1;
5735 if (row >= 0)
5737 if (wild_menu_showing == 0)
5739 if (msg_scrolled > 0)
5741 /* Put the wildmenu just above the command line. If there is
5742 * no room, scroll the screen one line up. */
5743 if (cmdline_row == Rows - 1)
5745 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
5746 ++msg_scrolled;
5748 else
5750 ++cmdline_row;
5751 ++row;
5753 wild_menu_showing = WM_SCROLLED;
5755 else
5757 /* Create status line if needed by setting 'laststatus' to 2.
5758 * Set 'winminheight' to zero to avoid that the window is
5759 * resized. */
5760 if (lastwin->w_status_height == 0)
5762 save_p_ls = p_ls;
5763 save_p_wmh = p_wmh;
5764 p_ls = 2;
5765 p_wmh = 0;
5766 last_status(FALSE);
5768 wild_menu_showing = WM_SHOWN;
5772 screen_puts(buf, row, 0, attr);
5773 if (selstart != NULL && highlight)
5775 *selend = NUL;
5776 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
5779 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
5782 #ifdef FEAT_VERTSPLIT
5783 win_redraw_last_status(topframe);
5784 #else
5785 lastwin->w_redr_status = TRUE;
5786 #endif
5787 vim_free(buf);
5789 #endif
5791 #if defined(FEAT_WINDOWS) || defined(PROTO)
5793 * Redraw the status line of window wp.
5795 * If inversion is possible we use it. Else '=' characters are used.
5797 void
5798 win_redr_status(wp)
5799 win_T *wp;
5801 int row;
5802 char_u *p;
5803 int len;
5804 int fillchar;
5805 int attr;
5806 int this_ru_col;
5807 static int busy = FALSE;
5809 /* It's possible to get here recursively when 'statusline' (indirectly)
5810 * invokes ":redrawstatus". Simply ignore the call then. */
5811 if (busy)
5812 return;
5813 busy = TRUE;
5815 wp->w_redr_status = FALSE;
5816 if (wp->w_status_height == 0)
5818 /* no status line, can only be last window */
5819 redraw_cmdline = TRUE;
5821 else if (!redrawing()
5822 #ifdef FEAT_INS_EXPAND
5823 /* don't update status line when popup menu is visible and may be
5824 * drawn over it */
5825 || pum_visible()
5826 #endif
5829 /* Don't redraw right now, do it later. */
5830 wp->w_redr_status = TRUE;
5832 #ifdef FEAT_STL_OPT
5833 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
5835 /* redraw custom status line */
5836 redraw_custom_statusline(wp);
5838 #endif
5839 else
5841 fillchar = fillchar_status(&attr, wp == curwin);
5843 get_trans_bufname(wp->w_buffer);
5844 p = NameBuff;
5845 len = (int)STRLEN(p);
5847 if (wp->w_buffer->b_help
5848 #ifdef FEAT_QUICKFIX
5849 || wp->w_p_pvw
5850 #endif
5851 || bufIsChanged(wp->w_buffer)
5852 || wp->w_buffer->b_p_ro)
5853 *(p + len++) = ' ';
5854 if (wp->w_buffer->b_help)
5856 STRCPY(p + len, _("[Help]"));
5857 len += (int)STRLEN(p + len);
5859 #ifdef FEAT_QUICKFIX
5860 if (wp->w_p_pvw)
5862 STRCPY(p + len, _("[Preview]"));
5863 len += (int)STRLEN(p + len);
5865 #endif
5866 if (bufIsChanged(wp->w_buffer))
5868 STRCPY(p + len, "[+]");
5869 len += 3;
5871 if (wp->w_buffer->b_p_ro)
5873 STRCPY(p + len, "[RO]");
5874 len += 4;
5877 #ifndef FEAT_VERTSPLIT
5878 this_ru_col = ru_col;
5879 if (this_ru_col < (Columns + 1) / 2)
5880 this_ru_col = (Columns + 1) / 2;
5881 #else
5882 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
5883 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
5884 this_ru_col = (W_WIDTH(wp) + 1) / 2;
5885 if (this_ru_col <= 1)
5887 p = (char_u *)"<"; /* No room for file name! */
5888 len = 1;
5890 else
5891 #endif
5892 #ifdef FEAT_MBYTE
5893 if (has_mbyte)
5895 int clen = 0, i;
5897 /* Count total number of display cells. */
5898 for (i = 0; p[i] != NUL; i += (*mb_ptr2len)(p + i))
5899 clen += (*mb_ptr2cells)(p + i);
5900 /* Find first character that will fit.
5901 * Going from start to end is much faster for DBCS. */
5902 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
5903 i += (*mb_ptr2len)(p + i))
5904 clen -= (*mb_ptr2cells)(p + i);
5905 len = clen;
5906 if (i > 0)
5908 p = p + i - 1;
5909 *p = '<';
5910 ++len;
5914 else
5915 #endif
5916 if (len > this_ru_col - 1)
5918 p += len - (this_ru_col - 1);
5919 *p = '<';
5920 len = this_ru_col - 1;
5923 row = W_WINROW(wp) + wp->w_height;
5924 screen_puts(p, row, W_WINCOL(wp), attr);
5925 screen_fill(row, row + 1, len + W_WINCOL(wp),
5926 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
5928 if (get_keymap_str(wp, NameBuff, MAXPATHL)
5929 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
5930 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
5931 - 1 + W_WINCOL(wp)), attr);
5933 #ifdef FEAT_CMDL_INFO
5934 win_redr_ruler(wp, TRUE);
5935 #endif
5938 #ifdef FEAT_VERTSPLIT
5940 * May need to draw the character below the vertical separator.
5942 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
5944 if (stl_connected(wp))
5945 fillchar = fillchar_status(&attr, wp == curwin);
5946 else
5947 fillchar = fillchar_vsep(&attr);
5948 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
5949 attr);
5951 #endif
5952 busy = FALSE;
5955 #ifdef FEAT_STL_OPT
5957 * Redraw the status line according to 'statusline' and take care of any
5958 * errors encountered.
5960 static void
5961 redraw_custom_statusline(wp)
5962 win_T *wp;
5964 static int entered = FALSE;
5965 int save_called_emsg = called_emsg;
5967 /* When called recursively return. This can happen when the statusline
5968 * contains an expression that triggers a redraw. */
5969 if (entered)
5970 return;
5971 entered = TRUE;
5973 called_emsg = FALSE;
5974 win_redr_custom(wp, FALSE);
5975 if (called_emsg)
5977 /* When there is an error disable the statusline, otherwise the
5978 * display is messed up with errors and a redraw triggers the problem
5979 * again and again. */
5980 set_string_option_direct((char_u *)"statusline", -1,
5981 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
5982 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
5984 called_emsg |= save_called_emsg;
5985 entered = FALSE;
5987 #endif
5989 # ifdef FEAT_VERTSPLIT
5991 * Return TRUE if the status line of window "wp" is connected to the status
5992 * line of the window right of it. If not, then it's a vertical separator.
5993 * Only call if (wp->w_vsep_width != 0).
5996 stl_connected(wp)
5997 win_T *wp;
5999 frame_T *fr;
6001 fr = wp->w_frame;
6002 while (fr->fr_parent != NULL)
6004 if (fr->fr_parent->fr_layout == FR_COL)
6006 if (fr->fr_next != NULL)
6007 break;
6009 else
6011 if (fr->fr_next != NULL)
6012 return TRUE;
6014 fr = fr->fr_parent;
6016 return FALSE;
6018 # endif
6020 #endif /* FEAT_WINDOWS */
6022 #if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
6024 * Get the value to show for the language mappings, active 'keymap'.
6027 get_keymap_str(wp, buf, len)
6028 win_T *wp;
6029 char_u *buf; /* buffer for the result */
6030 int len; /* length of buffer */
6032 char_u *p;
6034 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
6035 return FALSE;
6038 #ifdef FEAT_EVAL
6039 buf_T *old_curbuf = curbuf;
6040 win_T *old_curwin = curwin;
6041 char_u *s;
6043 curbuf = wp->w_buffer;
6044 curwin = wp;
6045 STRCPY(buf, "b:keymap_name"); /* must be writable */
6046 ++emsg_skip;
6047 s = p = eval_to_string(buf, NULL, FALSE);
6048 --emsg_skip;
6049 curbuf = old_curbuf;
6050 curwin = old_curwin;
6051 if (p == NULL || *p == NUL)
6052 #endif
6054 #ifdef FEAT_KEYMAP
6055 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
6056 p = wp->w_buffer->b_p_keymap;
6057 else
6058 #endif
6059 p = (char_u *)"lang";
6061 if ((int)(STRLEN(p) + 3) < len)
6062 sprintf((char *)buf, "<%s>", p);
6063 else
6064 buf[0] = NUL;
6065 #ifdef FEAT_EVAL
6066 vim_free(s);
6067 #endif
6069 return buf[0] != NUL;
6071 #endif
6073 #if defined(FEAT_STL_OPT) || defined(PROTO)
6075 * Redraw the status line or ruler of window "wp".
6076 * When "wp" is NULL redraw the tab pages line from 'tabline'.
6078 static void
6079 win_redr_custom(wp, draw_ruler)
6080 win_T *wp;
6081 int draw_ruler; /* TRUE or FALSE */
6083 int attr;
6084 int curattr;
6085 int row;
6086 int col = 0;
6087 int maxwidth;
6088 int width;
6089 int n;
6090 int len;
6091 int fillchar;
6092 char_u buf[MAXPATHL];
6093 char_u *stl;
6094 char_u *p;
6095 struct stl_hlrec hltab[STL_MAX_ITEM];
6096 struct stl_hlrec tabtab[STL_MAX_ITEM];
6097 int use_sandbox = FALSE;
6099 /* setup environment for the task at hand */
6100 if (wp == NULL)
6102 /* Use 'tabline'. Always at the first line of the screen. */
6103 stl = p_tal;
6104 row = 0;
6105 fillchar = ' ';
6106 attr = hl_attr(HLF_TPF);
6107 maxwidth = Columns;
6108 # ifdef FEAT_EVAL
6109 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
6110 # endif
6112 else
6114 row = W_WINROW(wp) + wp->w_height;
6115 fillchar = fillchar_status(&attr, wp == curwin);
6116 maxwidth = W_WIDTH(wp);
6118 if (draw_ruler)
6120 stl = p_ruf;
6121 /* advance past any leading group spec - implicit in ru_col */
6122 if (*stl == '%')
6124 if (*++stl == '-')
6125 stl++;
6126 if (atoi((char *)stl))
6127 while (VIM_ISDIGIT(*stl))
6128 stl++;
6129 if (*stl++ != '(')
6130 stl = p_ruf;
6132 #ifdef FEAT_VERTSPLIT
6133 col = ru_col - (Columns - W_WIDTH(wp));
6134 if (col < (W_WIDTH(wp) + 1) / 2)
6135 col = (W_WIDTH(wp) + 1) / 2;
6136 #else
6137 col = ru_col;
6138 if (col > (Columns + 1) / 2)
6139 col = (Columns + 1) / 2;
6140 #endif
6141 maxwidth = W_WIDTH(wp) - col;
6142 #ifdef FEAT_WINDOWS
6143 if (!wp->w_status_height)
6144 #endif
6146 row = Rows - 1;
6147 --maxwidth; /* writing in last column may cause scrolling */
6148 fillchar = ' ';
6149 attr = 0;
6152 # ifdef FEAT_EVAL
6153 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
6154 # endif
6156 else
6158 if (*wp->w_p_stl != NUL)
6159 stl = wp->w_p_stl;
6160 else
6161 stl = p_stl;
6162 # ifdef FEAT_EVAL
6163 use_sandbox = was_set_insecurely((char_u *)"statusline",
6164 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
6165 # endif
6168 #ifdef FEAT_VERTSPLIT
6169 col += W_WINCOL(wp);
6170 #endif
6173 if (maxwidth <= 0)
6174 return;
6176 /* Make a copy, because the statusline may include a function call that
6177 * might change the option value and free the memory. */
6178 stl = vim_strsave(stl);
6179 width = build_stl_str_hl(wp == NULL ? curwin : wp,
6180 buf, sizeof(buf),
6181 stl, use_sandbox,
6182 fillchar, maxwidth, hltab, tabtab);
6183 vim_free(stl);
6184 len = (int)STRLEN(buf);
6186 while (width < maxwidth && len < (int)sizeof(buf) - 1)
6188 #ifdef FEAT_MBYTE
6189 len += (*mb_char2bytes)(fillchar, buf + len);
6190 #else
6191 buf[len++] = fillchar;
6192 #endif
6193 ++width;
6195 buf[len] = NUL;
6198 * Draw each snippet with the specified highlighting.
6200 curattr = attr;
6201 p = buf;
6202 for (n = 0; hltab[n].start != NULL; n++)
6204 len = (int)(hltab[n].start - p);
6205 screen_puts_len(p, len, row, col, curattr);
6206 col += vim_strnsize(p, len);
6207 p = hltab[n].start;
6209 if (hltab[n].userhl == 0)
6210 curattr = attr;
6211 else if (hltab[n].userhl < 0)
6212 curattr = syn_id2attr(-hltab[n].userhl);
6213 #ifdef FEAT_WINDOWS
6214 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
6215 curattr = highlight_stlnc[hltab[n].userhl - 1];
6216 #endif
6217 else
6218 curattr = highlight_user[hltab[n].userhl - 1];
6220 screen_puts(p, row, col, curattr);
6222 if (wp == NULL)
6224 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
6225 col = 0;
6226 len = 0;
6227 p = buf;
6228 fillchar = 0;
6229 for (n = 0; tabtab[n].start != NULL; n++)
6231 len += vim_strnsize(p, (int)(tabtab[n].start - p));
6232 while (col < len)
6233 TabPageIdxs[col++] = fillchar;
6234 p = tabtab[n].start;
6235 fillchar = tabtab[n].userhl;
6237 while (col < Columns)
6238 TabPageIdxs[col++] = fillchar;
6242 #endif /* FEAT_STL_OPT */
6245 * Output a single character directly to the screen and update ScreenLines.
6247 void
6248 screen_putchar(c, row, col, attr)
6249 int c;
6250 int row, col;
6251 int attr;
6253 #ifdef FEAT_MBYTE
6254 char_u buf[MB_MAXBYTES + 1];
6256 buf[(*mb_char2bytes)(c, buf)] = NUL;
6257 #else
6258 char_u buf[2];
6260 buf[0] = c;
6261 buf[1] = NUL;
6262 #endif
6263 screen_puts(buf, row, col, attr);
6267 * Get a single character directly from ScreenLines into "bytes[]".
6268 * Also return its attribute in *attrp;
6270 void
6271 screen_getbytes(row, col, bytes, attrp)
6272 int row, col;
6273 char_u *bytes;
6274 int *attrp;
6276 unsigned off;
6278 /* safety check */
6279 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
6281 off = LineOffset[row] + col;
6282 *attrp = ScreenAttrs[off];
6283 bytes[0] = ScreenLines[off];
6284 bytes[1] = NUL;
6286 #ifdef FEAT_MBYTE
6287 if (enc_utf8 && ScreenLinesUC[off] != 0)
6288 bytes[utfc_char2bytes(off, bytes)] = NUL;
6289 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6291 bytes[0] = ScreenLines[off];
6292 bytes[1] = ScreenLines2[off];
6293 bytes[2] = NUL;
6295 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
6297 bytes[1] = ScreenLines[off + 1];
6298 bytes[2] = NUL;
6300 #endif
6304 #ifdef FEAT_MBYTE
6305 static int screen_comp_differs __ARGS((int, int*));
6308 * Return TRUE if composing characters for screen posn "off" differs from
6309 * composing characters in "u8cc".
6311 static int
6312 screen_comp_differs(off, u8cc)
6313 int off;
6314 int *u8cc;
6316 int i;
6318 for (i = 0; i < Screen_mco; ++i)
6320 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
6321 return TRUE;
6322 if (u8cc[i] == 0)
6323 break;
6325 return FALSE;
6327 #endif
6330 * Put string '*text' on the screen at position 'row' and 'col', with
6331 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
6332 * Note: only outputs within one row, message is truncated at screen boundary!
6333 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
6335 void
6336 screen_puts(text, row, col, attr)
6337 char_u *text;
6338 int row;
6339 int col;
6340 int attr;
6342 screen_puts_len(text, -1, row, col, attr);
6346 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
6347 * a NUL.
6349 void
6350 screen_puts_len(text, len, row, col, attr)
6351 char_u *text;
6352 int len;
6353 int row;
6354 int col;
6355 int attr;
6357 unsigned off;
6358 char_u *ptr = text;
6359 int c;
6360 #ifdef FEAT_MBYTE
6361 unsigned max_off;
6362 int mbyte_blen = 1;
6363 int mbyte_cells = 1;
6364 int u8c = 0;
6365 int u8cc[MAX_MCO];
6366 int clear_next_cell = FALSE;
6367 # ifdef FEAT_ARABIC
6368 int prev_c = 0; /* previous Arabic character */
6369 int pc, nc, nc1;
6370 int pcc[MAX_MCO];
6371 # endif
6372 #endif
6373 #if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6374 int force_redraw_this;
6375 int force_redraw_next = FALSE;
6376 #endif
6377 int need_redraw;
6379 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
6380 return;
6381 off = LineOffset[row] + col;
6383 #ifdef FEAT_MBYTE
6384 /* When drawing over the right halve of a double-wide char clear out the
6385 * left halve. Only needed in a terminal. */
6386 if (has_mbyte && col > 0 && col < screen_Columns
6387 # ifdef FEAT_GUI
6388 && !gui.in_use
6389 # endif
6390 && mb_fix_col(col, row) != col)
6392 ScreenLines[off - 1] = ' ';
6393 ScreenAttrs[off - 1] = 0;
6394 if (enc_utf8)
6396 ScreenLinesUC[off - 1] = 0;
6397 ScreenLinesC[0][off - 1] = 0;
6399 /* redraw the previous cell, make it empty */
6400 screen_char(off - 1, row, col - 1);
6401 /* force the cell at "col" to be redrawn */
6402 force_redraw_next = TRUE;
6404 #endif
6406 #ifdef FEAT_MBYTE
6407 max_off = LineOffset[row] + screen_Columns;
6408 #endif
6409 while (col < screen_Columns
6410 && (len < 0 || (int)(ptr - text) < len)
6411 && *ptr != NUL)
6413 c = *ptr;
6414 #ifdef FEAT_MBYTE
6415 /* check if this is the first byte of a multibyte */
6416 if (has_mbyte)
6418 if (enc_utf8 && len > 0)
6419 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
6420 else
6421 mbyte_blen = (*mb_ptr2len)(ptr);
6422 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6423 mbyte_cells = 1;
6424 else if (enc_dbcs != 0)
6425 mbyte_cells = mbyte_blen;
6426 else /* enc_utf8 */
6428 if (len >= 0)
6429 u8c = utfc_ptr2char_len(ptr, u8cc,
6430 (int)((text + len) - ptr));
6431 else
6432 u8c = utfc_ptr2char(ptr, u8cc);
6433 mbyte_cells = utf_char2cells(u8c);
6434 # ifdef UNICODE16
6435 /* Non-BMP character: display as ? or fullwidth ?. */
6436 if (u8c >= 0x10000)
6438 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
6439 if (attr == 0)
6440 attr = hl_attr(HLF_8);
6442 # endif
6443 # ifdef FEAT_ARABIC
6444 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
6446 /* Do Arabic shaping. */
6447 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
6449 /* Past end of string to be displayed. */
6450 nc = NUL;
6451 nc1 = NUL;
6453 else
6455 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
6456 (int)((text + len) - ptr - mbyte_blen));
6457 nc1 = pcc[0];
6459 pc = prev_c;
6460 prev_c = u8c;
6461 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
6463 else
6464 prev_c = u8c;
6465 # endif
6466 if (col + mbyte_cells > screen_Columns)
6468 /* Only 1 cell left, but character requires 2 cells:
6469 * display a '>' in the last column to avoid wrapping. */
6470 c = '>';
6471 mbyte_cells = 1;
6475 #endif
6477 #if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6478 force_redraw_this = force_redraw_next;
6479 force_redraw_next = FALSE;
6480 #endif
6482 need_redraw = ScreenLines[off] != c
6483 #ifdef FEAT_MBYTE
6484 || (mbyte_cells == 2
6485 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
6486 || (enc_dbcs == DBCS_JPNU
6487 && c == 0x8e
6488 && ScreenLines2[off] != ptr[1])
6489 || (enc_utf8
6490 && (ScreenLinesUC[off] != (u8char_T)(c >= 0x80 ? u8c : 0)
6491 || screen_comp_differs(off, u8cc)))
6492 #endif
6493 || ScreenAttrs[off] != attr
6494 || exmode_active;
6496 if (need_redraw
6497 #if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6498 || force_redraw_this
6499 #endif
6502 #if defined(FEAT_GUI) || defined(UNIX)
6503 /* The bold trick makes a single row of pixels appear in the next
6504 * character. When a bold character is removed, the next
6505 * character should be redrawn too. This happens for our own GUI
6506 * and for some xterms. */
6507 if (need_redraw && ScreenLines[off] != ' ' && (
6508 # ifdef FEAT_GUI
6509 gui.in_use
6510 # endif
6511 # if defined(FEAT_GUI) && defined(UNIX)
6513 # endif
6514 # ifdef UNIX
6515 term_is_xterm
6516 # endif
6519 int n = ScreenAttrs[off];
6521 if (n > HL_ALL)
6522 n = syn_attr2attr(n);
6523 if (n & HL_BOLD)
6524 force_redraw_next = TRUE;
6526 #endif
6527 #ifdef FEAT_MBYTE
6528 /* When at the end of the text and overwriting a two-cell
6529 * character with a one-cell character, need to clear the next
6530 * cell. Also when overwriting the left halve of a two-cell char
6531 * with the right halve of a two-cell char. Do this only once
6532 * (mb_off2cells() may return 2 on the right halve). */
6533 if (clear_next_cell)
6534 clear_next_cell = FALSE;
6535 else if (has_mbyte
6536 && (len < 0 ? ptr[mbyte_blen] == NUL
6537 : ptr + mbyte_blen >= text + len)
6538 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
6539 || (mbyte_cells == 2
6540 && (*mb_off2cells)(off, max_off) == 1
6541 && (*mb_off2cells)(off + 1, max_off) > 1)))
6542 clear_next_cell = TRUE;
6544 /* Make sure we never leave a second byte of a double-byte behind,
6545 * it confuses mb_off2cells(). */
6546 if (enc_dbcs
6547 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
6548 || (mbyte_cells == 2
6549 && (*mb_off2cells)(off, max_off) == 1
6550 && (*mb_off2cells)(off + 1, max_off) > 1)))
6551 ScreenLines[off + mbyte_blen] = 0;
6552 #endif
6553 ScreenLines[off] = c;
6554 ScreenAttrs[off] = attr;
6555 #ifdef FEAT_MBYTE
6556 if (enc_utf8)
6558 if (c < 0x80 && u8cc[0] == 0)
6559 ScreenLinesUC[off] = 0;
6560 else
6562 int i;
6564 ScreenLinesUC[off] = u8c;
6565 for (i = 0; i < Screen_mco; ++i)
6567 ScreenLinesC[i][off] = u8cc[i];
6568 if (u8cc[i] == 0)
6569 break;
6572 if (mbyte_cells == 2)
6574 ScreenLines[off + 1] = 0;
6575 ScreenAttrs[off + 1] = attr;
6577 screen_char(off, row, col);
6579 else if (mbyte_cells == 2)
6581 ScreenLines[off + 1] = ptr[1];
6582 ScreenAttrs[off + 1] = attr;
6583 screen_char_2(off, row, col);
6585 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6587 ScreenLines2[off] = ptr[1];
6588 screen_char(off, row, col);
6590 else
6591 #endif
6592 screen_char(off, row, col);
6594 #ifdef FEAT_MBYTE
6595 if (has_mbyte)
6597 off += mbyte_cells;
6598 col += mbyte_cells;
6599 ptr += mbyte_blen;
6600 if (clear_next_cell)
6601 ptr = (char_u *)" ";
6603 else
6604 #endif
6606 ++off;
6607 ++col;
6608 ++ptr;
6612 #if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6613 /* If we detected the next character needs to be redrawn, but the text
6614 * doesn't extend up to there, update the character here. */
6615 if (force_redraw_next && col < screen_Columns)
6617 # ifdef FEAT_MBYTE
6618 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
6619 screen_char_2(off, row, col);
6620 else
6621 # endif
6622 screen_char(off, row, col);
6624 #endif
6627 #ifdef FEAT_SEARCH_EXTRA
6629 * Prepare for 'hlsearch' highlighting.
6631 static void
6632 start_search_hl()
6634 if (p_hls && !no_hlsearch)
6636 last_pat_prog(&search_hl.rm);
6637 search_hl.attr = hl_attr(HLF_L);
6638 # ifdef FEAT_RELTIME
6639 /* Set the time limit to 'redrawtime'. */
6640 profile_setlimit(p_rdt, &search_hl.tm);
6641 # endif
6646 * Clean up for 'hlsearch' highlighting.
6648 static void
6649 end_search_hl()
6651 if (search_hl.rm.regprog != NULL)
6653 vim_free(search_hl.rm.regprog);
6654 search_hl.rm.regprog = NULL;
6659 * Advance to the match in window "wp" line "lnum" or past it.
6661 static void
6662 prepare_search_hl(wp, lnum)
6663 win_T *wp;
6664 linenr_T lnum;
6666 matchitem_T *cur; /* points to the match list */
6667 match_T *shl; /* points to search_hl or a match */
6668 int shl_flag; /* flag to indicate whether search_hl
6669 has been processed or not */
6670 int n;
6673 * When using a multi-line pattern, start searching at the top
6674 * of the window or just after a closed fold.
6675 * Do this both for search_hl and the match list.
6677 cur = wp->w_match_head;
6678 shl_flag = FALSE;
6679 while (cur != NULL || shl_flag == FALSE)
6681 if (shl_flag == FALSE)
6683 shl = &search_hl;
6684 shl_flag = TRUE;
6686 else
6687 shl = &cur->hl;
6688 if (shl->rm.regprog != NULL
6689 && shl->lnum == 0
6690 && re_multiline(shl->rm.regprog))
6692 if (shl->first_lnum == 0)
6694 # ifdef FEAT_FOLDING
6695 for (shl->first_lnum = lnum;
6696 shl->first_lnum > wp->w_topline; --shl->first_lnum)
6697 if (hasFoldingWin(wp, shl->first_lnum - 1,
6698 NULL, NULL, TRUE, NULL))
6699 break;
6700 # else
6701 shl->first_lnum = wp->w_topline;
6702 # endif
6704 n = 0;
6705 while (shl->first_lnum < lnum && shl->rm.regprog != NULL)
6707 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n);
6708 if (shl->lnum != 0)
6710 shl->first_lnum = shl->lnum
6711 + shl->rm.endpos[0].lnum
6712 - shl->rm.startpos[0].lnum;
6713 n = shl->rm.endpos[0].col;
6715 else
6717 ++shl->first_lnum;
6718 n = 0;
6722 if (shl != &search_hl && cur != NULL)
6723 cur = cur->next;
6728 * Search for a next 'hlsearch' or match.
6729 * Uses shl->buf.
6730 * Sets shl->lnum and shl->rm contents.
6731 * Note: Assumes a previous match is always before "lnum", unless
6732 * shl->lnum is zero.
6733 * Careful: Any pointers for buffer lines will become invalid.
6735 static void
6736 next_search_hl(win, shl, lnum, mincol)
6737 win_T *win;
6738 match_T *shl; /* points to search_hl or a match */
6739 linenr_T lnum;
6740 colnr_T mincol; /* minimal column for a match */
6742 linenr_T l;
6743 colnr_T matchcol;
6744 long nmatched;
6746 if (shl->lnum != 0)
6748 /* Check for three situations:
6749 * 1. If the "lnum" is below a previous match, start a new search.
6750 * 2. If the previous match includes "mincol", use it.
6751 * 3. Continue after the previous match.
6753 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
6754 if (lnum > l)
6755 shl->lnum = 0;
6756 else if (lnum < l || shl->rm.endpos[0].col > mincol)
6757 return;
6761 * Repeat searching for a match until one is found that includes "mincol"
6762 * or none is found in this line.
6764 called_emsg = FALSE;
6765 for (;;)
6767 #ifdef FEAT_RELTIME
6768 /* Stop searching after passing the time limit. */
6769 if (profile_passed_limit(&(shl->tm)))
6771 shl->lnum = 0; /* no match found in time */
6772 break;
6774 #endif
6775 /* Three situations:
6776 * 1. No useful previous match: search from start of line.
6777 * 2. Not Vi compatible or empty match: continue at next character.
6778 * Break the loop if this is beyond the end of the line.
6779 * 3. Vi compatible searching: continue at end of previous match.
6781 if (shl->lnum == 0)
6782 matchcol = 0;
6783 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
6784 || (shl->rm.endpos[0].lnum == 0
6785 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
6787 char_u *ml;
6789 matchcol = shl->rm.startpos[0].col;
6790 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
6791 if (*ml == NUL)
6793 ++matchcol;
6794 shl->lnum = 0;
6795 break;
6797 #ifdef FEAT_MBYTE
6798 if (has_mbyte)
6799 matchcol += mb_ptr2len(ml);
6800 else
6801 #endif
6802 ++matchcol;
6804 else
6805 matchcol = shl->rm.endpos[0].col;
6807 shl->lnum = lnum;
6808 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum, matchcol,
6809 #ifdef FEAT_RELTIME
6810 &(shl->tm)
6811 #else
6812 NULL
6813 #endif
6815 if (called_emsg)
6817 /* Error while handling regexp: stop using this regexp. */
6818 if (shl == &search_hl)
6820 /* don't free regprog in the match list, it's a copy */
6821 vim_free(shl->rm.regprog);
6822 no_hlsearch = TRUE;
6824 shl->rm.regprog = NULL;
6825 shl->lnum = 0;
6826 got_int = FALSE; /* avoid the "Type :quit to exit Vim" message */
6827 break;
6829 if (nmatched == 0)
6831 shl->lnum = 0; /* no match found */
6832 break;
6834 if (shl->rm.startpos[0].lnum > 0
6835 || shl->rm.startpos[0].col >= mincol
6836 || nmatched > 1
6837 || shl->rm.endpos[0].col > mincol)
6839 shl->lnum += shl->rm.startpos[0].lnum;
6840 break; /* useful match found */
6844 #endif
6846 static void
6847 screen_start_highlight(attr)
6848 int attr;
6850 attrentry_T *aep = NULL;
6852 screen_attr = attr;
6853 if (full_screen
6854 #ifdef WIN3264
6855 && termcap_active
6856 #endif
6859 #ifdef FEAT_GUI
6860 if (gui.in_use)
6862 char buf[20];
6864 /* The GUI handles this internally. */
6865 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
6866 OUT_STR(buf);
6868 else
6869 #endif
6871 if (attr > HL_ALL) /* special HL attr. */
6873 if (t_colors > 1)
6874 aep = syn_cterm_attr2entry(attr);
6875 else
6876 aep = syn_term_attr2entry(attr);
6877 if (aep == NULL) /* did ":syntax clear" */
6878 attr = 0;
6879 else
6880 attr = aep->ae_attr;
6882 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
6883 out_str(T_MD);
6884 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
6885 && cterm_normal_fg_bold)
6886 /* If the Normal FG color has BOLD attribute and the new HL
6887 * has a FG color defined, clear BOLD. */
6888 out_str(T_ME);
6889 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
6890 out_str(T_SO);
6891 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
6892 /* underline or undercurl */
6893 out_str(T_US);
6894 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
6895 out_str(T_CZH);
6896 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
6897 out_str(T_MR);
6900 * Output the color or start string after bold etc., in case the
6901 * bold etc. override the color setting.
6903 if (aep != NULL)
6905 if (t_colors > 1)
6907 if (aep->ae_u.cterm.fg_color)
6908 term_fg_color(aep->ae_u.cterm.fg_color - 1);
6909 if (aep->ae_u.cterm.bg_color)
6910 term_bg_color(aep->ae_u.cterm.bg_color - 1);
6912 else
6914 if (aep->ae_u.term.start != NULL)
6915 out_str(aep->ae_u.term.start);
6922 void
6923 screen_stop_highlight()
6925 int do_ME = FALSE; /* output T_ME code */
6927 if (screen_attr != 0
6928 #ifdef WIN3264
6929 && termcap_active
6930 #endif
6933 #ifdef FEAT_GUI
6934 if (gui.in_use)
6936 char buf[20];
6938 /* use internal GUI code */
6939 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
6940 OUT_STR(buf);
6942 else
6943 #endif
6945 if (screen_attr > HL_ALL) /* special HL attr. */
6947 attrentry_T *aep;
6949 if (t_colors > 1)
6952 * Assume that t_me restores the original colors!
6954 aep = syn_cterm_attr2entry(screen_attr);
6955 if (aep != NULL && (aep->ae_u.cterm.fg_color
6956 || aep->ae_u.cterm.bg_color))
6957 do_ME = TRUE;
6959 else
6961 aep = syn_term_attr2entry(screen_attr);
6962 if (aep != NULL && aep->ae_u.term.stop != NULL)
6964 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
6965 do_ME = TRUE;
6966 else
6967 out_str(aep->ae_u.term.stop);
6970 if (aep == NULL) /* did ":syntax clear" */
6971 screen_attr = 0;
6972 else
6973 screen_attr = aep->ae_attr;
6977 * Often all ending-codes are equal to T_ME. Avoid outputting the
6978 * same sequence several times.
6980 if (screen_attr & HL_STANDOUT)
6982 if (STRCMP(T_SE, T_ME) == 0)
6983 do_ME = TRUE;
6984 else
6985 out_str(T_SE);
6987 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
6989 if (STRCMP(T_UE, T_ME) == 0)
6990 do_ME = TRUE;
6991 else
6992 out_str(T_UE);
6994 if (screen_attr & HL_ITALIC)
6996 if (STRCMP(T_CZR, T_ME) == 0)
6997 do_ME = TRUE;
6998 else
6999 out_str(T_CZR);
7001 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
7002 out_str(T_ME);
7004 if (t_colors > 1)
7006 /* set Normal cterm colors */
7007 if (cterm_normal_fg_color != 0)
7008 term_fg_color(cterm_normal_fg_color - 1);
7009 if (cterm_normal_bg_color != 0)
7010 term_bg_color(cterm_normal_bg_color - 1);
7011 if (cterm_normal_fg_bold)
7012 out_str(T_MD);
7016 screen_attr = 0;
7020 * Reset the colors for a cterm. Used when leaving Vim.
7021 * The machine specific code may override this again.
7023 void
7024 reset_cterm_colors()
7026 if (t_colors > 1)
7028 /* set Normal cterm colors */
7029 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
7031 out_str(T_OP);
7032 screen_attr = -1;
7034 if (cterm_normal_fg_bold)
7036 out_str(T_ME);
7037 screen_attr = -1;
7043 * Put character ScreenLines["off"] on the screen at position "row" and "col",
7044 * using the attributes from ScreenAttrs["off"].
7046 static void
7047 screen_char(off, row, col)
7048 unsigned off;
7049 int row;
7050 int col;
7052 int attr;
7054 /* Check for illegal values, just in case (could happen just after
7055 * resizing). */
7056 if (row >= screen_Rows || col >= screen_Columns)
7057 return;
7059 /* Outputting the last character on the screen may scrollup the screen.
7060 * Don't to it! Mark the character invalid (update it when scrolled up) */
7061 if (row == screen_Rows - 1 && col == screen_Columns - 1
7062 #ifdef FEAT_RIGHTLEFT
7063 /* account for first command-line character in rightleft mode */
7064 && !cmdmsg_rl
7065 #endif
7068 ScreenAttrs[off] = (sattr_T)-1;
7069 return;
7073 * Stop highlighting first, so it's easier to move the cursor.
7075 #if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
7076 if (screen_char_attr != 0)
7077 attr = screen_char_attr;
7078 else
7079 #endif
7080 attr = ScreenAttrs[off];
7081 if (screen_attr != attr)
7082 screen_stop_highlight();
7084 windgoto(row, col);
7086 if (screen_attr != attr)
7087 screen_start_highlight(attr);
7089 #ifdef FEAT_MBYTE
7090 if (enc_utf8 && ScreenLinesUC[off] != 0)
7092 char_u buf[MB_MAXBYTES + 1];
7094 /* Convert UTF-8 character to bytes and write it. */
7096 buf[utfc_char2bytes(off, buf)] = NUL;
7098 out_str(buf);
7099 if (utf_char2cells(ScreenLinesUC[off]) > 1)
7100 ++screen_cur_col;
7102 else
7103 #endif
7105 #ifdef FEAT_MBYTE
7106 out_flush_check();
7107 #endif
7108 out_char(ScreenLines[off]);
7109 #ifdef FEAT_MBYTE
7110 /* double-byte character in single-width cell */
7111 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7112 out_char(ScreenLines2[off]);
7113 #endif
7116 screen_cur_col++;
7119 #ifdef FEAT_MBYTE
7122 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
7123 * on the screen at position 'row' and 'col'.
7124 * The attributes of the first byte is used for all. This is required to
7125 * output the two bytes of a double-byte character with nothing in between.
7127 static void
7128 screen_char_2(off, row, col)
7129 unsigned off;
7130 int row;
7131 int col;
7133 /* Check for illegal values (could be wrong when screen was resized). */
7134 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
7135 return;
7137 /* Outputting the last character on the screen may scrollup the screen.
7138 * Don't to it! Mark the character invalid (update it when scrolled up) */
7139 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
7141 ScreenAttrs[off] = (sattr_T)-1;
7142 return;
7145 /* Output the first byte normally (positions the cursor), then write the
7146 * second byte directly. */
7147 screen_char(off, row, col);
7148 out_char(ScreenLines[off + 1]);
7149 ++screen_cur_col;
7151 #endif
7153 #if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
7155 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
7156 * This uses the contents of ScreenLines[] and doesn't change it.
7158 void
7159 screen_draw_rectangle(row, col, height, width, invert)
7160 int row;
7161 int col;
7162 int height;
7163 int width;
7164 int invert;
7166 int r, c;
7167 int off;
7168 #ifdef FEAT_MBYTE
7169 int max_off;
7170 #endif
7172 /* Can't use ScreenLines unless initialized */
7173 if (ScreenLines == NULL)
7174 return;
7176 if (invert)
7177 screen_char_attr = HL_INVERSE;
7178 for (r = row; r < row + height; ++r)
7180 off = LineOffset[r];
7181 #ifdef FEAT_MBYTE
7182 max_off = off + screen_Columns;
7183 #endif
7184 for (c = col; c < col + width; ++c)
7186 #ifdef FEAT_MBYTE
7187 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
7189 screen_char_2(off + c, r, c);
7190 ++c;
7192 else
7193 #endif
7195 screen_char(off + c, r, c);
7196 #ifdef FEAT_MBYTE
7197 if (utf_off2cells(off + c, max_off) > 1)
7198 ++c;
7199 #endif
7203 screen_char_attr = 0;
7205 #endif
7207 #ifdef FEAT_VERTSPLIT
7209 * Redraw the characters for a vertically split window.
7211 static void
7212 redraw_block(row, end, wp)
7213 int row;
7214 int end;
7215 win_T *wp;
7217 int col;
7218 int width;
7220 # ifdef FEAT_CLIPBOARD
7221 clip_may_clear_selection(row, end - 1);
7222 # endif
7224 if (wp == NULL)
7226 col = 0;
7227 width = Columns;
7229 else
7231 col = wp->w_wincol;
7232 width = wp->w_width;
7234 screen_draw_rectangle(row, col, end - row, width, FALSE);
7236 #endif
7239 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
7240 * with character 'c1' in first column followed by 'c2' in the other columns.
7241 * Use attributes 'attr'.
7243 void
7244 screen_fill(start_row, end_row, start_col, end_col, c1, c2, attr)
7245 int start_row, end_row;
7246 int start_col, end_col;
7247 int c1, c2;
7248 int attr;
7250 int row;
7251 int col;
7252 int off;
7253 int end_off;
7254 int did_delete;
7255 int c;
7256 int norm_term;
7257 #if defined(FEAT_GUI) || defined(UNIX)
7258 int force_next = FALSE;
7259 #endif
7261 if (end_row > screen_Rows) /* safety check */
7262 end_row = screen_Rows;
7263 if (end_col > screen_Columns) /* safety check */
7264 end_col = screen_Columns;
7265 if (ScreenLines == NULL
7266 || start_row >= end_row
7267 || start_col >= end_col) /* nothing to do */
7268 return;
7270 /* it's a "normal" terminal when not in a GUI or cterm */
7271 norm_term = (
7272 #ifdef FEAT_GUI
7273 !gui.in_use &&
7274 #endif
7275 t_colors <= 1);
7276 for (row = start_row; row < end_row; ++row)
7278 #ifdef FEAT_MBYTE
7279 if (has_mbyte
7280 # ifdef FEAT_GUI
7281 && !gui.in_use
7282 # endif
7285 /* When drawing over the right halve of a double-wide char clear
7286 * out the left halve. When drawing over the left halve of a
7287 * double wide-char clear out the right halve. Only needed in a
7288 * terminal. */
7289 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
7290 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
7291 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
7292 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
7294 #endif
7296 * Try to use delete-line termcap code, when no attributes or in a
7297 * "normal" terminal, where a bold/italic space is just a
7298 * space.
7300 did_delete = FALSE;
7301 if (c2 == ' '
7302 && end_col == Columns
7303 && can_clear(T_CE)
7304 && (attr == 0
7305 || (norm_term
7306 && attr <= HL_ALL
7307 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
7310 * check if we really need to clear something
7312 col = start_col;
7313 if (c1 != ' ') /* don't clear first char */
7314 ++col;
7316 off = LineOffset[row] + col;
7317 end_off = LineOffset[row] + end_col;
7319 /* skip blanks (used often, keep it fast!) */
7320 #ifdef FEAT_MBYTE
7321 if (enc_utf8)
7322 while (off < end_off && ScreenLines[off] == ' '
7323 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
7324 ++off;
7325 else
7326 #endif
7327 while (off < end_off && ScreenLines[off] == ' '
7328 && ScreenAttrs[off] == 0)
7329 ++off;
7330 if (off < end_off) /* something to be cleared */
7332 col = off - LineOffset[row];
7333 screen_stop_highlight();
7334 term_windgoto(row, col);/* clear rest of this screen line */
7335 out_str(T_CE);
7336 screen_start(); /* don't know where cursor is now */
7337 col = end_col - col;
7338 while (col--) /* clear chars in ScreenLines */
7340 ScreenLines[off] = ' ';
7341 #ifdef FEAT_MBYTE
7342 if (enc_utf8)
7343 ScreenLinesUC[off] = 0;
7344 #endif
7345 ScreenAttrs[off] = 0;
7346 ++off;
7349 did_delete = TRUE; /* the chars are cleared now */
7352 off = LineOffset[row] + start_col;
7353 c = c1;
7354 for (col = start_col; col < end_col; ++col)
7356 if (ScreenLines[off] != c
7357 #ifdef FEAT_MBYTE
7358 || (enc_utf8 && (int)ScreenLinesUC[off]
7359 != (c >= 0x80 ? c : 0))
7360 #endif
7361 || ScreenAttrs[off] != attr
7362 #if defined(FEAT_GUI) || defined(UNIX)
7363 || force_next
7364 #endif
7367 #if defined(FEAT_GUI) || defined(UNIX)
7368 /* The bold trick may make a single row of pixels appear in
7369 * the next character. When a bold character is removed, the
7370 * next character should be redrawn too. This happens for our
7371 * own GUI and for some xterms. */
7372 if (
7373 # ifdef FEAT_GUI
7374 gui.in_use
7375 # endif
7376 # if defined(FEAT_GUI) && defined(UNIX)
7378 # endif
7379 # ifdef UNIX
7380 term_is_xterm
7381 # endif
7384 if (ScreenLines[off] != ' '
7385 && (ScreenAttrs[off] > HL_ALL
7386 || ScreenAttrs[off] & HL_BOLD))
7387 force_next = TRUE;
7388 else
7389 force_next = FALSE;
7391 #endif
7392 ScreenLines[off] = c;
7393 #ifdef FEAT_MBYTE
7394 if (enc_utf8)
7396 if (c >= 0x80)
7398 ScreenLinesUC[off] = c;
7399 ScreenLinesC[0][off] = 0;
7401 else
7402 ScreenLinesUC[off] = 0;
7404 #endif
7405 ScreenAttrs[off] = attr;
7406 if (!did_delete || c != ' ')
7407 screen_char(off, row, col);
7409 ++off;
7410 if (col == start_col)
7412 if (did_delete)
7413 break;
7414 c = c2;
7417 if (end_col == Columns)
7418 LineWraps[row] = FALSE;
7419 if (row == Rows - 1) /* overwritten the command line */
7421 redraw_cmdline = TRUE;
7422 if (c1 == ' ' && c2 == ' ')
7423 clear_cmdline = FALSE; /* command line has been cleared */
7424 if (start_col == 0)
7425 mode_displayed = FALSE; /* mode cleared or overwritten */
7431 * Check if there should be a delay. Used before clearing or redrawing the
7432 * screen or the command line.
7434 void
7435 check_for_delay(check_msg_scroll)
7436 int check_msg_scroll;
7438 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
7439 && !did_wait_return
7440 && emsg_silent == 0)
7442 out_flush();
7443 ui_delay(1000L, TRUE);
7444 emsg_on_display = FALSE;
7445 if (check_msg_scroll)
7446 msg_scroll = FALSE;
7451 * screen_valid - allocate screen buffers if size changed
7452 * If "clear" is TRUE: clear screen if it has been resized.
7453 * Returns TRUE if there is a valid screen to write to.
7454 * Returns FALSE when starting up and screen not initialized yet.
7457 screen_valid(clear)
7458 int clear;
7460 screenalloc(clear); /* allocate screen buffers if size changed */
7461 return (ScreenLines != NULL);
7465 * Resize the shell to Rows and Columns.
7466 * Allocate ScreenLines[] and associated items.
7468 * There may be some time between setting Rows and Columns and (re)allocating
7469 * ScreenLines[]. This happens when starting up and when (manually) changing
7470 * the shell size. Always use screen_Rows and screen_Columns to access items
7471 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
7472 * final size of the shell is needed.
7474 void
7475 screenalloc(clear)
7476 int clear;
7478 int new_row, old_row;
7479 #ifdef FEAT_GUI
7480 int old_Rows;
7481 #endif
7482 win_T *wp;
7483 int outofmem = FALSE;
7484 int len;
7485 schar_T *new_ScreenLines;
7486 #ifdef FEAT_MBYTE
7487 u8char_T *new_ScreenLinesUC = NULL;
7488 u8char_T *new_ScreenLinesC[MAX_MCO];
7489 schar_T *new_ScreenLines2 = NULL;
7490 int i;
7491 #endif
7492 sattr_T *new_ScreenAttrs;
7493 unsigned *new_LineOffset;
7494 char_u *new_LineWraps;
7495 #ifdef FEAT_WINDOWS
7496 short *new_TabPageIdxs;
7497 tabpage_T *tp;
7498 #endif
7499 static int entered = FALSE; /* avoid recursiveness */
7500 static int done_outofmem_msg = FALSE; /* did outofmem message */
7501 #ifdef FEAT_AUTOCMD
7502 int retry_count = 0;
7504 retry:
7505 #endif
7507 * Allocation of the screen buffers is done only when the size changes and
7508 * when Rows and Columns have been set and we have started doing full
7509 * screen stuff.
7511 if ((ScreenLines != NULL
7512 && Rows == screen_Rows
7513 && Columns == screen_Columns
7514 #ifdef FEAT_MBYTE
7515 && enc_utf8 == (ScreenLinesUC != NULL)
7516 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
7517 && p_mco == Screen_mco
7518 #endif
7520 || Rows == 0
7521 || Columns == 0
7522 || (!full_screen && ScreenLines == NULL))
7523 return;
7526 * It's possible that we produce an out-of-memory message below, which
7527 * will cause this function to be called again. To break the loop, just
7528 * return here.
7530 if (entered)
7531 return;
7532 entered = TRUE;
7535 * Note that the window sizes are updated before reallocating the arrays,
7536 * thus we must not redraw here!
7538 ++RedrawingDisabled;
7540 win_new_shellsize(); /* fit the windows in the new sized shell */
7542 comp_col(); /* recompute columns for shown command and ruler */
7545 * We're changing the size of the screen.
7546 * - Allocate new arrays for ScreenLines and ScreenAttrs.
7547 * - Move lines from the old arrays into the new arrays, clear extra
7548 * lines (unless the screen is going to be cleared).
7549 * - Free the old arrays.
7551 * If anything fails, make ScreenLines NULL, so we don't do anything!
7552 * Continuing with the old ScreenLines may result in a crash, because the
7553 * size is wrong.
7555 FOR_ALL_TAB_WINDOWS(tp, wp)
7556 win_free_lsize(wp);
7557 #ifdef FEAT_AUTOCMD
7558 if (aucmd_win != NULL)
7559 win_free_lsize(aucmd_win);
7560 #endif
7562 new_ScreenLines = (schar_T *)lalloc((long_u)(
7563 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7564 #ifdef FEAT_MBYTE
7565 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T) * MAX_MCO);
7566 if (enc_utf8)
7568 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
7569 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
7570 for (i = 0; i < p_mco; ++i)
7571 new_ScreenLinesC[i] = (u8char_T *)lalloc((long_u)(
7572 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
7574 if (enc_dbcs == DBCS_JPNU)
7575 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
7576 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7577 #endif
7578 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
7579 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
7580 new_LineOffset = (unsigned *)lalloc((long_u)(
7581 Rows * sizeof(unsigned)), FALSE);
7582 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
7583 #ifdef FEAT_WINDOWS
7584 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
7585 #endif
7587 FOR_ALL_TAB_WINDOWS(tp, wp)
7589 if (win_alloc_lines(wp) == FAIL)
7591 outofmem = TRUE;
7592 #ifdef FEAT_WINDOWS
7593 goto give_up;
7594 #endif
7597 #ifdef FEAT_AUTOCMD
7598 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
7599 && win_alloc_lines(aucmd_win) == FAIL)
7600 outofmem = TRUE;
7601 #endif
7602 #ifdef FEAT_WINDOWS
7603 give_up:
7604 #endif
7606 #ifdef FEAT_MBYTE
7607 for (i = 0; i < p_mco; ++i)
7608 if (new_ScreenLinesC[i] == NULL)
7609 break;
7610 #endif
7611 if (new_ScreenLines == NULL
7612 #ifdef FEAT_MBYTE
7613 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
7614 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
7615 #endif
7616 || new_ScreenAttrs == NULL
7617 || new_LineOffset == NULL
7618 || new_LineWraps == NULL
7619 #ifdef FEAT_WINDOWS
7620 || new_TabPageIdxs == NULL
7621 #endif
7622 || outofmem)
7624 if (ScreenLines != NULL || !done_outofmem_msg)
7626 /* guess the size */
7627 do_outofmem_msg((long_u)((Rows + 1) * Columns));
7629 /* Remember we did this to avoid getting outofmem messages over
7630 * and over again. */
7631 done_outofmem_msg = TRUE;
7633 vim_free(new_ScreenLines);
7634 new_ScreenLines = NULL;
7635 #ifdef FEAT_MBYTE
7636 vim_free(new_ScreenLinesUC);
7637 new_ScreenLinesUC = NULL;
7638 for (i = 0; i < p_mco; ++i)
7640 vim_free(new_ScreenLinesC[i]);
7641 new_ScreenLinesC[i] = NULL;
7643 vim_free(new_ScreenLines2);
7644 new_ScreenLines2 = NULL;
7645 #endif
7646 vim_free(new_ScreenAttrs);
7647 new_ScreenAttrs = NULL;
7648 vim_free(new_LineOffset);
7649 new_LineOffset = NULL;
7650 vim_free(new_LineWraps);
7651 new_LineWraps = NULL;
7652 #ifdef FEAT_WINDOWS
7653 vim_free(new_TabPageIdxs);
7654 new_TabPageIdxs = NULL;
7655 #endif
7657 else
7659 done_outofmem_msg = FALSE;
7661 for (new_row = 0; new_row < Rows; ++new_row)
7663 new_LineOffset[new_row] = new_row * Columns;
7664 new_LineWraps[new_row] = FALSE;
7667 * If the screen is not going to be cleared, copy as much as
7668 * possible from the old screen to the new one and clear the rest
7669 * (used when resizing the window at the "--more--" prompt or when
7670 * executing an external command, for the GUI).
7672 if (!clear)
7674 (void)vim_memset(new_ScreenLines + new_row * Columns,
7675 ' ', (size_t)Columns * sizeof(schar_T));
7676 #ifdef FEAT_MBYTE
7677 if (enc_utf8)
7679 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
7680 0, (size_t)Columns * sizeof(u8char_T));
7681 for (i = 0; i < p_mco; ++i)
7682 (void)vim_memset(new_ScreenLinesC[i]
7683 + new_row * Columns,
7684 0, (size_t)Columns * sizeof(u8char_T));
7686 if (enc_dbcs == DBCS_JPNU)
7687 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
7688 0, (size_t)Columns * sizeof(schar_T));
7689 #endif
7690 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
7691 0, (size_t)Columns * sizeof(sattr_T));
7692 old_row = new_row + (screen_Rows - Rows);
7693 if (old_row >= 0 && ScreenLines != NULL)
7695 if (screen_Columns < Columns)
7696 len = screen_Columns;
7697 else
7698 len = Columns;
7699 #ifdef FEAT_MBYTE
7700 /* When switching to utf-8 don't copy characters, they
7701 * may be invalid now. Also when p_mco changes. */
7702 if (!(enc_utf8 && ScreenLinesUC == NULL)
7703 && p_mco == Screen_mco)
7704 #endif
7705 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
7706 ScreenLines + LineOffset[old_row],
7707 (size_t)len * sizeof(schar_T));
7708 #ifdef FEAT_MBYTE
7709 if (enc_utf8 && ScreenLinesUC != NULL
7710 && p_mco == Screen_mco)
7712 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
7713 ScreenLinesUC + LineOffset[old_row],
7714 (size_t)len * sizeof(u8char_T));
7715 for (i = 0; i < p_mco; ++i)
7716 mch_memmove(new_ScreenLinesC[i]
7717 + new_LineOffset[new_row],
7718 ScreenLinesC[i] + LineOffset[old_row],
7719 (size_t)len * sizeof(u8char_T));
7721 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
7722 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
7723 ScreenLines2 + LineOffset[old_row],
7724 (size_t)len * sizeof(schar_T));
7725 #endif
7726 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
7727 ScreenAttrs + LineOffset[old_row],
7728 (size_t)len * sizeof(sattr_T));
7732 /* Use the last line of the screen for the current line. */
7733 current_ScreenLine = new_ScreenLines + Rows * Columns;
7736 free_screenlines();
7738 ScreenLines = new_ScreenLines;
7739 #ifdef FEAT_MBYTE
7740 ScreenLinesUC = new_ScreenLinesUC;
7741 for (i = 0; i < p_mco; ++i)
7742 ScreenLinesC[i] = new_ScreenLinesC[i];
7743 Screen_mco = p_mco;
7744 ScreenLines2 = new_ScreenLines2;
7745 #endif
7746 ScreenAttrs = new_ScreenAttrs;
7747 LineOffset = new_LineOffset;
7748 LineWraps = new_LineWraps;
7749 #ifdef FEAT_WINDOWS
7750 TabPageIdxs = new_TabPageIdxs;
7751 #endif
7753 /* It's important that screen_Rows and screen_Columns reflect the actual
7754 * size of ScreenLines[]. Set them before calling anything. */
7755 #ifdef FEAT_GUI
7756 old_Rows = screen_Rows;
7757 #endif
7758 screen_Rows = Rows;
7759 screen_Columns = Columns;
7761 must_redraw = CLEAR; /* need to clear the screen later */
7762 if (clear)
7763 screenclear2();
7765 #ifdef FEAT_GUI
7766 else if (gui.in_use
7767 && !gui.starting
7768 && ScreenLines != NULL
7769 && old_Rows != Rows)
7771 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
7773 * Adjust the position of the cursor, for when executing an external
7774 * command.
7776 if (msg_row >= Rows) /* Rows got smaller */
7777 msg_row = Rows - 1; /* put cursor at last row */
7778 else if (Rows > old_Rows) /* Rows got bigger */
7779 msg_row += Rows - old_Rows; /* put cursor in same place */
7780 if (msg_col >= Columns) /* Columns got smaller */
7781 msg_col = Columns - 1; /* put cursor at last column */
7783 #endif
7785 entered = FALSE;
7786 --RedrawingDisabled;
7788 #ifdef FEAT_AUTOCMD
7790 * Do not apply autocommands more than 3 times to avoid an endless loop
7791 * in case applying autocommands always changes Rows or Columns.
7793 if (starting == 0 && ++retry_count <= 3)
7795 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
7796 /* In rare cases, autocommands may have altered Rows or Columns,
7797 * jump back to check if we need to allocate the screen again. */
7798 goto retry;
7800 #endif
7803 void
7804 free_screenlines()
7806 #ifdef FEAT_MBYTE
7807 int i;
7809 vim_free(ScreenLinesUC);
7810 for (i = 0; i < Screen_mco; ++i)
7811 vim_free(ScreenLinesC[i]);
7812 vim_free(ScreenLines2);
7813 #endif
7814 vim_free(ScreenLines);
7815 vim_free(ScreenAttrs);
7816 vim_free(LineOffset);
7817 vim_free(LineWraps);
7818 #ifdef FEAT_WINDOWS
7819 vim_free(TabPageIdxs);
7820 #endif
7823 void
7824 screenclear()
7826 check_for_delay(FALSE);
7827 screenalloc(FALSE); /* allocate screen buffers if size changed */
7828 screenclear2(); /* clear the screen */
7831 static void
7832 screenclear2()
7834 int i;
7836 if (starting == NO_SCREEN || ScreenLines == NULL
7837 #ifdef FEAT_GUI
7838 || (gui.in_use && gui.starting)
7839 #endif
7841 return;
7843 #ifdef FEAT_GUI
7844 if (!gui.in_use)
7845 #endif
7846 screen_attr = -1; /* force setting the Normal colors */
7847 screen_stop_highlight(); /* don't want highlighting here */
7849 #ifdef FEAT_CLIPBOARD
7850 /* disable selection without redrawing it */
7851 clip_scroll_selection(9999);
7852 #endif
7854 /* blank out ScreenLines */
7855 for (i = 0; i < Rows; ++i)
7857 lineclear(LineOffset[i], (int)Columns);
7858 LineWraps[i] = FALSE;
7861 if (can_clear(T_CL))
7863 out_str(T_CL); /* clear the display */
7864 clear_cmdline = FALSE;
7865 mode_displayed = FALSE;
7867 else
7869 /* can't clear the screen, mark all chars with invalid attributes */
7870 for (i = 0; i < Rows; ++i)
7871 lineinvalid(LineOffset[i], (int)Columns);
7872 clear_cmdline = TRUE;
7875 screen_cleared = TRUE; /* can use contents of ScreenLines now */
7877 win_rest_invalid(firstwin);
7878 redraw_cmdline = TRUE;
7879 #ifdef FEAT_WINDOWS
7880 redraw_tabline = TRUE;
7881 #endif
7882 if (must_redraw == CLEAR) /* no need to clear again */
7883 must_redraw = NOT_VALID;
7884 compute_cmdrow();
7885 msg_row = cmdline_row; /* put cursor on last line for messages */
7886 msg_col = 0;
7887 screen_start(); /* don't know where cursor is now */
7888 msg_scrolled = 0; /* can't scroll back */
7889 msg_didany = FALSE;
7890 msg_didout = FALSE;
7894 * Clear one line in ScreenLines.
7896 static void
7897 lineclear(off, width)
7898 unsigned off;
7899 int width;
7901 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
7902 #ifdef FEAT_MBYTE
7903 if (enc_utf8)
7904 (void)vim_memset(ScreenLinesUC + off, 0,
7905 (size_t)width * sizeof(u8char_T));
7906 #endif
7907 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
7911 * Mark one line in ScreenLines invalid by setting the attributes to an
7912 * invalid value.
7914 static void
7915 lineinvalid(off, width)
7916 unsigned off;
7917 int width;
7919 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
7922 #ifdef FEAT_VERTSPLIT
7924 * Copy part of a Screenline for vertically split window "wp".
7926 static void
7927 linecopy(to, from, wp)
7928 int to;
7929 int from;
7930 win_T *wp;
7932 unsigned off_to = LineOffset[to] + wp->w_wincol;
7933 unsigned off_from = LineOffset[from] + wp->w_wincol;
7935 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
7936 wp->w_width * sizeof(schar_T));
7937 # ifdef FEAT_MBYTE
7938 if (enc_utf8)
7940 int i;
7942 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
7943 wp->w_width * sizeof(u8char_T));
7944 for (i = 0; i < p_mco; ++i)
7945 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
7946 wp->w_width * sizeof(u8char_T));
7948 if (enc_dbcs == DBCS_JPNU)
7949 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
7950 wp->w_width * sizeof(schar_T));
7951 # endif
7952 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
7953 wp->w_width * sizeof(sattr_T));
7955 #endif
7958 * Return TRUE if clearing with term string "p" would work.
7959 * It can't work when the string is empty or it won't set the right background.
7962 can_clear(p)
7963 char_u *p;
7965 return (*p != NUL && (t_colors <= 1
7966 #ifdef FEAT_GUI
7967 || gui.in_use
7968 #endif
7969 || cterm_normal_bg_color == 0 || *T_UT != NUL));
7973 * Reset cursor position. Use whenever cursor was moved because of outputting
7974 * something directly to the screen (shell commands) or a terminal control
7975 * code.
7977 void
7978 screen_start()
7980 screen_cur_row = screen_cur_col = 9999;
7984 * Move the cursor to position "row","col" in the screen.
7985 * This tries to find the most efficient way to move, minimizing the number of
7986 * characters sent to the terminal.
7988 void
7989 windgoto(row, col)
7990 int row;
7991 int col;
7993 sattr_T *p;
7994 int i;
7995 int plan;
7996 int cost;
7997 int wouldbe_col;
7998 int noinvcurs;
7999 char_u *bs;
8000 int goto_cost;
8001 int attr;
8003 #define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
8004 #define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
8006 #define PLAN_LE 1
8007 #define PLAN_CR 2
8008 #define PLAN_NL 3
8009 #define PLAN_WRITE 4
8010 /* Can't use ScreenLines unless initialized */
8011 if (ScreenLines == NULL)
8012 return;
8014 if (col != screen_cur_col || row != screen_cur_row)
8016 /* Check for valid position. */
8017 if (row < 0) /* window without text lines? */
8018 row = 0;
8019 if (row >= screen_Rows)
8020 row = screen_Rows - 1;
8021 if (col >= screen_Columns)
8022 col = screen_Columns - 1;
8024 /* check if no cursor movement is allowed in highlight mode */
8025 if (screen_attr && *T_MS == NUL)
8026 noinvcurs = HIGHL_COST;
8027 else
8028 noinvcurs = 0;
8029 goto_cost = GOTO_COST + noinvcurs;
8032 * Plan how to do the positioning:
8033 * 1. Use CR to move it to column 0, same row.
8034 * 2. Use T_LE to move it a few columns to the left.
8035 * 3. Use NL to move a few lines down, column 0.
8036 * 4. Move a few columns to the right with T_ND or by writing chars.
8038 * Don't do this if the cursor went beyond the last column, the cursor
8039 * position is unknown then (some terminals wrap, some don't )
8041 * First check if the highlighting attributes allow us to write
8042 * characters to move the cursor to the right.
8044 if (row >= screen_cur_row && screen_cur_col < Columns)
8047 * If the cursor is in the same row, bigger col, we can use CR
8048 * or T_LE.
8050 bs = NULL; /* init for GCC */
8051 attr = screen_attr;
8052 if (row == screen_cur_row && col < screen_cur_col)
8054 /* "le" is preferred over "bc", because "bc" is obsolete */
8055 if (*T_LE)
8056 bs = T_LE; /* "cursor left" */
8057 else
8058 bs = T_BC; /* "backspace character (old) */
8059 if (*bs)
8060 cost = (screen_cur_col - col) * (int)STRLEN(bs);
8061 else
8062 cost = 999;
8063 if (col + 1 < cost) /* using CR is less characters */
8065 plan = PLAN_CR;
8066 wouldbe_col = 0;
8067 cost = 1; /* CR is just one character */
8069 else
8071 plan = PLAN_LE;
8072 wouldbe_col = col;
8074 if (noinvcurs) /* will stop highlighting */
8076 cost += noinvcurs;
8077 attr = 0;
8082 * If the cursor is above where we want to be, we can use CR LF.
8084 else if (row > screen_cur_row)
8086 plan = PLAN_NL;
8087 wouldbe_col = 0;
8088 cost = (row - screen_cur_row) * 2; /* CR LF */
8089 if (noinvcurs) /* will stop highlighting */
8091 cost += noinvcurs;
8092 attr = 0;
8097 * If the cursor is in the same row, smaller col, just use write.
8099 else
8101 plan = PLAN_WRITE;
8102 wouldbe_col = screen_cur_col;
8103 cost = 0;
8107 * Check if any characters that need to be written have the
8108 * correct attributes. Also avoid UTF-8 characters.
8110 i = col - wouldbe_col;
8111 if (i > 0)
8112 cost += i;
8113 if (cost < goto_cost && i > 0)
8116 * Check if the attributes are correct without additionally
8117 * stopping highlighting.
8119 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
8120 while (i && *p++ == attr)
8121 --i;
8122 if (i != 0)
8125 * Try if it works when highlighting is stopped here.
8127 if (*--p == 0)
8129 cost += noinvcurs;
8130 while (i && *p++ == 0)
8131 --i;
8133 if (i != 0)
8134 cost = 999; /* different attributes, don't do it */
8136 #ifdef FEAT_MBYTE
8137 if (enc_utf8)
8139 /* Don't use an UTF-8 char for positioning, it's slow. */
8140 for (i = wouldbe_col; i < col; ++i)
8141 if (ScreenLinesUC[LineOffset[row] + i] != 0)
8143 cost = 999;
8144 break;
8147 #endif
8151 * We can do it without term_windgoto()!
8153 if (cost < goto_cost)
8155 if (plan == PLAN_LE)
8157 if (noinvcurs)
8158 screen_stop_highlight();
8159 while (screen_cur_col > col)
8161 out_str(bs);
8162 --screen_cur_col;
8165 else if (plan == PLAN_CR)
8167 if (noinvcurs)
8168 screen_stop_highlight();
8169 out_char('\r');
8170 screen_cur_col = 0;
8172 else if (plan == PLAN_NL)
8174 if (noinvcurs)
8175 screen_stop_highlight();
8176 while (screen_cur_row < row)
8178 out_char('\n');
8179 ++screen_cur_row;
8181 screen_cur_col = 0;
8184 i = col - screen_cur_col;
8185 if (i > 0)
8188 * Use cursor-right if it's one character only. Avoids
8189 * removing a line of pixels from the last bold char, when
8190 * using the bold trick in the GUI.
8192 if (T_ND[0] != NUL && T_ND[1] == NUL)
8194 while (i-- > 0)
8195 out_char(*T_ND);
8197 else
8199 int off;
8201 off = LineOffset[row] + screen_cur_col;
8202 while (i-- > 0)
8204 if (ScreenAttrs[off] != screen_attr)
8205 screen_stop_highlight();
8206 #ifdef FEAT_MBYTE
8207 out_flush_check();
8208 #endif
8209 out_char(ScreenLines[off]);
8210 #ifdef FEAT_MBYTE
8211 if (enc_dbcs == DBCS_JPNU
8212 && ScreenLines[off] == 0x8e)
8213 out_char(ScreenLines2[off]);
8214 #endif
8215 ++off;
8221 else
8222 cost = 999;
8224 if (cost >= goto_cost)
8226 if (noinvcurs)
8227 screen_stop_highlight();
8228 if (row == screen_cur_row && (col > screen_cur_col) &&
8229 *T_CRI != NUL)
8230 term_cursor_right(col - screen_cur_col);
8231 else
8232 term_windgoto(row, col);
8234 screen_cur_row = row;
8235 screen_cur_col = col;
8240 * Set cursor to its position in the current window.
8242 void
8243 setcursor()
8245 if (redrawing())
8247 validate_cursor();
8248 windgoto(W_WINROW(curwin) + curwin->w_wrow,
8249 W_WINCOL(curwin) + (
8250 #ifdef FEAT_RIGHTLEFT
8251 /* With 'rightleft' set and the cursor on a double-wide
8252 * character, position it on the leftmost column. */
8253 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
8254 # ifdef FEAT_MBYTE
8255 (has_mbyte
8256 && (*mb_ptr2cells)(ml_get_cursor()) == 2
8257 && vim_isprintc(gchar_cursor())) ? 2 :
8258 # endif
8259 1)) :
8260 #endif
8261 curwin->w_wcol));
8267 * insert 'line_count' lines at 'row' in window 'wp'
8268 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
8269 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
8270 * scrolling.
8271 * Returns FAIL if the lines are not inserted, OK for success.
8274 win_ins_lines(wp, row, line_count, invalid, mayclear)
8275 win_T *wp;
8276 int row;
8277 int line_count;
8278 int invalid;
8279 int mayclear;
8281 int did_delete;
8282 int nextrow;
8283 int lastrow;
8284 int retval;
8286 if (invalid)
8287 wp->w_lines_valid = 0;
8289 if (wp->w_height < 5)
8290 return FAIL;
8292 if (line_count > wp->w_height - row)
8293 line_count = wp->w_height - row;
8295 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
8296 if (retval != MAYBE)
8297 return retval;
8300 * If there is a next window or a status line, we first try to delete the
8301 * lines at the bottom to avoid messing what is after the window.
8302 * If this fails and there are following windows, don't do anything to avoid
8303 * messing up those windows, better just redraw.
8305 did_delete = FALSE;
8306 #ifdef FEAT_WINDOWS
8307 if (wp->w_next != NULL || wp->w_status_height)
8309 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8310 line_count, (int)Rows, FALSE, NULL) == OK)
8311 did_delete = TRUE;
8312 else if (wp->w_next)
8313 return FAIL;
8315 #endif
8317 * if no lines deleted, blank the lines that will end up below the window
8319 if (!did_delete)
8321 #ifdef FEAT_WINDOWS
8322 wp->w_redr_status = TRUE;
8323 #endif
8324 redraw_cmdline = TRUE;
8325 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
8326 lastrow = nextrow + line_count;
8327 if (lastrow > Rows)
8328 lastrow = Rows;
8329 screen_fill(nextrow - line_count, lastrow - line_count,
8330 W_WINCOL(wp), (int)W_ENDCOL(wp),
8331 ' ', ' ', 0);
8334 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
8335 == FAIL)
8337 /* deletion will have messed up other windows */
8338 if (did_delete)
8340 #ifdef FEAT_WINDOWS
8341 wp->w_redr_status = TRUE;
8342 #endif
8343 win_rest_invalid(W_NEXT(wp));
8345 return FAIL;
8348 return OK;
8352 * delete "line_count" window lines at "row" in window "wp"
8353 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
8354 * If "mayclear" is TRUE the screen will be cleared if it is faster than
8355 * scrolling
8356 * Return OK for success, FAIL if the lines are not deleted.
8359 win_del_lines(wp, row, line_count, invalid, mayclear)
8360 win_T *wp;
8361 int row;
8362 int line_count;
8363 int invalid;
8364 int mayclear;
8366 int retval;
8368 if (invalid)
8369 wp->w_lines_valid = 0;
8371 if (line_count > wp->w_height - row)
8372 line_count = wp->w_height - row;
8374 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
8375 if (retval != MAYBE)
8376 return retval;
8378 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
8379 (int)Rows, FALSE, NULL) == FAIL)
8380 return FAIL;
8382 #ifdef FEAT_WINDOWS
8384 * If there are windows or status lines below, try to put them at the
8385 * correct place. If we can't do that, they have to be redrawn.
8387 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
8389 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8390 line_count, (int)Rows, NULL) == FAIL)
8392 wp->w_redr_status = TRUE;
8393 win_rest_invalid(wp->w_next);
8397 * If this is the last window and there is no status line, redraw the
8398 * command line later.
8400 else
8401 #endif
8402 redraw_cmdline = TRUE;
8403 return OK;
8407 * Common code for win_ins_lines() and win_del_lines().
8408 * Returns OK or FAIL when the work has been done.
8409 * Returns MAYBE when not finished yet.
8411 static int
8412 win_do_lines(wp, row, line_count, mayclear, del)
8413 win_T *wp;
8414 int row;
8415 int line_count;
8416 int mayclear;
8417 int del;
8419 int retval;
8421 if (!redrawing() || line_count <= 0)
8422 return FAIL;
8424 /* only a few lines left: redraw is faster */
8425 if (mayclear && Rows - line_count < 5
8426 #ifdef FEAT_VERTSPLIT
8427 && wp->w_width == Columns
8428 #endif
8431 screenclear(); /* will set wp->w_lines_valid to 0 */
8432 return FAIL;
8436 * Delete all remaining lines
8438 if (row + line_count >= wp->w_height)
8440 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
8441 W_WINCOL(wp), (int)W_ENDCOL(wp),
8442 ' ', ' ', 0);
8443 return OK;
8447 * when scrolling, the message on the command line should be cleared,
8448 * otherwise it will stay there forever.
8450 clear_cmdline = TRUE;
8453 * If the terminal can set a scroll region, use that.
8454 * Always do this in a vertically split window. This will redraw from
8455 * ScreenLines[] when t_CV isn't defined. That's faster than using
8456 * win_line().
8457 * Don't use a scroll region when we are going to redraw the text, writing
8458 * a character in the lower right corner of the scroll region causes a
8459 * scroll-up in the DJGPP version.
8461 if (scroll_region
8462 #ifdef FEAT_VERTSPLIT
8463 || W_WIDTH(wp) != Columns
8464 #endif
8467 #ifdef FEAT_VERTSPLIT
8468 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8469 #endif
8470 scroll_region_set(wp, row);
8471 if (del)
8472 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
8473 wp->w_height - row, FALSE, wp);
8474 else
8475 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
8476 wp->w_height - row, wp);
8477 #ifdef FEAT_VERTSPLIT
8478 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8479 #endif
8480 scroll_region_reset();
8481 return retval;
8484 #ifdef FEAT_WINDOWS
8485 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
8486 return FAIL;
8487 #endif
8489 return MAYBE;
8493 * window 'wp' and everything after it is messed up, mark it for redraw
8495 static void
8496 win_rest_invalid(wp)
8497 win_T *wp;
8499 #ifdef FEAT_WINDOWS
8500 while (wp != NULL)
8501 #else
8502 if (wp != NULL)
8503 #endif
8505 redraw_win_later(wp, NOT_VALID);
8506 #ifdef FEAT_WINDOWS
8507 wp->w_redr_status = TRUE;
8508 wp = wp->w_next;
8509 #endif
8511 redraw_cmdline = TRUE;
8515 * The rest of the routines in this file perform screen manipulations. The
8516 * given operation is performed physically on the screen. The corresponding
8517 * change is also made to the internal screen image. In this way, the editor
8518 * anticipates the effect of editing changes on the appearance of the screen.
8519 * That way, when we call screenupdate a complete redraw isn't usually
8520 * necessary. Another advantage is that we can keep adding code to anticipate
8521 * screen changes, and in the meantime, everything still works.
8525 * types for inserting or deleting lines
8527 #define USE_T_CAL 1
8528 #define USE_T_CDL 2
8529 #define USE_T_AL 3
8530 #define USE_T_CE 4
8531 #define USE_T_DL 5
8532 #define USE_T_SR 6
8533 #define USE_NL 7
8534 #define USE_T_CD 8
8535 #define USE_REDRAW 9
8538 * insert lines on the screen and update ScreenLines[]
8539 * 'end' is the line after the scrolled part. Normally it is Rows.
8540 * When scrolling region used 'off' is the offset from the top for the region.
8541 * 'row' and 'end' are relative to the start of the region.
8543 * return FAIL for failure, OK for success.
8546 screen_ins_lines(off, row, line_count, end, wp)
8547 int off;
8548 int row;
8549 int line_count;
8550 int end;
8551 win_T *wp; /* NULL or window to use width from */
8553 int i;
8554 int j;
8555 unsigned temp;
8556 int cursor_row;
8557 int type;
8558 int result_empty;
8559 int can_ce = can_clear(T_CE);
8562 * FAIL if
8563 * - there is no valid screen
8564 * - the screen has to be redrawn completely
8565 * - the line count is less than one
8566 * - the line count is more than 'ttyscroll'
8568 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
8569 return FAIL;
8572 * There are seven ways to insert lines:
8573 * 0. When in a vertically split window and t_CV isn't set, redraw the
8574 * characters from ScreenLines[].
8575 * 1. Use T_CD (clear to end of display) if it exists and the result of
8576 * the insert is just empty lines
8577 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
8578 * present or line_count > 1. It looks better if we do all the inserts
8579 * at once.
8580 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
8581 * insert is just empty lines and T_CE is not present or line_count >
8582 * 1.
8583 * 4. Use T_AL (insert line) if it exists.
8584 * 5. Use T_CE (erase line) if it exists and the result of the insert is
8585 * just empty lines.
8586 * 6. Use T_DL (delete line) if it exists and the result of the insert is
8587 * just empty lines.
8588 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
8589 * the 'da' flag is not set or we have clear line capability.
8590 * 8. redraw the characters from ScreenLines[].
8592 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
8593 * the scrollbar for the window. It does have insert line, use that if it
8594 * exists.
8596 result_empty = (row + line_count >= end);
8597 #ifdef FEAT_VERTSPLIT
8598 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
8599 type = USE_REDRAW;
8600 else
8601 #endif
8602 if (can_clear(T_CD) && result_empty)
8603 type = USE_T_CD;
8604 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
8605 type = USE_T_CAL;
8606 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
8607 type = USE_T_CDL;
8608 else if (*T_AL != NUL)
8609 type = USE_T_AL;
8610 else if (can_ce && result_empty)
8611 type = USE_T_CE;
8612 else if (*T_DL != NUL && result_empty)
8613 type = USE_T_DL;
8614 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
8615 type = USE_T_SR;
8616 else
8617 return FAIL;
8620 * For clearing the lines screen_del_lines() is used. This will also take
8621 * care of t_db if necessary.
8623 if (type == USE_T_CD || type == USE_T_CDL ||
8624 type == USE_T_CE || type == USE_T_DL)
8625 return screen_del_lines(off, row, line_count, end, FALSE, wp);
8628 * If text is retained below the screen, first clear or delete as many
8629 * lines at the bottom of the window as are about to be inserted so that
8630 * the deleted lines won't later surface during a screen_del_lines.
8632 if (*T_DB)
8633 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
8635 #ifdef FEAT_CLIPBOARD
8636 /* Remove a modeless selection when inserting lines halfway the screen
8637 * or not the full width of the screen. */
8638 if (off + row > 0
8639 # ifdef FEAT_VERTSPLIT
8640 || (wp != NULL && wp->w_width != Columns)
8641 # endif
8643 clip_clear_selection();
8644 else
8645 clip_scroll_selection(-line_count);
8646 #endif
8648 #ifdef FEAT_GUI
8649 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
8650 * scrolling is actually carried out. */
8651 gui_dont_update_cursor();
8652 #endif
8654 if (*T_CCS != NUL) /* cursor relative to region */
8655 cursor_row = row;
8656 else
8657 cursor_row = row + off;
8660 * Shift LineOffset[] line_count down to reflect the inserted lines.
8661 * Clear the inserted lines in ScreenLines[].
8663 row += off;
8664 end += off;
8665 for (i = 0; i < line_count; ++i)
8667 #ifdef FEAT_VERTSPLIT
8668 if (wp != NULL && wp->w_width != Columns)
8670 /* need to copy part of a line */
8671 j = end - 1 - i;
8672 while ((j -= line_count) >= row)
8673 linecopy(j + line_count, j, wp);
8674 j += line_count;
8675 if (can_clear((char_u *)" "))
8676 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
8677 else
8678 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
8679 LineWraps[j] = FALSE;
8681 else
8682 #endif
8684 j = end - 1 - i;
8685 temp = LineOffset[j];
8686 while ((j -= line_count) >= row)
8688 LineOffset[j + line_count] = LineOffset[j];
8689 LineWraps[j + line_count] = LineWraps[j];
8691 LineOffset[j + line_count] = temp;
8692 LineWraps[j + line_count] = FALSE;
8693 if (can_clear((char_u *)" "))
8694 lineclear(temp, (int)Columns);
8695 else
8696 lineinvalid(temp, (int)Columns);
8700 screen_stop_highlight();
8701 windgoto(cursor_row, 0);
8703 #ifdef FEAT_VERTSPLIT
8704 /* redraw the characters */
8705 if (type == USE_REDRAW)
8706 redraw_block(row, end, wp);
8707 else
8708 #endif
8709 if (type == USE_T_CAL)
8711 term_append_lines(line_count);
8712 screen_start(); /* don't know where cursor is now */
8714 else
8716 for (i = 0; i < line_count; i++)
8718 if (type == USE_T_AL)
8720 if (i && cursor_row != 0)
8721 windgoto(cursor_row, 0);
8722 out_str(T_AL);
8724 else /* type == USE_T_SR */
8725 out_str(T_SR);
8726 screen_start(); /* don't know where cursor is now */
8731 * With scroll-reverse and 'da' flag set we need to clear the lines that
8732 * have been scrolled down into the region.
8734 if (type == USE_T_SR && *T_DA)
8736 for (i = 0; i < line_count; ++i)
8738 windgoto(off + i, 0);
8739 out_str(T_CE);
8740 screen_start(); /* don't know where cursor is now */
8744 #ifdef FEAT_GUI
8745 gui_can_update_cursor();
8746 if (gui.in_use)
8747 out_flush(); /* always flush after a scroll */
8748 #endif
8749 return OK;
8753 * delete lines on the screen and update ScreenLines[]
8754 * 'end' is the line after the scrolled part. Normally it is Rows.
8755 * When scrolling region used 'off' is the offset from the top for the region.
8756 * 'row' and 'end' are relative to the start of the region.
8758 * Return OK for success, FAIL if the lines are not deleted.
8761 screen_del_lines(off, row, line_count, end, force, wp)
8762 int off;
8763 int row;
8764 int line_count;
8765 int end;
8766 int force; /* even when line_count > p_ttyscroll */
8767 win_T *wp UNUSED; /* NULL or window to use width from */
8769 int j;
8770 int i;
8771 unsigned temp;
8772 int cursor_row;
8773 int cursor_end;
8774 int result_empty; /* result is empty until end of region */
8775 int can_delete; /* deleting line codes can be used */
8776 int type;
8779 * FAIL if
8780 * - there is no valid screen
8781 * - the screen has to be redrawn completely
8782 * - the line count is less than one
8783 * - the line count is more than 'ttyscroll'
8785 if (!screen_valid(TRUE) || line_count <= 0 ||
8786 (!force && line_count > p_ttyscroll))
8787 return FAIL;
8790 * Check if the rest of the current region will become empty.
8792 result_empty = row + line_count >= end;
8795 * We can delete lines only when 'db' flag not set or when 'ce' option
8796 * available.
8798 can_delete = (*T_DB == NUL || can_clear(T_CE));
8801 * There are six ways to delete lines:
8802 * 0. When in a vertically split window and t_CV isn't set, redraw the
8803 * characters from ScreenLines[].
8804 * 1. Use T_CD if it exists and the result is empty.
8805 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
8806 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
8807 * none of the other ways work.
8808 * 4. Use T_CE (erase line) if the result is empty.
8809 * 5. Use T_DL (delete line) if it exists.
8810 * 6. redraw the characters from ScreenLines[].
8812 #ifdef FEAT_VERTSPLIT
8813 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
8814 type = USE_REDRAW;
8815 else
8816 #endif
8817 if (can_clear(T_CD) && result_empty)
8818 type = USE_T_CD;
8819 #if defined(__BEOS__) && defined(BEOS_DR8)
8821 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
8822 * its internal termcap... this works okay for tests which test *T_DB !=
8823 * NUL. It has the disadvantage that the user cannot use any :set t_*
8824 * command to get T_DB (back) to empty_option, only :set term=... will do
8825 * the trick...
8826 * Anyway, this hack will hopefully go away with the next OS release.
8827 * (Olaf Seibert)
8829 else if (row == 0 && T_DB == empty_option
8830 && (line_count == 1 || *T_CDL == NUL))
8831 #else
8832 else if (row == 0 && (
8833 #ifndef AMIGA
8834 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
8835 * up, so use delete-line command */
8836 line_count == 1 ||
8837 #endif
8838 *T_CDL == NUL))
8839 #endif
8840 type = USE_NL;
8841 else if (*T_CDL != NUL && line_count > 1 && can_delete)
8842 type = USE_T_CDL;
8843 else if (can_clear(T_CE) && result_empty
8844 #ifdef FEAT_VERTSPLIT
8845 && (wp == NULL || wp->w_width == Columns)
8846 #endif
8848 type = USE_T_CE;
8849 else if (*T_DL != NUL && can_delete)
8850 type = USE_T_DL;
8851 else if (*T_CDL != NUL && can_delete)
8852 type = USE_T_CDL;
8853 else
8854 return FAIL;
8856 #ifdef FEAT_CLIPBOARD
8857 /* Remove a modeless selection when deleting lines halfway the screen or
8858 * not the full width of the screen. */
8859 if (off + row > 0
8860 # ifdef FEAT_VERTSPLIT
8861 || (wp != NULL && wp->w_width != Columns)
8862 # endif
8864 clip_clear_selection();
8865 else
8866 clip_scroll_selection(line_count);
8867 #endif
8869 #ifdef FEAT_GUI
8870 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
8871 * scrolling is actually carried out. */
8872 gui_dont_update_cursor();
8873 #endif
8875 if (*T_CCS != NUL) /* cursor relative to region */
8877 cursor_row = row;
8878 cursor_end = end;
8880 else
8882 cursor_row = row + off;
8883 cursor_end = end + off;
8887 * Now shift LineOffset[] line_count up to reflect the deleted lines.
8888 * Clear the inserted lines in ScreenLines[].
8890 row += off;
8891 end += off;
8892 for (i = 0; i < line_count; ++i)
8894 #ifdef FEAT_VERTSPLIT
8895 if (wp != NULL && wp->w_width != Columns)
8897 /* need to copy part of a line */
8898 j = row + i;
8899 while ((j += line_count) <= end - 1)
8900 linecopy(j - line_count, j, wp);
8901 j -= line_count;
8902 if (can_clear((char_u *)" "))
8903 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
8904 else
8905 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
8906 LineWraps[j] = FALSE;
8908 else
8909 #endif
8911 /* whole width, moving the line pointers is faster */
8912 j = row + i;
8913 temp = LineOffset[j];
8914 while ((j += line_count) <= end - 1)
8916 LineOffset[j - line_count] = LineOffset[j];
8917 LineWraps[j - line_count] = LineWraps[j];
8919 LineOffset[j - line_count] = temp;
8920 LineWraps[j - line_count] = FALSE;
8921 if (can_clear((char_u *)" "))
8922 lineclear(temp, (int)Columns);
8923 else
8924 lineinvalid(temp, (int)Columns);
8928 screen_stop_highlight();
8930 #ifdef FEAT_VERTSPLIT
8931 /* redraw the characters */
8932 if (type == USE_REDRAW)
8933 redraw_block(row, end, wp);
8934 else
8935 #endif
8936 if (type == USE_T_CD) /* delete the lines */
8938 windgoto(cursor_row, 0);
8939 out_str(T_CD);
8940 screen_start(); /* don't know where cursor is now */
8942 else if (type == USE_T_CDL)
8944 windgoto(cursor_row, 0);
8945 term_delete_lines(line_count);
8946 screen_start(); /* don't know where cursor is now */
8949 * Deleting lines at top of the screen or scroll region: Just scroll
8950 * the whole screen (scroll region) up by outputting newlines on the
8951 * last line.
8953 else if (type == USE_NL)
8955 windgoto(cursor_end - 1, 0);
8956 for (i = line_count; --i >= 0; )
8957 out_char('\n'); /* cursor will remain on same line */
8959 else
8961 for (i = line_count; --i >= 0; )
8963 if (type == USE_T_DL)
8965 windgoto(cursor_row, 0);
8966 out_str(T_DL); /* delete a line */
8968 else /* type == USE_T_CE */
8970 windgoto(cursor_row + i, 0);
8971 out_str(T_CE); /* erase a line */
8973 screen_start(); /* don't know where cursor is now */
8978 * If the 'db' flag is set, we need to clear the lines that have been
8979 * scrolled up at the bottom of the region.
8981 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
8983 for (i = line_count; i > 0; --i)
8985 windgoto(cursor_end - i, 0);
8986 out_str(T_CE); /* erase a line */
8987 screen_start(); /* don't know where cursor is now */
8991 #ifdef FEAT_GUI
8992 gui_can_update_cursor();
8993 if (gui.in_use)
8994 out_flush(); /* always flush after a scroll */
8995 #endif
8997 return OK;
9001 * show the current mode and ruler
9003 * If clear_cmdline is TRUE, clear the rest of the cmdline.
9004 * If clear_cmdline is FALSE there may be a message there that needs to be
9005 * cleared only if a mode is shown.
9006 * Return the length of the message (0 if no message).
9009 showmode()
9011 int need_clear;
9012 int length = 0;
9013 int do_mode;
9014 int attr;
9015 int nwr_save;
9016 #ifdef FEAT_INS_EXPAND
9017 int sub_attr;
9018 #endif
9020 do_mode = ((p_smd && msg_silent == 0)
9021 && ((State & INSERT)
9022 || restart_edit
9023 #ifdef FEAT_VISUAL
9024 || VIsual_active
9025 #endif
9027 if (do_mode || Recording)
9030 * Don't show mode right now, when not redrawing or inside a mapping.
9031 * Call char_avail() only when we are going to show something, because
9032 * it takes a bit of time.
9034 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
9036 redraw_cmdline = TRUE; /* show mode later */
9037 return 0;
9040 nwr_save = need_wait_return;
9042 /* wait a bit before overwriting an important message */
9043 check_for_delay(FALSE);
9045 /* if the cmdline is more than one line high, erase top lines */
9046 need_clear = clear_cmdline;
9047 if (clear_cmdline && cmdline_row < Rows - 1)
9048 msg_clr_cmdline(); /* will reset clear_cmdline */
9050 /* Position on the last line in the window, column 0 */
9051 msg_pos_mode();
9052 cursor_off();
9053 attr = hl_attr(HLF_CM); /* Highlight mode */
9054 if (do_mode)
9056 MSG_PUTS_ATTR("--", attr);
9057 #if defined(FEAT_XIM)
9058 # if 0 /* old version, changed by SungHyun Nam July 2008 */
9059 if (xic != NULL && im_get_status() && !p_imdisable
9060 && curbuf->b_p_iminsert == B_IMODE_IM)
9061 # else
9062 if (
9063 # ifdef HAVE_GTK2
9064 preedit_get_status()
9065 # else
9066 im_get_status()
9067 # endif
9069 # endif
9070 # ifdef HAVE_GTK2 /* most of the time, it's not XIM being used */
9071 MSG_PUTS_ATTR(" IM", attr);
9072 # else
9073 MSG_PUTS_ATTR(" XIM", attr);
9074 # endif
9075 #endif
9076 #if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
9077 if (gui.in_use)
9079 if (hangul_input_state_get())
9080 MSG_PUTS_ATTR(" \307\321\261\333", attr); /* HANGUL */
9082 #endif
9083 #ifdef FEAT_INS_EXPAND
9084 if (edit_submode != NULL) /* CTRL-X in Insert mode */
9086 /* These messages can get long, avoid a wrap in a narrow
9087 * window. Prefer showing edit_submode_extra. */
9088 length = (Rows - msg_row) * Columns - 3;
9089 if (edit_submode_extra != NULL)
9090 length -= vim_strsize(edit_submode_extra);
9091 if (length > 0)
9093 if (edit_submode_pre != NULL)
9094 length -= vim_strsize(edit_submode_pre);
9095 if (length - vim_strsize(edit_submode) > 0)
9097 if (edit_submode_pre != NULL)
9098 msg_puts_attr(edit_submode_pre, attr);
9099 msg_puts_attr(edit_submode, attr);
9101 if (edit_submode_extra != NULL)
9103 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
9104 if ((int)edit_submode_highl < (int)HLF_COUNT)
9105 sub_attr = hl_attr(edit_submode_highl);
9106 else
9107 sub_attr = attr;
9108 msg_puts_attr(edit_submode_extra, sub_attr);
9111 length = 0;
9113 else
9114 #endif
9116 #ifdef FEAT_VREPLACE
9117 if (State & VREPLACE_FLAG)
9118 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
9119 else
9120 #endif
9121 if (State & REPLACE_FLAG)
9122 MSG_PUTS_ATTR(_(" REPLACE"), attr);
9123 else if (State & INSERT)
9125 #ifdef FEAT_RIGHTLEFT
9126 if (p_ri)
9127 MSG_PUTS_ATTR(_(" REVERSE"), attr);
9128 #endif
9129 MSG_PUTS_ATTR(_(" INSERT"), attr);
9131 else if (restart_edit == 'I')
9132 MSG_PUTS_ATTR(_(" (insert)"), attr);
9133 else if (restart_edit == 'R')
9134 MSG_PUTS_ATTR(_(" (replace)"), attr);
9135 else if (restart_edit == 'V')
9136 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
9137 #ifdef FEAT_RIGHTLEFT
9138 if (p_hkmap)
9139 MSG_PUTS_ATTR(_(" Hebrew"), attr);
9140 # ifdef FEAT_FKMAP
9141 if (p_fkmap)
9142 MSG_PUTS_ATTR(farsi_text_5, attr);
9143 # endif
9144 #endif
9145 #ifdef FEAT_KEYMAP
9146 if (State & LANGMAP)
9148 # ifdef FEAT_ARABIC
9149 if (curwin->w_p_arab)
9150 MSG_PUTS_ATTR(_(" Arabic"), attr);
9151 else
9152 # endif
9153 MSG_PUTS_ATTR(_(" (lang)"), attr);
9155 #endif
9156 if ((State & INSERT) && p_paste)
9157 MSG_PUTS_ATTR(_(" (paste)"), attr);
9159 #ifdef FEAT_VISUAL
9160 if (VIsual_active)
9162 char *p;
9164 /* Don't concatenate separate words to avoid translation
9165 * problems. */
9166 switch ((VIsual_select ? 4 : 0)
9167 + (VIsual_mode == Ctrl_V) * 2
9168 + (VIsual_mode == 'V'))
9170 case 0: p = N_(" VISUAL"); break;
9171 case 1: p = N_(" VISUAL LINE"); break;
9172 case 2: p = N_(" VISUAL BLOCK"); break;
9173 case 4: p = N_(" SELECT"); break;
9174 case 5: p = N_(" SELECT LINE"); break;
9175 default: p = N_(" SELECT BLOCK"); break;
9177 MSG_PUTS_ATTR(_(p), attr);
9179 #endif
9180 MSG_PUTS_ATTR(" --", attr);
9183 need_clear = TRUE;
9185 if (Recording
9186 #ifdef FEAT_INS_EXPAND
9187 && edit_submode == NULL /* otherwise it gets too long */
9188 #endif
9191 MSG_PUTS_ATTR(_("recording"), attr);
9192 need_clear = TRUE;
9195 mode_displayed = TRUE;
9196 if (need_clear || clear_cmdline)
9197 msg_clr_eos();
9198 msg_didout = FALSE; /* overwrite this message */
9199 length = msg_col;
9200 msg_col = 0;
9201 need_wait_return = nwr_save; /* never ask for hit-return for this */
9203 else if (clear_cmdline && msg_silent == 0)
9204 /* Clear the whole command line. Will reset "clear_cmdline". */
9205 msg_clr_cmdline();
9207 #ifdef FEAT_CMDL_INFO
9208 # ifdef FEAT_VISUAL
9209 /* In Visual mode the size of the selected area must be redrawn. */
9210 if (VIsual_active)
9211 clear_showcmd();
9212 # endif
9214 /* If the last window has no status line, the ruler is after the mode
9215 * message and must be redrawn */
9216 if (redrawing()
9217 # ifdef FEAT_WINDOWS
9218 && lastwin->w_status_height == 0
9219 # endif
9221 win_redr_ruler(lastwin, TRUE);
9222 #endif
9223 redraw_cmdline = FALSE;
9224 clear_cmdline = FALSE;
9226 return length;
9230 * Position for a mode message.
9232 static void
9233 msg_pos_mode()
9235 msg_col = 0;
9236 msg_row = Rows - 1;
9240 * Delete mode message. Used when ESC is typed which is expected to end
9241 * Insert mode (but Insert mode didn't end yet!).
9242 * Caller should check "mode_displayed".
9244 void
9245 unshowmode(force)
9246 int force;
9249 * Don't delete it right now, when not redrawing or inside a mapping.
9251 if (!redrawing() || (!force && char_avail() && !KeyTyped))
9252 redraw_cmdline = TRUE; /* delete mode later */
9253 else
9255 msg_pos_mode();
9256 if (Recording)
9257 MSG_PUTS_ATTR(_("recording"), hl_attr(HLF_CM));
9258 msg_clr_eos();
9262 #if defined(FEAT_WINDOWS)
9264 * Draw the tab pages line at the top of the Vim window.
9266 static void
9267 draw_tabline()
9269 int tabcount = 0;
9270 tabpage_T *tp;
9271 int tabwidth;
9272 int col = 0;
9273 int scol = 0;
9274 int attr;
9275 win_T *wp;
9276 win_T *cwp;
9277 int wincount;
9278 int modified;
9279 int c;
9280 int len;
9281 int attr_sel = hl_attr(HLF_TPS);
9282 int attr_nosel = hl_attr(HLF_TP);
9283 int attr_fill = hl_attr(HLF_TPF);
9284 char_u *p;
9285 int room;
9286 int use_sep_chars = (t_colors < 8
9287 #ifdef FEAT_GUI
9288 && !gui.in_use
9289 #endif
9292 redraw_tabline = FALSE;
9294 #ifdef FEAT_GUI_TABLINE
9295 /* Take care of a GUI tabline. */
9296 if (gui_use_tabline())
9298 gui_update_tabline();
9299 return;
9301 #endif
9303 if (tabline_height() < 1)
9304 return;
9306 #if defined(FEAT_STL_OPT)
9308 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
9309 for (scol = 0; scol < Columns; ++scol)
9310 TabPageIdxs[scol] = 0;
9312 /* Use the 'tabline' option if it's set. */
9313 if (*p_tal != NUL)
9315 int save_called_emsg = called_emsg;
9317 /* Check for an error. If there is one we would loop in redrawing the
9318 * screen. Avoid that by making 'tabline' empty. */
9319 called_emsg = FALSE;
9320 win_redr_custom(NULL, FALSE);
9321 if (called_emsg)
9322 set_string_option_direct((char_u *)"tabline", -1,
9323 (char_u *)"", OPT_FREE, SID_ERROR);
9324 called_emsg |= save_called_emsg;
9326 else
9327 #endif
9329 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
9330 ++tabcount;
9332 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
9333 if (tabwidth < 6)
9334 tabwidth = 6;
9336 attr = attr_nosel;
9337 tabcount = 0;
9338 scol = 0;
9339 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
9340 tp = tp->tp_next)
9342 scol = col;
9344 if (tp->tp_topframe == topframe)
9345 attr = attr_sel;
9346 if (use_sep_chars && col > 0)
9347 screen_putchar('|', 0, col++, attr);
9349 if (tp->tp_topframe != topframe)
9350 attr = attr_nosel;
9352 screen_putchar(' ', 0, col++, attr);
9354 if (tp == curtab)
9356 cwp = curwin;
9357 wp = firstwin;
9359 else
9361 cwp = tp->tp_curwin;
9362 wp = tp->tp_firstwin;
9365 modified = FALSE;
9366 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
9367 if (bufIsChanged(wp->w_buffer))
9368 modified = TRUE;
9369 if (modified || wincount > 1)
9371 if (wincount > 1)
9373 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
9374 len = (int)STRLEN(NameBuff);
9375 if (col + len >= Columns - 3)
9376 break;
9377 screen_puts_len(NameBuff, len, 0, col,
9378 #if defined(FEAT_SYN_HL)
9379 hl_combine_attr(attr, hl_attr(HLF_T))
9380 #else
9381 attr
9382 #endif
9384 col += len;
9386 if (modified)
9387 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
9388 screen_putchar(' ', 0, col++, attr);
9391 room = scol - col + tabwidth - 1;
9392 if (room > 0)
9394 /* Get buffer name in NameBuff[] */
9395 get_trans_bufname(cwp->w_buffer);
9396 shorten_dir(NameBuff);
9397 len = vim_strsize(NameBuff);
9398 p = NameBuff;
9399 #ifdef FEAT_MBYTE
9400 if (has_mbyte)
9401 while (len > room)
9403 len -= ptr2cells(p);
9404 mb_ptr_adv(p);
9406 else
9407 #endif
9408 if (len > room)
9410 p += len - room;
9411 len = room;
9413 if (len > Columns - col - 1)
9414 len = Columns - col - 1;
9416 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
9417 col += len;
9419 screen_putchar(' ', 0, col++, attr);
9421 /* Store the tab page number in TabPageIdxs[], so that
9422 * jump_to_mouse() knows where each one is. */
9423 ++tabcount;
9424 while (scol < col)
9425 TabPageIdxs[scol++] = tabcount;
9428 if (use_sep_chars)
9429 c = '_';
9430 else
9431 c = ' ';
9432 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
9434 /* Put an "X" for closing the current tab if there are several. */
9435 if (first_tabpage->tp_next != NULL)
9437 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
9438 TabPageIdxs[Columns - 1] = -999;
9442 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
9443 * set. */
9444 redraw_tabline = FALSE;
9448 * Get buffer name for "buf" into NameBuff[].
9449 * Takes care of special buffer names and translates special characters.
9451 void
9452 get_trans_bufname(buf)
9453 buf_T *buf;
9455 if (buf_spname(buf) != NULL)
9456 STRCPY(NameBuff, buf_spname(buf));
9457 else
9458 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
9459 trans_characters(NameBuff, MAXPATHL);
9461 #endif
9463 #if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
9465 * Get the character to use in a status line. Get its attributes in "*attr".
9467 static int
9468 fillchar_status(attr, is_curwin)
9469 int *attr;
9470 int is_curwin;
9472 int fill;
9473 if (is_curwin)
9475 *attr = hl_attr(HLF_S);
9476 fill = fill_stl;
9478 else
9480 *attr = hl_attr(HLF_SNC);
9481 fill = fill_stlnc;
9483 /* Use fill when there is highlighting, and highlighting of current
9484 * window differs, or the fillchars differ, or this is not the
9485 * current window */
9486 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
9487 || !is_curwin || firstwin == lastwin)
9488 || (fill_stl != fill_stlnc)))
9489 return fill;
9490 if (is_curwin)
9491 return '^';
9492 return '=';
9494 #endif
9496 #ifdef FEAT_VERTSPLIT
9498 * Get the character to use in a separator between vertically split windows.
9499 * Get its attributes in "*attr".
9501 static int
9502 fillchar_vsep(attr)
9503 int *attr;
9505 *attr = hl_attr(HLF_C);
9506 if (*attr == 0 && fill_vert == ' ')
9507 return '|';
9508 else
9509 return fill_vert;
9511 #endif
9514 * Return TRUE if redrawing should currently be done.
9517 redrawing()
9519 return (!RedrawingDisabled
9520 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
9524 * Return TRUE if printing messages should currently be done.
9527 messaging()
9529 return (!(p_lz && char_avail() && !KeyTyped));
9533 * Show current status info in ruler and various other places
9534 * If always is FALSE, only show ruler if position has changed.
9536 void
9537 showruler(always)
9538 int always;
9540 if (!always && !redrawing())
9541 return;
9542 #ifdef FEAT_INS_EXPAND
9543 if (pum_visible())
9545 # ifdef FEAT_WINDOWS
9546 /* Don't redraw right now, do it later. */
9547 curwin->w_redr_status = TRUE;
9548 # endif
9549 return;
9551 #endif
9552 #if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
9553 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
9555 redraw_custom_statusline(curwin);
9557 else
9558 #endif
9559 #ifdef FEAT_CMDL_INFO
9560 win_redr_ruler(curwin, always);
9561 #endif
9563 #ifdef FEAT_TITLE
9564 if (need_maketitle
9565 # ifdef FEAT_STL_OPT
9566 || (p_icon && (stl_syntax & STL_IN_ICON))
9567 || (p_title && (stl_syntax & STL_IN_TITLE))
9568 # endif
9570 maketitle();
9571 #endif
9572 #ifdef FEAT_WINDOWS
9573 /* Redraw the tab pages line if needed. */
9574 if (redraw_tabline)
9575 draw_tabline();
9576 #endif
9579 #ifdef FEAT_CMDL_INFO
9580 static void
9581 win_redr_ruler(wp, always)
9582 win_T *wp;
9583 int always;
9585 #define RULER_BUF_LEN 70
9586 char_u buffer[RULER_BUF_LEN];
9587 int row;
9588 int fillchar;
9589 int attr;
9590 int empty_line = FALSE;
9591 colnr_T virtcol;
9592 int i;
9593 size_t len;
9594 int o;
9595 #ifdef FEAT_VERTSPLIT
9596 int this_ru_col;
9597 int off = 0;
9598 int width = Columns;
9599 # define WITH_OFF(x) x
9600 # define WITH_WIDTH(x) x
9601 #else
9602 # define WITH_OFF(x) 0
9603 # define WITH_WIDTH(x) Columns
9604 # define this_ru_col ru_col
9605 #endif
9607 /* If 'ruler' off or redrawing disabled, don't do anything */
9608 if (!p_ru)
9609 return;
9612 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
9613 * after deleting lines, before cursor.lnum is corrected.
9615 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
9616 return;
9618 #ifdef FEAT_INS_EXPAND
9619 /* Don't draw the ruler while doing insert-completion, it might overwrite
9620 * the (long) mode message. */
9621 # ifdef FEAT_WINDOWS
9622 if (wp == lastwin && lastwin->w_status_height == 0)
9623 # endif
9624 if (edit_submode != NULL)
9625 return;
9626 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
9627 if (pum_visible())
9628 return;
9629 #endif
9631 #ifdef FEAT_STL_OPT
9632 if (*p_ruf)
9634 int save_called_emsg = called_emsg;
9636 called_emsg = FALSE;
9637 win_redr_custom(wp, TRUE);
9638 if (called_emsg)
9639 set_string_option_direct((char_u *)"rulerformat", -1,
9640 (char_u *)"", OPT_FREE, SID_ERROR);
9641 called_emsg |= save_called_emsg;
9642 return;
9644 #endif
9647 * Check if not in Insert mode and the line is empty (will show "0-1").
9649 if (!(State & INSERT)
9650 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
9651 empty_line = TRUE;
9654 * Only draw the ruler when something changed.
9656 validate_virtcol_win(wp);
9657 if ( redraw_cmdline
9658 || always
9659 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
9660 || wp->w_cursor.col != wp->w_ru_cursor.col
9661 || wp->w_virtcol != wp->w_ru_virtcol
9662 #ifdef FEAT_VIRTUALEDIT
9663 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
9664 #endif
9665 || wp->w_topline != wp->w_ru_topline
9666 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
9667 #ifdef FEAT_DIFF
9668 || wp->w_topfill != wp->w_ru_topfill
9669 #endif
9670 || empty_line != wp->w_ru_empty)
9672 cursor_off();
9673 #ifdef FEAT_WINDOWS
9674 if (wp->w_status_height)
9676 row = W_WINROW(wp) + wp->w_height;
9677 fillchar = fillchar_status(&attr, wp == curwin);
9678 # ifdef FEAT_VERTSPLIT
9679 off = W_WINCOL(wp);
9680 width = W_WIDTH(wp);
9681 # endif
9683 else
9684 #endif
9686 row = Rows - 1;
9687 fillchar = ' ';
9688 attr = 0;
9689 #ifdef FEAT_VERTSPLIT
9690 width = Columns;
9691 off = 0;
9692 #endif
9695 /* In list mode virtcol needs to be recomputed */
9696 virtcol = wp->w_virtcol;
9697 if (wp->w_p_list && lcs_tab1 == NUL)
9699 wp->w_p_list = FALSE;
9700 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
9701 wp->w_p_list = TRUE;
9705 * Some sprintfs return the length, some return a pointer.
9706 * To avoid portability problems we use strlen() here.
9708 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
9709 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
9710 ? 0L
9711 : (long)(wp->w_cursor.lnum));
9712 len = STRLEN(buffer);
9713 col_print(buffer + len, RULER_BUF_LEN - len,
9714 empty_line ? 0 : (int)wp->w_cursor.col + 1,
9715 (int)virtcol + 1);
9718 * Add a "50%" if there is room for it.
9719 * On the last line, don't print in the last column (scrolls the
9720 * screen up on some terminals).
9722 i = (int)STRLEN(buffer);
9723 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
9724 o = i + vim_strsize(buffer + i + 1);
9725 #ifdef FEAT_WINDOWS
9726 if (wp->w_status_height == 0) /* can't use last char of screen */
9727 #endif
9728 ++o;
9729 #ifdef FEAT_VERTSPLIT
9730 this_ru_col = ru_col - (Columns - width);
9731 if (this_ru_col < 0)
9732 this_ru_col = 0;
9733 #endif
9734 /* Never use more than half the window/screen width, leave the other
9735 * half for the filename. */
9736 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
9737 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
9738 if (this_ru_col + o < WITH_WIDTH(width))
9740 while (this_ru_col + o < WITH_WIDTH(width))
9742 #ifdef FEAT_MBYTE
9743 if (has_mbyte)
9744 i += (*mb_char2bytes)(fillchar, buffer + i);
9745 else
9746 #endif
9747 buffer[i++] = fillchar;
9748 ++o;
9750 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
9752 /* Truncate at window boundary. */
9753 #ifdef FEAT_MBYTE
9754 if (has_mbyte)
9756 o = 0;
9757 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
9759 o += (*mb_ptr2cells)(buffer + i);
9760 if (this_ru_col + o > WITH_WIDTH(width))
9762 buffer[i] = NUL;
9763 break;
9767 else
9768 #endif
9769 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
9770 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
9772 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
9773 i = redraw_cmdline;
9774 screen_fill(row, row + 1,
9775 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
9776 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
9777 fillchar, fillchar, attr);
9778 /* don't redraw the cmdline because of showing the ruler */
9779 redraw_cmdline = i;
9780 wp->w_ru_cursor = wp->w_cursor;
9781 wp->w_ru_virtcol = wp->w_virtcol;
9782 wp->w_ru_empty = empty_line;
9783 wp->w_ru_topline = wp->w_topline;
9784 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
9785 #ifdef FEAT_DIFF
9786 wp->w_ru_topfill = wp->w_topfill;
9787 #endif
9790 #endif
9792 #if defined(FEAT_LINEBREAK) || defined(PROTO)
9794 * Return the width of the 'number' column.
9795 * Caller may need to check if 'number' is set.
9796 * Otherwise it depends on 'numberwidth' and the line count.
9799 number_width(wp)
9800 win_T *wp;
9802 int n;
9803 linenr_T lnum;
9805 lnum = wp->w_buffer->b_ml.ml_line_count;
9806 if (lnum == wp->w_nrwidth_line_count)
9807 return wp->w_nrwidth_width;
9808 wp->w_nrwidth_line_count = lnum;
9810 n = 0;
9813 lnum /= 10;
9814 ++n;
9815 } while (lnum > 0);
9817 /* 'numberwidth' gives the minimal width plus one */
9818 if (n < wp->w_p_nuw - 1)
9819 n = wp->w_p_nuw - 1;
9821 wp->w_nrwidth_width = n;
9822 return n;
9824 #endif