Merged from the latest developing branch.
[MacVim/jjgod.git] / src / screen.c
blob009968be2de1afa2cf3e6c099fd0da47cf864c54
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 cur = cur->next;
853 search_hl.buf = buf;
854 search_hl.lnum = 0;
855 search_hl.first_lnum = 0;
856 #endif
858 #ifdef FEAT_LINEBREAK
859 /* Force redraw when width of 'number' column changes. */
860 i = wp->w_p_nu ? number_width(wp) : 0;
861 if (wp->w_nrwidth != i)
863 type = NOT_VALID;
864 wp->w_nrwidth = i;
866 else
867 #endif
869 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
872 * When there are both inserted/deleted lines and specific lines to be
873 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
874 * everything (only happens when redrawing is off for while).
876 type = NOT_VALID;
878 else
881 * Set mod_top to the first line that needs displaying because of
882 * changes. Set mod_bot to the first line after the changes.
884 mod_top = wp->w_redraw_top;
885 if (wp->w_redraw_bot != 0)
886 mod_bot = wp->w_redraw_bot + 1;
887 else
888 mod_bot = 0;
889 wp->w_redraw_top = 0; /* reset for next time */
890 wp->w_redraw_bot = 0;
891 if (buf->b_mod_set)
893 if (mod_top == 0 || mod_top > buf->b_mod_top)
895 mod_top = buf->b_mod_top;
896 #ifdef FEAT_SYN_HL
897 /* Need to redraw lines above the change that may be included
898 * in a pattern match. */
899 if (syntax_present(buf))
901 mod_top -= buf->b_syn_sync_linebreaks;
902 if (mod_top < 1)
903 mod_top = 1;
905 #endif
907 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
908 mod_bot = buf->b_mod_bot;
910 #ifdef FEAT_SEARCH_EXTRA
911 /* When 'hlsearch' is on and using a multi-line search pattern, a
912 * change in one line may make the Search highlighting in a
913 * previous line invalid. Simple solution: redraw all visible
914 * lines above the change.
915 * Same for a match pattern.
917 if (search_hl.rm.regprog != NULL
918 && re_multiline(search_hl.rm.regprog))
919 top_to_mod = TRUE;
920 else
922 cur = wp->w_match_head;
923 while (cur != NULL)
925 if (cur->match.regprog != NULL
926 && re_multiline(cur->match.regprog))
928 top_to_mod = TRUE;
929 break;
931 cur = cur->next;
934 #endif
936 #ifdef FEAT_FOLDING
937 if (mod_top != 0 && hasAnyFolding(wp))
939 linenr_T lnumt, lnumb;
942 * A change in a line can cause lines above it to become folded or
943 * unfolded. Find the top most buffer line that may be affected.
944 * If the line was previously folded and displayed, get the first
945 * line of that fold. If the line is folded now, get the first
946 * folded line. Use the minimum of these two.
949 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
950 * the line below it. If there is no valid entry, use w_topline.
951 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
952 * to this line. If there is no valid entry, use MAXLNUM. */
953 lnumt = wp->w_topline;
954 lnumb = MAXLNUM;
955 for (i = 0; i < wp->w_lines_valid; ++i)
956 if (wp->w_lines[i].wl_valid)
958 if (wp->w_lines[i].wl_lastlnum < mod_top)
959 lnumt = wp->w_lines[i].wl_lastlnum + 1;
960 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
962 lnumb = wp->w_lines[i].wl_lnum;
963 /* When there is a fold column it might need updating
964 * in the next line ("J" just above an open fold). */
965 if (wp->w_p_fdc > 0)
966 ++lnumb;
970 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
971 if (mod_top > lnumt)
972 mod_top = lnumt;
974 /* Now do the same for the bottom line (one above mod_bot). */
975 --mod_bot;
976 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
977 ++mod_bot;
978 if (mod_bot < lnumb)
979 mod_bot = lnumb;
981 #endif
983 /* When a change starts above w_topline and the end is below
984 * w_topline, start redrawing at w_topline.
985 * If the end of the change is above w_topline: do like no change was
986 * made, but redraw the first line to find changes in syntax. */
987 if (mod_top != 0 && mod_top < wp->w_topline)
989 if (mod_bot > wp->w_topline)
990 mod_top = wp->w_topline;
991 #ifdef FEAT_SYN_HL
992 else if (syntax_present(buf))
993 top_end = 1;
994 #endif
997 /* When line numbers are displayed need to redraw all lines below
998 * inserted/deleted lines. */
999 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1000 mod_bot = MAXLNUM;
1004 * When only displaying the lines at the top, set top_end. Used when
1005 * window has scrolled down for msg_scrolled.
1007 if (type == REDRAW_TOP)
1009 j = 0;
1010 for (i = 0; i < wp->w_lines_valid; ++i)
1012 j += wp->w_lines[i].wl_size;
1013 if (j >= wp->w_upd_rows)
1015 top_end = j;
1016 break;
1019 if (top_end == 0)
1020 /* not found (cannot happen?): redraw everything */
1021 type = NOT_VALID;
1022 else
1023 /* top area defined, the rest is VALID */
1024 type = VALID;
1027 /* Trick: we want to avoid clearning the screen twice. screenclear() will
1028 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1029 * non-zero and thus not FALSE) will indicate that screenclear() was not
1030 * called. */
1031 if (screen_cleared)
1032 screen_cleared = MAYBE;
1035 * If there are no changes on the screen that require a complete redraw,
1036 * handle three cases:
1037 * 1: we are off the top of the screen by a few lines: scroll down
1038 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1039 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1040 * w_lines[] that needs updating.
1042 if ((type == VALID || type == SOME_VALID
1043 || type == INVERTED || type == INVERTED_ALL)
1044 #ifdef FEAT_DIFF
1045 && !wp->w_botfill && !wp->w_old_botfill
1046 #endif
1049 if (mod_top != 0 && wp->w_topline == mod_top)
1052 * w_topline is the first changed line, the scrolling will be done
1053 * further down.
1056 else if (wp->w_lines[0].wl_valid
1057 && (wp->w_topline < wp->w_lines[0].wl_lnum
1058 #ifdef FEAT_DIFF
1059 || (wp->w_topline == wp->w_lines[0].wl_lnum
1060 && wp->w_topfill > wp->w_old_topfill)
1061 #endif
1065 * New topline is above old topline: May scroll down.
1067 #ifdef FEAT_FOLDING
1068 if (hasAnyFolding(wp))
1070 linenr_T ln;
1072 /* count the number of lines we are off, counting a sequence
1073 * of folded lines as one */
1074 j = 0;
1075 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1077 ++j;
1078 if (j >= wp->w_height - 2)
1079 break;
1080 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1083 else
1084 #endif
1085 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1086 if (j < wp->w_height - 2) /* not too far off */
1088 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1089 #ifdef FEAT_DIFF
1090 /* insert extra lines for previously invisible filler lines */
1091 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1092 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1093 - wp->w_old_topfill;
1094 #endif
1095 if (i < wp->w_height - 2) /* less than a screen off */
1098 * Try to insert the correct number of lines.
1099 * If not the last window, delete the lines at the bottom.
1100 * win_ins_lines may fail when the terminal can't do it.
1102 if (i > 0)
1103 check_for_delay(FALSE);
1104 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1106 if (wp->w_lines_valid != 0)
1108 /* Need to update rows that are new, stop at the
1109 * first one that scrolled down. */
1110 top_end = i;
1111 #ifdef FEAT_VISUAL
1112 scrolled_down = TRUE;
1113 #endif
1115 /* Move the entries that were scrolled, disable
1116 * the entries for the lines to be redrawn. */
1117 if ((wp->w_lines_valid += j) > wp->w_height)
1118 wp->w_lines_valid = wp->w_height;
1119 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1120 wp->w_lines[idx] = wp->w_lines[idx - j];
1121 while (idx >= 0)
1122 wp->w_lines[idx--].wl_valid = FALSE;
1125 else
1126 mid_start = 0; /* redraw all lines */
1128 else
1129 mid_start = 0; /* redraw all lines */
1131 else
1132 mid_start = 0; /* redraw all lines */
1134 else
1137 * New topline is at or below old topline: May scroll up.
1138 * When topline didn't change, find first entry in w_lines[] that
1139 * needs updating.
1142 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1143 j = -1;
1144 row = 0;
1145 for (i = 0; i < wp->w_lines_valid; i++)
1147 if (wp->w_lines[i].wl_valid
1148 && wp->w_lines[i].wl_lnum == wp->w_topline)
1150 j = i;
1151 break;
1153 row += wp->w_lines[i].wl_size;
1155 if (j == -1)
1157 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1158 * lines */
1159 mid_start = 0;
1161 else
1164 * Try to delete the correct number of lines.
1165 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1167 #ifdef FEAT_DIFF
1168 /* If the topline didn't change, delete old filler lines,
1169 * otherwise delete filler lines of the new topline... */
1170 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1171 row += wp->w_old_topfill;
1172 else
1173 row += diff_check_fill(wp, wp->w_topline);
1174 /* ... but don't delete new filler lines. */
1175 row -= wp->w_topfill;
1176 #endif
1177 if (row > 0)
1179 check_for_delay(FALSE);
1180 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1181 bot_start = wp->w_height - row;
1182 else
1183 mid_start = 0; /* redraw all lines */
1185 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1188 * Skip the lines (below the deleted lines) that are still
1189 * valid and don't need redrawing. Copy their info
1190 * upwards, to compensate for the deleted lines. Set
1191 * bot_start to the first row that needs redrawing.
1193 bot_start = 0;
1194 idx = 0;
1195 for (;;)
1197 wp->w_lines[idx] = wp->w_lines[j];
1198 /* stop at line that didn't fit, unless it is still
1199 * valid (no lines deleted) */
1200 if (row > 0 && bot_start + row
1201 + (int)wp->w_lines[j].wl_size > wp->w_height)
1203 wp->w_lines_valid = idx + 1;
1204 break;
1206 bot_start += wp->w_lines[idx++].wl_size;
1208 /* stop at the last valid entry in w_lines[].wl_size */
1209 if (++j >= wp->w_lines_valid)
1211 wp->w_lines_valid = idx;
1212 break;
1215 #ifdef FEAT_DIFF
1216 /* Correct the first entry for filler lines at the top
1217 * when it won't get updated below. */
1218 if (wp->w_p_diff && bot_start > 0)
1219 wp->w_lines[0].wl_size =
1220 plines_win_nofill(wp, wp->w_topline, TRUE)
1221 + wp->w_topfill;
1222 #endif
1227 /* When starting redraw in the first line, redraw all lines. When
1228 * there is only one window it's probably faster to clear the screen
1229 * first. */
1230 if (mid_start == 0)
1232 mid_end = wp->w_height;
1233 if (lastwin == firstwin)
1235 /* Clear the screen when it was not done by win_del_lines() or
1236 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1237 * then. */
1238 if (screen_cleared != TRUE)
1239 screenclear();
1240 #ifdef FEAT_WINDOWS
1241 /* The screen was cleared, redraw the tab pages line. */
1242 if (redraw_tabline)
1243 draw_tabline();
1244 #endif
1248 /* When win_del_lines() or win_ins_lines() caused the screen to be
1249 * cleared (only happens for the first window) or when screenclear()
1250 * was called directly above, "must_redraw" will have been set to
1251 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1252 if (screen_cleared == TRUE)
1253 must_redraw = 0;
1255 else
1257 /* Not VALID or INVERTED: redraw all lines. */
1258 mid_start = 0;
1259 mid_end = wp->w_height;
1262 if (type == SOME_VALID)
1264 /* SOME_VALID: redraw all lines. */
1265 mid_start = 0;
1266 mid_end = wp->w_height;
1267 type = NOT_VALID;
1270 #ifdef FEAT_VISUAL
1271 /* check if we are updating or removing the inverted part */
1272 if ((VIsual_active && buf == curwin->w_buffer)
1273 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1275 linenr_T from, to;
1277 if (VIsual_active)
1279 if (VIsual_active
1280 && (VIsual_mode != wp->w_old_visual_mode
1281 || type == INVERTED_ALL))
1284 * If the type of Visual selection changed, redraw the whole
1285 * selection. Also when the ownership of the X selection is
1286 * gained or lost.
1288 if (curwin->w_cursor.lnum < VIsual.lnum)
1290 from = curwin->w_cursor.lnum;
1291 to = VIsual.lnum;
1293 else
1295 from = VIsual.lnum;
1296 to = curwin->w_cursor.lnum;
1298 /* redraw more when the cursor moved as well */
1299 if (wp->w_old_cursor_lnum < from)
1300 from = wp->w_old_cursor_lnum;
1301 if (wp->w_old_cursor_lnum > to)
1302 to = wp->w_old_cursor_lnum;
1303 if (wp->w_old_visual_lnum < from)
1304 from = wp->w_old_visual_lnum;
1305 if (wp->w_old_visual_lnum > to)
1306 to = wp->w_old_visual_lnum;
1308 else
1311 * Find the line numbers that need to be updated: The lines
1312 * between the old cursor position and the current cursor
1313 * position. Also check if the Visual position changed.
1315 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1317 from = curwin->w_cursor.lnum;
1318 to = wp->w_old_cursor_lnum;
1320 else
1322 from = wp->w_old_cursor_lnum;
1323 to = curwin->w_cursor.lnum;
1324 if (from == 0) /* Visual mode just started */
1325 from = to;
1328 if (VIsual.lnum != wp->w_old_visual_lnum
1329 || VIsual.col != wp->w_old_visual_col)
1331 if (wp->w_old_visual_lnum < from
1332 && wp->w_old_visual_lnum != 0)
1333 from = wp->w_old_visual_lnum;
1334 if (wp->w_old_visual_lnum > to)
1335 to = wp->w_old_visual_lnum;
1336 if (VIsual.lnum < from)
1337 from = VIsual.lnum;
1338 if (VIsual.lnum > to)
1339 to = VIsual.lnum;
1344 * If in block mode and changed column or curwin->w_curswant:
1345 * update all lines.
1346 * First compute the actual start and end column.
1348 if (VIsual_mode == Ctrl_V)
1350 colnr_T fromc, toc;
1352 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
1353 ++toc;
1354 if (curwin->w_curswant == MAXCOL)
1355 toc = MAXCOL;
1357 if (fromc != wp->w_old_cursor_fcol
1358 || toc != wp->w_old_cursor_lcol)
1360 if (from > VIsual.lnum)
1361 from = VIsual.lnum;
1362 if (to < VIsual.lnum)
1363 to = VIsual.lnum;
1365 wp->w_old_cursor_fcol = fromc;
1366 wp->w_old_cursor_lcol = toc;
1369 else
1371 /* Use the line numbers of the old Visual area. */
1372 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1374 from = wp->w_old_cursor_lnum;
1375 to = wp->w_old_visual_lnum;
1377 else
1379 from = wp->w_old_visual_lnum;
1380 to = wp->w_old_cursor_lnum;
1385 * There is no need to update lines above the top of the window.
1387 if (from < wp->w_topline)
1388 from = wp->w_topline;
1391 * If we know the value of w_botline, use it to restrict the update to
1392 * the lines that are visible in the window.
1394 if (wp->w_valid & VALID_BOTLINE)
1396 if (from >= wp->w_botline)
1397 from = wp->w_botline - 1;
1398 if (to >= wp->w_botline)
1399 to = wp->w_botline - 1;
1403 * Find the minimal part to be updated.
1404 * Watch out for scrolling that made entries in w_lines[] invalid.
1405 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1406 * top_end; need to redraw from top_end to the "to" line.
1407 * A middle mouse click with a Visual selection may change the text
1408 * above the Visual area and reset wl_valid, do count these for
1409 * mid_end (in srow).
1411 if (mid_start > 0)
1413 lnum = wp->w_topline;
1414 idx = 0;
1415 srow = 0;
1416 if (scrolled_down)
1417 mid_start = top_end;
1418 else
1419 mid_start = 0;
1420 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1422 if (wp->w_lines[idx].wl_valid)
1423 mid_start += wp->w_lines[idx].wl_size;
1424 else if (!scrolled_down)
1425 srow += wp->w_lines[idx].wl_size;
1426 ++idx;
1427 # ifdef FEAT_FOLDING
1428 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1429 lnum = wp->w_lines[idx].wl_lnum;
1430 else
1431 # endif
1432 ++lnum;
1434 srow += mid_start;
1435 mid_end = wp->w_height;
1436 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1438 if (wp->w_lines[idx].wl_valid
1439 && wp->w_lines[idx].wl_lnum >= to + 1)
1441 /* Only update until first row of this line */
1442 mid_end = srow;
1443 break;
1445 srow += wp->w_lines[idx].wl_size;
1450 if (VIsual_active && buf == curwin->w_buffer)
1452 wp->w_old_visual_mode = VIsual_mode;
1453 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1454 wp->w_old_visual_lnum = VIsual.lnum;
1455 wp->w_old_visual_col = VIsual.col;
1456 wp->w_old_curswant = curwin->w_curswant;
1458 else
1460 wp->w_old_visual_mode = 0;
1461 wp->w_old_cursor_lnum = 0;
1462 wp->w_old_visual_lnum = 0;
1463 wp->w_old_visual_col = 0;
1465 #endif /* FEAT_VISUAL */
1467 #if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1468 /* reset got_int, otherwise regexp won't work */
1469 save_got_int = got_int;
1470 got_int = 0;
1471 #endif
1472 #ifdef FEAT_FOLDING
1473 win_foldinfo.fi_level = 0;
1474 #endif
1477 * Update all the window rows.
1479 idx = 0; /* first entry in w_lines[].wl_size */
1480 row = 0;
1481 srow = 0;
1482 lnum = wp->w_topline; /* first line shown in window */
1483 for (;;)
1485 /* stop updating when reached the end of the window (check for _past_
1486 * the end of the window is at the end of the loop) */
1487 if (row == wp->w_height)
1489 didline = TRUE;
1490 break;
1493 /* stop updating when hit the end of the file */
1494 if (lnum > buf->b_ml.ml_line_count)
1496 eof = TRUE;
1497 break;
1500 /* Remember the starting row of the line that is going to be dealt
1501 * with. It is used further down when the line doesn't fit. */
1502 srow = row;
1505 * Update a line when it is in an area that needs updating, when it
1506 * has changes or w_lines[idx] is invalid.
1507 * bot_start may be halfway a wrapped line after using
1508 * win_del_lines(), check if the current line includes it.
1509 * When syntax folding is being used, the saved syntax states will
1510 * already have been updated, we can't see where the syntax state is
1511 * the same again, just update until the end of the window.
1513 if (row < top_end
1514 || (row >= mid_start && row < mid_end)
1515 #ifdef FEAT_SEARCH_EXTRA
1516 || top_to_mod
1517 #endif
1518 || idx >= wp->w_lines_valid
1519 || (row + wp->w_lines[idx].wl_size > bot_start)
1520 || (mod_top != 0
1521 && (lnum == mod_top
1522 || (lnum >= mod_top
1523 && (lnum < mod_bot
1524 #ifdef FEAT_SYN_HL
1525 || did_update == DID_FOLD
1526 || (did_update == DID_LINE
1527 && syntax_present(buf)
1528 && (
1529 # ifdef FEAT_FOLDING
1530 (foldmethodIsSyntax(wp)
1531 && hasAnyFolding(wp)) ||
1532 # endif
1533 syntax_check_changed(lnum)))
1534 #endif
1535 )))))
1537 #ifdef FEAT_SEARCH_EXTRA
1538 if (lnum == mod_top)
1539 top_to_mod = FALSE;
1540 #endif
1543 * When at start of changed lines: May scroll following lines
1544 * up or down to minimize redrawing.
1545 * Don't do this when the change continues until the end.
1546 * Don't scroll when dollar_vcol is non-zero, keep the "$".
1548 if (lnum == mod_top
1549 && mod_bot != MAXLNUM
1550 && !(dollar_vcol != 0 && mod_bot == mod_top + 1))
1552 int old_rows = 0;
1553 int new_rows = 0;
1554 int xtra_rows;
1555 linenr_T l;
1557 /* Count the old number of window rows, using w_lines[], which
1558 * should still contain the sizes for the lines as they are
1559 * currently displayed. */
1560 for (i = idx; i < wp->w_lines_valid; ++i)
1562 /* Only valid lines have a meaningful wl_lnum. Invalid
1563 * lines are part of the changed area. */
1564 if (wp->w_lines[i].wl_valid
1565 && wp->w_lines[i].wl_lnum == mod_bot)
1566 break;
1567 old_rows += wp->w_lines[i].wl_size;
1568 #ifdef FEAT_FOLDING
1569 if (wp->w_lines[i].wl_valid
1570 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1572 /* Must have found the last valid entry above mod_bot.
1573 * Add following invalid entries. */
1574 ++i;
1575 while (i < wp->w_lines_valid
1576 && !wp->w_lines[i].wl_valid)
1577 old_rows += wp->w_lines[i++].wl_size;
1578 break;
1580 #endif
1583 if (i >= wp->w_lines_valid)
1585 /* We can't find a valid line below the changed lines,
1586 * need to redraw until the end of the window.
1587 * Inserting/deleting lines has no use. */
1588 bot_start = 0;
1590 else
1592 /* Able to count old number of rows: Count new window
1593 * rows, and may insert/delete lines */
1594 j = idx;
1595 for (l = lnum; l < mod_bot; ++l)
1597 #ifdef FEAT_FOLDING
1598 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1599 ++new_rows;
1600 else
1601 #endif
1602 #ifdef FEAT_DIFF
1603 if (l == wp->w_topline)
1604 new_rows += plines_win_nofill(wp, l, TRUE)
1605 + wp->w_topfill;
1606 else
1607 #endif
1608 new_rows += plines_win(wp, l, TRUE);
1609 ++j;
1610 if (new_rows > wp->w_height - row - 2)
1612 /* it's getting too much, must redraw the rest */
1613 new_rows = 9999;
1614 break;
1617 xtra_rows = new_rows - old_rows;
1618 if (xtra_rows < 0)
1620 /* May scroll text up. If there is not enough
1621 * remaining text or scrolling fails, must redraw the
1622 * rest. If scrolling works, must redraw the text
1623 * below the scrolled text. */
1624 if (row - xtra_rows >= wp->w_height - 2)
1625 mod_bot = MAXLNUM;
1626 else
1628 check_for_delay(FALSE);
1629 if (win_del_lines(wp, row,
1630 -xtra_rows, FALSE, FALSE) == FAIL)
1631 mod_bot = MAXLNUM;
1632 else
1633 bot_start = wp->w_height + xtra_rows;
1636 else if (xtra_rows > 0)
1638 /* May scroll text down. If there is not enough
1639 * remaining text of scrolling fails, must redraw the
1640 * rest. */
1641 if (row + xtra_rows >= wp->w_height - 2)
1642 mod_bot = MAXLNUM;
1643 else
1645 check_for_delay(FALSE);
1646 if (win_ins_lines(wp, row + old_rows,
1647 xtra_rows, FALSE, FALSE) == FAIL)
1648 mod_bot = MAXLNUM;
1649 else if (top_end > row + old_rows)
1650 /* Scrolled the part at the top that requires
1651 * updating down. */
1652 top_end += xtra_rows;
1656 /* When not updating the rest, may need to move w_lines[]
1657 * entries. */
1658 if (mod_bot != MAXLNUM && i != j)
1660 if (j < i)
1662 int x = row + new_rows;
1664 /* move entries in w_lines[] upwards */
1665 for (;;)
1667 /* stop at last valid entry in w_lines[] */
1668 if (i >= wp->w_lines_valid)
1670 wp->w_lines_valid = j;
1671 break;
1673 wp->w_lines[j] = wp->w_lines[i];
1674 /* stop at a line that won't fit */
1675 if (x + (int)wp->w_lines[j].wl_size
1676 > wp->w_height)
1678 wp->w_lines_valid = j + 1;
1679 break;
1681 x += wp->w_lines[j++].wl_size;
1682 ++i;
1684 if (bot_start > x)
1685 bot_start = x;
1687 else /* j > i */
1689 /* move entries in w_lines[] downwards */
1690 j -= i;
1691 wp->w_lines_valid += j;
1692 if (wp->w_lines_valid > wp->w_height)
1693 wp->w_lines_valid = wp->w_height;
1694 for (i = wp->w_lines_valid; i - j >= idx; --i)
1695 wp->w_lines[i] = wp->w_lines[i - j];
1697 /* The w_lines[] entries for inserted lines are
1698 * now invalid, but wl_size may be used above.
1699 * Reset to zero. */
1700 while (i >= idx)
1702 wp->w_lines[i].wl_size = 0;
1703 wp->w_lines[i--].wl_valid = FALSE;
1710 #ifdef FEAT_FOLDING
1712 * When lines are folded, display one line for all of them.
1713 * Otherwise, display normally (can be several display lines when
1714 * 'wrap' is on).
1716 fold_count = foldedCount(wp, lnum, &win_foldinfo);
1717 if (fold_count != 0)
1719 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
1720 ++row;
1721 --fold_count;
1722 wp->w_lines[idx].wl_folded = TRUE;
1723 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
1724 # ifdef FEAT_SYN_HL
1725 did_update = DID_FOLD;
1726 # endif
1728 else
1729 #endif
1730 if (idx < wp->w_lines_valid
1731 && wp->w_lines[idx].wl_valid
1732 && wp->w_lines[idx].wl_lnum == lnum
1733 && lnum > wp->w_topline
1734 && !(dy_flags & DY_LASTLINE)
1735 && srow + wp->w_lines[idx].wl_size > wp->w_height
1736 #ifdef FEAT_DIFF
1737 && diff_check_fill(wp, lnum) == 0
1738 #endif
1741 /* This line is not going to fit. Don't draw anything here,
1742 * will draw "@ " lines below. */
1743 row = wp->w_height + 1;
1745 else
1747 #ifdef FEAT_SEARCH_EXTRA
1748 prepare_search_hl(wp, lnum);
1749 #endif
1750 #ifdef FEAT_SYN_HL
1751 /* Let the syntax stuff know we skipped a few lines. */
1752 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
1753 && syntax_present(buf))
1754 syntax_end_parsing(syntax_last_parsed + 1);
1755 #endif
1758 * Display one line.
1760 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
1762 #ifdef FEAT_FOLDING
1763 wp->w_lines[idx].wl_folded = FALSE;
1764 wp->w_lines[idx].wl_lastlnum = lnum;
1765 #endif
1766 #ifdef FEAT_SYN_HL
1767 did_update = DID_LINE;
1768 syntax_last_parsed = lnum;
1769 #endif
1772 wp->w_lines[idx].wl_lnum = lnum;
1773 wp->w_lines[idx].wl_valid = TRUE;
1774 if (row > wp->w_height) /* past end of screen */
1776 /* we may need the size of that too long line later on */
1777 if (dollar_vcol == 0)
1778 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
1779 ++idx;
1780 break;
1782 if (dollar_vcol == 0)
1783 wp->w_lines[idx].wl_size = row - srow;
1784 ++idx;
1785 #ifdef FEAT_FOLDING
1786 lnum += fold_count + 1;
1787 #else
1788 ++lnum;
1789 #endif
1791 else
1793 /* This line does not need updating, advance to the next one */
1794 row += wp->w_lines[idx++].wl_size;
1795 if (row > wp->w_height) /* past end of screen */
1796 break;
1797 #ifdef FEAT_FOLDING
1798 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
1799 #else
1800 ++lnum;
1801 #endif
1802 #ifdef FEAT_SYN_HL
1803 did_update = DID_NONE;
1804 #endif
1807 if (lnum > buf->b_ml.ml_line_count)
1809 eof = TRUE;
1810 break;
1814 * End of loop over all window lines.
1818 if (idx > wp->w_lines_valid)
1819 wp->w_lines_valid = idx;
1821 #ifdef FEAT_SYN_HL
1823 * Let the syntax stuff know we stop parsing here.
1825 if (syntax_last_parsed != 0 && syntax_present(buf))
1826 syntax_end_parsing(syntax_last_parsed + 1);
1827 #endif
1830 * If we didn't hit the end of the file, and we didn't finish the last
1831 * line we were working on, then the line didn't fit.
1833 wp->w_empty_rows = 0;
1834 #ifdef FEAT_DIFF
1835 wp->w_filler_rows = 0;
1836 #endif
1837 if (!eof && !didline)
1839 if (lnum == wp->w_topline)
1842 * Single line that does not fit!
1843 * Don't overwrite it, it can be edited.
1845 wp->w_botline = lnum + 1;
1847 #ifdef FEAT_DIFF
1848 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
1850 /* Window ends in filler lines. */
1851 wp->w_botline = lnum;
1852 wp->w_filler_rows = wp->w_height - srow;
1854 #endif
1855 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
1858 * Last line isn't finished: Display "@@@" at the end.
1860 screen_fill(W_WINROW(wp) + wp->w_height - 1,
1861 W_WINROW(wp) + wp->w_height,
1862 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
1863 '@', '@', hl_attr(HLF_AT));
1864 set_empty_rows(wp, srow);
1865 wp->w_botline = lnum;
1867 else
1869 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
1870 wp->w_botline = lnum;
1873 else
1875 #ifdef FEAT_VERTSPLIT
1876 draw_vsep_win(wp, row);
1877 #endif
1878 if (eof) /* we hit the end of the file */
1880 wp->w_botline = buf->b_ml.ml_line_count + 1;
1881 #ifdef FEAT_DIFF
1882 j = diff_check_fill(wp, wp->w_botline);
1883 if (j > 0 && !wp->w_botfill)
1886 * Display filler lines at the end of the file
1888 if (char2cells(fill_diff) > 1)
1889 i = '-';
1890 else
1891 i = fill_diff;
1892 if (row + j > wp->w_height)
1893 j = wp->w_height - row;
1894 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
1895 row += j;
1897 #endif
1899 else if (dollar_vcol == 0)
1900 wp->w_botline = lnum;
1902 /* make sure the rest of the screen is blank */
1903 /* put '~'s on rows that aren't part of the file. */
1904 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_AT);
1907 /* Reset the type of redrawing required, the window has been updated. */
1908 wp->w_redr_type = 0;
1909 #ifdef FEAT_DIFF
1910 wp->w_old_topfill = wp->w_topfill;
1911 wp->w_old_botfill = wp->w_botfill;
1912 #endif
1914 if (dollar_vcol == 0)
1917 * There is a trick with w_botline. If we invalidate it on each
1918 * change that might modify it, this will cause a lot of expensive
1919 * calls to plines() in update_topline() each time. Therefore the
1920 * value of w_botline is often approximated, and this value is used to
1921 * compute the value of w_topline. If the value of w_botline was
1922 * wrong, check that the value of w_topline is correct (cursor is on
1923 * the visible part of the text). If it's not, we need to redraw
1924 * again. Mostly this just means scrolling up a few lines, so it
1925 * doesn't look too bad. Only do this for the current window (where
1926 * changes are relevant).
1928 wp->w_valid |= VALID_BOTLINE;
1929 if (wp == curwin && wp->w_botline != old_botline && !recursive)
1931 recursive = TRUE;
1932 curwin->w_valid &= ~VALID_TOPLINE;
1933 update_topline(); /* may invalidate w_botline again */
1934 if (must_redraw != 0)
1936 /* Don't update for changes in buffer again. */
1937 i = curbuf->b_mod_set;
1938 curbuf->b_mod_set = FALSE;
1939 win_update(curwin);
1940 must_redraw = 0;
1941 curbuf->b_mod_set = i;
1943 recursive = FALSE;
1947 #if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1948 /* restore got_int, unless CTRL-C was hit while redrawing */
1949 if (!got_int)
1950 got_int = save_got_int;
1951 #endif
1954 #ifdef FEAT_SIGNS
1955 static int draw_signcolumn __ARGS((win_T *wp));
1958 * Return TRUE when window "wp" has a column to draw signs in.
1960 static int
1961 draw_signcolumn(wp)
1962 win_T *wp;
1964 return (wp->w_buffer->b_signlist != NULL
1965 # ifdef FEAT_NETBEANS_INTG
1966 || usingNetbeans
1967 # endif
1970 #endif
1973 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
1974 * as the filler character.
1976 static void
1977 win_draw_end(wp, c1, c2, row, endrow, hl)
1978 win_T *wp;
1979 int c1;
1980 int c2;
1981 int row;
1982 int endrow;
1983 hlf_T hl;
1985 #if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
1986 int n = 0;
1987 # define FDC_OFF n
1988 #else
1989 # define FDC_OFF 0
1990 #endif
1992 #ifdef FEAT_RIGHTLEFT
1993 if (wp->w_p_rl)
1995 /* No check for cmdline window: should never be right-left. */
1996 # ifdef FEAT_FOLDING
1997 n = wp->w_p_fdc;
1999 if (n > 0)
2001 /* draw the fold column at the right */
2002 if (n > W_WIDTH(wp))
2003 n = W_WIDTH(wp);
2004 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2005 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
2006 ' ', ' ', hl_attr(HLF_FC));
2008 # endif
2009 # ifdef FEAT_SIGNS
2010 if (draw_signcolumn(wp))
2012 int nn = n + 2;
2014 /* draw the sign column left of the fold column */
2015 if (nn > W_WIDTH(wp))
2016 nn = W_WIDTH(wp);
2017 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2018 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
2019 ' ', ' ', hl_attr(HLF_SC));
2020 n = nn;
2022 # endif
2023 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2024 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
2025 c2, c2, hl_attr(hl));
2026 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2027 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
2028 c1, c2, hl_attr(hl));
2030 else
2031 #endif
2033 #ifdef FEAT_CMDWIN
2034 if (cmdwin_type != 0 && wp == curwin)
2036 /* draw the cmdline character in the leftmost column */
2037 n = 1;
2038 if (n > wp->w_width)
2039 n = wp->w_width;
2040 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2041 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
2042 cmdwin_type, ' ', hl_attr(HLF_AT));
2044 #endif
2045 #ifdef FEAT_FOLDING
2046 if (wp->w_p_fdc > 0)
2048 int nn = n + wp->w_p_fdc;
2050 /* draw the fold column at the left */
2051 if (nn > W_WIDTH(wp))
2052 nn = W_WIDTH(wp);
2053 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2054 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2055 ' ', ' ', hl_attr(HLF_FC));
2056 n = nn;
2058 #endif
2059 #ifdef FEAT_SIGNS
2060 if (draw_signcolumn(wp))
2062 int nn = n + 2;
2064 /* draw the sign column after the fold column */
2065 if (nn > W_WIDTH(wp))
2066 nn = W_WIDTH(wp);
2067 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2068 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2069 ' ', ' ', hl_attr(HLF_SC));
2070 n = nn;
2072 #endif
2073 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2074 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
2075 c1, c2, hl_attr(hl));
2077 set_empty_rows(wp, row);
2080 #ifdef FEAT_FOLDING
2082 * Display one folded line.
2084 static void
2085 fold_line(wp, fold_count, foldinfo, lnum, row)
2086 win_T *wp;
2087 long fold_count;
2088 foldinfo_T *foldinfo;
2089 linenr_T lnum;
2090 int row;
2092 char_u buf[51];
2093 pos_T *top, *bot;
2094 linenr_T lnume = lnum + fold_count - 1;
2095 int len;
2096 char_u *text;
2097 int fdc;
2098 int col;
2099 int txtcol;
2100 int off = (int)(current_ScreenLine - ScreenLines);
2101 int ri;
2103 /* Build the fold line:
2104 * 1. Add the cmdwin_type for the command-line window
2105 * 2. Add the 'foldcolumn'
2106 * 3. Add the 'number' column
2107 * 4. Compose the text
2108 * 5. Add the text
2109 * 6. set highlighting for the Visual area an other text
2111 col = 0;
2114 * 1. Add the cmdwin_type for the command-line window
2115 * Ignores 'rightleft', this window is never right-left.
2117 #ifdef FEAT_CMDWIN
2118 if (cmdwin_type != 0 && wp == curwin)
2120 ScreenLines[off] = cmdwin_type;
2121 ScreenAttrs[off] = hl_attr(HLF_AT);
2122 #ifdef FEAT_MBYTE
2123 if (enc_utf8)
2124 ScreenLinesUC[off] = 0;
2125 #endif
2126 ++col;
2128 #endif
2131 * 2. Add the 'foldcolumn'
2133 fdc = wp->w_p_fdc;
2134 if (fdc > W_WIDTH(wp) - col)
2135 fdc = W_WIDTH(wp) - col;
2136 if (fdc > 0)
2138 fill_foldcolumn(buf, wp, TRUE, lnum);
2139 #ifdef FEAT_RIGHTLEFT
2140 if (wp->w_p_rl)
2142 int i;
2144 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
2145 hl_attr(HLF_FC));
2146 /* reverse the fold column */
2147 for (i = 0; i < fdc; ++i)
2148 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2150 else
2151 #endif
2152 copy_text_attr(off + col, buf, fdc, hl_attr(HLF_FC));
2153 col += fdc;
2156 #ifdef FEAT_RIGHTLEFT
2157 # define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2158 for (ri = 0; ri < l; ++ri) \
2159 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2160 else \
2161 for (ri = 0; ri < l; ++ri) \
2162 ScreenAttrs[off + (p) + ri] = v
2163 #else
2164 # define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2165 ScreenAttrs[off + (p) + ri] = v
2166 #endif
2168 /* Set all attributes of the 'number' column and the text */
2169 RL_MEMSET(col, hl_attr(HLF_FL), W_WIDTH(wp) - col);
2171 #ifdef FEAT_SIGNS
2172 /* If signs are being displayed, add two spaces. */
2173 if (draw_signcolumn(wp))
2175 len = W_WIDTH(wp) - col;
2176 if (len > 0)
2178 if (len > 2)
2179 len = 2;
2180 # ifdef FEAT_RIGHTLEFT
2181 if (wp->w_p_rl)
2182 /* the line number isn't reversed */
2183 copy_text_attr(off + W_WIDTH(wp) - len - col,
2184 (char_u *)" ", len, hl_attr(HLF_FL));
2185 else
2186 # endif
2187 copy_text_attr(off + col, (char_u *)" ", len, hl_attr(HLF_FL));
2188 col += len;
2191 #endif
2194 * 3. Add the 'number' column
2196 if (wp->w_p_nu)
2198 len = W_WIDTH(wp) - col;
2199 if (len > 0)
2201 int w = number_width(wp);
2203 if (len > w + 1)
2204 len = w + 1;
2205 sprintf((char *)buf, "%*ld ", w, (long)lnum);
2206 #ifdef FEAT_RIGHTLEFT
2207 if (wp->w_p_rl)
2208 /* the line number isn't reversed */
2209 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
2210 hl_attr(HLF_FL));
2211 else
2212 #endif
2213 copy_text_attr(off + col, buf, len, hl_attr(HLF_FL));
2214 col += len;
2219 * 4. Compose the folded-line string with 'foldtext', if set.
2221 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
2223 txtcol = col; /* remember where text starts */
2226 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2227 * Right-left text is put in columns 0 - number-col, normal text is put
2228 * in columns number-col - window-width.
2230 #ifdef FEAT_MBYTE
2231 if (has_mbyte)
2233 int cells;
2234 int u8c, u8cc[MAX_MCO];
2235 int i;
2236 int idx;
2237 int c_len;
2238 char_u *p;
2239 # ifdef FEAT_ARABIC
2240 int prev_c = 0; /* previous Arabic character */
2241 int prev_c1 = 0; /* first composing char for prev_c */
2242 # endif
2244 # ifdef FEAT_RIGHTLEFT
2245 if (wp->w_p_rl)
2246 idx = off;
2247 else
2248 # endif
2249 idx = off + col;
2251 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2252 for (p = text; *p != NUL; )
2254 cells = (*mb_ptr2cells)(p);
2255 c_len = (*mb_ptr2len)(p);
2256 if (col + cells > W_WIDTH(wp)
2257 # ifdef FEAT_RIGHTLEFT
2258 - (wp->w_p_rl ? col : 0)
2259 # endif
2261 break;
2262 ScreenLines[idx] = *p;
2263 if (enc_utf8)
2265 u8c = utfc_ptr2char(p, u8cc);
2266 if (*p < 0x80 && u8cc[0] == 0)
2268 ScreenLinesUC[idx] = 0;
2269 #ifdef FEAT_ARABIC
2270 prev_c = u8c;
2271 #endif
2273 else
2275 #ifdef FEAT_ARABIC
2276 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2278 /* Do Arabic shaping. */
2279 int pc, pc1, nc;
2280 int pcc[MAX_MCO];
2281 int firstbyte = *p;
2283 /* The idea of what is the previous and next
2284 * character depends on 'rightleft'. */
2285 if (wp->w_p_rl)
2287 pc = prev_c;
2288 pc1 = prev_c1;
2289 nc = utf_ptr2char(p + c_len);
2290 prev_c1 = u8cc[0];
2292 else
2294 pc = utfc_ptr2char(p + c_len, pcc);
2295 nc = prev_c;
2296 pc1 = pcc[0];
2298 prev_c = u8c;
2300 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2301 pc, pc1, nc);
2302 ScreenLines[idx] = firstbyte;
2304 else
2305 prev_c = u8c;
2306 #endif
2307 /* Non-BMP character: display as ? or fullwidth ?. */
2308 if (u8c >= 0x10000)
2309 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2310 else
2311 ScreenLinesUC[idx] = u8c;
2312 for (i = 0; i < Screen_mco; ++i)
2314 ScreenLinesC[i][idx] = u8cc[i];
2315 if (u8cc[i] == 0)
2316 break;
2319 if (cells > 1)
2320 ScreenLines[idx + 1] = 0;
2322 else if (cells > 1) /* double-byte character */
2324 if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2325 ScreenLines2[idx] = p[1];
2326 else
2327 ScreenLines[idx + 1] = p[1];
2329 col += cells;
2330 idx += cells;
2331 p += c_len;
2334 else
2335 #endif
2337 len = (int)STRLEN(text);
2338 if (len > W_WIDTH(wp) - col)
2339 len = W_WIDTH(wp) - col;
2340 if (len > 0)
2342 #ifdef FEAT_RIGHTLEFT
2343 if (wp->w_p_rl)
2344 STRNCPY(current_ScreenLine, text, len);
2345 else
2346 #endif
2347 STRNCPY(current_ScreenLine + col, text, len);
2348 col += len;
2352 /* Fill the rest of the line with the fold filler */
2353 #ifdef FEAT_RIGHTLEFT
2354 if (wp->w_p_rl)
2355 col -= txtcol;
2356 #endif
2357 while (col < W_WIDTH(wp)
2358 #ifdef FEAT_RIGHTLEFT
2359 - (wp->w_p_rl ? txtcol : 0)
2360 #endif
2363 #ifdef FEAT_MBYTE
2364 if (enc_utf8)
2366 if (fill_fold >= 0x80)
2368 ScreenLinesUC[off + col] = fill_fold;
2369 ScreenLinesC[0][off + col] = 0;
2371 else
2372 ScreenLinesUC[off + col] = 0;
2374 #endif
2375 ScreenLines[off + col++] = fill_fold;
2378 if (text != buf)
2379 vim_free(text);
2382 * 6. set highlighting for the Visual area an other text.
2383 * If all folded lines are in the Visual area, highlight the line.
2385 #ifdef FEAT_VISUAL
2386 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2388 if (ltoreq(curwin->w_cursor, VIsual))
2390 /* Visual is after curwin->w_cursor */
2391 top = &curwin->w_cursor;
2392 bot = &VIsual;
2394 else
2396 /* Visual is before curwin->w_cursor */
2397 top = &VIsual;
2398 bot = &curwin->w_cursor;
2400 if (lnum >= top->lnum
2401 && lnume <= bot->lnum
2402 && (VIsual_mode != 'v'
2403 || ((lnum > top->lnum
2404 || (lnum == top->lnum
2405 && top->col == 0))
2406 && (lnume < bot->lnum
2407 || (lnume == bot->lnum
2408 && (bot->col - (*p_sel == 'e'))
2409 >= STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
2411 if (VIsual_mode == Ctrl_V)
2413 /* Visual block mode: highlight the chars part of the block */
2414 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2416 if (wp->w_old_cursor_lcol + txtcol < (colnr_T)W_WIDTH(wp))
2417 len = wp->w_old_cursor_lcol;
2418 else
2419 len = W_WIDTH(wp) - txtcol;
2420 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, hl_attr(HLF_V),
2421 len - (int)wp->w_old_cursor_fcol);
2424 else
2426 /* Set all attributes of the text */
2427 RL_MEMSET(txtcol, hl_attr(HLF_V), W_WIDTH(wp) - txtcol);
2431 #endif
2433 #ifdef FEAT_SYN_HL
2434 /* Show 'cursorcolumn' in the fold line. */
2435 if (wp->w_p_cuc && (int)wp->w_virtcol + txtcol < W_WIDTH(wp))
2436 ScreenAttrs[off + wp->w_virtcol + txtcol] = hl_combine_attr(
2437 ScreenAttrs[off + wp->w_virtcol + txtcol], hl_attr(HLF_CUC));
2438 #endif
2440 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2441 (int)W_WIDTH(wp), FALSE);
2444 * Update w_cline_height and w_cline_folded if the cursor line was
2445 * updated (saves a call to plines() later).
2447 if (wp == curwin
2448 && lnum <= curwin->w_cursor.lnum
2449 && lnume >= curwin->w_cursor.lnum)
2451 curwin->w_cline_row = row;
2452 curwin->w_cline_height = 1;
2453 curwin->w_cline_folded = TRUE;
2454 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2459 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2461 static void
2462 copy_text_attr(off, buf, len, attr)
2463 int off;
2464 char_u *buf;
2465 int len;
2466 int attr;
2468 int i;
2470 mch_memmove(ScreenLines + off, buf, (size_t)len);
2471 # ifdef FEAT_MBYTE
2472 if (enc_utf8)
2473 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2474 # endif
2475 for (i = 0; i < len; ++i)
2476 ScreenAttrs[off + i] = attr;
2480 * Fill the foldcolumn at "p" for window "wp".
2481 * Only to be called when 'foldcolumn' > 0.
2483 static void
2484 fill_foldcolumn(p, wp, closed, lnum)
2485 char_u *p;
2486 win_T *wp;
2487 int closed; /* TRUE of FALSE */
2488 linenr_T lnum; /* current line number */
2490 int i = 0;
2491 int level;
2492 int first_level;
2493 int empty;
2495 /* Init to all spaces. */
2496 copy_spaces(p, (size_t)wp->w_p_fdc);
2498 level = win_foldinfo.fi_level;
2499 if (level > 0)
2501 /* If there is only one column put more info in it. */
2502 empty = (wp->w_p_fdc == 1) ? 0 : 1;
2504 /* If the column is too narrow, we start at the lowest level that
2505 * fits and use numbers to indicated the depth. */
2506 first_level = level - wp->w_p_fdc - closed + 1 + empty;
2507 if (first_level < 1)
2508 first_level = 1;
2510 for (i = 0; i + empty < wp->w_p_fdc; ++i)
2512 if (win_foldinfo.fi_lnum == lnum
2513 && first_level + i >= win_foldinfo.fi_low_level)
2514 p[i] = '-';
2515 else if (first_level == 1)
2516 p[i] = '|';
2517 else if (first_level + i <= 9)
2518 p[i] = '0' + first_level + i;
2519 else
2520 p[i] = '>';
2521 if (first_level + i == level)
2522 break;
2525 if (closed)
2526 p[i >= wp->w_p_fdc ? i - 1 : i] = '+';
2528 #endif /* FEAT_FOLDING */
2531 * Display line "lnum" of window 'wp' on the screen.
2532 * Start at row "startrow", stop when "endrow" is reached.
2533 * wp->w_virtcol needs to be valid.
2535 * Return the number of last row the line occupies.
2537 /* ARGSUSED */
2538 static int
2539 win_line(wp, lnum, startrow, endrow, nochange)
2540 win_T *wp;
2541 linenr_T lnum;
2542 int startrow;
2543 int endrow;
2544 int nochange; /* not updating for changed text */
2546 int col; /* visual column on screen */
2547 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2548 int c = 0; /* init for GCC */
2549 long vcol = 0; /* virtual column (for tabs) */
2550 long vcol_prev = -1; /* "vcol" of previous character */
2551 char_u *line; /* current line */
2552 char_u *ptr; /* current position in "line" */
2553 int row; /* row in the window, excl w_winrow */
2554 int screen_row; /* row on the screen, incl w_winrow */
2556 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2557 int n_extra = 0; /* number of extra chars */
2558 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
2559 int c_extra = NUL; /* extra chars, all the same */
2560 int extra_attr = 0; /* attributes when n_extra != 0 */
2561 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2562 displaying lcs_eol at end-of-line */
2563 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2564 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2566 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2567 int saved_n_extra = 0;
2568 char_u *saved_p_extra = NULL;
2569 int saved_c_extra = 0;
2570 int saved_char_attr = 0;
2572 int n_attr = 0; /* chars with special attr */
2573 int saved_attr2 = 0; /* char_attr saved for n_attr */
2574 int n_attr3 = 0; /* chars with overruling special attr */
2575 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2577 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2579 int fromcol, tocol; /* start/end of inverting */
2580 int fromcol_prev = -2; /* start of inverting after cursor */
2581 int noinvcur = FALSE; /* don't invert the cursor */
2582 #ifdef FEAT_VISUAL
2583 pos_T *top, *bot;
2584 #endif
2585 pos_T pos;
2586 long v;
2588 int char_attr = 0; /* attributes for next character */
2589 int attr_pri = FALSE; /* char_attr has priority */
2590 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2591 in this line */
2592 int attr = 0; /* attributes for area highlighting */
2593 int area_attr = 0; /* attributes desired by highlighting */
2594 int search_attr = 0; /* attributes desired by 'hlsearch' */
2595 #ifdef FEAT_SYN_HL
2596 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
2597 int syntax_attr = 0; /* attributes desired by syntax */
2598 int has_syntax = FALSE; /* this buffer has syntax highl. */
2599 int save_did_emsg;
2600 #endif
2601 #ifdef FEAT_SPELL
2602 int has_spell = FALSE; /* this buffer has spell checking */
2603 # define SPWORDLEN 150
2604 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
2605 int nextlinecol = 0; /* column where nextline[] starts */
2606 int nextline_idx = 0; /* index in nextline[] where next line
2607 starts */
2608 int spell_attr = 0; /* attributes desired by spelling */
2609 int word_end = 0; /* last byte with same spell_attr */
2610 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2611 static int checked_col = 0; /* column in "checked_lnum" up to which
2612 * there are no spell errors */
2613 static int cap_col = -1; /* column to check for Cap word */
2614 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
2615 int cur_checked_col = 0; /* checked column for current line */
2616 #endif
2617 int extra_check; /* has syntax or linebreak */
2618 #ifdef FEAT_MBYTE
2619 int multi_attr = 0; /* attributes desired by multibyte */
2620 int mb_l = 1; /* multi-byte byte length */
2621 int mb_c = 0; /* decoded multi-byte character */
2622 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
2623 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
2624 #endif
2625 #ifdef FEAT_DIFF
2626 int filler_lines; /* nr of filler lines to be drawn */
2627 int filler_todo; /* nr of filler lines still to do + 1 */
2628 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
2629 int change_start = MAXCOL; /* first col of changed area */
2630 int change_end = -1; /* last col of changed area */
2631 #endif
2632 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
2633 #ifdef FEAT_LINEBREAK
2634 int need_showbreak = FALSE;
2635 #endif
2636 #if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
2637 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
2638 # define LINE_ATTR
2639 int line_attr = 0; /* atrribute for the whole line */
2640 #endif
2641 #ifdef FEAT_SEARCH_EXTRA
2642 matchitem_T *cur; /* points to the match list */
2643 match_T *shl; /* points to search_hl or a match */
2644 int shl_flag; /* flag to indicate whether search_hl
2645 has been processed or not */
2646 int prevcol_hl_flag; /* flag to indicate whether prevcol
2647 equals startcol of search_hl or one
2648 of the matches */
2649 #endif
2650 #ifdef FEAT_ARABIC
2651 int prev_c = 0; /* previous Arabic character */
2652 int prev_c1 = 0; /* first composing char for prev_c */
2653 #endif
2654 #if defined(LINE_ATTR)
2655 int did_line_attr = 0;
2656 #endif
2658 /* draw_state: items that are drawn in sequence: */
2659 #define WL_START 0 /* nothing done yet */
2660 #ifdef FEAT_CMDWIN
2661 # define WL_CMDLINE WL_START + 1 /* cmdline window column */
2662 #else
2663 # define WL_CMDLINE WL_START
2664 #endif
2665 #ifdef FEAT_FOLDING
2666 # define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
2667 #else
2668 # define WL_FOLD WL_CMDLINE
2669 #endif
2670 #ifdef FEAT_SIGNS
2671 # define WL_SIGN WL_FOLD + 1 /* column for signs */
2672 #else
2673 # define WL_SIGN WL_FOLD /* column for signs */
2674 #endif
2675 #define WL_NR WL_SIGN + 1 /* line number */
2676 #if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
2677 # define WL_SBR WL_NR + 1 /* 'showbreak' or 'diff' */
2678 #else
2679 # define WL_SBR WL_NR
2680 #endif
2681 #define WL_LINE WL_SBR + 1 /* text in the line */
2682 int draw_state = WL_START; /* what to draw next */
2683 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2684 int feedback_col = 0;
2685 int feedback_old_attr = -1;
2686 #endif
2689 if (startrow > endrow) /* past the end already! */
2690 return startrow;
2692 row = startrow;
2693 screen_row = row + W_WINROW(wp);
2696 * To speed up the loop below, set extra_check when there is linebreak,
2697 * trailing white space and/or syntax processing to be done.
2699 #ifdef FEAT_LINEBREAK
2700 extra_check = wp->w_p_lbr;
2701 #else
2702 extra_check = 0;
2703 #endif
2704 #ifdef FEAT_SYN_HL
2705 if (syntax_present(wp->w_buffer) && !wp->w_buffer->b_syn_error)
2707 /* Prepare for syntax highlighting in this line. When there is an
2708 * error, stop syntax highlighting. */
2709 save_did_emsg = did_emsg;
2710 did_emsg = FALSE;
2711 syntax_start(wp, lnum);
2712 if (did_emsg)
2713 wp->w_buffer->b_syn_error = TRUE;
2714 else
2716 did_emsg = save_did_emsg;
2717 has_syntax = TRUE;
2718 extra_check = TRUE;
2721 #endif
2723 #ifdef FEAT_SPELL
2724 if (wp->w_p_spell
2725 && *wp->w_buffer->b_p_spl != NUL
2726 && wp->w_buffer->b_langp.ga_len > 0
2727 && *(char **)(wp->w_buffer->b_langp.ga_data) != NULL)
2729 /* Prepare for spell checking. */
2730 has_spell = TRUE;
2731 extra_check = TRUE;
2733 /* Get the start of the next line, so that words that wrap to the next
2734 * line are found too: "et<line-break>al.".
2735 * Trick: skip a few chars for C/shell/Vim comments */
2736 nextline[SPWORDLEN] = NUL;
2737 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2739 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
2740 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
2743 /* When a word wrapped from the previous line the start of the current
2744 * line is valid. */
2745 if (lnum == checked_lnum)
2746 cur_checked_col = checked_col;
2747 checked_lnum = 0;
2749 /* When there was a sentence end in the previous line may require a
2750 * word starting with capital in this line. In line 1 always check
2751 * the first word. */
2752 if (lnum != capcol_lnum)
2753 cap_col = -1;
2754 if (lnum == 1)
2755 cap_col = 0;
2756 capcol_lnum = 0;
2758 #endif
2761 * handle visual active in this window
2763 fromcol = -10;
2764 tocol = MAXCOL;
2765 #ifdef FEAT_VISUAL
2766 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2768 /* Visual is after curwin->w_cursor */
2769 if (ltoreq(curwin->w_cursor, VIsual))
2771 top = &curwin->w_cursor;
2772 bot = &VIsual;
2774 else /* Visual is before curwin->w_cursor */
2776 top = &VIsual;
2777 bot = &curwin->w_cursor;
2779 if (VIsual_mode == Ctrl_V) /* block mode */
2781 if (lnum >= top->lnum && lnum <= bot->lnum)
2783 fromcol = wp->w_old_cursor_fcol;
2784 tocol = wp->w_old_cursor_lcol;
2787 else /* non-block mode */
2789 if (lnum > top->lnum && lnum <= bot->lnum)
2790 fromcol = 0;
2791 else if (lnum == top->lnum)
2793 if (VIsual_mode == 'V') /* linewise */
2794 fromcol = 0;
2795 else
2797 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
2798 if (gchar_pos(top) == NUL)
2799 tocol = fromcol + 1;
2802 if (VIsual_mode != 'V' && lnum == bot->lnum)
2804 if (*p_sel == 'e' && bot->col == 0
2805 #ifdef FEAT_VIRTUALEDIT
2806 && bot->coladd == 0
2807 #endif
2810 fromcol = -10;
2811 tocol = MAXCOL;
2813 else if (bot->col == MAXCOL)
2814 tocol = MAXCOL;
2815 else
2817 pos = *bot;
2818 if (*p_sel == 'e')
2819 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
2820 else
2822 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
2823 ++tocol;
2829 #ifndef MSDOS
2830 /* Check if the character under the cursor should not be inverted */
2831 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
2832 # ifdef FEAT_GUI
2833 && !gui.in_use
2834 # endif
2836 noinvcur = TRUE;
2837 #endif
2839 /* if inverting in this line set area_highlighting */
2840 if (fromcol >= 0)
2842 area_highlighting = TRUE;
2843 attr = hl_attr(HLF_V);
2844 #if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
2845 if (clip_star.available && !clip_star.owned && clip_isautosel())
2846 attr = hl_attr(HLF_VNC);
2847 #endif
2852 * handle 'incsearch' and ":s///c" highlighting
2854 else
2855 #endif /* FEAT_VISUAL */
2856 if (highlight_match
2857 && wp == curwin
2858 && lnum >= curwin->w_cursor.lnum
2859 && lnum <= curwin->w_cursor.lnum + search_match_lines)
2861 if (lnum == curwin->w_cursor.lnum)
2862 getvcol(curwin, &(curwin->w_cursor),
2863 (colnr_T *)&fromcol, NULL, NULL);
2864 else
2865 fromcol = 0;
2866 if (lnum == curwin->w_cursor.lnum + search_match_lines)
2868 pos.lnum = lnum;
2869 pos.col = search_match_endcol;
2870 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
2872 else
2873 tocol = MAXCOL;
2874 if (fromcol == tocol) /* do at least one character */
2875 tocol = fromcol + 1; /* happens when past end of line */
2876 area_highlighting = TRUE;
2877 attr = hl_attr(HLF_I);
2880 #ifdef FEAT_DIFF
2881 filler_lines = diff_check(wp, lnum);
2882 if (filler_lines < 0)
2884 if (filler_lines == -1)
2886 if (diff_find_change(wp, lnum, &change_start, &change_end))
2887 diff_hlf = HLF_ADD; /* added line */
2888 else if (change_start == 0)
2889 diff_hlf = HLF_TXD; /* changed text */
2890 else
2891 diff_hlf = HLF_CHD; /* changed line */
2893 else
2894 diff_hlf = HLF_ADD; /* added line */
2895 filler_lines = 0;
2896 area_highlighting = TRUE;
2898 if (lnum == wp->w_topline)
2899 filler_lines = wp->w_topfill;
2900 filler_todo = filler_lines;
2901 #endif
2903 #ifdef LINE_ATTR
2904 # ifdef FEAT_SIGNS
2905 /* If this line has a sign with line highlighting set line_attr. */
2906 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
2907 if (v != 0)
2908 line_attr = sign_get_attr((int)v, TRUE);
2909 # endif
2910 # if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
2911 /* Highlight the current line in the quickfix window. */
2912 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
2913 line_attr = hl_attr(HLF_L);
2914 # endif
2915 if (line_attr != 0)
2916 area_highlighting = TRUE;
2917 #endif
2919 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
2920 ptr = line;
2922 #ifdef FEAT_SPELL
2923 if (has_spell)
2925 /* For checking first word with a capital skip white space. */
2926 if (cap_col == 0)
2927 cap_col = (int)(skipwhite(line) - line);
2929 /* To be able to spell-check over line boundaries copy the end of the
2930 * current line into nextline[]. Above the start of the next line was
2931 * copied to nextline[SPWORDLEN]. */
2932 if (nextline[SPWORDLEN] == NUL)
2934 /* No next line or it is empty. */
2935 nextlinecol = MAXCOL;
2936 nextline_idx = 0;
2938 else
2940 v = (long)STRLEN(line);
2941 if (v < SPWORDLEN)
2943 /* Short line, use it completely and append the start of the
2944 * next line. */
2945 nextlinecol = 0;
2946 mch_memmove(nextline, line, (size_t)v);
2947 mch_memmove(nextline + v, nextline + SPWORDLEN,
2948 STRLEN(nextline + SPWORDLEN) + 1);
2949 nextline_idx = v + 1;
2951 else
2953 /* Long line, use only the last SPWORDLEN bytes. */
2954 nextlinecol = v - SPWORDLEN;
2955 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
2956 nextline_idx = SPWORDLEN + 1;
2960 #endif
2962 /* find start of trailing whitespace */
2963 if (wp->w_p_list && lcs_trail)
2965 trailcol = (colnr_T)STRLEN(ptr);
2966 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
2967 --trailcol;
2968 trailcol += (colnr_T) (ptr - line);
2969 extra_check = TRUE;
2973 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
2974 * first character to be displayed.
2976 if (wp->w_p_wrap)
2977 v = wp->w_skipcol;
2978 else
2979 v = wp->w_leftcol;
2980 if (v > 0)
2982 #ifdef FEAT_MBYTE
2983 char_u *prev_ptr = ptr;
2984 #endif
2985 while (vcol < v && *ptr != NUL)
2987 c = win_lbr_chartabsize(wp, ptr, (colnr_T)vcol, NULL);
2988 vcol += c;
2989 #ifdef FEAT_MBYTE
2990 prev_ptr = ptr;
2991 #endif
2992 mb_ptr_adv(ptr);
2995 #ifdef FEAT_VIRTUALEDIT
2996 /* When 'virtualedit' is set the end of the line may be before the
2997 * start of the displayed part. */
2998 if (vcol < v && *ptr == NUL && virtual_active())
2999 vcol = v;
3000 #endif
3002 /* Handle a character that's not completely on the screen: Put ptr at
3003 * that character but skip the first few screen characters. */
3004 if (vcol > v)
3006 vcol -= c;
3007 #ifdef FEAT_MBYTE
3008 ptr = prev_ptr;
3009 #else
3010 --ptr;
3011 #endif
3012 n_skip = v - vcol;
3016 * Adjust for when the inverted text is before the screen,
3017 * and when the start of the inverted text is before the screen.
3019 if (tocol <= vcol)
3020 fromcol = 0;
3021 else if (fromcol >= 0 && fromcol < vcol)
3022 fromcol = vcol;
3024 #ifdef FEAT_LINEBREAK
3025 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3026 if (wp->w_p_wrap)
3027 need_showbreak = TRUE;
3028 #endif
3029 #ifdef FEAT_SPELL
3030 /* When spell checking a word we need to figure out the start of the
3031 * word and if it's badly spelled or not. */
3032 if (has_spell)
3034 int len;
3035 hlf_T spell_hlf = HLF_COUNT;
3037 pos = wp->w_cursor;
3038 wp->w_cursor.lnum = lnum;
3039 wp->w_cursor.col = (colnr_T)(ptr - line);
3040 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
3041 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
3043 /* no bad word found at line start, don't check until end of a
3044 * word */
3045 spell_hlf = HLF_COUNT;
3046 word_end = (int)(spell_to_word_end(ptr, wp->w_buffer) - line + 1);
3048 else
3050 /* bad word found, use attributes until end of word */
3051 word_end = wp->w_cursor.col + len + 1;
3053 /* Turn index into actual attributes. */
3054 if (spell_hlf != HLF_COUNT)
3055 spell_attr = highlight_attr[spell_hlf];
3057 wp->w_cursor = pos;
3059 # ifdef FEAT_SYN_HL
3060 /* Need to restart syntax highlighting for this line. */
3061 if (has_syntax)
3062 syntax_start(wp, lnum);
3063 # endif
3065 #endif
3069 * Correct highlighting for cursor that can't be disabled.
3070 * Avoids having to check this for each character.
3072 if (fromcol >= 0)
3074 if (noinvcur)
3076 if ((colnr_T)fromcol == wp->w_virtcol)
3078 /* highlighting starts at cursor, let it start just after the
3079 * cursor */
3080 fromcol_prev = fromcol;
3081 fromcol = -1;
3083 else if ((colnr_T)fromcol < wp->w_virtcol)
3084 /* restart highlighting after the cursor */
3085 fromcol_prev = wp->w_virtcol;
3087 if (fromcol >= tocol)
3088 fromcol = -1;
3091 #ifdef FEAT_SEARCH_EXTRA
3093 * Handle highlighting the last used search pattern and matches.
3094 * Do this for both search_hl and the match list.
3096 cur = wp->w_match_head;
3097 shl_flag = FALSE;
3098 while (cur != NULL || shl_flag == FALSE)
3100 if (shl_flag == FALSE)
3102 shl = &search_hl;
3103 shl_flag = TRUE;
3105 else
3106 shl = &cur->hl;
3107 shl->startcol = MAXCOL;
3108 shl->endcol = MAXCOL;
3109 shl->attr_cur = 0;
3110 if (shl->rm.regprog != NULL)
3112 v = (long)(ptr - line);
3113 next_search_hl(wp, shl, lnum, (colnr_T)v);
3115 /* Need to get the line again, a multi-line regexp may have made it
3116 * invalid. */
3117 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3118 ptr = line + v;
3120 if (shl->lnum != 0 && shl->lnum <= lnum)
3122 if (shl->lnum == lnum)
3123 shl->startcol = shl->rm.startpos[0].col;
3124 else
3125 shl->startcol = 0;
3126 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3127 - shl->rm.startpos[0].lnum)
3128 shl->endcol = shl->rm.endpos[0].col;
3129 else
3130 shl->endcol = MAXCOL;
3131 /* Highlight one character for an empty match. */
3132 if (shl->startcol == shl->endcol)
3134 #ifdef FEAT_MBYTE
3135 if (has_mbyte && line[shl->endcol] != NUL)
3136 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3137 else
3138 #endif
3139 ++shl->endcol;
3141 if ((long)shl->startcol < v) /* match at leftcol */
3143 shl->attr_cur = shl->attr;
3144 search_attr = shl->attr;
3146 area_highlighting = TRUE;
3149 if (shl != &search_hl && cur != NULL)
3150 cur = cur->next;
3152 #endif
3154 #ifdef FEAT_SYN_HL
3155 /* Cursor line highlighting for 'cursorline'. Not when Visual mode is
3156 * active, because it's not clear what is selected then. */
3157 if (wp->w_p_cul && lnum == wp->w_cursor.lnum && !VIsual_active)
3159 line_attr = hl_attr(HLF_CUL);
3160 area_highlighting = TRUE;
3162 #endif
3164 off = (unsigned)(current_ScreenLine - ScreenLines);
3165 col = 0;
3166 #ifdef FEAT_RIGHTLEFT
3167 if (wp->w_p_rl)
3169 /* Rightleft window: process the text in the normal direction, but put
3170 * it in current_ScreenLine[] from right to left. Start at the
3171 * rightmost column of the window. */
3172 col = W_WIDTH(wp) - 1;
3173 off += col;
3175 #endif
3178 * Repeat for the whole displayed line.
3180 for (;;)
3182 /* Skip this quickly when working on the text. */
3183 if (draw_state != WL_LINE)
3185 #ifdef FEAT_CMDWIN
3186 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3188 draw_state = WL_CMDLINE;
3189 if (cmdwin_type != 0 && wp == curwin)
3191 /* Draw the cmdline character. */
3192 n_extra = 1;
3193 c_extra = cmdwin_type;
3194 char_attr = hl_attr(HLF_AT);
3197 #endif
3199 #ifdef FEAT_FOLDING
3200 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3202 draw_state = WL_FOLD;
3203 if (wp->w_p_fdc > 0)
3205 /* Draw the 'foldcolumn'. */
3206 fill_foldcolumn(extra, wp, FALSE, lnum);
3207 n_extra = wp->w_p_fdc;
3208 p_extra = extra;
3209 p_extra[n_extra] = NUL;
3210 c_extra = NUL;
3211 char_attr = hl_attr(HLF_FC);
3214 #endif
3216 #ifdef FEAT_SIGNS
3217 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3219 draw_state = WL_SIGN;
3220 /* Show the sign column when there are any signs in this
3221 * buffer or when using Netbeans. */
3222 if (draw_signcolumn(wp)
3223 # ifdef FEAT_DIFF
3224 && filler_todo <= 0
3225 # endif
3228 int_u text_sign;
3229 # ifdef FEAT_SIGN_ICONS
3230 int_u icon_sign;
3231 # endif
3233 /* Draw two cells with the sign value or blank. */
3234 c_extra = ' ';
3235 char_attr = hl_attr(HLF_SC);
3236 n_extra = 2;
3238 if (row == startrow)
3240 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3241 SIGN_TEXT);
3242 # ifdef FEAT_SIGN_ICONS
3243 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3244 SIGN_ICON);
3245 if (gui.in_use && icon_sign != 0)
3247 /* Use the image in this position. */
3248 c_extra = SIGN_BYTE;
3249 # ifdef FEAT_NETBEANS_INTG
3250 if (buf_signcount(wp->w_buffer, lnum) > 1)
3251 c_extra = MULTISIGN_BYTE;
3252 # endif
3253 char_attr = icon_sign;
3255 else
3256 # endif
3257 if (text_sign != 0)
3259 p_extra = sign_get_text(text_sign);
3260 if (p_extra != NULL)
3262 c_extra = NUL;
3263 n_extra = (int)STRLEN(p_extra);
3265 char_attr = sign_get_attr(text_sign, FALSE);
3270 #endif
3272 if (draw_state == WL_NR - 1 && n_extra == 0)
3274 draw_state = WL_NR;
3275 /* Display the line number. After the first fill with blanks
3276 * when the 'n' flag isn't in 'cpo' */
3277 if (wp->w_p_nu
3278 && (row == startrow
3279 #ifdef FEAT_DIFF
3280 + filler_lines
3281 #endif
3282 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3284 /* Draw the line number (empty space after wrapping). */
3285 if (row == startrow
3286 #ifdef FEAT_DIFF
3287 + filler_lines
3288 #endif
3291 sprintf((char *)extra, "%*ld ",
3292 number_width(wp), (long)lnum);
3293 if (wp->w_skipcol > 0)
3294 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3295 *p_extra = '-';
3296 #ifdef FEAT_RIGHTLEFT
3297 if (wp->w_p_rl) /* reverse line numbers */
3298 rl_mirror(extra);
3299 #endif
3300 p_extra = extra;
3301 c_extra = NUL;
3303 else
3304 c_extra = ' ';
3305 n_extra = number_width(wp) + 1;
3306 char_attr = hl_attr(HLF_N);
3307 #ifdef FEAT_SYN_HL
3308 /* When 'cursorline' is set highlight the line number of
3309 * the current line differently. */
3310 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3311 char_attr = hl_combine_attr(hl_attr(HLF_CUL), char_attr);
3312 #endif
3316 #if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3317 if (draw_state == WL_SBR - 1 && n_extra == 0)
3319 draw_state = WL_SBR;
3320 # ifdef FEAT_DIFF
3321 if (filler_todo > 0)
3323 /* Draw "deleted" diff line(s). */
3324 if (char2cells(fill_diff) > 1)
3325 c_extra = '-';
3326 else
3327 c_extra = fill_diff;
3328 # ifdef FEAT_RIGHTLEFT
3329 if (wp->w_p_rl)
3330 n_extra = col + 1;
3331 else
3332 # endif
3333 n_extra = W_WIDTH(wp) - col;
3334 char_attr = hl_attr(HLF_DED);
3336 # endif
3337 # ifdef FEAT_LINEBREAK
3338 if (*p_sbr != NUL && need_showbreak)
3340 /* Draw 'showbreak' at the start of each broken line. */
3341 p_extra = p_sbr;
3342 c_extra = NUL;
3343 n_extra = (int)STRLEN(p_sbr);
3344 char_attr = hl_attr(HLF_AT);
3345 need_showbreak = FALSE;
3346 /* Correct end of highlighted area for 'showbreak',
3347 * required when 'linebreak' is also set. */
3348 if (tocol == vcol)
3349 tocol += n_extra;
3351 # endif
3353 #endif
3355 if (draw_state == WL_LINE - 1 && n_extra == 0)
3357 draw_state = WL_LINE;
3358 if (saved_n_extra)
3360 /* Continue item from end of wrapped line. */
3361 n_extra = saved_n_extra;
3362 c_extra = saved_c_extra;
3363 p_extra = saved_p_extra;
3364 char_attr = saved_char_attr;
3366 else
3367 char_attr = 0;
3371 /* When still displaying '$' of change command, stop at cursor */
3372 if (dollar_vcol != 0 && wp == curwin
3373 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
3374 #ifdef FEAT_DIFF
3375 && filler_todo <= 0
3376 #endif
3379 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3380 wp->w_p_rl);
3381 /* Pretend we have finished updating the window. Except when
3382 * 'cursorcolumn' is set. */
3383 #ifdef FEAT_SYN_HL
3384 if (wp->w_p_cuc)
3385 row = wp->w_cline_row + wp->w_cline_height;
3386 else
3387 #endif
3388 row = wp->w_height;
3389 break;
3392 if (draw_state == WL_LINE && area_highlighting)
3394 /* handle Visual or match highlighting in this line */
3395 if (vcol == fromcol
3396 #ifdef FEAT_MBYTE
3397 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3398 && (*mb_ptr2cells)(ptr) > 1)
3399 #endif
3400 || ((int)vcol_prev == fromcol_prev
3401 && vcol < tocol))
3402 area_attr = attr; /* start highlighting */
3403 else if (area_attr != 0
3404 && (vcol == tocol
3405 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
3406 area_attr = 0; /* stop highlighting */
3408 #ifdef FEAT_SEARCH_EXTRA
3409 if (!n_extra)
3412 * Check for start/end of search pattern match.
3413 * After end, check for start/end of next match.
3414 * When another match, have to check for start again.
3415 * Watch out for matching an empty string!
3416 * Do this for 'search_hl' and the match list (ordered by
3417 * priority).
3419 v = (long)(ptr - line);
3420 cur = wp->w_match_head;
3421 shl_flag = FALSE;
3422 while (cur != NULL || shl_flag == FALSE)
3424 if (shl_flag == FALSE
3425 && ((cur != NULL
3426 && cur->priority > SEARCH_HL_PRIORITY)
3427 || cur == NULL))
3429 shl = &search_hl;
3430 shl_flag = TRUE;
3432 else
3433 shl = &cur->hl;
3434 while (shl->rm.regprog != NULL)
3436 if (shl->startcol != MAXCOL
3437 && v >= (long)shl->startcol
3438 && v < (long)shl->endcol)
3440 shl->attr_cur = shl->attr;
3442 else if (v == (long)shl->endcol)
3444 shl->attr_cur = 0;
3446 next_search_hl(wp, shl, lnum, (colnr_T)v);
3448 /* Need to get the line again, a multi-line regexp
3449 * may have made it invalid. */
3450 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3451 ptr = line + v;
3453 if (shl->lnum == lnum)
3455 shl->startcol = shl->rm.startpos[0].col;
3456 if (shl->rm.endpos[0].lnum == 0)
3457 shl->endcol = shl->rm.endpos[0].col;
3458 else
3459 shl->endcol = MAXCOL;
3461 if (shl->startcol == shl->endcol)
3463 /* highlight empty match, try again after
3464 * it */
3465 #ifdef FEAT_MBYTE
3466 if (has_mbyte)
3467 shl->endcol += (*mb_ptr2len)(line
3468 + shl->endcol);
3469 else
3470 #endif
3471 ++shl->endcol;
3474 /* Loop to check if the match starts at the
3475 * current position */
3476 continue;
3479 break;
3481 if (shl != &search_hl && cur != NULL)
3482 cur = cur->next;
3485 /* Use attributes from match with highest priority among
3486 * 'search_hl' and the match list. */
3487 search_attr = search_hl.attr_cur;
3488 cur = wp->w_match_head;
3489 shl_flag = FALSE;
3490 while (cur != NULL || shl_flag == FALSE)
3492 if (shl_flag == FALSE
3493 && ((cur != NULL
3494 && cur->priority > SEARCH_HL_PRIORITY)
3495 || cur == NULL))
3497 shl = &search_hl;
3498 shl_flag = TRUE;
3500 else
3501 shl = &cur->hl;
3502 if (shl->attr_cur != 0)
3503 search_attr = shl->attr_cur;
3504 if (shl != &search_hl && cur != NULL)
3505 cur = cur->next;
3508 #endif
3510 #ifdef FEAT_DIFF
3511 if (diff_hlf != (hlf_T)0)
3513 if (diff_hlf == HLF_CHD && ptr - line >= change_start
3514 && n_extra == 0)
3515 diff_hlf = HLF_TXD; /* changed text */
3516 if (diff_hlf == HLF_TXD && ptr - line > change_end
3517 && n_extra == 0)
3518 diff_hlf = HLF_CHD; /* changed line */
3519 line_attr = hl_attr(diff_hlf);
3521 #endif
3523 /* Decide which of the highlight attributes to use. */
3524 attr_pri = TRUE;
3525 if (area_attr != 0)
3526 char_attr = area_attr;
3527 else if (search_attr != 0)
3528 char_attr = search_attr;
3529 #ifdef LINE_ATTR
3530 /* Use line_attr when not in the Visual or 'incsearch' area
3531 * (area_attr may be 0 when "noinvcur" is set). */
3532 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
3533 || (vcol < fromcol || vcol >= tocol)))
3534 char_attr = line_attr;
3535 #endif
3536 else
3538 attr_pri = FALSE;
3539 #ifdef FEAT_SYN_HL
3540 if (has_syntax)
3541 char_attr = syntax_attr;
3542 else
3543 #endif
3544 char_attr = 0;
3549 * Get the next character to put on the screen.
3552 * The "p_extra" points to the extra stuff that is inserted to
3553 * represent special characters (non-printable stuff) and other
3554 * things. When all characters are the same, c_extra is used.
3555 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
3556 * "p_extra[n_extra]".
3557 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
3559 if (n_extra > 0)
3561 if (c_extra != NUL)
3563 c = c_extra;
3564 #ifdef FEAT_MBYTE
3565 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
3566 if (enc_utf8 && (*mb_char2len)(c) > 1)
3568 mb_utf8 = TRUE;
3569 u8cc[0] = 0;
3570 c = 0xc0;
3572 else
3573 mb_utf8 = FALSE;
3574 #endif
3576 else
3578 c = *p_extra;
3579 #ifdef FEAT_MBYTE
3580 if (has_mbyte)
3582 mb_c = c;
3583 if (enc_utf8)
3585 /* If the UTF-8 character is more than one byte:
3586 * Decode it into "mb_c". */
3587 mb_l = (*mb_ptr2len)(p_extra);
3588 mb_utf8 = FALSE;
3589 if (mb_l > n_extra)
3590 mb_l = 1;
3591 else if (mb_l > 1)
3593 mb_c = utfc_ptr2char(p_extra, u8cc);
3594 mb_utf8 = TRUE;
3595 c = 0xc0;
3598 else
3600 /* if this is a DBCS character, put it in "mb_c" */
3601 mb_l = MB_BYTE2LEN(c);
3602 if (mb_l >= n_extra)
3603 mb_l = 1;
3604 else if (mb_l > 1)
3605 mb_c = (c << 8) + p_extra[1];
3607 if (mb_l == 0) /* at the NUL at end-of-line */
3608 mb_l = 1;
3610 /* If a double-width char doesn't fit display a '>' in the
3611 * last column. */
3612 if ((
3613 # ifdef FEAT_RIGHTLEFT
3614 wp->w_p_rl ? (col <= 0) :
3615 # endif
3616 (col >= W_WIDTH(wp) - 1))
3617 && (*mb_char2cells)(mb_c) == 2)
3619 c = '>';
3620 mb_c = c;
3621 mb_l = 1;
3622 mb_utf8 = FALSE;
3623 multi_attr = hl_attr(HLF_AT);
3624 /* put the pointer back to output the double-width
3625 * character at the start of the next line. */
3626 ++n_extra;
3627 --p_extra;
3629 else
3631 n_extra -= mb_l - 1;
3632 p_extra += mb_l - 1;
3635 #endif
3636 ++p_extra;
3638 --n_extra;
3640 else
3643 * Get a character from the line itself.
3645 c = *ptr;
3646 #ifdef FEAT_MBYTE
3647 if (has_mbyte)
3649 mb_c = c;
3650 if (enc_utf8)
3652 /* If the UTF-8 character is more than one byte: Decode it
3653 * into "mb_c". */
3654 mb_l = (*mb_ptr2len)(ptr);
3655 mb_utf8 = FALSE;
3656 if (mb_l > 1)
3658 mb_c = utfc_ptr2char(ptr, u8cc);
3659 /* Overlong encoded ASCII or ASCII with composing char
3660 * is displayed normally, except a NUL. */
3661 if (mb_c < 0x80)
3662 c = mb_c;
3663 mb_utf8 = TRUE;
3665 /* At start of the line we can have a composing char.
3666 * Draw it as a space with a composing char. */
3667 if (utf_iscomposing(mb_c))
3669 int i;
3671 for (i = Screen_mco - 1; i > 0; --i)
3672 u8cc[i] = u8cc[i - 1];
3673 u8cc[0] = mb_c;
3674 mb_c = ' ';
3678 if ((mb_l == 1 && c >= 0x80)
3679 || (mb_l >= 1 && mb_c == 0)
3680 || (mb_l > 1 && (!vim_isprintc(mb_c)
3681 || mb_c >= 0x10000)))
3684 * Illegal UTF-8 byte: display as <xx>.
3685 * Non-BMP character : display as ? or fullwidth ?.
3687 if (mb_c < 0x10000)
3689 transchar_hex(extra, mb_c);
3690 # ifdef FEAT_RIGHTLEFT
3691 if (wp->w_p_rl) /* reverse */
3692 rl_mirror(extra);
3693 # endif
3695 else if (utf_char2cells(mb_c) != 2)
3696 STRCPY(extra, "?");
3697 else
3698 /* 0xff1f in UTF-8: full-width '?' */
3699 STRCPY(extra, "\357\274\237");
3701 p_extra = extra;
3702 c = *p_extra;
3703 mb_c = mb_ptr2char_adv(&p_extra);
3704 mb_utf8 = (c >= 0x80);
3705 n_extra = (int)STRLEN(p_extra);
3706 c_extra = NUL;
3707 if (area_attr == 0 && search_attr == 0)
3709 n_attr = n_extra + 1;
3710 extra_attr = hl_attr(HLF_8);
3711 saved_attr2 = char_attr; /* save current attr */
3714 else if (mb_l == 0) /* at the NUL at end-of-line */
3715 mb_l = 1;
3716 #ifdef FEAT_ARABIC
3717 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
3719 /* Do Arabic shaping. */
3720 int pc, pc1, nc;
3721 int pcc[MAX_MCO];
3723 /* The idea of what is the previous and next
3724 * character depends on 'rightleft'. */
3725 if (wp->w_p_rl)
3727 pc = prev_c;
3728 pc1 = prev_c1;
3729 nc = utf_ptr2char(ptr + mb_l);
3730 prev_c1 = u8cc[0];
3732 else
3734 pc = utfc_ptr2char(ptr + mb_l, pcc);
3735 nc = prev_c;
3736 pc1 = pcc[0];
3738 prev_c = mb_c;
3740 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
3742 else
3743 prev_c = mb_c;
3744 #endif
3746 else /* enc_dbcs */
3748 mb_l = MB_BYTE2LEN(c);
3749 if (mb_l == 0) /* at the NUL at end-of-line */
3750 mb_l = 1;
3751 else if (mb_l > 1)
3753 /* We assume a second byte below 32 is illegal.
3754 * Hopefully this is OK for all double-byte encodings!
3756 if (ptr[1] >= 32)
3757 mb_c = (c << 8) + ptr[1];
3758 else
3760 if (ptr[1] == NUL)
3762 /* head byte at end of line */
3763 mb_l = 1;
3764 transchar_nonprint(extra, c);
3766 else
3768 /* illegal tail byte */
3769 mb_l = 2;
3770 STRCPY(extra, "XX");
3772 p_extra = extra;
3773 n_extra = (int)STRLEN(extra) - 1;
3774 c_extra = NUL;
3775 c = *p_extra++;
3776 if (area_attr == 0 && search_attr == 0)
3778 n_attr = n_extra + 1;
3779 extra_attr = hl_attr(HLF_8);
3780 saved_attr2 = char_attr; /* save current attr */
3782 mb_c = c;
3786 /* If a double-width char doesn't fit display a '>' in the
3787 * last column; the character is displayed at the start of the
3788 * next line. */
3789 if ((
3790 # ifdef FEAT_RIGHTLEFT
3791 wp->w_p_rl ? (col <= 0) :
3792 # endif
3793 (col >= W_WIDTH(wp) - 1))
3794 && (*mb_char2cells)(mb_c) == 2)
3796 c = '>';
3797 mb_c = c;
3798 mb_utf8 = FALSE;
3799 mb_l = 1;
3800 multi_attr = hl_attr(HLF_AT);
3801 /* Put pointer back so that the character will be
3802 * displayed at the start of the next line. */
3803 --ptr;
3805 else if (*ptr != NUL)
3806 ptr += mb_l - 1;
3808 /* If a double-width char doesn't fit at the left side display
3809 * a '<' in the first column. */
3810 if (n_skip > 0 && mb_l > 1)
3812 n_extra = 1;
3813 c_extra = '<';
3814 c = ' ';
3815 if (area_attr == 0 && search_attr == 0)
3817 n_attr = n_extra + 1;
3818 extra_attr = hl_attr(HLF_AT);
3819 saved_attr2 = char_attr; /* save current attr */
3821 mb_c = c;
3822 mb_utf8 = FALSE;
3823 mb_l = 1;
3827 #endif
3828 ++ptr;
3830 /* 'list' : change char 160 to lcs_nbsp. */
3831 if (wp->w_p_list && (c == 160
3832 #ifdef FEAT_MBYTE
3833 || (mb_utf8 && mb_c == 160)
3834 #endif
3835 ) && lcs_nbsp)
3837 c = lcs_nbsp;
3838 if (area_attr == 0 && search_attr == 0)
3840 n_attr = 1;
3841 extra_attr = hl_attr(HLF_8);
3842 saved_attr2 = char_attr; /* save current attr */
3844 #ifdef FEAT_MBYTE
3845 mb_c = c;
3846 if (enc_utf8 && (*mb_char2len)(c) > 1)
3848 mb_utf8 = TRUE;
3849 u8cc[0] = 0;
3850 c = 0xc0;
3852 else
3853 mb_utf8 = FALSE;
3854 #endif
3857 if (extra_check)
3859 #ifdef FEAT_SPELL
3860 int can_spell = TRUE;
3861 #endif
3863 #ifdef FEAT_SYN_HL
3864 /* Get syntax attribute, unless still at the start of the line
3865 * (double-wide char that doesn't fit). */
3866 v = (long)(ptr - line);
3867 if (has_syntax && v > 0)
3869 /* Get the syntax attribute for the character. If there
3870 * is an error, disable syntax highlighting. */
3871 save_did_emsg = did_emsg;
3872 did_emsg = FALSE;
3874 syntax_attr = get_syntax_attr((colnr_T)v - 1,
3875 # ifdef FEAT_SPELL
3876 has_spell ? &can_spell :
3877 # endif
3878 NULL);
3880 if (did_emsg)
3882 wp->w_buffer->b_syn_error = TRUE;
3883 has_syntax = FALSE;
3885 else
3886 did_emsg = save_did_emsg;
3888 /* Need to get the line again, a multi-line regexp may
3889 * have made it invalid. */
3890 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3891 ptr = line + v;
3893 if (!attr_pri)
3894 char_attr = syntax_attr;
3895 else
3896 char_attr = hl_combine_attr(syntax_attr, char_attr);
3898 #endif
3900 #ifdef FEAT_SPELL
3901 /* Check spelling (unless at the end of the line).
3902 * Only do this when there is no syntax highlighting, the
3903 * @Spell cluster is not used or the current syntax item
3904 * contains the @Spell cluster. */
3905 if (has_spell && v >= word_end && v > cur_checked_col)
3907 spell_attr = 0;
3908 # ifdef FEAT_SYN_HL
3909 if (!attr_pri)
3910 char_attr = syntax_attr;
3911 # endif
3912 if (c != 0 && (
3913 # ifdef FEAT_SYN_HL
3914 !has_syntax ||
3915 # endif
3916 can_spell))
3918 char_u *prev_ptr, *p;
3919 int len;
3920 hlf_T spell_hlf = HLF_COUNT;
3921 # ifdef FEAT_MBYTE
3922 if (has_mbyte)
3924 prev_ptr = ptr - mb_l;
3925 v -= mb_l - 1;
3927 else
3928 # endif
3929 prev_ptr = ptr - 1;
3931 /* Use nextline[] if possible, it has the start of the
3932 * next line concatenated. */
3933 if ((prev_ptr - line) - nextlinecol >= 0)
3934 p = nextline + (prev_ptr - line) - nextlinecol;
3935 else
3936 p = prev_ptr;
3937 cap_col -= (int)(prev_ptr - line);
3938 len = spell_check(wp, p, &spell_hlf, &cap_col,
3939 nochange);
3940 word_end = v + len;
3942 /* In Insert mode only highlight a word that
3943 * doesn't touch the cursor. */
3944 if (spell_hlf != HLF_COUNT
3945 && (State & INSERT) != 0
3946 && wp->w_cursor.lnum == lnum
3947 && wp->w_cursor.col >=
3948 (colnr_T)(prev_ptr - line)
3949 && wp->w_cursor.col < (colnr_T)word_end)
3951 spell_hlf = HLF_COUNT;
3952 spell_redraw_lnum = lnum;
3955 if (spell_hlf == HLF_COUNT && p != prev_ptr
3956 && (p - nextline) + len > nextline_idx)
3958 /* Remember that the good word continues at the
3959 * start of the next line. */
3960 checked_lnum = lnum + 1;
3961 checked_col = (int)((p - nextline) + len - nextline_idx);
3964 /* Turn index into actual attributes. */
3965 if (spell_hlf != HLF_COUNT)
3966 spell_attr = highlight_attr[spell_hlf];
3968 if (cap_col > 0)
3970 if (p != prev_ptr
3971 && (p - nextline) + cap_col >= nextline_idx)
3973 /* Remember that the word in the next line
3974 * must start with a capital. */
3975 capcol_lnum = lnum + 1;
3976 cap_col = (int)((p - nextline) + cap_col
3977 - nextline_idx);
3979 else
3980 /* Compute the actual column. */
3981 cap_col += (int)(prev_ptr - line);
3985 if (spell_attr != 0)
3987 if (!attr_pri)
3988 char_attr = hl_combine_attr(char_attr, spell_attr);
3989 else
3990 char_attr = hl_combine_attr(spell_attr, char_attr);
3992 #endif
3993 #ifdef FEAT_LINEBREAK
3995 * Found last space before word: check for line break.
3997 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr)
3998 && !wp->w_p_list)
4000 n_extra = win_lbr_chartabsize(wp, ptr - (
4001 # ifdef FEAT_MBYTE
4002 has_mbyte ? mb_l :
4003 # endif
4004 1), (colnr_T)vcol, NULL) - 1;
4005 c_extra = ' ';
4006 if (vim_iswhite(c))
4007 c = ' ';
4009 #endif
4011 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4013 c = lcs_trail;
4014 if (!attr_pri)
4016 n_attr = 1;
4017 extra_attr = hl_attr(HLF_8);
4018 saved_attr2 = char_attr; /* save current attr */
4020 #ifdef FEAT_MBYTE
4021 mb_c = c;
4022 if (enc_utf8 && (*mb_char2len)(c) > 1)
4024 mb_utf8 = TRUE;
4025 u8cc[0] = 0;
4026 c = 0xc0;
4028 else
4029 mb_utf8 = FALSE;
4030 #endif
4035 * Handling of non-printable characters.
4037 if (!(chartab[c & 0xff] & CT_PRINT_CHAR))
4040 * when getting a character from the file, we may have to
4041 * turn it into something else on the way to putting it
4042 * into "ScreenLines".
4044 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4046 /* tab amount depends on current column */
4047 n_extra = (int)wp->w_buffer->b_p_ts
4048 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4049 #ifdef FEAT_MBYTE
4050 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4051 #endif
4052 if (wp->w_p_list)
4054 c = lcs_tab1;
4055 c_extra = lcs_tab2;
4056 n_attr = n_extra + 1;
4057 extra_attr = hl_attr(HLF_8);
4058 saved_attr2 = char_attr; /* save current attr */
4059 #ifdef FEAT_MBYTE
4060 mb_c = c;
4061 if (enc_utf8 && (*mb_char2len)(c) > 1)
4063 mb_utf8 = TRUE;
4064 u8cc[0] = 0;
4065 c = 0xc0;
4067 #endif
4069 else
4071 c_extra = ' ';
4072 c = ' ';
4075 else if (c == NUL
4076 && ((wp->w_p_list && lcs_eol > 0)
4077 || ((fromcol >= 0 || fromcol_prev >= 0)
4078 && tocol > vcol
4079 #ifdef FEAT_VISUAL
4080 && VIsual_mode != Ctrl_V
4081 #endif
4082 && (
4083 # ifdef FEAT_RIGHTLEFT
4084 wp->w_p_rl ? (col >= 0) :
4085 # endif
4086 (col < W_WIDTH(wp)))
4087 && !(noinvcur
4088 && (colnr_T)vcol == wp->w_virtcol)))
4089 && lcs_eol_one >= 0)
4091 /* Display a '$' after the line or highlight an extra
4092 * character if the line break is included. */
4093 #if defined(FEAT_DIFF) || defined(LINE_ATTR)
4094 /* For a diff line the highlighting continues after the
4095 * "$". */
4096 if (
4097 # ifdef FEAT_DIFF
4098 diff_hlf == (hlf_T)0
4099 # ifdef LINE_ATTR
4101 # endif
4102 # endif
4103 # ifdef LINE_ATTR
4104 line_attr == 0
4105 # endif
4107 #endif
4109 #ifdef FEAT_VIRTUALEDIT
4110 /* In virtualedit, visual selections may extend
4111 * beyond end of line. */
4112 if (area_highlighting && virtual_active()
4113 && tocol != MAXCOL && vcol < tocol)
4114 n_extra = 0;
4115 else
4116 #endif
4118 p_extra = at_end_str;
4119 n_extra = 1;
4120 c_extra = NUL;
4123 if (wp->w_p_list)
4124 c = lcs_eol;
4125 else
4126 c = ' ';
4127 lcs_eol_one = -1;
4128 --ptr; /* put it back at the NUL */
4129 if (!attr_pri)
4131 extra_attr = hl_attr(HLF_AT);
4132 n_attr = 1;
4134 #ifdef FEAT_MBYTE
4135 mb_c = c;
4136 if (enc_utf8 && (*mb_char2len)(c) > 1)
4138 mb_utf8 = TRUE;
4139 u8cc[0] = 0;
4140 c = 0xc0;
4142 else
4143 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4144 #endif
4146 else if (c != NUL)
4148 p_extra = transchar(c);
4149 #ifdef FEAT_RIGHTLEFT
4150 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4151 rl_mirror(p_extra); /* reverse "<12>" */
4152 #endif
4153 n_extra = byte2cells(c) - 1;
4154 c_extra = NUL;
4155 c = *p_extra++;
4156 if (!attr_pri)
4158 n_attr = n_extra + 1;
4159 extra_attr = hl_attr(HLF_8);
4160 saved_attr2 = char_attr; /* save current attr */
4162 #ifdef FEAT_MBYTE
4163 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4164 #endif
4166 #ifdef FEAT_VIRTUALEDIT
4167 else if (VIsual_active
4168 && (VIsual_mode == Ctrl_V
4169 || VIsual_mode == 'v')
4170 && virtual_active()
4171 && tocol != MAXCOL
4172 && vcol < tocol
4173 && (
4174 # ifdef FEAT_RIGHTLEFT
4175 wp->w_p_rl ? (col >= 0) :
4176 # endif
4177 (col < W_WIDTH(wp))))
4179 c = ' ';
4180 --ptr; /* put it back at the NUL */
4182 #endif
4183 #if defined(LINE_ATTR)
4184 else if ((
4185 # ifdef FEAT_DIFF
4186 diff_hlf != (hlf_T)0 ||
4187 # endif
4188 line_attr != 0
4189 ) && (
4190 # ifdef FEAT_RIGHTLEFT
4191 wp->w_p_rl ? (col >= 0) :
4192 # endif
4193 (col < W_WIDTH(wp))))
4195 /* Highlight until the right side of the window */
4196 c = ' ';
4197 --ptr; /* put it back at the NUL */
4199 /* Remember we do the char for line highlighting. */
4200 ++did_line_attr;
4202 /* don't do search HL for the rest of the line */
4203 if (line_attr != 0 && char_attr == search_attr && col > 0)
4204 char_attr = line_attr;
4205 # ifdef FEAT_DIFF
4206 if (diff_hlf == HLF_TXD)
4208 diff_hlf = HLF_CHD;
4209 if (attr == 0 || char_attr != attr)
4210 char_attr = hl_attr(diff_hlf);
4212 # endif
4214 #endif
4218 /* Don't override visual selection highlighting. */
4219 if (n_attr > 0
4220 && draw_state == WL_LINE
4221 && !attr_pri)
4222 char_attr = extra_attr;
4224 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
4225 /* XIM don't send preedit_start and preedit_end, but they send
4226 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
4227 * im_is_preediting() here. */
4228 if (xic != NULL
4229 && lnum == curwin->w_cursor.lnum
4230 && (State & INSERT)
4231 && !p_imdisable
4232 && im_is_preediting()
4233 && draw_state == WL_LINE)
4235 colnr_T tcol;
4237 if (preedit_end_col == MAXCOL)
4238 getvcol(curwin, &(curwin->w_cursor), &tcol, NULL, NULL);
4239 else
4240 tcol = preedit_end_col;
4241 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
4243 if (feedback_old_attr < 0)
4245 feedback_col = 0;
4246 feedback_old_attr = char_attr;
4248 char_attr = im_get_feedback_attr(feedback_col);
4249 if (char_attr < 0)
4250 char_attr = feedback_old_attr;
4251 feedback_col++;
4253 else if (feedback_old_attr >= 0)
4255 char_attr = feedback_old_attr;
4256 feedback_old_attr = -1;
4257 feedback_col = 0;
4260 #endif
4262 * Handle the case where we are in column 0 but not on the first
4263 * character of the line and the user wants us to show us a
4264 * special character (via 'listchars' option "precedes:<char>".
4266 if (lcs_prec_todo != NUL
4267 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
4268 #ifdef FEAT_DIFF
4269 && filler_todo <= 0
4270 #endif
4271 && draw_state > WL_NR
4272 && c != NUL)
4274 c = lcs_prec;
4275 lcs_prec_todo = NUL;
4276 #ifdef FEAT_MBYTE
4277 mb_c = c;
4278 if (enc_utf8 && (*mb_char2len)(c) > 1)
4280 mb_utf8 = TRUE;
4281 u8cc[0] = 0;
4282 c = 0xc0;
4284 else
4285 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4286 #endif
4287 if (!attr_pri)
4289 saved_attr3 = char_attr; /* save current attr */
4290 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
4291 n_attr3 = 1;
4296 * At end of the text line or just after the last character.
4298 if (c == NUL
4299 #if defined(LINE_ATTR)
4300 || did_line_attr == 1
4301 #endif
4304 #ifdef FEAT_SEARCH_EXTRA
4305 long prevcol = (long)(ptr - line) - (c == NUL);
4306 #endif
4308 /* invert at least one char, used for Visual and empty line or
4309 * highlight match at end of line. If it's beyond the last
4310 * char on the screen, just overwrite that one (tricky!) Not
4311 * needed when a '$' was displayed for 'list'. */
4312 #ifdef FEAT_SEARCH_EXTRA
4313 prevcol_hl_flag = FALSE;
4314 if (prevcol == (long)search_hl.startcol)
4315 prevcol_hl_flag = TRUE;
4316 else
4318 cur = wp->w_match_head;
4319 while (cur != NULL)
4321 if (prevcol == (long)cur->hl.startcol)
4323 prevcol_hl_flag = TRUE;
4324 break;
4326 cur = cur->next;
4329 #endif
4330 if (lcs_eol == lcs_eol_one
4331 && ((area_attr != 0 && vcol == fromcol && c == NUL)
4332 #ifdef FEAT_SEARCH_EXTRA
4333 /* highlight 'hlsearch' match at end of line */
4334 || (prevcol_hl_flag == TRUE
4335 # if defined(LINE_ATTR)
4336 && did_line_attr <= 1
4337 # endif
4339 #endif
4342 int n = 0;
4344 #ifdef FEAT_RIGHTLEFT
4345 if (wp->w_p_rl)
4347 if (col < 0)
4348 n = 1;
4350 else
4351 #endif
4353 if (col >= W_WIDTH(wp))
4354 n = -1;
4356 if (n != 0)
4358 /* At the window boundary, highlight the last character
4359 * instead (better than nothing). */
4360 off += n;
4361 col += n;
4363 else
4365 /* Add a blank character to highlight. */
4366 ScreenLines[off] = ' ';
4367 #ifdef FEAT_MBYTE
4368 if (enc_utf8)
4369 ScreenLinesUC[off] = 0;
4370 #endif
4372 #ifdef FEAT_SEARCH_EXTRA
4373 if (area_attr == 0)
4375 /* Use attributes from match with highest priority among
4376 * 'search_hl' and the match list. */
4377 char_attr = search_hl.attr;
4378 cur = wp->w_match_head;
4379 shl_flag = FALSE;
4380 while (cur != NULL || shl_flag == FALSE)
4382 if (shl_flag == FALSE
4383 && ((cur != NULL
4384 && cur->priority > SEARCH_HL_PRIORITY)
4385 || cur == NULL))
4387 shl = &search_hl;
4388 shl_flag = TRUE;
4390 else
4391 shl = &cur->hl;
4392 if ((ptr - line) - 1 == (long)shl->startcol)
4393 char_attr = shl->attr;
4394 if (shl != &search_hl && cur != NULL)
4395 cur = cur->next;
4398 #endif
4399 ScreenAttrs[off] = char_attr;
4400 #ifdef FEAT_RIGHTLEFT
4401 if (wp->w_p_rl)
4402 --col;
4403 else
4404 #endif
4405 ++col;
4406 ++vcol;
4411 * At end of the text line.
4413 if (c == NUL)
4415 #ifdef FEAT_SYN_HL
4416 /* Highlight 'cursorcolumn' past end of the line. */
4417 if (wp->w_p_wrap)
4418 v = wp->w_skipcol;
4419 else
4420 v = wp->w_leftcol;
4421 /* check if line ends before left margin */
4422 if (vcol < v + col - win_col_off(wp))
4424 vcol = v + col - win_col_off(wp);
4425 if (wp->w_p_cuc
4426 && (int)wp->w_virtcol >= vcol
4427 && (int)wp->w_virtcol < W_WIDTH(wp) * (row - startrow + 1)
4429 && lnum != wp->w_cursor.lnum
4430 # ifdef FEAT_RIGHTLEFT
4431 && !wp->w_p_rl
4432 # endif
4435 while (col < W_WIDTH(wp))
4437 ScreenLines[off] = ' ';
4438 #ifdef FEAT_MBYTE
4439 if (enc_utf8)
4440 ScreenLinesUC[off] = 0;
4441 #endif
4442 ++col;
4443 if (vcol == (long)wp->w_virtcol)
4445 ScreenAttrs[off] = hl_attr(HLF_CUC);
4446 break;
4448 ScreenAttrs[off++] = 0;
4449 ++vcol;
4452 #endif
4454 SCREEN_LINE(screen_row, W_WINCOL(wp), col, (int)W_WIDTH(wp),
4455 wp->w_p_rl);
4456 row++;
4459 * Update w_cline_height and w_cline_folded if the cursor line was
4460 * updated (saves a call to plines() later).
4462 if (wp == curwin && lnum == curwin->w_cursor.lnum)
4464 curwin->w_cline_row = startrow;
4465 curwin->w_cline_height = row - startrow;
4466 #ifdef FEAT_FOLDING
4467 curwin->w_cline_folded = FALSE;
4468 #endif
4469 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
4472 break;
4475 /* line continues beyond line end */
4476 if (lcs_ext
4477 && !wp->w_p_wrap
4478 #ifdef FEAT_DIFF
4479 && filler_todo <= 0
4480 #endif
4481 && (
4482 #ifdef FEAT_RIGHTLEFT
4483 wp->w_p_rl ? col == 0 :
4484 #endif
4485 col == W_WIDTH(wp) - 1)
4486 && (*ptr != NUL
4487 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
4488 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
4490 c = lcs_ext;
4491 char_attr = hl_attr(HLF_AT);
4492 #ifdef FEAT_MBYTE
4493 mb_c = c;
4494 if (enc_utf8 && (*mb_char2len)(c) > 1)
4496 mb_utf8 = TRUE;
4497 u8cc[0] = 0;
4498 c = 0xc0;
4500 else
4501 mb_utf8 = FALSE;
4502 #endif
4505 #ifdef FEAT_SYN_HL
4506 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
4507 * highlight the cursor position itself. */
4508 if (wp->w_p_cuc && vcol == (long)wp->w_virtcol
4509 && lnum != wp->w_cursor.lnum
4510 && draw_state == WL_LINE)
4512 vcol_save_attr = char_attr;
4513 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
4515 else
4516 vcol_save_attr = -1;
4517 #endif
4520 * Store character to be displayed.
4521 * Skip characters that are left of the screen for 'nowrap'.
4523 vcol_prev = vcol;
4524 if (draw_state < WL_LINE || n_skip <= 0)
4527 * Store the character.
4529 #if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
4530 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
4532 /* A double-wide character is: put first halve in left cell. */
4533 --off;
4534 --col;
4536 #endif
4537 ScreenLines[off] = c;
4538 #ifdef FEAT_MBYTE
4539 if (enc_dbcs == DBCS_JPNU)
4540 ScreenLines2[off] = mb_c & 0xff;
4541 else if (enc_utf8)
4543 if (mb_utf8)
4545 int i;
4547 ScreenLinesUC[off] = mb_c;
4548 if ((c & 0xff) == 0)
4549 ScreenLines[off] = 0x80; /* avoid storing zero */
4550 for (i = 0; i < Screen_mco; ++i)
4552 ScreenLinesC[i][off] = u8cc[i];
4553 if (u8cc[i] == 0)
4554 break;
4557 else
4558 ScreenLinesUC[off] = 0;
4560 if (multi_attr)
4562 ScreenAttrs[off] = multi_attr;
4563 multi_attr = 0;
4565 else
4566 #endif
4567 ScreenAttrs[off] = char_attr;
4569 #ifdef FEAT_MBYTE
4570 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
4572 /* Need to fill two screen columns. */
4573 ++off;
4574 ++col;
4575 if (enc_utf8)
4576 /* UTF-8: Put a 0 in the second screen char. */
4577 ScreenLines[off] = 0;
4578 else
4579 /* DBCS: Put second byte in the second screen char. */
4580 ScreenLines[off] = mb_c & 0xff;
4581 ++vcol;
4582 /* When "tocol" is halfway a character, set it to the end of
4583 * the character, otherwise highlighting won't stop. */
4584 if (tocol == vcol)
4585 ++tocol;
4586 #ifdef FEAT_RIGHTLEFT
4587 if (wp->w_p_rl)
4589 /* now it's time to backup one cell */
4590 --off;
4591 --col;
4593 #endif
4595 #endif
4596 #ifdef FEAT_RIGHTLEFT
4597 if (wp->w_p_rl)
4599 --off;
4600 --col;
4602 else
4603 #endif
4605 ++off;
4606 ++col;
4609 else
4610 --n_skip;
4612 /* Only advance the "vcol" when after the 'number' column. */
4613 if (draw_state >= WL_SBR
4614 #ifdef FEAT_DIFF
4615 && filler_todo <= 0
4616 #endif
4618 ++vcol;
4620 #ifdef FEAT_SYN_HL
4621 if (vcol_save_attr >= 0)
4622 char_attr = vcol_save_attr;
4623 #endif
4625 /* restore attributes after "predeces" in 'listchars' */
4626 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
4627 char_attr = saved_attr3;
4629 /* restore attributes after last 'listchars' or 'number' char */
4630 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
4631 char_attr = saved_attr2;
4634 * At end of screen line and there is more to come: Display the line
4635 * so far. If there is no more to display it is catched above.
4637 if ((
4638 #ifdef FEAT_RIGHTLEFT
4639 wp->w_p_rl ? (col < 0) :
4640 #endif
4641 (col >= W_WIDTH(wp)))
4642 && (*ptr != NUL
4643 #ifdef FEAT_DIFF
4644 || filler_todo > 0
4645 #endif
4646 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
4647 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
4650 SCREEN_LINE(screen_row, W_WINCOL(wp), col, (int)W_WIDTH(wp),
4651 wp->w_p_rl);
4652 ++row;
4653 ++screen_row;
4655 /* When not wrapping and finished diff lines, or when displayed
4656 * '$' and highlighting until last column, break here. */
4657 if ((!wp->w_p_wrap
4658 #ifdef FEAT_DIFF
4659 && filler_todo <= 0
4660 #endif
4661 ) || lcs_eol_one == -1)
4662 break;
4664 /* When the window is too narrow draw all "@" lines. */
4665 if (draw_state != WL_LINE
4666 #ifdef FEAT_DIFF
4667 && filler_todo <= 0
4668 #endif
4671 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
4672 #ifdef FEAT_VERTSPLIT
4673 draw_vsep_win(wp, row);
4674 #endif
4675 row = endrow;
4678 /* When line got too long for screen break here. */
4679 if (row == endrow)
4681 ++row;
4682 break;
4685 if (screen_cur_row == screen_row - 1
4686 #ifdef FEAT_DIFF
4687 && filler_todo <= 0
4688 #endif
4689 && W_WIDTH(wp) == Columns)
4691 /* Remember that the line wraps, used for modeless copy. */
4692 LineWraps[screen_row - 1] = TRUE;
4695 * Special trick to make copy/paste of wrapped lines work with
4696 * xterm/screen: write an extra character beyond the end of
4697 * the line. This will work with all terminal types
4698 * (regardless of the xn,am settings).
4699 * Only do this on a fast tty.
4700 * Only do this if the cursor is on the current line
4701 * (something has been written in it).
4702 * Don't do this for the GUI.
4703 * Don't do this for double-width characters.
4704 * Don't do this for a window not at the right screen border.
4706 if (p_tf
4707 #ifdef FEAT_GUI
4708 && !gui.in_use
4709 #endif
4710 #ifdef FEAT_MBYTE
4711 && !(has_mbyte
4712 && ((*mb_off2cells)(LineOffset[screen_row]) == 2
4713 || (*mb_off2cells)(LineOffset[screen_row - 1]
4714 + (int)Columns - 2) == 2))
4715 #endif
4718 /* First make sure we are at the end of the screen line,
4719 * then output the same character again to let the
4720 * terminal know about the wrap. If the terminal doesn't
4721 * auto-wrap, we overwrite the character. */
4722 if (screen_cur_col != W_WIDTH(wp))
4723 screen_char(LineOffset[screen_row - 1]
4724 + (unsigned)Columns - 1,
4725 screen_row - 1, (int)(Columns - 1));
4727 #ifdef FEAT_MBYTE
4728 /* When there is a multi-byte character, just output a
4729 * space to keep it simple. */
4730 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
4731 screen_row - 1] + (Columns - 1)]) > 1)
4732 out_char(' ');
4733 else
4734 #endif
4735 out_char(ScreenLines[LineOffset[screen_row - 1]
4736 + (Columns - 1)]);
4737 /* force a redraw of the first char on the next line */
4738 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
4739 screen_start(); /* don't know where cursor is now */
4743 col = 0;
4744 off = (unsigned)(current_ScreenLine - ScreenLines);
4745 #ifdef FEAT_RIGHTLEFT
4746 if (wp->w_p_rl)
4748 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
4749 off += col;
4751 #endif
4753 /* reset the drawing state for the start of a wrapped line */
4754 draw_state = WL_START;
4755 saved_n_extra = n_extra;
4756 saved_p_extra = p_extra;
4757 saved_c_extra = c_extra;
4758 saved_char_attr = char_attr;
4759 n_extra = 0;
4760 lcs_prec_todo = lcs_prec;
4761 #ifdef FEAT_LINEBREAK
4762 # ifdef FEAT_DIFF
4763 if (filler_todo <= 0)
4764 # endif
4765 need_showbreak = TRUE;
4766 #endif
4767 #ifdef FEAT_DIFF
4768 --filler_todo;
4769 /* When the filler lines are actually below the last line of the
4770 * file, don't draw the line itself, break here. */
4771 if (filler_todo == 0 && wp->w_botfill)
4772 break;
4773 #endif
4776 } /* for every character in the line */
4778 #ifdef FEAT_SPELL
4779 /* After an empty line check first word for capital. */
4780 if (*skipwhite(line) == NUL)
4782 capcol_lnum = lnum + 1;
4783 cap_col = 0;
4785 #endif
4787 return row;
4790 #ifdef FEAT_MBYTE
4791 static int comp_char_differs __ARGS((int, int));
4794 * Return if the composing characters at "off_from" and "off_to" differ.
4796 static int
4797 comp_char_differs(off_from, off_to)
4798 int off_from;
4799 int off_to;
4801 int i;
4803 for (i = 0; i < Screen_mco; ++i)
4805 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
4806 return TRUE;
4807 if (ScreenLinesC[i][off_from] == 0)
4808 break;
4810 return FALSE;
4812 #endif
4815 * Check whether the given character needs redrawing:
4816 * - the (first byte of the) character is different
4817 * - the attributes are different
4818 * - the character is multi-byte and the next byte is different
4820 static int
4821 char_needs_redraw(off_from, off_to, cols)
4822 int off_from;
4823 int off_to;
4824 int cols;
4826 if (cols > 0
4827 && ((ScreenLines[off_from] != ScreenLines[off_to]
4828 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
4830 #ifdef FEAT_MBYTE
4831 || (enc_dbcs != 0
4832 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
4833 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
4834 ? ScreenLines2[off_from] != ScreenLines2[off_to]
4835 : (cols > 1 && ScreenLines[off_from + 1]
4836 != ScreenLines[off_to + 1])))
4837 || (enc_utf8
4838 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
4839 || (ScreenLinesUC[off_from] != 0
4840 && comp_char_differs(off_from, off_to))))
4841 #endif
4843 return TRUE;
4844 return FALSE;
4848 * Move one "cooked" screen line to the screen, but only the characters that
4849 * have actually changed. Handle insert/delete character.
4850 * "coloff" gives the first column on the screen for this line.
4851 * "endcol" gives the columns where valid characters are.
4852 * "clear_width" is the width of the window. It's > 0 if the rest of the line
4853 * needs to be cleared, negative otherwise.
4854 * "rlflag" is TRUE in a rightleft window:
4855 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
4856 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
4858 static void
4859 screen_line(row, coloff, endcol, clear_width
4860 #ifdef FEAT_RIGHTLEFT
4861 , rlflag
4862 #endif
4864 int row;
4865 int coloff;
4866 int endcol;
4867 int clear_width;
4868 #ifdef FEAT_RIGHTLEFT
4869 int rlflag;
4870 #endif
4872 unsigned off_from;
4873 unsigned off_to;
4874 int col = 0;
4875 #if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_VERTSPLIT)
4876 int hl;
4877 #endif
4878 int force = FALSE; /* force update rest of the line */
4879 int redraw_this /* bool: does character need redraw? */
4880 #ifdef FEAT_GUI
4881 = TRUE /* For GUI when while-loop empty */
4882 #endif
4884 int redraw_next; /* redraw_this for next character */
4885 #ifdef FEAT_MBYTE
4886 int clear_next = FALSE;
4887 int char_cells; /* 1: normal char */
4888 /* 2: occupies two display cells */
4889 # define CHAR_CELLS char_cells
4890 #else
4891 # define CHAR_CELLS 1
4892 #endif
4894 # ifdef FEAT_CLIPBOARD
4895 clip_may_clear_selection(row, row);
4896 # endif
4898 off_from = (unsigned)(current_ScreenLine - ScreenLines);
4899 off_to = LineOffset[row] + coloff;
4901 #ifdef FEAT_RIGHTLEFT
4902 if (rlflag)
4904 /* Clear rest first, because it's left of the text. */
4905 if (clear_width > 0)
4907 while (col <= endcol && ScreenLines[off_to] == ' '
4908 && ScreenAttrs[off_to] == 0
4909 # ifdef FEAT_MBYTE
4910 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
4911 # endif
4914 ++off_to;
4915 ++col;
4917 if (col <= endcol)
4918 screen_fill(row, row + 1, col + coloff,
4919 endcol + coloff + 1, ' ', ' ', 0);
4921 col = endcol + 1;
4922 off_to = LineOffset[row] + col + coloff;
4923 off_from += col;
4924 endcol = (clear_width > 0 ? clear_width : -clear_width);
4926 #endif /* FEAT_RIGHTLEFT */
4928 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
4930 while (col < endcol)
4932 #ifdef FEAT_MBYTE
4933 if (has_mbyte && (col + 1 < endcol))
4934 char_cells = (*mb_off2cells)(off_from);
4935 else
4936 char_cells = 1;
4937 #endif
4939 redraw_this = redraw_next;
4940 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
4941 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
4943 #ifdef FEAT_GUI
4944 /* If the next character was bold, then redraw the current character to
4945 * remove any pixels that might have spilt over into us. This only
4946 * happens in the GUI.
4948 if (redraw_next && gui.in_use)
4950 hl = ScreenAttrs[off_to + CHAR_CELLS];
4951 if (hl > HL_ALL)
4952 hl = syn_attr2attr(hl);
4953 if (hl & HL_BOLD)
4954 redraw_this = TRUE;
4956 #endif
4958 if (redraw_this)
4961 * Special handling when 'xs' termcap flag set (hpterm):
4962 * Attributes for characters are stored at the position where the
4963 * cursor is when writing the highlighting code. The
4964 * start-highlighting code must be written with the cursor on the
4965 * first highlighted character. The stop-highlighting code must
4966 * be written with the cursor just after the last highlighted
4967 * character.
4968 * Overwriting a character doesn't remove it's highlighting. Need
4969 * to clear the rest of the line, and force redrawing it
4970 * completely.
4972 if ( p_wiv
4973 && !force
4974 #ifdef FEAT_GUI
4975 && !gui.in_use
4976 #endif
4977 && ScreenAttrs[off_to] != 0
4978 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
4981 * Need to remove highlighting attributes here.
4983 windgoto(row, col + coloff);
4984 out_str(T_CE); /* clear rest of this screen line */
4985 screen_start(); /* don't know where cursor is now */
4986 force = TRUE; /* force redraw of rest of the line */
4987 redraw_next = TRUE; /* or else next char would miss out */
4990 * If the previous character was highlighted, need to stop
4991 * highlighting at this character.
4993 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
4995 screen_attr = ScreenAttrs[off_to - 1];
4996 term_windgoto(row, col + coloff);
4997 screen_stop_highlight();
4999 else
5000 screen_attr = 0; /* highlighting has stopped */
5002 #ifdef FEAT_MBYTE
5003 if (enc_dbcs != 0)
5005 /* Check if overwriting a double-byte with a single-byte or
5006 * the other way around requires another character to be
5007 * redrawn. For UTF-8 this isn't needed, because comparing
5008 * ScreenLinesUC[] is sufficient. */
5009 if (char_cells == 1
5010 && col + 1 < endcol
5011 && (*mb_off2cells)(off_to) > 1)
5013 /* Writing a single-cell character over a double-cell
5014 * character: need to redraw the next cell. */
5015 ScreenLines[off_to + 1] = 0;
5016 redraw_next = TRUE;
5018 else if (char_cells == 2
5019 && col + 2 < endcol
5020 && (*mb_off2cells)(off_to) == 1
5021 && (*mb_off2cells)(off_to + 1) > 1)
5023 /* Writing the second half of a double-cell character over
5024 * a double-cell character: need to redraw the second
5025 * cell. */
5026 ScreenLines[off_to + 2] = 0;
5027 redraw_next = TRUE;
5030 if (enc_dbcs == DBCS_JPNU)
5031 ScreenLines2[off_to] = ScreenLines2[off_from];
5033 /* When writing a single-width character over a double-width
5034 * character and at the end of the redrawn text, need to clear out
5035 * the right halve of the old character.
5036 * Also required when writing the right halve of a double-width
5037 * char over the left halve of an existing one. */
5038 if (has_mbyte && col + char_cells == endcol
5039 && ((char_cells == 1
5040 && (*mb_off2cells)(off_to) > 1)
5041 || (char_cells == 2
5042 && (*mb_off2cells)(off_to) == 1
5043 && (*mb_off2cells)(off_to + 1) > 1)))
5044 clear_next = TRUE;
5045 #endif
5047 ScreenLines[off_to] = ScreenLines[off_from];
5048 #ifdef FEAT_MBYTE
5049 if (enc_utf8)
5051 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
5052 if (ScreenLinesUC[off_from] != 0)
5054 int i;
5056 for (i = 0; i < Screen_mco; ++i)
5057 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
5060 if (char_cells == 2)
5061 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
5062 #endif
5064 #if defined(FEAT_GUI) || defined(UNIX)
5065 /* The bold trick makes a single row of pixels appear in the next
5066 * character. When a bold character is removed, the next
5067 * character should be redrawn too. This happens for our own GUI
5068 * and for some xterms. */
5069 if (
5070 # ifdef FEAT_GUI
5071 gui.in_use
5072 # endif
5073 # if defined(FEAT_GUI) && defined(UNIX)
5075 # endif
5076 # ifdef UNIX
5077 term_is_xterm
5078 # endif
5081 hl = ScreenAttrs[off_to];
5082 if (hl > HL_ALL)
5083 hl = syn_attr2attr(hl);
5084 if (hl & HL_BOLD)
5085 redraw_next = TRUE;
5087 #endif
5088 ScreenAttrs[off_to] = ScreenAttrs[off_from];
5089 #ifdef FEAT_MBYTE
5090 /* For simplicity set the attributes of second half of a
5091 * double-wide character equal to the first half. */
5092 if (char_cells == 2)
5093 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
5095 if (enc_dbcs != 0 && char_cells == 2)
5096 screen_char_2(off_to, row, col + coloff);
5097 else
5098 #endif
5099 screen_char(off_to, row, col + coloff);
5101 else if ( p_wiv
5102 #ifdef FEAT_GUI
5103 && !gui.in_use
5104 #endif
5105 && col + coloff > 0)
5107 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
5110 * Don't output stop-highlight when moving the cursor, it will
5111 * stop the highlighting when it should continue.
5113 screen_attr = 0;
5115 else if (screen_attr != 0)
5116 screen_stop_highlight();
5119 off_to += CHAR_CELLS;
5120 off_from += CHAR_CELLS;
5121 col += CHAR_CELLS;
5124 #ifdef FEAT_MBYTE
5125 if (clear_next)
5127 /* Clear the second half of a double-wide character of which the left
5128 * half was overwritten with a single-wide character. */
5129 ScreenLines[off_to] = ' ';
5130 if (enc_utf8)
5131 ScreenLinesUC[off_to] = 0;
5132 screen_char(off_to, row, col + coloff);
5134 #endif
5136 if (clear_width > 0
5137 #ifdef FEAT_RIGHTLEFT
5138 && !rlflag
5139 #endif
5142 #ifdef FEAT_GUI
5143 int startCol = col;
5144 #endif
5146 /* blank out the rest of the line */
5147 while (col < clear_width && ScreenLines[off_to] == ' '
5148 && ScreenAttrs[off_to] == 0
5149 #ifdef FEAT_MBYTE
5150 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5151 #endif
5154 ++off_to;
5155 ++col;
5157 if (col < clear_width)
5159 #ifdef FEAT_GUI
5161 * In the GUI, clearing the rest of the line may leave pixels
5162 * behind if the first character cleared was bold. Some bold
5163 * fonts spill over the left. In this case we redraw the previous
5164 * character too. If we didn't skip any blanks above, then we
5165 * only redraw if the character wasn't already redrawn anyway.
5167 if (gui.in_use && (col > startCol || !redraw_this))
5169 hl = ScreenAttrs[off_to];
5170 if (hl > HL_ALL || (hl & HL_BOLD))
5172 int prev_cells = 1;
5173 # ifdef FEAT_MBYTE
5174 if (enc_utf8)
5175 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
5176 * that its width is 2. */
5177 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
5178 else if (enc_dbcs != 0)
5180 /* find previous character by counting from first
5181 * column and get its width. */
5182 unsigned off = LineOffset[row];
5184 while (off < off_to)
5186 prev_cells = (*mb_off2cells)(off);
5187 off += prev_cells;
5191 if (enc_dbcs != 0 && prev_cells > 1)
5192 screen_char_2(off_to - prev_cells, row,
5193 col + coloff - prev_cells);
5194 else
5195 # endif
5196 screen_char(off_to - prev_cells, row,
5197 col + coloff - prev_cells);
5200 #endif
5201 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
5202 ' ', ' ', 0);
5203 #ifdef FEAT_VERTSPLIT
5204 off_to += clear_width - col;
5205 col = clear_width;
5206 #endif
5210 if (clear_width > 0)
5212 #ifdef FEAT_VERTSPLIT
5213 /* For a window that's left of another, draw the separator char. */
5214 if (col + coloff < Columns)
5216 int c;
5218 c = fillchar_vsep(&hl);
5219 if (ScreenLines[off_to] != c
5220 # ifdef FEAT_MBYTE
5221 || (enc_utf8 && (int)ScreenLinesUC[off_to]
5222 != (c >= 0x80 ? c : 0))
5223 # endif
5224 || ScreenAttrs[off_to] != hl)
5226 ScreenLines[off_to] = c;
5227 ScreenAttrs[off_to] = hl;
5228 # ifdef FEAT_MBYTE
5229 if (enc_utf8)
5231 if (c >= 0x80)
5233 ScreenLinesUC[off_to] = c;
5234 ScreenLinesC[0][off_to] = 0;
5236 else
5237 ScreenLinesUC[off_to] = 0;
5239 # endif
5240 screen_char(off_to, row, col + coloff);
5243 else
5244 #endif
5245 LineWraps[row] = FALSE;
5249 #if defined(FEAT_RIGHTLEFT) || defined(PROTO)
5251 * Mirror text "str" for right-left displaying.
5252 * Only works for single-byte characters (e.g., numbers).
5254 void
5255 rl_mirror(str)
5256 char_u *str;
5258 char_u *p1, *p2;
5259 int t;
5261 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
5263 t = *p1;
5264 *p1 = *p2;
5265 *p2 = t;
5268 #endif
5270 #if defined(FEAT_WINDOWS) || defined(PROTO)
5272 * mark all status lines for redraw; used after first :cd
5274 void
5275 status_redraw_all()
5277 win_T *wp;
5279 for (wp = firstwin; wp; wp = wp->w_next)
5280 if (wp->w_status_height)
5282 wp->w_redr_status = TRUE;
5283 redraw_later(VALID);
5288 * mark all status lines of the current buffer for redraw
5290 void
5291 status_redraw_curbuf()
5293 win_T *wp;
5295 for (wp = firstwin; wp; wp = wp->w_next)
5296 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
5298 wp->w_redr_status = TRUE;
5299 redraw_later(VALID);
5304 * Redraw all status lines that need to be redrawn.
5306 void
5307 redraw_statuslines()
5309 win_T *wp;
5311 for (wp = firstwin; wp; wp = wp->w_next)
5312 if (wp->w_redr_status)
5313 win_redr_status(wp);
5314 if (redraw_tabline)
5315 draw_tabline();
5317 #endif
5319 #if (defined(FEAT_WILDMENU) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
5321 * Redraw all status lines at the bottom of frame "frp".
5323 void
5324 win_redraw_last_status(frp)
5325 frame_T *frp;
5327 if (frp->fr_layout == FR_LEAF)
5328 frp->fr_win->w_redr_status = TRUE;
5329 else if (frp->fr_layout == FR_ROW)
5331 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
5332 win_redraw_last_status(frp);
5334 else /* frp->fr_layout == FR_COL */
5336 frp = frp->fr_child;
5337 while (frp->fr_next != NULL)
5338 frp = frp->fr_next;
5339 win_redraw_last_status(frp);
5342 #endif
5344 #ifdef FEAT_VERTSPLIT
5346 * Draw the verticap separator right of window "wp" starting with line "row".
5348 static void
5349 draw_vsep_win(wp, row)
5350 win_T *wp;
5351 int row;
5353 int hl;
5354 int c;
5356 if (wp->w_vsep_width)
5358 /* draw the vertical separator right of this window */
5359 c = fillchar_vsep(&hl);
5360 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
5361 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
5362 c, ' ', hl);
5365 #endif
5367 #ifdef FEAT_WILDMENU
5368 static int status_match_len __ARGS((expand_T *xp, char_u *s));
5369 static int skip_status_match_char __ARGS((expand_T *xp, char_u *s));
5372 * Get the lenght of an item as it will be shown in the status line.
5374 static int
5375 status_match_len(xp, s)
5376 expand_T *xp;
5377 char_u *s;
5379 int len = 0;
5381 #ifdef FEAT_MENU
5382 int emenu = (xp->xp_context == EXPAND_MENUS
5383 || xp->xp_context == EXPAND_MENUNAMES);
5385 /* Check for menu separators - replace with '|'. */
5386 if (emenu && menu_is_separator(s))
5387 return 1;
5388 #endif
5390 while (*s != NUL)
5392 if (skip_status_match_char(xp, s))
5393 ++s;
5394 len += ptr2cells(s);
5395 mb_ptr_adv(s);
5398 return len;
5402 * Return TRUE for characters that are not displayed in a status match.
5403 * These are backslashes used for escaping. Do show backslashes in help tags.
5405 static int
5406 skip_status_match_char(xp, s)
5407 expand_T *xp;
5408 char_u *s;
5410 return ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
5411 #ifdef FEAT_MENU
5412 || ((xp->xp_context == EXPAND_MENUS
5413 || xp->xp_context == EXPAND_MENUNAMES)
5414 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
5415 #endif
5420 * Show wildchar matches in the status line.
5421 * Show at least the "match" item.
5422 * We start at item 'first_match' in the list and show all matches that fit.
5424 * If inversion is possible we use it. Else '=' characters are used.
5426 void
5427 win_redr_status_matches(xp, num_matches, matches, match, showtail)
5428 expand_T *xp;
5429 int num_matches;
5430 char_u **matches; /* list of matches */
5431 int match;
5432 int showtail;
5434 #define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
5435 int row;
5436 char_u *buf;
5437 int len;
5438 int clen; /* lenght in screen cells */
5439 int fillchar;
5440 int attr;
5441 int i;
5442 int highlight = TRUE;
5443 char_u *selstart = NULL;
5444 int selstart_col = 0;
5445 char_u *selend = NULL;
5446 static int first_match = 0;
5447 int add_left = FALSE;
5448 char_u *s;
5449 #ifdef FEAT_MENU
5450 int emenu;
5451 #endif
5452 #if defined(FEAT_MBYTE) || defined(FEAT_MENU)
5453 int l;
5454 #endif
5456 if (matches == NULL) /* interrupted completion? */
5457 return;
5459 #ifdef FEAT_MBYTE
5460 if (has_mbyte)
5461 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
5462 else
5463 #endif
5464 buf = alloc((unsigned)Columns + 1);
5465 if (buf == NULL)
5466 return;
5468 if (match == -1) /* don't show match but original text */
5470 match = 0;
5471 highlight = FALSE;
5473 /* count 1 for the ending ">" */
5474 clen = status_match_len(xp, L_MATCH(match)) + 3;
5475 if (match == 0)
5476 first_match = 0;
5477 else if (match < first_match)
5479 /* jumping left, as far as we can go */
5480 first_match = match;
5481 add_left = TRUE;
5483 else
5485 /* check if match fits on the screen */
5486 for (i = first_match; i < match; ++i)
5487 clen += status_match_len(xp, L_MATCH(i)) + 2;
5488 if (first_match > 0)
5489 clen += 2;
5490 /* jumping right, put match at the left */
5491 if ((long)clen > Columns)
5493 first_match = match;
5494 /* if showing the last match, we can add some on the left */
5495 clen = 2;
5496 for (i = match; i < num_matches; ++i)
5498 clen += status_match_len(xp, L_MATCH(i)) + 2;
5499 if ((long)clen >= Columns)
5500 break;
5502 if (i == num_matches)
5503 add_left = TRUE;
5506 if (add_left)
5507 while (first_match > 0)
5509 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
5510 if ((long)clen >= Columns)
5511 break;
5512 --first_match;
5515 fillchar = fillchar_status(&attr, TRUE);
5517 if (first_match == 0)
5519 *buf = NUL;
5520 len = 0;
5522 else
5524 STRCPY(buf, "< ");
5525 len = 2;
5527 clen = len;
5529 i = first_match;
5530 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
5532 if (i == match)
5534 selstart = buf + len;
5535 selstart_col = clen;
5538 s = L_MATCH(i);
5539 /* Check for menu separators - replace with '|' */
5540 #ifdef FEAT_MENU
5541 emenu = (xp->xp_context == EXPAND_MENUS
5542 || xp->xp_context == EXPAND_MENUNAMES);
5543 if (emenu && menu_is_separator(s))
5545 STRCPY(buf + len, transchar('|'));
5546 l = (int)STRLEN(buf + len);
5547 len += l;
5548 clen += l;
5550 else
5551 #endif
5552 for ( ; *s != NUL; ++s)
5554 if (skip_status_match_char(xp, s))
5555 ++s;
5556 clen += ptr2cells(s);
5557 #ifdef FEAT_MBYTE
5558 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
5560 STRNCPY(buf + len, s, l);
5561 s += l - 1;
5562 len += l;
5564 else
5565 #endif
5567 STRCPY(buf + len, transchar_byte(*s));
5568 len += (int)STRLEN(buf + len);
5571 if (i == match)
5572 selend = buf + len;
5574 *(buf + len++) = ' ';
5575 *(buf + len++) = ' ';
5576 clen += 2;
5577 if (++i == num_matches)
5578 break;
5581 if (i != num_matches)
5583 *(buf + len++) = '>';
5584 ++clen;
5587 buf[len] = NUL;
5589 row = cmdline_row - 1;
5590 if (row >= 0)
5592 if (wild_menu_showing == 0)
5594 if (msg_scrolled > 0)
5596 /* Put the wildmenu just above the command line. If there is
5597 * no room, scroll the screen one line up. */
5598 if (cmdline_row == Rows - 1)
5600 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
5601 ++msg_scrolled;
5603 else
5605 ++cmdline_row;
5606 ++row;
5608 wild_menu_showing = WM_SCROLLED;
5610 else
5612 /* Create status line if needed by setting 'laststatus' to 2.
5613 * Set 'winminheight' to zero to avoid that the window is
5614 * resized. */
5615 if (lastwin->w_status_height == 0)
5617 save_p_ls = p_ls;
5618 save_p_wmh = p_wmh;
5619 p_ls = 2;
5620 p_wmh = 0;
5621 last_status(FALSE);
5623 wild_menu_showing = WM_SHOWN;
5627 screen_puts(buf, row, 0, attr);
5628 if (selstart != NULL && highlight)
5630 *selend = NUL;
5631 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
5634 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
5637 #ifdef FEAT_VERTSPLIT
5638 win_redraw_last_status(topframe);
5639 #else
5640 lastwin->w_redr_status = TRUE;
5641 #endif
5642 vim_free(buf);
5644 #endif
5646 #if defined(FEAT_WINDOWS) || defined(PROTO)
5648 * Redraw the status line of window wp.
5650 * If inversion is possible we use it. Else '=' characters are used.
5652 void
5653 win_redr_status(wp)
5654 win_T *wp;
5656 int row;
5657 char_u *p;
5658 int len;
5659 int fillchar;
5660 int attr;
5661 int this_ru_col;
5663 wp->w_redr_status = FALSE;
5664 if (wp->w_status_height == 0)
5666 /* no status line, can only be last window */
5667 redraw_cmdline = TRUE;
5669 else if (!redrawing()
5670 #ifdef FEAT_INS_EXPAND
5671 /* don't update status line when popup menu is visible and may be
5672 * drawn over it */
5673 || pum_visible()
5674 #endif
5677 /* Don't redraw right now, do it later. */
5678 wp->w_redr_status = TRUE;
5680 #ifdef FEAT_STL_OPT
5681 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
5683 /* redraw custom status line */
5684 redraw_custum_statusline(wp);
5686 #endif
5687 else
5689 fillchar = fillchar_status(&attr, wp == curwin);
5691 get_trans_bufname(wp->w_buffer);
5692 p = NameBuff;
5693 len = (int)STRLEN(p);
5695 if (wp->w_buffer->b_help
5696 #ifdef FEAT_QUICKFIX
5697 || wp->w_p_pvw
5698 #endif
5699 || bufIsChanged(wp->w_buffer)
5700 || wp->w_buffer->b_p_ro)
5701 *(p + len++) = ' ';
5702 if (wp->w_buffer->b_help)
5704 STRCPY(p + len, _("[Help]"));
5705 len += (int)STRLEN(p + len);
5707 #ifdef FEAT_QUICKFIX
5708 if (wp->w_p_pvw)
5710 STRCPY(p + len, _("[Preview]"));
5711 len += (int)STRLEN(p + len);
5713 #endif
5714 if (bufIsChanged(wp->w_buffer))
5716 STRCPY(p + len, "[+]");
5717 len += 3;
5719 if (wp->w_buffer->b_p_ro)
5721 STRCPY(p + len, "[RO]");
5722 len += 4;
5725 #ifndef FEAT_VERTSPLIT
5726 this_ru_col = ru_col;
5727 if (this_ru_col < (Columns + 1) / 2)
5728 this_ru_col = (Columns + 1) / 2;
5729 #else
5730 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
5731 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
5732 this_ru_col = (W_WIDTH(wp) + 1) / 2;
5733 if (this_ru_col <= 1)
5735 p = (char_u *)"<"; /* No room for file name! */
5736 len = 1;
5738 else
5739 #endif
5740 #ifdef FEAT_MBYTE
5741 if (has_mbyte)
5743 int clen = 0, i;
5745 /* Count total number of display cells. */
5746 for (i = 0; p[i] != NUL; i += (*mb_ptr2len)(p + i))
5747 clen += (*mb_ptr2cells)(p + i);
5748 /* Find first character that will fit.
5749 * Going from start to end is much faster for DBCS. */
5750 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
5751 i += (*mb_ptr2len)(p + i))
5752 clen -= (*mb_ptr2cells)(p + i);
5753 len = clen;
5754 if (i > 0)
5756 p = p + i - 1;
5757 *p = '<';
5758 ++len;
5762 else
5763 #endif
5764 if (len > this_ru_col - 1)
5766 p += len - (this_ru_col - 1);
5767 *p = '<';
5768 len = this_ru_col - 1;
5771 row = W_WINROW(wp) + wp->w_height;
5772 screen_puts(p, row, W_WINCOL(wp), attr);
5773 screen_fill(row, row + 1, len + W_WINCOL(wp),
5774 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
5776 if (get_keymap_str(wp, NameBuff, MAXPATHL)
5777 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
5778 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
5779 - 1 + W_WINCOL(wp)), attr);
5781 #ifdef FEAT_CMDL_INFO
5782 win_redr_ruler(wp, TRUE);
5783 #endif
5786 #ifdef FEAT_VERTSPLIT
5788 * May need to draw the character below the vertical separator.
5790 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
5792 if (stl_connected(wp))
5793 fillchar = fillchar_status(&attr, wp == curwin);
5794 else
5795 fillchar = fillchar_vsep(&attr);
5796 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
5797 attr);
5799 #endif
5802 #ifdef FEAT_STL_OPT
5804 * Redraw the status line according to 'statusline' and take care of any
5805 * errors encountered.
5807 static void
5808 redraw_custum_statusline(wp)
5809 win_T *wp;
5811 int save_called_emsg = called_emsg;
5813 called_emsg = FALSE;
5814 win_redr_custom(wp, FALSE);
5815 if (called_emsg)
5816 set_string_option_direct((char_u *)"statusline", -1,
5817 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
5818 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
5819 called_emsg |= save_called_emsg;
5821 #endif
5823 # ifdef FEAT_VERTSPLIT
5825 * Return TRUE if the status line of window "wp" is connected to the status
5826 * line of the window right of it. If not, then it's a vertical separator.
5827 * Only call if (wp->w_vsep_width != 0).
5830 stl_connected(wp)
5831 win_T *wp;
5833 frame_T *fr;
5835 fr = wp->w_frame;
5836 while (fr->fr_parent != NULL)
5838 if (fr->fr_parent->fr_layout == FR_COL)
5840 if (fr->fr_next != NULL)
5841 break;
5843 else
5845 if (fr->fr_next != NULL)
5846 return TRUE;
5848 fr = fr->fr_parent;
5850 return FALSE;
5852 # endif
5854 #endif /* FEAT_WINDOWS */
5856 #if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
5858 * Get the value to show for the language mappings, active 'keymap'.
5861 get_keymap_str(wp, buf, len)
5862 win_T *wp;
5863 char_u *buf; /* buffer for the result */
5864 int len; /* length of buffer */
5866 char_u *p;
5868 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
5869 return FALSE;
5872 #ifdef FEAT_EVAL
5873 buf_T *old_curbuf = curbuf;
5874 win_T *old_curwin = curwin;
5875 char_u *s;
5877 curbuf = wp->w_buffer;
5878 curwin = wp;
5879 STRCPY(buf, "b:keymap_name"); /* must be writable */
5880 ++emsg_skip;
5881 s = p = eval_to_string(buf, NULL, FALSE);
5882 --emsg_skip;
5883 curbuf = old_curbuf;
5884 curwin = old_curwin;
5885 if (p == NULL || *p == NUL)
5886 #endif
5888 #ifdef FEAT_KEYMAP
5889 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
5890 p = wp->w_buffer->b_p_keymap;
5891 else
5892 #endif
5893 p = (char_u *)"lang";
5895 if ((int)(STRLEN(p) + 3) < len)
5896 sprintf((char *)buf, "<%s>", p);
5897 else
5898 buf[0] = NUL;
5899 #ifdef FEAT_EVAL
5900 vim_free(s);
5901 #endif
5903 return buf[0] != NUL;
5905 #endif
5907 #if defined(FEAT_STL_OPT) || defined(PROTO)
5909 * Redraw the status line or ruler of window "wp".
5910 * When "wp" is NULL redraw the tab pages line from 'tabline'.
5912 static void
5913 win_redr_custom(wp, draw_ruler)
5914 win_T *wp;
5915 int draw_ruler; /* TRUE or FALSE */
5917 int attr;
5918 int curattr;
5919 int row;
5920 int col = 0;
5921 int maxwidth;
5922 int width;
5923 int n;
5924 int len;
5925 int fillchar;
5926 char_u buf[MAXPATHL];
5927 char_u *p;
5928 struct stl_hlrec hltab[STL_MAX_ITEM];
5929 struct stl_hlrec tabtab[STL_MAX_ITEM];
5930 int use_sandbox = FALSE;
5932 /* setup environment for the task at hand */
5933 if (wp == NULL)
5935 /* Use 'tabline'. Always at the first line of the screen. */
5936 p = p_tal;
5937 row = 0;
5938 fillchar = ' ';
5939 attr = hl_attr(HLF_TPF);
5940 maxwidth = Columns;
5941 # ifdef FEAT_EVAL
5942 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
5943 # endif
5945 else
5947 row = W_WINROW(wp) + wp->w_height;
5948 fillchar = fillchar_status(&attr, wp == curwin);
5949 maxwidth = W_WIDTH(wp);
5951 if (draw_ruler)
5953 p = p_ruf;
5954 /* advance past any leading group spec - implicit in ru_col */
5955 if (*p == '%')
5957 if (*++p == '-')
5958 p++;
5959 if (atoi((char *) p))
5960 while (VIM_ISDIGIT(*p))
5961 p++;
5962 if (*p++ != '(')
5963 p = p_ruf;
5965 #ifdef FEAT_VERTSPLIT
5966 col = ru_col - (Columns - W_WIDTH(wp));
5967 if (col < (W_WIDTH(wp) + 1) / 2)
5968 col = (W_WIDTH(wp) + 1) / 2;
5969 #else
5970 col = ru_col;
5971 if (col > (Columns + 1) / 2)
5972 col = (Columns + 1) / 2;
5973 #endif
5974 maxwidth = W_WIDTH(wp) - col;
5975 #ifdef FEAT_WINDOWS
5976 if (!wp->w_status_height)
5977 #endif
5979 row = Rows - 1;
5980 --maxwidth; /* writing in last column may cause scrolling */
5981 fillchar = ' ';
5982 attr = 0;
5985 # ifdef FEAT_EVAL
5986 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
5987 # endif
5989 else
5991 if (*wp->w_p_stl != NUL)
5992 p = wp->w_p_stl;
5993 else
5994 p = p_stl;
5995 # ifdef FEAT_EVAL
5996 use_sandbox = was_set_insecurely((char_u *)"statusline",
5997 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
5998 # endif
6001 #ifdef FEAT_VERTSPLIT
6002 col += W_WINCOL(wp);
6003 #endif
6006 if (maxwidth <= 0)
6007 return;
6009 width = build_stl_str_hl(wp == NULL ? curwin : wp,
6010 buf, sizeof(buf),
6011 p, use_sandbox,
6012 fillchar, maxwidth, hltab, tabtab);
6013 len = (int)STRLEN(buf);
6015 while (width < maxwidth && len < sizeof(buf) - 1)
6017 #ifdef FEAT_MBYTE
6018 len += (*mb_char2bytes)(fillchar, buf + len);
6019 #else
6020 buf[len++] = fillchar;
6021 #endif
6022 ++width;
6024 buf[len] = NUL;
6027 * Draw each snippet with the specified highlighting.
6029 curattr = attr;
6030 p = buf;
6031 for (n = 0; hltab[n].start != NULL; n++)
6033 len = (int)(hltab[n].start - p);
6034 screen_puts_len(p, len, row, col, curattr);
6035 col += vim_strnsize(p, len);
6036 p = hltab[n].start;
6038 if (hltab[n].userhl == 0)
6039 curattr = attr;
6040 else if (hltab[n].userhl < 0)
6041 curattr = syn_id2attr(-hltab[n].userhl);
6042 #ifdef FEAT_WINDOWS
6043 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
6044 curattr = highlight_stlnc[hltab[n].userhl - 1];
6045 #endif
6046 else
6047 curattr = highlight_user[hltab[n].userhl - 1];
6049 screen_puts(p, row, col, curattr);
6051 if (wp == NULL)
6053 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
6054 col = 0;
6055 len = 0;
6056 p = buf;
6057 fillchar = 0;
6058 for (n = 0; tabtab[n].start != NULL; n++)
6060 len += vim_strnsize(p, (int)(tabtab[n].start - p));
6061 while (col < len)
6062 TabPageIdxs[col++] = fillchar;
6063 p = tabtab[n].start;
6064 fillchar = tabtab[n].userhl;
6066 while (col < Columns)
6067 TabPageIdxs[col++] = fillchar;
6071 #endif /* FEAT_STL_OPT */
6074 * Output a single character directly to the screen and update ScreenLines.
6076 void
6077 screen_putchar(c, row, col, attr)
6078 int c;
6079 int row, col;
6080 int attr;
6082 #ifdef FEAT_MBYTE
6083 char_u buf[MB_MAXBYTES + 1];
6085 buf[(*mb_char2bytes)(c, buf)] = NUL;
6086 #else
6087 char_u buf[2];
6089 buf[0] = c;
6090 buf[1] = NUL;
6091 #endif
6092 screen_puts(buf, row, col, attr);
6096 * Get a single character directly from ScreenLines into "bytes[]".
6097 * Also return its attribute in *attrp;
6099 void
6100 screen_getbytes(row, col, bytes, attrp)
6101 int row, col;
6102 char_u *bytes;
6103 int *attrp;
6105 unsigned off;
6107 /* safety check */
6108 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
6110 off = LineOffset[row] + col;
6111 *attrp = ScreenAttrs[off];
6112 bytes[0] = ScreenLines[off];
6113 bytes[1] = NUL;
6115 #ifdef FEAT_MBYTE
6116 if (enc_utf8 && ScreenLinesUC[off] != 0)
6117 bytes[utfc_char2bytes(off, bytes)] = NUL;
6118 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6120 bytes[0] = ScreenLines[off];
6121 bytes[1] = ScreenLines2[off];
6122 bytes[2] = NUL;
6124 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
6126 bytes[1] = ScreenLines[off + 1];
6127 bytes[2] = NUL;
6129 #endif
6133 #ifdef FEAT_MBYTE
6134 static int screen_comp_differs __ARGS((int, int*));
6137 * Return TRUE if composing characters for screen posn "off" differs from
6138 * composing characters in "u8cc".
6140 static int
6141 screen_comp_differs(off, u8cc)
6142 int off;
6143 int *u8cc;
6145 int i;
6147 for (i = 0; i < Screen_mco; ++i)
6149 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
6150 return TRUE;
6151 if (u8cc[i] == 0)
6152 break;
6154 return FALSE;
6156 #endif
6159 * Put string '*text' on the screen at position 'row' and 'col', with
6160 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
6161 * Note: only outputs within one row, message is truncated at screen boundary!
6162 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
6164 void
6165 screen_puts(text, row, col, attr)
6166 char_u *text;
6167 int row;
6168 int col;
6169 int attr;
6171 screen_puts_len(text, -1, row, col, attr);
6175 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
6176 * a NUL.
6178 void
6179 screen_puts_len(text, len, row, col, attr)
6180 char_u *text;
6181 int len;
6182 int row;
6183 int col;
6184 int attr;
6186 unsigned off;
6187 char_u *ptr = text;
6188 int c;
6189 #ifdef FEAT_MBYTE
6190 int mbyte_blen = 1;
6191 int mbyte_cells = 1;
6192 int u8c = 0;
6193 int u8cc[MAX_MCO];
6194 int clear_next_cell = FALSE;
6195 # ifdef FEAT_ARABIC
6196 int prev_c = 0; /* previous Arabic character */
6197 int pc, nc, nc1;
6198 int pcc[MAX_MCO];
6199 # endif
6200 #endif
6202 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
6203 return;
6205 off = LineOffset[row] + col;
6206 while (col < screen_Columns
6207 && (len < 0 || (int)(ptr - text) < len)
6208 && *ptr != NUL)
6210 c = *ptr;
6211 #ifdef FEAT_MBYTE
6212 /* check if this is the first byte of a multibyte */
6213 if (has_mbyte)
6215 if (enc_utf8 && len > 0)
6216 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
6217 else
6218 mbyte_blen = (*mb_ptr2len)(ptr);
6219 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6220 mbyte_cells = 1;
6221 else if (enc_dbcs != 0)
6222 mbyte_cells = mbyte_blen;
6223 else /* enc_utf8 */
6225 if (len >= 0)
6226 u8c = utfc_ptr2char_len(ptr, u8cc,
6227 (int)((text + len) - ptr));
6228 else
6229 u8c = utfc_ptr2char(ptr, u8cc);
6230 mbyte_cells = utf_char2cells(u8c);
6231 /* Non-BMP character: display as ? or fullwidth ?. */
6232 if (u8c >= 0x10000)
6234 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
6235 if (attr == 0)
6236 attr = hl_attr(HLF_8);
6238 # ifdef FEAT_ARABIC
6239 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
6241 /* Do Arabic shaping. */
6242 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
6244 /* Past end of string to be displayed. */
6245 nc = NUL;
6246 nc1 = NUL;
6248 else
6250 nc = utfc_ptr2char(ptr + mbyte_blen, pcc);
6251 nc1 = pcc[0];
6253 pc = prev_c;
6254 prev_c = u8c;
6255 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
6257 else
6258 prev_c = u8c;
6259 # endif
6262 #endif
6264 if (ScreenLines[off] != c
6265 #ifdef FEAT_MBYTE
6266 || (mbyte_cells == 2
6267 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
6268 || (enc_dbcs == DBCS_JPNU
6269 && c == 0x8e
6270 && ScreenLines2[off] != ptr[1])
6271 || (enc_utf8
6272 && (ScreenLinesUC[off] != (u8char_T)u8c
6273 || screen_comp_differs(off, u8cc)))
6274 #endif
6275 || ScreenAttrs[off] != attr
6276 || exmode_active
6279 #if defined(FEAT_GUI) || defined(UNIX)
6280 /* The bold trick makes a single row of pixels appear in the next
6281 * character. When a bold character is removed, the next
6282 * character should be redrawn too. This happens for our own GUI
6283 * and for some xterms.
6284 * Force the redraw by setting the attribute to a different value
6285 * than "attr", the contents of ScreenLines[] may be needed by
6286 * mb_off2cells() further on.
6287 * Don't do this for the last drawn character, because the next
6288 * character may not be redrawn. */
6289 if (
6290 # ifdef FEAT_GUI
6291 gui.in_use
6292 # endif
6293 # if defined(FEAT_GUI) && defined(UNIX)
6295 # endif
6296 # ifdef UNIX
6297 term_is_xterm
6298 # endif
6301 int n;
6303 n = ScreenAttrs[off];
6304 # ifdef FEAT_MBYTE
6305 if (col + mbyte_cells < screen_Columns
6306 && (n > HL_ALL || (n & HL_BOLD))
6307 && (len < 0 ? ptr[mbyte_blen] != NUL
6308 : ptr + mbyte_blen < text + len))
6309 ScreenAttrs[off + mbyte_cells] = attr + 1;
6310 # else
6311 if (col + 1 < screen_Columns
6312 && (n > HL_ALL || (n & HL_BOLD))
6313 && (len < 0 ? ptr[1] != NUL : ptr + 1 < text + len))
6314 ScreenLines[off + 1] = 0;
6315 # endif
6317 #endif
6318 #ifdef FEAT_MBYTE
6319 /* When at the end of the text and overwriting a two-cell
6320 * character with a one-cell character, need to clear the next
6321 * cell. Also when overwriting the left halve of a two-cell char
6322 * with the right halve of a two-cell char. Do this only once
6323 * (mb_off2cells() may return 2 on the right halve). */
6324 if (clear_next_cell)
6325 clear_next_cell = FALSE;
6326 else if (has_mbyte
6327 && (len < 0 ? ptr[mbyte_blen] == NUL
6328 : ptr + mbyte_blen >= text + len)
6329 && ((mbyte_cells == 1 && (*mb_off2cells)(off) > 1)
6330 || (mbyte_cells == 2
6331 && (*mb_off2cells)(off) == 1
6332 && (*mb_off2cells)(off + 1) > 1)))
6333 clear_next_cell = TRUE;
6335 /* Make sure we never leave a second byte of a double-byte behind,
6336 * it confuses mb_off2cells(). */
6337 if (enc_dbcs
6338 && ((mbyte_cells == 1 && (*mb_off2cells)(off) > 1)
6339 || (mbyte_cells == 2
6340 && (*mb_off2cells)(off) == 1
6341 && (*mb_off2cells)(off + 1) > 1)))
6342 ScreenLines[off + mbyte_blen] = 0;
6343 #endif
6344 ScreenLines[off] = c;
6345 ScreenAttrs[off] = attr;
6346 #ifdef FEAT_MBYTE
6347 if (enc_utf8)
6349 if (c < 0x80 && u8cc[0] == 0)
6350 ScreenLinesUC[off] = 0;
6351 else
6353 int i;
6355 ScreenLinesUC[off] = u8c;
6356 for (i = 0; i < Screen_mco; ++i)
6358 ScreenLinesC[i][off] = u8cc[i];
6359 if (u8cc[i] == 0)
6360 break;
6363 if (mbyte_cells == 2)
6365 ScreenLines[off + 1] = 0;
6366 ScreenAttrs[off + 1] = attr;
6368 screen_char(off, row, col);
6370 else if (mbyte_cells == 2)
6372 ScreenLines[off + 1] = ptr[1];
6373 ScreenAttrs[off + 1] = attr;
6374 screen_char_2(off, row, col);
6376 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6378 ScreenLines2[off] = ptr[1];
6379 screen_char(off, row, col);
6381 else
6382 #endif
6383 screen_char(off, row, col);
6385 #ifdef FEAT_MBYTE
6386 if (has_mbyte)
6388 off += mbyte_cells;
6389 col += mbyte_cells;
6390 ptr += mbyte_blen;
6391 if (clear_next_cell)
6392 ptr = (char_u *)" ";
6394 else
6395 #endif
6397 ++off;
6398 ++col;
6399 ++ptr;
6404 #ifdef FEAT_SEARCH_EXTRA
6406 * Prepare for 'hlsearch' highlighting.
6408 static void
6409 start_search_hl()
6411 if (p_hls && !no_hlsearch)
6413 last_pat_prog(&search_hl.rm);
6414 search_hl.attr = hl_attr(HLF_L);
6419 * Clean up for 'hlsearch' highlighting.
6421 static void
6422 end_search_hl()
6424 if (search_hl.rm.regprog != NULL)
6426 vim_free(search_hl.rm.regprog);
6427 search_hl.rm.regprog = NULL;
6432 * Advance to the match in window "wp" line "lnum" or past it.
6434 static void
6435 prepare_search_hl(wp, lnum)
6436 win_T *wp;
6437 linenr_T lnum;
6439 matchitem_T *cur; /* points to the match list */
6440 match_T *shl; /* points to search_hl or a match */
6441 int shl_flag; /* flag to indicate whether search_hl
6442 has been processed or not */
6443 int n;
6446 * When using a multi-line pattern, start searching at the top
6447 * of the window or just after a closed fold.
6448 * Do this both for search_hl and the match list.
6450 cur = wp->w_match_head;
6451 shl_flag = FALSE;
6452 while (cur != NULL || shl_flag == FALSE)
6454 if (shl_flag == FALSE)
6456 shl = &search_hl;
6457 shl_flag = TRUE;
6459 else
6460 shl = &cur->hl;
6461 if (shl->rm.regprog != NULL
6462 && shl->lnum == 0
6463 && re_multiline(shl->rm.regprog))
6465 if (shl->first_lnum == 0)
6467 # ifdef FEAT_FOLDING
6468 for (shl->first_lnum = lnum;
6469 shl->first_lnum > wp->w_topline; --shl->first_lnum)
6470 if (hasFoldingWin(wp, shl->first_lnum - 1,
6471 NULL, NULL, TRUE, NULL))
6472 break;
6473 # else
6474 shl->first_lnum = wp->w_topline;
6475 # endif
6477 n = 0;
6478 while (shl->first_lnum < lnum && shl->rm.regprog != NULL)
6480 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n);
6481 if (shl->lnum != 0)
6483 shl->first_lnum = shl->lnum
6484 + shl->rm.endpos[0].lnum
6485 - shl->rm.startpos[0].lnum;
6486 n = shl->rm.endpos[0].col;
6488 else
6490 ++shl->first_lnum;
6491 n = 0;
6495 if (shl != &search_hl && cur != NULL)
6496 cur = cur->next;
6501 * Search for a next 'hlsearch' or match.
6502 * Uses shl->buf.
6503 * Sets shl->lnum and shl->rm contents.
6504 * Note: Assumes a previous match is always before "lnum", unless
6505 * shl->lnum is zero.
6506 * Careful: Any pointers for buffer lines will become invalid.
6508 static void
6509 next_search_hl(win, shl, lnum, mincol)
6510 win_T *win;
6511 match_T *shl; /* points to search_hl or a match */
6512 linenr_T lnum;
6513 colnr_T mincol; /* minimal column for a match */
6515 linenr_T l;
6516 colnr_T matchcol;
6517 long nmatched;
6519 if (shl->lnum != 0)
6521 /* Check for three situations:
6522 * 1. If the "lnum" is below a previous match, start a new search.
6523 * 2. If the previous match includes "mincol", use it.
6524 * 3. Continue after the previous match.
6526 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
6527 if (lnum > l)
6528 shl->lnum = 0;
6529 else if (lnum < l || shl->rm.endpos[0].col > mincol)
6530 return;
6534 * Repeat searching for a match until one is found that includes "mincol"
6535 * or none is found in this line.
6537 called_emsg = FALSE;
6538 for (;;)
6540 /* Three situations:
6541 * 1. No useful previous match: search from start of line.
6542 * 2. Not Vi compatible or empty match: continue at next character.
6543 * Break the loop if this is beyond the end of the line.
6544 * 3. Vi compatible searching: continue at end of previous match.
6546 if (shl->lnum == 0)
6547 matchcol = 0;
6548 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
6549 || (shl->rm.endpos[0].lnum == 0
6550 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
6552 char_u *ml;
6554 matchcol = shl->rm.startpos[0].col;
6555 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
6556 if (*ml == NUL)
6558 ++matchcol;
6559 shl->lnum = 0;
6560 break;
6562 #ifdef FEAT_MBYTE
6563 if (has_mbyte)
6564 matchcol += mb_ptr2len(ml);
6565 else
6566 #endif
6567 ++matchcol;
6569 else
6570 matchcol = shl->rm.endpos[0].col;
6572 shl->lnum = lnum;
6573 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum, matchcol);
6574 if (called_emsg)
6576 /* Error while handling regexp: stop using this regexp. */
6577 if (shl == &search_hl)
6579 /* don't free regprog in the match list, it's a copy */
6580 vim_free(shl->rm.regprog);
6581 no_hlsearch = TRUE;
6583 shl->rm.regprog = NULL;
6584 shl->lnum = 0;
6585 got_int = FALSE; /* avoid the "Type :quit to exit Vim" message */
6586 break;
6588 if (nmatched == 0)
6590 shl->lnum = 0; /* no match found */
6591 break;
6593 if (shl->rm.startpos[0].lnum > 0
6594 || shl->rm.startpos[0].col >= mincol
6595 || nmatched > 1
6596 || shl->rm.endpos[0].col > mincol)
6598 shl->lnum += shl->rm.startpos[0].lnum;
6599 break; /* useful match found */
6603 #endif
6605 static void
6606 screen_start_highlight(attr)
6607 int attr;
6609 attrentry_T *aep = NULL;
6611 screen_attr = attr;
6612 if (full_screen
6613 #ifdef WIN3264
6614 && termcap_active
6615 #endif
6618 #ifdef FEAT_GUI
6619 if (gui.in_use)
6621 char buf[20];
6623 /* The GUI handles this internally. */
6624 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
6625 OUT_STR(buf);
6627 else
6628 #endif
6630 if (attr > HL_ALL) /* special HL attr. */
6632 if (t_colors > 1)
6633 aep = syn_cterm_attr2entry(attr);
6634 else
6635 aep = syn_term_attr2entry(attr);
6636 if (aep == NULL) /* did ":syntax clear" */
6637 attr = 0;
6638 else
6639 attr = aep->ae_attr;
6641 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
6642 out_str(T_MD);
6643 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
6644 && cterm_normal_fg_bold)
6645 /* If the Normal FG color has BOLD attribute and the new HL
6646 * has a FG color defined, clear BOLD. */
6647 out_str(T_ME);
6648 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
6649 out_str(T_SO);
6650 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
6651 /* underline or undercurl */
6652 out_str(T_US);
6653 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
6654 out_str(T_CZH);
6655 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
6656 out_str(T_MR);
6659 * Output the color or start string after bold etc., in case the
6660 * bold etc. override the color setting.
6662 if (aep != NULL)
6664 if (t_colors > 1)
6666 if (aep->ae_u.cterm.fg_color)
6667 term_fg_color(aep->ae_u.cterm.fg_color - 1);
6668 if (aep->ae_u.cterm.bg_color)
6669 term_bg_color(aep->ae_u.cterm.bg_color - 1);
6671 else
6673 if (aep->ae_u.term.start != NULL)
6674 out_str(aep->ae_u.term.start);
6681 void
6682 screen_stop_highlight()
6684 int do_ME = FALSE; /* output T_ME code */
6686 if (screen_attr != 0
6687 #ifdef WIN3264
6688 && termcap_active
6689 #endif
6692 #ifdef FEAT_GUI
6693 if (gui.in_use)
6695 char buf[20];
6697 /* use internal GUI code */
6698 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
6699 OUT_STR(buf);
6701 else
6702 #endif
6704 if (screen_attr > HL_ALL) /* special HL attr. */
6706 attrentry_T *aep;
6708 if (t_colors > 1)
6711 * Assume that t_me restores the original colors!
6713 aep = syn_cterm_attr2entry(screen_attr);
6714 if (aep != NULL && (aep->ae_u.cterm.fg_color
6715 || aep->ae_u.cterm.bg_color))
6716 do_ME = TRUE;
6718 else
6720 aep = syn_term_attr2entry(screen_attr);
6721 if (aep != NULL && aep->ae_u.term.stop != NULL)
6723 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
6724 do_ME = TRUE;
6725 else
6726 out_str(aep->ae_u.term.stop);
6729 if (aep == NULL) /* did ":syntax clear" */
6730 screen_attr = 0;
6731 else
6732 screen_attr = aep->ae_attr;
6736 * Often all ending-codes are equal to T_ME. Avoid outputting the
6737 * same sequence several times.
6739 if (screen_attr & HL_STANDOUT)
6741 if (STRCMP(T_SE, T_ME) == 0)
6742 do_ME = TRUE;
6743 else
6744 out_str(T_SE);
6746 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
6748 if (STRCMP(T_UE, T_ME) == 0)
6749 do_ME = TRUE;
6750 else
6751 out_str(T_UE);
6753 if (screen_attr & HL_ITALIC)
6755 if (STRCMP(T_CZR, T_ME) == 0)
6756 do_ME = TRUE;
6757 else
6758 out_str(T_CZR);
6760 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
6761 out_str(T_ME);
6763 if (t_colors > 1)
6765 /* set Normal cterm colors */
6766 if (cterm_normal_fg_color != 0)
6767 term_fg_color(cterm_normal_fg_color - 1);
6768 if (cterm_normal_bg_color != 0)
6769 term_bg_color(cterm_normal_bg_color - 1);
6770 if (cterm_normal_fg_bold)
6771 out_str(T_MD);
6775 screen_attr = 0;
6779 * Reset the colors for a cterm. Used when leaving Vim.
6780 * The machine specific code may override this again.
6782 void
6783 reset_cterm_colors()
6785 if (t_colors > 1)
6787 /* set Normal cterm colors */
6788 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
6790 out_str(T_OP);
6791 screen_attr = -1;
6793 if (cterm_normal_fg_bold)
6795 out_str(T_ME);
6796 screen_attr = -1;
6802 * Put character ScreenLines["off"] on the screen at position "row" and "col",
6803 * using the attributes from ScreenAttrs["off"].
6805 static void
6806 screen_char(off, row, col)
6807 unsigned off;
6808 int row;
6809 int col;
6811 int attr;
6813 /* Check for illegal values, just in case (could happen just after
6814 * resizing). */
6815 if (row >= screen_Rows || col >= screen_Columns)
6816 return;
6818 /* Outputting the last character on the screen may scrollup the screen.
6819 * Don't to it! Mark the character invalid (update it when scrolled up) */
6820 if (row == screen_Rows - 1 && col == screen_Columns - 1
6821 #ifdef FEAT_RIGHTLEFT
6822 /* account for first command-line character in rightleft mode */
6823 && !cmdmsg_rl
6824 #endif
6827 ScreenAttrs[off] = (sattr_T)-1;
6828 return;
6832 * Stop highlighting first, so it's easier to move the cursor.
6834 #if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
6835 if (screen_char_attr != 0)
6836 attr = screen_char_attr;
6837 else
6838 #endif
6839 attr = ScreenAttrs[off];
6840 if (screen_attr != attr)
6841 screen_stop_highlight();
6843 windgoto(row, col);
6845 if (screen_attr != attr)
6846 screen_start_highlight(attr);
6848 #ifdef FEAT_MBYTE
6849 if (enc_utf8 && ScreenLinesUC[off] != 0)
6851 char_u buf[MB_MAXBYTES + 1];
6853 /* Convert UTF-8 character to bytes and write it. */
6855 buf[utfc_char2bytes(off, buf)] = NUL;
6857 out_str(buf);
6858 if (utf_char2cells(ScreenLinesUC[off]) > 1)
6859 ++screen_cur_col;
6861 else
6862 #endif
6864 #ifdef FEAT_MBYTE
6865 out_flush_check();
6866 #endif
6867 out_char(ScreenLines[off]);
6868 #ifdef FEAT_MBYTE
6869 /* double-byte character in single-width cell */
6870 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6871 out_char(ScreenLines2[off]);
6872 #endif
6875 screen_cur_col++;
6878 #ifdef FEAT_MBYTE
6881 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
6882 * on the screen at position 'row' and 'col'.
6883 * The attributes of the first byte is used for all. This is required to
6884 * output the two bytes of a double-byte character with nothing in between.
6886 static void
6887 screen_char_2(off, row, col)
6888 unsigned off;
6889 int row;
6890 int col;
6892 /* Check for illegal values (could be wrong when screen was resized). */
6893 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
6894 return;
6896 /* Outputting the last character on the screen may scrollup the screen.
6897 * Don't to it! Mark the character invalid (update it when scrolled up) */
6898 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
6900 ScreenAttrs[off] = (sattr_T)-1;
6901 return;
6904 /* Output the first byte normally (positions the cursor), then write the
6905 * second byte directly. */
6906 screen_char(off, row, col);
6907 out_char(ScreenLines[off + 1]);
6908 ++screen_cur_col;
6910 #endif
6912 #if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
6914 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
6915 * This uses the contents of ScreenLines[] and doesn't change it.
6917 void
6918 screen_draw_rectangle(row, col, height, width, invert)
6919 int row;
6920 int col;
6921 int height;
6922 int width;
6923 int invert;
6925 int r, c;
6926 int off;
6928 /* Can't use ScreenLines unless initialized */
6929 if (ScreenLines == NULL)
6930 return;
6932 if (invert)
6933 screen_char_attr = HL_INVERSE;
6934 for (r = row; r < row + height; ++r)
6936 off = LineOffset[r];
6937 for (c = col; c < col + width; ++c)
6939 #ifdef FEAT_MBYTE
6940 if (enc_dbcs != 0 && dbcs_off2cells(off + c) > 1)
6942 screen_char_2(off + c, r, c);
6943 ++c;
6945 else
6946 #endif
6948 screen_char(off + c, r, c);
6949 #ifdef FEAT_MBYTE
6950 if (utf_off2cells(off + c) > 1)
6951 ++c;
6952 #endif
6956 screen_char_attr = 0;
6958 #endif
6960 #ifdef FEAT_VERTSPLIT
6962 * Redraw the characters for a vertically split window.
6964 static void
6965 redraw_block(row, end, wp)
6966 int row;
6967 int end;
6968 win_T *wp;
6970 int col;
6971 int width;
6973 # ifdef FEAT_CLIPBOARD
6974 clip_may_clear_selection(row, end - 1);
6975 # endif
6977 if (wp == NULL)
6979 col = 0;
6980 width = Columns;
6982 else
6984 col = wp->w_wincol;
6985 width = wp->w_width;
6987 screen_draw_rectangle(row, col, end - row, width, FALSE);
6989 #endif
6992 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
6993 * with character 'c1' in first column followed by 'c2' in the other columns.
6994 * Use attributes 'attr'.
6996 void
6997 screen_fill(start_row, end_row, start_col, end_col, c1, c2, attr)
6998 int start_row, end_row;
6999 int start_col, end_col;
7000 int c1, c2;
7001 int attr;
7003 int row;
7004 int col;
7005 int off;
7006 int end_off;
7007 int did_delete;
7008 int c;
7009 int norm_term;
7010 #if defined(FEAT_GUI) || defined(UNIX)
7011 int force_next = FALSE;
7012 #endif
7014 if (end_row > screen_Rows) /* safety check */
7015 end_row = screen_Rows;
7016 if (end_col > screen_Columns) /* safety check */
7017 end_col = screen_Columns;
7018 if (ScreenLines == NULL
7019 || start_row >= end_row
7020 || start_col >= end_col) /* nothing to do */
7021 return;
7023 /* it's a "normal" terminal when not in a GUI or cterm */
7024 norm_term = (
7025 #ifdef FEAT_GUI
7026 !gui.in_use &&
7027 #endif
7028 t_colors <= 1);
7029 for (row = start_row; row < end_row; ++row)
7032 * Try to use delete-line termcap code, when no attributes or in a
7033 * "normal" terminal, where a bold/italic space is just a
7034 * space.
7036 did_delete = FALSE;
7037 if (c2 == ' '
7038 && end_col == Columns
7039 && can_clear(T_CE)
7040 && (attr == 0
7041 || (norm_term
7042 && attr <= HL_ALL
7043 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
7046 * check if we really need to clear something
7048 col = start_col;
7049 if (c1 != ' ') /* don't clear first char */
7050 ++col;
7052 off = LineOffset[row] + col;
7053 end_off = LineOffset[row] + end_col;
7055 /* skip blanks (used often, keep it fast!) */
7056 #ifdef FEAT_MBYTE
7057 if (enc_utf8)
7058 while (off < end_off && ScreenLines[off] == ' '
7059 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
7060 ++off;
7061 else
7062 #endif
7063 while (off < end_off && ScreenLines[off] == ' '
7064 && ScreenAttrs[off] == 0)
7065 ++off;
7066 if (off < end_off) /* something to be cleared */
7068 col = off - LineOffset[row];
7069 screen_stop_highlight();
7070 term_windgoto(row, col);/* clear rest of this screen line */
7071 out_str(T_CE);
7072 screen_start(); /* don't know where cursor is now */
7073 col = end_col - col;
7074 while (col--) /* clear chars in ScreenLines */
7076 ScreenLines[off] = ' ';
7077 #ifdef FEAT_MBYTE
7078 if (enc_utf8)
7079 ScreenLinesUC[off] = 0;
7080 #endif
7081 ScreenAttrs[off] = 0;
7082 ++off;
7085 did_delete = TRUE; /* the chars are cleared now */
7088 off = LineOffset[row] + start_col;
7089 c = c1;
7090 for (col = start_col; col < end_col; ++col)
7092 if (ScreenLines[off] != c
7093 #ifdef FEAT_MBYTE
7094 || (enc_utf8 && (int)ScreenLinesUC[off]
7095 != (c >= 0x80 ? c : 0))
7096 #endif
7097 || ScreenAttrs[off] != attr
7098 #if defined(FEAT_GUI) || defined(UNIX)
7099 || force_next
7100 #endif
7103 #if defined(FEAT_GUI) || defined(UNIX)
7104 /* The bold trick may make a single row of pixels appear in
7105 * the next character. When a bold character is removed, the
7106 * next character should be redrawn too. This happens for our
7107 * own GUI and for some xterms. */
7108 if (
7109 # ifdef FEAT_GUI
7110 gui.in_use
7111 # endif
7112 # if defined(FEAT_GUI) && defined(UNIX)
7114 # endif
7115 # ifdef UNIX
7116 term_is_xterm
7117 # endif
7120 if (ScreenLines[off] != ' '
7121 && (ScreenAttrs[off] > HL_ALL
7122 || ScreenAttrs[off] & HL_BOLD))
7123 force_next = TRUE;
7124 else
7125 force_next = FALSE;
7127 #endif
7128 ScreenLines[off] = c;
7129 #ifdef FEAT_MBYTE
7130 if (enc_utf8)
7132 if (c >= 0x80)
7134 ScreenLinesUC[off] = c;
7135 ScreenLinesC[0][off] = 0;
7137 else
7138 ScreenLinesUC[off] = 0;
7140 #endif
7141 ScreenAttrs[off] = attr;
7142 if (!did_delete || c != ' ')
7143 screen_char(off, row, col);
7145 ++off;
7146 if (col == start_col)
7148 if (did_delete)
7149 break;
7150 c = c2;
7153 if (end_col == Columns)
7154 LineWraps[row] = FALSE;
7155 if (row == Rows - 1) /* overwritten the command line */
7157 redraw_cmdline = TRUE;
7158 if (c1 == ' ' && c2 == ' ')
7159 clear_cmdline = FALSE; /* command line has been cleared */
7160 if (start_col == 0)
7161 mode_displayed = FALSE; /* mode cleared or overwritten */
7167 * Check if there should be a delay. Used before clearing or redrawing the
7168 * screen or the command line.
7170 void
7171 check_for_delay(check_msg_scroll)
7172 int check_msg_scroll;
7174 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
7175 && !did_wait_return
7176 && emsg_silent == 0)
7178 out_flush();
7179 ui_delay(1000L, TRUE);
7180 emsg_on_display = FALSE;
7181 if (check_msg_scroll)
7182 msg_scroll = FALSE;
7187 * screen_valid - allocate screen buffers if size changed
7188 * If "clear" is TRUE: clear screen if it has been resized.
7189 * Returns TRUE if there is a valid screen to write to.
7190 * Returns FALSE when starting up and screen not initialized yet.
7193 screen_valid(clear)
7194 int clear;
7196 screenalloc(clear); /* allocate screen buffers if size changed */
7197 return (ScreenLines != NULL);
7201 * Resize the shell to Rows and Columns.
7202 * Allocate ScreenLines[] and associated items.
7204 * There may be some time between setting Rows and Columns and (re)allocating
7205 * ScreenLines[]. This happens when starting up and when (manually) changing
7206 * the shell size. Always use screen_Rows and screen_Columns to access items
7207 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
7208 * final size of the shell is needed.
7210 void
7211 screenalloc(clear)
7212 int clear;
7214 int new_row, old_row;
7215 #ifdef FEAT_GUI
7216 int old_Rows;
7217 #endif
7218 win_T *wp;
7219 int outofmem = FALSE;
7220 int len;
7221 schar_T *new_ScreenLines;
7222 #ifdef FEAT_MBYTE
7223 u8char_T *new_ScreenLinesUC = NULL;
7224 u8char_T *new_ScreenLinesC[MAX_MCO];
7225 schar_T *new_ScreenLines2 = NULL;
7226 int i;
7227 #endif
7228 sattr_T *new_ScreenAttrs;
7229 unsigned *new_LineOffset;
7230 char_u *new_LineWraps;
7231 #ifdef FEAT_WINDOWS
7232 short *new_TabPageIdxs;
7233 tabpage_T *tp;
7234 #endif
7235 static int entered = FALSE; /* avoid recursiveness */
7236 static int done_outofmem_msg = FALSE; /* did outofmem message */
7239 * Allocation of the screen buffers is done only when the size changes and
7240 * when Rows and Columns have been set and we have started doing full
7241 * screen stuff.
7243 if ((ScreenLines != NULL
7244 && Rows == screen_Rows
7245 && Columns == screen_Columns
7246 #ifdef FEAT_MBYTE
7247 && enc_utf8 == (ScreenLinesUC != NULL)
7248 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
7249 && p_mco == Screen_mco
7250 #endif
7252 || Rows == 0
7253 || Columns == 0
7254 || (!full_screen && ScreenLines == NULL))
7255 return;
7258 * It's possible that we produce an out-of-memory message below, which
7259 * will cause this function to be called again. To break the loop, just
7260 * return here.
7262 if (entered)
7263 return;
7264 entered = TRUE;
7267 * Note that the window sizes are updated before reallocating the arrays,
7268 * thus we must not redraw here!
7270 ++RedrawingDisabled;
7272 win_new_shellsize(); /* fit the windows in the new sized shell */
7274 comp_col(); /* recompute columns for shown command and ruler */
7277 * We're changing the size of the screen.
7278 * - Allocate new arrays for ScreenLines and ScreenAttrs.
7279 * - Move lines from the old arrays into the new arrays, clear extra
7280 * lines (unless the screen is going to be cleared).
7281 * - Free the old arrays.
7283 * If anything fails, make ScreenLines NULL, so we don't do anything!
7284 * Continuing with the old ScreenLines may result in a crash, because the
7285 * size is wrong.
7287 FOR_ALL_TAB_WINDOWS(tp, wp)
7288 win_free_lsize(wp);
7290 new_ScreenLines = (schar_T *)lalloc((long_u)(
7291 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7292 #ifdef FEAT_MBYTE
7293 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T) * MAX_MCO);
7294 if (enc_utf8)
7296 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
7297 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
7298 for (i = 0; i < p_mco; ++i)
7299 new_ScreenLinesC[i] = (u8char_T *)lalloc((long_u)(
7300 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
7302 if (enc_dbcs == DBCS_JPNU)
7303 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
7304 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7305 #endif
7306 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
7307 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
7308 new_LineOffset = (unsigned *)lalloc((long_u)(
7309 Rows * sizeof(unsigned)), FALSE);
7310 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
7311 #ifdef FEAT_WINDOWS
7312 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
7313 #endif
7315 FOR_ALL_TAB_WINDOWS(tp, wp)
7317 if (win_alloc_lines(wp) == FAIL)
7319 outofmem = TRUE;
7320 #ifdef FEAT_WINDOWS
7321 break;
7322 #endif
7326 #ifdef FEAT_MBYTE
7327 for (i = 0; i < p_mco; ++i)
7328 if (new_ScreenLinesC[i] == NULL)
7329 break;
7330 #endif
7331 if (new_ScreenLines == NULL
7332 #ifdef FEAT_MBYTE
7333 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
7334 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
7335 #endif
7336 || new_ScreenAttrs == NULL
7337 || new_LineOffset == NULL
7338 || new_LineWraps == NULL
7339 #ifdef FEAT_WINDOWS
7340 || new_TabPageIdxs == NULL
7341 #endif
7342 || outofmem)
7344 if (ScreenLines != NULL || !done_outofmem_msg)
7346 /* guess the size */
7347 do_outofmem_msg((long_u)((Rows + 1) * Columns));
7349 /* Remember we did this to avoid getting outofmem messages over
7350 * and over again. */
7351 done_outofmem_msg = TRUE;
7353 vim_free(new_ScreenLines);
7354 new_ScreenLines = NULL;
7355 #ifdef FEAT_MBYTE
7356 vim_free(new_ScreenLinesUC);
7357 new_ScreenLinesUC = NULL;
7358 for (i = 0; i < p_mco; ++i)
7360 vim_free(new_ScreenLinesC[i]);
7361 new_ScreenLinesC[i] = NULL;
7363 vim_free(new_ScreenLines2);
7364 new_ScreenLines2 = NULL;
7365 #endif
7366 vim_free(new_ScreenAttrs);
7367 new_ScreenAttrs = NULL;
7368 vim_free(new_LineOffset);
7369 new_LineOffset = NULL;
7370 vim_free(new_LineWraps);
7371 new_LineWraps = NULL;
7372 #ifdef FEAT_WINDOWS
7373 vim_free(new_TabPageIdxs);
7374 new_TabPageIdxs = NULL;
7375 #endif
7377 else
7379 done_outofmem_msg = FALSE;
7381 for (new_row = 0; new_row < Rows; ++new_row)
7383 new_LineOffset[new_row] = new_row * Columns;
7384 new_LineWraps[new_row] = FALSE;
7387 * If the screen is not going to be cleared, copy as much as
7388 * possible from the old screen to the new one and clear the rest
7389 * (used when resizing the window at the "--more--" prompt or when
7390 * executing an external command, for the GUI).
7392 if (!clear)
7394 (void)vim_memset(new_ScreenLines + new_row * Columns,
7395 ' ', (size_t)Columns * sizeof(schar_T));
7396 #ifdef FEAT_MBYTE
7397 if (enc_utf8)
7399 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
7400 0, (size_t)Columns * sizeof(u8char_T));
7401 for (i = 0; i < p_mco; ++i)
7402 (void)vim_memset(new_ScreenLinesC[i]
7403 + new_row * Columns,
7404 0, (size_t)Columns * sizeof(u8char_T));
7406 if (enc_dbcs == DBCS_JPNU)
7407 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
7408 0, (size_t)Columns * sizeof(schar_T));
7409 #endif
7410 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
7411 0, (size_t)Columns * sizeof(sattr_T));
7412 old_row = new_row + (screen_Rows - Rows);
7413 if (old_row >= 0 && ScreenLines != NULL)
7415 if (screen_Columns < Columns)
7416 len = screen_Columns;
7417 else
7418 len = Columns;
7419 #ifdef FEAT_MBYTE
7420 /* When switching to utf-8 don't copy characters, they
7421 * may be invalid now. Also when p_mco changes. */
7422 if (!(enc_utf8 && ScreenLinesUC == NULL)
7423 && p_mco == Screen_mco)
7424 #endif
7425 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
7426 ScreenLines + LineOffset[old_row],
7427 (size_t)len * sizeof(schar_T));
7428 #ifdef FEAT_MBYTE
7429 if (enc_utf8 && ScreenLinesUC != NULL
7430 && p_mco == Screen_mco)
7432 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
7433 ScreenLinesUC + LineOffset[old_row],
7434 (size_t)len * sizeof(u8char_T));
7435 for (i = 0; i < p_mco; ++i)
7436 mch_memmove(new_ScreenLinesC[i]
7437 + new_LineOffset[new_row],
7438 ScreenLinesC[i] + LineOffset[old_row],
7439 (size_t)len * sizeof(u8char_T));
7441 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
7442 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
7443 ScreenLines2 + LineOffset[old_row],
7444 (size_t)len * sizeof(schar_T));
7445 #endif
7446 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
7447 ScreenAttrs + LineOffset[old_row],
7448 (size_t)len * sizeof(sattr_T));
7452 /* Use the last line of the screen for the current line. */
7453 current_ScreenLine = new_ScreenLines + Rows * Columns;
7456 free_screenlines();
7458 ScreenLines = new_ScreenLines;
7459 #ifdef FEAT_MBYTE
7460 ScreenLinesUC = new_ScreenLinesUC;
7461 for (i = 0; i < p_mco; ++i)
7462 ScreenLinesC[i] = new_ScreenLinesC[i];
7463 Screen_mco = p_mco;
7464 ScreenLines2 = new_ScreenLines2;
7465 #endif
7466 ScreenAttrs = new_ScreenAttrs;
7467 LineOffset = new_LineOffset;
7468 LineWraps = new_LineWraps;
7469 #ifdef FEAT_WINDOWS
7470 TabPageIdxs = new_TabPageIdxs;
7471 #endif
7473 /* It's important that screen_Rows and screen_Columns reflect the actual
7474 * size of ScreenLines[]. Set them before calling anything. */
7475 #ifdef FEAT_GUI
7476 old_Rows = screen_Rows;
7477 #endif
7478 screen_Rows = Rows;
7479 screen_Columns = Columns;
7481 must_redraw = CLEAR; /* need to clear the screen later */
7482 if (clear)
7483 screenclear2();
7485 #ifdef FEAT_GUI
7486 else if (gui.in_use
7487 && !gui.starting
7488 && ScreenLines != NULL
7489 && old_Rows != Rows)
7491 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
7493 * Adjust the position of the cursor, for when executing an external
7494 * command.
7496 if (msg_row >= Rows) /* Rows got smaller */
7497 msg_row = Rows - 1; /* put cursor at last row */
7498 else if (Rows > old_Rows) /* Rows got bigger */
7499 msg_row += Rows - old_Rows; /* put cursor in same place */
7500 if (msg_col >= Columns) /* Columns got smaller */
7501 msg_col = Columns - 1; /* put cursor at last column */
7503 #endif
7505 entered = FALSE;
7506 --RedrawingDisabled;
7508 #ifdef FEAT_AUTOCMD
7509 if (starting == 0)
7510 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
7511 #endif
7514 void
7515 free_screenlines()
7517 #ifdef FEAT_MBYTE
7518 int i;
7520 vim_free(ScreenLinesUC);
7521 for (i = 0; i < Screen_mco; ++i)
7522 vim_free(ScreenLinesC[i]);
7523 vim_free(ScreenLines2);
7524 #endif
7525 vim_free(ScreenLines);
7526 vim_free(ScreenAttrs);
7527 vim_free(LineOffset);
7528 vim_free(LineWraps);
7529 #ifdef FEAT_WINDOWS
7530 vim_free(TabPageIdxs);
7531 #endif
7534 void
7535 screenclear()
7537 check_for_delay(FALSE);
7538 screenalloc(FALSE); /* allocate screen buffers if size changed */
7539 screenclear2(); /* clear the screen */
7542 static void
7543 screenclear2()
7545 int i;
7547 if (starting == NO_SCREEN || ScreenLines == NULL
7548 #ifdef FEAT_GUI
7549 || (gui.in_use && gui.starting)
7550 #endif
7552 return;
7554 #ifdef FEAT_GUI
7555 if (!gui.in_use)
7556 #endif
7557 screen_attr = -1; /* force setting the Normal colors */
7558 screen_stop_highlight(); /* don't want highlighting here */
7560 #ifdef FEAT_CLIPBOARD
7561 /* disable selection without redrawing it */
7562 clip_scroll_selection(9999);
7563 #endif
7565 /* blank out ScreenLines */
7566 for (i = 0; i < Rows; ++i)
7568 lineclear(LineOffset[i], (int)Columns);
7569 LineWraps[i] = FALSE;
7572 if (can_clear(T_CL))
7574 out_str(T_CL); /* clear the display */
7575 clear_cmdline = FALSE;
7576 mode_displayed = FALSE;
7578 else
7580 /* can't clear the screen, mark all chars with invalid attributes */
7581 for (i = 0; i < Rows; ++i)
7582 lineinvalid(LineOffset[i], (int)Columns);
7583 clear_cmdline = TRUE;
7586 screen_cleared = TRUE; /* can use contents of ScreenLines now */
7588 win_rest_invalid(firstwin);
7589 redraw_cmdline = TRUE;
7590 #ifdef FEAT_WINDOWS
7591 redraw_tabline = TRUE;
7592 #endif
7593 if (must_redraw == CLEAR) /* no need to clear again */
7594 must_redraw = NOT_VALID;
7595 compute_cmdrow();
7596 msg_row = cmdline_row; /* put cursor on last line for messages */
7597 msg_col = 0;
7598 screen_start(); /* don't know where cursor is now */
7599 msg_scrolled = 0; /* can't scroll back */
7600 msg_didany = FALSE;
7601 msg_didout = FALSE;
7605 * Clear one line in ScreenLines.
7607 static void
7608 lineclear(off, width)
7609 unsigned off;
7610 int width;
7612 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
7613 #ifdef FEAT_MBYTE
7614 if (enc_utf8)
7615 (void)vim_memset(ScreenLinesUC + off, 0,
7616 (size_t)width * sizeof(u8char_T));
7617 #endif
7618 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
7622 * Mark one line in ScreenLines invalid by setting the attributes to an
7623 * invalid value.
7625 static void
7626 lineinvalid(off, width)
7627 unsigned off;
7628 int width;
7630 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
7633 #ifdef FEAT_VERTSPLIT
7635 * Copy part of a Screenline for vertically split window "wp".
7637 static void
7638 linecopy(to, from, wp)
7639 int to;
7640 int from;
7641 win_T *wp;
7643 unsigned off_to = LineOffset[to] + wp->w_wincol;
7644 unsigned off_from = LineOffset[from] + wp->w_wincol;
7646 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
7647 wp->w_width * sizeof(schar_T));
7648 # ifdef FEAT_MBYTE
7649 if (enc_utf8)
7651 int i;
7653 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
7654 wp->w_width * sizeof(u8char_T));
7655 for (i = 0; i < p_mco; ++i)
7656 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
7657 wp->w_width * sizeof(u8char_T));
7659 if (enc_dbcs == DBCS_JPNU)
7660 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
7661 wp->w_width * sizeof(schar_T));
7662 # endif
7663 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
7664 wp->w_width * sizeof(sattr_T));
7666 #endif
7669 * Return TRUE if clearing with term string "p" would work.
7670 * It can't work when the string is empty or it won't set the right background.
7673 can_clear(p)
7674 char_u *p;
7676 return (*p != NUL && (t_colors <= 1
7677 #ifdef FEAT_GUI
7678 || gui.in_use
7679 #endif
7680 || cterm_normal_bg_color == 0 || *T_UT != NUL));
7684 * Reset cursor position. Use whenever cursor was moved because of outputting
7685 * something directly to the screen (shell commands) or a terminal control
7686 * code.
7688 void
7689 screen_start()
7691 screen_cur_row = screen_cur_col = 9999;
7695 * Move the cursor to position "row","col" in the screen.
7696 * This tries to find the most efficient way to move, minimizing the number of
7697 * characters sent to the terminal.
7699 void
7700 windgoto(row, col)
7701 int row;
7702 int col;
7704 sattr_T *p;
7705 int i;
7706 int plan;
7707 int cost;
7708 int wouldbe_col;
7709 int noinvcurs;
7710 char_u *bs;
7711 int goto_cost;
7712 int attr;
7714 #define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
7715 #define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
7717 #define PLAN_LE 1
7718 #define PLAN_CR 2
7719 #define PLAN_NL 3
7720 #define PLAN_WRITE 4
7721 /* Can't use ScreenLines unless initialized */
7722 if (ScreenLines == NULL)
7723 return;
7725 if (col != screen_cur_col || row != screen_cur_row)
7727 /* Check for valid position. */
7728 if (row < 0) /* window without text lines? */
7729 row = 0;
7730 if (row >= screen_Rows)
7731 row = screen_Rows - 1;
7732 if (col >= screen_Columns)
7733 col = screen_Columns - 1;
7735 /* check if no cursor movement is allowed in highlight mode */
7736 if (screen_attr && *T_MS == NUL)
7737 noinvcurs = HIGHL_COST;
7738 else
7739 noinvcurs = 0;
7740 goto_cost = GOTO_COST + noinvcurs;
7743 * Plan how to do the positioning:
7744 * 1. Use CR to move it to column 0, same row.
7745 * 2. Use T_LE to move it a few columns to the left.
7746 * 3. Use NL to move a few lines down, column 0.
7747 * 4. Move a few columns to the right with T_ND or by writing chars.
7749 * Don't do this if the cursor went beyond the last column, the cursor
7750 * position is unknown then (some terminals wrap, some don't )
7752 * First check if the highlighting attributes allow us to write
7753 * characters to move the cursor to the right.
7755 if (row >= screen_cur_row && screen_cur_col < Columns)
7758 * If the cursor is in the same row, bigger col, we can use CR
7759 * or T_LE.
7761 bs = NULL; /* init for GCC */
7762 attr = screen_attr;
7763 if (row == screen_cur_row && col < screen_cur_col)
7765 /* "le" is preferred over "bc", because "bc" is obsolete */
7766 if (*T_LE)
7767 bs = T_LE; /* "cursor left" */
7768 else
7769 bs = T_BC; /* "backspace character (old) */
7770 if (*bs)
7771 cost = (screen_cur_col - col) * (int)STRLEN(bs);
7772 else
7773 cost = 999;
7774 if (col + 1 < cost) /* using CR is less characters */
7776 plan = PLAN_CR;
7777 wouldbe_col = 0;
7778 cost = 1; /* CR is just one character */
7780 else
7782 plan = PLAN_LE;
7783 wouldbe_col = col;
7785 if (noinvcurs) /* will stop highlighting */
7787 cost += noinvcurs;
7788 attr = 0;
7793 * If the cursor is above where we want to be, we can use CR LF.
7795 else if (row > screen_cur_row)
7797 plan = PLAN_NL;
7798 wouldbe_col = 0;
7799 cost = (row - screen_cur_row) * 2; /* CR LF */
7800 if (noinvcurs) /* will stop highlighting */
7802 cost += noinvcurs;
7803 attr = 0;
7808 * If the cursor is in the same row, smaller col, just use write.
7810 else
7812 plan = PLAN_WRITE;
7813 wouldbe_col = screen_cur_col;
7814 cost = 0;
7818 * Check if any characters that need to be written have the
7819 * correct attributes. Also avoid UTF-8 characters.
7821 i = col - wouldbe_col;
7822 if (i > 0)
7823 cost += i;
7824 if (cost < goto_cost && i > 0)
7827 * Check if the attributes are correct without additionally
7828 * stopping highlighting.
7830 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
7831 while (i && *p++ == attr)
7832 --i;
7833 if (i != 0)
7836 * Try if it works when highlighting is stopped here.
7838 if (*--p == 0)
7840 cost += noinvcurs;
7841 while (i && *p++ == 0)
7842 --i;
7844 if (i != 0)
7845 cost = 999; /* different attributes, don't do it */
7847 #ifdef FEAT_MBYTE
7848 if (enc_utf8)
7850 /* Don't use an UTF-8 char for positioning, it's slow. */
7851 for (i = wouldbe_col; i < col; ++i)
7852 if (ScreenLinesUC[LineOffset[row] + i] != 0)
7854 cost = 999;
7855 break;
7858 #endif
7862 * We can do it without term_windgoto()!
7864 if (cost < goto_cost)
7866 if (plan == PLAN_LE)
7868 if (noinvcurs)
7869 screen_stop_highlight();
7870 while (screen_cur_col > col)
7872 out_str(bs);
7873 --screen_cur_col;
7876 else if (plan == PLAN_CR)
7878 if (noinvcurs)
7879 screen_stop_highlight();
7880 out_char('\r');
7881 screen_cur_col = 0;
7883 else if (plan == PLAN_NL)
7885 if (noinvcurs)
7886 screen_stop_highlight();
7887 while (screen_cur_row < row)
7889 out_char('\n');
7890 ++screen_cur_row;
7892 screen_cur_col = 0;
7895 i = col - screen_cur_col;
7896 if (i > 0)
7899 * Use cursor-right if it's one character only. Avoids
7900 * removing a line of pixels from the last bold char, when
7901 * using the bold trick in the GUI.
7903 if (T_ND[0] != NUL && T_ND[1] == NUL)
7905 while (i-- > 0)
7906 out_char(*T_ND);
7908 else
7910 int off;
7912 off = LineOffset[row] + screen_cur_col;
7913 while (i-- > 0)
7915 if (ScreenAttrs[off] != screen_attr)
7916 screen_stop_highlight();
7917 #ifdef FEAT_MBYTE
7918 out_flush_check();
7919 #endif
7920 out_char(ScreenLines[off]);
7921 #ifdef FEAT_MBYTE
7922 if (enc_dbcs == DBCS_JPNU
7923 && ScreenLines[off] == 0x8e)
7924 out_char(ScreenLines2[off]);
7925 #endif
7926 ++off;
7932 else
7933 cost = 999;
7935 if (cost >= goto_cost)
7937 if (noinvcurs)
7938 screen_stop_highlight();
7939 if (row == screen_cur_row && (col > screen_cur_col) &&
7940 *T_CRI != NUL)
7941 term_cursor_right(col - screen_cur_col);
7942 else
7943 term_windgoto(row, col);
7945 screen_cur_row = row;
7946 screen_cur_col = col;
7951 * Set cursor to its position in the current window.
7953 void
7954 setcursor()
7956 if (redrawing())
7958 validate_cursor();
7959 windgoto(W_WINROW(curwin) + curwin->w_wrow,
7960 W_WINCOL(curwin) + (
7961 #ifdef FEAT_RIGHTLEFT
7962 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
7963 # ifdef FEAT_MBYTE
7964 has_mbyte ? (*mb_ptr2cells)(ml_get_cursor()) :
7965 # endif
7966 1)) :
7967 #endif
7968 curwin->w_wcol));
7974 * insert 'line_count' lines at 'row' in window 'wp'
7975 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
7976 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
7977 * scrolling.
7978 * Returns FAIL if the lines are not inserted, OK for success.
7981 win_ins_lines(wp, row, line_count, invalid, mayclear)
7982 win_T *wp;
7983 int row;
7984 int line_count;
7985 int invalid;
7986 int mayclear;
7988 int did_delete;
7989 int nextrow;
7990 int lastrow;
7991 int retval;
7993 if (invalid)
7994 wp->w_lines_valid = 0;
7996 if (wp->w_height < 5)
7997 return FAIL;
7999 if (line_count > wp->w_height - row)
8000 line_count = wp->w_height - row;
8002 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
8003 if (retval != MAYBE)
8004 return retval;
8007 * If there is a next window or a status line, we first try to delete the
8008 * lines at the bottom to avoid messing what is after the window.
8009 * If this fails and there are following windows, don't do anything to avoid
8010 * messing up those windows, better just redraw.
8012 did_delete = FALSE;
8013 #ifdef FEAT_WINDOWS
8014 if (wp->w_next != NULL || wp->w_status_height)
8016 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8017 line_count, (int)Rows, FALSE, NULL) == OK)
8018 did_delete = TRUE;
8019 else if (wp->w_next)
8020 return FAIL;
8022 #endif
8024 * if no lines deleted, blank the lines that will end up below the window
8026 if (!did_delete)
8028 #ifdef FEAT_WINDOWS
8029 wp->w_redr_status = TRUE;
8030 #endif
8031 redraw_cmdline = TRUE;
8032 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
8033 lastrow = nextrow + line_count;
8034 if (lastrow > Rows)
8035 lastrow = Rows;
8036 screen_fill(nextrow - line_count, lastrow - line_count,
8037 W_WINCOL(wp), (int)W_ENDCOL(wp),
8038 ' ', ' ', 0);
8041 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
8042 == FAIL)
8044 /* deletion will have messed up other windows */
8045 if (did_delete)
8047 #ifdef FEAT_WINDOWS
8048 wp->w_redr_status = TRUE;
8049 #endif
8050 win_rest_invalid(W_NEXT(wp));
8052 return FAIL;
8055 return OK;
8059 * delete "line_count" window lines at "row" in window "wp"
8060 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
8061 * If "mayclear" is TRUE the screen will be cleared if it is faster than
8062 * scrolling
8063 * Return OK for success, FAIL if the lines are not deleted.
8066 win_del_lines(wp, row, line_count, invalid, mayclear)
8067 win_T *wp;
8068 int row;
8069 int line_count;
8070 int invalid;
8071 int mayclear;
8073 int retval;
8075 if (invalid)
8076 wp->w_lines_valid = 0;
8078 if (line_count > wp->w_height - row)
8079 line_count = wp->w_height - row;
8081 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
8082 if (retval != MAYBE)
8083 return retval;
8085 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
8086 (int)Rows, FALSE, NULL) == FAIL)
8087 return FAIL;
8089 #ifdef FEAT_WINDOWS
8091 * If there are windows or status lines below, try to put them at the
8092 * correct place. If we can't do that, they have to be redrawn.
8094 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
8096 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8097 line_count, (int)Rows, NULL) == FAIL)
8099 wp->w_redr_status = TRUE;
8100 win_rest_invalid(wp->w_next);
8104 * If this is the last window and there is no status line, redraw the
8105 * command line later.
8107 else
8108 #endif
8109 redraw_cmdline = TRUE;
8110 return OK;
8114 * Common code for win_ins_lines() and win_del_lines().
8115 * Returns OK or FAIL when the work has been done.
8116 * Returns MAYBE when not finished yet.
8118 static int
8119 win_do_lines(wp, row, line_count, mayclear, del)
8120 win_T *wp;
8121 int row;
8122 int line_count;
8123 int mayclear;
8124 int del;
8126 int retval;
8128 if (!redrawing() || line_count <= 0)
8129 return FAIL;
8131 /* only a few lines left: redraw is faster */
8132 if (mayclear && Rows - line_count < 5
8133 #ifdef FEAT_VERTSPLIT
8134 && wp->w_width == Columns
8135 #endif
8138 screenclear(); /* will set wp->w_lines_valid to 0 */
8139 return FAIL;
8143 * Delete all remaining lines
8145 if (row + line_count >= wp->w_height)
8147 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
8148 W_WINCOL(wp), (int)W_ENDCOL(wp),
8149 ' ', ' ', 0);
8150 return OK;
8154 * when scrolling, the message on the command line should be cleared,
8155 * otherwise it will stay there forever.
8157 clear_cmdline = TRUE;
8160 * If the terminal can set a scroll region, use that.
8161 * Always do this in a vertically split window. This will redraw from
8162 * ScreenLines[] when t_CV isn't defined. That's faster than using
8163 * win_line().
8164 * Don't use a scroll region when we are going to redraw the text, writing
8165 * a character in the lower right corner of the scroll region causes a
8166 * scroll-up in the DJGPP version.
8168 if (scroll_region
8169 #ifdef FEAT_VERTSPLIT
8170 || W_WIDTH(wp) != Columns
8171 #endif
8174 #ifdef FEAT_VERTSPLIT
8175 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8176 #endif
8177 scroll_region_set(wp, row);
8178 if (del)
8179 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
8180 wp->w_height - row, FALSE, wp);
8181 else
8182 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
8183 wp->w_height - row, wp);
8184 #ifdef FEAT_VERTSPLIT
8185 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8186 #endif
8187 scroll_region_reset();
8188 return retval;
8191 #ifdef FEAT_WINDOWS
8192 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
8193 return FAIL;
8194 #endif
8196 return MAYBE;
8200 * window 'wp' and everything after it is messed up, mark it for redraw
8202 static void
8203 win_rest_invalid(wp)
8204 win_T *wp;
8206 #ifdef FEAT_WINDOWS
8207 while (wp != NULL)
8208 #else
8209 if (wp != NULL)
8210 #endif
8212 redraw_win_later(wp, NOT_VALID);
8213 #ifdef FEAT_WINDOWS
8214 wp->w_redr_status = TRUE;
8215 wp = wp->w_next;
8216 #endif
8218 redraw_cmdline = TRUE;
8222 * The rest of the routines in this file perform screen manipulations. The
8223 * given operation is performed physically on the screen. The corresponding
8224 * change is also made to the internal screen image. In this way, the editor
8225 * anticipates the effect of editing changes on the appearance of the screen.
8226 * That way, when we call screenupdate a complete redraw isn't usually
8227 * necessary. Another advantage is that we can keep adding code to anticipate
8228 * screen changes, and in the meantime, everything still works.
8232 * types for inserting or deleting lines
8234 #define USE_T_CAL 1
8235 #define USE_T_CDL 2
8236 #define USE_T_AL 3
8237 #define USE_T_CE 4
8238 #define USE_T_DL 5
8239 #define USE_T_SR 6
8240 #define USE_NL 7
8241 #define USE_T_CD 8
8242 #define USE_REDRAW 9
8245 * insert lines on the screen and update ScreenLines[]
8246 * 'end' is the line after the scrolled part. Normally it is Rows.
8247 * When scrolling region used 'off' is the offset from the top for the region.
8248 * 'row' and 'end' are relative to the start of the region.
8250 * return FAIL for failure, OK for success.
8253 screen_ins_lines(off, row, line_count, end, wp)
8254 int off;
8255 int row;
8256 int line_count;
8257 int end;
8258 win_T *wp; /* NULL or window to use width from */
8260 int i;
8261 int j;
8262 unsigned temp;
8263 int cursor_row;
8264 int type;
8265 int result_empty;
8266 int can_ce = can_clear(T_CE);
8269 * FAIL if
8270 * - there is no valid screen
8271 * - the screen has to be redrawn completely
8272 * - the line count is less than one
8273 * - the line count is more than 'ttyscroll'
8275 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
8276 return FAIL;
8279 * There are seven ways to insert lines:
8280 * 0. When in a vertically split window and t_CV isn't set, redraw the
8281 * characters from ScreenLines[].
8282 * 1. Use T_CD (clear to end of display) if it exists and the result of
8283 * the insert is just empty lines
8284 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
8285 * present or line_count > 1. It looks better if we do all the inserts
8286 * at once.
8287 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
8288 * insert is just empty lines and T_CE is not present or line_count >
8289 * 1.
8290 * 4. Use T_AL (insert line) if it exists.
8291 * 5. Use T_CE (erase line) if it exists and the result of the insert is
8292 * just empty lines.
8293 * 6. Use T_DL (delete line) if it exists and the result of the insert is
8294 * just empty lines.
8295 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
8296 * the 'da' flag is not set or we have clear line capability.
8297 * 8. redraw the characters from ScreenLines[].
8299 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
8300 * the scrollbar for the window. It does have insert line, use that if it
8301 * exists.
8303 result_empty = (row + line_count >= end);
8304 #ifdef FEAT_VERTSPLIT
8305 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
8306 type = USE_REDRAW;
8307 else
8308 #endif
8309 if (can_clear(T_CD) && result_empty)
8310 type = USE_T_CD;
8311 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
8312 type = USE_T_CAL;
8313 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
8314 type = USE_T_CDL;
8315 else if (*T_AL != NUL)
8316 type = USE_T_AL;
8317 else if (can_ce && result_empty)
8318 type = USE_T_CE;
8319 else if (*T_DL != NUL && result_empty)
8320 type = USE_T_DL;
8321 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
8322 type = USE_T_SR;
8323 else
8324 return FAIL;
8327 * For clearing the lines screen_del_lines() is used. This will also take
8328 * care of t_db if necessary.
8330 if (type == USE_T_CD || type == USE_T_CDL ||
8331 type == USE_T_CE || type == USE_T_DL)
8332 return screen_del_lines(off, row, line_count, end, FALSE, wp);
8335 * If text is retained below the screen, first clear or delete as many
8336 * lines at the bottom of the window as are about to be inserted so that
8337 * the deleted lines won't later surface during a screen_del_lines.
8339 if (*T_DB)
8340 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
8342 #ifdef FEAT_CLIPBOARD
8343 /* Remove a modeless selection when inserting lines halfway the screen
8344 * or not the full width of the screen. */
8345 if (off + row > 0
8346 # ifdef FEAT_VERTSPLIT
8347 || (wp != NULL && wp->w_width != Columns)
8348 # endif
8350 clip_clear_selection();
8351 else
8352 clip_scroll_selection(-line_count);
8353 #endif
8355 #ifdef FEAT_GUI
8356 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
8357 * scrolling is actually carried out. */
8358 gui_dont_update_cursor();
8359 #endif
8361 if (*T_CCS != NUL) /* cursor relative to region */
8362 cursor_row = row;
8363 else
8364 cursor_row = row + off;
8367 * Shift LineOffset[] line_count down to reflect the inserted lines.
8368 * Clear the inserted lines in ScreenLines[].
8370 row += off;
8371 end += off;
8372 for (i = 0; i < line_count; ++i)
8374 #ifdef FEAT_VERTSPLIT
8375 if (wp != NULL && wp->w_width != Columns)
8377 /* need to copy part of a line */
8378 j = end - 1 - i;
8379 while ((j -= line_count) >= row)
8380 linecopy(j + line_count, j, wp);
8381 j += line_count;
8382 if (can_clear((char_u *)" "))
8383 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
8384 else
8385 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
8386 LineWraps[j] = FALSE;
8388 else
8389 #endif
8391 j = end - 1 - i;
8392 temp = LineOffset[j];
8393 while ((j -= line_count) >= row)
8395 LineOffset[j + line_count] = LineOffset[j];
8396 LineWraps[j + line_count] = LineWraps[j];
8398 LineOffset[j + line_count] = temp;
8399 LineWraps[j + line_count] = FALSE;
8400 if (can_clear((char_u *)" "))
8401 lineclear(temp, (int)Columns);
8402 else
8403 lineinvalid(temp, (int)Columns);
8407 screen_stop_highlight();
8408 windgoto(cursor_row, 0);
8410 #ifdef FEAT_VERTSPLIT
8411 /* redraw the characters */
8412 if (type == USE_REDRAW)
8413 redraw_block(row, end, wp);
8414 else
8415 #endif
8416 if (type == USE_T_CAL)
8418 term_append_lines(line_count);
8419 screen_start(); /* don't know where cursor is now */
8421 else
8423 for (i = 0; i < line_count; i++)
8425 if (type == USE_T_AL)
8427 if (i && cursor_row != 0)
8428 windgoto(cursor_row, 0);
8429 out_str(T_AL);
8431 else /* type == USE_T_SR */
8432 out_str(T_SR);
8433 screen_start(); /* don't know where cursor is now */
8438 * With scroll-reverse and 'da' flag set we need to clear the lines that
8439 * have been scrolled down into the region.
8441 if (type == USE_T_SR && *T_DA)
8443 for (i = 0; i < line_count; ++i)
8445 windgoto(off + i, 0);
8446 out_str(T_CE);
8447 screen_start(); /* don't know where cursor is now */
8451 #ifdef FEAT_GUI
8452 gui_can_update_cursor();
8453 if (gui.in_use)
8454 out_flush(); /* always flush after a scroll */
8455 #endif
8456 return OK;
8460 * delete lines on the screen and update ScreenLines[]
8461 * 'end' is the line after the scrolled part. Normally it is Rows.
8462 * When scrolling region used 'off' is the offset from the top for the region.
8463 * 'row' and 'end' are relative to the start of the region.
8465 * Return OK for success, FAIL if the lines are not deleted.
8467 /*ARGSUSED*/
8469 screen_del_lines(off, row, line_count, end, force, wp)
8470 int off;
8471 int row;
8472 int line_count;
8473 int end;
8474 int force; /* even when line_count > p_ttyscroll */
8475 win_T *wp; /* NULL or window to use width from */
8477 int j;
8478 int i;
8479 unsigned temp;
8480 int cursor_row;
8481 int cursor_end;
8482 int result_empty; /* result is empty until end of region */
8483 int can_delete; /* deleting line codes can be used */
8484 int type;
8487 * FAIL if
8488 * - there is no valid screen
8489 * - the screen has to be redrawn completely
8490 * - the line count is less than one
8491 * - the line count is more than 'ttyscroll'
8493 if (!screen_valid(TRUE) || line_count <= 0 ||
8494 (!force && line_count > p_ttyscroll))
8495 return FAIL;
8498 * Check if the rest of the current region will become empty.
8500 result_empty = row + line_count >= end;
8503 * We can delete lines only when 'db' flag not set or when 'ce' option
8504 * available.
8506 can_delete = (*T_DB == NUL || can_clear(T_CE));
8509 * There are six ways to delete lines:
8510 * 0. When in a vertically split window and t_CV isn't set, redraw the
8511 * characters from ScreenLines[].
8512 * 1. Use T_CD if it exists and the result is empty.
8513 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
8514 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
8515 * none of the other ways work.
8516 * 4. Use T_CE (erase line) if the result is empty.
8517 * 5. Use T_DL (delete line) if it exists.
8518 * 6. redraw the characters from ScreenLines[].
8520 #ifdef FEAT_VERTSPLIT
8521 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
8522 type = USE_REDRAW;
8523 else
8524 #endif
8525 if (can_clear(T_CD) && result_empty)
8526 type = USE_T_CD;
8527 #if defined(__BEOS__) && defined(BEOS_DR8)
8529 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
8530 * its internal termcap... this works okay for tests which test *T_DB !=
8531 * NUL. It has the disadvantage that the user cannot use any :set t_*
8532 * command to get T_DB (back) to empty_option, only :set term=... will do
8533 * the trick...
8534 * Anyway, this hack will hopefully go away with the next OS release.
8535 * (Olaf Seibert)
8537 else if (row == 0 && T_DB == empty_option
8538 && (line_count == 1 || *T_CDL == NUL))
8539 #else
8540 else if (row == 0 && (
8541 #ifndef AMIGA
8542 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
8543 * up, so use delete-line command */
8544 line_count == 1 ||
8545 #endif
8546 *T_CDL == NUL))
8547 #endif
8548 type = USE_NL;
8549 else if (*T_CDL != NUL && line_count > 1 && can_delete)
8550 type = USE_T_CDL;
8551 else if (can_clear(T_CE) && result_empty
8552 #ifdef FEAT_VERTSPLIT
8553 && (wp == NULL || wp->w_width == Columns)
8554 #endif
8556 type = USE_T_CE;
8557 else if (*T_DL != NUL && can_delete)
8558 type = USE_T_DL;
8559 else if (*T_CDL != NUL && can_delete)
8560 type = USE_T_CDL;
8561 else
8562 return FAIL;
8564 #ifdef FEAT_CLIPBOARD
8565 /* Remove a modeless selection when deleting lines halfway the screen or
8566 * not the full width of the screen. */
8567 if (off + row > 0
8568 # ifdef FEAT_VERTSPLIT
8569 || (wp != NULL && wp->w_width != Columns)
8570 # endif
8572 clip_clear_selection();
8573 else
8574 clip_scroll_selection(line_count);
8575 #endif
8577 #ifdef FEAT_GUI
8578 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
8579 * scrolling is actually carried out. */
8580 gui_dont_update_cursor();
8581 #endif
8583 if (*T_CCS != NUL) /* cursor relative to region */
8585 cursor_row = row;
8586 cursor_end = end;
8588 else
8590 cursor_row = row + off;
8591 cursor_end = end + off;
8595 * Now shift LineOffset[] line_count up to reflect the deleted lines.
8596 * Clear the inserted lines in ScreenLines[].
8598 row += off;
8599 end += off;
8600 for (i = 0; i < line_count; ++i)
8602 #ifdef FEAT_VERTSPLIT
8603 if (wp != NULL && wp->w_width != Columns)
8605 /* need to copy part of a line */
8606 j = row + i;
8607 while ((j += line_count) <= end - 1)
8608 linecopy(j - line_count, j, wp);
8609 j -= line_count;
8610 if (can_clear((char_u *)" "))
8611 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
8612 else
8613 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
8614 LineWraps[j] = FALSE;
8616 else
8617 #endif
8619 /* whole width, moving the line pointers is faster */
8620 j = row + i;
8621 temp = LineOffset[j];
8622 while ((j += line_count) <= end - 1)
8624 LineOffset[j - line_count] = LineOffset[j];
8625 LineWraps[j - line_count] = LineWraps[j];
8627 LineOffset[j - line_count] = temp;
8628 LineWraps[j - line_count] = FALSE;
8629 if (can_clear((char_u *)" "))
8630 lineclear(temp, (int)Columns);
8631 else
8632 lineinvalid(temp, (int)Columns);
8636 screen_stop_highlight();
8638 #ifdef FEAT_VERTSPLIT
8639 /* redraw the characters */
8640 if (type == USE_REDRAW)
8641 redraw_block(row, end, wp);
8642 else
8643 #endif
8644 if (type == USE_T_CD) /* delete the lines */
8646 windgoto(cursor_row, 0);
8647 out_str(T_CD);
8648 screen_start(); /* don't know where cursor is now */
8650 else if (type == USE_T_CDL)
8652 windgoto(cursor_row, 0);
8653 term_delete_lines(line_count);
8654 screen_start(); /* don't know where cursor is now */
8657 * Deleting lines at top of the screen or scroll region: Just scroll
8658 * the whole screen (scroll region) up by outputting newlines on the
8659 * last line.
8661 else if (type == USE_NL)
8663 windgoto(cursor_end - 1, 0);
8664 for (i = line_count; --i >= 0; )
8665 out_char('\n'); /* cursor will remain on same line */
8667 else
8669 for (i = line_count; --i >= 0; )
8671 if (type == USE_T_DL)
8673 windgoto(cursor_row, 0);
8674 out_str(T_DL); /* delete a line */
8676 else /* type == USE_T_CE */
8678 windgoto(cursor_row + i, 0);
8679 out_str(T_CE); /* erase a line */
8681 screen_start(); /* don't know where cursor is now */
8686 * If the 'db' flag is set, we need to clear the lines that have been
8687 * scrolled up at the bottom of the region.
8689 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
8691 for (i = line_count; i > 0; --i)
8693 windgoto(cursor_end - i, 0);
8694 out_str(T_CE); /* erase a line */
8695 screen_start(); /* don't know where cursor is now */
8699 #ifdef FEAT_GUI
8700 gui_can_update_cursor();
8701 if (gui.in_use)
8702 out_flush(); /* always flush after a scroll */
8703 #endif
8705 return OK;
8709 * show the current mode and ruler
8711 * If clear_cmdline is TRUE, clear the rest of the cmdline.
8712 * If clear_cmdline is FALSE there may be a message there that needs to be
8713 * cleared only if a mode is shown.
8714 * Return the length of the message (0 if no message).
8717 showmode()
8719 int need_clear;
8720 int length = 0;
8721 int do_mode;
8722 int attr;
8723 int nwr_save;
8724 #ifdef FEAT_INS_EXPAND
8725 int sub_attr;
8726 #endif
8728 do_mode = ((p_smd && msg_silent == 0)
8729 && ((State & INSERT)
8730 || restart_edit
8731 #ifdef FEAT_VISUAL
8732 || VIsual_active
8733 #endif
8735 if (do_mode || Recording)
8738 * Don't show mode right now, when not redrawing or inside a mapping.
8739 * Call char_avail() only when we are going to show something, because
8740 * it takes a bit of time.
8742 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
8744 redraw_cmdline = TRUE; /* show mode later */
8745 return 0;
8748 nwr_save = need_wait_return;
8750 /* wait a bit before overwriting an important message */
8751 check_for_delay(FALSE);
8753 /* if the cmdline is more than one line high, erase top lines */
8754 need_clear = clear_cmdline;
8755 if (clear_cmdline && cmdline_row < Rows - 1)
8756 msg_clr_cmdline(); /* will reset clear_cmdline */
8758 /* Position on the last line in the window, column 0 */
8759 msg_pos_mode();
8760 cursor_off();
8761 attr = hl_attr(HLF_CM); /* Highlight mode */
8762 if (do_mode)
8764 MSG_PUTS_ATTR("--", attr);
8765 #if defined(FEAT_XIM)
8766 if (xic != NULL && im_get_status() && !p_imdisable
8767 && curbuf->b_p_iminsert == B_IMODE_IM)
8768 # ifdef HAVE_GTK2 /* most of the time, it's not XIM being used */
8769 MSG_PUTS_ATTR(" IM", attr);
8770 # else
8771 MSG_PUTS_ATTR(" XIM", attr);
8772 # endif
8773 #endif
8774 #if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
8775 if (gui.in_use)
8777 if (hangul_input_state_get())
8778 MSG_PUTS_ATTR(" \307\321\261\333", attr); /* HANGUL */
8780 #endif
8781 #ifdef FEAT_INS_EXPAND
8782 if (edit_submode != NULL) /* CTRL-X in Insert mode */
8784 /* These messages can get long, avoid a wrap in a narrow
8785 * window. Prefer showing edit_submode_extra. */
8786 length = (Rows - msg_row) * Columns - 3;
8787 if (edit_submode_extra != NULL)
8788 length -= vim_strsize(edit_submode_extra);
8789 if (length > 0)
8791 if (edit_submode_pre != NULL)
8792 length -= vim_strsize(edit_submode_pre);
8793 if (length - vim_strsize(edit_submode) > 0)
8795 if (edit_submode_pre != NULL)
8796 msg_puts_attr(edit_submode_pre, attr);
8797 msg_puts_attr(edit_submode, attr);
8799 if (edit_submode_extra != NULL)
8801 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
8802 if ((int)edit_submode_highl < (int)HLF_COUNT)
8803 sub_attr = hl_attr(edit_submode_highl);
8804 else
8805 sub_attr = attr;
8806 msg_puts_attr(edit_submode_extra, sub_attr);
8809 length = 0;
8811 else
8812 #endif
8814 #ifdef FEAT_VREPLACE
8815 if (State & VREPLACE_FLAG)
8816 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
8817 else
8818 #endif
8819 if (State & REPLACE_FLAG)
8820 MSG_PUTS_ATTR(_(" REPLACE"), attr);
8821 else if (State & INSERT)
8823 #ifdef FEAT_RIGHTLEFT
8824 if (p_ri)
8825 MSG_PUTS_ATTR(_(" REVERSE"), attr);
8826 #endif
8827 MSG_PUTS_ATTR(_(" INSERT"), attr);
8829 else if (restart_edit == 'I')
8830 MSG_PUTS_ATTR(_(" (insert)"), attr);
8831 else if (restart_edit == 'R')
8832 MSG_PUTS_ATTR(_(" (replace)"), attr);
8833 else if (restart_edit == 'V')
8834 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
8835 #ifdef FEAT_RIGHTLEFT
8836 if (p_hkmap)
8837 MSG_PUTS_ATTR(_(" Hebrew"), attr);
8838 # ifdef FEAT_FKMAP
8839 if (p_fkmap)
8840 MSG_PUTS_ATTR(farsi_text_5, attr);
8841 # endif
8842 #endif
8843 #ifdef FEAT_KEYMAP
8844 if (State & LANGMAP)
8846 # ifdef FEAT_ARABIC
8847 if (curwin->w_p_arab)
8848 MSG_PUTS_ATTR(_(" Arabic"), attr);
8849 else
8850 # endif
8851 MSG_PUTS_ATTR(_(" (lang)"), attr);
8853 #endif
8854 if ((State & INSERT) && p_paste)
8855 MSG_PUTS_ATTR(_(" (paste)"), attr);
8857 #ifdef FEAT_VISUAL
8858 if (VIsual_active)
8860 char *p;
8862 /* Don't concatenate separate words to avoid translation
8863 * problems. */
8864 switch ((VIsual_select ? 4 : 0)
8865 + (VIsual_mode == Ctrl_V) * 2
8866 + (VIsual_mode == 'V'))
8868 case 0: p = N_(" VISUAL"); break;
8869 case 1: p = N_(" VISUAL LINE"); break;
8870 case 2: p = N_(" VISUAL BLOCK"); break;
8871 case 4: p = N_(" SELECT"); break;
8872 case 5: p = N_(" SELECT LINE"); break;
8873 default: p = N_(" SELECT BLOCK"); break;
8875 MSG_PUTS_ATTR(_(p), attr);
8877 #endif
8878 MSG_PUTS_ATTR(" --", attr);
8881 need_clear = TRUE;
8883 if (Recording
8884 #ifdef FEAT_INS_EXPAND
8885 && edit_submode == NULL /* otherwise it gets too long */
8886 #endif
8889 MSG_PUTS_ATTR(_("recording"), attr);
8890 need_clear = TRUE;
8893 mode_displayed = TRUE;
8894 if (need_clear || clear_cmdline)
8895 msg_clr_eos();
8896 msg_didout = FALSE; /* overwrite this message */
8897 length = msg_col;
8898 msg_col = 0;
8899 need_wait_return = nwr_save; /* never ask for hit-return for this */
8901 else if (clear_cmdline && msg_silent == 0)
8902 /* Clear the whole command line. Will reset "clear_cmdline". */
8903 msg_clr_cmdline();
8905 #ifdef FEAT_CMDL_INFO
8906 # ifdef FEAT_VISUAL
8907 /* In Visual mode the size of the selected area must be redrawn. */
8908 if (VIsual_active)
8909 clear_showcmd();
8910 # endif
8912 /* If the last window has no status line, the ruler is after the mode
8913 * message and must be redrawn */
8914 if (redrawing()
8915 # ifdef FEAT_WINDOWS
8916 && lastwin->w_status_height == 0
8917 # endif
8919 win_redr_ruler(lastwin, TRUE);
8920 #endif
8921 redraw_cmdline = FALSE;
8922 clear_cmdline = FALSE;
8924 return length;
8928 * Position for a mode message.
8930 static void
8931 msg_pos_mode()
8933 msg_col = 0;
8934 msg_row = Rows - 1;
8938 * Delete mode message. Used when ESC is typed which is expected to end
8939 * Insert mode (but Insert mode didn't end yet!).
8940 * Caller should check "mode_displayed".
8942 void
8943 unshowmode(force)
8944 int force;
8947 * Don't delete it right now, when not redrawing or insided a mapping.
8949 if (!redrawing() || (!force && char_avail() && !KeyTyped))
8950 redraw_cmdline = TRUE; /* delete mode later */
8951 else
8953 msg_pos_mode();
8954 if (Recording)
8955 MSG_PUTS_ATTR(_("recording"), hl_attr(HLF_CM));
8956 msg_clr_eos();
8960 #if defined(FEAT_WINDOWS)
8962 * Draw the tab pages line at the top of the Vim window.
8964 static void
8965 draw_tabline()
8967 int tabcount = 0;
8968 tabpage_T *tp;
8969 int tabwidth;
8970 int col = 0;
8971 int scol = 0;
8972 int attr;
8973 win_T *wp;
8974 win_T *cwp;
8975 int wincount;
8976 int modified;
8977 int c;
8978 int len;
8979 int attr_sel = hl_attr(HLF_TPS);
8980 int attr_nosel = hl_attr(HLF_TP);
8981 int attr_fill = hl_attr(HLF_TPF);
8982 char_u *p;
8983 int room;
8984 int use_sep_chars = (t_colors < 8
8985 #ifdef FEAT_GUI
8986 && !gui.in_use
8987 #endif
8990 redraw_tabline = FALSE;
8992 #ifdef FEAT_GUI_TABLINE
8993 /* Take care of a GUI tabline. */
8994 if (gui_use_tabline())
8996 gui_update_tabline();
8997 return;
8999 #endif
9001 if (tabline_height() < 1)
9002 return;
9004 #if defined(FEAT_STL_OPT)
9006 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
9007 for (scol = 0; scol < Columns; ++scol)
9008 TabPageIdxs[scol] = 0;
9010 /* Use the 'tabline' option if it's set. */
9011 if (*p_tal != NUL)
9013 int save_called_emsg = called_emsg;
9015 /* Check for an error. If there is one we would loop in redrawing the
9016 * screen. Avoid that by making 'tabline' empty. */
9017 called_emsg = FALSE;
9018 win_redr_custom(NULL, FALSE);
9019 if (called_emsg)
9020 set_string_option_direct((char_u *)"tabline", -1,
9021 (char_u *)"", OPT_FREE, SID_ERROR);
9022 called_emsg |= save_called_emsg;
9024 else
9025 #endif
9027 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
9028 ++tabcount;
9030 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
9031 if (tabwidth < 6)
9032 tabwidth = 6;
9034 attr = attr_nosel;
9035 tabcount = 0;
9036 scol = 0;
9037 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
9038 tp = tp->tp_next)
9040 scol = col;
9042 if (tp->tp_topframe == topframe)
9043 attr = attr_sel;
9044 if (use_sep_chars && col > 0)
9045 screen_putchar('|', 0, col++, attr);
9047 if (tp->tp_topframe != topframe)
9048 attr = attr_nosel;
9050 screen_putchar(' ', 0, col++, attr);
9052 if (tp == curtab)
9054 cwp = curwin;
9055 wp = firstwin;
9057 else
9059 cwp = tp->tp_curwin;
9060 wp = tp->tp_firstwin;
9063 modified = FALSE;
9064 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
9065 if (bufIsChanged(wp->w_buffer))
9066 modified = TRUE;
9067 if (modified || wincount > 1)
9069 if (wincount > 1)
9071 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
9072 len = (int)STRLEN(NameBuff);
9073 if (col + len >= Columns - 3)
9074 break;
9075 screen_puts_len(NameBuff, len, 0, col,
9076 #if defined(FEAT_SYN_HL)
9077 hl_combine_attr(attr, hl_attr(HLF_T))
9078 #else
9079 attr
9080 #endif
9082 col += len;
9084 if (modified)
9085 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
9086 screen_putchar(' ', 0, col++, attr);
9089 room = scol - col + tabwidth - 1;
9090 if (room > 0)
9092 /* Get buffer name in NameBuff[] */
9093 get_trans_bufname(cwp->w_buffer);
9094 shorten_dir(NameBuff);
9095 len = vim_strsize(NameBuff);
9096 p = NameBuff;
9097 #ifdef FEAT_MBYTE
9098 if (has_mbyte)
9099 while (len > room)
9101 len -= ptr2cells(p);
9102 mb_ptr_adv(p);
9104 else
9105 #endif
9106 if (len > room)
9108 p += len - room;
9109 len = room;
9111 if (len > Columns - col - 1)
9112 len = Columns - col - 1;
9114 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
9115 col += len;
9117 screen_putchar(' ', 0, col++, attr);
9119 /* Store the tab page number in TabPageIdxs[], so that
9120 * jump_to_mouse() knows where each one is. */
9121 ++tabcount;
9122 while (scol < col)
9123 TabPageIdxs[scol++] = tabcount;
9126 if (use_sep_chars)
9127 c = '_';
9128 else
9129 c = ' ';
9130 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
9132 /* Put an "X" for closing the current tab if there are several. */
9133 if (first_tabpage->tp_next != NULL)
9135 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
9136 TabPageIdxs[Columns - 1] = -999;
9140 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
9141 * set. */
9142 redraw_tabline = FALSE;
9146 * Get buffer name for "buf" into NameBuff[].
9147 * Takes care of special buffer names and translates special characters.
9149 void
9150 get_trans_bufname(buf)
9151 buf_T *buf;
9153 if (buf_spname(buf) != NULL)
9154 STRCPY(NameBuff, buf_spname(buf));
9155 else
9156 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
9157 trans_characters(NameBuff, MAXPATHL);
9159 #endif
9161 #if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
9163 * Get the character to use in a status line. Get its attributes in "*attr".
9165 static int
9166 fillchar_status(attr, is_curwin)
9167 int *attr;
9168 int is_curwin;
9170 int fill;
9171 if (is_curwin)
9173 *attr = hl_attr(HLF_S);
9174 fill = fill_stl;
9176 else
9178 *attr = hl_attr(HLF_SNC);
9179 fill = fill_stlnc;
9181 /* Use fill when there is highlighting, and highlighting of current
9182 * window differs, or the fillchars differ, or this is not the
9183 * current window */
9184 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
9185 || !is_curwin || firstwin == lastwin)
9186 || (fill_stl != fill_stlnc)))
9187 return fill;
9188 if (is_curwin)
9189 return '^';
9190 return '=';
9192 #endif
9194 #ifdef FEAT_VERTSPLIT
9196 * Get the character to use in a separator between vertically split windows.
9197 * Get its attributes in "*attr".
9199 static int
9200 fillchar_vsep(attr)
9201 int *attr;
9203 *attr = hl_attr(HLF_C);
9204 if (*attr == 0 && fill_vert == ' ')
9205 return '|';
9206 else
9207 return fill_vert;
9209 #endif
9212 * Return TRUE if redrawing should currently be done.
9215 redrawing()
9217 return (!RedrawingDisabled
9218 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
9222 * Return TRUE if printing messages should currently be done.
9225 messaging()
9227 return (!(p_lz && char_avail() && !KeyTyped));
9231 * Show current status info in ruler and various other places
9232 * If always is FALSE, only show ruler if position has changed.
9234 void
9235 showruler(always)
9236 int always;
9238 if (!always && !redrawing())
9239 return;
9240 #ifdef FEAT_INS_EXPAND
9241 if (pum_visible())
9243 # ifdef FEAT_WINDOWS
9244 /* Don't redraw right now, do it later. */
9245 curwin->w_redr_status = TRUE;
9246 # endif
9247 return;
9249 #endif
9250 #if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
9251 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
9253 redraw_custum_statusline(curwin);
9255 else
9256 #endif
9257 #ifdef FEAT_CMDL_INFO
9258 win_redr_ruler(curwin, always);
9259 #endif
9261 #ifdef FEAT_TITLE
9262 if (need_maketitle
9263 # ifdef FEAT_STL_OPT
9264 || (p_icon && (stl_syntax & STL_IN_ICON))
9265 || (p_title && (stl_syntax & STL_IN_TITLE))
9266 # endif
9268 maketitle();
9269 #endif
9272 #ifdef FEAT_CMDL_INFO
9273 static void
9274 win_redr_ruler(wp, always)
9275 win_T *wp;
9276 int always;
9278 char_u buffer[70];
9279 int row;
9280 int fillchar;
9281 int attr;
9282 int empty_line = FALSE;
9283 colnr_T virtcol;
9284 int i;
9285 int o;
9286 #ifdef FEAT_VERTSPLIT
9287 int this_ru_col;
9288 int off = 0;
9289 int width = Columns;
9290 # define WITH_OFF(x) x
9291 # define WITH_WIDTH(x) x
9292 #else
9293 # define WITH_OFF(x) 0
9294 # define WITH_WIDTH(x) Columns
9295 # define this_ru_col ru_col
9296 #endif
9298 /* If 'ruler' off or redrawing disabled, don't do anything */
9299 if (!p_ru)
9300 return;
9303 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
9304 * after deleting lines, before cursor.lnum is corrected.
9306 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
9307 return;
9309 #ifdef FEAT_INS_EXPAND
9310 /* Don't draw the ruler while doing insert-completion, it might overwrite
9311 * the (long) mode message. */
9312 # ifdef FEAT_WINDOWS
9313 if (wp == lastwin && lastwin->w_status_height == 0)
9314 # endif
9315 if (edit_submode != NULL)
9316 return;
9317 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
9318 if (pum_visible())
9319 return;
9320 #endif
9322 #ifdef FEAT_STL_OPT
9323 if (*p_ruf)
9325 int save_called_emsg = called_emsg;
9327 called_emsg = FALSE;
9328 win_redr_custom(wp, TRUE);
9329 if (called_emsg)
9330 set_string_option_direct((char_u *)"rulerformat", -1,
9331 (char_u *)"", OPT_FREE, SID_ERROR);
9332 called_emsg |= save_called_emsg;
9333 return;
9335 #endif
9338 * Check if not in Insert mode and the line is empty (will show "0-1").
9340 if (!(State & INSERT)
9341 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
9342 empty_line = TRUE;
9345 * Only draw the ruler when something changed.
9347 validate_virtcol_win(wp);
9348 if ( redraw_cmdline
9349 || always
9350 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
9351 || wp->w_cursor.col != wp->w_ru_cursor.col
9352 || wp->w_virtcol != wp->w_ru_virtcol
9353 #ifdef FEAT_VIRTUALEDIT
9354 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
9355 #endif
9356 || wp->w_topline != wp->w_ru_topline
9357 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
9358 #ifdef FEAT_DIFF
9359 || wp->w_topfill != wp->w_ru_topfill
9360 #endif
9361 || empty_line != wp->w_ru_empty)
9363 cursor_off();
9364 #ifdef FEAT_WINDOWS
9365 if (wp->w_status_height)
9367 row = W_WINROW(wp) + wp->w_height;
9368 fillchar = fillchar_status(&attr, wp == curwin);
9369 # ifdef FEAT_VERTSPLIT
9370 off = W_WINCOL(wp);
9371 width = W_WIDTH(wp);
9372 # endif
9374 else
9375 #endif
9377 row = Rows - 1;
9378 fillchar = ' ';
9379 attr = 0;
9380 #ifdef FEAT_VERTSPLIT
9381 width = Columns;
9382 off = 0;
9383 #endif
9386 /* In list mode virtcol needs to be recomputed */
9387 virtcol = wp->w_virtcol;
9388 if (wp->w_p_list && lcs_tab1 == NUL)
9390 wp->w_p_list = FALSE;
9391 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
9392 wp->w_p_list = TRUE;
9396 * Some sprintfs return the length, some return a pointer.
9397 * To avoid portability problems we use strlen() here.
9399 sprintf((char *)buffer, "%ld,",
9400 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
9401 ? 0L
9402 : (long)(wp->w_cursor.lnum));
9403 col_print(buffer + STRLEN(buffer),
9404 empty_line ? 0 : (int)wp->w_cursor.col + 1,
9405 (int)virtcol + 1);
9408 * Add a "50%" if there is room for it.
9409 * On the last line, don't print in the last column (scrolls the
9410 * screen up on some terminals).
9412 i = (int)STRLEN(buffer);
9413 get_rel_pos(wp, buffer + i + 1);
9414 o = i + vim_strsize(buffer + i + 1);
9415 #ifdef FEAT_WINDOWS
9416 if (wp->w_status_height == 0) /* can't use last char of screen */
9417 #endif
9418 ++o;
9419 #ifdef FEAT_VERTSPLIT
9420 this_ru_col = ru_col - (Columns - width);
9421 if (this_ru_col < 0)
9422 this_ru_col = 0;
9423 #endif
9424 /* Never use more than half the window/screen width, leave the other
9425 * half for the filename. */
9426 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
9427 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
9428 if (this_ru_col + o < WITH_WIDTH(width))
9430 while (this_ru_col + o < WITH_WIDTH(width))
9432 #ifdef FEAT_MBYTE
9433 if (has_mbyte)
9434 i += (*mb_char2bytes)(fillchar, buffer + i);
9435 else
9436 #endif
9437 buffer[i++] = fillchar;
9438 ++o;
9440 get_rel_pos(wp, buffer + i);
9442 /* Truncate at window boundary. */
9443 #ifdef FEAT_MBYTE
9444 if (has_mbyte)
9446 o = 0;
9447 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
9449 o += (*mb_ptr2cells)(buffer + i);
9450 if (this_ru_col + o > WITH_WIDTH(width))
9452 buffer[i] = NUL;
9453 break;
9457 else
9458 #endif
9459 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
9460 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
9462 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
9463 i = redraw_cmdline;
9464 screen_fill(row, row + 1,
9465 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
9466 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
9467 fillchar, fillchar, attr);
9468 /* don't redraw the cmdline because of showing the ruler */
9469 redraw_cmdline = i;
9470 wp->w_ru_cursor = wp->w_cursor;
9471 wp->w_ru_virtcol = wp->w_virtcol;
9472 wp->w_ru_empty = empty_line;
9473 wp->w_ru_topline = wp->w_topline;
9474 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
9475 #ifdef FEAT_DIFF
9476 wp->w_ru_topfill = wp->w_topfill;
9477 #endif
9480 #endif
9482 #if defined(FEAT_LINEBREAK) || defined(PROTO)
9484 * Return the width of the 'number' column.
9485 * Caller may need to check if 'number' is set.
9486 * Otherwise it depends on 'numberwidth' and the line count.
9489 number_width(wp)
9490 win_T *wp;
9492 int n;
9493 linenr_T lnum;
9495 lnum = wp->w_buffer->b_ml.ml_line_count;
9496 if (lnum == wp->w_nrwidth_line_count)
9497 return wp->w_nrwidth_width;
9498 wp->w_nrwidth_line_count = lnum;
9500 n = 0;
9503 lnum /= 10;
9504 ++n;
9505 } while (lnum > 0);
9507 /* 'numberwidth' gives the minimal width plus one */
9508 if (n < wp->w_p_nuw - 1)
9509 n = wp->w_p_nuw - 1;
9511 wp->w_nrwidth_width = n;
9512 return n;
9514 #endif