Merge branch 'vim-with-runtime' into feat/code-check
[vim_extended.git] / src / screen.c
blobff08fe3340bc8bbfed43796b9e7ce669c71e063b
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 and
29 * ScreenLinesC[][] is not used. When the character occupies two display
30 * cells the next byte in ScreenLines[] is 0.
31 * ScreenLinesC[][] contain up to 'maxcombine' composing characters
32 * (drawn on top of the first character). There is 0 after the last one used.
33 * ScreenLines2[] is only used for euc-jp to store the second byte if the
34 * first byte is 0x8e (single-width character).
36 * The screen_*() functions write to the screen and handle updating
37 * ScreenLines[].
39 * update_screen() is the function that updates all windows and status lines.
40 * It is called form the main loop when must_redraw is non-zero. It may be
41 * called from other places when an immediate screen update is needed.
43 * The part of the buffer that is displayed in a window is set with:
44 * - w_topline (first buffer line in window)
45 * - w_topfill (filler line above the first line)
46 * - w_leftcol (leftmost window cell in window),
47 * - w_skipcol (skipped window cells of first line)
49 * Commands that only move the cursor around in a window, do not need to take
50 * action to update the display. The main loop will check if w_topline is
51 * valid and update it (scroll the window) when needed.
53 * Commands that scroll a window change w_topline and must call
54 * check_cursor() to move the cursor into the visible part of the window, and
55 * call redraw_later(VALID) to have the window displayed by update_screen()
56 * later.
58 * Commands that change text in the buffer must call changed_bytes() or
59 * changed_lines() to mark the area that changed and will require updating
60 * later. The main loop will call update_screen(), which will update each
61 * window that shows the changed buffer. This assumes text above the change
62 * can remain displayed as it is. Text after the change may need updating for
63 * scrolling, folding and syntax highlighting.
65 * Commands that change how a window is displayed (e.g., setting 'list') or
66 * invalidate the contents of a window in another way (e.g., change fold
67 * settings), must call redraw_later(NOT_VALID) to have the whole window
68 * redisplayed by update_screen() later.
70 * Commands that change how a buffer is displayed (e.g., setting 'tabstop')
71 * must call redraw_curbuf_later(NOT_VALID) to have all the windows for the
72 * buffer redisplayed by update_screen() later.
74 * Commands that change highlighting and possibly cause a scroll too must call
75 * redraw_later(SOME_VALID) to update the whole window but still use scrolling
76 * to avoid redrawing everything. But the length of displayed lines must not
77 * change, use NOT_VALID then.
79 * Commands that move the window position must call redraw_later(NOT_VALID).
80 * TODO: should minimize redrawing by scrolling when possible.
82 * Commands that change everything (e.g., resizing the screen) must call
83 * redraw_all_later(NOT_VALID) or redraw_all_later(CLEAR).
85 * Things that are handled indirectly:
86 * - When messages scroll the screen up, msg_scrolled will be set and
87 * update_screen() called to redraw.
90 #include "vim.h"
93 * The attributes that are actually active for writing to the screen.
95 static int screen_attr = 0;
98 * Positioning the cursor is reduced by remembering the last position.
99 * Mostly used by windgoto() and screen_char().
101 static int screen_cur_row, screen_cur_col; /* last known cursor position */
103 #ifdef FEAT_SEARCH_EXTRA
104 static match_T search_hl; /* used for 'hlsearch' highlight matching */
105 #endif
107 #ifdef FEAT_FOLDING
108 static foldinfo_T win_foldinfo; /* info for 'foldcolumn' */
109 #endif
112 * Buffer for one screen line (characters and attributes).
114 static schar_T *current_ScreenLine;
116 static void win_update __ARGS((win_T *wp));
117 static void win_draw_end __ARGS((win_T *wp, int c1, int c2, int row, int endrow, hlf_T hl));
118 #ifdef FEAT_FOLDING
119 static void fold_line __ARGS((win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row));
120 static void fill_foldcolumn __ARGS((char_u *p, win_T *wp, int closed, linenr_T lnum));
121 static void copy_text_attr __ARGS((int off, char_u *buf, int len, int attr));
122 #endif
123 static int win_line __ARGS((win_T *, linenr_T, int, int, int nochange));
124 static int char_needs_redraw __ARGS((int off_from, int off_to, int cols));
125 #ifdef FEAT_RIGHTLEFT
126 static void screen_line __ARGS((int row, int coloff, int endcol, int clear_width, int rlflag));
127 # define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c), (rl))
128 #else
129 static void screen_line __ARGS((int row, int coloff, int endcol, int clear_width));
130 # define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c))
131 #endif
132 #ifdef FEAT_VERTSPLIT
133 static void draw_vsep_win __ARGS((win_T *wp, int row));
134 #endif
135 #ifdef FEAT_STL_OPT
136 static void redraw_custom_statusline __ARGS((win_T *wp));
137 #endif
138 #ifdef FEAT_SEARCH_EXTRA
139 #define SEARCH_HL_PRIORITY 0
140 static void start_search_hl __ARGS((void));
141 static void end_search_hl __ARGS((void));
142 static void prepare_search_hl __ARGS((win_T *wp, linenr_T lnum));
143 static void next_search_hl __ARGS((win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol));
144 #endif
145 static void screen_start_highlight __ARGS((int attr));
146 static void screen_char __ARGS((unsigned off, int row, int col));
147 #ifdef FEAT_MBYTE
148 static void screen_char_2 __ARGS((unsigned off, int row, int col));
149 #endif
150 static void screenclear2 __ARGS((void));
151 static void lineclear __ARGS((unsigned off, int width));
152 static void lineinvalid __ARGS((unsigned off, int width));
153 #ifdef FEAT_VERTSPLIT
154 static void linecopy __ARGS((int to, int from, win_T *wp));
155 static void redraw_block __ARGS((int row, int end, win_T *wp));
156 #endif
157 static int win_do_lines __ARGS((win_T *wp, int row, int line_count, int mayclear, int del));
158 static void win_rest_invalid __ARGS((win_T *wp));
159 static void msg_pos_mode __ARGS((void));
160 #if defined(FEAT_WINDOWS)
161 static void draw_tabline __ARGS((void));
162 #endif
163 #if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
164 static int fillchar_status __ARGS((int *attr, int is_curwin));
165 #endif
166 #ifdef FEAT_VERTSPLIT
167 static int fillchar_vsep __ARGS((int *attr));
168 #endif
169 #ifdef FEAT_STL_OPT
170 static void win_redr_custom __ARGS((win_T *wp, int draw_ruler));
171 #endif
172 #ifdef FEAT_CMDL_INFO
173 static void win_redr_ruler __ARGS((win_T *wp, int always));
174 #endif
176 #if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
177 /* Ugly global: overrule attribute used by screen_char() */
178 static int screen_char_attr = 0;
179 #endif
182 * Redraw the current window later, with update_screen(type).
183 * Set must_redraw only if not already set to a higher value.
184 * e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
186 void
187 redraw_later(type)
188 int type;
190 redraw_win_later(curwin, type);
193 void
194 redraw_win_later(wp, type)
195 win_T *wp;
196 int type;
198 if (wp->w_redr_type < type)
200 wp->w_redr_type = type;
201 if (type >= NOT_VALID)
202 wp->w_lines_valid = 0;
203 if (must_redraw < type) /* must_redraw is the maximum of all windows */
204 must_redraw = type;
209 * Force a complete redraw later. Also resets the highlighting. To be used
210 * after executing a shell command that messes up the screen.
212 void
213 redraw_later_clear()
215 redraw_all_later(CLEAR);
216 #ifdef FEAT_GUI
217 if (gui.in_use)
218 /* Use a code that will reset gui.highlight_mask in
219 * gui_stop_highlight(). */
220 screen_attr = HL_ALL + 1;
221 else
222 #endif
223 /* Use attributes that is very unlikely to appear in text. */
224 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE;
228 * Mark all windows to be redrawn later.
230 void
231 redraw_all_later(type)
232 int type;
234 win_T *wp;
236 FOR_ALL_WINDOWS(wp)
238 redraw_win_later(wp, type);
243 * Mark all windows that are editing the current buffer to be updated later.
245 void
246 redraw_curbuf_later(type)
247 int type;
249 redraw_buf_later(curbuf, type);
252 void
253 redraw_buf_later(buf, type)
254 buf_T *buf;
255 int type;
257 win_T *wp;
259 FOR_ALL_WINDOWS(wp)
261 if (wp->w_buffer == buf)
262 redraw_win_later(wp, type);
267 * Changed something in the current window, at buffer line "lnum", that
268 * requires that line and possibly other lines to be redrawn.
269 * Used when entering/leaving Insert mode with the cursor on a folded line.
270 * Used to remove the "$" from a change command.
271 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
272 * may become invalid and the whole window will have to be redrawn.
274 void
275 redrawWinline(lnum, invalid)
276 linenr_T lnum;
277 int invalid UNUSED; /* window line height is invalid now */
279 #ifdef FEAT_FOLDING
280 int i;
281 #endif
283 if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
284 curwin->w_redraw_top = lnum;
285 if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
286 curwin->w_redraw_bot = lnum;
287 redraw_later(VALID);
289 #ifdef FEAT_FOLDING
290 if (invalid)
292 /* A w_lines[] entry for this lnum has become invalid. */
293 i = find_wl_entry(curwin, lnum);
294 if (i >= 0)
295 curwin->w_lines[i].wl_valid = FALSE;
297 #endif
301 * update all windows that are editing the current buffer
303 void
304 update_curbuf(type)
305 int type;
307 redraw_curbuf_later(type);
308 update_screen(type);
312 * update_screen()
314 * Based on the current value of curwin->w_topline, transfer a screenfull
315 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
317 void
318 update_screen(type)
319 int type;
321 win_T *wp;
322 static int did_intro = FALSE;
323 #if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
324 int did_one;
325 #endif
327 /* Don't do anything if the screen structures are (not yet) valid. */
328 if (!screen_valid(TRUE))
329 return;
331 if (must_redraw)
333 if (type < must_redraw) /* use maximal type */
334 type = must_redraw;
336 /* must_redraw is reset here, so that when we run into some weird
337 * reason to redraw while busy redrawing (e.g., asynchronous
338 * scrolling), or update_topline() in win_update() will cause a
339 * scroll, the screen will be redrawn later or in win_update(). */
340 must_redraw = 0;
343 /* Need to update w_lines[]. */
344 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
345 type = NOT_VALID;
347 /* Postpone the redrawing when it's not needed and when being called
348 * recursively. */
349 if (!redrawing() || updating_screen)
351 redraw_later(type); /* remember type for next time */
352 must_redraw = type;
353 if (type > INVERTED_ALL)
354 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
355 return;
358 updating_screen = TRUE;
359 #ifdef FEAT_SYN_HL
360 ++display_tick; /* let syntax code know we're in a next round of
361 * display updating */
362 #endif
365 * if the screen was scrolled up when displaying a message, scroll it down
367 if (msg_scrolled)
369 clear_cmdline = TRUE;
370 if (msg_scrolled > Rows - 5) /* clearing is faster */
371 type = CLEAR;
372 else if (type != CLEAR)
374 check_for_delay(FALSE);
375 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, NULL) == FAIL)
376 type = CLEAR;
377 FOR_ALL_WINDOWS(wp)
379 if (W_WINROW(wp) < msg_scrolled)
381 if (W_WINROW(wp) + wp->w_height > msg_scrolled
382 && wp->w_redr_type < REDRAW_TOP
383 && wp->w_lines_valid > 0
384 && wp->w_topline == wp->w_lines[0].wl_lnum)
386 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
387 wp->w_redr_type = REDRAW_TOP;
389 else
391 wp->w_redr_type = NOT_VALID;
392 #ifdef FEAT_WINDOWS
393 if (W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp)
394 <= msg_scrolled)
395 wp->w_redr_status = TRUE;
396 #endif
400 redraw_cmdline = TRUE;
401 #ifdef FEAT_WINDOWS
402 redraw_tabline = TRUE;
403 #endif
405 msg_scrolled = 0;
406 need_wait_return = FALSE;
409 /* reset cmdline_row now (may have been changed temporarily) */
410 compute_cmdrow();
412 /* Check for changed highlighting */
413 if (need_highlight_changed)
414 highlight_changed();
416 if (type == CLEAR) /* first clear screen */
418 screenclear(); /* will reset clear_cmdline */
419 type = NOT_VALID;
422 if (clear_cmdline) /* going to clear cmdline (done below) */
423 check_for_delay(FALSE);
425 #ifdef FEAT_LINEBREAK
426 /* Force redraw when width of 'number' column changes. */
427 if (curwin->w_redr_type < NOT_VALID
428 && curwin->w_nrwidth != (curwin->w_p_nu ? number_width(curwin) : 0))
429 curwin->w_redr_type = NOT_VALID;
430 #endif
433 * Only start redrawing if there is really something to do.
435 if (type == INVERTED)
436 update_curswant();
437 if (curwin->w_redr_type < type
438 && !((type == VALID
439 && curwin->w_lines[0].wl_valid
440 #ifdef FEAT_DIFF
441 && curwin->w_topfill == curwin->w_old_topfill
442 && curwin->w_botfill == curwin->w_old_botfill
443 #endif
444 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
445 #ifdef FEAT_VISUAL
446 || (type == INVERTED
447 && VIsual_active
448 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
449 && curwin->w_old_visual_mode == VIsual_mode
450 && (curwin->w_valid & VALID_VIRTCOL)
451 && curwin->w_old_curswant == curwin->w_curswant)
452 #endif
454 curwin->w_redr_type = type;
456 #ifdef FEAT_WINDOWS
457 /* Redraw the tab pages line if needed. */
458 if (redraw_tabline || type >= NOT_VALID)
459 draw_tabline();
460 #endif
462 #ifdef FEAT_SYN_HL
464 * Correct stored syntax highlighting info for changes in each displayed
465 * buffer. Each buffer must only be done once.
467 FOR_ALL_WINDOWS(wp)
469 if (wp->w_buffer->b_mod_set)
471 # ifdef FEAT_WINDOWS
472 win_T *wwp;
474 /* Check if we already did this buffer. */
475 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
476 if (wwp->w_buffer == wp->w_buffer)
477 break;
478 # endif
479 if (
480 # ifdef FEAT_WINDOWS
481 wwp == wp &&
482 # endif
483 syntax_present(wp->w_buffer))
484 syn_stack_apply_changes(wp->w_buffer);
487 #endif
490 * Go from top to bottom through the windows, redrawing the ones that need
491 * it.
493 #if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
494 did_one = FALSE;
495 #endif
496 #ifdef FEAT_SEARCH_EXTRA
497 search_hl.rm.regprog = NULL;
498 #endif
499 FOR_ALL_WINDOWS(wp)
501 if (wp->w_redr_type != 0)
503 cursor_off();
504 #if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
505 if (!did_one)
507 did_one = TRUE;
508 # ifdef FEAT_SEARCH_EXTRA
509 start_search_hl();
510 # endif
511 # ifdef FEAT_CLIPBOARD
512 /* When Visual area changed, may have to update selection. */
513 if (clip_star.available && clip_isautosel())
514 clip_update_selection();
515 # endif
516 #ifdef FEAT_GUI
517 /* Remove the cursor before starting to do anything, because
518 * scrolling may make it difficult to redraw the text under
519 * it. */
520 if (gui.in_use)
521 gui_undraw_cursor();
522 #endif
524 #endif
525 win_update(wp);
528 #ifdef FEAT_WINDOWS
529 /* redraw status line after the window to minimize cursor movement */
530 if (wp->w_redr_status)
532 cursor_off();
533 win_redr_status(wp);
535 #endif
537 #if defined(FEAT_SEARCH_EXTRA)
538 end_search_hl();
539 #endif
541 #ifdef FEAT_WINDOWS
542 /* Reset b_mod_set flags. Going through all windows is probably faster
543 * than going through all buffers (there could be many buffers). */
544 for (wp = firstwin; wp != NULL; wp = wp->w_next)
545 wp->w_buffer->b_mod_set = FALSE;
546 #else
547 curbuf->b_mod_set = FALSE;
548 #endif
550 updating_screen = FALSE;
551 #ifdef FEAT_GUI
552 gui_may_resize_shell();
553 #endif
555 /* Clear or redraw the command line. Done last, because scrolling may
556 * mess up the command line. */
557 if (clear_cmdline || redraw_cmdline)
558 showmode();
560 /* May put up an introductory message when not editing a file */
561 if (!did_intro && bufempty()
562 && curbuf->b_fname == NULL
563 #ifdef FEAT_WINDOWS
564 && firstwin->w_next == NULL
565 #endif
566 && vim_strchr(p_shm, SHM_INTRO) == NULL)
567 intro_message(FALSE);
568 did_intro = TRUE;
570 #ifdef FEAT_GUI
571 /* Redraw the cursor and update the scrollbars when all screen updating is
572 * done. */
573 if (gui.in_use)
575 out_flush(); /* required before updating the cursor */
576 if (did_one)
577 gui_update_cursor(FALSE, FALSE);
578 gui_update_scrollbars(FALSE);
580 #endif
583 #if defined(FEAT_SIGNS) || defined(FEAT_GUI)
584 static void update_prepare __ARGS((void));
585 static void update_finish __ARGS((void));
588 * Prepare for updating one or more windows.
589 * Caller must check for "updating_screen" already set to avoid recursiveness.
591 static void
592 update_prepare()
594 cursor_off();
595 updating_screen = TRUE;
596 #ifdef FEAT_GUI
597 /* Remove the cursor before starting to do anything, because scrolling may
598 * make it difficult to redraw the text under it. */
599 if (gui.in_use)
600 gui_undraw_cursor();
601 #endif
602 #ifdef FEAT_SEARCH_EXTRA
603 start_search_hl();
604 #endif
608 * Finish updating one or more windows.
610 static void
611 update_finish()
613 if (redraw_cmdline)
614 showmode();
616 # ifdef FEAT_SEARCH_EXTRA
617 end_search_hl();
618 # endif
620 updating_screen = FALSE;
622 # ifdef FEAT_GUI
623 gui_may_resize_shell();
625 /* Redraw the cursor and update the scrollbars when all screen updating is
626 * done. */
627 if (gui.in_use)
629 out_flush(); /* required before updating the cursor */
630 gui_update_cursor(FALSE, FALSE);
631 gui_update_scrollbars(FALSE);
633 # endif
635 #endif
637 #if defined(FEAT_SIGNS) || defined(PROTO)
638 void
639 update_debug_sign(buf, lnum)
640 buf_T *buf;
641 linenr_T lnum;
643 win_T *wp;
644 int doit = FALSE;
646 # ifdef FEAT_FOLDING
647 win_foldinfo.fi_level = 0;
648 # endif
650 /* update/delete a specific mark */
651 FOR_ALL_WINDOWS(wp)
653 if (buf != NULL && lnum > 0)
655 if (wp->w_buffer == buf && lnum >= wp->w_topline
656 && lnum < wp->w_botline)
658 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
659 wp->w_redraw_top = lnum;
660 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
661 wp->w_redraw_bot = lnum;
662 redraw_win_later(wp, VALID);
665 else
666 redraw_win_later(wp, VALID);
667 if (wp->w_redr_type != 0)
668 doit = TRUE;
671 /* Return when there is nothing to do or screen updating already
672 * happening. */
673 if (!doit || updating_screen)
674 return;
676 /* update all windows that need updating */
677 update_prepare();
679 # ifdef FEAT_WINDOWS
680 for (wp = firstwin; wp; wp = wp->w_next)
682 if (wp->w_redr_type != 0)
683 win_update(wp);
684 if (wp->w_redr_status)
685 win_redr_status(wp);
687 # else
688 if (curwin->w_redr_type != 0)
689 win_update(curwin);
690 # endif
692 update_finish();
694 #endif
697 #if defined(FEAT_GUI) || defined(PROTO)
699 * Update a single window, its status line and maybe the command line msg.
700 * Used for the GUI scrollbar.
702 void
703 updateWindow(wp)
704 win_T *wp;
706 /* return if already busy updating */
707 if (updating_screen)
708 return;
710 update_prepare();
712 #ifdef FEAT_CLIPBOARD
713 /* When Visual area changed, may have to update selection. */
714 if (clip_star.available && clip_isautosel())
715 clip_update_selection();
716 #endif
718 win_update(wp);
720 #ifdef FEAT_WINDOWS
721 /* When the screen was cleared redraw the tab pages line. */
722 if (redraw_tabline)
723 draw_tabline();
725 if (wp->w_redr_status
726 # ifdef FEAT_CMDL_INFO
727 || p_ru
728 # endif
729 # ifdef FEAT_STL_OPT
730 || *p_stl != NUL || *wp->w_p_stl != NUL
731 # endif
733 win_redr_status(wp);
734 #endif
736 update_finish();
738 #endif
741 * Update a single window.
743 * This may cause the windows below it also to be redrawn (when clearing the
744 * screen or scrolling lines).
746 * How the window is redrawn depends on wp->w_redr_type. Each type also
747 * implies the one below it.
748 * NOT_VALID redraw the whole window
749 * SOME_VALID redraw the whole window but do scroll when possible
750 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
751 * INVERTED redraw the changed part of the Visual area
752 * INVERTED_ALL redraw the whole Visual area
753 * VALID 1. scroll up/down to adjust for a changed w_topline
754 * 2. update lines at the top when scrolled down
755 * 3. redraw changed text:
756 * - if wp->w_buffer->b_mod_set set, update lines between
757 * b_mod_top and b_mod_bot.
758 * - if wp->w_redraw_top non-zero, redraw lines between
759 * wp->w_redraw_top and wp->w_redr_bot.
760 * - continue redrawing when syntax status is invalid.
761 * 4. if scrolled up, update lines at the bottom.
762 * This results in three areas that may need updating:
763 * top: from first row to top_end (when scrolled down)
764 * mid: from mid_start to mid_end (update inversion or changed text)
765 * bot: from bot_start to last row (when scrolled up)
767 static void
768 win_update(wp)
769 win_T *wp;
771 buf_T *buf = wp->w_buffer;
772 int type;
773 int top_end = 0; /* Below last row of the top area that needs
774 updating. 0 when no top area updating. */
775 int mid_start = 999;/* first row of the mid area that needs
776 updating. 999 when no mid area updating. */
777 int mid_end = 0; /* Below last row of the mid area that needs
778 updating. 0 when no mid area updating. */
779 int bot_start = 999;/* first row of the bot area that needs
780 updating. 999 when no bot area updating */
781 #ifdef FEAT_VISUAL
782 int scrolled_down = FALSE; /* TRUE when scrolled down when
783 w_topline got smaller a bit */
784 #endif
785 #ifdef FEAT_SEARCH_EXTRA
786 matchitem_T *cur; /* points to the match list */
787 int top_to_mod = FALSE; /* redraw above mod_top */
788 #endif
790 int row; /* current window row to display */
791 linenr_T lnum; /* current buffer lnum to display */
792 int idx; /* current index in w_lines[] */
793 int srow; /* starting row of the current line */
795 int eof = FALSE; /* if TRUE, we hit the end of the file */
796 int didline = FALSE; /* if TRUE, we finished the last line */
797 int i;
798 long j;
799 static int recursive = FALSE; /* being called recursively */
800 int old_botline = wp->w_botline;
801 #ifdef FEAT_FOLDING
802 long fold_count;
803 #endif
804 #ifdef FEAT_SYN_HL
805 /* remember what happened to the previous line, to know if
806 * check_visual_highlight() can be used */
807 #define DID_NONE 1 /* didn't update a line */
808 #define DID_LINE 2 /* updated a normal line */
809 #define DID_FOLD 3 /* updated a folded line */
810 int did_update = DID_NONE;
811 linenr_T syntax_last_parsed = 0; /* last parsed text line */
812 #endif
813 linenr_T mod_top = 0;
814 linenr_T mod_bot = 0;
815 #if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
816 int save_got_int;
817 #endif
819 type = wp->w_redr_type;
821 if (type == NOT_VALID)
823 #ifdef FEAT_WINDOWS
824 wp->w_redr_status = TRUE;
825 #endif
826 wp->w_lines_valid = 0;
829 /* Window is zero-height: nothing to draw. */
830 if (wp->w_height == 0)
832 wp->w_redr_type = 0;
833 return;
836 #ifdef FEAT_VERTSPLIT
837 /* Window is zero-width: Only need to draw the separator. */
838 if (wp->w_width == 0)
840 /* draw the vertical separator right of this window */
841 draw_vsep_win(wp, 0);
842 wp->w_redr_type = 0;
843 return;
845 #endif
847 #ifdef FEAT_SEARCH_EXTRA
848 /* Setup for match and 'hlsearch' highlighting. Disable any previous
849 * match */
850 cur = wp->w_match_head;
851 while (cur != NULL)
853 cur->hl.rm = cur->match;
854 if (cur->hlg_id == 0)
855 cur->hl.attr = 0;
856 else
857 cur->hl.attr = syn_id2attr(cur->hlg_id);
858 cur->hl.buf = buf;
859 cur->hl.lnum = 0;
860 cur->hl.first_lnum = 0;
861 # ifdef FEAT_RELTIME
862 /* Set the time limit to 'redrawtime'. */
863 profile_setlimit(p_rdt, &(cur->hl.tm));
864 # endif
865 cur = cur->next;
867 search_hl.buf = buf;
868 search_hl.lnum = 0;
869 search_hl.first_lnum = 0;
870 /* time limit is set at the toplevel, for all windows */
871 #endif
873 #ifdef FEAT_LINEBREAK
874 /* Force redraw when width of 'number' column changes. */
875 i = wp->w_p_nu ? number_width(wp) : 0;
876 if (wp->w_nrwidth != i)
878 type = NOT_VALID;
879 wp->w_nrwidth = i;
881 else
882 #endif
884 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
887 * When there are both inserted/deleted lines and specific lines to be
888 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
889 * everything (only happens when redrawing is off for while).
891 type = NOT_VALID;
893 else
896 * Set mod_top to the first line that needs displaying because of
897 * changes. Set mod_bot to the first line after the changes.
899 mod_top = wp->w_redraw_top;
900 if (wp->w_redraw_bot != 0)
901 mod_bot = wp->w_redraw_bot + 1;
902 else
903 mod_bot = 0;
904 wp->w_redraw_top = 0; /* reset for next time */
905 wp->w_redraw_bot = 0;
906 if (buf->b_mod_set)
908 if (mod_top == 0 || mod_top > buf->b_mod_top)
910 mod_top = buf->b_mod_top;
911 #ifdef FEAT_SYN_HL
912 /* Need to redraw lines above the change that may be included
913 * in a pattern match. */
914 if (syntax_present(buf))
916 mod_top -= buf->b_syn_sync_linebreaks;
917 if (mod_top < 1)
918 mod_top = 1;
920 #endif
922 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
923 mod_bot = buf->b_mod_bot;
925 #ifdef FEAT_SEARCH_EXTRA
926 /* When 'hlsearch' is on and using a multi-line search pattern, a
927 * change in one line may make the Search highlighting in a
928 * previous line invalid. Simple solution: redraw all visible
929 * lines above the change.
930 * Same for a match pattern.
932 if (search_hl.rm.regprog != NULL
933 && re_multiline(search_hl.rm.regprog))
934 top_to_mod = TRUE;
935 else
937 cur = wp->w_match_head;
938 while (cur != NULL)
940 if (cur->match.regprog != NULL
941 && re_multiline(cur->match.regprog))
943 top_to_mod = TRUE;
944 break;
946 cur = cur->next;
949 #endif
951 #ifdef FEAT_FOLDING
952 if (mod_top != 0 && hasAnyFolding(wp))
954 linenr_T lnumt, lnumb;
957 * A change in a line can cause lines above it to become folded or
958 * unfolded. Find the top most buffer line that may be affected.
959 * If the line was previously folded and displayed, get the first
960 * line of that fold. If the line is folded now, get the first
961 * folded line. Use the minimum of these two.
964 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
965 * the line below it. If there is no valid entry, use w_topline.
966 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
967 * to this line. If there is no valid entry, use MAXLNUM. */
968 lnumt = wp->w_topline;
969 lnumb = MAXLNUM;
970 for (i = 0; i < wp->w_lines_valid; ++i)
971 if (wp->w_lines[i].wl_valid)
973 if (wp->w_lines[i].wl_lastlnum < mod_top)
974 lnumt = wp->w_lines[i].wl_lastlnum + 1;
975 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
977 lnumb = wp->w_lines[i].wl_lnum;
978 /* When there is a fold column it might need updating
979 * in the next line ("J" just above an open fold). */
980 if (wp->w_p_fdc > 0)
981 ++lnumb;
985 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
986 if (mod_top > lnumt)
987 mod_top = lnumt;
989 /* Now do the same for the bottom line (one above mod_bot). */
990 --mod_bot;
991 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
992 ++mod_bot;
993 if (mod_bot < lnumb)
994 mod_bot = lnumb;
996 #endif
998 /* When a change starts above w_topline and the end is below
999 * w_topline, start redrawing at w_topline.
1000 * If the end of the change is above w_topline: do like no change was
1001 * made, but redraw the first line to find changes in syntax. */
1002 if (mod_top != 0 && mod_top < wp->w_topline)
1004 if (mod_bot > wp->w_topline)
1005 mod_top = wp->w_topline;
1006 #ifdef FEAT_SYN_HL
1007 else if (syntax_present(buf))
1008 top_end = 1;
1009 #endif
1012 /* When line numbers are displayed need to redraw all lines below
1013 * inserted/deleted lines. */
1014 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1015 mod_bot = MAXLNUM;
1019 * When only displaying the lines at the top, set top_end. Used when
1020 * window has scrolled down for msg_scrolled.
1022 if (type == REDRAW_TOP)
1024 j = 0;
1025 for (i = 0; i < wp->w_lines_valid; ++i)
1027 j += wp->w_lines[i].wl_size;
1028 if (j >= wp->w_upd_rows)
1030 top_end = j;
1031 break;
1034 if (top_end == 0)
1035 /* not found (cannot happen?): redraw everything */
1036 type = NOT_VALID;
1037 else
1038 /* top area defined, the rest is VALID */
1039 type = VALID;
1042 /* Trick: we want to avoid clearing the screen twice. screenclear() will
1043 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1044 * non-zero and thus not FALSE) will indicate that screenclear() was not
1045 * called. */
1046 if (screen_cleared)
1047 screen_cleared = MAYBE;
1050 * If there are no changes on the screen that require a complete redraw,
1051 * handle three cases:
1052 * 1: we are off the top of the screen by a few lines: scroll down
1053 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1054 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1055 * w_lines[] that needs updating.
1057 if ((type == VALID || type == SOME_VALID
1058 || type == INVERTED || type == INVERTED_ALL)
1059 #ifdef FEAT_DIFF
1060 && !wp->w_botfill && !wp->w_old_botfill
1061 #endif
1064 if (mod_top != 0 && wp->w_topline == mod_top)
1067 * w_topline is the first changed line, the scrolling will be done
1068 * further down.
1071 else if (wp->w_lines[0].wl_valid
1072 && (wp->w_topline < wp->w_lines[0].wl_lnum
1073 #ifdef FEAT_DIFF
1074 || (wp->w_topline == wp->w_lines[0].wl_lnum
1075 && wp->w_topfill > wp->w_old_topfill)
1076 #endif
1080 * New topline is above old topline: May scroll down.
1082 #ifdef FEAT_FOLDING
1083 if (hasAnyFolding(wp))
1085 linenr_T ln;
1087 /* count the number of lines we are off, counting a sequence
1088 * of folded lines as one */
1089 j = 0;
1090 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1092 ++j;
1093 if (j >= wp->w_height - 2)
1094 break;
1095 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1098 else
1099 #endif
1100 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1101 if (j < wp->w_height - 2) /* not too far off */
1103 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1104 #ifdef FEAT_DIFF
1105 /* insert extra lines for previously invisible filler lines */
1106 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1107 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1108 - wp->w_old_topfill;
1109 #endif
1110 if (i < wp->w_height - 2) /* less than a screen off */
1113 * Try to insert the correct number of lines.
1114 * If not the last window, delete the lines at the bottom.
1115 * win_ins_lines may fail when the terminal can't do it.
1117 if (i > 0)
1118 check_for_delay(FALSE);
1119 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1121 if (wp->w_lines_valid != 0)
1123 /* Need to update rows that are new, stop at the
1124 * first one that scrolled down. */
1125 top_end = i;
1126 #ifdef FEAT_VISUAL
1127 scrolled_down = TRUE;
1128 #endif
1130 /* Move the entries that were scrolled, disable
1131 * the entries for the lines to be redrawn. */
1132 if ((wp->w_lines_valid += j) > wp->w_height)
1133 wp->w_lines_valid = wp->w_height;
1134 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1135 wp->w_lines[idx] = wp->w_lines[idx - j];
1136 while (idx >= 0)
1137 wp->w_lines[idx--].wl_valid = FALSE;
1140 else
1141 mid_start = 0; /* redraw all lines */
1143 else
1144 mid_start = 0; /* redraw all lines */
1146 else
1147 mid_start = 0; /* redraw all lines */
1149 else
1152 * New topline is at or below old topline: May scroll up.
1153 * When topline didn't change, find first entry in w_lines[] that
1154 * needs updating.
1157 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1158 j = -1;
1159 row = 0;
1160 for (i = 0; i < wp->w_lines_valid; i++)
1162 if (wp->w_lines[i].wl_valid
1163 && wp->w_lines[i].wl_lnum == wp->w_topline)
1165 j = i;
1166 break;
1168 row += wp->w_lines[i].wl_size;
1170 if (j == -1)
1172 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1173 * lines */
1174 mid_start = 0;
1176 else
1179 * Try to delete the correct number of lines.
1180 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1182 #ifdef FEAT_DIFF
1183 /* If the topline didn't change, delete old filler lines,
1184 * otherwise delete filler lines of the new topline... */
1185 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1186 row += wp->w_old_topfill;
1187 else
1188 row += diff_check_fill(wp, wp->w_topline);
1189 /* ... but don't delete new filler lines. */
1190 row -= wp->w_topfill;
1191 #endif
1192 if (row > 0)
1194 check_for_delay(FALSE);
1195 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1196 bot_start = wp->w_height - row;
1197 else
1198 mid_start = 0; /* redraw all lines */
1200 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1203 * Skip the lines (below the deleted lines) that are still
1204 * valid and don't need redrawing. Copy their info
1205 * upwards, to compensate for the deleted lines. Set
1206 * bot_start to the first row that needs redrawing.
1208 bot_start = 0;
1209 idx = 0;
1210 for (;;)
1212 wp->w_lines[idx] = wp->w_lines[j];
1213 /* stop at line that didn't fit, unless it is still
1214 * valid (no lines deleted) */
1215 if (row > 0 && bot_start + row
1216 + (int)wp->w_lines[j].wl_size > wp->w_height)
1218 wp->w_lines_valid = idx + 1;
1219 break;
1221 bot_start += wp->w_lines[idx++].wl_size;
1223 /* stop at the last valid entry in w_lines[].wl_size */
1224 if (++j >= wp->w_lines_valid)
1226 wp->w_lines_valid = idx;
1227 break;
1230 #ifdef FEAT_DIFF
1231 /* Correct the first entry for filler lines at the top
1232 * when it won't get updated below. */
1233 if (wp->w_p_diff && bot_start > 0)
1234 wp->w_lines[0].wl_size =
1235 plines_win_nofill(wp, wp->w_topline, TRUE)
1236 + wp->w_topfill;
1237 #endif
1242 /* When starting redraw in the first line, redraw all lines. When
1243 * there is only one window it's probably faster to clear the screen
1244 * first. */
1245 if (mid_start == 0)
1247 mid_end = wp->w_height;
1248 if (lastwin == firstwin)
1250 /* Clear the screen when it was not done by win_del_lines() or
1251 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1252 * then. */
1253 if (screen_cleared != TRUE)
1254 screenclear();
1255 #ifdef FEAT_WINDOWS
1256 /* The screen was cleared, redraw the tab pages line. */
1257 if (redraw_tabline)
1258 draw_tabline();
1259 #endif
1263 /* When win_del_lines() or win_ins_lines() caused the screen to be
1264 * cleared (only happens for the first window) or when screenclear()
1265 * was called directly above, "must_redraw" will have been set to
1266 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1267 if (screen_cleared == TRUE)
1268 must_redraw = 0;
1270 else
1272 /* Not VALID or INVERTED: redraw all lines. */
1273 mid_start = 0;
1274 mid_end = wp->w_height;
1277 if (type == SOME_VALID)
1279 /* SOME_VALID: redraw all lines. */
1280 mid_start = 0;
1281 mid_end = wp->w_height;
1282 type = NOT_VALID;
1285 #ifdef FEAT_VISUAL
1286 /* check if we are updating or removing the inverted part */
1287 if ((VIsual_active && buf == curwin->w_buffer)
1288 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1290 linenr_T from, to;
1292 if (VIsual_active)
1294 if (VIsual_active
1295 && (VIsual_mode != wp->w_old_visual_mode
1296 || type == INVERTED_ALL))
1299 * If the type of Visual selection changed, redraw the whole
1300 * selection. Also when the ownership of the X selection is
1301 * gained or lost.
1303 if (curwin->w_cursor.lnum < VIsual.lnum)
1305 from = curwin->w_cursor.lnum;
1306 to = VIsual.lnum;
1308 else
1310 from = VIsual.lnum;
1311 to = curwin->w_cursor.lnum;
1313 /* redraw more when the cursor moved as well */
1314 if (wp->w_old_cursor_lnum < from)
1315 from = wp->w_old_cursor_lnum;
1316 if (wp->w_old_cursor_lnum > to)
1317 to = wp->w_old_cursor_lnum;
1318 if (wp->w_old_visual_lnum < from)
1319 from = wp->w_old_visual_lnum;
1320 if (wp->w_old_visual_lnum > to)
1321 to = wp->w_old_visual_lnum;
1323 else
1326 * Find the line numbers that need to be updated: The lines
1327 * between the old cursor position and the current cursor
1328 * position. Also check if the Visual position changed.
1330 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1332 from = curwin->w_cursor.lnum;
1333 to = wp->w_old_cursor_lnum;
1335 else
1337 from = wp->w_old_cursor_lnum;
1338 to = curwin->w_cursor.lnum;
1339 if (from == 0) /* Visual mode just started */
1340 from = to;
1343 if (VIsual.lnum != wp->w_old_visual_lnum
1344 || VIsual.col != wp->w_old_visual_col)
1346 if (wp->w_old_visual_lnum < from
1347 && wp->w_old_visual_lnum != 0)
1348 from = wp->w_old_visual_lnum;
1349 if (wp->w_old_visual_lnum > to)
1350 to = wp->w_old_visual_lnum;
1351 if (VIsual.lnum < from)
1352 from = VIsual.lnum;
1353 if (VIsual.lnum > to)
1354 to = VIsual.lnum;
1359 * If in block mode and changed column or curwin->w_curswant:
1360 * update all lines.
1361 * First compute the actual start and end column.
1363 if (VIsual_mode == Ctrl_V)
1365 colnr_T fromc, toc;
1367 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
1368 ++toc;
1369 if (curwin->w_curswant == MAXCOL)
1370 toc = MAXCOL;
1372 if (fromc != wp->w_old_cursor_fcol
1373 || toc != wp->w_old_cursor_lcol)
1375 if (from > VIsual.lnum)
1376 from = VIsual.lnum;
1377 if (to < VIsual.lnum)
1378 to = VIsual.lnum;
1380 wp->w_old_cursor_fcol = fromc;
1381 wp->w_old_cursor_lcol = toc;
1384 else
1386 /* Use the line numbers of the old Visual area. */
1387 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1389 from = wp->w_old_cursor_lnum;
1390 to = wp->w_old_visual_lnum;
1392 else
1394 from = wp->w_old_visual_lnum;
1395 to = wp->w_old_cursor_lnum;
1400 * There is no need to update lines above the top of the window.
1402 if (from < wp->w_topline)
1403 from = wp->w_topline;
1406 * If we know the value of w_botline, use it to restrict the update to
1407 * the lines that are visible in the window.
1409 if (wp->w_valid & VALID_BOTLINE)
1411 if (from >= wp->w_botline)
1412 from = wp->w_botline - 1;
1413 if (to >= wp->w_botline)
1414 to = wp->w_botline - 1;
1418 * Find the minimal part to be updated.
1419 * Watch out for scrolling that made entries in w_lines[] invalid.
1420 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1421 * top_end; need to redraw from top_end to the "to" line.
1422 * A middle mouse click with a Visual selection may change the text
1423 * above the Visual area and reset wl_valid, do count these for
1424 * mid_end (in srow).
1426 if (mid_start > 0)
1428 lnum = wp->w_topline;
1429 idx = 0;
1430 srow = 0;
1431 if (scrolled_down)
1432 mid_start = top_end;
1433 else
1434 mid_start = 0;
1435 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1437 if (wp->w_lines[idx].wl_valid)
1438 mid_start += wp->w_lines[idx].wl_size;
1439 else if (!scrolled_down)
1440 srow += wp->w_lines[idx].wl_size;
1441 ++idx;
1442 # ifdef FEAT_FOLDING
1443 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1444 lnum = wp->w_lines[idx].wl_lnum;
1445 else
1446 # endif
1447 ++lnum;
1449 srow += mid_start;
1450 mid_end = wp->w_height;
1451 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1453 if (wp->w_lines[idx].wl_valid
1454 && wp->w_lines[idx].wl_lnum >= to + 1)
1456 /* Only update until first row of this line */
1457 mid_end = srow;
1458 break;
1460 srow += wp->w_lines[idx].wl_size;
1465 if (VIsual_active && buf == curwin->w_buffer)
1467 wp->w_old_visual_mode = VIsual_mode;
1468 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1469 wp->w_old_visual_lnum = VIsual.lnum;
1470 wp->w_old_visual_col = VIsual.col;
1471 wp->w_old_curswant = curwin->w_curswant;
1473 else
1475 wp->w_old_visual_mode = 0;
1476 wp->w_old_cursor_lnum = 0;
1477 wp->w_old_visual_lnum = 0;
1478 wp->w_old_visual_col = 0;
1480 #endif /* FEAT_VISUAL */
1482 #if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1483 /* reset got_int, otherwise regexp won't work */
1484 save_got_int = got_int;
1485 got_int = 0;
1486 #endif
1487 #ifdef FEAT_FOLDING
1488 win_foldinfo.fi_level = 0;
1489 #endif
1492 * Update all the window rows.
1494 idx = 0; /* first entry in w_lines[].wl_size */
1495 row = 0;
1496 srow = 0;
1497 lnum = wp->w_topline; /* first line shown in window */
1498 for (;;)
1500 /* stop updating when reached the end of the window (check for _past_
1501 * the end of the window is at the end of the loop) */
1502 if (row == wp->w_height)
1504 didline = TRUE;
1505 break;
1508 /* stop updating when hit the end of the file */
1509 if (lnum > buf->b_ml.ml_line_count)
1511 eof = TRUE;
1512 break;
1515 /* Remember the starting row of the line that is going to be dealt
1516 * with. It is used further down when the line doesn't fit. */
1517 srow = row;
1520 * Update a line when it is in an area that needs updating, when it
1521 * has changes or w_lines[idx] is invalid.
1522 * bot_start may be halfway a wrapped line after using
1523 * win_del_lines(), check if the current line includes it.
1524 * When syntax folding is being used, the saved syntax states will
1525 * already have been updated, we can't see where the syntax state is
1526 * the same again, just update until the end of the window.
1528 if (row < top_end
1529 || (row >= mid_start && row < mid_end)
1530 #ifdef FEAT_SEARCH_EXTRA
1531 || top_to_mod
1532 #endif
1533 || idx >= wp->w_lines_valid
1534 || (row + wp->w_lines[idx].wl_size > bot_start)
1535 || (mod_top != 0
1536 && (lnum == mod_top
1537 || (lnum >= mod_top
1538 && (lnum < mod_bot
1539 #ifdef FEAT_SYN_HL
1540 || did_update == DID_FOLD
1541 || (did_update == DID_LINE
1542 && syntax_present(buf)
1543 && (
1544 # ifdef FEAT_FOLDING
1545 (foldmethodIsSyntax(wp)
1546 && hasAnyFolding(wp)) ||
1547 # endif
1548 syntax_check_changed(lnum)))
1549 #endif
1550 )))))
1552 #ifdef FEAT_SEARCH_EXTRA
1553 if (lnum == mod_top)
1554 top_to_mod = FALSE;
1555 #endif
1558 * When at start of changed lines: May scroll following lines
1559 * up or down to minimize redrawing.
1560 * Don't do this when the change continues until the end.
1561 * Don't scroll when dollar_vcol is non-zero, keep the "$".
1563 if (lnum == mod_top
1564 && mod_bot != MAXLNUM
1565 && !(dollar_vcol != 0 && mod_bot == mod_top + 1))
1567 int old_rows = 0;
1568 int new_rows = 0;
1569 int xtra_rows;
1570 linenr_T l;
1572 /* Count the old number of window rows, using w_lines[], which
1573 * should still contain the sizes for the lines as they are
1574 * currently displayed. */
1575 for (i = idx; i < wp->w_lines_valid; ++i)
1577 /* Only valid lines have a meaningful wl_lnum. Invalid
1578 * lines are part of the changed area. */
1579 if (wp->w_lines[i].wl_valid
1580 && wp->w_lines[i].wl_lnum == mod_bot)
1581 break;
1582 old_rows += wp->w_lines[i].wl_size;
1583 #ifdef FEAT_FOLDING
1584 if (wp->w_lines[i].wl_valid
1585 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1587 /* Must have found the last valid entry above mod_bot.
1588 * Add following invalid entries. */
1589 ++i;
1590 while (i < wp->w_lines_valid
1591 && !wp->w_lines[i].wl_valid)
1592 old_rows += wp->w_lines[i++].wl_size;
1593 break;
1595 #endif
1598 if (i >= wp->w_lines_valid)
1600 /* We can't find a valid line below the changed lines,
1601 * need to redraw until the end of the window.
1602 * Inserting/deleting lines has no use. */
1603 bot_start = 0;
1605 else
1607 /* Able to count old number of rows: Count new window
1608 * rows, and may insert/delete lines */
1609 j = idx;
1610 for (l = lnum; l < mod_bot; ++l)
1612 #ifdef FEAT_FOLDING
1613 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1614 ++new_rows;
1615 else
1616 #endif
1617 #ifdef FEAT_DIFF
1618 if (l == wp->w_topline)
1619 new_rows += plines_win_nofill(wp, l, TRUE)
1620 + wp->w_topfill;
1621 else
1622 #endif
1623 new_rows += plines_win(wp, l, TRUE);
1624 ++j;
1625 if (new_rows > wp->w_height - row - 2)
1627 /* it's getting too much, must redraw the rest */
1628 new_rows = 9999;
1629 break;
1632 xtra_rows = new_rows - old_rows;
1633 if (xtra_rows < 0)
1635 /* May scroll text up. If there is not enough
1636 * remaining text or scrolling fails, must redraw the
1637 * rest. If scrolling works, must redraw the text
1638 * below the scrolled text. */
1639 if (row - xtra_rows >= wp->w_height - 2)
1640 mod_bot = MAXLNUM;
1641 else
1643 check_for_delay(FALSE);
1644 if (win_del_lines(wp, row,
1645 -xtra_rows, FALSE, FALSE) == FAIL)
1646 mod_bot = MAXLNUM;
1647 else
1648 bot_start = wp->w_height + xtra_rows;
1651 else if (xtra_rows > 0)
1653 /* May scroll text down. If there is not enough
1654 * remaining text of scrolling fails, must redraw the
1655 * rest. */
1656 if (row + xtra_rows >= wp->w_height - 2)
1657 mod_bot = MAXLNUM;
1658 else
1660 check_for_delay(FALSE);
1661 if (win_ins_lines(wp, row + old_rows,
1662 xtra_rows, FALSE, FALSE) == FAIL)
1663 mod_bot = MAXLNUM;
1664 else if (top_end > row + old_rows)
1665 /* Scrolled the part at the top that requires
1666 * updating down. */
1667 top_end += xtra_rows;
1671 /* When not updating the rest, may need to move w_lines[]
1672 * entries. */
1673 if (mod_bot != MAXLNUM && i != j)
1675 if (j < i)
1677 int x = row + new_rows;
1679 /* move entries in w_lines[] upwards */
1680 for (;;)
1682 /* stop at last valid entry in w_lines[] */
1683 if (i >= wp->w_lines_valid)
1685 wp->w_lines_valid = j;
1686 break;
1688 wp->w_lines[j] = wp->w_lines[i];
1689 /* stop at a line that won't fit */
1690 if (x + (int)wp->w_lines[j].wl_size
1691 > wp->w_height)
1693 wp->w_lines_valid = j + 1;
1694 break;
1696 x += wp->w_lines[j++].wl_size;
1697 ++i;
1699 if (bot_start > x)
1700 bot_start = x;
1702 else /* j > i */
1704 /* move entries in w_lines[] downwards */
1705 j -= i;
1706 wp->w_lines_valid += j;
1707 if (wp->w_lines_valid > wp->w_height)
1708 wp->w_lines_valid = wp->w_height;
1709 for (i = wp->w_lines_valid; i - j >= idx; --i)
1710 wp->w_lines[i] = wp->w_lines[i - j];
1712 /* The w_lines[] entries for inserted lines are
1713 * now invalid, but wl_size may be used above.
1714 * Reset to zero. */
1715 while (i >= idx)
1717 wp->w_lines[i].wl_size = 0;
1718 wp->w_lines[i--].wl_valid = FALSE;
1725 #ifdef FEAT_FOLDING
1727 * When lines are folded, display one line for all of them.
1728 * Otherwise, display normally (can be several display lines when
1729 * 'wrap' is on).
1731 fold_count = foldedCount(wp, lnum, &win_foldinfo);
1732 if (fold_count != 0)
1734 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
1735 ++row;
1736 --fold_count;
1737 wp->w_lines[idx].wl_folded = TRUE;
1738 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
1739 # ifdef FEAT_SYN_HL
1740 did_update = DID_FOLD;
1741 # endif
1743 else
1744 #endif
1745 if (idx < wp->w_lines_valid
1746 && wp->w_lines[idx].wl_valid
1747 && wp->w_lines[idx].wl_lnum == lnum
1748 && lnum > wp->w_topline
1749 && !(dy_flags & DY_LASTLINE)
1750 && srow + wp->w_lines[idx].wl_size > wp->w_height
1751 #ifdef FEAT_DIFF
1752 && diff_check_fill(wp, lnum) == 0
1753 #endif
1756 /* This line is not going to fit. Don't draw anything here,
1757 * will draw "@ " lines below. */
1758 row = wp->w_height + 1;
1760 else
1762 #ifdef FEAT_SEARCH_EXTRA
1763 prepare_search_hl(wp, lnum);
1764 #endif
1765 #ifdef FEAT_SYN_HL
1766 /* Let the syntax stuff know we skipped a few lines. */
1767 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
1768 && syntax_present(buf))
1769 syntax_end_parsing(syntax_last_parsed + 1);
1770 #endif
1773 * Display one line.
1775 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
1777 #ifdef FEAT_FOLDING
1778 wp->w_lines[idx].wl_folded = FALSE;
1779 wp->w_lines[idx].wl_lastlnum = lnum;
1780 #endif
1781 #ifdef FEAT_SYN_HL
1782 did_update = DID_LINE;
1783 syntax_last_parsed = lnum;
1784 #endif
1787 wp->w_lines[idx].wl_lnum = lnum;
1788 wp->w_lines[idx].wl_valid = TRUE;
1789 if (row > wp->w_height) /* past end of screen */
1791 /* we may need the size of that too long line later on */
1792 if (dollar_vcol == 0)
1793 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
1794 ++idx;
1795 break;
1797 if (dollar_vcol == 0)
1798 wp->w_lines[idx].wl_size = row - srow;
1799 ++idx;
1800 #ifdef FEAT_FOLDING
1801 lnum += fold_count + 1;
1802 #else
1803 ++lnum;
1804 #endif
1806 else
1808 /* This line does not need updating, advance to the next one */
1809 row += wp->w_lines[idx++].wl_size;
1810 if (row > wp->w_height) /* past end of screen */
1811 break;
1812 #ifdef FEAT_FOLDING
1813 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
1814 #else
1815 ++lnum;
1816 #endif
1817 #ifdef FEAT_SYN_HL
1818 did_update = DID_NONE;
1819 #endif
1822 if (lnum > buf->b_ml.ml_line_count)
1824 eof = TRUE;
1825 break;
1829 * End of loop over all window lines.
1833 if (idx > wp->w_lines_valid)
1834 wp->w_lines_valid = idx;
1836 #ifdef FEAT_SYN_HL
1838 * Let the syntax stuff know we stop parsing here.
1840 if (syntax_last_parsed != 0 && syntax_present(buf))
1841 syntax_end_parsing(syntax_last_parsed + 1);
1842 #endif
1845 * If we didn't hit the end of the file, and we didn't finish the last
1846 * line we were working on, then the line didn't fit.
1848 wp->w_empty_rows = 0;
1849 #ifdef FEAT_DIFF
1850 wp->w_filler_rows = 0;
1851 #endif
1852 if (!eof && !didline)
1854 if (lnum == wp->w_topline)
1857 * Single line that does not fit!
1858 * Don't overwrite it, it can be edited.
1860 wp->w_botline = lnum + 1;
1862 #ifdef FEAT_DIFF
1863 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
1865 /* Window ends in filler lines. */
1866 wp->w_botline = lnum;
1867 wp->w_filler_rows = wp->w_height - srow;
1869 #endif
1870 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
1873 * Last line isn't finished: Display "@@@" at the end.
1875 screen_fill(W_WINROW(wp) + wp->w_height - 1,
1876 W_WINROW(wp) + wp->w_height,
1877 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
1878 '@', '@', hl_attr(HLF_AT));
1879 set_empty_rows(wp, srow);
1880 wp->w_botline = lnum;
1882 else
1884 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
1885 wp->w_botline = lnum;
1888 else
1890 #ifdef FEAT_VERTSPLIT
1891 draw_vsep_win(wp, row);
1892 #endif
1893 if (eof) /* we hit the end of the file */
1895 wp->w_botline = buf->b_ml.ml_line_count + 1;
1896 #ifdef FEAT_DIFF
1897 j = diff_check_fill(wp, wp->w_botline);
1898 if (j > 0 && !wp->w_botfill)
1901 * Display filler lines at the end of the file
1903 if (char2cells(fill_diff) > 1)
1904 i = '-';
1905 else
1906 i = fill_diff;
1907 if (row + j > wp->w_height)
1908 j = wp->w_height - row;
1909 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
1910 row += j;
1912 #endif
1914 else if (dollar_vcol == 0)
1915 wp->w_botline = lnum;
1917 /* make sure the rest of the screen is blank */
1918 /* put '~'s on rows that aren't part of the file. */
1919 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_AT);
1922 /* Reset the type of redrawing required, the window has been updated. */
1923 wp->w_redr_type = 0;
1924 #ifdef FEAT_DIFF
1925 wp->w_old_topfill = wp->w_topfill;
1926 wp->w_old_botfill = wp->w_botfill;
1927 #endif
1929 if (dollar_vcol == 0)
1932 * There is a trick with w_botline. If we invalidate it on each
1933 * change that might modify it, this will cause a lot of expensive
1934 * calls to plines() in update_topline() each time. Therefore the
1935 * value of w_botline is often approximated, and this value is used to
1936 * compute the value of w_topline. If the value of w_botline was
1937 * wrong, check that the value of w_topline is correct (cursor is on
1938 * the visible part of the text). If it's not, we need to redraw
1939 * again. Mostly this just means scrolling up a few lines, so it
1940 * doesn't look too bad. Only do this for the current window (where
1941 * changes are relevant).
1943 wp->w_valid |= VALID_BOTLINE;
1944 if (wp == curwin && wp->w_botline != old_botline && !recursive)
1946 recursive = TRUE;
1947 curwin->w_valid &= ~VALID_TOPLINE;
1948 update_topline(); /* may invalidate w_botline again */
1949 if (must_redraw != 0)
1951 /* Don't update for changes in buffer again. */
1952 i = curbuf->b_mod_set;
1953 curbuf->b_mod_set = FALSE;
1954 win_update(curwin);
1955 must_redraw = 0;
1956 curbuf->b_mod_set = i;
1958 recursive = FALSE;
1962 #if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1963 /* restore got_int, unless CTRL-C was hit while redrawing */
1964 if (!got_int)
1965 got_int = save_got_int;
1966 #endif
1969 #ifdef FEAT_SIGNS
1970 static int draw_signcolumn __ARGS((win_T *wp));
1973 * Return TRUE when window "wp" has a column to draw signs in.
1975 static int
1976 draw_signcolumn(wp)
1977 win_T *wp;
1979 return (wp->w_buffer->b_signlist != NULL
1980 # ifdef FEAT_NETBEANS_INTG
1981 || usingNetbeans
1982 # endif
1985 #endif
1988 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
1989 * as the filler character.
1991 static void
1992 win_draw_end(wp, c1, c2, row, endrow, hl)
1993 win_T *wp;
1994 int c1;
1995 int c2;
1996 int row;
1997 int endrow;
1998 hlf_T hl;
2000 #if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2001 int n = 0;
2002 # define FDC_OFF n
2003 #else
2004 # define FDC_OFF 0
2005 #endif
2007 #ifdef FEAT_RIGHTLEFT
2008 if (wp->w_p_rl)
2010 /* No check for cmdline window: should never be right-left. */
2011 # ifdef FEAT_FOLDING
2012 n = wp->w_p_fdc;
2014 if (n > 0)
2016 /* draw the fold column at the right */
2017 if (n > W_WIDTH(wp))
2018 n = W_WIDTH(wp);
2019 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2020 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
2021 ' ', ' ', hl_attr(HLF_FC));
2023 # endif
2024 # ifdef FEAT_SIGNS
2025 if (draw_signcolumn(wp))
2027 int nn = n + 2;
2029 /* draw the sign column left of the fold column */
2030 if (nn > W_WIDTH(wp))
2031 nn = W_WIDTH(wp);
2032 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2033 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
2034 ' ', ' ', hl_attr(HLF_SC));
2035 n = nn;
2037 # endif
2038 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2039 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
2040 c2, c2, hl_attr(hl));
2041 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2042 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
2043 c1, c2, hl_attr(hl));
2045 else
2046 #endif
2048 #ifdef FEAT_CMDWIN
2049 if (cmdwin_type != 0 && wp == curwin)
2051 /* draw the cmdline character in the leftmost column */
2052 n = 1;
2053 if (n > wp->w_width)
2054 n = wp->w_width;
2055 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2056 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
2057 cmdwin_type, ' ', hl_attr(HLF_AT));
2059 #endif
2060 #ifdef FEAT_FOLDING
2061 if (wp->w_p_fdc > 0)
2063 int nn = n + wp->w_p_fdc;
2065 /* draw the fold column at the left */
2066 if (nn > W_WIDTH(wp))
2067 nn = W_WIDTH(wp);
2068 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2069 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2070 ' ', ' ', hl_attr(HLF_FC));
2071 n = nn;
2073 #endif
2074 #ifdef FEAT_SIGNS
2075 if (draw_signcolumn(wp))
2077 int nn = n + 2;
2079 /* draw the sign column after the fold column */
2080 if (nn > W_WIDTH(wp))
2081 nn = W_WIDTH(wp);
2082 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2083 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2084 ' ', ' ', hl_attr(HLF_SC));
2085 n = nn;
2087 #endif
2088 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2089 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
2090 c1, c2, hl_attr(hl));
2092 set_empty_rows(wp, row);
2095 #ifdef FEAT_FOLDING
2097 * Display one folded line.
2099 static void
2100 fold_line(wp, fold_count, foldinfo, lnum, row)
2101 win_T *wp;
2102 long fold_count;
2103 foldinfo_T *foldinfo;
2104 linenr_T lnum;
2105 int row;
2107 char_u buf[51];
2108 pos_T *top, *bot;
2109 linenr_T lnume = lnum + fold_count - 1;
2110 int len;
2111 char_u *text;
2112 int fdc;
2113 int col;
2114 int txtcol;
2115 int off = (int)(current_ScreenLine - ScreenLines);
2116 int ri;
2118 /* Build the fold line:
2119 * 1. Add the cmdwin_type for the command-line window
2120 * 2. Add the 'foldcolumn'
2121 * 3. Add the 'number' column
2122 * 4. Compose the text
2123 * 5. Add the text
2124 * 6. set highlighting for the Visual area an other text
2126 col = 0;
2129 * 1. Add the cmdwin_type for the command-line window
2130 * Ignores 'rightleft', this window is never right-left.
2132 #ifdef FEAT_CMDWIN
2133 if (cmdwin_type != 0 && wp == curwin)
2135 ScreenLines[off] = cmdwin_type;
2136 ScreenAttrs[off] = hl_attr(HLF_AT);
2137 #ifdef FEAT_MBYTE
2138 if (enc_utf8)
2139 ScreenLinesUC[off] = 0;
2140 #endif
2141 ++col;
2143 #endif
2146 * 2. Add the 'foldcolumn'
2148 fdc = wp->w_p_fdc;
2149 if (fdc > W_WIDTH(wp) - col)
2150 fdc = W_WIDTH(wp) - col;
2151 if (fdc > 0)
2153 fill_foldcolumn(buf, wp, TRUE, lnum);
2154 #ifdef FEAT_RIGHTLEFT
2155 if (wp->w_p_rl)
2157 int i;
2159 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
2160 hl_attr(HLF_FC));
2161 /* reverse the fold column */
2162 for (i = 0; i < fdc; ++i)
2163 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2165 else
2166 #endif
2167 copy_text_attr(off + col, buf, fdc, hl_attr(HLF_FC));
2168 col += fdc;
2171 #ifdef FEAT_RIGHTLEFT
2172 # define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2173 for (ri = 0; ri < l; ++ri) \
2174 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2175 else \
2176 for (ri = 0; ri < l; ++ri) \
2177 ScreenAttrs[off + (p) + ri] = v
2178 #else
2179 # define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2180 ScreenAttrs[off + (p) + ri] = v
2181 #endif
2183 /* Set all attributes of the 'number' column and the text */
2184 RL_MEMSET(col, hl_attr(HLF_FL), W_WIDTH(wp) - col);
2186 #ifdef FEAT_SIGNS
2187 /* If signs are being displayed, add two spaces. */
2188 if (draw_signcolumn(wp))
2190 len = W_WIDTH(wp) - col;
2191 if (len > 0)
2193 if (len > 2)
2194 len = 2;
2195 # ifdef FEAT_RIGHTLEFT
2196 if (wp->w_p_rl)
2197 /* the line number isn't reversed */
2198 copy_text_attr(off + W_WIDTH(wp) - len - col,
2199 (char_u *)" ", len, hl_attr(HLF_FL));
2200 else
2201 # endif
2202 copy_text_attr(off + col, (char_u *)" ", len, hl_attr(HLF_FL));
2203 col += len;
2206 #endif
2209 * 3. Add the 'number' column
2211 if (wp->w_p_nu)
2213 len = W_WIDTH(wp) - col;
2214 if (len > 0)
2216 int w = number_width(wp);
2218 if (len > w + 1)
2219 len = w + 1;
2220 sprintf((char *)buf, "%*ld ", w, (long)lnum);
2221 #ifdef FEAT_RIGHTLEFT
2222 if (wp->w_p_rl)
2223 /* the line number isn't reversed */
2224 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
2225 hl_attr(HLF_FL));
2226 else
2227 #endif
2228 copy_text_attr(off + col, buf, len, hl_attr(HLF_FL));
2229 col += len;
2234 * 4. Compose the folded-line string with 'foldtext', if set.
2236 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
2238 txtcol = col; /* remember where text starts */
2241 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2242 * Right-left text is put in columns 0 - number-col, normal text is put
2243 * in columns number-col - window-width.
2245 #ifdef FEAT_MBYTE
2246 if (has_mbyte)
2248 int cells;
2249 int u8c, u8cc[MAX_MCO];
2250 int i;
2251 int idx;
2252 int c_len;
2253 char_u *p;
2254 # ifdef FEAT_ARABIC
2255 int prev_c = 0; /* previous Arabic character */
2256 int prev_c1 = 0; /* first composing char for prev_c */
2257 # endif
2259 # ifdef FEAT_RIGHTLEFT
2260 if (wp->w_p_rl)
2261 idx = off;
2262 else
2263 # endif
2264 idx = off + col;
2266 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2267 for (p = text; *p != NUL; )
2269 cells = (*mb_ptr2cells)(p);
2270 c_len = (*mb_ptr2len)(p);
2271 if (col + cells > W_WIDTH(wp)
2272 # ifdef FEAT_RIGHTLEFT
2273 - (wp->w_p_rl ? col : 0)
2274 # endif
2276 break;
2277 ScreenLines[idx] = *p;
2278 if (enc_utf8)
2280 u8c = utfc_ptr2char(p, u8cc);
2281 if (*p < 0x80 && u8cc[0] == 0)
2283 ScreenLinesUC[idx] = 0;
2284 #ifdef FEAT_ARABIC
2285 prev_c = u8c;
2286 #endif
2288 else
2290 #ifdef FEAT_ARABIC
2291 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2293 /* Do Arabic shaping. */
2294 int pc, pc1, nc;
2295 int pcc[MAX_MCO];
2296 int firstbyte = *p;
2298 /* The idea of what is the previous and next
2299 * character depends on 'rightleft'. */
2300 if (wp->w_p_rl)
2302 pc = prev_c;
2303 pc1 = prev_c1;
2304 nc = utf_ptr2char(p + c_len);
2305 prev_c1 = u8cc[0];
2307 else
2309 pc = utfc_ptr2char(p + c_len, pcc);
2310 nc = prev_c;
2311 pc1 = pcc[0];
2313 prev_c = u8c;
2315 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2316 pc, pc1, nc);
2317 ScreenLines[idx] = firstbyte;
2319 else
2320 prev_c = u8c;
2321 #endif
2322 /* Non-BMP character: display as ? or fullwidth ?. */
2323 #ifdef UNICODE16
2324 if (u8c >= 0x10000)
2325 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2326 else
2327 #endif
2328 ScreenLinesUC[idx] = u8c;
2329 for (i = 0; i < Screen_mco; ++i)
2331 ScreenLinesC[i][idx] = u8cc[i];
2332 if (u8cc[i] == 0)
2333 break;
2336 if (cells > 1)
2337 ScreenLines[idx + 1] = 0;
2339 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2340 /* double-byte single width character */
2341 ScreenLines2[idx] = p[1];
2342 else if (cells > 1)
2343 /* double-width character */
2344 ScreenLines[idx + 1] = p[1];
2345 col += cells;
2346 idx += cells;
2347 p += c_len;
2350 else
2351 #endif
2353 len = (int)STRLEN(text);
2354 if (len > W_WIDTH(wp) - col)
2355 len = W_WIDTH(wp) - col;
2356 if (len > 0)
2358 #ifdef FEAT_RIGHTLEFT
2359 if (wp->w_p_rl)
2360 STRNCPY(current_ScreenLine, text, len);
2361 else
2362 #endif
2363 STRNCPY(current_ScreenLine + col, text, len);
2364 col += len;
2368 /* Fill the rest of the line with the fold filler */
2369 #ifdef FEAT_RIGHTLEFT
2370 if (wp->w_p_rl)
2371 col -= txtcol;
2372 #endif
2373 while (col < W_WIDTH(wp)
2374 #ifdef FEAT_RIGHTLEFT
2375 - (wp->w_p_rl ? txtcol : 0)
2376 #endif
2379 #ifdef FEAT_MBYTE
2380 if (enc_utf8)
2382 if (fill_fold >= 0x80)
2384 ScreenLinesUC[off + col] = fill_fold;
2385 ScreenLinesC[0][off + col] = 0;
2387 else
2388 ScreenLinesUC[off + col] = 0;
2390 #endif
2391 ScreenLines[off + col++] = fill_fold;
2394 if (text != buf)
2395 vim_free(text);
2398 * 6. set highlighting for the Visual area an other text.
2399 * If all folded lines are in the Visual area, highlight the line.
2401 #ifdef FEAT_VISUAL
2402 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2404 if (ltoreq(curwin->w_cursor, VIsual))
2406 /* Visual is after curwin->w_cursor */
2407 top = &curwin->w_cursor;
2408 bot = &VIsual;
2410 else
2412 /* Visual is before curwin->w_cursor */
2413 top = &VIsual;
2414 bot = &curwin->w_cursor;
2416 if (lnum >= top->lnum
2417 && lnume <= bot->lnum
2418 && (VIsual_mode != 'v'
2419 || ((lnum > top->lnum
2420 || (lnum == top->lnum
2421 && top->col == 0))
2422 && (lnume < bot->lnum
2423 || (lnume == bot->lnum
2424 && (bot->col - (*p_sel == 'e'))
2425 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
2427 if (VIsual_mode == Ctrl_V)
2429 /* Visual block mode: highlight the chars part of the block */
2430 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2432 if (wp->w_old_cursor_lcol + txtcol < (colnr_T)W_WIDTH(wp))
2433 len = wp->w_old_cursor_lcol;
2434 else
2435 len = W_WIDTH(wp) - txtcol;
2436 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, hl_attr(HLF_V),
2437 len - (int)wp->w_old_cursor_fcol);
2440 else
2442 /* Set all attributes of the text */
2443 RL_MEMSET(txtcol, hl_attr(HLF_V), W_WIDTH(wp) - txtcol);
2447 #endif
2449 #ifdef FEAT_SYN_HL
2450 /* Show 'cursorcolumn' in the fold line. */
2451 if (wp->w_p_cuc)
2453 txtcol += wp->w_virtcol;
2454 if (wp->w_p_wrap)
2455 txtcol -= wp->w_skipcol;
2456 else
2457 txtcol -= wp->w_leftcol;
2458 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2459 ScreenAttrs[off + txtcol] = hl_combine_attr(
2460 ScreenAttrs[off + txtcol], hl_attr(HLF_CUC));
2462 #endif
2464 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2465 (int)W_WIDTH(wp), FALSE);
2468 * Update w_cline_height and w_cline_folded if the cursor line was
2469 * updated (saves a call to plines() later).
2471 if (wp == curwin
2472 && lnum <= curwin->w_cursor.lnum
2473 && lnume >= curwin->w_cursor.lnum)
2475 curwin->w_cline_row = row;
2476 curwin->w_cline_height = 1;
2477 curwin->w_cline_folded = TRUE;
2478 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2483 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2485 static void
2486 copy_text_attr(off, buf, len, attr)
2487 int off;
2488 char_u *buf;
2489 int len;
2490 int attr;
2492 int i;
2494 mch_memmove(ScreenLines + off, buf, (size_t)len);
2495 # ifdef FEAT_MBYTE
2496 if (enc_utf8)
2497 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2498 # endif
2499 for (i = 0; i < len; ++i)
2500 ScreenAttrs[off + i] = attr;
2504 * Fill the foldcolumn at "p" for window "wp".
2505 * Only to be called when 'foldcolumn' > 0.
2507 static void
2508 fill_foldcolumn(p, wp, closed, lnum)
2509 char_u *p;
2510 win_T *wp;
2511 int closed; /* TRUE of FALSE */
2512 linenr_T lnum; /* current line number */
2514 int i = 0;
2515 int level;
2516 int first_level;
2517 int empty;
2519 /* Init to all spaces. */
2520 copy_spaces(p, (size_t)wp->w_p_fdc);
2522 level = win_foldinfo.fi_level;
2523 if (level > 0)
2525 /* If there is only one column put more info in it. */
2526 empty = (wp->w_p_fdc == 1) ? 0 : 1;
2528 /* If the column is too narrow, we start at the lowest level that
2529 * fits and use numbers to indicated the depth. */
2530 first_level = level - wp->w_p_fdc - closed + 1 + empty;
2531 if (first_level < 1)
2532 first_level = 1;
2534 for (i = 0; i + empty < wp->w_p_fdc; ++i)
2536 if (win_foldinfo.fi_lnum == lnum
2537 && first_level + i >= win_foldinfo.fi_low_level)
2538 p[i] = '-';
2539 else if (first_level == 1)
2540 p[i] = '|';
2541 else if (first_level + i <= 9)
2542 p[i] = '0' + first_level + i;
2543 else
2544 p[i] = '>';
2545 if (first_level + i == level)
2546 break;
2549 if (closed)
2550 p[i >= wp->w_p_fdc ? i - 1 : i] = '+';
2552 #endif /* FEAT_FOLDING */
2555 * Display line "lnum" of window 'wp' on the screen.
2556 * Start at row "startrow", stop when "endrow" is reached.
2557 * wp->w_virtcol needs to be valid.
2559 * Return the number of last row the line occupies.
2561 static int
2562 win_line(wp, lnum, startrow, endrow, nochange)
2563 win_T *wp;
2564 linenr_T lnum;
2565 int startrow;
2566 int endrow;
2567 int nochange UNUSED; /* not updating for changed text */
2569 int col; /* visual column on screen */
2570 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2571 int c = 0; /* init for GCC */
2572 long vcol = 0; /* virtual column (for tabs) */
2573 long vcol_prev = -1; /* "vcol" of previous character */
2574 char_u *line; /* current line */
2575 char_u *ptr; /* current position in "line" */
2576 int row; /* row in the window, excl w_winrow */
2577 int screen_row; /* row on the screen, incl w_winrow */
2579 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2580 int n_extra = 0; /* number of extra chars */
2581 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
2582 int c_extra = NUL; /* extra chars, all the same */
2583 int extra_attr = 0; /* attributes when n_extra != 0 */
2584 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2585 displaying lcs_eol at end-of-line */
2586 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2587 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2589 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2590 int saved_n_extra = 0;
2591 char_u *saved_p_extra = NULL;
2592 int saved_c_extra = 0;
2593 int saved_char_attr = 0;
2595 int n_attr = 0; /* chars with special attr */
2596 int saved_attr2 = 0; /* char_attr saved for n_attr */
2597 int n_attr3 = 0; /* chars with overruling special attr */
2598 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2600 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2602 int fromcol, tocol; /* start/end of inverting */
2603 int fromcol_prev = -2; /* start of inverting after cursor */
2604 int noinvcur = FALSE; /* don't invert the cursor */
2605 #ifdef FEAT_VISUAL
2606 pos_T *top, *bot;
2607 int lnum_in_visual_area = FALSE;
2608 #endif
2609 pos_T pos;
2610 long v;
2612 int char_attr = 0; /* attributes for next character */
2613 int attr_pri = FALSE; /* char_attr has priority */
2614 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2615 in this line */
2616 int attr = 0; /* attributes for area highlighting */
2617 int area_attr = 0; /* attributes desired by highlighting */
2618 int search_attr = 0; /* attributes desired by 'hlsearch' */
2619 #ifdef FEAT_SYN_HL
2620 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
2621 int syntax_attr = 0; /* attributes desired by syntax */
2622 int has_syntax = FALSE; /* this buffer has syntax highl. */
2623 int save_did_emsg;
2624 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
2625 #endif
2626 #ifdef FEAT_SPELL
2627 int has_spell = FALSE; /* this buffer has spell checking */
2628 # define SPWORDLEN 150
2629 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
2630 int nextlinecol = 0; /* column where nextline[] starts */
2631 int nextline_idx = 0; /* index in nextline[] where next line
2632 starts */
2633 int spell_attr = 0; /* attributes desired by spelling */
2634 int word_end = 0; /* last byte with same spell_attr */
2635 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2636 static int checked_col = 0; /* column in "checked_lnum" up to which
2637 * there are no spell errors */
2638 static int cap_col = -1; /* column to check for Cap word */
2639 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
2640 int cur_checked_col = 0; /* checked column for current line */
2641 #endif
2642 int extra_check; /* has syntax or linebreak */
2643 #ifdef FEAT_MBYTE
2644 int multi_attr = 0; /* attributes desired by multibyte */
2645 int mb_l = 1; /* multi-byte byte length */
2646 int mb_c = 0; /* decoded multi-byte character */
2647 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
2648 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
2649 #endif
2650 #ifdef FEAT_DIFF
2651 int filler_lines; /* nr of filler lines to be drawn */
2652 int filler_todo; /* nr of filler lines still to do + 1 */
2653 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
2654 int change_start = MAXCOL; /* first col of changed area */
2655 int change_end = -1; /* last col of changed area */
2656 #endif
2657 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
2658 #ifdef FEAT_LINEBREAK
2659 int need_showbreak = FALSE;
2660 #endif
2661 #if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
2662 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
2663 # define LINE_ATTR
2664 int line_attr = 0; /* attribute for the whole line */
2665 #endif
2666 #ifdef FEAT_SEARCH_EXTRA
2667 matchitem_T *cur; /* points to the match list */
2668 match_T *shl; /* points to search_hl or a match */
2669 int shl_flag; /* flag to indicate whether search_hl
2670 has been processed or not */
2671 int prevcol_hl_flag; /* flag to indicate whether prevcol
2672 equals startcol of search_hl or one
2673 of the matches */
2674 #endif
2675 #ifdef FEAT_ARABIC
2676 int prev_c = 0; /* previous Arabic character */
2677 int prev_c1 = 0; /* first composing char for prev_c */
2678 #endif
2679 #if defined(LINE_ATTR)
2680 int did_line_attr = 0;
2681 #endif
2683 /* draw_state: items that are drawn in sequence: */
2684 #define WL_START 0 /* nothing done yet */
2685 #ifdef FEAT_CMDWIN
2686 # define WL_CMDLINE WL_START + 1 /* cmdline window column */
2687 #else
2688 # define WL_CMDLINE WL_START
2689 #endif
2690 #ifdef FEAT_FOLDING
2691 # define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
2692 #else
2693 # define WL_FOLD WL_CMDLINE
2694 #endif
2695 #ifdef FEAT_SIGNS
2696 # define WL_SIGN WL_FOLD + 1 /* column for signs */
2697 #else
2698 # define WL_SIGN WL_FOLD /* column for signs */
2699 #endif
2700 #define WL_NR WL_SIGN + 1 /* line number */
2701 #if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
2702 # define WL_SBR WL_NR + 1 /* 'showbreak' or 'diff' */
2703 #else
2704 # define WL_SBR WL_NR
2705 #endif
2706 #define WL_LINE WL_SBR + 1 /* text in the line */
2707 int draw_state = WL_START; /* what to draw next */
2708 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2709 int feedback_col = 0;
2710 int feedback_old_attr = -1;
2711 #endif
2714 if (startrow > endrow) /* past the end already! */
2715 return startrow;
2717 row = startrow;
2718 screen_row = row + W_WINROW(wp);
2721 * To speed up the loop below, set extra_check when there is linebreak,
2722 * trailing white space and/or syntax processing to be done.
2724 #ifdef FEAT_LINEBREAK
2725 extra_check = wp->w_p_lbr;
2726 #else
2727 extra_check = 0;
2728 #endif
2731 * Highlight the line if this buffer is in the code_check.c watchlist,
2732 * and there is an associated error in the corresponding error/warning
2733 * list.
2735 if (cc_is_buf_watched(curbuf)) {
2736 switch (cc_get_ew_type(curbuf, lnum)) {
2737 case /*CC_WARNING*/1:
2738 line_attr = hl_attr(HLF_WM);
2739 break;
2740 case /*CC_ERROR*/2:
2741 line_attr = hl_attr(HLF_E);
2742 break;
2743 default:
2744 line_attr = 0;
2745 break;
2749 #ifdef FEAT_SYN_HL
2750 if (syntax_present(wp->w_buffer) && !wp->w_buffer->b_syn_error)
2752 /* Prepare for syntax highlighting in this line. When there is an
2753 * error, stop syntax highlighting. */
2754 save_did_emsg = did_emsg;
2755 did_emsg = FALSE;
2756 syntax_start(wp, lnum);
2757 if (did_emsg)
2758 wp->w_buffer->b_syn_error = TRUE;
2759 else
2761 did_emsg = save_did_emsg;
2762 has_syntax = TRUE;
2763 extra_check = TRUE;
2766 #endif
2768 #ifdef FEAT_SPELL
2769 if (wp->w_p_spell
2770 && *wp->w_buffer->b_p_spl != NUL
2771 && wp->w_buffer->b_langp.ga_len > 0
2772 && *(char **)(wp->w_buffer->b_langp.ga_data) != NULL)
2774 /* Prepare for spell checking. */
2775 has_spell = TRUE;
2776 extra_check = TRUE;
2778 /* Get the start of the next line, so that words that wrap to the next
2779 * line are found too: "et<line-break>al.".
2780 * Trick: skip a few chars for C/shell/Vim comments */
2781 nextline[SPWORDLEN] = NUL;
2782 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2784 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
2785 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
2788 /* When a word wrapped from the previous line the start of the current
2789 * line is valid. */
2790 if (lnum == checked_lnum)
2791 cur_checked_col = checked_col;
2792 checked_lnum = 0;
2794 /* When there was a sentence end in the previous line may require a
2795 * word starting with capital in this line. In line 1 always check
2796 * the first word. */
2797 if (lnum != capcol_lnum)
2798 cap_col = -1;
2799 if (lnum == 1)
2800 cap_col = 0;
2801 capcol_lnum = 0;
2803 #endif
2806 * handle visual active in this window
2808 fromcol = -10;
2809 tocol = MAXCOL;
2810 #ifdef FEAT_VISUAL
2811 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2813 /* Visual is after curwin->w_cursor */
2814 if (ltoreq(curwin->w_cursor, VIsual))
2816 top = &curwin->w_cursor;
2817 bot = &VIsual;
2819 else /* Visual is before curwin->w_cursor */
2821 top = &VIsual;
2822 bot = &curwin->w_cursor;
2824 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
2825 if (VIsual_mode == Ctrl_V) /* block mode */
2827 if (lnum_in_visual_area)
2829 fromcol = wp->w_old_cursor_fcol;
2830 tocol = wp->w_old_cursor_lcol;
2833 else /* non-block mode */
2835 if (lnum > top->lnum && lnum <= bot->lnum)
2836 fromcol = 0;
2837 else if (lnum == top->lnum)
2839 if (VIsual_mode == 'V') /* linewise */
2840 fromcol = 0;
2841 else
2843 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
2844 if (gchar_pos(top) == NUL)
2845 tocol = fromcol + 1;
2848 if (VIsual_mode != 'V' && lnum == bot->lnum)
2850 if (*p_sel == 'e' && bot->col == 0
2851 #ifdef FEAT_VIRTUALEDIT
2852 && bot->coladd == 0
2853 #endif
2856 fromcol = -10;
2857 tocol = MAXCOL;
2859 else if (bot->col == MAXCOL)
2860 tocol = MAXCOL;
2861 else
2863 pos = *bot;
2864 if (*p_sel == 'e')
2865 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
2866 else
2868 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
2869 ++tocol;
2875 #ifndef MSDOS
2876 /* Check if the character under the cursor should not be inverted */
2877 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
2878 # ifdef FEAT_GUI
2879 && !gui.in_use
2880 # endif
2882 noinvcur = TRUE;
2883 #endif
2885 /* if inverting in this line set area_highlighting */
2886 if (fromcol >= 0)
2888 area_highlighting = TRUE;
2889 attr = hl_attr(HLF_V);
2890 #if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
2891 if (clip_star.available && !clip_star.owned && clip_isautosel())
2892 attr = hl_attr(HLF_VNC);
2893 #endif
2898 * handle 'incsearch' and ":s///c" highlighting
2900 else
2901 #endif /* FEAT_VISUAL */
2902 if (highlight_match
2903 && wp == curwin
2904 && lnum >= curwin->w_cursor.lnum
2905 && lnum <= curwin->w_cursor.lnum + search_match_lines)
2907 if (lnum == curwin->w_cursor.lnum)
2908 getvcol(curwin, &(curwin->w_cursor),
2909 (colnr_T *)&fromcol, NULL, NULL);
2910 else
2911 fromcol = 0;
2912 if (lnum == curwin->w_cursor.lnum + search_match_lines)
2914 pos.lnum = lnum;
2915 pos.col = search_match_endcol;
2916 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
2918 else
2919 tocol = MAXCOL;
2920 /* do at least one character; happens when past end of line */
2921 if (fromcol == tocol)
2922 tocol = fromcol + 1;
2923 area_highlighting = TRUE;
2924 attr = hl_attr(HLF_I);
2927 #ifdef FEAT_DIFF
2928 filler_lines = diff_check(wp, lnum);
2929 if (filler_lines < 0)
2931 if (filler_lines == -1)
2933 if (diff_find_change(wp, lnum, &change_start, &change_end))
2934 diff_hlf = HLF_ADD; /* added line */
2935 else if (change_start == 0)
2936 diff_hlf = HLF_TXD; /* changed text */
2937 else
2938 diff_hlf = HLF_CHD; /* changed line */
2940 else
2941 diff_hlf = HLF_ADD; /* added line */
2942 filler_lines = 0;
2943 area_highlighting = TRUE;
2945 if (lnum == wp->w_topline)
2946 filler_lines = wp->w_topfill;
2947 filler_todo = filler_lines;
2948 #endif
2950 #ifdef LINE_ATTR
2951 # ifdef FEAT_SIGNS
2952 /* If this line has a sign with line highlighting set line_attr. */
2953 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
2954 if (v != 0)
2955 line_attr = sign_get_attr((int)v, TRUE);
2956 # endif
2957 # if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
2958 /* Highlight the current line in the quickfix window. */
2959 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
2960 line_attr = hl_attr(HLF_L);
2961 # endif
2962 if (line_attr != 0)
2963 area_highlighting = TRUE;
2964 #endif
2966 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
2967 ptr = line;
2969 #ifdef FEAT_SPELL
2970 if (has_spell)
2972 /* For checking first word with a capital skip white space. */
2973 if (cap_col == 0)
2974 cap_col = (int)(skipwhite(line) - line);
2976 /* To be able to spell-check over line boundaries copy the end of the
2977 * current line into nextline[]. Above the start of the next line was
2978 * copied to nextline[SPWORDLEN]. */
2979 if (nextline[SPWORDLEN] == NUL)
2981 /* No next line or it is empty. */
2982 nextlinecol = MAXCOL;
2983 nextline_idx = 0;
2985 else
2987 v = (long)STRLEN(line);
2988 if (v < SPWORDLEN)
2990 /* Short line, use it completely and append the start of the
2991 * next line. */
2992 nextlinecol = 0;
2993 mch_memmove(nextline, line, (size_t)v);
2994 STRMOVE(nextline + v, nextline + SPWORDLEN);
2995 nextline_idx = v + 1;
2997 else
2999 /* Long line, use only the last SPWORDLEN bytes. */
3000 nextlinecol = v - SPWORDLEN;
3001 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3002 nextline_idx = SPWORDLEN + 1;
3006 #endif
3008 /* find start of trailing whitespace */
3009 if (wp->w_p_list && lcs_trail)
3011 trailcol = (colnr_T)STRLEN(ptr);
3012 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
3013 --trailcol;
3014 trailcol += (colnr_T) (ptr - line);
3015 extra_check = TRUE;
3019 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3020 * first character to be displayed.
3022 if (wp->w_p_wrap)
3023 v = wp->w_skipcol;
3024 else
3025 v = wp->w_leftcol;
3026 if (v > 0)
3028 #ifdef FEAT_MBYTE
3029 char_u *prev_ptr = ptr;
3030 #endif
3031 while (vcol < v && *ptr != NUL)
3033 c = win_lbr_chartabsize(wp, ptr, (colnr_T)vcol, NULL);
3034 vcol += c;
3035 #ifdef FEAT_MBYTE
3036 prev_ptr = ptr;
3037 #endif
3038 mb_ptr_adv(ptr);
3041 #if defined(FEAT_SYN_HL) || defined(FEAT_VIRTUALEDIT) || defined(FEAT_VISUAL)
3042 /* When:
3043 * - 'cuc' is set, or
3044 * - 'virtualedit' is set, or
3045 * - the visual mode is active,
3046 * the end of the line may be before the start of the displayed part.
3048 if (vcol < v && (
3049 # ifdef FEAT_SYN_HL
3050 wp->w_p_cuc
3051 # if defined(FEAT_VIRTUALEDIT) || defined(FEAT_VISUAL)
3053 # endif
3054 # endif
3055 # ifdef FEAT_VIRTUALEDIT
3056 virtual_active()
3057 # ifdef FEAT_VISUAL
3059 # endif
3060 # endif
3061 # ifdef FEAT_VISUAL
3062 (VIsual_active && wp->w_buffer == curwin->w_buffer)
3063 # endif
3066 vcol = v;
3068 #endif
3070 /* Handle a character that's not completely on the screen: Put ptr at
3071 * that character but skip the first few screen characters. */
3072 if (vcol > v)
3074 vcol -= c;
3075 #ifdef FEAT_MBYTE
3076 ptr = prev_ptr;
3077 #else
3078 --ptr;
3079 #endif
3080 n_skip = v - vcol;
3084 * Adjust for when the inverted text is before the screen,
3085 * and when the start of the inverted text is before the screen.
3087 if (tocol <= vcol)
3088 fromcol = 0;
3089 else if (fromcol >= 0 && fromcol < vcol)
3090 fromcol = vcol;
3092 #ifdef FEAT_LINEBREAK
3093 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3094 if (wp->w_p_wrap)
3095 need_showbreak = TRUE;
3096 #endif
3097 #ifdef FEAT_SPELL
3098 /* When spell checking a word we need to figure out the start of the
3099 * word and if it's badly spelled or not. */
3100 if (has_spell)
3102 int len;
3103 colnr_T linecol = (colnr_T)(ptr - line);
3104 hlf_T spell_hlf = HLF_COUNT;
3106 pos = wp->w_cursor;
3107 wp->w_cursor.lnum = lnum;
3108 wp->w_cursor.col = linecol;
3109 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
3111 /* spell_move_to() may call ml_get() and make "line" invalid */
3112 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3113 ptr = line + linecol;
3115 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
3117 /* no bad word found at line start, don't check until end of a
3118 * word */
3119 spell_hlf = HLF_COUNT;
3120 word_end = (int)(spell_to_word_end(ptr, wp->w_buffer)
3121 - line + 1);
3123 else
3125 /* bad word found, use attributes until end of word */
3126 word_end = wp->w_cursor.col + len + 1;
3128 /* Turn index into actual attributes. */
3129 if (spell_hlf != HLF_COUNT)
3130 spell_attr = highlight_attr[spell_hlf];
3132 wp->w_cursor = pos;
3134 # ifdef FEAT_SYN_HL
3135 /* Need to restart syntax highlighting for this line. */
3136 if (has_syntax)
3137 syntax_start(wp, lnum);
3138 # endif
3140 #endif
3144 * Correct highlighting for cursor that can't be disabled.
3145 * Avoids having to check this for each character.
3147 if (fromcol >= 0)
3149 if (noinvcur)
3151 if ((colnr_T)fromcol == wp->w_virtcol)
3153 /* highlighting starts at cursor, let it start just after the
3154 * cursor */
3155 fromcol_prev = fromcol;
3156 fromcol = -1;
3158 else if ((colnr_T)fromcol < wp->w_virtcol)
3159 /* restart highlighting after the cursor */
3160 fromcol_prev = wp->w_virtcol;
3162 if (fromcol >= tocol)
3163 fromcol = -1;
3166 #ifdef FEAT_SEARCH_EXTRA
3168 * Handle highlighting the last used search pattern and matches.
3169 * Do this for both search_hl and the match list.
3171 cur = wp->w_match_head;
3172 shl_flag = FALSE;
3173 while (cur != NULL || shl_flag == FALSE)
3175 if (shl_flag == FALSE)
3177 shl = &search_hl;
3178 shl_flag = TRUE;
3180 else
3181 shl = &cur->hl;
3182 shl->startcol = MAXCOL;
3183 shl->endcol = MAXCOL;
3184 shl->attr_cur = 0;
3185 if (shl->rm.regprog != NULL)
3187 v = (long)(ptr - line);
3188 next_search_hl(wp, shl, lnum, (colnr_T)v);
3190 /* Need to get the line again, a multi-line regexp may have made it
3191 * invalid. */
3192 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3193 ptr = line + v;
3195 if (shl->lnum != 0 && shl->lnum <= lnum)
3197 if (shl->lnum == lnum)
3198 shl->startcol = shl->rm.startpos[0].col;
3199 else
3200 shl->startcol = 0;
3201 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3202 - shl->rm.startpos[0].lnum)
3203 shl->endcol = shl->rm.endpos[0].col;
3204 else
3205 shl->endcol = MAXCOL;
3206 /* Highlight one character for an empty match. */
3207 if (shl->startcol == shl->endcol)
3209 #ifdef FEAT_MBYTE
3210 if (has_mbyte && line[shl->endcol] != NUL)
3211 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3212 else
3213 #endif
3214 ++shl->endcol;
3216 if ((long)shl->startcol < v) /* match at leftcol */
3218 shl->attr_cur = shl->attr;
3219 search_attr = shl->attr;
3221 area_highlighting = TRUE;
3224 if (shl != &search_hl && cur != NULL)
3225 cur = cur->next;
3227 #endif
3229 #ifdef FEAT_SYN_HL
3230 /* Cursor line highlighting for 'cursorline'. Not when Visual mode is
3231 * active, because it's not clear what is selected then. */
3232 if (wp->w_p_cul && lnum == wp->w_cursor.lnum && !VIsual_active)
3234 line_attr = hl_attr(HLF_CUL);
3235 area_highlighting = TRUE;
3237 #endif
3239 off = (unsigned)(current_ScreenLine - ScreenLines);
3240 col = 0;
3241 #ifdef FEAT_RIGHTLEFT
3242 if (wp->w_p_rl)
3244 /* Rightleft window: process the text in the normal direction, but put
3245 * it in current_ScreenLine[] from right to left. Start at the
3246 * rightmost column of the window. */
3247 col = W_WIDTH(wp) - 1;
3248 off += col;
3250 #endif
3253 * Repeat for the whole displayed line.
3255 for (;;)
3257 /* Skip this quickly when working on the text. */
3258 if (draw_state != WL_LINE)
3260 #ifdef FEAT_CMDWIN
3261 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3263 draw_state = WL_CMDLINE;
3264 if (cmdwin_type != 0 && wp == curwin)
3266 /* Draw the cmdline character. */
3267 n_extra = 1;
3268 c_extra = cmdwin_type;
3269 char_attr = hl_attr(HLF_AT);
3272 #endif
3274 #ifdef FEAT_FOLDING
3275 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3277 draw_state = WL_FOLD;
3278 if (wp->w_p_fdc > 0)
3280 /* Draw the 'foldcolumn'. */
3281 fill_foldcolumn(extra, wp, FALSE, lnum);
3282 n_extra = wp->w_p_fdc;
3283 p_extra = extra;
3284 p_extra[n_extra] = NUL;
3285 c_extra = NUL;
3286 char_attr = hl_attr(HLF_FC);
3289 #endif
3291 #ifdef FEAT_SIGNS
3292 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3294 draw_state = WL_SIGN;
3295 /* Show the sign column when there are any signs in this
3296 * buffer or when using Netbeans. */
3297 if (draw_signcolumn(wp)
3298 # ifdef FEAT_DIFF
3299 && filler_todo <= 0
3300 # endif
3303 int_u text_sign;
3304 # ifdef FEAT_SIGN_ICONS
3305 int_u icon_sign;
3306 # endif
3308 /* Draw two cells with the sign value or blank. */
3309 c_extra = ' ';
3310 char_attr = hl_attr(HLF_SC);
3311 n_extra = 2;
3313 if (row == startrow)
3315 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3316 SIGN_TEXT);
3317 # ifdef FEAT_SIGN_ICONS
3318 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3319 SIGN_ICON);
3320 if (gui.in_use && icon_sign != 0)
3322 /* Use the image in this position. */
3323 c_extra = SIGN_BYTE;
3324 # ifdef FEAT_NETBEANS_INTG
3325 if (buf_signcount(wp->w_buffer, lnum) > 1)
3326 c_extra = MULTISIGN_BYTE;
3327 # endif
3328 char_attr = icon_sign;
3330 else
3331 # endif
3332 if (text_sign != 0)
3334 p_extra = sign_get_text(text_sign);
3335 if (p_extra != NULL)
3337 c_extra = NUL;
3338 n_extra = (int)STRLEN(p_extra);
3340 char_attr = sign_get_attr(text_sign, FALSE);
3345 #endif
3347 if (draw_state == WL_NR - 1 && n_extra == 0)
3349 draw_state = WL_NR;
3350 /* Display the line number. After the first fill with blanks
3351 * when the 'n' flag isn't in 'cpo' */
3352 if (wp->w_p_nu
3353 && (row == startrow
3354 #ifdef FEAT_DIFF
3355 + filler_lines
3356 #endif
3357 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3359 /* Draw the line number (empty space after wrapping). */
3360 if (row == startrow
3361 #ifdef FEAT_DIFF
3362 + filler_lines
3363 #endif
3366 sprintf((char *)extra, "%*ld ",
3367 number_width(wp), (long)lnum);
3368 if (wp->w_skipcol > 0)
3369 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3370 *p_extra = '-';
3371 #ifdef FEAT_RIGHTLEFT
3372 if (wp->w_p_rl) /* reverse line numbers */
3373 rl_mirror(extra);
3374 #endif
3375 p_extra = extra;
3376 c_extra = NUL;
3378 else
3379 c_extra = ' ';
3380 n_extra = number_width(wp) + 1;
3381 char_attr = hl_attr(HLF_N);
3382 #ifdef FEAT_SYN_HL
3383 /* When 'cursorline' is set highlight the line number of
3384 * the current line differently. */
3385 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3386 char_attr = hl_combine_attr(hl_attr(HLF_CUL), char_attr);
3387 #endif
3391 #if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3392 if (draw_state == WL_SBR - 1 && n_extra == 0)
3394 draw_state = WL_SBR;
3395 # ifdef FEAT_DIFF
3396 if (filler_todo > 0)
3398 /* Draw "deleted" diff line(s). */
3399 if (char2cells(fill_diff) > 1)
3400 c_extra = '-';
3401 else
3402 c_extra = fill_diff;
3403 # ifdef FEAT_RIGHTLEFT
3404 if (wp->w_p_rl)
3405 n_extra = col + 1;
3406 else
3407 # endif
3408 n_extra = W_WIDTH(wp) - col;
3409 char_attr = hl_attr(HLF_DED);
3411 # endif
3412 # ifdef FEAT_LINEBREAK
3413 if (*p_sbr != NUL && need_showbreak)
3415 /* Draw 'showbreak' at the start of each broken line. */
3416 p_extra = p_sbr;
3417 c_extra = NUL;
3418 n_extra = (int)STRLEN(p_sbr);
3419 char_attr = hl_attr(HLF_AT);
3420 need_showbreak = FALSE;
3421 /* Correct end of highlighted area for 'showbreak',
3422 * required when 'linebreak' is also set. */
3423 if (tocol == vcol)
3424 tocol += n_extra;
3426 # endif
3428 #endif
3430 if (draw_state == WL_LINE - 1 && n_extra == 0)
3432 draw_state = WL_LINE;
3433 if (saved_n_extra)
3435 /* Continue item from end of wrapped line. */
3436 n_extra = saved_n_extra;
3437 c_extra = saved_c_extra;
3438 p_extra = saved_p_extra;
3439 char_attr = saved_char_attr;
3441 else
3442 char_attr = 0;
3446 /* When still displaying '$' of change command, stop at cursor */
3447 if (dollar_vcol != 0 && wp == curwin
3448 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
3449 #ifdef FEAT_DIFF
3450 && filler_todo <= 0
3451 #endif
3454 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3455 wp->w_p_rl);
3456 /* Pretend we have finished updating the window. Except when
3457 * 'cursorcolumn' is set. */
3458 #ifdef FEAT_SYN_HL
3459 if (wp->w_p_cuc)
3460 row = wp->w_cline_row + wp->w_cline_height;
3461 else
3462 #endif
3463 row = wp->w_height;
3464 break;
3467 if (draw_state == WL_LINE && area_highlighting)
3469 /* handle Visual or match highlighting in this line */
3470 if (vcol == fromcol
3471 #ifdef FEAT_MBYTE
3472 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3473 && (*mb_ptr2cells)(ptr) > 1)
3474 #endif
3475 || ((int)vcol_prev == fromcol_prev
3476 && vcol_prev < vcol /* not at margin */
3477 && vcol < tocol))
3478 area_attr = attr; /* start highlighting */
3479 else if (area_attr != 0
3480 && (vcol == tocol
3481 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
3482 area_attr = 0; /* stop highlighting */
3484 #ifdef FEAT_SEARCH_EXTRA
3485 if (!n_extra)
3488 * Check for start/end of search pattern match.
3489 * After end, check for start/end of next match.
3490 * When another match, have to check for start again.
3491 * Watch out for matching an empty string!
3492 * Do this for 'search_hl' and the match list (ordered by
3493 * priority).
3495 v = (long)(ptr - line);
3496 cur = wp->w_match_head;
3497 shl_flag = FALSE;
3498 while (cur != NULL || shl_flag == FALSE)
3500 if (shl_flag == FALSE
3501 && ((cur != NULL
3502 && cur->priority > SEARCH_HL_PRIORITY)
3503 || cur == NULL))
3505 shl = &search_hl;
3506 shl_flag = TRUE;
3508 else
3509 shl = &cur->hl;
3510 while (shl->rm.regprog != NULL)
3512 if (shl->startcol != MAXCOL
3513 && v >= (long)shl->startcol
3514 && v < (long)shl->endcol)
3516 shl->attr_cur = shl->attr;
3518 else if (v == (long)shl->endcol)
3520 shl->attr_cur = 0;
3522 next_search_hl(wp, shl, lnum, (colnr_T)v);
3524 /* Need to get the line again, a multi-line regexp
3525 * may have made it invalid. */
3526 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3527 ptr = line + v;
3529 if (shl->lnum == lnum)
3531 shl->startcol = shl->rm.startpos[0].col;
3532 if (shl->rm.endpos[0].lnum == 0)
3533 shl->endcol = shl->rm.endpos[0].col;
3534 else
3535 shl->endcol = MAXCOL;
3537 if (shl->startcol == shl->endcol)
3539 /* highlight empty match, try again after
3540 * it */
3541 #ifdef FEAT_MBYTE
3542 if (has_mbyte)
3543 shl->endcol += (*mb_ptr2len)(line
3544 + shl->endcol);
3545 else
3546 #endif
3547 ++shl->endcol;
3550 /* Loop to check if the match starts at the
3551 * current position */
3552 continue;
3555 break;
3557 if (shl != &search_hl && cur != NULL)
3558 cur = cur->next;
3561 /* Use attributes from match with highest priority among
3562 * 'search_hl' and the match list. */
3563 search_attr = search_hl.attr_cur;
3564 cur = wp->w_match_head;
3565 shl_flag = FALSE;
3566 while (cur != NULL || shl_flag == FALSE)
3568 if (shl_flag == FALSE
3569 && ((cur != NULL
3570 && cur->priority > SEARCH_HL_PRIORITY)
3571 || cur == NULL))
3573 shl = &search_hl;
3574 shl_flag = TRUE;
3576 else
3577 shl = &cur->hl;
3578 if (shl->attr_cur != 0)
3579 search_attr = shl->attr_cur;
3580 if (shl != &search_hl && cur != NULL)
3581 cur = cur->next;
3584 #endif
3586 #ifdef FEAT_DIFF
3587 if (diff_hlf != (hlf_T)0)
3589 if (diff_hlf == HLF_CHD && ptr - line >= change_start
3590 && n_extra == 0)
3591 diff_hlf = HLF_TXD; /* changed text */
3592 if (diff_hlf == HLF_TXD && ptr - line > change_end
3593 && n_extra == 0)
3594 diff_hlf = HLF_CHD; /* changed line */
3595 line_attr = hl_attr(diff_hlf);
3597 #endif
3599 /* Decide which of the highlight attributes to use. */
3600 attr_pri = TRUE;
3601 if (area_attr != 0)
3602 char_attr = area_attr;
3603 else if (search_attr != 0)
3604 char_attr = search_attr;
3605 #ifdef LINE_ATTR
3606 /* Use line_attr when not in the Visual or 'incsearch' area
3607 * (area_attr may be 0 when "noinvcur" is set). */
3608 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
3609 || vcol < fromcol || vcol_prev < fromcol_prev
3610 || vcol >= tocol))
3611 char_attr = line_attr;
3612 #endif
3613 else
3615 attr_pri = FALSE;
3616 #ifdef FEAT_SYN_HL
3617 if (has_syntax)
3618 char_attr = syntax_attr;
3619 else
3620 #endif
3621 char_attr = 0;
3626 * Get the next character to put on the screen.
3629 * The "p_extra" points to the extra stuff that is inserted to
3630 * represent special characters (non-printable stuff) and other
3631 * things. When all characters are the same, c_extra is used.
3632 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
3633 * "p_extra[n_extra]".
3634 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
3636 if (n_extra > 0)
3638 if (c_extra != NUL)
3640 c = c_extra;
3641 #ifdef FEAT_MBYTE
3642 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
3643 if (enc_utf8 && (*mb_char2len)(c) > 1)
3645 mb_utf8 = TRUE;
3646 u8cc[0] = 0;
3647 c = 0xc0;
3649 else
3650 mb_utf8 = FALSE;
3651 #endif
3653 else
3655 c = *p_extra;
3656 #ifdef FEAT_MBYTE
3657 if (has_mbyte)
3659 mb_c = c;
3660 if (enc_utf8)
3662 /* If the UTF-8 character is more than one byte:
3663 * Decode it into "mb_c". */
3664 mb_l = (*mb_ptr2len)(p_extra);
3665 mb_utf8 = FALSE;
3666 if (mb_l > n_extra)
3667 mb_l = 1;
3668 else if (mb_l > 1)
3670 mb_c = utfc_ptr2char(p_extra, u8cc);
3671 mb_utf8 = TRUE;
3672 c = 0xc0;
3675 else
3677 /* if this is a DBCS character, put it in "mb_c" */
3678 mb_l = MB_BYTE2LEN(c);
3679 if (mb_l >= n_extra)
3680 mb_l = 1;
3681 else if (mb_l > 1)
3682 mb_c = (c << 8) + p_extra[1];
3684 if (mb_l == 0) /* at the NUL at end-of-line */
3685 mb_l = 1;
3687 /* If a double-width char doesn't fit display a '>' in the
3688 * last column. */
3689 if ((
3690 # ifdef FEAT_RIGHTLEFT
3691 wp->w_p_rl ? (col <= 0) :
3692 # endif
3693 (col >= W_WIDTH(wp) - 1))
3694 && (*mb_char2cells)(mb_c) == 2)
3696 c = '>';
3697 mb_c = c;
3698 mb_l = 1;
3699 mb_utf8 = FALSE;
3700 multi_attr = hl_attr(HLF_AT);
3701 /* put the pointer back to output the double-width
3702 * character at the start of the next line. */
3703 ++n_extra;
3704 --p_extra;
3706 else
3708 n_extra -= mb_l - 1;
3709 p_extra += mb_l - 1;
3712 #endif
3713 ++p_extra;
3715 --n_extra;
3717 else
3720 * Get a character from the line itself.
3722 c = *ptr;
3723 #ifdef FEAT_MBYTE
3724 if (has_mbyte)
3726 mb_c = c;
3727 if (enc_utf8)
3729 /* If the UTF-8 character is more than one byte: Decode it
3730 * into "mb_c". */
3731 mb_l = (*mb_ptr2len)(ptr);
3732 mb_utf8 = FALSE;
3733 if (mb_l > 1)
3735 mb_c = utfc_ptr2char(ptr, u8cc);
3736 /* Overlong encoded ASCII or ASCII with composing char
3737 * is displayed normally, except a NUL. */
3738 if (mb_c < 0x80)
3739 c = mb_c;
3740 mb_utf8 = TRUE;
3742 /* At start of the line we can have a composing char.
3743 * Draw it as a space with a composing char. */
3744 if (utf_iscomposing(mb_c))
3746 int i;
3748 for (i = Screen_mco - 1; i > 0; --i)
3749 u8cc[i] = u8cc[i - 1];
3750 u8cc[0] = mb_c;
3751 mb_c = ' ';
3755 if ((mb_l == 1 && c >= 0x80)
3756 || (mb_l >= 1 && mb_c == 0)
3757 || (mb_l > 1 && (!vim_isprintc(mb_c)
3758 # ifdef UNICODE16
3759 || mb_c >= 0x10000
3760 # endif
3764 * Illegal UTF-8 byte: display as <xx>.
3765 * Non-BMP character : display as ? or fullwidth ?.
3767 # ifdef UNICODE16
3768 if (mb_c < 0x10000)
3769 # endif
3771 transchar_hex(extra, mb_c);
3772 # ifdef FEAT_RIGHTLEFT
3773 if (wp->w_p_rl) /* reverse */
3774 rl_mirror(extra);
3775 # endif
3777 # ifdef UNICODE16
3778 else if (utf_char2cells(mb_c) != 2)
3779 STRCPY(extra, "?");
3780 else
3781 /* 0xff1f in UTF-8: full-width '?' */
3782 STRCPY(extra, "\357\274\237");
3783 # endif
3785 p_extra = extra;
3786 c = *p_extra;
3787 mb_c = mb_ptr2char_adv(&p_extra);
3788 mb_utf8 = (c >= 0x80);
3789 n_extra = (int)STRLEN(p_extra);
3790 c_extra = NUL;
3791 if (area_attr == 0 && search_attr == 0)
3793 n_attr = n_extra + 1;
3794 extra_attr = hl_attr(HLF_8);
3795 saved_attr2 = char_attr; /* save current attr */
3798 else if (mb_l == 0) /* at the NUL at end-of-line */
3799 mb_l = 1;
3800 #ifdef FEAT_ARABIC
3801 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
3803 /* Do Arabic shaping. */
3804 int pc, pc1, nc;
3805 int pcc[MAX_MCO];
3807 /* The idea of what is the previous and next
3808 * character depends on 'rightleft'. */
3809 if (wp->w_p_rl)
3811 pc = prev_c;
3812 pc1 = prev_c1;
3813 nc = utf_ptr2char(ptr + mb_l);
3814 prev_c1 = u8cc[0];
3816 else
3818 pc = utfc_ptr2char(ptr + mb_l, pcc);
3819 nc = prev_c;
3820 pc1 = pcc[0];
3822 prev_c = mb_c;
3824 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
3826 else
3827 prev_c = mb_c;
3828 #endif
3830 else /* enc_dbcs */
3832 mb_l = MB_BYTE2LEN(c);
3833 if (mb_l == 0) /* at the NUL at end-of-line */
3834 mb_l = 1;
3835 else if (mb_l > 1)
3837 /* We assume a second byte below 32 is illegal.
3838 * Hopefully this is OK for all double-byte encodings!
3840 if (ptr[1] >= 32)
3841 mb_c = (c << 8) + ptr[1];
3842 else
3844 if (ptr[1] == NUL)
3846 /* head byte at end of line */
3847 mb_l = 1;
3848 transchar_nonprint(extra, c);
3850 else
3852 /* illegal tail byte */
3853 mb_l = 2;
3854 STRCPY(extra, "XX");
3856 p_extra = extra;
3857 n_extra = (int)STRLEN(extra) - 1;
3858 c_extra = NUL;
3859 c = *p_extra++;
3860 if (area_attr == 0 && search_attr == 0)
3862 n_attr = n_extra + 1;
3863 extra_attr = hl_attr(HLF_8);
3864 saved_attr2 = char_attr; /* save current attr */
3866 mb_c = c;
3870 /* If a double-width char doesn't fit display a '>' in the
3871 * last column; the character is displayed at the start of the
3872 * next line. */
3873 if ((
3874 # ifdef FEAT_RIGHTLEFT
3875 wp->w_p_rl ? (col <= 0) :
3876 # endif
3877 (col >= W_WIDTH(wp) - 1))
3878 && (*mb_char2cells)(mb_c) == 2)
3880 c = '>';
3881 mb_c = c;
3882 mb_utf8 = FALSE;
3883 mb_l = 1;
3884 multi_attr = hl_attr(HLF_AT);
3885 /* Put pointer back so that the character will be
3886 * displayed at the start of the next line. */
3887 --ptr;
3889 else if (*ptr != NUL)
3890 ptr += mb_l - 1;
3892 /* If a double-width char doesn't fit at the left side display
3893 * a '<' in the first column. */
3894 if (n_skip > 0 && mb_l > 1)
3896 n_extra = 1;
3897 c_extra = '<';
3898 c = ' ';
3899 if (area_attr == 0 && search_attr == 0)
3901 n_attr = n_extra + 1;
3902 extra_attr = hl_attr(HLF_AT);
3903 saved_attr2 = char_attr; /* save current attr */
3905 mb_c = c;
3906 mb_utf8 = FALSE;
3907 mb_l = 1;
3911 #endif
3912 ++ptr;
3914 /* 'list' : change char 160 to lcs_nbsp. */
3915 if (wp->w_p_list && (c == 160
3916 #ifdef FEAT_MBYTE
3917 || (mb_utf8 && mb_c == 160)
3918 #endif
3919 ) && lcs_nbsp)
3921 c = lcs_nbsp;
3922 if (area_attr == 0 && search_attr == 0)
3924 n_attr = 1;
3925 extra_attr = hl_attr(HLF_8);
3926 saved_attr2 = char_attr; /* save current attr */
3928 #ifdef FEAT_MBYTE
3929 mb_c = c;
3930 if (enc_utf8 && (*mb_char2len)(c) > 1)
3932 mb_utf8 = TRUE;
3933 u8cc[0] = 0;
3934 c = 0xc0;
3936 else
3937 mb_utf8 = FALSE;
3938 #endif
3941 if (extra_check)
3943 #ifdef FEAT_SPELL
3944 int can_spell = TRUE;
3945 #endif
3947 #ifdef FEAT_SYN_HL
3948 /* Get syntax attribute, unless still at the start of the line
3949 * (double-wide char that doesn't fit). */
3950 v = (long)(ptr - line);
3951 if (has_syntax && v > 0)
3953 /* Get the syntax attribute for the character. If there
3954 * is an error, disable syntax highlighting. */
3955 save_did_emsg = did_emsg;
3956 did_emsg = FALSE;
3958 syntax_attr = get_syntax_attr((colnr_T)v - 1,
3959 # ifdef FEAT_SPELL
3960 has_spell ? &can_spell :
3961 # endif
3962 NULL, FALSE);
3964 if (did_emsg)
3966 wp->w_buffer->b_syn_error = TRUE;
3967 has_syntax = FALSE;
3969 else
3970 did_emsg = save_did_emsg;
3972 /* Need to get the line again, a multi-line regexp may
3973 * have made it invalid. */
3974 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3975 ptr = line + v;
3977 if (!attr_pri)
3978 char_attr = syntax_attr;
3979 else
3980 char_attr = hl_combine_attr(syntax_attr, char_attr);
3982 #endif
3984 #ifdef FEAT_SPELL
3985 /* Check spelling (unless at the end of the line).
3986 * Only do this when there is no syntax highlighting, the
3987 * @Spell cluster is not used or the current syntax item
3988 * contains the @Spell cluster. */
3989 if (has_spell && v >= word_end && v > cur_checked_col)
3991 spell_attr = 0;
3992 # ifdef FEAT_SYN_HL
3993 if (!attr_pri)
3994 char_attr = syntax_attr;
3995 # endif
3996 if (c != 0 && (
3997 # ifdef FEAT_SYN_HL
3998 !has_syntax ||
3999 # endif
4000 can_spell))
4002 char_u *prev_ptr, *p;
4003 int len;
4004 hlf_T spell_hlf = HLF_COUNT;
4005 # ifdef FEAT_MBYTE
4006 if (has_mbyte)
4008 prev_ptr = ptr - mb_l;
4009 v -= mb_l - 1;
4011 else
4012 # endif
4013 prev_ptr = ptr - 1;
4015 /* Use nextline[] if possible, it has the start of the
4016 * next line concatenated. */
4017 if ((prev_ptr - line) - nextlinecol >= 0)
4018 p = nextline + (prev_ptr - line) - nextlinecol;
4019 else
4020 p = prev_ptr;
4021 cap_col -= (int)(prev_ptr - line);
4022 len = spell_check(wp, p, &spell_hlf, &cap_col,
4023 nochange);
4024 word_end = v + len;
4026 /* In Insert mode only highlight a word that
4027 * doesn't touch the cursor. */
4028 if (spell_hlf != HLF_COUNT
4029 && (State & INSERT) != 0
4030 && wp->w_cursor.lnum == lnum
4031 && wp->w_cursor.col >=
4032 (colnr_T)(prev_ptr - line)
4033 && wp->w_cursor.col < (colnr_T)word_end)
4035 spell_hlf = HLF_COUNT;
4036 spell_redraw_lnum = lnum;
4039 if (spell_hlf == HLF_COUNT && p != prev_ptr
4040 && (p - nextline) + len > nextline_idx)
4042 /* Remember that the good word continues at the
4043 * start of the next line. */
4044 checked_lnum = lnum + 1;
4045 checked_col = (int)((p - nextline) + len - nextline_idx);
4048 /* Turn index into actual attributes. */
4049 if (spell_hlf != HLF_COUNT)
4050 spell_attr = highlight_attr[spell_hlf];
4052 if (cap_col > 0)
4054 if (p != prev_ptr
4055 && (p - nextline) + cap_col >= nextline_idx)
4057 /* Remember that the word in the next line
4058 * must start with a capital. */
4059 capcol_lnum = lnum + 1;
4060 cap_col = (int)((p - nextline) + cap_col
4061 - nextline_idx);
4063 else
4064 /* Compute the actual column. */
4065 cap_col += (int)(prev_ptr - line);
4069 if (spell_attr != 0)
4071 if (!attr_pri)
4072 char_attr = hl_combine_attr(char_attr, spell_attr);
4073 else
4074 char_attr = hl_combine_attr(spell_attr, char_attr);
4076 #endif
4077 #ifdef FEAT_LINEBREAK
4079 * Found last space before word: check for line break.
4081 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr)
4082 && !wp->w_p_list)
4084 n_extra = win_lbr_chartabsize(wp, ptr - (
4085 # ifdef FEAT_MBYTE
4086 has_mbyte ? mb_l :
4087 # endif
4088 1), (colnr_T)vcol, NULL) - 1;
4089 c_extra = ' ';
4090 if (vim_iswhite(c))
4091 c = ' ';
4093 #endif
4095 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4097 c = lcs_trail;
4098 if (!attr_pri)
4100 n_attr = 1;
4101 extra_attr = hl_attr(HLF_8);
4102 saved_attr2 = char_attr; /* save current attr */
4104 #ifdef FEAT_MBYTE
4105 mb_c = c;
4106 if (enc_utf8 && (*mb_char2len)(c) > 1)
4108 mb_utf8 = TRUE;
4109 u8cc[0] = 0;
4110 c = 0xc0;
4112 else
4113 mb_utf8 = FALSE;
4114 #endif
4119 * Handling of non-printable characters.
4121 if (!(chartab[c & 0xff] & CT_PRINT_CHAR))
4124 * when getting a character from the file, we may have to
4125 * turn it into something else on the way to putting it
4126 * into "ScreenLines".
4128 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4130 /* tab amount depends on current column */
4131 n_extra = (int)wp->w_buffer->b_p_ts
4132 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4133 #ifdef FEAT_MBYTE
4134 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4135 #endif
4136 if (wp->w_p_list)
4138 c = lcs_tab1;
4139 c_extra = lcs_tab2;
4140 n_attr = n_extra + 1;
4141 extra_attr = hl_attr(HLF_8);
4142 saved_attr2 = char_attr; /* save current attr */
4143 #ifdef FEAT_MBYTE
4144 mb_c = c;
4145 if (enc_utf8 && (*mb_char2len)(c) > 1)
4147 mb_utf8 = TRUE;
4148 u8cc[0] = 0;
4149 c = 0xc0;
4151 #endif
4153 else
4155 c_extra = ' ';
4156 c = ' ';
4159 else if (c == NUL
4160 && ((wp->w_p_list && lcs_eol > 0)
4161 || ((fromcol >= 0 || fromcol_prev >= 0)
4162 && tocol > vcol
4163 #ifdef FEAT_VISUAL
4164 && VIsual_mode != Ctrl_V
4165 #endif
4166 && (
4167 # ifdef FEAT_RIGHTLEFT
4168 wp->w_p_rl ? (col >= 0) :
4169 # endif
4170 (col < W_WIDTH(wp)))
4171 && !(noinvcur
4172 && lnum == wp->w_cursor.lnum
4173 && (colnr_T)vcol == wp->w_virtcol)))
4174 && lcs_eol_one >= 0)
4176 /* Display a '$' after the line or highlight an extra
4177 * character if the line break is included. */
4178 #if defined(FEAT_DIFF) || defined(LINE_ATTR)
4179 /* For a diff line the highlighting continues after the
4180 * "$". */
4181 if (
4182 # ifdef FEAT_DIFF
4183 diff_hlf == (hlf_T)0
4184 # ifdef LINE_ATTR
4186 # endif
4187 # endif
4188 # ifdef LINE_ATTR
4189 line_attr == 0
4190 # endif
4192 #endif
4194 #ifdef FEAT_VIRTUALEDIT
4195 /* In virtualedit, visual selections may extend
4196 * beyond end of line. */
4197 if (area_highlighting && virtual_active()
4198 && tocol != MAXCOL && vcol < tocol)
4199 n_extra = 0;
4200 else
4201 #endif
4203 p_extra = at_end_str;
4204 n_extra = 1;
4205 c_extra = NUL;
4208 if (wp->w_p_list)
4209 c = lcs_eol;
4210 else
4211 c = ' ';
4212 lcs_eol_one = -1;
4213 --ptr; /* put it back at the NUL */
4214 if (!attr_pri)
4216 extra_attr = hl_attr(HLF_AT);
4217 n_attr = 1;
4219 #ifdef FEAT_MBYTE
4220 mb_c = c;
4221 if (enc_utf8 && (*mb_char2len)(c) > 1)
4223 mb_utf8 = TRUE;
4224 u8cc[0] = 0;
4225 c = 0xc0;
4227 else
4228 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4229 #endif
4231 else if (c != NUL)
4233 p_extra = transchar(c);
4234 #ifdef FEAT_RIGHTLEFT
4235 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4236 rl_mirror(p_extra); /* reverse "<12>" */
4237 #endif
4238 n_extra = byte2cells(c) - 1;
4239 c_extra = NUL;
4240 c = *p_extra++;
4241 if (!attr_pri)
4243 n_attr = n_extra + 1;
4244 extra_attr = hl_attr(HLF_8);
4245 saved_attr2 = char_attr; /* save current attr */
4247 #ifdef FEAT_MBYTE
4248 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4249 #endif
4251 #ifdef FEAT_VIRTUALEDIT
4252 else if (VIsual_active
4253 && (VIsual_mode == Ctrl_V
4254 || VIsual_mode == 'v')
4255 && virtual_active()
4256 && tocol != MAXCOL
4257 && vcol < tocol
4258 && (
4259 # ifdef FEAT_RIGHTLEFT
4260 wp->w_p_rl ? (col >= 0) :
4261 # endif
4262 (col < W_WIDTH(wp))))
4264 c = ' ';
4265 --ptr; /* put it back at the NUL */
4267 #endif
4268 #if defined(LINE_ATTR)
4269 else if ((
4270 # ifdef FEAT_DIFF
4271 diff_hlf != (hlf_T)0 ||
4272 # endif
4273 line_attr != 0
4274 ) && (
4275 # ifdef FEAT_RIGHTLEFT
4276 wp->w_p_rl ? (col >= 0) :
4277 # endif
4278 (col < W_WIDTH(wp))))
4280 /* Highlight until the right side of the window */
4281 c = ' ';
4282 --ptr; /* put it back at the NUL */
4284 /* Remember we do the char for line highlighting. */
4285 ++did_line_attr;
4287 /* don't do search HL for the rest of the line */
4288 if (line_attr != 0 && char_attr == search_attr && col > 0)
4289 char_attr = line_attr;
4290 # ifdef FEAT_DIFF
4291 if (diff_hlf == HLF_TXD)
4293 diff_hlf = HLF_CHD;
4294 if (attr == 0 || char_attr != attr)
4295 char_attr = hl_attr(diff_hlf);
4297 # endif
4299 #endif
4303 /* Don't override visual selection highlighting. */
4304 if (n_attr > 0
4305 && draw_state == WL_LINE
4306 && !attr_pri)
4307 char_attr = extra_attr;
4309 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
4310 /* XIM don't send preedit_start and preedit_end, but they send
4311 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
4312 * im_is_preediting() here. */
4313 if (xic != NULL
4314 && lnum == wp->w_cursor.lnum
4315 && (State & INSERT)
4316 && !p_imdisable
4317 && im_is_preediting()
4318 && draw_state == WL_LINE)
4320 colnr_T tcol;
4322 if (preedit_end_col == MAXCOL)
4323 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
4324 else
4325 tcol = preedit_end_col;
4326 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
4328 if (feedback_old_attr < 0)
4330 feedback_col = 0;
4331 feedback_old_attr = char_attr;
4333 char_attr = im_get_feedback_attr(feedback_col);
4334 if (char_attr < 0)
4335 char_attr = feedback_old_attr;
4336 feedback_col++;
4338 else if (feedback_old_attr >= 0)
4340 char_attr = feedback_old_attr;
4341 feedback_old_attr = -1;
4342 feedback_col = 0;
4345 #endif
4347 * Handle the case where we are in column 0 but not on the first
4348 * character of the line and the user wants us to show us a
4349 * special character (via 'listchars' option "precedes:<char>".
4351 if (lcs_prec_todo != NUL
4352 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
4353 #ifdef FEAT_DIFF
4354 && filler_todo <= 0
4355 #endif
4356 && draw_state > WL_NR
4357 && c != NUL)
4359 c = lcs_prec;
4360 lcs_prec_todo = NUL;
4361 #ifdef FEAT_MBYTE
4362 mb_c = c;
4363 if (enc_utf8 && (*mb_char2len)(c) > 1)
4365 mb_utf8 = TRUE;
4366 u8cc[0] = 0;
4367 c = 0xc0;
4369 else
4370 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4371 #endif
4372 if (!attr_pri)
4374 saved_attr3 = char_attr; /* save current attr */
4375 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
4376 n_attr3 = 1;
4381 * At end of the text line or just after the last character.
4383 if (c == NUL
4384 #if defined(LINE_ATTR)
4385 || did_line_attr == 1
4386 #endif
4389 #ifdef FEAT_SEARCH_EXTRA
4390 long prevcol = (long)(ptr - line) - (c == NUL);
4392 /* we're not really at that column when skipping some text */
4393 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
4394 ++prevcol;
4395 #endif
4397 /* invert at least one char, used for Visual and empty line or
4398 * highlight match at end of line. If it's beyond the last
4399 * char on the screen, just overwrite that one (tricky!) Not
4400 * needed when a '$' was displayed for 'list'. */
4401 #ifdef FEAT_SEARCH_EXTRA
4402 prevcol_hl_flag = FALSE;
4403 if (prevcol == (long)search_hl.startcol)
4404 prevcol_hl_flag = TRUE;
4405 else
4407 cur = wp->w_match_head;
4408 while (cur != NULL)
4410 if (prevcol == (long)cur->hl.startcol)
4412 prevcol_hl_flag = TRUE;
4413 break;
4415 cur = cur->next;
4418 #endif
4419 if (lcs_eol == lcs_eol_one
4420 && ((area_attr != 0 && vcol == fromcol
4421 #ifdef FEAT_VISUAL
4422 && (VIsual_mode != Ctrl_V
4423 || lnum == VIsual.lnum
4424 || lnum == curwin->w_cursor.lnum)
4425 #endif
4426 && c == NUL)
4427 #ifdef FEAT_SEARCH_EXTRA
4428 /* highlight 'hlsearch' match at end of line */
4429 || (prevcol_hl_flag == TRUE
4430 # if defined(LINE_ATTR)
4431 && did_line_attr <= 1
4432 # endif
4434 #endif
4437 int n = 0;
4439 #ifdef FEAT_RIGHTLEFT
4440 if (wp->w_p_rl)
4442 if (col < 0)
4443 n = 1;
4445 else
4446 #endif
4448 if (col >= W_WIDTH(wp))
4449 n = -1;
4451 if (n != 0)
4453 /* At the window boundary, highlight the last character
4454 * instead (better than nothing). */
4455 off += n;
4456 col += n;
4458 else
4460 /* Add a blank character to highlight. */
4461 ScreenLines[off] = ' ';
4462 #ifdef FEAT_MBYTE
4463 if (enc_utf8)
4464 ScreenLinesUC[off] = 0;
4465 #endif
4467 #ifdef FEAT_SEARCH_EXTRA
4468 if (area_attr == 0)
4470 /* Use attributes from match with highest priority among
4471 * 'search_hl' and the match list. */
4472 char_attr = search_hl.attr;
4473 cur = wp->w_match_head;
4474 shl_flag = FALSE;
4475 while (cur != NULL || shl_flag == FALSE)
4477 if (shl_flag == FALSE
4478 && ((cur != NULL
4479 && cur->priority > SEARCH_HL_PRIORITY)
4480 || cur == NULL))
4482 shl = &search_hl;
4483 shl_flag = TRUE;
4485 else
4486 shl = &cur->hl;
4487 if ((ptr - line) - 1 == (long)shl->startcol)
4488 char_attr = shl->attr;
4489 if (shl != &search_hl && cur != NULL)
4490 cur = cur->next;
4493 #endif
4494 ScreenAttrs[off] = char_attr;
4495 #ifdef FEAT_RIGHTLEFT
4496 if (wp->w_p_rl)
4498 --col;
4499 --off;
4501 else
4502 #endif
4504 ++col;
4505 ++off;
4507 ++vcol;
4508 #ifdef FEAT_SYN_HL
4509 eol_hl_off = 1;
4510 #endif
4515 * At end of the text line.
4517 if (c == NUL)
4519 #ifdef FEAT_SYN_HL
4520 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
4521 && lnum == wp->w_cursor.lnum)
4523 /* highlight last char after line */
4524 --col;
4525 --off;
4526 --vcol;
4529 /* Highlight 'cursorcolumn' past end of the line. */
4530 if (wp->w_p_wrap)
4531 v = wp->w_skipcol;
4532 else
4533 v = wp->w_leftcol;
4534 /* check if line ends before left margin */
4535 if (vcol < v + col - win_col_off(wp))
4537 vcol = v + col - win_col_off(wp);
4538 if (wp->w_p_cuc
4539 && (int)wp->w_virtcol >= vcol - eol_hl_off
4540 && (int)wp->w_virtcol < W_WIDTH(wp) * (row - startrow + 1)
4542 && lnum != wp->w_cursor.lnum
4543 # ifdef FEAT_RIGHTLEFT
4544 && !wp->w_p_rl
4545 # endif
4548 while (col < W_WIDTH(wp))
4550 ScreenLines[off] = ' ';
4551 #ifdef FEAT_MBYTE
4552 if (enc_utf8)
4553 ScreenLinesUC[off] = 0;
4554 #endif
4555 ++col;
4556 if (vcol == (long)wp->w_virtcol)
4558 ScreenAttrs[off] = hl_attr(HLF_CUC);
4559 break;
4561 ScreenAttrs[off++] = 0;
4562 ++vcol;
4565 #endif
4567 SCREEN_LINE(screen_row, W_WINCOL(wp), col, (int)W_WIDTH(wp),
4568 wp->w_p_rl);
4569 row++;
4572 * Update w_cline_height and w_cline_folded if the cursor line was
4573 * updated (saves a call to plines() later).
4575 if (wp == curwin && lnum == curwin->w_cursor.lnum)
4577 curwin->w_cline_row = startrow;
4578 curwin->w_cline_height = row - startrow;
4579 #ifdef FEAT_FOLDING
4580 curwin->w_cline_folded = FALSE;
4581 #endif
4582 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
4585 break;
4588 /* line continues beyond line end */
4589 if (lcs_ext
4590 && !wp->w_p_wrap
4591 #ifdef FEAT_DIFF
4592 && filler_todo <= 0
4593 #endif
4594 && (
4595 #ifdef FEAT_RIGHTLEFT
4596 wp->w_p_rl ? col == 0 :
4597 #endif
4598 col == W_WIDTH(wp) - 1)
4599 && (*ptr != NUL
4600 || (wp->w_p_list && lcs_eol_one > 0)
4601 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
4603 c = lcs_ext;
4604 char_attr = hl_attr(HLF_AT);
4605 #ifdef FEAT_MBYTE
4606 mb_c = c;
4607 if (enc_utf8 && (*mb_char2len)(c) > 1)
4609 mb_utf8 = TRUE;
4610 u8cc[0] = 0;
4611 c = 0xc0;
4613 else
4614 mb_utf8 = FALSE;
4615 #endif
4618 #ifdef FEAT_SYN_HL
4619 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
4620 * highlight the cursor position itself. */
4621 if (wp->w_p_cuc && vcol == (long)wp->w_virtcol
4622 && lnum != wp->w_cursor.lnum
4623 && draw_state == WL_LINE
4624 && !lnum_in_visual_area)
4626 vcol_save_attr = char_attr;
4627 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
4629 else
4630 vcol_save_attr = -1;
4631 #endif
4634 * Store character to be displayed.
4635 * Skip characters that are left of the screen for 'nowrap'.
4637 vcol_prev = vcol;
4638 if (draw_state < WL_LINE || n_skip <= 0)
4641 * Store the character.
4643 #if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
4644 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
4646 /* A double-wide character is: put first halve in left cell. */
4647 --off;
4648 --col;
4650 #endif
4651 ScreenLines[off] = c;
4652 #ifdef FEAT_MBYTE
4653 if (enc_dbcs == DBCS_JPNU)
4655 if ((mb_c & 0xff00) == 0x8e00)
4656 ScreenLines[off] = 0x8e;
4657 ScreenLines2[off] = mb_c & 0xff;
4659 else if (enc_utf8)
4661 if (mb_utf8)
4663 int i;
4665 ScreenLinesUC[off] = mb_c;
4666 if ((c & 0xff) == 0)
4667 ScreenLines[off] = 0x80; /* avoid storing zero */
4668 for (i = 0; i < Screen_mco; ++i)
4670 ScreenLinesC[i][off] = u8cc[i];
4671 if (u8cc[i] == 0)
4672 break;
4675 else
4676 ScreenLinesUC[off] = 0;
4678 if (multi_attr)
4680 ScreenAttrs[off] = multi_attr;
4681 multi_attr = 0;
4683 else
4684 #endif
4685 ScreenAttrs[off] = char_attr;
4687 #ifdef FEAT_MBYTE
4688 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
4690 /* Need to fill two screen columns. */
4691 ++off;
4692 ++col;
4693 if (enc_utf8)
4694 /* UTF-8: Put a 0 in the second screen char. */
4695 ScreenLines[off] = 0;
4696 else
4697 /* DBCS: Put second byte in the second screen char. */
4698 ScreenLines[off] = mb_c & 0xff;
4699 ++vcol;
4700 /* When "tocol" is halfway a character, set it to the end of
4701 * the character, otherwise highlighting won't stop. */
4702 if (tocol == vcol)
4703 ++tocol;
4704 #ifdef FEAT_RIGHTLEFT
4705 if (wp->w_p_rl)
4707 /* now it's time to backup one cell */
4708 --off;
4709 --col;
4711 #endif
4713 #endif
4714 #ifdef FEAT_RIGHTLEFT
4715 if (wp->w_p_rl)
4717 --off;
4718 --col;
4720 else
4721 #endif
4723 ++off;
4724 ++col;
4727 else
4728 --n_skip;
4730 /* Only advance the "vcol" when after the 'number' column. */
4731 if (draw_state > WL_NR
4732 #ifdef FEAT_DIFF
4733 && filler_todo <= 0
4734 #endif
4736 ++vcol;
4738 #ifdef FEAT_SYN_HL
4739 if (vcol_save_attr >= 0)
4740 char_attr = vcol_save_attr;
4741 #endif
4743 /* restore attributes after "predeces" in 'listchars' */
4744 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
4745 char_attr = saved_attr3;
4747 /* restore attributes after last 'listchars' or 'number' char */
4748 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
4749 char_attr = saved_attr2;
4752 * At end of screen line and there is more to come: Display the line
4753 * so far. If there is no more to display it is caught above.
4755 if ((
4756 #ifdef FEAT_RIGHTLEFT
4757 wp->w_p_rl ? (col < 0) :
4758 #endif
4759 (col >= W_WIDTH(wp)))
4760 && (*ptr != NUL
4761 #ifdef FEAT_DIFF
4762 || filler_todo > 0
4763 #endif
4764 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
4765 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
4768 SCREEN_LINE(screen_row, W_WINCOL(wp), col, (int)W_WIDTH(wp),
4769 wp->w_p_rl);
4770 ++row;
4771 ++screen_row;
4773 /* When not wrapping and finished diff lines, or when displayed
4774 * '$' and highlighting until last column, break here. */
4775 if ((!wp->w_p_wrap
4776 #ifdef FEAT_DIFF
4777 && filler_todo <= 0
4778 #endif
4779 ) || lcs_eol_one == -1)
4780 break;
4782 /* When the window is too narrow draw all "@" lines. */
4783 if (draw_state != WL_LINE
4784 #ifdef FEAT_DIFF
4785 && filler_todo <= 0
4786 #endif
4789 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
4790 #ifdef FEAT_VERTSPLIT
4791 draw_vsep_win(wp, row);
4792 #endif
4793 row = endrow;
4796 /* When line got too long for screen break here. */
4797 if (row == endrow)
4799 ++row;
4800 break;
4803 if (screen_cur_row == screen_row - 1
4804 #ifdef FEAT_DIFF
4805 && filler_todo <= 0
4806 #endif
4807 && W_WIDTH(wp) == Columns)
4809 /* Remember that the line wraps, used for modeless copy. */
4810 LineWraps[screen_row - 1] = TRUE;
4813 * Special trick to make copy/paste of wrapped lines work with
4814 * xterm/screen: write an extra character beyond the end of
4815 * the line. This will work with all terminal types
4816 * (regardless of the xn,am settings).
4817 * Only do this on a fast tty.
4818 * Only do this if the cursor is on the current line
4819 * (something has been written in it).
4820 * Don't do this for the GUI.
4821 * Don't do this for double-width characters.
4822 * Don't do this for a window not at the right screen border.
4824 if (p_tf
4825 #ifdef FEAT_GUI
4826 && !gui.in_use
4827 #endif
4828 #ifdef FEAT_MBYTE
4829 && !(has_mbyte
4830 && ((*mb_off2cells)(LineOffset[screen_row],
4831 LineOffset[screen_row] + screen_Columns)
4832 == 2
4833 || (*mb_off2cells)(LineOffset[screen_row - 1]
4834 + (int)Columns - 2,
4835 LineOffset[screen_row] + screen_Columns)
4836 == 2))
4837 #endif
4840 /* First make sure we are at the end of the screen line,
4841 * then output the same character again to let the
4842 * terminal know about the wrap. If the terminal doesn't
4843 * auto-wrap, we overwrite the character. */
4844 if (screen_cur_col != W_WIDTH(wp))
4845 screen_char(LineOffset[screen_row - 1]
4846 + (unsigned)Columns - 1,
4847 screen_row - 1, (int)(Columns - 1));
4849 #ifdef FEAT_MBYTE
4850 /* When there is a multi-byte character, just output a
4851 * space to keep it simple. */
4852 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
4853 screen_row - 1] + (Columns - 1)]) > 1)
4854 out_char(' ');
4855 else
4856 #endif
4857 out_char(ScreenLines[LineOffset[screen_row - 1]
4858 + (Columns - 1)]);
4859 /* force a redraw of the first char on the next line */
4860 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
4861 screen_start(); /* don't know where cursor is now */
4865 col = 0;
4866 off = (unsigned)(current_ScreenLine - ScreenLines);
4867 #ifdef FEAT_RIGHTLEFT
4868 if (wp->w_p_rl)
4870 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
4871 off += col;
4873 #endif
4875 /* reset the drawing state for the start of a wrapped line */
4876 draw_state = WL_START;
4877 saved_n_extra = n_extra;
4878 saved_p_extra = p_extra;
4879 saved_c_extra = c_extra;
4880 saved_char_attr = char_attr;
4881 n_extra = 0;
4882 lcs_prec_todo = lcs_prec;
4883 #ifdef FEAT_LINEBREAK
4884 # ifdef FEAT_DIFF
4885 if (filler_todo <= 0)
4886 # endif
4887 need_showbreak = TRUE;
4888 #endif
4889 #ifdef FEAT_DIFF
4890 --filler_todo;
4891 /* When the filler lines are actually below the last line of the
4892 * file, don't draw the line itself, break here. */
4893 if (filler_todo == 0 && wp->w_botfill)
4894 break;
4895 #endif
4898 } /* for every character in the line */
4900 #ifdef FEAT_SPELL
4901 /* After an empty line check first word for capital. */
4902 if (*skipwhite(line) == NUL)
4904 capcol_lnum = lnum + 1;
4905 cap_col = 0;
4907 #endif
4909 return row;
4912 #ifdef FEAT_MBYTE
4913 static int comp_char_differs __ARGS((int, int));
4916 * Return if the composing characters at "off_from" and "off_to" differ.
4917 * Only to be used when ScreenLinesUC[off_from] != 0.
4919 static int
4920 comp_char_differs(off_from, off_to)
4921 int off_from;
4922 int off_to;
4924 int i;
4926 for (i = 0; i < Screen_mco; ++i)
4928 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
4929 return TRUE;
4930 if (ScreenLinesC[i][off_from] == 0)
4931 break;
4933 return FALSE;
4935 #endif
4938 * Check whether the given character needs redrawing:
4939 * - the (first byte of the) character is different
4940 * - the attributes are different
4941 * - the character is multi-byte and the next byte is different
4942 * - the character is two cells wide and the second cell differs.
4944 static int
4945 char_needs_redraw(off_from, off_to, cols)
4946 int off_from;
4947 int off_to;
4948 int cols;
4950 if (cols > 0
4951 && ((ScreenLines[off_from] != ScreenLines[off_to]
4952 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
4954 #ifdef FEAT_MBYTE
4955 || (enc_dbcs != 0
4956 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
4957 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
4958 ? ScreenLines2[off_from] != ScreenLines2[off_to]
4959 : (cols > 1 && ScreenLines[off_from + 1]
4960 != ScreenLines[off_to + 1])))
4961 || (enc_utf8
4962 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
4963 || (ScreenLinesUC[off_from] != 0
4964 && comp_char_differs(off_from, off_to))
4965 || (cols > 1 && ScreenLines[off_from + 1]
4966 != ScreenLines[off_to + 1])))
4967 #endif
4969 return TRUE;
4970 return FALSE;
4974 * Move one "cooked" screen line to the screen, but only the characters that
4975 * have actually changed. Handle insert/delete character.
4976 * "coloff" gives the first column on the screen for this line.
4977 * "endcol" gives the columns where valid characters are.
4978 * "clear_width" is the width of the window. It's > 0 if the rest of the line
4979 * needs to be cleared, negative otherwise.
4980 * "rlflag" is TRUE in a rightleft window:
4981 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
4982 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
4984 static void
4985 screen_line(row, coloff, endcol, clear_width
4986 #ifdef FEAT_RIGHTLEFT
4987 , rlflag
4988 #endif
4990 int row;
4991 int coloff;
4992 int endcol;
4993 int clear_width;
4994 #ifdef FEAT_RIGHTLEFT
4995 int rlflag;
4996 #endif
4998 unsigned off_from;
4999 unsigned off_to;
5000 #ifdef FEAT_MBYTE
5001 unsigned max_off_from;
5002 unsigned max_off_to;
5003 #endif
5004 int col = 0;
5005 #if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_VERTSPLIT)
5006 int hl;
5007 #endif
5008 int force = FALSE; /* force update rest of the line */
5009 int redraw_this /* bool: does character need redraw? */
5010 #ifdef FEAT_GUI
5011 = TRUE /* For GUI when while-loop empty */
5012 #endif
5014 int redraw_next; /* redraw_this for next character */
5015 #ifdef FEAT_MBYTE
5016 int clear_next = FALSE;
5017 int char_cells; /* 1: normal char */
5018 /* 2: occupies two display cells */
5019 # define CHAR_CELLS char_cells
5020 #else
5021 # define CHAR_CELLS 1
5022 #endif
5024 # ifdef FEAT_CLIPBOARD
5025 clip_may_clear_selection(row, row);
5026 # endif
5028 off_from = (unsigned)(current_ScreenLine - ScreenLines);
5029 off_to = LineOffset[row] + coloff;
5030 #ifdef FEAT_MBYTE
5031 max_off_from = off_from + screen_Columns;
5032 max_off_to = LineOffset[row] + screen_Columns;
5033 #endif
5035 #ifdef FEAT_RIGHTLEFT
5036 if (rlflag)
5038 /* Clear rest first, because it's left of the text. */
5039 if (clear_width > 0)
5041 while (col <= endcol && ScreenLines[off_to] == ' '
5042 && ScreenAttrs[off_to] == 0
5043 # ifdef FEAT_MBYTE
5044 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5045 # endif
5048 ++off_to;
5049 ++col;
5051 if (col <= endcol)
5052 screen_fill(row, row + 1, col + coloff,
5053 endcol + coloff + 1, ' ', ' ', 0);
5055 col = endcol + 1;
5056 off_to = LineOffset[row] + col + coloff;
5057 off_from += col;
5058 endcol = (clear_width > 0 ? clear_width : -clear_width);
5060 #endif /* FEAT_RIGHTLEFT */
5062 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
5064 while (col < endcol)
5066 #ifdef FEAT_MBYTE
5067 if (has_mbyte && (col + 1 < endcol))
5068 char_cells = (*mb_off2cells)(off_from, max_off_from);
5069 else
5070 char_cells = 1;
5071 #endif
5073 redraw_this = redraw_next;
5074 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
5075 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
5077 #ifdef FEAT_GUI
5078 /* If the next character was bold, then redraw the current character to
5079 * remove any pixels that might have spilt over into us. This only
5080 * happens in the GUI.
5082 if (redraw_next && gui.in_use)
5084 hl = ScreenAttrs[off_to + CHAR_CELLS];
5085 if (hl > HL_ALL)
5086 hl = syn_attr2attr(hl);
5087 if (hl & HL_BOLD)
5088 redraw_this = TRUE;
5090 #endif
5092 if (redraw_this)
5095 * Special handling when 'xs' termcap flag set (hpterm):
5096 * Attributes for characters are stored at the position where the
5097 * cursor is when writing the highlighting code. The
5098 * start-highlighting code must be written with the cursor on the
5099 * first highlighted character. The stop-highlighting code must
5100 * be written with the cursor just after the last highlighted
5101 * character.
5102 * Overwriting a character doesn't remove it's highlighting. Need
5103 * to clear the rest of the line, and force redrawing it
5104 * completely.
5106 if ( p_wiv
5107 && !force
5108 #ifdef FEAT_GUI
5109 && !gui.in_use
5110 #endif
5111 && ScreenAttrs[off_to] != 0
5112 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
5115 * Need to remove highlighting attributes here.
5117 windgoto(row, col + coloff);
5118 out_str(T_CE); /* clear rest of this screen line */
5119 screen_start(); /* don't know where cursor is now */
5120 force = TRUE; /* force redraw of rest of the line */
5121 redraw_next = TRUE; /* or else next char would miss out */
5124 * If the previous character was highlighted, need to stop
5125 * highlighting at this character.
5127 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
5129 screen_attr = ScreenAttrs[off_to - 1];
5130 term_windgoto(row, col + coloff);
5131 screen_stop_highlight();
5133 else
5134 screen_attr = 0; /* highlighting has stopped */
5136 #ifdef FEAT_MBYTE
5137 if (enc_dbcs != 0)
5139 /* Check if overwriting a double-byte with a single-byte or
5140 * the other way around requires another character to be
5141 * redrawn. For UTF-8 this isn't needed, because comparing
5142 * ScreenLinesUC[] is sufficient. */
5143 if (char_cells == 1
5144 && col + 1 < endcol
5145 && (*mb_off2cells)(off_to, max_off_to) > 1)
5147 /* Writing a single-cell character over a double-cell
5148 * character: need to redraw the next cell. */
5149 ScreenLines[off_to + 1] = 0;
5150 redraw_next = TRUE;
5152 else if (char_cells == 2
5153 && col + 2 < endcol
5154 && (*mb_off2cells)(off_to, max_off_to) == 1
5155 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
5157 /* Writing the second half of a double-cell character over
5158 * a double-cell character: need to redraw the second
5159 * cell. */
5160 ScreenLines[off_to + 2] = 0;
5161 redraw_next = TRUE;
5164 if (enc_dbcs == DBCS_JPNU)
5165 ScreenLines2[off_to] = ScreenLines2[off_from];
5167 /* When writing a single-width character over a double-width
5168 * character and at the end of the redrawn text, need to clear out
5169 * the right halve of the old character.
5170 * Also required when writing the right halve of a double-width
5171 * char over the left halve of an existing one. */
5172 if (has_mbyte && col + char_cells == endcol
5173 && ((char_cells == 1
5174 && (*mb_off2cells)(off_to, max_off_to) > 1)
5175 || (char_cells == 2
5176 && (*mb_off2cells)(off_to, max_off_to) == 1
5177 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
5178 clear_next = TRUE;
5179 #endif
5181 ScreenLines[off_to] = ScreenLines[off_from];
5182 #ifdef FEAT_MBYTE
5183 if (enc_utf8)
5185 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
5186 if (ScreenLinesUC[off_from] != 0)
5188 int i;
5190 for (i = 0; i < Screen_mco; ++i)
5191 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
5194 if (char_cells == 2)
5195 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
5196 #endif
5198 #if defined(FEAT_GUI) || defined(UNIX)
5199 /* The bold trick makes a single column of pixels appear in the
5200 * next character. When a bold character is removed, the next
5201 * character should be redrawn too. This happens for our own GUI
5202 * and for some xterms. */
5203 if (
5204 # ifdef FEAT_GUI
5205 gui.in_use
5206 # endif
5207 # if defined(FEAT_GUI) && defined(UNIX)
5209 # endif
5210 # ifdef UNIX
5211 term_is_xterm
5212 # endif
5215 hl = ScreenAttrs[off_to];
5216 if (hl > HL_ALL)
5217 hl = syn_attr2attr(hl);
5218 if (hl & HL_BOLD)
5219 redraw_next = TRUE;
5221 #endif
5222 ScreenAttrs[off_to] = ScreenAttrs[off_from];
5223 #ifdef FEAT_MBYTE
5224 /* For simplicity set the attributes of second half of a
5225 * double-wide character equal to the first half. */
5226 if (char_cells == 2)
5227 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
5229 if (enc_dbcs != 0 && char_cells == 2)
5230 screen_char_2(off_to, row, col + coloff);
5231 else
5232 #endif
5233 screen_char(off_to, row, col + coloff);
5235 else if ( p_wiv
5236 #ifdef FEAT_GUI
5237 && !gui.in_use
5238 #endif
5239 && col + coloff > 0)
5241 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
5244 * Don't output stop-highlight when moving the cursor, it will
5245 * stop the highlighting when it should continue.
5247 screen_attr = 0;
5249 else if (screen_attr != 0)
5250 screen_stop_highlight();
5253 off_to += CHAR_CELLS;
5254 off_from += CHAR_CELLS;
5255 col += CHAR_CELLS;
5258 #ifdef FEAT_MBYTE
5259 if (clear_next)
5261 /* Clear the second half of a double-wide character of which the left
5262 * half was overwritten with a single-wide character. */
5263 ScreenLines[off_to] = ' ';
5264 if (enc_utf8)
5265 ScreenLinesUC[off_to] = 0;
5266 screen_char(off_to, row, col + coloff);
5268 #endif
5270 if (clear_width > 0
5271 #ifdef FEAT_RIGHTLEFT
5272 && !rlflag
5273 #endif
5276 #ifdef FEAT_GUI
5277 int startCol = col;
5278 #endif
5280 /* blank out the rest of the line */
5281 while (col < clear_width && ScreenLines[off_to] == ' '
5282 && ScreenAttrs[off_to] == 0
5283 #ifdef FEAT_MBYTE
5284 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5285 #endif
5288 ++off_to;
5289 ++col;
5291 if (col < clear_width)
5293 #ifdef FEAT_GUI
5295 * In the GUI, clearing the rest of the line may leave pixels
5296 * behind if the first character cleared was bold. Some bold
5297 * fonts spill over the left. In this case we redraw the previous
5298 * character too. If we didn't skip any blanks above, then we
5299 * only redraw if the character wasn't already redrawn anyway.
5301 if (gui.in_use && (col > startCol || !redraw_this))
5303 hl = ScreenAttrs[off_to];
5304 if (hl > HL_ALL || (hl & HL_BOLD))
5306 int prev_cells = 1;
5307 # ifdef FEAT_MBYTE
5308 if (enc_utf8)
5309 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
5310 * that its width is 2. */
5311 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
5312 else if (enc_dbcs != 0)
5314 /* find previous character by counting from first
5315 * column and get its width. */
5316 unsigned off = LineOffset[row];
5317 unsigned max_off = LineOffset[row] + screen_Columns;
5319 while (off < off_to)
5321 prev_cells = (*mb_off2cells)(off, max_off);
5322 off += prev_cells;
5326 if (enc_dbcs != 0 && prev_cells > 1)
5327 screen_char_2(off_to - prev_cells, row,
5328 col + coloff - prev_cells);
5329 else
5330 # endif
5331 screen_char(off_to - prev_cells, row,
5332 col + coloff - prev_cells);
5335 #endif
5336 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
5337 ' ', ' ', 0);
5338 #ifdef FEAT_VERTSPLIT
5339 off_to += clear_width - col;
5340 col = clear_width;
5341 #endif
5345 if (clear_width > 0)
5347 #ifdef FEAT_VERTSPLIT
5348 /* For a window that's left of another, draw the separator char. */
5349 if (col + coloff < Columns)
5351 int c;
5353 c = fillchar_vsep(&hl);
5354 if (ScreenLines[off_to] != c
5355 # ifdef FEAT_MBYTE
5356 || (enc_utf8 && (int)ScreenLinesUC[off_to]
5357 != (c >= 0x80 ? c : 0))
5358 # endif
5359 || ScreenAttrs[off_to] != hl)
5361 ScreenLines[off_to] = c;
5362 ScreenAttrs[off_to] = hl;
5363 # ifdef FEAT_MBYTE
5364 if (enc_utf8)
5366 if (c >= 0x80)
5368 ScreenLinesUC[off_to] = c;
5369 ScreenLinesC[0][off_to] = 0;
5371 else
5372 ScreenLinesUC[off_to] = 0;
5374 # endif
5375 screen_char(off_to, row, col + coloff);
5378 else
5379 #endif
5380 LineWraps[row] = FALSE;
5384 #if defined(FEAT_RIGHTLEFT) || defined(PROTO)
5386 * Mirror text "str" for right-left displaying.
5387 * Only works for single-byte characters (e.g., numbers).
5389 void
5390 rl_mirror(str)
5391 char_u *str;
5393 char_u *p1, *p2;
5394 int t;
5396 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
5398 t = *p1;
5399 *p1 = *p2;
5400 *p2 = t;
5403 #endif
5405 #if defined(FEAT_WINDOWS) || defined(PROTO)
5407 * mark all status lines for redraw; used after first :cd
5409 void
5410 status_redraw_all()
5412 win_T *wp;
5414 for (wp = firstwin; wp; wp = wp->w_next)
5415 if (wp->w_status_height)
5417 wp->w_redr_status = TRUE;
5418 redraw_later(VALID);
5423 * mark all status lines of the current buffer for redraw
5425 void
5426 status_redraw_curbuf()
5428 win_T *wp;
5430 for (wp = firstwin; wp; wp = wp->w_next)
5431 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
5433 wp->w_redr_status = TRUE;
5434 redraw_later(VALID);
5439 * Redraw all status lines that need to be redrawn.
5441 void
5442 redraw_statuslines()
5444 win_T *wp;
5446 for (wp = firstwin; wp; wp = wp->w_next)
5447 if (wp->w_redr_status)
5448 win_redr_status(wp);
5449 if (redraw_tabline)
5450 draw_tabline();
5452 #endif
5454 #if (defined(FEAT_WILDMENU) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
5456 * Redraw all status lines at the bottom of frame "frp".
5458 void
5459 win_redraw_last_status(frp)
5460 frame_T *frp;
5462 if (frp->fr_layout == FR_LEAF)
5463 frp->fr_win->w_redr_status = TRUE;
5464 else if (frp->fr_layout == FR_ROW)
5466 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
5467 win_redraw_last_status(frp);
5469 else /* frp->fr_layout == FR_COL */
5471 frp = frp->fr_child;
5472 while (frp->fr_next != NULL)
5473 frp = frp->fr_next;
5474 win_redraw_last_status(frp);
5477 #endif
5479 #ifdef FEAT_VERTSPLIT
5481 * Draw the verticap separator right of window "wp" starting with line "row".
5483 static void
5484 draw_vsep_win(wp, row)
5485 win_T *wp;
5486 int row;
5488 int hl;
5489 int c;
5491 if (wp->w_vsep_width)
5493 /* draw the vertical separator right of this window */
5494 c = fillchar_vsep(&hl);
5495 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
5496 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
5497 c, ' ', hl);
5500 #endif
5502 #ifdef FEAT_WILDMENU
5503 static int status_match_len __ARGS((expand_T *xp, char_u *s));
5504 static int skip_status_match_char __ARGS((expand_T *xp, char_u *s));
5507 * Get the length of an item as it will be shown in the status line.
5509 static int
5510 status_match_len(xp, s)
5511 expand_T *xp;
5512 char_u *s;
5514 int len = 0;
5516 #ifdef FEAT_MENU
5517 int emenu = (xp->xp_context == EXPAND_MENUS
5518 || xp->xp_context == EXPAND_MENUNAMES);
5520 /* Check for menu separators - replace with '|'. */
5521 if (emenu && menu_is_separator(s))
5522 return 1;
5523 #endif
5525 while (*s != NUL)
5527 s += skip_status_match_char(xp, s);
5528 len += ptr2cells(s);
5529 mb_ptr_adv(s);
5532 return len;
5536 * Return the number of characters that should be skipped in a status match.
5537 * These are backslashes used for escaping. Do show backslashes in help tags.
5539 static int
5540 skip_status_match_char(xp, s)
5541 expand_T *xp;
5542 char_u *s;
5544 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
5545 #ifdef FEAT_MENU
5546 || ((xp->xp_context == EXPAND_MENUS
5547 || xp->xp_context == EXPAND_MENUNAMES)
5548 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
5549 #endif
5552 #ifndef BACKSLASH_IN_FILENAME
5553 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
5554 return 2;
5555 #endif
5556 return 1;
5558 return 0;
5562 * Show wildchar matches in the status line.
5563 * Show at least the "match" item.
5564 * We start at item 'first_match' in the list and show all matches that fit.
5566 * If inversion is possible we use it. Else '=' characters are used.
5568 void
5569 win_redr_status_matches(xp, num_matches, matches, match, showtail)
5570 expand_T *xp;
5571 int num_matches;
5572 char_u **matches; /* list of matches */
5573 int match;
5574 int showtail;
5576 #define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
5577 int row;
5578 char_u *buf;
5579 int len;
5580 int clen; /* length in screen cells */
5581 int fillchar;
5582 int attr;
5583 int i;
5584 int highlight = TRUE;
5585 char_u *selstart = NULL;
5586 int selstart_col = 0;
5587 char_u *selend = NULL;
5588 static int first_match = 0;
5589 int add_left = FALSE;
5590 char_u *s;
5591 #ifdef FEAT_MENU
5592 int emenu;
5593 #endif
5594 #if defined(FEAT_MBYTE) || defined(FEAT_MENU)
5595 int l;
5596 #endif
5598 if (matches == NULL) /* interrupted completion? */
5599 return;
5601 #ifdef FEAT_MBYTE
5602 if (has_mbyte)
5603 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
5604 else
5605 #endif
5606 buf = alloc((unsigned)Columns + 1);
5607 if (buf == NULL)
5608 return;
5610 if (match == -1) /* don't show match but original text */
5612 match = 0;
5613 highlight = FALSE;
5615 /* count 1 for the ending ">" */
5616 clen = status_match_len(xp, L_MATCH(match)) + 3;
5617 if (match == 0)
5618 first_match = 0;
5619 else if (match < first_match)
5621 /* jumping left, as far as we can go */
5622 first_match = match;
5623 add_left = TRUE;
5625 else
5627 /* check if match fits on the screen */
5628 for (i = first_match; i < match; ++i)
5629 clen += status_match_len(xp, L_MATCH(i)) + 2;
5630 if (first_match > 0)
5631 clen += 2;
5632 /* jumping right, put match at the left */
5633 if ((long)clen > Columns)
5635 first_match = match;
5636 /* if showing the last match, we can add some on the left */
5637 clen = 2;
5638 for (i = match; i < num_matches; ++i)
5640 clen += status_match_len(xp, L_MATCH(i)) + 2;
5641 if ((long)clen >= Columns)
5642 break;
5644 if (i == num_matches)
5645 add_left = TRUE;
5648 if (add_left)
5649 while (first_match > 0)
5651 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
5652 if ((long)clen >= Columns)
5653 break;
5654 --first_match;
5657 fillchar = fillchar_status(&attr, TRUE);
5659 if (first_match == 0)
5661 *buf = NUL;
5662 len = 0;
5664 else
5666 STRCPY(buf, "< ");
5667 len = 2;
5669 clen = len;
5671 i = first_match;
5672 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
5674 if (i == match)
5676 selstart = buf + len;
5677 selstart_col = clen;
5680 s = L_MATCH(i);
5681 /* Check for menu separators - replace with '|' */
5682 #ifdef FEAT_MENU
5683 emenu = (xp->xp_context == EXPAND_MENUS
5684 || xp->xp_context == EXPAND_MENUNAMES);
5685 if (emenu && menu_is_separator(s))
5687 STRCPY(buf + len, transchar('|'));
5688 l = (int)STRLEN(buf + len);
5689 len += l;
5690 clen += l;
5692 else
5693 #endif
5694 for ( ; *s != NUL; ++s)
5696 s += skip_status_match_char(xp, s);
5697 clen += ptr2cells(s);
5698 #ifdef FEAT_MBYTE
5699 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
5701 STRNCPY(buf + len, s, l);
5702 s += l - 1;
5703 len += l;
5705 else
5706 #endif
5708 STRCPY(buf + len, transchar_byte(*s));
5709 len += (int)STRLEN(buf + len);
5712 if (i == match)
5713 selend = buf + len;
5715 *(buf + len++) = ' ';
5716 *(buf + len++) = ' ';
5717 clen += 2;
5718 if (++i == num_matches)
5719 break;
5722 if (i != num_matches)
5724 *(buf + len++) = '>';
5725 ++clen;
5728 buf[len] = NUL;
5730 row = cmdline_row - 1;
5731 if (row >= 0)
5733 if (wild_menu_showing == 0)
5735 if (msg_scrolled > 0)
5737 /* Put the wildmenu just above the command line. If there is
5738 * no room, scroll the screen one line up. */
5739 if (cmdline_row == Rows - 1)
5741 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
5742 ++msg_scrolled;
5744 else
5746 ++cmdline_row;
5747 ++row;
5749 wild_menu_showing = WM_SCROLLED;
5751 else
5753 /* Create status line if needed by setting 'laststatus' to 2.
5754 * Set 'winminheight' to zero to avoid that the window is
5755 * resized. */
5756 if (lastwin->w_status_height == 0)
5758 save_p_ls = p_ls;
5759 save_p_wmh = p_wmh;
5760 p_ls = 2;
5761 p_wmh = 0;
5762 last_status(FALSE);
5764 wild_menu_showing = WM_SHOWN;
5768 screen_puts(buf, row, 0, attr);
5769 if (selstart != NULL && highlight)
5771 *selend = NUL;
5772 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
5775 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
5778 #ifdef FEAT_VERTSPLIT
5779 win_redraw_last_status(topframe);
5780 #else
5781 lastwin->w_redr_status = TRUE;
5782 #endif
5783 vim_free(buf);
5785 #endif
5787 #if defined(FEAT_WINDOWS) || defined(PROTO)
5789 * Redraw the status line of window wp.
5791 * If inversion is possible we use it. Else '=' characters are used.
5793 void
5794 win_redr_status(wp)
5795 win_T *wp;
5797 int row;
5798 char_u *p;
5799 int len;
5800 int fillchar;
5801 int attr;
5802 int this_ru_col;
5803 static int busy = FALSE;
5805 /* It's possible to get here recursively when 'statusline' (indirectly)
5806 * invokes ":redrawstatus". Simply ignore the call then. */
5807 if (busy)
5808 return;
5809 busy = TRUE;
5811 wp->w_redr_status = FALSE;
5812 if (wp->w_status_height == 0)
5814 /* no status line, can only be last window */
5815 redraw_cmdline = TRUE;
5817 else if (!redrawing()
5818 #ifdef FEAT_INS_EXPAND
5819 /* don't update status line when popup menu is visible and may be
5820 * drawn over it */
5821 || pum_visible()
5822 #endif
5825 /* Don't redraw right now, do it later. */
5826 wp->w_redr_status = TRUE;
5828 #ifdef FEAT_STL_OPT
5829 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
5831 /* redraw custom status line */
5832 redraw_custom_statusline(wp);
5834 #endif
5835 else
5837 fillchar = fillchar_status(&attr, wp == curwin);
5839 get_trans_bufname(wp->w_buffer);
5840 p = NameBuff;
5841 len = (int)STRLEN(p);
5843 if (wp->w_buffer->b_help
5844 #ifdef FEAT_QUICKFIX
5845 || wp->w_p_pvw
5846 #endif
5847 || bufIsChanged(wp->w_buffer)
5848 || wp->w_buffer->b_p_ro)
5849 *(p + len++) = ' ';
5850 if (wp->w_buffer->b_help)
5852 STRCPY(p + len, _("[Help]"));
5853 len += (int)STRLEN(p + len);
5855 #ifdef FEAT_QUICKFIX
5856 if (wp->w_p_pvw)
5858 STRCPY(p + len, _("[Preview]"));
5859 len += (int)STRLEN(p + len);
5861 #endif
5862 if (bufIsChanged(wp->w_buffer))
5864 STRCPY(p + len, "[+]");
5865 len += 3;
5867 if (wp->w_buffer->b_p_ro)
5869 STRCPY(p + len, "[RO]");
5870 len += 4;
5873 #ifndef FEAT_VERTSPLIT
5874 this_ru_col = ru_col;
5875 if (this_ru_col < (Columns + 1) / 2)
5876 this_ru_col = (Columns + 1) / 2;
5877 #else
5878 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
5879 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
5880 this_ru_col = (W_WIDTH(wp) + 1) / 2;
5881 if (this_ru_col <= 1)
5883 p = (char_u *)"<"; /* No room for file name! */
5884 len = 1;
5886 else
5887 #endif
5888 #ifdef FEAT_MBYTE
5889 if (has_mbyte)
5891 int clen = 0, i;
5893 /* Count total number of display cells. */
5894 for (i = 0; p[i] != NUL; i += (*mb_ptr2len)(p + i))
5895 clen += (*mb_ptr2cells)(p + i);
5896 /* Find first character that will fit.
5897 * Going from start to end is much faster for DBCS. */
5898 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
5899 i += (*mb_ptr2len)(p + i))
5900 clen -= (*mb_ptr2cells)(p + i);
5901 len = clen;
5902 if (i > 0)
5904 p = p + i - 1;
5905 *p = '<';
5906 ++len;
5910 else
5911 #endif
5912 if (len > this_ru_col - 1)
5914 p += len - (this_ru_col - 1);
5915 *p = '<';
5916 len = this_ru_col - 1;
5919 row = W_WINROW(wp) + wp->w_height;
5920 screen_puts(p, row, W_WINCOL(wp), attr);
5921 screen_fill(row, row + 1, len + W_WINCOL(wp),
5922 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
5924 if (get_keymap_str(wp, NameBuff, MAXPATHL)
5925 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
5926 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
5927 - 1 + W_WINCOL(wp)), attr);
5929 #ifdef FEAT_CMDL_INFO
5930 win_redr_ruler(wp, TRUE);
5931 #endif
5934 #ifdef FEAT_VERTSPLIT
5936 * May need to draw the character below the vertical separator.
5938 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
5940 if (stl_connected(wp))
5941 fillchar = fillchar_status(&attr, wp == curwin);
5942 else
5943 fillchar = fillchar_vsep(&attr);
5944 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
5945 attr);
5947 #endif
5948 busy = FALSE;
5951 #ifdef FEAT_STL_OPT
5953 * Redraw the status line according to 'statusline' and take care of any
5954 * errors encountered.
5956 static void
5957 redraw_custom_statusline(wp)
5958 win_T *wp;
5960 static int entered = FALSE;
5961 int save_called_emsg = called_emsg;
5963 /* When called recursively return. This can happen when the statusline
5964 * contains an expression that triggers a redraw. */
5965 if (entered)
5966 return;
5967 entered = TRUE;
5969 called_emsg = FALSE;
5970 win_redr_custom(wp, FALSE);
5971 if (called_emsg)
5973 /* When there is an error disable the statusline, otherwise the
5974 * display is messed up with errors and a redraw triggers the problem
5975 * again and again. */
5976 set_string_option_direct((char_u *)"statusline", -1,
5977 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
5978 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
5980 called_emsg |= save_called_emsg;
5981 entered = FALSE;
5983 #endif
5985 # ifdef FEAT_VERTSPLIT
5987 * Return TRUE if the status line of window "wp" is connected to the status
5988 * line of the window right of it. If not, then it's a vertical separator.
5989 * Only call if (wp->w_vsep_width != 0).
5992 stl_connected(wp)
5993 win_T *wp;
5995 frame_T *fr;
5997 fr = wp->w_frame;
5998 while (fr->fr_parent != NULL)
6000 if (fr->fr_parent->fr_layout == FR_COL)
6002 if (fr->fr_next != NULL)
6003 break;
6005 else
6007 if (fr->fr_next != NULL)
6008 return TRUE;
6010 fr = fr->fr_parent;
6012 return FALSE;
6014 # endif
6016 #endif /* FEAT_WINDOWS */
6018 #if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
6020 * Get the value to show for the language mappings, active 'keymap'.
6023 get_keymap_str(wp, buf, len)
6024 win_T *wp;
6025 char_u *buf; /* buffer for the result */
6026 int len; /* length of buffer */
6028 char_u *p;
6030 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
6031 return FALSE;
6034 #ifdef FEAT_EVAL
6035 buf_T *old_curbuf = curbuf;
6036 win_T *old_curwin = curwin;
6037 char_u *s;
6039 curbuf = wp->w_buffer;
6040 curwin = wp;
6041 STRCPY(buf, "b:keymap_name"); /* must be writable */
6042 ++emsg_skip;
6043 s = p = eval_to_string(buf, NULL, FALSE);
6044 --emsg_skip;
6045 curbuf = old_curbuf;
6046 curwin = old_curwin;
6047 if (p == NULL || *p == NUL)
6048 #endif
6050 #ifdef FEAT_KEYMAP
6051 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
6052 p = wp->w_buffer->b_p_keymap;
6053 else
6054 #endif
6055 p = (char_u *)"lang";
6057 if ((int)(STRLEN(p) + 3) < len)
6058 sprintf((char *)buf, "<%s>", p);
6059 else
6060 buf[0] = NUL;
6061 #ifdef FEAT_EVAL
6062 vim_free(s);
6063 #endif
6065 return buf[0] != NUL;
6067 #endif
6069 #if defined(FEAT_STL_OPT) || defined(PROTO)
6071 * Redraw the status line or ruler of window "wp".
6072 * When "wp" is NULL redraw the tab pages line from 'tabline'.
6074 static void
6075 win_redr_custom(wp, draw_ruler)
6076 win_T *wp;
6077 int draw_ruler; /* TRUE or FALSE */
6079 int attr;
6080 int curattr;
6081 int row;
6082 int col = 0;
6083 int maxwidth;
6084 int width;
6085 int n;
6086 int len;
6087 int fillchar;
6088 char_u buf[MAXPATHL];
6089 char_u *stl;
6090 char_u *p;
6091 struct stl_hlrec hltab[STL_MAX_ITEM];
6092 struct stl_hlrec tabtab[STL_MAX_ITEM];
6093 int use_sandbox = FALSE;
6095 /* setup environment for the task at hand */
6096 if (wp == NULL)
6098 /* Use 'tabline'. Always at the first line of the screen. */
6099 stl = p_tal;
6100 row = 0;
6101 fillchar = ' ';
6102 attr = hl_attr(HLF_TPF);
6103 maxwidth = Columns;
6104 # ifdef FEAT_EVAL
6105 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
6106 # endif
6108 else
6110 row = W_WINROW(wp) + wp->w_height;
6111 fillchar = fillchar_status(&attr, wp == curwin);
6112 maxwidth = W_WIDTH(wp);
6114 if (draw_ruler)
6116 stl = p_ruf;
6117 /* advance past any leading group spec - implicit in ru_col */
6118 if (*stl == '%')
6120 if (*++stl == '-')
6121 stl++;
6122 if (atoi((char *)stl))
6123 while (VIM_ISDIGIT(*stl))
6124 stl++;
6125 if (*stl++ != '(')
6126 stl = p_ruf;
6128 #ifdef FEAT_VERTSPLIT
6129 col = ru_col - (Columns - W_WIDTH(wp));
6130 if (col < (W_WIDTH(wp) + 1) / 2)
6131 col = (W_WIDTH(wp) + 1) / 2;
6132 #else
6133 col = ru_col;
6134 if (col > (Columns + 1) / 2)
6135 col = (Columns + 1) / 2;
6136 #endif
6137 maxwidth = W_WIDTH(wp) - col;
6138 #ifdef FEAT_WINDOWS
6139 if (!wp->w_status_height)
6140 #endif
6142 row = Rows - 1;
6143 --maxwidth; /* writing in last column may cause scrolling */
6144 fillchar = ' ';
6145 attr = 0;
6148 # ifdef FEAT_EVAL
6149 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
6150 # endif
6152 else
6154 if (*wp->w_p_stl != NUL)
6155 stl = wp->w_p_stl;
6156 else
6157 stl = p_stl;
6158 # ifdef FEAT_EVAL
6159 use_sandbox = was_set_insecurely((char_u *)"statusline",
6160 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
6161 # endif
6164 #ifdef FEAT_VERTSPLIT
6165 col += W_WINCOL(wp);
6166 #endif
6169 if (maxwidth <= 0)
6170 return;
6172 /* Make a copy, because the statusline may include a function call that
6173 * might change the option value and free the memory. */
6174 stl = vim_strsave(stl);
6175 width = build_stl_str_hl(wp == NULL ? curwin : wp,
6176 buf, sizeof(buf),
6177 stl, use_sandbox,
6178 fillchar, maxwidth, hltab, tabtab);
6179 vim_free(stl);
6180 len = (int)STRLEN(buf);
6182 while (width < maxwidth && len < (int)sizeof(buf) - 1)
6184 #ifdef FEAT_MBYTE
6185 len += (*mb_char2bytes)(fillchar, buf + len);
6186 #else
6187 buf[len++] = fillchar;
6188 #endif
6189 ++width;
6191 buf[len] = NUL;
6194 * Draw each snippet with the specified highlighting.
6196 curattr = attr;
6197 p = buf;
6198 for (n = 0; hltab[n].start != NULL; n++)
6200 len = (int)(hltab[n].start - p);
6201 screen_puts_len(p, len, row, col, curattr);
6202 col += vim_strnsize(p, len);
6203 p = hltab[n].start;
6205 if (hltab[n].userhl == 0)
6206 curattr = attr;
6207 else if (hltab[n].userhl < 0)
6208 curattr = syn_id2attr(-hltab[n].userhl);
6209 #ifdef FEAT_WINDOWS
6210 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
6211 curattr = highlight_stlnc[hltab[n].userhl - 1];
6212 #endif
6213 else
6214 curattr = highlight_user[hltab[n].userhl - 1];
6216 screen_puts(p, row, col, curattr);
6218 if (wp == NULL)
6220 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
6221 col = 0;
6222 len = 0;
6223 p = buf;
6224 fillchar = 0;
6225 for (n = 0; tabtab[n].start != NULL; n++)
6227 len += vim_strnsize(p, (int)(tabtab[n].start - p));
6228 while (col < len)
6229 TabPageIdxs[col++] = fillchar;
6230 p = tabtab[n].start;
6231 fillchar = tabtab[n].userhl;
6233 while (col < Columns)
6234 TabPageIdxs[col++] = fillchar;
6238 #endif /* FEAT_STL_OPT */
6241 * Output a single character directly to the screen and update ScreenLines.
6243 void
6244 screen_putchar(c, row, col, attr)
6245 int c;
6246 int row, col;
6247 int attr;
6249 #ifdef FEAT_MBYTE
6250 char_u buf[MB_MAXBYTES + 1];
6252 buf[(*mb_char2bytes)(c, buf)] = NUL;
6253 #else
6254 char_u buf[2];
6256 buf[0] = c;
6257 buf[1] = NUL;
6258 #endif
6259 screen_puts(buf, row, col, attr);
6263 * Get a single character directly from ScreenLines into "bytes[]".
6264 * Also return its attribute in *attrp;
6266 void
6267 screen_getbytes(row, col, bytes, attrp)
6268 int row, col;
6269 char_u *bytes;
6270 int *attrp;
6272 unsigned off;
6274 /* safety check */
6275 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
6277 off = LineOffset[row] + col;
6278 *attrp = ScreenAttrs[off];
6279 bytes[0] = ScreenLines[off];
6280 bytes[1] = NUL;
6282 #ifdef FEAT_MBYTE
6283 if (enc_utf8 && ScreenLinesUC[off] != 0)
6284 bytes[utfc_char2bytes(off, bytes)] = NUL;
6285 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6287 bytes[0] = ScreenLines[off];
6288 bytes[1] = ScreenLines2[off];
6289 bytes[2] = NUL;
6291 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
6293 bytes[1] = ScreenLines[off + 1];
6294 bytes[2] = NUL;
6296 #endif
6300 #ifdef FEAT_MBYTE
6301 static int screen_comp_differs __ARGS((int, int*));
6304 * Return TRUE if composing characters for screen posn "off" differs from
6305 * composing characters in "u8cc".
6306 * Only to be used when ScreenLinesUC[off] != 0.
6308 static int
6309 screen_comp_differs(off, u8cc)
6310 int off;
6311 int *u8cc;
6313 int i;
6315 for (i = 0; i < Screen_mco; ++i)
6317 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
6318 return TRUE;
6319 if (u8cc[i] == 0)
6320 break;
6322 return FALSE;
6324 #endif
6327 * Put string '*text' on the screen at position 'row' and 'col', with
6328 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
6329 * Note: only outputs within one row, message is truncated at screen boundary!
6330 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
6332 void
6333 screen_puts(text, row, col, attr)
6334 char_u *text;
6335 int row;
6336 int col;
6337 int attr;
6339 screen_puts_len(text, -1, row, col, attr);
6343 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
6344 * a NUL.
6346 void
6347 screen_puts_len(text, len, row, col, attr)
6348 char_u *text;
6349 int len;
6350 int row;
6351 int col;
6352 int attr;
6354 unsigned off;
6355 char_u *ptr = text;
6356 int c;
6357 #ifdef FEAT_MBYTE
6358 unsigned max_off;
6359 int mbyte_blen = 1;
6360 int mbyte_cells = 1;
6361 int u8c = 0;
6362 int u8cc[MAX_MCO];
6363 int clear_next_cell = FALSE;
6364 # ifdef FEAT_ARABIC
6365 int prev_c = 0; /* previous Arabic character */
6366 int pc, nc, nc1;
6367 int pcc[MAX_MCO];
6368 # endif
6369 #endif
6370 #if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6371 int force_redraw_this;
6372 int force_redraw_next = FALSE;
6373 #endif
6374 int need_redraw;
6376 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
6377 return;
6378 off = LineOffset[row] + col;
6380 #ifdef FEAT_MBYTE
6381 /* When drawing over the right halve of a double-wide char clear out the
6382 * left halve. Only needed in a terminal. */
6383 if (has_mbyte && col > 0 && col < screen_Columns
6384 # ifdef FEAT_GUI
6385 && !gui.in_use
6386 # endif
6387 && mb_fix_col(col, row) != col)
6389 ScreenLines[off - 1] = ' ';
6390 ScreenAttrs[off - 1] = 0;
6391 if (enc_utf8)
6393 ScreenLinesUC[off - 1] = 0;
6394 ScreenLinesC[0][off - 1] = 0;
6396 /* redraw the previous cell, make it empty */
6397 screen_char(off - 1, row, col - 1);
6398 /* force the cell at "col" to be redrawn */
6399 force_redraw_next = TRUE;
6401 #endif
6403 #ifdef FEAT_MBYTE
6404 max_off = LineOffset[row] + screen_Columns;
6405 #endif
6406 while (col < screen_Columns
6407 && (len < 0 || (int)(ptr - text) < len)
6408 && *ptr != NUL)
6410 c = *ptr;
6411 #ifdef FEAT_MBYTE
6412 /* check if this is the first byte of a multibyte */
6413 if (has_mbyte)
6415 if (enc_utf8 && len > 0)
6416 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
6417 else
6418 mbyte_blen = (*mb_ptr2len)(ptr);
6419 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6420 mbyte_cells = 1;
6421 else if (enc_dbcs != 0)
6422 mbyte_cells = mbyte_blen;
6423 else /* enc_utf8 */
6425 if (len >= 0)
6426 u8c = utfc_ptr2char_len(ptr, u8cc,
6427 (int)((text + len) - ptr));
6428 else
6429 u8c = utfc_ptr2char(ptr, u8cc);
6430 mbyte_cells = utf_char2cells(u8c);
6431 # ifdef UNICODE16
6432 /* Non-BMP character: display as ? or fullwidth ?. */
6433 if (u8c >= 0x10000)
6435 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
6436 if (attr == 0)
6437 attr = hl_attr(HLF_8);
6439 # endif
6440 # ifdef FEAT_ARABIC
6441 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
6443 /* Do Arabic shaping. */
6444 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
6446 /* Past end of string to be displayed. */
6447 nc = NUL;
6448 nc1 = NUL;
6450 else
6452 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
6453 (int)((text + len) - ptr - mbyte_blen));
6454 nc1 = pcc[0];
6456 pc = prev_c;
6457 prev_c = u8c;
6458 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
6460 else
6461 prev_c = u8c;
6462 # endif
6463 if (col + mbyte_cells > screen_Columns)
6465 /* Only 1 cell left, but character requires 2 cells:
6466 * display a '>' in the last column to avoid wrapping. */
6467 c = '>';
6468 mbyte_cells = 1;
6472 #endif
6474 #if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6475 force_redraw_this = force_redraw_next;
6476 force_redraw_next = FALSE;
6477 #endif
6479 need_redraw = ScreenLines[off] != c
6480 #ifdef FEAT_MBYTE
6481 || (mbyte_cells == 2
6482 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
6483 || (enc_dbcs == DBCS_JPNU
6484 && c == 0x8e
6485 && ScreenLines2[off] != ptr[1])
6486 || (enc_utf8
6487 && (ScreenLinesUC[off] !=
6488 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
6489 || (ScreenLinesUC[off] != 0
6490 && screen_comp_differs(off, u8cc))))
6491 #endif
6492 || ScreenAttrs[off] != attr
6493 || exmode_active;
6495 if (need_redraw
6496 #if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6497 || force_redraw_this
6498 #endif
6501 #if defined(FEAT_GUI) || defined(UNIX)
6502 /* The bold trick makes a single row of pixels appear in the next
6503 * character. When a bold character is removed, the next
6504 * character should be redrawn too. This happens for our own GUI
6505 * and for some xterms. */
6506 if (need_redraw && ScreenLines[off] != ' ' && (
6507 # ifdef FEAT_GUI
6508 gui.in_use
6509 # endif
6510 # if defined(FEAT_GUI) && defined(UNIX)
6512 # endif
6513 # ifdef UNIX
6514 term_is_xterm
6515 # endif
6518 int n = ScreenAttrs[off];
6520 if (n > HL_ALL)
6521 n = syn_attr2attr(n);
6522 if (n & HL_BOLD)
6523 force_redraw_next = TRUE;
6525 #endif
6526 #ifdef FEAT_MBYTE
6527 /* When at the end of the text and overwriting a two-cell
6528 * character with a one-cell character, need to clear the next
6529 * cell. Also when overwriting the left halve of a two-cell char
6530 * with the right halve of a two-cell char. Do this only once
6531 * (mb_off2cells() may return 2 on the right halve). */
6532 if (clear_next_cell)
6533 clear_next_cell = FALSE;
6534 else if (has_mbyte
6535 && (len < 0 ? ptr[mbyte_blen] == NUL
6536 : ptr + mbyte_blen >= text + len)
6537 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
6538 || (mbyte_cells == 2
6539 && (*mb_off2cells)(off, max_off) == 1
6540 && (*mb_off2cells)(off + 1, max_off) > 1)))
6541 clear_next_cell = TRUE;
6543 /* Make sure we never leave a second byte of a double-byte behind,
6544 * it confuses mb_off2cells(). */
6545 if (enc_dbcs
6546 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
6547 || (mbyte_cells == 2
6548 && (*mb_off2cells)(off, max_off) == 1
6549 && (*mb_off2cells)(off + 1, max_off) > 1)))
6550 ScreenLines[off + mbyte_blen] = 0;
6551 #endif
6552 ScreenLines[off] = c;
6553 ScreenAttrs[off] = attr;
6554 #ifdef FEAT_MBYTE
6555 if (enc_utf8)
6557 if (c < 0x80 && u8cc[0] == 0)
6558 ScreenLinesUC[off] = 0;
6559 else
6561 int i;
6563 ScreenLinesUC[off] = u8c;
6564 for (i = 0; i < Screen_mco; ++i)
6566 ScreenLinesC[i][off] = u8cc[i];
6567 if (u8cc[i] == 0)
6568 break;
6571 if (mbyte_cells == 2)
6573 ScreenLines[off + 1] = 0;
6574 ScreenAttrs[off + 1] = attr;
6576 screen_char(off, row, col);
6578 else if (mbyte_cells == 2)
6580 ScreenLines[off + 1] = ptr[1];
6581 ScreenAttrs[off + 1] = attr;
6582 screen_char_2(off, row, col);
6584 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6586 ScreenLines2[off] = ptr[1];
6587 screen_char(off, row, col);
6589 else
6590 #endif
6591 screen_char(off, row, col);
6593 #ifdef FEAT_MBYTE
6594 if (has_mbyte)
6596 off += mbyte_cells;
6597 col += mbyte_cells;
6598 ptr += mbyte_blen;
6599 if (clear_next_cell)
6600 ptr = (char_u *)" ";
6602 else
6603 #endif
6605 ++off;
6606 ++col;
6607 ++ptr;
6611 #if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6612 /* If we detected the next character needs to be redrawn, but the text
6613 * doesn't extend up to there, update the character here. */
6614 if (force_redraw_next && col < screen_Columns)
6616 # ifdef FEAT_MBYTE
6617 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
6618 screen_char_2(off, row, col);
6619 else
6620 # endif
6621 screen_char(off, row, col);
6623 #endif
6626 #ifdef FEAT_SEARCH_EXTRA
6628 * Prepare for 'hlsearch' highlighting.
6630 static void
6631 start_search_hl()
6633 if (p_hls && !no_hlsearch)
6635 last_pat_prog(&search_hl.rm);
6636 search_hl.attr = hl_attr(HLF_L);
6637 # ifdef FEAT_RELTIME
6638 /* Set the time limit to 'redrawtime'. */
6639 profile_setlimit(p_rdt, &search_hl.tm);
6640 # endif
6645 * Clean up for 'hlsearch' highlighting.
6647 static void
6648 end_search_hl()
6650 if (search_hl.rm.regprog != NULL)
6652 vim_free(search_hl.rm.regprog);
6653 search_hl.rm.regprog = NULL;
6658 * Advance to the match in window "wp" line "lnum" or past it.
6660 static void
6661 prepare_search_hl(wp, lnum)
6662 win_T *wp;
6663 linenr_T lnum;
6665 matchitem_T *cur; /* points to the match list */
6666 match_T *shl; /* points to search_hl or a match */
6667 int shl_flag; /* flag to indicate whether search_hl
6668 has been processed or not */
6669 int n;
6672 * When using a multi-line pattern, start searching at the top
6673 * of the window or just after a closed fold.
6674 * Do this both for search_hl and the match list.
6676 cur = wp->w_match_head;
6677 shl_flag = FALSE;
6678 while (cur != NULL || shl_flag == FALSE)
6680 if (shl_flag == FALSE)
6682 shl = &search_hl;
6683 shl_flag = TRUE;
6685 else
6686 shl = &cur->hl;
6687 if (shl->rm.regprog != NULL
6688 && shl->lnum == 0
6689 && re_multiline(shl->rm.regprog))
6691 if (shl->first_lnum == 0)
6693 # ifdef FEAT_FOLDING
6694 for (shl->first_lnum = lnum;
6695 shl->first_lnum > wp->w_topline; --shl->first_lnum)
6696 if (hasFoldingWin(wp, shl->first_lnum - 1,
6697 NULL, NULL, TRUE, NULL))
6698 break;
6699 # else
6700 shl->first_lnum = wp->w_topline;
6701 # endif
6703 n = 0;
6704 while (shl->first_lnum < lnum && shl->rm.regprog != NULL)
6706 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n);
6707 if (shl->lnum != 0)
6709 shl->first_lnum = shl->lnum
6710 + shl->rm.endpos[0].lnum
6711 - shl->rm.startpos[0].lnum;
6712 n = shl->rm.endpos[0].col;
6714 else
6716 ++shl->first_lnum;
6717 n = 0;
6721 if (shl != &search_hl && cur != NULL)
6722 cur = cur->next;
6727 * Search for a next 'hlsearch' or match.
6728 * Uses shl->buf.
6729 * Sets shl->lnum and shl->rm contents.
6730 * Note: Assumes a previous match is always before "lnum", unless
6731 * shl->lnum is zero.
6732 * Careful: Any pointers for buffer lines will become invalid.
6734 static void
6735 next_search_hl(win, shl, lnum, mincol)
6736 win_T *win;
6737 match_T *shl; /* points to search_hl or a match */
6738 linenr_T lnum;
6739 colnr_T mincol; /* minimal column for a match */
6741 linenr_T l;
6742 colnr_T matchcol;
6743 long nmatched;
6745 if (shl->lnum != 0)
6747 /* Check for three situations:
6748 * 1. If the "lnum" is below a previous match, start a new search.
6749 * 2. If the previous match includes "mincol", use it.
6750 * 3. Continue after the previous match.
6752 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
6753 if (lnum > l)
6754 shl->lnum = 0;
6755 else if (lnum < l || shl->rm.endpos[0].col > mincol)
6756 return;
6760 * Repeat searching for a match until one is found that includes "mincol"
6761 * or none is found in this line.
6763 called_emsg = FALSE;
6764 for (;;)
6766 #ifdef FEAT_RELTIME
6767 /* Stop searching after passing the time limit. */
6768 if (profile_passed_limit(&(shl->tm)))
6770 shl->lnum = 0; /* no match found in time */
6771 break;
6773 #endif
6774 /* Three situations:
6775 * 1. No useful previous match: search from start of line.
6776 * 2. Not Vi compatible or empty match: continue at next character.
6777 * Break the loop if this is beyond the end of the line.
6778 * 3. Vi compatible searching: continue at end of previous match.
6780 if (shl->lnum == 0)
6781 matchcol = 0;
6782 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
6783 || (shl->rm.endpos[0].lnum == 0
6784 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
6786 char_u *ml;
6788 matchcol = shl->rm.startpos[0].col;
6789 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
6790 if (*ml == NUL)
6792 ++matchcol;
6793 shl->lnum = 0;
6794 break;
6796 #ifdef FEAT_MBYTE
6797 if (has_mbyte)
6798 matchcol += mb_ptr2len(ml);
6799 else
6800 #endif
6801 ++matchcol;
6803 else
6804 matchcol = shl->rm.endpos[0].col;
6806 shl->lnum = lnum;
6807 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum, matchcol,
6808 #ifdef FEAT_RELTIME
6809 &(shl->tm)
6810 #else
6811 NULL
6812 #endif
6814 if (called_emsg)
6816 /* Error while handling regexp: stop using this regexp. */
6817 if (shl == &search_hl)
6819 /* don't free regprog in the match list, it's a copy */
6820 vim_free(shl->rm.regprog);
6821 no_hlsearch = TRUE;
6823 shl->rm.regprog = NULL;
6824 shl->lnum = 0;
6825 got_int = FALSE; /* avoid the "Type :quit to exit Vim" message */
6826 break;
6828 if (nmatched == 0)
6830 shl->lnum = 0; /* no match found */
6831 break;
6833 if (shl->rm.startpos[0].lnum > 0
6834 || shl->rm.startpos[0].col >= mincol
6835 || nmatched > 1
6836 || shl->rm.endpos[0].col > mincol)
6838 shl->lnum += shl->rm.startpos[0].lnum;
6839 break; /* useful match found */
6843 #endif
6845 static void
6846 screen_start_highlight(attr)
6847 int attr;
6849 attrentry_T *aep = NULL;
6851 screen_attr = attr;
6852 if (full_screen
6853 #ifdef WIN3264
6854 && termcap_active
6855 #endif
6858 #ifdef FEAT_GUI
6859 if (gui.in_use)
6861 char buf[20];
6863 /* The GUI handles this internally. */
6864 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
6865 OUT_STR(buf);
6867 else
6868 #endif
6870 if (attr > HL_ALL) /* special HL attr. */
6872 if (t_colors > 1)
6873 aep = syn_cterm_attr2entry(attr);
6874 else
6875 aep = syn_term_attr2entry(attr);
6876 if (aep == NULL) /* did ":syntax clear" */
6877 attr = 0;
6878 else
6879 attr = aep->ae_attr;
6881 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
6882 out_str(T_MD);
6883 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
6884 && cterm_normal_fg_bold)
6885 /* If the Normal FG color has BOLD attribute and the new HL
6886 * has a FG color defined, clear BOLD. */
6887 out_str(T_ME);
6888 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
6889 out_str(T_SO);
6890 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
6891 /* underline or undercurl */
6892 out_str(T_US);
6893 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
6894 out_str(T_CZH);
6895 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
6896 out_str(T_MR);
6899 * Output the color or start string after bold etc., in case the
6900 * bold etc. override the color setting.
6902 if (aep != NULL)
6904 if (t_colors > 1)
6906 if (aep->ae_u.cterm.fg_color)
6907 term_fg_color(aep->ae_u.cterm.fg_color - 1);
6908 if (aep->ae_u.cterm.bg_color)
6909 term_bg_color(aep->ae_u.cterm.bg_color - 1);
6911 else
6913 if (aep->ae_u.term.start != NULL)
6914 out_str(aep->ae_u.term.start);
6921 void
6922 screen_stop_highlight()
6924 int do_ME = FALSE; /* output T_ME code */
6926 if (screen_attr != 0
6927 #ifdef WIN3264
6928 && termcap_active
6929 #endif
6932 #ifdef FEAT_GUI
6933 if (gui.in_use)
6935 char buf[20];
6937 /* use internal GUI code */
6938 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
6939 OUT_STR(buf);
6941 else
6942 #endif
6944 if (screen_attr > HL_ALL) /* special HL attr. */
6946 attrentry_T *aep;
6948 if (t_colors > 1)
6951 * Assume that t_me restores the original colors!
6953 aep = syn_cterm_attr2entry(screen_attr);
6954 if (aep != NULL && (aep->ae_u.cterm.fg_color
6955 || aep->ae_u.cterm.bg_color))
6956 do_ME = TRUE;
6958 else
6960 aep = syn_term_attr2entry(screen_attr);
6961 if (aep != NULL && aep->ae_u.term.stop != NULL)
6963 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
6964 do_ME = TRUE;
6965 else
6966 out_str(aep->ae_u.term.stop);
6969 if (aep == NULL) /* did ":syntax clear" */
6970 screen_attr = 0;
6971 else
6972 screen_attr = aep->ae_attr;
6976 * Often all ending-codes are equal to T_ME. Avoid outputting the
6977 * same sequence several times.
6979 if (screen_attr & HL_STANDOUT)
6981 if (STRCMP(T_SE, T_ME) == 0)
6982 do_ME = TRUE;
6983 else
6984 out_str(T_SE);
6986 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
6988 if (STRCMP(T_UE, T_ME) == 0)
6989 do_ME = TRUE;
6990 else
6991 out_str(T_UE);
6993 if (screen_attr & HL_ITALIC)
6995 if (STRCMP(T_CZR, T_ME) == 0)
6996 do_ME = TRUE;
6997 else
6998 out_str(T_CZR);
7000 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
7001 out_str(T_ME);
7003 if (t_colors > 1)
7005 /* set Normal cterm colors */
7006 if (cterm_normal_fg_color != 0)
7007 term_fg_color(cterm_normal_fg_color - 1);
7008 if (cterm_normal_bg_color != 0)
7009 term_bg_color(cterm_normal_bg_color - 1);
7010 if (cterm_normal_fg_bold)
7011 out_str(T_MD);
7015 screen_attr = 0;
7019 * Reset the colors for a cterm. Used when leaving Vim.
7020 * The machine specific code may override this again.
7022 void
7023 reset_cterm_colors()
7025 if (t_colors > 1)
7027 /* set Normal cterm colors */
7028 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
7030 out_str(T_OP);
7031 screen_attr = -1;
7033 if (cterm_normal_fg_bold)
7035 out_str(T_ME);
7036 screen_attr = -1;
7042 * Put character ScreenLines["off"] on the screen at position "row" and "col",
7043 * using the attributes from ScreenAttrs["off"].
7045 static void
7046 screen_char(off, row, col)
7047 unsigned off;
7048 int row;
7049 int col;
7051 int attr;
7053 /* Check for illegal values, just in case (could happen just after
7054 * resizing). */
7055 if (row >= screen_Rows || col >= screen_Columns)
7056 return;
7058 /* Outputting the last character on the screen may scrollup the screen.
7059 * Don't to it! Mark the character invalid (update it when scrolled up) */
7060 if (row == screen_Rows - 1 && col == screen_Columns - 1
7061 #ifdef FEAT_RIGHTLEFT
7062 /* account for first command-line character in rightleft mode */
7063 && !cmdmsg_rl
7064 #endif
7067 ScreenAttrs[off] = (sattr_T)-1;
7068 return;
7072 * Stop highlighting first, so it's easier to move the cursor.
7074 #if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
7075 if (screen_char_attr != 0)
7076 attr = screen_char_attr;
7077 else
7078 #endif
7079 attr = ScreenAttrs[off];
7080 if (screen_attr != attr)
7081 screen_stop_highlight();
7083 windgoto(row, col);
7085 if (screen_attr != attr)
7086 screen_start_highlight(attr);
7088 #ifdef FEAT_MBYTE
7089 if (enc_utf8 && ScreenLinesUC[off] != 0)
7091 char_u buf[MB_MAXBYTES + 1];
7093 /* Convert UTF-8 character to bytes and write it. */
7095 buf[utfc_char2bytes(off, buf)] = NUL;
7097 out_str(buf);
7098 if (utf_char2cells(ScreenLinesUC[off]) > 1)
7099 ++screen_cur_col;
7101 else
7102 #endif
7104 #ifdef FEAT_MBYTE
7105 out_flush_check();
7106 #endif
7107 out_char(ScreenLines[off]);
7108 #ifdef FEAT_MBYTE
7109 /* double-byte character in single-width cell */
7110 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7111 out_char(ScreenLines2[off]);
7112 #endif
7115 screen_cur_col++;
7118 #ifdef FEAT_MBYTE
7121 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
7122 * on the screen at position 'row' and 'col'.
7123 * The attributes of the first byte is used for all. This is required to
7124 * output the two bytes of a double-byte character with nothing in between.
7126 static void
7127 screen_char_2(off, row, col)
7128 unsigned off;
7129 int row;
7130 int col;
7132 /* Check for illegal values (could be wrong when screen was resized). */
7133 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
7134 return;
7136 /* Outputting the last character on the screen may scrollup the screen.
7137 * Don't to it! Mark the character invalid (update it when scrolled up) */
7138 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
7140 ScreenAttrs[off] = (sattr_T)-1;
7141 return;
7144 /* Output the first byte normally (positions the cursor), then write the
7145 * second byte directly. */
7146 screen_char(off, row, col);
7147 out_char(ScreenLines[off + 1]);
7148 ++screen_cur_col;
7150 #endif
7152 #if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
7154 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
7155 * This uses the contents of ScreenLines[] and doesn't change it.
7157 void
7158 screen_draw_rectangle(row, col, height, width, invert)
7159 int row;
7160 int col;
7161 int height;
7162 int width;
7163 int invert;
7165 int r, c;
7166 int off;
7167 #ifdef FEAT_MBYTE
7168 int max_off;
7169 #endif
7171 /* Can't use ScreenLines unless initialized */
7172 if (ScreenLines == NULL)
7173 return;
7175 if (invert)
7176 screen_char_attr = HL_INVERSE;
7177 for (r = row; r < row + height; ++r)
7179 off = LineOffset[r];
7180 #ifdef FEAT_MBYTE
7181 max_off = off + screen_Columns;
7182 #endif
7183 for (c = col; c < col + width; ++c)
7185 #ifdef FEAT_MBYTE
7186 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
7188 screen_char_2(off + c, r, c);
7189 ++c;
7191 else
7192 #endif
7194 screen_char(off + c, r, c);
7195 #ifdef FEAT_MBYTE
7196 if (utf_off2cells(off + c, max_off) > 1)
7197 ++c;
7198 #endif
7202 screen_char_attr = 0;
7204 #endif
7206 #ifdef FEAT_VERTSPLIT
7208 * Redraw the characters for a vertically split window.
7210 static void
7211 redraw_block(row, end, wp)
7212 int row;
7213 int end;
7214 win_T *wp;
7216 int col;
7217 int width;
7219 # ifdef FEAT_CLIPBOARD
7220 clip_may_clear_selection(row, end - 1);
7221 # endif
7223 if (wp == NULL)
7225 col = 0;
7226 width = Columns;
7228 else
7230 col = wp->w_wincol;
7231 width = wp->w_width;
7233 screen_draw_rectangle(row, col, end - row, width, FALSE);
7235 #endif
7238 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
7239 * with character 'c1' in first column followed by 'c2' in the other columns.
7240 * Use attributes 'attr'.
7242 void
7243 screen_fill(start_row, end_row, start_col, end_col, c1, c2, attr)
7244 int start_row, end_row;
7245 int start_col, end_col;
7246 int c1, c2;
7247 int attr;
7249 int row;
7250 int col;
7251 int off;
7252 int end_off;
7253 int did_delete;
7254 int c;
7255 int norm_term;
7256 #if defined(FEAT_GUI) || defined(UNIX)
7257 int force_next = FALSE;
7258 #endif
7260 if (end_row > screen_Rows) /* safety check */
7261 end_row = screen_Rows;
7262 if (end_col > screen_Columns) /* safety check */
7263 end_col = screen_Columns;
7264 if (ScreenLines == NULL
7265 || start_row >= end_row
7266 || start_col >= end_col) /* nothing to do */
7267 return;
7269 /* it's a "normal" terminal when not in a GUI or cterm */
7270 norm_term = (
7271 #ifdef FEAT_GUI
7272 !gui.in_use &&
7273 #endif
7274 t_colors <= 1);
7275 for (row = start_row; row < end_row; ++row)
7277 #ifdef FEAT_MBYTE
7278 if (has_mbyte
7279 # ifdef FEAT_GUI
7280 && !gui.in_use
7281 # endif
7284 /* When drawing over the right halve of a double-wide char clear
7285 * out the left halve. When drawing over the left halve of a
7286 * double wide-char clear out the right halve. Only needed in a
7287 * terminal. */
7288 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
7289 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
7290 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
7291 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
7293 #endif
7295 * Try to use delete-line termcap code, when no attributes or in a
7296 * "normal" terminal, where a bold/italic space is just a
7297 * space.
7299 did_delete = FALSE;
7300 if (c2 == ' '
7301 && end_col == Columns
7302 && can_clear(T_CE)
7303 && (attr == 0
7304 || (norm_term
7305 && attr <= HL_ALL
7306 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
7309 * check if we really need to clear something
7311 col = start_col;
7312 if (c1 != ' ') /* don't clear first char */
7313 ++col;
7315 off = LineOffset[row] + col;
7316 end_off = LineOffset[row] + end_col;
7318 /* skip blanks (used often, keep it fast!) */
7319 #ifdef FEAT_MBYTE
7320 if (enc_utf8)
7321 while (off < end_off && ScreenLines[off] == ' '
7322 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
7323 ++off;
7324 else
7325 #endif
7326 while (off < end_off && ScreenLines[off] == ' '
7327 && ScreenAttrs[off] == 0)
7328 ++off;
7329 if (off < end_off) /* something to be cleared */
7331 col = off - LineOffset[row];
7332 screen_stop_highlight();
7333 term_windgoto(row, col);/* clear rest of this screen line */
7334 out_str(T_CE);
7335 screen_start(); /* don't know where cursor is now */
7336 col = end_col - col;
7337 while (col--) /* clear chars in ScreenLines */
7339 ScreenLines[off] = ' ';
7340 #ifdef FEAT_MBYTE
7341 if (enc_utf8)
7342 ScreenLinesUC[off] = 0;
7343 #endif
7344 ScreenAttrs[off] = 0;
7345 ++off;
7348 did_delete = TRUE; /* the chars are cleared now */
7351 off = LineOffset[row] + start_col;
7352 c = c1;
7353 for (col = start_col; col < end_col; ++col)
7355 if (ScreenLines[off] != c
7356 #ifdef FEAT_MBYTE
7357 || (enc_utf8 && (int)ScreenLinesUC[off]
7358 != (c >= 0x80 ? c : 0))
7359 #endif
7360 || ScreenAttrs[off] != attr
7361 #if defined(FEAT_GUI) || defined(UNIX)
7362 || force_next
7363 #endif
7366 #if defined(FEAT_GUI) || defined(UNIX)
7367 /* The bold trick may make a single row of pixels appear in
7368 * the next character. When a bold character is removed, the
7369 * next character should be redrawn too. This happens for our
7370 * own GUI and for some xterms. */
7371 if (
7372 # ifdef FEAT_GUI
7373 gui.in_use
7374 # endif
7375 # if defined(FEAT_GUI) && defined(UNIX)
7377 # endif
7378 # ifdef UNIX
7379 term_is_xterm
7380 # endif
7383 if (ScreenLines[off] != ' '
7384 && (ScreenAttrs[off] > HL_ALL
7385 || ScreenAttrs[off] & HL_BOLD))
7386 force_next = TRUE;
7387 else
7388 force_next = FALSE;
7390 #endif
7391 ScreenLines[off] = c;
7392 #ifdef FEAT_MBYTE
7393 if (enc_utf8)
7395 if (c >= 0x80)
7397 ScreenLinesUC[off] = c;
7398 ScreenLinesC[0][off] = 0;
7400 else
7401 ScreenLinesUC[off] = 0;
7403 #endif
7404 ScreenAttrs[off] = attr;
7405 if (!did_delete || c != ' ')
7406 screen_char(off, row, col);
7408 ++off;
7409 if (col == start_col)
7411 if (did_delete)
7412 break;
7413 c = c2;
7416 if (end_col == Columns)
7417 LineWraps[row] = FALSE;
7418 if (row == Rows - 1) /* overwritten the command line */
7420 redraw_cmdline = TRUE;
7421 if (c1 == ' ' && c2 == ' ')
7422 clear_cmdline = FALSE; /* command line has been cleared */
7423 if (start_col == 0)
7424 mode_displayed = FALSE; /* mode cleared or overwritten */
7430 * Check if there should be a delay. Used before clearing or redrawing the
7431 * screen or the command line.
7433 void
7434 check_for_delay(check_msg_scroll)
7435 int check_msg_scroll;
7437 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
7438 && !did_wait_return
7439 && emsg_silent == 0)
7441 out_flush();
7442 ui_delay(1000L, TRUE);
7443 emsg_on_display = FALSE;
7444 if (check_msg_scroll)
7445 msg_scroll = FALSE;
7450 * screen_valid - allocate screen buffers if size changed
7451 * If "clear" is TRUE: clear screen if it has been resized.
7452 * Returns TRUE if there is a valid screen to write to.
7453 * Returns FALSE when starting up and screen not initialized yet.
7456 screen_valid(clear)
7457 int clear;
7459 screenalloc(clear); /* allocate screen buffers if size changed */
7460 return (ScreenLines != NULL);
7464 * Resize the shell to Rows and Columns.
7465 * Allocate ScreenLines[] and associated items.
7467 * There may be some time between setting Rows and Columns and (re)allocating
7468 * ScreenLines[]. This happens when starting up and when (manually) changing
7469 * the shell size. Always use screen_Rows and screen_Columns to access items
7470 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
7471 * final size of the shell is needed.
7473 void
7474 screenalloc(clear)
7475 int clear;
7477 int new_row, old_row;
7478 #ifdef FEAT_GUI
7479 int old_Rows;
7480 #endif
7481 win_T *wp;
7482 int outofmem = FALSE;
7483 int len;
7484 schar_T *new_ScreenLines;
7485 #ifdef FEAT_MBYTE
7486 u8char_T *new_ScreenLinesUC = NULL;
7487 u8char_T *new_ScreenLinesC[MAX_MCO];
7488 schar_T *new_ScreenLines2 = NULL;
7489 int i;
7490 #endif
7491 sattr_T *new_ScreenAttrs;
7492 unsigned *new_LineOffset;
7493 char_u *new_LineWraps;
7494 #ifdef FEAT_WINDOWS
7495 short *new_TabPageIdxs;
7496 tabpage_T *tp;
7497 #endif
7498 static int entered = FALSE; /* avoid recursiveness */
7499 static int done_outofmem_msg = FALSE; /* did outofmem message */
7500 #ifdef FEAT_AUTOCMD
7501 int retry_count = 0;
7503 retry:
7504 #endif
7506 * Allocation of the screen buffers is done only when the size changes and
7507 * when Rows and Columns have been set and we have started doing full
7508 * screen stuff.
7510 if ((ScreenLines != NULL
7511 && Rows == screen_Rows
7512 && Columns == screen_Columns
7513 #ifdef FEAT_MBYTE
7514 && enc_utf8 == (ScreenLinesUC != NULL)
7515 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
7516 && p_mco == Screen_mco
7517 #endif
7519 || Rows == 0
7520 || Columns == 0
7521 || (!full_screen && ScreenLines == NULL))
7522 return;
7525 * It's possible that we produce an out-of-memory message below, which
7526 * will cause this function to be called again. To break the loop, just
7527 * return here.
7529 if (entered)
7530 return;
7531 entered = TRUE;
7534 * Note that the window sizes are updated before reallocating the arrays,
7535 * thus we must not redraw here!
7537 ++RedrawingDisabled;
7539 win_new_shellsize(); /* fit the windows in the new sized shell */
7541 comp_col(); /* recompute columns for shown command and ruler */
7544 * We're changing the size of the screen.
7545 * - Allocate new arrays for ScreenLines and ScreenAttrs.
7546 * - Move lines from the old arrays into the new arrays, clear extra
7547 * lines (unless the screen is going to be cleared).
7548 * - Free the old arrays.
7550 * If anything fails, make ScreenLines NULL, so we don't do anything!
7551 * Continuing with the old ScreenLines may result in a crash, because the
7552 * size is wrong.
7554 FOR_ALL_TAB_WINDOWS(tp, wp)
7555 win_free_lsize(wp);
7556 #ifdef FEAT_AUTOCMD
7557 if (aucmd_win != NULL)
7558 win_free_lsize(aucmd_win);
7559 #endif
7561 new_ScreenLines = (schar_T *)lalloc((long_u)(
7562 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7563 #ifdef FEAT_MBYTE
7564 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
7565 if (enc_utf8)
7567 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
7568 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
7569 for (i = 0; i < p_mco; ++i)
7570 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
7571 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
7573 if (enc_dbcs == DBCS_JPNU)
7574 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
7575 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7576 #endif
7577 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
7578 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
7579 new_LineOffset = (unsigned *)lalloc((long_u)(
7580 Rows * sizeof(unsigned)), FALSE);
7581 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
7582 #ifdef FEAT_WINDOWS
7583 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
7584 #endif
7586 FOR_ALL_TAB_WINDOWS(tp, wp)
7588 if (win_alloc_lines(wp) == FAIL)
7590 outofmem = TRUE;
7591 #ifdef FEAT_WINDOWS
7592 goto give_up;
7593 #endif
7596 #ifdef FEAT_AUTOCMD
7597 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
7598 && win_alloc_lines(aucmd_win) == FAIL)
7599 outofmem = TRUE;
7600 #endif
7601 #ifdef FEAT_WINDOWS
7602 give_up:
7603 #endif
7605 #ifdef FEAT_MBYTE
7606 for (i = 0; i < p_mco; ++i)
7607 if (new_ScreenLinesC[i] == NULL)
7608 break;
7609 #endif
7610 if (new_ScreenLines == NULL
7611 #ifdef FEAT_MBYTE
7612 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
7613 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
7614 #endif
7615 || new_ScreenAttrs == NULL
7616 || new_LineOffset == NULL
7617 || new_LineWraps == NULL
7618 #ifdef FEAT_WINDOWS
7619 || new_TabPageIdxs == NULL
7620 #endif
7621 || outofmem)
7623 if (ScreenLines != NULL || !done_outofmem_msg)
7625 /* guess the size */
7626 do_outofmem_msg((long_u)((Rows + 1) * Columns));
7628 /* Remember we did this to avoid getting outofmem messages over
7629 * and over again. */
7630 done_outofmem_msg = TRUE;
7632 vim_free(new_ScreenLines);
7633 new_ScreenLines = NULL;
7634 #ifdef FEAT_MBYTE
7635 vim_free(new_ScreenLinesUC);
7636 new_ScreenLinesUC = NULL;
7637 for (i = 0; i < p_mco; ++i)
7639 vim_free(new_ScreenLinesC[i]);
7640 new_ScreenLinesC[i] = NULL;
7642 vim_free(new_ScreenLines2);
7643 new_ScreenLines2 = NULL;
7644 #endif
7645 vim_free(new_ScreenAttrs);
7646 new_ScreenAttrs = NULL;
7647 vim_free(new_LineOffset);
7648 new_LineOffset = NULL;
7649 vim_free(new_LineWraps);
7650 new_LineWraps = NULL;
7651 #ifdef FEAT_WINDOWS
7652 vim_free(new_TabPageIdxs);
7653 new_TabPageIdxs = NULL;
7654 #endif
7656 else
7658 done_outofmem_msg = FALSE;
7660 for (new_row = 0; new_row < Rows; ++new_row)
7662 new_LineOffset[new_row] = new_row * Columns;
7663 new_LineWraps[new_row] = FALSE;
7666 * If the screen is not going to be cleared, copy as much as
7667 * possible from the old screen to the new one and clear the rest
7668 * (used when resizing the window at the "--more--" prompt or when
7669 * executing an external command, for the GUI).
7671 if (!clear)
7673 (void)vim_memset(new_ScreenLines + new_row * Columns,
7674 ' ', (size_t)Columns * sizeof(schar_T));
7675 #ifdef FEAT_MBYTE
7676 if (enc_utf8)
7678 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
7679 0, (size_t)Columns * sizeof(u8char_T));
7680 for (i = 0; i < p_mco; ++i)
7681 (void)vim_memset(new_ScreenLinesC[i]
7682 + new_row * Columns,
7683 0, (size_t)Columns * sizeof(u8char_T));
7685 if (enc_dbcs == DBCS_JPNU)
7686 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
7687 0, (size_t)Columns * sizeof(schar_T));
7688 #endif
7689 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
7690 0, (size_t)Columns * sizeof(sattr_T));
7691 old_row = new_row + (screen_Rows - Rows);
7692 if (old_row >= 0 && ScreenLines != NULL)
7694 if (screen_Columns < Columns)
7695 len = screen_Columns;
7696 else
7697 len = Columns;
7698 #ifdef FEAT_MBYTE
7699 /* When switching to utf-8 don't copy characters, they
7700 * may be invalid now. Also when p_mco changes. */
7701 if (!(enc_utf8 && ScreenLinesUC == NULL)
7702 && p_mco == Screen_mco)
7703 #endif
7704 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
7705 ScreenLines + LineOffset[old_row],
7706 (size_t)len * sizeof(schar_T));
7707 #ifdef FEAT_MBYTE
7708 if (enc_utf8 && ScreenLinesUC != NULL
7709 && p_mco == Screen_mco)
7711 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
7712 ScreenLinesUC + LineOffset[old_row],
7713 (size_t)len * sizeof(u8char_T));
7714 for (i = 0; i < p_mco; ++i)
7715 mch_memmove(new_ScreenLinesC[i]
7716 + new_LineOffset[new_row],
7717 ScreenLinesC[i] + LineOffset[old_row],
7718 (size_t)len * sizeof(u8char_T));
7720 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
7721 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
7722 ScreenLines2 + LineOffset[old_row],
7723 (size_t)len * sizeof(schar_T));
7724 #endif
7725 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
7726 ScreenAttrs + LineOffset[old_row],
7727 (size_t)len * sizeof(sattr_T));
7731 /* Use the last line of the screen for the current line. */
7732 current_ScreenLine = new_ScreenLines + Rows * Columns;
7735 free_screenlines();
7737 ScreenLines = new_ScreenLines;
7738 #ifdef FEAT_MBYTE
7739 ScreenLinesUC = new_ScreenLinesUC;
7740 for (i = 0; i < p_mco; ++i)
7741 ScreenLinesC[i] = new_ScreenLinesC[i];
7742 Screen_mco = p_mco;
7743 ScreenLines2 = new_ScreenLines2;
7744 #endif
7745 ScreenAttrs = new_ScreenAttrs;
7746 LineOffset = new_LineOffset;
7747 LineWraps = new_LineWraps;
7748 #ifdef FEAT_WINDOWS
7749 TabPageIdxs = new_TabPageIdxs;
7750 #endif
7752 /* It's important that screen_Rows and screen_Columns reflect the actual
7753 * size of ScreenLines[]. Set them before calling anything. */
7754 #ifdef FEAT_GUI
7755 old_Rows = screen_Rows;
7756 #endif
7757 screen_Rows = Rows;
7758 screen_Columns = Columns;
7760 must_redraw = CLEAR; /* need to clear the screen later */
7761 if (clear)
7762 screenclear2();
7764 #ifdef FEAT_GUI
7765 else if (gui.in_use
7766 && !gui.starting
7767 && ScreenLines != NULL
7768 && old_Rows != Rows)
7770 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
7772 * Adjust the position of the cursor, for when executing an external
7773 * command.
7775 if (msg_row >= Rows) /* Rows got smaller */
7776 msg_row = Rows - 1; /* put cursor at last row */
7777 else if (Rows > old_Rows) /* Rows got bigger */
7778 msg_row += Rows - old_Rows; /* put cursor in same place */
7779 if (msg_col >= Columns) /* Columns got smaller */
7780 msg_col = Columns - 1; /* put cursor at last column */
7782 #endif
7784 entered = FALSE;
7785 --RedrawingDisabled;
7787 #ifdef FEAT_AUTOCMD
7789 * Do not apply autocommands more than 3 times to avoid an endless loop
7790 * in case applying autocommands always changes Rows or Columns.
7792 if (starting == 0 && ++retry_count <= 3)
7794 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
7795 /* In rare cases, autocommands may have altered Rows or Columns,
7796 * jump back to check if we need to allocate the screen again. */
7797 goto retry;
7799 #endif
7802 void
7803 free_screenlines()
7805 #ifdef FEAT_MBYTE
7806 int i;
7808 vim_free(ScreenLinesUC);
7809 for (i = 0; i < Screen_mco; ++i)
7810 vim_free(ScreenLinesC[i]);
7811 vim_free(ScreenLines2);
7812 #endif
7813 vim_free(ScreenLines);
7814 vim_free(ScreenAttrs);
7815 vim_free(LineOffset);
7816 vim_free(LineWraps);
7817 #ifdef FEAT_WINDOWS
7818 vim_free(TabPageIdxs);
7819 #endif
7822 void
7823 screenclear()
7825 check_for_delay(FALSE);
7826 screenalloc(FALSE); /* allocate screen buffers if size changed */
7827 screenclear2(); /* clear the screen */
7830 static void
7831 screenclear2()
7833 int i;
7835 if (starting == NO_SCREEN || ScreenLines == NULL
7836 #ifdef FEAT_GUI
7837 || (gui.in_use && gui.starting)
7838 #endif
7840 return;
7842 #ifdef FEAT_GUI
7843 if (!gui.in_use)
7844 #endif
7845 screen_attr = -1; /* force setting the Normal colors */
7846 screen_stop_highlight(); /* don't want highlighting here */
7848 #ifdef FEAT_CLIPBOARD
7849 /* disable selection without redrawing it */
7850 clip_scroll_selection(9999);
7851 #endif
7853 /* blank out ScreenLines */
7854 for (i = 0; i < Rows; ++i)
7856 lineclear(LineOffset[i], (int)Columns);
7857 LineWraps[i] = FALSE;
7860 if (can_clear(T_CL))
7862 out_str(T_CL); /* clear the display */
7863 clear_cmdline = FALSE;
7864 mode_displayed = FALSE;
7866 else
7868 /* can't clear the screen, mark all chars with invalid attributes */
7869 for (i = 0; i < Rows; ++i)
7870 lineinvalid(LineOffset[i], (int)Columns);
7871 clear_cmdline = TRUE;
7874 screen_cleared = TRUE; /* can use contents of ScreenLines now */
7876 win_rest_invalid(firstwin);
7877 redraw_cmdline = TRUE;
7878 #ifdef FEAT_WINDOWS
7879 redraw_tabline = TRUE;
7880 #endif
7881 if (must_redraw == CLEAR) /* no need to clear again */
7882 must_redraw = NOT_VALID;
7883 compute_cmdrow();
7884 msg_row = cmdline_row; /* put cursor on last line for messages */
7885 msg_col = 0;
7886 screen_start(); /* don't know where cursor is now */
7887 msg_scrolled = 0; /* can't scroll back */
7888 msg_didany = FALSE;
7889 msg_didout = FALSE;
7893 * Clear one line in ScreenLines.
7895 static void
7896 lineclear(off, width)
7897 unsigned off;
7898 int width;
7900 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
7901 #ifdef FEAT_MBYTE
7902 if (enc_utf8)
7903 (void)vim_memset(ScreenLinesUC + off, 0,
7904 (size_t)width * sizeof(u8char_T));
7905 #endif
7906 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
7910 * Mark one line in ScreenLines invalid by setting the attributes to an
7911 * invalid value.
7913 static void
7914 lineinvalid(off, width)
7915 unsigned off;
7916 int width;
7918 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
7921 #ifdef FEAT_VERTSPLIT
7923 * Copy part of a Screenline for vertically split window "wp".
7925 static void
7926 linecopy(to, from, wp)
7927 int to;
7928 int from;
7929 win_T *wp;
7931 unsigned off_to = LineOffset[to] + wp->w_wincol;
7932 unsigned off_from = LineOffset[from] + wp->w_wincol;
7934 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
7935 wp->w_width * sizeof(schar_T));
7936 # ifdef FEAT_MBYTE
7937 if (enc_utf8)
7939 int i;
7941 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
7942 wp->w_width * sizeof(u8char_T));
7943 for (i = 0; i < p_mco; ++i)
7944 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
7945 wp->w_width * sizeof(u8char_T));
7947 if (enc_dbcs == DBCS_JPNU)
7948 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
7949 wp->w_width * sizeof(schar_T));
7950 # endif
7951 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
7952 wp->w_width * sizeof(sattr_T));
7954 #endif
7957 * Return TRUE if clearing with term string "p" would work.
7958 * It can't work when the string is empty or it won't set the right background.
7961 can_clear(p)
7962 char_u *p;
7964 return (*p != NUL && (t_colors <= 1
7965 #ifdef FEAT_GUI
7966 || gui.in_use
7967 #endif
7968 || cterm_normal_bg_color == 0 || *T_UT != NUL));
7972 * Reset cursor position. Use whenever cursor was moved because of outputting
7973 * something directly to the screen (shell commands) or a terminal control
7974 * code.
7976 void
7977 screen_start()
7979 screen_cur_row = screen_cur_col = 9999;
7983 * Move the cursor to position "row","col" in the screen.
7984 * This tries to find the most efficient way to move, minimizing the number of
7985 * characters sent to the terminal.
7987 void
7988 windgoto(row, col)
7989 int row;
7990 int col;
7992 sattr_T *p;
7993 int i;
7994 int plan;
7995 int cost;
7996 int wouldbe_col;
7997 int noinvcurs;
7998 char_u *bs;
7999 int goto_cost;
8000 int attr;
8002 #define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
8003 #define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
8005 #define PLAN_LE 1
8006 #define PLAN_CR 2
8007 #define PLAN_NL 3
8008 #define PLAN_WRITE 4
8009 /* Can't use ScreenLines unless initialized */
8010 if (ScreenLines == NULL)
8011 return;
8013 if (col != screen_cur_col || row != screen_cur_row)
8015 /* Check for valid position. */
8016 if (row < 0) /* window without text lines? */
8017 row = 0;
8018 if (row >= screen_Rows)
8019 row = screen_Rows - 1;
8020 if (col >= screen_Columns)
8021 col = screen_Columns - 1;
8023 /* check if no cursor movement is allowed in highlight mode */
8024 if (screen_attr && *T_MS == NUL)
8025 noinvcurs = HIGHL_COST;
8026 else
8027 noinvcurs = 0;
8028 goto_cost = GOTO_COST + noinvcurs;
8031 * Plan how to do the positioning:
8032 * 1. Use CR to move it to column 0, same row.
8033 * 2. Use T_LE to move it a few columns to the left.
8034 * 3. Use NL to move a few lines down, column 0.
8035 * 4. Move a few columns to the right with T_ND or by writing chars.
8037 * Don't do this if the cursor went beyond the last column, the cursor
8038 * position is unknown then (some terminals wrap, some don't )
8040 * First check if the highlighting attributes allow us to write
8041 * characters to move the cursor to the right.
8043 if (row >= screen_cur_row && screen_cur_col < Columns)
8046 * If the cursor is in the same row, bigger col, we can use CR
8047 * or T_LE.
8049 bs = NULL; /* init for GCC */
8050 attr = screen_attr;
8051 if (row == screen_cur_row && col < screen_cur_col)
8053 /* "le" is preferred over "bc", because "bc" is obsolete */
8054 if (*T_LE)
8055 bs = T_LE; /* "cursor left" */
8056 else
8057 bs = T_BC; /* "backspace character (old) */
8058 if (*bs)
8059 cost = (screen_cur_col - col) * (int)STRLEN(bs);
8060 else
8061 cost = 999;
8062 if (col + 1 < cost) /* using CR is less characters */
8064 plan = PLAN_CR;
8065 wouldbe_col = 0;
8066 cost = 1; /* CR is just one character */
8068 else
8070 plan = PLAN_LE;
8071 wouldbe_col = col;
8073 if (noinvcurs) /* will stop highlighting */
8075 cost += noinvcurs;
8076 attr = 0;
8081 * If the cursor is above where we want to be, we can use CR LF.
8083 else if (row > screen_cur_row)
8085 plan = PLAN_NL;
8086 wouldbe_col = 0;
8087 cost = (row - screen_cur_row) * 2; /* CR LF */
8088 if (noinvcurs) /* will stop highlighting */
8090 cost += noinvcurs;
8091 attr = 0;
8096 * If the cursor is in the same row, smaller col, just use write.
8098 else
8100 plan = PLAN_WRITE;
8101 wouldbe_col = screen_cur_col;
8102 cost = 0;
8106 * Check if any characters that need to be written have the
8107 * correct attributes. Also avoid UTF-8 characters.
8109 i = col - wouldbe_col;
8110 if (i > 0)
8111 cost += i;
8112 if (cost < goto_cost && i > 0)
8115 * Check if the attributes are correct without additionally
8116 * stopping highlighting.
8118 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
8119 while (i && *p++ == attr)
8120 --i;
8121 if (i != 0)
8124 * Try if it works when highlighting is stopped here.
8126 if (*--p == 0)
8128 cost += noinvcurs;
8129 while (i && *p++ == 0)
8130 --i;
8132 if (i != 0)
8133 cost = 999; /* different attributes, don't do it */
8135 #ifdef FEAT_MBYTE
8136 if (enc_utf8)
8138 /* Don't use an UTF-8 char for positioning, it's slow. */
8139 for (i = wouldbe_col; i < col; ++i)
8140 if (ScreenLinesUC[LineOffset[row] + i] != 0)
8142 cost = 999;
8143 break;
8146 #endif
8150 * We can do it without term_windgoto()!
8152 if (cost < goto_cost)
8154 if (plan == PLAN_LE)
8156 if (noinvcurs)
8157 screen_stop_highlight();
8158 while (screen_cur_col > col)
8160 out_str(bs);
8161 --screen_cur_col;
8164 else if (plan == PLAN_CR)
8166 if (noinvcurs)
8167 screen_stop_highlight();
8168 out_char('\r');
8169 screen_cur_col = 0;
8171 else if (plan == PLAN_NL)
8173 if (noinvcurs)
8174 screen_stop_highlight();
8175 while (screen_cur_row < row)
8177 out_char('\n');
8178 ++screen_cur_row;
8180 screen_cur_col = 0;
8183 i = col - screen_cur_col;
8184 if (i > 0)
8187 * Use cursor-right if it's one character only. Avoids
8188 * removing a line of pixels from the last bold char, when
8189 * using the bold trick in the GUI.
8191 if (T_ND[0] != NUL && T_ND[1] == NUL)
8193 while (i-- > 0)
8194 out_char(*T_ND);
8196 else
8198 int off;
8200 off = LineOffset[row] + screen_cur_col;
8201 while (i-- > 0)
8203 if (ScreenAttrs[off] != screen_attr)
8204 screen_stop_highlight();
8205 #ifdef FEAT_MBYTE
8206 out_flush_check();
8207 #endif
8208 out_char(ScreenLines[off]);
8209 #ifdef FEAT_MBYTE
8210 if (enc_dbcs == DBCS_JPNU
8211 && ScreenLines[off] == 0x8e)
8212 out_char(ScreenLines2[off]);
8213 #endif
8214 ++off;
8220 else
8221 cost = 999;
8223 if (cost >= goto_cost)
8225 if (noinvcurs)
8226 screen_stop_highlight();
8227 if (row == screen_cur_row && (col > screen_cur_col) &&
8228 *T_CRI != NUL)
8229 term_cursor_right(col - screen_cur_col);
8230 else
8231 term_windgoto(row, col);
8233 screen_cur_row = row;
8234 screen_cur_col = col;
8239 * Set cursor to its position in the current window.
8241 void
8242 setcursor()
8244 if (redrawing())
8246 validate_cursor();
8247 windgoto(W_WINROW(curwin) + curwin->w_wrow,
8248 W_WINCOL(curwin) + (
8249 #ifdef FEAT_RIGHTLEFT
8250 /* With 'rightleft' set and the cursor on a double-wide
8251 * character, position it on the leftmost column. */
8252 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
8253 # ifdef FEAT_MBYTE
8254 (has_mbyte
8255 && (*mb_ptr2cells)(ml_get_cursor()) == 2
8256 && vim_isprintc(gchar_cursor())) ? 2 :
8257 # endif
8258 1)) :
8259 #endif
8260 curwin->w_wcol));
8266 * insert 'line_count' lines at 'row' in window 'wp'
8267 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
8268 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
8269 * scrolling.
8270 * Returns FAIL if the lines are not inserted, OK for success.
8273 win_ins_lines(wp, row, line_count, invalid, mayclear)
8274 win_T *wp;
8275 int row;
8276 int line_count;
8277 int invalid;
8278 int mayclear;
8280 int did_delete;
8281 int nextrow;
8282 int lastrow;
8283 int retval;
8285 if (invalid)
8286 wp->w_lines_valid = 0;
8288 if (wp->w_height < 5)
8289 return FAIL;
8291 if (line_count > wp->w_height - row)
8292 line_count = wp->w_height - row;
8294 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
8295 if (retval != MAYBE)
8296 return retval;
8299 * If there is a next window or a status line, we first try to delete the
8300 * lines at the bottom to avoid messing what is after the window.
8301 * If this fails and there are following windows, don't do anything to avoid
8302 * messing up those windows, better just redraw.
8304 did_delete = FALSE;
8305 #ifdef FEAT_WINDOWS
8306 if (wp->w_next != NULL || wp->w_status_height)
8308 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8309 line_count, (int)Rows, FALSE, NULL) == OK)
8310 did_delete = TRUE;
8311 else if (wp->w_next)
8312 return FAIL;
8314 #endif
8316 * if no lines deleted, blank the lines that will end up below the window
8318 if (!did_delete)
8320 #ifdef FEAT_WINDOWS
8321 wp->w_redr_status = TRUE;
8322 #endif
8323 redraw_cmdline = TRUE;
8324 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
8325 lastrow = nextrow + line_count;
8326 if (lastrow > Rows)
8327 lastrow = Rows;
8328 screen_fill(nextrow - line_count, lastrow - line_count,
8329 W_WINCOL(wp), (int)W_ENDCOL(wp),
8330 ' ', ' ', 0);
8333 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
8334 == FAIL)
8336 /* deletion will have messed up other windows */
8337 if (did_delete)
8339 #ifdef FEAT_WINDOWS
8340 wp->w_redr_status = TRUE;
8341 #endif
8342 win_rest_invalid(W_NEXT(wp));
8344 return FAIL;
8347 return OK;
8351 * delete "line_count" window lines at "row" in window "wp"
8352 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
8353 * If "mayclear" is TRUE the screen will be cleared if it is faster than
8354 * scrolling
8355 * Return OK for success, FAIL if the lines are not deleted.
8358 win_del_lines(wp, row, line_count, invalid, mayclear)
8359 win_T *wp;
8360 int row;
8361 int line_count;
8362 int invalid;
8363 int mayclear;
8365 int retval;
8367 if (invalid)
8368 wp->w_lines_valid = 0;
8370 if (line_count > wp->w_height - row)
8371 line_count = wp->w_height - row;
8373 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
8374 if (retval != MAYBE)
8375 return retval;
8377 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
8378 (int)Rows, FALSE, NULL) == FAIL)
8379 return FAIL;
8381 #ifdef FEAT_WINDOWS
8383 * If there are windows or status lines below, try to put them at the
8384 * correct place. If we can't do that, they have to be redrawn.
8386 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
8388 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8389 line_count, (int)Rows, NULL) == FAIL)
8391 wp->w_redr_status = TRUE;
8392 win_rest_invalid(wp->w_next);
8396 * If this is the last window and there is no status line, redraw the
8397 * command line later.
8399 else
8400 #endif
8401 redraw_cmdline = TRUE;
8402 return OK;
8406 * Common code for win_ins_lines() and win_del_lines().
8407 * Returns OK or FAIL when the work has been done.
8408 * Returns MAYBE when not finished yet.
8410 static int
8411 win_do_lines(wp, row, line_count, mayclear, del)
8412 win_T *wp;
8413 int row;
8414 int line_count;
8415 int mayclear;
8416 int del;
8418 int retval;
8420 if (!redrawing() || line_count <= 0)
8421 return FAIL;
8423 /* only a few lines left: redraw is faster */
8424 if (mayclear && Rows - line_count < 5
8425 #ifdef FEAT_VERTSPLIT
8426 && wp->w_width == Columns
8427 #endif
8430 screenclear(); /* will set wp->w_lines_valid to 0 */
8431 return FAIL;
8435 * Delete all remaining lines
8437 if (row + line_count >= wp->w_height)
8439 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
8440 W_WINCOL(wp), (int)W_ENDCOL(wp),
8441 ' ', ' ', 0);
8442 return OK;
8446 * when scrolling, the message on the command line should be cleared,
8447 * otherwise it will stay there forever.
8449 clear_cmdline = TRUE;
8452 * If the terminal can set a scroll region, use that.
8453 * Always do this in a vertically split window. This will redraw from
8454 * ScreenLines[] when t_CV isn't defined. That's faster than using
8455 * win_line().
8456 * Don't use a scroll region when we are going to redraw the text, writing
8457 * a character in the lower right corner of the scroll region causes a
8458 * scroll-up in the DJGPP version.
8460 if (scroll_region
8461 #ifdef FEAT_VERTSPLIT
8462 || W_WIDTH(wp) != Columns
8463 #endif
8466 #ifdef FEAT_VERTSPLIT
8467 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8468 #endif
8469 scroll_region_set(wp, row);
8470 if (del)
8471 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
8472 wp->w_height - row, FALSE, wp);
8473 else
8474 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
8475 wp->w_height - row, wp);
8476 #ifdef FEAT_VERTSPLIT
8477 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8478 #endif
8479 scroll_region_reset();
8480 return retval;
8483 #ifdef FEAT_WINDOWS
8484 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
8485 return FAIL;
8486 #endif
8488 return MAYBE;
8492 * window 'wp' and everything after it is messed up, mark it for redraw
8494 static void
8495 win_rest_invalid(wp)
8496 win_T *wp;
8498 #ifdef FEAT_WINDOWS
8499 while (wp != NULL)
8500 #else
8501 if (wp != NULL)
8502 #endif
8504 redraw_win_later(wp, NOT_VALID);
8505 #ifdef FEAT_WINDOWS
8506 wp->w_redr_status = TRUE;
8507 wp = wp->w_next;
8508 #endif
8510 redraw_cmdline = TRUE;
8514 * The rest of the routines in this file perform screen manipulations. The
8515 * given operation is performed physically on the screen. The corresponding
8516 * change is also made to the internal screen image. In this way, the editor
8517 * anticipates the effect of editing changes on the appearance of the screen.
8518 * That way, when we call screenupdate a complete redraw isn't usually
8519 * necessary. Another advantage is that we can keep adding code to anticipate
8520 * screen changes, and in the meantime, everything still works.
8524 * types for inserting or deleting lines
8526 #define USE_T_CAL 1
8527 #define USE_T_CDL 2
8528 #define USE_T_AL 3
8529 #define USE_T_CE 4
8530 #define USE_T_DL 5
8531 #define USE_T_SR 6
8532 #define USE_NL 7
8533 #define USE_T_CD 8
8534 #define USE_REDRAW 9
8537 * insert lines on the screen and update ScreenLines[]
8538 * 'end' is the line after the scrolled part. Normally it is Rows.
8539 * When scrolling region used 'off' is the offset from the top for the region.
8540 * 'row' and 'end' are relative to the start of the region.
8542 * return FAIL for failure, OK for success.
8545 screen_ins_lines(off, row, line_count, end, wp)
8546 int off;
8547 int row;
8548 int line_count;
8549 int end;
8550 win_T *wp; /* NULL or window to use width from */
8552 int i;
8553 int j;
8554 unsigned temp;
8555 int cursor_row;
8556 int type;
8557 int result_empty;
8558 int can_ce = can_clear(T_CE);
8561 * FAIL if
8562 * - there is no valid screen
8563 * - the screen has to be redrawn completely
8564 * - the line count is less than one
8565 * - the line count is more than 'ttyscroll'
8567 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
8568 return FAIL;
8571 * There are seven ways to insert lines:
8572 * 0. When in a vertically split window and t_CV isn't set, redraw the
8573 * characters from ScreenLines[].
8574 * 1. Use T_CD (clear to end of display) if it exists and the result of
8575 * the insert is just empty lines
8576 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
8577 * present or line_count > 1. It looks better if we do all the inserts
8578 * at once.
8579 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
8580 * insert is just empty lines and T_CE is not present or line_count >
8581 * 1.
8582 * 4. Use T_AL (insert line) if it exists.
8583 * 5. Use T_CE (erase line) if it exists and the result of the insert is
8584 * just empty lines.
8585 * 6. Use T_DL (delete line) if it exists and the result of the insert is
8586 * just empty lines.
8587 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
8588 * the 'da' flag is not set or we have clear line capability.
8589 * 8. redraw the characters from ScreenLines[].
8591 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
8592 * the scrollbar for the window. It does have insert line, use that if it
8593 * exists.
8595 result_empty = (row + line_count >= end);
8596 #ifdef FEAT_VERTSPLIT
8597 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
8598 type = USE_REDRAW;
8599 else
8600 #endif
8601 if (can_clear(T_CD) && result_empty)
8602 type = USE_T_CD;
8603 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
8604 type = USE_T_CAL;
8605 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
8606 type = USE_T_CDL;
8607 else if (*T_AL != NUL)
8608 type = USE_T_AL;
8609 else if (can_ce && result_empty)
8610 type = USE_T_CE;
8611 else if (*T_DL != NUL && result_empty)
8612 type = USE_T_DL;
8613 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
8614 type = USE_T_SR;
8615 else
8616 return FAIL;
8619 * For clearing the lines screen_del_lines() is used. This will also take
8620 * care of t_db if necessary.
8622 if (type == USE_T_CD || type == USE_T_CDL ||
8623 type == USE_T_CE || type == USE_T_DL)
8624 return screen_del_lines(off, row, line_count, end, FALSE, wp);
8627 * If text is retained below the screen, first clear or delete as many
8628 * lines at the bottom of the window as are about to be inserted so that
8629 * the deleted lines won't later surface during a screen_del_lines.
8631 if (*T_DB)
8632 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
8634 #ifdef FEAT_CLIPBOARD
8635 /* Remove a modeless selection when inserting lines halfway the screen
8636 * or not the full width of the screen. */
8637 if (off + row > 0
8638 # ifdef FEAT_VERTSPLIT
8639 || (wp != NULL && wp->w_width != Columns)
8640 # endif
8642 clip_clear_selection();
8643 else
8644 clip_scroll_selection(-line_count);
8645 #endif
8647 #ifdef FEAT_GUI
8648 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
8649 * scrolling is actually carried out. */
8650 gui_dont_update_cursor();
8651 #endif
8653 if (*T_CCS != NUL) /* cursor relative to region */
8654 cursor_row = row;
8655 else
8656 cursor_row = row + off;
8659 * Shift LineOffset[] line_count down to reflect the inserted lines.
8660 * Clear the inserted lines in ScreenLines[].
8662 row += off;
8663 end += off;
8664 for (i = 0; i < line_count; ++i)
8666 #ifdef FEAT_VERTSPLIT
8667 if (wp != NULL && wp->w_width != Columns)
8669 /* need to copy part of a line */
8670 j = end - 1 - i;
8671 while ((j -= line_count) >= row)
8672 linecopy(j + line_count, j, wp);
8673 j += line_count;
8674 if (can_clear((char_u *)" "))
8675 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
8676 else
8677 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
8678 LineWraps[j] = FALSE;
8680 else
8681 #endif
8683 j = end - 1 - i;
8684 temp = LineOffset[j];
8685 while ((j -= line_count) >= row)
8687 LineOffset[j + line_count] = LineOffset[j];
8688 LineWraps[j + line_count] = LineWraps[j];
8690 LineOffset[j + line_count] = temp;
8691 LineWraps[j + line_count] = FALSE;
8692 if (can_clear((char_u *)" "))
8693 lineclear(temp, (int)Columns);
8694 else
8695 lineinvalid(temp, (int)Columns);
8699 screen_stop_highlight();
8700 windgoto(cursor_row, 0);
8702 #ifdef FEAT_VERTSPLIT
8703 /* redraw the characters */
8704 if (type == USE_REDRAW)
8705 redraw_block(row, end, wp);
8706 else
8707 #endif
8708 if (type == USE_T_CAL)
8710 term_append_lines(line_count);
8711 screen_start(); /* don't know where cursor is now */
8713 else
8715 for (i = 0; i < line_count; i++)
8717 if (type == USE_T_AL)
8719 if (i && cursor_row != 0)
8720 windgoto(cursor_row, 0);
8721 out_str(T_AL);
8723 else /* type == USE_T_SR */
8724 out_str(T_SR);
8725 screen_start(); /* don't know where cursor is now */
8730 * With scroll-reverse and 'da' flag set we need to clear the lines that
8731 * have been scrolled down into the region.
8733 if (type == USE_T_SR && *T_DA)
8735 for (i = 0; i < line_count; ++i)
8737 windgoto(off + i, 0);
8738 out_str(T_CE);
8739 screen_start(); /* don't know where cursor is now */
8743 #ifdef FEAT_GUI
8744 gui_can_update_cursor();
8745 if (gui.in_use)
8746 out_flush(); /* always flush after a scroll */
8747 #endif
8748 return OK;
8752 * delete lines on the screen and update ScreenLines[]
8753 * 'end' is the line after the scrolled part. Normally it is Rows.
8754 * When scrolling region used 'off' is the offset from the top for the region.
8755 * 'row' and 'end' are relative to the start of the region.
8757 * Return OK for success, FAIL if the lines are not deleted.
8760 screen_del_lines(off, row, line_count, end, force, wp)
8761 int off;
8762 int row;
8763 int line_count;
8764 int end;
8765 int force; /* even when line_count > p_ttyscroll */
8766 win_T *wp UNUSED; /* NULL or window to use width from */
8768 int j;
8769 int i;
8770 unsigned temp;
8771 int cursor_row;
8772 int cursor_end;
8773 int result_empty; /* result is empty until end of region */
8774 int can_delete; /* deleting line codes can be used */
8775 int type;
8778 * FAIL if
8779 * - there is no valid screen
8780 * - the screen has to be redrawn completely
8781 * - the line count is less than one
8782 * - the line count is more than 'ttyscroll'
8784 if (!screen_valid(TRUE) || line_count <= 0 ||
8785 (!force && line_count > p_ttyscroll))
8786 return FAIL;
8789 * Check if the rest of the current region will become empty.
8791 result_empty = row + line_count >= end;
8794 * We can delete lines only when 'db' flag not set or when 'ce' option
8795 * available.
8797 can_delete = (*T_DB == NUL || can_clear(T_CE));
8800 * There are six ways to delete lines:
8801 * 0. When in a vertically split window and t_CV isn't set, redraw the
8802 * characters from ScreenLines[].
8803 * 1. Use T_CD if it exists and the result is empty.
8804 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
8805 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
8806 * none of the other ways work.
8807 * 4. Use T_CE (erase line) if the result is empty.
8808 * 5. Use T_DL (delete line) if it exists.
8809 * 6. redraw the characters from ScreenLines[].
8811 #ifdef FEAT_VERTSPLIT
8812 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
8813 type = USE_REDRAW;
8814 else
8815 #endif
8816 if (can_clear(T_CD) && result_empty)
8817 type = USE_T_CD;
8818 #if defined(__BEOS__) && defined(BEOS_DR8)
8820 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
8821 * its internal termcap... this works okay for tests which test *T_DB !=
8822 * NUL. It has the disadvantage that the user cannot use any :set t_*
8823 * command to get T_DB (back) to empty_option, only :set term=... will do
8824 * the trick...
8825 * Anyway, this hack will hopefully go away with the next OS release.
8826 * (Olaf Seibert)
8828 else if (row == 0 && T_DB == empty_option
8829 && (line_count == 1 || *T_CDL == NUL))
8830 #else
8831 else if (row == 0 && (
8832 #ifndef AMIGA
8833 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
8834 * up, so use delete-line command */
8835 line_count == 1 ||
8836 #endif
8837 *T_CDL == NUL))
8838 #endif
8839 type = USE_NL;
8840 else if (*T_CDL != NUL && line_count > 1 && can_delete)
8841 type = USE_T_CDL;
8842 else if (can_clear(T_CE) && result_empty
8843 #ifdef FEAT_VERTSPLIT
8844 && (wp == NULL || wp->w_width == Columns)
8845 #endif
8847 type = USE_T_CE;
8848 else if (*T_DL != NUL && can_delete)
8849 type = USE_T_DL;
8850 else if (*T_CDL != NUL && can_delete)
8851 type = USE_T_CDL;
8852 else
8853 return FAIL;
8855 #ifdef FEAT_CLIPBOARD
8856 /* Remove a modeless selection when deleting lines halfway the screen or
8857 * not the full width of the screen. */
8858 if (off + row > 0
8859 # ifdef FEAT_VERTSPLIT
8860 || (wp != NULL && wp->w_width != Columns)
8861 # endif
8863 clip_clear_selection();
8864 else
8865 clip_scroll_selection(line_count);
8866 #endif
8868 #ifdef FEAT_GUI
8869 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
8870 * scrolling is actually carried out. */
8871 gui_dont_update_cursor();
8872 #endif
8874 if (*T_CCS != NUL) /* cursor relative to region */
8876 cursor_row = row;
8877 cursor_end = end;
8879 else
8881 cursor_row = row + off;
8882 cursor_end = end + off;
8886 * Now shift LineOffset[] line_count up to reflect the deleted lines.
8887 * Clear the inserted lines in ScreenLines[].
8889 row += off;
8890 end += off;
8891 for (i = 0; i < line_count; ++i)
8893 #ifdef FEAT_VERTSPLIT
8894 if (wp != NULL && wp->w_width != Columns)
8896 /* need to copy part of a line */
8897 j = row + i;
8898 while ((j += line_count) <= end - 1)
8899 linecopy(j - line_count, j, wp);
8900 j -= line_count;
8901 if (can_clear((char_u *)" "))
8902 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
8903 else
8904 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
8905 LineWraps[j] = FALSE;
8907 else
8908 #endif
8910 /* whole width, moving the line pointers is faster */
8911 j = row + i;
8912 temp = LineOffset[j];
8913 while ((j += line_count) <= end - 1)
8915 LineOffset[j - line_count] = LineOffset[j];
8916 LineWraps[j - line_count] = LineWraps[j];
8918 LineOffset[j - line_count] = temp;
8919 LineWraps[j - line_count] = FALSE;
8920 if (can_clear((char_u *)" "))
8921 lineclear(temp, (int)Columns);
8922 else
8923 lineinvalid(temp, (int)Columns);
8927 screen_stop_highlight();
8929 #ifdef FEAT_VERTSPLIT
8930 /* redraw the characters */
8931 if (type == USE_REDRAW)
8932 redraw_block(row, end, wp);
8933 else
8934 #endif
8935 if (type == USE_T_CD) /* delete the lines */
8937 windgoto(cursor_row, 0);
8938 out_str(T_CD);
8939 screen_start(); /* don't know where cursor is now */
8941 else if (type == USE_T_CDL)
8943 windgoto(cursor_row, 0);
8944 term_delete_lines(line_count);
8945 screen_start(); /* don't know where cursor is now */
8948 * Deleting lines at top of the screen or scroll region: Just scroll
8949 * the whole screen (scroll region) up by outputting newlines on the
8950 * last line.
8952 else if (type == USE_NL)
8954 windgoto(cursor_end - 1, 0);
8955 for (i = line_count; --i >= 0; )
8956 out_char('\n'); /* cursor will remain on same line */
8958 else
8960 for (i = line_count; --i >= 0; )
8962 if (type == USE_T_DL)
8964 windgoto(cursor_row, 0);
8965 out_str(T_DL); /* delete a line */
8967 else /* type == USE_T_CE */
8969 windgoto(cursor_row + i, 0);
8970 out_str(T_CE); /* erase a line */
8972 screen_start(); /* don't know where cursor is now */
8977 * If the 'db' flag is set, we need to clear the lines that have been
8978 * scrolled up at the bottom of the region.
8980 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
8982 for (i = line_count; i > 0; --i)
8984 windgoto(cursor_end - i, 0);
8985 out_str(T_CE); /* erase a line */
8986 screen_start(); /* don't know where cursor is now */
8990 #ifdef FEAT_GUI
8991 gui_can_update_cursor();
8992 if (gui.in_use)
8993 out_flush(); /* always flush after a scroll */
8994 #endif
8996 return OK;
9000 * show the current mode and ruler
9002 * If clear_cmdline is TRUE, clear the rest of the cmdline.
9003 * If clear_cmdline is FALSE there may be a message there that needs to be
9004 * cleared only if a mode is shown.
9005 * Return the length of the message (0 if no message).
9008 showmode()
9010 int need_clear;
9011 int length = 0;
9012 int do_mode;
9013 int attr;
9014 int nwr_save;
9015 #ifdef FEAT_INS_EXPAND
9016 int sub_attr;
9017 #endif
9019 do_mode = ((p_smd && msg_silent == 0)
9020 && ((State & INSERT)
9021 || restart_edit
9022 #ifdef FEAT_VISUAL
9023 || VIsual_active
9024 #endif
9026 if (do_mode || Recording)
9029 * Don't show mode right now, when not redrawing or inside a mapping.
9030 * Call char_avail() only when we are going to show something, because
9031 * it takes a bit of time.
9033 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
9035 redraw_cmdline = TRUE; /* show mode later */
9036 return 0;
9039 nwr_save = need_wait_return;
9041 /* wait a bit before overwriting an important message */
9042 check_for_delay(FALSE);
9044 /* if the cmdline is more than one line high, erase top lines */
9045 need_clear = clear_cmdline;
9046 if (clear_cmdline && cmdline_row < Rows - 1)
9047 msg_clr_cmdline(); /* will reset clear_cmdline */
9049 /* Position on the last line in the window, column 0 */
9050 msg_pos_mode();
9051 cursor_off();
9052 attr = hl_attr(HLF_CM); /* Highlight mode */
9053 if (do_mode)
9055 MSG_PUTS_ATTR("--", attr);
9056 #if defined(FEAT_XIM)
9057 # if 0 /* old version, changed by SungHyun Nam July 2008 */
9058 if (xic != NULL && im_get_status() && !p_imdisable
9059 && curbuf->b_p_iminsert == B_IMODE_IM)
9060 # else
9061 if (
9062 # ifdef HAVE_GTK2
9063 preedit_get_status()
9064 # else
9065 im_get_status()
9066 # endif
9068 # endif
9069 # ifdef HAVE_GTK2 /* most of the time, it's not XIM being used */
9070 MSG_PUTS_ATTR(" IM", attr);
9071 # else
9072 MSG_PUTS_ATTR(" XIM", attr);
9073 # endif
9074 #endif
9075 #if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
9076 if (gui.in_use)
9078 if (hangul_input_state_get())
9079 MSG_PUTS_ATTR(" \307\321\261\333", attr); /* HANGUL */
9081 #endif
9082 #ifdef FEAT_INS_EXPAND
9083 if (edit_submode != NULL) /* CTRL-X in Insert mode */
9085 /* These messages can get long, avoid a wrap in a narrow
9086 * window. Prefer showing edit_submode_extra. */
9087 length = (Rows - msg_row) * Columns - 3;
9088 if (edit_submode_extra != NULL)
9089 length -= vim_strsize(edit_submode_extra);
9090 if (length > 0)
9092 if (edit_submode_pre != NULL)
9093 length -= vim_strsize(edit_submode_pre);
9094 if (length - vim_strsize(edit_submode) > 0)
9096 if (edit_submode_pre != NULL)
9097 msg_puts_attr(edit_submode_pre, attr);
9098 msg_puts_attr(edit_submode, attr);
9100 if (edit_submode_extra != NULL)
9102 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
9103 if ((int)edit_submode_highl < (int)HLF_COUNT)
9104 sub_attr = hl_attr(edit_submode_highl);
9105 else
9106 sub_attr = attr;
9107 msg_puts_attr(edit_submode_extra, sub_attr);
9110 length = 0;
9112 else
9113 #endif
9115 #ifdef FEAT_VREPLACE
9116 if (State & VREPLACE_FLAG)
9117 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
9118 else
9119 #endif
9120 if (State & REPLACE_FLAG)
9121 MSG_PUTS_ATTR(_(" REPLACE"), attr);
9122 else if (State & INSERT)
9124 #ifdef FEAT_RIGHTLEFT
9125 if (p_ri)
9126 MSG_PUTS_ATTR(_(" REVERSE"), attr);
9127 #endif
9128 MSG_PUTS_ATTR(_(" INSERT"), attr);
9130 else if (restart_edit == 'I')
9131 MSG_PUTS_ATTR(_(" (insert)"), attr);
9132 else if (restart_edit == 'R')
9133 MSG_PUTS_ATTR(_(" (replace)"), attr);
9134 else if (restart_edit == 'V')
9135 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
9136 #ifdef FEAT_RIGHTLEFT
9137 if (p_hkmap)
9138 MSG_PUTS_ATTR(_(" Hebrew"), attr);
9139 # ifdef FEAT_FKMAP
9140 if (p_fkmap)
9141 MSG_PUTS_ATTR(farsi_text_5, attr);
9142 # endif
9143 #endif
9144 #ifdef FEAT_KEYMAP
9145 if (State & LANGMAP)
9147 # ifdef FEAT_ARABIC
9148 if (curwin->w_p_arab)
9149 MSG_PUTS_ATTR(_(" Arabic"), attr);
9150 else
9151 # endif
9152 MSG_PUTS_ATTR(_(" (lang)"), attr);
9154 #endif
9155 if ((State & INSERT) && p_paste)
9156 MSG_PUTS_ATTR(_(" (paste)"), attr);
9158 #ifdef FEAT_VISUAL
9159 if (VIsual_active)
9161 char *p;
9163 /* Don't concatenate separate words to avoid translation
9164 * problems. */
9165 switch ((VIsual_select ? 4 : 0)
9166 + (VIsual_mode == Ctrl_V) * 2
9167 + (VIsual_mode == 'V'))
9169 case 0: p = N_(" VISUAL"); break;
9170 case 1: p = N_(" VISUAL LINE"); break;
9171 case 2: p = N_(" VISUAL BLOCK"); break;
9172 case 4: p = N_(" SELECT"); break;
9173 case 5: p = N_(" SELECT LINE"); break;
9174 default: p = N_(" SELECT BLOCK"); break;
9176 MSG_PUTS_ATTR(_(p), attr);
9178 #endif
9179 MSG_PUTS_ATTR(" --", attr);
9182 need_clear = TRUE;
9184 if (Recording
9185 #ifdef FEAT_INS_EXPAND
9186 && edit_submode == NULL /* otherwise it gets too long */
9187 #endif
9190 MSG_PUTS_ATTR(_("recording"), attr);
9191 need_clear = TRUE;
9194 mode_displayed = TRUE;
9195 if (need_clear || clear_cmdline)
9196 msg_clr_eos();
9197 msg_didout = FALSE; /* overwrite this message */
9198 length = msg_col;
9199 msg_col = 0;
9200 need_wait_return = nwr_save; /* never ask for hit-return for this */
9202 else if (clear_cmdline && msg_silent == 0)
9203 /* Clear the whole command line. Will reset "clear_cmdline". */
9204 msg_clr_cmdline();
9206 #ifdef FEAT_CMDL_INFO
9207 # ifdef FEAT_VISUAL
9208 /* In Visual mode the size of the selected area must be redrawn. */
9209 if (VIsual_active)
9210 clear_showcmd();
9211 # endif
9213 /* If the last window has no status line, the ruler is after the mode
9214 * message and must be redrawn */
9215 if (redrawing()
9216 # ifdef FEAT_WINDOWS
9217 && lastwin->w_status_height == 0
9218 # endif
9220 win_redr_ruler(lastwin, TRUE);
9221 #endif
9222 redraw_cmdline = FALSE;
9223 clear_cmdline = FALSE;
9225 return length;
9229 * Position for a mode message.
9231 static void
9232 msg_pos_mode()
9234 msg_col = 0;
9235 msg_row = Rows - 1;
9239 * Delete mode message. Used when ESC is typed which is expected to end
9240 * Insert mode (but Insert mode didn't end yet!).
9241 * Caller should check "mode_displayed".
9243 void
9244 unshowmode(force)
9245 int force;
9248 * Don't delete it right now, when not redrawing or inside a mapping.
9250 if (!redrawing() || (!force && char_avail() && !KeyTyped))
9251 redraw_cmdline = TRUE; /* delete mode later */
9252 else
9254 msg_pos_mode();
9255 if (Recording)
9256 MSG_PUTS_ATTR(_("recording"), hl_attr(HLF_CM));
9257 msg_clr_eos();
9261 #if defined(FEAT_WINDOWS)
9263 * Draw the tab pages line at the top of the Vim window.
9265 static void
9266 draw_tabline()
9268 int tabcount = 0;
9269 tabpage_T *tp;
9270 int tabwidth;
9271 int col = 0;
9272 int scol = 0;
9273 int attr;
9274 win_T *wp;
9275 win_T *cwp;
9276 int wincount;
9277 int modified;
9278 int c;
9279 int len;
9280 int attr_sel = hl_attr(HLF_TPS);
9281 int attr_nosel = hl_attr(HLF_TP);
9282 int attr_fill = hl_attr(HLF_TPF);
9283 char_u *p;
9284 int room;
9285 int use_sep_chars = (t_colors < 8
9286 #ifdef FEAT_GUI
9287 && !gui.in_use
9288 #endif
9291 redraw_tabline = FALSE;
9293 #ifdef FEAT_GUI_TABLINE
9294 /* Take care of a GUI tabline. */
9295 if (gui_use_tabline())
9297 gui_update_tabline();
9298 return;
9300 #endif
9302 if (tabline_height() < 1)
9303 return;
9305 #if defined(FEAT_STL_OPT)
9307 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
9308 for (scol = 0; scol < Columns; ++scol)
9309 TabPageIdxs[scol] = 0;
9311 /* Use the 'tabline' option if it's set. */
9312 if (*p_tal != NUL)
9314 int save_called_emsg = called_emsg;
9316 /* Check for an error. If there is one we would loop in redrawing the
9317 * screen. Avoid that by making 'tabline' empty. */
9318 called_emsg = FALSE;
9319 win_redr_custom(NULL, FALSE);
9320 if (called_emsg)
9321 set_string_option_direct((char_u *)"tabline", -1,
9322 (char_u *)"", OPT_FREE, SID_ERROR);
9323 called_emsg |= save_called_emsg;
9325 else
9326 #endif
9328 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
9329 ++tabcount;
9331 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
9332 if (tabwidth < 6)
9333 tabwidth = 6;
9335 attr = attr_nosel;
9336 tabcount = 0;
9337 scol = 0;
9338 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
9339 tp = tp->tp_next)
9341 scol = col;
9343 if (tp->tp_topframe == topframe)
9344 attr = attr_sel;
9345 if (use_sep_chars && col > 0)
9346 screen_putchar('|', 0, col++, attr);
9348 if (tp->tp_topframe != topframe)
9349 attr = attr_nosel;
9351 screen_putchar(' ', 0, col++, attr);
9353 if (tp == curtab)
9355 cwp = curwin;
9356 wp = firstwin;
9358 else
9360 cwp = tp->tp_curwin;
9361 wp = tp->tp_firstwin;
9364 modified = FALSE;
9365 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
9366 if (bufIsChanged(wp->w_buffer))
9367 modified = TRUE;
9368 if (modified || wincount > 1)
9370 if (wincount > 1)
9372 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
9373 len = (int)STRLEN(NameBuff);
9374 if (col + len >= Columns - 3)
9375 break;
9376 screen_puts_len(NameBuff, len, 0, col,
9377 #if defined(FEAT_SYN_HL)
9378 hl_combine_attr(attr, hl_attr(HLF_T))
9379 #else
9380 attr
9381 #endif
9383 col += len;
9385 if (modified)
9386 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
9387 screen_putchar(' ', 0, col++, attr);
9390 room = scol - col + tabwidth - 1;
9391 if (room > 0)
9393 /* Get buffer name in NameBuff[] */
9394 get_trans_bufname(cwp->w_buffer);
9395 shorten_dir(NameBuff);
9396 len = vim_strsize(NameBuff);
9397 p = NameBuff;
9398 #ifdef FEAT_MBYTE
9399 if (has_mbyte)
9400 while (len > room)
9402 len -= ptr2cells(p);
9403 mb_ptr_adv(p);
9405 else
9406 #endif
9407 if (len > room)
9409 p += len - room;
9410 len = room;
9412 if (len > Columns - col - 1)
9413 len = Columns - col - 1;
9415 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
9416 col += len;
9418 screen_putchar(' ', 0, col++, attr);
9420 /* Store the tab page number in TabPageIdxs[], so that
9421 * jump_to_mouse() knows where each one is. */
9422 ++tabcount;
9423 while (scol < col)
9424 TabPageIdxs[scol++] = tabcount;
9427 if (use_sep_chars)
9428 c = '_';
9429 else
9430 c = ' ';
9431 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
9433 /* Put an "X" for closing the current tab if there are several. */
9434 if (first_tabpage->tp_next != NULL)
9436 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
9437 TabPageIdxs[Columns - 1] = -999;
9441 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
9442 * set. */
9443 redraw_tabline = FALSE;
9447 * Get buffer name for "buf" into NameBuff[].
9448 * Takes care of special buffer names and translates special characters.
9450 void
9451 get_trans_bufname(buf)
9452 buf_T *buf;
9454 if (buf_spname(buf) != NULL)
9455 STRCPY(NameBuff, buf_spname(buf));
9456 else
9457 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
9458 trans_characters(NameBuff, MAXPATHL);
9460 #endif
9462 #if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
9464 * Get the character to use in a status line. Get its attributes in "*attr".
9466 static int
9467 fillchar_status(attr, is_curwin)
9468 int *attr;
9469 int is_curwin;
9471 int fill;
9472 if (is_curwin)
9474 *attr = hl_attr(HLF_S);
9475 fill = fill_stl;
9477 else
9479 *attr = hl_attr(HLF_SNC);
9480 fill = fill_stlnc;
9482 /* Use fill when there is highlighting, and highlighting of current
9483 * window differs, or the fillchars differ, or this is not the
9484 * current window */
9485 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
9486 || !is_curwin || firstwin == lastwin)
9487 || (fill_stl != fill_stlnc)))
9488 return fill;
9489 if (is_curwin)
9490 return '^';
9491 return '=';
9493 #endif
9495 #ifdef FEAT_VERTSPLIT
9497 * Get the character to use in a separator between vertically split windows.
9498 * Get its attributes in "*attr".
9500 static int
9501 fillchar_vsep(attr)
9502 int *attr;
9504 *attr = hl_attr(HLF_C);
9505 if (*attr == 0 && fill_vert == ' ')
9506 return '|';
9507 else
9508 return fill_vert;
9510 #endif
9513 * Return TRUE if redrawing should currently be done.
9516 redrawing()
9518 return (!RedrawingDisabled
9519 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
9523 * Return TRUE if printing messages should currently be done.
9526 messaging()
9528 return (!(p_lz && char_avail() && !KeyTyped));
9532 * Show current status info in ruler and various other places
9533 * If always is FALSE, only show ruler if position has changed.
9535 void
9536 showruler(always)
9537 int always;
9539 if (!always && !redrawing())
9540 return;
9541 #ifdef FEAT_INS_EXPAND
9542 if (pum_visible())
9544 # ifdef FEAT_WINDOWS
9545 /* Don't redraw right now, do it later. */
9546 curwin->w_redr_status = TRUE;
9547 # endif
9548 return;
9550 #endif
9551 #if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
9552 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
9554 redraw_custom_statusline(curwin);
9556 else
9557 #endif
9558 #ifdef FEAT_CMDL_INFO
9559 win_redr_ruler(curwin, always);
9560 #endif
9562 #ifdef FEAT_TITLE
9563 if (need_maketitle
9564 # ifdef FEAT_STL_OPT
9565 || (p_icon && (stl_syntax & STL_IN_ICON))
9566 || (p_title && (stl_syntax & STL_IN_TITLE))
9567 # endif
9569 maketitle();
9570 #endif
9571 #ifdef FEAT_WINDOWS
9572 /* Redraw the tab pages line if needed. */
9573 if (redraw_tabline)
9574 draw_tabline();
9575 #endif
9578 #ifdef FEAT_CMDL_INFO
9579 static void
9580 win_redr_ruler(wp, always)
9581 win_T *wp;
9582 int always;
9584 #define RULER_BUF_LEN 70
9585 char_u buffer[RULER_BUF_LEN];
9586 int row;
9587 int fillchar;
9588 int attr;
9589 int empty_line = FALSE;
9590 colnr_T virtcol;
9591 int i;
9592 size_t len;
9593 int o;
9594 #ifdef FEAT_VERTSPLIT
9595 int this_ru_col;
9596 int off = 0;
9597 int width = Columns;
9598 # define WITH_OFF(x) x
9599 # define WITH_WIDTH(x) x
9600 #else
9601 # define WITH_OFF(x) 0
9602 # define WITH_WIDTH(x) Columns
9603 # define this_ru_col ru_col
9604 #endif
9606 /* If 'ruler' off or redrawing disabled, don't do anything */
9607 if (!p_ru)
9608 return;
9611 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
9612 * after deleting lines, before cursor.lnum is corrected.
9614 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
9615 return;
9617 #ifdef FEAT_INS_EXPAND
9618 /* Don't draw the ruler while doing insert-completion, it might overwrite
9619 * the (long) mode message. */
9620 # ifdef FEAT_WINDOWS
9621 if (wp == lastwin && lastwin->w_status_height == 0)
9622 # endif
9623 if (edit_submode != NULL)
9624 return;
9625 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
9626 if (pum_visible())
9627 return;
9628 #endif
9630 #ifdef FEAT_STL_OPT
9631 if (*p_ruf)
9633 int save_called_emsg = called_emsg;
9635 called_emsg = FALSE;
9636 win_redr_custom(wp, TRUE);
9637 if (called_emsg)
9638 set_string_option_direct((char_u *)"rulerformat", -1,
9639 (char_u *)"", OPT_FREE, SID_ERROR);
9640 called_emsg |= save_called_emsg;
9641 return;
9643 #endif
9646 * Check if not in Insert mode and the line is empty (will show "0-1").
9648 if (!(State & INSERT)
9649 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
9650 empty_line = TRUE;
9653 * Only draw the ruler when something changed.
9655 validate_virtcol_win(wp);
9656 if ( redraw_cmdline
9657 || always
9658 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
9659 || wp->w_cursor.col != wp->w_ru_cursor.col
9660 || wp->w_virtcol != wp->w_ru_virtcol
9661 #ifdef FEAT_VIRTUALEDIT
9662 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
9663 #endif
9664 || wp->w_topline != wp->w_ru_topline
9665 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
9666 #ifdef FEAT_DIFF
9667 || wp->w_topfill != wp->w_ru_topfill
9668 #endif
9669 || empty_line != wp->w_ru_empty)
9671 cursor_off();
9672 #ifdef FEAT_WINDOWS
9673 if (wp->w_status_height)
9675 row = W_WINROW(wp) + wp->w_height;
9676 fillchar = fillchar_status(&attr, wp == curwin);
9677 # ifdef FEAT_VERTSPLIT
9678 off = W_WINCOL(wp);
9679 width = W_WIDTH(wp);
9680 # endif
9682 else
9683 #endif
9685 row = Rows - 1;
9686 fillchar = ' ';
9687 attr = 0;
9688 #ifdef FEAT_VERTSPLIT
9689 width = Columns;
9690 off = 0;
9691 #endif
9694 /* In list mode virtcol needs to be recomputed */
9695 virtcol = wp->w_virtcol;
9696 if (wp->w_p_list && lcs_tab1 == NUL)
9698 wp->w_p_list = FALSE;
9699 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
9700 wp->w_p_list = TRUE;
9704 * Some sprintfs return the length, some return a pointer.
9705 * To avoid portability problems we use strlen() here.
9707 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
9708 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
9709 ? 0L
9710 : (long)(wp->w_cursor.lnum));
9711 len = STRLEN(buffer);
9712 col_print(buffer + len, RULER_BUF_LEN - len,
9713 empty_line ? 0 : (int)wp->w_cursor.col + 1,
9714 (int)virtcol + 1);
9717 * Add a "50%" if there is room for it.
9718 * On the last line, don't print in the last column (scrolls the
9719 * screen up on some terminals).
9721 i = (int)STRLEN(buffer);
9722 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
9723 o = i + vim_strsize(buffer + i + 1);
9724 #ifdef FEAT_WINDOWS
9725 if (wp->w_status_height == 0) /* can't use last char of screen */
9726 #endif
9727 ++o;
9728 #ifdef FEAT_VERTSPLIT
9729 this_ru_col = ru_col - (Columns - width);
9730 if (this_ru_col < 0)
9731 this_ru_col = 0;
9732 #endif
9733 /* Never use more than half the window/screen width, leave the other
9734 * half for the filename. */
9735 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
9736 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
9737 if (this_ru_col + o < WITH_WIDTH(width))
9739 while (this_ru_col + o < WITH_WIDTH(width))
9741 #ifdef FEAT_MBYTE
9742 if (has_mbyte)
9743 i += (*mb_char2bytes)(fillchar, buffer + i);
9744 else
9745 #endif
9746 buffer[i++] = fillchar;
9747 ++o;
9749 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
9751 /* Truncate at window boundary. */
9752 #ifdef FEAT_MBYTE
9753 if (has_mbyte)
9755 o = 0;
9756 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
9758 o += (*mb_ptr2cells)(buffer + i);
9759 if (this_ru_col + o > WITH_WIDTH(width))
9761 buffer[i] = NUL;
9762 break;
9766 else
9767 #endif
9768 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
9769 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
9771 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
9772 i = redraw_cmdline;
9773 screen_fill(row, row + 1,
9774 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
9775 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
9776 fillchar, fillchar, attr);
9777 /* don't redraw the cmdline because of showing the ruler */
9778 redraw_cmdline = i;
9779 wp->w_ru_cursor = wp->w_cursor;
9780 wp->w_ru_virtcol = wp->w_virtcol;
9781 wp->w_ru_empty = empty_line;
9782 wp->w_ru_topline = wp->w_topline;
9783 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
9784 #ifdef FEAT_DIFF
9785 wp->w_ru_topfill = wp->w_topfill;
9786 #endif
9789 #endif
9791 #if defined(FEAT_LINEBREAK) || defined(PROTO)
9793 * Return the width of the 'number' column.
9794 * Caller may need to check if 'number' is set.
9795 * Otherwise it depends on 'numberwidth' and the line count.
9798 number_width(wp)
9799 win_T *wp;
9801 int n;
9802 linenr_T lnum;
9804 lnum = wp->w_buffer->b_ml.ml_line_count;
9805 if (lnum == wp->w_nrwidth_line_count)
9806 return wp->w_nrwidth_width;
9807 wp->w_nrwidth_line_count = lnum;
9809 n = 0;
9812 lnum /= 10;
9813 ++n;
9814 } while (lnum > 0);
9816 /* 'numberwidth' gives the minimal width plus one */
9817 if (n < wp->w_p_nuw - 1)
9818 n = wp->w_p_nuw - 1;
9820 wp->w_nrwidth_width = n;
9821 return n;
9823 #endif