Use 7.2a "spell" folder (and remove all extra spell files)
[MacVim.git] / src / screen.c
blob78dd27787da475c52f6bee2d37e04f56b7490c03
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 && (int)wp->w_virtcol + txtcol < W_WIDTH(wp))
2443 ScreenAttrs[off + wp->w_virtcol + txtcol] = hl_combine_attr(
2444 ScreenAttrs[off + wp->w_virtcol + txtcol], hl_attr(HLF_CUC));
2445 #endif
2447 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2448 (int)W_WIDTH(wp), FALSE);
2451 * Update w_cline_height and w_cline_folded if the cursor line was
2452 * updated (saves a call to plines() later).
2454 if (wp == curwin
2455 && lnum <= curwin->w_cursor.lnum
2456 && lnume >= curwin->w_cursor.lnum)
2458 curwin->w_cline_row = row;
2459 curwin->w_cline_height = 1;
2460 curwin->w_cline_folded = TRUE;
2461 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2466 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2468 static void
2469 copy_text_attr(off, buf, len, attr)
2470 int off;
2471 char_u *buf;
2472 int len;
2473 int attr;
2475 int i;
2477 mch_memmove(ScreenLines + off, buf, (size_t)len);
2478 # ifdef FEAT_MBYTE
2479 if (enc_utf8)
2480 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2481 # endif
2482 for (i = 0; i < len; ++i)
2483 ScreenAttrs[off + i] = attr;
2487 * Fill the foldcolumn at "p" for window "wp".
2488 * Only to be called when 'foldcolumn' > 0.
2490 static void
2491 fill_foldcolumn(p, wp, closed, lnum)
2492 char_u *p;
2493 win_T *wp;
2494 int closed; /* TRUE of FALSE */
2495 linenr_T lnum; /* current line number */
2497 int i = 0;
2498 int level;
2499 int first_level;
2500 int empty;
2502 /* Init to all spaces. */
2503 copy_spaces(p, (size_t)wp->w_p_fdc);
2505 level = win_foldinfo.fi_level;
2506 if (level > 0)
2508 /* If there is only one column put more info in it. */
2509 empty = (wp->w_p_fdc == 1) ? 0 : 1;
2511 /* If the column is too narrow, we start at the lowest level that
2512 * fits and use numbers to indicated the depth. */
2513 first_level = level - wp->w_p_fdc - closed + 1 + empty;
2514 if (first_level < 1)
2515 first_level = 1;
2517 for (i = 0; i + empty < wp->w_p_fdc; ++i)
2519 if (win_foldinfo.fi_lnum == lnum
2520 && first_level + i >= win_foldinfo.fi_low_level)
2521 p[i] = '-';
2522 else if (first_level == 1)
2523 p[i] = '|';
2524 else if (first_level + i <= 9)
2525 p[i] = '0' + first_level + i;
2526 else
2527 p[i] = '>';
2528 if (first_level + i == level)
2529 break;
2532 if (closed)
2533 p[i >= wp->w_p_fdc ? i - 1 : i] = '+';
2535 #endif /* FEAT_FOLDING */
2538 * Display line "lnum" of window 'wp' on the screen.
2539 * Start at row "startrow", stop when "endrow" is reached.
2540 * wp->w_virtcol needs to be valid.
2542 * Return the number of last row the line occupies.
2544 /* ARGSUSED */
2545 static int
2546 win_line(wp, lnum, startrow, endrow, nochange)
2547 win_T *wp;
2548 linenr_T lnum;
2549 int startrow;
2550 int endrow;
2551 int nochange; /* not updating for changed text */
2553 int col; /* visual column on screen */
2554 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2555 int c = 0; /* init for GCC */
2556 long vcol = 0; /* virtual column (for tabs) */
2557 long vcol_prev = -1; /* "vcol" of previous character */
2558 char_u *line; /* current line */
2559 char_u *ptr; /* current position in "line" */
2560 int row; /* row in the window, excl w_winrow */
2561 int screen_row; /* row on the screen, incl w_winrow */
2563 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2564 int n_extra = 0; /* number of extra chars */
2565 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
2566 int c_extra = NUL; /* extra chars, all the same */
2567 int extra_attr = 0; /* attributes when n_extra != 0 */
2568 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2569 displaying lcs_eol at end-of-line */
2570 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2571 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2573 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2574 int saved_n_extra = 0;
2575 char_u *saved_p_extra = NULL;
2576 int saved_c_extra = 0;
2577 int saved_char_attr = 0;
2579 int n_attr = 0; /* chars with special attr */
2580 int saved_attr2 = 0; /* char_attr saved for n_attr */
2581 int n_attr3 = 0; /* chars with overruling special attr */
2582 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2584 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2586 int fromcol, tocol; /* start/end of inverting */
2587 int fromcol_prev = -2; /* start of inverting after cursor */
2588 int noinvcur = FALSE; /* don't invert the cursor */
2589 #ifdef FEAT_VISUAL
2590 pos_T *top, *bot;
2591 #endif
2592 pos_T pos;
2593 long v;
2595 int char_attr = 0; /* attributes for next character */
2596 int attr_pri = FALSE; /* char_attr has priority */
2597 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2598 in this line */
2599 int attr = 0; /* attributes for area highlighting */
2600 int area_attr = 0; /* attributes desired by highlighting */
2601 int search_attr = 0; /* attributes desired by 'hlsearch' */
2602 #ifdef FEAT_SYN_HL
2603 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
2604 int syntax_attr = 0; /* attributes desired by syntax */
2605 int has_syntax = FALSE; /* this buffer has syntax highl. */
2606 int save_did_emsg;
2607 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
2608 #endif
2609 #ifdef FEAT_SPELL
2610 int has_spell = FALSE; /* this buffer has spell checking */
2611 # define SPWORDLEN 150
2612 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
2613 int nextlinecol = 0; /* column where nextline[] starts */
2614 int nextline_idx = 0; /* index in nextline[] where next line
2615 starts */
2616 int spell_attr = 0; /* attributes desired by spelling */
2617 int word_end = 0; /* last byte with same spell_attr */
2618 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2619 static int checked_col = 0; /* column in "checked_lnum" up to which
2620 * there are no spell errors */
2621 static int cap_col = -1; /* column to check for Cap word */
2622 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
2623 int cur_checked_col = 0; /* checked column for current line */
2624 #endif
2625 int extra_check; /* has syntax or linebreak */
2626 #ifdef FEAT_MBYTE
2627 int multi_attr = 0; /* attributes desired by multibyte */
2628 int mb_l = 1; /* multi-byte byte length */
2629 int mb_c = 0; /* decoded multi-byte character */
2630 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
2631 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
2632 #endif
2633 #ifdef FEAT_DIFF
2634 int filler_lines; /* nr of filler lines to be drawn */
2635 int filler_todo; /* nr of filler lines still to do + 1 */
2636 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
2637 int change_start = MAXCOL; /* first col of changed area */
2638 int change_end = -1; /* last col of changed area */
2639 #endif
2640 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
2641 #ifdef FEAT_LINEBREAK
2642 int need_showbreak = FALSE;
2643 #endif
2644 #if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
2645 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
2646 # define LINE_ATTR
2647 int line_attr = 0; /* attribute for the whole line */
2648 #endif
2649 #ifdef FEAT_SEARCH_EXTRA
2650 matchitem_T *cur; /* points to the match list */
2651 match_T *shl; /* points to search_hl or a match */
2652 int shl_flag; /* flag to indicate whether search_hl
2653 has been processed or not */
2654 int prevcol_hl_flag; /* flag to indicate whether prevcol
2655 equals startcol of search_hl or one
2656 of the matches */
2657 #endif
2658 #ifdef FEAT_ARABIC
2659 int prev_c = 0; /* previous Arabic character */
2660 int prev_c1 = 0; /* first composing char for prev_c */
2661 #endif
2662 #if defined(LINE_ATTR)
2663 int did_line_attr = 0;
2664 #endif
2666 /* draw_state: items that are drawn in sequence: */
2667 #define WL_START 0 /* nothing done yet */
2668 #ifdef FEAT_CMDWIN
2669 # define WL_CMDLINE WL_START + 1 /* cmdline window column */
2670 #else
2671 # define WL_CMDLINE WL_START
2672 #endif
2673 #ifdef FEAT_FOLDING
2674 # define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
2675 #else
2676 # define WL_FOLD WL_CMDLINE
2677 #endif
2678 #ifdef FEAT_SIGNS
2679 # define WL_SIGN WL_FOLD + 1 /* column for signs */
2680 #else
2681 # define WL_SIGN WL_FOLD /* column for signs */
2682 #endif
2683 #define WL_NR WL_SIGN + 1 /* line number */
2684 #if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
2685 # define WL_SBR WL_NR + 1 /* 'showbreak' or 'diff' */
2686 #else
2687 # define WL_SBR WL_NR
2688 #endif
2689 #define WL_LINE WL_SBR + 1 /* text in the line */
2690 int draw_state = WL_START; /* what to draw next */
2691 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2692 int feedback_col = 0;
2693 int feedback_old_attr = -1;
2694 #endif
2697 if (startrow > endrow) /* past the end already! */
2698 return startrow;
2700 row = startrow;
2701 screen_row = row + W_WINROW(wp);
2704 * To speed up the loop below, set extra_check when there is linebreak,
2705 * trailing white space and/or syntax processing to be done.
2707 #ifdef FEAT_LINEBREAK
2708 extra_check = wp->w_p_lbr;
2709 #else
2710 extra_check = 0;
2711 #endif
2712 #ifdef FEAT_SYN_HL
2713 if (syntax_present(wp->w_buffer) && !wp->w_buffer->b_syn_error)
2715 /* Prepare for syntax highlighting in this line. When there is an
2716 * error, stop syntax highlighting. */
2717 save_did_emsg = did_emsg;
2718 did_emsg = FALSE;
2719 syntax_start(wp, lnum);
2720 if (did_emsg)
2721 wp->w_buffer->b_syn_error = TRUE;
2722 else
2724 did_emsg = save_did_emsg;
2725 has_syntax = TRUE;
2726 extra_check = TRUE;
2729 #endif
2731 #ifdef FEAT_SPELL
2732 if (wp->w_p_spell
2733 && *wp->w_buffer->b_p_spl != NUL
2734 && wp->w_buffer->b_langp.ga_len > 0
2735 && *(char **)(wp->w_buffer->b_langp.ga_data) != NULL)
2737 /* Prepare for spell checking. */
2738 has_spell = TRUE;
2739 extra_check = TRUE;
2741 /* Get the start of the next line, so that words that wrap to the next
2742 * line are found too: "et<line-break>al.".
2743 * Trick: skip a few chars for C/shell/Vim comments */
2744 nextline[SPWORDLEN] = NUL;
2745 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2747 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
2748 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
2751 /* When a word wrapped from the previous line the start of the current
2752 * line is valid. */
2753 if (lnum == checked_lnum)
2754 cur_checked_col = checked_col;
2755 checked_lnum = 0;
2757 /* When there was a sentence end in the previous line may require a
2758 * word starting with capital in this line. In line 1 always check
2759 * the first word. */
2760 if (lnum != capcol_lnum)
2761 cap_col = -1;
2762 if (lnum == 1)
2763 cap_col = 0;
2764 capcol_lnum = 0;
2766 #endif
2769 * handle visual active in this window
2771 fromcol = -10;
2772 tocol = MAXCOL;
2773 #ifdef FEAT_VISUAL
2774 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2776 /* Visual is after curwin->w_cursor */
2777 if (ltoreq(curwin->w_cursor, VIsual))
2779 top = &curwin->w_cursor;
2780 bot = &VIsual;
2782 else /* Visual is before curwin->w_cursor */
2784 top = &VIsual;
2785 bot = &curwin->w_cursor;
2787 if (VIsual_mode == Ctrl_V) /* block mode */
2789 if (lnum >= top->lnum && lnum <= bot->lnum)
2791 fromcol = wp->w_old_cursor_fcol;
2792 tocol = wp->w_old_cursor_lcol;
2795 else /* non-block mode */
2797 if (lnum > top->lnum && lnum <= bot->lnum)
2798 fromcol = 0;
2799 else if (lnum == top->lnum)
2801 if (VIsual_mode == 'V') /* linewise */
2802 fromcol = 0;
2803 else
2805 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
2806 if (gchar_pos(top) == NUL)
2807 tocol = fromcol + 1;
2810 if (VIsual_mode != 'V' && lnum == bot->lnum)
2812 if (*p_sel == 'e' && bot->col == 0
2813 #ifdef FEAT_VIRTUALEDIT
2814 && bot->coladd == 0
2815 #endif
2818 fromcol = -10;
2819 tocol = MAXCOL;
2821 else if (bot->col == MAXCOL)
2822 tocol = MAXCOL;
2823 else
2825 pos = *bot;
2826 if (*p_sel == 'e')
2827 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
2828 else
2830 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
2831 ++tocol;
2837 #ifndef MSDOS
2838 /* Check if the character under the cursor should not be inverted */
2839 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
2840 # ifdef FEAT_GUI
2841 && !gui.in_use
2842 # endif
2844 noinvcur = TRUE;
2845 #endif
2847 /* if inverting in this line set area_highlighting */
2848 if (fromcol >= 0)
2850 area_highlighting = TRUE;
2851 attr = hl_attr(HLF_V);
2852 #if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
2853 if (clip_star.available && !clip_star.owned && clip_isautosel())
2854 attr = hl_attr(HLF_VNC);
2855 #endif
2860 * handle 'incsearch' and ":s///c" highlighting
2862 else
2863 #endif /* FEAT_VISUAL */
2864 if (highlight_match
2865 && wp == curwin
2866 && lnum >= curwin->w_cursor.lnum
2867 && lnum <= curwin->w_cursor.lnum + search_match_lines)
2869 if (lnum == curwin->w_cursor.lnum)
2870 getvcol(curwin, &(curwin->w_cursor),
2871 (colnr_T *)&fromcol, NULL, NULL);
2872 else
2873 fromcol = 0;
2874 if (lnum == curwin->w_cursor.lnum + search_match_lines)
2876 pos.lnum = lnum;
2877 pos.col = search_match_endcol;
2878 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
2880 else
2881 tocol = MAXCOL;
2882 if (fromcol == tocol) /* do at least one character */
2883 tocol = fromcol + 1; /* happens when past end of line */
2884 area_highlighting = TRUE;
2885 attr = hl_attr(HLF_I);
2888 #ifdef FEAT_DIFF
2889 filler_lines = diff_check(wp, lnum);
2890 if (filler_lines < 0)
2892 if (filler_lines == -1)
2894 if (diff_find_change(wp, lnum, &change_start, &change_end))
2895 diff_hlf = HLF_ADD; /* added line */
2896 else if (change_start == 0)
2897 diff_hlf = HLF_TXD; /* changed text */
2898 else
2899 diff_hlf = HLF_CHD; /* changed line */
2901 else
2902 diff_hlf = HLF_ADD; /* added line */
2903 filler_lines = 0;
2904 area_highlighting = TRUE;
2906 if (lnum == wp->w_topline)
2907 filler_lines = wp->w_topfill;
2908 filler_todo = filler_lines;
2909 #endif
2911 #ifdef LINE_ATTR
2912 # ifdef FEAT_SIGNS
2913 /* If this line has a sign with line highlighting set line_attr. */
2914 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
2915 if (v != 0)
2916 line_attr = sign_get_attr((int)v, TRUE);
2917 # endif
2918 # if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
2919 /* Highlight the current line in the quickfix window. */
2920 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
2921 line_attr = hl_attr(HLF_L);
2922 # endif
2923 if (line_attr != 0)
2924 area_highlighting = TRUE;
2925 #endif
2927 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
2928 ptr = line;
2930 #ifdef FEAT_SPELL
2931 if (has_spell)
2933 /* For checking first word with a capital skip white space. */
2934 if (cap_col == 0)
2935 cap_col = (int)(skipwhite(line) - line);
2937 /* To be able to spell-check over line boundaries copy the end of the
2938 * current line into nextline[]. Above the start of the next line was
2939 * copied to nextline[SPWORDLEN]. */
2940 if (nextline[SPWORDLEN] == NUL)
2942 /* No next line or it is empty. */
2943 nextlinecol = MAXCOL;
2944 nextline_idx = 0;
2946 else
2948 v = (long)STRLEN(line);
2949 if (v < SPWORDLEN)
2951 /* Short line, use it completely and append the start of the
2952 * next line. */
2953 nextlinecol = 0;
2954 mch_memmove(nextline, line, (size_t)v);
2955 STRMOVE(nextline + v, nextline + SPWORDLEN);
2956 nextline_idx = v + 1;
2958 else
2960 /* Long line, use only the last SPWORDLEN bytes. */
2961 nextlinecol = v - SPWORDLEN;
2962 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
2963 nextline_idx = SPWORDLEN + 1;
2967 #endif
2969 /* find start of trailing whitespace */
2970 if (wp->w_p_list && lcs_trail)
2972 trailcol = (colnr_T)STRLEN(ptr);
2973 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
2974 --trailcol;
2975 trailcol += (colnr_T) (ptr - line);
2976 extra_check = TRUE;
2980 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
2981 * first character to be displayed.
2983 if (wp->w_p_wrap)
2984 v = wp->w_skipcol;
2985 else
2986 v = wp->w_leftcol;
2987 if (v > 0)
2989 #ifdef FEAT_MBYTE
2990 char_u *prev_ptr = ptr;
2991 #endif
2992 while (vcol < v && *ptr != NUL)
2994 c = win_lbr_chartabsize(wp, ptr, (colnr_T)vcol, NULL);
2995 vcol += c;
2996 #ifdef FEAT_MBYTE
2997 prev_ptr = ptr;
2998 #endif
2999 mb_ptr_adv(ptr);
3002 #ifdef FEAT_VIRTUALEDIT
3003 /* When 'virtualedit' is set the end of the line may be before the
3004 * start of the displayed part. */
3005 if (vcol < v && *ptr == NUL && virtual_active())
3006 vcol = v;
3007 #endif
3009 /* Handle a character that's not completely on the screen: Put ptr at
3010 * that character but skip the first few screen characters. */
3011 if (vcol > v)
3013 vcol -= c;
3014 #ifdef FEAT_MBYTE
3015 ptr = prev_ptr;
3016 #else
3017 --ptr;
3018 #endif
3019 n_skip = v - vcol;
3023 * Adjust for when the inverted text is before the screen,
3024 * and when the start of the inverted text is before the screen.
3026 if (tocol <= vcol)
3027 fromcol = 0;
3028 else if (fromcol >= 0 && fromcol < vcol)
3029 fromcol = vcol;
3031 #ifdef FEAT_LINEBREAK
3032 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3033 if (wp->w_p_wrap)
3034 need_showbreak = TRUE;
3035 #endif
3036 #ifdef FEAT_SPELL
3037 /* When spell checking a word we need to figure out the start of the
3038 * word and if it's badly spelled or not. */
3039 if (has_spell)
3041 int len;
3042 colnr_T linecol = (colnr_T)(ptr - line);
3043 hlf_T spell_hlf = HLF_COUNT;
3045 pos = wp->w_cursor;
3046 wp->w_cursor.lnum = lnum;
3047 wp->w_cursor.col = linecol;
3048 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
3050 /* spell_move_to() may call ml_get() and make "line" invalid */
3051 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3052 ptr = line + linecol;
3054 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
3056 /* no bad word found at line start, don't check until end of a
3057 * word */
3058 spell_hlf = HLF_COUNT;
3059 word_end = (int)(spell_to_word_end(ptr, wp->w_buffer)
3060 - line + 1);
3062 else
3064 /* bad word found, use attributes until end of word */
3065 word_end = wp->w_cursor.col + len + 1;
3067 /* Turn index into actual attributes. */
3068 if (spell_hlf != HLF_COUNT)
3069 spell_attr = highlight_attr[spell_hlf];
3071 wp->w_cursor = pos;
3073 # ifdef FEAT_SYN_HL
3074 /* Need to restart syntax highlighting for this line. */
3075 if (has_syntax)
3076 syntax_start(wp, lnum);
3077 # endif
3079 #endif
3083 * Correct highlighting for cursor that can't be disabled.
3084 * Avoids having to check this for each character.
3086 if (fromcol >= 0)
3088 if (noinvcur)
3090 if ((colnr_T)fromcol == wp->w_virtcol)
3092 /* highlighting starts at cursor, let it start just after the
3093 * cursor */
3094 fromcol_prev = fromcol;
3095 fromcol = -1;
3097 else if ((colnr_T)fromcol < wp->w_virtcol)
3098 /* restart highlighting after the cursor */
3099 fromcol_prev = wp->w_virtcol;
3101 if (fromcol >= tocol)
3102 fromcol = -1;
3105 #ifdef FEAT_SEARCH_EXTRA
3107 * Handle highlighting the last used search pattern and matches.
3108 * Do this for both search_hl and the match list.
3110 cur = wp->w_match_head;
3111 shl_flag = FALSE;
3112 while (cur != NULL || shl_flag == FALSE)
3114 if (shl_flag == FALSE)
3116 shl = &search_hl;
3117 shl_flag = TRUE;
3119 else
3120 shl = &cur->hl;
3121 shl->startcol = MAXCOL;
3122 shl->endcol = MAXCOL;
3123 shl->attr_cur = 0;
3124 if (shl->rm.regprog != NULL)
3126 v = (long)(ptr - line);
3127 next_search_hl(wp, shl, lnum, (colnr_T)v);
3129 /* Need to get the line again, a multi-line regexp may have made it
3130 * invalid. */
3131 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3132 ptr = line + v;
3134 if (shl->lnum != 0 && shl->lnum <= lnum)
3136 if (shl->lnum == lnum)
3137 shl->startcol = shl->rm.startpos[0].col;
3138 else
3139 shl->startcol = 0;
3140 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3141 - shl->rm.startpos[0].lnum)
3142 shl->endcol = shl->rm.endpos[0].col;
3143 else
3144 shl->endcol = MAXCOL;
3145 /* Highlight one character for an empty match. */
3146 if (shl->startcol == shl->endcol)
3148 #ifdef FEAT_MBYTE
3149 if (has_mbyte && line[shl->endcol] != NUL)
3150 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3151 else
3152 #endif
3153 ++shl->endcol;
3155 if ((long)shl->startcol < v) /* match at leftcol */
3157 shl->attr_cur = shl->attr;
3158 search_attr = shl->attr;
3160 area_highlighting = TRUE;
3163 if (shl != &search_hl && cur != NULL)
3164 cur = cur->next;
3166 #endif
3168 #ifdef FEAT_SYN_HL
3169 /* Cursor line highlighting for 'cursorline'. Not when Visual mode is
3170 * active, because it's not clear what is selected then. */
3171 if (wp->w_p_cul && lnum == wp->w_cursor.lnum && !VIsual_active)
3173 line_attr = hl_attr(HLF_CUL);
3174 area_highlighting = TRUE;
3176 #endif
3178 off = (unsigned)(current_ScreenLine - ScreenLines);
3179 col = 0;
3180 #ifdef FEAT_RIGHTLEFT
3181 if (wp->w_p_rl)
3183 /* Rightleft window: process the text in the normal direction, but put
3184 * it in current_ScreenLine[] from right to left. Start at the
3185 * rightmost column of the window. */
3186 col = W_WIDTH(wp) - 1;
3187 off += col;
3189 #endif
3192 * Repeat for the whole displayed line.
3194 for (;;)
3196 /* Skip this quickly when working on the text. */
3197 if (draw_state != WL_LINE)
3199 #ifdef FEAT_CMDWIN
3200 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3202 draw_state = WL_CMDLINE;
3203 if (cmdwin_type != 0 && wp == curwin)
3205 /* Draw the cmdline character. */
3206 n_extra = 1;
3207 c_extra = cmdwin_type;
3208 char_attr = hl_attr(HLF_AT);
3211 #endif
3213 #ifdef FEAT_FOLDING
3214 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3216 draw_state = WL_FOLD;
3217 if (wp->w_p_fdc > 0)
3219 /* Draw the 'foldcolumn'. */
3220 fill_foldcolumn(extra, wp, FALSE, lnum);
3221 n_extra = wp->w_p_fdc;
3222 p_extra = extra;
3223 p_extra[n_extra] = NUL;
3224 c_extra = NUL;
3225 char_attr = hl_attr(HLF_FC);
3228 #endif
3230 #ifdef FEAT_SIGNS
3231 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3233 draw_state = WL_SIGN;
3234 /* Show the sign column when there are any signs in this
3235 * buffer or when using Netbeans. */
3236 if (draw_signcolumn(wp)
3237 # ifdef FEAT_DIFF
3238 && filler_todo <= 0
3239 # endif
3242 int_u text_sign;
3243 # ifdef FEAT_SIGN_ICONS
3244 int_u icon_sign;
3245 # endif
3247 /* Draw two cells with the sign value or blank. */
3248 c_extra = ' ';
3249 char_attr = hl_attr(HLF_SC);
3250 n_extra = 2;
3252 if (row == startrow)
3254 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3255 SIGN_TEXT);
3256 # ifdef FEAT_SIGN_ICONS
3257 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3258 SIGN_ICON);
3259 if (gui.in_use && icon_sign != 0)
3261 /* Use the image in this position. */
3262 c_extra = SIGN_BYTE;
3263 # ifdef FEAT_NETBEANS_INTG
3264 if (buf_signcount(wp->w_buffer, lnum) > 1)
3265 c_extra = MULTISIGN_BYTE;
3266 # endif
3267 char_attr = icon_sign;
3269 else
3270 # endif
3271 if (text_sign != 0)
3273 p_extra = sign_get_text(text_sign);
3274 if (p_extra != NULL)
3276 c_extra = NUL;
3277 n_extra = (int)STRLEN(p_extra);
3279 char_attr = sign_get_attr(text_sign, FALSE);
3284 #endif
3286 if (draw_state == WL_NR - 1 && n_extra == 0)
3288 draw_state = WL_NR;
3289 /* Display the line number. After the first fill with blanks
3290 * when the 'n' flag isn't in 'cpo' */
3291 if (wp->w_p_nu
3292 && (row == startrow
3293 #ifdef FEAT_DIFF
3294 + filler_lines
3295 #endif
3296 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3298 /* Draw the line number (empty space after wrapping). */
3299 if (row == startrow
3300 #ifdef FEAT_DIFF
3301 + filler_lines
3302 #endif
3305 sprintf((char *)extra, "%*ld ",
3306 number_width(wp), (long)lnum);
3307 if (wp->w_skipcol > 0)
3308 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3309 *p_extra = '-';
3310 #ifdef FEAT_RIGHTLEFT
3311 if (wp->w_p_rl) /* reverse line numbers */
3312 rl_mirror(extra);
3313 #endif
3314 p_extra = extra;
3315 c_extra = NUL;
3317 else
3318 c_extra = ' ';
3319 n_extra = number_width(wp) + 1;
3320 char_attr = hl_attr(HLF_N);
3321 #ifdef FEAT_SYN_HL
3322 /* When 'cursorline' is set highlight the line number of
3323 * the current line differently. */
3324 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3325 char_attr = hl_combine_attr(hl_attr(HLF_CUL), char_attr);
3326 #endif
3330 #if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3331 if (draw_state == WL_SBR - 1 && n_extra == 0)
3333 draw_state = WL_SBR;
3334 # ifdef FEAT_DIFF
3335 if (filler_todo > 0)
3337 /* Draw "deleted" diff line(s). */
3338 if (char2cells(fill_diff) > 1)
3339 c_extra = '-';
3340 else
3341 c_extra = fill_diff;
3342 # ifdef FEAT_RIGHTLEFT
3343 if (wp->w_p_rl)
3344 n_extra = col + 1;
3345 else
3346 # endif
3347 n_extra = W_WIDTH(wp) - col;
3348 char_attr = hl_attr(HLF_DED);
3350 # endif
3351 # ifdef FEAT_LINEBREAK
3352 if (*p_sbr != NUL && need_showbreak)
3354 /* Draw 'showbreak' at the start of each broken line. */
3355 p_extra = p_sbr;
3356 c_extra = NUL;
3357 n_extra = (int)STRLEN(p_sbr);
3358 char_attr = hl_attr(HLF_AT);
3359 need_showbreak = FALSE;
3360 /* Correct end of highlighted area for 'showbreak',
3361 * required when 'linebreak' is also set. */
3362 if (tocol == vcol)
3363 tocol += n_extra;
3365 # endif
3367 #endif
3369 if (draw_state == WL_LINE - 1 && n_extra == 0)
3371 draw_state = WL_LINE;
3372 if (saved_n_extra)
3374 /* Continue item from end of wrapped line. */
3375 n_extra = saved_n_extra;
3376 c_extra = saved_c_extra;
3377 p_extra = saved_p_extra;
3378 char_attr = saved_char_attr;
3380 else
3381 char_attr = 0;
3385 /* When still displaying '$' of change command, stop at cursor */
3386 if (dollar_vcol != 0 && wp == curwin
3387 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
3388 #ifdef FEAT_DIFF
3389 && filler_todo <= 0
3390 #endif
3393 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3394 wp->w_p_rl);
3395 /* Pretend we have finished updating the window. Except when
3396 * 'cursorcolumn' is set. */
3397 #ifdef FEAT_SYN_HL
3398 if (wp->w_p_cuc)
3399 row = wp->w_cline_row + wp->w_cline_height;
3400 else
3401 #endif
3402 row = wp->w_height;
3403 break;
3406 if (draw_state == WL_LINE && area_highlighting)
3408 /* handle Visual or match highlighting in this line */
3409 if (vcol == fromcol
3410 #ifdef FEAT_MBYTE
3411 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3412 && (*mb_ptr2cells)(ptr) > 1)
3413 #endif
3414 || ((int)vcol_prev == fromcol_prev
3415 && vcol < tocol))
3416 area_attr = attr; /* start highlighting */
3417 else if (area_attr != 0
3418 && (vcol == tocol
3419 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
3420 area_attr = 0; /* stop highlighting */
3422 #ifdef FEAT_SEARCH_EXTRA
3423 if (!n_extra)
3426 * Check for start/end of search pattern match.
3427 * After end, check for start/end of next match.
3428 * When another match, have to check for start again.
3429 * Watch out for matching an empty string!
3430 * Do this for 'search_hl' and the match list (ordered by
3431 * priority).
3433 v = (long)(ptr - line);
3434 cur = wp->w_match_head;
3435 shl_flag = FALSE;
3436 while (cur != NULL || shl_flag == FALSE)
3438 if (shl_flag == FALSE
3439 && ((cur != NULL
3440 && cur->priority > SEARCH_HL_PRIORITY)
3441 || cur == NULL))
3443 shl = &search_hl;
3444 shl_flag = TRUE;
3446 else
3447 shl = &cur->hl;
3448 while (shl->rm.regprog != NULL)
3450 if (shl->startcol != MAXCOL
3451 && v >= (long)shl->startcol
3452 && v < (long)shl->endcol)
3454 shl->attr_cur = shl->attr;
3456 else if (v == (long)shl->endcol)
3458 shl->attr_cur = 0;
3460 next_search_hl(wp, shl, lnum, (colnr_T)v);
3462 /* Need to get the line again, a multi-line regexp
3463 * may have made it invalid. */
3464 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3465 ptr = line + v;
3467 if (shl->lnum == lnum)
3469 shl->startcol = shl->rm.startpos[0].col;
3470 if (shl->rm.endpos[0].lnum == 0)
3471 shl->endcol = shl->rm.endpos[0].col;
3472 else
3473 shl->endcol = MAXCOL;
3475 if (shl->startcol == shl->endcol)
3477 /* highlight empty match, try again after
3478 * it */
3479 #ifdef FEAT_MBYTE
3480 if (has_mbyte)
3481 shl->endcol += (*mb_ptr2len)(line
3482 + shl->endcol);
3483 else
3484 #endif
3485 ++shl->endcol;
3488 /* Loop to check if the match starts at the
3489 * current position */
3490 continue;
3493 break;
3495 if (shl != &search_hl && cur != NULL)
3496 cur = cur->next;
3499 /* Use attributes from match with highest priority among
3500 * 'search_hl' and the match list. */
3501 search_attr = search_hl.attr_cur;
3502 cur = wp->w_match_head;
3503 shl_flag = FALSE;
3504 while (cur != NULL || shl_flag == FALSE)
3506 if (shl_flag == FALSE
3507 && ((cur != NULL
3508 && cur->priority > SEARCH_HL_PRIORITY)
3509 || cur == NULL))
3511 shl = &search_hl;
3512 shl_flag = TRUE;
3514 else
3515 shl = &cur->hl;
3516 if (shl->attr_cur != 0)
3517 search_attr = shl->attr_cur;
3518 if (shl != &search_hl && cur != NULL)
3519 cur = cur->next;
3522 #endif
3524 #ifdef FEAT_DIFF
3525 if (diff_hlf != (hlf_T)0)
3527 if (diff_hlf == HLF_CHD && ptr - line >= change_start
3528 && n_extra == 0)
3529 diff_hlf = HLF_TXD; /* changed text */
3530 if (diff_hlf == HLF_TXD && ptr - line > change_end
3531 && n_extra == 0)
3532 diff_hlf = HLF_CHD; /* changed line */
3533 line_attr = hl_attr(diff_hlf);
3535 #endif
3537 /* Decide which of the highlight attributes to use. */
3538 attr_pri = TRUE;
3539 if (area_attr != 0)
3540 char_attr = area_attr;
3541 else if (search_attr != 0)
3542 char_attr = search_attr;
3543 #ifdef LINE_ATTR
3544 /* Use line_attr when not in the Visual or 'incsearch' area
3545 * (area_attr may be 0 when "noinvcur" is set). */
3546 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
3547 || (vcol < fromcol || vcol >= tocol)))
3548 char_attr = line_attr;
3549 #endif
3550 else
3552 attr_pri = FALSE;
3553 #ifdef FEAT_SYN_HL
3554 if (has_syntax)
3555 char_attr = syntax_attr;
3556 else
3557 #endif
3558 char_attr = 0;
3563 * Get the next character to put on the screen.
3566 * The "p_extra" points to the extra stuff that is inserted to
3567 * represent special characters (non-printable stuff) and other
3568 * things. When all characters are the same, c_extra is used.
3569 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
3570 * "p_extra[n_extra]".
3571 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
3573 if (n_extra > 0)
3575 if (c_extra != NUL)
3577 c = c_extra;
3578 #ifdef FEAT_MBYTE
3579 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
3580 if (enc_utf8 && (*mb_char2len)(c) > 1)
3582 mb_utf8 = TRUE;
3583 u8cc[0] = 0;
3584 c = 0xc0;
3586 else
3587 mb_utf8 = FALSE;
3588 #endif
3590 else
3592 c = *p_extra;
3593 #ifdef FEAT_MBYTE
3594 if (has_mbyte)
3596 mb_c = c;
3597 if (enc_utf8)
3599 /* If the UTF-8 character is more than one byte:
3600 * Decode it into "mb_c". */
3601 mb_l = (*mb_ptr2len)(p_extra);
3602 mb_utf8 = FALSE;
3603 if (mb_l > n_extra)
3604 mb_l = 1;
3605 else if (mb_l > 1)
3607 mb_c = utfc_ptr2char(p_extra, u8cc);
3608 mb_utf8 = TRUE;
3609 c = 0xc0;
3612 else
3614 /* if this is a DBCS character, put it in "mb_c" */
3615 mb_l = MB_BYTE2LEN(c);
3616 if (mb_l >= n_extra)
3617 mb_l = 1;
3618 else if (mb_l > 1)
3619 mb_c = (c << 8) + p_extra[1];
3621 if (mb_l == 0) /* at the NUL at end-of-line */
3622 mb_l = 1;
3624 /* If a double-width char doesn't fit display a '>' in the
3625 * last column. */
3626 if ((
3627 # ifdef FEAT_RIGHTLEFT
3628 wp->w_p_rl ? (col <= 0) :
3629 # endif
3630 (col >= W_WIDTH(wp) - 1))
3631 && (*mb_char2cells)(mb_c) == 2)
3633 c = '>';
3634 mb_c = c;
3635 mb_l = 1;
3636 mb_utf8 = FALSE;
3637 multi_attr = hl_attr(HLF_AT);
3638 /* put the pointer back to output the double-width
3639 * character at the start of the next line. */
3640 ++n_extra;
3641 --p_extra;
3643 else
3645 n_extra -= mb_l - 1;
3646 p_extra += mb_l - 1;
3649 #endif
3650 ++p_extra;
3652 --n_extra;
3654 else
3657 * Get a character from the line itself.
3659 c = *ptr;
3660 #ifdef FEAT_MBYTE
3661 if (has_mbyte)
3663 mb_c = c;
3664 if (enc_utf8)
3666 /* If the UTF-8 character is more than one byte: Decode it
3667 * into "mb_c". */
3668 mb_l = (*mb_ptr2len)(ptr);
3669 mb_utf8 = FALSE;
3670 if (mb_l > 1)
3672 mb_c = utfc_ptr2char(ptr, u8cc);
3673 /* Overlong encoded ASCII or ASCII with composing char
3674 * is displayed normally, except a NUL. */
3675 if (mb_c < 0x80)
3676 c = mb_c;
3677 mb_utf8 = TRUE;
3679 /* At start of the line we can have a composing char.
3680 * Draw it as a space with a composing char. */
3681 if (utf_iscomposing(mb_c))
3683 int i;
3685 for (i = Screen_mco - 1; i > 0; --i)
3686 u8cc[i] = u8cc[i - 1];
3687 u8cc[0] = mb_c;
3688 mb_c = ' ';
3692 if ((mb_l == 1 && c >= 0x80)
3693 || (mb_l >= 1 && mb_c == 0)
3694 || (mb_l > 1 && (!vim_isprintc(mb_c)
3695 # ifdef UNICODE16
3696 || mb_c >= 0x10000
3697 # endif
3701 * Illegal UTF-8 byte: display as <xx>.
3702 * Non-BMP character : display as ? or fullwidth ?.
3704 # ifdef UNICODE16
3705 if (mb_c < 0x10000)
3706 # endif
3708 transchar_hex(extra, mb_c);
3709 # ifdef FEAT_RIGHTLEFT
3710 if (wp->w_p_rl) /* reverse */
3711 rl_mirror(extra);
3712 # endif
3714 # ifdef UNICODE16
3715 else if (utf_char2cells(mb_c) != 2)
3716 STRCPY(extra, "?");
3717 else
3718 /* 0xff1f in UTF-8: full-width '?' */
3719 STRCPY(extra, "\357\274\237");
3720 # endif
3722 p_extra = extra;
3723 c = *p_extra;
3724 mb_c = mb_ptr2char_adv(&p_extra);
3725 mb_utf8 = (c >= 0x80);
3726 n_extra = (int)STRLEN(p_extra);
3727 c_extra = NUL;
3728 if (area_attr == 0 && search_attr == 0)
3730 n_attr = n_extra + 1;
3731 extra_attr = hl_attr(HLF_8);
3732 saved_attr2 = char_attr; /* save current attr */
3735 else if (mb_l == 0) /* at the NUL at end-of-line */
3736 mb_l = 1;
3737 #ifdef FEAT_ARABIC
3738 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
3740 /* Do Arabic shaping. */
3741 int pc, pc1, nc;
3742 int pcc[MAX_MCO];
3744 /* The idea of what is the previous and next
3745 * character depends on 'rightleft'. */
3746 if (wp->w_p_rl)
3748 pc = prev_c;
3749 pc1 = prev_c1;
3750 nc = utf_ptr2char(ptr + mb_l);
3751 prev_c1 = u8cc[0];
3753 else
3755 pc = utfc_ptr2char(ptr + mb_l, pcc);
3756 nc = prev_c;
3757 pc1 = pcc[0];
3759 prev_c = mb_c;
3761 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
3763 else
3764 prev_c = mb_c;
3765 #endif
3767 else /* enc_dbcs */
3769 mb_l = MB_BYTE2LEN(c);
3770 if (mb_l == 0) /* at the NUL at end-of-line */
3771 mb_l = 1;
3772 else if (mb_l > 1)
3774 /* We assume a second byte below 32 is illegal.
3775 * Hopefully this is OK for all double-byte encodings!
3777 if (ptr[1] >= 32)
3778 mb_c = (c << 8) + ptr[1];
3779 else
3781 if (ptr[1] == NUL)
3783 /* head byte at end of line */
3784 mb_l = 1;
3785 transchar_nonprint(extra, c);
3787 else
3789 /* illegal tail byte */
3790 mb_l = 2;
3791 STRCPY(extra, "XX");
3793 p_extra = extra;
3794 n_extra = (int)STRLEN(extra) - 1;
3795 c_extra = NUL;
3796 c = *p_extra++;
3797 if (area_attr == 0 && search_attr == 0)
3799 n_attr = n_extra + 1;
3800 extra_attr = hl_attr(HLF_8);
3801 saved_attr2 = char_attr; /* save current attr */
3803 mb_c = c;
3807 /* If a double-width char doesn't fit display a '>' in the
3808 * last column; the character is displayed at the start of the
3809 * next line. */
3810 if ((
3811 # ifdef FEAT_RIGHTLEFT
3812 wp->w_p_rl ? (col <= 0) :
3813 # endif
3814 (col >= W_WIDTH(wp) - 1))
3815 && (*mb_char2cells)(mb_c) == 2)
3817 c = '>';
3818 mb_c = c;
3819 mb_utf8 = FALSE;
3820 mb_l = 1;
3821 multi_attr = hl_attr(HLF_AT);
3822 /* Put pointer back so that the character will be
3823 * displayed at the start of the next line. */
3824 --ptr;
3826 else if (*ptr != NUL)
3827 ptr += mb_l - 1;
3829 /* If a double-width char doesn't fit at the left side display
3830 * a '<' in the first column. */
3831 if (n_skip > 0 && mb_l > 1)
3833 n_extra = 1;
3834 c_extra = '<';
3835 c = ' ';
3836 if (area_attr == 0 && search_attr == 0)
3838 n_attr = n_extra + 1;
3839 extra_attr = hl_attr(HLF_AT);
3840 saved_attr2 = char_attr; /* save current attr */
3842 mb_c = c;
3843 mb_utf8 = FALSE;
3844 mb_l = 1;
3848 #endif
3849 ++ptr;
3851 /* 'list' : change char 160 to lcs_nbsp. */
3852 if (wp->w_p_list && (c == 160
3853 #ifdef FEAT_MBYTE
3854 || (mb_utf8 && mb_c == 160)
3855 #endif
3856 ) && lcs_nbsp)
3858 c = lcs_nbsp;
3859 if (area_attr == 0 && search_attr == 0)
3861 n_attr = 1;
3862 extra_attr = hl_attr(HLF_8);
3863 saved_attr2 = char_attr; /* save current attr */
3865 #ifdef FEAT_MBYTE
3866 mb_c = c;
3867 if (enc_utf8 && (*mb_char2len)(c) > 1)
3869 mb_utf8 = TRUE;
3870 u8cc[0] = 0;
3871 c = 0xc0;
3873 else
3874 mb_utf8 = FALSE;
3875 #endif
3878 if (extra_check)
3880 #ifdef FEAT_SPELL
3881 int can_spell = TRUE;
3882 #endif
3884 #ifdef FEAT_SYN_HL
3885 /* Get syntax attribute, unless still at the start of the line
3886 * (double-wide char that doesn't fit). */
3887 v = (long)(ptr - line);
3888 if (has_syntax && v > 0)
3890 /* Get the syntax attribute for the character. If there
3891 * is an error, disable syntax highlighting. */
3892 save_did_emsg = did_emsg;
3893 did_emsg = FALSE;
3895 syntax_attr = get_syntax_attr((colnr_T)v - 1,
3896 # ifdef FEAT_SPELL
3897 has_spell ? &can_spell :
3898 # endif
3899 NULL, FALSE);
3901 if (did_emsg)
3903 wp->w_buffer->b_syn_error = TRUE;
3904 has_syntax = FALSE;
3906 else
3907 did_emsg = save_did_emsg;
3909 /* Need to get the line again, a multi-line regexp may
3910 * have made it invalid. */
3911 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3912 ptr = line + v;
3914 if (!attr_pri)
3915 char_attr = syntax_attr;
3916 else
3917 char_attr = hl_combine_attr(syntax_attr, char_attr);
3919 #endif
3921 #ifdef FEAT_SPELL
3922 /* Check spelling (unless at the end of the line).
3923 * Only do this when there is no syntax highlighting, the
3924 * @Spell cluster is not used or the current syntax item
3925 * contains the @Spell cluster. */
3926 if (has_spell && v >= word_end && v > cur_checked_col)
3928 spell_attr = 0;
3929 # ifdef FEAT_SYN_HL
3930 if (!attr_pri)
3931 char_attr = syntax_attr;
3932 # endif
3933 if (c != 0 && (
3934 # ifdef FEAT_SYN_HL
3935 !has_syntax ||
3936 # endif
3937 can_spell))
3939 char_u *prev_ptr, *p;
3940 int len;
3941 hlf_T spell_hlf = HLF_COUNT;
3942 # ifdef FEAT_MBYTE
3943 if (has_mbyte)
3945 prev_ptr = ptr - mb_l;
3946 v -= mb_l - 1;
3948 else
3949 # endif
3950 prev_ptr = ptr - 1;
3952 /* Use nextline[] if possible, it has the start of the
3953 * next line concatenated. */
3954 if ((prev_ptr - line) - nextlinecol >= 0)
3955 p = nextline + (prev_ptr - line) - nextlinecol;
3956 else
3957 p = prev_ptr;
3958 cap_col -= (int)(prev_ptr - line);
3959 len = spell_check(wp, p, &spell_hlf, &cap_col,
3960 nochange);
3961 word_end = v + len;
3963 /* In Insert mode only highlight a word that
3964 * doesn't touch the cursor. */
3965 if (spell_hlf != HLF_COUNT
3966 && (State & INSERT) != 0
3967 && wp->w_cursor.lnum == lnum
3968 && wp->w_cursor.col >=
3969 (colnr_T)(prev_ptr - line)
3970 && wp->w_cursor.col < (colnr_T)word_end)
3972 spell_hlf = HLF_COUNT;
3973 spell_redraw_lnum = lnum;
3976 if (spell_hlf == HLF_COUNT && p != prev_ptr
3977 && (p - nextline) + len > nextline_idx)
3979 /* Remember that the good word continues at the
3980 * start of the next line. */
3981 checked_lnum = lnum + 1;
3982 checked_col = (int)((p - nextline) + len - nextline_idx);
3985 /* Turn index into actual attributes. */
3986 if (spell_hlf != HLF_COUNT)
3987 spell_attr = highlight_attr[spell_hlf];
3989 if (cap_col > 0)
3991 if (p != prev_ptr
3992 && (p - nextline) + cap_col >= nextline_idx)
3994 /* Remember that the word in the next line
3995 * must start with a capital. */
3996 capcol_lnum = lnum + 1;
3997 cap_col = (int)((p - nextline) + cap_col
3998 - nextline_idx);
4000 else
4001 /* Compute the actual column. */
4002 cap_col += (int)(prev_ptr - line);
4006 if (spell_attr != 0)
4008 if (!attr_pri)
4009 char_attr = hl_combine_attr(char_attr, spell_attr);
4010 else
4011 char_attr = hl_combine_attr(spell_attr, char_attr);
4013 #endif
4014 #ifdef FEAT_LINEBREAK
4016 * Found last space before word: check for line break.
4018 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr)
4019 && !wp->w_p_list)
4021 n_extra = win_lbr_chartabsize(wp, ptr - (
4022 # ifdef FEAT_MBYTE
4023 has_mbyte ? mb_l :
4024 # endif
4025 1), (colnr_T)vcol, NULL) - 1;
4026 c_extra = ' ';
4027 if (vim_iswhite(c))
4028 c = ' ';
4030 #endif
4032 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4034 c = lcs_trail;
4035 if (!attr_pri)
4037 n_attr = 1;
4038 extra_attr = hl_attr(HLF_8);
4039 saved_attr2 = char_attr; /* save current attr */
4041 #ifdef FEAT_MBYTE
4042 mb_c = c;
4043 if (enc_utf8 && (*mb_char2len)(c) > 1)
4045 mb_utf8 = TRUE;
4046 u8cc[0] = 0;
4047 c = 0xc0;
4049 else
4050 mb_utf8 = FALSE;
4051 #endif
4056 * Handling of non-printable characters.
4058 if (!(chartab[c & 0xff] & CT_PRINT_CHAR))
4061 * when getting a character from the file, we may have to
4062 * turn it into something else on the way to putting it
4063 * into "ScreenLines".
4065 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4067 /* tab amount depends on current column */
4068 n_extra = (int)wp->w_buffer->b_p_ts
4069 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4070 #ifdef FEAT_MBYTE
4071 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4072 #endif
4073 if (wp->w_p_list)
4075 c = lcs_tab1;
4076 c_extra = lcs_tab2;
4077 n_attr = n_extra + 1;
4078 extra_attr = hl_attr(HLF_8);
4079 saved_attr2 = char_attr; /* save current attr */
4080 #ifdef FEAT_MBYTE
4081 mb_c = c;
4082 if (enc_utf8 && (*mb_char2len)(c) > 1)
4084 mb_utf8 = TRUE;
4085 u8cc[0] = 0;
4086 c = 0xc0;
4088 #endif
4090 else
4092 c_extra = ' ';
4093 c = ' ';
4096 else if (c == NUL
4097 && ((wp->w_p_list && lcs_eol > 0)
4098 || ((fromcol >= 0 || fromcol_prev >= 0)
4099 && tocol > vcol
4100 #ifdef FEAT_VISUAL
4101 && VIsual_mode != Ctrl_V
4102 #endif
4103 && (
4104 # ifdef FEAT_RIGHTLEFT
4105 wp->w_p_rl ? (col >= 0) :
4106 # endif
4107 (col < W_WIDTH(wp)))
4108 && !(noinvcur
4109 && (colnr_T)vcol == wp->w_virtcol)))
4110 && lcs_eol_one >= 0)
4112 /* Display a '$' after the line or highlight an extra
4113 * character if the line break is included. */
4114 #if defined(FEAT_DIFF) || defined(LINE_ATTR)
4115 /* For a diff line the highlighting continues after the
4116 * "$". */
4117 if (
4118 # ifdef FEAT_DIFF
4119 diff_hlf == (hlf_T)0
4120 # ifdef LINE_ATTR
4122 # endif
4123 # endif
4124 # ifdef LINE_ATTR
4125 line_attr == 0
4126 # endif
4128 #endif
4130 #ifdef FEAT_VIRTUALEDIT
4131 /* In virtualedit, visual selections may extend
4132 * beyond end of line. */
4133 if (area_highlighting && virtual_active()
4134 && tocol != MAXCOL && vcol < tocol)
4135 n_extra = 0;
4136 else
4137 #endif
4139 p_extra = at_end_str;
4140 n_extra = 1;
4141 c_extra = NUL;
4144 if (wp->w_p_list)
4145 c = lcs_eol;
4146 else
4147 c = ' ';
4148 lcs_eol_one = -1;
4149 --ptr; /* put it back at the NUL */
4150 if (!attr_pri)
4152 extra_attr = hl_attr(HLF_AT);
4153 n_attr = 1;
4155 #ifdef FEAT_MBYTE
4156 mb_c = c;
4157 if (enc_utf8 && (*mb_char2len)(c) > 1)
4159 mb_utf8 = TRUE;
4160 u8cc[0] = 0;
4161 c = 0xc0;
4163 else
4164 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4165 #endif
4167 else if (c != NUL)
4169 p_extra = transchar(c);
4170 #ifdef FEAT_RIGHTLEFT
4171 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4172 rl_mirror(p_extra); /* reverse "<12>" */
4173 #endif
4174 n_extra = byte2cells(c) - 1;
4175 c_extra = NUL;
4176 c = *p_extra++;
4177 if (!attr_pri)
4179 n_attr = n_extra + 1;
4180 extra_attr = hl_attr(HLF_8);
4181 saved_attr2 = char_attr; /* save current attr */
4183 #ifdef FEAT_MBYTE
4184 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4185 #endif
4187 #ifdef FEAT_VIRTUALEDIT
4188 else if (VIsual_active
4189 && (VIsual_mode == Ctrl_V
4190 || VIsual_mode == 'v')
4191 && virtual_active()
4192 && tocol != MAXCOL
4193 && vcol < tocol
4194 && (
4195 # ifdef FEAT_RIGHTLEFT
4196 wp->w_p_rl ? (col >= 0) :
4197 # endif
4198 (col < W_WIDTH(wp))))
4200 c = ' ';
4201 --ptr; /* put it back at the NUL */
4203 #endif
4204 #if defined(LINE_ATTR)
4205 else if ((
4206 # ifdef FEAT_DIFF
4207 diff_hlf != (hlf_T)0 ||
4208 # endif
4209 line_attr != 0
4210 ) && (
4211 # ifdef FEAT_RIGHTLEFT
4212 wp->w_p_rl ? (col >= 0) :
4213 # endif
4214 (col < W_WIDTH(wp))))
4216 /* Highlight until the right side of the window */
4217 c = ' ';
4218 --ptr; /* put it back at the NUL */
4220 /* Remember we do the char for line highlighting. */
4221 ++did_line_attr;
4223 /* don't do search HL for the rest of the line */
4224 if (line_attr != 0 && char_attr == search_attr && col > 0)
4225 char_attr = line_attr;
4226 # ifdef FEAT_DIFF
4227 if (diff_hlf == HLF_TXD)
4229 diff_hlf = HLF_CHD;
4230 if (attr == 0 || char_attr != attr)
4231 char_attr = hl_attr(diff_hlf);
4233 # endif
4235 #endif
4239 /* Don't override visual selection highlighting. */
4240 if (n_attr > 0
4241 && draw_state == WL_LINE
4242 && !attr_pri)
4243 char_attr = extra_attr;
4245 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
4246 /* XIM don't send preedit_start and preedit_end, but they send
4247 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
4248 * im_is_preediting() here. */
4249 if (xic != NULL
4250 && lnum == curwin->w_cursor.lnum
4251 && (State & INSERT)
4252 && !p_imdisable
4253 && im_is_preediting()
4254 && draw_state == WL_LINE)
4256 colnr_T tcol;
4258 if (preedit_end_col == MAXCOL)
4259 getvcol(curwin, &(curwin->w_cursor), &tcol, NULL, NULL);
4260 else
4261 tcol = preedit_end_col;
4262 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
4264 if (feedback_old_attr < 0)
4266 feedback_col = 0;
4267 feedback_old_attr = char_attr;
4269 char_attr = im_get_feedback_attr(feedback_col);
4270 if (char_attr < 0)
4271 char_attr = feedback_old_attr;
4272 feedback_col++;
4274 else if (feedback_old_attr >= 0)
4276 char_attr = feedback_old_attr;
4277 feedback_old_attr = -1;
4278 feedback_col = 0;
4281 #endif
4283 * Handle the case where we are in column 0 but not on the first
4284 * character of the line and the user wants us to show us a
4285 * special character (via 'listchars' option "precedes:<char>".
4287 if (lcs_prec_todo != NUL
4288 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
4289 #ifdef FEAT_DIFF
4290 && filler_todo <= 0
4291 #endif
4292 && draw_state > WL_NR
4293 && c != NUL)
4295 c = lcs_prec;
4296 lcs_prec_todo = NUL;
4297 #ifdef FEAT_MBYTE
4298 mb_c = c;
4299 if (enc_utf8 && (*mb_char2len)(c) > 1)
4301 mb_utf8 = TRUE;
4302 u8cc[0] = 0;
4303 c = 0xc0;
4305 else
4306 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4307 #endif
4308 if (!attr_pri)
4310 saved_attr3 = char_attr; /* save current attr */
4311 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
4312 n_attr3 = 1;
4317 * At end of the text line or just after the last character.
4319 if (c == NUL
4320 #if defined(LINE_ATTR)
4321 || did_line_attr == 1
4322 #endif
4325 #ifdef FEAT_SEARCH_EXTRA
4326 long prevcol = (long)(ptr - line) - (c == NUL);
4328 /* we're not really at that column when skipping some text */
4329 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
4330 ++prevcol;
4331 #endif
4333 /* invert at least one char, used for Visual and empty line or
4334 * highlight match at end of line. If it's beyond the last
4335 * char on the screen, just overwrite that one (tricky!) Not
4336 * needed when a '$' was displayed for 'list'. */
4337 #ifdef FEAT_SEARCH_EXTRA
4338 prevcol_hl_flag = FALSE;
4339 if (prevcol == (long)search_hl.startcol)
4340 prevcol_hl_flag = TRUE;
4341 else
4343 cur = wp->w_match_head;
4344 while (cur != NULL)
4346 if (prevcol == (long)cur->hl.startcol)
4348 prevcol_hl_flag = TRUE;
4349 break;
4351 cur = cur->next;
4354 #endif
4355 if (lcs_eol == lcs_eol_one
4356 && ((area_attr != 0 && vcol == fromcol && c == NUL)
4357 #ifdef FEAT_SEARCH_EXTRA
4358 /* highlight 'hlsearch' match at end of line */
4359 || (prevcol_hl_flag == TRUE
4360 # if defined(LINE_ATTR)
4361 && did_line_attr <= 1
4362 # endif
4364 #endif
4367 int n = 0;
4369 #ifdef FEAT_RIGHTLEFT
4370 if (wp->w_p_rl)
4372 if (col < 0)
4373 n = 1;
4375 else
4376 #endif
4378 if (col >= W_WIDTH(wp))
4379 n = -1;
4381 if (n != 0)
4383 /* At the window boundary, highlight the last character
4384 * instead (better than nothing). */
4385 off += n;
4386 col += n;
4388 else
4390 /* Add a blank character to highlight. */
4391 ScreenLines[off] = ' ';
4392 #ifdef FEAT_MBYTE
4393 if (enc_utf8)
4394 ScreenLinesUC[off] = 0;
4395 #endif
4397 #ifdef FEAT_SEARCH_EXTRA
4398 if (area_attr == 0)
4400 /* Use attributes from match with highest priority among
4401 * 'search_hl' and the match list. */
4402 char_attr = search_hl.attr;
4403 cur = wp->w_match_head;
4404 shl_flag = FALSE;
4405 while (cur != NULL || shl_flag == FALSE)
4407 if (shl_flag == FALSE
4408 && ((cur != NULL
4409 && cur->priority > SEARCH_HL_PRIORITY)
4410 || cur == NULL))
4412 shl = &search_hl;
4413 shl_flag = TRUE;
4415 else
4416 shl = &cur->hl;
4417 if ((ptr - line) - 1 == (long)shl->startcol)
4418 char_attr = shl->attr;
4419 if (shl != &search_hl && cur != NULL)
4420 cur = cur->next;
4423 #endif
4424 ScreenAttrs[off] = char_attr;
4425 #ifdef FEAT_RIGHTLEFT
4426 if (wp->w_p_rl)
4428 --col;
4429 --off;
4431 else
4432 #endif
4434 ++col;
4435 ++off;
4437 ++vcol;
4438 #ifdef FEAT_SYN_HL
4439 eol_hl_off = 1;
4440 #endif
4445 * At end of the text line.
4447 if (c == NUL)
4449 #ifdef FEAT_SYN_HL
4450 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol)
4452 /* highlight last char after line */
4453 --col;
4454 --off;
4455 --vcol;
4458 /* Highlight 'cursorcolumn' past end of the line. */
4459 if (wp->w_p_wrap)
4460 v = wp->w_skipcol;
4461 else
4462 v = wp->w_leftcol;
4463 /* check if line ends before left margin */
4464 if (vcol < v + col - win_col_off(wp))
4466 vcol = v + col - win_col_off(wp);
4467 if (wp->w_p_cuc
4468 && (int)wp->w_virtcol >= vcol - eol_hl_off
4469 && (int)wp->w_virtcol < W_WIDTH(wp) * (row - startrow + 1)
4471 && lnum != wp->w_cursor.lnum
4472 # ifdef FEAT_RIGHTLEFT
4473 && !wp->w_p_rl
4474 # endif
4477 while (col < W_WIDTH(wp))
4479 ScreenLines[off] = ' ';
4480 #ifdef FEAT_MBYTE
4481 if (enc_utf8)
4482 ScreenLinesUC[off] = 0;
4483 #endif
4484 ++col;
4485 if (vcol == (long)wp->w_virtcol)
4487 ScreenAttrs[off] = hl_attr(HLF_CUC);
4488 break;
4490 ScreenAttrs[off++] = 0;
4491 ++vcol;
4494 #endif
4496 SCREEN_LINE(screen_row, W_WINCOL(wp), col, (int)W_WIDTH(wp),
4497 wp->w_p_rl);
4498 row++;
4501 * Update w_cline_height and w_cline_folded if the cursor line was
4502 * updated (saves a call to plines() later).
4504 if (wp == curwin && lnum == curwin->w_cursor.lnum)
4506 curwin->w_cline_row = startrow;
4507 curwin->w_cline_height = row - startrow;
4508 #ifdef FEAT_FOLDING
4509 curwin->w_cline_folded = FALSE;
4510 #endif
4511 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
4514 break;
4517 /* line continues beyond line end */
4518 if (lcs_ext
4519 && !wp->w_p_wrap
4520 #ifdef FEAT_DIFF
4521 && filler_todo <= 0
4522 #endif
4523 && (
4524 #ifdef FEAT_RIGHTLEFT
4525 wp->w_p_rl ? col == 0 :
4526 #endif
4527 col == W_WIDTH(wp) - 1)
4528 && (*ptr != NUL
4529 || (wp->w_p_list && lcs_eol_one > 0)
4530 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
4532 c = lcs_ext;
4533 char_attr = hl_attr(HLF_AT);
4534 #ifdef FEAT_MBYTE
4535 mb_c = c;
4536 if (enc_utf8 && (*mb_char2len)(c) > 1)
4538 mb_utf8 = TRUE;
4539 u8cc[0] = 0;
4540 c = 0xc0;
4542 else
4543 mb_utf8 = FALSE;
4544 #endif
4547 #ifdef FEAT_SYN_HL
4548 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
4549 * highlight the cursor position itself. */
4550 if (wp->w_p_cuc && vcol == (long)wp->w_virtcol
4551 && lnum != wp->w_cursor.lnum
4552 && draw_state == WL_LINE)
4554 vcol_save_attr = char_attr;
4555 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
4557 else
4558 vcol_save_attr = -1;
4559 #endif
4562 * Store character to be displayed.
4563 * Skip characters that are left of the screen for 'nowrap'.
4565 vcol_prev = vcol;
4566 if (draw_state < WL_LINE || n_skip <= 0)
4569 * Store the character.
4571 #if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
4572 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
4574 /* A double-wide character is: put first halve in left cell. */
4575 --off;
4576 --col;
4578 #endif
4579 ScreenLines[off] = c;
4580 #ifdef FEAT_MBYTE
4581 if (enc_dbcs == DBCS_JPNU)
4582 ScreenLines2[off] = mb_c & 0xff;
4583 else if (enc_utf8)
4585 if (mb_utf8)
4587 int i;
4589 ScreenLinesUC[off] = mb_c;
4590 if ((c & 0xff) == 0)
4591 ScreenLines[off] = 0x80; /* avoid storing zero */
4592 for (i = 0; i < Screen_mco; ++i)
4594 ScreenLinesC[i][off] = u8cc[i];
4595 if (u8cc[i] == 0)
4596 break;
4599 else
4600 ScreenLinesUC[off] = 0;
4602 if (multi_attr)
4604 ScreenAttrs[off] = multi_attr;
4605 multi_attr = 0;
4607 else
4608 #endif
4609 ScreenAttrs[off] = char_attr;
4611 #ifdef FEAT_MBYTE
4612 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
4614 /* Need to fill two screen columns. */
4615 ++off;
4616 ++col;
4617 if (enc_utf8)
4618 /* UTF-8: Put a 0 in the second screen char. */
4619 ScreenLines[off] = 0;
4620 else
4621 /* DBCS: Put second byte in the second screen char. */
4622 ScreenLines[off] = mb_c & 0xff;
4623 ++vcol;
4624 /* When "tocol" is halfway a character, set it to the end of
4625 * the character, otherwise highlighting won't stop. */
4626 if (tocol == vcol)
4627 ++tocol;
4628 #ifdef FEAT_RIGHTLEFT
4629 if (wp->w_p_rl)
4631 /* now it's time to backup one cell */
4632 --off;
4633 --col;
4635 #endif
4637 #endif
4638 #ifdef FEAT_RIGHTLEFT
4639 if (wp->w_p_rl)
4641 --off;
4642 --col;
4644 else
4645 #endif
4647 ++off;
4648 ++col;
4651 else
4652 --n_skip;
4654 /* Only advance the "vcol" when after the 'number' column. */
4655 if (draw_state >= WL_SBR
4656 #ifdef FEAT_DIFF
4657 && filler_todo <= 0
4658 #endif
4660 ++vcol;
4662 #ifdef FEAT_SYN_HL
4663 if (vcol_save_attr >= 0)
4664 char_attr = vcol_save_attr;
4665 #endif
4667 /* restore attributes after "predeces" in 'listchars' */
4668 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
4669 char_attr = saved_attr3;
4671 /* restore attributes after last 'listchars' or 'number' char */
4672 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
4673 char_attr = saved_attr2;
4676 * At end of screen line and there is more to come: Display the line
4677 * so far. If there is no more to display it is caught above.
4679 if ((
4680 #ifdef FEAT_RIGHTLEFT
4681 wp->w_p_rl ? (col < 0) :
4682 #endif
4683 (col >= W_WIDTH(wp)))
4684 && (*ptr != NUL
4685 #ifdef FEAT_DIFF
4686 || filler_todo > 0
4687 #endif
4688 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
4689 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
4692 SCREEN_LINE(screen_row, W_WINCOL(wp), col, (int)W_WIDTH(wp),
4693 wp->w_p_rl);
4694 ++row;
4695 ++screen_row;
4697 /* When not wrapping and finished diff lines, or when displayed
4698 * '$' and highlighting until last column, break here. */
4699 if ((!wp->w_p_wrap
4700 #ifdef FEAT_DIFF
4701 && filler_todo <= 0
4702 #endif
4703 ) || lcs_eol_one == -1)
4704 break;
4706 /* When the window is too narrow draw all "@" lines. */
4707 if (draw_state != WL_LINE
4708 #ifdef FEAT_DIFF
4709 && filler_todo <= 0
4710 #endif
4713 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
4714 #ifdef FEAT_VERTSPLIT
4715 draw_vsep_win(wp, row);
4716 #endif
4717 row = endrow;
4720 /* When line got too long for screen break here. */
4721 if (row == endrow)
4723 ++row;
4724 break;
4727 if (screen_cur_row == screen_row - 1
4728 #ifdef FEAT_DIFF
4729 && filler_todo <= 0
4730 #endif
4731 && W_WIDTH(wp) == Columns)
4733 /* Remember that the line wraps, used for modeless copy. */
4734 LineWraps[screen_row - 1] = TRUE;
4737 * Special trick to make copy/paste of wrapped lines work with
4738 * xterm/screen: write an extra character beyond the end of
4739 * the line. This will work with all terminal types
4740 * (regardless of the xn,am settings).
4741 * Only do this on a fast tty.
4742 * Only do this if the cursor is on the current line
4743 * (something has been written in it).
4744 * Don't do this for the GUI.
4745 * Don't do this for double-width characters.
4746 * Don't do this for a window not at the right screen border.
4748 if (p_tf
4749 #ifdef FEAT_GUI
4750 && !gui.in_use
4751 #endif
4752 #ifdef FEAT_MBYTE
4753 && !(has_mbyte
4754 && ((*mb_off2cells)(LineOffset[screen_row],
4755 LineOffset[screen_row] + screen_Columns)
4756 == 2
4757 || (*mb_off2cells)(LineOffset[screen_row - 1]
4758 + (int)Columns - 2,
4759 LineOffset[screen_row] + screen_Columns)
4760 == 2))
4761 #endif
4764 /* First make sure we are at the end of the screen line,
4765 * then output the same character again to let the
4766 * terminal know about the wrap. If the terminal doesn't
4767 * auto-wrap, we overwrite the character. */
4768 if (screen_cur_col != W_WIDTH(wp))
4769 screen_char(LineOffset[screen_row - 1]
4770 + (unsigned)Columns - 1,
4771 screen_row - 1, (int)(Columns - 1));
4773 #ifdef FEAT_MBYTE
4774 /* When there is a multi-byte character, just output a
4775 * space to keep it simple. */
4776 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
4777 screen_row - 1] + (Columns - 1)]) > 1)
4778 out_char(' ');
4779 else
4780 #endif
4781 out_char(ScreenLines[LineOffset[screen_row - 1]
4782 + (Columns - 1)]);
4783 /* force a redraw of the first char on the next line */
4784 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
4785 screen_start(); /* don't know where cursor is now */
4789 col = 0;
4790 off = (unsigned)(current_ScreenLine - ScreenLines);
4791 #ifdef FEAT_RIGHTLEFT
4792 if (wp->w_p_rl)
4794 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
4795 off += col;
4797 #endif
4799 /* reset the drawing state for the start of a wrapped line */
4800 draw_state = WL_START;
4801 saved_n_extra = n_extra;
4802 saved_p_extra = p_extra;
4803 saved_c_extra = c_extra;
4804 saved_char_attr = char_attr;
4805 n_extra = 0;
4806 lcs_prec_todo = lcs_prec;
4807 #ifdef FEAT_LINEBREAK
4808 # ifdef FEAT_DIFF
4809 if (filler_todo <= 0)
4810 # endif
4811 need_showbreak = TRUE;
4812 #endif
4813 #ifdef FEAT_DIFF
4814 --filler_todo;
4815 /* When the filler lines are actually below the last line of the
4816 * file, don't draw the line itself, break here. */
4817 if (filler_todo == 0 && wp->w_botfill)
4818 break;
4819 #endif
4822 } /* for every character in the line */
4824 #ifdef FEAT_SPELL
4825 /* After an empty line check first word for capital. */
4826 if (*skipwhite(line) == NUL)
4828 capcol_lnum = lnum + 1;
4829 cap_col = 0;
4831 #endif
4833 return row;
4836 #ifdef FEAT_MBYTE
4837 static int comp_char_differs __ARGS((int, int));
4840 * Return if the composing characters at "off_from" and "off_to" differ.
4842 static int
4843 comp_char_differs(off_from, off_to)
4844 int off_from;
4845 int off_to;
4847 int i;
4849 for (i = 0; i < Screen_mco; ++i)
4851 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
4852 return TRUE;
4853 if (ScreenLinesC[i][off_from] == 0)
4854 break;
4856 return FALSE;
4858 #endif
4861 * Check whether the given character needs redrawing:
4862 * - the (first byte of the) character is different
4863 * - the attributes are different
4864 * - the character is multi-byte and the next byte is different
4865 * - the character is two cells wide and the second cell differs.
4867 static int
4868 char_needs_redraw(off_from, off_to, cols)
4869 int off_from;
4870 int off_to;
4871 int cols;
4873 if (cols > 0
4874 && ((ScreenLines[off_from] != ScreenLines[off_to]
4875 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
4877 #ifdef FEAT_MBYTE
4878 || (enc_dbcs != 0
4879 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
4880 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
4881 ? ScreenLines2[off_from] != ScreenLines2[off_to]
4882 : (cols > 1 && ScreenLines[off_from + 1]
4883 != ScreenLines[off_to + 1])))
4884 || (enc_utf8
4885 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
4886 || (ScreenLinesUC[off_from] != 0
4887 && comp_char_differs(off_from, off_to))
4888 || (cols > 1 && ScreenLines[off_from + 1]
4889 != ScreenLines[off_to + 1])))
4890 #endif
4892 return TRUE;
4893 return FALSE;
4897 * Move one "cooked" screen line to the screen, but only the characters that
4898 * have actually changed. Handle insert/delete character.
4899 * "coloff" gives the first column on the screen for this line.
4900 * "endcol" gives the columns where valid characters are.
4901 * "clear_width" is the width of the window. It's > 0 if the rest of the line
4902 * needs to be cleared, negative otherwise.
4903 * "rlflag" is TRUE in a rightleft window:
4904 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
4905 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
4907 static void
4908 screen_line(row, coloff, endcol, clear_width
4909 #ifdef FEAT_RIGHTLEFT
4910 , rlflag
4911 #endif
4913 int row;
4914 int coloff;
4915 int endcol;
4916 int clear_width;
4917 #ifdef FEAT_RIGHTLEFT
4918 int rlflag;
4919 #endif
4921 unsigned off_from;
4922 unsigned off_to;
4923 #ifdef FEAT_MBYTE
4924 unsigned max_off_from;
4925 unsigned max_off_to;
4926 #endif
4927 int col = 0;
4928 #if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_VERTSPLIT)
4929 int hl;
4930 #endif
4931 int force = FALSE; /* force update rest of the line */
4932 int redraw_this /* bool: does character need redraw? */
4933 #ifdef FEAT_GUI
4934 = TRUE /* For GUI when while-loop empty */
4935 #endif
4937 int redraw_next; /* redraw_this for next character */
4938 #ifdef FEAT_MBYTE
4939 int clear_next = FALSE;
4940 int char_cells; /* 1: normal char */
4941 /* 2: occupies two display cells */
4942 # define CHAR_CELLS char_cells
4943 #else
4944 # define CHAR_CELLS 1
4945 #endif
4947 # ifdef FEAT_CLIPBOARD
4948 clip_may_clear_selection(row, row);
4949 # endif
4951 off_from = (unsigned)(current_ScreenLine - ScreenLines);
4952 off_to = LineOffset[row] + coloff;
4953 #ifdef FEAT_MBYTE
4954 max_off_from = off_from + screen_Columns;
4955 max_off_to = LineOffset[row] + screen_Columns;
4956 #endif
4958 #ifdef FEAT_RIGHTLEFT
4959 if (rlflag)
4961 /* Clear rest first, because it's left of the text. */
4962 if (clear_width > 0)
4964 while (col <= endcol && ScreenLines[off_to] == ' '
4965 && ScreenAttrs[off_to] == 0
4966 # ifdef FEAT_MBYTE
4967 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
4968 # endif
4971 ++off_to;
4972 ++col;
4974 if (col <= endcol)
4975 screen_fill(row, row + 1, col + coloff,
4976 endcol + coloff + 1, ' ', ' ', 0);
4978 col = endcol + 1;
4979 off_to = LineOffset[row] + col + coloff;
4980 off_from += col;
4981 endcol = (clear_width > 0 ? clear_width : -clear_width);
4983 #endif /* FEAT_RIGHTLEFT */
4985 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
4987 while (col < endcol)
4989 #ifdef FEAT_MBYTE
4990 if (has_mbyte && (col + 1 < endcol))
4991 char_cells = (*mb_off2cells)(off_from, max_off_from);
4992 else
4993 char_cells = 1;
4994 #endif
4996 redraw_this = redraw_next;
4997 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
4998 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
5000 #ifdef FEAT_GUI
5001 /* If the next character was bold, then redraw the current character to
5002 * remove any pixels that might have spilt over into us. This only
5003 * happens in the GUI.
5005 if (redraw_next && gui.in_use)
5007 hl = ScreenAttrs[off_to + CHAR_CELLS];
5008 if (hl > HL_ALL)
5009 hl = syn_attr2attr(hl);
5010 if (hl & HL_BOLD)
5011 redraw_this = TRUE;
5013 #endif
5015 if (redraw_this)
5018 * Special handling when 'xs' termcap flag set (hpterm):
5019 * Attributes for characters are stored at the position where the
5020 * cursor is when writing the highlighting code. The
5021 * start-highlighting code must be written with the cursor on the
5022 * first highlighted character. The stop-highlighting code must
5023 * be written with the cursor just after the last highlighted
5024 * character.
5025 * Overwriting a character doesn't remove it's highlighting. Need
5026 * to clear the rest of the line, and force redrawing it
5027 * completely.
5029 if ( p_wiv
5030 && !force
5031 #ifdef FEAT_GUI
5032 && !gui.in_use
5033 #endif
5034 && ScreenAttrs[off_to] != 0
5035 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
5038 * Need to remove highlighting attributes here.
5040 windgoto(row, col + coloff);
5041 out_str(T_CE); /* clear rest of this screen line */
5042 screen_start(); /* don't know where cursor is now */
5043 force = TRUE; /* force redraw of rest of the line */
5044 redraw_next = TRUE; /* or else next char would miss out */
5047 * If the previous character was highlighted, need to stop
5048 * highlighting at this character.
5050 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
5052 screen_attr = ScreenAttrs[off_to - 1];
5053 term_windgoto(row, col + coloff);
5054 screen_stop_highlight();
5056 else
5057 screen_attr = 0; /* highlighting has stopped */
5059 #ifdef FEAT_MBYTE
5060 if (enc_dbcs != 0)
5062 /* Check if overwriting a double-byte with a single-byte or
5063 * the other way around requires another character to be
5064 * redrawn. For UTF-8 this isn't needed, because comparing
5065 * ScreenLinesUC[] is sufficient. */
5066 if (char_cells == 1
5067 && col + 1 < endcol
5068 && (*mb_off2cells)(off_to, max_off_to) > 1)
5070 /* Writing a single-cell character over a double-cell
5071 * character: need to redraw the next cell. */
5072 ScreenLines[off_to + 1] = 0;
5073 redraw_next = TRUE;
5075 else if (char_cells == 2
5076 && col + 2 < endcol
5077 && (*mb_off2cells)(off_to, max_off_to) == 1
5078 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
5080 /* Writing the second half of a double-cell character over
5081 * a double-cell character: need to redraw the second
5082 * cell. */
5083 ScreenLines[off_to + 2] = 0;
5084 redraw_next = TRUE;
5087 if (enc_dbcs == DBCS_JPNU)
5088 ScreenLines2[off_to] = ScreenLines2[off_from];
5090 /* When writing a single-width character over a double-width
5091 * character and at the end of the redrawn text, need to clear out
5092 * the right halve of the old character.
5093 * Also required when writing the right halve of a double-width
5094 * char over the left halve of an existing one. */
5095 if (has_mbyte && col + char_cells == endcol
5096 && ((char_cells == 1
5097 && (*mb_off2cells)(off_to, max_off_to) > 1)
5098 || (char_cells == 2
5099 && (*mb_off2cells)(off_to, max_off_to) == 1
5100 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
5101 clear_next = TRUE;
5102 #endif
5104 ScreenLines[off_to] = ScreenLines[off_from];
5105 #ifdef FEAT_MBYTE
5106 if (enc_utf8)
5108 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
5109 if (ScreenLinesUC[off_from] != 0)
5111 int i;
5113 for (i = 0; i < Screen_mco; ++i)
5114 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
5117 if (char_cells == 2)
5118 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
5119 #endif
5121 #if defined(FEAT_GUI) || defined(UNIX)
5122 /* The bold trick makes a single row of pixels appear in the next
5123 * character. When a bold character is removed, the next
5124 * character should be redrawn too. This happens for our own GUI
5125 * and for some xterms. */
5126 if (
5127 # ifdef FEAT_GUI
5128 gui.in_use
5129 # endif
5130 # if defined(FEAT_GUI) && defined(UNIX)
5132 # endif
5133 # ifdef UNIX
5134 term_is_xterm
5135 # endif
5138 hl = ScreenAttrs[off_to];
5139 if (hl > HL_ALL)
5140 hl = syn_attr2attr(hl);
5141 if (hl & HL_BOLD)
5142 redraw_next = TRUE;
5144 #endif
5145 ScreenAttrs[off_to] = ScreenAttrs[off_from];
5146 #ifdef FEAT_MBYTE
5147 /* For simplicity set the attributes of second half of a
5148 * double-wide character equal to the first half. */
5149 if (char_cells == 2)
5150 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
5152 if (enc_dbcs != 0 && char_cells == 2)
5153 screen_char_2(off_to, row, col + coloff);
5154 else
5155 #endif
5156 screen_char(off_to, row, col + coloff);
5158 else if ( p_wiv
5159 #ifdef FEAT_GUI
5160 && !gui.in_use
5161 #endif
5162 && col + coloff > 0)
5164 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
5167 * Don't output stop-highlight when moving the cursor, it will
5168 * stop the highlighting when it should continue.
5170 screen_attr = 0;
5172 else if (screen_attr != 0)
5173 screen_stop_highlight();
5176 off_to += CHAR_CELLS;
5177 off_from += CHAR_CELLS;
5178 col += CHAR_CELLS;
5181 #ifdef FEAT_MBYTE
5182 if (clear_next)
5184 /* Clear the second half of a double-wide character of which the left
5185 * half was overwritten with a single-wide character. */
5186 ScreenLines[off_to] = ' ';
5187 if (enc_utf8)
5188 ScreenLinesUC[off_to] = 0;
5189 screen_char(off_to, row, col + coloff);
5191 #endif
5193 if (clear_width > 0
5194 #ifdef FEAT_RIGHTLEFT
5195 && !rlflag
5196 #endif
5199 #ifdef FEAT_GUI
5200 int startCol = col;
5201 #endif
5203 /* blank out the rest of the line */
5204 while (col < clear_width && ScreenLines[off_to] == ' '
5205 && ScreenAttrs[off_to] == 0
5206 #ifdef FEAT_MBYTE
5207 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5208 #endif
5211 ++off_to;
5212 ++col;
5214 if (col < clear_width)
5216 #ifdef FEAT_GUI
5218 * In the GUI, clearing the rest of the line may leave pixels
5219 * behind if the first character cleared was bold. Some bold
5220 * fonts spill over the left. In this case we redraw the previous
5221 * character too. If we didn't skip any blanks above, then we
5222 * only redraw if the character wasn't already redrawn anyway.
5224 if (gui.in_use && (col > startCol || !redraw_this))
5226 hl = ScreenAttrs[off_to];
5227 if (hl > HL_ALL || (hl & HL_BOLD))
5229 int prev_cells = 1;
5230 # ifdef FEAT_MBYTE
5231 if (enc_utf8)
5232 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
5233 * that its width is 2. */
5234 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
5235 else if (enc_dbcs != 0)
5237 /* find previous character by counting from first
5238 * column and get its width. */
5239 unsigned off = LineOffset[row];
5240 unsigned max_off = LineOffset[row] + screen_Columns;
5242 while (off < off_to)
5244 prev_cells = (*mb_off2cells)(off, max_off);
5245 off += prev_cells;
5249 if (enc_dbcs != 0 && prev_cells > 1)
5250 screen_char_2(off_to - prev_cells, row,
5251 col + coloff - prev_cells);
5252 else
5253 # endif
5254 screen_char(off_to - prev_cells, row,
5255 col + coloff - prev_cells);
5258 #endif
5259 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
5260 ' ', ' ', 0);
5261 #ifdef FEAT_VERTSPLIT
5262 off_to += clear_width - col;
5263 col = clear_width;
5264 #endif
5268 if (clear_width > 0)
5270 #ifdef FEAT_VERTSPLIT
5271 /* For a window that's left of another, draw the separator char. */
5272 if (col + coloff < Columns)
5274 int c;
5276 c = fillchar_vsep(&hl);
5277 if (ScreenLines[off_to] != c
5278 # ifdef FEAT_MBYTE
5279 || (enc_utf8 && (int)ScreenLinesUC[off_to]
5280 != (c >= 0x80 ? c : 0))
5281 # endif
5282 || ScreenAttrs[off_to] != hl)
5284 ScreenLines[off_to] = c;
5285 ScreenAttrs[off_to] = hl;
5286 # ifdef FEAT_MBYTE
5287 if (enc_utf8)
5289 if (c >= 0x80)
5291 ScreenLinesUC[off_to] = c;
5292 ScreenLinesC[0][off_to] = 0;
5294 else
5295 ScreenLinesUC[off_to] = 0;
5297 # endif
5298 screen_char(off_to, row, col + coloff);
5301 else
5302 #endif
5303 LineWraps[row] = FALSE;
5307 #if defined(FEAT_RIGHTLEFT) || defined(PROTO)
5309 * Mirror text "str" for right-left displaying.
5310 * Only works for single-byte characters (e.g., numbers).
5312 void
5313 rl_mirror(str)
5314 char_u *str;
5316 char_u *p1, *p2;
5317 int t;
5319 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
5321 t = *p1;
5322 *p1 = *p2;
5323 *p2 = t;
5326 #endif
5328 #if defined(FEAT_WINDOWS) || defined(PROTO)
5330 * mark all status lines for redraw; used after first :cd
5332 void
5333 status_redraw_all()
5335 win_T *wp;
5337 for (wp = firstwin; wp; wp = wp->w_next)
5338 if (wp->w_status_height)
5340 wp->w_redr_status = TRUE;
5341 redraw_later(VALID);
5346 * mark all status lines of the current buffer for redraw
5348 void
5349 status_redraw_curbuf()
5351 win_T *wp;
5353 for (wp = firstwin; wp; wp = wp->w_next)
5354 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
5356 wp->w_redr_status = TRUE;
5357 redraw_later(VALID);
5362 * Redraw all status lines that need to be redrawn.
5364 void
5365 redraw_statuslines()
5367 win_T *wp;
5369 for (wp = firstwin; wp; wp = wp->w_next)
5370 if (wp->w_redr_status)
5371 win_redr_status(wp);
5372 if (redraw_tabline)
5373 draw_tabline();
5375 #endif
5377 #if (defined(FEAT_WILDMENU) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
5379 * Redraw all status lines at the bottom of frame "frp".
5381 void
5382 win_redraw_last_status(frp)
5383 frame_T *frp;
5385 if (frp->fr_layout == FR_LEAF)
5386 frp->fr_win->w_redr_status = TRUE;
5387 else if (frp->fr_layout == FR_ROW)
5389 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
5390 win_redraw_last_status(frp);
5392 else /* frp->fr_layout == FR_COL */
5394 frp = frp->fr_child;
5395 while (frp->fr_next != NULL)
5396 frp = frp->fr_next;
5397 win_redraw_last_status(frp);
5400 #endif
5402 #ifdef FEAT_VERTSPLIT
5404 * Draw the verticap separator right of window "wp" starting with line "row".
5406 static void
5407 draw_vsep_win(wp, row)
5408 win_T *wp;
5409 int row;
5411 int hl;
5412 int c;
5414 if (wp->w_vsep_width)
5416 /* draw the vertical separator right of this window */
5417 c = fillchar_vsep(&hl);
5418 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
5419 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
5420 c, ' ', hl);
5423 #endif
5425 #ifdef FEAT_WILDMENU
5426 static int status_match_len __ARGS((expand_T *xp, char_u *s));
5427 static int skip_status_match_char __ARGS((expand_T *xp, char_u *s));
5430 * Get the length of an item as it will be shown in the status line.
5432 static int
5433 status_match_len(xp, s)
5434 expand_T *xp;
5435 char_u *s;
5437 int len = 0;
5439 #ifdef FEAT_MENU
5440 int emenu = (xp->xp_context == EXPAND_MENUS
5441 || xp->xp_context == EXPAND_MENUNAMES);
5443 /* Check for menu separators - replace with '|'. */
5444 if (emenu && menu_is_separator(s))
5445 return 1;
5446 #endif
5448 while (*s != NUL)
5450 if (skip_status_match_char(xp, s))
5451 ++s;
5452 len += ptr2cells(s);
5453 mb_ptr_adv(s);
5456 return len;
5460 * Return TRUE for characters that are not displayed in a status match.
5461 * These are backslashes used for escaping. Do show backslashes in help tags.
5463 static int
5464 skip_status_match_char(xp, s)
5465 expand_T *xp;
5466 char_u *s;
5468 return ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
5469 #ifdef FEAT_MENU
5470 || ((xp->xp_context == EXPAND_MENUS
5471 || xp->xp_context == EXPAND_MENUNAMES)
5472 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
5473 #endif
5478 * Show wildchar matches in the status line.
5479 * Show at least the "match" item.
5480 * We start at item 'first_match' in the list and show all matches that fit.
5482 * If inversion is possible we use it. Else '=' characters are used.
5484 void
5485 win_redr_status_matches(xp, num_matches, matches, match, showtail)
5486 expand_T *xp;
5487 int num_matches;
5488 char_u **matches; /* list of matches */
5489 int match;
5490 int showtail;
5492 #define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
5493 int row;
5494 char_u *buf;
5495 int len;
5496 int clen; /* length in screen cells */
5497 int fillchar;
5498 int attr;
5499 int i;
5500 int highlight = TRUE;
5501 char_u *selstart = NULL;
5502 int selstart_col = 0;
5503 char_u *selend = NULL;
5504 static int first_match = 0;
5505 int add_left = FALSE;
5506 char_u *s;
5507 #ifdef FEAT_MENU
5508 int emenu;
5509 #endif
5510 #if defined(FEAT_MBYTE) || defined(FEAT_MENU)
5511 int l;
5512 #endif
5514 if (matches == NULL) /* interrupted completion? */
5515 return;
5517 #ifdef FEAT_MBYTE
5518 if (has_mbyte)
5519 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
5520 else
5521 #endif
5522 buf = alloc((unsigned)Columns + 1);
5523 if (buf == NULL)
5524 return;
5526 if (match == -1) /* don't show match but original text */
5528 match = 0;
5529 highlight = FALSE;
5531 /* count 1 for the ending ">" */
5532 clen = status_match_len(xp, L_MATCH(match)) + 3;
5533 if (match == 0)
5534 first_match = 0;
5535 else if (match < first_match)
5537 /* jumping left, as far as we can go */
5538 first_match = match;
5539 add_left = TRUE;
5541 else
5543 /* check if match fits on the screen */
5544 for (i = first_match; i < match; ++i)
5545 clen += status_match_len(xp, L_MATCH(i)) + 2;
5546 if (first_match > 0)
5547 clen += 2;
5548 /* jumping right, put match at the left */
5549 if ((long)clen > Columns)
5551 first_match = match;
5552 /* if showing the last match, we can add some on the left */
5553 clen = 2;
5554 for (i = match; i < num_matches; ++i)
5556 clen += status_match_len(xp, L_MATCH(i)) + 2;
5557 if ((long)clen >= Columns)
5558 break;
5560 if (i == num_matches)
5561 add_left = TRUE;
5564 if (add_left)
5565 while (first_match > 0)
5567 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
5568 if ((long)clen >= Columns)
5569 break;
5570 --first_match;
5573 fillchar = fillchar_status(&attr, TRUE);
5575 if (first_match == 0)
5577 *buf = NUL;
5578 len = 0;
5580 else
5582 STRCPY(buf, "< ");
5583 len = 2;
5585 clen = len;
5587 i = first_match;
5588 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
5590 if (i == match)
5592 selstart = buf + len;
5593 selstart_col = clen;
5596 s = L_MATCH(i);
5597 /* Check for menu separators - replace with '|' */
5598 #ifdef FEAT_MENU
5599 emenu = (xp->xp_context == EXPAND_MENUS
5600 || xp->xp_context == EXPAND_MENUNAMES);
5601 if (emenu && menu_is_separator(s))
5603 STRCPY(buf + len, transchar('|'));
5604 l = (int)STRLEN(buf + len);
5605 len += l;
5606 clen += l;
5608 else
5609 #endif
5610 for ( ; *s != NUL; ++s)
5612 if (skip_status_match_char(xp, s))
5613 ++s;
5614 clen += ptr2cells(s);
5615 #ifdef FEAT_MBYTE
5616 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
5618 STRNCPY(buf + len, s, l);
5619 s += l - 1;
5620 len += l;
5622 else
5623 #endif
5625 STRCPY(buf + len, transchar_byte(*s));
5626 len += (int)STRLEN(buf + len);
5629 if (i == match)
5630 selend = buf + len;
5632 *(buf + len++) = ' ';
5633 *(buf + len++) = ' ';
5634 clen += 2;
5635 if (++i == num_matches)
5636 break;
5639 if (i != num_matches)
5641 *(buf + len++) = '>';
5642 ++clen;
5645 buf[len] = NUL;
5647 row = cmdline_row - 1;
5648 if (row >= 0)
5650 if (wild_menu_showing == 0)
5652 if (msg_scrolled > 0)
5654 /* Put the wildmenu just above the command line. If there is
5655 * no room, scroll the screen one line up. */
5656 if (cmdline_row == Rows - 1)
5658 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
5659 ++msg_scrolled;
5661 else
5663 ++cmdline_row;
5664 ++row;
5666 wild_menu_showing = WM_SCROLLED;
5668 else
5670 /* Create status line if needed by setting 'laststatus' to 2.
5671 * Set 'winminheight' to zero to avoid that the window is
5672 * resized. */
5673 if (lastwin->w_status_height == 0)
5675 save_p_ls = p_ls;
5676 save_p_wmh = p_wmh;
5677 p_ls = 2;
5678 p_wmh = 0;
5679 last_status(FALSE);
5681 wild_menu_showing = WM_SHOWN;
5685 screen_puts(buf, row, 0, attr);
5686 if (selstart != NULL && highlight)
5688 *selend = NUL;
5689 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
5692 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
5695 #ifdef FEAT_VERTSPLIT
5696 win_redraw_last_status(topframe);
5697 #else
5698 lastwin->w_redr_status = TRUE;
5699 #endif
5700 vim_free(buf);
5702 #endif
5704 #if defined(FEAT_WINDOWS) || defined(PROTO)
5706 * Redraw the status line of window wp.
5708 * If inversion is possible we use it. Else '=' characters are used.
5710 void
5711 win_redr_status(wp)
5712 win_T *wp;
5714 int row;
5715 char_u *p;
5716 int len;
5717 int fillchar;
5718 int attr;
5719 int this_ru_col;
5721 wp->w_redr_status = FALSE;
5722 if (wp->w_status_height == 0)
5724 /* no status line, can only be last window */
5725 redraw_cmdline = TRUE;
5727 else if (!redrawing()
5728 #ifdef FEAT_INS_EXPAND
5729 /* don't update status line when popup menu is visible and may be
5730 * drawn over it */
5731 || pum_visible()
5732 #endif
5735 /* Don't redraw right now, do it later. */
5736 wp->w_redr_status = TRUE;
5738 #ifdef FEAT_STL_OPT
5739 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
5741 /* redraw custom status line */
5742 redraw_custum_statusline(wp);
5744 #endif
5745 else
5747 fillchar = fillchar_status(&attr, wp == curwin);
5749 get_trans_bufname(wp->w_buffer);
5750 p = NameBuff;
5751 len = (int)STRLEN(p);
5753 if (wp->w_buffer->b_help
5754 #ifdef FEAT_QUICKFIX
5755 || wp->w_p_pvw
5756 #endif
5757 || bufIsChanged(wp->w_buffer)
5758 || wp->w_buffer->b_p_ro)
5759 *(p + len++) = ' ';
5760 if (wp->w_buffer->b_help)
5762 STRCPY(p + len, _("[Help]"));
5763 len += (int)STRLEN(p + len);
5765 #ifdef FEAT_QUICKFIX
5766 if (wp->w_p_pvw)
5768 STRCPY(p + len, _("[Preview]"));
5769 len += (int)STRLEN(p + len);
5771 #endif
5772 if (bufIsChanged(wp->w_buffer))
5774 STRCPY(p + len, "[+]");
5775 len += 3;
5777 if (wp->w_buffer->b_p_ro)
5779 STRCPY(p + len, "[RO]");
5780 len += 4;
5783 #ifndef FEAT_VERTSPLIT
5784 this_ru_col = ru_col;
5785 if (this_ru_col < (Columns + 1) / 2)
5786 this_ru_col = (Columns + 1) / 2;
5787 #else
5788 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
5789 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
5790 this_ru_col = (W_WIDTH(wp) + 1) / 2;
5791 if (this_ru_col <= 1)
5793 p = (char_u *)"<"; /* No room for file name! */
5794 len = 1;
5796 else
5797 #endif
5798 #ifdef FEAT_MBYTE
5799 if (has_mbyte)
5801 int clen = 0, i;
5803 /* Count total number of display cells. */
5804 for (i = 0; p[i] != NUL; i += (*mb_ptr2len)(p + i))
5805 clen += (*mb_ptr2cells)(p + i);
5806 /* Find first character that will fit.
5807 * Going from start to end is much faster for DBCS. */
5808 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
5809 i += (*mb_ptr2len)(p + i))
5810 clen -= (*mb_ptr2cells)(p + i);
5811 len = clen;
5812 if (i > 0)
5814 p = p + i - 1;
5815 *p = '<';
5816 ++len;
5820 else
5821 #endif
5822 if (len > this_ru_col - 1)
5824 p += len - (this_ru_col - 1);
5825 *p = '<';
5826 len = this_ru_col - 1;
5829 row = W_WINROW(wp) + wp->w_height;
5830 screen_puts(p, row, W_WINCOL(wp), attr);
5831 screen_fill(row, row + 1, len + W_WINCOL(wp),
5832 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
5834 if (get_keymap_str(wp, NameBuff, MAXPATHL)
5835 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
5836 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
5837 - 1 + W_WINCOL(wp)), attr);
5839 #ifdef FEAT_CMDL_INFO
5840 win_redr_ruler(wp, TRUE);
5841 #endif
5844 #ifdef FEAT_VERTSPLIT
5846 * May need to draw the character below the vertical separator.
5848 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
5850 if (stl_connected(wp))
5851 fillchar = fillchar_status(&attr, wp == curwin);
5852 else
5853 fillchar = fillchar_vsep(&attr);
5854 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
5855 attr);
5857 #endif
5860 #ifdef FEAT_STL_OPT
5862 * Redraw the status line according to 'statusline' and take care of any
5863 * errors encountered.
5865 static void
5866 redraw_custum_statusline(wp)
5867 win_T *wp;
5869 int save_called_emsg = called_emsg;
5871 called_emsg = FALSE;
5872 win_redr_custom(wp, FALSE);
5873 if (called_emsg)
5874 set_string_option_direct((char_u *)"statusline", -1,
5875 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
5876 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
5877 called_emsg |= save_called_emsg;
5879 #endif
5881 # ifdef FEAT_VERTSPLIT
5883 * Return TRUE if the status line of window "wp" is connected to the status
5884 * line of the window right of it. If not, then it's a vertical separator.
5885 * Only call if (wp->w_vsep_width != 0).
5888 stl_connected(wp)
5889 win_T *wp;
5891 frame_T *fr;
5893 fr = wp->w_frame;
5894 while (fr->fr_parent != NULL)
5896 if (fr->fr_parent->fr_layout == FR_COL)
5898 if (fr->fr_next != NULL)
5899 break;
5901 else
5903 if (fr->fr_next != NULL)
5904 return TRUE;
5906 fr = fr->fr_parent;
5908 return FALSE;
5910 # endif
5912 #endif /* FEAT_WINDOWS */
5914 #if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
5916 * Get the value to show for the language mappings, active 'keymap'.
5919 get_keymap_str(wp, buf, len)
5920 win_T *wp;
5921 char_u *buf; /* buffer for the result */
5922 int len; /* length of buffer */
5924 char_u *p;
5926 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
5927 return FALSE;
5930 #ifdef FEAT_EVAL
5931 buf_T *old_curbuf = curbuf;
5932 win_T *old_curwin = curwin;
5933 char_u *s;
5935 curbuf = wp->w_buffer;
5936 curwin = wp;
5937 STRCPY(buf, "b:keymap_name"); /* must be writable */
5938 ++emsg_skip;
5939 s = p = eval_to_string(buf, NULL, FALSE);
5940 --emsg_skip;
5941 curbuf = old_curbuf;
5942 curwin = old_curwin;
5943 if (p == NULL || *p == NUL)
5944 #endif
5946 #ifdef FEAT_KEYMAP
5947 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
5948 p = wp->w_buffer->b_p_keymap;
5949 else
5950 #endif
5951 p = (char_u *)"lang";
5953 if ((int)(STRLEN(p) + 3) < len)
5954 sprintf((char *)buf, "<%s>", p);
5955 else
5956 buf[0] = NUL;
5957 #ifdef FEAT_EVAL
5958 vim_free(s);
5959 #endif
5961 return buf[0] != NUL;
5963 #endif
5965 #if defined(FEAT_STL_OPT) || defined(PROTO)
5967 * Redraw the status line or ruler of window "wp".
5968 * When "wp" is NULL redraw the tab pages line from 'tabline'.
5970 static void
5971 win_redr_custom(wp, draw_ruler)
5972 win_T *wp;
5973 int draw_ruler; /* TRUE or FALSE */
5975 int attr;
5976 int curattr;
5977 int row;
5978 int col = 0;
5979 int maxwidth;
5980 int width;
5981 int n;
5982 int len;
5983 int fillchar;
5984 char_u buf[MAXPATHL];
5985 char_u *p;
5986 struct stl_hlrec hltab[STL_MAX_ITEM];
5987 struct stl_hlrec tabtab[STL_MAX_ITEM];
5988 int use_sandbox = FALSE;
5990 /* setup environment for the task at hand */
5991 if (wp == NULL)
5993 /* Use 'tabline'. Always at the first line of the screen. */
5994 p = p_tal;
5995 row = 0;
5996 fillchar = ' ';
5997 attr = hl_attr(HLF_TPF);
5998 maxwidth = Columns;
5999 # ifdef FEAT_EVAL
6000 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
6001 # endif
6003 else
6005 row = W_WINROW(wp) + wp->w_height;
6006 fillchar = fillchar_status(&attr, wp == curwin);
6007 maxwidth = W_WIDTH(wp);
6009 if (draw_ruler)
6011 p = p_ruf;
6012 /* advance past any leading group spec - implicit in ru_col */
6013 if (*p == '%')
6015 if (*++p == '-')
6016 p++;
6017 if (atoi((char *) p))
6018 while (VIM_ISDIGIT(*p))
6019 p++;
6020 if (*p++ != '(')
6021 p = p_ruf;
6023 #ifdef FEAT_VERTSPLIT
6024 col = ru_col - (Columns - W_WIDTH(wp));
6025 if (col < (W_WIDTH(wp) + 1) / 2)
6026 col = (W_WIDTH(wp) + 1) / 2;
6027 #else
6028 col = ru_col;
6029 if (col > (Columns + 1) / 2)
6030 col = (Columns + 1) / 2;
6031 #endif
6032 maxwidth = W_WIDTH(wp) - col;
6033 #ifdef FEAT_WINDOWS
6034 if (!wp->w_status_height)
6035 #endif
6037 row = Rows - 1;
6038 --maxwidth; /* writing in last column may cause scrolling */
6039 fillchar = ' ';
6040 attr = 0;
6043 # ifdef FEAT_EVAL
6044 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
6045 # endif
6047 else
6049 if (*wp->w_p_stl != NUL)
6050 p = wp->w_p_stl;
6051 else
6052 p = p_stl;
6053 # ifdef FEAT_EVAL
6054 use_sandbox = was_set_insecurely((char_u *)"statusline",
6055 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
6056 # endif
6059 #ifdef FEAT_VERTSPLIT
6060 col += W_WINCOL(wp);
6061 #endif
6064 if (maxwidth <= 0)
6065 return;
6067 width = build_stl_str_hl(wp == NULL ? curwin : wp,
6068 buf, sizeof(buf),
6069 p, use_sandbox,
6070 fillchar, maxwidth, hltab, tabtab);
6071 len = (int)STRLEN(buf);
6073 while (width < maxwidth && len < sizeof(buf) - 1)
6075 #ifdef FEAT_MBYTE
6076 len += (*mb_char2bytes)(fillchar, buf + len);
6077 #else
6078 buf[len++] = fillchar;
6079 #endif
6080 ++width;
6082 buf[len] = NUL;
6085 * Draw each snippet with the specified highlighting.
6087 curattr = attr;
6088 p = buf;
6089 for (n = 0; hltab[n].start != NULL; n++)
6091 len = (int)(hltab[n].start - p);
6092 screen_puts_len(p, len, row, col, curattr);
6093 col += vim_strnsize(p, len);
6094 p = hltab[n].start;
6096 if (hltab[n].userhl == 0)
6097 curattr = attr;
6098 else if (hltab[n].userhl < 0)
6099 curattr = syn_id2attr(-hltab[n].userhl);
6100 #ifdef FEAT_WINDOWS
6101 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
6102 curattr = highlight_stlnc[hltab[n].userhl - 1];
6103 #endif
6104 else
6105 curattr = highlight_user[hltab[n].userhl - 1];
6107 screen_puts(p, row, col, curattr);
6109 if (wp == NULL)
6111 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
6112 col = 0;
6113 len = 0;
6114 p = buf;
6115 fillchar = 0;
6116 for (n = 0; tabtab[n].start != NULL; n++)
6118 len += vim_strnsize(p, (int)(tabtab[n].start - p));
6119 while (col < len)
6120 TabPageIdxs[col++] = fillchar;
6121 p = tabtab[n].start;
6122 fillchar = tabtab[n].userhl;
6124 while (col < Columns)
6125 TabPageIdxs[col++] = fillchar;
6129 #endif /* FEAT_STL_OPT */
6132 * Output a single character directly to the screen and update ScreenLines.
6134 void
6135 screen_putchar(c, row, col, attr)
6136 int c;
6137 int row, col;
6138 int attr;
6140 #ifdef FEAT_MBYTE
6141 char_u buf[MB_MAXBYTES + 1];
6143 buf[(*mb_char2bytes)(c, buf)] = NUL;
6144 #else
6145 char_u buf[2];
6147 buf[0] = c;
6148 buf[1] = NUL;
6149 #endif
6150 screen_puts(buf, row, col, attr);
6154 * Get a single character directly from ScreenLines into "bytes[]".
6155 * Also return its attribute in *attrp;
6157 void
6158 screen_getbytes(row, col, bytes, attrp)
6159 int row, col;
6160 char_u *bytes;
6161 int *attrp;
6163 unsigned off;
6165 /* safety check */
6166 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
6168 off = LineOffset[row] + col;
6169 *attrp = ScreenAttrs[off];
6170 bytes[0] = ScreenLines[off];
6171 bytes[1] = NUL;
6173 #ifdef FEAT_MBYTE
6174 if (enc_utf8 && ScreenLinesUC[off] != 0)
6175 bytes[utfc_char2bytes(off, bytes)] = NUL;
6176 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6178 bytes[0] = ScreenLines[off];
6179 bytes[1] = ScreenLines2[off];
6180 bytes[2] = NUL;
6182 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
6184 bytes[1] = ScreenLines[off + 1];
6185 bytes[2] = NUL;
6187 #endif
6191 #ifdef FEAT_MBYTE
6192 static int screen_comp_differs __ARGS((int, int*));
6195 * Return TRUE if composing characters for screen posn "off" differs from
6196 * composing characters in "u8cc".
6198 static int
6199 screen_comp_differs(off, u8cc)
6200 int off;
6201 int *u8cc;
6203 int i;
6205 for (i = 0; i < Screen_mco; ++i)
6207 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
6208 return TRUE;
6209 if (u8cc[i] == 0)
6210 break;
6212 return FALSE;
6214 #endif
6217 * Put string '*text' on the screen at position 'row' and 'col', with
6218 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
6219 * Note: only outputs within one row, message is truncated at screen boundary!
6220 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
6222 void
6223 screen_puts(text, row, col, attr)
6224 char_u *text;
6225 int row;
6226 int col;
6227 int attr;
6229 screen_puts_len(text, -1, row, col, attr);
6233 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
6234 * a NUL.
6236 void
6237 screen_puts_len(text, len, row, col, attr)
6238 char_u *text;
6239 int len;
6240 int row;
6241 int col;
6242 int attr;
6244 unsigned off;
6245 char_u *ptr = text;
6246 int c;
6247 #ifdef FEAT_MBYTE
6248 unsigned max_off;
6249 int mbyte_blen = 1;
6250 int mbyte_cells = 1;
6251 int u8c = 0;
6252 int u8cc[MAX_MCO];
6253 int clear_next_cell = FALSE;
6254 # ifdef FEAT_ARABIC
6255 int prev_c = 0; /* previous Arabic character */
6256 int pc, nc, nc1;
6257 int pcc[MAX_MCO];
6258 # endif
6259 #endif
6261 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
6262 return;
6264 off = LineOffset[row] + col;
6265 #ifdef FEAT_MBYTE
6266 max_off = LineOffset[row] + screen_Columns;
6267 #endif
6268 while (col < screen_Columns
6269 && (len < 0 || (int)(ptr - text) < len)
6270 && *ptr != NUL)
6272 c = *ptr;
6273 #ifdef FEAT_MBYTE
6274 /* check if this is the first byte of a multibyte */
6275 if (has_mbyte)
6277 if (enc_utf8 && len > 0)
6278 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
6279 else
6280 mbyte_blen = (*mb_ptr2len)(ptr);
6281 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6282 mbyte_cells = 1;
6283 else if (enc_dbcs != 0)
6284 mbyte_cells = mbyte_blen;
6285 else /* enc_utf8 */
6287 if (len >= 0)
6288 u8c = utfc_ptr2char_len(ptr, u8cc,
6289 (int)((text + len) - ptr));
6290 else
6291 u8c = utfc_ptr2char(ptr, u8cc);
6292 mbyte_cells = utf_char2cells(u8c);
6293 # ifdef UNICODE16
6294 /* Non-BMP character: display as ? or fullwidth ?. */
6295 if (u8c >= 0x10000)
6297 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
6298 if (attr == 0)
6299 attr = hl_attr(HLF_8);
6301 # endif
6302 # ifdef FEAT_ARABIC
6303 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
6305 /* Do Arabic shaping. */
6306 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
6308 /* Past end of string to be displayed. */
6309 nc = NUL;
6310 nc1 = NUL;
6312 else
6314 nc = utfc_ptr2char(ptr + mbyte_blen, pcc);
6315 nc1 = pcc[0];
6317 pc = prev_c;
6318 prev_c = u8c;
6319 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
6321 else
6322 prev_c = u8c;
6323 # endif
6326 #endif
6328 if (ScreenLines[off] != c
6329 #ifdef FEAT_MBYTE
6330 || (mbyte_cells == 2
6331 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
6332 || (enc_dbcs == DBCS_JPNU
6333 && c == 0x8e
6334 && ScreenLines2[off] != ptr[1])
6335 || (enc_utf8
6336 && (ScreenLinesUC[off] != (u8char_T)u8c
6337 || screen_comp_differs(off, u8cc)))
6338 #endif
6339 || ScreenAttrs[off] != attr
6340 || exmode_active
6343 #if defined(FEAT_GUI) || defined(UNIX)
6344 /* The bold trick makes a single row of pixels appear in the next
6345 * character. When a bold character is removed, the next
6346 * character should be redrawn too. This happens for our own GUI
6347 * and for some xterms.
6348 * Force the redraw by setting the attribute to a different value
6349 * than "attr", the contents of ScreenLines[] may be needed by
6350 * mb_off2cells() further on.
6351 * Don't do this for the last drawn character, because the next
6352 * character may not be redrawn. */
6353 if (
6354 # ifdef FEAT_GUI
6355 gui.in_use
6356 # endif
6357 # if defined(FEAT_GUI) && defined(UNIX)
6359 # endif
6360 # ifdef UNIX
6361 term_is_xterm
6362 # endif
6365 int n;
6367 n = ScreenAttrs[off];
6368 # ifdef FEAT_MBYTE
6369 if (col + mbyte_cells < screen_Columns
6370 && (n > HL_ALL || (n & HL_BOLD))
6371 && (len < 0 ? ptr[mbyte_blen] != NUL
6372 : ptr + mbyte_blen < text + len))
6373 ScreenAttrs[off + mbyte_cells] = attr + 1;
6374 # else
6375 if (col + 1 < screen_Columns
6376 && (n > HL_ALL || (n & HL_BOLD))
6377 && (len < 0 ? ptr[1] != NUL : ptr + 1 < text + len))
6378 ScreenLines[off + 1] = 0;
6379 # endif
6381 #endif
6382 #ifdef FEAT_MBYTE
6383 /* When at the end of the text and overwriting a two-cell
6384 * character with a one-cell character, need to clear the next
6385 * cell. Also when overwriting the left halve of a two-cell char
6386 * with the right halve of a two-cell char. Do this only once
6387 * (mb_off2cells() may return 2 on the right halve). */
6388 if (clear_next_cell)
6389 clear_next_cell = FALSE;
6390 else if (has_mbyte
6391 && (len < 0 ? ptr[mbyte_blen] == NUL
6392 : ptr + mbyte_blen >= text + len)
6393 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
6394 || (mbyte_cells == 2
6395 && (*mb_off2cells)(off, max_off) == 1
6396 && (*mb_off2cells)(off + 1, max_off) > 1)))
6397 clear_next_cell = TRUE;
6399 /* Make sure we never leave a second byte of a double-byte behind,
6400 * it confuses mb_off2cells(). */
6401 if (enc_dbcs
6402 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
6403 || (mbyte_cells == 2
6404 && (*mb_off2cells)(off, max_off) == 1
6405 && (*mb_off2cells)(off + 1, max_off) > 1)))
6406 ScreenLines[off + mbyte_blen] = 0;
6407 #endif
6408 ScreenLines[off] = c;
6409 ScreenAttrs[off] = attr;
6410 #ifdef FEAT_MBYTE
6411 if (enc_utf8)
6413 if (c < 0x80 && u8cc[0] == 0)
6414 ScreenLinesUC[off] = 0;
6415 else
6417 int i;
6419 ScreenLinesUC[off] = u8c;
6420 for (i = 0; i < Screen_mco; ++i)
6422 ScreenLinesC[i][off] = u8cc[i];
6423 if (u8cc[i] == 0)
6424 break;
6427 if (mbyte_cells == 2)
6429 ScreenLines[off + 1] = 0;
6430 ScreenAttrs[off + 1] = attr;
6432 screen_char(off, row, col);
6434 else if (mbyte_cells == 2)
6436 ScreenLines[off + 1] = ptr[1];
6437 ScreenAttrs[off + 1] = attr;
6438 screen_char_2(off, row, col);
6440 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6442 ScreenLines2[off] = ptr[1];
6443 screen_char(off, row, col);
6445 else
6446 #endif
6447 screen_char(off, row, col);
6449 #ifdef FEAT_MBYTE
6450 if (has_mbyte)
6452 off += mbyte_cells;
6453 col += mbyte_cells;
6454 ptr += mbyte_blen;
6455 if (clear_next_cell)
6456 ptr = (char_u *)" ";
6458 else
6459 #endif
6461 ++off;
6462 ++col;
6463 ++ptr;
6468 #ifdef FEAT_SEARCH_EXTRA
6470 * Prepare for 'hlsearch' highlighting.
6472 static void
6473 start_search_hl()
6475 if (p_hls && !no_hlsearch)
6477 last_pat_prog(&search_hl.rm);
6478 search_hl.attr = hl_attr(HLF_L);
6479 # ifdef FEAT_RELTIME
6480 /* Set the time limit to 'redrawtime'. */
6481 profile_setlimit(p_rdt, &search_hl.tm);
6482 # endif
6487 * Clean up for 'hlsearch' highlighting.
6489 static void
6490 end_search_hl()
6492 if (search_hl.rm.regprog != NULL)
6494 vim_free(search_hl.rm.regprog);
6495 search_hl.rm.regprog = NULL;
6500 * Advance to the match in window "wp" line "lnum" or past it.
6502 static void
6503 prepare_search_hl(wp, lnum)
6504 win_T *wp;
6505 linenr_T lnum;
6507 matchitem_T *cur; /* points to the match list */
6508 match_T *shl; /* points to search_hl or a match */
6509 int shl_flag; /* flag to indicate whether search_hl
6510 has been processed or not */
6511 int n;
6514 * When using a multi-line pattern, start searching at the top
6515 * of the window or just after a closed fold.
6516 * Do this both for search_hl and the match list.
6518 cur = wp->w_match_head;
6519 shl_flag = FALSE;
6520 while (cur != NULL || shl_flag == FALSE)
6522 if (shl_flag == FALSE)
6524 shl = &search_hl;
6525 shl_flag = TRUE;
6527 else
6528 shl = &cur->hl;
6529 if (shl->rm.regprog != NULL
6530 && shl->lnum == 0
6531 && re_multiline(shl->rm.regprog))
6533 if (shl->first_lnum == 0)
6535 # ifdef FEAT_FOLDING
6536 for (shl->first_lnum = lnum;
6537 shl->first_lnum > wp->w_topline; --shl->first_lnum)
6538 if (hasFoldingWin(wp, shl->first_lnum - 1,
6539 NULL, NULL, TRUE, NULL))
6540 break;
6541 # else
6542 shl->first_lnum = wp->w_topline;
6543 # endif
6545 n = 0;
6546 while (shl->first_lnum < lnum && shl->rm.regprog != NULL)
6548 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n);
6549 if (shl->lnum != 0)
6551 shl->first_lnum = shl->lnum
6552 + shl->rm.endpos[0].lnum
6553 - shl->rm.startpos[0].lnum;
6554 n = shl->rm.endpos[0].col;
6556 else
6558 ++shl->first_lnum;
6559 n = 0;
6563 if (shl != &search_hl && cur != NULL)
6564 cur = cur->next;
6569 * Search for a next 'hlsearch' or match.
6570 * Uses shl->buf.
6571 * Sets shl->lnum and shl->rm contents.
6572 * Note: Assumes a previous match is always before "lnum", unless
6573 * shl->lnum is zero.
6574 * Careful: Any pointers for buffer lines will become invalid.
6576 static void
6577 next_search_hl(win, shl, lnum, mincol)
6578 win_T *win;
6579 match_T *shl; /* points to search_hl or a match */
6580 linenr_T lnum;
6581 colnr_T mincol; /* minimal column for a match */
6583 linenr_T l;
6584 colnr_T matchcol;
6585 long nmatched;
6587 if (shl->lnum != 0)
6589 /* Check for three situations:
6590 * 1. If the "lnum" is below a previous match, start a new search.
6591 * 2. If the previous match includes "mincol", use it.
6592 * 3. Continue after the previous match.
6594 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
6595 if (lnum > l)
6596 shl->lnum = 0;
6597 else if (lnum < l || shl->rm.endpos[0].col > mincol)
6598 return;
6602 * Repeat searching for a match until one is found that includes "mincol"
6603 * or none is found in this line.
6605 called_emsg = FALSE;
6606 for (;;)
6608 #ifdef FEAT_RELTIME
6609 /* Stop searching after passing the time limit. */
6610 if (profile_passed_limit(&(shl->tm)))
6612 shl->lnum = 0; /* no match found in time */
6613 break;
6615 #endif
6616 /* Three situations:
6617 * 1. No useful previous match: search from start of line.
6618 * 2. Not Vi compatible or empty match: continue at next character.
6619 * Break the loop if this is beyond the end of the line.
6620 * 3. Vi compatible searching: continue at end of previous match.
6622 if (shl->lnum == 0)
6623 matchcol = 0;
6624 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
6625 || (shl->rm.endpos[0].lnum == 0
6626 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
6628 char_u *ml;
6630 matchcol = shl->rm.startpos[0].col;
6631 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
6632 if (*ml == NUL)
6634 ++matchcol;
6635 shl->lnum = 0;
6636 break;
6638 #ifdef FEAT_MBYTE
6639 if (has_mbyte)
6640 matchcol += mb_ptr2len(ml);
6641 else
6642 #endif
6643 ++matchcol;
6645 else
6646 matchcol = shl->rm.endpos[0].col;
6648 shl->lnum = lnum;
6649 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum, matchcol,
6650 #ifdef FEAT_RELTIME
6651 &(shl->tm)
6652 #else
6653 NULL
6654 #endif
6656 if (called_emsg)
6658 /* Error while handling regexp: stop using this regexp. */
6659 if (shl == &search_hl)
6661 /* don't free regprog in the match list, it's a copy */
6662 vim_free(shl->rm.regprog);
6663 no_hlsearch = TRUE;
6665 shl->rm.regprog = NULL;
6666 shl->lnum = 0;
6667 got_int = FALSE; /* avoid the "Type :quit to exit Vim" message */
6668 break;
6670 if (nmatched == 0)
6672 shl->lnum = 0; /* no match found */
6673 break;
6675 if (shl->rm.startpos[0].lnum > 0
6676 || shl->rm.startpos[0].col >= mincol
6677 || nmatched > 1
6678 || shl->rm.endpos[0].col > mincol)
6680 shl->lnum += shl->rm.startpos[0].lnum;
6681 break; /* useful match found */
6685 #endif
6687 static void
6688 screen_start_highlight(attr)
6689 int attr;
6691 attrentry_T *aep = NULL;
6693 screen_attr = attr;
6694 if (full_screen
6695 #ifdef WIN3264
6696 && termcap_active
6697 #endif
6700 #ifdef FEAT_GUI
6701 if (gui.in_use)
6703 char buf[20];
6705 /* The GUI handles this internally. */
6706 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
6707 OUT_STR(buf);
6709 else
6710 #endif
6712 if (attr > HL_ALL) /* special HL attr. */
6714 if (t_colors > 1)
6715 aep = syn_cterm_attr2entry(attr);
6716 else
6717 aep = syn_term_attr2entry(attr);
6718 if (aep == NULL) /* did ":syntax clear" */
6719 attr = 0;
6720 else
6721 attr = aep->ae_attr;
6723 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
6724 out_str(T_MD);
6725 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
6726 && cterm_normal_fg_bold)
6727 /* If the Normal FG color has BOLD attribute and the new HL
6728 * has a FG color defined, clear BOLD. */
6729 out_str(T_ME);
6730 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
6731 out_str(T_SO);
6732 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
6733 /* underline or undercurl */
6734 out_str(T_US);
6735 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
6736 out_str(T_CZH);
6737 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
6738 out_str(T_MR);
6741 * Output the color or start string after bold etc., in case the
6742 * bold etc. override the color setting.
6744 if (aep != NULL)
6746 if (t_colors > 1)
6748 if (aep->ae_u.cterm.fg_color)
6749 term_fg_color(aep->ae_u.cterm.fg_color - 1);
6750 if (aep->ae_u.cterm.bg_color)
6751 term_bg_color(aep->ae_u.cterm.bg_color - 1);
6753 else
6755 if (aep->ae_u.term.start != NULL)
6756 out_str(aep->ae_u.term.start);
6763 void
6764 screen_stop_highlight()
6766 int do_ME = FALSE; /* output T_ME code */
6768 if (screen_attr != 0
6769 #ifdef WIN3264
6770 && termcap_active
6771 #endif
6774 #ifdef FEAT_GUI
6775 if (gui.in_use)
6777 char buf[20];
6779 /* use internal GUI code */
6780 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
6781 OUT_STR(buf);
6783 else
6784 #endif
6786 if (screen_attr > HL_ALL) /* special HL attr. */
6788 attrentry_T *aep;
6790 if (t_colors > 1)
6793 * Assume that t_me restores the original colors!
6795 aep = syn_cterm_attr2entry(screen_attr);
6796 if (aep != NULL && (aep->ae_u.cterm.fg_color
6797 || aep->ae_u.cterm.bg_color))
6798 do_ME = TRUE;
6800 else
6802 aep = syn_term_attr2entry(screen_attr);
6803 if (aep != NULL && aep->ae_u.term.stop != NULL)
6805 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
6806 do_ME = TRUE;
6807 else
6808 out_str(aep->ae_u.term.stop);
6811 if (aep == NULL) /* did ":syntax clear" */
6812 screen_attr = 0;
6813 else
6814 screen_attr = aep->ae_attr;
6818 * Often all ending-codes are equal to T_ME. Avoid outputting the
6819 * same sequence several times.
6821 if (screen_attr & HL_STANDOUT)
6823 if (STRCMP(T_SE, T_ME) == 0)
6824 do_ME = TRUE;
6825 else
6826 out_str(T_SE);
6828 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
6830 if (STRCMP(T_UE, T_ME) == 0)
6831 do_ME = TRUE;
6832 else
6833 out_str(T_UE);
6835 if (screen_attr & HL_ITALIC)
6837 if (STRCMP(T_CZR, T_ME) == 0)
6838 do_ME = TRUE;
6839 else
6840 out_str(T_CZR);
6842 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
6843 out_str(T_ME);
6845 if (t_colors > 1)
6847 /* set Normal cterm colors */
6848 if (cterm_normal_fg_color != 0)
6849 term_fg_color(cterm_normal_fg_color - 1);
6850 if (cterm_normal_bg_color != 0)
6851 term_bg_color(cterm_normal_bg_color - 1);
6852 if (cterm_normal_fg_bold)
6853 out_str(T_MD);
6857 screen_attr = 0;
6861 * Reset the colors for a cterm. Used when leaving Vim.
6862 * The machine specific code may override this again.
6864 void
6865 reset_cterm_colors()
6867 if (t_colors > 1)
6869 /* set Normal cterm colors */
6870 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
6872 out_str(T_OP);
6873 screen_attr = -1;
6875 if (cterm_normal_fg_bold)
6877 out_str(T_ME);
6878 screen_attr = -1;
6884 * Put character ScreenLines["off"] on the screen at position "row" and "col",
6885 * using the attributes from ScreenAttrs["off"].
6887 static void
6888 screen_char(off, row, col)
6889 unsigned off;
6890 int row;
6891 int col;
6893 int attr;
6895 /* Check for illegal values, just in case (could happen just after
6896 * resizing). */
6897 if (row >= screen_Rows || col >= screen_Columns)
6898 return;
6900 /* Outputting the last character on the screen may scrollup the screen.
6901 * Don't to it! Mark the character invalid (update it when scrolled up) */
6902 if (row == screen_Rows - 1 && col == screen_Columns - 1
6903 #ifdef FEAT_RIGHTLEFT
6904 /* account for first command-line character in rightleft mode */
6905 && !cmdmsg_rl
6906 #endif
6909 ScreenAttrs[off] = (sattr_T)-1;
6910 return;
6914 * Stop highlighting first, so it's easier to move the cursor.
6916 #if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
6917 if (screen_char_attr != 0)
6918 attr = screen_char_attr;
6919 else
6920 #endif
6921 attr = ScreenAttrs[off];
6922 if (screen_attr != attr)
6923 screen_stop_highlight();
6925 windgoto(row, col);
6927 if (screen_attr != attr)
6928 screen_start_highlight(attr);
6930 #ifdef FEAT_MBYTE
6931 if (enc_utf8 && ScreenLinesUC[off] != 0)
6933 char_u buf[MB_MAXBYTES + 1];
6935 /* Convert UTF-8 character to bytes and write it. */
6937 buf[utfc_char2bytes(off, buf)] = NUL;
6939 out_str(buf);
6940 if (utf_char2cells(ScreenLinesUC[off]) > 1)
6941 ++screen_cur_col;
6943 else
6944 #endif
6946 #ifdef FEAT_MBYTE
6947 out_flush_check();
6948 #endif
6949 out_char(ScreenLines[off]);
6950 #ifdef FEAT_MBYTE
6951 /* double-byte character in single-width cell */
6952 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6953 out_char(ScreenLines2[off]);
6954 #endif
6957 screen_cur_col++;
6960 #ifdef FEAT_MBYTE
6963 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
6964 * on the screen at position 'row' and 'col'.
6965 * The attributes of the first byte is used for all. This is required to
6966 * output the two bytes of a double-byte character with nothing in between.
6968 static void
6969 screen_char_2(off, row, col)
6970 unsigned off;
6971 int row;
6972 int col;
6974 /* Check for illegal values (could be wrong when screen was resized). */
6975 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
6976 return;
6978 /* Outputting the last character on the screen may scrollup the screen.
6979 * Don't to it! Mark the character invalid (update it when scrolled up) */
6980 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
6982 ScreenAttrs[off] = (sattr_T)-1;
6983 return;
6986 /* Output the first byte normally (positions the cursor), then write the
6987 * second byte directly. */
6988 screen_char(off, row, col);
6989 out_char(ScreenLines[off + 1]);
6990 ++screen_cur_col;
6992 #endif
6994 #if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
6996 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
6997 * This uses the contents of ScreenLines[] and doesn't change it.
6999 void
7000 screen_draw_rectangle(row, col, height, width, invert)
7001 int row;
7002 int col;
7003 int height;
7004 int width;
7005 int invert;
7007 int r, c;
7008 int off;
7009 #ifdef FEAT_MBYTE
7010 int max_off;
7011 #endif
7013 /* Can't use ScreenLines unless initialized */
7014 if (ScreenLines == NULL)
7015 return;
7017 if (invert)
7018 screen_char_attr = HL_INVERSE;
7019 for (r = row; r < row + height; ++r)
7021 off = LineOffset[r];
7022 #ifdef FEAT_MBYTE
7023 max_off = off + screen_Columns;
7024 #endif
7025 for (c = col; c < col + width; ++c)
7027 #ifdef FEAT_MBYTE
7028 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
7030 screen_char_2(off + c, r, c);
7031 ++c;
7033 else
7034 #endif
7036 screen_char(off + c, r, c);
7037 #ifdef FEAT_MBYTE
7038 if (utf_off2cells(off + c, max_off) > 1)
7039 ++c;
7040 #endif
7044 screen_char_attr = 0;
7046 #endif
7048 #ifdef FEAT_VERTSPLIT
7050 * Redraw the characters for a vertically split window.
7052 static void
7053 redraw_block(row, end, wp)
7054 int row;
7055 int end;
7056 win_T *wp;
7058 int col;
7059 int width;
7061 # ifdef FEAT_CLIPBOARD
7062 clip_may_clear_selection(row, end - 1);
7063 # endif
7065 if (wp == NULL)
7067 col = 0;
7068 width = Columns;
7070 else
7072 col = wp->w_wincol;
7073 width = wp->w_width;
7075 screen_draw_rectangle(row, col, end - row, width, FALSE);
7077 #endif
7080 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
7081 * with character 'c1' in first column followed by 'c2' in the other columns.
7082 * Use attributes 'attr'.
7084 void
7085 screen_fill(start_row, end_row, start_col, end_col, c1, c2, attr)
7086 int start_row, end_row;
7087 int start_col, end_col;
7088 int c1, c2;
7089 int attr;
7091 int row;
7092 int col;
7093 int off;
7094 int end_off;
7095 int did_delete;
7096 int c;
7097 int norm_term;
7098 #if defined(FEAT_GUI) || defined(UNIX)
7099 int force_next = FALSE;
7100 #endif
7102 if (end_row > screen_Rows) /* safety check */
7103 end_row = screen_Rows;
7104 if (end_col > screen_Columns) /* safety check */
7105 end_col = screen_Columns;
7106 if (ScreenLines == NULL
7107 || start_row >= end_row
7108 || start_col >= end_col) /* nothing to do */
7109 return;
7111 /* it's a "normal" terminal when not in a GUI or cterm */
7112 norm_term = (
7113 #ifdef FEAT_GUI
7114 !gui.in_use &&
7115 #endif
7116 t_colors <= 1);
7117 for (row = start_row; row < end_row; ++row)
7120 * Try to use delete-line termcap code, when no attributes or in a
7121 * "normal" terminal, where a bold/italic space is just a
7122 * space.
7124 did_delete = FALSE;
7125 if (c2 == ' '
7126 && end_col == Columns
7127 && can_clear(T_CE)
7128 && (attr == 0
7129 || (norm_term
7130 && attr <= HL_ALL
7131 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
7134 * check if we really need to clear something
7136 col = start_col;
7137 if (c1 != ' ') /* don't clear first char */
7138 ++col;
7140 off = LineOffset[row] + col;
7141 end_off = LineOffset[row] + end_col;
7143 /* skip blanks (used often, keep it fast!) */
7144 #ifdef FEAT_MBYTE
7145 if (enc_utf8)
7146 while (off < end_off && ScreenLines[off] == ' '
7147 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
7148 ++off;
7149 else
7150 #endif
7151 while (off < end_off && ScreenLines[off] == ' '
7152 && ScreenAttrs[off] == 0)
7153 ++off;
7154 if (off < end_off) /* something to be cleared */
7156 col = off - LineOffset[row];
7157 screen_stop_highlight();
7158 term_windgoto(row, col);/* clear rest of this screen line */
7159 out_str(T_CE);
7160 screen_start(); /* don't know where cursor is now */
7161 col = end_col - col;
7162 while (col--) /* clear chars in ScreenLines */
7164 ScreenLines[off] = ' ';
7165 #ifdef FEAT_MBYTE
7166 if (enc_utf8)
7167 ScreenLinesUC[off] = 0;
7168 #endif
7169 ScreenAttrs[off] = 0;
7170 ++off;
7173 did_delete = TRUE; /* the chars are cleared now */
7176 off = LineOffset[row] + start_col;
7177 c = c1;
7178 for (col = start_col; col < end_col; ++col)
7180 if (ScreenLines[off] != c
7181 #ifdef FEAT_MBYTE
7182 || (enc_utf8 && (int)ScreenLinesUC[off]
7183 != (c >= 0x80 ? c : 0))
7184 #endif
7185 || ScreenAttrs[off] != attr
7186 #if defined(FEAT_GUI) || defined(UNIX)
7187 || force_next
7188 #endif
7191 #if defined(FEAT_GUI) || defined(UNIX)
7192 /* The bold trick may make a single row of pixels appear in
7193 * the next character. When a bold character is removed, the
7194 * next character should be redrawn too. This happens for our
7195 * own GUI and for some xterms. */
7196 if (
7197 # ifdef FEAT_GUI
7198 gui.in_use
7199 # endif
7200 # if defined(FEAT_GUI) && defined(UNIX)
7202 # endif
7203 # ifdef UNIX
7204 term_is_xterm
7205 # endif
7208 if (ScreenLines[off] != ' '
7209 && (ScreenAttrs[off] > HL_ALL
7210 || ScreenAttrs[off] & HL_BOLD))
7211 force_next = TRUE;
7212 else
7213 force_next = FALSE;
7215 #endif
7216 ScreenLines[off] = c;
7217 #ifdef FEAT_MBYTE
7218 if (enc_utf8)
7220 if (c >= 0x80)
7222 ScreenLinesUC[off] = c;
7223 ScreenLinesC[0][off] = 0;
7225 else
7226 ScreenLinesUC[off] = 0;
7228 #endif
7229 ScreenAttrs[off] = attr;
7230 if (!did_delete || c != ' ')
7231 screen_char(off, row, col);
7233 ++off;
7234 if (col == start_col)
7236 if (did_delete)
7237 break;
7238 c = c2;
7241 if (end_col == Columns)
7242 LineWraps[row] = FALSE;
7243 if (row == Rows - 1) /* overwritten the command line */
7245 redraw_cmdline = TRUE;
7246 if (c1 == ' ' && c2 == ' ')
7247 clear_cmdline = FALSE; /* command line has been cleared */
7248 if (start_col == 0)
7249 mode_displayed = FALSE; /* mode cleared or overwritten */
7255 * Check if there should be a delay. Used before clearing or redrawing the
7256 * screen or the command line.
7258 void
7259 check_for_delay(check_msg_scroll)
7260 int check_msg_scroll;
7262 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
7263 && !did_wait_return
7264 && emsg_silent == 0)
7266 out_flush();
7267 ui_delay(1000L, TRUE);
7268 emsg_on_display = FALSE;
7269 if (check_msg_scroll)
7270 msg_scroll = FALSE;
7275 * screen_valid - allocate screen buffers if size changed
7276 * If "clear" is TRUE: clear screen if it has been resized.
7277 * Returns TRUE if there is a valid screen to write to.
7278 * Returns FALSE when starting up and screen not initialized yet.
7281 screen_valid(clear)
7282 int clear;
7284 screenalloc(clear); /* allocate screen buffers if size changed */
7285 return (ScreenLines != NULL);
7289 * Resize the shell to Rows and Columns.
7290 * Allocate ScreenLines[] and associated items.
7292 * There may be some time between setting Rows and Columns and (re)allocating
7293 * ScreenLines[]. This happens when starting up and when (manually) changing
7294 * the shell size. Always use screen_Rows and screen_Columns to access items
7295 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
7296 * final size of the shell is needed.
7298 void
7299 screenalloc(clear)
7300 int clear;
7302 int new_row, old_row;
7303 #ifdef FEAT_GUI
7304 int old_Rows;
7305 #endif
7306 win_T *wp;
7307 int outofmem = FALSE;
7308 int len;
7309 schar_T *new_ScreenLines;
7310 #ifdef FEAT_MBYTE
7311 u8char_T *new_ScreenLinesUC = NULL;
7312 u8char_T *new_ScreenLinesC[MAX_MCO];
7313 schar_T *new_ScreenLines2 = NULL;
7314 int i;
7315 #endif
7316 sattr_T *new_ScreenAttrs;
7317 unsigned *new_LineOffset;
7318 char_u *new_LineWraps;
7319 #ifdef FEAT_WINDOWS
7320 short *new_TabPageIdxs;
7321 tabpage_T *tp;
7322 #endif
7323 static int entered = FALSE; /* avoid recursiveness */
7324 static int done_outofmem_msg = FALSE; /* did outofmem message */
7327 * Allocation of the screen buffers is done only when the size changes and
7328 * when Rows and Columns have been set and we have started doing full
7329 * screen stuff.
7331 if ((ScreenLines != NULL
7332 && Rows == screen_Rows
7333 && Columns == screen_Columns
7334 #ifdef FEAT_MBYTE
7335 && enc_utf8 == (ScreenLinesUC != NULL)
7336 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
7337 && p_mco == Screen_mco
7338 #endif
7340 || Rows == 0
7341 || Columns == 0
7342 || (!full_screen && ScreenLines == NULL))
7343 return;
7346 * It's possible that we produce an out-of-memory message below, which
7347 * will cause this function to be called again. To break the loop, just
7348 * return here.
7350 if (entered)
7351 return;
7352 entered = TRUE;
7355 * Note that the window sizes are updated before reallocating the arrays,
7356 * thus we must not redraw here!
7358 ++RedrawingDisabled;
7360 win_new_shellsize(); /* fit the windows in the new sized shell */
7362 comp_col(); /* recompute columns for shown command and ruler */
7365 * We're changing the size of the screen.
7366 * - Allocate new arrays for ScreenLines and ScreenAttrs.
7367 * - Move lines from the old arrays into the new arrays, clear extra
7368 * lines (unless the screen is going to be cleared).
7369 * - Free the old arrays.
7371 * If anything fails, make ScreenLines NULL, so we don't do anything!
7372 * Continuing with the old ScreenLines may result in a crash, because the
7373 * size is wrong.
7375 FOR_ALL_TAB_WINDOWS(tp, wp)
7376 win_free_lsize(wp);
7378 new_ScreenLines = (schar_T *)lalloc((long_u)(
7379 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7380 #ifdef FEAT_MBYTE
7381 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T) * MAX_MCO);
7382 if (enc_utf8)
7384 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
7385 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
7386 for (i = 0; i < p_mco; ++i)
7387 new_ScreenLinesC[i] = (u8char_T *)lalloc((long_u)(
7388 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
7390 if (enc_dbcs == DBCS_JPNU)
7391 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
7392 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7393 #endif
7394 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
7395 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
7396 new_LineOffset = (unsigned *)lalloc((long_u)(
7397 Rows * sizeof(unsigned)), FALSE);
7398 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
7399 #ifdef FEAT_WINDOWS
7400 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
7401 #endif
7403 FOR_ALL_TAB_WINDOWS(tp, wp)
7405 if (win_alloc_lines(wp) == FAIL)
7407 outofmem = TRUE;
7408 #ifdef FEAT_WINDOWS
7409 break;
7410 #endif
7414 #ifdef FEAT_MBYTE
7415 for (i = 0; i < p_mco; ++i)
7416 if (new_ScreenLinesC[i] == NULL)
7417 break;
7418 #endif
7419 if (new_ScreenLines == NULL
7420 #ifdef FEAT_MBYTE
7421 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
7422 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
7423 #endif
7424 || new_ScreenAttrs == NULL
7425 || new_LineOffset == NULL
7426 || new_LineWraps == NULL
7427 #ifdef FEAT_WINDOWS
7428 || new_TabPageIdxs == NULL
7429 #endif
7430 || outofmem)
7432 if (ScreenLines != NULL || !done_outofmem_msg)
7434 /* guess the size */
7435 do_outofmem_msg((long_u)((Rows + 1) * Columns));
7437 /* Remember we did this to avoid getting outofmem messages over
7438 * and over again. */
7439 done_outofmem_msg = TRUE;
7441 vim_free(new_ScreenLines);
7442 new_ScreenLines = NULL;
7443 #ifdef FEAT_MBYTE
7444 vim_free(new_ScreenLinesUC);
7445 new_ScreenLinesUC = NULL;
7446 for (i = 0; i < p_mco; ++i)
7448 vim_free(new_ScreenLinesC[i]);
7449 new_ScreenLinesC[i] = NULL;
7451 vim_free(new_ScreenLines2);
7452 new_ScreenLines2 = NULL;
7453 #endif
7454 vim_free(new_ScreenAttrs);
7455 new_ScreenAttrs = NULL;
7456 vim_free(new_LineOffset);
7457 new_LineOffset = NULL;
7458 vim_free(new_LineWraps);
7459 new_LineWraps = NULL;
7460 #ifdef FEAT_WINDOWS
7461 vim_free(new_TabPageIdxs);
7462 new_TabPageIdxs = NULL;
7463 #endif
7465 else
7467 done_outofmem_msg = FALSE;
7469 for (new_row = 0; new_row < Rows; ++new_row)
7471 new_LineOffset[new_row] = new_row * Columns;
7472 new_LineWraps[new_row] = FALSE;
7475 * If the screen is not going to be cleared, copy as much as
7476 * possible from the old screen to the new one and clear the rest
7477 * (used when resizing the window at the "--more--" prompt or when
7478 * executing an external command, for the GUI).
7480 if (!clear)
7482 (void)vim_memset(new_ScreenLines + new_row * Columns,
7483 ' ', (size_t)Columns * sizeof(schar_T));
7484 #ifdef FEAT_MBYTE
7485 if (enc_utf8)
7487 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
7488 0, (size_t)Columns * sizeof(u8char_T));
7489 for (i = 0; i < p_mco; ++i)
7490 (void)vim_memset(new_ScreenLinesC[i]
7491 + new_row * Columns,
7492 0, (size_t)Columns * sizeof(u8char_T));
7494 if (enc_dbcs == DBCS_JPNU)
7495 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
7496 0, (size_t)Columns * sizeof(schar_T));
7497 #endif
7498 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
7499 0, (size_t)Columns * sizeof(sattr_T));
7500 old_row = new_row + (screen_Rows - Rows);
7501 if (old_row >= 0 && ScreenLines != NULL)
7503 if (screen_Columns < Columns)
7504 len = screen_Columns;
7505 else
7506 len = Columns;
7507 #ifdef FEAT_MBYTE
7508 /* When switching to utf-8 don't copy characters, they
7509 * may be invalid now. Also when p_mco changes. */
7510 if (!(enc_utf8 && ScreenLinesUC == NULL)
7511 && p_mco == Screen_mco)
7512 #endif
7513 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
7514 ScreenLines + LineOffset[old_row],
7515 (size_t)len * sizeof(schar_T));
7516 #ifdef FEAT_MBYTE
7517 if (enc_utf8 && ScreenLinesUC != NULL
7518 && p_mco == Screen_mco)
7520 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
7521 ScreenLinesUC + LineOffset[old_row],
7522 (size_t)len * sizeof(u8char_T));
7523 for (i = 0; i < p_mco; ++i)
7524 mch_memmove(new_ScreenLinesC[i]
7525 + new_LineOffset[new_row],
7526 ScreenLinesC[i] + LineOffset[old_row],
7527 (size_t)len * sizeof(u8char_T));
7529 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
7530 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
7531 ScreenLines2 + LineOffset[old_row],
7532 (size_t)len * sizeof(schar_T));
7533 #endif
7534 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
7535 ScreenAttrs + LineOffset[old_row],
7536 (size_t)len * sizeof(sattr_T));
7540 /* Use the last line of the screen for the current line. */
7541 current_ScreenLine = new_ScreenLines + Rows * Columns;
7544 free_screenlines();
7546 ScreenLines = new_ScreenLines;
7547 #ifdef FEAT_MBYTE
7548 ScreenLinesUC = new_ScreenLinesUC;
7549 for (i = 0; i < p_mco; ++i)
7550 ScreenLinesC[i] = new_ScreenLinesC[i];
7551 Screen_mco = p_mco;
7552 ScreenLines2 = new_ScreenLines2;
7553 #endif
7554 ScreenAttrs = new_ScreenAttrs;
7555 LineOffset = new_LineOffset;
7556 LineWraps = new_LineWraps;
7557 #ifdef FEAT_WINDOWS
7558 TabPageIdxs = new_TabPageIdxs;
7559 #endif
7561 /* It's important that screen_Rows and screen_Columns reflect the actual
7562 * size of ScreenLines[]. Set them before calling anything. */
7563 #ifdef FEAT_GUI
7564 old_Rows = screen_Rows;
7565 #endif
7566 screen_Rows = Rows;
7567 screen_Columns = Columns;
7569 must_redraw = CLEAR; /* need to clear the screen later */
7570 if (clear)
7571 screenclear2();
7573 #ifdef FEAT_GUI
7574 else if (gui.in_use
7575 && !gui.starting
7576 && ScreenLines != NULL
7577 && old_Rows != Rows)
7579 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
7581 * Adjust the position of the cursor, for when executing an external
7582 * command.
7584 if (msg_row >= Rows) /* Rows got smaller */
7585 msg_row = Rows - 1; /* put cursor at last row */
7586 else if (Rows > old_Rows) /* Rows got bigger */
7587 msg_row += Rows - old_Rows; /* put cursor in same place */
7588 if (msg_col >= Columns) /* Columns got smaller */
7589 msg_col = Columns - 1; /* put cursor at last column */
7591 #endif
7593 entered = FALSE;
7594 --RedrawingDisabled;
7596 #ifdef FEAT_AUTOCMD
7597 if (starting == 0)
7598 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
7599 #endif
7602 void
7603 free_screenlines()
7605 #ifdef FEAT_MBYTE
7606 int i;
7608 vim_free(ScreenLinesUC);
7609 for (i = 0; i < Screen_mco; ++i)
7610 vim_free(ScreenLinesC[i]);
7611 vim_free(ScreenLines2);
7612 #endif
7613 vim_free(ScreenLines);
7614 vim_free(ScreenAttrs);
7615 vim_free(LineOffset);
7616 vim_free(LineWraps);
7617 #ifdef FEAT_WINDOWS
7618 vim_free(TabPageIdxs);
7619 #endif
7622 void
7623 screenclear()
7625 check_for_delay(FALSE);
7626 screenalloc(FALSE); /* allocate screen buffers if size changed */
7627 screenclear2(); /* clear the screen */
7630 static void
7631 screenclear2()
7633 int i;
7635 if (starting == NO_SCREEN || ScreenLines == NULL
7636 #ifdef FEAT_GUI
7637 || (gui.in_use && gui.starting)
7638 #endif
7640 return;
7642 #ifdef FEAT_GUI
7643 if (!gui.in_use)
7644 #endif
7645 screen_attr = -1; /* force setting the Normal colors */
7646 screen_stop_highlight(); /* don't want highlighting here */
7648 #ifdef FEAT_CLIPBOARD
7649 /* disable selection without redrawing it */
7650 clip_scroll_selection(9999);
7651 #endif
7653 /* blank out ScreenLines */
7654 for (i = 0; i < Rows; ++i)
7656 lineclear(LineOffset[i], (int)Columns);
7657 LineWraps[i] = FALSE;
7660 if (can_clear(T_CL))
7662 out_str(T_CL); /* clear the display */
7663 clear_cmdline = FALSE;
7664 mode_displayed = FALSE;
7666 else
7668 /* can't clear the screen, mark all chars with invalid attributes */
7669 for (i = 0; i < Rows; ++i)
7670 lineinvalid(LineOffset[i], (int)Columns);
7671 clear_cmdline = TRUE;
7674 screen_cleared = TRUE; /* can use contents of ScreenLines now */
7676 win_rest_invalid(firstwin);
7677 redraw_cmdline = TRUE;
7678 #ifdef FEAT_WINDOWS
7679 redraw_tabline = TRUE;
7680 #endif
7681 if (must_redraw == CLEAR) /* no need to clear again */
7682 must_redraw = NOT_VALID;
7683 compute_cmdrow();
7684 msg_row = cmdline_row; /* put cursor on last line for messages */
7685 msg_col = 0;
7686 screen_start(); /* don't know where cursor is now */
7687 msg_scrolled = 0; /* can't scroll back */
7688 msg_didany = FALSE;
7689 msg_didout = FALSE;
7693 * Clear one line in ScreenLines.
7695 static void
7696 lineclear(off, width)
7697 unsigned off;
7698 int width;
7700 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
7701 #ifdef FEAT_MBYTE
7702 if (enc_utf8)
7703 (void)vim_memset(ScreenLinesUC + off, 0,
7704 (size_t)width * sizeof(u8char_T));
7705 #endif
7706 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
7710 * Mark one line in ScreenLines invalid by setting the attributes to an
7711 * invalid value.
7713 static void
7714 lineinvalid(off, width)
7715 unsigned off;
7716 int width;
7718 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
7721 #ifdef FEAT_VERTSPLIT
7723 * Copy part of a Screenline for vertically split window "wp".
7725 static void
7726 linecopy(to, from, wp)
7727 int to;
7728 int from;
7729 win_T *wp;
7731 unsigned off_to = LineOffset[to] + wp->w_wincol;
7732 unsigned off_from = LineOffset[from] + wp->w_wincol;
7734 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
7735 wp->w_width * sizeof(schar_T));
7736 # ifdef FEAT_MBYTE
7737 if (enc_utf8)
7739 int i;
7741 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
7742 wp->w_width * sizeof(u8char_T));
7743 for (i = 0; i < p_mco; ++i)
7744 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
7745 wp->w_width * sizeof(u8char_T));
7747 if (enc_dbcs == DBCS_JPNU)
7748 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
7749 wp->w_width * sizeof(schar_T));
7750 # endif
7751 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
7752 wp->w_width * sizeof(sattr_T));
7754 #endif
7757 * Return TRUE if clearing with term string "p" would work.
7758 * It can't work when the string is empty or it won't set the right background.
7761 can_clear(p)
7762 char_u *p;
7764 return (*p != NUL && (t_colors <= 1
7765 #ifdef FEAT_GUI
7766 || gui.in_use
7767 #endif
7768 || cterm_normal_bg_color == 0 || *T_UT != NUL));
7772 * Reset cursor position. Use whenever cursor was moved because of outputting
7773 * something directly to the screen (shell commands) or a terminal control
7774 * code.
7776 void
7777 screen_start()
7779 screen_cur_row = screen_cur_col = 9999;
7783 * Move the cursor to position "row","col" in the screen.
7784 * This tries to find the most efficient way to move, minimizing the number of
7785 * characters sent to the terminal.
7787 void
7788 windgoto(row, col)
7789 int row;
7790 int col;
7792 sattr_T *p;
7793 int i;
7794 int plan;
7795 int cost;
7796 int wouldbe_col;
7797 int noinvcurs;
7798 char_u *bs;
7799 int goto_cost;
7800 int attr;
7802 #define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
7803 #define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
7805 #define PLAN_LE 1
7806 #define PLAN_CR 2
7807 #define PLAN_NL 3
7808 #define PLAN_WRITE 4
7809 /* Can't use ScreenLines unless initialized */
7810 if (ScreenLines == NULL)
7811 return;
7813 if (col != screen_cur_col || row != screen_cur_row)
7815 /* Check for valid position. */
7816 if (row < 0) /* window without text lines? */
7817 row = 0;
7818 if (row >= screen_Rows)
7819 row = screen_Rows - 1;
7820 if (col >= screen_Columns)
7821 col = screen_Columns - 1;
7823 /* check if no cursor movement is allowed in highlight mode */
7824 if (screen_attr && *T_MS == NUL)
7825 noinvcurs = HIGHL_COST;
7826 else
7827 noinvcurs = 0;
7828 goto_cost = GOTO_COST + noinvcurs;
7831 * Plan how to do the positioning:
7832 * 1. Use CR to move it to column 0, same row.
7833 * 2. Use T_LE to move it a few columns to the left.
7834 * 3. Use NL to move a few lines down, column 0.
7835 * 4. Move a few columns to the right with T_ND or by writing chars.
7837 * Don't do this if the cursor went beyond the last column, the cursor
7838 * position is unknown then (some terminals wrap, some don't )
7840 * First check if the highlighting attributes allow us to write
7841 * characters to move the cursor to the right.
7843 if (row >= screen_cur_row && screen_cur_col < Columns)
7846 * If the cursor is in the same row, bigger col, we can use CR
7847 * or T_LE.
7849 bs = NULL; /* init for GCC */
7850 attr = screen_attr;
7851 if (row == screen_cur_row && col < screen_cur_col)
7853 /* "le" is preferred over "bc", because "bc" is obsolete */
7854 if (*T_LE)
7855 bs = T_LE; /* "cursor left" */
7856 else
7857 bs = T_BC; /* "backspace character (old) */
7858 if (*bs)
7859 cost = (screen_cur_col - col) * (int)STRLEN(bs);
7860 else
7861 cost = 999;
7862 if (col + 1 < cost) /* using CR is less characters */
7864 plan = PLAN_CR;
7865 wouldbe_col = 0;
7866 cost = 1; /* CR is just one character */
7868 else
7870 plan = PLAN_LE;
7871 wouldbe_col = col;
7873 if (noinvcurs) /* will stop highlighting */
7875 cost += noinvcurs;
7876 attr = 0;
7881 * If the cursor is above where we want to be, we can use CR LF.
7883 else if (row > screen_cur_row)
7885 plan = PLAN_NL;
7886 wouldbe_col = 0;
7887 cost = (row - screen_cur_row) * 2; /* CR LF */
7888 if (noinvcurs) /* will stop highlighting */
7890 cost += noinvcurs;
7891 attr = 0;
7896 * If the cursor is in the same row, smaller col, just use write.
7898 else
7900 plan = PLAN_WRITE;
7901 wouldbe_col = screen_cur_col;
7902 cost = 0;
7906 * Check if any characters that need to be written have the
7907 * correct attributes. Also avoid UTF-8 characters.
7909 i = col - wouldbe_col;
7910 if (i > 0)
7911 cost += i;
7912 if (cost < goto_cost && i > 0)
7915 * Check if the attributes are correct without additionally
7916 * stopping highlighting.
7918 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
7919 while (i && *p++ == attr)
7920 --i;
7921 if (i != 0)
7924 * Try if it works when highlighting is stopped here.
7926 if (*--p == 0)
7928 cost += noinvcurs;
7929 while (i && *p++ == 0)
7930 --i;
7932 if (i != 0)
7933 cost = 999; /* different attributes, don't do it */
7935 #ifdef FEAT_MBYTE
7936 if (enc_utf8)
7938 /* Don't use an UTF-8 char for positioning, it's slow. */
7939 for (i = wouldbe_col; i < col; ++i)
7940 if (ScreenLinesUC[LineOffset[row] + i] != 0)
7942 cost = 999;
7943 break;
7946 #endif
7950 * We can do it without term_windgoto()!
7952 if (cost < goto_cost)
7954 if (plan == PLAN_LE)
7956 if (noinvcurs)
7957 screen_stop_highlight();
7958 while (screen_cur_col > col)
7960 out_str(bs);
7961 --screen_cur_col;
7964 else if (plan == PLAN_CR)
7966 if (noinvcurs)
7967 screen_stop_highlight();
7968 out_char('\r');
7969 screen_cur_col = 0;
7971 else if (plan == PLAN_NL)
7973 if (noinvcurs)
7974 screen_stop_highlight();
7975 while (screen_cur_row < row)
7977 out_char('\n');
7978 ++screen_cur_row;
7980 screen_cur_col = 0;
7983 i = col - screen_cur_col;
7984 if (i > 0)
7987 * Use cursor-right if it's one character only. Avoids
7988 * removing a line of pixels from the last bold char, when
7989 * using the bold trick in the GUI.
7991 if (T_ND[0] != NUL && T_ND[1] == NUL)
7993 while (i-- > 0)
7994 out_char(*T_ND);
7996 else
7998 int off;
8000 off = LineOffset[row] + screen_cur_col;
8001 while (i-- > 0)
8003 if (ScreenAttrs[off] != screen_attr)
8004 screen_stop_highlight();
8005 #ifdef FEAT_MBYTE
8006 out_flush_check();
8007 #endif
8008 out_char(ScreenLines[off]);
8009 #ifdef FEAT_MBYTE
8010 if (enc_dbcs == DBCS_JPNU
8011 && ScreenLines[off] == 0x8e)
8012 out_char(ScreenLines2[off]);
8013 #endif
8014 ++off;
8020 else
8021 cost = 999;
8023 if (cost >= goto_cost)
8025 if (noinvcurs)
8026 screen_stop_highlight();
8027 if (row == screen_cur_row && (col > screen_cur_col) &&
8028 *T_CRI != NUL)
8029 term_cursor_right(col - screen_cur_col);
8030 else
8031 term_windgoto(row, col);
8033 screen_cur_row = row;
8034 screen_cur_col = col;
8039 * Set cursor to its position in the current window.
8041 void
8042 setcursor()
8044 if (redrawing())
8046 validate_cursor();
8047 windgoto(W_WINROW(curwin) + curwin->w_wrow,
8048 W_WINCOL(curwin) + (
8049 #ifdef FEAT_RIGHTLEFT
8050 /* With 'rightleft' set and the cursor on a double-wide
8051 * character, position it on the leftmost column. */
8052 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
8053 # ifdef FEAT_MBYTE
8054 (has_mbyte
8055 && (*mb_ptr2cells)(ml_get_cursor()) == 2
8056 && vim_isprintc(gchar_cursor())) ? 2 :
8057 # endif
8058 1)) :
8059 #endif
8060 curwin->w_wcol));
8066 * insert 'line_count' lines at 'row' in window 'wp'
8067 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
8068 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
8069 * scrolling.
8070 * Returns FAIL if the lines are not inserted, OK for success.
8073 win_ins_lines(wp, row, line_count, invalid, mayclear)
8074 win_T *wp;
8075 int row;
8076 int line_count;
8077 int invalid;
8078 int mayclear;
8080 int did_delete;
8081 int nextrow;
8082 int lastrow;
8083 int retval;
8085 if (invalid)
8086 wp->w_lines_valid = 0;
8088 if (wp->w_height < 5)
8089 return FAIL;
8091 if (line_count > wp->w_height - row)
8092 line_count = wp->w_height - row;
8094 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
8095 if (retval != MAYBE)
8096 return retval;
8099 * If there is a next window or a status line, we first try to delete the
8100 * lines at the bottom to avoid messing what is after the window.
8101 * If this fails and there are following windows, don't do anything to avoid
8102 * messing up those windows, better just redraw.
8104 did_delete = FALSE;
8105 #ifdef FEAT_WINDOWS
8106 if (wp->w_next != NULL || wp->w_status_height)
8108 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8109 line_count, (int)Rows, FALSE, NULL) == OK)
8110 did_delete = TRUE;
8111 else if (wp->w_next)
8112 return FAIL;
8114 #endif
8116 * if no lines deleted, blank the lines that will end up below the window
8118 if (!did_delete)
8120 #ifdef FEAT_WINDOWS
8121 wp->w_redr_status = TRUE;
8122 #endif
8123 redraw_cmdline = TRUE;
8124 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
8125 lastrow = nextrow + line_count;
8126 if (lastrow > Rows)
8127 lastrow = Rows;
8128 screen_fill(nextrow - line_count, lastrow - line_count,
8129 W_WINCOL(wp), (int)W_ENDCOL(wp),
8130 ' ', ' ', 0);
8133 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
8134 == FAIL)
8136 /* deletion will have messed up other windows */
8137 if (did_delete)
8139 #ifdef FEAT_WINDOWS
8140 wp->w_redr_status = TRUE;
8141 #endif
8142 win_rest_invalid(W_NEXT(wp));
8144 return FAIL;
8147 return OK;
8151 * delete "line_count" window lines at "row" in window "wp"
8152 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
8153 * If "mayclear" is TRUE the screen will be cleared if it is faster than
8154 * scrolling
8155 * Return OK for success, FAIL if the lines are not deleted.
8158 win_del_lines(wp, row, line_count, invalid, mayclear)
8159 win_T *wp;
8160 int row;
8161 int line_count;
8162 int invalid;
8163 int mayclear;
8165 int retval;
8167 if (invalid)
8168 wp->w_lines_valid = 0;
8170 if (line_count > wp->w_height - row)
8171 line_count = wp->w_height - row;
8173 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
8174 if (retval != MAYBE)
8175 return retval;
8177 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
8178 (int)Rows, FALSE, NULL) == FAIL)
8179 return FAIL;
8181 #ifdef FEAT_WINDOWS
8183 * If there are windows or status lines below, try to put them at the
8184 * correct place. If we can't do that, they have to be redrawn.
8186 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
8188 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8189 line_count, (int)Rows, NULL) == FAIL)
8191 wp->w_redr_status = TRUE;
8192 win_rest_invalid(wp->w_next);
8196 * If this is the last window and there is no status line, redraw the
8197 * command line later.
8199 else
8200 #endif
8201 redraw_cmdline = TRUE;
8202 return OK;
8206 * Common code for win_ins_lines() and win_del_lines().
8207 * Returns OK or FAIL when the work has been done.
8208 * Returns MAYBE when not finished yet.
8210 static int
8211 win_do_lines(wp, row, line_count, mayclear, del)
8212 win_T *wp;
8213 int row;
8214 int line_count;
8215 int mayclear;
8216 int del;
8218 int retval;
8220 if (!redrawing() || line_count <= 0)
8221 return FAIL;
8223 /* only a few lines left: redraw is faster */
8224 if (mayclear && Rows - line_count < 5
8225 #ifdef FEAT_VERTSPLIT
8226 && wp->w_width == Columns
8227 #endif
8230 screenclear(); /* will set wp->w_lines_valid to 0 */
8231 return FAIL;
8235 * Delete all remaining lines
8237 if (row + line_count >= wp->w_height)
8239 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
8240 W_WINCOL(wp), (int)W_ENDCOL(wp),
8241 ' ', ' ', 0);
8242 return OK;
8246 * when scrolling, the message on the command line should be cleared,
8247 * otherwise it will stay there forever.
8249 clear_cmdline = TRUE;
8252 * If the terminal can set a scroll region, use that.
8253 * Always do this in a vertically split window. This will redraw from
8254 * ScreenLines[] when t_CV isn't defined. That's faster than using
8255 * win_line().
8256 * Don't use a scroll region when we are going to redraw the text, writing
8257 * a character in the lower right corner of the scroll region causes a
8258 * scroll-up in the DJGPP version.
8260 if (scroll_region
8261 #ifdef FEAT_VERTSPLIT
8262 || W_WIDTH(wp) != Columns
8263 #endif
8266 #ifdef FEAT_VERTSPLIT
8267 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8268 #endif
8269 scroll_region_set(wp, row);
8270 if (del)
8271 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
8272 wp->w_height - row, FALSE, wp);
8273 else
8274 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
8275 wp->w_height - row, wp);
8276 #ifdef FEAT_VERTSPLIT
8277 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8278 #endif
8279 scroll_region_reset();
8280 return retval;
8283 #ifdef FEAT_WINDOWS
8284 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
8285 return FAIL;
8286 #endif
8288 return MAYBE;
8292 * window 'wp' and everything after it is messed up, mark it for redraw
8294 static void
8295 win_rest_invalid(wp)
8296 win_T *wp;
8298 #ifdef FEAT_WINDOWS
8299 while (wp != NULL)
8300 #else
8301 if (wp != NULL)
8302 #endif
8304 redraw_win_later(wp, NOT_VALID);
8305 #ifdef FEAT_WINDOWS
8306 wp->w_redr_status = TRUE;
8307 wp = wp->w_next;
8308 #endif
8310 redraw_cmdline = TRUE;
8314 * The rest of the routines in this file perform screen manipulations. The
8315 * given operation is performed physically on the screen. The corresponding
8316 * change is also made to the internal screen image. In this way, the editor
8317 * anticipates the effect of editing changes on the appearance of the screen.
8318 * That way, when we call screenupdate a complete redraw isn't usually
8319 * necessary. Another advantage is that we can keep adding code to anticipate
8320 * screen changes, and in the meantime, everything still works.
8324 * types for inserting or deleting lines
8326 #define USE_T_CAL 1
8327 #define USE_T_CDL 2
8328 #define USE_T_AL 3
8329 #define USE_T_CE 4
8330 #define USE_T_DL 5
8331 #define USE_T_SR 6
8332 #define USE_NL 7
8333 #define USE_T_CD 8
8334 #define USE_REDRAW 9
8337 * insert lines on the screen and update ScreenLines[]
8338 * 'end' is the line after the scrolled part. Normally it is Rows.
8339 * When scrolling region used 'off' is the offset from the top for the region.
8340 * 'row' and 'end' are relative to the start of the region.
8342 * return FAIL for failure, OK for success.
8345 screen_ins_lines(off, row, line_count, end, wp)
8346 int off;
8347 int row;
8348 int line_count;
8349 int end;
8350 win_T *wp; /* NULL or window to use width from */
8352 int i;
8353 int j;
8354 unsigned temp;
8355 int cursor_row;
8356 int type;
8357 int result_empty;
8358 int can_ce = can_clear(T_CE);
8361 * FAIL if
8362 * - there is no valid screen
8363 * - the screen has to be redrawn completely
8364 * - the line count is less than one
8365 * - the line count is more than 'ttyscroll'
8367 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
8368 return FAIL;
8371 * There are seven ways to insert lines:
8372 * 0. When in a vertically split window and t_CV isn't set, redraw the
8373 * characters from ScreenLines[].
8374 * 1. Use T_CD (clear to end of display) if it exists and the result of
8375 * the insert is just empty lines
8376 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
8377 * present or line_count > 1. It looks better if we do all the inserts
8378 * at once.
8379 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
8380 * insert is just empty lines and T_CE is not present or line_count >
8381 * 1.
8382 * 4. Use T_AL (insert line) if it exists.
8383 * 5. Use T_CE (erase line) if it exists and the result of the insert is
8384 * just empty lines.
8385 * 6. Use T_DL (delete line) if it exists and the result of the insert is
8386 * just empty lines.
8387 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
8388 * the 'da' flag is not set or we have clear line capability.
8389 * 8. redraw the characters from ScreenLines[].
8391 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
8392 * the scrollbar for the window. It does have insert line, use that if it
8393 * exists.
8395 result_empty = (row + line_count >= end);
8396 #ifdef FEAT_VERTSPLIT
8397 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
8398 type = USE_REDRAW;
8399 else
8400 #endif
8401 if (can_clear(T_CD) && result_empty)
8402 type = USE_T_CD;
8403 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
8404 type = USE_T_CAL;
8405 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
8406 type = USE_T_CDL;
8407 else if (*T_AL != NUL)
8408 type = USE_T_AL;
8409 else if (can_ce && result_empty)
8410 type = USE_T_CE;
8411 else if (*T_DL != NUL && result_empty)
8412 type = USE_T_DL;
8413 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
8414 type = USE_T_SR;
8415 else
8416 return FAIL;
8419 * For clearing the lines screen_del_lines() is used. This will also take
8420 * care of t_db if necessary.
8422 if (type == USE_T_CD || type == USE_T_CDL ||
8423 type == USE_T_CE || type == USE_T_DL)
8424 return screen_del_lines(off, row, line_count, end, FALSE, wp);
8427 * If text is retained below the screen, first clear or delete as many
8428 * lines at the bottom of the window as are about to be inserted so that
8429 * the deleted lines won't later surface during a screen_del_lines.
8431 if (*T_DB)
8432 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
8434 #ifdef FEAT_CLIPBOARD
8435 /* Remove a modeless selection when inserting lines halfway the screen
8436 * or not the full width of the screen. */
8437 if (off + row > 0
8438 # ifdef FEAT_VERTSPLIT
8439 || (wp != NULL && wp->w_width != Columns)
8440 # endif
8442 clip_clear_selection();
8443 else
8444 clip_scroll_selection(-line_count);
8445 #endif
8447 #ifdef FEAT_GUI
8448 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
8449 * scrolling is actually carried out. */
8450 gui_dont_update_cursor();
8451 #endif
8453 if (*T_CCS != NUL) /* cursor relative to region */
8454 cursor_row = row;
8455 else
8456 cursor_row = row + off;
8459 * Shift LineOffset[] line_count down to reflect the inserted lines.
8460 * Clear the inserted lines in ScreenLines[].
8462 row += off;
8463 end += off;
8464 for (i = 0; i < line_count; ++i)
8466 #ifdef FEAT_VERTSPLIT
8467 if (wp != NULL && wp->w_width != Columns)
8469 /* need to copy part of a line */
8470 j = end - 1 - i;
8471 while ((j -= line_count) >= row)
8472 linecopy(j + line_count, j, wp);
8473 j += line_count;
8474 if (can_clear((char_u *)" "))
8475 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
8476 else
8477 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
8478 LineWraps[j] = FALSE;
8480 else
8481 #endif
8483 j = end - 1 - i;
8484 temp = LineOffset[j];
8485 while ((j -= line_count) >= row)
8487 LineOffset[j + line_count] = LineOffset[j];
8488 LineWraps[j + line_count] = LineWraps[j];
8490 LineOffset[j + line_count] = temp;
8491 LineWraps[j + line_count] = FALSE;
8492 if (can_clear((char_u *)" "))
8493 lineclear(temp, (int)Columns);
8494 else
8495 lineinvalid(temp, (int)Columns);
8499 screen_stop_highlight();
8500 windgoto(cursor_row, 0);
8502 #ifdef FEAT_VERTSPLIT
8503 /* redraw the characters */
8504 if (type == USE_REDRAW)
8505 redraw_block(row, end, wp);
8506 else
8507 #endif
8508 if (type == USE_T_CAL)
8510 term_append_lines(line_count);
8511 screen_start(); /* don't know where cursor is now */
8513 else
8515 for (i = 0; i < line_count; i++)
8517 if (type == USE_T_AL)
8519 if (i && cursor_row != 0)
8520 windgoto(cursor_row, 0);
8521 out_str(T_AL);
8523 else /* type == USE_T_SR */
8524 out_str(T_SR);
8525 screen_start(); /* don't know where cursor is now */
8530 * With scroll-reverse and 'da' flag set we need to clear the lines that
8531 * have been scrolled down into the region.
8533 if (type == USE_T_SR && *T_DA)
8535 for (i = 0; i < line_count; ++i)
8537 windgoto(off + i, 0);
8538 out_str(T_CE);
8539 screen_start(); /* don't know where cursor is now */
8543 #ifdef FEAT_GUI
8544 gui_can_update_cursor();
8545 if (gui.in_use)
8546 out_flush(); /* always flush after a scroll */
8547 #endif
8548 return OK;
8552 * delete lines on the screen and update ScreenLines[]
8553 * 'end' is the line after the scrolled part. Normally it is Rows.
8554 * When scrolling region used 'off' is the offset from the top for the region.
8555 * 'row' and 'end' are relative to the start of the region.
8557 * Return OK for success, FAIL if the lines are not deleted.
8559 /*ARGSUSED*/
8561 screen_del_lines(off, row, line_count, end, force, wp)
8562 int off;
8563 int row;
8564 int line_count;
8565 int end;
8566 int force; /* even when line_count > p_ttyscroll */
8567 win_T *wp; /* NULL or window to use width from */
8569 int j;
8570 int i;
8571 unsigned temp;
8572 int cursor_row;
8573 int cursor_end;
8574 int result_empty; /* result is empty until end of region */
8575 int can_delete; /* deleting line codes can be used */
8576 int type;
8579 * FAIL if
8580 * - there is no valid screen
8581 * - the screen has to be redrawn completely
8582 * - the line count is less than one
8583 * - the line count is more than 'ttyscroll'
8585 if (!screen_valid(TRUE) || line_count <= 0 ||
8586 (!force && line_count > p_ttyscroll))
8587 return FAIL;
8590 * Check if the rest of the current region will become empty.
8592 result_empty = row + line_count >= end;
8595 * We can delete lines only when 'db' flag not set or when 'ce' option
8596 * available.
8598 can_delete = (*T_DB == NUL || can_clear(T_CE));
8601 * There are six ways to delete lines:
8602 * 0. When in a vertically split window and t_CV isn't set, redraw the
8603 * characters from ScreenLines[].
8604 * 1. Use T_CD if it exists and the result is empty.
8605 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
8606 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
8607 * none of the other ways work.
8608 * 4. Use T_CE (erase line) if the result is empty.
8609 * 5. Use T_DL (delete line) if it exists.
8610 * 6. redraw the characters from ScreenLines[].
8612 #ifdef FEAT_VERTSPLIT
8613 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
8614 type = USE_REDRAW;
8615 else
8616 #endif
8617 if (can_clear(T_CD) && result_empty)
8618 type = USE_T_CD;
8619 #if defined(__BEOS__) && defined(BEOS_DR8)
8621 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
8622 * its internal termcap... this works okay for tests which test *T_DB !=
8623 * NUL. It has the disadvantage that the user cannot use any :set t_*
8624 * command to get T_DB (back) to empty_option, only :set term=... will do
8625 * the trick...
8626 * Anyway, this hack will hopefully go away with the next OS release.
8627 * (Olaf Seibert)
8629 else if (row == 0 && T_DB == empty_option
8630 && (line_count == 1 || *T_CDL == NUL))
8631 #else
8632 else if (row == 0 && (
8633 #ifndef AMIGA
8634 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
8635 * up, so use delete-line command */
8636 line_count == 1 ||
8637 #endif
8638 *T_CDL == NUL))
8639 #endif
8640 type = USE_NL;
8641 else if (*T_CDL != NUL && line_count > 1 && can_delete)
8642 type = USE_T_CDL;
8643 else if (can_clear(T_CE) && result_empty
8644 #ifdef FEAT_VERTSPLIT
8645 && (wp == NULL || wp->w_width == Columns)
8646 #endif
8648 type = USE_T_CE;
8649 else if (*T_DL != NUL && can_delete)
8650 type = USE_T_DL;
8651 else if (*T_CDL != NUL && can_delete)
8652 type = USE_T_CDL;
8653 else
8654 return FAIL;
8656 #ifdef FEAT_CLIPBOARD
8657 /* Remove a modeless selection when deleting lines halfway the screen or
8658 * not the full width of the screen. */
8659 if (off + row > 0
8660 # ifdef FEAT_VERTSPLIT
8661 || (wp != NULL && wp->w_width != Columns)
8662 # endif
8664 clip_clear_selection();
8665 else
8666 clip_scroll_selection(line_count);
8667 #endif
8669 #ifdef FEAT_GUI
8670 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
8671 * scrolling is actually carried out. */
8672 gui_dont_update_cursor();
8673 #endif
8675 if (*T_CCS != NUL) /* cursor relative to region */
8677 cursor_row = row;
8678 cursor_end = end;
8680 else
8682 cursor_row = row + off;
8683 cursor_end = end + off;
8687 * Now shift LineOffset[] line_count up to reflect the deleted lines.
8688 * Clear the inserted lines in ScreenLines[].
8690 row += off;
8691 end += off;
8692 for (i = 0; i < line_count; ++i)
8694 #ifdef FEAT_VERTSPLIT
8695 if (wp != NULL && wp->w_width != Columns)
8697 /* need to copy part of a line */
8698 j = row + i;
8699 while ((j += line_count) <= end - 1)
8700 linecopy(j - line_count, j, wp);
8701 j -= line_count;
8702 if (can_clear((char_u *)" "))
8703 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
8704 else
8705 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
8706 LineWraps[j] = FALSE;
8708 else
8709 #endif
8711 /* whole width, moving the line pointers is faster */
8712 j = row + i;
8713 temp = LineOffset[j];
8714 while ((j += line_count) <= end - 1)
8716 LineOffset[j - line_count] = LineOffset[j];
8717 LineWraps[j - line_count] = LineWraps[j];
8719 LineOffset[j - line_count] = temp;
8720 LineWraps[j - line_count] = FALSE;
8721 if (can_clear((char_u *)" "))
8722 lineclear(temp, (int)Columns);
8723 else
8724 lineinvalid(temp, (int)Columns);
8728 screen_stop_highlight();
8730 #ifdef FEAT_VERTSPLIT
8731 /* redraw the characters */
8732 if (type == USE_REDRAW)
8733 redraw_block(row, end, wp);
8734 else
8735 #endif
8736 if (type == USE_T_CD) /* delete the lines */
8738 windgoto(cursor_row, 0);
8739 out_str(T_CD);
8740 screen_start(); /* don't know where cursor is now */
8742 else if (type == USE_T_CDL)
8744 windgoto(cursor_row, 0);
8745 term_delete_lines(line_count);
8746 screen_start(); /* don't know where cursor is now */
8749 * Deleting lines at top of the screen or scroll region: Just scroll
8750 * the whole screen (scroll region) up by outputting newlines on the
8751 * last line.
8753 else if (type == USE_NL)
8755 windgoto(cursor_end - 1, 0);
8756 for (i = line_count; --i >= 0; )
8757 out_char('\n'); /* cursor will remain on same line */
8759 else
8761 for (i = line_count; --i >= 0; )
8763 if (type == USE_T_DL)
8765 windgoto(cursor_row, 0);
8766 out_str(T_DL); /* delete a line */
8768 else /* type == USE_T_CE */
8770 windgoto(cursor_row + i, 0);
8771 out_str(T_CE); /* erase a line */
8773 screen_start(); /* don't know where cursor is now */
8778 * If the 'db' flag is set, we need to clear the lines that have been
8779 * scrolled up at the bottom of the region.
8781 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
8783 for (i = line_count; i > 0; --i)
8785 windgoto(cursor_end - i, 0);
8786 out_str(T_CE); /* erase a line */
8787 screen_start(); /* don't know where cursor is now */
8791 #ifdef FEAT_GUI
8792 gui_can_update_cursor();
8793 if (gui.in_use)
8794 out_flush(); /* always flush after a scroll */
8795 #endif
8797 return OK;
8801 * show the current mode and ruler
8803 * If clear_cmdline is TRUE, clear the rest of the cmdline.
8804 * If clear_cmdline is FALSE there may be a message there that needs to be
8805 * cleared only if a mode is shown.
8806 * Return the length of the message (0 if no message).
8809 showmode()
8811 int need_clear;
8812 int length = 0;
8813 int do_mode;
8814 int attr;
8815 int nwr_save;
8816 #ifdef FEAT_INS_EXPAND
8817 int sub_attr;
8818 #endif
8820 do_mode = ((p_smd && msg_silent == 0)
8821 && ((State & INSERT)
8822 || restart_edit
8823 #ifdef FEAT_VISUAL
8824 || VIsual_active
8825 #endif
8827 if (do_mode || Recording)
8830 * Don't show mode right now, when not redrawing or inside a mapping.
8831 * Call char_avail() only when we are going to show something, because
8832 * it takes a bit of time.
8834 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
8836 redraw_cmdline = TRUE; /* show mode later */
8837 return 0;
8840 nwr_save = need_wait_return;
8842 /* wait a bit before overwriting an important message */
8843 check_for_delay(FALSE);
8845 /* if the cmdline is more than one line high, erase top lines */
8846 need_clear = clear_cmdline;
8847 if (clear_cmdline && cmdline_row < Rows - 1)
8848 msg_clr_cmdline(); /* will reset clear_cmdline */
8850 /* Position on the last line in the window, column 0 */
8851 msg_pos_mode();
8852 cursor_off();
8853 attr = hl_attr(HLF_CM); /* Highlight mode */
8854 if (do_mode)
8856 MSG_PUTS_ATTR("--", attr);
8857 #if defined(FEAT_XIM)
8858 if (xic != NULL && im_get_status() && !p_imdisable
8859 && curbuf->b_p_iminsert == B_IMODE_IM)
8860 # ifdef HAVE_GTK2 /* most of the time, it's not XIM being used */
8861 MSG_PUTS_ATTR(" IM", attr);
8862 # else
8863 MSG_PUTS_ATTR(" XIM", attr);
8864 # endif
8865 #endif
8866 #if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
8867 if (gui.in_use)
8869 if (hangul_input_state_get())
8870 MSG_PUTS_ATTR(" \307\321\261\333", attr); /* HANGUL */
8872 #endif
8873 #ifdef FEAT_INS_EXPAND
8874 if (edit_submode != NULL) /* CTRL-X in Insert mode */
8876 /* These messages can get long, avoid a wrap in a narrow
8877 * window. Prefer showing edit_submode_extra. */
8878 length = (Rows - msg_row) * Columns - 3;
8879 if (edit_submode_extra != NULL)
8880 length -= vim_strsize(edit_submode_extra);
8881 if (length > 0)
8883 if (edit_submode_pre != NULL)
8884 length -= vim_strsize(edit_submode_pre);
8885 if (length - vim_strsize(edit_submode) > 0)
8887 if (edit_submode_pre != NULL)
8888 msg_puts_attr(edit_submode_pre, attr);
8889 msg_puts_attr(edit_submode, attr);
8891 if (edit_submode_extra != NULL)
8893 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
8894 if ((int)edit_submode_highl < (int)HLF_COUNT)
8895 sub_attr = hl_attr(edit_submode_highl);
8896 else
8897 sub_attr = attr;
8898 msg_puts_attr(edit_submode_extra, sub_attr);
8901 length = 0;
8903 else
8904 #endif
8906 #ifdef FEAT_VREPLACE
8907 if (State & VREPLACE_FLAG)
8908 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
8909 else
8910 #endif
8911 if (State & REPLACE_FLAG)
8912 MSG_PUTS_ATTR(_(" REPLACE"), attr);
8913 else if (State & INSERT)
8915 #ifdef FEAT_RIGHTLEFT
8916 if (p_ri)
8917 MSG_PUTS_ATTR(_(" REVERSE"), attr);
8918 #endif
8919 MSG_PUTS_ATTR(_(" INSERT"), attr);
8921 else if (restart_edit == 'I')
8922 MSG_PUTS_ATTR(_(" (insert)"), attr);
8923 else if (restart_edit == 'R')
8924 MSG_PUTS_ATTR(_(" (replace)"), attr);
8925 else if (restart_edit == 'V')
8926 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
8927 #ifdef FEAT_RIGHTLEFT
8928 if (p_hkmap)
8929 MSG_PUTS_ATTR(_(" Hebrew"), attr);
8930 # ifdef FEAT_FKMAP
8931 if (p_fkmap)
8932 MSG_PUTS_ATTR(farsi_text_5, attr);
8933 # endif
8934 #endif
8935 #ifdef FEAT_KEYMAP
8936 if (State & LANGMAP)
8938 # ifdef FEAT_ARABIC
8939 if (curwin->w_p_arab)
8940 MSG_PUTS_ATTR(_(" Arabic"), attr);
8941 else
8942 # endif
8943 MSG_PUTS_ATTR(_(" (lang)"), attr);
8945 #endif
8946 if ((State & INSERT) && p_paste)
8947 MSG_PUTS_ATTR(_(" (paste)"), attr);
8949 #ifdef FEAT_VISUAL
8950 if (VIsual_active)
8952 char *p;
8954 /* Don't concatenate separate words to avoid translation
8955 * problems. */
8956 switch ((VIsual_select ? 4 : 0)
8957 + (VIsual_mode == Ctrl_V) * 2
8958 + (VIsual_mode == 'V'))
8960 case 0: p = N_(" VISUAL"); break;
8961 case 1: p = N_(" VISUAL LINE"); break;
8962 case 2: p = N_(" VISUAL BLOCK"); break;
8963 case 4: p = N_(" SELECT"); break;
8964 case 5: p = N_(" SELECT LINE"); break;
8965 default: p = N_(" SELECT BLOCK"); break;
8967 MSG_PUTS_ATTR(_(p), attr);
8969 #endif
8970 MSG_PUTS_ATTR(" --", attr);
8973 need_clear = TRUE;
8975 if (Recording
8976 #ifdef FEAT_INS_EXPAND
8977 && edit_submode == NULL /* otherwise it gets too long */
8978 #endif
8981 MSG_PUTS_ATTR(_("recording"), attr);
8982 need_clear = TRUE;
8985 mode_displayed = TRUE;
8986 if (need_clear || clear_cmdline)
8987 msg_clr_eos();
8988 msg_didout = FALSE; /* overwrite this message */
8989 length = msg_col;
8990 msg_col = 0;
8991 need_wait_return = nwr_save; /* never ask for hit-return for this */
8993 else if (clear_cmdline && msg_silent == 0)
8994 /* Clear the whole command line. Will reset "clear_cmdline". */
8995 msg_clr_cmdline();
8997 #ifdef FEAT_CMDL_INFO
8998 # ifdef FEAT_VISUAL
8999 /* In Visual mode the size of the selected area must be redrawn. */
9000 if (VIsual_active)
9001 clear_showcmd();
9002 # endif
9004 /* If the last window has no status line, the ruler is after the mode
9005 * message and must be redrawn */
9006 if (redrawing()
9007 # ifdef FEAT_WINDOWS
9008 && lastwin->w_status_height == 0
9009 # endif
9011 win_redr_ruler(lastwin, TRUE);
9012 #endif
9013 redraw_cmdline = FALSE;
9014 clear_cmdline = FALSE;
9016 return length;
9020 * Position for a mode message.
9022 static void
9023 msg_pos_mode()
9025 msg_col = 0;
9026 msg_row = Rows - 1;
9030 * Delete mode message. Used when ESC is typed which is expected to end
9031 * Insert mode (but Insert mode didn't end yet!).
9032 * Caller should check "mode_displayed".
9034 void
9035 unshowmode(force)
9036 int force;
9039 * Don't delete it right now, when not redrawing or insided a mapping.
9041 if (!redrawing() || (!force && char_avail() && !KeyTyped))
9042 redraw_cmdline = TRUE; /* delete mode later */
9043 else
9045 msg_pos_mode();
9046 if (Recording)
9047 MSG_PUTS_ATTR(_("recording"), hl_attr(HLF_CM));
9048 msg_clr_eos();
9052 #if defined(FEAT_WINDOWS)
9054 * Draw the tab pages line at the top of the Vim window.
9056 static void
9057 draw_tabline()
9059 int tabcount = 0;
9060 tabpage_T *tp;
9061 int tabwidth;
9062 int col = 0;
9063 int scol = 0;
9064 int attr;
9065 win_T *wp;
9066 win_T *cwp;
9067 int wincount;
9068 int modified;
9069 int c;
9070 int len;
9071 int attr_sel = hl_attr(HLF_TPS);
9072 int attr_nosel = hl_attr(HLF_TP);
9073 int attr_fill = hl_attr(HLF_TPF);
9074 char_u *p;
9075 int room;
9076 int use_sep_chars = (t_colors < 8
9077 #ifdef FEAT_GUI
9078 && !gui.in_use
9079 #endif
9082 redraw_tabline = FALSE;
9084 #ifdef FEAT_GUI_TABLINE
9085 /* Take care of a GUI tabline. */
9086 if (gui_use_tabline())
9088 gui_update_tabline();
9089 return;
9091 #endif
9093 if (tabline_height() < 1)
9094 return;
9096 #if defined(FEAT_STL_OPT)
9098 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
9099 for (scol = 0; scol < Columns; ++scol)
9100 TabPageIdxs[scol] = 0;
9102 /* Use the 'tabline' option if it's set. */
9103 if (*p_tal != NUL)
9105 int save_called_emsg = called_emsg;
9107 /* Check for an error. If there is one we would loop in redrawing the
9108 * screen. Avoid that by making 'tabline' empty. */
9109 called_emsg = FALSE;
9110 win_redr_custom(NULL, FALSE);
9111 if (called_emsg)
9112 set_string_option_direct((char_u *)"tabline", -1,
9113 (char_u *)"", OPT_FREE, SID_ERROR);
9114 called_emsg |= save_called_emsg;
9116 else
9117 #endif
9119 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
9120 ++tabcount;
9122 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
9123 if (tabwidth < 6)
9124 tabwidth = 6;
9126 attr = attr_nosel;
9127 tabcount = 0;
9128 scol = 0;
9129 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
9130 tp = tp->tp_next)
9132 scol = col;
9134 if (tp->tp_topframe == topframe)
9135 attr = attr_sel;
9136 if (use_sep_chars && col > 0)
9137 screen_putchar('|', 0, col++, attr);
9139 if (tp->tp_topframe != topframe)
9140 attr = attr_nosel;
9142 screen_putchar(' ', 0, col++, attr);
9144 if (tp == curtab)
9146 cwp = curwin;
9147 wp = firstwin;
9149 else
9151 cwp = tp->tp_curwin;
9152 wp = tp->tp_firstwin;
9155 modified = FALSE;
9156 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
9157 if (bufIsChanged(wp->w_buffer))
9158 modified = TRUE;
9159 if (modified || wincount > 1)
9161 if (wincount > 1)
9163 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
9164 len = (int)STRLEN(NameBuff);
9165 if (col + len >= Columns - 3)
9166 break;
9167 screen_puts_len(NameBuff, len, 0, col,
9168 #if defined(FEAT_SYN_HL)
9169 hl_combine_attr(attr, hl_attr(HLF_T))
9170 #else
9171 attr
9172 #endif
9174 col += len;
9176 if (modified)
9177 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
9178 screen_putchar(' ', 0, col++, attr);
9181 room = scol - col + tabwidth - 1;
9182 if (room > 0)
9184 /* Get buffer name in NameBuff[] */
9185 get_trans_bufname(cwp->w_buffer);
9186 shorten_dir(NameBuff);
9187 len = vim_strsize(NameBuff);
9188 p = NameBuff;
9189 #ifdef FEAT_MBYTE
9190 if (has_mbyte)
9191 while (len > room)
9193 len -= ptr2cells(p);
9194 mb_ptr_adv(p);
9196 else
9197 #endif
9198 if (len > room)
9200 p += len - room;
9201 len = room;
9203 if (len > Columns - col - 1)
9204 len = Columns - col - 1;
9206 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
9207 col += len;
9209 screen_putchar(' ', 0, col++, attr);
9211 /* Store the tab page number in TabPageIdxs[], so that
9212 * jump_to_mouse() knows where each one is. */
9213 ++tabcount;
9214 while (scol < col)
9215 TabPageIdxs[scol++] = tabcount;
9218 if (use_sep_chars)
9219 c = '_';
9220 else
9221 c = ' ';
9222 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
9224 /* Put an "X" for closing the current tab if there are several. */
9225 if (first_tabpage->tp_next != NULL)
9227 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
9228 TabPageIdxs[Columns - 1] = -999;
9232 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
9233 * set. */
9234 redraw_tabline = FALSE;
9238 * Get buffer name for "buf" into NameBuff[].
9239 * Takes care of special buffer names and translates special characters.
9241 void
9242 get_trans_bufname(buf)
9243 buf_T *buf;
9245 if (buf_spname(buf) != NULL)
9246 STRCPY(NameBuff, buf_spname(buf));
9247 else
9248 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
9249 trans_characters(NameBuff, MAXPATHL);
9251 #endif
9253 #if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
9255 * Get the character to use in a status line. Get its attributes in "*attr".
9257 static int
9258 fillchar_status(attr, is_curwin)
9259 int *attr;
9260 int is_curwin;
9262 int fill;
9263 if (is_curwin)
9265 *attr = hl_attr(HLF_S);
9266 fill = fill_stl;
9268 else
9270 *attr = hl_attr(HLF_SNC);
9271 fill = fill_stlnc;
9273 /* Use fill when there is highlighting, and highlighting of current
9274 * window differs, or the fillchars differ, or this is not the
9275 * current window */
9276 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
9277 || !is_curwin || firstwin == lastwin)
9278 || (fill_stl != fill_stlnc)))
9279 return fill;
9280 if (is_curwin)
9281 return '^';
9282 return '=';
9284 #endif
9286 #ifdef FEAT_VERTSPLIT
9288 * Get the character to use in a separator between vertically split windows.
9289 * Get its attributes in "*attr".
9291 static int
9292 fillchar_vsep(attr)
9293 int *attr;
9295 *attr = hl_attr(HLF_C);
9296 if (*attr == 0 && fill_vert == ' ')
9297 return '|';
9298 else
9299 return fill_vert;
9301 #endif
9304 * Return TRUE if redrawing should currently be done.
9307 redrawing()
9309 return (!RedrawingDisabled
9310 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
9314 * Return TRUE if printing messages should currently be done.
9317 messaging()
9319 return (!(p_lz && char_avail() && !KeyTyped));
9323 * Show current status info in ruler and various other places
9324 * If always is FALSE, only show ruler if position has changed.
9326 void
9327 showruler(always)
9328 int always;
9330 if (!always && !redrawing())
9331 return;
9332 #ifdef FEAT_INS_EXPAND
9333 if (pum_visible())
9335 # ifdef FEAT_WINDOWS
9336 /* Don't redraw right now, do it later. */
9337 curwin->w_redr_status = TRUE;
9338 # endif
9339 return;
9341 #endif
9342 #if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
9343 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
9345 redraw_custum_statusline(curwin);
9347 else
9348 #endif
9349 #ifdef FEAT_CMDL_INFO
9350 win_redr_ruler(curwin, always);
9351 #endif
9353 #ifdef FEAT_TITLE
9354 if (need_maketitle
9355 # ifdef FEAT_STL_OPT
9356 || (p_icon && (stl_syntax & STL_IN_ICON))
9357 || (p_title && (stl_syntax & STL_IN_TITLE))
9358 # endif
9360 maketitle();
9361 #endif
9362 #ifdef FEAT_WINDOWS
9363 /* Redraw the tab pages line if needed. */
9364 if (redraw_tabline)
9365 draw_tabline();
9366 #endif
9369 #ifdef FEAT_CMDL_INFO
9370 static void
9371 win_redr_ruler(wp, always)
9372 win_T *wp;
9373 int always;
9375 char_u buffer[70];
9376 int row;
9377 int fillchar;
9378 int attr;
9379 int empty_line = FALSE;
9380 colnr_T virtcol;
9381 int i;
9382 int o;
9383 #ifdef FEAT_VERTSPLIT
9384 int this_ru_col;
9385 int off = 0;
9386 int width = Columns;
9387 # define WITH_OFF(x) x
9388 # define WITH_WIDTH(x) x
9389 #else
9390 # define WITH_OFF(x) 0
9391 # define WITH_WIDTH(x) Columns
9392 # define this_ru_col ru_col
9393 #endif
9395 /* If 'ruler' off or redrawing disabled, don't do anything */
9396 if (!p_ru)
9397 return;
9400 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
9401 * after deleting lines, before cursor.lnum is corrected.
9403 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
9404 return;
9406 #ifdef FEAT_INS_EXPAND
9407 /* Don't draw the ruler while doing insert-completion, it might overwrite
9408 * the (long) mode message. */
9409 # ifdef FEAT_WINDOWS
9410 if (wp == lastwin && lastwin->w_status_height == 0)
9411 # endif
9412 if (edit_submode != NULL)
9413 return;
9414 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
9415 if (pum_visible())
9416 return;
9417 #endif
9419 #ifdef FEAT_STL_OPT
9420 if (*p_ruf)
9422 int save_called_emsg = called_emsg;
9424 called_emsg = FALSE;
9425 win_redr_custom(wp, TRUE);
9426 if (called_emsg)
9427 set_string_option_direct((char_u *)"rulerformat", -1,
9428 (char_u *)"", OPT_FREE, SID_ERROR);
9429 called_emsg |= save_called_emsg;
9430 return;
9432 #endif
9435 * Check if not in Insert mode and the line is empty (will show "0-1").
9437 if (!(State & INSERT)
9438 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
9439 empty_line = TRUE;
9442 * Only draw the ruler when something changed.
9444 validate_virtcol_win(wp);
9445 if ( redraw_cmdline
9446 || always
9447 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
9448 || wp->w_cursor.col != wp->w_ru_cursor.col
9449 || wp->w_virtcol != wp->w_ru_virtcol
9450 #ifdef FEAT_VIRTUALEDIT
9451 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
9452 #endif
9453 || wp->w_topline != wp->w_ru_topline
9454 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
9455 #ifdef FEAT_DIFF
9456 || wp->w_topfill != wp->w_ru_topfill
9457 #endif
9458 || empty_line != wp->w_ru_empty)
9460 cursor_off();
9461 #ifdef FEAT_WINDOWS
9462 if (wp->w_status_height)
9464 row = W_WINROW(wp) + wp->w_height;
9465 fillchar = fillchar_status(&attr, wp == curwin);
9466 # ifdef FEAT_VERTSPLIT
9467 off = W_WINCOL(wp);
9468 width = W_WIDTH(wp);
9469 # endif
9471 else
9472 #endif
9474 row = Rows - 1;
9475 fillchar = ' ';
9476 attr = 0;
9477 #ifdef FEAT_VERTSPLIT
9478 width = Columns;
9479 off = 0;
9480 #endif
9483 /* In list mode virtcol needs to be recomputed */
9484 virtcol = wp->w_virtcol;
9485 if (wp->w_p_list && lcs_tab1 == NUL)
9487 wp->w_p_list = FALSE;
9488 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
9489 wp->w_p_list = TRUE;
9493 * Some sprintfs return the length, some return a pointer.
9494 * To avoid portability problems we use strlen() here.
9496 sprintf((char *)buffer, "%ld,",
9497 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
9498 ? 0L
9499 : (long)(wp->w_cursor.lnum));
9500 col_print(buffer + STRLEN(buffer),
9501 empty_line ? 0 : (int)wp->w_cursor.col + 1,
9502 (int)virtcol + 1);
9505 * Add a "50%" if there is room for it.
9506 * On the last line, don't print in the last column (scrolls the
9507 * screen up on some terminals).
9509 i = (int)STRLEN(buffer);
9510 get_rel_pos(wp, buffer + i + 1);
9511 o = i + vim_strsize(buffer + i + 1);
9512 #ifdef FEAT_WINDOWS
9513 if (wp->w_status_height == 0) /* can't use last char of screen */
9514 #endif
9515 ++o;
9516 #ifdef FEAT_VERTSPLIT
9517 this_ru_col = ru_col - (Columns - width);
9518 if (this_ru_col < 0)
9519 this_ru_col = 0;
9520 #endif
9521 /* Never use more than half the window/screen width, leave the other
9522 * half for the filename. */
9523 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
9524 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
9525 if (this_ru_col + o < WITH_WIDTH(width))
9527 while (this_ru_col + o < WITH_WIDTH(width))
9529 #ifdef FEAT_MBYTE
9530 if (has_mbyte)
9531 i += (*mb_char2bytes)(fillchar, buffer + i);
9532 else
9533 #endif
9534 buffer[i++] = fillchar;
9535 ++o;
9537 get_rel_pos(wp, buffer + i);
9539 /* Truncate at window boundary. */
9540 #ifdef FEAT_MBYTE
9541 if (has_mbyte)
9543 o = 0;
9544 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
9546 o += (*mb_ptr2cells)(buffer + i);
9547 if (this_ru_col + o > WITH_WIDTH(width))
9549 buffer[i] = NUL;
9550 break;
9554 else
9555 #endif
9556 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
9557 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
9559 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
9560 i = redraw_cmdline;
9561 screen_fill(row, row + 1,
9562 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
9563 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
9564 fillchar, fillchar, attr);
9565 /* don't redraw the cmdline because of showing the ruler */
9566 redraw_cmdline = i;
9567 wp->w_ru_cursor = wp->w_cursor;
9568 wp->w_ru_virtcol = wp->w_virtcol;
9569 wp->w_ru_empty = empty_line;
9570 wp->w_ru_topline = wp->w_topline;
9571 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
9572 #ifdef FEAT_DIFF
9573 wp->w_ru_topfill = wp->w_topfill;
9574 #endif
9577 #endif
9579 #if defined(FEAT_LINEBREAK) || defined(PROTO)
9581 * Return the width of the 'number' column.
9582 * Caller may need to check if 'number' is set.
9583 * Otherwise it depends on 'numberwidth' and the line count.
9586 number_width(wp)
9587 win_T *wp;
9589 int n;
9590 linenr_T lnum;
9592 lnum = wp->w_buffer->b_ml.ml_line_count;
9593 if (lnum == wp->w_nrwidth_line_count)
9594 return wp->w_nrwidth_width;
9595 wp->w_nrwidth_line_count = lnum;
9597 n = 0;
9600 lnum /= 10;
9601 ++n;
9602 } while (lnum > 0);
9604 /* 'numberwidth' gives the minimal width plus one */
9605 if (n < wp->w_p_nuw - 1)
9606 n = wp->w_p_nuw - 1;
9608 wp->w_nrwidth_width = n;
9609 return n;
9611 #endif