Merge commit 'git-svn' into vim
[vim_extended.git] / src / screen.c
blob684c3942d48aebda10af4925addd26cb21ef3750
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' column changes. */
424 if (curwin->w_redr_type < NOT_VALID
425 && curwin->w_nrwidth != (curwin->w_p_nu ? number_width(curwin) : 0))
426 curwin->w_redr_type = NOT_VALID;
427 #endif
430 * Only start redrawing if there is really something to do.
432 if (type == INVERTED)
433 update_curswant();
434 if (curwin->w_redr_type < type
435 && !((type == VALID
436 && curwin->w_lines[0].wl_valid
437 #ifdef FEAT_DIFF
438 && curwin->w_topfill == curwin->w_old_topfill
439 && curwin->w_botfill == curwin->w_old_botfill
440 #endif
441 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
442 #ifdef FEAT_VISUAL
443 || (type == INVERTED
444 && VIsual_active
445 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
446 && curwin->w_old_visual_mode == VIsual_mode
447 && (curwin->w_valid & VALID_VIRTCOL)
448 && curwin->w_old_curswant == curwin->w_curswant)
449 #endif
451 curwin->w_redr_type = type;
453 #ifdef FEAT_WINDOWS
454 /* Redraw the tab pages line if needed. */
455 if (redraw_tabline || type >= NOT_VALID)
456 draw_tabline();
457 #endif
459 #ifdef FEAT_SYN_HL
461 * Correct stored syntax highlighting info for changes in each displayed
462 * buffer. Each buffer must only be done once.
464 FOR_ALL_WINDOWS(wp)
466 if (wp->w_buffer->b_mod_set)
468 # ifdef FEAT_WINDOWS
469 win_T *wwp;
471 /* Check if we already did this buffer. */
472 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
473 if (wwp->w_buffer == wp->w_buffer)
474 break;
475 # endif
476 if (
477 # ifdef FEAT_WINDOWS
478 wwp == wp &&
479 # endif
480 syntax_present(wp->w_buffer))
481 syn_stack_apply_changes(wp->w_buffer);
484 #endif
487 * Go from top to bottom through the windows, redrawing the ones that need
488 * it.
490 #if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
491 did_one = FALSE;
492 #endif
493 #ifdef FEAT_SEARCH_EXTRA
494 search_hl.rm.regprog = NULL;
495 #endif
496 FOR_ALL_WINDOWS(wp)
498 if (wp->w_redr_type != 0)
500 cursor_off();
501 #if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
502 if (!did_one)
504 did_one = TRUE;
505 # ifdef FEAT_SEARCH_EXTRA
506 start_search_hl();
507 # endif
508 # ifdef FEAT_CLIPBOARD
509 /* When Visual area changed, may have to update selection. */
510 if (clip_star.available && clip_isautosel())
511 clip_update_selection();
512 # endif
513 #ifdef FEAT_GUI
514 /* Remove the cursor before starting to do anything, because
515 * scrolling may make it difficult to redraw the text under
516 * it. */
517 if (gui.in_use)
518 gui_undraw_cursor();
519 #endif
521 #endif
522 win_update(wp);
525 #ifdef FEAT_WINDOWS
526 /* redraw status line after the window to minimize cursor movement */
527 if (wp->w_redr_status)
529 cursor_off();
530 win_redr_status(wp);
532 #endif
534 #if defined(FEAT_SEARCH_EXTRA)
535 end_search_hl();
536 #endif
538 #ifdef FEAT_WINDOWS
539 /* Reset b_mod_set flags. Going through all windows is probably faster
540 * than going through all buffers (there could be many buffers). */
541 for (wp = firstwin; wp != NULL; wp = wp->w_next)
542 wp->w_buffer->b_mod_set = FALSE;
543 #else
544 curbuf->b_mod_set = FALSE;
545 #endif
547 updating_screen = FALSE;
548 #ifdef FEAT_GUI
549 gui_may_resize_shell();
550 #endif
552 /* Clear or redraw the command line. Done last, because scrolling may
553 * mess up the command line. */
554 if (clear_cmdline || redraw_cmdline)
555 showmode();
557 /* May put up an introductory message when not editing a file */
558 if (!did_intro && bufempty()
559 && curbuf->b_fname == NULL
560 #ifdef FEAT_WINDOWS
561 && firstwin->w_next == NULL
562 #endif
563 && vim_strchr(p_shm, SHM_INTRO) == NULL)
564 intro_message(FALSE);
565 did_intro = TRUE;
567 #ifdef FEAT_GUI
568 /* Redraw the cursor and update the scrollbars when all screen updating is
569 * done. */
570 if (gui.in_use)
572 out_flush(); /* required before updating the cursor */
573 if (did_one)
574 gui_update_cursor(FALSE, FALSE);
575 gui_update_scrollbars(FALSE);
577 #endif
580 #if defined(FEAT_SIGNS) || defined(FEAT_GUI)
581 static void update_prepare __ARGS((void));
582 static void update_finish __ARGS((void));
585 * Prepare for updating one or more windows.
587 static void
588 update_prepare()
590 cursor_off();
591 updating_screen = TRUE;
592 #ifdef FEAT_GUI
593 /* Remove the cursor before starting to do anything, because scrolling may
594 * make it difficult to redraw the text under it. */
595 if (gui.in_use)
596 gui_undraw_cursor();
597 #endif
598 #ifdef FEAT_SEARCH_EXTRA
599 start_search_hl();
600 #endif
604 * Finish updating one or more windows.
606 static void
607 update_finish()
609 if (redraw_cmdline)
610 showmode();
612 # ifdef FEAT_SEARCH_EXTRA
613 end_search_hl();
614 # endif
616 updating_screen = FALSE;
618 # ifdef FEAT_GUI
619 gui_may_resize_shell();
621 /* Redraw the cursor and update the scrollbars when all screen updating is
622 * done. */
623 if (gui.in_use)
625 out_flush(); /* required before updating the cursor */
626 gui_update_cursor(FALSE, FALSE);
627 gui_update_scrollbars(FALSE);
629 # endif
631 #endif
633 #if defined(FEAT_SIGNS) || defined(PROTO)
634 void
635 update_debug_sign(buf, lnum)
636 buf_T *buf;
637 linenr_T lnum;
639 win_T *wp;
640 int doit = FALSE;
642 # ifdef FEAT_FOLDING
643 win_foldinfo.fi_level = 0;
644 # endif
646 /* update/delete a specific mark */
647 FOR_ALL_WINDOWS(wp)
649 if (buf != NULL && lnum > 0)
651 if (wp->w_buffer == buf && lnum >= wp->w_topline
652 && lnum < wp->w_botline)
654 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
655 wp->w_redraw_top = lnum;
656 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
657 wp->w_redraw_bot = lnum;
658 redraw_win_later(wp, VALID);
661 else
662 redraw_win_later(wp, VALID);
663 if (wp->w_redr_type != 0)
664 doit = TRUE;
667 if (!doit)
668 return;
670 /* update all windows that need updating */
671 update_prepare();
673 # ifdef FEAT_WINDOWS
674 for (wp = firstwin; wp; wp = wp->w_next)
676 if (wp->w_redr_type != 0)
677 win_update(wp);
678 if (wp->w_redr_status)
679 win_redr_status(wp);
681 # else
682 if (curwin->w_redr_type != 0)
683 win_update(curwin);
684 # endif
686 update_finish();
688 #endif
691 #if defined(FEAT_GUI) || defined(PROTO)
693 * Update a single window, its status line and maybe the command line msg.
694 * Used for the GUI scrollbar.
696 void
697 updateWindow(wp)
698 win_T *wp;
700 update_prepare();
702 #ifdef FEAT_CLIPBOARD
703 /* When Visual area changed, may have to update selection. */
704 if (clip_star.available && clip_isautosel())
705 clip_update_selection();
706 #endif
708 win_update(wp);
710 #ifdef FEAT_WINDOWS
711 /* When the screen was cleared redraw the tab pages line. */
712 if (redraw_tabline)
713 draw_tabline();
715 if (wp->w_redr_status
716 # ifdef FEAT_CMDL_INFO
717 || p_ru
718 # endif
719 # ifdef FEAT_STL_OPT
720 || *p_stl != NUL || *wp->w_p_stl != NUL
721 # endif
723 win_redr_status(wp);
724 #endif
726 update_finish();
728 #endif
731 * Update a single window.
733 * This may cause the windows below it also to be redrawn (when clearing the
734 * screen or scrolling lines).
736 * How the window is redrawn depends on wp->w_redr_type. Each type also
737 * implies the one below it.
738 * NOT_VALID redraw the whole window
739 * SOME_VALID redraw the whole window but do scroll when possible
740 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
741 * INVERTED redraw the changed part of the Visual area
742 * INVERTED_ALL redraw the whole Visual area
743 * VALID 1. scroll up/down to adjust for a changed w_topline
744 * 2. update lines at the top when scrolled down
745 * 3. redraw changed text:
746 * - if wp->w_buffer->b_mod_set set, update lines between
747 * b_mod_top and b_mod_bot.
748 * - if wp->w_redraw_top non-zero, redraw lines between
749 * wp->w_redraw_top and wp->w_redr_bot.
750 * - continue redrawing when syntax status is invalid.
751 * 4. if scrolled up, update lines at the bottom.
752 * This results in three areas that may need updating:
753 * top: from first row to top_end (when scrolled down)
754 * mid: from mid_start to mid_end (update inversion or changed text)
755 * bot: from bot_start to last row (when scrolled up)
757 static void
758 win_update(wp)
759 win_T *wp;
761 buf_T *buf = wp->w_buffer;
762 int type;
763 int top_end = 0; /* Below last row of the top area that needs
764 updating. 0 when no top area updating. */
765 int mid_start = 999;/* first row of the mid area that needs
766 updating. 999 when no mid area updating. */
767 int mid_end = 0; /* Below last row of the mid area that needs
768 updating. 0 when no mid area updating. */
769 int bot_start = 999;/* first row of the bot area that needs
770 updating. 999 when no bot area updating */
771 #ifdef FEAT_VISUAL
772 int scrolled_down = FALSE; /* TRUE when scrolled down when
773 w_topline got smaller a bit */
774 #endif
775 #ifdef FEAT_SEARCH_EXTRA
776 matchitem_T *cur; /* points to the match list */
777 int top_to_mod = FALSE; /* redraw above mod_top */
778 #endif
780 int row; /* current window row to display */
781 linenr_T lnum; /* current buffer lnum to display */
782 int idx; /* current index in w_lines[] */
783 int srow; /* starting row of the current line */
785 int eof = FALSE; /* if TRUE, we hit the end of the file */
786 int didline = FALSE; /* if TRUE, we finished the last line */
787 int i;
788 long j;
789 static int recursive = FALSE; /* being called recursively */
790 int old_botline = wp->w_botline;
791 #ifdef FEAT_FOLDING
792 long fold_count;
793 #endif
794 #ifdef FEAT_SYN_HL
795 /* remember what happened to the previous line, to know if
796 * check_visual_highlight() can be used */
797 #define DID_NONE 1 /* didn't update a line */
798 #define DID_LINE 2 /* updated a normal line */
799 #define DID_FOLD 3 /* updated a folded line */
800 int did_update = DID_NONE;
801 linenr_T syntax_last_parsed = 0; /* last parsed text line */
802 #endif
803 linenr_T mod_top = 0;
804 linenr_T mod_bot = 0;
805 #if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
806 int save_got_int;
807 #endif
809 type = wp->w_redr_type;
811 if (type == NOT_VALID)
813 #ifdef FEAT_WINDOWS
814 wp->w_redr_status = TRUE;
815 #endif
816 wp->w_lines_valid = 0;
819 /* Window is zero-height: nothing to draw. */
820 if (wp->w_height == 0)
822 wp->w_redr_type = 0;
823 return;
826 #ifdef FEAT_VERTSPLIT
827 /* Window is zero-width: Only need to draw the separator. */
828 if (wp->w_width == 0)
830 /* draw the vertical separator right of this window */
831 draw_vsep_win(wp, 0);
832 wp->w_redr_type = 0;
833 return;
835 #endif
837 #ifdef FEAT_SEARCH_EXTRA
838 /* Setup for match and 'hlsearch' highlighting. Disable any previous
839 * match */
840 cur = wp->w_match_head;
841 while (cur != NULL)
843 cur->hl.rm = cur->match;
844 if (cur->hlg_id == 0)
845 cur->hl.attr = 0;
846 else
847 cur->hl.attr = syn_id2attr(cur->hlg_id);
848 cur->hl.buf = buf;
849 cur->hl.lnum = 0;
850 cur->hl.first_lnum = 0;
851 # ifdef FEAT_RELTIME
852 /* Set the time limit to 'redrawtime'. */
853 profile_setlimit(p_rdt, &(cur->hl.tm));
854 # endif
855 cur = cur->next;
857 search_hl.buf = buf;
858 search_hl.lnum = 0;
859 search_hl.first_lnum = 0;
860 /* time limit is set at the toplevel, for all windows */
861 #endif
863 #ifdef FEAT_LINEBREAK
864 /* Force redraw when width of 'number' column changes. */
865 i = wp->w_p_nu ? number_width(wp) : 0;
866 if (wp->w_nrwidth != i)
868 type = NOT_VALID;
869 wp->w_nrwidth = i;
871 else
872 #endif
874 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
877 * When there are both inserted/deleted lines and specific lines to be
878 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
879 * everything (only happens when redrawing is off for while).
881 type = NOT_VALID;
883 else
886 * Set mod_top to the first line that needs displaying because of
887 * changes. Set mod_bot to the first line after the changes.
889 mod_top = wp->w_redraw_top;
890 if (wp->w_redraw_bot != 0)
891 mod_bot = wp->w_redraw_bot + 1;
892 else
893 mod_bot = 0;
894 wp->w_redraw_top = 0; /* reset for next time */
895 wp->w_redraw_bot = 0;
896 if (buf->b_mod_set)
898 if (mod_top == 0 || mod_top > buf->b_mod_top)
900 mod_top = buf->b_mod_top;
901 #ifdef FEAT_SYN_HL
902 /* Need to redraw lines above the change that may be included
903 * in a pattern match. */
904 if (syntax_present(buf))
906 mod_top -= buf->b_syn_sync_linebreaks;
907 if (mod_top < 1)
908 mod_top = 1;
910 #endif
912 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
913 mod_bot = buf->b_mod_bot;
915 #ifdef FEAT_SEARCH_EXTRA
916 /* When 'hlsearch' is on and using a multi-line search pattern, a
917 * change in one line may make the Search highlighting in a
918 * previous line invalid. Simple solution: redraw all visible
919 * lines above the change.
920 * Same for a match pattern.
922 if (search_hl.rm.regprog != NULL
923 && re_multiline(search_hl.rm.regprog))
924 top_to_mod = TRUE;
925 else
927 cur = wp->w_match_head;
928 while (cur != NULL)
930 if (cur->match.regprog != NULL
931 && re_multiline(cur->match.regprog))
933 top_to_mod = TRUE;
934 break;
936 cur = cur->next;
939 #endif
941 #ifdef FEAT_FOLDING
942 if (mod_top != 0 && hasAnyFolding(wp))
944 linenr_T lnumt, lnumb;
947 * A change in a line can cause lines above it to become folded or
948 * unfolded. Find the top most buffer line that may be affected.
949 * If the line was previously folded and displayed, get the first
950 * line of that fold. If the line is folded now, get the first
951 * folded line. Use the minimum of these two.
954 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
955 * the line below it. If there is no valid entry, use w_topline.
956 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
957 * to this line. If there is no valid entry, use MAXLNUM. */
958 lnumt = wp->w_topline;
959 lnumb = MAXLNUM;
960 for (i = 0; i < wp->w_lines_valid; ++i)
961 if (wp->w_lines[i].wl_valid)
963 if (wp->w_lines[i].wl_lastlnum < mod_top)
964 lnumt = wp->w_lines[i].wl_lastlnum + 1;
965 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
967 lnumb = wp->w_lines[i].wl_lnum;
968 /* When there is a fold column it might need updating
969 * in the next line ("J" just above an open fold). */
970 if (wp->w_p_fdc > 0)
971 ++lnumb;
975 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
976 if (mod_top > lnumt)
977 mod_top = lnumt;
979 /* Now do the same for the bottom line (one above mod_bot). */
980 --mod_bot;
981 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
982 ++mod_bot;
983 if (mod_bot < lnumb)
984 mod_bot = lnumb;
986 #endif
988 /* When a change starts above w_topline and the end is below
989 * w_topline, start redrawing at w_topline.
990 * If the end of the change is above w_topline: do like no change was
991 * made, but redraw the first line to find changes in syntax. */
992 if (mod_top != 0 && mod_top < wp->w_topline)
994 if (mod_bot > wp->w_topline)
995 mod_top = wp->w_topline;
996 #ifdef FEAT_SYN_HL
997 else if (syntax_present(buf))
998 top_end = 1;
999 #endif
1002 /* When line numbers are displayed need to redraw all lines below
1003 * inserted/deleted lines. */
1004 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1005 mod_bot = MAXLNUM;
1009 * When only displaying the lines at the top, set top_end. Used when
1010 * window has scrolled down for msg_scrolled.
1012 if (type == REDRAW_TOP)
1014 j = 0;
1015 for (i = 0; i < wp->w_lines_valid; ++i)
1017 j += wp->w_lines[i].wl_size;
1018 if (j >= wp->w_upd_rows)
1020 top_end = j;
1021 break;
1024 if (top_end == 0)
1025 /* not found (cannot happen?): redraw everything */
1026 type = NOT_VALID;
1027 else
1028 /* top area defined, the rest is VALID */
1029 type = VALID;
1032 /* Trick: we want to avoid clearing the screen twice. screenclear() will
1033 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1034 * non-zero and thus not FALSE) will indicate that screenclear() was not
1035 * called. */
1036 if (screen_cleared)
1037 screen_cleared = MAYBE;
1040 * If there are no changes on the screen that require a complete redraw,
1041 * handle three cases:
1042 * 1: we are off the top of the screen by a few lines: scroll down
1043 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1044 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1045 * w_lines[] that needs updating.
1047 if ((type == VALID || type == SOME_VALID
1048 || type == INVERTED || type == INVERTED_ALL)
1049 #ifdef FEAT_DIFF
1050 && !wp->w_botfill && !wp->w_old_botfill
1051 #endif
1054 if (mod_top != 0 && wp->w_topline == mod_top)
1057 * w_topline is the first changed line, the scrolling will be done
1058 * further down.
1061 else if (wp->w_lines[0].wl_valid
1062 && (wp->w_topline < wp->w_lines[0].wl_lnum
1063 #ifdef FEAT_DIFF
1064 || (wp->w_topline == wp->w_lines[0].wl_lnum
1065 && wp->w_topfill > wp->w_old_topfill)
1066 #endif
1070 * New topline is above old topline: May scroll down.
1072 #ifdef FEAT_FOLDING
1073 if (hasAnyFolding(wp))
1075 linenr_T ln;
1077 /* count the number of lines we are off, counting a sequence
1078 * of folded lines as one */
1079 j = 0;
1080 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1082 ++j;
1083 if (j >= wp->w_height - 2)
1084 break;
1085 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1088 else
1089 #endif
1090 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1091 if (j < wp->w_height - 2) /* not too far off */
1093 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1094 #ifdef FEAT_DIFF
1095 /* insert extra lines for previously invisible filler lines */
1096 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1097 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1098 - wp->w_old_topfill;
1099 #endif
1100 if (i < wp->w_height - 2) /* less than a screen off */
1103 * Try to insert the correct number of lines.
1104 * If not the last window, delete the lines at the bottom.
1105 * win_ins_lines may fail when the terminal can't do it.
1107 if (i > 0)
1108 check_for_delay(FALSE);
1109 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1111 if (wp->w_lines_valid != 0)
1113 /* Need to update rows that are new, stop at the
1114 * first one that scrolled down. */
1115 top_end = i;
1116 #ifdef FEAT_VISUAL
1117 scrolled_down = TRUE;
1118 #endif
1120 /* Move the entries that were scrolled, disable
1121 * the entries for the lines to be redrawn. */
1122 if ((wp->w_lines_valid += j) > wp->w_height)
1123 wp->w_lines_valid = wp->w_height;
1124 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1125 wp->w_lines[idx] = wp->w_lines[idx - j];
1126 while (idx >= 0)
1127 wp->w_lines[idx--].wl_valid = FALSE;
1130 else
1131 mid_start = 0; /* redraw all lines */
1133 else
1134 mid_start = 0; /* redraw all lines */
1136 else
1137 mid_start = 0; /* redraw all lines */
1139 else
1142 * New topline is at or below old topline: May scroll up.
1143 * When topline didn't change, find first entry in w_lines[] that
1144 * needs updating.
1147 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1148 j = -1;
1149 row = 0;
1150 for (i = 0; i < wp->w_lines_valid; i++)
1152 if (wp->w_lines[i].wl_valid
1153 && wp->w_lines[i].wl_lnum == wp->w_topline)
1155 j = i;
1156 break;
1158 row += wp->w_lines[i].wl_size;
1160 if (j == -1)
1162 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1163 * lines */
1164 mid_start = 0;
1166 else
1169 * Try to delete the correct number of lines.
1170 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1172 #ifdef FEAT_DIFF
1173 /* If the topline didn't change, delete old filler lines,
1174 * otherwise delete filler lines of the new topline... */
1175 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1176 row += wp->w_old_topfill;
1177 else
1178 row += diff_check_fill(wp, wp->w_topline);
1179 /* ... but don't delete new filler lines. */
1180 row -= wp->w_topfill;
1181 #endif
1182 if (row > 0)
1184 check_for_delay(FALSE);
1185 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1186 bot_start = wp->w_height - row;
1187 else
1188 mid_start = 0; /* redraw all lines */
1190 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1193 * Skip the lines (below the deleted lines) that are still
1194 * valid and don't need redrawing. Copy their info
1195 * upwards, to compensate for the deleted lines. Set
1196 * bot_start to the first row that needs redrawing.
1198 bot_start = 0;
1199 idx = 0;
1200 for (;;)
1202 wp->w_lines[idx] = wp->w_lines[j];
1203 /* stop at line that didn't fit, unless it is still
1204 * valid (no lines deleted) */
1205 if (row > 0 && bot_start + row
1206 + (int)wp->w_lines[j].wl_size > wp->w_height)
1208 wp->w_lines_valid = idx + 1;
1209 break;
1211 bot_start += wp->w_lines[idx++].wl_size;
1213 /* stop at the last valid entry in w_lines[].wl_size */
1214 if (++j >= wp->w_lines_valid)
1216 wp->w_lines_valid = idx;
1217 break;
1220 #ifdef FEAT_DIFF
1221 /* Correct the first entry for filler lines at the top
1222 * when it won't get updated below. */
1223 if (wp->w_p_diff && bot_start > 0)
1224 wp->w_lines[0].wl_size =
1225 plines_win_nofill(wp, wp->w_topline, TRUE)
1226 + wp->w_topfill;
1227 #endif
1232 /* When starting redraw in the first line, redraw all lines. When
1233 * there is only one window it's probably faster to clear the screen
1234 * first. */
1235 if (mid_start == 0)
1237 mid_end = wp->w_height;
1238 if (lastwin == firstwin)
1240 /* Clear the screen when it was not done by win_del_lines() or
1241 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1242 * then. */
1243 if (screen_cleared != TRUE)
1244 screenclear();
1245 #ifdef FEAT_WINDOWS
1246 /* The screen was cleared, redraw the tab pages line. */
1247 if (redraw_tabline)
1248 draw_tabline();
1249 #endif
1253 /* When win_del_lines() or win_ins_lines() caused the screen to be
1254 * cleared (only happens for the first window) or when screenclear()
1255 * was called directly above, "must_redraw" will have been set to
1256 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1257 if (screen_cleared == TRUE)
1258 must_redraw = 0;
1260 else
1262 /* Not VALID or INVERTED: redraw all lines. */
1263 mid_start = 0;
1264 mid_end = wp->w_height;
1267 if (type == SOME_VALID)
1269 /* SOME_VALID: redraw all lines. */
1270 mid_start = 0;
1271 mid_end = wp->w_height;
1272 type = NOT_VALID;
1275 #ifdef FEAT_VISUAL
1276 /* check if we are updating or removing the inverted part */
1277 if ((VIsual_active && buf == curwin->w_buffer)
1278 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1280 linenr_T from, to;
1282 if (VIsual_active)
1284 if (VIsual_active
1285 && (VIsual_mode != wp->w_old_visual_mode
1286 || type == INVERTED_ALL))
1289 * If the type of Visual selection changed, redraw the whole
1290 * selection. Also when the ownership of the X selection is
1291 * gained or lost.
1293 if (curwin->w_cursor.lnum < VIsual.lnum)
1295 from = curwin->w_cursor.lnum;
1296 to = VIsual.lnum;
1298 else
1300 from = VIsual.lnum;
1301 to = curwin->w_cursor.lnum;
1303 /* redraw more when the cursor moved as well */
1304 if (wp->w_old_cursor_lnum < from)
1305 from = wp->w_old_cursor_lnum;
1306 if (wp->w_old_cursor_lnum > to)
1307 to = wp->w_old_cursor_lnum;
1308 if (wp->w_old_visual_lnum < from)
1309 from = wp->w_old_visual_lnum;
1310 if (wp->w_old_visual_lnum > to)
1311 to = wp->w_old_visual_lnum;
1313 else
1316 * Find the line numbers that need to be updated: The lines
1317 * between the old cursor position and the current cursor
1318 * position. Also check if the Visual position changed.
1320 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1322 from = curwin->w_cursor.lnum;
1323 to = wp->w_old_cursor_lnum;
1325 else
1327 from = wp->w_old_cursor_lnum;
1328 to = curwin->w_cursor.lnum;
1329 if (from == 0) /* Visual mode just started */
1330 from = to;
1333 if (VIsual.lnum != wp->w_old_visual_lnum
1334 || VIsual.col != wp->w_old_visual_col)
1336 if (wp->w_old_visual_lnum < from
1337 && wp->w_old_visual_lnum != 0)
1338 from = wp->w_old_visual_lnum;
1339 if (wp->w_old_visual_lnum > to)
1340 to = wp->w_old_visual_lnum;
1341 if (VIsual.lnum < from)
1342 from = VIsual.lnum;
1343 if (VIsual.lnum > to)
1344 to = VIsual.lnum;
1349 * If in block mode and changed column or curwin->w_curswant:
1350 * update all lines.
1351 * First compute the actual start and end column.
1353 if (VIsual_mode == Ctrl_V)
1355 colnr_T fromc, toc;
1357 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
1358 ++toc;
1359 if (curwin->w_curswant == MAXCOL)
1360 toc = MAXCOL;
1362 if (fromc != wp->w_old_cursor_fcol
1363 || toc != wp->w_old_cursor_lcol)
1365 if (from > VIsual.lnum)
1366 from = VIsual.lnum;
1367 if (to < VIsual.lnum)
1368 to = VIsual.lnum;
1370 wp->w_old_cursor_fcol = fromc;
1371 wp->w_old_cursor_lcol = toc;
1374 else
1376 /* Use the line numbers of the old Visual area. */
1377 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1379 from = wp->w_old_cursor_lnum;
1380 to = wp->w_old_visual_lnum;
1382 else
1384 from = wp->w_old_visual_lnum;
1385 to = wp->w_old_cursor_lnum;
1390 * There is no need to update lines above the top of the window.
1392 if (from < wp->w_topline)
1393 from = wp->w_topline;
1396 * If we know the value of w_botline, use it to restrict the update to
1397 * the lines that are visible in the window.
1399 if (wp->w_valid & VALID_BOTLINE)
1401 if (from >= wp->w_botline)
1402 from = wp->w_botline - 1;
1403 if (to >= wp->w_botline)
1404 to = wp->w_botline - 1;
1408 * Find the minimal part to be updated.
1409 * Watch out for scrolling that made entries in w_lines[] invalid.
1410 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1411 * top_end; need to redraw from top_end to the "to" line.
1412 * A middle mouse click with a Visual selection may change the text
1413 * above the Visual area and reset wl_valid, do count these for
1414 * mid_end (in srow).
1416 if (mid_start > 0)
1418 lnum = wp->w_topline;
1419 idx = 0;
1420 srow = 0;
1421 if (scrolled_down)
1422 mid_start = top_end;
1423 else
1424 mid_start = 0;
1425 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1427 if (wp->w_lines[idx].wl_valid)
1428 mid_start += wp->w_lines[idx].wl_size;
1429 else if (!scrolled_down)
1430 srow += wp->w_lines[idx].wl_size;
1431 ++idx;
1432 # ifdef FEAT_FOLDING
1433 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1434 lnum = wp->w_lines[idx].wl_lnum;
1435 else
1436 # endif
1437 ++lnum;
1439 srow += mid_start;
1440 mid_end = wp->w_height;
1441 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1443 if (wp->w_lines[idx].wl_valid
1444 && wp->w_lines[idx].wl_lnum >= to + 1)
1446 /* Only update until first row of this line */
1447 mid_end = srow;
1448 break;
1450 srow += wp->w_lines[idx].wl_size;
1455 if (VIsual_active && buf == curwin->w_buffer)
1457 wp->w_old_visual_mode = VIsual_mode;
1458 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1459 wp->w_old_visual_lnum = VIsual.lnum;
1460 wp->w_old_visual_col = VIsual.col;
1461 wp->w_old_curswant = curwin->w_curswant;
1463 else
1465 wp->w_old_visual_mode = 0;
1466 wp->w_old_cursor_lnum = 0;
1467 wp->w_old_visual_lnum = 0;
1468 wp->w_old_visual_col = 0;
1470 #endif /* FEAT_VISUAL */
1472 #if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1473 /* reset got_int, otherwise regexp won't work */
1474 save_got_int = got_int;
1475 got_int = 0;
1476 #endif
1477 #ifdef FEAT_FOLDING
1478 win_foldinfo.fi_level = 0;
1479 #endif
1482 * Update all the window rows.
1484 idx = 0; /* first entry in w_lines[].wl_size */
1485 row = 0;
1486 srow = 0;
1487 lnum = wp->w_topline; /* first line shown in window */
1488 for (;;)
1490 /* stop updating when reached the end of the window (check for _past_
1491 * the end of the window is at the end of the loop) */
1492 if (row == wp->w_height)
1494 didline = TRUE;
1495 break;
1498 /* stop updating when hit the end of the file */
1499 if (lnum > buf->b_ml.ml_line_count)
1501 eof = TRUE;
1502 break;
1505 /* Remember the starting row of the line that is going to be dealt
1506 * with. It is used further down when the line doesn't fit. */
1507 srow = row;
1510 * Update a line when it is in an area that needs updating, when it
1511 * has changes or w_lines[idx] is invalid.
1512 * bot_start may be halfway a wrapped line after using
1513 * win_del_lines(), check if the current line includes it.
1514 * When syntax folding is being used, the saved syntax states will
1515 * already have been updated, we can't see where the syntax state is
1516 * the same again, just update until the end of the window.
1518 if (row < top_end
1519 || (row >= mid_start && row < mid_end)
1520 #ifdef FEAT_SEARCH_EXTRA
1521 || top_to_mod
1522 #endif
1523 || idx >= wp->w_lines_valid
1524 || (row + wp->w_lines[idx].wl_size > bot_start)
1525 || (mod_top != 0
1526 && (lnum == mod_top
1527 || (lnum >= mod_top
1528 && (lnum < mod_bot
1529 #ifdef FEAT_SYN_HL
1530 || did_update == DID_FOLD
1531 || (did_update == DID_LINE
1532 && syntax_present(buf)
1533 && (
1534 # ifdef FEAT_FOLDING
1535 (foldmethodIsSyntax(wp)
1536 && hasAnyFolding(wp)) ||
1537 # endif
1538 syntax_check_changed(lnum)))
1539 #endif
1540 )))))
1542 #ifdef FEAT_SEARCH_EXTRA
1543 if (lnum == mod_top)
1544 top_to_mod = FALSE;
1545 #endif
1548 * When at start of changed lines: May scroll following lines
1549 * up or down to minimize redrawing.
1550 * Don't do this when the change continues until the end.
1551 * Don't scroll when dollar_vcol is non-zero, keep the "$".
1553 if (lnum == mod_top
1554 && mod_bot != MAXLNUM
1555 && !(dollar_vcol != 0 && mod_bot == mod_top + 1))
1557 int old_rows = 0;
1558 int new_rows = 0;
1559 int xtra_rows;
1560 linenr_T l;
1562 /* Count the old number of window rows, using w_lines[], which
1563 * should still contain the sizes for the lines as they are
1564 * currently displayed. */
1565 for (i = idx; i < wp->w_lines_valid; ++i)
1567 /* Only valid lines have a meaningful wl_lnum. Invalid
1568 * lines are part of the changed area. */
1569 if (wp->w_lines[i].wl_valid
1570 && wp->w_lines[i].wl_lnum == mod_bot)
1571 break;
1572 old_rows += wp->w_lines[i].wl_size;
1573 #ifdef FEAT_FOLDING
1574 if (wp->w_lines[i].wl_valid
1575 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1577 /* Must have found the last valid entry above mod_bot.
1578 * Add following invalid entries. */
1579 ++i;
1580 while (i < wp->w_lines_valid
1581 && !wp->w_lines[i].wl_valid)
1582 old_rows += wp->w_lines[i++].wl_size;
1583 break;
1585 #endif
1588 if (i >= wp->w_lines_valid)
1590 /* We can't find a valid line below the changed lines,
1591 * need to redraw until the end of the window.
1592 * Inserting/deleting lines has no use. */
1593 bot_start = 0;
1595 else
1597 /* Able to count old number of rows: Count new window
1598 * rows, and may insert/delete lines */
1599 j = idx;
1600 for (l = lnum; l < mod_bot; ++l)
1602 #ifdef FEAT_FOLDING
1603 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1604 ++new_rows;
1605 else
1606 #endif
1607 #ifdef FEAT_DIFF
1608 if (l == wp->w_topline)
1609 new_rows += plines_win_nofill(wp, l, TRUE)
1610 + wp->w_topfill;
1611 else
1612 #endif
1613 new_rows += plines_win(wp, l, TRUE);
1614 ++j;
1615 if (new_rows > wp->w_height - row - 2)
1617 /* it's getting too much, must redraw the rest */
1618 new_rows = 9999;
1619 break;
1622 xtra_rows = new_rows - old_rows;
1623 if (xtra_rows < 0)
1625 /* May scroll text up. If there is not enough
1626 * remaining text or scrolling fails, must redraw the
1627 * rest. If scrolling works, must redraw the text
1628 * below the scrolled text. */
1629 if (row - xtra_rows >= wp->w_height - 2)
1630 mod_bot = MAXLNUM;
1631 else
1633 check_for_delay(FALSE);
1634 if (win_del_lines(wp, row,
1635 -xtra_rows, FALSE, FALSE) == FAIL)
1636 mod_bot = MAXLNUM;
1637 else
1638 bot_start = wp->w_height + xtra_rows;
1641 else if (xtra_rows > 0)
1643 /* May scroll text down. If there is not enough
1644 * remaining text of scrolling fails, must redraw the
1645 * rest. */
1646 if (row + xtra_rows >= wp->w_height - 2)
1647 mod_bot = MAXLNUM;
1648 else
1650 check_for_delay(FALSE);
1651 if (win_ins_lines(wp, row + old_rows,
1652 xtra_rows, FALSE, FALSE) == FAIL)
1653 mod_bot = MAXLNUM;
1654 else if (top_end > row + old_rows)
1655 /* Scrolled the part at the top that requires
1656 * updating down. */
1657 top_end += xtra_rows;
1661 /* When not updating the rest, may need to move w_lines[]
1662 * entries. */
1663 if (mod_bot != MAXLNUM && i != j)
1665 if (j < i)
1667 int x = row + new_rows;
1669 /* move entries in w_lines[] upwards */
1670 for (;;)
1672 /* stop at last valid entry in w_lines[] */
1673 if (i >= wp->w_lines_valid)
1675 wp->w_lines_valid = j;
1676 break;
1678 wp->w_lines[j] = wp->w_lines[i];
1679 /* stop at a line that won't fit */
1680 if (x + (int)wp->w_lines[j].wl_size
1681 > wp->w_height)
1683 wp->w_lines_valid = j + 1;
1684 break;
1686 x += wp->w_lines[j++].wl_size;
1687 ++i;
1689 if (bot_start > x)
1690 bot_start = x;
1692 else /* j > i */
1694 /* move entries in w_lines[] downwards */
1695 j -= i;
1696 wp->w_lines_valid += j;
1697 if (wp->w_lines_valid > wp->w_height)
1698 wp->w_lines_valid = wp->w_height;
1699 for (i = wp->w_lines_valid; i - j >= idx; --i)
1700 wp->w_lines[i] = wp->w_lines[i - j];
1702 /* The w_lines[] entries for inserted lines are
1703 * now invalid, but wl_size may be used above.
1704 * Reset to zero. */
1705 while (i >= idx)
1707 wp->w_lines[i].wl_size = 0;
1708 wp->w_lines[i--].wl_valid = FALSE;
1715 #ifdef FEAT_FOLDING
1717 * When lines are folded, display one line for all of them.
1718 * Otherwise, display normally (can be several display lines when
1719 * 'wrap' is on).
1721 fold_count = foldedCount(wp, lnum, &win_foldinfo);
1722 if (fold_count != 0)
1724 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
1725 ++row;
1726 --fold_count;
1727 wp->w_lines[idx].wl_folded = TRUE;
1728 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
1729 # ifdef FEAT_SYN_HL
1730 did_update = DID_FOLD;
1731 # endif
1733 else
1734 #endif
1735 if (idx < wp->w_lines_valid
1736 && wp->w_lines[idx].wl_valid
1737 && wp->w_lines[idx].wl_lnum == lnum
1738 && lnum > wp->w_topline
1739 && !(dy_flags & DY_LASTLINE)
1740 && srow + wp->w_lines[idx].wl_size > wp->w_height
1741 #ifdef FEAT_DIFF
1742 && diff_check_fill(wp, lnum) == 0
1743 #endif
1746 /* This line is not going to fit. Don't draw anything here,
1747 * will draw "@ " lines below. */
1748 row = wp->w_height + 1;
1750 else
1752 #ifdef FEAT_SEARCH_EXTRA
1753 prepare_search_hl(wp, lnum);
1754 #endif
1755 #ifdef FEAT_SYN_HL
1756 /* Let the syntax stuff know we skipped a few lines. */
1757 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
1758 && syntax_present(buf))
1759 syntax_end_parsing(syntax_last_parsed + 1);
1760 #endif
1763 * Display one line.
1765 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
1767 #ifdef FEAT_FOLDING
1768 wp->w_lines[idx].wl_folded = FALSE;
1769 wp->w_lines[idx].wl_lastlnum = lnum;
1770 #endif
1771 #ifdef FEAT_SYN_HL
1772 did_update = DID_LINE;
1773 syntax_last_parsed = lnum;
1774 #endif
1777 wp->w_lines[idx].wl_lnum = lnum;
1778 wp->w_lines[idx].wl_valid = TRUE;
1779 if (row > wp->w_height) /* past end of screen */
1781 /* we may need the size of that too long line later on */
1782 if (dollar_vcol == 0)
1783 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
1784 ++idx;
1785 break;
1787 if (dollar_vcol == 0)
1788 wp->w_lines[idx].wl_size = row - srow;
1789 ++idx;
1790 #ifdef FEAT_FOLDING
1791 lnum += fold_count + 1;
1792 #else
1793 ++lnum;
1794 #endif
1796 else
1798 /* This line does not need updating, advance to the next one */
1799 row += wp->w_lines[idx++].wl_size;
1800 if (row > wp->w_height) /* past end of screen */
1801 break;
1802 #ifdef FEAT_FOLDING
1803 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
1804 #else
1805 ++lnum;
1806 #endif
1807 #ifdef FEAT_SYN_HL
1808 did_update = DID_NONE;
1809 #endif
1812 if (lnum > buf->b_ml.ml_line_count)
1814 eof = TRUE;
1815 break;
1819 * End of loop over all window lines.
1823 if (idx > wp->w_lines_valid)
1824 wp->w_lines_valid = idx;
1826 #ifdef FEAT_SYN_HL
1828 * Let the syntax stuff know we stop parsing here.
1830 if (syntax_last_parsed != 0 && syntax_present(buf))
1831 syntax_end_parsing(syntax_last_parsed + 1);
1832 #endif
1835 * If we didn't hit the end of the file, and we didn't finish the last
1836 * line we were working on, then the line didn't fit.
1838 wp->w_empty_rows = 0;
1839 #ifdef FEAT_DIFF
1840 wp->w_filler_rows = 0;
1841 #endif
1842 if (!eof && !didline)
1844 if (lnum == wp->w_topline)
1847 * Single line that does not fit!
1848 * Don't overwrite it, it can be edited.
1850 wp->w_botline = lnum + 1;
1852 #ifdef FEAT_DIFF
1853 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
1855 /* Window ends in filler lines. */
1856 wp->w_botline = lnum;
1857 wp->w_filler_rows = wp->w_height - srow;
1859 #endif
1860 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
1863 * Last line isn't finished: Display "@@@" at the end.
1865 screen_fill(W_WINROW(wp) + wp->w_height - 1,
1866 W_WINROW(wp) + wp->w_height,
1867 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
1868 '@', '@', hl_attr(HLF_AT));
1869 set_empty_rows(wp, srow);
1870 wp->w_botline = lnum;
1872 else
1874 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
1875 wp->w_botline = lnum;
1878 else
1880 #ifdef FEAT_VERTSPLIT
1881 draw_vsep_win(wp, row);
1882 #endif
1883 if (eof) /* we hit the end of the file */
1885 wp->w_botline = buf->b_ml.ml_line_count + 1;
1886 #ifdef FEAT_DIFF
1887 j = diff_check_fill(wp, wp->w_botline);
1888 if (j > 0 && !wp->w_botfill)
1891 * Display filler lines at the end of the file
1893 if (char2cells(fill_diff) > 1)
1894 i = '-';
1895 else
1896 i = fill_diff;
1897 if (row + j > wp->w_height)
1898 j = wp->w_height - row;
1899 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
1900 row += j;
1902 #endif
1904 else if (dollar_vcol == 0)
1905 wp->w_botline = lnum;
1907 /* make sure the rest of the screen is blank */
1908 /* put '~'s on rows that aren't part of the file. */
1909 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_AT);
1912 /* Reset the type of redrawing required, the window has been updated. */
1913 wp->w_redr_type = 0;
1914 #ifdef FEAT_DIFF
1915 wp->w_old_topfill = wp->w_topfill;
1916 wp->w_old_botfill = wp->w_botfill;
1917 #endif
1919 if (dollar_vcol == 0)
1922 * There is a trick with w_botline. If we invalidate it on each
1923 * change that might modify it, this will cause a lot of expensive
1924 * calls to plines() in update_topline() each time. Therefore the
1925 * value of w_botline is often approximated, and this value is used to
1926 * compute the value of w_topline. If the value of w_botline was
1927 * wrong, check that the value of w_topline is correct (cursor is on
1928 * the visible part of the text). If it's not, we need to redraw
1929 * again. Mostly this just means scrolling up a few lines, so it
1930 * doesn't look too bad. Only do this for the current window (where
1931 * changes are relevant).
1933 wp->w_valid |= VALID_BOTLINE;
1934 if (wp == curwin && wp->w_botline != old_botline && !recursive)
1936 recursive = TRUE;
1937 curwin->w_valid &= ~VALID_TOPLINE;
1938 update_topline(); /* may invalidate w_botline again */
1939 if (must_redraw != 0)
1941 /* Don't update for changes in buffer again. */
1942 i = curbuf->b_mod_set;
1943 curbuf->b_mod_set = FALSE;
1944 win_update(curwin);
1945 must_redraw = 0;
1946 curbuf->b_mod_set = i;
1948 recursive = FALSE;
1952 #if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1953 /* restore got_int, unless CTRL-C was hit while redrawing */
1954 if (!got_int)
1955 got_int = save_got_int;
1956 #endif
1959 #ifdef FEAT_SIGNS
1960 static int draw_signcolumn __ARGS((win_T *wp));
1963 * Return TRUE when window "wp" has a column to draw signs in.
1965 static int
1966 draw_signcolumn(wp)
1967 win_T *wp;
1969 return (wp->w_buffer->b_signlist != NULL
1970 # ifdef FEAT_NETBEANS_INTG
1971 || usingNetbeans
1972 # endif
1975 #endif
1978 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
1979 * as the filler character.
1981 static void
1982 win_draw_end(wp, c1, c2, row, endrow, hl)
1983 win_T *wp;
1984 int c1;
1985 int c2;
1986 int row;
1987 int endrow;
1988 hlf_T hl;
1990 #if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
1991 int n = 0;
1992 # define FDC_OFF n
1993 #else
1994 # define FDC_OFF 0
1995 #endif
1997 #ifdef FEAT_RIGHTLEFT
1998 if (wp->w_p_rl)
2000 /* No check for cmdline window: should never be right-left. */
2001 # ifdef FEAT_FOLDING
2002 n = wp->w_p_fdc;
2004 if (n > 0)
2006 /* draw the fold column at the right */
2007 if (n > W_WIDTH(wp))
2008 n = W_WIDTH(wp);
2009 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2010 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
2011 ' ', ' ', hl_attr(HLF_FC));
2013 # endif
2014 # ifdef FEAT_SIGNS
2015 if (draw_signcolumn(wp))
2017 int nn = n + 2;
2019 /* draw the sign column left of the fold column */
2020 if (nn > W_WIDTH(wp))
2021 nn = W_WIDTH(wp);
2022 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2023 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
2024 ' ', ' ', hl_attr(HLF_SC));
2025 n = nn;
2027 # endif
2028 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2029 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
2030 c2, c2, hl_attr(hl));
2031 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2032 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
2033 c1, c2, hl_attr(hl));
2035 else
2036 #endif
2038 #ifdef FEAT_CMDWIN
2039 if (cmdwin_type != 0 && wp == curwin)
2041 /* draw the cmdline character in the leftmost column */
2042 n = 1;
2043 if (n > wp->w_width)
2044 n = wp->w_width;
2045 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2046 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
2047 cmdwin_type, ' ', hl_attr(HLF_AT));
2049 #endif
2050 #ifdef FEAT_FOLDING
2051 if (wp->w_p_fdc > 0)
2053 int nn = n + wp->w_p_fdc;
2055 /* draw the fold column at the left */
2056 if (nn > W_WIDTH(wp))
2057 nn = W_WIDTH(wp);
2058 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2059 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2060 ' ', ' ', hl_attr(HLF_FC));
2061 n = nn;
2063 #endif
2064 #ifdef FEAT_SIGNS
2065 if (draw_signcolumn(wp))
2067 int nn = n + 2;
2069 /* draw the sign column after the fold column */
2070 if (nn > W_WIDTH(wp))
2071 nn = W_WIDTH(wp);
2072 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2073 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2074 ' ', ' ', hl_attr(HLF_SC));
2075 n = nn;
2077 #endif
2078 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2079 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
2080 c1, c2, hl_attr(hl));
2082 set_empty_rows(wp, row);
2085 #ifdef FEAT_FOLDING
2087 * Display one folded line.
2089 static void
2090 fold_line(wp, fold_count, foldinfo, lnum, row)
2091 win_T *wp;
2092 long fold_count;
2093 foldinfo_T *foldinfo;
2094 linenr_T lnum;
2095 int row;
2097 char_u buf[51];
2098 pos_T *top, *bot;
2099 linenr_T lnume = lnum + fold_count - 1;
2100 int len;
2101 char_u *text;
2102 int fdc;
2103 int col;
2104 int txtcol;
2105 int off = (int)(current_ScreenLine - ScreenLines);
2106 int ri;
2108 /* Build the fold line:
2109 * 1. Add the cmdwin_type for the command-line window
2110 * 2. Add the 'foldcolumn'
2111 * 3. Add the 'number' column
2112 * 4. Compose the text
2113 * 5. Add the text
2114 * 6. set highlighting for the Visual area an other text
2116 col = 0;
2119 * 1. Add the cmdwin_type for the command-line window
2120 * Ignores 'rightleft', this window is never right-left.
2122 #ifdef FEAT_CMDWIN
2123 if (cmdwin_type != 0 && wp == curwin)
2125 ScreenLines[off] = cmdwin_type;
2126 ScreenAttrs[off] = hl_attr(HLF_AT);
2127 #ifdef FEAT_MBYTE
2128 if (enc_utf8)
2129 ScreenLinesUC[off] = 0;
2130 #endif
2131 ++col;
2133 #endif
2136 * 2. Add the 'foldcolumn'
2138 fdc = wp->w_p_fdc;
2139 if (fdc > W_WIDTH(wp) - col)
2140 fdc = W_WIDTH(wp) - col;
2141 if (fdc > 0)
2143 fill_foldcolumn(buf, wp, TRUE, lnum);
2144 #ifdef FEAT_RIGHTLEFT
2145 if (wp->w_p_rl)
2147 int i;
2149 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
2150 hl_attr(HLF_FC));
2151 /* reverse the fold column */
2152 for (i = 0; i < fdc; ++i)
2153 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2155 else
2156 #endif
2157 copy_text_attr(off + col, buf, fdc, hl_attr(HLF_FC));
2158 col += fdc;
2161 #ifdef FEAT_RIGHTLEFT
2162 # define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2163 for (ri = 0; ri < l; ++ri) \
2164 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2165 else \
2166 for (ri = 0; ri < l; ++ri) \
2167 ScreenAttrs[off + (p) + ri] = v
2168 #else
2169 # define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2170 ScreenAttrs[off + (p) + ri] = v
2171 #endif
2173 /* Set all attributes of the 'number' column and the text */
2174 RL_MEMSET(col, hl_attr(HLF_FL), W_WIDTH(wp) - col);
2176 #ifdef FEAT_SIGNS
2177 /* If signs are being displayed, add two spaces. */
2178 if (draw_signcolumn(wp))
2180 len = W_WIDTH(wp) - col;
2181 if (len > 0)
2183 if (len > 2)
2184 len = 2;
2185 # ifdef FEAT_RIGHTLEFT
2186 if (wp->w_p_rl)
2187 /* the line number isn't reversed */
2188 copy_text_attr(off + W_WIDTH(wp) - len - col,
2189 (char_u *)" ", len, hl_attr(HLF_FL));
2190 else
2191 # endif
2192 copy_text_attr(off + col, (char_u *)" ", len, hl_attr(HLF_FL));
2193 col += len;
2196 #endif
2199 * 3. Add the 'number' column
2201 if (wp->w_p_nu)
2203 len = W_WIDTH(wp) - col;
2204 if (len > 0)
2206 int w = number_width(wp);
2208 if (len > w + 1)
2209 len = w + 1;
2210 sprintf((char *)buf, "%*ld ", w, (long)lnum);
2211 #ifdef FEAT_RIGHTLEFT
2212 if (wp->w_p_rl)
2213 /* the line number isn't reversed */
2214 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
2215 hl_attr(HLF_FL));
2216 else
2217 #endif
2218 copy_text_attr(off + col, buf, len, hl_attr(HLF_FL));
2219 col += len;
2224 * 4. Compose the folded-line string with 'foldtext', if set.
2226 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
2228 txtcol = col; /* remember where text starts */
2231 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2232 * Right-left text is put in columns 0 - number-col, normal text is put
2233 * in columns number-col - window-width.
2235 #ifdef FEAT_MBYTE
2236 if (has_mbyte)
2238 int cells;
2239 int u8c, u8cc[MAX_MCO];
2240 int i;
2241 int idx;
2242 int c_len;
2243 char_u *p;
2244 # ifdef FEAT_ARABIC
2245 int prev_c = 0; /* previous Arabic character */
2246 int prev_c1 = 0; /* first composing char for prev_c */
2247 # endif
2249 # ifdef FEAT_RIGHTLEFT
2250 if (wp->w_p_rl)
2251 idx = off;
2252 else
2253 # endif
2254 idx = off + col;
2256 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2257 for (p = text; *p != NUL; )
2259 cells = (*mb_ptr2cells)(p);
2260 c_len = (*mb_ptr2len)(p);
2261 if (col + cells > W_WIDTH(wp)
2262 # ifdef FEAT_RIGHTLEFT
2263 - (wp->w_p_rl ? col : 0)
2264 # endif
2266 break;
2267 ScreenLines[idx] = *p;
2268 if (enc_utf8)
2270 u8c = utfc_ptr2char(p, u8cc);
2271 if (*p < 0x80 && u8cc[0] == 0)
2273 ScreenLinesUC[idx] = 0;
2274 #ifdef FEAT_ARABIC
2275 prev_c = u8c;
2276 #endif
2278 else
2280 #ifdef FEAT_ARABIC
2281 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2283 /* Do Arabic shaping. */
2284 int pc, pc1, nc;
2285 int pcc[MAX_MCO];
2286 int firstbyte = *p;
2288 /* The idea of what is the previous and next
2289 * character depends on 'rightleft'. */
2290 if (wp->w_p_rl)
2292 pc = prev_c;
2293 pc1 = prev_c1;
2294 nc = utf_ptr2char(p + c_len);
2295 prev_c1 = u8cc[0];
2297 else
2299 pc = utfc_ptr2char(p + c_len, pcc);
2300 nc = prev_c;
2301 pc1 = pcc[0];
2303 prev_c = u8c;
2305 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2306 pc, pc1, nc);
2307 ScreenLines[idx] = firstbyte;
2309 else
2310 prev_c = u8c;
2311 #endif
2312 /* Non-BMP character: display as ? or fullwidth ?. */
2313 #ifdef UNICODE16
2314 if (u8c >= 0x10000)
2315 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2316 else
2317 #endif
2318 ScreenLinesUC[idx] = u8c;
2319 for (i = 0; i < Screen_mco; ++i)
2321 ScreenLinesC[i][idx] = u8cc[i];
2322 if (u8cc[i] == 0)
2323 break;
2326 if (cells > 1)
2327 ScreenLines[idx + 1] = 0;
2329 else if (cells > 1) /* double-byte character */
2331 if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2332 ScreenLines2[idx] = p[1];
2333 else
2334 ScreenLines[idx + 1] = p[1];
2336 col += cells;
2337 idx += cells;
2338 p += c_len;
2341 else
2342 #endif
2344 len = (int)STRLEN(text);
2345 if (len > W_WIDTH(wp) - col)
2346 len = W_WIDTH(wp) - col;
2347 if (len > 0)
2349 #ifdef FEAT_RIGHTLEFT
2350 if (wp->w_p_rl)
2351 STRNCPY(current_ScreenLine, text, len);
2352 else
2353 #endif
2354 STRNCPY(current_ScreenLine + col, text, len);
2355 col += len;
2359 /* Fill the rest of the line with the fold filler */
2360 #ifdef FEAT_RIGHTLEFT
2361 if (wp->w_p_rl)
2362 col -= txtcol;
2363 #endif
2364 while (col < W_WIDTH(wp)
2365 #ifdef FEAT_RIGHTLEFT
2366 - (wp->w_p_rl ? txtcol : 0)
2367 #endif
2370 #ifdef FEAT_MBYTE
2371 if (enc_utf8)
2373 if (fill_fold >= 0x80)
2375 ScreenLinesUC[off + col] = fill_fold;
2376 ScreenLinesC[0][off + col] = 0;
2378 else
2379 ScreenLinesUC[off + col] = 0;
2381 #endif
2382 ScreenLines[off + col++] = fill_fold;
2385 if (text != buf)
2386 vim_free(text);
2389 * 6. set highlighting for the Visual area an other text.
2390 * If all folded lines are in the Visual area, highlight the line.
2392 #ifdef FEAT_VISUAL
2393 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2395 if (ltoreq(curwin->w_cursor, VIsual))
2397 /* Visual is after curwin->w_cursor */
2398 top = &curwin->w_cursor;
2399 bot = &VIsual;
2401 else
2403 /* Visual is before curwin->w_cursor */
2404 top = &VIsual;
2405 bot = &curwin->w_cursor;
2407 if (lnum >= top->lnum
2408 && lnume <= bot->lnum
2409 && (VIsual_mode != 'v'
2410 || ((lnum > top->lnum
2411 || (lnum == top->lnum
2412 && top->col == 0))
2413 && (lnume < bot->lnum
2414 || (lnume == bot->lnum
2415 && (bot->col - (*p_sel == 'e'))
2416 >= STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
2418 if (VIsual_mode == Ctrl_V)
2420 /* Visual block mode: highlight the chars part of the block */
2421 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2423 if (wp->w_old_cursor_lcol + txtcol < (colnr_T)W_WIDTH(wp))
2424 len = wp->w_old_cursor_lcol;
2425 else
2426 len = W_WIDTH(wp) - txtcol;
2427 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, hl_attr(HLF_V),
2428 len - (int)wp->w_old_cursor_fcol);
2431 else
2433 /* Set all attributes of the text */
2434 RL_MEMSET(txtcol, hl_attr(HLF_V), W_WIDTH(wp) - txtcol);
2438 #endif
2440 #ifdef FEAT_SYN_HL
2441 /* Show 'cursorcolumn' in the fold line. */
2442 if (wp->w_p_cuc)
2444 txtcol += wp->w_virtcol;
2445 if (wp->w_p_wrap)
2446 txtcol -= wp->w_skipcol;
2447 else
2448 txtcol -= wp->w_leftcol;
2449 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2450 ScreenAttrs[off + txtcol] = hl_combine_attr(
2451 ScreenAttrs[off + txtcol], hl_attr(HLF_CUC));
2453 #endif
2455 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2456 (int)W_WIDTH(wp), FALSE);
2459 * Update w_cline_height and w_cline_folded if the cursor line was
2460 * updated (saves a call to plines() later).
2462 if (wp == curwin
2463 && lnum <= curwin->w_cursor.lnum
2464 && lnume >= curwin->w_cursor.lnum)
2466 curwin->w_cline_row = row;
2467 curwin->w_cline_height = 1;
2468 curwin->w_cline_folded = TRUE;
2469 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2474 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2476 static void
2477 copy_text_attr(off, buf, len, attr)
2478 int off;
2479 char_u *buf;
2480 int len;
2481 int attr;
2483 int i;
2485 mch_memmove(ScreenLines + off, buf, (size_t)len);
2486 # ifdef FEAT_MBYTE
2487 if (enc_utf8)
2488 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2489 # endif
2490 for (i = 0; i < len; ++i)
2491 ScreenAttrs[off + i] = attr;
2495 * Fill the foldcolumn at "p" for window "wp".
2496 * Only to be called when 'foldcolumn' > 0.
2498 static void
2499 fill_foldcolumn(p, wp, closed, lnum)
2500 char_u *p;
2501 win_T *wp;
2502 int closed; /* TRUE of FALSE */
2503 linenr_T lnum; /* current line number */
2505 int i = 0;
2506 int level;
2507 int first_level;
2508 int empty;
2510 /* Init to all spaces. */
2511 copy_spaces(p, (size_t)wp->w_p_fdc);
2513 level = win_foldinfo.fi_level;
2514 if (level > 0)
2516 /* If there is only one column put more info in it. */
2517 empty = (wp->w_p_fdc == 1) ? 0 : 1;
2519 /* If the column is too narrow, we start at the lowest level that
2520 * fits and use numbers to indicated the depth. */
2521 first_level = level - wp->w_p_fdc - closed + 1 + empty;
2522 if (first_level < 1)
2523 first_level = 1;
2525 for (i = 0; i + empty < wp->w_p_fdc; ++i)
2527 if (win_foldinfo.fi_lnum == lnum
2528 && first_level + i >= win_foldinfo.fi_low_level)
2529 p[i] = '-';
2530 else if (first_level == 1)
2531 p[i] = '|';
2532 else if (first_level + i <= 9)
2533 p[i] = '0' + first_level + i;
2534 else
2535 p[i] = '>';
2536 if (first_level + i == level)
2537 break;
2540 if (closed)
2541 p[i >= wp->w_p_fdc ? i - 1 : i] = '+';
2543 #endif /* FEAT_FOLDING */
2546 * Display line "lnum" of window 'wp' on the screen.
2547 * Start at row "startrow", stop when "endrow" is reached.
2548 * wp->w_virtcol needs to be valid.
2550 * Return the number of last row the line occupies.
2552 /* ARGSUSED */
2553 static int
2554 win_line(wp, lnum, startrow, endrow, nochange)
2555 win_T *wp;
2556 linenr_T lnum;
2557 int startrow;
2558 int endrow;
2559 int nochange; /* not updating for changed text */
2561 int col; /* visual column on screen */
2562 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2563 int c = 0; /* init for GCC */
2564 long vcol = 0; /* virtual column (for tabs) */
2565 long vcol_prev = -1; /* "vcol" of previous character */
2566 char_u *line; /* current line */
2567 char_u *ptr; /* current position in "line" */
2568 int row; /* row in the window, excl w_winrow */
2569 int screen_row; /* row on the screen, incl w_winrow */
2571 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2572 int n_extra = 0; /* number of extra chars */
2573 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
2574 int c_extra = NUL; /* extra chars, all the same */
2575 int extra_attr = 0; /* attributes when n_extra != 0 */
2576 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2577 displaying lcs_eol at end-of-line */
2578 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2579 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2581 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2582 int saved_n_extra = 0;
2583 char_u *saved_p_extra = NULL;
2584 int saved_c_extra = 0;
2585 int saved_char_attr = 0;
2587 int n_attr = 0; /* chars with special attr */
2588 int saved_attr2 = 0; /* char_attr saved for n_attr */
2589 int n_attr3 = 0; /* chars with overruling special attr */
2590 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2592 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2594 int fromcol, tocol; /* start/end of inverting */
2595 int fromcol_prev = -2; /* start of inverting after cursor */
2596 int noinvcur = FALSE; /* don't invert the cursor */
2597 #ifdef FEAT_VISUAL
2598 pos_T *top, *bot;
2599 #endif
2600 pos_T pos;
2601 long v;
2603 int char_attr = 0; /* attributes for next character */
2604 int attr_pri = FALSE; /* char_attr has priority */
2605 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2606 in this line */
2607 int attr = 0; /* attributes for area highlighting */
2608 int area_attr = 0; /* attributes desired by highlighting */
2609 int search_attr = 0; /* attributes desired by 'hlsearch' */
2610 #ifdef FEAT_SYN_HL
2611 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
2612 int syntax_attr = 0; /* attributes desired by syntax */
2613 int has_syntax = FALSE; /* this buffer has syntax highl. */
2614 int save_did_emsg;
2615 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
2616 #endif
2617 #ifdef FEAT_SPELL
2618 int has_spell = FALSE; /* this buffer has spell checking */
2619 # define SPWORDLEN 150
2620 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
2621 int nextlinecol = 0; /* column where nextline[] starts */
2622 int nextline_idx = 0; /* index in nextline[] where next line
2623 starts */
2624 int spell_attr = 0; /* attributes desired by spelling */
2625 int word_end = 0; /* last byte with same spell_attr */
2626 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2627 static int checked_col = 0; /* column in "checked_lnum" up to which
2628 * there are no spell errors */
2629 static int cap_col = -1; /* column to check for Cap word */
2630 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
2631 int cur_checked_col = 0; /* checked column for current line */
2632 #endif
2633 int extra_check; /* has syntax or linebreak */
2634 #ifdef FEAT_MBYTE
2635 int multi_attr = 0; /* attributes desired by multibyte */
2636 int mb_l = 1; /* multi-byte byte length */
2637 int mb_c = 0; /* decoded multi-byte character */
2638 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
2639 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
2640 #endif
2641 #ifdef FEAT_DIFF
2642 int filler_lines; /* nr of filler lines to be drawn */
2643 int filler_todo; /* nr of filler lines still to do + 1 */
2644 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
2645 int change_start = MAXCOL; /* first col of changed area */
2646 int change_end = -1; /* last col of changed area */
2647 #endif
2648 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
2649 #ifdef FEAT_LINEBREAK
2650 int need_showbreak = FALSE;
2651 #endif
2652 #if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
2653 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
2654 # define LINE_ATTR
2655 int line_attr = 0; /* attribute for the whole line */
2656 #endif
2657 #ifdef FEAT_SEARCH_EXTRA
2658 matchitem_T *cur; /* points to the match list */
2659 match_T *shl; /* points to search_hl or a match */
2660 int shl_flag; /* flag to indicate whether search_hl
2661 has been processed or not */
2662 int prevcol_hl_flag; /* flag to indicate whether prevcol
2663 equals startcol of search_hl or one
2664 of the matches */
2665 #endif
2666 #ifdef FEAT_ARABIC
2667 int prev_c = 0; /* previous Arabic character */
2668 int prev_c1 = 0; /* first composing char for prev_c */
2669 #endif
2670 #if defined(LINE_ATTR)
2671 int did_line_attr = 0;
2672 #endif
2674 /* draw_state: items that are drawn in sequence: */
2675 #define WL_START 0 /* nothing done yet */
2676 #ifdef FEAT_CMDWIN
2677 # define WL_CMDLINE WL_START + 1 /* cmdline window column */
2678 #else
2679 # define WL_CMDLINE WL_START
2680 #endif
2681 #ifdef FEAT_FOLDING
2682 # define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
2683 #else
2684 # define WL_FOLD WL_CMDLINE
2685 #endif
2686 #ifdef FEAT_SIGNS
2687 # define WL_SIGN WL_FOLD + 1 /* column for signs */
2688 #else
2689 # define WL_SIGN WL_FOLD /* column for signs */
2690 #endif
2691 #define WL_NR WL_SIGN + 1 /* line number */
2692 #if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
2693 # define WL_SBR WL_NR + 1 /* 'showbreak' or 'diff' */
2694 #else
2695 # define WL_SBR WL_NR
2696 #endif
2697 #define WL_LINE WL_SBR + 1 /* text in the line */
2698 int draw_state = WL_START; /* what to draw next */
2699 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2700 int feedback_col = 0;
2701 int feedback_old_attr = -1;
2702 #endif
2705 if (startrow > endrow) /* past the end already! */
2706 return startrow;
2708 row = startrow;
2709 screen_row = row + W_WINROW(wp);
2712 * To speed up the loop below, set extra_check when there is linebreak,
2713 * trailing white space and/or syntax processing to be done.
2715 #ifdef FEAT_LINEBREAK
2716 extra_check = wp->w_p_lbr;
2717 #else
2718 extra_check = 0;
2719 #endif
2720 #ifdef FEAT_SYN_HL
2721 if (syntax_present(wp->w_buffer) && !wp->w_buffer->b_syn_error)
2723 /* Prepare for syntax highlighting in this line. When there is an
2724 * error, stop syntax highlighting. */
2725 save_did_emsg = did_emsg;
2726 did_emsg = FALSE;
2727 syntax_start(wp, lnum);
2728 if (did_emsg)
2729 wp->w_buffer->b_syn_error = TRUE;
2730 else
2732 did_emsg = save_did_emsg;
2733 has_syntax = TRUE;
2734 extra_check = TRUE;
2737 #endif
2739 #ifdef FEAT_SPELL
2740 if (wp->w_p_spell
2741 && *wp->w_buffer->b_p_spl != NUL
2742 && wp->w_buffer->b_langp.ga_len > 0
2743 && *(char **)(wp->w_buffer->b_langp.ga_data) != NULL)
2745 /* Prepare for spell checking. */
2746 has_spell = TRUE;
2747 extra_check = TRUE;
2749 /* Get the start of the next line, so that words that wrap to the next
2750 * line are found too: "et<line-break>al.".
2751 * Trick: skip a few chars for C/shell/Vim comments */
2752 nextline[SPWORDLEN] = NUL;
2753 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2755 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
2756 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
2759 /* When a word wrapped from the previous line the start of the current
2760 * line is valid. */
2761 if (lnum == checked_lnum)
2762 cur_checked_col = checked_col;
2763 checked_lnum = 0;
2765 /* When there was a sentence end in the previous line may require a
2766 * word starting with capital in this line. In line 1 always check
2767 * the first word. */
2768 if (lnum != capcol_lnum)
2769 cap_col = -1;
2770 if (lnum == 1)
2771 cap_col = 0;
2772 capcol_lnum = 0;
2774 #endif
2777 * handle visual active in this window
2779 fromcol = -10;
2780 tocol = MAXCOL;
2781 #ifdef FEAT_VISUAL
2782 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2784 /* Visual is after curwin->w_cursor */
2785 if (ltoreq(curwin->w_cursor, VIsual))
2787 top = &curwin->w_cursor;
2788 bot = &VIsual;
2790 else /* Visual is before curwin->w_cursor */
2792 top = &VIsual;
2793 bot = &curwin->w_cursor;
2795 if (VIsual_mode == Ctrl_V) /* block mode */
2797 if (lnum >= top->lnum && lnum <= bot->lnum)
2799 fromcol = wp->w_old_cursor_fcol;
2800 tocol = wp->w_old_cursor_lcol;
2803 else /* non-block mode */
2805 if (lnum > top->lnum && lnum <= bot->lnum)
2806 fromcol = 0;
2807 else if (lnum == top->lnum)
2809 if (VIsual_mode == 'V') /* linewise */
2810 fromcol = 0;
2811 else
2813 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
2814 if (gchar_pos(top) == NUL)
2815 tocol = fromcol + 1;
2818 if (VIsual_mode != 'V' && lnum == bot->lnum)
2820 if (*p_sel == 'e' && bot->col == 0
2821 #ifdef FEAT_VIRTUALEDIT
2822 && bot->coladd == 0
2823 #endif
2826 fromcol = -10;
2827 tocol = MAXCOL;
2829 else if (bot->col == MAXCOL)
2830 tocol = MAXCOL;
2831 else
2833 pos = *bot;
2834 if (*p_sel == 'e')
2835 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
2836 else
2838 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
2839 ++tocol;
2845 #ifndef MSDOS
2846 /* Check if the character under the cursor should not be inverted */
2847 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
2848 # ifdef FEAT_GUI
2849 && !gui.in_use
2850 # endif
2852 noinvcur = TRUE;
2853 #endif
2855 /* if inverting in this line set area_highlighting */
2856 if (fromcol >= 0)
2858 area_highlighting = TRUE;
2859 attr = hl_attr(HLF_V);
2860 #if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
2861 if (clip_star.available && !clip_star.owned && clip_isautosel())
2862 attr = hl_attr(HLF_VNC);
2863 #endif
2868 * handle 'incsearch' and ":s///c" highlighting
2870 else
2871 #endif /* FEAT_VISUAL */
2872 if (highlight_match
2873 && wp == curwin
2874 && lnum >= curwin->w_cursor.lnum
2875 && lnum <= curwin->w_cursor.lnum + search_match_lines)
2877 if (lnum == curwin->w_cursor.lnum)
2878 getvcol(curwin, &(curwin->w_cursor),
2879 (colnr_T *)&fromcol, NULL, NULL);
2880 else
2881 fromcol = 0;
2882 if (lnum == curwin->w_cursor.lnum + search_match_lines)
2884 pos.lnum = lnum;
2885 pos.col = search_match_endcol;
2886 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
2888 else
2889 tocol = MAXCOL;
2890 if (fromcol == tocol) /* do at least one character */
2891 tocol = fromcol + 1; /* happens when past end of line */
2892 area_highlighting = TRUE;
2893 attr = hl_attr(HLF_I);
2896 #ifdef FEAT_DIFF
2897 filler_lines = diff_check(wp, lnum);
2898 if (filler_lines < 0)
2900 if (filler_lines == -1)
2902 if (diff_find_change(wp, lnum, &change_start, &change_end))
2903 diff_hlf = HLF_ADD; /* added line */
2904 else if (change_start == 0)
2905 diff_hlf = HLF_TXD; /* changed text */
2906 else
2907 diff_hlf = HLF_CHD; /* changed line */
2909 else
2910 diff_hlf = HLF_ADD; /* added line */
2911 filler_lines = 0;
2912 area_highlighting = TRUE;
2914 if (lnum == wp->w_topline)
2915 filler_lines = wp->w_topfill;
2916 filler_todo = filler_lines;
2917 #endif
2919 #ifdef LINE_ATTR
2920 # ifdef FEAT_SIGNS
2921 /* If this line has a sign with line highlighting set line_attr. */
2922 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
2923 if (v != 0)
2924 line_attr = sign_get_attr((int)v, TRUE);
2925 # endif
2926 # if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
2927 /* Highlight the current line in the quickfix window. */
2928 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
2929 line_attr = hl_attr(HLF_L);
2930 # endif
2931 if (line_attr != 0)
2932 area_highlighting = TRUE;
2933 #endif
2935 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
2936 ptr = line;
2938 #ifdef FEAT_SPELL
2939 if (has_spell)
2941 /* For checking first word with a capital skip white space. */
2942 if (cap_col == 0)
2943 cap_col = (int)(skipwhite(line) - line);
2945 /* To be able to spell-check over line boundaries copy the end of the
2946 * current line into nextline[]. Above the start of the next line was
2947 * copied to nextline[SPWORDLEN]. */
2948 if (nextline[SPWORDLEN] == NUL)
2950 /* No next line or it is empty. */
2951 nextlinecol = MAXCOL;
2952 nextline_idx = 0;
2954 else
2956 v = (long)STRLEN(line);
2957 if (v < SPWORDLEN)
2959 /* Short line, use it completely and append the start of the
2960 * next line. */
2961 nextlinecol = 0;
2962 mch_memmove(nextline, line, (size_t)v);
2963 STRMOVE(nextline + v, nextline + SPWORDLEN);
2964 nextline_idx = v + 1;
2966 else
2968 /* Long line, use only the last SPWORDLEN bytes. */
2969 nextlinecol = v - SPWORDLEN;
2970 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
2971 nextline_idx = SPWORDLEN + 1;
2975 #endif
2977 /* find start of trailing whitespace */
2978 if (wp->w_p_list && lcs_trail)
2980 trailcol = (colnr_T)STRLEN(ptr);
2981 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
2982 --trailcol;
2983 trailcol += (colnr_T) (ptr - line);
2984 extra_check = TRUE;
2988 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
2989 * first character to be displayed.
2991 if (wp->w_p_wrap)
2992 v = wp->w_skipcol;
2993 else
2994 v = wp->w_leftcol;
2995 if (v > 0)
2997 #ifdef FEAT_MBYTE
2998 char_u *prev_ptr = ptr;
2999 #endif
3000 while (vcol < v && *ptr != NUL)
3002 c = win_lbr_chartabsize(wp, ptr, (colnr_T)vcol, NULL);
3003 vcol += c;
3004 #ifdef FEAT_MBYTE
3005 prev_ptr = ptr;
3006 #endif
3007 mb_ptr_adv(ptr);
3010 #ifdef FEAT_VIRTUALEDIT
3011 /* When 'virtualedit' is set the end of the line may be before the
3012 * start of the displayed part. */
3013 if (vcol < v && *ptr == NUL && virtual_active())
3014 vcol = v;
3015 #endif
3017 /* Handle a character that's not completely on the screen: Put ptr at
3018 * that character but skip the first few screen characters. */
3019 if (vcol > v)
3021 vcol -= c;
3022 #ifdef FEAT_MBYTE
3023 ptr = prev_ptr;
3024 #else
3025 --ptr;
3026 #endif
3027 n_skip = v - vcol;
3031 * Adjust for when the inverted text is before the screen,
3032 * and when the start of the inverted text is before the screen.
3034 if (tocol <= vcol)
3035 fromcol = 0;
3036 else if (fromcol >= 0 && fromcol < vcol)
3037 fromcol = vcol;
3039 #ifdef FEAT_LINEBREAK
3040 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3041 if (wp->w_p_wrap)
3042 need_showbreak = TRUE;
3043 #endif
3044 #ifdef FEAT_SPELL
3045 /* When spell checking a word we need to figure out the start of the
3046 * word and if it's badly spelled or not. */
3047 if (has_spell)
3049 int len;
3050 colnr_T linecol = (colnr_T)(ptr - line);
3051 hlf_T spell_hlf = HLF_COUNT;
3053 pos = wp->w_cursor;
3054 wp->w_cursor.lnum = lnum;
3055 wp->w_cursor.col = linecol;
3056 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
3058 /* spell_move_to() may call ml_get() and make "line" invalid */
3059 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3060 ptr = line + linecol;
3062 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
3064 /* no bad word found at line start, don't check until end of a
3065 * word */
3066 spell_hlf = HLF_COUNT;
3067 word_end = (int)(spell_to_word_end(ptr, wp->w_buffer)
3068 - line + 1);
3070 else
3072 /* bad word found, use attributes until end of word */
3073 word_end = wp->w_cursor.col + len + 1;
3075 /* Turn index into actual attributes. */
3076 if (spell_hlf != HLF_COUNT)
3077 spell_attr = highlight_attr[spell_hlf];
3079 wp->w_cursor = pos;
3081 # ifdef FEAT_SYN_HL
3082 /* Need to restart syntax highlighting for this line. */
3083 if (has_syntax)
3084 syntax_start(wp, lnum);
3085 # endif
3087 #endif
3091 * Correct highlighting for cursor that can't be disabled.
3092 * Avoids having to check this for each character.
3094 if (fromcol >= 0)
3096 if (noinvcur)
3098 if ((colnr_T)fromcol == wp->w_virtcol)
3100 /* highlighting starts at cursor, let it start just after the
3101 * cursor */
3102 fromcol_prev = fromcol;
3103 fromcol = -1;
3105 else if ((colnr_T)fromcol < wp->w_virtcol)
3106 /* restart highlighting after the cursor */
3107 fromcol_prev = wp->w_virtcol;
3109 if (fromcol >= tocol)
3110 fromcol = -1;
3113 #ifdef FEAT_SEARCH_EXTRA
3115 * Handle highlighting the last used search pattern and matches.
3116 * Do this for both search_hl and the match list.
3118 cur = wp->w_match_head;
3119 shl_flag = FALSE;
3120 while (cur != NULL || shl_flag == FALSE)
3122 if (shl_flag == FALSE)
3124 shl = &search_hl;
3125 shl_flag = TRUE;
3127 else
3128 shl = &cur->hl;
3129 shl->startcol = MAXCOL;
3130 shl->endcol = MAXCOL;
3131 shl->attr_cur = 0;
3132 if (shl->rm.regprog != NULL)
3134 v = (long)(ptr - line);
3135 next_search_hl(wp, shl, lnum, (colnr_T)v);
3137 /* Need to get the line again, a multi-line regexp may have made it
3138 * invalid. */
3139 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3140 ptr = line + v;
3142 if (shl->lnum != 0 && shl->lnum <= lnum)
3144 if (shl->lnum == lnum)
3145 shl->startcol = shl->rm.startpos[0].col;
3146 else
3147 shl->startcol = 0;
3148 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3149 - shl->rm.startpos[0].lnum)
3150 shl->endcol = shl->rm.endpos[0].col;
3151 else
3152 shl->endcol = MAXCOL;
3153 /* Highlight one character for an empty match. */
3154 if (shl->startcol == shl->endcol)
3156 #ifdef FEAT_MBYTE
3157 if (has_mbyte && line[shl->endcol] != NUL)
3158 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3159 else
3160 #endif
3161 ++shl->endcol;
3163 if ((long)shl->startcol < v) /* match at leftcol */
3165 shl->attr_cur = shl->attr;
3166 search_attr = shl->attr;
3168 area_highlighting = TRUE;
3171 if (shl != &search_hl && cur != NULL)
3172 cur = cur->next;
3174 #endif
3176 #ifdef FEAT_SYN_HL
3177 /* Cursor line highlighting for 'cursorline'. Not when Visual mode is
3178 * active, because it's not clear what is selected then. */
3179 if (wp->w_p_cul && lnum == wp->w_cursor.lnum && !VIsual_active)
3181 line_attr = hl_attr(HLF_CUL);
3182 area_highlighting = TRUE;
3184 #endif
3186 off = (unsigned)(current_ScreenLine - ScreenLines);
3187 col = 0;
3188 #ifdef FEAT_RIGHTLEFT
3189 if (wp->w_p_rl)
3191 /* Rightleft window: process the text in the normal direction, but put
3192 * it in current_ScreenLine[] from right to left. Start at the
3193 * rightmost column of the window. */
3194 col = W_WIDTH(wp) - 1;
3195 off += col;
3197 #endif
3200 * Repeat for the whole displayed line.
3202 for (;;)
3204 /* Skip this quickly when working on the text. */
3205 if (draw_state != WL_LINE)
3207 #ifdef FEAT_CMDWIN
3208 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3210 draw_state = WL_CMDLINE;
3211 if (cmdwin_type != 0 && wp == curwin)
3213 /* Draw the cmdline character. */
3214 n_extra = 1;
3215 c_extra = cmdwin_type;
3216 char_attr = hl_attr(HLF_AT);
3219 #endif
3221 #ifdef FEAT_FOLDING
3222 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3224 draw_state = WL_FOLD;
3225 if (wp->w_p_fdc > 0)
3227 /* Draw the 'foldcolumn'. */
3228 fill_foldcolumn(extra, wp, FALSE, lnum);
3229 n_extra = wp->w_p_fdc;
3230 p_extra = extra;
3231 p_extra[n_extra] = NUL;
3232 c_extra = NUL;
3233 char_attr = hl_attr(HLF_FC);
3236 #endif
3238 #ifdef FEAT_SIGNS
3239 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3241 draw_state = WL_SIGN;
3242 /* Show the sign column when there are any signs in this
3243 * buffer or when using Netbeans. */
3244 if (draw_signcolumn(wp)
3245 # ifdef FEAT_DIFF
3246 && filler_todo <= 0
3247 # endif
3250 int_u text_sign;
3251 # ifdef FEAT_SIGN_ICONS
3252 int_u icon_sign;
3253 # endif
3255 /* Draw two cells with the sign value or blank. */
3256 c_extra = ' ';
3257 char_attr = hl_attr(HLF_SC);
3258 n_extra = 2;
3260 if (row == startrow)
3262 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3263 SIGN_TEXT);
3264 # ifdef FEAT_SIGN_ICONS
3265 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3266 SIGN_ICON);
3267 if (gui.in_use && icon_sign != 0)
3269 /* Use the image in this position. */
3270 c_extra = SIGN_BYTE;
3271 # ifdef FEAT_NETBEANS_INTG
3272 if (buf_signcount(wp->w_buffer, lnum) > 1)
3273 c_extra = MULTISIGN_BYTE;
3274 # endif
3275 char_attr = icon_sign;
3277 else
3278 # endif
3279 if (text_sign != 0)
3281 p_extra = sign_get_text(text_sign);
3282 if (p_extra != NULL)
3284 c_extra = NUL;
3285 n_extra = (int)STRLEN(p_extra);
3287 char_attr = sign_get_attr(text_sign, FALSE);
3292 #endif
3294 if (draw_state == WL_NR - 1 && n_extra == 0)
3296 draw_state = WL_NR;
3297 /* Display the line number. After the first fill with blanks
3298 * when the 'n' flag isn't in 'cpo' */
3299 if (wp->w_p_nu
3300 && (row == startrow
3301 #ifdef FEAT_DIFF
3302 + filler_lines
3303 #endif
3304 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3306 /* Draw the line number (empty space after wrapping). */
3307 if (row == startrow
3308 #ifdef FEAT_DIFF
3309 + filler_lines
3310 #endif
3313 sprintf((char *)extra, "%*ld ",
3314 number_width(wp), (long)lnum);
3315 if (wp->w_skipcol > 0)
3316 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3317 *p_extra = '-';
3318 #ifdef FEAT_RIGHTLEFT
3319 if (wp->w_p_rl) /* reverse line numbers */
3320 rl_mirror(extra);
3321 #endif
3322 p_extra = extra;
3323 c_extra = NUL;
3325 else
3326 c_extra = ' ';
3327 n_extra = number_width(wp) + 1;
3328 char_attr = hl_attr(HLF_N);
3329 #ifdef FEAT_SYN_HL
3330 /* When 'cursorline' is set highlight the line number of
3331 * the current line differently. */
3332 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3333 char_attr = hl_combine_attr(hl_attr(HLF_CUL), char_attr);
3334 #endif
3338 #if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3339 if (draw_state == WL_SBR - 1 && n_extra == 0)
3341 draw_state = WL_SBR;
3342 # ifdef FEAT_DIFF
3343 if (filler_todo > 0)
3345 /* Draw "deleted" diff line(s). */
3346 if (char2cells(fill_diff) > 1)
3347 c_extra = '-';
3348 else
3349 c_extra = fill_diff;
3350 # ifdef FEAT_RIGHTLEFT
3351 if (wp->w_p_rl)
3352 n_extra = col + 1;
3353 else
3354 # endif
3355 n_extra = W_WIDTH(wp) - col;
3356 char_attr = hl_attr(HLF_DED);
3358 # endif
3359 # ifdef FEAT_LINEBREAK
3360 if (*p_sbr != NUL && need_showbreak)
3362 /* Draw 'showbreak' at the start of each broken line. */
3363 p_extra = p_sbr;
3364 c_extra = NUL;
3365 n_extra = (int)STRLEN(p_sbr);
3366 char_attr = hl_attr(HLF_AT);
3367 need_showbreak = FALSE;
3368 /* Correct end of highlighted area for 'showbreak',
3369 * required when 'linebreak' is also set. */
3370 if (tocol == vcol)
3371 tocol += n_extra;
3373 # endif
3375 #endif
3377 if (draw_state == WL_LINE - 1 && n_extra == 0)
3379 draw_state = WL_LINE;
3380 if (saved_n_extra)
3382 /* Continue item from end of wrapped line. */
3383 n_extra = saved_n_extra;
3384 c_extra = saved_c_extra;
3385 p_extra = saved_p_extra;
3386 char_attr = saved_char_attr;
3388 else
3389 char_attr = 0;
3393 /* When still displaying '$' of change command, stop at cursor */
3394 if (dollar_vcol != 0 && wp == curwin
3395 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
3396 #ifdef FEAT_DIFF
3397 && filler_todo <= 0
3398 #endif
3401 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3402 wp->w_p_rl);
3403 /* Pretend we have finished updating the window. Except when
3404 * 'cursorcolumn' is set. */
3405 #ifdef FEAT_SYN_HL
3406 if (wp->w_p_cuc)
3407 row = wp->w_cline_row + wp->w_cline_height;
3408 else
3409 #endif
3410 row = wp->w_height;
3411 break;
3414 if (draw_state == WL_LINE && area_highlighting)
3416 /* handle Visual or match highlighting in this line */
3417 if (vcol == fromcol
3418 #ifdef FEAT_MBYTE
3419 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3420 && (*mb_ptr2cells)(ptr) > 1)
3421 #endif
3422 || ((int)vcol_prev == fromcol_prev
3423 && vcol < tocol))
3424 area_attr = attr; /* start highlighting */
3425 else if (area_attr != 0
3426 && (vcol == tocol
3427 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
3428 area_attr = 0; /* stop highlighting */
3430 #ifdef FEAT_SEARCH_EXTRA
3431 if (!n_extra)
3434 * Check for start/end of search pattern match.
3435 * After end, check for start/end of next match.
3436 * When another match, have to check for start again.
3437 * Watch out for matching an empty string!
3438 * Do this for 'search_hl' and the match list (ordered by
3439 * priority).
3441 v = (long)(ptr - line);
3442 cur = wp->w_match_head;
3443 shl_flag = FALSE;
3444 while (cur != NULL || shl_flag == FALSE)
3446 if (shl_flag == FALSE
3447 && ((cur != NULL
3448 && cur->priority > SEARCH_HL_PRIORITY)
3449 || cur == NULL))
3451 shl = &search_hl;
3452 shl_flag = TRUE;
3454 else
3455 shl = &cur->hl;
3456 while (shl->rm.regprog != NULL)
3458 if (shl->startcol != MAXCOL
3459 && v >= (long)shl->startcol
3460 && v < (long)shl->endcol)
3462 shl->attr_cur = shl->attr;
3464 else if (v == (long)shl->endcol)
3466 shl->attr_cur = 0;
3468 next_search_hl(wp, shl, lnum, (colnr_T)v);
3470 /* Need to get the line again, a multi-line regexp
3471 * may have made it invalid. */
3472 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3473 ptr = line + v;
3475 if (shl->lnum == lnum)
3477 shl->startcol = shl->rm.startpos[0].col;
3478 if (shl->rm.endpos[0].lnum == 0)
3479 shl->endcol = shl->rm.endpos[0].col;
3480 else
3481 shl->endcol = MAXCOL;
3483 if (shl->startcol == shl->endcol)
3485 /* highlight empty match, try again after
3486 * it */
3487 #ifdef FEAT_MBYTE
3488 if (has_mbyte)
3489 shl->endcol += (*mb_ptr2len)(line
3490 + shl->endcol);
3491 else
3492 #endif
3493 ++shl->endcol;
3496 /* Loop to check if the match starts at the
3497 * current position */
3498 continue;
3501 break;
3503 if (shl != &search_hl && cur != NULL)
3504 cur = cur->next;
3507 /* Use attributes from match with highest priority among
3508 * 'search_hl' and the match list. */
3509 search_attr = search_hl.attr_cur;
3510 cur = wp->w_match_head;
3511 shl_flag = FALSE;
3512 while (cur != NULL || shl_flag == FALSE)
3514 if (shl_flag == FALSE
3515 && ((cur != NULL
3516 && cur->priority > SEARCH_HL_PRIORITY)
3517 || cur == NULL))
3519 shl = &search_hl;
3520 shl_flag = TRUE;
3522 else
3523 shl = &cur->hl;
3524 if (shl->attr_cur != 0)
3525 search_attr = shl->attr_cur;
3526 if (shl != &search_hl && cur != NULL)
3527 cur = cur->next;
3530 #endif
3532 #ifdef FEAT_DIFF
3533 if (diff_hlf != (hlf_T)0)
3535 if (diff_hlf == HLF_CHD && ptr - line >= change_start
3536 && n_extra == 0)
3537 diff_hlf = HLF_TXD; /* changed text */
3538 if (diff_hlf == HLF_TXD && ptr - line > change_end
3539 && n_extra == 0)
3540 diff_hlf = HLF_CHD; /* changed line */
3541 line_attr = hl_attr(diff_hlf);
3543 #endif
3545 /* Decide which of the highlight attributes to use. */
3546 attr_pri = TRUE;
3547 if (area_attr != 0)
3548 char_attr = area_attr;
3549 else if (search_attr != 0)
3550 char_attr = search_attr;
3551 #ifdef LINE_ATTR
3552 /* Use line_attr when not in the Visual or 'incsearch' area
3553 * (area_attr may be 0 when "noinvcur" is set). */
3554 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
3555 || (vcol < fromcol || vcol >= tocol)))
3556 char_attr = line_attr;
3557 #endif
3558 else
3560 attr_pri = FALSE;
3561 #ifdef FEAT_SYN_HL
3562 if (has_syntax)
3563 char_attr = syntax_attr;
3564 else
3565 #endif
3566 char_attr = 0;
3571 * Get the next character to put on the screen.
3574 * The "p_extra" points to the extra stuff that is inserted to
3575 * represent special characters (non-printable stuff) and other
3576 * things. When all characters are the same, c_extra is used.
3577 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
3578 * "p_extra[n_extra]".
3579 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
3581 if (n_extra > 0)
3583 if (c_extra != NUL)
3585 c = c_extra;
3586 #ifdef FEAT_MBYTE
3587 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
3588 if (enc_utf8 && (*mb_char2len)(c) > 1)
3590 mb_utf8 = TRUE;
3591 u8cc[0] = 0;
3592 c = 0xc0;
3594 else
3595 mb_utf8 = FALSE;
3596 #endif
3598 else
3600 c = *p_extra;
3601 #ifdef FEAT_MBYTE
3602 if (has_mbyte)
3604 mb_c = c;
3605 if (enc_utf8)
3607 /* If the UTF-8 character is more than one byte:
3608 * Decode it into "mb_c". */
3609 mb_l = (*mb_ptr2len)(p_extra);
3610 mb_utf8 = FALSE;
3611 if (mb_l > n_extra)
3612 mb_l = 1;
3613 else if (mb_l > 1)
3615 mb_c = utfc_ptr2char(p_extra, u8cc);
3616 mb_utf8 = TRUE;
3617 c = 0xc0;
3620 else
3622 /* if this is a DBCS character, put it in "mb_c" */
3623 mb_l = MB_BYTE2LEN(c);
3624 if (mb_l >= n_extra)
3625 mb_l = 1;
3626 else if (mb_l > 1)
3627 mb_c = (c << 8) + p_extra[1];
3629 if (mb_l == 0) /* at the NUL at end-of-line */
3630 mb_l = 1;
3632 /* If a double-width char doesn't fit display a '>' in the
3633 * last column. */
3634 if ((
3635 # ifdef FEAT_RIGHTLEFT
3636 wp->w_p_rl ? (col <= 0) :
3637 # endif
3638 (col >= W_WIDTH(wp) - 1))
3639 && (*mb_char2cells)(mb_c) == 2)
3641 c = '>';
3642 mb_c = c;
3643 mb_l = 1;
3644 mb_utf8 = FALSE;
3645 multi_attr = hl_attr(HLF_AT);
3646 /* put the pointer back to output the double-width
3647 * character at the start of the next line. */
3648 ++n_extra;
3649 --p_extra;
3651 else
3653 n_extra -= mb_l - 1;
3654 p_extra += mb_l - 1;
3657 #endif
3658 ++p_extra;
3660 --n_extra;
3662 else
3665 * Get a character from the line itself.
3667 c = *ptr;
3668 #ifdef FEAT_MBYTE
3669 if (has_mbyte)
3671 mb_c = c;
3672 if (enc_utf8)
3674 /* If the UTF-8 character is more than one byte: Decode it
3675 * into "mb_c". */
3676 mb_l = (*mb_ptr2len)(ptr);
3677 mb_utf8 = FALSE;
3678 if (mb_l > 1)
3680 mb_c = utfc_ptr2char(ptr, u8cc);
3681 /* Overlong encoded ASCII or ASCII with composing char
3682 * is displayed normally, except a NUL. */
3683 if (mb_c < 0x80)
3684 c = mb_c;
3685 mb_utf8 = TRUE;
3687 /* At start of the line we can have a composing char.
3688 * Draw it as a space with a composing char. */
3689 if (utf_iscomposing(mb_c))
3691 int i;
3693 for (i = Screen_mco - 1; i > 0; --i)
3694 u8cc[i] = u8cc[i - 1];
3695 u8cc[0] = mb_c;
3696 mb_c = ' ';
3700 if ((mb_l == 1 && c >= 0x80)
3701 || (mb_l >= 1 && mb_c == 0)
3702 || (mb_l > 1 && (!vim_isprintc(mb_c)
3703 # ifdef UNICODE16
3704 || mb_c >= 0x10000
3705 # endif
3709 * Illegal UTF-8 byte: display as <xx>.
3710 * Non-BMP character : display as ? or fullwidth ?.
3712 # ifdef UNICODE16
3713 if (mb_c < 0x10000)
3714 # endif
3716 transchar_hex(extra, mb_c);
3717 # ifdef FEAT_RIGHTLEFT
3718 if (wp->w_p_rl) /* reverse */
3719 rl_mirror(extra);
3720 # endif
3722 # ifdef UNICODE16
3723 else if (utf_char2cells(mb_c) != 2)
3724 STRCPY(extra, "?");
3725 else
3726 /* 0xff1f in UTF-8: full-width '?' */
3727 STRCPY(extra, "\357\274\237");
3728 # endif
3730 p_extra = extra;
3731 c = *p_extra;
3732 mb_c = mb_ptr2char_adv(&p_extra);
3733 mb_utf8 = (c >= 0x80);
3734 n_extra = (int)STRLEN(p_extra);
3735 c_extra = NUL;
3736 if (area_attr == 0 && search_attr == 0)
3738 n_attr = n_extra + 1;
3739 extra_attr = hl_attr(HLF_8);
3740 saved_attr2 = char_attr; /* save current attr */
3743 else if (mb_l == 0) /* at the NUL at end-of-line */
3744 mb_l = 1;
3745 #ifdef FEAT_ARABIC
3746 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
3748 /* Do Arabic shaping. */
3749 int pc, pc1, nc;
3750 int pcc[MAX_MCO];
3752 /* The idea of what is the previous and next
3753 * character depends on 'rightleft'. */
3754 if (wp->w_p_rl)
3756 pc = prev_c;
3757 pc1 = prev_c1;
3758 nc = utf_ptr2char(ptr + mb_l);
3759 prev_c1 = u8cc[0];
3761 else
3763 pc = utfc_ptr2char(ptr + mb_l, pcc);
3764 nc = prev_c;
3765 pc1 = pcc[0];
3767 prev_c = mb_c;
3769 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
3771 else
3772 prev_c = mb_c;
3773 #endif
3775 else /* enc_dbcs */
3777 mb_l = MB_BYTE2LEN(c);
3778 if (mb_l == 0) /* at the NUL at end-of-line */
3779 mb_l = 1;
3780 else if (mb_l > 1)
3782 /* We assume a second byte below 32 is illegal.
3783 * Hopefully this is OK for all double-byte encodings!
3785 if (ptr[1] >= 32)
3786 mb_c = (c << 8) + ptr[1];
3787 else
3789 if (ptr[1] == NUL)
3791 /* head byte at end of line */
3792 mb_l = 1;
3793 transchar_nonprint(extra, c);
3795 else
3797 /* illegal tail byte */
3798 mb_l = 2;
3799 STRCPY(extra, "XX");
3801 p_extra = extra;
3802 n_extra = (int)STRLEN(extra) - 1;
3803 c_extra = NUL;
3804 c = *p_extra++;
3805 if (area_attr == 0 && search_attr == 0)
3807 n_attr = n_extra + 1;
3808 extra_attr = hl_attr(HLF_8);
3809 saved_attr2 = char_attr; /* save current attr */
3811 mb_c = c;
3815 /* If a double-width char doesn't fit display a '>' in the
3816 * last column; the character is displayed at the start of the
3817 * next line. */
3818 if ((
3819 # ifdef FEAT_RIGHTLEFT
3820 wp->w_p_rl ? (col <= 0) :
3821 # endif
3822 (col >= W_WIDTH(wp) - 1))
3823 && (*mb_char2cells)(mb_c) == 2)
3825 c = '>';
3826 mb_c = c;
3827 mb_utf8 = FALSE;
3828 mb_l = 1;
3829 multi_attr = hl_attr(HLF_AT);
3830 /* Put pointer back so that the character will be
3831 * displayed at the start of the next line. */
3832 --ptr;
3834 else if (*ptr != NUL)
3835 ptr += mb_l - 1;
3837 /* If a double-width char doesn't fit at the left side display
3838 * a '<' in the first column. */
3839 if (n_skip > 0 && mb_l > 1)
3841 n_extra = 1;
3842 c_extra = '<';
3843 c = ' ';
3844 if (area_attr == 0 && search_attr == 0)
3846 n_attr = n_extra + 1;
3847 extra_attr = hl_attr(HLF_AT);
3848 saved_attr2 = char_attr; /* save current attr */
3850 mb_c = c;
3851 mb_utf8 = FALSE;
3852 mb_l = 1;
3856 #endif
3857 ++ptr;
3859 /* 'list' : change char 160 to lcs_nbsp. */
3860 if (wp->w_p_list && (c == 160
3861 #ifdef FEAT_MBYTE
3862 || (mb_utf8 && mb_c == 160)
3863 #endif
3864 ) && lcs_nbsp)
3866 c = lcs_nbsp;
3867 if (area_attr == 0 && search_attr == 0)
3869 n_attr = 1;
3870 extra_attr = hl_attr(HLF_8);
3871 saved_attr2 = char_attr; /* save current attr */
3873 #ifdef FEAT_MBYTE
3874 mb_c = c;
3875 if (enc_utf8 && (*mb_char2len)(c) > 1)
3877 mb_utf8 = TRUE;
3878 u8cc[0] = 0;
3879 c = 0xc0;
3881 else
3882 mb_utf8 = FALSE;
3883 #endif
3886 if (extra_check)
3888 #ifdef FEAT_SPELL
3889 int can_spell = TRUE;
3890 #endif
3892 #ifdef FEAT_SYN_HL
3893 /* Get syntax attribute, unless still at the start of the line
3894 * (double-wide char that doesn't fit). */
3895 v = (long)(ptr - line);
3896 if (has_syntax && v > 0)
3898 /* Get the syntax attribute for the character. If there
3899 * is an error, disable syntax highlighting. */
3900 save_did_emsg = did_emsg;
3901 did_emsg = FALSE;
3903 syntax_attr = get_syntax_attr((colnr_T)v - 1,
3904 # ifdef FEAT_SPELL
3905 has_spell ? &can_spell :
3906 # endif
3907 NULL, FALSE);
3909 if (did_emsg)
3911 wp->w_buffer->b_syn_error = TRUE;
3912 has_syntax = FALSE;
3914 else
3915 did_emsg = save_did_emsg;
3917 /* Need to get the line again, a multi-line regexp may
3918 * have made it invalid. */
3919 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3920 ptr = line + v;
3922 if (!attr_pri)
3923 char_attr = syntax_attr;
3924 else
3925 char_attr = hl_combine_attr(syntax_attr, char_attr);
3927 #endif
3929 #ifdef FEAT_SPELL
3930 /* Check spelling (unless at the end of the line).
3931 * Only do this when there is no syntax highlighting, the
3932 * @Spell cluster is not used or the current syntax item
3933 * contains the @Spell cluster. */
3934 if (has_spell && v >= word_end && v > cur_checked_col)
3936 spell_attr = 0;
3937 # ifdef FEAT_SYN_HL
3938 if (!attr_pri)
3939 char_attr = syntax_attr;
3940 # endif
3941 if (c != 0 && (
3942 # ifdef FEAT_SYN_HL
3943 !has_syntax ||
3944 # endif
3945 can_spell))
3947 char_u *prev_ptr, *p;
3948 int len;
3949 hlf_T spell_hlf = HLF_COUNT;
3950 # ifdef FEAT_MBYTE
3951 if (has_mbyte)
3953 prev_ptr = ptr - mb_l;
3954 v -= mb_l - 1;
3956 else
3957 # endif
3958 prev_ptr = ptr - 1;
3960 /* Use nextline[] if possible, it has the start of the
3961 * next line concatenated. */
3962 if ((prev_ptr - line) - nextlinecol >= 0)
3963 p = nextline + (prev_ptr - line) - nextlinecol;
3964 else
3965 p = prev_ptr;
3966 cap_col -= (int)(prev_ptr - line);
3967 len = spell_check(wp, p, &spell_hlf, &cap_col,
3968 nochange);
3969 word_end = v + len;
3971 /* In Insert mode only highlight a word that
3972 * doesn't touch the cursor. */
3973 if (spell_hlf != HLF_COUNT
3974 && (State & INSERT) != 0
3975 && wp->w_cursor.lnum == lnum
3976 && wp->w_cursor.col >=
3977 (colnr_T)(prev_ptr - line)
3978 && wp->w_cursor.col < (colnr_T)word_end)
3980 spell_hlf = HLF_COUNT;
3981 spell_redraw_lnum = lnum;
3984 if (spell_hlf == HLF_COUNT && p != prev_ptr
3985 && (p - nextline) + len > nextline_idx)
3987 /* Remember that the good word continues at the
3988 * start of the next line. */
3989 checked_lnum = lnum + 1;
3990 checked_col = (int)((p - nextline) + len - nextline_idx);
3993 /* Turn index into actual attributes. */
3994 if (spell_hlf != HLF_COUNT)
3995 spell_attr = highlight_attr[spell_hlf];
3997 if (cap_col > 0)
3999 if (p != prev_ptr
4000 && (p - nextline) + cap_col >= nextline_idx)
4002 /* Remember that the word in the next line
4003 * must start with a capital. */
4004 capcol_lnum = lnum + 1;
4005 cap_col = (int)((p - nextline) + cap_col
4006 - nextline_idx);
4008 else
4009 /* Compute the actual column. */
4010 cap_col += (int)(prev_ptr - line);
4014 if (spell_attr != 0)
4016 if (!attr_pri)
4017 char_attr = hl_combine_attr(char_attr, spell_attr);
4018 else
4019 char_attr = hl_combine_attr(spell_attr, char_attr);
4021 #endif
4022 #ifdef FEAT_LINEBREAK
4024 * Found last space before word: check for line break.
4026 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr)
4027 && !wp->w_p_list)
4029 n_extra = win_lbr_chartabsize(wp, ptr - (
4030 # ifdef FEAT_MBYTE
4031 has_mbyte ? mb_l :
4032 # endif
4033 1), (colnr_T)vcol, NULL) - 1;
4034 c_extra = ' ';
4035 if (vim_iswhite(c))
4036 c = ' ';
4038 #endif
4040 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4042 c = lcs_trail;
4043 if (!attr_pri)
4045 n_attr = 1;
4046 extra_attr = hl_attr(HLF_8);
4047 saved_attr2 = char_attr; /* save current attr */
4049 #ifdef FEAT_MBYTE
4050 mb_c = c;
4051 if (enc_utf8 && (*mb_char2len)(c) > 1)
4053 mb_utf8 = TRUE;
4054 u8cc[0] = 0;
4055 c = 0xc0;
4057 else
4058 mb_utf8 = FALSE;
4059 #endif
4064 * Handling of non-printable characters.
4066 if (!(chartab[c & 0xff] & CT_PRINT_CHAR))
4069 * when getting a character from the file, we may have to
4070 * turn it into something else on the way to putting it
4071 * into "ScreenLines".
4073 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4075 /* tab amount depends on current column */
4076 n_extra = (int)wp->w_buffer->b_p_ts
4077 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4078 #ifdef FEAT_MBYTE
4079 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4080 #endif
4081 if (wp->w_p_list)
4083 c = lcs_tab1;
4084 c_extra = lcs_tab2;
4085 n_attr = n_extra + 1;
4086 extra_attr = hl_attr(HLF_8);
4087 saved_attr2 = char_attr; /* save current attr */
4088 #ifdef FEAT_MBYTE
4089 mb_c = c;
4090 if (enc_utf8 && (*mb_char2len)(c) > 1)
4092 mb_utf8 = TRUE;
4093 u8cc[0] = 0;
4094 c = 0xc0;
4096 #endif
4098 else
4100 c_extra = ' ';
4101 c = ' ';
4104 else if (c == NUL
4105 && ((wp->w_p_list && lcs_eol > 0)
4106 || ((fromcol >= 0 || fromcol_prev >= 0)
4107 && tocol > vcol
4108 #ifdef FEAT_VISUAL
4109 && VIsual_mode != Ctrl_V
4110 #endif
4111 && (
4112 # ifdef FEAT_RIGHTLEFT
4113 wp->w_p_rl ? (col >= 0) :
4114 # endif
4115 (col < W_WIDTH(wp)))
4116 && !(noinvcur
4117 && (colnr_T)vcol == wp->w_virtcol)))
4118 && lcs_eol_one >= 0)
4120 /* Display a '$' after the line or highlight an extra
4121 * character if the line break is included. */
4122 #if defined(FEAT_DIFF) || defined(LINE_ATTR)
4123 /* For a diff line the highlighting continues after the
4124 * "$". */
4125 if (
4126 # ifdef FEAT_DIFF
4127 diff_hlf == (hlf_T)0
4128 # ifdef LINE_ATTR
4130 # endif
4131 # endif
4132 # ifdef LINE_ATTR
4133 line_attr == 0
4134 # endif
4136 #endif
4138 #ifdef FEAT_VIRTUALEDIT
4139 /* In virtualedit, visual selections may extend
4140 * beyond end of line. */
4141 if (area_highlighting && virtual_active()
4142 && tocol != MAXCOL && vcol < tocol)
4143 n_extra = 0;
4144 else
4145 #endif
4147 p_extra = at_end_str;
4148 n_extra = 1;
4149 c_extra = NUL;
4152 if (wp->w_p_list)
4153 c = lcs_eol;
4154 else
4155 c = ' ';
4156 lcs_eol_one = -1;
4157 --ptr; /* put it back at the NUL */
4158 if (!attr_pri)
4160 extra_attr = hl_attr(HLF_AT);
4161 n_attr = 1;
4163 #ifdef FEAT_MBYTE
4164 mb_c = c;
4165 if (enc_utf8 && (*mb_char2len)(c) > 1)
4167 mb_utf8 = TRUE;
4168 u8cc[0] = 0;
4169 c = 0xc0;
4171 else
4172 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4173 #endif
4175 else if (c != NUL)
4177 p_extra = transchar(c);
4178 #ifdef FEAT_RIGHTLEFT
4179 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4180 rl_mirror(p_extra); /* reverse "<12>" */
4181 #endif
4182 n_extra = byte2cells(c) - 1;
4183 c_extra = NUL;
4184 c = *p_extra++;
4185 if (!attr_pri)
4187 n_attr = n_extra + 1;
4188 extra_attr = hl_attr(HLF_8);
4189 saved_attr2 = char_attr; /* save current attr */
4191 #ifdef FEAT_MBYTE
4192 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4193 #endif
4195 #ifdef FEAT_VIRTUALEDIT
4196 else if (VIsual_active
4197 && (VIsual_mode == Ctrl_V
4198 || VIsual_mode == 'v')
4199 && virtual_active()
4200 && tocol != MAXCOL
4201 && vcol < tocol
4202 && (
4203 # ifdef FEAT_RIGHTLEFT
4204 wp->w_p_rl ? (col >= 0) :
4205 # endif
4206 (col < W_WIDTH(wp))))
4208 c = ' ';
4209 --ptr; /* put it back at the NUL */
4211 #endif
4212 #if defined(LINE_ATTR)
4213 else if ((
4214 # ifdef FEAT_DIFF
4215 diff_hlf != (hlf_T)0 ||
4216 # endif
4217 line_attr != 0
4218 ) && (
4219 # ifdef FEAT_RIGHTLEFT
4220 wp->w_p_rl ? (col >= 0) :
4221 # endif
4222 (col < W_WIDTH(wp))))
4224 /* Highlight until the right side of the window */
4225 c = ' ';
4226 --ptr; /* put it back at the NUL */
4228 /* Remember we do the char for line highlighting. */
4229 ++did_line_attr;
4231 /* don't do search HL for the rest of the line */
4232 if (line_attr != 0 && char_attr == search_attr && col > 0)
4233 char_attr = line_attr;
4234 # ifdef FEAT_DIFF
4235 if (diff_hlf == HLF_TXD)
4237 diff_hlf = HLF_CHD;
4238 if (attr == 0 || char_attr != attr)
4239 char_attr = hl_attr(diff_hlf);
4241 # endif
4243 #endif
4247 /* Don't override visual selection highlighting. */
4248 if (n_attr > 0
4249 && draw_state == WL_LINE
4250 && !attr_pri)
4251 char_attr = extra_attr;
4253 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
4254 /* XIM don't send preedit_start and preedit_end, but they send
4255 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
4256 * im_is_preediting() here. */
4257 if (xic != NULL
4258 && lnum == curwin->w_cursor.lnum
4259 && (State & INSERT)
4260 && !p_imdisable
4261 && im_is_preediting()
4262 && draw_state == WL_LINE)
4264 colnr_T tcol;
4266 if (preedit_end_col == MAXCOL)
4267 getvcol(curwin, &(curwin->w_cursor), &tcol, NULL, NULL);
4268 else
4269 tcol = preedit_end_col;
4270 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
4272 if (feedback_old_attr < 0)
4274 feedback_col = 0;
4275 feedback_old_attr = char_attr;
4277 char_attr = im_get_feedback_attr(feedback_col);
4278 if (char_attr < 0)
4279 char_attr = feedback_old_attr;
4280 feedback_col++;
4282 else if (feedback_old_attr >= 0)
4284 char_attr = feedback_old_attr;
4285 feedback_old_attr = -1;
4286 feedback_col = 0;
4289 #endif
4291 * Handle the case where we are in column 0 but not on the first
4292 * character of the line and the user wants us to show us a
4293 * special character (via 'listchars' option "precedes:<char>".
4295 if (lcs_prec_todo != NUL
4296 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
4297 #ifdef FEAT_DIFF
4298 && filler_todo <= 0
4299 #endif
4300 && draw_state > WL_NR
4301 && c != NUL)
4303 c = lcs_prec;
4304 lcs_prec_todo = NUL;
4305 #ifdef FEAT_MBYTE
4306 mb_c = c;
4307 if (enc_utf8 && (*mb_char2len)(c) > 1)
4309 mb_utf8 = TRUE;
4310 u8cc[0] = 0;
4311 c = 0xc0;
4313 else
4314 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4315 #endif
4316 if (!attr_pri)
4318 saved_attr3 = char_attr; /* save current attr */
4319 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
4320 n_attr3 = 1;
4325 * At end of the text line or just after the last character.
4327 if (c == NUL
4328 #if defined(LINE_ATTR)
4329 || did_line_attr == 1
4330 #endif
4333 #ifdef FEAT_SEARCH_EXTRA
4334 long prevcol = (long)(ptr - line) - (c == NUL);
4336 /* we're not really at that column when skipping some text */
4337 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
4338 ++prevcol;
4339 #endif
4341 /* invert at least one char, used for Visual and empty line or
4342 * highlight match at end of line. If it's beyond the last
4343 * char on the screen, just overwrite that one (tricky!) Not
4344 * needed when a '$' was displayed for 'list'. */
4345 #ifdef FEAT_SEARCH_EXTRA
4346 prevcol_hl_flag = FALSE;
4347 if (prevcol == (long)search_hl.startcol)
4348 prevcol_hl_flag = TRUE;
4349 else
4351 cur = wp->w_match_head;
4352 while (cur != NULL)
4354 if (prevcol == (long)cur->hl.startcol)
4356 prevcol_hl_flag = TRUE;
4357 break;
4359 cur = cur->next;
4362 #endif
4363 if (lcs_eol == lcs_eol_one
4364 && ((area_attr != 0 && vcol == fromcol && c == NUL)
4365 #ifdef FEAT_SEARCH_EXTRA
4366 /* highlight 'hlsearch' match at end of line */
4367 || (prevcol_hl_flag == TRUE
4368 # if defined(LINE_ATTR)
4369 && did_line_attr <= 1
4370 # endif
4372 #endif
4375 int n = 0;
4377 #ifdef FEAT_RIGHTLEFT
4378 if (wp->w_p_rl)
4380 if (col < 0)
4381 n = 1;
4383 else
4384 #endif
4386 if (col >= W_WIDTH(wp))
4387 n = -1;
4389 if (n != 0)
4391 /* At the window boundary, highlight the last character
4392 * instead (better than nothing). */
4393 off += n;
4394 col += n;
4396 else
4398 /* Add a blank character to highlight. */
4399 ScreenLines[off] = ' ';
4400 #ifdef FEAT_MBYTE
4401 if (enc_utf8)
4402 ScreenLinesUC[off] = 0;
4403 #endif
4405 #ifdef FEAT_SEARCH_EXTRA
4406 if (area_attr == 0)
4408 /* Use attributes from match with highest priority among
4409 * 'search_hl' and the match list. */
4410 char_attr = search_hl.attr;
4411 cur = wp->w_match_head;
4412 shl_flag = FALSE;
4413 while (cur != NULL || shl_flag == FALSE)
4415 if (shl_flag == FALSE
4416 && ((cur != NULL
4417 && cur->priority > SEARCH_HL_PRIORITY)
4418 || cur == NULL))
4420 shl = &search_hl;
4421 shl_flag = TRUE;
4423 else
4424 shl = &cur->hl;
4425 if ((ptr - line) - 1 == (long)shl->startcol)
4426 char_attr = shl->attr;
4427 if (shl != &search_hl && cur != NULL)
4428 cur = cur->next;
4431 #endif
4432 ScreenAttrs[off] = char_attr;
4433 #ifdef FEAT_RIGHTLEFT
4434 if (wp->w_p_rl)
4436 --col;
4437 --off;
4439 else
4440 #endif
4442 ++col;
4443 ++off;
4445 ++vcol;
4446 #ifdef FEAT_SYN_HL
4447 eol_hl_off = 1;
4448 #endif
4453 * At end of the text line.
4455 if (c == NUL)
4457 #ifdef FEAT_SYN_HL
4458 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol)
4460 /* highlight last char after line */
4461 --col;
4462 --off;
4463 --vcol;
4466 /* Highlight 'cursorcolumn' past end of the line. */
4467 if (wp->w_p_wrap)
4468 v = wp->w_skipcol;
4469 else
4470 v = wp->w_leftcol;
4471 /* check if line ends before left margin */
4472 if (vcol < v + col - win_col_off(wp))
4474 vcol = v + col - win_col_off(wp);
4475 if (wp->w_p_cuc
4476 && (int)wp->w_virtcol >= vcol - eol_hl_off
4477 && (int)wp->w_virtcol < W_WIDTH(wp) * (row - startrow + 1)
4479 && lnum != wp->w_cursor.lnum
4480 # ifdef FEAT_RIGHTLEFT
4481 && !wp->w_p_rl
4482 # endif
4485 while (col < W_WIDTH(wp))
4487 ScreenLines[off] = ' ';
4488 #ifdef FEAT_MBYTE
4489 if (enc_utf8)
4490 ScreenLinesUC[off] = 0;
4491 #endif
4492 ++col;
4493 if (vcol == (long)wp->w_virtcol)
4495 ScreenAttrs[off] = hl_attr(HLF_CUC);
4496 break;
4498 ScreenAttrs[off++] = 0;
4499 ++vcol;
4502 #endif
4504 SCREEN_LINE(screen_row, W_WINCOL(wp), col, (int)W_WIDTH(wp),
4505 wp->w_p_rl);
4506 row++;
4509 * Update w_cline_height and w_cline_folded if the cursor line was
4510 * updated (saves a call to plines() later).
4512 if (wp == curwin && lnum == curwin->w_cursor.lnum)
4514 curwin->w_cline_row = startrow;
4515 curwin->w_cline_height = row - startrow;
4516 #ifdef FEAT_FOLDING
4517 curwin->w_cline_folded = FALSE;
4518 #endif
4519 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
4522 break;
4525 /* line continues beyond line end */
4526 if (lcs_ext
4527 && !wp->w_p_wrap
4528 #ifdef FEAT_DIFF
4529 && filler_todo <= 0
4530 #endif
4531 && (
4532 #ifdef FEAT_RIGHTLEFT
4533 wp->w_p_rl ? col == 0 :
4534 #endif
4535 col == W_WIDTH(wp) - 1)
4536 && (*ptr != NUL
4537 || (wp->w_p_list && lcs_eol_one > 0)
4538 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
4540 c = lcs_ext;
4541 char_attr = hl_attr(HLF_AT);
4542 #ifdef FEAT_MBYTE
4543 mb_c = c;
4544 if (enc_utf8 && (*mb_char2len)(c) > 1)
4546 mb_utf8 = TRUE;
4547 u8cc[0] = 0;
4548 c = 0xc0;
4550 else
4551 mb_utf8 = FALSE;
4552 #endif
4555 #ifdef FEAT_SYN_HL
4556 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
4557 * highlight the cursor position itself. */
4558 if (wp->w_p_cuc && vcol == (long)wp->w_virtcol
4559 && lnum != wp->w_cursor.lnum
4560 && draw_state == WL_LINE)
4562 vcol_save_attr = char_attr;
4563 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
4565 else
4566 vcol_save_attr = -1;
4567 #endif
4570 * Store character to be displayed.
4571 * Skip characters that are left of the screen for 'nowrap'.
4573 vcol_prev = vcol;
4574 if (draw_state < WL_LINE || n_skip <= 0)
4577 * Store the character.
4579 #if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
4580 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
4582 /* A double-wide character is: put first halve in left cell. */
4583 --off;
4584 --col;
4586 #endif
4587 ScreenLines[off] = c;
4588 #ifdef FEAT_MBYTE
4589 if (enc_dbcs == DBCS_JPNU)
4590 ScreenLines2[off] = mb_c & 0xff;
4591 else if (enc_utf8)
4593 if (mb_utf8)
4595 int i;
4597 ScreenLinesUC[off] = mb_c;
4598 if ((c & 0xff) == 0)
4599 ScreenLines[off] = 0x80; /* avoid storing zero */
4600 for (i = 0; i < Screen_mco; ++i)
4602 ScreenLinesC[i][off] = u8cc[i];
4603 if (u8cc[i] == 0)
4604 break;
4607 else
4608 ScreenLinesUC[off] = 0;
4610 if (multi_attr)
4612 ScreenAttrs[off] = multi_attr;
4613 multi_attr = 0;
4615 else
4616 #endif
4617 ScreenAttrs[off] = char_attr;
4619 #ifdef FEAT_MBYTE
4620 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
4622 /* Need to fill two screen columns. */
4623 ++off;
4624 ++col;
4625 if (enc_utf8)
4626 /* UTF-8: Put a 0 in the second screen char. */
4627 ScreenLines[off] = 0;
4628 else
4629 /* DBCS: Put second byte in the second screen char. */
4630 ScreenLines[off] = mb_c & 0xff;
4631 ++vcol;
4632 /* When "tocol" is halfway a character, set it to the end of
4633 * the character, otherwise highlighting won't stop. */
4634 if (tocol == vcol)
4635 ++tocol;
4636 #ifdef FEAT_RIGHTLEFT
4637 if (wp->w_p_rl)
4639 /* now it's time to backup one cell */
4640 --off;
4641 --col;
4643 #endif
4645 #endif
4646 #ifdef FEAT_RIGHTLEFT
4647 if (wp->w_p_rl)
4649 --off;
4650 --col;
4652 else
4653 #endif
4655 ++off;
4656 ++col;
4659 else
4660 --n_skip;
4662 /* Only advance the "vcol" when after the 'number' column. */
4663 if (draw_state >= WL_SBR
4664 #ifdef FEAT_DIFF
4665 && filler_todo <= 0
4666 #endif
4668 ++vcol;
4670 #ifdef FEAT_SYN_HL
4671 if (vcol_save_attr >= 0)
4672 char_attr = vcol_save_attr;
4673 #endif
4675 /* restore attributes after "predeces" in 'listchars' */
4676 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
4677 char_attr = saved_attr3;
4679 /* restore attributes after last 'listchars' or 'number' char */
4680 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
4681 char_attr = saved_attr2;
4684 * At end of screen line and there is more to come: Display the line
4685 * so far. If there is no more to display it is caught above.
4687 if ((
4688 #ifdef FEAT_RIGHTLEFT
4689 wp->w_p_rl ? (col < 0) :
4690 #endif
4691 (col >= W_WIDTH(wp)))
4692 && (*ptr != NUL
4693 #ifdef FEAT_DIFF
4694 || filler_todo > 0
4695 #endif
4696 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
4697 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
4700 SCREEN_LINE(screen_row, W_WINCOL(wp), col, (int)W_WIDTH(wp),
4701 wp->w_p_rl);
4702 ++row;
4703 ++screen_row;
4705 /* When not wrapping and finished diff lines, or when displayed
4706 * '$' and highlighting until last column, break here. */
4707 if ((!wp->w_p_wrap
4708 #ifdef FEAT_DIFF
4709 && filler_todo <= 0
4710 #endif
4711 ) || lcs_eol_one == -1)
4712 break;
4714 /* When the window is too narrow draw all "@" lines. */
4715 if (draw_state != WL_LINE
4716 #ifdef FEAT_DIFF
4717 && filler_todo <= 0
4718 #endif
4721 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
4722 #ifdef FEAT_VERTSPLIT
4723 draw_vsep_win(wp, row);
4724 #endif
4725 row = endrow;
4728 /* When line got too long for screen break here. */
4729 if (row == endrow)
4731 ++row;
4732 break;
4735 if (screen_cur_row == screen_row - 1
4736 #ifdef FEAT_DIFF
4737 && filler_todo <= 0
4738 #endif
4739 && W_WIDTH(wp) == Columns)
4741 /* Remember that the line wraps, used for modeless copy. */
4742 LineWraps[screen_row - 1] = TRUE;
4745 * Special trick to make copy/paste of wrapped lines work with
4746 * xterm/screen: write an extra character beyond the end of
4747 * the line. This will work with all terminal types
4748 * (regardless of the xn,am settings).
4749 * Only do this on a fast tty.
4750 * Only do this if the cursor is on the current line
4751 * (something has been written in it).
4752 * Don't do this for the GUI.
4753 * Don't do this for double-width characters.
4754 * Don't do this for a window not at the right screen border.
4756 if (p_tf
4757 #ifdef FEAT_GUI
4758 && !gui.in_use
4759 #endif
4760 #ifdef FEAT_MBYTE
4761 && !(has_mbyte
4762 && ((*mb_off2cells)(LineOffset[screen_row],
4763 LineOffset[screen_row] + screen_Columns)
4764 == 2
4765 || (*mb_off2cells)(LineOffset[screen_row - 1]
4766 + (int)Columns - 2,
4767 LineOffset[screen_row] + screen_Columns)
4768 == 2))
4769 #endif
4772 /* First make sure we are at the end of the screen line,
4773 * then output the same character again to let the
4774 * terminal know about the wrap. If the terminal doesn't
4775 * auto-wrap, we overwrite the character. */
4776 if (screen_cur_col != W_WIDTH(wp))
4777 screen_char(LineOffset[screen_row - 1]
4778 + (unsigned)Columns - 1,
4779 screen_row - 1, (int)(Columns - 1));
4781 #ifdef FEAT_MBYTE
4782 /* When there is a multi-byte character, just output a
4783 * space to keep it simple. */
4784 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
4785 screen_row - 1] + (Columns - 1)]) > 1)
4786 out_char(' ');
4787 else
4788 #endif
4789 out_char(ScreenLines[LineOffset[screen_row - 1]
4790 + (Columns - 1)]);
4791 /* force a redraw of the first char on the next line */
4792 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
4793 screen_start(); /* don't know where cursor is now */
4797 col = 0;
4798 off = (unsigned)(current_ScreenLine - ScreenLines);
4799 #ifdef FEAT_RIGHTLEFT
4800 if (wp->w_p_rl)
4802 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
4803 off += col;
4805 #endif
4807 /* reset the drawing state for the start of a wrapped line */
4808 draw_state = WL_START;
4809 saved_n_extra = n_extra;
4810 saved_p_extra = p_extra;
4811 saved_c_extra = c_extra;
4812 saved_char_attr = char_attr;
4813 n_extra = 0;
4814 lcs_prec_todo = lcs_prec;
4815 #ifdef FEAT_LINEBREAK
4816 # ifdef FEAT_DIFF
4817 if (filler_todo <= 0)
4818 # endif
4819 need_showbreak = TRUE;
4820 #endif
4821 #ifdef FEAT_DIFF
4822 --filler_todo;
4823 /* When the filler lines are actually below the last line of the
4824 * file, don't draw the line itself, break here. */
4825 if (filler_todo == 0 && wp->w_botfill)
4826 break;
4827 #endif
4830 } /* for every character in the line */
4832 #ifdef FEAT_SPELL
4833 /* After an empty line check first word for capital. */
4834 if (*skipwhite(line) == NUL)
4836 capcol_lnum = lnum + 1;
4837 cap_col = 0;
4839 #endif
4841 return row;
4844 #ifdef FEAT_MBYTE
4845 static int comp_char_differs __ARGS((int, int));
4848 * Return if the composing characters at "off_from" and "off_to" differ.
4850 static int
4851 comp_char_differs(off_from, off_to)
4852 int off_from;
4853 int off_to;
4855 int i;
4857 for (i = 0; i < Screen_mco; ++i)
4859 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
4860 return TRUE;
4861 if (ScreenLinesC[i][off_from] == 0)
4862 break;
4864 return FALSE;
4866 #endif
4869 * Check whether the given character needs redrawing:
4870 * - the (first byte of the) character is different
4871 * - the attributes are different
4872 * - the character is multi-byte and the next byte is different
4873 * - the character is two cells wide and the second cell differs.
4875 static int
4876 char_needs_redraw(off_from, off_to, cols)
4877 int off_from;
4878 int off_to;
4879 int cols;
4881 if (cols > 0
4882 && ((ScreenLines[off_from] != ScreenLines[off_to]
4883 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
4885 #ifdef FEAT_MBYTE
4886 || (enc_dbcs != 0
4887 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
4888 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
4889 ? ScreenLines2[off_from] != ScreenLines2[off_to]
4890 : (cols > 1 && ScreenLines[off_from + 1]
4891 != ScreenLines[off_to + 1])))
4892 || (enc_utf8
4893 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
4894 || (ScreenLinesUC[off_from] != 0
4895 && comp_char_differs(off_from, off_to))
4896 || (cols > 1 && ScreenLines[off_from + 1]
4897 != ScreenLines[off_to + 1])))
4898 #endif
4900 return TRUE;
4901 return FALSE;
4905 * Move one "cooked" screen line to the screen, but only the characters that
4906 * have actually changed. Handle insert/delete character.
4907 * "coloff" gives the first column on the screen for this line.
4908 * "endcol" gives the columns where valid characters are.
4909 * "clear_width" is the width of the window. It's > 0 if the rest of the line
4910 * needs to be cleared, negative otherwise.
4911 * "rlflag" is TRUE in a rightleft window:
4912 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
4913 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
4915 static void
4916 screen_line(row, coloff, endcol, clear_width
4917 #ifdef FEAT_RIGHTLEFT
4918 , rlflag
4919 #endif
4921 int row;
4922 int coloff;
4923 int endcol;
4924 int clear_width;
4925 #ifdef FEAT_RIGHTLEFT
4926 int rlflag;
4927 #endif
4929 unsigned off_from;
4930 unsigned off_to;
4931 #ifdef FEAT_MBYTE
4932 unsigned max_off_from;
4933 unsigned max_off_to;
4934 #endif
4935 int col = 0;
4936 #if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_VERTSPLIT)
4937 int hl;
4938 #endif
4939 int force = FALSE; /* force update rest of the line */
4940 int redraw_this /* bool: does character need redraw? */
4941 #ifdef FEAT_GUI
4942 = TRUE /* For GUI when while-loop empty */
4943 #endif
4945 int redraw_next; /* redraw_this for next character */
4946 #ifdef FEAT_MBYTE
4947 int clear_next = FALSE;
4948 int char_cells; /* 1: normal char */
4949 /* 2: occupies two display cells */
4950 # define CHAR_CELLS char_cells
4951 #else
4952 # define CHAR_CELLS 1
4953 #endif
4955 # ifdef FEAT_CLIPBOARD
4956 clip_may_clear_selection(row, row);
4957 # endif
4959 off_from = (unsigned)(current_ScreenLine - ScreenLines);
4960 off_to = LineOffset[row] + coloff;
4961 #ifdef FEAT_MBYTE
4962 max_off_from = off_from + screen_Columns;
4963 max_off_to = LineOffset[row] + screen_Columns;
4964 #endif
4966 #ifdef FEAT_RIGHTLEFT
4967 if (rlflag)
4969 /* Clear rest first, because it's left of the text. */
4970 if (clear_width > 0)
4972 while (col <= endcol && ScreenLines[off_to] == ' '
4973 && ScreenAttrs[off_to] == 0
4974 # ifdef FEAT_MBYTE
4975 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
4976 # endif
4979 ++off_to;
4980 ++col;
4982 if (col <= endcol)
4983 screen_fill(row, row + 1, col + coloff,
4984 endcol + coloff + 1, ' ', ' ', 0);
4986 col = endcol + 1;
4987 off_to = LineOffset[row] + col + coloff;
4988 off_from += col;
4989 endcol = (clear_width > 0 ? clear_width : -clear_width);
4991 #endif /* FEAT_RIGHTLEFT */
4993 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
4995 while (col < endcol)
4997 #ifdef FEAT_MBYTE
4998 if (has_mbyte && (col + 1 < endcol))
4999 char_cells = (*mb_off2cells)(off_from, max_off_from);
5000 else
5001 char_cells = 1;
5002 #endif
5004 redraw_this = redraw_next;
5005 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
5006 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
5008 #ifdef FEAT_GUI
5009 /* If the next character was bold, then redraw the current character to
5010 * remove any pixels that might have spilt over into us. This only
5011 * happens in the GUI.
5013 if (redraw_next && gui.in_use)
5015 hl = ScreenAttrs[off_to + CHAR_CELLS];
5016 if (hl > HL_ALL)
5017 hl = syn_attr2attr(hl);
5018 if (hl & HL_BOLD)
5019 redraw_this = TRUE;
5021 #endif
5023 if (redraw_this)
5026 * Special handling when 'xs' termcap flag set (hpterm):
5027 * Attributes for characters are stored at the position where the
5028 * cursor is when writing the highlighting code. The
5029 * start-highlighting code must be written with the cursor on the
5030 * first highlighted character. The stop-highlighting code must
5031 * be written with the cursor just after the last highlighted
5032 * character.
5033 * Overwriting a character doesn't remove it's highlighting. Need
5034 * to clear the rest of the line, and force redrawing it
5035 * completely.
5037 if ( p_wiv
5038 && !force
5039 #ifdef FEAT_GUI
5040 && !gui.in_use
5041 #endif
5042 && ScreenAttrs[off_to] != 0
5043 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
5046 * Need to remove highlighting attributes here.
5048 windgoto(row, col + coloff);
5049 out_str(T_CE); /* clear rest of this screen line */
5050 screen_start(); /* don't know where cursor is now */
5051 force = TRUE; /* force redraw of rest of the line */
5052 redraw_next = TRUE; /* or else next char would miss out */
5055 * If the previous character was highlighted, need to stop
5056 * highlighting at this character.
5058 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
5060 screen_attr = ScreenAttrs[off_to - 1];
5061 term_windgoto(row, col + coloff);
5062 screen_stop_highlight();
5064 else
5065 screen_attr = 0; /* highlighting has stopped */
5067 #ifdef FEAT_MBYTE
5068 if (enc_dbcs != 0)
5070 /* Check if overwriting a double-byte with a single-byte or
5071 * the other way around requires another character to be
5072 * redrawn. For UTF-8 this isn't needed, because comparing
5073 * ScreenLinesUC[] is sufficient. */
5074 if (char_cells == 1
5075 && col + 1 < endcol
5076 && (*mb_off2cells)(off_to, max_off_to) > 1)
5078 /* Writing a single-cell character over a double-cell
5079 * character: need to redraw the next cell. */
5080 ScreenLines[off_to + 1] = 0;
5081 redraw_next = TRUE;
5083 else if (char_cells == 2
5084 && col + 2 < endcol
5085 && (*mb_off2cells)(off_to, max_off_to) == 1
5086 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
5088 /* Writing the second half of a double-cell character over
5089 * a double-cell character: need to redraw the second
5090 * cell. */
5091 ScreenLines[off_to + 2] = 0;
5092 redraw_next = TRUE;
5095 if (enc_dbcs == DBCS_JPNU)
5096 ScreenLines2[off_to] = ScreenLines2[off_from];
5098 /* When writing a single-width character over a double-width
5099 * character and at the end of the redrawn text, need to clear out
5100 * the right halve of the old character.
5101 * Also required when writing the right halve of a double-width
5102 * char over the left halve of an existing one. */
5103 if (has_mbyte && col + char_cells == endcol
5104 && ((char_cells == 1
5105 && (*mb_off2cells)(off_to, max_off_to) > 1)
5106 || (char_cells == 2
5107 && (*mb_off2cells)(off_to, max_off_to) == 1
5108 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
5109 clear_next = TRUE;
5110 #endif
5112 ScreenLines[off_to] = ScreenLines[off_from];
5113 #ifdef FEAT_MBYTE
5114 if (enc_utf8)
5116 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
5117 if (ScreenLinesUC[off_from] != 0)
5119 int i;
5121 for (i = 0; i < Screen_mco; ++i)
5122 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
5125 if (char_cells == 2)
5126 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
5127 #endif
5129 #if defined(FEAT_GUI) || defined(UNIX)
5130 /* The bold trick makes a single row of pixels appear in the next
5131 * character. When a bold character is removed, the next
5132 * character should be redrawn too. This happens for our own GUI
5133 * and for some xterms. */
5134 if (
5135 # ifdef FEAT_GUI
5136 gui.in_use
5137 # endif
5138 # if defined(FEAT_GUI) && defined(UNIX)
5140 # endif
5141 # ifdef UNIX
5142 term_is_xterm
5143 # endif
5146 hl = ScreenAttrs[off_to];
5147 if (hl > HL_ALL)
5148 hl = syn_attr2attr(hl);
5149 if (hl & HL_BOLD)
5150 redraw_next = TRUE;
5152 #endif
5153 ScreenAttrs[off_to] = ScreenAttrs[off_from];
5154 #ifdef FEAT_MBYTE
5155 /* For simplicity set the attributes of second half of a
5156 * double-wide character equal to the first half. */
5157 if (char_cells == 2)
5158 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
5160 if (enc_dbcs != 0 && char_cells == 2)
5161 screen_char_2(off_to, row, col + coloff);
5162 else
5163 #endif
5164 screen_char(off_to, row, col + coloff);
5166 else if ( p_wiv
5167 #ifdef FEAT_GUI
5168 && !gui.in_use
5169 #endif
5170 && col + coloff > 0)
5172 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
5175 * Don't output stop-highlight when moving the cursor, it will
5176 * stop the highlighting when it should continue.
5178 screen_attr = 0;
5180 else if (screen_attr != 0)
5181 screen_stop_highlight();
5184 off_to += CHAR_CELLS;
5185 off_from += CHAR_CELLS;
5186 col += CHAR_CELLS;
5189 #ifdef FEAT_MBYTE
5190 if (clear_next)
5192 /* Clear the second half of a double-wide character of which the left
5193 * half was overwritten with a single-wide character. */
5194 ScreenLines[off_to] = ' ';
5195 if (enc_utf8)
5196 ScreenLinesUC[off_to] = 0;
5197 screen_char(off_to, row, col + coloff);
5199 #endif
5201 if (clear_width > 0
5202 #ifdef FEAT_RIGHTLEFT
5203 && !rlflag
5204 #endif
5207 #ifdef FEAT_GUI
5208 int startCol = col;
5209 #endif
5211 /* blank out the rest of the line */
5212 while (col < clear_width && ScreenLines[off_to] == ' '
5213 && ScreenAttrs[off_to] == 0
5214 #ifdef FEAT_MBYTE
5215 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5216 #endif
5219 ++off_to;
5220 ++col;
5222 if (col < clear_width)
5224 #ifdef FEAT_GUI
5226 * In the GUI, clearing the rest of the line may leave pixels
5227 * behind if the first character cleared was bold. Some bold
5228 * fonts spill over the left. In this case we redraw the previous
5229 * character too. If we didn't skip any blanks above, then we
5230 * only redraw if the character wasn't already redrawn anyway.
5232 if (gui.in_use && (col > startCol || !redraw_this))
5234 hl = ScreenAttrs[off_to];
5235 if (hl > HL_ALL || (hl & HL_BOLD))
5237 int prev_cells = 1;
5238 # ifdef FEAT_MBYTE
5239 if (enc_utf8)
5240 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
5241 * that its width is 2. */
5242 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
5243 else if (enc_dbcs != 0)
5245 /* find previous character by counting from first
5246 * column and get its width. */
5247 unsigned off = LineOffset[row];
5248 unsigned max_off = LineOffset[row] + screen_Columns;
5250 while (off < off_to)
5252 prev_cells = (*mb_off2cells)(off, max_off);
5253 off += prev_cells;
5257 if (enc_dbcs != 0 && prev_cells > 1)
5258 screen_char_2(off_to - prev_cells, row,
5259 col + coloff - prev_cells);
5260 else
5261 # endif
5262 screen_char(off_to - prev_cells, row,
5263 col + coloff - prev_cells);
5266 #endif
5267 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
5268 ' ', ' ', 0);
5269 #ifdef FEAT_VERTSPLIT
5270 off_to += clear_width - col;
5271 col = clear_width;
5272 #endif
5276 if (clear_width > 0)
5278 #ifdef FEAT_VERTSPLIT
5279 /* For a window that's left of another, draw the separator char. */
5280 if (col + coloff < Columns)
5282 int c;
5284 c = fillchar_vsep(&hl);
5285 if (ScreenLines[off_to] != c
5286 # ifdef FEAT_MBYTE
5287 || (enc_utf8 && (int)ScreenLinesUC[off_to]
5288 != (c >= 0x80 ? c : 0))
5289 # endif
5290 || ScreenAttrs[off_to] != hl)
5292 ScreenLines[off_to] = c;
5293 ScreenAttrs[off_to] = hl;
5294 # ifdef FEAT_MBYTE
5295 if (enc_utf8)
5297 if (c >= 0x80)
5299 ScreenLinesUC[off_to] = c;
5300 ScreenLinesC[0][off_to] = 0;
5302 else
5303 ScreenLinesUC[off_to] = 0;
5305 # endif
5306 screen_char(off_to, row, col + coloff);
5309 else
5310 #endif
5311 LineWraps[row] = FALSE;
5315 #if defined(FEAT_RIGHTLEFT) || defined(PROTO)
5317 * Mirror text "str" for right-left displaying.
5318 * Only works for single-byte characters (e.g., numbers).
5320 void
5321 rl_mirror(str)
5322 char_u *str;
5324 char_u *p1, *p2;
5325 int t;
5327 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
5329 t = *p1;
5330 *p1 = *p2;
5331 *p2 = t;
5334 #endif
5336 #if defined(FEAT_WINDOWS) || defined(PROTO)
5338 * mark all status lines for redraw; used after first :cd
5340 void
5341 status_redraw_all()
5343 win_T *wp;
5345 for (wp = firstwin; wp; wp = wp->w_next)
5346 if (wp->w_status_height)
5348 wp->w_redr_status = TRUE;
5349 redraw_later(VALID);
5354 * mark all status lines of the current buffer for redraw
5356 void
5357 status_redraw_curbuf()
5359 win_T *wp;
5361 for (wp = firstwin; wp; wp = wp->w_next)
5362 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
5364 wp->w_redr_status = TRUE;
5365 redraw_later(VALID);
5370 * Redraw all status lines that need to be redrawn.
5372 void
5373 redraw_statuslines()
5375 win_T *wp;
5377 for (wp = firstwin; wp; wp = wp->w_next)
5378 if (wp->w_redr_status)
5379 win_redr_status(wp);
5380 if (redraw_tabline)
5381 draw_tabline();
5383 #endif
5385 #if (defined(FEAT_WILDMENU) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
5387 * Redraw all status lines at the bottom of frame "frp".
5389 void
5390 win_redraw_last_status(frp)
5391 frame_T *frp;
5393 if (frp->fr_layout == FR_LEAF)
5394 frp->fr_win->w_redr_status = TRUE;
5395 else if (frp->fr_layout == FR_ROW)
5397 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
5398 win_redraw_last_status(frp);
5400 else /* frp->fr_layout == FR_COL */
5402 frp = frp->fr_child;
5403 while (frp->fr_next != NULL)
5404 frp = frp->fr_next;
5405 win_redraw_last_status(frp);
5408 #endif
5410 #ifdef FEAT_VERTSPLIT
5412 * Draw the verticap separator right of window "wp" starting with line "row".
5414 static void
5415 draw_vsep_win(wp, row)
5416 win_T *wp;
5417 int row;
5419 int hl;
5420 int c;
5422 if (wp->w_vsep_width)
5424 /* draw the vertical separator right of this window */
5425 c = fillchar_vsep(&hl);
5426 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
5427 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
5428 c, ' ', hl);
5431 #endif
5433 #ifdef FEAT_WILDMENU
5434 static int status_match_len __ARGS((expand_T *xp, char_u *s));
5435 static int skip_status_match_char __ARGS((expand_T *xp, char_u *s));
5438 * Get the length of an item as it will be shown in the status line.
5440 static int
5441 status_match_len(xp, s)
5442 expand_T *xp;
5443 char_u *s;
5445 int len = 0;
5447 #ifdef FEAT_MENU
5448 int emenu = (xp->xp_context == EXPAND_MENUS
5449 || xp->xp_context == EXPAND_MENUNAMES);
5451 /* Check for menu separators - replace with '|'. */
5452 if (emenu && menu_is_separator(s))
5453 return 1;
5454 #endif
5456 while (*s != NUL)
5458 s += skip_status_match_char(xp, s);
5459 len += ptr2cells(s);
5460 mb_ptr_adv(s);
5463 return len;
5467 * Return the number of characters that should be skipped in a status match.
5468 * These are backslashes used for escaping. Do show backslashes in help tags.
5470 static int
5471 skip_status_match_char(xp, s)
5472 expand_T *xp;
5473 char_u *s;
5475 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
5476 #ifdef FEAT_MENU
5477 || ((xp->xp_context == EXPAND_MENUS
5478 || xp->xp_context == EXPAND_MENUNAMES)
5479 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
5480 #endif
5483 #ifndef BACKSLASH_IN_FILENAME
5484 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
5485 return 2;
5486 #endif
5487 return 1;
5489 return 0;
5493 * Show wildchar matches in the status line.
5494 * Show at least the "match" item.
5495 * We start at item 'first_match' in the list and show all matches that fit.
5497 * If inversion is possible we use it. Else '=' characters are used.
5499 void
5500 win_redr_status_matches(xp, num_matches, matches, match, showtail)
5501 expand_T *xp;
5502 int num_matches;
5503 char_u **matches; /* list of matches */
5504 int match;
5505 int showtail;
5507 #define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
5508 int row;
5509 char_u *buf;
5510 int len;
5511 int clen; /* length in screen cells */
5512 int fillchar;
5513 int attr;
5514 int i;
5515 int highlight = TRUE;
5516 char_u *selstart = NULL;
5517 int selstart_col = 0;
5518 char_u *selend = NULL;
5519 static int first_match = 0;
5520 int add_left = FALSE;
5521 char_u *s;
5522 #ifdef FEAT_MENU
5523 int emenu;
5524 #endif
5525 #if defined(FEAT_MBYTE) || defined(FEAT_MENU)
5526 int l;
5527 #endif
5529 if (matches == NULL) /* interrupted completion? */
5530 return;
5532 #ifdef FEAT_MBYTE
5533 if (has_mbyte)
5534 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
5535 else
5536 #endif
5537 buf = alloc((unsigned)Columns + 1);
5538 if (buf == NULL)
5539 return;
5541 if (match == -1) /* don't show match but original text */
5543 match = 0;
5544 highlight = FALSE;
5546 /* count 1 for the ending ">" */
5547 clen = status_match_len(xp, L_MATCH(match)) + 3;
5548 if (match == 0)
5549 first_match = 0;
5550 else if (match < first_match)
5552 /* jumping left, as far as we can go */
5553 first_match = match;
5554 add_left = TRUE;
5556 else
5558 /* check if match fits on the screen */
5559 for (i = first_match; i < match; ++i)
5560 clen += status_match_len(xp, L_MATCH(i)) + 2;
5561 if (first_match > 0)
5562 clen += 2;
5563 /* jumping right, put match at the left */
5564 if ((long)clen > Columns)
5566 first_match = match;
5567 /* if showing the last match, we can add some on the left */
5568 clen = 2;
5569 for (i = match; i < num_matches; ++i)
5571 clen += status_match_len(xp, L_MATCH(i)) + 2;
5572 if ((long)clen >= Columns)
5573 break;
5575 if (i == num_matches)
5576 add_left = TRUE;
5579 if (add_left)
5580 while (first_match > 0)
5582 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
5583 if ((long)clen >= Columns)
5584 break;
5585 --first_match;
5588 fillchar = fillchar_status(&attr, TRUE);
5590 if (first_match == 0)
5592 *buf = NUL;
5593 len = 0;
5595 else
5597 STRCPY(buf, "< ");
5598 len = 2;
5600 clen = len;
5602 i = first_match;
5603 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
5605 if (i == match)
5607 selstart = buf + len;
5608 selstart_col = clen;
5611 s = L_MATCH(i);
5612 /* Check for menu separators - replace with '|' */
5613 #ifdef FEAT_MENU
5614 emenu = (xp->xp_context == EXPAND_MENUS
5615 || xp->xp_context == EXPAND_MENUNAMES);
5616 if (emenu && menu_is_separator(s))
5618 STRCPY(buf + len, transchar('|'));
5619 l = (int)STRLEN(buf + len);
5620 len += l;
5621 clen += l;
5623 else
5624 #endif
5625 for ( ; *s != NUL; ++s)
5627 s += skip_status_match_char(xp, s);
5628 clen += ptr2cells(s);
5629 #ifdef FEAT_MBYTE
5630 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
5632 STRNCPY(buf + len, s, l);
5633 s += l - 1;
5634 len += l;
5636 else
5637 #endif
5639 STRCPY(buf + len, transchar_byte(*s));
5640 len += (int)STRLEN(buf + len);
5643 if (i == match)
5644 selend = buf + len;
5646 *(buf + len++) = ' ';
5647 *(buf + len++) = ' ';
5648 clen += 2;
5649 if (++i == num_matches)
5650 break;
5653 if (i != num_matches)
5655 *(buf + len++) = '>';
5656 ++clen;
5659 buf[len] = NUL;
5661 row = cmdline_row - 1;
5662 if (row >= 0)
5664 if (wild_menu_showing == 0)
5666 if (msg_scrolled > 0)
5668 /* Put the wildmenu just above the command line. If there is
5669 * no room, scroll the screen one line up. */
5670 if (cmdline_row == Rows - 1)
5672 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
5673 ++msg_scrolled;
5675 else
5677 ++cmdline_row;
5678 ++row;
5680 wild_menu_showing = WM_SCROLLED;
5682 else
5684 /* Create status line if needed by setting 'laststatus' to 2.
5685 * Set 'winminheight' to zero to avoid that the window is
5686 * resized. */
5687 if (lastwin->w_status_height == 0)
5689 save_p_ls = p_ls;
5690 save_p_wmh = p_wmh;
5691 p_ls = 2;
5692 p_wmh = 0;
5693 last_status(FALSE);
5695 wild_menu_showing = WM_SHOWN;
5699 screen_puts(buf, row, 0, attr);
5700 if (selstart != NULL && highlight)
5702 *selend = NUL;
5703 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
5706 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
5709 #ifdef FEAT_VERTSPLIT
5710 win_redraw_last_status(topframe);
5711 #else
5712 lastwin->w_redr_status = TRUE;
5713 #endif
5714 vim_free(buf);
5716 #endif
5718 #if defined(FEAT_WINDOWS) || defined(PROTO)
5720 * Redraw the status line of window wp.
5722 * If inversion is possible we use it. Else '=' characters are used.
5724 void
5725 win_redr_status(wp)
5726 win_T *wp;
5728 int row;
5729 char_u *p;
5730 int len;
5731 int fillchar;
5732 int attr;
5733 int this_ru_col;
5735 wp->w_redr_status = FALSE;
5736 if (wp->w_status_height == 0)
5738 /* no status line, can only be last window */
5739 redraw_cmdline = TRUE;
5741 else if (!redrawing()
5742 #ifdef FEAT_INS_EXPAND
5743 /* don't update status line when popup menu is visible and may be
5744 * drawn over it */
5745 || pum_visible()
5746 #endif
5749 /* Don't redraw right now, do it later. */
5750 wp->w_redr_status = TRUE;
5752 #ifdef FEAT_STL_OPT
5753 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
5755 /* redraw custom status line */
5756 redraw_custum_statusline(wp);
5758 #endif
5759 else
5761 fillchar = fillchar_status(&attr, wp == curwin);
5763 get_trans_bufname(wp->w_buffer);
5764 p = NameBuff;
5765 len = (int)STRLEN(p);
5767 if (wp->w_buffer->b_help
5768 #ifdef FEAT_QUICKFIX
5769 || wp->w_p_pvw
5770 #endif
5771 || bufIsChanged(wp->w_buffer)
5772 || wp->w_buffer->b_p_ro)
5773 *(p + len++) = ' ';
5774 if (wp->w_buffer->b_help)
5776 STRCPY(p + len, _("[Help]"));
5777 len += (int)STRLEN(p + len);
5779 #ifdef FEAT_QUICKFIX
5780 if (wp->w_p_pvw)
5782 STRCPY(p + len, _("[Preview]"));
5783 len += (int)STRLEN(p + len);
5785 #endif
5786 if (bufIsChanged(wp->w_buffer))
5788 STRCPY(p + len, "[+]");
5789 len += 3;
5791 if (wp->w_buffer->b_p_ro)
5793 STRCPY(p + len, "[RO]");
5794 len += 4;
5797 #ifndef FEAT_VERTSPLIT
5798 this_ru_col = ru_col;
5799 if (this_ru_col < (Columns + 1) / 2)
5800 this_ru_col = (Columns + 1) / 2;
5801 #else
5802 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
5803 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
5804 this_ru_col = (W_WIDTH(wp) + 1) / 2;
5805 if (this_ru_col <= 1)
5807 p = (char_u *)"<"; /* No room for file name! */
5808 len = 1;
5810 else
5811 #endif
5812 #ifdef FEAT_MBYTE
5813 if (has_mbyte)
5815 int clen = 0, i;
5817 /* Count total number of display cells. */
5818 for (i = 0; p[i] != NUL; i += (*mb_ptr2len)(p + i))
5819 clen += (*mb_ptr2cells)(p + i);
5820 /* Find first character that will fit.
5821 * Going from start to end is much faster for DBCS. */
5822 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
5823 i += (*mb_ptr2len)(p + i))
5824 clen -= (*mb_ptr2cells)(p + i);
5825 len = clen;
5826 if (i > 0)
5828 p = p + i - 1;
5829 *p = '<';
5830 ++len;
5834 else
5835 #endif
5836 if (len > this_ru_col - 1)
5838 p += len - (this_ru_col - 1);
5839 *p = '<';
5840 len = this_ru_col - 1;
5843 row = W_WINROW(wp) + wp->w_height;
5844 screen_puts(p, row, W_WINCOL(wp), attr);
5845 screen_fill(row, row + 1, len + W_WINCOL(wp),
5846 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
5848 if (get_keymap_str(wp, NameBuff, MAXPATHL)
5849 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
5850 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
5851 - 1 + W_WINCOL(wp)), attr);
5853 #ifdef FEAT_CMDL_INFO
5854 win_redr_ruler(wp, TRUE);
5855 #endif
5858 #ifdef FEAT_VERTSPLIT
5860 * May need to draw the character below the vertical separator.
5862 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
5864 if (stl_connected(wp))
5865 fillchar = fillchar_status(&attr, wp == curwin);
5866 else
5867 fillchar = fillchar_vsep(&attr);
5868 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
5869 attr);
5871 #endif
5874 #ifdef FEAT_STL_OPT
5876 * Redraw the status line according to 'statusline' and take care of any
5877 * errors encountered.
5879 static void
5880 redraw_custum_statusline(wp)
5881 win_T *wp;
5883 int save_called_emsg = called_emsg;
5885 called_emsg = FALSE;
5886 win_redr_custom(wp, FALSE);
5887 if (called_emsg)
5888 set_string_option_direct((char_u *)"statusline", -1,
5889 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
5890 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
5891 called_emsg |= save_called_emsg;
5893 #endif
5895 # ifdef FEAT_VERTSPLIT
5897 * Return TRUE if the status line of window "wp" is connected to the status
5898 * line of the window right of it. If not, then it's a vertical separator.
5899 * Only call if (wp->w_vsep_width != 0).
5902 stl_connected(wp)
5903 win_T *wp;
5905 frame_T *fr;
5907 fr = wp->w_frame;
5908 while (fr->fr_parent != NULL)
5910 if (fr->fr_parent->fr_layout == FR_COL)
5912 if (fr->fr_next != NULL)
5913 break;
5915 else
5917 if (fr->fr_next != NULL)
5918 return TRUE;
5920 fr = fr->fr_parent;
5922 return FALSE;
5924 # endif
5926 #endif /* FEAT_WINDOWS */
5928 #if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
5930 * Get the value to show for the language mappings, active 'keymap'.
5933 get_keymap_str(wp, buf, len)
5934 win_T *wp;
5935 char_u *buf; /* buffer for the result */
5936 int len; /* length of buffer */
5938 char_u *p;
5940 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
5941 return FALSE;
5944 #ifdef FEAT_EVAL
5945 buf_T *old_curbuf = curbuf;
5946 win_T *old_curwin = curwin;
5947 char_u *s;
5949 curbuf = wp->w_buffer;
5950 curwin = wp;
5951 STRCPY(buf, "b:keymap_name"); /* must be writable */
5952 ++emsg_skip;
5953 s = p = eval_to_string(buf, NULL, FALSE);
5954 --emsg_skip;
5955 curbuf = old_curbuf;
5956 curwin = old_curwin;
5957 if (p == NULL || *p == NUL)
5958 #endif
5960 #ifdef FEAT_KEYMAP
5961 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
5962 p = wp->w_buffer->b_p_keymap;
5963 else
5964 #endif
5965 p = (char_u *)"lang";
5967 if ((int)(STRLEN(p) + 3) < len)
5968 sprintf((char *)buf, "<%s>", p);
5969 else
5970 buf[0] = NUL;
5971 #ifdef FEAT_EVAL
5972 vim_free(s);
5973 #endif
5975 return buf[0] != NUL;
5977 #endif
5979 #if defined(FEAT_STL_OPT) || defined(PROTO)
5981 * Redraw the status line or ruler of window "wp".
5982 * When "wp" is NULL redraw the tab pages line from 'tabline'.
5984 static void
5985 win_redr_custom(wp, draw_ruler)
5986 win_T *wp;
5987 int draw_ruler; /* TRUE or FALSE */
5989 int attr;
5990 int curattr;
5991 int row;
5992 int col = 0;
5993 int maxwidth;
5994 int width;
5995 int n;
5996 int len;
5997 int fillchar;
5998 char_u buf[MAXPATHL];
5999 char_u *p;
6000 struct stl_hlrec hltab[STL_MAX_ITEM];
6001 struct stl_hlrec tabtab[STL_MAX_ITEM];
6002 int use_sandbox = FALSE;
6004 /* setup environment for the task at hand */
6005 if (wp == NULL)
6007 /* Use 'tabline'. Always at the first line of the screen. */
6008 p = p_tal;
6009 row = 0;
6010 fillchar = ' ';
6011 attr = hl_attr(HLF_TPF);
6012 maxwidth = Columns;
6013 # ifdef FEAT_EVAL
6014 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
6015 # endif
6017 else
6019 row = W_WINROW(wp) + wp->w_height;
6020 fillchar = fillchar_status(&attr, wp == curwin);
6021 maxwidth = W_WIDTH(wp);
6023 if (draw_ruler)
6025 p = p_ruf;
6026 /* advance past any leading group spec - implicit in ru_col */
6027 if (*p == '%')
6029 if (*++p == '-')
6030 p++;
6031 if (atoi((char *) p))
6032 while (VIM_ISDIGIT(*p))
6033 p++;
6034 if (*p++ != '(')
6035 p = p_ruf;
6037 #ifdef FEAT_VERTSPLIT
6038 col = ru_col - (Columns - W_WIDTH(wp));
6039 if (col < (W_WIDTH(wp) + 1) / 2)
6040 col = (W_WIDTH(wp) + 1) / 2;
6041 #else
6042 col = ru_col;
6043 if (col > (Columns + 1) / 2)
6044 col = (Columns + 1) / 2;
6045 #endif
6046 maxwidth = W_WIDTH(wp) - col;
6047 #ifdef FEAT_WINDOWS
6048 if (!wp->w_status_height)
6049 #endif
6051 row = Rows - 1;
6052 --maxwidth; /* writing in last column may cause scrolling */
6053 fillchar = ' ';
6054 attr = 0;
6057 # ifdef FEAT_EVAL
6058 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
6059 # endif
6061 else
6063 if (*wp->w_p_stl != NUL)
6064 p = wp->w_p_stl;
6065 else
6066 p = p_stl;
6067 # ifdef FEAT_EVAL
6068 use_sandbox = was_set_insecurely((char_u *)"statusline",
6069 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
6070 # endif
6073 #ifdef FEAT_VERTSPLIT
6074 col += W_WINCOL(wp);
6075 #endif
6078 if (maxwidth <= 0)
6079 return;
6081 width = build_stl_str_hl(wp == NULL ? curwin : wp,
6082 buf, sizeof(buf),
6083 p, use_sandbox,
6084 fillchar, maxwidth, hltab, tabtab);
6085 len = (int)STRLEN(buf);
6087 while (width < maxwidth && len < sizeof(buf) - 1)
6089 #ifdef FEAT_MBYTE
6090 len += (*mb_char2bytes)(fillchar, buf + len);
6091 #else
6092 buf[len++] = fillchar;
6093 #endif
6094 ++width;
6096 buf[len] = NUL;
6099 * Draw each snippet with the specified highlighting.
6101 curattr = attr;
6102 p = buf;
6103 for (n = 0; hltab[n].start != NULL; n++)
6105 len = (int)(hltab[n].start - p);
6106 screen_puts_len(p, len, row, col, curattr);
6107 col += vim_strnsize(p, len);
6108 p = hltab[n].start;
6110 if (hltab[n].userhl == 0)
6111 curattr = attr;
6112 else if (hltab[n].userhl < 0)
6113 curattr = syn_id2attr(-hltab[n].userhl);
6114 #ifdef FEAT_WINDOWS
6115 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
6116 curattr = highlight_stlnc[hltab[n].userhl - 1];
6117 #endif
6118 else
6119 curattr = highlight_user[hltab[n].userhl - 1];
6121 screen_puts(p, row, col, curattr);
6123 if (wp == NULL)
6125 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
6126 col = 0;
6127 len = 0;
6128 p = buf;
6129 fillchar = 0;
6130 for (n = 0; tabtab[n].start != NULL; n++)
6132 len += vim_strnsize(p, (int)(tabtab[n].start - p));
6133 while (col < len)
6134 TabPageIdxs[col++] = fillchar;
6135 p = tabtab[n].start;
6136 fillchar = tabtab[n].userhl;
6138 while (col < Columns)
6139 TabPageIdxs[col++] = fillchar;
6143 #endif /* FEAT_STL_OPT */
6146 * Output a single character directly to the screen and update ScreenLines.
6148 void
6149 screen_putchar(c, row, col, attr)
6150 int c;
6151 int row, col;
6152 int attr;
6154 #ifdef FEAT_MBYTE
6155 char_u buf[MB_MAXBYTES + 1];
6157 buf[(*mb_char2bytes)(c, buf)] = NUL;
6158 #else
6159 char_u buf[2];
6161 buf[0] = c;
6162 buf[1] = NUL;
6163 #endif
6164 screen_puts(buf, row, col, attr);
6168 * Get a single character directly from ScreenLines into "bytes[]".
6169 * Also return its attribute in *attrp;
6171 void
6172 screen_getbytes(row, col, bytes, attrp)
6173 int row, col;
6174 char_u *bytes;
6175 int *attrp;
6177 unsigned off;
6179 /* safety check */
6180 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
6182 off = LineOffset[row] + col;
6183 *attrp = ScreenAttrs[off];
6184 bytes[0] = ScreenLines[off];
6185 bytes[1] = NUL;
6187 #ifdef FEAT_MBYTE
6188 if (enc_utf8 && ScreenLinesUC[off] != 0)
6189 bytes[utfc_char2bytes(off, bytes)] = NUL;
6190 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6192 bytes[0] = ScreenLines[off];
6193 bytes[1] = ScreenLines2[off];
6194 bytes[2] = NUL;
6196 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
6198 bytes[1] = ScreenLines[off + 1];
6199 bytes[2] = NUL;
6201 #endif
6205 #ifdef FEAT_MBYTE
6206 static int screen_comp_differs __ARGS((int, int*));
6209 * Return TRUE if composing characters for screen posn "off" differs from
6210 * composing characters in "u8cc".
6212 static int
6213 screen_comp_differs(off, u8cc)
6214 int off;
6215 int *u8cc;
6217 int i;
6219 for (i = 0; i < Screen_mco; ++i)
6221 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
6222 return TRUE;
6223 if (u8cc[i] == 0)
6224 break;
6226 return FALSE;
6228 #endif
6231 * Put string '*text' on the screen at position 'row' and 'col', with
6232 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
6233 * Note: only outputs within one row, message is truncated at screen boundary!
6234 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
6236 void
6237 screen_puts(text, row, col, attr)
6238 char_u *text;
6239 int row;
6240 int col;
6241 int attr;
6243 screen_puts_len(text, -1, row, col, attr);
6247 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
6248 * a NUL.
6250 void
6251 screen_puts_len(text, len, row, col, attr)
6252 char_u *text;
6253 int len;
6254 int row;
6255 int col;
6256 int attr;
6258 unsigned off;
6259 char_u *ptr = text;
6260 int c;
6261 #ifdef FEAT_MBYTE
6262 unsigned max_off;
6263 int mbyte_blen = 1;
6264 int mbyte_cells = 1;
6265 int u8c = 0;
6266 int u8cc[MAX_MCO];
6267 int clear_next_cell = FALSE;
6268 # ifdef FEAT_ARABIC
6269 int prev_c = 0; /* previous Arabic character */
6270 int pc, nc, nc1;
6271 int pcc[MAX_MCO];
6272 # endif
6273 #endif
6275 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
6276 return;
6278 #ifdef FEAT_MBYTE
6279 /* When drawing over the right halve of a double-wide char clear out the
6280 * left halve. Only needed in a terminal. */
6281 if (has_mbyte && col > 0 && col < screen_Columns
6282 # ifdef FEAT_GUI
6283 && !gui.in_use
6284 # endif
6285 && mb_fix_col(col, row) != col)
6286 screen_puts_len((char_u *)" ", 1, row, col - 1, 0);
6287 #endif
6289 off = LineOffset[row] + col;
6290 #ifdef FEAT_MBYTE
6291 max_off = LineOffset[row] + screen_Columns;
6292 #endif
6293 while (col < screen_Columns
6294 && (len < 0 || (int)(ptr - text) < len)
6295 && *ptr != NUL)
6297 c = *ptr;
6298 #ifdef FEAT_MBYTE
6299 /* check if this is the first byte of a multibyte */
6300 if (has_mbyte)
6302 if (enc_utf8 && len > 0)
6303 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
6304 else
6305 mbyte_blen = (*mb_ptr2len)(ptr);
6306 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6307 mbyte_cells = 1;
6308 else if (enc_dbcs != 0)
6309 mbyte_cells = mbyte_blen;
6310 else /* enc_utf8 */
6312 if (len >= 0)
6313 u8c = utfc_ptr2char_len(ptr, u8cc,
6314 (int)((text + len) - ptr));
6315 else
6316 u8c = utfc_ptr2char(ptr, u8cc);
6317 mbyte_cells = utf_char2cells(u8c);
6318 # ifdef UNICODE16
6319 /* Non-BMP character: display as ? or fullwidth ?. */
6320 if (u8c >= 0x10000)
6322 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
6323 if (attr == 0)
6324 attr = hl_attr(HLF_8);
6326 # endif
6327 # ifdef FEAT_ARABIC
6328 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
6330 /* Do Arabic shaping. */
6331 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
6333 /* Past end of string to be displayed. */
6334 nc = NUL;
6335 nc1 = NUL;
6337 else
6339 nc = utfc_ptr2char(ptr + mbyte_blen, pcc);
6340 nc1 = pcc[0];
6342 pc = prev_c;
6343 prev_c = u8c;
6344 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
6346 else
6347 prev_c = u8c;
6348 # endif
6351 #endif
6353 if (ScreenLines[off] != c
6354 #ifdef FEAT_MBYTE
6355 || (mbyte_cells == 2
6356 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
6357 || (enc_dbcs == DBCS_JPNU
6358 && c == 0x8e
6359 && ScreenLines2[off] != ptr[1])
6360 || (enc_utf8
6361 && (ScreenLinesUC[off] != (u8char_T)u8c
6362 || screen_comp_differs(off, u8cc)))
6363 #endif
6364 || ScreenAttrs[off] != attr
6365 || exmode_active
6368 #if defined(FEAT_GUI) || defined(UNIX)
6369 /* The bold trick makes a single row of pixels appear in the next
6370 * character. When a bold character is removed, the next
6371 * character should be redrawn too. This happens for our own GUI
6372 * and for some xterms.
6373 * Force the redraw by setting the attribute to a different value
6374 * than "attr", the contents of ScreenLines[] may be needed by
6375 * mb_off2cells() further on.
6376 * Don't do this for the last drawn character, because the next
6377 * character may not be redrawn. */
6378 if (
6379 # ifdef FEAT_GUI
6380 gui.in_use
6381 # endif
6382 # if defined(FEAT_GUI) && defined(UNIX)
6384 # endif
6385 # ifdef UNIX
6386 term_is_xterm
6387 # endif
6390 int n;
6392 n = ScreenAttrs[off];
6393 # ifdef FEAT_MBYTE
6394 if (col + mbyte_cells < screen_Columns
6395 && (n > HL_ALL || (n & HL_BOLD))
6396 && (len < 0 ? ptr[mbyte_blen] != NUL
6397 : ptr + mbyte_blen < text + len))
6398 ScreenAttrs[off + mbyte_cells] = attr + 1;
6399 # else
6400 if (col + 1 < screen_Columns
6401 && (n > HL_ALL || (n & HL_BOLD))
6402 && (len < 0 ? ptr[1] != NUL : ptr + 1 < text + len))
6403 ScreenLines[off + 1] = 0;
6404 # endif
6406 #endif
6407 #ifdef FEAT_MBYTE
6408 /* When at the end of the text and overwriting a two-cell
6409 * character with a one-cell character, need to clear the next
6410 * cell. Also when overwriting the left halve of a two-cell char
6411 * with the right halve of a two-cell char. Do this only once
6412 * (mb_off2cells() may return 2 on the right halve). */
6413 if (clear_next_cell)
6414 clear_next_cell = FALSE;
6415 else if (has_mbyte
6416 && (len < 0 ? ptr[mbyte_blen] == NUL
6417 : ptr + mbyte_blen >= text + len)
6418 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
6419 || (mbyte_cells == 2
6420 && (*mb_off2cells)(off, max_off) == 1
6421 && (*mb_off2cells)(off + 1, max_off) > 1)))
6422 clear_next_cell = TRUE;
6424 /* Make sure we never leave a second byte of a double-byte behind,
6425 * it confuses mb_off2cells(). */
6426 if (enc_dbcs
6427 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
6428 || (mbyte_cells == 2
6429 && (*mb_off2cells)(off, max_off) == 1
6430 && (*mb_off2cells)(off + 1, max_off) > 1)))
6431 ScreenLines[off + mbyte_blen] = 0;
6432 #endif
6433 ScreenLines[off] = c;
6434 ScreenAttrs[off] = attr;
6435 #ifdef FEAT_MBYTE
6436 if (enc_utf8)
6438 if (c < 0x80 && u8cc[0] == 0)
6439 ScreenLinesUC[off] = 0;
6440 else
6442 int i;
6444 ScreenLinesUC[off] = u8c;
6445 for (i = 0; i < Screen_mco; ++i)
6447 ScreenLinesC[i][off] = u8cc[i];
6448 if (u8cc[i] == 0)
6449 break;
6452 if (mbyte_cells == 2)
6454 ScreenLines[off + 1] = 0;
6455 ScreenAttrs[off + 1] = attr;
6457 screen_char(off, row, col);
6459 else if (mbyte_cells == 2)
6461 ScreenLines[off + 1] = ptr[1];
6462 ScreenAttrs[off + 1] = attr;
6463 screen_char_2(off, row, col);
6465 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6467 ScreenLines2[off] = ptr[1];
6468 screen_char(off, row, col);
6470 else
6471 #endif
6472 screen_char(off, row, col);
6474 #ifdef FEAT_MBYTE
6475 if (has_mbyte)
6477 off += mbyte_cells;
6478 col += mbyte_cells;
6479 ptr += mbyte_blen;
6480 if (clear_next_cell)
6481 ptr = (char_u *)" ";
6483 else
6484 #endif
6486 ++off;
6487 ++col;
6488 ++ptr;
6493 #ifdef FEAT_SEARCH_EXTRA
6495 * Prepare for 'hlsearch' highlighting.
6497 static void
6498 start_search_hl()
6500 if (p_hls && !no_hlsearch)
6502 last_pat_prog(&search_hl.rm);
6503 search_hl.attr = hl_attr(HLF_L);
6504 # ifdef FEAT_RELTIME
6505 /* Set the time limit to 'redrawtime'. */
6506 profile_setlimit(p_rdt, &search_hl.tm);
6507 # endif
6512 * Clean up for 'hlsearch' highlighting.
6514 static void
6515 end_search_hl()
6517 if (search_hl.rm.regprog != NULL)
6519 vim_free(search_hl.rm.regprog);
6520 search_hl.rm.regprog = NULL;
6525 * Advance to the match in window "wp" line "lnum" or past it.
6527 static void
6528 prepare_search_hl(wp, lnum)
6529 win_T *wp;
6530 linenr_T lnum;
6532 matchitem_T *cur; /* points to the match list */
6533 match_T *shl; /* points to search_hl or a match */
6534 int shl_flag; /* flag to indicate whether search_hl
6535 has been processed or not */
6536 int n;
6539 * When using a multi-line pattern, start searching at the top
6540 * of the window or just after a closed fold.
6541 * Do this both for search_hl and the match list.
6543 cur = wp->w_match_head;
6544 shl_flag = FALSE;
6545 while (cur != NULL || shl_flag == FALSE)
6547 if (shl_flag == FALSE)
6549 shl = &search_hl;
6550 shl_flag = TRUE;
6552 else
6553 shl = &cur->hl;
6554 if (shl->rm.regprog != NULL
6555 && shl->lnum == 0
6556 && re_multiline(shl->rm.regprog))
6558 if (shl->first_lnum == 0)
6560 # ifdef FEAT_FOLDING
6561 for (shl->first_lnum = lnum;
6562 shl->first_lnum > wp->w_topline; --shl->first_lnum)
6563 if (hasFoldingWin(wp, shl->first_lnum - 1,
6564 NULL, NULL, TRUE, NULL))
6565 break;
6566 # else
6567 shl->first_lnum = wp->w_topline;
6568 # endif
6570 n = 0;
6571 while (shl->first_lnum < lnum && shl->rm.regprog != NULL)
6573 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n);
6574 if (shl->lnum != 0)
6576 shl->first_lnum = shl->lnum
6577 + shl->rm.endpos[0].lnum
6578 - shl->rm.startpos[0].lnum;
6579 n = shl->rm.endpos[0].col;
6581 else
6583 ++shl->first_lnum;
6584 n = 0;
6588 if (shl != &search_hl && cur != NULL)
6589 cur = cur->next;
6594 * Search for a next 'hlsearch' or match.
6595 * Uses shl->buf.
6596 * Sets shl->lnum and shl->rm contents.
6597 * Note: Assumes a previous match is always before "lnum", unless
6598 * shl->lnum is zero.
6599 * Careful: Any pointers for buffer lines will become invalid.
6601 static void
6602 next_search_hl(win, shl, lnum, mincol)
6603 win_T *win;
6604 match_T *shl; /* points to search_hl or a match */
6605 linenr_T lnum;
6606 colnr_T mincol; /* minimal column for a match */
6608 linenr_T l;
6609 colnr_T matchcol;
6610 long nmatched;
6612 if (shl->lnum != 0)
6614 /* Check for three situations:
6615 * 1. If the "lnum" is below a previous match, start a new search.
6616 * 2. If the previous match includes "mincol", use it.
6617 * 3. Continue after the previous match.
6619 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
6620 if (lnum > l)
6621 shl->lnum = 0;
6622 else if (lnum < l || shl->rm.endpos[0].col > mincol)
6623 return;
6627 * Repeat searching for a match until one is found that includes "mincol"
6628 * or none is found in this line.
6630 called_emsg = FALSE;
6631 for (;;)
6633 #ifdef FEAT_RELTIME
6634 /* Stop searching after passing the time limit. */
6635 if (profile_passed_limit(&(shl->tm)))
6637 shl->lnum = 0; /* no match found in time */
6638 break;
6640 #endif
6641 /* Three situations:
6642 * 1. No useful previous match: search from start of line.
6643 * 2. Not Vi compatible or empty match: continue at next character.
6644 * Break the loop if this is beyond the end of the line.
6645 * 3. Vi compatible searching: continue at end of previous match.
6647 if (shl->lnum == 0)
6648 matchcol = 0;
6649 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
6650 || (shl->rm.endpos[0].lnum == 0
6651 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
6653 char_u *ml;
6655 matchcol = shl->rm.startpos[0].col;
6656 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
6657 if (*ml == NUL)
6659 ++matchcol;
6660 shl->lnum = 0;
6661 break;
6663 #ifdef FEAT_MBYTE
6664 if (has_mbyte)
6665 matchcol += mb_ptr2len(ml);
6666 else
6667 #endif
6668 ++matchcol;
6670 else
6671 matchcol = shl->rm.endpos[0].col;
6673 shl->lnum = lnum;
6674 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum, matchcol,
6675 #ifdef FEAT_RELTIME
6676 &(shl->tm)
6677 #else
6678 NULL
6679 #endif
6681 if (called_emsg)
6683 /* Error while handling regexp: stop using this regexp. */
6684 if (shl == &search_hl)
6686 /* don't free regprog in the match list, it's a copy */
6687 vim_free(shl->rm.regprog);
6688 no_hlsearch = TRUE;
6690 shl->rm.regprog = NULL;
6691 shl->lnum = 0;
6692 got_int = FALSE; /* avoid the "Type :quit to exit Vim" message */
6693 break;
6695 if (nmatched == 0)
6697 shl->lnum = 0; /* no match found */
6698 break;
6700 if (shl->rm.startpos[0].lnum > 0
6701 || shl->rm.startpos[0].col >= mincol
6702 || nmatched > 1
6703 || shl->rm.endpos[0].col > mincol)
6705 shl->lnum += shl->rm.startpos[0].lnum;
6706 break; /* useful match found */
6710 #endif
6712 static void
6713 screen_start_highlight(attr)
6714 int attr;
6716 attrentry_T *aep = NULL;
6718 screen_attr = attr;
6719 if (full_screen
6720 #ifdef WIN3264
6721 && termcap_active
6722 #endif
6725 #ifdef FEAT_GUI
6726 if (gui.in_use)
6728 char buf[20];
6730 /* The GUI handles this internally. */
6731 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
6732 OUT_STR(buf);
6734 else
6735 #endif
6737 if (attr > HL_ALL) /* special HL attr. */
6739 if (t_colors > 1)
6740 aep = syn_cterm_attr2entry(attr);
6741 else
6742 aep = syn_term_attr2entry(attr);
6743 if (aep == NULL) /* did ":syntax clear" */
6744 attr = 0;
6745 else
6746 attr = aep->ae_attr;
6748 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
6749 out_str(T_MD);
6750 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
6751 && cterm_normal_fg_bold)
6752 /* If the Normal FG color has BOLD attribute and the new HL
6753 * has a FG color defined, clear BOLD. */
6754 out_str(T_ME);
6755 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
6756 out_str(T_SO);
6757 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
6758 /* underline or undercurl */
6759 out_str(T_US);
6760 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
6761 out_str(T_CZH);
6762 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
6763 out_str(T_MR);
6766 * Output the color or start string after bold etc., in case the
6767 * bold etc. override the color setting.
6769 if (aep != NULL)
6771 if (t_colors > 1)
6773 if (aep->ae_u.cterm.fg_color)
6774 term_fg_color(aep->ae_u.cterm.fg_color - 1);
6775 if (aep->ae_u.cterm.bg_color)
6776 term_bg_color(aep->ae_u.cterm.bg_color - 1);
6778 else
6780 if (aep->ae_u.term.start != NULL)
6781 out_str(aep->ae_u.term.start);
6788 void
6789 screen_stop_highlight()
6791 int do_ME = FALSE; /* output T_ME code */
6793 if (screen_attr != 0
6794 #ifdef WIN3264
6795 && termcap_active
6796 #endif
6799 #ifdef FEAT_GUI
6800 if (gui.in_use)
6802 char buf[20];
6804 /* use internal GUI code */
6805 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
6806 OUT_STR(buf);
6808 else
6809 #endif
6811 if (screen_attr > HL_ALL) /* special HL attr. */
6813 attrentry_T *aep;
6815 if (t_colors > 1)
6818 * Assume that t_me restores the original colors!
6820 aep = syn_cterm_attr2entry(screen_attr);
6821 if (aep != NULL && (aep->ae_u.cterm.fg_color
6822 || aep->ae_u.cterm.bg_color))
6823 do_ME = TRUE;
6825 else
6827 aep = syn_term_attr2entry(screen_attr);
6828 if (aep != NULL && aep->ae_u.term.stop != NULL)
6830 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
6831 do_ME = TRUE;
6832 else
6833 out_str(aep->ae_u.term.stop);
6836 if (aep == NULL) /* did ":syntax clear" */
6837 screen_attr = 0;
6838 else
6839 screen_attr = aep->ae_attr;
6843 * Often all ending-codes are equal to T_ME. Avoid outputting the
6844 * same sequence several times.
6846 if (screen_attr & HL_STANDOUT)
6848 if (STRCMP(T_SE, T_ME) == 0)
6849 do_ME = TRUE;
6850 else
6851 out_str(T_SE);
6853 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
6855 if (STRCMP(T_UE, T_ME) == 0)
6856 do_ME = TRUE;
6857 else
6858 out_str(T_UE);
6860 if (screen_attr & HL_ITALIC)
6862 if (STRCMP(T_CZR, T_ME) == 0)
6863 do_ME = TRUE;
6864 else
6865 out_str(T_CZR);
6867 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
6868 out_str(T_ME);
6870 if (t_colors > 1)
6872 /* set Normal cterm colors */
6873 if (cterm_normal_fg_color != 0)
6874 term_fg_color(cterm_normal_fg_color - 1);
6875 if (cterm_normal_bg_color != 0)
6876 term_bg_color(cterm_normal_bg_color - 1);
6877 if (cterm_normal_fg_bold)
6878 out_str(T_MD);
6882 screen_attr = 0;
6886 * Reset the colors for a cterm. Used when leaving Vim.
6887 * The machine specific code may override this again.
6889 void
6890 reset_cterm_colors()
6892 if (t_colors > 1)
6894 /* set Normal cterm colors */
6895 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
6897 out_str(T_OP);
6898 screen_attr = -1;
6900 if (cterm_normal_fg_bold)
6902 out_str(T_ME);
6903 screen_attr = -1;
6909 * Put character ScreenLines["off"] on the screen at position "row" and "col",
6910 * using the attributes from ScreenAttrs["off"].
6912 static void
6913 screen_char(off, row, col)
6914 unsigned off;
6915 int row;
6916 int col;
6918 int attr;
6920 /* Check for illegal values, just in case (could happen just after
6921 * resizing). */
6922 if (row >= screen_Rows || col >= screen_Columns)
6923 return;
6925 /* Outputting the last character on the screen may scrollup the screen.
6926 * Don't to it! Mark the character invalid (update it when scrolled up) */
6927 if (row == screen_Rows - 1 && col == screen_Columns - 1
6928 #ifdef FEAT_RIGHTLEFT
6929 /* account for first command-line character in rightleft mode */
6930 && !cmdmsg_rl
6931 #endif
6934 ScreenAttrs[off] = (sattr_T)-1;
6935 return;
6939 * Stop highlighting first, so it's easier to move the cursor.
6941 #if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
6942 if (screen_char_attr != 0)
6943 attr = screen_char_attr;
6944 else
6945 #endif
6946 attr = ScreenAttrs[off];
6947 if (screen_attr != attr)
6948 screen_stop_highlight();
6950 windgoto(row, col);
6952 if (screen_attr != attr)
6953 screen_start_highlight(attr);
6955 #ifdef FEAT_MBYTE
6956 if (enc_utf8 && ScreenLinesUC[off] != 0)
6958 char_u buf[MB_MAXBYTES + 1];
6960 /* Convert UTF-8 character to bytes and write it. */
6962 buf[utfc_char2bytes(off, buf)] = NUL;
6964 out_str(buf);
6965 if (utf_char2cells(ScreenLinesUC[off]) > 1)
6966 ++screen_cur_col;
6968 else
6969 #endif
6971 #ifdef FEAT_MBYTE
6972 out_flush_check();
6973 #endif
6974 out_char(ScreenLines[off]);
6975 #ifdef FEAT_MBYTE
6976 /* double-byte character in single-width cell */
6977 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6978 out_char(ScreenLines2[off]);
6979 #endif
6982 screen_cur_col++;
6985 #ifdef FEAT_MBYTE
6988 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
6989 * on the screen at position 'row' and 'col'.
6990 * The attributes of the first byte is used for all. This is required to
6991 * output the two bytes of a double-byte character with nothing in between.
6993 static void
6994 screen_char_2(off, row, col)
6995 unsigned off;
6996 int row;
6997 int col;
6999 /* Check for illegal values (could be wrong when screen was resized). */
7000 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
7001 return;
7003 /* Outputting the last character on the screen may scrollup the screen.
7004 * Don't to it! Mark the character invalid (update it when scrolled up) */
7005 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
7007 ScreenAttrs[off] = (sattr_T)-1;
7008 return;
7011 /* Output the first byte normally (positions the cursor), then write the
7012 * second byte directly. */
7013 screen_char(off, row, col);
7014 out_char(ScreenLines[off + 1]);
7015 ++screen_cur_col;
7017 #endif
7019 #if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
7021 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
7022 * This uses the contents of ScreenLines[] and doesn't change it.
7024 void
7025 screen_draw_rectangle(row, col, height, width, invert)
7026 int row;
7027 int col;
7028 int height;
7029 int width;
7030 int invert;
7032 int r, c;
7033 int off;
7034 #ifdef FEAT_MBYTE
7035 int max_off;
7036 #endif
7038 /* Can't use ScreenLines unless initialized */
7039 if (ScreenLines == NULL)
7040 return;
7042 if (invert)
7043 screen_char_attr = HL_INVERSE;
7044 for (r = row; r < row + height; ++r)
7046 off = LineOffset[r];
7047 #ifdef FEAT_MBYTE
7048 max_off = off + screen_Columns;
7049 #endif
7050 for (c = col; c < col + width; ++c)
7052 #ifdef FEAT_MBYTE
7053 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
7055 screen_char_2(off + c, r, c);
7056 ++c;
7058 else
7059 #endif
7061 screen_char(off + c, r, c);
7062 #ifdef FEAT_MBYTE
7063 if (utf_off2cells(off + c, max_off) > 1)
7064 ++c;
7065 #endif
7069 screen_char_attr = 0;
7071 #endif
7073 #ifdef FEAT_VERTSPLIT
7075 * Redraw the characters for a vertically split window.
7077 static void
7078 redraw_block(row, end, wp)
7079 int row;
7080 int end;
7081 win_T *wp;
7083 int col;
7084 int width;
7086 # ifdef FEAT_CLIPBOARD
7087 clip_may_clear_selection(row, end - 1);
7088 # endif
7090 if (wp == NULL)
7092 col = 0;
7093 width = Columns;
7095 else
7097 col = wp->w_wincol;
7098 width = wp->w_width;
7100 screen_draw_rectangle(row, col, end - row, width, FALSE);
7102 #endif
7105 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
7106 * with character 'c1' in first column followed by 'c2' in the other columns.
7107 * Use attributes 'attr'.
7109 void
7110 screen_fill(start_row, end_row, start_col, end_col, c1, c2, attr)
7111 int start_row, end_row;
7112 int start_col, end_col;
7113 int c1, c2;
7114 int attr;
7116 int row;
7117 int col;
7118 int off;
7119 int end_off;
7120 int did_delete;
7121 int c;
7122 int norm_term;
7123 #if defined(FEAT_GUI) || defined(UNIX)
7124 int force_next = FALSE;
7125 #endif
7127 if (end_row > screen_Rows) /* safety check */
7128 end_row = screen_Rows;
7129 if (end_col > screen_Columns) /* safety check */
7130 end_col = screen_Columns;
7131 if (ScreenLines == NULL
7132 || start_row >= end_row
7133 || start_col >= end_col) /* nothing to do */
7134 return;
7136 /* it's a "normal" terminal when not in a GUI or cterm */
7137 norm_term = (
7138 #ifdef FEAT_GUI
7139 !gui.in_use &&
7140 #endif
7141 t_colors <= 1);
7142 for (row = start_row; row < end_row; ++row)
7144 #ifdef FEAT_MBYTE
7145 if (has_mbyte
7146 # ifdef FEAT_GUI
7147 && !gui.in_use
7148 # endif
7151 /* When drawing over the right halve of a double-wide char clear
7152 * out the left halve. When drawing over the left halve of a
7153 * double wide-char clear out the right halve. Only needed in a
7154 * terminal. */
7155 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
7156 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
7157 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
7158 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
7160 #endif
7162 * Try to use delete-line termcap code, when no attributes or in a
7163 * "normal" terminal, where a bold/italic space is just a
7164 * space.
7166 did_delete = FALSE;
7167 if (c2 == ' '
7168 && end_col == Columns
7169 && can_clear(T_CE)
7170 && (attr == 0
7171 || (norm_term
7172 && attr <= HL_ALL
7173 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
7176 * check if we really need to clear something
7178 col = start_col;
7179 if (c1 != ' ') /* don't clear first char */
7180 ++col;
7182 off = LineOffset[row] + col;
7183 end_off = LineOffset[row] + end_col;
7185 /* skip blanks (used often, keep it fast!) */
7186 #ifdef FEAT_MBYTE
7187 if (enc_utf8)
7188 while (off < end_off && ScreenLines[off] == ' '
7189 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
7190 ++off;
7191 else
7192 #endif
7193 while (off < end_off && ScreenLines[off] == ' '
7194 && ScreenAttrs[off] == 0)
7195 ++off;
7196 if (off < end_off) /* something to be cleared */
7198 col = off - LineOffset[row];
7199 screen_stop_highlight();
7200 term_windgoto(row, col);/* clear rest of this screen line */
7201 out_str(T_CE);
7202 screen_start(); /* don't know where cursor is now */
7203 col = end_col - col;
7204 while (col--) /* clear chars in ScreenLines */
7206 ScreenLines[off] = ' ';
7207 #ifdef FEAT_MBYTE
7208 if (enc_utf8)
7209 ScreenLinesUC[off] = 0;
7210 #endif
7211 ScreenAttrs[off] = 0;
7212 ++off;
7215 did_delete = TRUE; /* the chars are cleared now */
7218 off = LineOffset[row] + start_col;
7219 c = c1;
7220 for (col = start_col; col < end_col; ++col)
7222 if (ScreenLines[off] != c
7223 #ifdef FEAT_MBYTE
7224 || (enc_utf8 && (int)ScreenLinesUC[off]
7225 != (c >= 0x80 ? c : 0))
7226 #endif
7227 || ScreenAttrs[off] != attr
7228 #if defined(FEAT_GUI) || defined(UNIX)
7229 || force_next
7230 #endif
7233 #if defined(FEAT_GUI) || defined(UNIX)
7234 /* The bold trick may make a single row of pixels appear in
7235 * the next character. When a bold character is removed, the
7236 * next character should be redrawn too. This happens for our
7237 * own GUI and for some xterms. */
7238 if (
7239 # ifdef FEAT_GUI
7240 gui.in_use
7241 # endif
7242 # if defined(FEAT_GUI) && defined(UNIX)
7244 # endif
7245 # ifdef UNIX
7246 term_is_xterm
7247 # endif
7250 if (ScreenLines[off] != ' '
7251 && (ScreenAttrs[off] > HL_ALL
7252 || ScreenAttrs[off] & HL_BOLD))
7253 force_next = TRUE;
7254 else
7255 force_next = FALSE;
7257 #endif
7258 ScreenLines[off] = c;
7259 #ifdef FEAT_MBYTE
7260 if (enc_utf8)
7262 if (c >= 0x80)
7264 ScreenLinesUC[off] = c;
7265 ScreenLinesC[0][off] = 0;
7267 else
7268 ScreenLinesUC[off] = 0;
7270 #endif
7271 ScreenAttrs[off] = attr;
7272 if (!did_delete || c != ' ')
7273 screen_char(off, row, col);
7275 ++off;
7276 if (col == start_col)
7278 if (did_delete)
7279 break;
7280 c = c2;
7283 if (end_col == Columns)
7284 LineWraps[row] = FALSE;
7285 if (row == Rows - 1) /* overwritten the command line */
7287 redraw_cmdline = TRUE;
7288 if (c1 == ' ' && c2 == ' ')
7289 clear_cmdline = FALSE; /* command line has been cleared */
7290 if (start_col == 0)
7291 mode_displayed = FALSE; /* mode cleared or overwritten */
7297 * Check if there should be a delay. Used before clearing or redrawing the
7298 * screen or the command line.
7300 void
7301 check_for_delay(check_msg_scroll)
7302 int check_msg_scroll;
7304 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
7305 && !did_wait_return
7306 && emsg_silent == 0)
7308 out_flush();
7309 ui_delay(1000L, TRUE);
7310 emsg_on_display = FALSE;
7311 if (check_msg_scroll)
7312 msg_scroll = FALSE;
7317 * screen_valid - allocate screen buffers if size changed
7318 * If "clear" is TRUE: clear screen if it has been resized.
7319 * Returns TRUE if there is a valid screen to write to.
7320 * Returns FALSE when starting up and screen not initialized yet.
7323 screen_valid(clear)
7324 int clear;
7326 screenalloc(clear); /* allocate screen buffers if size changed */
7327 return (ScreenLines != NULL);
7331 * Resize the shell to Rows and Columns.
7332 * Allocate ScreenLines[] and associated items.
7334 * There may be some time between setting Rows and Columns and (re)allocating
7335 * ScreenLines[]. This happens when starting up and when (manually) changing
7336 * the shell size. Always use screen_Rows and screen_Columns to access items
7337 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
7338 * final size of the shell is needed.
7340 void
7341 screenalloc(clear)
7342 int clear;
7344 int new_row, old_row;
7345 #ifdef FEAT_GUI
7346 int old_Rows;
7347 #endif
7348 win_T *wp;
7349 int outofmem = FALSE;
7350 int len;
7351 schar_T *new_ScreenLines;
7352 #ifdef FEAT_MBYTE
7353 u8char_T *new_ScreenLinesUC = NULL;
7354 u8char_T *new_ScreenLinesC[MAX_MCO];
7355 schar_T *new_ScreenLines2 = NULL;
7356 int i;
7357 #endif
7358 sattr_T *new_ScreenAttrs;
7359 unsigned *new_LineOffset;
7360 char_u *new_LineWraps;
7361 #ifdef FEAT_WINDOWS
7362 short *new_TabPageIdxs;
7363 tabpage_T *tp;
7364 #endif
7365 static int entered = FALSE; /* avoid recursiveness */
7366 static int done_outofmem_msg = FALSE; /* did outofmem message */
7369 * Allocation of the screen buffers is done only when the size changes and
7370 * when Rows and Columns have been set and we have started doing full
7371 * screen stuff.
7373 if ((ScreenLines != NULL
7374 && Rows == screen_Rows
7375 && Columns == screen_Columns
7376 #ifdef FEAT_MBYTE
7377 && enc_utf8 == (ScreenLinesUC != NULL)
7378 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
7379 && p_mco == Screen_mco
7380 #endif
7382 || Rows == 0
7383 || Columns == 0
7384 || (!full_screen && ScreenLines == NULL))
7385 return;
7388 * It's possible that we produce an out-of-memory message below, which
7389 * will cause this function to be called again. To break the loop, just
7390 * return here.
7392 if (entered)
7393 return;
7394 entered = TRUE;
7397 * Note that the window sizes are updated before reallocating the arrays,
7398 * thus we must not redraw here!
7400 ++RedrawingDisabled;
7402 win_new_shellsize(); /* fit the windows in the new sized shell */
7404 comp_col(); /* recompute columns for shown command and ruler */
7407 * We're changing the size of the screen.
7408 * - Allocate new arrays for ScreenLines and ScreenAttrs.
7409 * - Move lines from the old arrays into the new arrays, clear extra
7410 * lines (unless the screen is going to be cleared).
7411 * - Free the old arrays.
7413 * If anything fails, make ScreenLines NULL, so we don't do anything!
7414 * Continuing with the old ScreenLines may result in a crash, because the
7415 * size is wrong.
7417 FOR_ALL_TAB_WINDOWS(tp, wp)
7418 win_free_lsize(wp);
7420 new_ScreenLines = (schar_T *)lalloc((long_u)(
7421 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7422 #ifdef FEAT_MBYTE
7423 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T) * MAX_MCO);
7424 if (enc_utf8)
7426 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
7427 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
7428 for (i = 0; i < p_mco; ++i)
7429 new_ScreenLinesC[i] = (u8char_T *)lalloc((long_u)(
7430 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
7432 if (enc_dbcs == DBCS_JPNU)
7433 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
7434 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7435 #endif
7436 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
7437 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
7438 new_LineOffset = (unsigned *)lalloc((long_u)(
7439 Rows * sizeof(unsigned)), FALSE);
7440 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
7441 #ifdef FEAT_WINDOWS
7442 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
7443 #endif
7445 FOR_ALL_TAB_WINDOWS(tp, wp)
7447 if (win_alloc_lines(wp) == FAIL)
7449 outofmem = TRUE;
7450 #ifdef FEAT_WINDOWS
7451 break;
7452 #endif
7456 #ifdef FEAT_MBYTE
7457 for (i = 0; i < p_mco; ++i)
7458 if (new_ScreenLinesC[i] == NULL)
7459 break;
7460 #endif
7461 if (new_ScreenLines == NULL
7462 #ifdef FEAT_MBYTE
7463 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
7464 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
7465 #endif
7466 || new_ScreenAttrs == NULL
7467 || new_LineOffset == NULL
7468 || new_LineWraps == NULL
7469 #ifdef FEAT_WINDOWS
7470 || new_TabPageIdxs == NULL
7471 #endif
7472 || outofmem)
7474 if (ScreenLines != NULL || !done_outofmem_msg)
7476 /* guess the size */
7477 do_outofmem_msg((long_u)((Rows + 1) * Columns));
7479 /* Remember we did this to avoid getting outofmem messages over
7480 * and over again. */
7481 done_outofmem_msg = TRUE;
7483 vim_free(new_ScreenLines);
7484 new_ScreenLines = NULL;
7485 #ifdef FEAT_MBYTE
7486 vim_free(new_ScreenLinesUC);
7487 new_ScreenLinesUC = NULL;
7488 for (i = 0; i < p_mco; ++i)
7490 vim_free(new_ScreenLinesC[i]);
7491 new_ScreenLinesC[i] = NULL;
7493 vim_free(new_ScreenLines2);
7494 new_ScreenLines2 = NULL;
7495 #endif
7496 vim_free(new_ScreenAttrs);
7497 new_ScreenAttrs = NULL;
7498 vim_free(new_LineOffset);
7499 new_LineOffset = NULL;
7500 vim_free(new_LineWraps);
7501 new_LineWraps = NULL;
7502 #ifdef FEAT_WINDOWS
7503 vim_free(new_TabPageIdxs);
7504 new_TabPageIdxs = NULL;
7505 #endif
7507 else
7509 done_outofmem_msg = FALSE;
7511 for (new_row = 0; new_row < Rows; ++new_row)
7513 new_LineOffset[new_row] = new_row * Columns;
7514 new_LineWraps[new_row] = FALSE;
7517 * If the screen is not going to be cleared, copy as much as
7518 * possible from the old screen to the new one and clear the rest
7519 * (used when resizing the window at the "--more--" prompt or when
7520 * executing an external command, for the GUI).
7522 if (!clear)
7524 (void)vim_memset(new_ScreenLines + new_row * Columns,
7525 ' ', (size_t)Columns * sizeof(schar_T));
7526 #ifdef FEAT_MBYTE
7527 if (enc_utf8)
7529 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
7530 0, (size_t)Columns * sizeof(u8char_T));
7531 for (i = 0; i < p_mco; ++i)
7532 (void)vim_memset(new_ScreenLinesC[i]
7533 + new_row * Columns,
7534 0, (size_t)Columns * sizeof(u8char_T));
7536 if (enc_dbcs == DBCS_JPNU)
7537 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
7538 0, (size_t)Columns * sizeof(schar_T));
7539 #endif
7540 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
7541 0, (size_t)Columns * sizeof(sattr_T));
7542 old_row = new_row + (screen_Rows - Rows);
7543 if (old_row >= 0 && ScreenLines != NULL)
7545 if (screen_Columns < Columns)
7546 len = screen_Columns;
7547 else
7548 len = Columns;
7549 #ifdef FEAT_MBYTE
7550 /* When switching to utf-8 don't copy characters, they
7551 * may be invalid now. Also when p_mco changes. */
7552 if (!(enc_utf8 && ScreenLinesUC == NULL)
7553 && p_mco == Screen_mco)
7554 #endif
7555 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
7556 ScreenLines + LineOffset[old_row],
7557 (size_t)len * sizeof(schar_T));
7558 #ifdef FEAT_MBYTE
7559 if (enc_utf8 && ScreenLinesUC != NULL
7560 && p_mco == Screen_mco)
7562 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
7563 ScreenLinesUC + LineOffset[old_row],
7564 (size_t)len * sizeof(u8char_T));
7565 for (i = 0; i < p_mco; ++i)
7566 mch_memmove(new_ScreenLinesC[i]
7567 + new_LineOffset[new_row],
7568 ScreenLinesC[i] + LineOffset[old_row],
7569 (size_t)len * sizeof(u8char_T));
7571 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
7572 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
7573 ScreenLines2 + LineOffset[old_row],
7574 (size_t)len * sizeof(schar_T));
7575 #endif
7576 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
7577 ScreenAttrs + LineOffset[old_row],
7578 (size_t)len * sizeof(sattr_T));
7582 /* Use the last line of the screen for the current line. */
7583 current_ScreenLine = new_ScreenLines + Rows * Columns;
7586 free_screenlines();
7588 ScreenLines = new_ScreenLines;
7589 #ifdef FEAT_MBYTE
7590 ScreenLinesUC = new_ScreenLinesUC;
7591 for (i = 0; i < p_mco; ++i)
7592 ScreenLinesC[i] = new_ScreenLinesC[i];
7593 Screen_mco = p_mco;
7594 ScreenLines2 = new_ScreenLines2;
7595 #endif
7596 ScreenAttrs = new_ScreenAttrs;
7597 LineOffset = new_LineOffset;
7598 LineWraps = new_LineWraps;
7599 #ifdef FEAT_WINDOWS
7600 TabPageIdxs = new_TabPageIdxs;
7601 #endif
7603 /* It's important that screen_Rows and screen_Columns reflect the actual
7604 * size of ScreenLines[]. Set them before calling anything. */
7605 #ifdef FEAT_GUI
7606 old_Rows = screen_Rows;
7607 #endif
7608 screen_Rows = Rows;
7609 screen_Columns = Columns;
7611 must_redraw = CLEAR; /* need to clear the screen later */
7612 if (clear)
7613 screenclear2();
7615 #ifdef FEAT_GUI
7616 else if (gui.in_use
7617 && !gui.starting
7618 && ScreenLines != NULL
7619 && old_Rows != Rows)
7621 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
7623 * Adjust the position of the cursor, for when executing an external
7624 * command.
7626 if (msg_row >= Rows) /* Rows got smaller */
7627 msg_row = Rows - 1; /* put cursor at last row */
7628 else if (Rows > old_Rows) /* Rows got bigger */
7629 msg_row += Rows - old_Rows; /* put cursor in same place */
7630 if (msg_col >= Columns) /* Columns got smaller */
7631 msg_col = Columns - 1; /* put cursor at last column */
7633 #endif
7635 entered = FALSE;
7636 --RedrawingDisabled;
7638 #ifdef FEAT_AUTOCMD
7639 if (starting == 0)
7640 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
7641 #endif
7644 void
7645 free_screenlines()
7647 #ifdef FEAT_MBYTE
7648 int i;
7650 vim_free(ScreenLinesUC);
7651 for (i = 0; i < Screen_mco; ++i)
7652 vim_free(ScreenLinesC[i]);
7653 vim_free(ScreenLines2);
7654 #endif
7655 vim_free(ScreenLines);
7656 vim_free(ScreenAttrs);
7657 vim_free(LineOffset);
7658 vim_free(LineWraps);
7659 #ifdef FEAT_WINDOWS
7660 vim_free(TabPageIdxs);
7661 #endif
7664 void
7665 screenclear()
7667 check_for_delay(FALSE);
7668 screenalloc(FALSE); /* allocate screen buffers if size changed */
7669 screenclear2(); /* clear the screen */
7672 static void
7673 screenclear2()
7675 int i;
7677 if (starting == NO_SCREEN || ScreenLines == NULL
7678 #ifdef FEAT_GUI
7679 || (gui.in_use && gui.starting)
7680 #endif
7682 return;
7684 #ifdef FEAT_GUI
7685 if (!gui.in_use)
7686 #endif
7687 screen_attr = -1; /* force setting the Normal colors */
7688 screen_stop_highlight(); /* don't want highlighting here */
7690 #ifdef FEAT_CLIPBOARD
7691 /* disable selection without redrawing it */
7692 clip_scroll_selection(9999);
7693 #endif
7695 /* blank out ScreenLines */
7696 for (i = 0; i < Rows; ++i)
7698 lineclear(LineOffset[i], (int)Columns);
7699 LineWraps[i] = FALSE;
7702 if (can_clear(T_CL))
7704 out_str(T_CL); /* clear the display */
7705 clear_cmdline = FALSE;
7706 mode_displayed = FALSE;
7708 else
7710 /* can't clear the screen, mark all chars with invalid attributes */
7711 for (i = 0; i < Rows; ++i)
7712 lineinvalid(LineOffset[i], (int)Columns);
7713 clear_cmdline = TRUE;
7716 screen_cleared = TRUE; /* can use contents of ScreenLines now */
7718 win_rest_invalid(firstwin);
7719 redraw_cmdline = TRUE;
7720 #ifdef FEAT_WINDOWS
7721 redraw_tabline = TRUE;
7722 #endif
7723 if (must_redraw == CLEAR) /* no need to clear again */
7724 must_redraw = NOT_VALID;
7725 compute_cmdrow();
7726 msg_row = cmdline_row; /* put cursor on last line for messages */
7727 msg_col = 0;
7728 screen_start(); /* don't know where cursor is now */
7729 msg_scrolled = 0; /* can't scroll back */
7730 msg_didany = FALSE;
7731 msg_didout = FALSE;
7735 * Clear one line in ScreenLines.
7737 static void
7738 lineclear(off, width)
7739 unsigned off;
7740 int width;
7742 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
7743 #ifdef FEAT_MBYTE
7744 if (enc_utf8)
7745 (void)vim_memset(ScreenLinesUC + off, 0,
7746 (size_t)width * sizeof(u8char_T));
7747 #endif
7748 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
7752 * Mark one line in ScreenLines invalid by setting the attributes to an
7753 * invalid value.
7755 static void
7756 lineinvalid(off, width)
7757 unsigned off;
7758 int width;
7760 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
7763 #ifdef FEAT_VERTSPLIT
7765 * Copy part of a Screenline for vertically split window "wp".
7767 static void
7768 linecopy(to, from, wp)
7769 int to;
7770 int from;
7771 win_T *wp;
7773 unsigned off_to = LineOffset[to] + wp->w_wincol;
7774 unsigned off_from = LineOffset[from] + wp->w_wincol;
7776 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
7777 wp->w_width * sizeof(schar_T));
7778 # ifdef FEAT_MBYTE
7779 if (enc_utf8)
7781 int i;
7783 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
7784 wp->w_width * sizeof(u8char_T));
7785 for (i = 0; i < p_mco; ++i)
7786 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
7787 wp->w_width * sizeof(u8char_T));
7789 if (enc_dbcs == DBCS_JPNU)
7790 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
7791 wp->w_width * sizeof(schar_T));
7792 # endif
7793 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
7794 wp->w_width * sizeof(sattr_T));
7796 #endif
7799 * Return TRUE if clearing with term string "p" would work.
7800 * It can't work when the string is empty or it won't set the right background.
7803 can_clear(p)
7804 char_u *p;
7806 return (*p != NUL && (t_colors <= 1
7807 #ifdef FEAT_GUI
7808 || gui.in_use
7809 #endif
7810 || cterm_normal_bg_color == 0 || *T_UT != NUL));
7814 * Reset cursor position. Use whenever cursor was moved because of outputting
7815 * something directly to the screen (shell commands) or a terminal control
7816 * code.
7818 void
7819 screen_start()
7821 screen_cur_row = screen_cur_col = 9999;
7825 * Move the cursor to position "row","col" in the screen.
7826 * This tries to find the most efficient way to move, minimizing the number of
7827 * characters sent to the terminal.
7829 void
7830 windgoto(row, col)
7831 int row;
7832 int col;
7834 sattr_T *p;
7835 int i;
7836 int plan;
7837 int cost;
7838 int wouldbe_col;
7839 int noinvcurs;
7840 char_u *bs;
7841 int goto_cost;
7842 int attr;
7844 #define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
7845 #define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
7847 #define PLAN_LE 1
7848 #define PLAN_CR 2
7849 #define PLAN_NL 3
7850 #define PLAN_WRITE 4
7851 /* Can't use ScreenLines unless initialized */
7852 if (ScreenLines == NULL)
7853 return;
7855 if (col != screen_cur_col || row != screen_cur_row)
7857 /* Check for valid position. */
7858 if (row < 0) /* window without text lines? */
7859 row = 0;
7860 if (row >= screen_Rows)
7861 row = screen_Rows - 1;
7862 if (col >= screen_Columns)
7863 col = screen_Columns - 1;
7865 /* check if no cursor movement is allowed in highlight mode */
7866 if (screen_attr && *T_MS == NUL)
7867 noinvcurs = HIGHL_COST;
7868 else
7869 noinvcurs = 0;
7870 goto_cost = GOTO_COST + noinvcurs;
7873 * Plan how to do the positioning:
7874 * 1. Use CR to move it to column 0, same row.
7875 * 2. Use T_LE to move it a few columns to the left.
7876 * 3. Use NL to move a few lines down, column 0.
7877 * 4. Move a few columns to the right with T_ND or by writing chars.
7879 * Don't do this if the cursor went beyond the last column, the cursor
7880 * position is unknown then (some terminals wrap, some don't )
7882 * First check if the highlighting attributes allow us to write
7883 * characters to move the cursor to the right.
7885 if (row >= screen_cur_row && screen_cur_col < Columns)
7888 * If the cursor is in the same row, bigger col, we can use CR
7889 * or T_LE.
7891 bs = NULL; /* init for GCC */
7892 attr = screen_attr;
7893 if (row == screen_cur_row && col < screen_cur_col)
7895 /* "le" is preferred over "bc", because "bc" is obsolete */
7896 if (*T_LE)
7897 bs = T_LE; /* "cursor left" */
7898 else
7899 bs = T_BC; /* "backspace character (old) */
7900 if (*bs)
7901 cost = (screen_cur_col - col) * (int)STRLEN(bs);
7902 else
7903 cost = 999;
7904 if (col + 1 < cost) /* using CR is less characters */
7906 plan = PLAN_CR;
7907 wouldbe_col = 0;
7908 cost = 1; /* CR is just one character */
7910 else
7912 plan = PLAN_LE;
7913 wouldbe_col = col;
7915 if (noinvcurs) /* will stop highlighting */
7917 cost += noinvcurs;
7918 attr = 0;
7923 * If the cursor is above where we want to be, we can use CR LF.
7925 else if (row > screen_cur_row)
7927 plan = PLAN_NL;
7928 wouldbe_col = 0;
7929 cost = (row - screen_cur_row) * 2; /* CR LF */
7930 if (noinvcurs) /* will stop highlighting */
7932 cost += noinvcurs;
7933 attr = 0;
7938 * If the cursor is in the same row, smaller col, just use write.
7940 else
7942 plan = PLAN_WRITE;
7943 wouldbe_col = screen_cur_col;
7944 cost = 0;
7948 * Check if any characters that need to be written have the
7949 * correct attributes. Also avoid UTF-8 characters.
7951 i = col - wouldbe_col;
7952 if (i > 0)
7953 cost += i;
7954 if (cost < goto_cost && i > 0)
7957 * Check if the attributes are correct without additionally
7958 * stopping highlighting.
7960 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
7961 while (i && *p++ == attr)
7962 --i;
7963 if (i != 0)
7966 * Try if it works when highlighting is stopped here.
7968 if (*--p == 0)
7970 cost += noinvcurs;
7971 while (i && *p++ == 0)
7972 --i;
7974 if (i != 0)
7975 cost = 999; /* different attributes, don't do it */
7977 #ifdef FEAT_MBYTE
7978 if (enc_utf8)
7980 /* Don't use an UTF-8 char for positioning, it's slow. */
7981 for (i = wouldbe_col; i < col; ++i)
7982 if (ScreenLinesUC[LineOffset[row] + i] != 0)
7984 cost = 999;
7985 break;
7988 #endif
7992 * We can do it without term_windgoto()!
7994 if (cost < goto_cost)
7996 if (plan == PLAN_LE)
7998 if (noinvcurs)
7999 screen_stop_highlight();
8000 while (screen_cur_col > col)
8002 out_str(bs);
8003 --screen_cur_col;
8006 else if (plan == PLAN_CR)
8008 if (noinvcurs)
8009 screen_stop_highlight();
8010 out_char('\r');
8011 screen_cur_col = 0;
8013 else if (plan == PLAN_NL)
8015 if (noinvcurs)
8016 screen_stop_highlight();
8017 while (screen_cur_row < row)
8019 out_char('\n');
8020 ++screen_cur_row;
8022 screen_cur_col = 0;
8025 i = col - screen_cur_col;
8026 if (i > 0)
8029 * Use cursor-right if it's one character only. Avoids
8030 * removing a line of pixels from the last bold char, when
8031 * using the bold trick in the GUI.
8033 if (T_ND[0] != NUL && T_ND[1] == NUL)
8035 while (i-- > 0)
8036 out_char(*T_ND);
8038 else
8040 int off;
8042 off = LineOffset[row] + screen_cur_col;
8043 while (i-- > 0)
8045 if (ScreenAttrs[off] != screen_attr)
8046 screen_stop_highlight();
8047 #ifdef FEAT_MBYTE
8048 out_flush_check();
8049 #endif
8050 out_char(ScreenLines[off]);
8051 #ifdef FEAT_MBYTE
8052 if (enc_dbcs == DBCS_JPNU
8053 && ScreenLines[off] == 0x8e)
8054 out_char(ScreenLines2[off]);
8055 #endif
8056 ++off;
8062 else
8063 cost = 999;
8065 if (cost >= goto_cost)
8067 if (noinvcurs)
8068 screen_stop_highlight();
8069 if (row == screen_cur_row && (col > screen_cur_col) &&
8070 *T_CRI != NUL)
8071 term_cursor_right(col - screen_cur_col);
8072 else
8073 term_windgoto(row, col);
8075 screen_cur_row = row;
8076 screen_cur_col = col;
8081 * Set cursor to its position in the current window.
8083 void
8084 setcursor()
8086 if (redrawing())
8088 validate_cursor();
8089 windgoto(W_WINROW(curwin) + curwin->w_wrow,
8090 W_WINCOL(curwin) + (
8091 #ifdef FEAT_RIGHTLEFT
8092 /* With 'rightleft' set and the cursor on a double-wide
8093 * character, position it on the leftmost column. */
8094 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
8095 # ifdef FEAT_MBYTE
8096 (has_mbyte
8097 && (*mb_ptr2cells)(ml_get_cursor()) == 2
8098 && vim_isprintc(gchar_cursor())) ? 2 :
8099 # endif
8100 1)) :
8101 #endif
8102 curwin->w_wcol));
8108 * insert 'line_count' lines at 'row' in window 'wp'
8109 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
8110 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
8111 * scrolling.
8112 * Returns FAIL if the lines are not inserted, OK for success.
8115 win_ins_lines(wp, row, line_count, invalid, mayclear)
8116 win_T *wp;
8117 int row;
8118 int line_count;
8119 int invalid;
8120 int mayclear;
8122 int did_delete;
8123 int nextrow;
8124 int lastrow;
8125 int retval;
8127 if (invalid)
8128 wp->w_lines_valid = 0;
8130 if (wp->w_height < 5)
8131 return FAIL;
8133 if (line_count > wp->w_height - row)
8134 line_count = wp->w_height - row;
8136 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
8137 if (retval != MAYBE)
8138 return retval;
8141 * If there is a next window or a status line, we first try to delete the
8142 * lines at the bottom to avoid messing what is after the window.
8143 * If this fails and there are following windows, don't do anything to avoid
8144 * messing up those windows, better just redraw.
8146 did_delete = FALSE;
8147 #ifdef FEAT_WINDOWS
8148 if (wp->w_next != NULL || wp->w_status_height)
8150 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8151 line_count, (int)Rows, FALSE, NULL) == OK)
8152 did_delete = TRUE;
8153 else if (wp->w_next)
8154 return FAIL;
8156 #endif
8158 * if no lines deleted, blank the lines that will end up below the window
8160 if (!did_delete)
8162 #ifdef FEAT_WINDOWS
8163 wp->w_redr_status = TRUE;
8164 #endif
8165 redraw_cmdline = TRUE;
8166 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
8167 lastrow = nextrow + line_count;
8168 if (lastrow > Rows)
8169 lastrow = Rows;
8170 screen_fill(nextrow - line_count, lastrow - line_count,
8171 W_WINCOL(wp), (int)W_ENDCOL(wp),
8172 ' ', ' ', 0);
8175 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
8176 == FAIL)
8178 /* deletion will have messed up other windows */
8179 if (did_delete)
8181 #ifdef FEAT_WINDOWS
8182 wp->w_redr_status = TRUE;
8183 #endif
8184 win_rest_invalid(W_NEXT(wp));
8186 return FAIL;
8189 return OK;
8193 * delete "line_count" window lines at "row" in window "wp"
8194 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
8195 * If "mayclear" is TRUE the screen will be cleared if it is faster than
8196 * scrolling
8197 * Return OK for success, FAIL if the lines are not deleted.
8200 win_del_lines(wp, row, line_count, invalid, mayclear)
8201 win_T *wp;
8202 int row;
8203 int line_count;
8204 int invalid;
8205 int mayclear;
8207 int retval;
8209 if (invalid)
8210 wp->w_lines_valid = 0;
8212 if (line_count > wp->w_height - row)
8213 line_count = wp->w_height - row;
8215 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
8216 if (retval != MAYBE)
8217 return retval;
8219 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
8220 (int)Rows, FALSE, NULL) == FAIL)
8221 return FAIL;
8223 #ifdef FEAT_WINDOWS
8225 * If there are windows or status lines below, try to put them at the
8226 * correct place. If we can't do that, they have to be redrawn.
8228 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
8230 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8231 line_count, (int)Rows, NULL) == FAIL)
8233 wp->w_redr_status = TRUE;
8234 win_rest_invalid(wp->w_next);
8238 * If this is the last window and there is no status line, redraw the
8239 * command line later.
8241 else
8242 #endif
8243 redraw_cmdline = TRUE;
8244 return OK;
8248 * Common code for win_ins_lines() and win_del_lines().
8249 * Returns OK or FAIL when the work has been done.
8250 * Returns MAYBE when not finished yet.
8252 static int
8253 win_do_lines(wp, row, line_count, mayclear, del)
8254 win_T *wp;
8255 int row;
8256 int line_count;
8257 int mayclear;
8258 int del;
8260 int retval;
8262 if (!redrawing() || line_count <= 0)
8263 return FAIL;
8265 /* only a few lines left: redraw is faster */
8266 if (mayclear && Rows - line_count < 5
8267 #ifdef FEAT_VERTSPLIT
8268 && wp->w_width == Columns
8269 #endif
8272 screenclear(); /* will set wp->w_lines_valid to 0 */
8273 return FAIL;
8277 * Delete all remaining lines
8279 if (row + line_count >= wp->w_height)
8281 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
8282 W_WINCOL(wp), (int)W_ENDCOL(wp),
8283 ' ', ' ', 0);
8284 return OK;
8288 * when scrolling, the message on the command line should be cleared,
8289 * otherwise it will stay there forever.
8291 clear_cmdline = TRUE;
8294 * If the terminal can set a scroll region, use that.
8295 * Always do this in a vertically split window. This will redraw from
8296 * ScreenLines[] when t_CV isn't defined. That's faster than using
8297 * win_line().
8298 * Don't use a scroll region when we are going to redraw the text, writing
8299 * a character in the lower right corner of the scroll region causes a
8300 * scroll-up in the DJGPP version.
8302 if (scroll_region
8303 #ifdef FEAT_VERTSPLIT
8304 || W_WIDTH(wp) != Columns
8305 #endif
8308 #ifdef FEAT_VERTSPLIT
8309 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8310 #endif
8311 scroll_region_set(wp, row);
8312 if (del)
8313 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
8314 wp->w_height - row, FALSE, wp);
8315 else
8316 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
8317 wp->w_height - row, wp);
8318 #ifdef FEAT_VERTSPLIT
8319 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8320 #endif
8321 scroll_region_reset();
8322 return retval;
8325 #ifdef FEAT_WINDOWS
8326 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
8327 return FAIL;
8328 #endif
8330 return MAYBE;
8334 * window 'wp' and everything after it is messed up, mark it for redraw
8336 static void
8337 win_rest_invalid(wp)
8338 win_T *wp;
8340 #ifdef FEAT_WINDOWS
8341 while (wp != NULL)
8342 #else
8343 if (wp != NULL)
8344 #endif
8346 redraw_win_later(wp, NOT_VALID);
8347 #ifdef FEAT_WINDOWS
8348 wp->w_redr_status = TRUE;
8349 wp = wp->w_next;
8350 #endif
8352 redraw_cmdline = TRUE;
8356 * The rest of the routines in this file perform screen manipulations. The
8357 * given operation is performed physically on the screen. The corresponding
8358 * change is also made to the internal screen image. In this way, the editor
8359 * anticipates the effect of editing changes on the appearance of the screen.
8360 * That way, when we call screenupdate a complete redraw isn't usually
8361 * necessary. Another advantage is that we can keep adding code to anticipate
8362 * screen changes, and in the meantime, everything still works.
8366 * types for inserting or deleting lines
8368 #define USE_T_CAL 1
8369 #define USE_T_CDL 2
8370 #define USE_T_AL 3
8371 #define USE_T_CE 4
8372 #define USE_T_DL 5
8373 #define USE_T_SR 6
8374 #define USE_NL 7
8375 #define USE_T_CD 8
8376 #define USE_REDRAW 9
8379 * insert lines on the screen and update ScreenLines[]
8380 * 'end' is the line after the scrolled part. Normally it is Rows.
8381 * When scrolling region used 'off' is the offset from the top for the region.
8382 * 'row' and 'end' are relative to the start of the region.
8384 * return FAIL for failure, OK for success.
8387 screen_ins_lines(off, row, line_count, end, wp)
8388 int off;
8389 int row;
8390 int line_count;
8391 int end;
8392 win_T *wp; /* NULL or window to use width from */
8394 int i;
8395 int j;
8396 unsigned temp;
8397 int cursor_row;
8398 int type;
8399 int result_empty;
8400 int can_ce = can_clear(T_CE);
8403 * FAIL if
8404 * - there is no valid screen
8405 * - the screen has to be redrawn completely
8406 * - the line count is less than one
8407 * - the line count is more than 'ttyscroll'
8409 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
8410 return FAIL;
8413 * There are seven ways to insert lines:
8414 * 0. When in a vertically split window and t_CV isn't set, redraw the
8415 * characters from ScreenLines[].
8416 * 1. Use T_CD (clear to end of display) if it exists and the result of
8417 * the insert is just empty lines
8418 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
8419 * present or line_count > 1. It looks better if we do all the inserts
8420 * at once.
8421 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
8422 * insert is just empty lines and T_CE is not present or line_count >
8423 * 1.
8424 * 4. Use T_AL (insert line) if it exists.
8425 * 5. Use T_CE (erase line) if it exists and the result of the insert is
8426 * just empty lines.
8427 * 6. Use T_DL (delete line) if it exists and the result of the insert is
8428 * just empty lines.
8429 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
8430 * the 'da' flag is not set or we have clear line capability.
8431 * 8. redraw the characters from ScreenLines[].
8433 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
8434 * the scrollbar for the window. It does have insert line, use that if it
8435 * exists.
8437 result_empty = (row + line_count >= end);
8438 #ifdef FEAT_VERTSPLIT
8439 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
8440 type = USE_REDRAW;
8441 else
8442 #endif
8443 if (can_clear(T_CD) && result_empty)
8444 type = USE_T_CD;
8445 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
8446 type = USE_T_CAL;
8447 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
8448 type = USE_T_CDL;
8449 else if (*T_AL != NUL)
8450 type = USE_T_AL;
8451 else if (can_ce && result_empty)
8452 type = USE_T_CE;
8453 else if (*T_DL != NUL && result_empty)
8454 type = USE_T_DL;
8455 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
8456 type = USE_T_SR;
8457 else
8458 return FAIL;
8461 * For clearing the lines screen_del_lines() is used. This will also take
8462 * care of t_db if necessary.
8464 if (type == USE_T_CD || type == USE_T_CDL ||
8465 type == USE_T_CE || type == USE_T_DL)
8466 return screen_del_lines(off, row, line_count, end, FALSE, wp);
8469 * If text is retained below the screen, first clear or delete as many
8470 * lines at the bottom of the window as are about to be inserted so that
8471 * the deleted lines won't later surface during a screen_del_lines.
8473 if (*T_DB)
8474 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
8476 #ifdef FEAT_CLIPBOARD
8477 /* Remove a modeless selection when inserting lines halfway the screen
8478 * or not the full width of the screen. */
8479 if (off + row > 0
8480 # ifdef FEAT_VERTSPLIT
8481 || (wp != NULL && wp->w_width != Columns)
8482 # endif
8484 clip_clear_selection();
8485 else
8486 clip_scroll_selection(-line_count);
8487 #endif
8489 #ifdef FEAT_GUI
8490 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
8491 * scrolling is actually carried out. */
8492 gui_dont_update_cursor();
8493 #endif
8495 if (*T_CCS != NUL) /* cursor relative to region */
8496 cursor_row = row;
8497 else
8498 cursor_row = row + off;
8501 * Shift LineOffset[] line_count down to reflect the inserted lines.
8502 * Clear the inserted lines in ScreenLines[].
8504 row += off;
8505 end += off;
8506 for (i = 0; i < line_count; ++i)
8508 #ifdef FEAT_VERTSPLIT
8509 if (wp != NULL && wp->w_width != Columns)
8511 /* need to copy part of a line */
8512 j = end - 1 - i;
8513 while ((j -= line_count) >= row)
8514 linecopy(j + line_count, j, wp);
8515 j += line_count;
8516 if (can_clear((char_u *)" "))
8517 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
8518 else
8519 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
8520 LineWraps[j] = FALSE;
8522 else
8523 #endif
8525 j = end - 1 - i;
8526 temp = LineOffset[j];
8527 while ((j -= line_count) >= row)
8529 LineOffset[j + line_count] = LineOffset[j];
8530 LineWraps[j + line_count] = LineWraps[j];
8532 LineOffset[j + line_count] = temp;
8533 LineWraps[j + line_count] = FALSE;
8534 if (can_clear((char_u *)" "))
8535 lineclear(temp, (int)Columns);
8536 else
8537 lineinvalid(temp, (int)Columns);
8541 screen_stop_highlight();
8542 windgoto(cursor_row, 0);
8544 #ifdef FEAT_VERTSPLIT
8545 /* redraw the characters */
8546 if (type == USE_REDRAW)
8547 redraw_block(row, end, wp);
8548 else
8549 #endif
8550 if (type == USE_T_CAL)
8552 term_append_lines(line_count);
8553 screen_start(); /* don't know where cursor is now */
8555 else
8557 for (i = 0; i < line_count; i++)
8559 if (type == USE_T_AL)
8561 if (i && cursor_row != 0)
8562 windgoto(cursor_row, 0);
8563 out_str(T_AL);
8565 else /* type == USE_T_SR */
8566 out_str(T_SR);
8567 screen_start(); /* don't know where cursor is now */
8572 * With scroll-reverse and 'da' flag set we need to clear the lines that
8573 * have been scrolled down into the region.
8575 if (type == USE_T_SR && *T_DA)
8577 for (i = 0; i < line_count; ++i)
8579 windgoto(off + i, 0);
8580 out_str(T_CE);
8581 screen_start(); /* don't know where cursor is now */
8585 #ifdef FEAT_GUI
8586 gui_can_update_cursor();
8587 if (gui.in_use)
8588 out_flush(); /* always flush after a scroll */
8589 #endif
8590 return OK;
8594 * delete lines on the screen and update ScreenLines[]
8595 * 'end' is the line after the scrolled part. Normally it is Rows.
8596 * When scrolling region used 'off' is the offset from the top for the region.
8597 * 'row' and 'end' are relative to the start of the region.
8599 * Return OK for success, FAIL if the lines are not deleted.
8601 /*ARGSUSED*/
8603 screen_del_lines(off, row, line_count, end, force, wp)
8604 int off;
8605 int row;
8606 int line_count;
8607 int end;
8608 int force; /* even when line_count > p_ttyscroll */
8609 win_T *wp; /* NULL or window to use width from */
8611 int j;
8612 int i;
8613 unsigned temp;
8614 int cursor_row;
8615 int cursor_end;
8616 int result_empty; /* result is empty until end of region */
8617 int can_delete; /* deleting line codes can be used */
8618 int type;
8621 * FAIL if
8622 * - there is no valid screen
8623 * - the screen has to be redrawn completely
8624 * - the line count is less than one
8625 * - the line count is more than 'ttyscroll'
8627 if (!screen_valid(TRUE) || line_count <= 0 ||
8628 (!force && line_count > p_ttyscroll))
8629 return FAIL;
8632 * Check if the rest of the current region will become empty.
8634 result_empty = row + line_count >= end;
8637 * We can delete lines only when 'db' flag not set or when 'ce' option
8638 * available.
8640 can_delete = (*T_DB == NUL || can_clear(T_CE));
8643 * There are six ways to delete lines:
8644 * 0. When in a vertically split window and t_CV isn't set, redraw the
8645 * characters from ScreenLines[].
8646 * 1. Use T_CD if it exists and the result is empty.
8647 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
8648 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
8649 * none of the other ways work.
8650 * 4. Use T_CE (erase line) if the result is empty.
8651 * 5. Use T_DL (delete line) if it exists.
8652 * 6. redraw the characters from ScreenLines[].
8654 #ifdef FEAT_VERTSPLIT
8655 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
8656 type = USE_REDRAW;
8657 else
8658 #endif
8659 if (can_clear(T_CD) && result_empty)
8660 type = USE_T_CD;
8661 #if defined(__BEOS__) && defined(BEOS_DR8)
8663 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
8664 * its internal termcap... this works okay for tests which test *T_DB !=
8665 * NUL. It has the disadvantage that the user cannot use any :set t_*
8666 * command to get T_DB (back) to empty_option, only :set term=... will do
8667 * the trick...
8668 * Anyway, this hack will hopefully go away with the next OS release.
8669 * (Olaf Seibert)
8671 else if (row == 0 && T_DB == empty_option
8672 && (line_count == 1 || *T_CDL == NUL))
8673 #else
8674 else if (row == 0 && (
8675 #ifndef AMIGA
8676 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
8677 * up, so use delete-line command */
8678 line_count == 1 ||
8679 #endif
8680 *T_CDL == NUL))
8681 #endif
8682 type = USE_NL;
8683 else if (*T_CDL != NUL && line_count > 1 && can_delete)
8684 type = USE_T_CDL;
8685 else if (can_clear(T_CE) && result_empty
8686 #ifdef FEAT_VERTSPLIT
8687 && (wp == NULL || wp->w_width == Columns)
8688 #endif
8690 type = USE_T_CE;
8691 else if (*T_DL != NUL && can_delete)
8692 type = USE_T_DL;
8693 else if (*T_CDL != NUL && can_delete)
8694 type = USE_T_CDL;
8695 else
8696 return FAIL;
8698 #ifdef FEAT_CLIPBOARD
8699 /* Remove a modeless selection when deleting lines halfway the screen or
8700 * not the full width of the screen. */
8701 if (off + row > 0
8702 # ifdef FEAT_VERTSPLIT
8703 || (wp != NULL && wp->w_width != Columns)
8704 # endif
8706 clip_clear_selection();
8707 else
8708 clip_scroll_selection(line_count);
8709 #endif
8711 #ifdef FEAT_GUI
8712 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
8713 * scrolling is actually carried out. */
8714 gui_dont_update_cursor();
8715 #endif
8717 if (*T_CCS != NUL) /* cursor relative to region */
8719 cursor_row = row;
8720 cursor_end = end;
8722 else
8724 cursor_row = row + off;
8725 cursor_end = end + off;
8729 * Now shift LineOffset[] line_count up to reflect the deleted lines.
8730 * Clear the inserted lines in ScreenLines[].
8732 row += off;
8733 end += off;
8734 for (i = 0; i < line_count; ++i)
8736 #ifdef FEAT_VERTSPLIT
8737 if (wp != NULL && wp->w_width != Columns)
8739 /* need to copy part of a line */
8740 j = row + i;
8741 while ((j += line_count) <= end - 1)
8742 linecopy(j - line_count, j, wp);
8743 j -= line_count;
8744 if (can_clear((char_u *)" "))
8745 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
8746 else
8747 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
8748 LineWraps[j] = FALSE;
8750 else
8751 #endif
8753 /* whole width, moving the line pointers is faster */
8754 j = row + i;
8755 temp = LineOffset[j];
8756 while ((j += line_count) <= end - 1)
8758 LineOffset[j - line_count] = LineOffset[j];
8759 LineWraps[j - line_count] = LineWraps[j];
8761 LineOffset[j - line_count] = temp;
8762 LineWraps[j - line_count] = FALSE;
8763 if (can_clear((char_u *)" "))
8764 lineclear(temp, (int)Columns);
8765 else
8766 lineinvalid(temp, (int)Columns);
8770 screen_stop_highlight();
8772 #ifdef FEAT_VERTSPLIT
8773 /* redraw the characters */
8774 if (type == USE_REDRAW)
8775 redraw_block(row, end, wp);
8776 else
8777 #endif
8778 if (type == USE_T_CD) /* delete the lines */
8780 windgoto(cursor_row, 0);
8781 out_str(T_CD);
8782 screen_start(); /* don't know where cursor is now */
8784 else if (type == USE_T_CDL)
8786 windgoto(cursor_row, 0);
8787 term_delete_lines(line_count);
8788 screen_start(); /* don't know where cursor is now */
8791 * Deleting lines at top of the screen or scroll region: Just scroll
8792 * the whole screen (scroll region) up by outputting newlines on the
8793 * last line.
8795 else if (type == USE_NL)
8797 windgoto(cursor_end - 1, 0);
8798 for (i = line_count; --i >= 0; )
8799 out_char('\n'); /* cursor will remain on same line */
8801 else
8803 for (i = line_count; --i >= 0; )
8805 if (type == USE_T_DL)
8807 windgoto(cursor_row, 0);
8808 out_str(T_DL); /* delete a line */
8810 else /* type == USE_T_CE */
8812 windgoto(cursor_row + i, 0);
8813 out_str(T_CE); /* erase a line */
8815 screen_start(); /* don't know where cursor is now */
8820 * If the 'db' flag is set, we need to clear the lines that have been
8821 * scrolled up at the bottom of the region.
8823 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
8825 for (i = line_count; i > 0; --i)
8827 windgoto(cursor_end - i, 0);
8828 out_str(T_CE); /* erase a line */
8829 screen_start(); /* don't know where cursor is now */
8833 #ifdef FEAT_GUI
8834 gui_can_update_cursor();
8835 if (gui.in_use)
8836 out_flush(); /* always flush after a scroll */
8837 #endif
8839 return OK;
8843 * show the current mode and ruler
8845 * If clear_cmdline is TRUE, clear the rest of the cmdline.
8846 * If clear_cmdline is FALSE there may be a message there that needs to be
8847 * cleared only if a mode is shown.
8848 * Return the length of the message (0 if no message).
8851 showmode()
8853 int need_clear;
8854 int length = 0;
8855 int do_mode;
8856 int attr;
8857 int nwr_save;
8858 #ifdef FEAT_INS_EXPAND
8859 int sub_attr;
8860 #endif
8862 do_mode = ((p_smd && msg_silent == 0)
8863 && ((State & INSERT)
8864 || restart_edit
8865 #ifdef FEAT_VISUAL
8866 || VIsual_active
8867 #endif
8869 if (do_mode || Recording)
8872 * Don't show mode right now, when not redrawing or inside a mapping.
8873 * Call char_avail() only when we are going to show something, because
8874 * it takes a bit of time.
8876 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
8878 redraw_cmdline = TRUE; /* show mode later */
8879 return 0;
8882 nwr_save = need_wait_return;
8884 /* wait a bit before overwriting an important message */
8885 check_for_delay(FALSE);
8887 /* if the cmdline is more than one line high, erase top lines */
8888 need_clear = clear_cmdline;
8889 if (clear_cmdline && cmdline_row < Rows - 1)
8890 msg_clr_cmdline(); /* will reset clear_cmdline */
8892 /* Position on the last line in the window, column 0 */
8893 msg_pos_mode();
8894 cursor_off();
8895 attr = hl_attr(HLF_CM); /* Highlight mode */
8896 if (do_mode)
8898 MSG_PUTS_ATTR("--", attr);
8899 #if defined(FEAT_XIM)
8900 # if 0 /* old version, changed by SungHyun Nam July 2008 */
8901 if (xic != NULL && im_get_status() && !p_imdisable
8902 && curbuf->b_p_iminsert == B_IMODE_IM)
8903 # else
8904 if (
8905 # ifdef HAVE_GTK2
8906 preedit_get_status()
8907 # else
8908 im_get_status()
8909 # endif
8911 # endif
8912 # ifdef HAVE_GTK2 /* most of the time, it's not XIM being used */
8913 MSG_PUTS_ATTR(" IM", attr);
8914 # else
8915 MSG_PUTS_ATTR(" XIM", attr);
8916 # endif
8917 #endif
8918 #if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
8919 if (gui.in_use)
8921 if (hangul_input_state_get())
8922 MSG_PUTS_ATTR(" \307\321\261\333", attr); /* HANGUL */
8924 #endif
8925 #ifdef FEAT_INS_EXPAND
8926 if (edit_submode != NULL) /* CTRL-X in Insert mode */
8928 /* These messages can get long, avoid a wrap in a narrow
8929 * window. Prefer showing edit_submode_extra. */
8930 length = (Rows - msg_row) * Columns - 3;
8931 if (edit_submode_extra != NULL)
8932 length -= vim_strsize(edit_submode_extra);
8933 if (length > 0)
8935 if (edit_submode_pre != NULL)
8936 length -= vim_strsize(edit_submode_pre);
8937 if (length - vim_strsize(edit_submode) > 0)
8939 if (edit_submode_pre != NULL)
8940 msg_puts_attr(edit_submode_pre, attr);
8941 msg_puts_attr(edit_submode, attr);
8943 if (edit_submode_extra != NULL)
8945 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
8946 if ((int)edit_submode_highl < (int)HLF_COUNT)
8947 sub_attr = hl_attr(edit_submode_highl);
8948 else
8949 sub_attr = attr;
8950 msg_puts_attr(edit_submode_extra, sub_attr);
8953 length = 0;
8955 else
8956 #endif
8958 #ifdef FEAT_VREPLACE
8959 if (State & VREPLACE_FLAG)
8960 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
8961 else
8962 #endif
8963 if (State & REPLACE_FLAG)
8964 MSG_PUTS_ATTR(_(" REPLACE"), attr);
8965 else if (State & INSERT)
8967 #ifdef FEAT_RIGHTLEFT
8968 if (p_ri)
8969 MSG_PUTS_ATTR(_(" REVERSE"), attr);
8970 #endif
8971 MSG_PUTS_ATTR(_(" INSERT"), attr);
8973 else if (restart_edit == 'I')
8974 MSG_PUTS_ATTR(_(" (insert)"), attr);
8975 else if (restart_edit == 'R')
8976 MSG_PUTS_ATTR(_(" (replace)"), attr);
8977 else if (restart_edit == 'V')
8978 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
8979 #ifdef FEAT_RIGHTLEFT
8980 if (p_hkmap)
8981 MSG_PUTS_ATTR(_(" Hebrew"), attr);
8982 # ifdef FEAT_FKMAP
8983 if (p_fkmap)
8984 MSG_PUTS_ATTR(farsi_text_5, attr);
8985 # endif
8986 #endif
8987 #ifdef FEAT_KEYMAP
8988 if (State & LANGMAP)
8990 # ifdef FEAT_ARABIC
8991 if (curwin->w_p_arab)
8992 MSG_PUTS_ATTR(_(" Arabic"), attr);
8993 else
8994 # endif
8995 MSG_PUTS_ATTR(_(" (lang)"), attr);
8997 #endif
8998 if ((State & INSERT) && p_paste)
8999 MSG_PUTS_ATTR(_(" (paste)"), attr);
9001 #ifdef FEAT_VISUAL
9002 if (VIsual_active)
9004 char *p;
9006 /* Don't concatenate separate words to avoid translation
9007 * problems. */
9008 switch ((VIsual_select ? 4 : 0)
9009 + (VIsual_mode == Ctrl_V) * 2
9010 + (VIsual_mode == 'V'))
9012 case 0: p = N_(" VISUAL"); break;
9013 case 1: p = N_(" VISUAL LINE"); break;
9014 case 2: p = N_(" VISUAL BLOCK"); break;
9015 case 4: p = N_(" SELECT"); break;
9016 case 5: p = N_(" SELECT LINE"); break;
9017 default: p = N_(" SELECT BLOCK"); break;
9019 MSG_PUTS_ATTR(_(p), attr);
9021 #endif
9022 MSG_PUTS_ATTR(" --", attr);
9025 need_clear = TRUE;
9027 if (Recording
9028 #ifdef FEAT_INS_EXPAND
9029 && edit_submode == NULL /* otherwise it gets too long */
9030 #endif
9033 MSG_PUTS_ATTR(_("recording"), attr);
9034 need_clear = TRUE;
9037 mode_displayed = TRUE;
9038 if (need_clear || clear_cmdline)
9039 msg_clr_eos();
9040 msg_didout = FALSE; /* overwrite this message */
9041 length = msg_col;
9042 msg_col = 0;
9043 need_wait_return = nwr_save; /* never ask for hit-return for this */
9045 else if (clear_cmdline && msg_silent == 0)
9046 /* Clear the whole command line. Will reset "clear_cmdline". */
9047 msg_clr_cmdline();
9049 #ifdef FEAT_CMDL_INFO
9050 # ifdef FEAT_VISUAL
9051 /* In Visual mode the size of the selected area must be redrawn. */
9052 if (VIsual_active)
9053 clear_showcmd();
9054 # endif
9056 /* If the last window has no status line, the ruler is after the mode
9057 * message and must be redrawn */
9058 if (redrawing()
9059 # ifdef FEAT_WINDOWS
9060 && lastwin->w_status_height == 0
9061 # endif
9063 win_redr_ruler(lastwin, TRUE);
9064 #endif
9065 redraw_cmdline = FALSE;
9066 clear_cmdline = FALSE;
9068 return length;
9072 * Position for a mode message.
9074 static void
9075 msg_pos_mode()
9077 msg_col = 0;
9078 msg_row = Rows - 1;
9082 * Delete mode message. Used when ESC is typed which is expected to end
9083 * Insert mode (but Insert mode didn't end yet!).
9084 * Caller should check "mode_displayed".
9086 void
9087 unshowmode(force)
9088 int force;
9091 * Don't delete it right now, when not redrawing or insided a mapping.
9093 if (!redrawing() || (!force && char_avail() && !KeyTyped))
9094 redraw_cmdline = TRUE; /* delete mode later */
9095 else
9097 msg_pos_mode();
9098 if (Recording)
9099 MSG_PUTS_ATTR(_("recording"), hl_attr(HLF_CM));
9100 msg_clr_eos();
9104 #if defined(FEAT_WINDOWS)
9106 * Draw the tab pages line at the top of the Vim window.
9108 static void
9109 draw_tabline()
9111 int tabcount = 0;
9112 tabpage_T *tp;
9113 int tabwidth;
9114 int col = 0;
9115 int scol = 0;
9116 int attr;
9117 win_T *wp;
9118 win_T *cwp;
9119 int wincount;
9120 int modified;
9121 int c;
9122 int len;
9123 int attr_sel = hl_attr(HLF_TPS);
9124 int attr_nosel = hl_attr(HLF_TP);
9125 int attr_fill = hl_attr(HLF_TPF);
9126 char_u *p;
9127 int room;
9128 int use_sep_chars = (t_colors < 8
9129 #ifdef FEAT_GUI
9130 && !gui.in_use
9131 #endif
9134 redraw_tabline = FALSE;
9136 #ifdef FEAT_GUI_TABLINE
9137 /* Take care of a GUI tabline. */
9138 if (gui_use_tabline())
9140 gui_update_tabline();
9141 return;
9143 #endif
9145 if (tabline_height() < 1)
9146 return;
9148 #if defined(FEAT_STL_OPT)
9150 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
9151 for (scol = 0; scol < Columns; ++scol)
9152 TabPageIdxs[scol] = 0;
9154 /* Use the 'tabline' option if it's set. */
9155 if (*p_tal != NUL)
9157 int save_called_emsg = called_emsg;
9159 /* Check for an error. If there is one we would loop in redrawing the
9160 * screen. Avoid that by making 'tabline' empty. */
9161 called_emsg = FALSE;
9162 win_redr_custom(NULL, FALSE);
9163 if (called_emsg)
9164 set_string_option_direct((char_u *)"tabline", -1,
9165 (char_u *)"", OPT_FREE, SID_ERROR);
9166 called_emsg |= save_called_emsg;
9168 else
9169 #endif
9171 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
9172 ++tabcount;
9174 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
9175 if (tabwidth < 6)
9176 tabwidth = 6;
9178 attr = attr_nosel;
9179 tabcount = 0;
9180 scol = 0;
9181 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
9182 tp = tp->tp_next)
9184 scol = col;
9186 if (tp->tp_topframe == topframe)
9187 attr = attr_sel;
9188 if (use_sep_chars && col > 0)
9189 screen_putchar('|', 0, col++, attr);
9191 if (tp->tp_topframe != topframe)
9192 attr = attr_nosel;
9194 screen_putchar(' ', 0, col++, attr);
9196 if (tp == curtab)
9198 cwp = curwin;
9199 wp = firstwin;
9201 else
9203 cwp = tp->tp_curwin;
9204 wp = tp->tp_firstwin;
9207 modified = FALSE;
9208 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
9209 if (bufIsChanged(wp->w_buffer))
9210 modified = TRUE;
9211 if (modified || wincount > 1)
9213 if (wincount > 1)
9215 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
9216 len = (int)STRLEN(NameBuff);
9217 if (col + len >= Columns - 3)
9218 break;
9219 screen_puts_len(NameBuff, len, 0, col,
9220 #if defined(FEAT_SYN_HL)
9221 hl_combine_attr(attr, hl_attr(HLF_T))
9222 #else
9223 attr
9224 #endif
9226 col += len;
9228 if (modified)
9229 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
9230 screen_putchar(' ', 0, col++, attr);
9233 room = scol - col + tabwidth - 1;
9234 if (room > 0)
9236 /* Get buffer name in NameBuff[] */
9237 get_trans_bufname(cwp->w_buffer);
9238 shorten_dir(NameBuff);
9239 len = vim_strsize(NameBuff);
9240 p = NameBuff;
9241 #ifdef FEAT_MBYTE
9242 if (has_mbyte)
9243 while (len > room)
9245 len -= ptr2cells(p);
9246 mb_ptr_adv(p);
9248 else
9249 #endif
9250 if (len > room)
9252 p += len - room;
9253 len = room;
9255 if (len > Columns - col - 1)
9256 len = Columns - col - 1;
9258 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
9259 col += len;
9261 screen_putchar(' ', 0, col++, attr);
9263 /* Store the tab page number in TabPageIdxs[], so that
9264 * jump_to_mouse() knows where each one is. */
9265 ++tabcount;
9266 while (scol < col)
9267 TabPageIdxs[scol++] = tabcount;
9270 if (use_sep_chars)
9271 c = '_';
9272 else
9273 c = ' ';
9274 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
9276 /* Put an "X" for closing the current tab if there are several. */
9277 if (first_tabpage->tp_next != NULL)
9279 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
9280 TabPageIdxs[Columns - 1] = -999;
9284 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
9285 * set. */
9286 redraw_tabline = FALSE;
9290 * Get buffer name for "buf" into NameBuff[].
9291 * Takes care of special buffer names and translates special characters.
9293 void
9294 get_trans_bufname(buf)
9295 buf_T *buf;
9297 if (buf_spname(buf) != NULL)
9298 STRCPY(NameBuff, buf_spname(buf));
9299 else
9300 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
9301 trans_characters(NameBuff, MAXPATHL);
9303 #endif
9305 #if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
9307 * Get the character to use in a status line. Get its attributes in "*attr".
9309 static int
9310 fillchar_status(attr, is_curwin)
9311 int *attr;
9312 int is_curwin;
9314 int fill;
9315 if (is_curwin)
9317 *attr = hl_attr(HLF_S);
9318 fill = fill_stl;
9320 else
9322 *attr = hl_attr(HLF_SNC);
9323 fill = fill_stlnc;
9325 /* Use fill when there is highlighting, and highlighting of current
9326 * window differs, or the fillchars differ, or this is not the
9327 * current window */
9328 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
9329 || !is_curwin || firstwin == lastwin)
9330 || (fill_stl != fill_stlnc)))
9331 return fill;
9332 if (is_curwin)
9333 return '^';
9334 return '=';
9336 #endif
9338 #ifdef FEAT_VERTSPLIT
9340 * Get the character to use in a separator between vertically split windows.
9341 * Get its attributes in "*attr".
9343 static int
9344 fillchar_vsep(attr)
9345 int *attr;
9347 *attr = hl_attr(HLF_C);
9348 if (*attr == 0 && fill_vert == ' ')
9349 return '|';
9350 else
9351 return fill_vert;
9353 #endif
9356 * Return TRUE if redrawing should currently be done.
9359 redrawing()
9361 return (!RedrawingDisabled
9362 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
9366 * Return TRUE if printing messages should currently be done.
9369 messaging()
9371 return (!(p_lz && char_avail() && !KeyTyped));
9375 * Show current status info in ruler and various other places
9376 * If always is FALSE, only show ruler if position has changed.
9378 void
9379 showruler(always)
9380 int always;
9382 if (!always && !redrawing())
9383 return;
9384 #ifdef FEAT_INS_EXPAND
9385 if (pum_visible())
9387 # ifdef FEAT_WINDOWS
9388 /* Don't redraw right now, do it later. */
9389 curwin->w_redr_status = TRUE;
9390 # endif
9391 return;
9393 #endif
9394 #if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
9395 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
9397 redraw_custum_statusline(curwin);
9399 else
9400 #endif
9401 #ifdef FEAT_CMDL_INFO
9402 win_redr_ruler(curwin, always);
9403 #endif
9405 #ifdef FEAT_TITLE
9406 if (need_maketitle
9407 # ifdef FEAT_STL_OPT
9408 || (p_icon && (stl_syntax & STL_IN_ICON))
9409 || (p_title && (stl_syntax & STL_IN_TITLE))
9410 # endif
9412 maketitle();
9413 #endif
9414 #ifdef FEAT_WINDOWS
9415 /* Redraw the tab pages line if needed. */
9416 if (redraw_tabline)
9417 draw_tabline();
9418 #endif
9421 #ifdef FEAT_CMDL_INFO
9422 static void
9423 win_redr_ruler(wp, always)
9424 win_T *wp;
9425 int always;
9427 char_u buffer[70];
9428 int row;
9429 int fillchar;
9430 int attr;
9431 int empty_line = FALSE;
9432 colnr_T virtcol;
9433 int i;
9434 int o;
9435 #ifdef FEAT_VERTSPLIT
9436 int this_ru_col;
9437 int off = 0;
9438 int width = Columns;
9439 # define WITH_OFF(x) x
9440 # define WITH_WIDTH(x) x
9441 #else
9442 # define WITH_OFF(x) 0
9443 # define WITH_WIDTH(x) Columns
9444 # define this_ru_col ru_col
9445 #endif
9447 /* If 'ruler' off or redrawing disabled, don't do anything */
9448 if (!p_ru)
9449 return;
9452 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
9453 * after deleting lines, before cursor.lnum is corrected.
9455 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
9456 return;
9458 #ifdef FEAT_INS_EXPAND
9459 /* Don't draw the ruler while doing insert-completion, it might overwrite
9460 * the (long) mode message. */
9461 # ifdef FEAT_WINDOWS
9462 if (wp == lastwin && lastwin->w_status_height == 0)
9463 # endif
9464 if (edit_submode != NULL)
9465 return;
9466 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
9467 if (pum_visible())
9468 return;
9469 #endif
9471 #ifdef FEAT_STL_OPT
9472 if (*p_ruf)
9474 int save_called_emsg = called_emsg;
9476 called_emsg = FALSE;
9477 win_redr_custom(wp, TRUE);
9478 if (called_emsg)
9479 set_string_option_direct((char_u *)"rulerformat", -1,
9480 (char_u *)"", OPT_FREE, SID_ERROR);
9481 called_emsg |= save_called_emsg;
9482 return;
9484 #endif
9487 * Check if not in Insert mode and the line is empty (will show "0-1").
9489 if (!(State & INSERT)
9490 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
9491 empty_line = TRUE;
9494 * Only draw the ruler when something changed.
9496 validate_virtcol_win(wp);
9497 if ( redraw_cmdline
9498 || always
9499 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
9500 || wp->w_cursor.col != wp->w_ru_cursor.col
9501 || wp->w_virtcol != wp->w_ru_virtcol
9502 #ifdef FEAT_VIRTUALEDIT
9503 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
9504 #endif
9505 || wp->w_topline != wp->w_ru_topline
9506 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
9507 #ifdef FEAT_DIFF
9508 || wp->w_topfill != wp->w_ru_topfill
9509 #endif
9510 || empty_line != wp->w_ru_empty)
9512 cursor_off();
9513 #ifdef FEAT_WINDOWS
9514 if (wp->w_status_height)
9516 row = W_WINROW(wp) + wp->w_height;
9517 fillchar = fillchar_status(&attr, wp == curwin);
9518 # ifdef FEAT_VERTSPLIT
9519 off = W_WINCOL(wp);
9520 width = W_WIDTH(wp);
9521 # endif
9523 else
9524 #endif
9526 row = Rows - 1;
9527 fillchar = ' ';
9528 attr = 0;
9529 #ifdef FEAT_VERTSPLIT
9530 width = Columns;
9531 off = 0;
9532 #endif
9535 /* In list mode virtcol needs to be recomputed */
9536 virtcol = wp->w_virtcol;
9537 if (wp->w_p_list && lcs_tab1 == NUL)
9539 wp->w_p_list = FALSE;
9540 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
9541 wp->w_p_list = TRUE;
9545 * Some sprintfs return the length, some return a pointer.
9546 * To avoid portability problems we use strlen() here.
9548 sprintf((char *)buffer, "%ld,",
9549 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
9550 ? 0L
9551 : (long)(wp->w_cursor.lnum));
9552 col_print(buffer + STRLEN(buffer),
9553 empty_line ? 0 : (int)wp->w_cursor.col + 1,
9554 (int)virtcol + 1);
9557 * Add a "50%" if there is room for it.
9558 * On the last line, don't print in the last column (scrolls the
9559 * screen up on some terminals).
9561 i = (int)STRLEN(buffer);
9562 get_rel_pos(wp, buffer + i + 1);
9563 o = i + vim_strsize(buffer + i + 1);
9564 #ifdef FEAT_WINDOWS
9565 if (wp->w_status_height == 0) /* can't use last char of screen */
9566 #endif
9567 ++o;
9568 #ifdef FEAT_VERTSPLIT
9569 this_ru_col = ru_col - (Columns - width);
9570 if (this_ru_col < 0)
9571 this_ru_col = 0;
9572 #endif
9573 /* Never use more than half the window/screen width, leave the other
9574 * half for the filename. */
9575 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
9576 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
9577 if (this_ru_col + o < WITH_WIDTH(width))
9579 while (this_ru_col + o < WITH_WIDTH(width))
9581 #ifdef FEAT_MBYTE
9582 if (has_mbyte)
9583 i += (*mb_char2bytes)(fillchar, buffer + i);
9584 else
9585 #endif
9586 buffer[i++] = fillchar;
9587 ++o;
9589 get_rel_pos(wp, buffer + i);
9591 /* Truncate at window boundary. */
9592 #ifdef FEAT_MBYTE
9593 if (has_mbyte)
9595 o = 0;
9596 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
9598 o += (*mb_ptr2cells)(buffer + i);
9599 if (this_ru_col + o > WITH_WIDTH(width))
9601 buffer[i] = NUL;
9602 break;
9606 else
9607 #endif
9608 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
9609 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
9611 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
9612 i = redraw_cmdline;
9613 screen_fill(row, row + 1,
9614 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
9615 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
9616 fillchar, fillchar, attr);
9617 /* don't redraw the cmdline because of showing the ruler */
9618 redraw_cmdline = i;
9619 wp->w_ru_cursor = wp->w_cursor;
9620 wp->w_ru_virtcol = wp->w_virtcol;
9621 wp->w_ru_empty = empty_line;
9622 wp->w_ru_topline = wp->w_topline;
9623 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
9624 #ifdef FEAT_DIFF
9625 wp->w_ru_topfill = wp->w_topfill;
9626 #endif
9629 #endif
9631 #if defined(FEAT_LINEBREAK) || defined(PROTO)
9633 * Return the width of the 'number' column.
9634 * Caller may need to check if 'number' is set.
9635 * Otherwise it depends on 'numberwidth' and the line count.
9638 number_width(wp)
9639 win_T *wp;
9641 int n;
9642 linenr_T lnum;
9644 lnum = wp->w_buffer->b_ml.ml_line_count;
9645 if (lnum == wp->w_nrwidth_line_count)
9646 return wp->w_nrwidth_width;
9647 wp->w_nrwidth_line_count = lnum;
9649 n = 0;
9652 lnum /= 10;
9653 ++n;
9654 } while (lnum > 0);
9656 /* 'numberwidth' gives the minimal width plus one */
9657 if (n < wp->w_p_nuw - 1)
9658 n = wp->w_p_nuw - 1;
9660 wp->w_nrwidth_width = n;
9661 return n;
9663 #endif