Merge branch 'vim' into feat/rel-line-numbers
[vim_extended.git] / src / screen.c
blob4518bdd4fbfe1af75b4e0098279250292ea75547
1 /* vi:set ts=8 sts=4 sw=4:
3 * VIM - Vi IMproved by Bram Moolenaar
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
11 * screen.c: code for displaying on the screen
13 * Output to the screen (console, terminal emulator or GUI window) is minimized
14 * by remembering what is already on the screen, and only updating the parts
15 * that changed.
17 * ScreenLines[off] Contains a copy of the whole screen, as it is currently
18 * displayed (excluding text written by external commands).
19 * ScreenAttrs[off] Contains the associated attributes.
20 * LineOffset[row] Contains the offset into ScreenLines*[] and ScreenAttrs[]
21 * for each line.
22 * LineWraps[row] Flag for each line whether it wraps to the next line.
24 * For double-byte characters, two consecutive bytes in ScreenLines[] can form
25 * one character which occupies two display cells.
26 * For UTF-8 a multi-byte character is converted to Unicode and stored in
27 * ScreenLinesUC[]. ScreenLines[] contains the first byte only. For an ASCII
28 * character without composing chars ScreenLinesUC[] will be 0. When the
29 * character occupies two display cells the next byte in ScreenLines[] is 0.
30 * ScreenLinesC[][] contain up to 'maxcombine' composing characters
31 * (drawn on top of the first character). They are 0 when not used.
32 * ScreenLines2[] is only used for euc-jp to store the second byte if the
33 * first byte is 0x8e (single-width character).
35 * The screen_*() functions write to the screen and handle updating
36 * ScreenLines[].
38 * update_screen() is the function that updates all windows and status lines.
39 * It is called form the main loop when must_redraw is non-zero. It may be
40 * called from other places when an immediate screen update is needed.
42 * The part of the buffer that is displayed in a window is set with:
43 * - w_topline (first buffer line in window)
44 * - w_topfill (filler line above the first line)
45 * - w_leftcol (leftmost window cell in window),
46 * - w_skipcol (skipped window cells of first line)
48 * Commands that only move the cursor around in a window, do not need to take
49 * action to update the display. The main loop will check if w_topline is
50 * valid and update it (scroll the window) when needed.
52 * Commands that scroll a window change w_topline and must call
53 * check_cursor() to move the cursor into the visible part of the window, and
54 * call redraw_later(VALID) to have the window displayed by update_screen()
55 * later.
57 * Commands that change text in the buffer must call changed_bytes() or
58 * changed_lines() to mark the area that changed and will require updating
59 * later. The main loop will call update_screen(), which will update each
60 * window that shows the changed buffer. This assumes text above the change
61 * can remain displayed as it is. Text after the change may need updating for
62 * scrolling, folding and syntax highlighting.
64 * Commands that change how a window is displayed (e.g., setting 'list') or
65 * invalidate the contents of a window in another way (e.g., change fold
66 * settings), must call redraw_later(NOT_VALID) to have the whole window
67 * redisplayed by update_screen() later.
69 * Commands that change how a buffer is displayed (e.g., setting 'tabstop')
70 * must call redraw_curbuf_later(NOT_VALID) to have all the windows for the
71 * buffer redisplayed by update_screen() later.
73 * Commands that change highlighting and possibly cause a scroll too must call
74 * redraw_later(SOME_VALID) to update the whole window but still use scrolling
75 * to avoid redrawing everything. But the length of displayed lines must not
76 * change, use NOT_VALID then.
78 * Commands that move the window position must call redraw_later(NOT_VALID).
79 * TODO: should minimize redrawing by scrolling when possible.
81 * Commands that change everything (e.g., resizing the screen) must call
82 * redraw_all_later(NOT_VALID) or redraw_all_later(CLEAR).
84 * Things that are handled indirectly:
85 * - When messages scroll the screen up, msg_scrolled will be set and
86 * update_screen() called to redraw.
89 #include "vim.h"
92 * The attributes that are actually active for writing to the screen.
94 static int screen_attr = 0;
97 * Positioning the cursor is reduced by remembering the last position.
98 * Mostly used by windgoto() and screen_char().
100 static int screen_cur_row, screen_cur_col; /* last known cursor position */
102 #ifdef FEAT_SEARCH_EXTRA
103 static match_T search_hl; /* used for 'hlsearch' highlight matching */
104 #endif
106 #ifdef FEAT_FOLDING
107 static foldinfo_T win_foldinfo; /* info for 'foldcolumn' */
108 #endif
111 * Buffer for one screen line (characters and attributes).
113 static schar_T *current_ScreenLine;
115 static void win_update __ARGS((win_T *wp));
116 static void win_draw_end __ARGS((win_T *wp, int c1, int c2, int row, int endrow, hlf_T hl));
117 #ifdef FEAT_FOLDING
118 static void fold_line __ARGS((win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row));
119 static void fill_foldcolumn __ARGS((char_u *p, win_T *wp, int closed, linenr_T lnum));
120 static void copy_text_attr __ARGS((int off, char_u *buf, int len, int attr));
121 #endif
122 static int win_line __ARGS((win_T *, linenr_T, int, int, int nochange));
123 static int char_needs_redraw __ARGS((int off_from, int off_to, int cols));
124 #ifdef FEAT_RIGHTLEFT
125 static void screen_line __ARGS((int row, int coloff, int endcol, int clear_width, int rlflag));
126 # define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c), (rl))
127 #else
128 static void screen_line __ARGS((int row, int coloff, int endcol, int clear_width));
129 # define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c))
130 #endif
131 #ifdef FEAT_VERTSPLIT
132 static void draw_vsep_win __ARGS((win_T *wp, int row));
133 #endif
134 #ifdef FEAT_STL_OPT
135 static void redraw_custum_statusline __ARGS((win_T *wp));
136 #endif
137 #ifdef FEAT_SEARCH_EXTRA
138 #define SEARCH_HL_PRIORITY 0
139 static void start_search_hl __ARGS((void));
140 static void end_search_hl __ARGS((void));
141 static void prepare_search_hl __ARGS((win_T *wp, linenr_T lnum));
142 static void next_search_hl __ARGS((win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol));
143 #endif
144 static void screen_start_highlight __ARGS((int attr));
145 static void screen_char __ARGS((unsigned off, int row, int col));
146 #ifdef FEAT_MBYTE
147 static void screen_char_2 __ARGS((unsigned off, int row, int col));
148 #endif
149 static void screenclear2 __ARGS((void));
150 static void lineclear __ARGS((unsigned off, int width));
151 static void lineinvalid __ARGS((unsigned off, int width));
152 #ifdef FEAT_VERTSPLIT
153 static void linecopy __ARGS((int to, int from, win_T *wp));
154 static void redraw_block __ARGS((int row, int end, win_T *wp));
155 #endif
156 static int win_do_lines __ARGS((win_T *wp, int row, int line_count, int mayclear, int del));
157 static void win_rest_invalid __ARGS((win_T *wp));
158 static void msg_pos_mode __ARGS((void));
159 #if defined(FEAT_WINDOWS)
160 static void draw_tabline __ARGS((void));
161 #endif
162 #if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
163 static int fillchar_status __ARGS((int *attr, int is_curwin));
164 #endif
165 #ifdef FEAT_VERTSPLIT
166 static int fillchar_vsep __ARGS((int *attr));
167 #endif
168 #ifdef FEAT_STL_OPT
169 static void win_redr_custom __ARGS((win_T *wp, int draw_ruler));
170 #endif
171 #ifdef FEAT_CMDL_INFO
172 static void win_redr_ruler __ARGS((win_T *wp, int always));
173 #endif
175 #if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
176 /* Ugly global: overrule attribute used by screen_char() */
177 static int screen_char_attr = 0;
178 #endif
181 * Redraw the current window later, with update_screen(type).
182 * Set must_redraw only if not already set to a higher value.
183 * e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
185 void
186 redraw_later(type)
187 int type;
189 redraw_win_later(curwin, type);
192 void
193 redraw_win_later(wp, type)
194 win_T *wp;
195 int type;
197 if (wp->w_redr_type < type)
199 wp->w_redr_type = type;
200 if (type >= NOT_VALID)
201 wp->w_lines_valid = 0;
202 if (must_redraw < type) /* must_redraw is the maximum of all windows */
203 must_redraw = type;
208 * Force a complete redraw later. Also resets the highlighting. To be used
209 * after executing a shell command that messes up the screen.
211 void
212 redraw_later_clear()
214 redraw_all_later(CLEAR);
215 #ifdef FEAT_GUI
216 if (gui.in_use)
217 /* Use a code that will reset gui.highlight_mask in
218 * gui_stop_highlight(). */
219 screen_attr = HL_ALL + 1;
220 else
221 #endif
222 /* Use attributes that is very unlikely to appear in text. */
223 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE;
227 * Mark all windows to be redrawn later.
229 void
230 redraw_all_later(type)
231 int type;
233 win_T *wp;
235 FOR_ALL_WINDOWS(wp)
237 redraw_win_later(wp, type);
242 * Mark all windows that are editing the current buffer to be updated later.
244 void
245 redraw_curbuf_later(type)
246 int type;
248 redraw_buf_later(curbuf, type);
251 void
252 redraw_buf_later(buf, type)
253 buf_T *buf;
254 int type;
256 win_T *wp;
258 FOR_ALL_WINDOWS(wp)
260 if (wp->w_buffer == buf)
261 redraw_win_later(wp, type);
266 * Changed something in the current window, at buffer line "lnum", that
267 * requires that line and possibly other lines to be redrawn.
268 * Used when entering/leaving Insert mode with the cursor on a folded line.
269 * Used to remove the "$" from a change command.
270 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
271 * may become invalid and the whole window will have to be redrawn.
273 /*ARGSUSED*/
274 void
275 redrawWinline(lnum, invalid)
276 linenr_T lnum;
277 int invalid; /* 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 if (!screen_valid(TRUE))
328 return;
330 if (must_redraw)
332 if (type < must_redraw) /* use maximal type */
333 type = must_redraw;
335 /* must_redraw is reset here, so that when we run into some weird
336 * reason to redraw while busy redrawing (e.g., asynchronous
337 * scrolling), or update_topline() in win_update() will cause a
338 * scroll, the screen will be redrawn later or in win_update(). */
339 must_redraw = 0;
342 /* Need to update w_lines[]. */
343 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
344 type = NOT_VALID;
346 if (!redrawing())
348 redraw_later(type); /* remember type for next time */
349 must_redraw = type;
350 if (type > INVERTED_ALL)
351 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
352 return;
355 updating_screen = TRUE;
356 #ifdef FEAT_SYN_HL
357 ++display_tick; /* let syntax code know we're in a next round of
358 * display updating */
359 #endif
362 * if the screen was scrolled up when displaying a message, scroll it down
364 if (msg_scrolled)
366 clear_cmdline = TRUE;
367 if (msg_scrolled > Rows - 5) /* clearing is faster */
368 type = CLEAR;
369 else if (type != CLEAR)
371 check_for_delay(FALSE);
372 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, NULL) == FAIL)
373 type = CLEAR;
374 FOR_ALL_WINDOWS(wp)
376 if (W_WINROW(wp) < msg_scrolled)
378 if (W_WINROW(wp) + wp->w_height > msg_scrolled
379 && wp->w_redr_type < REDRAW_TOP
380 && wp->w_lines_valid > 0
381 && wp->w_topline == wp->w_lines[0].wl_lnum)
383 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
384 wp->w_redr_type = REDRAW_TOP;
386 else
388 wp->w_redr_type = NOT_VALID;
389 #ifdef FEAT_WINDOWS
390 if (W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp)
391 <= msg_scrolled)
392 wp->w_redr_status = TRUE;
393 #endif
397 redraw_cmdline = TRUE;
398 #ifdef FEAT_WINDOWS
399 redraw_tabline = TRUE;
400 #endif
402 msg_scrolled = 0;
403 need_wait_return = FALSE;
406 /* reset cmdline_row now (may have been changed temporarily) */
407 compute_cmdrow();
409 /* Check for changed highlighting */
410 if (need_highlight_changed)
411 highlight_changed();
413 if (type == CLEAR) /* first clear screen */
415 screenclear(); /* will reset clear_cmdline */
416 type = NOT_VALID;
419 if (clear_cmdline) /* going to clear cmdline (done below) */
420 check_for_delay(FALSE);
422 #ifdef FEAT_LINEBREAK
423 /* Force redraw when width of 'number' or 'relativenumber' column
424 * changes. */
425 if (curwin->w_redr_type < NOT_VALID
426 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
427 ? number_width(curwin) : 0))
428 curwin->w_redr_type = NOT_VALID;
429 #endif
432 * Only start redrawing if there is really something to do.
434 if (type == INVERTED)
435 update_curswant();
436 if (curwin->w_redr_type < type
437 && !((type == VALID
438 && curwin->w_lines[0].wl_valid
439 #ifdef FEAT_DIFF
440 && curwin->w_topfill == curwin->w_old_topfill
441 && curwin->w_botfill == curwin->w_old_botfill
442 #endif
443 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
444 #ifdef FEAT_VISUAL
445 || (type == INVERTED
446 && VIsual_active
447 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
448 && curwin->w_old_visual_mode == VIsual_mode
449 && (curwin->w_valid & VALID_VIRTCOL)
450 && curwin->w_old_curswant == curwin->w_curswant)
451 #endif
453 curwin->w_redr_type = type;
455 #ifdef FEAT_WINDOWS
456 /* Redraw the tab pages line if needed. */
457 if (redraw_tabline || type >= NOT_VALID)
458 draw_tabline();
459 #endif
461 #ifdef FEAT_SYN_HL
463 * Correct stored syntax highlighting info for changes in each displayed
464 * buffer. Each buffer must only be done once.
466 FOR_ALL_WINDOWS(wp)
468 if (wp->w_buffer->b_mod_set)
470 # ifdef FEAT_WINDOWS
471 win_T *wwp;
473 /* Check if we already did this buffer. */
474 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
475 if (wwp->w_buffer == wp->w_buffer)
476 break;
477 # endif
478 if (
479 # ifdef FEAT_WINDOWS
480 wwp == wp &&
481 # endif
482 syntax_present(wp->w_buffer))
483 syn_stack_apply_changes(wp->w_buffer);
486 #endif
489 * Go from top to bottom through the windows, redrawing the ones that need
490 * it.
492 #if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
493 did_one = FALSE;
494 #endif
495 #ifdef FEAT_SEARCH_EXTRA
496 search_hl.rm.regprog = NULL;
497 #endif
498 FOR_ALL_WINDOWS(wp)
500 if (wp->w_redr_type != 0)
502 cursor_off();
503 #if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
504 if (!did_one)
506 did_one = TRUE;
507 # ifdef FEAT_SEARCH_EXTRA
508 start_search_hl();
509 # endif
510 # ifdef FEAT_CLIPBOARD
511 /* When Visual area changed, may have to update selection. */
512 if (clip_star.available && clip_isautosel())
513 clip_update_selection();
514 # endif
515 #ifdef FEAT_GUI
516 /* Remove the cursor before starting to do anything, because
517 * scrolling may make it difficult to redraw the text under
518 * it. */
519 if (gui.in_use)
520 gui_undraw_cursor();
521 #endif
523 #endif
524 win_update(wp);
527 #ifdef FEAT_WINDOWS
528 /* redraw status line after the window to minimize cursor movement */
529 if (wp->w_redr_status)
531 cursor_off();
532 win_redr_status(wp);
534 #endif
536 #if defined(FEAT_SEARCH_EXTRA)
537 end_search_hl();
538 #endif
540 #ifdef FEAT_WINDOWS
541 /* Reset b_mod_set flags. Going through all windows is probably faster
542 * than going through all buffers (there could be many buffers). */
543 for (wp = firstwin; wp != NULL; wp = wp->w_next)
544 wp->w_buffer->b_mod_set = FALSE;
545 #else
546 curbuf->b_mod_set = FALSE;
547 #endif
549 updating_screen = FALSE;
550 #ifdef FEAT_GUI
551 gui_may_resize_shell();
552 #endif
554 /* Clear or redraw the command line. Done last, because scrolling may
555 * mess up the command line. */
556 if (clear_cmdline || redraw_cmdline)
557 showmode();
559 /* May put up an introductory message when not editing a file */
560 if (!did_intro && bufempty()
561 && curbuf->b_fname == NULL
562 #ifdef FEAT_WINDOWS
563 && firstwin->w_next == NULL
564 #endif
565 && vim_strchr(p_shm, SHM_INTRO) == NULL)
566 intro_message(FALSE);
567 did_intro = TRUE;
569 #ifdef FEAT_GUI
570 /* Redraw the cursor and update the scrollbars when all screen updating is
571 * done. */
572 if (gui.in_use)
574 out_flush(); /* required before updating the cursor */
575 if (did_one)
576 gui_update_cursor(FALSE, FALSE);
577 gui_update_scrollbars(FALSE);
579 #endif
582 #if defined(FEAT_SIGNS) || defined(FEAT_GUI)
583 static void update_prepare __ARGS((void));
584 static void update_finish __ARGS((void));
587 * Prepare for updating one or more windows.
589 static void
590 update_prepare()
592 cursor_off();
593 updating_screen = TRUE;
594 #ifdef FEAT_GUI
595 /* Remove the cursor before starting to do anything, because scrolling may
596 * make it difficult to redraw the text under it. */
597 if (gui.in_use)
598 gui_undraw_cursor();
599 #endif
600 #ifdef FEAT_SEARCH_EXTRA
601 start_search_hl();
602 #endif
606 * Finish updating one or more windows.
608 static void
609 update_finish()
611 if (redraw_cmdline)
612 showmode();
614 # ifdef FEAT_SEARCH_EXTRA
615 end_search_hl();
616 # endif
618 updating_screen = FALSE;
620 # ifdef FEAT_GUI
621 gui_may_resize_shell();
623 /* Redraw the cursor and update the scrollbars when all screen updating is
624 * done. */
625 if (gui.in_use)
627 out_flush(); /* required before updating the cursor */
628 gui_update_cursor(FALSE, FALSE);
629 gui_update_scrollbars(FALSE);
631 # endif
633 #endif
635 #if defined(FEAT_SIGNS) || defined(PROTO)
636 void
637 update_debug_sign(buf, lnum)
638 buf_T *buf;
639 linenr_T lnum;
641 win_T *wp;
642 int doit = FALSE;
644 # ifdef FEAT_FOLDING
645 win_foldinfo.fi_level = 0;
646 # endif
648 /* update/delete a specific mark */
649 FOR_ALL_WINDOWS(wp)
651 if (buf != NULL && lnum > 0)
653 if (wp->w_buffer == buf && lnum >= wp->w_topline
654 && lnum < wp->w_botline)
656 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
657 wp->w_redraw_top = lnum;
658 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
659 wp->w_redraw_bot = lnum;
660 redraw_win_later(wp, VALID);
663 else
664 redraw_win_later(wp, VALID);
665 if (wp->w_redr_type != 0)
666 doit = TRUE;
669 if (!doit)
670 return;
672 /* update all windows that need updating */
673 update_prepare();
675 # ifdef FEAT_WINDOWS
676 for (wp = firstwin; wp; wp = wp->w_next)
678 if (wp->w_redr_type != 0)
679 win_update(wp);
680 if (wp->w_redr_status)
681 win_redr_status(wp);
683 # else
684 if (curwin->w_redr_type != 0)
685 win_update(curwin);
686 # endif
688 update_finish();
690 #endif
693 #if defined(FEAT_GUI) || defined(PROTO)
695 * Update a single window, its status line and maybe the command line msg.
696 * Used for the GUI scrollbar.
698 void
699 updateWindow(wp)
700 win_T *wp;
702 update_prepare();
704 #ifdef FEAT_CLIPBOARD
705 /* When Visual area changed, may have to update selection. */
706 if (clip_star.available && clip_isautosel())
707 clip_update_selection();
708 #endif
710 win_update(wp);
712 #ifdef FEAT_WINDOWS
713 /* When the screen was cleared redraw the tab pages line. */
714 if (redraw_tabline)
715 draw_tabline();
717 if (wp->w_redr_status
718 # ifdef FEAT_CMDL_INFO
719 || p_ru
720 # endif
721 # ifdef FEAT_STL_OPT
722 || *p_stl != NUL || *wp->w_p_stl != NUL
723 # endif
725 win_redr_status(wp);
726 #endif
728 update_finish();
730 #endif
733 * Update a single window.
735 * This may cause the windows below it also to be redrawn (when clearing the
736 * screen or scrolling lines).
738 * How the window is redrawn depends on wp->w_redr_type. Each type also
739 * implies the one below it.
740 * NOT_VALID redraw the whole window
741 * SOME_VALID redraw the whole window but do scroll when possible
742 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
743 * INVERTED redraw the changed part of the Visual area
744 * INVERTED_ALL redraw the whole Visual area
745 * VALID 1. scroll up/down to adjust for a changed w_topline
746 * 2. update lines at the top when scrolled down
747 * 3. redraw changed text:
748 * - if wp->w_buffer->b_mod_set set, update lines between
749 * b_mod_top and b_mod_bot.
750 * - if wp->w_redraw_top non-zero, redraw lines between
751 * wp->w_redraw_top and wp->w_redr_bot.
752 * - continue redrawing when syntax status is invalid.
753 * 4. if scrolled up, update lines at the bottom.
754 * This results in three areas that may need updating:
755 * top: from first row to top_end (when scrolled down)
756 * mid: from mid_start to mid_end (update inversion or changed text)
757 * bot: from bot_start to last row (when scrolled up)
759 static void
760 win_update(wp)
761 win_T *wp;
763 buf_T *buf = wp->w_buffer;
764 int type;
765 int top_end = 0; /* Below last row of the top area that needs
766 updating. 0 when no top area updating. */
767 int mid_start = 999;/* first row of the mid area that needs
768 updating. 999 when no mid area updating. */
769 int mid_end = 0; /* Below last row of the mid area that needs
770 updating. 0 when no mid area updating. */
771 int bot_start = 999;/* first row of the bot area that needs
772 updating. 999 when no bot area updating */
773 #ifdef FEAT_VISUAL
774 int scrolled_down = FALSE; /* TRUE when scrolled down when
775 w_topline got smaller a bit */
776 #endif
777 #ifdef FEAT_SEARCH_EXTRA
778 matchitem_T *cur; /* points to the match list */
779 int top_to_mod = FALSE; /* redraw above mod_top */
780 #endif
782 int row; /* current window row to display */
783 linenr_T lnum; /* current buffer lnum to display */
784 int idx; /* current index in w_lines[] */
785 int srow; /* starting row of the current line */
787 int eof = FALSE; /* if TRUE, we hit the end of the file */
788 int didline = FALSE; /* if TRUE, we finished the last line */
789 int i;
790 long j;
791 static int recursive = FALSE; /* being called recursively */
792 int old_botline = wp->w_botline;
793 #ifdef FEAT_FOLDING
794 long fold_count;
795 #endif
796 #ifdef FEAT_SYN_HL
797 /* remember what happened to the previous line, to know if
798 * check_visual_highlight() can be used */
799 #define DID_NONE 1 /* didn't update a line */
800 #define DID_LINE 2 /* updated a normal line */
801 #define DID_FOLD 3 /* updated a folded line */
802 int did_update = DID_NONE;
803 linenr_T syntax_last_parsed = 0; /* last parsed text line */
804 #endif
805 linenr_T mod_top = 0;
806 linenr_T mod_bot = 0;
807 #if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
808 int save_got_int;
809 #endif
811 type = wp->w_redr_type;
813 if (type == NOT_VALID)
815 #ifdef FEAT_WINDOWS
816 wp->w_redr_status = TRUE;
817 #endif
818 wp->w_lines_valid = 0;
821 /* Window is zero-height: nothing to draw. */
822 if (wp->w_height == 0)
824 wp->w_redr_type = 0;
825 return;
828 #ifdef FEAT_VERTSPLIT
829 /* Window is zero-width: Only need to draw the separator. */
830 if (wp->w_width == 0)
832 /* draw the vertical separator right of this window */
833 draw_vsep_win(wp, 0);
834 wp->w_redr_type = 0;
835 return;
837 #endif
839 #ifdef FEAT_SEARCH_EXTRA
840 /* Setup for match and 'hlsearch' highlighting. Disable any previous
841 * match */
842 cur = wp->w_match_head;
843 while (cur != NULL)
845 cur->hl.rm = cur->match;
846 if (cur->hlg_id == 0)
847 cur->hl.attr = 0;
848 else
849 cur->hl.attr = syn_id2attr(cur->hlg_id);
850 cur->hl.buf = buf;
851 cur->hl.lnum = 0;
852 cur->hl.first_lnum = 0;
853 # ifdef FEAT_RELTIME
854 /* Set the time limit to 'redrawtime'. */
855 profile_setlimit(p_rdt, &(cur->hl.tm));
856 # endif
857 cur = cur->next;
859 search_hl.buf = buf;
860 search_hl.lnum = 0;
861 search_hl.first_lnum = 0;
862 /* time limit is set at the toplevel, for all windows */
863 #endif
865 #ifdef FEAT_LINEBREAK
866 /* Force redraw when width of 'number' or 'relativenumber' column
867 * changes. */
868 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
869 if (wp->w_nrwidth != i)
871 type = NOT_VALID;
872 wp->w_nrwidth = i;
874 else
875 #endif
877 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
880 * When there are both inserted/deleted lines and specific lines to be
881 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
882 * everything (only happens when redrawing is off for while).
884 type = NOT_VALID;
886 else
889 * Set mod_top to the first line that needs displaying because of
890 * changes. Set mod_bot to the first line after the changes.
892 mod_top = wp->w_redraw_top;
893 if (wp->w_redraw_bot != 0)
894 mod_bot = wp->w_redraw_bot + 1;
895 else
896 mod_bot = 0;
897 wp->w_redraw_top = 0; /* reset for next time */
898 wp->w_redraw_bot = 0;
899 if (buf->b_mod_set)
901 if (mod_top == 0 || mod_top > buf->b_mod_top)
903 mod_top = buf->b_mod_top;
904 #ifdef FEAT_SYN_HL
905 /* Need to redraw lines above the change that may be included
906 * in a pattern match. */
907 if (syntax_present(buf))
909 mod_top -= buf->b_syn_sync_linebreaks;
910 if (mod_top < 1)
911 mod_top = 1;
913 #endif
915 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
916 mod_bot = buf->b_mod_bot;
918 #ifdef FEAT_SEARCH_EXTRA
919 /* When 'hlsearch' is on and using a multi-line search pattern, a
920 * change in one line may make the Search highlighting in a
921 * previous line invalid. Simple solution: redraw all visible
922 * lines above the change.
923 * Same for a match pattern.
925 if (search_hl.rm.regprog != NULL
926 && re_multiline(search_hl.rm.regprog))
927 top_to_mod = TRUE;
928 else
930 cur = wp->w_match_head;
931 while (cur != NULL)
933 if (cur->match.regprog != NULL
934 && re_multiline(cur->match.regprog))
936 top_to_mod = TRUE;
937 break;
939 cur = cur->next;
942 #endif
944 #ifdef FEAT_FOLDING
945 if (mod_top != 0 && hasAnyFolding(wp))
947 linenr_T lnumt, lnumb;
950 * A change in a line can cause lines above it to become folded or
951 * unfolded. Find the top most buffer line that may be affected.
952 * If the line was previously folded and displayed, get the first
953 * line of that fold. If the line is folded now, get the first
954 * folded line. Use the minimum of these two.
957 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
958 * the line below it. If there is no valid entry, use w_topline.
959 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
960 * to this line. If there is no valid entry, use MAXLNUM. */
961 lnumt = wp->w_topline;
962 lnumb = MAXLNUM;
963 for (i = 0; i < wp->w_lines_valid; ++i)
964 if (wp->w_lines[i].wl_valid)
966 if (wp->w_lines[i].wl_lastlnum < mod_top)
967 lnumt = wp->w_lines[i].wl_lastlnum + 1;
968 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
970 lnumb = wp->w_lines[i].wl_lnum;
971 /* When there is a fold column it might need updating
972 * in the next line ("J" just above an open fold). */
973 if (wp->w_p_fdc > 0)
974 ++lnumb;
978 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
979 if (mod_top > lnumt)
980 mod_top = lnumt;
982 /* Now do the same for the bottom line (one above mod_bot). */
983 --mod_bot;
984 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
985 ++mod_bot;
986 if (mod_bot < lnumb)
987 mod_bot = lnumb;
989 #endif
991 /* When a change starts above w_topline and the end is below
992 * w_topline, start redrawing at w_topline.
993 * If the end of the change is above w_topline: do like no change was
994 * made, but redraw the first line to find changes in syntax. */
995 if (mod_top != 0 && mod_top < wp->w_topline)
997 if (mod_bot > wp->w_topline)
998 mod_top = wp->w_topline;
999 #ifdef FEAT_SYN_HL
1000 else if (syntax_present(buf))
1001 top_end = 1;
1002 #endif
1005 /* When line numbers are displayed need to redraw all lines below
1006 * inserted/deleted lines. */
1007 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1008 mod_bot = MAXLNUM;
1012 * When only displaying the lines at the top, set top_end. Used when
1013 * window has scrolled down for msg_scrolled.
1015 if (type == REDRAW_TOP)
1017 j = 0;
1018 for (i = 0; i < wp->w_lines_valid; ++i)
1020 j += wp->w_lines[i].wl_size;
1021 if (j >= wp->w_upd_rows)
1023 top_end = j;
1024 break;
1027 if (top_end == 0)
1028 /* not found (cannot happen?): redraw everything */
1029 type = NOT_VALID;
1030 else
1031 /* top area defined, the rest is VALID */
1032 type = VALID;
1035 /* Trick: we want to avoid clearing the screen twice. screenclear() will
1036 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1037 * non-zero and thus not FALSE) will indicate that screenclear() was not
1038 * called. */
1039 if (screen_cleared)
1040 screen_cleared = MAYBE;
1043 * If there are no changes on the screen that require a complete redraw,
1044 * handle three cases:
1045 * 1: we are off the top of the screen by a few lines: scroll down
1046 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1047 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1048 * w_lines[] that needs updating.
1050 if ((type == VALID || type == SOME_VALID
1051 || type == INVERTED || type == INVERTED_ALL)
1052 #ifdef FEAT_DIFF
1053 && !wp->w_botfill && !wp->w_old_botfill
1054 #endif
1057 if (mod_top != 0 && wp->w_topline == mod_top)
1060 * w_topline is the first changed line, the scrolling will be done
1061 * further down.
1064 else if (wp->w_lines[0].wl_valid
1065 && (wp->w_topline < wp->w_lines[0].wl_lnum
1066 #ifdef FEAT_DIFF
1067 || (wp->w_topline == wp->w_lines[0].wl_lnum
1068 && wp->w_topfill > wp->w_old_topfill)
1069 #endif
1073 * New topline is above old topline: May scroll down.
1075 #ifdef FEAT_FOLDING
1076 if (hasAnyFolding(wp))
1078 linenr_T ln;
1080 /* count the number of lines we are off, counting a sequence
1081 * of folded lines as one */
1082 j = 0;
1083 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1085 ++j;
1086 if (j >= wp->w_height - 2)
1087 break;
1088 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1091 else
1092 #endif
1093 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1094 if (j < wp->w_height - 2) /* not too far off */
1096 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1097 #ifdef FEAT_DIFF
1098 /* insert extra lines for previously invisible filler lines */
1099 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1100 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1101 - wp->w_old_topfill;
1102 #endif
1103 if (i < wp->w_height - 2) /* less than a screen off */
1106 * Try to insert the correct number of lines.
1107 * If not the last window, delete the lines at the bottom.
1108 * win_ins_lines may fail when the terminal can't do it.
1110 if (i > 0)
1111 check_for_delay(FALSE);
1112 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1114 if (wp->w_lines_valid != 0)
1116 /* Need to update rows that are new, stop at the
1117 * first one that scrolled down. */
1118 top_end = i;
1119 #ifdef FEAT_VISUAL
1120 scrolled_down = TRUE;
1121 #endif
1123 /* Move the entries that were scrolled, disable
1124 * the entries for the lines to be redrawn. */
1125 if ((wp->w_lines_valid += j) > wp->w_height)
1126 wp->w_lines_valid = wp->w_height;
1127 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1128 wp->w_lines[idx] = wp->w_lines[idx - j];
1129 while (idx >= 0)
1130 wp->w_lines[idx--].wl_valid = FALSE;
1133 else
1134 mid_start = 0; /* redraw all lines */
1136 else
1137 mid_start = 0; /* redraw all lines */
1139 else
1140 mid_start = 0; /* redraw all lines */
1142 else
1145 * New topline is at or below old topline: May scroll up.
1146 * When topline didn't change, find first entry in w_lines[] that
1147 * needs updating.
1150 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1151 j = -1;
1152 row = 0;
1153 for (i = 0; i < wp->w_lines_valid; i++)
1155 if (wp->w_lines[i].wl_valid
1156 && wp->w_lines[i].wl_lnum == wp->w_topline)
1158 j = i;
1159 break;
1161 row += wp->w_lines[i].wl_size;
1163 if (j == -1)
1165 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1166 * lines */
1167 mid_start = 0;
1169 else
1172 * Try to delete the correct number of lines.
1173 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1175 #ifdef FEAT_DIFF
1176 /* If the topline didn't change, delete old filler lines,
1177 * otherwise delete filler lines of the new topline... */
1178 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1179 row += wp->w_old_topfill;
1180 else
1181 row += diff_check_fill(wp, wp->w_topline);
1182 /* ... but don't delete new filler lines. */
1183 row -= wp->w_topfill;
1184 #endif
1185 if (row > 0)
1187 check_for_delay(FALSE);
1188 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1189 bot_start = wp->w_height - row;
1190 else
1191 mid_start = 0; /* redraw all lines */
1193 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1196 * Skip the lines (below the deleted lines) that are still
1197 * valid and don't need redrawing. Copy their info
1198 * upwards, to compensate for the deleted lines. Set
1199 * bot_start to the first row that needs redrawing.
1201 bot_start = 0;
1202 idx = 0;
1203 for (;;)
1205 wp->w_lines[idx] = wp->w_lines[j];
1206 /* stop at line that didn't fit, unless it is still
1207 * valid (no lines deleted) */
1208 if (row > 0 && bot_start + row
1209 + (int)wp->w_lines[j].wl_size > wp->w_height)
1211 wp->w_lines_valid = idx + 1;
1212 break;
1214 bot_start += wp->w_lines[idx++].wl_size;
1216 /* stop at the last valid entry in w_lines[].wl_size */
1217 if (++j >= wp->w_lines_valid)
1219 wp->w_lines_valid = idx;
1220 break;
1223 #ifdef FEAT_DIFF
1224 /* Correct the first entry for filler lines at the top
1225 * when it won't get updated below. */
1226 if (wp->w_p_diff && bot_start > 0)
1227 wp->w_lines[0].wl_size =
1228 plines_win_nofill(wp, wp->w_topline, TRUE)
1229 + wp->w_topfill;
1230 #endif
1235 /* When starting redraw in the first line, redraw all lines. When
1236 * there is only one window it's probably faster to clear the screen
1237 * first. */
1238 if (mid_start == 0)
1240 mid_end = wp->w_height;
1241 if (lastwin == firstwin)
1243 /* Clear the screen when it was not done by win_del_lines() or
1244 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1245 * then. */
1246 if (screen_cleared != TRUE)
1247 screenclear();
1248 #ifdef FEAT_WINDOWS
1249 /* The screen was cleared, redraw the tab pages line. */
1250 if (redraw_tabline)
1251 draw_tabline();
1252 #endif
1256 /* When win_del_lines() or win_ins_lines() caused the screen to be
1257 * cleared (only happens for the first window) or when screenclear()
1258 * was called directly above, "must_redraw" will have been set to
1259 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1260 if (screen_cleared == TRUE)
1261 must_redraw = 0;
1263 else
1265 /* Not VALID or INVERTED: redraw all lines. */
1266 mid_start = 0;
1267 mid_end = wp->w_height;
1270 if (type == SOME_VALID)
1272 /* SOME_VALID: redraw all lines. */
1273 mid_start = 0;
1274 mid_end = wp->w_height;
1275 type = NOT_VALID;
1278 #ifdef FEAT_VISUAL
1279 /* check if we are updating or removing the inverted part */
1280 if ((VIsual_active && buf == curwin->w_buffer)
1281 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1283 linenr_T from, to;
1285 if (VIsual_active)
1287 if (VIsual_active
1288 && (VIsual_mode != wp->w_old_visual_mode
1289 || type == INVERTED_ALL))
1292 * If the type of Visual selection changed, redraw the whole
1293 * selection. Also when the ownership of the X selection is
1294 * gained or lost.
1296 if (curwin->w_cursor.lnum < VIsual.lnum)
1298 from = curwin->w_cursor.lnum;
1299 to = VIsual.lnum;
1301 else
1303 from = VIsual.lnum;
1304 to = curwin->w_cursor.lnum;
1306 /* redraw more when the cursor moved as well */
1307 if (wp->w_old_cursor_lnum < from)
1308 from = wp->w_old_cursor_lnum;
1309 if (wp->w_old_cursor_lnum > to)
1310 to = wp->w_old_cursor_lnum;
1311 if (wp->w_old_visual_lnum < from)
1312 from = wp->w_old_visual_lnum;
1313 if (wp->w_old_visual_lnum > to)
1314 to = wp->w_old_visual_lnum;
1316 else
1319 * Find the line numbers that need to be updated: The lines
1320 * between the old cursor position and the current cursor
1321 * position. Also check if the Visual position changed.
1323 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1325 from = curwin->w_cursor.lnum;
1326 to = wp->w_old_cursor_lnum;
1328 else
1330 from = wp->w_old_cursor_lnum;
1331 to = curwin->w_cursor.lnum;
1332 if (from == 0) /* Visual mode just started */
1333 from = to;
1336 if (VIsual.lnum != wp->w_old_visual_lnum
1337 || VIsual.col != wp->w_old_visual_col)
1339 if (wp->w_old_visual_lnum < from
1340 && wp->w_old_visual_lnum != 0)
1341 from = wp->w_old_visual_lnum;
1342 if (wp->w_old_visual_lnum > to)
1343 to = wp->w_old_visual_lnum;
1344 if (VIsual.lnum < from)
1345 from = VIsual.lnum;
1346 if (VIsual.lnum > to)
1347 to = VIsual.lnum;
1352 * If in block mode and changed column or curwin->w_curswant:
1353 * update all lines.
1354 * First compute the actual start and end column.
1356 if (VIsual_mode == Ctrl_V)
1358 colnr_T fromc, toc;
1360 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
1361 ++toc;
1362 if (curwin->w_curswant == MAXCOL)
1363 toc = MAXCOL;
1365 if (fromc != wp->w_old_cursor_fcol
1366 || toc != wp->w_old_cursor_lcol)
1368 if (from > VIsual.lnum)
1369 from = VIsual.lnum;
1370 if (to < VIsual.lnum)
1371 to = VIsual.lnum;
1373 wp->w_old_cursor_fcol = fromc;
1374 wp->w_old_cursor_lcol = toc;
1377 else
1379 /* Use the line numbers of the old Visual area. */
1380 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1382 from = wp->w_old_cursor_lnum;
1383 to = wp->w_old_visual_lnum;
1385 else
1387 from = wp->w_old_visual_lnum;
1388 to = wp->w_old_cursor_lnum;
1393 * There is no need to update lines above the top of the window.
1395 if (from < wp->w_topline)
1396 from = wp->w_topline;
1399 * If we know the value of w_botline, use it to restrict the update to
1400 * the lines that are visible in the window.
1402 if (wp->w_valid & VALID_BOTLINE)
1404 if (from >= wp->w_botline)
1405 from = wp->w_botline - 1;
1406 if (to >= wp->w_botline)
1407 to = wp->w_botline - 1;
1411 * Find the minimal part to be updated.
1412 * Watch out for scrolling that made entries in w_lines[] invalid.
1413 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1414 * top_end; need to redraw from top_end to the "to" line.
1415 * A middle mouse click with a Visual selection may change the text
1416 * above the Visual area and reset wl_valid, do count these for
1417 * mid_end (in srow).
1419 if (mid_start > 0)
1421 lnum = wp->w_topline;
1422 idx = 0;
1423 srow = 0;
1424 if (scrolled_down)
1425 mid_start = top_end;
1426 else
1427 mid_start = 0;
1428 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1430 if (wp->w_lines[idx].wl_valid)
1431 mid_start += wp->w_lines[idx].wl_size;
1432 else if (!scrolled_down)
1433 srow += wp->w_lines[idx].wl_size;
1434 ++idx;
1435 # ifdef FEAT_FOLDING
1436 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1437 lnum = wp->w_lines[idx].wl_lnum;
1438 else
1439 # endif
1440 ++lnum;
1442 srow += mid_start;
1443 mid_end = wp->w_height;
1444 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1446 if (wp->w_lines[idx].wl_valid
1447 && wp->w_lines[idx].wl_lnum >= to + 1)
1449 /* Only update until first row of this line */
1450 mid_end = srow;
1451 break;
1453 srow += wp->w_lines[idx].wl_size;
1458 if (VIsual_active && buf == curwin->w_buffer)
1460 wp->w_old_visual_mode = VIsual_mode;
1461 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1462 wp->w_old_visual_lnum = VIsual.lnum;
1463 wp->w_old_visual_col = VIsual.col;
1464 wp->w_old_curswant = curwin->w_curswant;
1466 else
1468 wp->w_old_visual_mode = 0;
1469 wp->w_old_cursor_lnum = 0;
1470 wp->w_old_visual_lnum = 0;
1471 wp->w_old_visual_col = 0;
1473 #endif /* FEAT_VISUAL */
1475 #if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1476 /* reset got_int, otherwise regexp won't work */
1477 save_got_int = got_int;
1478 got_int = 0;
1479 #endif
1480 #ifdef FEAT_FOLDING
1481 win_foldinfo.fi_level = 0;
1482 #endif
1485 * Update all the window rows.
1487 idx = 0; /* first entry in w_lines[].wl_size */
1488 row = 0;
1489 srow = 0;
1490 lnum = wp->w_topline; /* first line shown in window */
1491 for (;;)
1493 /* stop updating when reached the end of the window (check for _past_
1494 * the end of the window is at the end of the loop) */
1495 if (row == wp->w_height)
1497 didline = TRUE;
1498 break;
1501 /* stop updating when hit the end of the file */
1502 if (lnum > buf->b_ml.ml_line_count)
1504 eof = TRUE;
1505 break;
1508 /* Remember the starting row of the line that is going to be dealt
1509 * with. It is used further down when the line doesn't fit. */
1510 srow = row;
1513 * Update a line when it is in an area that needs updating, when it
1514 * has changes or w_lines[idx] is invalid.
1515 * bot_start may be halfway a wrapped line after using
1516 * win_del_lines(), check if the current line includes it.
1517 * When syntax folding is being used, the saved syntax states will
1518 * already have been updated, we can't see where the syntax state is
1519 * the same again, just update until the end of the window.
1521 if (row < top_end
1522 || (row >= mid_start && row < mid_end)
1523 #ifdef FEAT_SEARCH_EXTRA
1524 || top_to_mod
1525 #endif
1526 || idx >= wp->w_lines_valid
1527 || (row + wp->w_lines[idx].wl_size > bot_start)
1528 || (mod_top != 0
1529 && (lnum == mod_top
1530 || (lnum >= mod_top
1531 && (lnum < mod_bot
1532 #ifdef FEAT_SYN_HL
1533 || did_update == DID_FOLD
1534 || (did_update == DID_LINE
1535 && syntax_present(buf)
1536 && (
1537 # ifdef FEAT_FOLDING
1538 (foldmethodIsSyntax(wp)
1539 && hasAnyFolding(wp)) ||
1540 # endif
1541 syntax_check_changed(lnum)))
1542 #endif
1543 )))))
1545 #ifdef FEAT_SEARCH_EXTRA
1546 if (lnum == mod_top)
1547 top_to_mod = FALSE;
1548 #endif
1551 * When at start of changed lines: May scroll following lines
1552 * up or down to minimize redrawing.
1553 * Don't do this when the change continues until the end.
1554 * Don't scroll when dollar_vcol is non-zero, keep the "$".
1556 if (lnum == mod_top
1557 && mod_bot != MAXLNUM
1558 && !(dollar_vcol != 0 && mod_bot == mod_top + 1))
1560 int old_rows = 0;
1561 int new_rows = 0;
1562 int xtra_rows;
1563 linenr_T l;
1565 /* Count the old number of window rows, using w_lines[], which
1566 * should still contain the sizes for the lines as they are
1567 * currently displayed. */
1568 for (i = idx; i < wp->w_lines_valid; ++i)
1570 /* Only valid lines have a meaningful wl_lnum. Invalid
1571 * lines are part of the changed area. */
1572 if (wp->w_lines[i].wl_valid
1573 && wp->w_lines[i].wl_lnum == mod_bot)
1574 break;
1575 old_rows += wp->w_lines[i].wl_size;
1576 #ifdef FEAT_FOLDING
1577 if (wp->w_lines[i].wl_valid
1578 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1580 /* Must have found the last valid entry above mod_bot.
1581 * Add following invalid entries. */
1582 ++i;
1583 while (i < wp->w_lines_valid
1584 && !wp->w_lines[i].wl_valid)
1585 old_rows += wp->w_lines[i++].wl_size;
1586 break;
1588 #endif
1591 if (i >= wp->w_lines_valid)
1593 /* We can't find a valid line below the changed lines,
1594 * need to redraw until the end of the window.
1595 * Inserting/deleting lines has no use. */
1596 bot_start = 0;
1598 else
1600 /* Able to count old number of rows: Count new window
1601 * rows, and may insert/delete lines */
1602 j = idx;
1603 for (l = lnum; l < mod_bot; ++l)
1605 #ifdef FEAT_FOLDING
1606 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1607 ++new_rows;
1608 else
1609 #endif
1610 #ifdef FEAT_DIFF
1611 if (l == wp->w_topline)
1612 new_rows += plines_win_nofill(wp, l, TRUE)
1613 + wp->w_topfill;
1614 else
1615 #endif
1616 new_rows += plines_win(wp, l, TRUE);
1617 ++j;
1618 if (new_rows > wp->w_height - row - 2)
1620 /* it's getting too much, must redraw the rest */
1621 new_rows = 9999;
1622 break;
1625 xtra_rows = new_rows - old_rows;
1626 if (xtra_rows < 0)
1628 /* May scroll text up. If there is not enough
1629 * remaining text or scrolling fails, must redraw the
1630 * rest. If scrolling works, must redraw the text
1631 * below the scrolled text. */
1632 if (row - xtra_rows >= wp->w_height - 2)
1633 mod_bot = MAXLNUM;
1634 else
1636 check_for_delay(FALSE);
1637 if (win_del_lines(wp, row,
1638 -xtra_rows, FALSE, FALSE) == FAIL)
1639 mod_bot = MAXLNUM;
1640 else
1641 bot_start = wp->w_height + xtra_rows;
1644 else if (xtra_rows > 0)
1646 /* May scroll text down. If there is not enough
1647 * remaining text of scrolling fails, must redraw the
1648 * rest. */
1649 if (row + xtra_rows >= wp->w_height - 2)
1650 mod_bot = MAXLNUM;
1651 else
1653 check_for_delay(FALSE);
1654 if (win_ins_lines(wp, row + old_rows,
1655 xtra_rows, FALSE, FALSE) == FAIL)
1656 mod_bot = MAXLNUM;
1657 else if (top_end > row + old_rows)
1658 /* Scrolled the part at the top that requires
1659 * updating down. */
1660 top_end += xtra_rows;
1664 /* When not updating the rest, may need to move w_lines[]
1665 * entries. */
1666 if (mod_bot != MAXLNUM && i != j)
1668 if (j < i)
1670 int x = row + new_rows;
1672 /* move entries in w_lines[] upwards */
1673 for (;;)
1675 /* stop at last valid entry in w_lines[] */
1676 if (i >= wp->w_lines_valid)
1678 wp->w_lines_valid = j;
1679 break;
1681 wp->w_lines[j] = wp->w_lines[i];
1682 /* stop at a line that won't fit */
1683 if (x + (int)wp->w_lines[j].wl_size
1684 > wp->w_height)
1686 wp->w_lines_valid = j + 1;
1687 break;
1689 x += wp->w_lines[j++].wl_size;
1690 ++i;
1692 if (bot_start > x)
1693 bot_start = x;
1695 else /* j > i */
1697 /* move entries in w_lines[] downwards */
1698 j -= i;
1699 wp->w_lines_valid += j;
1700 if (wp->w_lines_valid > wp->w_height)
1701 wp->w_lines_valid = wp->w_height;
1702 for (i = wp->w_lines_valid; i - j >= idx; --i)
1703 wp->w_lines[i] = wp->w_lines[i - j];
1705 /* The w_lines[] entries for inserted lines are
1706 * now invalid, but wl_size may be used above.
1707 * Reset to zero. */
1708 while (i >= idx)
1710 wp->w_lines[i].wl_size = 0;
1711 wp->w_lines[i--].wl_valid = FALSE;
1718 #ifdef FEAT_FOLDING
1720 * When lines are folded, display one line for all of them.
1721 * Otherwise, display normally (can be several display lines when
1722 * 'wrap' is on).
1724 fold_count = foldedCount(wp, lnum, &win_foldinfo);
1725 if (fold_count != 0)
1727 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
1728 ++row;
1729 --fold_count;
1730 wp->w_lines[idx].wl_folded = TRUE;
1731 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
1732 # ifdef FEAT_SYN_HL
1733 did_update = DID_FOLD;
1734 # endif
1736 else
1737 #endif
1738 if (idx < wp->w_lines_valid
1739 && wp->w_lines[idx].wl_valid
1740 && wp->w_lines[idx].wl_lnum == lnum
1741 && lnum > wp->w_topline
1742 && !(dy_flags & DY_LASTLINE)
1743 && srow + wp->w_lines[idx].wl_size > wp->w_height
1744 #ifdef FEAT_DIFF
1745 && diff_check_fill(wp, lnum) == 0
1746 #endif
1749 /* This line is not going to fit. Don't draw anything here,
1750 * will draw "@ " lines below. */
1751 row = wp->w_height + 1;
1753 else
1755 #ifdef FEAT_SEARCH_EXTRA
1756 prepare_search_hl(wp, lnum);
1757 #endif
1758 #ifdef FEAT_SYN_HL
1759 /* Let the syntax stuff know we skipped a few lines. */
1760 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
1761 && syntax_present(buf))
1762 syntax_end_parsing(syntax_last_parsed + 1);
1763 #endif
1766 * Display one line.
1768 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
1770 #ifdef FEAT_FOLDING
1771 wp->w_lines[idx].wl_folded = FALSE;
1772 wp->w_lines[idx].wl_lastlnum = lnum;
1773 #endif
1774 #ifdef FEAT_SYN_HL
1775 did_update = DID_LINE;
1776 syntax_last_parsed = lnum;
1777 #endif
1780 wp->w_lines[idx].wl_lnum = lnum;
1781 wp->w_lines[idx].wl_valid = TRUE;
1782 if (row > wp->w_height) /* past end of screen */
1784 /* we may need the size of that too long line later on */
1785 if (dollar_vcol == 0)
1786 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
1787 ++idx;
1788 break;
1790 if (dollar_vcol == 0)
1791 wp->w_lines[idx].wl_size = row - srow;
1792 ++idx;
1793 #ifdef FEAT_FOLDING
1794 lnum += fold_count + 1;
1795 #else
1796 ++lnum;
1797 #endif
1799 else
1801 /* This line does not need updating, advance to the next one */
1802 row += wp->w_lines[idx++].wl_size;
1803 if (row > wp->w_height) /* past end of screen */
1804 break;
1805 #ifdef FEAT_FOLDING
1806 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
1807 #else
1808 ++lnum;
1809 #endif
1810 #ifdef FEAT_SYN_HL
1811 did_update = DID_NONE;
1812 #endif
1815 if (lnum > buf->b_ml.ml_line_count)
1817 eof = TRUE;
1818 break;
1822 * End of loop over all window lines.
1826 if (idx > wp->w_lines_valid)
1827 wp->w_lines_valid = idx;
1829 #ifdef FEAT_SYN_HL
1831 * Let the syntax stuff know we stop parsing here.
1833 if (syntax_last_parsed != 0 && syntax_present(buf))
1834 syntax_end_parsing(syntax_last_parsed + 1);
1835 #endif
1838 * If we didn't hit the end of the file, and we didn't finish the last
1839 * line we were working on, then the line didn't fit.
1841 wp->w_empty_rows = 0;
1842 #ifdef FEAT_DIFF
1843 wp->w_filler_rows = 0;
1844 #endif
1845 if (!eof && !didline)
1847 if (lnum == wp->w_topline)
1850 * Single line that does not fit!
1851 * Don't overwrite it, it can be edited.
1853 wp->w_botline = lnum + 1;
1855 #ifdef FEAT_DIFF
1856 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
1858 /* Window ends in filler lines. */
1859 wp->w_botline = lnum;
1860 wp->w_filler_rows = wp->w_height - srow;
1862 #endif
1863 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
1866 * Last line isn't finished: Display "@@@" at the end.
1868 screen_fill(W_WINROW(wp) + wp->w_height - 1,
1869 W_WINROW(wp) + wp->w_height,
1870 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
1871 '@', '@', hl_attr(HLF_AT));
1872 set_empty_rows(wp, srow);
1873 wp->w_botline = lnum;
1875 else
1877 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
1878 wp->w_botline = lnum;
1881 else
1883 #ifdef FEAT_VERTSPLIT
1884 draw_vsep_win(wp, row);
1885 #endif
1886 if (eof) /* we hit the end of the file */
1888 wp->w_botline = buf->b_ml.ml_line_count + 1;
1889 #ifdef FEAT_DIFF
1890 j = diff_check_fill(wp, wp->w_botline);
1891 if (j > 0 && !wp->w_botfill)
1894 * Display filler lines at the end of the file
1896 if (char2cells(fill_diff) > 1)
1897 i = '-';
1898 else
1899 i = fill_diff;
1900 if (row + j > wp->w_height)
1901 j = wp->w_height - row;
1902 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
1903 row += j;
1905 #endif
1907 else if (dollar_vcol == 0)
1908 wp->w_botline = lnum;
1910 /* make sure the rest of the screen is blank */
1911 /* put '~'s on rows that aren't part of the file. */
1912 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_AT);
1915 /* Reset the type of redrawing required, the window has been updated. */
1916 wp->w_redr_type = 0;
1917 #ifdef FEAT_DIFF
1918 wp->w_old_topfill = wp->w_topfill;
1919 wp->w_old_botfill = wp->w_botfill;
1920 #endif
1922 if (dollar_vcol == 0)
1925 * There is a trick with w_botline. If we invalidate it on each
1926 * change that might modify it, this will cause a lot of expensive
1927 * calls to plines() in update_topline() each time. Therefore the
1928 * value of w_botline is often approximated, and this value is used to
1929 * compute the value of w_topline. If the value of w_botline was
1930 * wrong, check that the value of w_topline is correct (cursor is on
1931 * the visible part of the text). If it's not, we need to redraw
1932 * again. Mostly this just means scrolling up a few lines, so it
1933 * doesn't look too bad. Only do this for the current window (where
1934 * changes are relevant).
1936 wp->w_valid |= VALID_BOTLINE;
1937 if (wp == curwin && wp->w_botline != old_botline && !recursive)
1939 recursive = TRUE;
1940 curwin->w_valid &= ~VALID_TOPLINE;
1941 update_topline(); /* may invalidate w_botline again */
1942 if (must_redraw != 0)
1944 /* Don't update for changes in buffer again. */
1945 i = curbuf->b_mod_set;
1946 curbuf->b_mod_set = FALSE;
1947 win_update(curwin);
1948 must_redraw = 0;
1949 curbuf->b_mod_set = i;
1951 recursive = FALSE;
1955 #if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1956 /* restore got_int, unless CTRL-C was hit while redrawing */
1957 if (!got_int)
1958 got_int = save_got_int;
1959 #endif
1962 #ifdef FEAT_SIGNS
1963 static int draw_signcolumn __ARGS((win_T *wp));
1966 * Return TRUE when window "wp" has a column to draw signs in.
1968 static int
1969 draw_signcolumn(wp)
1970 win_T *wp;
1972 return (wp->w_buffer->b_signlist != NULL
1973 # ifdef FEAT_NETBEANS_INTG
1974 || usingNetbeans
1975 # endif
1978 #endif
1981 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
1982 * as the filler character.
1984 static void
1985 win_draw_end(wp, c1, c2, row, endrow, hl)
1986 win_T *wp;
1987 int c1;
1988 int c2;
1989 int row;
1990 int endrow;
1991 hlf_T hl;
1993 #if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
1994 int n = 0;
1995 # define FDC_OFF n
1996 #else
1997 # define FDC_OFF 0
1998 #endif
2000 #ifdef FEAT_RIGHTLEFT
2001 if (wp->w_p_rl)
2003 /* No check for cmdline window: should never be right-left. */
2004 # ifdef FEAT_FOLDING
2005 n = wp->w_p_fdc;
2007 if (n > 0)
2009 /* draw the fold column at the right */
2010 if (n > W_WIDTH(wp))
2011 n = W_WIDTH(wp);
2012 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2013 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
2014 ' ', ' ', hl_attr(HLF_FC));
2016 # endif
2017 # ifdef FEAT_SIGNS
2018 if (draw_signcolumn(wp))
2020 int nn = n + 2;
2022 /* draw the sign column left of the fold column */
2023 if (nn > W_WIDTH(wp))
2024 nn = W_WIDTH(wp);
2025 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2026 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
2027 ' ', ' ', hl_attr(HLF_SC));
2028 n = nn;
2030 # endif
2031 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2032 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
2033 c2, c2, hl_attr(hl));
2034 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2035 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
2036 c1, c2, hl_attr(hl));
2038 else
2039 #endif
2041 #ifdef FEAT_CMDWIN
2042 if (cmdwin_type != 0 && wp == curwin)
2044 /* draw the cmdline character in the leftmost column */
2045 n = 1;
2046 if (n > wp->w_width)
2047 n = wp->w_width;
2048 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2049 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
2050 cmdwin_type, ' ', hl_attr(HLF_AT));
2052 #endif
2053 #ifdef FEAT_FOLDING
2054 if (wp->w_p_fdc > 0)
2056 int nn = n + wp->w_p_fdc;
2058 /* draw the fold column at the left */
2059 if (nn > W_WIDTH(wp))
2060 nn = W_WIDTH(wp);
2061 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2062 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2063 ' ', ' ', hl_attr(HLF_FC));
2064 n = nn;
2066 #endif
2067 #ifdef FEAT_SIGNS
2068 if (draw_signcolumn(wp))
2070 int nn = n + 2;
2072 /* draw the sign column after the fold column */
2073 if (nn > W_WIDTH(wp))
2074 nn = W_WIDTH(wp);
2075 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2076 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2077 ' ', ' ', hl_attr(HLF_SC));
2078 n = nn;
2080 #endif
2081 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2082 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
2083 c1, c2, hl_attr(hl));
2085 set_empty_rows(wp, row);
2088 #ifdef FEAT_FOLDING
2090 * Display one folded line.
2092 static void
2093 fold_line(wp, fold_count, foldinfo, lnum, row)
2094 win_T *wp;
2095 long fold_count;
2096 foldinfo_T *foldinfo;
2097 linenr_T lnum;
2098 int row;
2100 char_u buf[51];
2101 pos_T *top, *bot;
2102 linenr_T lnume = lnum + fold_count - 1;
2103 int len;
2104 char_u *text;
2105 int fdc;
2106 int col;
2107 int txtcol;
2108 int off = (int)(current_ScreenLine - ScreenLines);
2109 int ri;
2111 /* Build the fold line:
2112 * 1. Add the cmdwin_type for the command-line window
2113 * 2. Add the 'foldcolumn'
2114 * 3. Add the 'number' or 'relativenumber' column
2115 * 4. Compose the text
2116 * 5. Add the text
2117 * 6. set highlighting for the Visual area an other text
2119 col = 0;
2122 * 1. Add the cmdwin_type for the command-line window
2123 * Ignores 'rightleft', this window is never right-left.
2125 #ifdef FEAT_CMDWIN
2126 if (cmdwin_type != 0 && wp == curwin)
2128 ScreenLines[off] = cmdwin_type;
2129 ScreenAttrs[off] = hl_attr(HLF_AT);
2130 #ifdef FEAT_MBYTE
2131 if (enc_utf8)
2132 ScreenLinesUC[off] = 0;
2133 #endif
2134 ++col;
2136 #endif
2139 * 2. Add the 'foldcolumn'
2141 fdc = wp->w_p_fdc;
2142 if (fdc > W_WIDTH(wp) - col)
2143 fdc = W_WIDTH(wp) - col;
2144 if (fdc > 0)
2146 fill_foldcolumn(buf, wp, TRUE, lnum);
2147 #ifdef FEAT_RIGHTLEFT
2148 if (wp->w_p_rl)
2150 int i;
2152 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
2153 hl_attr(HLF_FC));
2154 /* reverse the fold column */
2155 for (i = 0; i < fdc; ++i)
2156 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2158 else
2159 #endif
2160 copy_text_attr(off + col, buf, fdc, hl_attr(HLF_FC));
2161 col += fdc;
2164 #ifdef FEAT_RIGHTLEFT
2165 # define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2166 for (ri = 0; ri < l; ++ri) \
2167 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2168 else \
2169 for (ri = 0; ri < l; ++ri) \
2170 ScreenAttrs[off + (p) + ri] = v
2171 #else
2172 # define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2173 ScreenAttrs[off + (p) + ri] = v
2174 #endif
2176 /* Set all attributes of the 'number' or 'relativenumber' column and the
2177 * text */
2178 RL_MEMSET(col, hl_attr(HLF_FL), W_WIDTH(wp) - col);
2180 #ifdef FEAT_SIGNS
2181 /* If signs are being displayed, add two spaces. */
2182 if (draw_signcolumn(wp))
2184 len = W_WIDTH(wp) - col;
2185 if (len > 0)
2187 if (len > 2)
2188 len = 2;
2189 # ifdef FEAT_RIGHTLEFT
2190 if (wp->w_p_rl)
2191 /* the line number isn't reversed */
2192 copy_text_attr(off + W_WIDTH(wp) - len - col,
2193 (char_u *)" ", len, hl_attr(HLF_FL));
2194 else
2195 # endif
2196 copy_text_attr(off + col, (char_u *)" ", len, hl_attr(HLF_FL));
2197 col += len;
2200 #endif
2203 * 3. Add the 'number' or 'relativenumber' column
2205 if (wp->w_p_nu || wp->w_p_rnu)
2207 len = W_WIDTH(wp) - col;
2208 if (len > 0)
2210 int w = number_width(wp);
2211 long num;
2213 if (len > w + 1)
2214 len = w + 1;
2216 if (wp->w_p_nu)
2217 /* 'number' */
2218 num = (long)lnum;
2219 else
2220 /* 'relativenumber', don't use negative numbers */
2221 num = (long)abs((int)get_cursor_rel_lnum(wp, lnum));
2223 sprintf((char *)buf, "%*ld ", w, num);
2224 #ifdef FEAT_RIGHTLEFT
2225 if (wp->w_p_rl)
2226 /* the line number isn't reversed */
2227 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
2228 hl_attr(HLF_FL));
2229 else
2230 #endif
2231 copy_text_attr(off + col, buf, len, hl_attr(HLF_FL));
2232 col += len;
2237 * 4. Compose the folded-line string with 'foldtext', if set.
2239 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
2241 txtcol = col; /* remember where text starts */
2244 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2245 * Right-left text is put in columns 0 - number-col, normal text is put
2246 * in columns number-col - window-width.
2248 #ifdef FEAT_MBYTE
2249 if (has_mbyte)
2251 int cells;
2252 int u8c, u8cc[MAX_MCO];
2253 int i;
2254 int idx;
2255 int c_len;
2256 char_u *p;
2257 # ifdef FEAT_ARABIC
2258 int prev_c = 0; /* previous Arabic character */
2259 int prev_c1 = 0; /* first composing char for prev_c */
2260 # endif
2262 # ifdef FEAT_RIGHTLEFT
2263 if (wp->w_p_rl)
2264 idx = off;
2265 else
2266 # endif
2267 idx = off + col;
2269 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2270 for (p = text; *p != NUL; )
2272 cells = (*mb_ptr2cells)(p);
2273 c_len = (*mb_ptr2len)(p);
2274 if (col + cells > W_WIDTH(wp)
2275 # ifdef FEAT_RIGHTLEFT
2276 - (wp->w_p_rl ? col : 0)
2277 # endif
2279 break;
2280 ScreenLines[idx] = *p;
2281 if (enc_utf8)
2283 u8c = utfc_ptr2char(p, u8cc);
2284 if (*p < 0x80 && u8cc[0] == 0)
2286 ScreenLinesUC[idx] = 0;
2287 #ifdef FEAT_ARABIC
2288 prev_c = u8c;
2289 #endif
2291 else
2293 #ifdef FEAT_ARABIC
2294 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2296 /* Do Arabic shaping. */
2297 int pc, pc1, nc;
2298 int pcc[MAX_MCO];
2299 int firstbyte = *p;
2301 /* The idea of what is the previous and next
2302 * character depends on 'rightleft'. */
2303 if (wp->w_p_rl)
2305 pc = prev_c;
2306 pc1 = prev_c1;
2307 nc = utf_ptr2char(p + c_len);
2308 prev_c1 = u8cc[0];
2310 else
2312 pc = utfc_ptr2char(p + c_len, pcc);
2313 nc = prev_c;
2314 pc1 = pcc[0];
2316 prev_c = u8c;
2318 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2319 pc, pc1, nc);
2320 ScreenLines[idx] = firstbyte;
2322 else
2323 prev_c = u8c;
2324 #endif
2325 /* Non-BMP character: display as ? or fullwidth ?. */
2326 #ifdef UNICODE16
2327 if (u8c >= 0x10000)
2328 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2329 else
2330 #endif
2331 ScreenLinesUC[idx] = u8c;
2332 for (i = 0; i < Screen_mco; ++i)
2334 ScreenLinesC[i][idx] = u8cc[i];
2335 if (u8cc[i] == 0)
2336 break;
2339 if (cells > 1)
2340 ScreenLines[idx + 1] = 0;
2342 else if (cells > 1) /* double-byte character */
2344 if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2345 ScreenLines2[idx] = p[1];
2346 else
2347 ScreenLines[idx + 1] = p[1];
2349 col += cells;
2350 idx += cells;
2351 p += c_len;
2354 else
2355 #endif
2357 len = (int)STRLEN(text);
2358 if (len > W_WIDTH(wp) - col)
2359 len = W_WIDTH(wp) - col;
2360 if (len > 0)
2362 #ifdef FEAT_RIGHTLEFT
2363 if (wp->w_p_rl)
2364 STRNCPY(current_ScreenLine, text, len);
2365 else
2366 #endif
2367 STRNCPY(current_ScreenLine + col, text, len);
2368 col += len;
2372 /* Fill the rest of the line with the fold filler */
2373 #ifdef FEAT_RIGHTLEFT
2374 if (wp->w_p_rl)
2375 col -= txtcol;
2376 #endif
2377 while (col < W_WIDTH(wp)
2378 #ifdef FEAT_RIGHTLEFT
2379 - (wp->w_p_rl ? txtcol : 0)
2380 #endif
2383 #ifdef FEAT_MBYTE
2384 if (enc_utf8)
2386 if (fill_fold >= 0x80)
2388 ScreenLinesUC[off + col] = fill_fold;
2389 ScreenLinesC[0][off + col] = 0;
2391 else
2392 ScreenLinesUC[off + col] = 0;
2394 #endif
2395 ScreenLines[off + col++] = fill_fold;
2398 if (text != buf)
2399 vim_free(text);
2402 * 6. set highlighting for the Visual area an other text.
2403 * If all folded lines are in the Visual area, highlight the line.
2405 #ifdef FEAT_VISUAL
2406 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2408 if (ltoreq(curwin->w_cursor, VIsual))
2410 /* Visual is after curwin->w_cursor */
2411 top = &curwin->w_cursor;
2412 bot = &VIsual;
2414 else
2416 /* Visual is before curwin->w_cursor */
2417 top = &VIsual;
2418 bot = &curwin->w_cursor;
2420 if (lnum >= top->lnum
2421 && lnume <= bot->lnum
2422 && (VIsual_mode != 'v'
2423 || ((lnum > top->lnum
2424 || (lnum == top->lnum
2425 && top->col == 0))
2426 && (lnume < bot->lnum
2427 || (lnume == bot->lnum
2428 && (bot->col - (*p_sel == 'e'))
2429 >= STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
2431 if (VIsual_mode == Ctrl_V)
2433 /* Visual block mode: highlight the chars part of the block */
2434 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2436 if (wp->w_old_cursor_lcol + txtcol < (colnr_T)W_WIDTH(wp))
2437 len = wp->w_old_cursor_lcol;
2438 else
2439 len = W_WIDTH(wp) - txtcol;
2440 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, hl_attr(HLF_V),
2441 len - (int)wp->w_old_cursor_fcol);
2444 else
2446 /* Set all attributes of the text */
2447 RL_MEMSET(txtcol, hl_attr(HLF_V), W_WIDTH(wp) - txtcol);
2451 #endif
2453 #ifdef FEAT_SYN_HL
2454 /* Show 'cursorcolumn' in the fold line. */
2455 if (wp->w_p_cuc)
2457 txtcol += wp->w_virtcol;
2458 if (wp->w_p_wrap)
2459 txtcol -= wp->w_skipcol;
2460 else
2461 txtcol -= wp->w_leftcol;
2462 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2463 ScreenAttrs[off + txtcol] = hl_combine_attr(
2464 ScreenAttrs[off + txtcol], hl_attr(HLF_CUC));
2466 #endif
2468 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2469 (int)W_WIDTH(wp), FALSE);
2472 * Update w_cline_height and w_cline_folded if the cursor line was
2473 * updated (saves a call to plines() later).
2475 if (wp == curwin
2476 && lnum <= curwin->w_cursor.lnum
2477 && lnume >= curwin->w_cursor.lnum)
2479 curwin->w_cline_row = row;
2480 curwin->w_cline_height = 1;
2481 curwin->w_cline_folded = TRUE;
2482 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2487 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2489 static void
2490 copy_text_attr(off, buf, len, attr)
2491 int off;
2492 char_u *buf;
2493 int len;
2494 int attr;
2496 int i;
2498 mch_memmove(ScreenLines + off, buf, (size_t)len);
2499 # ifdef FEAT_MBYTE
2500 if (enc_utf8)
2501 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2502 # endif
2503 for (i = 0; i < len; ++i)
2504 ScreenAttrs[off + i] = attr;
2508 * Fill the foldcolumn at "p" for window "wp".
2509 * Only to be called when 'foldcolumn' > 0.
2511 static void
2512 fill_foldcolumn(p, wp, closed, lnum)
2513 char_u *p;
2514 win_T *wp;
2515 int closed; /* TRUE of FALSE */
2516 linenr_T lnum; /* current line number */
2518 int i = 0;
2519 int level;
2520 int first_level;
2521 int empty;
2523 /* Init to all spaces. */
2524 copy_spaces(p, (size_t)wp->w_p_fdc);
2526 level = win_foldinfo.fi_level;
2527 if (level > 0)
2529 /* If there is only one column put more info in it. */
2530 empty = (wp->w_p_fdc == 1) ? 0 : 1;
2532 /* If the column is too narrow, we start at the lowest level that
2533 * fits and use numbers to indicated the depth. */
2534 first_level = level - wp->w_p_fdc - closed + 1 + empty;
2535 if (first_level < 1)
2536 first_level = 1;
2538 for (i = 0; i + empty < wp->w_p_fdc; ++i)
2540 if (win_foldinfo.fi_lnum == lnum
2541 && first_level + i >= win_foldinfo.fi_low_level)
2542 p[i] = '-';
2543 else if (first_level == 1)
2544 p[i] = '|';
2545 else if (first_level + i <= 9)
2546 p[i] = '0' + first_level + i;
2547 else
2548 p[i] = '>';
2549 if (first_level + i == level)
2550 break;
2553 if (closed)
2554 p[i >= wp->w_p_fdc ? i - 1 : i] = '+';
2556 #endif /* FEAT_FOLDING */
2559 * Display line "lnum" of window 'wp' on the screen.
2560 * Start at row "startrow", stop when "endrow" is reached.
2561 * wp->w_virtcol needs to be valid.
2563 * Return the number of last row the line occupies.
2565 /* ARGSUSED */
2566 static int
2567 win_line(wp, lnum, startrow, endrow, nochange)
2568 win_T *wp;
2569 linenr_T lnum;
2570 int startrow;
2571 int endrow;
2572 int nochange; /* not updating for changed text */
2574 int col; /* visual column on screen */
2575 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2576 int c = 0; /* init for GCC */
2577 long vcol = 0; /* virtual column (for tabs) */
2578 long vcol_prev = -1; /* "vcol" of previous character */
2579 char_u *line; /* current line */
2580 char_u *ptr; /* current position in "line" */
2581 int row; /* row in the window, excl w_winrow */
2582 int screen_row; /* row on the screen, incl w_winrow */
2584 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2585 int n_extra = 0; /* number of extra chars */
2586 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
2587 int c_extra = NUL; /* extra chars, all the same */
2588 int extra_attr = 0; /* attributes when n_extra != 0 */
2589 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2590 displaying lcs_eol at end-of-line */
2591 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2592 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2594 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2595 int saved_n_extra = 0;
2596 char_u *saved_p_extra = NULL;
2597 int saved_c_extra = 0;
2598 int saved_char_attr = 0;
2600 int n_attr = 0; /* chars with special attr */
2601 int saved_attr2 = 0; /* char_attr saved for n_attr */
2602 int n_attr3 = 0; /* chars with overruling special attr */
2603 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2605 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2607 int fromcol, tocol; /* start/end of inverting */
2608 int fromcol_prev = -2; /* start of inverting after cursor */
2609 int noinvcur = FALSE; /* don't invert the cursor */
2610 #ifdef FEAT_VISUAL
2611 pos_T *top, *bot;
2612 int lnum_in_visual_area = FALSE;
2613 #endif
2614 pos_T pos;
2615 long v;
2617 int char_attr = 0; /* attributes for next character */
2618 int attr_pri = FALSE; /* char_attr has priority */
2619 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2620 in this line */
2621 int attr = 0; /* attributes for area highlighting */
2622 int area_attr = 0; /* attributes desired by highlighting */
2623 int search_attr = 0; /* attributes desired by 'hlsearch' */
2624 #ifdef FEAT_SYN_HL
2625 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
2626 int syntax_attr = 0; /* attributes desired by syntax */
2627 int has_syntax = FALSE; /* this buffer has syntax highl. */
2628 int save_did_emsg;
2629 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
2630 #endif
2631 #ifdef FEAT_SPELL
2632 int has_spell = FALSE; /* this buffer has spell checking */
2633 # define SPWORDLEN 150
2634 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
2635 int nextlinecol = 0; /* column where nextline[] starts */
2636 int nextline_idx = 0; /* index in nextline[] where next line
2637 starts */
2638 int spell_attr = 0; /* attributes desired by spelling */
2639 int word_end = 0; /* last byte with same spell_attr */
2640 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2641 static int checked_col = 0; /* column in "checked_lnum" up to which
2642 * there are no spell errors */
2643 static int cap_col = -1; /* column to check for Cap word */
2644 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
2645 int cur_checked_col = 0; /* checked column for current line */
2646 #endif
2647 int extra_check; /* has syntax or linebreak */
2648 #ifdef FEAT_MBYTE
2649 int multi_attr = 0; /* attributes desired by multibyte */
2650 int mb_l = 1; /* multi-byte byte length */
2651 int mb_c = 0; /* decoded multi-byte character */
2652 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
2653 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
2654 #endif
2655 #ifdef FEAT_DIFF
2656 int filler_lines; /* nr of filler lines to be drawn */
2657 int filler_todo; /* nr of filler lines still to do + 1 */
2658 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
2659 int change_start = MAXCOL; /* first col of changed area */
2660 int change_end = -1; /* last col of changed area */
2661 #endif
2662 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
2663 #ifdef FEAT_LINEBREAK
2664 int need_showbreak = FALSE;
2665 #endif
2666 #if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
2667 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
2668 # define LINE_ATTR
2669 int line_attr = 0; /* attribute for the whole line */
2670 #endif
2671 #ifdef FEAT_SEARCH_EXTRA
2672 matchitem_T *cur; /* points to the match list */
2673 match_T *shl; /* points to search_hl or a match */
2674 int shl_flag; /* flag to indicate whether search_hl
2675 has been processed or not */
2676 int prevcol_hl_flag; /* flag to indicate whether prevcol
2677 equals startcol of search_hl or one
2678 of the matches */
2679 #endif
2680 #ifdef FEAT_ARABIC
2681 int prev_c = 0; /* previous Arabic character */
2682 int prev_c1 = 0; /* first composing char for prev_c */
2683 #endif
2684 #if defined(LINE_ATTR)
2685 int did_line_attr = 0;
2686 #endif
2688 /* draw_state: items that are drawn in sequence: */
2689 #define WL_START 0 /* nothing done yet */
2690 #ifdef FEAT_CMDWIN
2691 # define WL_CMDLINE WL_START + 1 /* cmdline window column */
2692 #else
2693 # define WL_CMDLINE WL_START
2694 #endif
2695 #ifdef FEAT_FOLDING
2696 # define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
2697 #else
2698 # define WL_FOLD WL_CMDLINE
2699 #endif
2700 #ifdef FEAT_SIGNS
2701 # define WL_SIGN WL_FOLD + 1 /* column for signs */
2702 #else
2703 # define WL_SIGN WL_FOLD /* column for signs */
2704 #endif
2705 #define WL_NR WL_SIGN + 1 /* line number */
2706 #if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
2707 # define WL_SBR WL_NR + 1 /* 'showbreak' or 'diff' */
2708 #else
2709 # define WL_SBR WL_NR
2710 #endif
2711 #define WL_LINE WL_SBR + 1 /* text in the line */
2712 int draw_state = WL_START; /* what to draw next */
2713 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2714 int feedback_col = 0;
2715 int feedback_old_attr = -1;
2716 #endif
2719 if (startrow > endrow) /* past the end already! */
2720 return startrow;
2722 row = startrow;
2723 screen_row = row + W_WINROW(wp);
2726 * To speed up the loop below, set extra_check when there is linebreak,
2727 * trailing white space and/or syntax processing to be done.
2729 #ifdef FEAT_LINEBREAK
2730 extra_check = wp->w_p_lbr;
2731 #else
2732 extra_check = 0;
2733 #endif
2734 #ifdef FEAT_SYN_HL
2735 if (syntax_present(wp->w_buffer) && !wp->w_buffer->b_syn_error)
2737 /* Prepare for syntax highlighting in this line. When there is an
2738 * error, stop syntax highlighting. */
2739 save_did_emsg = did_emsg;
2740 did_emsg = FALSE;
2741 syntax_start(wp, lnum);
2742 if (did_emsg)
2743 wp->w_buffer->b_syn_error = TRUE;
2744 else
2746 did_emsg = save_did_emsg;
2747 has_syntax = TRUE;
2748 extra_check = TRUE;
2751 #endif
2753 #ifdef FEAT_SPELL
2754 if (wp->w_p_spell
2755 && *wp->w_buffer->b_p_spl != NUL
2756 && wp->w_buffer->b_langp.ga_len > 0
2757 && *(char **)(wp->w_buffer->b_langp.ga_data) != NULL)
2759 /* Prepare for spell checking. */
2760 has_spell = TRUE;
2761 extra_check = TRUE;
2763 /* Get the start of the next line, so that words that wrap to the next
2764 * line are found too: "et<line-break>al.".
2765 * Trick: skip a few chars for C/shell/Vim comments */
2766 nextline[SPWORDLEN] = NUL;
2767 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2769 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
2770 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
2773 /* When a word wrapped from the previous line the start of the current
2774 * line is valid. */
2775 if (lnum == checked_lnum)
2776 cur_checked_col = checked_col;
2777 checked_lnum = 0;
2779 /* When there was a sentence end in the previous line may require a
2780 * word starting with capital in this line. In line 1 always check
2781 * the first word. */
2782 if (lnum != capcol_lnum)
2783 cap_col = -1;
2784 if (lnum == 1)
2785 cap_col = 0;
2786 capcol_lnum = 0;
2788 #endif
2791 * handle visual active in this window
2793 fromcol = -10;
2794 tocol = MAXCOL;
2795 #ifdef FEAT_VISUAL
2796 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2798 /* Visual is after curwin->w_cursor */
2799 if (ltoreq(curwin->w_cursor, VIsual))
2801 top = &curwin->w_cursor;
2802 bot = &VIsual;
2804 else /* Visual is before curwin->w_cursor */
2806 top = &VIsual;
2807 bot = &curwin->w_cursor;
2809 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
2810 if (VIsual_mode == Ctrl_V) /* block mode */
2812 if (lnum_in_visual_area)
2814 fromcol = wp->w_old_cursor_fcol;
2815 tocol = wp->w_old_cursor_lcol;
2818 else /* non-block mode */
2820 if (lnum > top->lnum && lnum <= bot->lnum)
2821 fromcol = 0;
2822 else if (lnum == top->lnum)
2824 if (VIsual_mode == 'V') /* linewise */
2825 fromcol = 0;
2826 else
2828 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
2829 if (gchar_pos(top) == NUL)
2830 tocol = fromcol + 1;
2833 if (VIsual_mode != 'V' && lnum == bot->lnum)
2835 if (*p_sel == 'e' && bot->col == 0
2836 #ifdef FEAT_VIRTUALEDIT
2837 && bot->coladd == 0
2838 #endif
2841 fromcol = -10;
2842 tocol = MAXCOL;
2844 else if (bot->col == MAXCOL)
2845 tocol = MAXCOL;
2846 else
2848 pos = *bot;
2849 if (*p_sel == 'e')
2850 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
2851 else
2853 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
2854 ++tocol;
2860 #ifndef MSDOS
2861 /* Check if the character under the cursor should not be inverted */
2862 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
2863 # ifdef FEAT_GUI
2864 && !gui.in_use
2865 # endif
2867 noinvcur = TRUE;
2868 #endif
2870 /* if inverting in this line set area_highlighting */
2871 if (fromcol >= 0)
2873 area_highlighting = TRUE;
2874 attr = hl_attr(HLF_V);
2875 #if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
2876 if (clip_star.available && !clip_star.owned && clip_isautosel())
2877 attr = hl_attr(HLF_VNC);
2878 #endif
2883 * handle 'incsearch' and ":s///c" highlighting
2885 else
2886 #endif /* FEAT_VISUAL */
2887 if (highlight_match
2888 && wp == curwin
2889 && lnum >= curwin->w_cursor.lnum
2890 && lnum <= curwin->w_cursor.lnum + search_match_lines)
2892 if (lnum == curwin->w_cursor.lnum)
2893 getvcol(curwin, &(curwin->w_cursor),
2894 (colnr_T *)&fromcol, NULL, NULL);
2895 else
2896 fromcol = 0;
2897 if (lnum == curwin->w_cursor.lnum + search_match_lines)
2899 pos.lnum = lnum;
2900 pos.col = search_match_endcol;
2901 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
2903 else
2904 tocol = MAXCOL;
2905 if (fromcol == tocol) /* do at least one character */
2906 tocol = fromcol + 1; /* happens when past end of line */
2907 area_highlighting = TRUE;
2908 attr = hl_attr(HLF_I);
2911 #ifdef FEAT_DIFF
2912 filler_lines = diff_check(wp, lnum);
2913 if (filler_lines < 0)
2915 if (filler_lines == -1)
2917 if (diff_find_change(wp, lnum, &change_start, &change_end))
2918 diff_hlf = HLF_ADD; /* added line */
2919 else if (change_start == 0)
2920 diff_hlf = HLF_TXD; /* changed text */
2921 else
2922 diff_hlf = HLF_CHD; /* changed line */
2924 else
2925 diff_hlf = HLF_ADD; /* added line */
2926 filler_lines = 0;
2927 area_highlighting = TRUE;
2929 if (lnum == wp->w_topline)
2930 filler_lines = wp->w_topfill;
2931 filler_todo = filler_lines;
2932 #endif
2934 #ifdef LINE_ATTR
2935 # ifdef FEAT_SIGNS
2936 /* If this line has a sign with line highlighting set line_attr. */
2937 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
2938 if (v != 0)
2939 line_attr = sign_get_attr((int)v, TRUE);
2940 # endif
2941 # if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
2942 /* Highlight the current line in the quickfix window. */
2943 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
2944 line_attr = hl_attr(HLF_L);
2945 # endif
2946 if (line_attr != 0)
2947 area_highlighting = TRUE;
2948 #endif
2950 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
2951 ptr = line;
2953 #ifdef FEAT_SPELL
2954 if (has_spell)
2956 /* For checking first word with a capital skip white space. */
2957 if (cap_col == 0)
2958 cap_col = (int)(skipwhite(line) - line);
2960 /* To be able to spell-check over line boundaries copy the end of the
2961 * current line into nextline[]. Above the start of the next line was
2962 * copied to nextline[SPWORDLEN]. */
2963 if (nextline[SPWORDLEN] == NUL)
2965 /* No next line or it is empty. */
2966 nextlinecol = MAXCOL;
2967 nextline_idx = 0;
2969 else
2971 v = (long)STRLEN(line);
2972 if (v < SPWORDLEN)
2974 /* Short line, use it completely and append the start of the
2975 * next line. */
2976 nextlinecol = 0;
2977 mch_memmove(nextline, line, (size_t)v);
2978 STRMOVE(nextline + v, nextline + SPWORDLEN);
2979 nextline_idx = v + 1;
2981 else
2983 /* Long line, use only the last SPWORDLEN bytes. */
2984 nextlinecol = v - SPWORDLEN;
2985 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
2986 nextline_idx = SPWORDLEN + 1;
2990 #endif
2992 /* find start of trailing whitespace */
2993 if (wp->w_p_list && lcs_trail)
2995 trailcol = (colnr_T)STRLEN(ptr);
2996 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
2997 --trailcol;
2998 trailcol += (colnr_T) (ptr - line);
2999 extra_check = TRUE;
3003 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3004 * first character to be displayed.
3006 if (wp->w_p_wrap)
3007 v = wp->w_skipcol;
3008 else
3009 v = wp->w_leftcol;
3010 if (v > 0)
3012 #ifdef FEAT_MBYTE
3013 char_u *prev_ptr = ptr;
3014 #endif
3015 while (vcol < v && *ptr != NUL)
3017 c = win_lbr_chartabsize(wp, ptr, (colnr_T)vcol, NULL);
3018 vcol += c;
3019 #ifdef FEAT_MBYTE
3020 prev_ptr = ptr;
3021 #endif
3022 mb_ptr_adv(ptr);
3025 #ifdef FEAT_VIRTUALEDIT
3026 /* When 'virtualedit' is set the end of the line may be before the
3027 * start of the displayed part. */
3028 if (vcol < v && *ptr == NUL && virtual_active())
3029 vcol = v;
3030 #endif
3032 /* Handle a character that's not completely on the screen: Put ptr at
3033 * that character but skip the first few screen characters. */
3034 if (vcol > v)
3036 vcol -= c;
3037 #ifdef FEAT_MBYTE
3038 ptr = prev_ptr;
3039 #else
3040 --ptr;
3041 #endif
3042 n_skip = v - vcol;
3046 * Adjust for when the inverted text is before the screen,
3047 * and when the start of the inverted text is before the screen.
3049 if (tocol <= vcol)
3050 fromcol = 0;
3051 else if (fromcol >= 0 && fromcol < vcol)
3052 fromcol = vcol;
3054 #ifdef FEAT_LINEBREAK
3055 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3056 if (wp->w_p_wrap)
3057 need_showbreak = TRUE;
3058 #endif
3059 #ifdef FEAT_SPELL
3060 /* When spell checking a word we need to figure out the start of the
3061 * word and if it's badly spelled or not. */
3062 if (has_spell)
3064 int len;
3065 colnr_T linecol = (colnr_T)(ptr - line);
3066 hlf_T spell_hlf = HLF_COUNT;
3068 pos = wp->w_cursor;
3069 wp->w_cursor.lnum = lnum;
3070 wp->w_cursor.col = linecol;
3071 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
3073 /* spell_move_to() may call ml_get() and make "line" invalid */
3074 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3075 ptr = line + linecol;
3077 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
3079 /* no bad word found at line start, don't check until end of a
3080 * word */
3081 spell_hlf = HLF_COUNT;
3082 word_end = (int)(spell_to_word_end(ptr, wp->w_buffer)
3083 - line + 1);
3085 else
3087 /* bad word found, use attributes until end of word */
3088 word_end = wp->w_cursor.col + len + 1;
3090 /* Turn index into actual attributes. */
3091 if (spell_hlf != HLF_COUNT)
3092 spell_attr = highlight_attr[spell_hlf];
3094 wp->w_cursor = pos;
3096 # ifdef FEAT_SYN_HL
3097 /* Need to restart syntax highlighting for this line. */
3098 if (has_syntax)
3099 syntax_start(wp, lnum);
3100 # endif
3102 #endif
3106 * Correct highlighting for cursor that can't be disabled.
3107 * Avoids having to check this for each character.
3109 if (fromcol >= 0)
3111 if (noinvcur)
3113 if ((colnr_T)fromcol == wp->w_virtcol)
3115 /* highlighting starts at cursor, let it start just after the
3116 * cursor */
3117 fromcol_prev = fromcol;
3118 fromcol = -1;
3120 else if ((colnr_T)fromcol < wp->w_virtcol)
3121 /* restart highlighting after the cursor */
3122 fromcol_prev = wp->w_virtcol;
3124 if (fromcol >= tocol)
3125 fromcol = -1;
3128 #ifdef FEAT_SEARCH_EXTRA
3130 * Handle highlighting the last used search pattern and matches.
3131 * Do this for both search_hl and the match list.
3133 cur = wp->w_match_head;
3134 shl_flag = FALSE;
3135 while (cur != NULL || shl_flag == FALSE)
3137 if (shl_flag == FALSE)
3139 shl = &search_hl;
3140 shl_flag = TRUE;
3142 else
3143 shl = &cur->hl;
3144 shl->startcol = MAXCOL;
3145 shl->endcol = MAXCOL;
3146 shl->attr_cur = 0;
3147 if (shl->rm.regprog != NULL)
3149 v = (long)(ptr - line);
3150 next_search_hl(wp, shl, lnum, (colnr_T)v);
3152 /* Need to get the line again, a multi-line regexp may have made it
3153 * invalid. */
3154 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3155 ptr = line + v;
3157 if (shl->lnum != 0 && shl->lnum <= lnum)
3159 if (shl->lnum == lnum)
3160 shl->startcol = shl->rm.startpos[0].col;
3161 else
3162 shl->startcol = 0;
3163 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3164 - shl->rm.startpos[0].lnum)
3165 shl->endcol = shl->rm.endpos[0].col;
3166 else
3167 shl->endcol = MAXCOL;
3168 /* Highlight one character for an empty match. */
3169 if (shl->startcol == shl->endcol)
3171 #ifdef FEAT_MBYTE
3172 if (has_mbyte && line[shl->endcol] != NUL)
3173 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3174 else
3175 #endif
3176 ++shl->endcol;
3178 if ((long)shl->startcol < v) /* match at leftcol */
3180 shl->attr_cur = shl->attr;
3181 search_attr = shl->attr;
3183 area_highlighting = TRUE;
3186 if (shl != &search_hl && cur != NULL)
3187 cur = cur->next;
3189 #endif
3191 #ifdef FEAT_SYN_HL
3192 /* Cursor line highlighting for 'cursorline'. Not when Visual mode is
3193 * active, because it's not clear what is selected then. */
3194 if (wp->w_p_cul && lnum == wp->w_cursor.lnum && !VIsual_active)
3196 line_attr = hl_attr(HLF_CUL);
3197 area_highlighting = TRUE;
3199 #endif
3201 off = (unsigned)(current_ScreenLine - ScreenLines);
3202 col = 0;
3203 #ifdef FEAT_RIGHTLEFT
3204 if (wp->w_p_rl)
3206 /* Rightleft window: process the text in the normal direction, but put
3207 * it in current_ScreenLine[] from right to left. Start at the
3208 * rightmost column of the window. */
3209 col = W_WIDTH(wp) - 1;
3210 off += col;
3212 #endif
3215 * Repeat for the whole displayed line.
3217 for (;;)
3219 /* Skip this quickly when working on the text. */
3220 if (draw_state != WL_LINE)
3222 #ifdef FEAT_CMDWIN
3223 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3225 draw_state = WL_CMDLINE;
3226 if (cmdwin_type != 0 && wp == curwin)
3228 /* Draw the cmdline character. */
3229 n_extra = 1;
3230 c_extra = cmdwin_type;
3231 char_attr = hl_attr(HLF_AT);
3234 #endif
3236 #ifdef FEAT_FOLDING
3237 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3239 draw_state = WL_FOLD;
3240 if (wp->w_p_fdc > 0)
3242 /* Draw the 'foldcolumn'. */
3243 fill_foldcolumn(extra, wp, FALSE, lnum);
3244 n_extra = wp->w_p_fdc;
3245 p_extra = extra;
3246 p_extra[n_extra] = NUL;
3247 c_extra = NUL;
3248 char_attr = hl_attr(HLF_FC);
3251 #endif
3253 #ifdef FEAT_SIGNS
3254 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3256 draw_state = WL_SIGN;
3257 /* Show the sign column when there are any signs in this
3258 * buffer or when using Netbeans. */
3259 if (draw_signcolumn(wp)
3260 # ifdef FEAT_DIFF
3261 && filler_todo <= 0
3262 # endif
3265 int_u text_sign;
3266 # ifdef FEAT_SIGN_ICONS
3267 int_u icon_sign;
3268 # endif
3270 /* Draw two cells with the sign value or blank. */
3271 c_extra = ' ';
3272 char_attr = hl_attr(HLF_SC);
3273 n_extra = 2;
3275 if (row == startrow)
3277 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3278 SIGN_TEXT);
3279 # ifdef FEAT_SIGN_ICONS
3280 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3281 SIGN_ICON);
3282 if (gui.in_use && icon_sign != 0)
3284 /* Use the image in this position. */
3285 c_extra = SIGN_BYTE;
3286 # ifdef FEAT_NETBEANS_INTG
3287 if (buf_signcount(wp->w_buffer, lnum) > 1)
3288 c_extra = MULTISIGN_BYTE;
3289 # endif
3290 char_attr = icon_sign;
3292 else
3293 # endif
3294 if (text_sign != 0)
3296 p_extra = sign_get_text(text_sign);
3297 if (p_extra != NULL)
3299 c_extra = NUL;
3300 n_extra = (int)STRLEN(p_extra);
3302 char_attr = sign_get_attr(text_sign, FALSE);
3307 #endif
3309 if (draw_state == WL_NR - 1 && n_extra == 0)
3311 draw_state = WL_NR;
3312 /* Display the absolute or relative line number. After the
3313 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3314 if ((wp->w_p_nu || wp->w_p_rnu)
3315 && (row == startrow
3316 #ifdef FEAT_DIFF
3317 + filler_lines
3318 #endif
3319 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3321 /* Draw the line number (empty space after wrapping). */
3322 if (row == startrow
3323 #ifdef FEAT_DIFF
3324 + filler_lines
3325 #endif
3328 long num;
3330 if (wp->w_p_nu)
3331 /* 'number' */
3332 num = (long)lnum;
3333 else
3334 /* 'relativenumber', don't use negative numbers */
3335 num = (long)abs((int)get_cursor_rel_lnum(wp,
3336 lnum));
3338 sprintf((char *)extra, "%*ld ",
3339 number_width(wp), num);
3340 if (wp->w_skipcol > 0)
3341 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3342 *p_extra = '-';
3343 #ifdef FEAT_RIGHTLEFT
3344 if (wp->w_p_rl) /* reverse line numbers */
3345 rl_mirror(extra);
3346 #endif
3347 p_extra = extra;
3348 c_extra = NUL;
3350 else
3351 c_extra = ' ';
3352 n_extra = number_width(wp) + 1;
3353 char_attr = hl_attr(HLF_N);
3354 #ifdef FEAT_SYN_HL
3355 /* When 'cursorline' is set highlight the line number of
3356 * the current line differently. */
3357 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3358 char_attr = hl_combine_attr(hl_attr(HLF_CUL), char_attr);
3359 #endif
3363 #if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3364 if (draw_state == WL_SBR - 1 && n_extra == 0)
3366 draw_state = WL_SBR;
3367 # ifdef FEAT_DIFF
3368 if (filler_todo > 0)
3370 /* Draw "deleted" diff line(s). */
3371 if (char2cells(fill_diff) > 1)
3372 c_extra = '-';
3373 else
3374 c_extra = fill_diff;
3375 # ifdef FEAT_RIGHTLEFT
3376 if (wp->w_p_rl)
3377 n_extra = col + 1;
3378 else
3379 # endif
3380 n_extra = W_WIDTH(wp) - col;
3381 char_attr = hl_attr(HLF_DED);
3383 # endif
3384 # ifdef FEAT_LINEBREAK
3385 if (*p_sbr != NUL && need_showbreak)
3387 /* Draw 'showbreak' at the start of each broken line. */
3388 p_extra = p_sbr;
3389 c_extra = NUL;
3390 n_extra = (int)STRLEN(p_sbr);
3391 char_attr = hl_attr(HLF_AT);
3392 need_showbreak = FALSE;
3393 /* Correct end of highlighted area for 'showbreak',
3394 * required when 'linebreak' is also set. */
3395 if (tocol == vcol)
3396 tocol += n_extra;
3398 # endif
3400 #endif
3402 if (draw_state == WL_LINE - 1 && n_extra == 0)
3404 draw_state = WL_LINE;
3405 if (saved_n_extra)
3407 /* Continue item from end of wrapped line. */
3408 n_extra = saved_n_extra;
3409 c_extra = saved_c_extra;
3410 p_extra = saved_p_extra;
3411 char_attr = saved_char_attr;
3413 else
3414 char_attr = 0;
3418 /* When still displaying '$' of change command, stop at cursor */
3419 if (dollar_vcol != 0 && wp == curwin
3420 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
3421 #ifdef FEAT_DIFF
3422 && filler_todo <= 0
3423 #endif
3426 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3427 wp->w_p_rl);
3428 /* Pretend we have finished updating the window. Except when
3429 * 'cursorcolumn' is set. */
3430 #ifdef FEAT_SYN_HL
3431 if (wp->w_p_cuc)
3432 row = wp->w_cline_row + wp->w_cline_height;
3433 else
3434 #endif
3435 row = wp->w_height;
3436 break;
3439 if (draw_state == WL_LINE && area_highlighting)
3441 /* handle Visual or match highlighting in this line */
3442 if (vcol == fromcol
3443 #ifdef FEAT_MBYTE
3444 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3445 && (*mb_ptr2cells)(ptr) > 1)
3446 #endif
3447 || ((int)vcol_prev == fromcol_prev
3448 && vcol_prev < vcol /* not at margin */
3449 && vcol < tocol))
3450 area_attr = attr; /* start highlighting */
3451 else if (area_attr != 0
3452 && (vcol == tocol
3453 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
3454 area_attr = 0; /* stop highlighting */
3456 #ifdef FEAT_SEARCH_EXTRA
3457 if (!n_extra)
3460 * Check for start/end of search pattern match.
3461 * After end, check for start/end of next match.
3462 * When another match, have to check for start again.
3463 * Watch out for matching an empty string!
3464 * Do this for 'search_hl' and the match list (ordered by
3465 * priority).
3467 v = (long)(ptr - line);
3468 cur = wp->w_match_head;
3469 shl_flag = FALSE;
3470 while (cur != NULL || shl_flag == FALSE)
3472 if (shl_flag == FALSE
3473 && ((cur != NULL
3474 && cur->priority > SEARCH_HL_PRIORITY)
3475 || cur == NULL))
3477 shl = &search_hl;
3478 shl_flag = TRUE;
3480 else
3481 shl = &cur->hl;
3482 while (shl->rm.regprog != NULL)
3484 if (shl->startcol != MAXCOL
3485 && v >= (long)shl->startcol
3486 && v < (long)shl->endcol)
3488 shl->attr_cur = shl->attr;
3490 else if (v == (long)shl->endcol)
3492 shl->attr_cur = 0;
3494 next_search_hl(wp, shl, lnum, (colnr_T)v);
3496 /* Need to get the line again, a multi-line regexp
3497 * may have made it invalid. */
3498 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3499 ptr = line + v;
3501 if (shl->lnum == lnum)
3503 shl->startcol = shl->rm.startpos[0].col;
3504 if (shl->rm.endpos[0].lnum == 0)
3505 shl->endcol = shl->rm.endpos[0].col;
3506 else
3507 shl->endcol = MAXCOL;
3509 if (shl->startcol == shl->endcol)
3511 /* highlight empty match, try again after
3512 * it */
3513 #ifdef FEAT_MBYTE
3514 if (has_mbyte)
3515 shl->endcol += (*mb_ptr2len)(line
3516 + shl->endcol);
3517 else
3518 #endif
3519 ++shl->endcol;
3522 /* Loop to check if the match starts at the
3523 * current position */
3524 continue;
3527 break;
3529 if (shl != &search_hl && cur != NULL)
3530 cur = cur->next;
3533 /* Use attributes from match with highest priority among
3534 * 'search_hl' and the match list. */
3535 search_attr = search_hl.attr_cur;
3536 cur = wp->w_match_head;
3537 shl_flag = FALSE;
3538 while (cur != NULL || shl_flag == FALSE)
3540 if (shl_flag == FALSE
3541 && ((cur != NULL
3542 && cur->priority > SEARCH_HL_PRIORITY)
3543 || cur == NULL))
3545 shl = &search_hl;
3546 shl_flag = TRUE;
3548 else
3549 shl = &cur->hl;
3550 if (shl->attr_cur != 0)
3551 search_attr = shl->attr_cur;
3552 if (shl != &search_hl && cur != NULL)
3553 cur = cur->next;
3556 #endif
3558 #ifdef FEAT_DIFF
3559 if (diff_hlf != (hlf_T)0)
3561 if (diff_hlf == HLF_CHD && ptr - line >= change_start
3562 && n_extra == 0)
3563 diff_hlf = HLF_TXD; /* changed text */
3564 if (diff_hlf == HLF_TXD && ptr - line > change_end
3565 && n_extra == 0)
3566 diff_hlf = HLF_CHD; /* changed line */
3567 line_attr = hl_attr(diff_hlf);
3569 #endif
3571 /* Decide which of the highlight attributes to use. */
3572 attr_pri = TRUE;
3573 if (area_attr != 0)
3574 char_attr = area_attr;
3575 else if (search_attr != 0)
3576 char_attr = search_attr;
3577 #ifdef LINE_ATTR
3578 /* Use line_attr when not in the Visual or 'incsearch' area
3579 * (area_attr may be 0 when "noinvcur" is set). */
3580 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
3581 || (vcol < fromcol || vcol >= tocol)))
3582 char_attr = line_attr;
3583 #endif
3584 else
3586 attr_pri = FALSE;
3587 #ifdef FEAT_SYN_HL
3588 if (has_syntax)
3589 char_attr = syntax_attr;
3590 else
3591 #endif
3592 char_attr = 0;
3597 * Get the next character to put on the screen.
3600 * The "p_extra" points to the extra stuff that is inserted to
3601 * represent special characters (non-printable stuff) and other
3602 * things. When all characters are the same, c_extra is used.
3603 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
3604 * "p_extra[n_extra]".
3605 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
3607 if (n_extra > 0)
3609 if (c_extra != NUL)
3611 c = c_extra;
3612 #ifdef FEAT_MBYTE
3613 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
3614 if (enc_utf8 && (*mb_char2len)(c) > 1)
3616 mb_utf8 = TRUE;
3617 u8cc[0] = 0;
3618 c = 0xc0;
3620 else
3621 mb_utf8 = FALSE;
3622 #endif
3624 else
3626 c = *p_extra;
3627 #ifdef FEAT_MBYTE
3628 if (has_mbyte)
3630 mb_c = c;
3631 if (enc_utf8)
3633 /* If the UTF-8 character is more than one byte:
3634 * Decode it into "mb_c". */
3635 mb_l = (*mb_ptr2len)(p_extra);
3636 mb_utf8 = FALSE;
3637 if (mb_l > n_extra)
3638 mb_l = 1;
3639 else if (mb_l > 1)
3641 mb_c = utfc_ptr2char(p_extra, u8cc);
3642 mb_utf8 = TRUE;
3643 c = 0xc0;
3646 else
3648 /* if this is a DBCS character, put it in "mb_c" */
3649 mb_l = MB_BYTE2LEN(c);
3650 if (mb_l >= n_extra)
3651 mb_l = 1;
3652 else if (mb_l > 1)
3653 mb_c = (c << 8) + p_extra[1];
3655 if (mb_l == 0) /* at the NUL at end-of-line */
3656 mb_l = 1;
3658 /* If a double-width char doesn't fit display a '>' in the
3659 * last column. */
3660 if ((
3661 # ifdef FEAT_RIGHTLEFT
3662 wp->w_p_rl ? (col <= 0) :
3663 # endif
3664 (col >= W_WIDTH(wp) - 1))
3665 && (*mb_char2cells)(mb_c) == 2)
3667 c = '>';
3668 mb_c = c;
3669 mb_l = 1;
3670 mb_utf8 = FALSE;
3671 multi_attr = hl_attr(HLF_AT);
3672 /* put the pointer back to output the double-width
3673 * character at the start of the next line. */
3674 ++n_extra;
3675 --p_extra;
3677 else
3679 n_extra -= mb_l - 1;
3680 p_extra += mb_l - 1;
3683 #endif
3684 ++p_extra;
3686 --n_extra;
3688 else
3691 * Get a character from the line itself.
3693 c = *ptr;
3694 #ifdef FEAT_MBYTE
3695 if (has_mbyte)
3697 mb_c = c;
3698 if (enc_utf8)
3700 /* If the UTF-8 character is more than one byte: Decode it
3701 * into "mb_c". */
3702 mb_l = (*mb_ptr2len)(ptr);
3703 mb_utf8 = FALSE;
3704 if (mb_l > 1)
3706 mb_c = utfc_ptr2char(ptr, u8cc);
3707 /* Overlong encoded ASCII or ASCII with composing char
3708 * is displayed normally, except a NUL. */
3709 if (mb_c < 0x80)
3710 c = mb_c;
3711 mb_utf8 = TRUE;
3713 /* At start of the line we can have a composing char.
3714 * Draw it as a space with a composing char. */
3715 if (utf_iscomposing(mb_c))
3717 int i;
3719 for (i = Screen_mco - 1; i > 0; --i)
3720 u8cc[i] = u8cc[i - 1];
3721 u8cc[0] = mb_c;
3722 mb_c = ' ';
3726 if ((mb_l == 1 && c >= 0x80)
3727 || (mb_l >= 1 && mb_c == 0)
3728 || (mb_l > 1 && (!vim_isprintc(mb_c)
3729 # ifdef UNICODE16
3730 || mb_c >= 0x10000
3731 # endif
3735 * Illegal UTF-8 byte: display as <xx>.
3736 * Non-BMP character : display as ? or fullwidth ?.
3738 # ifdef UNICODE16
3739 if (mb_c < 0x10000)
3740 # endif
3742 transchar_hex(extra, mb_c);
3743 # ifdef FEAT_RIGHTLEFT
3744 if (wp->w_p_rl) /* reverse */
3745 rl_mirror(extra);
3746 # endif
3748 # ifdef UNICODE16
3749 else if (utf_char2cells(mb_c) != 2)
3750 STRCPY(extra, "?");
3751 else
3752 /* 0xff1f in UTF-8: full-width '?' */
3753 STRCPY(extra, "\357\274\237");
3754 # endif
3756 p_extra = extra;
3757 c = *p_extra;
3758 mb_c = mb_ptr2char_adv(&p_extra);
3759 mb_utf8 = (c >= 0x80);
3760 n_extra = (int)STRLEN(p_extra);
3761 c_extra = NUL;
3762 if (area_attr == 0 && search_attr == 0)
3764 n_attr = n_extra + 1;
3765 extra_attr = hl_attr(HLF_8);
3766 saved_attr2 = char_attr; /* save current attr */
3769 else if (mb_l == 0) /* at the NUL at end-of-line */
3770 mb_l = 1;
3771 #ifdef FEAT_ARABIC
3772 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
3774 /* Do Arabic shaping. */
3775 int pc, pc1, nc;
3776 int pcc[MAX_MCO];
3778 /* The idea of what is the previous and next
3779 * character depends on 'rightleft'. */
3780 if (wp->w_p_rl)
3782 pc = prev_c;
3783 pc1 = prev_c1;
3784 nc = utf_ptr2char(ptr + mb_l);
3785 prev_c1 = u8cc[0];
3787 else
3789 pc = utfc_ptr2char(ptr + mb_l, pcc);
3790 nc = prev_c;
3791 pc1 = pcc[0];
3793 prev_c = mb_c;
3795 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
3797 else
3798 prev_c = mb_c;
3799 #endif
3801 else /* enc_dbcs */
3803 mb_l = MB_BYTE2LEN(c);
3804 if (mb_l == 0) /* at the NUL at end-of-line */
3805 mb_l = 1;
3806 else if (mb_l > 1)
3808 /* We assume a second byte below 32 is illegal.
3809 * Hopefully this is OK for all double-byte encodings!
3811 if (ptr[1] >= 32)
3812 mb_c = (c << 8) + ptr[1];
3813 else
3815 if (ptr[1] == NUL)
3817 /* head byte at end of line */
3818 mb_l = 1;
3819 transchar_nonprint(extra, c);
3821 else
3823 /* illegal tail byte */
3824 mb_l = 2;
3825 STRCPY(extra, "XX");
3827 p_extra = extra;
3828 n_extra = (int)STRLEN(extra) - 1;
3829 c_extra = NUL;
3830 c = *p_extra++;
3831 if (area_attr == 0 && search_attr == 0)
3833 n_attr = n_extra + 1;
3834 extra_attr = hl_attr(HLF_8);
3835 saved_attr2 = char_attr; /* save current attr */
3837 mb_c = c;
3841 /* If a double-width char doesn't fit display a '>' in the
3842 * last column; the character is displayed at the start of the
3843 * next line. */
3844 if ((
3845 # ifdef FEAT_RIGHTLEFT
3846 wp->w_p_rl ? (col <= 0) :
3847 # endif
3848 (col >= W_WIDTH(wp) - 1))
3849 && (*mb_char2cells)(mb_c) == 2)
3851 c = '>';
3852 mb_c = c;
3853 mb_utf8 = FALSE;
3854 mb_l = 1;
3855 multi_attr = hl_attr(HLF_AT);
3856 /* Put pointer back so that the character will be
3857 * displayed at the start of the next line. */
3858 --ptr;
3860 else if (*ptr != NUL)
3861 ptr += mb_l - 1;
3863 /* If a double-width char doesn't fit at the left side display
3864 * a '<' in the first column. */
3865 if (n_skip > 0 && mb_l > 1)
3867 n_extra = 1;
3868 c_extra = '<';
3869 c = ' ';
3870 if (area_attr == 0 && search_attr == 0)
3872 n_attr = n_extra + 1;
3873 extra_attr = hl_attr(HLF_AT);
3874 saved_attr2 = char_attr; /* save current attr */
3876 mb_c = c;
3877 mb_utf8 = FALSE;
3878 mb_l = 1;
3882 #endif
3883 ++ptr;
3885 /* 'list' : change char 160 to lcs_nbsp. */
3886 if (wp->w_p_list && (c == 160
3887 #ifdef FEAT_MBYTE
3888 || (mb_utf8 && mb_c == 160)
3889 #endif
3890 ) && lcs_nbsp)
3892 c = lcs_nbsp;
3893 if (area_attr == 0 && search_attr == 0)
3895 n_attr = 1;
3896 extra_attr = hl_attr(HLF_8);
3897 saved_attr2 = char_attr; /* save current attr */
3899 #ifdef FEAT_MBYTE
3900 mb_c = c;
3901 if (enc_utf8 && (*mb_char2len)(c) > 1)
3903 mb_utf8 = TRUE;
3904 u8cc[0] = 0;
3905 c = 0xc0;
3907 else
3908 mb_utf8 = FALSE;
3909 #endif
3912 if (extra_check)
3914 #ifdef FEAT_SPELL
3915 int can_spell = TRUE;
3916 #endif
3918 #ifdef FEAT_SYN_HL
3919 /* Get syntax attribute, unless still at the start of the line
3920 * (double-wide char that doesn't fit). */
3921 v = (long)(ptr - line);
3922 if (has_syntax && v > 0)
3924 /* Get the syntax attribute for the character. If there
3925 * is an error, disable syntax highlighting. */
3926 save_did_emsg = did_emsg;
3927 did_emsg = FALSE;
3929 syntax_attr = get_syntax_attr((colnr_T)v - 1,
3930 # ifdef FEAT_SPELL
3931 has_spell ? &can_spell :
3932 # endif
3933 NULL, FALSE);
3935 if (did_emsg)
3937 wp->w_buffer->b_syn_error = TRUE;
3938 has_syntax = FALSE;
3940 else
3941 did_emsg = save_did_emsg;
3943 /* Need to get the line again, a multi-line regexp may
3944 * have made it invalid. */
3945 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3946 ptr = line + v;
3948 if (!attr_pri)
3949 char_attr = syntax_attr;
3950 else
3951 char_attr = hl_combine_attr(syntax_attr, char_attr);
3953 #endif
3955 #ifdef FEAT_SPELL
3956 /* Check spelling (unless at the end of the line).
3957 * Only do this when there is no syntax highlighting, the
3958 * @Spell cluster is not used or the current syntax item
3959 * contains the @Spell cluster. */
3960 if (has_spell && v >= word_end && v > cur_checked_col)
3962 spell_attr = 0;
3963 # ifdef FEAT_SYN_HL
3964 if (!attr_pri)
3965 char_attr = syntax_attr;
3966 # endif
3967 if (c != 0 && (
3968 # ifdef FEAT_SYN_HL
3969 !has_syntax ||
3970 # endif
3971 can_spell))
3973 char_u *prev_ptr, *p;
3974 int len;
3975 hlf_T spell_hlf = HLF_COUNT;
3976 # ifdef FEAT_MBYTE
3977 if (has_mbyte)
3979 prev_ptr = ptr - mb_l;
3980 v -= mb_l - 1;
3982 else
3983 # endif
3984 prev_ptr = ptr - 1;
3986 /* Use nextline[] if possible, it has the start of the
3987 * next line concatenated. */
3988 if ((prev_ptr - line) - nextlinecol >= 0)
3989 p = nextline + (prev_ptr - line) - nextlinecol;
3990 else
3991 p = prev_ptr;
3992 cap_col -= (int)(prev_ptr - line);
3993 len = spell_check(wp, p, &spell_hlf, &cap_col,
3994 nochange);
3995 word_end = v + len;
3997 /* In Insert mode only highlight a word that
3998 * doesn't touch the cursor. */
3999 if (spell_hlf != HLF_COUNT
4000 && (State & INSERT) != 0
4001 && wp->w_cursor.lnum == lnum
4002 && wp->w_cursor.col >=
4003 (colnr_T)(prev_ptr - line)
4004 && wp->w_cursor.col < (colnr_T)word_end)
4006 spell_hlf = HLF_COUNT;
4007 spell_redraw_lnum = lnum;
4010 if (spell_hlf == HLF_COUNT && p != prev_ptr
4011 && (p - nextline) + len > nextline_idx)
4013 /* Remember that the good word continues at the
4014 * start of the next line. */
4015 checked_lnum = lnum + 1;
4016 checked_col = (int)((p - nextline) + len - nextline_idx);
4019 /* Turn index into actual attributes. */
4020 if (spell_hlf != HLF_COUNT)
4021 spell_attr = highlight_attr[spell_hlf];
4023 if (cap_col > 0)
4025 if (p != prev_ptr
4026 && (p - nextline) + cap_col >= nextline_idx)
4028 /* Remember that the word in the next line
4029 * must start with a capital. */
4030 capcol_lnum = lnum + 1;
4031 cap_col = (int)((p - nextline) + cap_col
4032 - nextline_idx);
4034 else
4035 /* Compute the actual column. */
4036 cap_col += (int)(prev_ptr - line);
4040 if (spell_attr != 0)
4042 if (!attr_pri)
4043 char_attr = hl_combine_attr(char_attr, spell_attr);
4044 else
4045 char_attr = hl_combine_attr(spell_attr, char_attr);
4047 #endif
4048 #ifdef FEAT_LINEBREAK
4050 * Found last space before word: check for line break.
4052 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr)
4053 && !wp->w_p_list)
4055 n_extra = win_lbr_chartabsize(wp, ptr - (
4056 # ifdef FEAT_MBYTE
4057 has_mbyte ? mb_l :
4058 # endif
4059 1), (colnr_T)vcol, NULL) - 1;
4060 c_extra = ' ';
4061 if (vim_iswhite(c))
4062 c = ' ';
4064 #endif
4066 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4068 c = lcs_trail;
4069 if (!attr_pri)
4071 n_attr = 1;
4072 extra_attr = hl_attr(HLF_8);
4073 saved_attr2 = char_attr; /* save current attr */
4075 #ifdef FEAT_MBYTE
4076 mb_c = c;
4077 if (enc_utf8 && (*mb_char2len)(c) > 1)
4079 mb_utf8 = TRUE;
4080 u8cc[0] = 0;
4081 c = 0xc0;
4083 else
4084 mb_utf8 = FALSE;
4085 #endif
4090 * Handling of non-printable characters.
4092 if (!(chartab[c & 0xff] & CT_PRINT_CHAR))
4095 * when getting a character from the file, we may have to
4096 * turn it into something else on the way to putting it
4097 * into "ScreenLines".
4099 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4101 /* tab amount depends on current column */
4102 n_extra = (int)wp->w_buffer->b_p_ts
4103 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4104 #ifdef FEAT_MBYTE
4105 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4106 #endif
4107 if (wp->w_p_list)
4109 c = lcs_tab1;
4110 c_extra = lcs_tab2;
4111 n_attr = n_extra + 1;
4112 extra_attr = hl_attr(HLF_8);
4113 saved_attr2 = char_attr; /* save current attr */
4114 #ifdef FEAT_MBYTE
4115 mb_c = c;
4116 if (enc_utf8 && (*mb_char2len)(c) > 1)
4118 mb_utf8 = TRUE;
4119 u8cc[0] = 0;
4120 c = 0xc0;
4122 #endif
4124 else
4126 c_extra = ' ';
4127 c = ' ';
4130 else if (c == NUL
4131 && ((wp->w_p_list && lcs_eol > 0)
4132 || ((fromcol >= 0 || fromcol_prev >= 0)
4133 && tocol > vcol
4134 #ifdef FEAT_VISUAL
4135 && VIsual_mode != Ctrl_V
4136 #endif
4137 && (
4138 # ifdef FEAT_RIGHTLEFT
4139 wp->w_p_rl ? (col >= 0) :
4140 # endif
4141 (col < W_WIDTH(wp)))
4142 && !(noinvcur
4143 && (colnr_T)vcol == wp->w_virtcol)))
4144 && lcs_eol_one >= 0)
4146 /* Display a '$' after the line or highlight an extra
4147 * character if the line break is included. */
4148 #if defined(FEAT_DIFF) || defined(LINE_ATTR)
4149 /* For a diff line the highlighting continues after the
4150 * "$". */
4151 if (
4152 # ifdef FEAT_DIFF
4153 diff_hlf == (hlf_T)0
4154 # ifdef LINE_ATTR
4156 # endif
4157 # endif
4158 # ifdef LINE_ATTR
4159 line_attr == 0
4160 # endif
4162 #endif
4164 #ifdef FEAT_VIRTUALEDIT
4165 /* In virtualedit, visual selections may extend
4166 * beyond end of line. */
4167 if (area_highlighting && virtual_active()
4168 && tocol != MAXCOL && vcol < tocol)
4169 n_extra = 0;
4170 else
4171 #endif
4173 p_extra = at_end_str;
4174 n_extra = 1;
4175 c_extra = NUL;
4178 if (wp->w_p_list)
4179 c = lcs_eol;
4180 else
4181 c = ' ';
4182 lcs_eol_one = -1;
4183 --ptr; /* put it back at the NUL */
4184 if (!attr_pri)
4186 extra_attr = hl_attr(HLF_AT);
4187 n_attr = 1;
4189 #ifdef FEAT_MBYTE
4190 mb_c = c;
4191 if (enc_utf8 && (*mb_char2len)(c) > 1)
4193 mb_utf8 = TRUE;
4194 u8cc[0] = 0;
4195 c = 0xc0;
4197 else
4198 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4199 #endif
4201 else if (c != NUL)
4203 p_extra = transchar(c);
4204 #ifdef FEAT_RIGHTLEFT
4205 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4206 rl_mirror(p_extra); /* reverse "<12>" */
4207 #endif
4208 n_extra = byte2cells(c) - 1;
4209 c_extra = NUL;
4210 c = *p_extra++;
4211 if (!attr_pri)
4213 n_attr = n_extra + 1;
4214 extra_attr = hl_attr(HLF_8);
4215 saved_attr2 = char_attr; /* save current attr */
4217 #ifdef FEAT_MBYTE
4218 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4219 #endif
4221 #ifdef FEAT_VIRTUALEDIT
4222 else if (VIsual_active
4223 && (VIsual_mode == Ctrl_V
4224 || VIsual_mode == 'v')
4225 && virtual_active()
4226 && tocol != MAXCOL
4227 && vcol < tocol
4228 && (
4229 # ifdef FEAT_RIGHTLEFT
4230 wp->w_p_rl ? (col >= 0) :
4231 # endif
4232 (col < W_WIDTH(wp))))
4234 c = ' ';
4235 --ptr; /* put it back at the NUL */
4237 #endif
4238 #if defined(LINE_ATTR)
4239 else if ((
4240 # ifdef FEAT_DIFF
4241 diff_hlf != (hlf_T)0 ||
4242 # endif
4243 line_attr != 0
4244 ) && (
4245 # ifdef FEAT_RIGHTLEFT
4246 wp->w_p_rl ? (col >= 0) :
4247 # endif
4248 (col < W_WIDTH(wp))))
4250 /* Highlight until the right side of the window */
4251 c = ' ';
4252 --ptr; /* put it back at the NUL */
4254 /* Remember we do the char for line highlighting. */
4255 ++did_line_attr;
4257 /* don't do search HL for the rest of the line */
4258 if (line_attr != 0 && char_attr == search_attr && col > 0)
4259 char_attr = line_attr;
4260 # ifdef FEAT_DIFF
4261 if (diff_hlf == HLF_TXD)
4263 diff_hlf = HLF_CHD;
4264 if (attr == 0 || char_attr != attr)
4265 char_attr = hl_attr(diff_hlf);
4267 # endif
4269 #endif
4273 /* Don't override visual selection highlighting. */
4274 if (n_attr > 0
4275 && draw_state == WL_LINE
4276 && !attr_pri)
4277 char_attr = extra_attr;
4279 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
4280 /* XIM don't send preedit_start and preedit_end, but they send
4281 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
4282 * im_is_preediting() here. */
4283 if (xic != NULL
4284 && lnum == curwin->w_cursor.lnum
4285 && (State & INSERT)
4286 && !p_imdisable
4287 && im_is_preediting()
4288 && draw_state == WL_LINE)
4290 colnr_T tcol;
4292 if (preedit_end_col == MAXCOL)
4293 getvcol(curwin, &(curwin->w_cursor), &tcol, NULL, NULL);
4294 else
4295 tcol = preedit_end_col;
4296 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
4298 if (feedback_old_attr < 0)
4300 feedback_col = 0;
4301 feedback_old_attr = char_attr;
4303 char_attr = im_get_feedback_attr(feedback_col);
4304 if (char_attr < 0)
4305 char_attr = feedback_old_attr;
4306 feedback_col++;
4308 else if (feedback_old_attr >= 0)
4310 char_attr = feedback_old_attr;
4311 feedback_old_attr = -1;
4312 feedback_col = 0;
4315 #endif
4317 * Handle the case where we are in column 0 but not on the first
4318 * character of the line and the user wants us to show us a
4319 * special character (via 'listchars' option "precedes:<char>".
4321 if (lcs_prec_todo != NUL
4322 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
4323 #ifdef FEAT_DIFF
4324 && filler_todo <= 0
4325 #endif
4326 && draw_state > WL_NR
4327 && c != NUL)
4329 c = lcs_prec;
4330 lcs_prec_todo = NUL;
4331 #ifdef FEAT_MBYTE
4332 mb_c = c;
4333 if (enc_utf8 && (*mb_char2len)(c) > 1)
4335 mb_utf8 = TRUE;
4336 u8cc[0] = 0;
4337 c = 0xc0;
4339 else
4340 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4341 #endif
4342 if (!attr_pri)
4344 saved_attr3 = char_attr; /* save current attr */
4345 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
4346 n_attr3 = 1;
4351 * At end of the text line or just after the last character.
4353 if (c == NUL
4354 #if defined(LINE_ATTR)
4355 || did_line_attr == 1
4356 #endif
4359 #ifdef FEAT_SEARCH_EXTRA
4360 long prevcol = (long)(ptr - line) - (c == NUL);
4362 /* we're not really at that column when skipping some text */
4363 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
4364 ++prevcol;
4365 #endif
4367 /* invert at least one char, used for Visual and empty line or
4368 * highlight match at end of line. If it's beyond the last
4369 * char on the screen, just overwrite that one (tricky!) Not
4370 * needed when a '$' was displayed for 'list'. */
4371 #ifdef FEAT_SEARCH_EXTRA
4372 prevcol_hl_flag = FALSE;
4373 if (prevcol == (long)search_hl.startcol)
4374 prevcol_hl_flag = TRUE;
4375 else
4377 cur = wp->w_match_head;
4378 while (cur != NULL)
4380 if (prevcol == (long)cur->hl.startcol)
4382 prevcol_hl_flag = TRUE;
4383 break;
4385 cur = cur->next;
4388 #endif
4389 if (lcs_eol == lcs_eol_one
4390 && ((area_attr != 0 && vcol == fromcol && c == NUL)
4391 #ifdef FEAT_SEARCH_EXTRA
4392 /* highlight 'hlsearch' match at end of line */
4393 || (prevcol_hl_flag == TRUE
4394 # if defined(LINE_ATTR)
4395 && did_line_attr <= 1
4396 # endif
4398 #endif
4401 int n = 0;
4403 #ifdef FEAT_RIGHTLEFT
4404 if (wp->w_p_rl)
4406 if (col < 0)
4407 n = 1;
4409 else
4410 #endif
4412 if (col >= W_WIDTH(wp))
4413 n = -1;
4415 if (n != 0)
4417 /* At the window boundary, highlight the last character
4418 * instead (better than nothing). */
4419 off += n;
4420 col += n;
4422 else
4424 /* Add a blank character to highlight. */
4425 ScreenLines[off] = ' ';
4426 #ifdef FEAT_MBYTE
4427 if (enc_utf8)
4428 ScreenLinesUC[off] = 0;
4429 #endif
4431 #ifdef FEAT_SEARCH_EXTRA
4432 if (area_attr == 0)
4434 /* Use attributes from match with highest priority among
4435 * 'search_hl' and the match list. */
4436 char_attr = search_hl.attr;
4437 cur = wp->w_match_head;
4438 shl_flag = FALSE;
4439 while (cur != NULL || shl_flag == FALSE)
4441 if (shl_flag == FALSE
4442 && ((cur != NULL
4443 && cur->priority > SEARCH_HL_PRIORITY)
4444 || cur == NULL))
4446 shl = &search_hl;
4447 shl_flag = TRUE;
4449 else
4450 shl = &cur->hl;
4451 if ((ptr - line) - 1 == (long)shl->startcol)
4452 char_attr = shl->attr;
4453 if (shl != &search_hl && cur != NULL)
4454 cur = cur->next;
4457 #endif
4458 ScreenAttrs[off] = char_attr;
4459 #ifdef FEAT_RIGHTLEFT
4460 if (wp->w_p_rl)
4462 --col;
4463 --off;
4465 else
4466 #endif
4468 ++col;
4469 ++off;
4471 ++vcol;
4472 #ifdef FEAT_SYN_HL
4473 eol_hl_off = 1;
4474 #endif
4479 * At end of the text line.
4481 if (c == NUL)
4483 #ifdef FEAT_SYN_HL
4484 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol)
4486 /* highlight last char after line */
4487 --col;
4488 --off;
4489 --vcol;
4492 /* Highlight 'cursorcolumn' past end of the line. */
4493 if (wp->w_p_wrap)
4494 v = wp->w_skipcol;
4495 else
4496 v = wp->w_leftcol;
4497 /* check if line ends before left margin */
4498 if (vcol < v + col - win_col_off(wp))
4500 vcol = v + col - win_col_off(wp);
4501 if (wp->w_p_cuc
4502 && (int)wp->w_virtcol >= vcol - eol_hl_off
4503 && (int)wp->w_virtcol < W_WIDTH(wp) * (row - startrow + 1)
4505 && lnum != wp->w_cursor.lnum
4506 # ifdef FEAT_RIGHTLEFT
4507 && !wp->w_p_rl
4508 # endif
4511 while (col < W_WIDTH(wp))
4513 ScreenLines[off] = ' ';
4514 #ifdef FEAT_MBYTE
4515 if (enc_utf8)
4516 ScreenLinesUC[off] = 0;
4517 #endif
4518 ++col;
4519 if (vcol == (long)wp->w_virtcol)
4521 ScreenAttrs[off] = hl_attr(HLF_CUC);
4522 break;
4524 ScreenAttrs[off++] = 0;
4525 ++vcol;
4528 #endif
4530 SCREEN_LINE(screen_row, W_WINCOL(wp), col, (int)W_WIDTH(wp),
4531 wp->w_p_rl);
4532 row++;
4535 * Update w_cline_height and w_cline_folded if the cursor line was
4536 * updated (saves a call to plines() later).
4538 if (wp == curwin && lnum == curwin->w_cursor.lnum)
4540 curwin->w_cline_row = startrow;
4541 curwin->w_cline_height = row - startrow;
4542 #ifdef FEAT_FOLDING
4543 curwin->w_cline_folded = FALSE;
4544 #endif
4545 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
4548 break;
4551 /* line continues beyond line end */
4552 if (lcs_ext
4553 && !wp->w_p_wrap
4554 #ifdef FEAT_DIFF
4555 && filler_todo <= 0
4556 #endif
4557 && (
4558 #ifdef FEAT_RIGHTLEFT
4559 wp->w_p_rl ? col == 0 :
4560 #endif
4561 col == W_WIDTH(wp) - 1)
4562 && (*ptr != NUL
4563 || (wp->w_p_list && lcs_eol_one > 0)
4564 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
4566 c = lcs_ext;
4567 char_attr = hl_attr(HLF_AT);
4568 #ifdef FEAT_MBYTE
4569 mb_c = c;
4570 if (enc_utf8 && (*mb_char2len)(c) > 1)
4572 mb_utf8 = TRUE;
4573 u8cc[0] = 0;
4574 c = 0xc0;
4576 else
4577 mb_utf8 = FALSE;
4578 #endif
4581 #ifdef FEAT_SYN_HL
4582 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
4583 * highlight the cursor position itself. */
4584 if (wp->w_p_cuc && vcol == (long)wp->w_virtcol
4585 && lnum != wp->w_cursor.lnum
4586 && draw_state == WL_LINE
4587 && !lnum_in_visual_area)
4589 vcol_save_attr = char_attr;
4590 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
4592 else
4593 vcol_save_attr = -1;
4594 #endif
4597 * Store character to be displayed.
4598 * Skip characters that are left of the screen for 'nowrap'.
4600 vcol_prev = vcol;
4601 if (draw_state < WL_LINE || n_skip <= 0)
4604 * Store the character.
4606 #if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
4607 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
4609 /* A double-wide character is: put first halve in left cell. */
4610 --off;
4611 --col;
4613 #endif
4614 ScreenLines[off] = c;
4615 #ifdef FEAT_MBYTE
4616 if (enc_dbcs == DBCS_JPNU)
4617 ScreenLines2[off] = mb_c & 0xff;
4618 else if (enc_utf8)
4620 if (mb_utf8)
4622 int i;
4624 ScreenLinesUC[off] = mb_c;
4625 if ((c & 0xff) == 0)
4626 ScreenLines[off] = 0x80; /* avoid storing zero */
4627 for (i = 0; i < Screen_mco; ++i)
4629 ScreenLinesC[i][off] = u8cc[i];
4630 if (u8cc[i] == 0)
4631 break;
4634 else
4635 ScreenLinesUC[off] = 0;
4637 if (multi_attr)
4639 ScreenAttrs[off] = multi_attr;
4640 multi_attr = 0;
4642 else
4643 #endif
4644 ScreenAttrs[off] = char_attr;
4646 #ifdef FEAT_MBYTE
4647 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
4649 /* Need to fill two screen columns. */
4650 ++off;
4651 ++col;
4652 if (enc_utf8)
4653 /* UTF-8: Put a 0 in the second screen char. */
4654 ScreenLines[off] = 0;
4655 else
4656 /* DBCS: Put second byte in the second screen char. */
4657 ScreenLines[off] = mb_c & 0xff;
4658 ++vcol;
4659 /* When "tocol" is halfway a character, set it to the end of
4660 * the character, otherwise highlighting won't stop. */
4661 if (tocol == vcol)
4662 ++tocol;
4663 #ifdef FEAT_RIGHTLEFT
4664 if (wp->w_p_rl)
4666 /* now it's time to backup one cell */
4667 --off;
4668 --col;
4670 #endif
4672 #endif
4673 #ifdef FEAT_RIGHTLEFT
4674 if (wp->w_p_rl)
4676 --off;
4677 --col;
4679 else
4680 #endif
4682 ++off;
4683 ++col;
4686 else
4687 --n_skip;
4689 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
4690 * column. */
4691 if (draw_state >= WL_SBR
4692 #ifdef FEAT_DIFF
4693 && filler_todo <= 0
4694 #endif
4696 ++vcol;
4698 #ifdef FEAT_SYN_HL
4699 if (vcol_save_attr >= 0)
4700 char_attr = vcol_save_attr;
4701 #endif
4703 /* restore attributes after "predeces" in 'listchars' */
4704 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
4705 char_attr = saved_attr3;
4707 /* restore attributes after last 'listchars' or 'number' char */
4708 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
4709 char_attr = saved_attr2;
4712 * At end of screen line and there is more to come: Display the line
4713 * so far. If there is no more to display it is caught above.
4715 if ((
4716 #ifdef FEAT_RIGHTLEFT
4717 wp->w_p_rl ? (col < 0) :
4718 #endif
4719 (col >= W_WIDTH(wp)))
4720 && (*ptr != NUL
4721 #ifdef FEAT_DIFF
4722 || filler_todo > 0
4723 #endif
4724 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
4725 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
4728 SCREEN_LINE(screen_row, W_WINCOL(wp), col, (int)W_WIDTH(wp),
4729 wp->w_p_rl);
4730 ++row;
4731 ++screen_row;
4733 /* When not wrapping and finished diff lines, or when displayed
4734 * '$' and highlighting until last column, break here. */
4735 if ((!wp->w_p_wrap
4736 #ifdef FEAT_DIFF
4737 && filler_todo <= 0
4738 #endif
4739 ) || lcs_eol_one == -1)
4740 break;
4742 /* When the window is too narrow draw all "@" lines. */
4743 if (draw_state != WL_LINE
4744 #ifdef FEAT_DIFF
4745 && filler_todo <= 0
4746 #endif
4749 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
4750 #ifdef FEAT_VERTSPLIT
4751 draw_vsep_win(wp, row);
4752 #endif
4753 row = endrow;
4756 /* When line got too long for screen break here. */
4757 if (row == endrow)
4759 ++row;
4760 break;
4763 if (screen_cur_row == screen_row - 1
4764 #ifdef FEAT_DIFF
4765 && filler_todo <= 0
4766 #endif
4767 && W_WIDTH(wp) == Columns)
4769 /* Remember that the line wraps, used for modeless copy. */
4770 LineWraps[screen_row - 1] = TRUE;
4773 * Special trick to make copy/paste of wrapped lines work with
4774 * xterm/screen: write an extra character beyond the end of
4775 * the line. This will work with all terminal types
4776 * (regardless of the xn,am settings).
4777 * Only do this on a fast tty.
4778 * Only do this if the cursor is on the current line
4779 * (something has been written in it).
4780 * Don't do this for the GUI.
4781 * Don't do this for double-width characters.
4782 * Don't do this for a window not at the right screen border.
4784 if (p_tf
4785 #ifdef FEAT_GUI
4786 && !gui.in_use
4787 #endif
4788 #ifdef FEAT_MBYTE
4789 && !(has_mbyte
4790 && ((*mb_off2cells)(LineOffset[screen_row],
4791 LineOffset[screen_row] + screen_Columns)
4792 == 2
4793 || (*mb_off2cells)(LineOffset[screen_row - 1]
4794 + (int)Columns - 2,
4795 LineOffset[screen_row] + screen_Columns)
4796 == 2))
4797 #endif
4800 /* First make sure we are at the end of the screen line,
4801 * then output the same character again to let the
4802 * terminal know about the wrap. If the terminal doesn't
4803 * auto-wrap, we overwrite the character. */
4804 if (screen_cur_col != W_WIDTH(wp))
4805 screen_char(LineOffset[screen_row - 1]
4806 + (unsigned)Columns - 1,
4807 screen_row - 1, (int)(Columns - 1));
4809 #ifdef FEAT_MBYTE
4810 /* When there is a multi-byte character, just output a
4811 * space to keep it simple. */
4812 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
4813 screen_row - 1] + (Columns - 1)]) > 1)
4814 out_char(' ');
4815 else
4816 #endif
4817 out_char(ScreenLines[LineOffset[screen_row - 1]
4818 + (Columns - 1)]);
4819 /* force a redraw of the first char on the next line */
4820 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
4821 screen_start(); /* don't know where cursor is now */
4825 col = 0;
4826 off = (unsigned)(current_ScreenLine - ScreenLines);
4827 #ifdef FEAT_RIGHTLEFT
4828 if (wp->w_p_rl)
4830 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
4831 off += col;
4833 #endif
4835 /* reset the drawing state for the start of a wrapped line */
4836 draw_state = WL_START;
4837 saved_n_extra = n_extra;
4838 saved_p_extra = p_extra;
4839 saved_c_extra = c_extra;
4840 saved_char_attr = char_attr;
4841 n_extra = 0;
4842 lcs_prec_todo = lcs_prec;
4843 #ifdef FEAT_LINEBREAK
4844 # ifdef FEAT_DIFF
4845 if (filler_todo <= 0)
4846 # endif
4847 need_showbreak = TRUE;
4848 #endif
4849 #ifdef FEAT_DIFF
4850 --filler_todo;
4851 /* When the filler lines are actually below the last line of the
4852 * file, don't draw the line itself, break here. */
4853 if (filler_todo == 0 && wp->w_botfill)
4854 break;
4855 #endif
4858 } /* for every character in the line */
4860 #ifdef FEAT_SPELL
4861 /* After an empty line check first word for capital. */
4862 if (*skipwhite(line) == NUL)
4864 capcol_lnum = lnum + 1;
4865 cap_col = 0;
4867 #endif
4869 return row;
4872 #ifdef FEAT_MBYTE
4873 static int comp_char_differs __ARGS((int, int));
4876 * Return if the composing characters at "off_from" and "off_to" differ.
4878 static int
4879 comp_char_differs(off_from, off_to)
4880 int off_from;
4881 int off_to;
4883 int i;
4885 for (i = 0; i < Screen_mco; ++i)
4887 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
4888 return TRUE;
4889 if (ScreenLinesC[i][off_from] == 0)
4890 break;
4892 return FALSE;
4894 #endif
4897 * Check whether the given character needs redrawing:
4898 * - the (first byte of the) character is different
4899 * - the attributes are different
4900 * - the character is multi-byte and the next byte is different
4901 * - the character is two cells wide and the second cell differs.
4903 static int
4904 char_needs_redraw(off_from, off_to, cols)
4905 int off_from;
4906 int off_to;
4907 int cols;
4909 if (cols > 0
4910 && ((ScreenLines[off_from] != ScreenLines[off_to]
4911 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
4913 #ifdef FEAT_MBYTE
4914 || (enc_dbcs != 0
4915 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
4916 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
4917 ? ScreenLines2[off_from] != ScreenLines2[off_to]
4918 : (cols > 1 && ScreenLines[off_from + 1]
4919 != ScreenLines[off_to + 1])))
4920 || (enc_utf8
4921 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
4922 || (ScreenLinesUC[off_from] != 0
4923 && comp_char_differs(off_from, off_to))
4924 || (cols > 1 && ScreenLines[off_from + 1]
4925 != ScreenLines[off_to + 1])))
4926 #endif
4928 return TRUE;
4929 return FALSE;
4933 * Move one "cooked" screen line to the screen, but only the characters that
4934 * have actually changed. Handle insert/delete character.
4935 * "coloff" gives the first column on the screen for this line.
4936 * "endcol" gives the columns where valid characters are.
4937 * "clear_width" is the width of the window. It's > 0 if the rest of the line
4938 * needs to be cleared, negative otherwise.
4939 * "rlflag" is TRUE in a rightleft window:
4940 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
4941 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
4943 static void
4944 screen_line(row, coloff, endcol, clear_width
4945 #ifdef FEAT_RIGHTLEFT
4946 , rlflag
4947 #endif
4949 int row;
4950 int coloff;
4951 int endcol;
4952 int clear_width;
4953 #ifdef FEAT_RIGHTLEFT
4954 int rlflag;
4955 #endif
4957 unsigned off_from;
4958 unsigned off_to;
4959 #ifdef FEAT_MBYTE
4960 unsigned max_off_from;
4961 unsigned max_off_to;
4962 #endif
4963 int col = 0;
4964 #if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_VERTSPLIT)
4965 int hl;
4966 #endif
4967 int force = FALSE; /* force update rest of the line */
4968 int redraw_this /* bool: does character need redraw? */
4969 #ifdef FEAT_GUI
4970 = TRUE /* For GUI when while-loop empty */
4971 #endif
4973 int redraw_next; /* redraw_this for next character */
4974 #ifdef FEAT_MBYTE
4975 int clear_next = FALSE;
4976 int char_cells; /* 1: normal char */
4977 /* 2: occupies two display cells */
4978 # define CHAR_CELLS char_cells
4979 #else
4980 # define CHAR_CELLS 1
4981 #endif
4983 # ifdef FEAT_CLIPBOARD
4984 clip_may_clear_selection(row, row);
4985 # endif
4987 off_from = (unsigned)(current_ScreenLine - ScreenLines);
4988 off_to = LineOffset[row] + coloff;
4989 #ifdef FEAT_MBYTE
4990 max_off_from = off_from + screen_Columns;
4991 max_off_to = LineOffset[row] + screen_Columns;
4992 #endif
4994 #ifdef FEAT_RIGHTLEFT
4995 if (rlflag)
4997 /* Clear rest first, because it's left of the text. */
4998 if (clear_width > 0)
5000 while (col <= endcol && ScreenLines[off_to] == ' '
5001 && ScreenAttrs[off_to] == 0
5002 # ifdef FEAT_MBYTE
5003 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5004 # endif
5007 ++off_to;
5008 ++col;
5010 if (col <= endcol)
5011 screen_fill(row, row + 1, col + coloff,
5012 endcol + coloff + 1, ' ', ' ', 0);
5014 col = endcol + 1;
5015 off_to = LineOffset[row] + col + coloff;
5016 off_from += col;
5017 endcol = (clear_width > 0 ? clear_width : -clear_width);
5019 #endif /* FEAT_RIGHTLEFT */
5021 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
5023 while (col < endcol)
5025 #ifdef FEAT_MBYTE
5026 if (has_mbyte && (col + 1 < endcol))
5027 char_cells = (*mb_off2cells)(off_from, max_off_from);
5028 else
5029 char_cells = 1;
5030 #endif
5032 redraw_this = redraw_next;
5033 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
5034 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
5036 #ifdef FEAT_GUI
5037 /* If the next character was bold, then redraw the current character to
5038 * remove any pixels that might have spilt over into us. This only
5039 * happens in the GUI.
5041 if (redraw_next && gui.in_use)
5043 hl = ScreenAttrs[off_to + CHAR_CELLS];
5044 if (hl > HL_ALL)
5045 hl = syn_attr2attr(hl);
5046 if (hl & HL_BOLD)
5047 redraw_this = TRUE;
5049 #endif
5051 if (redraw_this)
5054 * Special handling when 'xs' termcap flag set (hpterm):
5055 * Attributes for characters are stored at the position where the
5056 * cursor is when writing the highlighting code. The
5057 * start-highlighting code must be written with the cursor on the
5058 * first highlighted character. The stop-highlighting code must
5059 * be written with the cursor just after the last highlighted
5060 * character.
5061 * Overwriting a character doesn't remove it's highlighting. Need
5062 * to clear the rest of the line, and force redrawing it
5063 * completely.
5065 if ( p_wiv
5066 && !force
5067 #ifdef FEAT_GUI
5068 && !gui.in_use
5069 #endif
5070 && ScreenAttrs[off_to] != 0
5071 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
5074 * Need to remove highlighting attributes here.
5076 windgoto(row, col + coloff);
5077 out_str(T_CE); /* clear rest of this screen line */
5078 screen_start(); /* don't know where cursor is now */
5079 force = TRUE; /* force redraw of rest of the line */
5080 redraw_next = TRUE; /* or else next char would miss out */
5083 * If the previous character was highlighted, need to stop
5084 * highlighting at this character.
5086 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
5088 screen_attr = ScreenAttrs[off_to - 1];
5089 term_windgoto(row, col + coloff);
5090 screen_stop_highlight();
5092 else
5093 screen_attr = 0; /* highlighting has stopped */
5095 #ifdef FEAT_MBYTE
5096 if (enc_dbcs != 0)
5098 /* Check if overwriting a double-byte with a single-byte or
5099 * the other way around requires another character to be
5100 * redrawn. For UTF-8 this isn't needed, because comparing
5101 * ScreenLinesUC[] is sufficient. */
5102 if (char_cells == 1
5103 && col + 1 < endcol
5104 && (*mb_off2cells)(off_to, max_off_to) > 1)
5106 /* Writing a single-cell character over a double-cell
5107 * character: need to redraw the next cell. */
5108 ScreenLines[off_to + 1] = 0;
5109 redraw_next = TRUE;
5111 else if (char_cells == 2
5112 && col + 2 < endcol
5113 && (*mb_off2cells)(off_to, max_off_to) == 1
5114 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
5116 /* Writing the second half of a double-cell character over
5117 * a double-cell character: need to redraw the second
5118 * cell. */
5119 ScreenLines[off_to + 2] = 0;
5120 redraw_next = TRUE;
5123 if (enc_dbcs == DBCS_JPNU)
5124 ScreenLines2[off_to] = ScreenLines2[off_from];
5126 /* When writing a single-width character over a double-width
5127 * character and at the end of the redrawn text, need to clear out
5128 * the right halve of the old character.
5129 * Also required when writing the right halve of a double-width
5130 * char over the left halve of an existing one. */
5131 if (has_mbyte && col + char_cells == endcol
5132 && ((char_cells == 1
5133 && (*mb_off2cells)(off_to, max_off_to) > 1)
5134 || (char_cells == 2
5135 && (*mb_off2cells)(off_to, max_off_to) == 1
5136 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
5137 clear_next = TRUE;
5138 #endif
5140 ScreenLines[off_to] = ScreenLines[off_from];
5141 #ifdef FEAT_MBYTE
5142 if (enc_utf8)
5144 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
5145 if (ScreenLinesUC[off_from] != 0)
5147 int i;
5149 for (i = 0; i < Screen_mco; ++i)
5150 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
5153 if (char_cells == 2)
5154 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
5155 #endif
5157 #if defined(FEAT_GUI) || defined(UNIX)
5158 /* The bold trick makes a single row of pixels appear in the next
5159 * character. When a bold character is removed, the next
5160 * character should be redrawn too. This happens for our own GUI
5161 * and for some xterms. */
5162 if (
5163 # ifdef FEAT_GUI
5164 gui.in_use
5165 # endif
5166 # if defined(FEAT_GUI) && defined(UNIX)
5168 # endif
5169 # ifdef UNIX
5170 term_is_xterm
5171 # endif
5174 hl = ScreenAttrs[off_to];
5175 if (hl > HL_ALL)
5176 hl = syn_attr2attr(hl);
5177 if (hl & HL_BOLD)
5178 redraw_next = TRUE;
5180 #endif
5181 ScreenAttrs[off_to] = ScreenAttrs[off_from];
5182 #ifdef FEAT_MBYTE
5183 /* For simplicity set the attributes of second half of a
5184 * double-wide character equal to the first half. */
5185 if (char_cells == 2)
5186 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
5188 if (enc_dbcs != 0 && char_cells == 2)
5189 screen_char_2(off_to, row, col + coloff);
5190 else
5191 #endif
5192 screen_char(off_to, row, col + coloff);
5194 else if ( p_wiv
5195 #ifdef FEAT_GUI
5196 && !gui.in_use
5197 #endif
5198 && col + coloff > 0)
5200 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
5203 * Don't output stop-highlight when moving the cursor, it will
5204 * stop the highlighting when it should continue.
5206 screen_attr = 0;
5208 else if (screen_attr != 0)
5209 screen_stop_highlight();
5212 off_to += CHAR_CELLS;
5213 off_from += CHAR_CELLS;
5214 col += CHAR_CELLS;
5217 #ifdef FEAT_MBYTE
5218 if (clear_next)
5220 /* Clear the second half of a double-wide character of which the left
5221 * half was overwritten with a single-wide character. */
5222 ScreenLines[off_to] = ' ';
5223 if (enc_utf8)
5224 ScreenLinesUC[off_to] = 0;
5225 screen_char(off_to, row, col + coloff);
5227 #endif
5229 if (clear_width > 0
5230 #ifdef FEAT_RIGHTLEFT
5231 && !rlflag
5232 #endif
5235 #ifdef FEAT_GUI
5236 int startCol = col;
5237 #endif
5239 /* blank out the rest of the line */
5240 while (col < clear_width && ScreenLines[off_to] == ' '
5241 && ScreenAttrs[off_to] == 0
5242 #ifdef FEAT_MBYTE
5243 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5244 #endif
5247 ++off_to;
5248 ++col;
5250 if (col < clear_width)
5252 #ifdef FEAT_GUI
5254 * In the GUI, clearing the rest of the line may leave pixels
5255 * behind if the first character cleared was bold. Some bold
5256 * fonts spill over the left. In this case we redraw the previous
5257 * character too. If we didn't skip any blanks above, then we
5258 * only redraw if the character wasn't already redrawn anyway.
5260 if (gui.in_use && (col > startCol || !redraw_this))
5262 hl = ScreenAttrs[off_to];
5263 if (hl > HL_ALL || (hl & HL_BOLD))
5265 int prev_cells = 1;
5266 # ifdef FEAT_MBYTE
5267 if (enc_utf8)
5268 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
5269 * that its width is 2. */
5270 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
5271 else if (enc_dbcs != 0)
5273 /* find previous character by counting from first
5274 * column and get its width. */
5275 unsigned off = LineOffset[row];
5276 unsigned max_off = LineOffset[row] + screen_Columns;
5278 while (off < off_to)
5280 prev_cells = (*mb_off2cells)(off, max_off);
5281 off += prev_cells;
5285 if (enc_dbcs != 0 && prev_cells > 1)
5286 screen_char_2(off_to - prev_cells, row,
5287 col + coloff - prev_cells);
5288 else
5289 # endif
5290 screen_char(off_to - prev_cells, row,
5291 col + coloff - prev_cells);
5294 #endif
5295 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
5296 ' ', ' ', 0);
5297 #ifdef FEAT_VERTSPLIT
5298 off_to += clear_width - col;
5299 col = clear_width;
5300 #endif
5304 if (clear_width > 0)
5306 #ifdef FEAT_VERTSPLIT
5307 /* For a window that's left of another, draw the separator char. */
5308 if (col + coloff < Columns)
5310 int c;
5312 c = fillchar_vsep(&hl);
5313 if (ScreenLines[off_to] != c
5314 # ifdef FEAT_MBYTE
5315 || (enc_utf8 && (int)ScreenLinesUC[off_to]
5316 != (c >= 0x80 ? c : 0))
5317 # endif
5318 || ScreenAttrs[off_to] != hl)
5320 ScreenLines[off_to] = c;
5321 ScreenAttrs[off_to] = hl;
5322 # ifdef FEAT_MBYTE
5323 if (enc_utf8)
5325 if (c >= 0x80)
5327 ScreenLinesUC[off_to] = c;
5328 ScreenLinesC[0][off_to] = 0;
5330 else
5331 ScreenLinesUC[off_to] = 0;
5333 # endif
5334 screen_char(off_to, row, col + coloff);
5337 else
5338 #endif
5339 LineWraps[row] = FALSE;
5343 #if defined(FEAT_RIGHTLEFT) || defined(PROTO)
5345 * Mirror text "str" for right-left displaying.
5346 * Only works for single-byte characters (e.g., numbers).
5348 void
5349 rl_mirror(str)
5350 char_u *str;
5352 char_u *p1, *p2;
5353 int t;
5355 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
5357 t = *p1;
5358 *p1 = *p2;
5359 *p2 = t;
5362 #endif
5364 #if defined(FEAT_WINDOWS) || defined(PROTO)
5366 * mark all status lines for redraw; used after first :cd
5368 void
5369 status_redraw_all()
5371 win_T *wp;
5373 for (wp = firstwin; wp; wp = wp->w_next)
5374 if (wp->w_status_height)
5376 wp->w_redr_status = TRUE;
5377 redraw_later(VALID);
5382 * mark all status lines of the current buffer for redraw
5384 void
5385 status_redraw_curbuf()
5387 win_T *wp;
5389 for (wp = firstwin; wp; wp = wp->w_next)
5390 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
5392 wp->w_redr_status = TRUE;
5393 redraw_later(VALID);
5398 * Redraw all status lines that need to be redrawn.
5400 void
5401 redraw_statuslines()
5403 win_T *wp;
5405 for (wp = firstwin; wp; wp = wp->w_next)
5406 if (wp->w_redr_status)
5407 win_redr_status(wp);
5408 if (redraw_tabline)
5409 draw_tabline();
5411 #endif
5413 #if (defined(FEAT_WILDMENU) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
5415 * Redraw all status lines at the bottom of frame "frp".
5417 void
5418 win_redraw_last_status(frp)
5419 frame_T *frp;
5421 if (frp->fr_layout == FR_LEAF)
5422 frp->fr_win->w_redr_status = TRUE;
5423 else if (frp->fr_layout == FR_ROW)
5425 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
5426 win_redraw_last_status(frp);
5428 else /* frp->fr_layout == FR_COL */
5430 frp = frp->fr_child;
5431 while (frp->fr_next != NULL)
5432 frp = frp->fr_next;
5433 win_redraw_last_status(frp);
5436 #endif
5438 #ifdef FEAT_VERTSPLIT
5440 * Draw the verticap separator right of window "wp" starting with line "row".
5442 static void
5443 draw_vsep_win(wp, row)
5444 win_T *wp;
5445 int row;
5447 int hl;
5448 int c;
5450 if (wp->w_vsep_width)
5452 /* draw the vertical separator right of this window */
5453 c = fillchar_vsep(&hl);
5454 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
5455 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
5456 c, ' ', hl);
5459 #endif
5461 #ifdef FEAT_WILDMENU
5462 static int status_match_len __ARGS((expand_T *xp, char_u *s));
5463 static int skip_status_match_char __ARGS((expand_T *xp, char_u *s));
5466 * Get the length of an item as it will be shown in the status line.
5468 static int
5469 status_match_len(xp, s)
5470 expand_T *xp;
5471 char_u *s;
5473 int len = 0;
5475 #ifdef FEAT_MENU
5476 int emenu = (xp->xp_context == EXPAND_MENUS
5477 || xp->xp_context == EXPAND_MENUNAMES);
5479 /* Check for menu separators - replace with '|'. */
5480 if (emenu && menu_is_separator(s))
5481 return 1;
5482 #endif
5484 while (*s != NUL)
5486 s += skip_status_match_char(xp, s);
5487 len += ptr2cells(s);
5488 mb_ptr_adv(s);
5491 return len;
5495 * Return the number of characters that should be skipped in a status match.
5496 * These are backslashes used for escaping. Do show backslashes in help tags.
5498 static int
5499 skip_status_match_char(xp, s)
5500 expand_T *xp;
5501 char_u *s;
5503 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
5504 #ifdef FEAT_MENU
5505 || ((xp->xp_context == EXPAND_MENUS
5506 || xp->xp_context == EXPAND_MENUNAMES)
5507 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
5508 #endif
5511 #ifndef BACKSLASH_IN_FILENAME
5512 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
5513 return 2;
5514 #endif
5515 return 1;
5517 return 0;
5521 * Show wildchar matches in the status line.
5522 * Show at least the "match" item.
5523 * We start at item 'first_match' in the list and show all matches that fit.
5525 * If inversion is possible we use it. Else '=' characters are used.
5527 void
5528 win_redr_status_matches(xp, num_matches, matches, match, showtail)
5529 expand_T *xp;
5530 int num_matches;
5531 char_u **matches; /* list of matches */
5532 int match;
5533 int showtail;
5535 #define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
5536 int row;
5537 char_u *buf;
5538 int len;
5539 int clen; /* length in screen cells */
5540 int fillchar;
5541 int attr;
5542 int i;
5543 int highlight = TRUE;
5544 char_u *selstart = NULL;
5545 int selstart_col = 0;
5546 char_u *selend = NULL;
5547 static int first_match = 0;
5548 int add_left = FALSE;
5549 char_u *s;
5550 #ifdef FEAT_MENU
5551 int emenu;
5552 #endif
5553 #if defined(FEAT_MBYTE) || defined(FEAT_MENU)
5554 int l;
5555 #endif
5557 if (matches == NULL) /* interrupted completion? */
5558 return;
5560 #ifdef FEAT_MBYTE
5561 if (has_mbyte)
5562 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
5563 else
5564 #endif
5565 buf = alloc((unsigned)Columns + 1);
5566 if (buf == NULL)
5567 return;
5569 if (match == -1) /* don't show match but original text */
5571 match = 0;
5572 highlight = FALSE;
5574 /* count 1 for the ending ">" */
5575 clen = status_match_len(xp, L_MATCH(match)) + 3;
5576 if (match == 0)
5577 first_match = 0;
5578 else if (match < first_match)
5580 /* jumping left, as far as we can go */
5581 first_match = match;
5582 add_left = TRUE;
5584 else
5586 /* check if match fits on the screen */
5587 for (i = first_match; i < match; ++i)
5588 clen += status_match_len(xp, L_MATCH(i)) + 2;
5589 if (first_match > 0)
5590 clen += 2;
5591 /* jumping right, put match at the left */
5592 if ((long)clen > Columns)
5594 first_match = match;
5595 /* if showing the last match, we can add some on the left */
5596 clen = 2;
5597 for (i = match; i < num_matches; ++i)
5599 clen += status_match_len(xp, L_MATCH(i)) + 2;
5600 if ((long)clen >= Columns)
5601 break;
5603 if (i == num_matches)
5604 add_left = TRUE;
5607 if (add_left)
5608 while (first_match > 0)
5610 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
5611 if ((long)clen >= Columns)
5612 break;
5613 --first_match;
5616 fillchar = fillchar_status(&attr, TRUE);
5618 if (first_match == 0)
5620 *buf = NUL;
5621 len = 0;
5623 else
5625 STRCPY(buf, "< ");
5626 len = 2;
5628 clen = len;
5630 i = first_match;
5631 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
5633 if (i == match)
5635 selstart = buf + len;
5636 selstart_col = clen;
5639 s = L_MATCH(i);
5640 /* Check for menu separators - replace with '|' */
5641 #ifdef FEAT_MENU
5642 emenu = (xp->xp_context == EXPAND_MENUS
5643 || xp->xp_context == EXPAND_MENUNAMES);
5644 if (emenu && menu_is_separator(s))
5646 STRCPY(buf + len, transchar('|'));
5647 l = (int)STRLEN(buf + len);
5648 len += l;
5649 clen += l;
5651 else
5652 #endif
5653 for ( ; *s != NUL; ++s)
5655 s += skip_status_match_char(xp, s);
5656 clen += ptr2cells(s);
5657 #ifdef FEAT_MBYTE
5658 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
5660 STRNCPY(buf + len, s, l);
5661 s += l - 1;
5662 len += l;
5664 else
5665 #endif
5667 STRCPY(buf + len, transchar_byte(*s));
5668 len += (int)STRLEN(buf + len);
5671 if (i == match)
5672 selend = buf + len;
5674 *(buf + len++) = ' ';
5675 *(buf + len++) = ' ';
5676 clen += 2;
5677 if (++i == num_matches)
5678 break;
5681 if (i != num_matches)
5683 *(buf + len++) = '>';
5684 ++clen;
5687 buf[len] = NUL;
5689 row = cmdline_row - 1;
5690 if (row >= 0)
5692 if (wild_menu_showing == 0)
5694 if (msg_scrolled > 0)
5696 /* Put the wildmenu just above the command line. If there is
5697 * no room, scroll the screen one line up. */
5698 if (cmdline_row == Rows - 1)
5700 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
5701 ++msg_scrolled;
5703 else
5705 ++cmdline_row;
5706 ++row;
5708 wild_menu_showing = WM_SCROLLED;
5710 else
5712 /* Create status line if needed by setting 'laststatus' to 2.
5713 * Set 'winminheight' to zero to avoid that the window is
5714 * resized. */
5715 if (lastwin->w_status_height == 0)
5717 save_p_ls = p_ls;
5718 save_p_wmh = p_wmh;
5719 p_ls = 2;
5720 p_wmh = 0;
5721 last_status(FALSE);
5723 wild_menu_showing = WM_SHOWN;
5727 screen_puts(buf, row, 0, attr);
5728 if (selstart != NULL && highlight)
5730 *selend = NUL;
5731 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
5734 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
5737 #ifdef FEAT_VERTSPLIT
5738 win_redraw_last_status(topframe);
5739 #else
5740 lastwin->w_redr_status = TRUE;
5741 #endif
5742 vim_free(buf);
5744 #endif
5746 #if defined(FEAT_WINDOWS) || defined(PROTO)
5748 * Redraw the status line of window wp.
5750 * If inversion is possible we use it. Else '=' characters are used.
5752 void
5753 win_redr_status(wp)
5754 win_T *wp;
5756 int row;
5757 char_u *p;
5758 int len;
5759 int fillchar;
5760 int attr;
5761 int this_ru_col;
5763 wp->w_redr_status = FALSE;
5764 if (wp->w_status_height == 0)
5766 /* no status line, can only be last window */
5767 redraw_cmdline = TRUE;
5769 else if (!redrawing()
5770 #ifdef FEAT_INS_EXPAND
5771 /* don't update status line when popup menu is visible and may be
5772 * drawn over it */
5773 || pum_visible()
5774 #endif
5777 /* Don't redraw right now, do it later. */
5778 wp->w_redr_status = TRUE;
5780 #ifdef FEAT_STL_OPT
5781 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
5783 /* redraw custom status line */
5784 redraw_custum_statusline(wp);
5786 #endif
5787 else
5789 fillchar = fillchar_status(&attr, wp == curwin);
5791 get_trans_bufname(wp->w_buffer);
5792 p = NameBuff;
5793 len = (int)STRLEN(p);
5795 if (wp->w_buffer->b_help
5796 #ifdef FEAT_QUICKFIX
5797 || wp->w_p_pvw
5798 #endif
5799 || bufIsChanged(wp->w_buffer)
5800 || wp->w_buffer->b_p_ro)
5801 *(p + len++) = ' ';
5802 if (wp->w_buffer->b_help)
5804 STRCPY(p + len, _("[Help]"));
5805 len += (int)STRLEN(p + len);
5807 #ifdef FEAT_QUICKFIX
5808 if (wp->w_p_pvw)
5810 STRCPY(p + len, _("[Preview]"));
5811 len += (int)STRLEN(p + len);
5813 #endif
5814 if (bufIsChanged(wp->w_buffer))
5816 STRCPY(p + len, "[+]");
5817 len += 3;
5819 if (wp->w_buffer->b_p_ro)
5821 STRCPY(p + len, "[RO]");
5822 len += 4;
5825 #ifndef FEAT_VERTSPLIT
5826 this_ru_col = ru_col;
5827 if (this_ru_col < (Columns + 1) / 2)
5828 this_ru_col = (Columns + 1) / 2;
5829 #else
5830 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
5831 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
5832 this_ru_col = (W_WIDTH(wp) + 1) / 2;
5833 if (this_ru_col <= 1)
5835 p = (char_u *)"<"; /* No room for file name! */
5836 len = 1;
5838 else
5839 #endif
5840 #ifdef FEAT_MBYTE
5841 if (has_mbyte)
5843 int clen = 0, i;
5845 /* Count total number of display cells. */
5846 for (i = 0; p[i] != NUL; i += (*mb_ptr2len)(p + i))
5847 clen += (*mb_ptr2cells)(p + i);
5848 /* Find first character that will fit.
5849 * Going from start to end is much faster for DBCS. */
5850 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
5851 i += (*mb_ptr2len)(p + i))
5852 clen -= (*mb_ptr2cells)(p + i);
5853 len = clen;
5854 if (i > 0)
5856 p = p + i - 1;
5857 *p = '<';
5858 ++len;
5862 else
5863 #endif
5864 if (len > this_ru_col - 1)
5866 p += len - (this_ru_col - 1);
5867 *p = '<';
5868 len = this_ru_col - 1;
5871 row = W_WINROW(wp) + wp->w_height;
5872 screen_puts(p, row, W_WINCOL(wp), attr);
5873 screen_fill(row, row + 1, len + W_WINCOL(wp),
5874 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
5876 if (get_keymap_str(wp, NameBuff, MAXPATHL)
5877 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
5878 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
5879 - 1 + W_WINCOL(wp)), attr);
5881 #ifdef FEAT_CMDL_INFO
5882 win_redr_ruler(wp, TRUE);
5883 #endif
5886 #ifdef FEAT_VERTSPLIT
5888 * May need to draw the character below the vertical separator.
5890 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
5892 if (stl_connected(wp))
5893 fillchar = fillchar_status(&attr, wp == curwin);
5894 else
5895 fillchar = fillchar_vsep(&attr);
5896 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
5897 attr);
5899 #endif
5902 #ifdef FEAT_STL_OPT
5904 * Redraw the status line according to 'statusline' and take care of any
5905 * errors encountered.
5907 static void
5908 redraw_custum_statusline(wp)
5909 win_T *wp;
5911 int save_called_emsg = called_emsg;
5913 called_emsg = FALSE;
5914 win_redr_custom(wp, FALSE);
5915 if (called_emsg)
5916 set_string_option_direct((char_u *)"statusline", -1,
5917 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
5918 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
5919 called_emsg |= save_called_emsg;
5921 #endif
5923 # ifdef FEAT_VERTSPLIT
5925 * Return TRUE if the status line of window "wp" is connected to the status
5926 * line of the window right of it. If not, then it's a vertical separator.
5927 * Only call if (wp->w_vsep_width != 0).
5930 stl_connected(wp)
5931 win_T *wp;
5933 frame_T *fr;
5935 fr = wp->w_frame;
5936 while (fr->fr_parent != NULL)
5938 if (fr->fr_parent->fr_layout == FR_COL)
5940 if (fr->fr_next != NULL)
5941 break;
5943 else
5945 if (fr->fr_next != NULL)
5946 return TRUE;
5948 fr = fr->fr_parent;
5950 return FALSE;
5952 # endif
5954 #endif /* FEAT_WINDOWS */
5956 #if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
5958 * Get the value to show for the language mappings, active 'keymap'.
5961 get_keymap_str(wp, buf, len)
5962 win_T *wp;
5963 char_u *buf; /* buffer for the result */
5964 int len; /* length of buffer */
5966 char_u *p;
5968 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
5969 return FALSE;
5972 #ifdef FEAT_EVAL
5973 buf_T *old_curbuf = curbuf;
5974 win_T *old_curwin = curwin;
5975 char_u *s;
5977 curbuf = wp->w_buffer;
5978 curwin = wp;
5979 STRCPY(buf, "b:keymap_name"); /* must be writable */
5980 ++emsg_skip;
5981 s = p = eval_to_string(buf, NULL, FALSE);
5982 --emsg_skip;
5983 curbuf = old_curbuf;
5984 curwin = old_curwin;
5985 if (p == NULL || *p == NUL)
5986 #endif
5988 #ifdef FEAT_KEYMAP
5989 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
5990 p = wp->w_buffer->b_p_keymap;
5991 else
5992 #endif
5993 p = (char_u *)"lang";
5995 if ((int)(STRLEN(p) + 3) < len)
5996 sprintf((char *)buf, "<%s>", p);
5997 else
5998 buf[0] = NUL;
5999 #ifdef FEAT_EVAL
6000 vim_free(s);
6001 #endif
6003 return buf[0] != NUL;
6005 #endif
6007 #if defined(FEAT_STL_OPT) || defined(PROTO)
6009 * Redraw the status line or ruler of window "wp".
6010 * When "wp" is NULL redraw the tab pages line from 'tabline'.
6012 static void
6013 win_redr_custom(wp, draw_ruler)
6014 win_T *wp;
6015 int draw_ruler; /* TRUE or FALSE */
6017 int attr;
6018 int curattr;
6019 int row;
6020 int col = 0;
6021 int maxwidth;
6022 int width;
6023 int n;
6024 int len;
6025 int fillchar;
6026 char_u buf[MAXPATHL];
6027 char_u *p;
6028 struct stl_hlrec hltab[STL_MAX_ITEM];
6029 struct stl_hlrec tabtab[STL_MAX_ITEM];
6030 int use_sandbox = FALSE;
6032 /* setup environment for the task at hand */
6033 if (wp == NULL)
6035 /* Use 'tabline'. Always at the first line of the screen. */
6036 p = p_tal;
6037 row = 0;
6038 fillchar = ' ';
6039 attr = hl_attr(HLF_TPF);
6040 maxwidth = Columns;
6041 # ifdef FEAT_EVAL
6042 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
6043 # endif
6045 else
6047 row = W_WINROW(wp) + wp->w_height;
6048 fillchar = fillchar_status(&attr, wp == curwin);
6049 maxwidth = W_WIDTH(wp);
6051 if (draw_ruler)
6053 p = p_ruf;
6054 /* advance past any leading group spec - implicit in ru_col */
6055 if (*p == '%')
6057 if (*++p == '-')
6058 p++;
6059 if (atoi((char *) p))
6060 while (VIM_ISDIGIT(*p))
6061 p++;
6062 if (*p++ != '(')
6063 p = p_ruf;
6065 #ifdef FEAT_VERTSPLIT
6066 col = ru_col - (Columns - W_WIDTH(wp));
6067 if (col < (W_WIDTH(wp) + 1) / 2)
6068 col = (W_WIDTH(wp) + 1) / 2;
6069 #else
6070 col = ru_col;
6071 if (col > (Columns + 1) / 2)
6072 col = (Columns + 1) / 2;
6073 #endif
6074 maxwidth = W_WIDTH(wp) - col;
6075 #ifdef FEAT_WINDOWS
6076 if (!wp->w_status_height)
6077 #endif
6079 row = Rows - 1;
6080 --maxwidth; /* writing in last column may cause scrolling */
6081 fillchar = ' ';
6082 attr = 0;
6085 # ifdef FEAT_EVAL
6086 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
6087 # endif
6089 else
6091 if (*wp->w_p_stl != NUL)
6092 p = wp->w_p_stl;
6093 else
6094 p = p_stl;
6095 # ifdef FEAT_EVAL
6096 use_sandbox = was_set_insecurely((char_u *)"statusline",
6097 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
6098 # endif
6101 #ifdef FEAT_VERTSPLIT
6102 col += W_WINCOL(wp);
6103 #endif
6106 if (maxwidth <= 0)
6107 return;
6109 width = build_stl_str_hl(wp == NULL ? curwin : wp,
6110 buf, sizeof(buf),
6111 p, use_sandbox,
6112 fillchar, maxwidth, hltab, tabtab);
6113 len = (int)STRLEN(buf);
6115 while (width < maxwidth && len < sizeof(buf) - 1)
6117 #ifdef FEAT_MBYTE
6118 len += (*mb_char2bytes)(fillchar, buf + len);
6119 #else
6120 buf[len++] = fillchar;
6121 #endif
6122 ++width;
6124 buf[len] = NUL;
6127 * Draw each snippet with the specified highlighting.
6129 curattr = attr;
6130 p = buf;
6131 for (n = 0; hltab[n].start != NULL; n++)
6133 len = (int)(hltab[n].start - p);
6134 screen_puts_len(p, len, row, col, curattr);
6135 col += vim_strnsize(p, len);
6136 p = hltab[n].start;
6138 if (hltab[n].userhl == 0)
6139 curattr = attr;
6140 else if (hltab[n].userhl < 0)
6141 curattr = syn_id2attr(-hltab[n].userhl);
6142 #ifdef FEAT_WINDOWS
6143 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
6144 curattr = highlight_stlnc[hltab[n].userhl - 1];
6145 #endif
6146 else
6147 curattr = highlight_user[hltab[n].userhl - 1];
6149 screen_puts(p, row, col, curattr);
6151 if (wp == NULL)
6153 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
6154 col = 0;
6155 len = 0;
6156 p = buf;
6157 fillchar = 0;
6158 for (n = 0; tabtab[n].start != NULL; n++)
6160 len += vim_strnsize(p, (int)(tabtab[n].start - p));
6161 while (col < len)
6162 TabPageIdxs[col++] = fillchar;
6163 p = tabtab[n].start;
6164 fillchar = tabtab[n].userhl;
6166 while (col < Columns)
6167 TabPageIdxs[col++] = fillchar;
6171 #endif /* FEAT_STL_OPT */
6174 * Output a single character directly to the screen and update ScreenLines.
6176 void
6177 screen_putchar(c, row, col, attr)
6178 int c;
6179 int row, col;
6180 int attr;
6182 #ifdef FEAT_MBYTE
6183 char_u buf[MB_MAXBYTES + 1];
6185 buf[(*mb_char2bytes)(c, buf)] = NUL;
6186 #else
6187 char_u buf[2];
6189 buf[0] = c;
6190 buf[1] = NUL;
6191 #endif
6192 screen_puts(buf, row, col, attr);
6196 * Get a single character directly from ScreenLines into "bytes[]".
6197 * Also return its attribute in *attrp;
6199 void
6200 screen_getbytes(row, col, bytes, attrp)
6201 int row, col;
6202 char_u *bytes;
6203 int *attrp;
6205 unsigned off;
6207 /* safety check */
6208 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
6210 off = LineOffset[row] + col;
6211 *attrp = ScreenAttrs[off];
6212 bytes[0] = ScreenLines[off];
6213 bytes[1] = NUL;
6215 #ifdef FEAT_MBYTE
6216 if (enc_utf8 && ScreenLinesUC[off] != 0)
6217 bytes[utfc_char2bytes(off, bytes)] = NUL;
6218 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6220 bytes[0] = ScreenLines[off];
6221 bytes[1] = ScreenLines2[off];
6222 bytes[2] = NUL;
6224 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
6226 bytes[1] = ScreenLines[off + 1];
6227 bytes[2] = NUL;
6229 #endif
6233 #ifdef FEAT_MBYTE
6234 static int screen_comp_differs __ARGS((int, int*));
6237 * Return TRUE if composing characters for screen posn "off" differs from
6238 * composing characters in "u8cc".
6240 static int
6241 screen_comp_differs(off, u8cc)
6242 int off;
6243 int *u8cc;
6245 int i;
6247 for (i = 0; i < Screen_mco; ++i)
6249 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
6250 return TRUE;
6251 if (u8cc[i] == 0)
6252 break;
6254 return FALSE;
6256 #endif
6259 * Put string '*text' on the screen at position 'row' and 'col', with
6260 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
6261 * Note: only outputs within one row, message is truncated at screen boundary!
6262 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
6264 void
6265 screen_puts(text, row, col, attr)
6266 char_u *text;
6267 int row;
6268 int col;
6269 int attr;
6271 screen_puts_len(text, -1, row, col, attr);
6275 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
6276 * a NUL.
6278 void
6279 screen_puts_len(text, len, row, col, attr)
6280 char_u *text;
6281 int len;
6282 int row;
6283 int col;
6284 int attr;
6286 unsigned off;
6287 char_u *ptr = text;
6288 int c;
6289 #ifdef FEAT_MBYTE
6290 unsigned max_off;
6291 int mbyte_blen = 1;
6292 int mbyte_cells = 1;
6293 int u8c = 0;
6294 int u8cc[MAX_MCO];
6295 int clear_next_cell = FALSE;
6296 # ifdef FEAT_ARABIC
6297 int prev_c = 0; /* previous Arabic character */
6298 int pc, nc, nc1;
6299 int pcc[MAX_MCO];
6300 # endif
6301 #endif
6303 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
6304 return;
6306 #ifdef FEAT_MBYTE
6307 /* When drawing over the right halve of a double-wide char clear out the
6308 * left halve. Only needed in a terminal. */
6309 if (has_mbyte && col > 0 && col < screen_Columns
6310 # ifdef FEAT_GUI
6311 && !gui.in_use
6312 # endif
6313 && mb_fix_col(col, row) != col)
6314 screen_puts_len((char_u *)" ", 1, row, col - 1, 0);
6315 #endif
6317 off = LineOffset[row] + col;
6318 #ifdef FEAT_MBYTE
6319 max_off = LineOffset[row] + screen_Columns;
6320 #endif
6321 while (col < screen_Columns
6322 && (len < 0 || (int)(ptr - text) < len)
6323 && *ptr != NUL)
6325 c = *ptr;
6326 #ifdef FEAT_MBYTE
6327 /* check if this is the first byte of a multibyte */
6328 if (has_mbyte)
6330 if (enc_utf8 && len > 0)
6331 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
6332 else
6333 mbyte_blen = (*mb_ptr2len)(ptr);
6334 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6335 mbyte_cells = 1;
6336 else if (enc_dbcs != 0)
6337 mbyte_cells = mbyte_blen;
6338 else /* enc_utf8 */
6340 if (len >= 0)
6341 u8c = utfc_ptr2char_len(ptr, u8cc,
6342 (int)((text + len) - ptr));
6343 else
6344 u8c = utfc_ptr2char(ptr, u8cc);
6345 mbyte_cells = utf_char2cells(u8c);
6346 # ifdef UNICODE16
6347 /* Non-BMP character: display as ? or fullwidth ?. */
6348 if (u8c >= 0x10000)
6350 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
6351 if (attr == 0)
6352 attr = hl_attr(HLF_8);
6354 # endif
6355 # ifdef FEAT_ARABIC
6356 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
6358 /* Do Arabic shaping. */
6359 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
6361 /* Past end of string to be displayed. */
6362 nc = NUL;
6363 nc1 = NUL;
6365 else
6367 nc = utfc_ptr2char(ptr + mbyte_blen, pcc);
6368 nc1 = pcc[0];
6370 pc = prev_c;
6371 prev_c = u8c;
6372 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
6374 else
6375 prev_c = u8c;
6376 # endif
6379 #endif
6381 if (ScreenLines[off] != c
6382 #ifdef FEAT_MBYTE
6383 || (mbyte_cells == 2
6384 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
6385 || (enc_dbcs == DBCS_JPNU
6386 && c == 0x8e
6387 && ScreenLines2[off] != ptr[1])
6388 || (enc_utf8
6389 && (ScreenLinesUC[off] != (u8char_T)(c >= 0x80 ? u8c : 0)
6390 || screen_comp_differs(off, u8cc)))
6391 #endif
6392 || ScreenAttrs[off] != attr
6393 || exmode_active
6396 #if defined(FEAT_GUI) || defined(UNIX)
6397 /* The bold trick makes a single row of pixels appear in the next
6398 * character. When a bold character is removed, the next
6399 * character should be redrawn too. This happens for our own GUI
6400 * and for some xterms.
6401 * Force the redraw by setting the attribute to a different value
6402 * than "attr", the contents of ScreenLines[] may be needed by
6403 * mb_off2cells() further on.
6404 * Don't do this for the last drawn character, because the next
6405 * character may not be redrawn. */
6406 if (
6407 # ifdef FEAT_GUI
6408 gui.in_use
6409 # endif
6410 # if defined(FEAT_GUI) && defined(UNIX)
6412 # endif
6413 # ifdef UNIX
6414 term_is_xterm
6415 # endif
6418 int n;
6420 n = ScreenAttrs[off];
6421 # ifdef FEAT_MBYTE
6422 if (col + mbyte_cells < screen_Columns
6423 && (n > HL_ALL || (n & HL_BOLD))
6424 && (len < 0 ? ptr[mbyte_blen] != NUL
6425 : ptr + mbyte_blen < text + len))
6426 ScreenAttrs[off + mbyte_cells] = attr + 1;
6427 # else
6428 if (col + 1 < screen_Columns
6429 && (n > HL_ALL || (n & HL_BOLD))
6430 && (len < 0 ? ptr[1] != NUL : ptr + 1 < text + len))
6431 ScreenLines[off + 1] = 0;
6432 # endif
6434 #endif
6435 #ifdef FEAT_MBYTE
6436 /* When at the end of the text and overwriting a two-cell
6437 * character with a one-cell character, need to clear the next
6438 * cell. Also when overwriting the left halve of a two-cell char
6439 * with the right halve of a two-cell char. Do this only once
6440 * (mb_off2cells() may return 2 on the right halve). */
6441 if (clear_next_cell)
6442 clear_next_cell = FALSE;
6443 else if (has_mbyte
6444 && (len < 0 ? ptr[mbyte_blen] == NUL
6445 : ptr + mbyte_blen >= text + len)
6446 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
6447 || (mbyte_cells == 2
6448 && (*mb_off2cells)(off, max_off) == 1
6449 && (*mb_off2cells)(off + 1, max_off) > 1)))
6450 clear_next_cell = TRUE;
6452 /* Make sure we never leave a second byte of a double-byte behind,
6453 * it confuses mb_off2cells(). */
6454 if (enc_dbcs
6455 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
6456 || (mbyte_cells == 2
6457 && (*mb_off2cells)(off, max_off) == 1
6458 && (*mb_off2cells)(off + 1, max_off) > 1)))
6459 ScreenLines[off + mbyte_blen] = 0;
6460 #endif
6461 ScreenLines[off] = c;
6462 ScreenAttrs[off] = attr;
6463 #ifdef FEAT_MBYTE
6464 if (enc_utf8)
6466 if (c < 0x80 && u8cc[0] == 0)
6467 ScreenLinesUC[off] = 0;
6468 else
6470 int i;
6472 ScreenLinesUC[off] = u8c;
6473 for (i = 0; i < Screen_mco; ++i)
6475 ScreenLinesC[i][off] = u8cc[i];
6476 if (u8cc[i] == 0)
6477 break;
6480 if (mbyte_cells == 2)
6482 ScreenLines[off + 1] = 0;
6483 ScreenAttrs[off + 1] = attr;
6485 screen_char(off, row, col);
6487 else if (mbyte_cells == 2)
6489 ScreenLines[off + 1] = ptr[1];
6490 ScreenAttrs[off + 1] = attr;
6491 screen_char_2(off, row, col);
6493 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6495 ScreenLines2[off] = ptr[1];
6496 screen_char(off, row, col);
6498 else
6499 #endif
6500 screen_char(off, row, col);
6502 #ifdef FEAT_MBYTE
6503 if (has_mbyte)
6505 off += mbyte_cells;
6506 col += mbyte_cells;
6507 ptr += mbyte_blen;
6508 if (clear_next_cell)
6509 ptr = (char_u *)" ";
6511 else
6512 #endif
6514 ++off;
6515 ++col;
6516 ++ptr;
6521 #ifdef FEAT_SEARCH_EXTRA
6523 * Prepare for 'hlsearch' highlighting.
6525 static void
6526 start_search_hl()
6528 if (p_hls && !no_hlsearch)
6530 last_pat_prog(&search_hl.rm);
6531 search_hl.attr = hl_attr(HLF_L);
6532 # ifdef FEAT_RELTIME
6533 /* Set the time limit to 'redrawtime'. */
6534 profile_setlimit(p_rdt, &search_hl.tm);
6535 # endif
6540 * Clean up for 'hlsearch' highlighting.
6542 static void
6543 end_search_hl()
6545 if (search_hl.rm.regprog != NULL)
6547 vim_free(search_hl.rm.regprog);
6548 search_hl.rm.regprog = NULL;
6553 * Advance to the match in window "wp" line "lnum" or past it.
6555 static void
6556 prepare_search_hl(wp, lnum)
6557 win_T *wp;
6558 linenr_T lnum;
6560 matchitem_T *cur; /* points to the match list */
6561 match_T *shl; /* points to search_hl or a match */
6562 int shl_flag; /* flag to indicate whether search_hl
6563 has been processed or not */
6564 int n;
6567 * When using a multi-line pattern, start searching at the top
6568 * of the window or just after a closed fold.
6569 * Do this both for search_hl and the match list.
6571 cur = wp->w_match_head;
6572 shl_flag = FALSE;
6573 while (cur != NULL || shl_flag == FALSE)
6575 if (shl_flag == FALSE)
6577 shl = &search_hl;
6578 shl_flag = TRUE;
6580 else
6581 shl = &cur->hl;
6582 if (shl->rm.regprog != NULL
6583 && shl->lnum == 0
6584 && re_multiline(shl->rm.regprog))
6586 if (shl->first_lnum == 0)
6588 # ifdef FEAT_FOLDING
6589 for (shl->first_lnum = lnum;
6590 shl->first_lnum > wp->w_topline; --shl->first_lnum)
6591 if (hasFoldingWin(wp, shl->first_lnum - 1,
6592 NULL, NULL, TRUE, NULL))
6593 break;
6594 # else
6595 shl->first_lnum = wp->w_topline;
6596 # endif
6598 n = 0;
6599 while (shl->first_lnum < lnum && shl->rm.regprog != NULL)
6601 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n);
6602 if (shl->lnum != 0)
6604 shl->first_lnum = shl->lnum
6605 + shl->rm.endpos[0].lnum
6606 - shl->rm.startpos[0].lnum;
6607 n = shl->rm.endpos[0].col;
6609 else
6611 ++shl->first_lnum;
6612 n = 0;
6616 if (shl != &search_hl && cur != NULL)
6617 cur = cur->next;
6622 * Search for a next 'hlsearch' or match.
6623 * Uses shl->buf.
6624 * Sets shl->lnum and shl->rm contents.
6625 * Note: Assumes a previous match is always before "lnum", unless
6626 * shl->lnum is zero.
6627 * Careful: Any pointers for buffer lines will become invalid.
6629 static void
6630 next_search_hl(win, shl, lnum, mincol)
6631 win_T *win;
6632 match_T *shl; /* points to search_hl or a match */
6633 linenr_T lnum;
6634 colnr_T mincol; /* minimal column for a match */
6636 linenr_T l;
6637 colnr_T matchcol;
6638 long nmatched;
6640 if (shl->lnum != 0)
6642 /* Check for three situations:
6643 * 1. If the "lnum" is below a previous match, start a new search.
6644 * 2. If the previous match includes "mincol", use it.
6645 * 3. Continue after the previous match.
6647 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
6648 if (lnum > l)
6649 shl->lnum = 0;
6650 else if (lnum < l || shl->rm.endpos[0].col > mincol)
6651 return;
6655 * Repeat searching for a match until one is found that includes "mincol"
6656 * or none is found in this line.
6658 called_emsg = FALSE;
6659 for (;;)
6661 #ifdef FEAT_RELTIME
6662 /* Stop searching after passing the time limit. */
6663 if (profile_passed_limit(&(shl->tm)))
6665 shl->lnum = 0; /* no match found in time */
6666 break;
6668 #endif
6669 /* Three situations:
6670 * 1. No useful previous match: search from start of line.
6671 * 2. Not Vi compatible or empty match: continue at next character.
6672 * Break the loop if this is beyond the end of the line.
6673 * 3. Vi compatible searching: continue at end of previous match.
6675 if (shl->lnum == 0)
6676 matchcol = 0;
6677 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
6678 || (shl->rm.endpos[0].lnum == 0
6679 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
6681 char_u *ml;
6683 matchcol = shl->rm.startpos[0].col;
6684 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
6685 if (*ml == NUL)
6687 ++matchcol;
6688 shl->lnum = 0;
6689 break;
6691 #ifdef FEAT_MBYTE
6692 if (has_mbyte)
6693 matchcol += mb_ptr2len(ml);
6694 else
6695 #endif
6696 ++matchcol;
6698 else
6699 matchcol = shl->rm.endpos[0].col;
6701 shl->lnum = lnum;
6702 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum, matchcol,
6703 #ifdef FEAT_RELTIME
6704 &(shl->tm)
6705 #else
6706 NULL
6707 #endif
6709 if (called_emsg)
6711 /* Error while handling regexp: stop using this regexp. */
6712 if (shl == &search_hl)
6714 /* don't free regprog in the match list, it's a copy */
6715 vim_free(shl->rm.regprog);
6716 no_hlsearch = TRUE;
6718 shl->rm.regprog = NULL;
6719 shl->lnum = 0;
6720 got_int = FALSE; /* avoid the "Type :quit to exit Vim" message */
6721 break;
6723 if (nmatched == 0)
6725 shl->lnum = 0; /* no match found */
6726 break;
6728 if (shl->rm.startpos[0].lnum > 0
6729 || shl->rm.startpos[0].col >= mincol
6730 || nmatched > 1
6731 || shl->rm.endpos[0].col > mincol)
6733 shl->lnum += shl->rm.startpos[0].lnum;
6734 break; /* useful match found */
6738 #endif
6740 static void
6741 screen_start_highlight(attr)
6742 int attr;
6744 attrentry_T *aep = NULL;
6746 screen_attr = attr;
6747 if (full_screen
6748 #ifdef WIN3264
6749 && termcap_active
6750 #endif
6753 #ifdef FEAT_GUI
6754 if (gui.in_use)
6756 char buf[20];
6758 /* The GUI handles this internally. */
6759 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
6760 OUT_STR(buf);
6762 else
6763 #endif
6765 if (attr > HL_ALL) /* special HL attr. */
6767 if (t_colors > 1)
6768 aep = syn_cterm_attr2entry(attr);
6769 else
6770 aep = syn_term_attr2entry(attr);
6771 if (aep == NULL) /* did ":syntax clear" */
6772 attr = 0;
6773 else
6774 attr = aep->ae_attr;
6776 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
6777 out_str(T_MD);
6778 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
6779 && cterm_normal_fg_bold)
6780 /* If the Normal FG color has BOLD attribute and the new HL
6781 * has a FG color defined, clear BOLD. */
6782 out_str(T_ME);
6783 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
6784 out_str(T_SO);
6785 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
6786 /* underline or undercurl */
6787 out_str(T_US);
6788 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
6789 out_str(T_CZH);
6790 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
6791 out_str(T_MR);
6794 * Output the color or start string after bold etc., in case the
6795 * bold etc. override the color setting.
6797 if (aep != NULL)
6799 if (t_colors > 1)
6801 if (aep->ae_u.cterm.fg_color)
6802 term_fg_color(aep->ae_u.cterm.fg_color - 1);
6803 if (aep->ae_u.cterm.bg_color)
6804 term_bg_color(aep->ae_u.cterm.bg_color - 1);
6806 else
6808 if (aep->ae_u.term.start != NULL)
6809 out_str(aep->ae_u.term.start);
6816 void
6817 screen_stop_highlight()
6819 int do_ME = FALSE; /* output T_ME code */
6821 if (screen_attr != 0
6822 #ifdef WIN3264
6823 && termcap_active
6824 #endif
6827 #ifdef FEAT_GUI
6828 if (gui.in_use)
6830 char buf[20];
6832 /* use internal GUI code */
6833 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
6834 OUT_STR(buf);
6836 else
6837 #endif
6839 if (screen_attr > HL_ALL) /* special HL attr. */
6841 attrentry_T *aep;
6843 if (t_colors > 1)
6846 * Assume that t_me restores the original colors!
6848 aep = syn_cterm_attr2entry(screen_attr);
6849 if (aep != NULL && (aep->ae_u.cterm.fg_color
6850 || aep->ae_u.cterm.bg_color))
6851 do_ME = TRUE;
6853 else
6855 aep = syn_term_attr2entry(screen_attr);
6856 if (aep != NULL && aep->ae_u.term.stop != NULL)
6858 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
6859 do_ME = TRUE;
6860 else
6861 out_str(aep->ae_u.term.stop);
6864 if (aep == NULL) /* did ":syntax clear" */
6865 screen_attr = 0;
6866 else
6867 screen_attr = aep->ae_attr;
6871 * Often all ending-codes are equal to T_ME. Avoid outputting the
6872 * same sequence several times.
6874 if (screen_attr & HL_STANDOUT)
6876 if (STRCMP(T_SE, T_ME) == 0)
6877 do_ME = TRUE;
6878 else
6879 out_str(T_SE);
6881 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
6883 if (STRCMP(T_UE, T_ME) == 0)
6884 do_ME = TRUE;
6885 else
6886 out_str(T_UE);
6888 if (screen_attr & HL_ITALIC)
6890 if (STRCMP(T_CZR, T_ME) == 0)
6891 do_ME = TRUE;
6892 else
6893 out_str(T_CZR);
6895 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
6896 out_str(T_ME);
6898 if (t_colors > 1)
6900 /* set Normal cterm colors */
6901 if (cterm_normal_fg_color != 0)
6902 term_fg_color(cterm_normal_fg_color - 1);
6903 if (cterm_normal_bg_color != 0)
6904 term_bg_color(cterm_normal_bg_color - 1);
6905 if (cterm_normal_fg_bold)
6906 out_str(T_MD);
6910 screen_attr = 0;
6914 * Reset the colors for a cterm. Used when leaving Vim.
6915 * The machine specific code may override this again.
6917 void
6918 reset_cterm_colors()
6920 if (t_colors > 1)
6922 /* set Normal cterm colors */
6923 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
6925 out_str(T_OP);
6926 screen_attr = -1;
6928 if (cterm_normal_fg_bold)
6930 out_str(T_ME);
6931 screen_attr = -1;
6937 * Put character ScreenLines["off"] on the screen at position "row" and "col",
6938 * using the attributes from ScreenAttrs["off"].
6940 static void
6941 screen_char(off, row, col)
6942 unsigned off;
6943 int row;
6944 int col;
6946 int attr;
6948 /* Check for illegal values, just in case (could happen just after
6949 * resizing). */
6950 if (row >= screen_Rows || col >= screen_Columns)
6951 return;
6953 /* Outputting the last character on the screen may scrollup the screen.
6954 * Don't to it! Mark the character invalid (update it when scrolled up) */
6955 if (row == screen_Rows - 1 && col == screen_Columns - 1
6956 #ifdef FEAT_RIGHTLEFT
6957 /* account for first command-line character in rightleft mode */
6958 && !cmdmsg_rl
6959 #endif
6962 ScreenAttrs[off] = (sattr_T)-1;
6963 return;
6967 * Stop highlighting first, so it's easier to move the cursor.
6969 #if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
6970 if (screen_char_attr != 0)
6971 attr = screen_char_attr;
6972 else
6973 #endif
6974 attr = ScreenAttrs[off];
6975 if (screen_attr != attr)
6976 screen_stop_highlight();
6978 windgoto(row, col);
6980 if (screen_attr != attr)
6981 screen_start_highlight(attr);
6983 #ifdef FEAT_MBYTE
6984 if (enc_utf8 && ScreenLinesUC[off] != 0)
6986 char_u buf[MB_MAXBYTES + 1];
6988 /* Convert UTF-8 character to bytes and write it. */
6990 buf[utfc_char2bytes(off, buf)] = NUL;
6992 out_str(buf);
6993 if (utf_char2cells(ScreenLinesUC[off]) > 1)
6994 ++screen_cur_col;
6996 else
6997 #endif
6999 #ifdef FEAT_MBYTE
7000 out_flush_check();
7001 #endif
7002 out_char(ScreenLines[off]);
7003 #ifdef FEAT_MBYTE
7004 /* double-byte character in single-width cell */
7005 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7006 out_char(ScreenLines2[off]);
7007 #endif
7010 screen_cur_col++;
7013 #ifdef FEAT_MBYTE
7016 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
7017 * on the screen at position 'row' and 'col'.
7018 * The attributes of the first byte is used for all. This is required to
7019 * output the two bytes of a double-byte character with nothing in between.
7021 static void
7022 screen_char_2(off, row, col)
7023 unsigned off;
7024 int row;
7025 int col;
7027 /* Check for illegal values (could be wrong when screen was resized). */
7028 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
7029 return;
7031 /* Outputting the last character on the screen may scrollup the screen.
7032 * Don't to it! Mark the character invalid (update it when scrolled up) */
7033 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
7035 ScreenAttrs[off] = (sattr_T)-1;
7036 return;
7039 /* Output the first byte normally (positions the cursor), then write the
7040 * second byte directly. */
7041 screen_char(off, row, col);
7042 out_char(ScreenLines[off + 1]);
7043 ++screen_cur_col;
7045 #endif
7047 #if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
7049 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
7050 * This uses the contents of ScreenLines[] and doesn't change it.
7052 void
7053 screen_draw_rectangle(row, col, height, width, invert)
7054 int row;
7055 int col;
7056 int height;
7057 int width;
7058 int invert;
7060 int r, c;
7061 int off;
7062 #ifdef FEAT_MBYTE
7063 int max_off;
7064 #endif
7066 /* Can't use ScreenLines unless initialized */
7067 if (ScreenLines == NULL)
7068 return;
7070 if (invert)
7071 screen_char_attr = HL_INVERSE;
7072 for (r = row; r < row + height; ++r)
7074 off = LineOffset[r];
7075 #ifdef FEAT_MBYTE
7076 max_off = off + screen_Columns;
7077 #endif
7078 for (c = col; c < col + width; ++c)
7080 #ifdef FEAT_MBYTE
7081 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
7083 screen_char_2(off + c, r, c);
7084 ++c;
7086 else
7087 #endif
7089 screen_char(off + c, r, c);
7090 #ifdef FEAT_MBYTE
7091 if (utf_off2cells(off + c, max_off) > 1)
7092 ++c;
7093 #endif
7097 screen_char_attr = 0;
7099 #endif
7101 #ifdef FEAT_VERTSPLIT
7103 * Redraw the characters for a vertically split window.
7105 static void
7106 redraw_block(row, end, wp)
7107 int row;
7108 int end;
7109 win_T *wp;
7111 int col;
7112 int width;
7114 # ifdef FEAT_CLIPBOARD
7115 clip_may_clear_selection(row, end - 1);
7116 # endif
7118 if (wp == NULL)
7120 col = 0;
7121 width = Columns;
7123 else
7125 col = wp->w_wincol;
7126 width = wp->w_width;
7128 screen_draw_rectangle(row, col, end - row, width, FALSE);
7130 #endif
7133 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
7134 * with character 'c1' in first column followed by 'c2' in the other columns.
7135 * Use attributes 'attr'.
7137 void
7138 screen_fill(start_row, end_row, start_col, end_col, c1, c2, attr)
7139 int start_row, end_row;
7140 int start_col, end_col;
7141 int c1, c2;
7142 int attr;
7144 int row;
7145 int col;
7146 int off;
7147 int end_off;
7148 int did_delete;
7149 int c;
7150 int norm_term;
7151 #if defined(FEAT_GUI) || defined(UNIX)
7152 int force_next = FALSE;
7153 #endif
7155 if (end_row > screen_Rows) /* safety check */
7156 end_row = screen_Rows;
7157 if (end_col > screen_Columns) /* safety check */
7158 end_col = screen_Columns;
7159 if (ScreenLines == NULL
7160 || start_row >= end_row
7161 || start_col >= end_col) /* nothing to do */
7162 return;
7164 /* it's a "normal" terminal when not in a GUI or cterm */
7165 norm_term = (
7166 #ifdef FEAT_GUI
7167 !gui.in_use &&
7168 #endif
7169 t_colors <= 1);
7170 for (row = start_row; row < end_row; ++row)
7172 #ifdef FEAT_MBYTE
7173 if (has_mbyte
7174 # ifdef FEAT_GUI
7175 && !gui.in_use
7176 # endif
7179 /* When drawing over the right halve of a double-wide char clear
7180 * out the left halve. When drawing over the left halve of a
7181 * double wide-char clear out the right halve. Only needed in a
7182 * terminal. */
7183 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
7184 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
7185 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
7186 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
7188 #endif
7190 * Try to use delete-line termcap code, when no attributes or in a
7191 * "normal" terminal, where a bold/italic space is just a
7192 * space.
7194 did_delete = FALSE;
7195 if (c2 == ' '
7196 && end_col == Columns
7197 && can_clear(T_CE)
7198 && (attr == 0
7199 || (norm_term
7200 && attr <= HL_ALL
7201 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
7204 * check if we really need to clear something
7206 col = start_col;
7207 if (c1 != ' ') /* don't clear first char */
7208 ++col;
7210 off = LineOffset[row] + col;
7211 end_off = LineOffset[row] + end_col;
7213 /* skip blanks (used often, keep it fast!) */
7214 #ifdef FEAT_MBYTE
7215 if (enc_utf8)
7216 while (off < end_off && ScreenLines[off] == ' '
7217 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
7218 ++off;
7219 else
7220 #endif
7221 while (off < end_off && ScreenLines[off] == ' '
7222 && ScreenAttrs[off] == 0)
7223 ++off;
7224 if (off < end_off) /* something to be cleared */
7226 col = off - LineOffset[row];
7227 screen_stop_highlight();
7228 term_windgoto(row, col);/* clear rest of this screen line */
7229 out_str(T_CE);
7230 screen_start(); /* don't know where cursor is now */
7231 col = end_col - col;
7232 while (col--) /* clear chars in ScreenLines */
7234 ScreenLines[off] = ' ';
7235 #ifdef FEAT_MBYTE
7236 if (enc_utf8)
7237 ScreenLinesUC[off] = 0;
7238 #endif
7239 ScreenAttrs[off] = 0;
7240 ++off;
7243 did_delete = TRUE; /* the chars are cleared now */
7246 off = LineOffset[row] + start_col;
7247 c = c1;
7248 for (col = start_col; col < end_col; ++col)
7250 if (ScreenLines[off] != c
7251 #ifdef FEAT_MBYTE
7252 || (enc_utf8 && (int)ScreenLinesUC[off]
7253 != (c >= 0x80 ? c : 0))
7254 #endif
7255 || ScreenAttrs[off] != attr
7256 #if defined(FEAT_GUI) || defined(UNIX)
7257 || force_next
7258 #endif
7261 #if defined(FEAT_GUI) || defined(UNIX)
7262 /* The bold trick may make a single row of pixels appear in
7263 * the next character. When a bold character is removed, the
7264 * next character should be redrawn too. This happens for our
7265 * own GUI and for some xterms. */
7266 if (
7267 # ifdef FEAT_GUI
7268 gui.in_use
7269 # endif
7270 # if defined(FEAT_GUI) && defined(UNIX)
7272 # endif
7273 # ifdef UNIX
7274 term_is_xterm
7275 # endif
7278 if (ScreenLines[off] != ' '
7279 && (ScreenAttrs[off] > HL_ALL
7280 || ScreenAttrs[off] & HL_BOLD))
7281 force_next = TRUE;
7282 else
7283 force_next = FALSE;
7285 #endif
7286 ScreenLines[off] = c;
7287 #ifdef FEAT_MBYTE
7288 if (enc_utf8)
7290 if (c >= 0x80)
7292 ScreenLinesUC[off] = c;
7293 ScreenLinesC[0][off] = 0;
7295 else
7296 ScreenLinesUC[off] = 0;
7298 #endif
7299 ScreenAttrs[off] = attr;
7300 if (!did_delete || c != ' ')
7301 screen_char(off, row, col);
7303 ++off;
7304 if (col == start_col)
7306 if (did_delete)
7307 break;
7308 c = c2;
7311 if (end_col == Columns)
7312 LineWraps[row] = FALSE;
7313 if (row == Rows - 1) /* overwritten the command line */
7315 redraw_cmdline = TRUE;
7316 if (c1 == ' ' && c2 == ' ')
7317 clear_cmdline = FALSE; /* command line has been cleared */
7318 if (start_col == 0)
7319 mode_displayed = FALSE; /* mode cleared or overwritten */
7325 * Check if there should be a delay. Used before clearing or redrawing the
7326 * screen or the command line.
7328 void
7329 check_for_delay(check_msg_scroll)
7330 int check_msg_scroll;
7332 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
7333 && !did_wait_return
7334 && emsg_silent == 0)
7336 out_flush();
7337 ui_delay(1000L, TRUE);
7338 emsg_on_display = FALSE;
7339 if (check_msg_scroll)
7340 msg_scroll = FALSE;
7345 * screen_valid - allocate screen buffers if size changed
7346 * If "clear" is TRUE: clear screen if it has been resized.
7347 * Returns TRUE if there is a valid screen to write to.
7348 * Returns FALSE when starting up and screen not initialized yet.
7351 screen_valid(clear)
7352 int clear;
7354 screenalloc(clear); /* allocate screen buffers if size changed */
7355 return (ScreenLines != NULL);
7359 * Resize the shell to Rows and Columns.
7360 * Allocate ScreenLines[] and associated items.
7362 * There may be some time between setting Rows and Columns and (re)allocating
7363 * ScreenLines[]. This happens when starting up and when (manually) changing
7364 * the shell size. Always use screen_Rows and screen_Columns to access items
7365 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
7366 * final size of the shell is needed.
7368 void
7369 screenalloc(clear)
7370 int clear;
7372 int new_row, old_row;
7373 #ifdef FEAT_GUI
7374 int old_Rows;
7375 #endif
7376 win_T *wp;
7377 int outofmem = FALSE;
7378 int len;
7379 schar_T *new_ScreenLines;
7380 #ifdef FEAT_MBYTE
7381 u8char_T *new_ScreenLinesUC = NULL;
7382 u8char_T *new_ScreenLinesC[MAX_MCO];
7383 schar_T *new_ScreenLines2 = NULL;
7384 int i;
7385 #endif
7386 sattr_T *new_ScreenAttrs;
7387 unsigned *new_LineOffset;
7388 char_u *new_LineWraps;
7389 #ifdef FEAT_WINDOWS
7390 short *new_TabPageIdxs;
7391 tabpage_T *tp;
7392 #endif
7393 static int entered = FALSE; /* avoid recursiveness */
7394 static int done_outofmem_msg = FALSE; /* did outofmem message */
7395 #ifdef FEAT_AUTOCMD
7396 int retry_count = 0;
7398 retry:
7399 #endif
7401 * Allocation of the screen buffers is done only when the size changes and
7402 * when Rows and Columns have been set and we have started doing full
7403 * screen stuff.
7405 if ((ScreenLines != NULL
7406 && Rows == screen_Rows
7407 && Columns == screen_Columns
7408 #ifdef FEAT_MBYTE
7409 && enc_utf8 == (ScreenLinesUC != NULL)
7410 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
7411 && p_mco == Screen_mco
7412 #endif
7414 || Rows == 0
7415 || Columns == 0
7416 || (!full_screen && ScreenLines == NULL))
7417 return;
7420 * It's possible that we produce an out-of-memory message below, which
7421 * will cause this function to be called again. To break the loop, just
7422 * return here.
7424 if (entered)
7425 return;
7426 entered = TRUE;
7429 * Note that the window sizes are updated before reallocating the arrays,
7430 * thus we must not redraw here!
7432 ++RedrawingDisabled;
7434 win_new_shellsize(); /* fit the windows in the new sized shell */
7436 comp_col(); /* recompute columns for shown command and ruler */
7439 * We're changing the size of the screen.
7440 * - Allocate new arrays for ScreenLines and ScreenAttrs.
7441 * - Move lines from the old arrays into the new arrays, clear extra
7442 * lines (unless the screen is going to be cleared).
7443 * - Free the old arrays.
7445 * If anything fails, make ScreenLines NULL, so we don't do anything!
7446 * Continuing with the old ScreenLines may result in a crash, because the
7447 * size is wrong.
7449 FOR_ALL_TAB_WINDOWS(tp, wp)
7450 win_free_lsize(wp);
7452 new_ScreenLines = (schar_T *)lalloc((long_u)(
7453 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7454 #ifdef FEAT_MBYTE
7455 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T) * MAX_MCO);
7456 if (enc_utf8)
7458 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
7459 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
7460 for (i = 0; i < p_mco; ++i)
7461 new_ScreenLinesC[i] = (u8char_T *)lalloc((long_u)(
7462 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
7464 if (enc_dbcs == DBCS_JPNU)
7465 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
7466 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7467 #endif
7468 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
7469 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
7470 new_LineOffset = (unsigned *)lalloc((long_u)(
7471 Rows * sizeof(unsigned)), FALSE);
7472 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
7473 #ifdef FEAT_WINDOWS
7474 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
7475 #endif
7477 FOR_ALL_TAB_WINDOWS(tp, wp)
7479 if (win_alloc_lines(wp) == FAIL)
7481 outofmem = TRUE;
7482 #ifdef FEAT_WINDOWS
7483 goto give_up;
7484 #endif
7487 #ifdef FEAT_WINDOWS
7488 give_up:
7489 #endif
7491 #ifdef FEAT_MBYTE
7492 for (i = 0; i < p_mco; ++i)
7493 if (new_ScreenLinesC[i] == NULL)
7494 break;
7495 #endif
7496 if (new_ScreenLines == NULL
7497 #ifdef FEAT_MBYTE
7498 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
7499 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
7500 #endif
7501 || new_ScreenAttrs == NULL
7502 || new_LineOffset == NULL
7503 || new_LineWraps == NULL
7504 #ifdef FEAT_WINDOWS
7505 || new_TabPageIdxs == NULL
7506 #endif
7507 || outofmem)
7509 if (ScreenLines != NULL || !done_outofmem_msg)
7511 /* guess the size */
7512 do_outofmem_msg((long_u)((Rows + 1) * Columns));
7514 /* Remember we did this to avoid getting outofmem messages over
7515 * and over again. */
7516 done_outofmem_msg = TRUE;
7518 vim_free(new_ScreenLines);
7519 new_ScreenLines = NULL;
7520 #ifdef FEAT_MBYTE
7521 vim_free(new_ScreenLinesUC);
7522 new_ScreenLinesUC = NULL;
7523 for (i = 0; i < p_mco; ++i)
7525 vim_free(new_ScreenLinesC[i]);
7526 new_ScreenLinesC[i] = NULL;
7528 vim_free(new_ScreenLines2);
7529 new_ScreenLines2 = NULL;
7530 #endif
7531 vim_free(new_ScreenAttrs);
7532 new_ScreenAttrs = NULL;
7533 vim_free(new_LineOffset);
7534 new_LineOffset = NULL;
7535 vim_free(new_LineWraps);
7536 new_LineWraps = NULL;
7537 #ifdef FEAT_WINDOWS
7538 vim_free(new_TabPageIdxs);
7539 new_TabPageIdxs = NULL;
7540 #endif
7542 else
7544 done_outofmem_msg = FALSE;
7546 for (new_row = 0; new_row < Rows; ++new_row)
7548 new_LineOffset[new_row] = new_row * Columns;
7549 new_LineWraps[new_row] = FALSE;
7552 * If the screen is not going to be cleared, copy as much as
7553 * possible from the old screen to the new one and clear the rest
7554 * (used when resizing the window at the "--more--" prompt or when
7555 * executing an external command, for the GUI).
7557 if (!clear)
7559 (void)vim_memset(new_ScreenLines + new_row * Columns,
7560 ' ', (size_t)Columns * sizeof(schar_T));
7561 #ifdef FEAT_MBYTE
7562 if (enc_utf8)
7564 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
7565 0, (size_t)Columns * sizeof(u8char_T));
7566 for (i = 0; i < p_mco; ++i)
7567 (void)vim_memset(new_ScreenLinesC[i]
7568 + new_row * Columns,
7569 0, (size_t)Columns * sizeof(u8char_T));
7571 if (enc_dbcs == DBCS_JPNU)
7572 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
7573 0, (size_t)Columns * sizeof(schar_T));
7574 #endif
7575 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
7576 0, (size_t)Columns * sizeof(sattr_T));
7577 old_row = new_row + (screen_Rows - Rows);
7578 if (old_row >= 0 && ScreenLines != NULL)
7580 if (screen_Columns < Columns)
7581 len = screen_Columns;
7582 else
7583 len = Columns;
7584 #ifdef FEAT_MBYTE
7585 /* When switching to utf-8 don't copy characters, they
7586 * may be invalid now. Also when p_mco changes. */
7587 if (!(enc_utf8 && ScreenLinesUC == NULL)
7588 && p_mco == Screen_mco)
7589 #endif
7590 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
7591 ScreenLines + LineOffset[old_row],
7592 (size_t)len * sizeof(schar_T));
7593 #ifdef FEAT_MBYTE
7594 if (enc_utf8 && ScreenLinesUC != NULL
7595 && p_mco == Screen_mco)
7597 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
7598 ScreenLinesUC + LineOffset[old_row],
7599 (size_t)len * sizeof(u8char_T));
7600 for (i = 0; i < p_mco; ++i)
7601 mch_memmove(new_ScreenLinesC[i]
7602 + new_LineOffset[new_row],
7603 ScreenLinesC[i] + LineOffset[old_row],
7604 (size_t)len * sizeof(u8char_T));
7606 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
7607 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
7608 ScreenLines2 + LineOffset[old_row],
7609 (size_t)len * sizeof(schar_T));
7610 #endif
7611 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
7612 ScreenAttrs + LineOffset[old_row],
7613 (size_t)len * sizeof(sattr_T));
7617 /* Use the last line of the screen for the current line. */
7618 current_ScreenLine = new_ScreenLines + Rows * Columns;
7621 free_screenlines();
7623 ScreenLines = new_ScreenLines;
7624 #ifdef FEAT_MBYTE
7625 ScreenLinesUC = new_ScreenLinesUC;
7626 for (i = 0; i < p_mco; ++i)
7627 ScreenLinesC[i] = new_ScreenLinesC[i];
7628 Screen_mco = p_mco;
7629 ScreenLines2 = new_ScreenLines2;
7630 #endif
7631 ScreenAttrs = new_ScreenAttrs;
7632 LineOffset = new_LineOffset;
7633 LineWraps = new_LineWraps;
7634 #ifdef FEAT_WINDOWS
7635 TabPageIdxs = new_TabPageIdxs;
7636 #endif
7638 /* It's important that screen_Rows and screen_Columns reflect the actual
7639 * size of ScreenLines[]. Set them before calling anything. */
7640 #ifdef FEAT_GUI
7641 old_Rows = screen_Rows;
7642 #endif
7643 screen_Rows = Rows;
7644 screen_Columns = Columns;
7646 must_redraw = CLEAR; /* need to clear the screen later */
7647 if (clear)
7648 screenclear2();
7650 #ifdef FEAT_GUI
7651 else if (gui.in_use
7652 && !gui.starting
7653 && ScreenLines != NULL
7654 && old_Rows != Rows)
7656 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
7658 * Adjust the position of the cursor, for when executing an external
7659 * command.
7661 if (msg_row >= Rows) /* Rows got smaller */
7662 msg_row = Rows - 1; /* put cursor at last row */
7663 else if (Rows > old_Rows) /* Rows got bigger */
7664 msg_row += Rows - old_Rows; /* put cursor in same place */
7665 if (msg_col >= Columns) /* Columns got smaller */
7666 msg_col = Columns - 1; /* put cursor at last column */
7668 #endif
7670 entered = FALSE;
7671 --RedrawingDisabled;
7673 #ifdef FEAT_AUTOCMD
7675 * Do not apply autocommands more than 3 times to avoid an endless loop
7676 * in case applying autocommands always changes Rows or Columns.
7678 if (starting == 0 && ++retry_count <= 3)
7680 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
7681 /* In rare cases, autocommands may have altered Rows or Columns,
7682 * jump back to check if we need to allocate the screen again. */
7683 goto retry;
7685 #endif
7688 void
7689 free_screenlines()
7691 #ifdef FEAT_MBYTE
7692 int i;
7694 vim_free(ScreenLinesUC);
7695 for (i = 0; i < Screen_mco; ++i)
7696 vim_free(ScreenLinesC[i]);
7697 vim_free(ScreenLines2);
7698 #endif
7699 vim_free(ScreenLines);
7700 vim_free(ScreenAttrs);
7701 vim_free(LineOffset);
7702 vim_free(LineWraps);
7703 #ifdef FEAT_WINDOWS
7704 vim_free(TabPageIdxs);
7705 #endif
7708 void
7709 screenclear()
7711 check_for_delay(FALSE);
7712 screenalloc(FALSE); /* allocate screen buffers if size changed */
7713 screenclear2(); /* clear the screen */
7716 static void
7717 screenclear2()
7719 int i;
7721 if (starting == NO_SCREEN || ScreenLines == NULL
7722 #ifdef FEAT_GUI
7723 || (gui.in_use && gui.starting)
7724 #endif
7726 return;
7728 #ifdef FEAT_GUI
7729 if (!gui.in_use)
7730 #endif
7731 screen_attr = -1; /* force setting the Normal colors */
7732 screen_stop_highlight(); /* don't want highlighting here */
7734 #ifdef FEAT_CLIPBOARD
7735 /* disable selection without redrawing it */
7736 clip_scroll_selection(9999);
7737 #endif
7739 /* blank out ScreenLines */
7740 for (i = 0; i < Rows; ++i)
7742 lineclear(LineOffset[i], (int)Columns);
7743 LineWraps[i] = FALSE;
7746 if (can_clear(T_CL))
7748 out_str(T_CL); /* clear the display */
7749 clear_cmdline = FALSE;
7750 mode_displayed = FALSE;
7752 else
7754 /* can't clear the screen, mark all chars with invalid attributes */
7755 for (i = 0; i < Rows; ++i)
7756 lineinvalid(LineOffset[i], (int)Columns);
7757 clear_cmdline = TRUE;
7760 screen_cleared = TRUE; /* can use contents of ScreenLines now */
7762 win_rest_invalid(firstwin);
7763 redraw_cmdline = TRUE;
7764 #ifdef FEAT_WINDOWS
7765 redraw_tabline = TRUE;
7766 #endif
7767 if (must_redraw == CLEAR) /* no need to clear again */
7768 must_redraw = NOT_VALID;
7769 compute_cmdrow();
7770 msg_row = cmdline_row; /* put cursor on last line for messages */
7771 msg_col = 0;
7772 screen_start(); /* don't know where cursor is now */
7773 msg_scrolled = 0; /* can't scroll back */
7774 msg_didany = FALSE;
7775 msg_didout = FALSE;
7779 * Clear one line in ScreenLines.
7781 static void
7782 lineclear(off, width)
7783 unsigned off;
7784 int width;
7786 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
7787 #ifdef FEAT_MBYTE
7788 if (enc_utf8)
7789 (void)vim_memset(ScreenLinesUC + off, 0,
7790 (size_t)width * sizeof(u8char_T));
7791 #endif
7792 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
7796 * Mark one line in ScreenLines invalid by setting the attributes to an
7797 * invalid value.
7799 static void
7800 lineinvalid(off, width)
7801 unsigned off;
7802 int width;
7804 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
7807 #ifdef FEAT_VERTSPLIT
7809 * Copy part of a Screenline for vertically split window "wp".
7811 static void
7812 linecopy(to, from, wp)
7813 int to;
7814 int from;
7815 win_T *wp;
7817 unsigned off_to = LineOffset[to] + wp->w_wincol;
7818 unsigned off_from = LineOffset[from] + wp->w_wincol;
7820 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
7821 wp->w_width * sizeof(schar_T));
7822 # ifdef FEAT_MBYTE
7823 if (enc_utf8)
7825 int i;
7827 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
7828 wp->w_width * sizeof(u8char_T));
7829 for (i = 0; i < p_mco; ++i)
7830 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
7831 wp->w_width * sizeof(u8char_T));
7833 if (enc_dbcs == DBCS_JPNU)
7834 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
7835 wp->w_width * sizeof(schar_T));
7836 # endif
7837 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
7838 wp->w_width * sizeof(sattr_T));
7840 #endif
7843 * Return TRUE if clearing with term string "p" would work.
7844 * It can't work when the string is empty or it won't set the right background.
7847 can_clear(p)
7848 char_u *p;
7850 return (*p != NUL && (t_colors <= 1
7851 #ifdef FEAT_GUI
7852 || gui.in_use
7853 #endif
7854 || cterm_normal_bg_color == 0 || *T_UT != NUL));
7858 * Reset cursor position. Use whenever cursor was moved because of outputting
7859 * something directly to the screen (shell commands) or a terminal control
7860 * code.
7862 void
7863 screen_start()
7865 screen_cur_row = screen_cur_col = 9999;
7869 * Move the cursor to position "row","col" in the screen.
7870 * This tries to find the most efficient way to move, minimizing the number of
7871 * characters sent to the terminal.
7873 void
7874 windgoto(row, col)
7875 int row;
7876 int col;
7878 sattr_T *p;
7879 int i;
7880 int plan;
7881 int cost;
7882 int wouldbe_col;
7883 int noinvcurs;
7884 char_u *bs;
7885 int goto_cost;
7886 int attr;
7888 #define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
7889 #define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
7891 #define PLAN_LE 1
7892 #define PLAN_CR 2
7893 #define PLAN_NL 3
7894 #define PLAN_WRITE 4
7895 /* Can't use ScreenLines unless initialized */
7896 if (ScreenLines == NULL)
7897 return;
7899 if (col != screen_cur_col || row != screen_cur_row)
7901 /* Check for valid position. */
7902 if (row < 0) /* window without text lines? */
7903 row = 0;
7904 if (row >= screen_Rows)
7905 row = screen_Rows - 1;
7906 if (col >= screen_Columns)
7907 col = screen_Columns - 1;
7909 /* check if no cursor movement is allowed in highlight mode */
7910 if (screen_attr && *T_MS == NUL)
7911 noinvcurs = HIGHL_COST;
7912 else
7913 noinvcurs = 0;
7914 goto_cost = GOTO_COST + noinvcurs;
7917 * Plan how to do the positioning:
7918 * 1. Use CR to move it to column 0, same row.
7919 * 2. Use T_LE to move it a few columns to the left.
7920 * 3. Use NL to move a few lines down, column 0.
7921 * 4. Move a few columns to the right with T_ND or by writing chars.
7923 * Don't do this if the cursor went beyond the last column, the cursor
7924 * position is unknown then (some terminals wrap, some don't )
7926 * First check if the highlighting attributes allow us to write
7927 * characters to move the cursor to the right.
7929 if (row >= screen_cur_row && screen_cur_col < Columns)
7932 * If the cursor is in the same row, bigger col, we can use CR
7933 * or T_LE.
7935 bs = NULL; /* init for GCC */
7936 attr = screen_attr;
7937 if (row == screen_cur_row && col < screen_cur_col)
7939 /* "le" is preferred over "bc", because "bc" is obsolete */
7940 if (*T_LE)
7941 bs = T_LE; /* "cursor left" */
7942 else
7943 bs = T_BC; /* "backspace character (old) */
7944 if (*bs)
7945 cost = (screen_cur_col - col) * (int)STRLEN(bs);
7946 else
7947 cost = 999;
7948 if (col + 1 < cost) /* using CR is less characters */
7950 plan = PLAN_CR;
7951 wouldbe_col = 0;
7952 cost = 1; /* CR is just one character */
7954 else
7956 plan = PLAN_LE;
7957 wouldbe_col = col;
7959 if (noinvcurs) /* will stop highlighting */
7961 cost += noinvcurs;
7962 attr = 0;
7967 * If the cursor is above where we want to be, we can use CR LF.
7969 else if (row > screen_cur_row)
7971 plan = PLAN_NL;
7972 wouldbe_col = 0;
7973 cost = (row - screen_cur_row) * 2; /* CR LF */
7974 if (noinvcurs) /* will stop highlighting */
7976 cost += noinvcurs;
7977 attr = 0;
7982 * If the cursor is in the same row, smaller col, just use write.
7984 else
7986 plan = PLAN_WRITE;
7987 wouldbe_col = screen_cur_col;
7988 cost = 0;
7992 * Check if any characters that need to be written have the
7993 * correct attributes. Also avoid UTF-8 characters.
7995 i = col - wouldbe_col;
7996 if (i > 0)
7997 cost += i;
7998 if (cost < goto_cost && i > 0)
8001 * Check if the attributes are correct without additionally
8002 * stopping highlighting.
8004 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
8005 while (i && *p++ == attr)
8006 --i;
8007 if (i != 0)
8010 * Try if it works when highlighting is stopped here.
8012 if (*--p == 0)
8014 cost += noinvcurs;
8015 while (i && *p++ == 0)
8016 --i;
8018 if (i != 0)
8019 cost = 999; /* different attributes, don't do it */
8021 #ifdef FEAT_MBYTE
8022 if (enc_utf8)
8024 /* Don't use an UTF-8 char for positioning, it's slow. */
8025 for (i = wouldbe_col; i < col; ++i)
8026 if (ScreenLinesUC[LineOffset[row] + i] != 0)
8028 cost = 999;
8029 break;
8032 #endif
8036 * We can do it without term_windgoto()!
8038 if (cost < goto_cost)
8040 if (plan == PLAN_LE)
8042 if (noinvcurs)
8043 screen_stop_highlight();
8044 while (screen_cur_col > col)
8046 out_str(bs);
8047 --screen_cur_col;
8050 else if (plan == PLAN_CR)
8052 if (noinvcurs)
8053 screen_stop_highlight();
8054 out_char('\r');
8055 screen_cur_col = 0;
8057 else if (plan == PLAN_NL)
8059 if (noinvcurs)
8060 screen_stop_highlight();
8061 while (screen_cur_row < row)
8063 out_char('\n');
8064 ++screen_cur_row;
8066 screen_cur_col = 0;
8069 i = col - screen_cur_col;
8070 if (i > 0)
8073 * Use cursor-right if it's one character only. Avoids
8074 * removing a line of pixels from the last bold char, when
8075 * using the bold trick in the GUI.
8077 if (T_ND[0] != NUL && T_ND[1] == NUL)
8079 while (i-- > 0)
8080 out_char(*T_ND);
8082 else
8084 int off;
8086 off = LineOffset[row] + screen_cur_col;
8087 while (i-- > 0)
8089 if (ScreenAttrs[off] != screen_attr)
8090 screen_stop_highlight();
8091 #ifdef FEAT_MBYTE
8092 out_flush_check();
8093 #endif
8094 out_char(ScreenLines[off]);
8095 #ifdef FEAT_MBYTE
8096 if (enc_dbcs == DBCS_JPNU
8097 && ScreenLines[off] == 0x8e)
8098 out_char(ScreenLines2[off]);
8099 #endif
8100 ++off;
8106 else
8107 cost = 999;
8109 if (cost >= goto_cost)
8111 if (noinvcurs)
8112 screen_stop_highlight();
8113 if (row == screen_cur_row && (col > screen_cur_col) &&
8114 *T_CRI != NUL)
8115 term_cursor_right(col - screen_cur_col);
8116 else
8117 term_windgoto(row, col);
8119 screen_cur_row = row;
8120 screen_cur_col = col;
8125 * Set cursor to its position in the current window.
8127 void
8128 setcursor()
8130 if (redrawing())
8132 validate_cursor();
8133 windgoto(W_WINROW(curwin) + curwin->w_wrow,
8134 W_WINCOL(curwin) + (
8135 #ifdef FEAT_RIGHTLEFT
8136 /* With 'rightleft' set and the cursor on a double-wide
8137 * character, position it on the leftmost column. */
8138 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
8139 # ifdef FEAT_MBYTE
8140 (has_mbyte
8141 && (*mb_ptr2cells)(ml_get_cursor()) == 2
8142 && vim_isprintc(gchar_cursor())) ? 2 :
8143 # endif
8144 1)) :
8145 #endif
8146 curwin->w_wcol));
8152 * insert 'line_count' lines at 'row' in window 'wp'
8153 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
8154 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
8155 * scrolling.
8156 * Returns FAIL if the lines are not inserted, OK for success.
8159 win_ins_lines(wp, row, line_count, invalid, mayclear)
8160 win_T *wp;
8161 int row;
8162 int line_count;
8163 int invalid;
8164 int mayclear;
8166 int did_delete;
8167 int nextrow;
8168 int lastrow;
8169 int retval;
8171 if (invalid)
8172 wp->w_lines_valid = 0;
8174 if (wp->w_height < 5)
8175 return FAIL;
8177 if (line_count > wp->w_height - row)
8178 line_count = wp->w_height - row;
8180 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
8181 if (retval != MAYBE)
8182 return retval;
8185 * If there is a next window or a status line, we first try to delete the
8186 * lines at the bottom to avoid messing what is after the window.
8187 * If this fails and there are following windows, don't do anything to avoid
8188 * messing up those windows, better just redraw.
8190 did_delete = FALSE;
8191 #ifdef FEAT_WINDOWS
8192 if (wp->w_next != NULL || wp->w_status_height)
8194 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8195 line_count, (int)Rows, FALSE, NULL) == OK)
8196 did_delete = TRUE;
8197 else if (wp->w_next)
8198 return FAIL;
8200 #endif
8202 * if no lines deleted, blank the lines that will end up below the window
8204 if (!did_delete)
8206 #ifdef FEAT_WINDOWS
8207 wp->w_redr_status = TRUE;
8208 #endif
8209 redraw_cmdline = TRUE;
8210 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
8211 lastrow = nextrow + line_count;
8212 if (lastrow > Rows)
8213 lastrow = Rows;
8214 screen_fill(nextrow - line_count, lastrow - line_count,
8215 W_WINCOL(wp), (int)W_ENDCOL(wp),
8216 ' ', ' ', 0);
8219 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
8220 == FAIL)
8222 /* deletion will have messed up other windows */
8223 if (did_delete)
8225 #ifdef FEAT_WINDOWS
8226 wp->w_redr_status = TRUE;
8227 #endif
8228 win_rest_invalid(W_NEXT(wp));
8230 return FAIL;
8233 return OK;
8237 * delete "line_count" window lines at "row" in window "wp"
8238 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
8239 * If "mayclear" is TRUE the screen will be cleared if it is faster than
8240 * scrolling
8241 * Return OK for success, FAIL if the lines are not deleted.
8244 win_del_lines(wp, row, line_count, invalid, mayclear)
8245 win_T *wp;
8246 int row;
8247 int line_count;
8248 int invalid;
8249 int mayclear;
8251 int retval;
8253 if (invalid)
8254 wp->w_lines_valid = 0;
8256 if (line_count > wp->w_height - row)
8257 line_count = wp->w_height - row;
8259 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
8260 if (retval != MAYBE)
8261 return retval;
8263 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
8264 (int)Rows, FALSE, NULL) == FAIL)
8265 return FAIL;
8267 #ifdef FEAT_WINDOWS
8269 * If there are windows or status lines below, try to put them at the
8270 * correct place. If we can't do that, they have to be redrawn.
8272 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
8274 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8275 line_count, (int)Rows, NULL) == FAIL)
8277 wp->w_redr_status = TRUE;
8278 win_rest_invalid(wp->w_next);
8282 * If this is the last window and there is no status line, redraw the
8283 * command line later.
8285 else
8286 #endif
8287 redraw_cmdline = TRUE;
8288 return OK;
8292 * Common code for win_ins_lines() and win_del_lines().
8293 * Returns OK or FAIL when the work has been done.
8294 * Returns MAYBE when not finished yet.
8296 static int
8297 win_do_lines(wp, row, line_count, mayclear, del)
8298 win_T *wp;
8299 int row;
8300 int line_count;
8301 int mayclear;
8302 int del;
8304 int retval;
8306 if (!redrawing() || line_count <= 0)
8307 return FAIL;
8309 /* only a few lines left: redraw is faster */
8310 if (mayclear && Rows - line_count < 5
8311 #ifdef FEAT_VERTSPLIT
8312 && wp->w_width == Columns
8313 #endif
8316 screenclear(); /* will set wp->w_lines_valid to 0 */
8317 return FAIL;
8321 * Delete all remaining lines
8323 if (row + line_count >= wp->w_height)
8325 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
8326 W_WINCOL(wp), (int)W_ENDCOL(wp),
8327 ' ', ' ', 0);
8328 return OK;
8332 * when scrolling, the message on the command line should be cleared,
8333 * otherwise it will stay there forever.
8335 clear_cmdline = TRUE;
8338 * If the terminal can set a scroll region, use that.
8339 * Always do this in a vertically split window. This will redraw from
8340 * ScreenLines[] when t_CV isn't defined. That's faster than using
8341 * win_line().
8342 * Don't use a scroll region when we are going to redraw the text, writing
8343 * a character in the lower right corner of the scroll region causes a
8344 * scroll-up in the DJGPP version.
8346 if (scroll_region
8347 #ifdef FEAT_VERTSPLIT
8348 || W_WIDTH(wp) != Columns
8349 #endif
8352 #ifdef FEAT_VERTSPLIT
8353 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8354 #endif
8355 scroll_region_set(wp, row);
8356 if (del)
8357 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
8358 wp->w_height - row, FALSE, wp);
8359 else
8360 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
8361 wp->w_height - row, wp);
8362 #ifdef FEAT_VERTSPLIT
8363 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8364 #endif
8365 scroll_region_reset();
8366 return retval;
8369 #ifdef FEAT_WINDOWS
8370 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
8371 return FAIL;
8372 #endif
8374 return MAYBE;
8378 * window 'wp' and everything after it is messed up, mark it for redraw
8380 static void
8381 win_rest_invalid(wp)
8382 win_T *wp;
8384 #ifdef FEAT_WINDOWS
8385 while (wp != NULL)
8386 #else
8387 if (wp != NULL)
8388 #endif
8390 redraw_win_later(wp, NOT_VALID);
8391 #ifdef FEAT_WINDOWS
8392 wp->w_redr_status = TRUE;
8393 wp = wp->w_next;
8394 #endif
8396 redraw_cmdline = TRUE;
8400 * The rest of the routines in this file perform screen manipulations. The
8401 * given operation is performed physically on the screen. The corresponding
8402 * change is also made to the internal screen image. In this way, the editor
8403 * anticipates the effect of editing changes on the appearance of the screen.
8404 * That way, when we call screenupdate a complete redraw isn't usually
8405 * necessary. Another advantage is that we can keep adding code to anticipate
8406 * screen changes, and in the meantime, everything still works.
8410 * types for inserting or deleting lines
8412 #define USE_T_CAL 1
8413 #define USE_T_CDL 2
8414 #define USE_T_AL 3
8415 #define USE_T_CE 4
8416 #define USE_T_DL 5
8417 #define USE_T_SR 6
8418 #define USE_NL 7
8419 #define USE_T_CD 8
8420 #define USE_REDRAW 9
8423 * insert lines on the screen and update ScreenLines[]
8424 * 'end' is the line after the scrolled part. Normally it is Rows.
8425 * When scrolling region used 'off' is the offset from the top for the region.
8426 * 'row' and 'end' are relative to the start of the region.
8428 * return FAIL for failure, OK for success.
8431 screen_ins_lines(off, row, line_count, end, wp)
8432 int off;
8433 int row;
8434 int line_count;
8435 int end;
8436 win_T *wp; /* NULL or window to use width from */
8438 int i;
8439 int j;
8440 unsigned temp;
8441 int cursor_row;
8442 int type;
8443 int result_empty;
8444 int can_ce = can_clear(T_CE);
8447 * FAIL if
8448 * - there is no valid screen
8449 * - the screen has to be redrawn completely
8450 * - the line count is less than one
8451 * - the line count is more than 'ttyscroll'
8453 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
8454 return FAIL;
8457 * There are seven ways to insert lines:
8458 * 0. When in a vertically split window and t_CV isn't set, redraw the
8459 * characters from ScreenLines[].
8460 * 1. Use T_CD (clear to end of display) if it exists and the result of
8461 * the insert is just empty lines
8462 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
8463 * present or line_count > 1. It looks better if we do all the inserts
8464 * at once.
8465 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
8466 * insert is just empty lines and T_CE is not present or line_count >
8467 * 1.
8468 * 4. Use T_AL (insert line) if it exists.
8469 * 5. Use T_CE (erase line) if it exists and the result of the insert is
8470 * just empty lines.
8471 * 6. Use T_DL (delete line) if it exists and the result of the insert is
8472 * just empty lines.
8473 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
8474 * the 'da' flag is not set or we have clear line capability.
8475 * 8. redraw the characters from ScreenLines[].
8477 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
8478 * the scrollbar for the window. It does have insert line, use that if it
8479 * exists.
8481 result_empty = (row + line_count >= end);
8482 #ifdef FEAT_VERTSPLIT
8483 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
8484 type = USE_REDRAW;
8485 else
8486 #endif
8487 if (can_clear(T_CD) && result_empty)
8488 type = USE_T_CD;
8489 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
8490 type = USE_T_CAL;
8491 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
8492 type = USE_T_CDL;
8493 else if (*T_AL != NUL)
8494 type = USE_T_AL;
8495 else if (can_ce && result_empty)
8496 type = USE_T_CE;
8497 else if (*T_DL != NUL && result_empty)
8498 type = USE_T_DL;
8499 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
8500 type = USE_T_SR;
8501 else
8502 return FAIL;
8505 * For clearing the lines screen_del_lines() is used. This will also take
8506 * care of t_db if necessary.
8508 if (type == USE_T_CD || type == USE_T_CDL ||
8509 type == USE_T_CE || type == USE_T_DL)
8510 return screen_del_lines(off, row, line_count, end, FALSE, wp);
8513 * If text is retained below the screen, first clear or delete as many
8514 * lines at the bottom of the window as are about to be inserted so that
8515 * the deleted lines won't later surface during a screen_del_lines.
8517 if (*T_DB)
8518 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
8520 #ifdef FEAT_CLIPBOARD
8521 /* Remove a modeless selection when inserting lines halfway the screen
8522 * or not the full width of the screen. */
8523 if (off + row > 0
8524 # ifdef FEAT_VERTSPLIT
8525 || (wp != NULL && wp->w_width != Columns)
8526 # endif
8528 clip_clear_selection();
8529 else
8530 clip_scroll_selection(-line_count);
8531 #endif
8533 #ifdef FEAT_GUI
8534 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
8535 * scrolling is actually carried out. */
8536 gui_dont_update_cursor();
8537 #endif
8539 if (*T_CCS != NUL) /* cursor relative to region */
8540 cursor_row = row;
8541 else
8542 cursor_row = row + off;
8545 * Shift LineOffset[] line_count down to reflect the inserted lines.
8546 * Clear the inserted lines in ScreenLines[].
8548 row += off;
8549 end += off;
8550 for (i = 0; i < line_count; ++i)
8552 #ifdef FEAT_VERTSPLIT
8553 if (wp != NULL && wp->w_width != Columns)
8555 /* need to copy part of a line */
8556 j = end - 1 - i;
8557 while ((j -= line_count) >= row)
8558 linecopy(j + line_count, j, wp);
8559 j += line_count;
8560 if (can_clear((char_u *)" "))
8561 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
8562 else
8563 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
8564 LineWraps[j] = FALSE;
8566 else
8567 #endif
8569 j = end - 1 - i;
8570 temp = LineOffset[j];
8571 while ((j -= line_count) >= row)
8573 LineOffset[j + line_count] = LineOffset[j];
8574 LineWraps[j + line_count] = LineWraps[j];
8576 LineOffset[j + line_count] = temp;
8577 LineWraps[j + line_count] = FALSE;
8578 if (can_clear((char_u *)" "))
8579 lineclear(temp, (int)Columns);
8580 else
8581 lineinvalid(temp, (int)Columns);
8585 screen_stop_highlight();
8586 windgoto(cursor_row, 0);
8588 #ifdef FEAT_VERTSPLIT
8589 /* redraw the characters */
8590 if (type == USE_REDRAW)
8591 redraw_block(row, end, wp);
8592 else
8593 #endif
8594 if (type == USE_T_CAL)
8596 term_append_lines(line_count);
8597 screen_start(); /* don't know where cursor is now */
8599 else
8601 for (i = 0; i < line_count; i++)
8603 if (type == USE_T_AL)
8605 if (i && cursor_row != 0)
8606 windgoto(cursor_row, 0);
8607 out_str(T_AL);
8609 else /* type == USE_T_SR */
8610 out_str(T_SR);
8611 screen_start(); /* don't know where cursor is now */
8616 * With scroll-reverse and 'da' flag set we need to clear the lines that
8617 * have been scrolled down into the region.
8619 if (type == USE_T_SR && *T_DA)
8621 for (i = 0; i < line_count; ++i)
8623 windgoto(off + i, 0);
8624 out_str(T_CE);
8625 screen_start(); /* don't know where cursor is now */
8629 #ifdef FEAT_GUI
8630 gui_can_update_cursor();
8631 if (gui.in_use)
8632 out_flush(); /* always flush after a scroll */
8633 #endif
8634 return OK;
8638 * delete lines on the screen and update ScreenLines[]
8639 * 'end' is the line after the scrolled part. Normally it is Rows.
8640 * When scrolling region used 'off' is the offset from the top for the region.
8641 * 'row' and 'end' are relative to the start of the region.
8643 * Return OK for success, FAIL if the lines are not deleted.
8645 /*ARGSUSED*/
8647 screen_del_lines(off, row, line_count, end, force, wp)
8648 int off;
8649 int row;
8650 int line_count;
8651 int end;
8652 int force; /* even when line_count > p_ttyscroll */
8653 win_T *wp; /* NULL or window to use width from */
8655 int j;
8656 int i;
8657 unsigned temp;
8658 int cursor_row;
8659 int cursor_end;
8660 int result_empty; /* result is empty until end of region */
8661 int can_delete; /* deleting line codes can be used */
8662 int type;
8665 * FAIL if
8666 * - there is no valid screen
8667 * - the screen has to be redrawn completely
8668 * - the line count is less than one
8669 * - the line count is more than 'ttyscroll'
8671 if (!screen_valid(TRUE) || line_count <= 0 ||
8672 (!force && line_count > p_ttyscroll))
8673 return FAIL;
8676 * Check if the rest of the current region will become empty.
8678 result_empty = row + line_count >= end;
8681 * We can delete lines only when 'db' flag not set or when 'ce' option
8682 * available.
8684 can_delete = (*T_DB == NUL || can_clear(T_CE));
8687 * There are six ways to delete lines:
8688 * 0. When in a vertically split window and t_CV isn't set, redraw the
8689 * characters from ScreenLines[].
8690 * 1. Use T_CD if it exists and the result is empty.
8691 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
8692 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
8693 * none of the other ways work.
8694 * 4. Use T_CE (erase line) if the result is empty.
8695 * 5. Use T_DL (delete line) if it exists.
8696 * 6. redraw the characters from ScreenLines[].
8698 #ifdef FEAT_VERTSPLIT
8699 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
8700 type = USE_REDRAW;
8701 else
8702 #endif
8703 if (can_clear(T_CD) && result_empty)
8704 type = USE_T_CD;
8705 #if defined(__BEOS__) && defined(BEOS_DR8)
8707 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
8708 * its internal termcap... this works okay for tests which test *T_DB !=
8709 * NUL. It has the disadvantage that the user cannot use any :set t_*
8710 * command to get T_DB (back) to empty_option, only :set term=... will do
8711 * the trick...
8712 * Anyway, this hack will hopefully go away with the next OS release.
8713 * (Olaf Seibert)
8715 else if (row == 0 && T_DB == empty_option
8716 && (line_count == 1 || *T_CDL == NUL))
8717 #else
8718 else if (row == 0 && (
8719 #ifndef AMIGA
8720 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
8721 * up, so use delete-line command */
8722 line_count == 1 ||
8723 #endif
8724 *T_CDL == NUL))
8725 #endif
8726 type = USE_NL;
8727 else if (*T_CDL != NUL && line_count > 1 && can_delete)
8728 type = USE_T_CDL;
8729 else if (can_clear(T_CE) && result_empty
8730 #ifdef FEAT_VERTSPLIT
8731 && (wp == NULL || wp->w_width == Columns)
8732 #endif
8734 type = USE_T_CE;
8735 else if (*T_DL != NUL && can_delete)
8736 type = USE_T_DL;
8737 else if (*T_CDL != NUL && can_delete)
8738 type = USE_T_CDL;
8739 else
8740 return FAIL;
8742 #ifdef FEAT_CLIPBOARD
8743 /* Remove a modeless selection when deleting lines halfway the screen or
8744 * not the full width of the screen. */
8745 if (off + row > 0
8746 # ifdef FEAT_VERTSPLIT
8747 || (wp != NULL && wp->w_width != Columns)
8748 # endif
8750 clip_clear_selection();
8751 else
8752 clip_scroll_selection(line_count);
8753 #endif
8755 #ifdef FEAT_GUI
8756 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
8757 * scrolling is actually carried out. */
8758 gui_dont_update_cursor();
8759 #endif
8761 if (*T_CCS != NUL) /* cursor relative to region */
8763 cursor_row = row;
8764 cursor_end = end;
8766 else
8768 cursor_row = row + off;
8769 cursor_end = end + off;
8773 * Now shift LineOffset[] line_count up to reflect the deleted lines.
8774 * Clear the inserted lines in ScreenLines[].
8776 row += off;
8777 end += off;
8778 for (i = 0; i < line_count; ++i)
8780 #ifdef FEAT_VERTSPLIT
8781 if (wp != NULL && wp->w_width != Columns)
8783 /* need to copy part of a line */
8784 j = row + i;
8785 while ((j += line_count) <= end - 1)
8786 linecopy(j - line_count, j, wp);
8787 j -= line_count;
8788 if (can_clear((char_u *)" "))
8789 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
8790 else
8791 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
8792 LineWraps[j] = FALSE;
8794 else
8795 #endif
8797 /* whole width, moving the line pointers is faster */
8798 j = row + i;
8799 temp = LineOffset[j];
8800 while ((j += line_count) <= end - 1)
8802 LineOffset[j - line_count] = LineOffset[j];
8803 LineWraps[j - line_count] = LineWraps[j];
8805 LineOffset[j - line_count] = temp;
8806 LineWraps[j - line_count] = FALSE;
8807 if (can_clear((char_u *)" "))
8808 lineclear(temp, (int)Columns);
8809 else
8810 lineinvalid(temp, (int)Columns);
8814 screen_stop_highlight();
8816 #ifdef FEAT_VERTSPLIT
8817 /* redraw the characters */
8818 if (type == USE_REDRAW)
8819 redraw_block(row, end, wp);
8820 else
8821 #endif
8822 if (type == USE_T_CD) /* delete the lines */
8824 windgoto(cursor_row, 0);
8825 out_str(T_CD);
8826 screen_start(); /* don't know where cursor is now */
8828 else if (type == USE_T_CDL)
8830 windgoto(cursor_row, 0);
8831 term_delete_lines(line_count);
8832 screen_start(); /* don't know where cursor is now */
8835 * Deleting lines at top of the screen or scroll region: Just scroll
8836 * the whole screen (scroll region) up by outputting newlines on the
8837 * last line.
8839 else if (type == USE_NL)
8841 windgoto(cursor_end - 1, 0);
8842 for (i = line_count; --i >= 0; )
8843 out_char('\n'); /* cursor will remain on same line */
8845 else
8847 for (i = line_count; --i >= 0; )
8849 if (type == USE_T_DL)
8851 windgoto(cursor_row, 0);
8852 out_str(T_DL); /* delete a line */
8854 else /* type == USE_T_CE */
8856 windgoto(cursor_row + i, 0);
8857 out_str(T_CE); /* erase a line */
8859 screen_start(); /* don't know where cursor is now */
8864 * If the 'db' flag is set, we need to clear the lines that have been
8865 * scrolled up at the bottom of the region.
8867 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
8869 for (i = line_count; i > 0; --i)
8871 windgoto(cursor_end - i, 0);
8872 out_str(T_CE); /* erase a line */
8873 screen_start(); /* don't know where cursor is now */
8877 #ifdef FEAT_GUI
8878 gui_can_update_cursor();
8879 if (gui.in_use)
8880 out_flush(); /* always flush after a scroll */
8881 #endif
8883 return OK;
8887 * show the current mode and ruler
8889 * If clear_cmdline is TRUE, clear the rest of the cmdline.
8890 * If clear_cmdline is FALSE there may be a message there that needs to be
8891 * cleared only if a mode is shown.
8892 * Return the length of the message (0 if no message).
8895 showmode()
8897 int need_clear;
8898 int length = 0;
8899 int do_mode;
8900 int attr;
8901 int nwr_save;
8902 #ifdef FEAT_INS_EXPAND
8903 int sub_attr;
8904 #endif
8906 do_mode = ((p_smd && msg_silent == 0)
8907 && ((State & INSERT)
8908 || restart_edit
8909 #ifdef FEAT_VISUAL
8910 || VIsual_active
8911 #endif
8913 if (do_mode || Recording)
8916 * Don't show mode right now, when not redrawing or inside a mapping.
8917 * Call char_avail() only when we are going to show something, because
8918 * it takes a bit of time.
8920 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
8922 redraw_cmdline = TRUE; /* show mode later */
8923 return 0;
8926 nwr_save = need_wait_return;
8928 /* wait a bit before overwriting an important message */
8929 check_for_delay(FALSE);
8931 /* if the cmdline is more than one line high, erase top lines */
8932 need_clear = clear_cmdline;
8933 if (clear_cmdline && cmdline_row < Rows - 1)
8934 msg_clr_cmdline(); /* will reset clear_cmdline */
8936 /* Position on the last line in the window, column 0 */
8937 msg_pos_mode();
8938 cursor_off();
8939 attr = hl_attr(HLF_CM); /* Highlight mode */
8940 if (do_mode)
8942 MSG_PUTS_ATTR("--", attr);
8943 #if defined(FEAT_XIM)
8944 # if 0 /* old version, changed by SungHyun Nam July 2008 */
8945 if (xic != NULL && im_get_status() && !p_imdisable
8946 && curbuf->b_p_iminsert == B_IMODE_IM)
8947 # else
8948 if (
8949 # ifdef HAVE_GTK2
8950 preedit_get_status()
8951 # else
8952 im_get_status()
8953 # endif
8955 # endif
8956 # ifdef HAVE_GTK2 /* most of the time, it's not XIM being used */
8957 MSG_PUTS_ATTR(" IM", attr);
8958 # else
8959 MSG_PUTS_ATTR(" XIM", attr);
8960 # endif
8961 #endif
8962 #if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
8963 if (gui.in_use)
8965 if (hangul_input_state_get())
8966 MSG_PUTS_ATTR(" \307\321\261\333", attr); /* HANGUL */
8968 #endif
8969 #ifdef FEAT_INS_EXPAND
8970 if (edit_submode != NULL) /* CTRL-X in Insert mode */
8972 /* These messages can get long, avoid a wrap in a narrow
8973 * window. Prefer showing edit_submode_extra. */
8974 length = (Rows - msg_row) * Columns - 3;
8975 if (edit_submode_extra != NULL)
8976 length -= vim_strsize(edit_submode_extra);
8977 if (length > 0)
8979 if (edit_submode_pre != NULL)
8980 length -= vim_strsize(edit_submode_pre);
8981 if (length - vim_strsize(edit_submode) > 0)
8983 if (edit_submode_pre != NULL)
8984 msg_puts_attr(edit_submode_pre, attr);
8985 msg_puts_attr(edit_submode, attr);
8987 if (edit_submode_extra != NULL)
8989 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
8990 if ((int)edit_submode_highl < (int)HLF_COUNT)
8991 sub_attr = hl_attr(edit_submode_highl);
8992 else
8993 sub_attr = attr;
8994 msg_puts_attr(edit_submode_extra, sub_attr);
8997 length = 0;
8999 else
9000 #endif
9002 #ifdef FEAT_VREPLACE
9003 if (State & VREPLACE_FLAG)
9004 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
9005 else
9006 #endif
9007 if (State & REPLACE_FLAG)
9008 MSG_PUTS_ATTR(_(" REPLACE"), attr);
9009 else if (State & INSERT)
9011 #ifdef FEAT_RIGHTLEFT
9012 if (p_ri)
9013 MSG_PUTS_ATTR(_(" REVERSE"), attr);
9014 #endif
9015 MSG_PUTS_ATTR(_(" INSERT"), attr);
9017 else if (restart_edit == 'I')
9018 MSG_PUTS_ATTR(_(" (insert)"), attr);
9019 else if (restart_edit == 'R')
9020 MSG_PUTS_ATTR(_(" (replace)"), attr);
9021 else if (restart_edit == 'V')
9022 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
9023 #ifdef FEAT_RIGHTLEFT
9024 if (p_hkmap)
9025 MSG_PUTS_ATTR(_(" Hebrew"), attr);
9026 # ifdef FEAT_FKMAP
9027 if (p_fkmap)
9028 MSG_PUTS_ATTR(farsi_text_5, attr);
9029 # endif
9030 #endif
9031 #ifdef FEAT_KEYMAP
9032 if (State & LANGMAP)
9034 # ifdef FEAT_ARABIC
9035 if (curwin->w_p_arab)
9036 MSG_PUTS_ATTR(_(" Arabic"), attr);
9037 else
9038 # endif
9039 MSG_PUTS_ATTR(_(" (lang)"), attr);
9041 #endif
9042 if ((State & INSERT) && p_paste)
9043 MSG_PUTS_ATTR(_(" (paste)"), attr);
9045 #ifdef FEAT_VISUAL
9046 if (VIsual_active)
9048 char *p;
9050 /* Don't concatenate separate words to avoid translation
9051 * problems. */
9052 switch ((VIsual_select ? 4 : 0)
9053 + (VIsual_mode == Ctrl_V) * 2
9054 + (VIsual_mode == 'V'))
9056 case 0: p = N_(" VISUAL"); break;
9057 case 1: p = N_(" VISUAL LINE"); break;
9058 case 2: p = N_(" VISUAL BLOCK"); break;
9059 case 4: p = N_(" SELECT"); break;
9060 case 5: p = N_(" SELECT LINE"); break;
9061 default: p = N_(" SELECT BLOCK"); break;
9063 MSG_PUTS_ATTR(_(p), attr);
9065 #endif
9066 MSG_PUTS_ATTR(" --", attr);
9069 need_clear = TRUE;
9071 if (Recording
9072 #ifdef FEAT_INS_EXPAND
9073 && edit_submode == NULL /* otherwise it gets too long */
9074 #endif
9077 MSG_PUTS_ATTR(_("recording"), attr);
9078 need_clear = TRUE;
9081 mode_displayed = TRUE;
9082 if (need_clear || clear_cmdline)
9083 msg_clr_eos();
9084 msg_didout = FALSE; /* overwrite this message */
9085 length = msg_col;
9086 msg_col = 0;
9087 need_wait_return = nwr_save; /* never ask for hit-return for this */
9089 else if (clear_cmdline && msg_silent == 0)
9090 /* Clear the whole command line. Will reset "clear_cmdline". */
9091 msg_clr_cmdline();
9093 #ifdef FEAT_CMDL_INFO
9094 # ifdef FEAT_VISUAL
9095 /* In Visual mode the size of the selected area must be redrawn. */
9096 if (VIsual_active)
9097 clear_showcmd();
9098 # endif
9100 /* If the last window has no status line, the ruler is after the mode
9101 * message and must be redrawn */
9102 if (redrawing()
9103 # ifdef FEAT_WINDOWS
9104 && lastwin->w_status_height == 0
9105 # endif
9107 win_redr_ruler(lastwin, TRUE);
9108 #endif
9109 redraw_cmdline = FALSE;
9110 clear_cmdline = FALSE;
9112 return length;
9116 * Position for a mode message.
9118 static void
9119 msg_pos_mode()
9121 msg_col = 0;
9122 msg_row = Rows - 1;
9126 * Delete mode message. Used when ESC is typed which is expected to end
9127 * Insert mode (but Insert mode didn't end yet!).
9128 * Caller should check "mode_displayed".
9130 void
9131 unshowmode(force)
9132 int force;
9135 * Don't delete it right now, when not redrawing or insided a mapping.
9137 if (!redrawing() || (!force && char_avail() && !KeyTyped))
9138 redraw_cmdline = TRUE; /* delete mode later */
9139 else
9141 msg_pos_mode();
9142 if (Recording)
9143 MSG_PUTS_ATTR(_("recording"), hl_attr(HLF_CM));
9144 msg_clr_eos();
9148 #if defined(FEAT_WINDOWS)
9150 * Draw the tab pages line at the top of the Vim window.
9152 static void
9153 draw_tabline()
9155 int tabcount = 0;
9156 tabpage_T *tp;
9157 int tabwidth;
9158 int col = 0;
9159 int scol = 0;
9160 int attr;
9161 win_T *wp;
9162 win_T *cwp;
9163 int wincount;
9164 int modified;
9165 int c;
9166 int len;
9167 int attr_sel = hl_attr(HLF_TPS);
9168 int attr_nosel = hl_attr(HLF_TP);
9169 int attr_fill = hl_attr(HLF_TPF);
9170 char_u *p;
9171 int room;
9172 int use_sep_chars = (t_colors < 8
9173 #ifdef FEAT_GUI
9174 && !gui.in_use
9175 #endif
9178 redraw_tabline = FALSE;
9180 #ifdef FEAT_GUI_TABLINE
9181 /* Take care of a GUI tabline. */
9182 if (gui_use_tabline())
9184 gui_update_tabline();
9185 return;
9187 #endif
9189 if (tabline_height() < 1)
9190 return;
9192 #if defined(FEAT_STL_OPT)
9194 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
9195 for (scol = 0; scol < Columns; ++scol)
9196 TabPageIdxs[scol] = 0;
9198 /* Use the 'tabline' option if it's set. */
9199 if (*p_tal != NUL)
9201 int save_called_emsg = called_emsg;
9203 /* Check for an error. If there is one we would loop in redrawing the
9204 * screen. Avoid that by making 'tabline' empty. */
9205 called_emsg = FALSE;
9206 win_redr_custom(NULL, FALSE);
9207 if (called_emsg)
9208 set_string_option_direct((char_u *)"tabline", -1,
9209 (char_u *)"", OPT_FREE, SID_ERROR);
9210 called_emsg |= save_called_emsg;
9212 else
9213 #endif
9215 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
9216 ++tabcount;
9218 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
9219 if (tabwidth < 6)
9220 tabwidth = 6;
9222 attr = attr_nosel;
9223 tabcount = 0;
9224 scol = 0;
9225 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
9226 tp = tp->tp_next)
9228 scol = col;
9230 if (tp->tp_topframe == topframe)
9231 attr = attr_sel;
9232 if (use_sep_chars && col > 0)
9233 screen_putchar('|', 0, col++, attr);
9235 if (tp->tp_topframe != topframe)
9236 attr = attr_nosel;
9238 screen_putchar(' ', 0, col++, attr);
9240 if (tp == curtab)
9242 cwp = curwin;
9243 wp = firstwin;
9245 else
9247 cwp = tp->tp_curwin;
9248 wp = tp->tp_firstwin;
9251 modified = FALSE;
9252 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
9253 if (bufIsChanged(wp->w_buffer))
9254 modified = TRUE;
9255 if (modified || wincount > 1)
9257 if (wincount > 1)
9259 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
9260 len = (int)STRLEN(NameBuff);
9261 if (col + len >= Columns - 3)
9262 break;
9263 screen_puts_len(NameBuff, len, 0, col,
9264 #if defined(FEAT_SYN_HL)
9265 hl_combine_attr(attr, hl_attr(HLF_T))
9266 #else
9267 attr
9268 #endif
9270 col += len;
9272 if (modified)
9273 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
9274 screen_putchar(' ', 0, col++, attr);
9277 room = scol - col + tabwidth - 1;
9278 if (room > 0)
9280 /* Get buffer name in NameBuff[] */
9281 get_trans_bufname(cwp->w_buffer);
9282 shorten_dir(NameBuff);
9283 len = vim_strsize(NameBuff);
9284 p = NameBuff;
9285 #ifdef FEAT_MBYTE
9286 if (has_mbyte)
9287 while (len > room)
9289 len -= ptr2cells(p);
9290 mb_ptr_adv(p);
9292 else
9293 #endif
9294 if (len > room)
9296 p += len - room;
9297 len = room;
9299 if (len > Columns - col - 1)
9300 len = Columns - col - 1;
9302 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
9303 col += len;
9305 screen_putchar(' ', 0, col++, attr);
9307 /* Store the tab page number in TabPageIdxs[], so that
9308 * jump_to_mouse() knows where each one is. */
9309 ++tabcount;
9310 while (scol < col)
9311 TabPageIdxs[scol++] = tabcount;
9314 if (use_sep_chars)
9315 c = '_';
9316 else
9317 c = ' ';
9318 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
9320 /* Put an "X" for closing the current tab if there are several. */
9321 if (first_tabpage->tp_next != NULL)
9323 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
9324 TabPageIdxs[Columns - 1] = -999;
9328 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
9329 * set. */
9330 redraw_tabline = FALSE;
9334 * Get buffer name for "buf" into NameBuff[].
9335 * Takes care of special buffer names and translates special characters.
9337 void
9338 get_trans_bufname(buf)
9339 buf_T *buf;
9341 if (buf_spname(buf) != NULL)
9342 STRCPY(NameBuff, buf_spname(buf));
9343 else
9344 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
9345 trans_characters(NameBuff, MAXPATHL);
9347 #endif
9349 #if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
9351 * Get the character to use in a status line. Get its attributes in "*attr".
9353 static int
9354 fillchar_status(attr, is_curwin)
9355 int *attr;
9356 int is_curwin;
9358 int fill;
9359 if (is_curwin)
9361 *attr = hl_attr(HLF_S);
9362 fill = fill_stl;
9364 else
9366 *attr = hl_attr(HLF_SNC);
9367 fill = fill_stlnc;
9369 /* Use fill when there is highlighting, and highlighting of current
9370 * window differs, or the fillchars differ, or this is not the
9371 * current window */
9372 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
9373 || !is_curwin || firstwin == lastwin)
9374 || (fill_stl != fill_stlnc)))
9375 return fill;
9376 if (is_curwin)
9377 return '^';
9378 return '=';
9380 #endif
9382 #ifdef FEAT_VERTSPLIT
9384 * Get the character to use in a separator between vertically split windows.
9385 * Get its attributes in "*attr".
9387 static int
9388 fillchar_vsep(attr)
9389 int *attr;
9391 *attr = hl_attr(HLF_C);
9392 if (*attr == 0 && fill_vert == ' ')
9393 return '|';
9394 else
9395 return fill_vert;
9397 #endif
9400 * Return TRUE if redrawing should currently be done.
9403 redrawing()
9405 return (!RedrawingDisabled
9406 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
9410 * Return TRUE if printing messages should currently be done.
9413 messaging()
9415 return (!(p_lz && char_avail() && !KeyTyped));
9419 * Show current status info in ruler and various other places
9420 * If always is FALSE, only show ruler if position has changed.
9422 void
9423 showruler(always)
9424 int always;
9426 if (!always && !redrawing())
9427 return;
9428 #ifdef FEAT_INS_EXPAND
9429 if (pum_visible())
9431 # ifdef FEAT_WINDOWS
9432 /* Don't redraw right now, do it later. */
9433 curwin->w_redr_status = TRUE;
9434 # endif
9435 return;
9437 #endif
9438 #if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
9439 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
9441 redraw_custum_statusline(curwin);
9443 else
9444 #endif
9445 #ifdef FEAT_CMDL_INFO
9446 win_redr_ruler(curwin, always);
9447 #endif
9449 #ifdef FEAT_TITLE
9450 if (need_maketitle
9451 # ifdef FEAT_STL_OPT
9452 || (p_icon && (stl_syntax & STL_IN_ICON))
9453 || (p_title && (stl_syntax & STL_IN_TITLE))
9454 # endif
9456 maketitle();
9457 #endif
9458 #ifdef FEAT_WINDOWS
9459 /* Redraw the tab pages line if needed. */
9460 if (redraw_tabline)
9461 draw_tabline();
9462 #endif
9465 #ifdef FEAT_CMDL_INFO
9466 static void
9467 win_redr_ruler(wp, always)
9468 win_T *wp;
9469 int always;
9471 char_u buffer[70];
9472 int row;
9473 int fillchar;
9474 int attr;
9475 int empty_line = FALSE;
9476 colnr_T virtcol;
9477 int i;
9478 int o;
9479 #ifdef FEAT_VERTSPLIT
9480 int this_ru_col;
9481 int off = 0;
9482 int width = Columns;
9483 # define WITH_OFF(x) x
9484 # define WITH_WIDTH(x) x
9485 #else
9486 # define WITH_OFF(x) 0
9487 # define WITH_WIDTH(x) Columns
9488 # define this_ru_col ru_col
9489 #endif
9491 /* If 'ruler' off or redrawing disabled, don't do anything */
9492 if (!p_ru)
9493 return;
9496 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
9497 * after deleting lines, before cursor.lnum is corrected.
9499 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
9500 return;
9502 #ifdef FEAT_INS_EXPAND
9503 /* Don't draw the ruler while doing insert-completion, it might overwrite
9504 * the (long) mode message. */
9505 # ifdef FEAT_WINDOWS
9506 if (wp == lastwin && lastwin->w_status_height == 0)
9507 # endif
9508 if (edit_submode != NULL)
9509 return;
9510 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
9511 if (pum_visible())
9512 return;
9513 #endif
9515 #ifdef FEAT_STL_OPT
9516 if (*p_ruf)
9518 int save_called_emsg = called_emsg;
9520 called_emsg = FALSE;
9521 win_redr_custom(wp, TRUE);
9522 if (called_emsg)
9523 set_string_option_direct((char_u *)"rulerformat", -1,
9524 (char_u *)"", OPT_FREE, SID_ERROR);
9525 called_emsg |= save_called_emsg;
9526 return;
9528 #endif
9531 * Check if not in Insert mode and the line is empty (will show "0-1").
9533 if (!(State & INSERT)
9534 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
9535 empty_line = TRUE;
9538 * Only draw the ruler when something changed.
9540 validate_virtcol_win(wp);
9541 if ( redraw_cmdline
9542 || always
9543 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
9544 || wp->w_cursor.col != wp->w_ru_cursor.col
9545 || wp->w_virtcol != wp->w_ru_virtcol
9546 #ifdef FEAT_VIRTUALEDIT
9547 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
9548 #endif
9549 || wp->w_topline != wp->w_ru_topline
9550 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
9551 #ifdef FEAT_DIFF
9552 || wp->w_topfill != wp->w_ru_topfill
9553 #endif
9554 || empty_line != wp->w_ru_empty)
9556 cursor_off();
9557 #ifdef FEAT_WINDOWS
9558 if (wp->w_status_height)
9560 row = W_WINROW(wp) + wp->w_height;
9561 fillchar = fillchar_status(&attr, wp == curwin);
9562 # ifdef FEAT_VERTSPLIT
9563 off = W_WINCOL(wp);
9564 width = W_WIDTH(wp);
9565 # endif
9567 else
9568 #endif
9570 row = Rows - 1;
9571 fillchar = ' ';
9572 attr = 0;
9573 #ifdef FEAT_VERTSPLIT
9574 width = Columns;
9575 off = 0;
9576 #endif
9579 /* In list mode virtcol needs to be recomputed */
9580 virtcol = wp->w_virtcol;
9581 if (wp->w_p_list && lcs_tab1 == NUL)
9583 wp->w_p_list = FALSE;
9584 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
9585 wp->w_p_list = TRUE;
9589 * Some sprintfs return the length, some return a pointer.
9590 * To avoid portability problems we use strlen() here.
9592 sprintf((char *)buffer, "%ld,",
9593 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
9594 ? 0L
9595 : (long)(wp->w_cursor.lnum));
9596 col_print(buffer + STRLEN(buffer),
9597 empty_line ? 0 : (int)wp->w_cursor.col + 1,
9598 (int)virtcol + 1);
9601 * Add a "50%" if there is room for it.
9602 * On the last line, don't print in the last column (scrolls the
9603 * screen up on some terminals).
9605 i = (int)STRLEN(buffer);
9606 get_rel_pos(wp, buffer + i + 1);
9607 o = i + vim_strsize(buffer + i + 1);
9608 #ifdef FEAT_WINDOWS
9609 if (wp->w_status_height == 0) /* can't use last char of screen */
9610 #endif
9611 ++o;
9612 #ifdef FEAT_VERTSPLIT
9613 this_ru_col = ru_col - (Columns - width);
9614 if (this_ru_col < 0)
9615 this_ru_col = 0;
9616 #endif
9617 /* Never use more than half the window/screen width, leave the other
9618 * half for the filename. */
9619 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
9620 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
9621 if (this_ru_col + o < WITH_WIDTH(width))
9623 while (this_ru_col + o < WITH_WIDTH(width))
9625 #ifdef FEAT_MBYTE
9626 if (has_mbyte)
9627 i += (*mb_char2bytes)(fillchar, buffer + i);
9628 else
9629 #endif
9630 buffer[i++] = fillchar;
9631 ++o;
9633 get_rel_pos(wp, buffer + i);
9635 /* Truncate at window boundary. */
9636 #ifdef FEAT_MBYTE
9637 if (has_mbyte)
9639 o = 0;
9640 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
9642 o += (*mb_ptr2cells)(buffer + i);
9643 if (this_ru_col + o > WITH_WIDTH(width))
9645 buffer[i] = NUL;
9646 break;
9650 else
9651 #endif
9652 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
9653 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
9655 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
9656 i = redraw_cmdline;
9657 screen_fill(row, row + 1,
9658 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
9659 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
9660 fillchar, fillchar, attr);
9661 /* don't redraw the cmdline because of showing the ruler */
9662 redraw_cmdline = i;
9663 wp->w_ru_cursor = wp->w_cursor;
9664 wp->w_ru_virtcol = wp->w_virtcol;
9665 wp->w_ru_empty = empty_line;
9666 wp->w_ru_topline = wp->w_topline;
9667 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
9668 #ifdef FEAT_DIFF
9669 wp->w_ru_topfill = wp->w_topfill;
9670 #endif
9673 #endif
9675 #if defined(FEAT_LINEBREAK) || defined(PROTO)
9677 * Return the width of the 'number' and 'relativenumber' column.
9678 * Caller may need to check if 'number' or 'relativenumber' is set.
9679 * Otherwise it depends on 'numberwidth' and the line count.
9682 number_width(wp)
9683 win_T *wp;
9685 int n;
9686 linenr_T lnum;
9688 if (wp->w_p_nu)
9689 /* 'number' */
9690 lnum = wp->w_buffer->b_ml.ml_line_count;
9691 else
9692 /* 'relativenumber' */
9693 lnum = wp->w_height;
9695 if (lnum == wp->w_nrwidth_line_count)
9696 return wp->w_nrwidth_width;
9697 wp->w_nrwidth_line_count = lnum;
9699 n = 0;
9702 lnum /= 10;
9703 ++n;
9704 } while (lnum > 0);
9706 /* 'numberwidth' gives the minimal width plus one */
9707 if (n < wp->w_p_nuw - 1)
9708 n = wp->w_p_nuw - 1;
9710 wp->w_nrwidth_width = n;
9711 return n;
9713 #endif