Validate screen before constraining window
[MacVim.git] / src / screen.c
blob0c60bc0accfdb806728bd9605e848844a1b71e5d
1 /* vi:set ts=8 sts=4 sw=4:
3 * VIM - Vi IMproved by Bram Moolenaar
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
11 * screen.c: code for displaying on the screen
13 * Output to the screen (console, terminal emulator or GUI window) is minimized
14 * by remembering what is already on the screen, and only updating the parts
15 * that changed.
17 * ScreenLines[off] Contains a copy of the whole screen, as it is currently
18 * displayed (excluding text written by external commands).
19 * ScreenAttrs[off] Contains the associated attributes.
20 * LineOffset[row] Contains the offset into ScreenLines*[] and ScreenAttrs[]
21 * for each line.
22 * LineWraps[row] Flag for each line whether it wraps to the next line.
24 * For double-byte characters, two consecutive bytes in ScreenLines[] can form
25 * one character which occupies two display cells.
26 * For UTF-8 a multi-byte character is converted to Unicode and stored in
27 * ScreenLinesUC[]. ScreenLines[] contains the first byte only. For an ASCII
28 * character without composing chars ScreenLinesUC[] will be 0. When the
29 * character occupies two display cells the next byte in ScreenLines[] is 0.
30 * ScreenLinesC[][] contain up to 'maxcombine' composing characters
31 * (drawn on top of the first character). They are 0 when not used.
32 * ScreenLines2[] is only used for euc-jp to store the second byte if the
33 * first byte is 0x8e (single-width character).
35 * The screen_*() functions write to the screen and handle updating
36 * ScreenLines[].
38 * update_screen() is the function that updates all windows and status lines.
39 * It is called form the main loop when must_redraw is non-zero. It may be
40 * called from other places when an immediate screen update is needed.
42 * The part of the buffer that is displayed in a window is set with:
43 * - w_topline (first buffer line in window)
44 * - w_topfill (filler line above the first line)
45 * - w_leftcol (leftmost window cell in window),
46 * - w_skipcol (skipped window cells of first line)
48 * Commands that only move the cursor around in a window, do not need to take
49 * action to update the display. The main loop will check if w_topline is
50 * valid and update it (scroll the window) when needed.
52 * Commands that scroll a window change w_topline and must call
53 * check_cursor() to move the cursor into the visible part of the window, and
54 * call redraw_later(VALID) to have the window displayed by update_screen()
55 * later.
57 * Commands that change text in the buffer must call changed_bytes() or
58 * changed_lines() to mark the area that changed and will require updating
59 * later. The main loop will call update_screen(), which will update each
60 * window that shows the changed buffer. This assumes text above the change
61 * can remain displayed as it is. Text after the change may need updating for
62 * scrolling, folding and syntax highlighting.
64 * Commands that change how a window is displayed (e.g., setting 'list') or
65 * invalidate the contents of a window in another way (e.g., change fold
66 * settings), must call redraw_later(NOT_VALID) to have the whole window
67 * redisplayed by update_screen() later.
69 * Commands that change how a buffer is displayed (e.g., setting 'tabstop')
70 * must call redraw_curbuf_later(NOT_VALID) to have all the windows for the
71 * buffer redisplayed by update_screen() later.
73 * Commands that change highlighting and possibly cause a scroll too must call
74 * redraw_later(SOME_VALID) to update the whole window but still use scrolling
75 * to avoid redrawing everything. But the length of displayed lines must not
76 * change, use NOT_VALID then.
78 * Commands that move the window position must call redraw_later(NOT_VALID).
79 * TODO: should minimize redrawing by scrolling when possible.
81 * Commands that change everything (e.g., resizing the screen) must call
82 * redraw_all_later(NOT_VALID) or redraw_all_later(CLEAR).
84 * Things that are handled indirectly:
85 * - When messages scroll the screen up, msg_scrolled will be set and
86 * update_screen() called to redraw.
89 #include "vim.h"
92 * The attributes that are actually active for writing to the screen.
94 static int screen_attr = 0;
97 * Positioning the cursor is reduced by remembering the last position.
98 * Mostly used by windgoto() and screen_char().
100 static int screen_cur_row, screen_cur_col; /* last known cursor position */
102 #ifdef FEAT_SEARCH_EXTRA
103 static match_T search_hl; /* used for 'hlsearch' highlight matching */
104 #endif
106 #ifdef FEAT_FOLDING
107 static foldinfo_T win_foldinfo; /* info for 'foldcolumn' */
108 #endif
111 * Buffer for one screen line (characters and attributes).
113 static schar_T *current_ScreenLine;
115 static void win_update __ARGS((win_T *wp));
116 static void win_draw_end __ARGS((win_T *wp, int c1, int c2, int row, int endrow, hlf_T hl));
117 #ifdef FEAT_FOLDING
118 static void fold_line __ARGS((win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row));
119 static void fill_foldcolumn __ARGS((char_u *p, win_T *wp, int closed, linenr_T lnum));
120 static void copy_text_attr __ARGS((int off, char_u *buf, int len, int attr));
121 #endif
122 static int win_line __ARGS((win_T *, linenr_T, int, int, int nochange));
123 static int char_needs_redraw __ARGS((int off_from, int off_to, int cols));
124 #ifdef FEAT_RIGHTLEFT
125 static void screen_line __ARGS((int row, int coloff, int endcol, int clear_width, int rlflag));
126 # define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c), (rl))
127 #else
128 static void screen_line __ARGS((int row, int coloff, int endcol, int clear_width));
129 # define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c))
130 #endif
131 #ifdef FEAT_VERTSPLIT
132 static void draw_vsep_win __ARGS((win_T *wp, int row));
133 #endif
134 #ifdef FEAT_STL_OPT
135 static void redraw_custum_statusline __ARGS((win_T *wp));
136 #endif
137 #ifdef FEAT_SEARCH_EXTRA
138 #define SEARCH_HL_PRIORITY 0
139 static void start_search_hl __ARGS((void));
140 static void end_search_hl __ARGS((void));
141 static void prepare_search_hl __ARGS((win_T *wp, linenr_T lnum));
142 static void next_search_hl __ARGS((win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol));
143 #endif
144 static void screen_start_highlight __ARGS((int attr));
145 static void screen_char __ARGS((unsigned off, int row, int col));
146 #ifdef FEAT_MBYTE
147 static void screen_char_2 __ARGS((unsigned off, int row, int col));
148 #endif
149 static void screenclear2 __ARGS((void));
150 static void lineclear __ARGS((unsigned off, int width));
151 static void lineinvalid __ARGS((unsigned off, int width));
152 #ifdef FEAT_VERTSPLIT
153 static void linecopy __ARGS((int to, int from, win_T *wp));
154 static void redraw_block __ARGS((int row, int end, win_T *wp));
155 #endif
156 static int win_do_lines __ARGS((win_T *wp, int row, int line_count, int mayclear, int del));
157 static void win_rest_invalid __ARGS((win_T *wp));
158 static void msg_pos_mode __ARGS((void));
159 #if defined(FEAT_WINDOWS)
160 static void draw_tabline __ARGS((void));
161 #endif
162 #if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
163 static int fillchar_status __ARGS((int *attr, int is_curwin));
164 #endif
165 #ifdef FEAT_VERTSPLIT
166 static int fillchar_vsep __ARGS((int *attr));
167 #endif
168 #ifdef FEAT_STL_OPT
169 static void win_redr_custom __ARGS((win_T *wp, int draw_ruler));
170 #endif
171 #ifdef FEAT_CMDL_INFO
172 static void win_redr_ruler __ARGS((win_T *wp, int always));
173 #endif
175 #if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
176 /* Ugly global: overrule attribute used by screen_char() */
177 static int screen_char_attr = 0;
178 #endif
181 * Redraw the current window later, with update_screen(type).
182 * Set must_redraw only if not already set to a higher value.
183 * e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
185 void
186 redraw_later(type)
187 int type;
189 redraw_win_later(curwin, type);
192 void
193 redraw_win_later(wp, type)
194 win_T *wp;
195 int type;
197 if (wp->w_redr_type < type)
199 wp->w_redr_type = type;
200 if (type >= NOT_VALID)
201 wp->w_lines_valid = 0;
202 if (must_redraw < type) /* must_redraw is the maximum of all windows */
203 must_redraw = type;
208 * Force a complete redraw later. Also resets the highlighting. To be used
209 * after executing a shell command that messes up the screen.
211 void
212 redraw_later_clear()
214 redraw_all_later(CLEAR);
215 #ifdef FEAT_GUI
216 if (gui.in_use)
217 /* Use a code that will reset gui.highlight_mask in
218 * gui_stop_highlight(). */
219 screen_attr = HL_ALL + 1;
220 else
221 #endif
222 /* Use attributes that is very unlikely to appear in text. */
223 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE;
227 * Mark all windows to be redrawn later.
229 void
230 redraw_all_later(type)
231 int type;
233 win_T *wp;
235 FOR_ALL_WINDOWS(wp)
237 redraw_win_later(wp, type);
242 * Mark all windows that are editing the current buffer to be updated later.
244 void
245 redraw_curbuf_later(type)
246 int type;
248 redraw_buf_later(curbuf, type);
251 void
252 redraw_buf_later(buf, type)
253 buf_T *buf;
254 int type;
256 win_T *wp;
258 FOR_ALL_WINDOWS(wp)
260 if (wp->w_buffer == buf)
261 redraw_win_later(wp, type);
266 * Changed something in the current window, at buffer line "lnum", that
267 * requires that line and possibly other lines to be redrawn.
268 * Used when entering/leaving Insert mode with the cursor on a folded line.
269 * Used to remove the "$" from a change command.
270 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
271 * may become invalid and the whole window will have to be redrawn.
273 /*ARGSUSED*/
274 void
275 redrawWinline(lnum, invalid)
276 linenr_T lnum;
277 int invalid; /* window line height is invalid now */
279 #ifdef FEAT_FOLDING
280 int i;
281 #endif
283 if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
284 curwin->w_redraw_top = lnum;
285 if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
286 curwin->w_redraw_bot = lnum;
287 redraw_later(VALID);
289 #ifdef FEAT_FOLDING
290 if (invalid)
292 /* A w_lines[] entry for this lnum has become invalid. */
293 i = find_wl_entry(curwin, lnum);
294 if (i >= 0)
295 curwin->w_lines[i].wl_valid = FALSE;
297 #endif
301 * update all windows that are editing the current buffer
303 void
304 update_curbuf(type)
305 int type;
307 redraw_curbuf_later(type);
308 update_screen(type);
312 * update_screen()
314 * Based on the current value of curwin->w_topline, transfer a screenfull
315 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
317 void
318 update_screen(type)
319 int type;
321 win_T *wp;
322 static int did_intro = FALSE;
323 #if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
324 int did_one;
325 #endif
327 if (!screen_valid(TRUE))
328 return;
330 if (must_redraw)
332 if (type < must_redraw) /* use maximal type */
333 type = must_redraw;
335 /* must_redraw is reset here, so that when we run into some weird
336 * reason to redraw while busy redrawing (e.g., asynchronous
337 * scrolling), or update_topline() in win_update() will cause a
338 * scroll, the screen will be redrawn later or in win_update(). */
339 must_redraw = 0;
342 /* Need to update w_lines[]. */
343 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
344 type = NOT_VALID;
346 if (!redrawing())
348 redraw_later(type); /* remember type for next time */
349 must_redraw = type;
350 if (type > INVERTED_ALL)
351 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
352 return;
355 updating_screen = TRUE;
356 #ifdef FEAT_SYN_HL
357 ++display_tick; /* let syntax code know we're in a next round of
358 * display updating */
359 #endif
362 * if the screen was scrolled up when displaying a message, scroll it down
364 if (msg_scrolled)
366 clear_cmdline = TRUE;
367 if (msg_scrolled > Rows - 5) /* clearing is faster */
368 type = CLEAR;
369 else if (type != CLEAR)
371 check_for_delay(FALSE);
372 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, NULL) == FAIL)
373 type = CLEAR;
374 FOR_ALL_WINDOWS(wp)
376 if (W_WINROW(wp) < msg_scrolled)
378 if (W_WINROW(wp) + wp->w_height > msg_scrolled
379 && wp->w_redr_type < REDRAW_TOP
380 && wp->w_lines_valid > 0
381 && wp->w_topline == wp->w_lines[0].wl_lnum)
383 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
384 wp->w_redr_type = REDRAW_TOP;
386 else
388 wp->w_redr_type = NOT_VALID;
389 #ifdef FEAT_WINDOWS
390 if (W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp)
391 <= msg_scrolled)
392 wp->w_redr_status = TRUE;
393 #endif
397 redraw_cmdline = TRUE;
398 #ifdef FEAT_WINDOWS
399 redraw_tabline = TRUE;
400 #endif
402 msg_scrolled = 0;
403 need_wait_return = FALSE;
406 /* reset cmdline_row now (may have been changed temporarily) */
407 compute_cmdrow();
409 /* Check for changed highlighting */
410 if (need_highlight_changed)
411 highlight_changed();
413 if (type == CLEAR) /* first clear screen */
415 screenclear(); /* will reset clear_cmdline */
416 type = NOT_VALID;
419 if (clear_cmdline) /* going to clear cmdline (done below) */
420 check_for_delay(FALSE);
422 #ifdef FEAT_LINEBREAK
423 /* Force redraw when width of 'number' column changes. */
424 if (curwin->w_redr_type < NOT_VALID
425 && curwin->w_nrwidth != (curwin->w_p_nu ? number_width(curwin) : 0))
426 curwin->w_redr_type = NOT_VALID;
427 #endif
430 * Only start redrawing if there is really something to do.
432 if (type == INVERTED)
433 update_curswant();
434 if (curwin->w_redr_type < type
435 && !((type == VALID
436 && curwin->w_lines[0].wl_valid
437 #ifdef FEAT_DIFF
438 && curwin->w_topfill == curwin->w_old_topfill
439 && curwin->w_botfill == curwin->w_old_botfill
440 #endif
441 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
442 #ifdef FEAT_VISUAL
443 || (type == INVERTED
444 && VIsual_active
445 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
446 && curwin->w_old_visual_mode == VIsual_mode
447 && (curwin->w_valid & VALID_VIRTCOL)
448 && curwin->w_old_curswant == curwin->w_curswant)
449 #endif
451 curwin->w_redr_type = type;
453 #ifdef FEAT_WINDOWS
454 /* Redraw the tab pages line if needed. */
455 if (redraw_tabline || type >= NOT_VALID)
456 draw_tabline();
457 #endif
459 #ifdef FEAT_SYN_HL
461 * Correct stored syntax highlighting info for changes in each displayed
462 * buffer. Each buffer must only be done once.
464 FOR_ALL_WINDOWS(wp)
466 if (wp->w_buffer->b_mod_set)
468 # ifdef FEAT_WINDOWS
469 win_T *wwp;
471 /* Check if we already did this buffer. */
472 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
473 if (wwp->w_buffer == wp->w_buffer)
474 break;
475 # endif
476 if (
477 # ifdef FEAT_WINDOWS
478 wwp == wp &&
479 # endif
480 syntax_present(wp->w_buffer))
481 syn_stack_apply_changes(wp->w_buffer);
484 #endif
487 * Go from top to bottom through the windows, redrawing the ones that need
488 * it.
490 #if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
491 did_one = FALSE;
492 #endif
493 #ifdef FEAT_SEARCH_EXTRA
494 search_hl.rm.regprog = NULL;
495 #endif
496 FOR_ALL_WINDOWS(wp)
498 if (wp->w_redr_type != 0)
500 cursor_off();
501 #if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
502 if (!did_one)
504 did_one = TRUE;
505 # ifdef FEAT_SEARCH_EXTRA
506 start_search_hl();
507 # endif
508 # ifdef FEAT_CLIPBOARD
509 /* When Visual area changed, may have to update selection. */
510 if (clip_star.available && clip_isautosel())
511 clip_update_selection();
512 # endif
513 #ifdef FEAT_GUI
514 /* Remove the cursor before starting to do anything, because
515 * scrolling may make it difficult to redraw the text under
516 * it. */
517 if (gui.in_use)
518 gui_undraw_cursor();
519 #endif
521 #endif
522 win_update(wp);
525 #ifdef FEAT_WINDOWS
526 /* redraw status line after the window to minimize cursor movement */
527 if (wp->w_redr_status)
529 cursor_off();
530 win_redr_status(wp);
532 #endif
534 #if defined(FEAT_SEARCH_EXTRA)
535 end_search_hl();
536 #endif
538 #ifdef FEAT_WINDOWS
539 /* Reset b_mod_set flags. Going through all windows is probably faster
540 * than going through all buffers (there could be many buffers). */
541 for (wp = firstwin; wp != NULL; wp = wp->w_next)
542 wp->w_buffer->b_mod_set = FALSE;
543 #else
544 curbuf->b_mod_set = FALSE;
545 #endif
547 updating_screen = FALSE;
548 #ifdef FEAT_GUI
549 gui_may_resize_shell();
550 #endif
552 /* Clear or redraw the command line. Done last, because scrolling may
553 * mess up the command line. */
554 if (clear_cmdline || redraw_cmdline)
555 showmode();
557 /* May put up an introductory message when not editing a file */
558 if (!did_intro && bufempty()
559 && curbuf->b_fname == NULL
560 #ifdef FEAT_WINDOWS
561 && firstwin->w_next == NULL
562 #endif
563 && vim_strchr(p_shm, SHM_INTRO) == NULL)
564 intro_message(FALSE);
565 did_intro = TRUE;
567 #ifdef FEAT_GUI
568 /* Redraw the cursor and update the scrollbars when all screen updating is
569 * done. */
570 if (gui.in_use)
572 out_flush(); /* required before updating the cursor */
573 if (did_one)
574 gui_update_cursor(FALSE, FALSE);
575 gui_update_scrollbars(FALSE);
577 #endif
580 #if defined(FEAT_SIGNS) || defined(FEAT_GUI)
581 static void update_prepare __ARGS((void));
582 static void update_finish __ARGS((void));
585 * Prepare for updating one or more windows.
587 static void
588 update_prepare()
590 cursor_off();
591 updating_screen = TRUE;
592 #ifdef FEAT_GUI
593 /* Remove the cursor before starting to do anything, because scrolling may
594 * make it difficult to redraw the text under it. */
595 if (gui.in_use)
596 gui_undraw_cursor();
597 #endif
598 #ifdef FEAT_SEARCH_EXTRA
599 start_search_hl();
600 #endif
604 * Finish updating one or more windows.
606 static void
607 update_finish()
609 if (redraw_cmdline)
610 showmode();
612 # ifdef FEAT_SEARCH_EXTRA
613 end_search_hl();
614 # endif
616 updating_screen = FALSE;
618 # ifdef FEAT_GUI
619 gui_may_resize_shell();
621 /* Redraw the cursor and update the scrollbars when all screen updating is
622 * done. */
623 if (gui.in_use)
625 out_flush(); /* required before updating the cursor */
626 gui_update_cursor(FALSE, FALSE);
627 gui_update_scrollbars(FALSE);
629 # endif
631 #endif
633 #if defined(FEAT_SIGNS) || defined(PROTO)
634 void
635 update_debug_sign(buf, lnum)
636 buf_T *buf;
637 linenr_T lnum;
639 win_T *wp;
640 int doit = FALSE;
642 # ifdef FEAT_FOLDING
643 win_foldinfo.fi_level = 0;
644 # endif
646 /* update/delete a specific mark */
647 FOR_ALL_WINDOWS(wp)
649 if (buf != NULL && lnum > 0)
651 if (wp->w_buffer == buf && lnum >= wp->w_topline
652 && lnum < wp->w_botline)
654 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
655 wp->w_redraw_top = lnum;
656 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
657 wp->w_redraw_bot = lnum;
658 redraw_win_later(wp, VALID);
661 else
662 redraw_win_later(wp, VALID);
663 if (wp->w_redr_type != 0)
664 doit = TRUE;
667 if (!doit)
668 return;
670 /* update all windows that need updating */
671 update_prepare();
673 # ifdef FEAT_WINDOWS
674 for (wp = firstwin; wp; wp = wp->w_next)
676 if (wp->w_redr_type != 0)
677 win_update(wp);
678 if (wp->w_redr_status)
679 win_redr_status(wp);
681 # else
682 if (curwin->w_redr_type != 0)
683 win_update(curwin);
684 # endif
686 update_finish();
688 #endif
691 #if defined(FEAT_GUI) || defined(PROTO)
693 * Update a single window, its status line and maybe the command line msg.
694 * Used for the GUI scrollbar.
696 void
697 updateWindow(wp)
698 win_T *wp;
700 update_prepare();
702 #ifdef FEAT_CLIPBOARD
703 /* When Visual area changed, may have to update selection. */
704 if (clip_star.available && clip_isautosel())
705 clip_update_selection();
706 #endif
708 win_update(wp);
710 #ifdef FEAT_WINDOWS
711 /* When the screen was cleared redraw the tab pages line. */
712 if (redraw_tabline)
713 draw_tabline();
715 if (wp->w_redr_status
716 # ifdef FEAT_CMDL_INFO
717 || p_ru
718 # endif
719 # ifdef FEAT_STL_OPT
720 || *p_stl != NUL || *wp->w_p_stl != NUL
721 # endif
723 win_redr_status(wp);
724 #endif
726 update_finish();
728 #endif
731 * Update a single window.
733 * This may cause the windows below it also to be redrawn (when clearing the
734 * screen or scrolling lines).
736 * How the window is redrawn depends on wp->w_redr_type. Each type also
737 * implies the one below it.
738 * NOT_VALID redraw the whole window
739 * SOME_VALID redraw the whole window but do scroll when possible
740 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
741 * INVERTED redraw the changed part of the Visual area
742 * INVERTED_ALL redraw the whole Visual area
743 * VALID 1. scroll up/down to adjust for a changed w_topline
744 * 2. update lines at the top when scrolled down
745 * 3. redraw changed text:
746 * - if wp->w_buffer->b_mod_set set, update lines between
747 * b_mod_top and b_mod_bot.
748 * - if wp->w_redraw_top non-zero, redraw lines between
749 * wp->w_redraw_top and wp->w_redr_bot.
750 * - continue redrawing when syntax status is invalid.
751 * 4. if scrolled up, update lines at the bottom.
752 * This results in three areas that may need updating:
753 * top: from first row to top_end (when scrolled down)
754 * mid: from mid_start to mid_end (update inversion or changed text)
755 * bot: from bot_start to last row (when scrolled up)
757 static void
758 win_update(wp)
759 win_T *wp;
761 buf_T *buf = wp->w_buffer;
762 int type;
763 int top_end = 0; /* Below last row of the top area that needs
764 updating. 0 when no top area updating. */
765 int mid_start = 999;/* first row of the mid area that needs
766 updating. 999 when no mid area updating. */
767 int mid_end = 0; /* Below last row of the mid area that needs
768 updating. 0 when no mid area updating. */
769 int bot_start = 999;/* first row of the bot area that needs
770 updating. 999 when no bot area updating */
771 #ifdef FEAT_VISUAL
772 int scrolled_down = FALSE; /* TRUE when scrolled down when
773 w_topline got smaller a bit */
774 #endif
775 #ifdef FEAT_SEARCH_EXTRA
776 matchitem_T *cur; /* points to the match list */
777 int top_to_mod = FALSE; /* redraw above mod_top */
778 #endif
780 int row; /* current window row to display */
781 linenr_T lnum; /* current buffer lnum to display */
782 int idx; /* current index in w_lines[] */
783 int srow; /* starting row of the current line */
785 int eof = FALSE; /* if TRUE, we hit the end of the file */
786 int didline = FALSE; /* if TRUE, we finished the last line */
787 int i;
788 long j;
789 static int recursive = FALSE; /* being called recursively */
790 int old_botline = wp->w_botline;
791 #ifdef FEAT_FOLDING
792 long fold_count;
793 #endif
794 #ifdef FEAT_SYN_HL
795 /* remember what happened to the previous line, to know if
796 * check_visual_highlight() can be used */
797 #define DID_NONE 1 /* didn't update a line */
798 #define DID_LINE 2 /* updated a normal line */
799 #define DID_FOLD 3 /* updated a folded line */
800 int did_update = DID_NONE;
801 linenr_T syntax_last_parsed = 0; /* last parsed text line */
802 #endif
803 linenr_T mod_top = 0;
804 linenr_T mod_bot = 0;
805 #if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
806 int save_got_int;
807 #endif
809 type = wp->w_redr_type;
811 if (type == NOT_VALID)
813 #ifdef FEAT_WINDOWS
814 wp->w_redr_status = TRUE;
815 #endif
816 wp->w_lines_valid = 0;
819 /* Window is zero-height: nothing to draw. */
820 if (wp->w_height == 0)
822 wp->w_redr_type = 0;
823 return;
826 #ifdef FEAT_VERTSPLIT
827 /* Window is zero-width: Only need to draw the separator. */
828 if (wp->w_width == 0)
830 /* draw the vertical separator right of this window */
831 draw_vsep_win(wp, 0);
832 wp->w_redr_type = 0;
833 return;
835 #endif
837 #ifdef FEAT_SEARCH_EXTRA
838 /* Setup for match and 'hlsearch' highlighting. Disable any previous
839 * match */
840 cur = wp->w_match_head;
841 while (cur != NULL)
843 cur->hl.rm = cur->match;
844 if (cur->hlg_id == 0)
845 cur->hl.attr = 0;
846 else
847 cur->hl.attr = syn_id2attr(cur->hlg_id);
848 cur->hl.buf = buf;
849 cur->hl.lnum = 0;
850 cur->hl.first_lnum = 0;
851 # ifdef FEAT_RELTIME
852 /* Set the time limit to 'redrawtime'. */
853 profile_setlimit(p_rdt, &(cur->hl.tm));
854 # endif
855 cur = cur->next;
857 search_hl.buf = buf;
858 search_hl.lnum = 0;
859 search_hl.first_lnum = 0;
860 /* time limit is set at the toplevel, for all windows */
861 #endif
863 #ifdef FEAT_LINEBREAK
864 /* Force redraw when width of 'number' column changes. */
865 i = wp->w_p_nu ? number_width(wp) : 0;
866 if (wp->w_nrwidth != i)
868 type = NOT_VALID;
869 wp->w_nrwidth = i;
871 else
872 #endif
874 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
877 * When there are both inserted/deleted lines and specific lines to be
878 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
879 * everything (only happens when redrawing is off for while).
881 type = NOT_VALID;
883 else
886 * Set mod_top to the first line that needs displaying because of
887 * changes. Set mod_bot to the first line after the changes.
889 mod_top = wp->w_redraw_top;
890 if (wp->w_redraw_bot != 0)
891 mod_bot = wp->w_redraw_bot + 1;
892 else
893 mod_bot = 0;
894 wp->w_redraw_top = 0; /* reset for next time */
895 wp->w_redraw_bot = 0;
896 if (buf->b_mod_set)
898 if (mod_top == 0 || mod_top > buf->b_mod_top)
900 mod_top = buf->b_mod_top;
901 #ifdef FEAT_SYN_HL
902 /* Need to redraw lines above the change that may be included
903 * in a pattern match. */
904 if (syntax_present(buf))
906 mod_top -= buf->b_syn_sync_linebreaks;
907 if (mod_top < 1)
908 mod_top = 1;
910 #endif
912 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
913 mod_bot = buf->b_mod_bot;
915 #ifdef FEAT_SEARCH_EXTRA
916 /* When 'hlsearch' is on and using a multi-line search pattern, a
917 * change in one line may make the Search highlighting in a
918 * previous line invalid. Simple solution: redraw all visible
919 * lines above the change.
920 * Same for a match pattern.
922 if (search_hl.rm.regprog != NULL
923 && re_multiline(search_hl.rm.regprog))
924 top_to_mod = TRUE;
925 else
927 cur = wp->w_match_head;
928 while (cur != NULL)
930 if (cur->match.regprog != NULL
931 && re_multiline(cur->match.regprog))
933 top_to_mod = TRUE;
934 break;
936 cur = cur->next;
939 #endif
941 #ifdef FEAT_FOLDING
942 if (mod_top != 0 && hasAnyFolding(wp))
944 linenr_T lnumt, lnumb;
947 * A change in a line can cause lines above it to become folded or
948 * unfolded. Find the top most buffer line that may be affected.
949 * If the line was previously folded and displayed, get the first
950 * line of that fold. If the line is folded now, get the first
951 * folded line. Use the minimum of these two.
954 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
955 * the line below it. If there is no valid entry, use w_topline.
956 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
957 * to this line. If there is no valid entry, use MAXLNUM. */
958 lnumt = wp->w_topline;
959 lnumb = MAXLNUM;
960 for (i = 0; i < wp->w_lines_valid; ++i)
961 if (wp->w_lines[i].wl_valid)
963 if (wp->w_lines[i].wl_lastlnum < mod_top)
964 lnumt = wp->w_lines[i].wl_lastlnum + 1;
965 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
967 lnumb = wp->w_lines[i].wl_lnum;
968 /* When there is a fold column it might need updating
969 * in the next line ("J" just above an open fold). */
970 if (wp->w_p_fdc > 0)
971 ++lnumb;
975 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
976 if (mod_top > lnumt)
977 mod_top = lnumt;
979 /* Now do the same for the bottom line (one above mod_bot). */
980 --mod_bot;
981 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
982 ++mod_bot;
983 if (mod_bot < lnumb)
984 mod_bot = lnumb;
986 #endif
988 /* When a change starts above w_topline and the end is below
989 * w_topline, start redrawing at w_topline.
990 * If the end of the change is above w_topline: do like no change was
991 * made, but redraw the first line to find changes in syntax. */
992 if (mod_top != 0 && mod_top < wp->w_topline)
994 if (mod_bot > wp->w_topline)
995 mod_top = wp->w_topline;
996 #ifdef FEAT_SYN_HL
997 else if (syntax_present(buf))
998 top_end = 1;
999 #endif
1002 /* When line numbers are displayed need to redraw all lines below
1003 * inserted/deleted lines. */
1004 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1005 mod_bot = MAXLNUM;
1009 * When only displaying the lines at the top, set top_end. Used when
1010 * window has scrolled down for msg_scrolled.
1012 if (type == REDRAW_TOP)
1014 j = 0;
1015 for (i = 0; i < wp->w_lines_valid; ++i)
1017 j += wp->w_lines[i].wl_size;
1018 if (j >= wp->w_upd_rows)
1020 top_end = j;
1021 break;
1024 if (top_end == 0)
1025 /* not found (cannot happen?): redraw everything */
1026 type = NOT_VALID;
1027 else
1028 /* top area defined, the rest is VALID */
1029 type = VALID;
1032 /* Trick: we want to avoid clearing the screen twice. screenclear() will
1033 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1034 * non-zero and thus not FALSE) will indicate that screenclear() was not
1035 * called. */
1036 if (screen_cleared)
1037 screen_cleared = MAYBE;
1040 * If there are no changes on the screen that require a complete redraw,
1041 * handle three cases:
1042 * 1: we are off the top of the screen by a few lines: scroll down
1043 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1044 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1045 * w_lines[] that needs updating.
1047 if ((type == VALID || type == SOME_VALID
1048 || type == INVERTED || type == INVERTED_ALL)
1049 #ifdef FEAT_DIFF
1050 && !wp->w_botfill && !wp->w_old_botfill
1051 #endif
1054 if (mod_top != 0 && wp->w_topline == mod_top)
1057 * w_topline is the first changed line, the scrolling will be done
1058 * further down.
1061 else if (wp->w_lines[0].wl_valid
1062 && (wp->w_topline < wp->w_lines[0].wl_lnum
1063 #ifdef FEAT_DIFF
1064 || (wp->w_topline == wp->w_lines[0].wl_lnum
1065 && wp->w_topfill > wp->w_old_topfill)
1066 #endif
1070 * New topline is above old topline: May scroll down.
1072 #ifdef FEAT_FOLDING
1073 if (hasAnyFolding(wp))
1075 linenr_T ln;
1077 /* count the number of lines we are off, counting a sequence
1078 * of folded lines as one */
1079 j = 0;
1080 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1082 ++j;
1083 if (j >= wp->w_height - 2)
1084 break;
1085 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1088 else
1089 #endif
1090 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1091 if (j < wp->w_height - 2) /* not too far off */
1093 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1094 #ifdef FEAT_DIFF
1095 /* insert extra lines for previously invisible filler lines */
1096 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1097 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1098 - wp->w_old_topfill;
1099 #endif
1100 if (i < wp->w_height - 2) /* less than a screen off */
1103 * Try to insert the correct number of lines.
1104 * If not the last window, delete the lines at the bottom.
1105 * win_ins_lines may fail when the terminal can't do it.
1107 if (i > 0)
1108 check_for_delay(FALSE);
1109 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1111 if (wp->w_lines_valid != 0)
1113 /* Need to update rows that are new, stop at the
1114 * first one that scrolled down. */
1115 top_end = i;
1116 #ifdef FEAT_VISUAL
1117 scrolled_down = TRUE;
1118 #endif
1120 /* Move the entries that were scrolled, disable
1121 * the entries for the lines to be redrawn. */
1122 if ((wp->w_lines_valid += j) > wp->w_height)
1123 wp->w_lines_valid = wp->w_height;
1124 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1125 wp->w_lines[idx] = wp->w_lines[idx - j];
1126 while (idx >= 0)
1127 wp->w_lines[idx--].wl_valid = FALSE;
1130 else
1131 mid_start = 0; /* redraw all lines */
1133 else
1134 mid_start = 0; /* redraw all lines */
1136 else
1137 mid_start = 0; /* redraw all lines */
1139 else
1142 * New topline is at or below old topline: May scroll up.
1143 * When topline didn't change, find first entry in w_lines[] that
1144 * needs updating.
1147 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1148 j = -1;
1149 row = 0;
1150 for (i = 0; i < wp->w_lines_valid; i++)
1152 if (wp->w_lines[i].wl_valid
1153 && wp->w_lines[i].wl_lnum == wp->w_topline)
1155 j = i;
1156 break;
1158 row += wp->w_lines[i].wl_size;
1160 if (j == -1)
1162 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1163 * lines */
1164 mid_start = 0;
1166 else
1169 * Try to delete the correct number of lines.
1170 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1172 #ifdef FEAT_DIFF
1173 /* If the topline didn't change, delete old filler lines,
1174 * otherwise delete filler lines of the new topline... */
1175 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1176 row += wp->w_old_topfill;
1177 else
1178 row += diff_check_fill(wp, wp->w_topline);
1179 /* ... but don't delete new filler lines. */
1180 row -= wp->w_topfill;
1181 #endif
1182 if (row > 0)
1184 check_for_delay(FALSE);
1185 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1186 bot_start = wp->w_height - row;
1187 else
1188 mid_start = 0; /* redraw all lines */
1190 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1193 * Skip the lines (below the deleted lines) that are still
1194 * valid and don't need redrawing. Copy their info
1195 * upwards, to compensate for the deleted lines. Set
1196 * bot_start to the first row that needs redrawing.
1198 bot_start = 0;
1199 idx = 0;
1200 for (;;)
1202 wp->w_lines[idx] = wp->w_lines[j];
1203 /* stop at line that didn't fit, unless it is still
1204 * valid (no lines deleted) */
1205 if (row > 0 && bot_start + row
1206 + (int)wp->w_lines[j].wl_size > wp->w_height)
1208 wp->w_lines_valid = idx + 1;
1209 break;
1211 bot_start += wp->w_lines[idx++].wl_size;
1213 /* stop at the last valid entry in w_lines[].wl_size */
1214 if (++j >= wp->w_lines_valid)
1216 wp->w_lines_valid = idx;
1217 break;
1220 #ifdef FEAT_DIFF
1221 /* Correct the first entry for filler lines at the top
1222 * when it won't get updated below. */
1223 if (wp->w_p_diff && bot_start > 0)
1224 wp->w_lines[0].wl_size =
1225 plines_win_nofill(wp, wp->w_topline, TRUE)
1226 + wp->w_topfill;
1227 #endif
1232 /* When starting redraw in the first line, redraw all lines. When
1233 * there is only one window it's probably faster to clear the screen
1234 * first. */
1235 if (mid_start == 0)
1237 mid_end = wp->w_height;
1238 if (lastwin == firstwin)
1240 /* Clear the screen when it was not done by win_del_lines() or
1241 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1242 * then. */
1243 if (screen_cleared != TRUE)
1244 screenclear();
1245 #ifdef FEAT_WINDOWS
1246 /* The screen was cleared, redraw the tab pages line. */
1247 if (redraw_tabline)
1248 draw_tabline();
1249 #endif
1253 /* When win_del_lines() or win_ins_lines() caused the screen to be
1254 * cleared (only happens for the first window) or when screenclear()
1255 * was called directly above, "must_redraw" will have been set to
1256 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1257 if (screen_cleared == TRUE)
1258 must_redraw = 0;
1260 else
1262 /* Not VALID or INVERTED: redraw all lines. */
1263 mid_start = 0;
1264 mid_end = wp->w_height;
1267 if (type == SOME_VALID)
1269 /* SOME_VALID: redraw all lines. */
1270 mid_start = 0;
1271 mid_end = wp->w_height;
1272 type = NOT_VALID;
1275 #ifdef FEAT_VISUAL
1276 /* check if we are updating or removing the inverted part */
1277 if ((VIsual_active && buf == curwin->w_buffer)
1278 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1280 linenr_T from, to;
1282 if (VIsual_active)
1284 if (VIsual_active
1285 && (VIsual_mode != wp->w_old_visual_mode
1286 || type == INVERTED_ALL))
1289 * If the type of Visual selection changed, redraw the whole
1290 * selection. Also when the ownership of the X selection is
1291 * gained or lost.
1293 if (curwin->w_cursor.lnum < VIsual.lnum)
1295 from = curwin->w_cursor.lnum;
1296 to = VIsual.lnum;
1298 else
1300 from = VIsual.lnum;
1301 to = curwin->w_cursor.lnum;
1303 /* redraw more when the cursor moved as well */
1304 if (wp->w_old_cursor_lnum < from)
1305 from = wp->w_old_cursor_lnum;
1306 if (wp->w_old_cursor_lnum > to)
1307 to = wp->w_old_cursor_lnum;
1308 if (wp->w_old_visual_lnum < from)
1309 from = wp->w_old_visual_lnum;
1310 if (wp->w_old_visual_lnum > to)
1311 to = wp->w_old_visual_lnum;
1313 else
1316 * Find the line numbers that need to be updated: The lines
1317 * between the old cursor position and the current cursor
1318 * position. Also check if the Visual position changed.
1320 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1322 from = curwin->w_cursor.lnum;
1323 to = wp->w_old_cursor_lnum;
1325 else
1327 from = wp->w_old_cursor_lnum;
1328 to = curwin->w_cursor.lnum;
1329 if (from == 0) /* Visual mode just started */
1330 from = to;
1333 if (VIsual.lnum != wp->w_old_visual_lnum
1334 || VIsual.col != wp->w_old_visual_col)
1336 if (wp->w_old_visual_lnum < from
1337 && wp->w_old_visual_lnum != 0)
1338 from = wp->w_old_visual_lnum;
1339 if (wp->w_old_visual_lnum > to)
1340 to = wp->w_old_visual_lnum;
1341 if (VIsual.lnum < from)
1342 from = VIsual.lnum;
1343 if (VIsual.lnum > to)
1344 to = VIsual.lnum;
1349 * If in block mode and changed column or curwin->w_curswant:
1350 * update all lines.
1351 * First compute the actual start and end column.
1353 if (VIsual_mode == Ctrl_V)
1355 colnr_T fromc, toc;
1357 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
1358 ++toc;
1359 if (curwin->w_curswant == MAXCOL)
1360 toc = MAXCOL;
1362 if (fromc != wp->w_old_cursor_fcol
1363 || toc != wp->w_old_cursor_lcol)
1365 if (from > VIsual.lnum)
1366 from = VIsual.lnum;
1367 if (to < VIsual.lnum)
1368 to = VIsual.lnum;
1370 wp->w_old_cursor_fcol = fromc;
1371 wp->w_old_cursor_lcol = toc;
1374 else
1376 /* Use the line numbers of the old Visual area. */
1377 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1379 from = wp->w_old_cursor_lnum;
1380 to = wp->w_old_visual_lnum;
1382 else
1384 from = wp->w_old_visual_lnum;
1385 to = wp->w_old_cursor_lnum;
1390 * There is no need to update lines above the top of the window.
1392 if (from < wp->w_topline)
1393 from = wp->w_topline;
1396 * If we know the value of w_botline, use it to restrict the update to
1397 * the lines that are visible in the window.
1399 if (wp->w_valid & VALID_BOTLINE)
1401 if (from >= wp->w_botline)
1402 from = wp->w_botline - 1;
1403 if (to >= wp->w_botline)
1404 to = wp->w_botline - 1;
1408 * Find the minimal part to be updated.
1409 * Watch out for scrolling that made entries in w_lines[] invalid.
1410 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1411 * top_end; need to redraw from top_end to the "to" line.
1412 * A middle mouse click with a Visual selection may change the text
1413 * above the Visual area and reset wl_valid, do count these for
1414 * mid_end (in srow).
1416 if (mid_start > 0)
1418 lnum = wp->w_topline;
1419 idx = 0;
1420 srow = 0;
1421 if (scrolled_down)
1422 mid_start = top_end;
1423 else
1424 mid_start = 0;
1425 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1427 if (wp->w_lines[idx].wl_valid)
1428 mid_start += wp->w_lines[idx].wl_size;
1429 else if (!scrolled_down)
1430 srow += wp->w_lines[idx].wl_size;
1431 ++idx;
1432 # ifdef FEAT_FOLDING
1433 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1434 lnum = wp->w_lines[idx].wl_lnum;
1435 else
1436 # endif
1437 ++lnum;
1439 srow += mid_start;
1440 mid_end = wp->w_height;
1441 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1443 if (wp->w_lines[idx].wl_valid
1444 && wp->w_lines[idx].wl_lnum >= to + 1)
1446 /* Only update until first row of this line */
1447 mid_end = srow;
1448 break;
1450 srow += wp->w_lines[idx].wl_size;
1455 if (VIsual_active && buf == curwin->w_buffer)
1457 wp->w_old_visual_mode = VIsual_mode;
1458 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1459 wp->w_old_visual_lnum = VIsual.lnum;
1460 wp->w_old_visual_col = VIsual.col;
1461 wp->w_old_curswant = curwin->w_curswant;
1463 else
1465 wp->w_old_visual_mode = 0;
1466 wp->w_old_cursor_lnum = 0;
1467 wp->w_old_visual_lnum = 0;
1468 wp->w_old_visual_col = 0;
1470 #endif /* FEAT_VISUAL */
1472 #if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1473 /* reset got_int, otherwise regexp won't work */
1474 save_got_int = got_int;
1475 got_int = 0;
1476 #endif
1477 #ifdef FEAT_FOLDING
1478 win_foldinfo.fi_level = 0;
1479 #endif
1482 * Update all the window rows.
1484 idx = 0; /* first entry in w_lines[].wl_size */
1485 row = 0;
1486 srow = 0;
1487 lnum = wp->w_topline; /* first line shown in window */
1488 for (;;)
1490 /* stop updating when reached the end of the window (check for _past_
1491 * the end of the window is at the end of the loop) */
1492 if (row == wp->w_height)
1494 didline = TRUE;
1495 break;
1498 /* stop updating when hit the end of the file */
1499 if (lnum > buf->b_ml.ml_line_count)
1501 eof = TRUE;
1502 break;
1505 /* Remember the starting row of the line that is going to be dealt
1506 * with. It is used further down when the line doesn't fit. */
1507 srow = row;
1510 * Update a line when it is in an area that needs updating, when it
1511 * has changes or w_lines[idx] is invalid.
1512 * bot_start may be halfway a wrapped line after using
1513 * win_del_lines(), check if the current line includes it.
1514 * When syntax folding is being used, the saved syntax states will
1515 * already have been updated, we can't see where the syntax state is
1516 * the same again, just update until the end of the window.
1518 if (row < top_end
1519 || (row >= mid_start && row < mid_end)
1520 #ifdef FEAT_SEARCH_EXTRA
1521 || top_to_mod
1522 #endif
1523 || idx >= wp->w_lines_valid
1524 || (row + wp->w_lines[idx].wl_size > bot_start)
1525 || (mod_top != 0
1526 && (lnum == mod_top
1527 || (lnum >= mod_top
1528 && (lnum < mod_bot
1529 #ifdef FEAT_SYN_HL
1530 || did_update == DID_FOLD
1531 || (did_update == DID_LINE
1532 && syntax_present(buf)
1533 && (
1534 # ifdef FEAT_FOLDING
1535 (foldmethodIsSyntax(wp)
1536 && hasAnyFolding(wp)) ||
1537 # endif
1538 syntax_check_changed(lnum)))
1539 #endif
1540 )))))
1542 #ifdef FEAT_SEARCH_EXTRA
1543 if (lnum == mod_top)
1544 top_to_mod = FALSE;
1545 #endif
1548 * When at start of changed lines: May scroll following lines
1549 * up or down to minimize redrawing.
1550 * Don't do this when the change continues until the end.
1551 * Don't scroll when dollar_vcol is non-zero, keep the "$".
1553 if (lnum == mod_top
1554 && mod_bot != MAXLNUM
1555 && !(dollar_vcol != 0 && mod_bot == mod_top + 1))
1557 int old_rows = 0;
1558 int new_rows = 0;
1559 int xtra_rows;
1560 linenr_T l;
1562 /* Count the old number of window rows, using w_lines[], which
1563 * should still contain the sizes for the lines as they are
1564 * currently displayed. */
1565 for (i = idx; i < wp->w_lines_valid; ++i)
1567 /* Only valid lines have a meaningful wl_lnum. Invalid
1568 * lines are part of the changed area. */
1569 if (wp->w_lines[i].wl_valid
1570 && wp->w_lines[i].wl_lnum == mod_bot)
1571 break;
1572 old_rows += wp->w_lines[i].wl_size;
1573 #ifdef FEAT_FOLDING
1574 if (wp->w_lines[i].wl_valid
1575 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1577 /* Must have found the last valid entry above mod_bot.
1578 * Add following invalid entries. */
1579 ++i;
1580 while (i < wp->w_lines_valid
1581 && !wp->w_lines[i].wl_valid)
1582 old_rows += wp->w_lines[i++].wl_size;
1583 break;
1585 #endif
1588 if (i >= wp->w_lines_valid)
1590 /* We can't find a valid line below the changed lines,
1591 * need to redraw until the end of the window.
1592 * Inserting/deleting lines has no use. */
1593 bot_start = 0;
1595 else
1597 /* Able to count old number of rows: Count new window
1598 * rows, and may insert/delete lines */
1599 j = idx;
1600 for (l = lnum; l < mod_bot; ++l)
1602 #ifdef FEAT_FOLDING
1603 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1604 ++new_rows;
1605 else
1606 #endif
1607 #ifdef FEAT_DIFF
1608 if (l == wp->w_topline)
1609 new_rows += plines_win_nofill(wp, l, TRUE)
1610 + wp->w_topfill;
1611 else
1612 #endif
1613 new_rows += plines_win(wp, l, TRUE);
1614 ++j;
1615 if (new_rows > wp->w_height - row - 2)
1617 /* it's getting too much, must redraw the rest */
1618 new_rows = 9999;
1619 break;
1622 xtra_rows = new_rows - old_rows;
1623 if (xtra_rows < 0)
1625 /* May scroll text up. If there is not enough
1626 * remaining text or scrolling fails, must redraw the
1627 * rest. If scrolling works, must redraw the text
1628 * below the scrolled text. */
1629 if (row - xtra_rows >= wp->w_height - 2)
1630 mod_bot = MAXLNUM;
1631 else
1633 check_for_delay(FALSE);
1634 if (win_del_lines(wp, row,
1635 -xtra_rows, FALSE, FALSE) == FAIL)
1636 mod_bot = MAXLNUM;
1637 else
1638 bot_start = wp->w_height + xtra_rows;
1641 else if (xtra_rows > 0)
1643 /* May scroll text down. If there is not enough
1644 * remaining text of scrolling fails, must redraw the
1645 * rest. */
1646 if (row + xtra_rows >= wp->w_height - 2)
1647 mod_bot = MAXLNUM;
1648 else
1650 check_for_delay(FALSE);
1651 if (win_ins_lines(wp, row + old_rows,
1652 xtra_rows, FALSE, FALSE) == FAIL)
1653 mod_bot = MAXLNUM;
1654 else if (top_end > row + old_rows)
1655 /* Scrolled the part at the top that requires
1656 * updating down. */
1657 top_end += xtra_rows;
1661 /* When not updating the rest, may need to move w_lines[]
1662 * entries. */
1663 if (mod_bot != MAXLNUM && i != j)
1665 if (j < i)
1667 int x = row + new_rows;
1669 /* move entries in w_lines[] upwards */
1670 for (;;)
1672 /* stop at last valid entry in w_lines[] */
1673 if (i >= wp->w_lines_valid)
1675 wp->w_lines_valid = j;
1676 break;
1678 wp->w_lines[j] = wp->w_lines[i];
1679 /* stop at a line that won't fit */
1680 if (x + (int)wp->w_lines[j].wl_size
1681 > wp->w_height)
1683 wp->w_lines_valid = j + 1;
1684 break;
1686 x += wp->w_lines[j++].wl_size;
1687 ++i;
1689 if (bot_start > x)
1690 bot_start = x;
1692 else /* j > i */
1694 /* move entries in w_lines[] downwards */
1695 j -= i;
1696 wp->w_lines_valid += j;
1697 if (wp->w_lines_valid > wp->w_height)
1698 wp->w_lines_valid = wp->w_height;
1699 for (i = wp->w_lines_valid; i - j >= idx; --i)
1700 wp->w_lines[i] = wp->w_lines[i - j];
1702 /* The w_lines[] entries for inserted lines are
1703 * now invalid, but wl_size may be used above.
1704 * Reset to zero. */
1705 while (i >= idx)
1707 wp->w_lines[i].wl_size = 0;
1708 wp->w_lines[i--].wl_valid = FALSE;
1715 #ifdef FEAT_FOLDING
1717 * When lines are folded, display one line for all of them.
1718 * Otherwise, display normally (can be several display lines when
1719 * 'wrap' is on).
1721 fold_count = foldedCount(wp, lnum, &win_foldinfo);
1722 if (fold_count != 0)
1724 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
1725 ++row;
1726 --fold_count;
1727 wp->w_lines[idx].wl_folded = TRUE;
1728 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
1729 # ifdef FEAT_SYN_HL
1730 did_update = DID_FOLD;
1731 # endif
1733 else
1734 #endif
1735 if (idx < wp->w_lines_valid
1736 && wp->w_lines[idx].wl_valid
1737 && wp->w_lines[idx].wl_lnum == lnum
1738 && lnum > wp->w_topline
1739 && !(dy_flags & DY_LASTLINE)
1740 && srow + wp->w_lines[idx].wl_size > wp->w_height
1741 #ifdef FEAT_DIFF
1742 && diff_check_fill(wp, lnum) == 0
1743 #endif
1746 /* This line is not going to fit. Don't draw anything here,
1747 * will draw "@ " lines below. */
1748 row = wp->w_height + 1;
1750 else
1752 #ifdef FEAT_SEARCH_EXTRA
1753 prepare_search_hl(wp, lnum);
1754 #endif
1755 #ifdef FEAT_SYN_HL
1756 /* Let the syntax stuff know we skipped a few lines. */
1757 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
1758 && syntax_present(buf))
1759 syntax_end_parsing(syntax_last_parsed + 1);
1760 #endif
1763 * Display one line.
1765 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
1767 #ifdef FEAT_FOLDING
1768 wp->w_lines[idx].wl_folded = FALSE;
1769 wp->w_lines[idx].wl_lastlnum = lnum;
1770 #endif
1771 #ifdef FEAT_SYN_HL
1772 did_update = DID_LINE;
1773 syntax_last_parsed = lnum;
1774 #endif
1777 wp->w_lines[idx].wl_lnum = lnum;
1778 wp->w_lines[idx].wl_valid = TRUE;
1779 if (row > wp->w_height) /* past end of screen */
1781 /* we may need the size of that too long line later on */
1782 if (dollar_vcol == 0)
1783 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
1784 ++idx;
1785 break;
1787 if (dollar_vcol == 0)
1788 wp->w_lines[idx].wl_size = row - srow;
1789 ++idx;
1790 #ifdef FEAT_FOLDING
1791 lnum += fold_count + 1;
1792 #else
1793 ++lnum;
1794 #endif
1796 else
1798 /* This line does not need updating, advance to the next one */
1799 row += wp->w_lines[idx++].wl_size;
1800 if (row > wp->w_height) /* past end of screen */
1801 break;
1802 #ifdef FEAT_FOLDING
1803 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
1804 #else
1805 ++lnum;
1806 #endif
1807 #ifdef FEAT_SYN_HL
1808 did_update = DID_NONE;
1809 #endif
1812 if (lnum > buf->b_ml.ml_line_count)
1814 eof = TRUE;
1815 break;
1819 * End of loop over all window lines.
1823 if (idx > wp->w_lines_valid)
1824 wp->w_lines_valid = idx;
1826 #ifdef FEAT_SYN_HL
1828 * Let the syntax stuff know we stop parsing here.
1830 if (syntax_last_parsed != 0 && syntax_present(buf))
1831 syntax_end_parsing(syntax_last_parsed + 1);
1832 #endif
1835 * If we didn't hit the end of the file, and we didn't finish the last
1836 * line we were working on, then the line didn't fit.
1838 wp->w_empty_rows = 0;
1839 #ifdef FEAT_DIFF
1840 wp->w_filler_rows = 0;
1841 #endif
1842 if (!eof && !didline)
1844 if (lnum == wp->w_topline)
1847 * Single line that does not fit!
1848 * Don't overwrite it, it can be edited.
1850 wp->w_botline = lnum + 1;
1852 #ifdef FEAT_DIFF
1853 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
1855 /* Window ends in filler lines. */
1856 wp->w_botline = lnum;
1857 wp->w_filler_rows = wp->w_height - srow;
1859 #endif
1860 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
1863 * Last line isn't finished: Display "@@@" at the end.
1865 screen_fill(W_WINROW(wp) + wp->w_height - 1,
1866 W_WINROW(wp) + wp->w_height,
1867 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
1868 '@', '@', hl_attr(HLF_AT));
1869 set_empty_rows(wp, srow);
1870 wp->w_botline = lnum;
1872 else
1874 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
1875 wp->w_botline = lnum;
1878 else
1880 #ifdef FEAT_VERTSPLIT
1881 draw_vsep_win(wp, row);
1882 #endif
1883 if (eof) /* we hit the end of the file */
1885 wp->w_botline = buf->b_ml.ml_line_count + 1;
1886 #ifdef FEAT_DIFF
1887 j = diff_check_fill(wp, wp->w_botline);
1888 if (j > 0 && !wp->w_botfill)
1891 * Display filler lines at the end of the file
1893 if (char2cells(fill_diff) > 1)
1894 i = '-';
1895 else
1896 i = fill_diff;
1897 if (row + j > wp->w_height)
1898 j = wp->w_height - row;
1899 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
1900 row += j;
1902 #endif
1904 else if (dollar_vcol == 0)
1905 wp->w_botline = lnum;
1907 /* make sure the rest of the screen is blank */
1908 /* put '~'s on rows that aren't part of the file. */
1909 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_AT);
1912 /* Reset the type of redrawing required, the window has been updated. */
1913 wp->w_redr_type = 0;
1914 #ifdef FEAT_DIFF
1915 wp->w_old_topfill = wp->w_topfill;
1916 wp->w_old_botfill = wp->w_botfill;
1917 #endif
1919 if (dollar_vcol == 0)
1922 * There is a trick with w_botline. If we invalidate it on each
1923 * change that might modify it, this will cause a lot of expensive
1924 * calls to plines() in update_topline() each time. Therefore the
1925 * value of w_botline is often approximated, and this value is used to
1926 * compute the value of w_topline. If the value of w_botline was
1927 * wrong, check that the value of w_topline is correct (cursor is on
1928 * the visible part of the text). If it's not, we need to redraw
1929 * again. Mostly this just means scrolling up a few lines, so it
1930 * doesn't look too bad. Only do this for the current window (where
1931 * changes are relevant).
1933 wp->w_valid |= VALID_BOTLINE;
1934 if (wp == curwin && wp->w_botline != old_botline && !recursive)
1936 recursive = TRUE;
1937 curwin->w_valid &= ~VALID_TOPLINE;
1938 update_topline(); /* may invalidate w_botline again */
1939 if (must_redraw != 0)
1941 /* Don't update for changes in buffer again. */
1942 i = curbuf->b_mod_set;
1943 curbuf->b_mod_set = FALSE;
1944 win_update(curwin);
1945 must_redraw = 0;
1946 curbuf->b_mod_set = i;
1948 recursive = FALSE;
1952 #if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1953 /* restore got_int, unless CTRL-C was hit while redrawing */
1954 if (!got_int)
1955 got_int = save_got_int;
1956 #endif
1959 #ifdef FEAT_SIGNS
1960 static int draw_signcolumn __ARGS((win_T *wp));
1963 * Return TRUE when window "wp" has a column to draw signs in.
1965 static int
1966 draw_signcolumn(wp)
1967 win_T *wp;
1969 return (wp->w_buffer->b_signlist != NULL
1970 # ifdef FEAT_NETBEANS_INTG
1971 || usingNetbeans
1972 # endif
1975 #endif
1978 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
1979 * as the filler character.
1981 static void
1982 win_draw_end(wp, c1, c2, row, endrow, hl)
1983 win_T *wp;
1984 int c1;
1985 int c2;
1986 int row;
1987 int endrow;
1988 hlf_T hl;
1990 #if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
1991 int n = 0;
1992 # define FDC_OFF n
1993 #else
1994 # define FDC_OFF 0
1995 #endif
1997 #ifdef FEAT_RIGHTLEFT
1998 if (wp->w_p_rl)
2000 /* No check for cmdline window: should never be right-left. */
2001 # ifdef FEAT_FOLDING
2002 n = wp->w_p_fdc;
2004 if (n > 0)
2006 /* draw the fold column at the right */
2007 if (n > W_WIDTH(wp))
2008 n = W_WIDTH(wp);
2009 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2010 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
2011 ' ', ' ', hl_attr(HLF_FC));
2013 # endif
2014 # ifdef FEAT_SIGNS
2015 if (draw_signcolumn(wp))
2017 int nn = n + 2;
2019 /* draw the sign column left of the fold column */
2020 if (nn > W_WIDTH(wp))
2021 nn = W_WIDTH(wp);
2022 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2023 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
2024 ' ', ' ', hl_attr(HLF_SC));
2025 n = nn;
2027 # endif
2028 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2029 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
2030 c2, c2, hl_attr(hl));
2031 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2032 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
2033 c1, c2, hl_attr(hl));
2035 else
2036 #endif
2038 #ifdef FEAT_CMDWIN
2039 if (cmdwin_type != 0 && wp == curwin)
2041 /* draw the cmdline character in the leftmost column */
2042 n = 1;
2043 if (n > wp->w_width)
2044 n = wp->w_width;
2045 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2046 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
2047 cmdwin_type, ' ', hl_attr(HLF_AT));
2049 #endif
2050 #ifdef FEAT_FOLDING
2051 if (wp->w_p_fdc > 0)
2053 int nn = n + wp->w_p_fdc;
2055 /* draw the fold column at the left */
2056 if (nn > W_WIDTH(wp))
2057 nn = W_WIDTH(wp);
2058 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2059 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2060 ' ', ' ', hl_attr(HLF_FC));
2061 n = nn;
2063 #endif
2064 #ifdef FEAT_SIGNS
2065 if (draw_signcolumn(wp))
2067 int nn = n + 2;
2069 /* draw the sign column after the fold column */
2070 if (nn > W_WIDTH(wp))
2071 nn = W_WIDTH(wp);
2072 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2073 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2074 ' ', ' ', hl_attr(HLF_SC));
2075 n = nn;
2077 #endif
2078 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2079 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
2080 c1, c2, hl_attr(hl));
2082 set_empty_rows(wp, row);
2085 #ifdef FEAT_FOLDING
2087 * Display one folded line.
2089 static void
2090 fold_line(wp, fold_count, foldinfo, lnum, row)
2091 win_T *wp;
2092 long fold_count;
2093 foldinfo_T *foldinfo;
2094 linenr_T lnum;
2095 int row;
2097 char_u buf[51];
2098 pos_T *top, *bot;
2099 linenr_T lnume = lnum + fold_count - 1;
2100 int len;
2101 char_u *text;
2102 int fdc;
2103 int col;
2104 int txtcol;
2105 int off = (int)(current_ScreenLine - ScreenLines);
2106 int ri;
2108 /* Build the fold line:
2109 * 1. Add the cmdwin_type for the command-line window
2110 * 2. Add the 'foldcolumn'
2111 * 3. Add the 'number' column
2112 * 4. Compose the text
2113 * 5. Add the text
2114 * 6. set highlighting for the Visual area an other text
2116 col = 0;
2119 * 1. Add the cmdwin_type for the command-line window
2120 * Ignores 'rightleft', this window is never right-left.
2122 #ifdef FEAT_CMDWIN
2123 if (cmdwin_type != 0 && wp == curwin)
2125 ScreenLines[off] = cmdwin_type;
2126 ScreenAttrs[off] = hl_attr(HLF_AT);
2127 #ifdef FEAT_MBYTE
2128 if (enc_utf8)
2129 ScreenLinesUC[off] = 0;
2130 #endif
2131 ++col;
2133 #endif
2136 * 2. Add the 'foldcolumn'
2138 fdc = wp->w_p_fdc;
2139 if (fdc > W_WIDTH(wp) - col)
2140 fdc = W_WIDTH(wp) - col;
2141 if (fdc > 0)
2143 fill_foldcolumn(buf, wp, TRUE, lnum);
2144 #ifdef FEAT_RIGHTLEFT
2145 if (wp->w_p_rl)
2147 int i;
2149 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
2150 hl_attr(HLF_FC));
2151 /* reverse the fold column */
2152 for (i = 0; i < fdc; ++i)
2153 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2155 else
2156 #endif
2157 copy_text_attr(off + col, buf, fdc, hl_attr(HLF_FC));
2158 col += fdc;
2161 #ifdef FEAT_RIGHTLEFT
2162 # define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2163 for (ri = 0; ri < l; ++ri) \
2164 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2165 else \
2166 for (ri = 0; ri < l; ++ri) \
2167 ScreenAttrs[off + (p) + ri] = v
2168 #else
2169 # define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2170 ScreenAttrs[off + (p) + ri] = v
2171 #endif
2173 /* Set all attributes of the 'number' column and the text */
2174 RL_MEMSET(col, hl_attr(HLF_FL), W_WIDTH(wp) - col);
2176 #ifdef FEAT_SIGNS
2177 /* If signs are being displayed, add two spaces. */
2178 if (draw_signcolumn(wp))
2180 len = W_WIDTH(wp) - col;
2181 if (len > 0)
2183 if (len > 2)
2184 len = 2;
2185 # ifdef FEAT_RIGHTLEFT
2186 if (wp->w_p_rl)
2187 /* the line number isn't reversed */
2188 copy_text_attr(off + W_WIDTH(wp) - len - col,
2189 (char_u *)" ", len, hl_attr(HLF_FL));
2190 else
2191 # endif
2192 copy_text_attr(off + col, (char_u *)" ", len, hl_attr(HLF_FL));
2193 col += len;
2196 #endif
2199 * 3. Add the 'number' column
2201 if (wp->w_p_nu)
2203 len = W_WIDTH(wp) - col;
2204 if (len > 0)
2206 int w = number_width(wp);
2208 if (len > w + 1)
2209 len = w + 1;
2210 sprintf((char *)buf, "%*ld ", w, (long)lnum);
2211 #ifdef FEAT_RIGHTLEFT
2212 if (wp->w_p_rl)
2213 /* the line number isn't reversed */
2214 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
2215 hl_attr(HLF_FL));
2216 else
2217 #endif
2218 copy_text_attr(off + col, buf, len, hl_attr(HLF_FL));
2219 col += len;
2224 * 4. Compose the folded-line string with 'foldtext', if set.
2226 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
2228 txtcol = col; /* remember where text starts */
2231 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2232 * Right-left text is put in columns 0 - number-col, normal text is put
2233 * in columns number-col - window-width.
2235 #ifdef FEAT_MBYTE
2236 if (has_mbyte)
2238 int cells;
2239 int u8c, u8cc[MAX_MCO];
2240 int i;
2241 int idx;
2242 int c_len;
2243 char_u *p;
2244 # ifdef FEAT_ARABIC
2245 int prev_c = 0; /* previous Arabic character */
2246 int prev_c1 = 0; /* first composing char for prev_c */
2247 # endif
2249 # ifdef FEAT_RIGHTLEFT
2250 if (wp->w_p_rl)
2251 idx = off;
2252 else
2253 # endif
2254 idx = off + col;
2256 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2257 for (p = text; *p != NUL; )
2259 cells = (*mb_ptr2cells)(p);
2260 c_len = (*mb_ptr2len)(p);
2261 if (col + cells > W_WIDTH(wp)
2262 # ifdef FEAT_RIGHTLEFT
2263 - (wp->w_p_rl ? col : 0)
2264 # endif
2266 break;
2267 ScreenLines[idx] = *p;
2268 if (enc_utf8)
2270 u8c = utfc_ptr2char(p, u8cc);
2271 if (*p < 0x80 && u8cc[0] == 0)
2273 ScreenLinesUC[idx] = 0;
2274 #ifdef FEAT_ARABIC
2275 prev_c = u8c;
2276 #endif
2278 else
2280 #ifdef FEAT_ARABIC
2281 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2283 /* Do Arabic shaping. */
2284 int pc, pc1, nc;
2285 int pcc[MAX_MCO];
2286 int firstbyte = *p;
2288 /* The idea of what is the previous and next
2289 * character depends on 'rightleft'. */
2290 if (wp->w_p_rl)
2292 pc = prev_c;
2293 pc1 = prev_c1;
2294 nc = utf_ptr2char(p + c_len);
2295 prev_c1 = u8cc[0];
2297 else
2299 pc = utfc_ptr2char(p + c_len, pcc);
2300 nc = prev_c;
2301 pc1 = pcc[0];
2303 prev_c = u8c;
2305 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2306 pc, pc1, nc);
2307 ScreenLines[idx] = firstbyte;
2309 else
2310 prev_c = u8c;
2311 #endif
2312 /* Non-BMP character: display as ? or fullwidth ?. */
2313 #ifdef UNICODE16
2314 if (u8c >= 0x10000)
2315 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2316 else
2317 #endif
2318 ScreenLinesUC[idx] = u8c;
2319 for (i = 0; i < Screen_mco; ++i)
2321 ScreenLinesC[i][idx] = u8cc[i];
2322 if (u8cc[i] == 0)
2323 break;
2326 if (cells > 1)
2327 ScreenLines[idx + 1] = 0;
2329 else if (cells > 1) /* double-byte character */
2331 if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2332 ScreenLines2[idx] = p[1];
2333 else
2334 ScreenLines[idx + 1] = p[1];
2336 col += cells;
2337 idx += cells;
2338 p += c_len;
2341 else
2342 #endif
2344 len = (int)STRLEN(text);
2345 if (len > W_WIDTH(wp) - col)
2346 len = W_WIDTH(wp) - col;
2347 if (len > 0)
2349 #ifdef FEAT_RIGHTLEFT
2350 if (wp->w_p_rl)
2351 STRNCPY(current_ScreenLine, text, len);
2352 else
2353 #endif
2354 STRNCPY(current_ScreenLine + col, text, len);
2355 col += len;
2359 /* Fill the rest of the line with the fold filler */
2360 #ifdef FEAT_RIGHTLEFT
2361 if (wp->w_p_rl)
2362 col -= txtcol;
2363 #endif
2364 while (col < W_WIDTH(wp)
2365 #ifdef FEAT_RIGHTLEFT
2366 - (wp->w_p_rl ? txtcol : 0)
2367 #endif
2370 #ifdef FEAT_MBYTE
2371 if (enc_utf8)
2373 if (fill_fold >= 0x80)
2375 ScreenLinesUC[off + col] = fill_fold;
2376 ScreenLinesC[0][off + col] = 0;
2378 else
2379 ScreenLinesUC[off + col] = 0;
2381 #endif
2382 ScreenLines[off + col++] = fill_fold;
2385 if (text != buf)
2386 vim_free(text);
2389 * 6. set highlighting for the Visual area an other text.
2390 * If all folded lines are in the Visual area, highlight the line.
2392 #ifdef FEAT_VISUAL
2393 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2395 if (ltoreq(curwin->w_cursor, VIsual))
2397 /* Visual is after curwin->w_cursor */
2398 top = &curwin->w_cursor;
2399 bot = &VIsual;
2401 else
2403 /* Visual is before curwin->w_cursor */
2404 top = &VIsual;
2405 bot = &curwin->w_cursor;
2407 if (lnum >= top->lnum
2408 && lnume <= bot->lnum
2409 && (VIsual_mode != 'v'
2410 || ((lnum > top->lnum
2411 || (lnum == top->lnum
2412 && top->col == 0))
2413 && (lnume < bot->lnum
2414 || (lnume == bot->lnum
2415 && (bot->col - (*p_sel == 'e'))
2416 >= STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
2418 if (VIsual_mode == Ctrl_V)
2420 /* Visual block mode: highlight the chars part of the block */
2421 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2423 if (wp->w_old_cursor_lcol + txtcol < (colnr_T)W_WIDTH(wp))
2424 len = wp->w_old_cursor_lcol;
2425 else
2426 len = W_WIDTH(wp) - txtcol;
2427 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, hl_attr(HLF_V),
2428 len - (int)wp->w_old_cursor_fcol);
2431 else
2433 /* Set all attributes of the text */
2434 RL_MEMSET(txtcol, hl_attr(HLF_V), W_WIDTH(wp) - txtcol);
2438 #endif
2440 #ifdef FEAT_SYN_HL
2441 /* Show 'cursorcolumn' in the fold line. */
2442 if (wp->w_p_cuc)
2444 txtcol += wp->w_virtcol;
2445 if (wp->w_p_wrap)
2446 txtcol -= wp->w_skipcol;
2447 else
2448 txtcol -= wp->w_leftcol;
2449 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2450 ScreenAttrs[off + txtcol] = hl_combine_attr(
2451 ScreenAttrs[off + txtcol], hl_attr(HLF_CUC));
2453 #endif
2455 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2456 (int)W_WIDTH(wp), FALSE);
2459 * Update w_cline_height and w_cline_folded if the cursor line was
2460 * updated (saves a call to plines() later).
2462 if (wp == curwin
2463 && lnum <= curwin->w_cursor.lnum
2464 && lnume >= curwin->w_cursor.lnum)
2466 curwin->w_cline_row = row;
2467 curwin->w_cline_height = 1;
2468 curwin->w_cline_folded = TRUE;
2469 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2474 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2476 static void
2477 copy_text_attr(off, buf, len, attr)
2478 int off;
2479 char_u *buf;
2480 int len;
2481 int attr;
2483 int i;
2485 mch_memmove(ScreenLines + off, buf, (size_t)len);
2486 # ifdef FEAT_MBYTE
2487 if (enc_utf8)
2488 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2489 # endif
2490 for (i = 0; i < len; ++i)
2491 ScreenAttrs[off + i] = attr;
2495 * Fill the foldcolumn at "p" for window "wp".
2496 * Only to be called when 'foldcolumn' > 0.
2498 static void
2499 fill_foldcolumn(p, wp, closed, lnum)
2500 char_u *p;
2501 win_T *wp;
2502 int closed; /* TRUE of FALSE */
2503 linenr_T lnum; /* current line number */
2505 int i = 0;
2506 int level;
2507 int first_level;
2508 int empty;
2510 /* Init to all spaces. */
2511 copy_spaces(p, (size_t)wp->w_p_fdc);
2513 level = win_foldinfo.fi_level;
2514 if (level > 0)
2516 /* If there is only one column put more info in it. */
2517 empty = (wp->w_p_fdc == 1) ? 0 : 1;
2519 /* If the column is too narrow, we start at the lowest level that
2520 * fits and use numbers to indicated the depth. */
2521 first_level = level - wp->w_p_fdc - closed + 1 + empty;
2522 if (first_level < 1)
2523 first_level = 1;
2525 for (i = 0; i + empty < wp->w_p_fdc; ++i)
2527 if (win_foldinfo.fi_lnum == lnum
2528 && first_level + i >= win_foldinfo.fi_low_level)
2529 p[i] = '-';
2530 else if (first_level == 1)
2531 p[i] = '|';
2532 else if (first_level + i <= 9)
2533 p[i] = '0' + first_level + i;
2534 else
2535 p[i] = '>';
2536 if (first_level + i == level)
2537 break;
2540 if (closed)
2541 p[i >= wp->w_p_fdc ? i - 1 : i] = '+';
2543 #endif /* FEAT_FOLDING */
2546 * Display line "lnum" of window 'wp' on the screen.
2547 * Start at row "startrow", stop when "endrow" is reached.
2548 * wp->w_virtcol needs to be valid.
2550 * Return the number of last row the line occupies.
2552 /* ARGSUSED */
2553 static int
2554 win_line(wp, lnum, startrow, endrow, nochange)
2555 win_T *wp;
2556 linenr_T lnum;
2557 int startrow;
2558 int endrow;
2559 int nochange; /* not updating for changed text */
2561 int col; /* visual column on screen */
2562 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2563 int c = 0; /* init for GCC */
2564 long vcol = 0; /* virtual column (for tabs) */
2565 long vcol_prev = -1; /* "vcol" of previous character */
2566 char_u *line; /* current line */
2567 char_u *ptr; /* current position in "line" */
2568 int row; /* row in the window, excl w_winrow */
2569 int screen_row; /* row on the screen, incl w_winrow */
2571 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2572 int n_extra = 0; /* number of extra chars */
2573 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
2574 int c_extra = NUL; /* extra chars, all the same */
2575 int extra_attr = 0; /* attributes when n_extra != 0 */
2576 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2577 displaying lcs_eol at end-of-line */
2578 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2579 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2581 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2582 int saved_n_extra = 0;
2583 char_u *saved_p_extra = NULL;
2584 int saved_c_extra = 0;
2585 int saved_char_attr = 0;
2587 int n_attr = 0; /* chars with special attr */
2588 int saved_attr2 = 0; /* char_attr saved for n_attr */
2589 int n_attr3 = 0; /* chars with overruling special attr */
2590 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2592 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2594 int fromcol, tocol; /* start/end of inverting */
2595 int fromcol_prev = -2; /* start of inverting after cursor */
2596 int noinvcur = FALSE; /* don't invert the cursor */
2597 #ifdef FEAT_VISUAL
2598 pos_T *top, *bot;
2599 int lnum_in_visual_area = FALSE;
2600 #endif
2601 pos_T pos;
2602 long v;
2604 int char_attr = 0; /* attributes for next character */
2605 int attr_pri = FALSE; /* char_attr has priority */
2606 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2607 in this line */
2608 int attr = 0; /* attributes for area highlighting */
2609 int area_attr = 0; /* attributes desired by highlighting */
2610 int search_attr = 0; /* attributes desired by 'hlsearch' */
2611 #ifdef FEAT_SYN_HL
2612 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
2613 int syntax_attr = 0; /* attributes desired by syntax */
2614 int has_syntax = FALSE; /* this buffer has syntax highl. */
2615 int save_did_emsg;
2616 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
2617 #endif
2618 #ifdef FEAT_SPELL
2619 int has_spell = FALSE; /* this buffer has spell checking */
2620 # define SPWORDLEN 150
2621 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
2622 int nextlinecol = 0; /* column where nextline[] starts */
2623 int nextline_idx = 0; /* index in nextline[] where next line
2624 starts */
2625 int spell_attr = 0; /* attributes desired by spelling */
2626 int word_end = 0; /* last byte with same spell_attr */
2627 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2628 static int checked_col = 0; /* column in "checked_lnum" up to which
2629 * there are no spell errors */
2630 static int cap_col = -1; /* column to check for Cap word */
2631 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
2632 int cur_checked_col = 0; /* checked column for current line */
2633 #endif
2634 int extra_check; /* has syntax or linebreak */
2635 #ifdef FEAT_MBYTE
2636 int multi_attr = 0; /* attributes desired by multibyte */
2637 int mb_l = 1; /* multi-byte byte length */
2638 int mb_c = 0; /* decoded multi-byte character */
2639 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
2640 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
2641 #endif
2642 #ifdef FEAT_DIFF
2643 int filler_lines; /* nr of filler lines to be drawn */
2644 int filler_todo; /* nr of filler lines still to do + 1 */
2645 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
2646 int change_start = MAXCOL; /* first col of changed area */
2647 int change_end = -1; /* last col of changed area */
2648 #endif
2649 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
2650 #ifdef FEAT_LINEBREAK
2651 int need_showbreak = FALSE;
2652 #endif
2653 #if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
2654 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
2655 # define LINE_ATTR
2656 int line_attr = 0; /* attribute for the whole line */
2657 #endif
2658 #ifdef FEAT_SEARCH_EXTRA
2659 matchitem_T *cur; /* points to the match list */
2660 match_T *shl; /* points to search_hl or a match */
2661 int shl_flag; /* flag to indicate whether search_hl
2662 has been processed or not */
2663 int prevcol_hl_flag; /* flag to indicate whether prevcol
2664 equals startcol of search_hl or one
2665 of the matches */
2666 #endif
2667 #ifdef FEAT_ARABIC
2668 int prev_c = 0; /* previous Arabic character */
2669 int prev_c1 = 0; /* first composing char for prev_c */
2670 #endif
2671 #if defined(LINE_ATTR)
2672 int did_line_attr = 0;
2673 #endif
2675 /* draw_state: items that are drawn in sequence: */
2676 #define WL_START 0 /* nothing done yet */
2677 #ifdef FEAT_CMDWIN
2678 # define WL_CMDLINE WL_START + 1 /* cmdline window column */
2679 #else
2680 # define WL_CMDLINE WL_START
2681 #endif
2682 #ifdef FEAT_FOLDING
2683 # define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
2684 #else
2685 # define WL_FOLD WL_CMDLINE
2686 #endif
2687 #ifdef FEAT_SIGNS
2688 # define WL_SIGN WL_FOLD + 1 /* column for signs */
2689 #else
2690 # define WL_SIGN WL_FOLD /* column for signs */
2691 #endif
2692 #define WL_NR WL_SIGN + 1 /* line number */
2693 #if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
2694 # define WL_SBR WL_NR + 1 /* 'showbreak' or 'diff' */
2695 #else
2696 # define WL_SBR WL_NR
2697 #endif
2698 #define WL_LINE WL_SBR + 1 /* text in the line */
2699 int draw_state = WL_START; /* what to draw next */
2700 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2701 int feedback_col = 0;
2702 int feedback_old_attr = -1;
2703 #endif
2706 if (startrow > endrow) /* past the end already! */
2707 return startrow;
2709 row = startrow;
2710 screen_row = row + W_WINROW(wp);
2713 * To speed up the loop below, set extra_check when there is linebreak,
2714 * trailing white space and/or syntax processing to be done.
2716 #ifdef FEAT_LINEBREAK
2717 extra_check = wp->w_p_lbr;
2718 #else
2719 extra_check = 0;
2720 #endif
2721 #ifdef FEAT_SYN_HL
2722 if (syntax_present(wp->w_buffer) && !wp->w_buffer->b_syn_error)
2724 /* Prepare for syntax highlighting in this line. When there is an
2725 * error, stop syntax highlighting. */
2726 save_did_emsg = did_emsg;
2727 did_emsg = FALSE;
2728 syntax_start(wp, lnum);
2729 if (did_emsg)
2730 wp->w_buffer->b_syn_error = TRUE;
2731 else
2733 did_emsg = save_did_emsg;
2734 has_syntax = TRUE;
2735 extra_check = TRUE;
2738 #endif
2740 #ifdef FEAT_SPELL
2741 if (wp->w_p_spell
2742 && *wp->w_buffer->b_p_spl != NUL
2743 && wp->w_buffer->b_langp.ga_len > 0
2744 && *(char **)(wp->w_buffer->b_langp.ga_data) != NULL)
2746 /* Prepare for spell checking. */
2747 has_spell = TRUE;
2748 extra_check = TRUE;
2750 /* Get the start of the next line, so that words that wrap to the next
2751 * line are found too: "et<line-break>al.".
2752 * Trick: skip a few chars for C/shell/Vim comments */
2753 nextline[SPWORDLEN] = NUL;
2754 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2756 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
2757 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
2760 /* When a word wrapped from the previous line the start of the current
2761 * line is valid. */
2762 if (lnum == checked_lnum)
2763 cur_checked_col = checked_col;
2764 checked_lnum = 0;
2766 /* When there was a sentence end in the previous line may require a
2767 * word starting with capital in this line. In line 1 always check
2768 * the first word. */
2769 if (lnum != capcol_lnum)
2770 cap_col = -1;
2771 if (lnum == 1)
2772 cap_col = 0;
2773 capcol_lnum = 0;
2775 #endif
2778 * handle visual active in this window
2780 fromcol = -10;
2781 tocol = MAXCOL;
2782 #ifdef FEAT_VISUAL
2783 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2785 /* Visual is after curwin->w_cursor */
2786 if (ltoreq(curwin->w_cursor, VIsual))
2788 top = &curwin->w_cursor;
2789 bot = &VIsual;
2791 else /* Visual is before curwin->w_cursor */
2793 top = &VIsual;
2794 bot = &curwin->w_cursor;
2796 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
2797 if (VIsual_mode == Ctrl_V) /* block mode */
2799 if (lnum_in_visual_area)
2801 fromcol = wp->w_old_cursor_fcol;
2802 tocol = wp->w_old_cursor_lcol;
2805 else /* non-block mode */
2807 if (lnum > top->lnum && lnum <= bot->lnum)
2808 fromcol = 0;
2809 else if (lnum == top->lnum)
2811 if (VIsual_mode == 'V') /* linewise */
2812 fromcol = 0;
2813 else
2815 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
2816 if (gchar_pos(top) == NUL)
2817 tocol = fromcol + 1;
2820 if (VIsual_mode != 'V' && lnum == bot->lnum)
2822 if (*p_sel == 'e' && bot->col == 0
2823 #ifdef FEAT_VIRTUALEDIT
2824 && bot->coladd == 0
2825 #endif
2828 fromcol = -10;
2829 tocol = MAXCOL;
2831 else if (bot->col == MAXCOL)
2832 tocol = MAXCOL;
2833 else
2835 pos = *bot;
2836 if (*p_sel == 'e')
2837 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
2838 else
2840 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
2841 ++tocol;
2847 #ifndef MSDOS
2848 /* Check if the character under the cursor should not be inverted */
2849 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
2850 # ifdef FEAT_GUI
2851 && !gui.in_use
2852 # endif
2854 noinvcur = TRUE;
2855 #endif
2857 /* if inverting in this line set area_highlighting */
2858 if (fromcol >= 0)
2860 area_highlighting = TRUE;
2861 attr = hl_attr(HLF_V);
2862 #if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
2863 if (clip_star.available && !clip_star.owned && clip_isautosel())
2864 attr = hl_attr(HLF_VNC);
2865 #endif
2870 * handle 'incsearch' and ":s///c" highlighting
2872 else
2873 #endif /* FEAT_VISUAL */
2874 if (highlight_match
2875 && wp == curwin
2876 && lnum >= curwin->w_cursor.lnum
2877 && lnum <= curwin->w_cursor.lnum + search_match_lines)
2879 if (lnum == curwin->w_cursor.lnum)
2880 getvcol(curwin, &(curwin->w_cursor),
2881 (colnr_T *)&fromcol, NULL, NULL);
2882 else
2883 fromcol = 0;
2884 if (lnum == curwin->w_cursor.lnum + search_match_lines)
2886 pos.lnum = lnum;
2887 pos.col = search_match_endcol;
2888 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
2890 else
2891 tocol = MAXCOL;
2892 if (fromcol == tocol) /* do at least one character */
2893 tocol = fromcol + 1; /* happens when past end of line */
2894 area_highlighting = TRUE;
2895 attr = hl_attr(HLF_I);
2898 #ifdef FEAT_DIFF
2899 filler_lines = diff_check(wp, lnum);
2900 if (filler_lines < 0)
2902 if (filler_lines == -1)
2904 if (diff_find_change(wp, lnum, &change_start, &change_end))
2905 diff_hlf = HLF_ADD; /* added line */
2906 else if (change_start == 0)
2907 diff_hlf = HLF_TXD; /* changed text */
2908 else
2909 diff_hlf = HLF_CHD; /* changed line */
2911 else
2912 diff_hlf = HLF_ADD; /* added line */
2913 filler_lines = 0;
2914 area_highlighting = TRUE;
2916 if (lnum == wp->w_topline)
2917 filler_lines = wp->w_topfill;
2918 filler_todo = filler_lines;
2919 #endif
2921 #ifdef LINE_ATTR
2922 # ifdef FEAT_SIGNS
2923 /* If this line has a sign with line highlighting set line_attr. */
2924 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
2925 if (v != 0)
2926 line_attr = sign_get_attr((int)v, TRUE);
2927 # endif
2928 # if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
2929 /* Highlight the current line in the quickfix window. */
2930 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
2931 line_attr = hl_attr(HLF_L);
2932 # endif
2933 if (line_attr != 0)
2934 area_highlighting = TRUE;
2935 #endif
2937 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
2938 ptr = line;
2940 #ifdef FEAT_SPELL
2941 if (has_spell)
2943 /* For checking first word with a capital skip white space. */
2944 if (cap_col == 0)
2945 cap_col = (int)(skipwhite(line) - line);
2947 /* To be able to spell-check over line boundaries copy the end of the
2948 * current line into nextline[]. Above the start of the next line was
2949 * copied to nextline[SPWORDLEN]. */
2950 if (nextline[SPWORDLEN] == NUL)
2952 /* No next line or it is empty. */
2953 nextlinecol = MAXCOL;
2954 nextline_idx = 0;
2956 else
2958 v = (long)STRLEN(line);
2959 if (v < SPWORDLEN)
2961 /* Short line, use it completely and append the start of the
2962 * next line. */
2963 nextlinecol = 0;
2964 mch_memmove(nextline, line, (size_t)v);
2965 STRMOVE(nextline + v, nextline + SPWORDLEN);
2966 nextline_idx = v + 1;
2968 else
2970 /* Long line, use only the last SPWORDLEN bytes. */
2971 nextlinecol = v - SPWORDLEN;
2972 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
2973 nextline_idx = SPWORDLEN + 1;
2977 #endif
2979 /* find start of trailing whitespace */
2980 if (wp->w_p_list && lcs_trail)
2982 trailcol = (colnr_T)STRLEN(ptr);
2983 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
2984 --trailcol;
2985 trailcol += (colnr_T) (ptr - line);
2986 extra_check = TRUE;
2990 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
2991 * first character to be displayed.
2993 if (wp->w_p_wrap)
2994 v = wp->w_skipcol;
2995 else
2996 v = wp->w_leftcol;
2997 if (v > 0)
2999 #ifdef FEAT_MBYTE
3000 char_u *prev_ptr = ptr;
3001 #endif
3002 while (vcol < v && *ptr != NUL)
3004 c = win_lbr_chartabsize(wp, ptr, (colnr_T)vcol, NULL);
3005 vcol += c;
3006 #ifdef FEAT_MBYTE
3007 prev_ptr = ptr;
3008 #endif
3009 mb_ptr_adv(ptr);
3012 #ifdef FEAT_VIRTUALEDIT
3013 /* When 'virtualedit' is set the end of the line may be before the
3014 * start of the displayed part. */
3015 if (vcol < v && *ptr == NUL && virtual_active())
3016 vcol = v;
3017 #endif
3019 /* Handle a character that's not completely on the screen: Put ptr at
3020 * that character but skip the first few screen characters. */
3021 if (vcol > v)
3023 vcol -= c;
3024 #ifdef FEAT_MBYTE
3025 ptr = prev_ptr;
3026 #else
3027 --ptr;
3028 #endif
3029 n_skip = v - vcol;
3033 * Adjust for when the inverted text is before the screen,
3034 * and when the start of the inverted text is before the screen.
3036 if (tocol <= vcol)
3037 fromcol = 0;
3038 else if (fromcol >= 0 && fromcol < vcol)
3039 fromcol = vcol;
3041 #ifdef FEAT_LINEBREAK
3042 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3043 if (wp->w_p_wrap)
3044 need_showbreak = TRUE;
3045 #endif
3046 #ifdef FEAT_SPELL
3047 /* When spell checking a word we need to figure out the start of the
3048 * word and if it's badly spelled or not. */
3049 if (has_spell)
3051 int len;
3052 colnr_T linecol = (colnr_T)(ptr - line);
3053 hlf_T spell_hlf = HLF_COUNT;
3055 pos = wp->w_cursor;
3056 wp->w_cursor.lnum = lnum;
3057 wp->w_cursor.col = linecol;
3058 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
3060 /* spell_move_to() may call ml_get() and make "line" invalid */
3061 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3062 ptr = line + linecol;
3064 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
3066 /* no bad word found at line start, don't check until end of a
3067 * word */
3068 spell_hlf = HLF_COUNT;
3069 word_end = (int)(spell_to_word_end(ptr, wp->w_buffer)
3070 - line + 1);
3072 else
3074 /* bad word found, use attributes until end of word */
3075 word_end = wp->w_cursor.col + len + 1;
3077 /* Turn index into actual attributes. */
3078 if (spell_hlf != HLF_COUNT)
3079 spell_attr = highlight_attr[spell_hlf];
3081 wp->w_cursor = pos;
3083 # ifdef FEAT_SYN_HL
3084 /* Need to restart syntax highlighting for this line. */
3085 if (has_syntax)
3086 syntax_start(wp, lnum);
3087 # endif
3089 #endif
3093 * Correct highlighting for cursor that can't be disabled.
3094 * Avoids having to check this for each character.
3096 if (fromcol >= 0)
3098 if (noinvcur)
3100 if ((colnr_T)fromcol == wp->w_virtcol)
3102 /* highlighting starts at cursor, let it start just after the
3103 * cursor */
3104 fromcol_prev = fromcol;
3105 fromcol = -1;
3107 else if ((colnr_T)fromcol < wp->w_virtcol)
3108 /* restart highlighting after the cursor */
3109 fromcol_prev = wp->w_virtcol;
3111 if (fromcol >= tocol)
3112 fromcol = -1;
3115 #ifdef FEAT_SEARCH_EXTRA
3117 * Handle highlighting the last used search pattern and matches.
3118 * Do this for both search_hl and the match list.
3120 cur = wp->w_match_head;
3121 shl_flag = FALSE;
3122 while (cur != NULL || shl_flag == FALSE)
3124 if (shl_flag == FALSE)
3126 shl = &search_hl;
3127 shl_flag = TRUE;
3129 else
3130 shl = &cur->hl;
3131 shl->startcol = MAXCOL;
3132 shl->endcol = MAXCOL;
3133 shl->attr_cur = 0;
3134 if (shl->rm.regprog != NULL)
3136 v = (long)(ptr - line);
3137 next_search_hl(wp, shl, lnum, (colnr_T)v);
3139 /* Need to get the line again, a multi-line regexp may have made it
3140 * invalid. */
3141 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3142 ptr = line + v;
3144 if (shl->lnum != 0 && shl->lnum <= lnum)
3146 if (shl->lnum == lnum)
3147 shl->startcol = shl->rm.startpos[0].col;
3148 else
3149 shl->startcol = 0;
3150 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3151 - shl->rm.startpos[0].lnum)
3152 shl->endcol = shl->rm.endpos[0].col;
3153 else
3154 shl->endcol = MAXCOL;
3155 /* Highlight one character for an empty match. */
3156 if (shl->startcol == shl->endcol)
3158 #ifdef FEAT_MBYTE
3159 if (has_mbyte && line[shl->endcol] != NUL)
3160 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3161 else
3162 #endif
3163 ++shl->endcol;
3165 if ((long)shl->startcol < v) /* match at leftcol */
3167 shl->attr_cur = shl->attr;
3168 search_attr = shl->attr;
3170 area_highlighting = TRUE;
3173 if (shl != &search_hl && cur != NULL)
3174 cur = cur->next;
3176 #endif
3178 #ifdef FEAT_SYN_HL
3179 /* Cursor line highlighting for 'cursorline'. Not when Visual mode is
3180 * active, because it's not clear what is selected then. */
3181 if (wp->w_p_cul && lnum == wp->w_cursor.lnum && !VIsual_active)
3183 line_attr = hl_attr(HLF_CUL);
3184 area_highlighting = TRUE;
3186 #endif
3188 off = (unsigned)(current_ScreenLine - ScreenLines);
3189 col = 0;
3190 #ifdef FEAT_RIGHTLEFT
3191 if (wp->w_p_rl)
3193 /* Rightleft window: process the text in the normal direction, but put
3194 * it in current_ScreenLine[] from right to left. Start at the
3195 * rightmost column of the window. */
3196 col = W_WIDTH(wp) - 1;
3197 off += col;
3199 #endif
3202 * Repeat for the whole displayed line.
3204 for (;;)
3206 /* Skip this quickly when working on the text. */
3207 if (draw_state != WL_LINE)
3209 #ifdef FEAT_CMDWIN
3210 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3212 draw_state = WL_CMDLINE;
3213 if (cmdwin_type != 0 && wp == curwin)
3215 /* Draw the cmdline character. */
3216 n_extra = 1;
3217 c_extra = cmdwin_type;
3218 char_attr = hl_attr(HLF_AT);
3221 #endif
3223 #ifdef FEAT_FOLDING
3224 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3226 draw_state = WL_FOLD;
3227 if (wp->w_p_fdc > 0)
3229 /* Draw the 'foldcolumn'. */
3230 fill_foldcolumn(extra, wp, FALSE, lnum);
3231 n_extra = wp->w_p_fdc;
3232 p_extra = extra;
3233 p_extra[n_extra] = NUL;
3234 c_extra = NUL;
3235 char_attr = hl_attr(HLF_FC);
3238 #endif
3240 #ifdef FEAT_SIGNS
3241 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3243 draw_state = WL_SIGN;
3244 /* Show the sign column when there are any signs in this
3245 * buffer or when using Netbeans. */
3246 if (draw_signcolumn(wp)
3247 # ifdef FEAT_DIFF
3248 && filler_todo <= 0
3249 # endif
3252 int_u text_sign;
3253 # ifdef FEAT_SIGN_ICONS
3254 int_u icon_sign;
3255 # endif
3257 /* Draw two cells with the sign value or blank. */
3258 c_extra = ' ';
3259 char_attr = hl_attr(HLF_SC);
3260 n_extra = 2;
3262 if (row == startrow)
3264 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3265 SIGN_TEXT);
3266 # ifdef FEAT_SIGN_ICONS
3267 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3268 SIGN_ICON);
3269 if (gui.in_use && icon_sign != 0)
3271 /* Use the image in this position. */
3272 c_extra = SIGN_BYTE;
3273 # ifdef FEAT_NETBEANS_INTG
3274 if (buf_signcount(wp->w_buffer, lnum) > 1)
3275 c_extra = MULTISIGN_BYTE;
3276 # endif
3277 char_attr = icon_sign;
3279 else
3280 # endif
3281 if (text_sign != 0)
3283 p_extra = sign_get_text(text_sign);
3284 if (p_extra != NULL)
3286 c_extra = NUL;
3287 n_extra = (int)STRLEN(p_extra);
3289 char_attr = sign_get_attr(text_sign, FALSE);
3294 #endif
3296 if (draw_state == WL_NR - 1 && n_extra == 0)
3298 draw_state = WL_NR;
3299 /* Display the line number. After the first fill with blanks
3300 * when the 'n' flag isn't in 'cpo' */
3301 if (wp->w_p_nu
3302 && (row == startrow
3303 #ifdef FEAT_DIFF
3304 + filler_lines
3305 #endif
3306 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3308 /* Draw the line number (empty space after wrapping). */
3309 if (row == startrow
3310 #ifdef FEAT_DIFF
3311 + filler_lines
3312 #endif
3315 sprintf((char *)extra, "%*ld ",
3316 number_width(wp), (long)lnum);
3317 if (wp->w_skipcol > 0)
3318 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3319 *p_extra = '-';
3320 #ifdef FEAT_RIGHTLEFT
3321 if (wp->w_p_rl) /* reverse line numbers */
3322 rl_mirror(extra);
3323 #endif
3324 p_extra = extra;
3325 c_extra = NUL;
3327 else
3328 c_extra = ' ';
3329 n_extra = number_width(wp) + 1;
3330 char_attr = hl_attr(HLF_N);
3331 #ifdef FEAT_SYN_HL
3332 /* When 'cursorline' is set highlight the line number of
3333 * the current line differently. */
3334 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3335 char_attr = hl_combine_attr(hl_attr(HLF_CUL), char_attr);
3336 #endif
3340 #if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3341 if (draw_state == WL_SBR - 1 && n_extra == 0)
3343 draw_state = WL_SBR;
3344 # ifdef FEAT_DIFF
3345 if (filler_todo > 0)
3347 /* Draw "deleted" diff line(s). */
3348 if (char2cells(fill_diff) > 1)
3349 c_extra = '-';
3350 else
3351 c_extra = fill_diff;
3352 # ifdef FEAT_RIGHTLEFT
3353 if (wp->w_p_rl)
3354 n_extra = col + 1;
3355 else
3356 # endif
3357 n_extra = W_WIDTH(wp) - col;
3358 char_attr = hl_attr(HLF_DED);
3360 # endif
3361 # ifdef FEAT_LINEBREAK
3362 if (*p_sbr != NUL && need_showbreak)
3364 /* Draw 'showbreak' at the start of each broken line. */
3365 p_extra = p_sbr;
3366 c_extra = NUL;
3367 n_extra = (int)STRLEN(p_sbr);
3368 char_attr = hl_attr(HLF_AT);
3369 need_showbreak = FALSE;
3370 /* Correct end of highlighted area for 'showbreak',
3371 * required when 'linebreak' is also set. */
3372 if (tocol == vcol)
3373 tocol += n_extra;
3375 # endif
3377 #endif
3379 if (draw_state == WL_LINE - 1 && n_extra == 0)
3381 draw_state = WL_LINE;
3382 if (saved_n_extra)
3384 /* Continue item from end of wrapped line. */
3385 n_extra = saved_n_extra;
3386 c_extra = saved_c_extra;
3387 p_extra = saved_p_extra;
3388 char_attr = saved_char_attr;
3390 else
3391 char_attr = 0;
3395 /* When still displaying '$' of change command, stop at cursor */
3396 if (dollar_vcol != 0 && wp == curwin
3397 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
3398 #ifdef FEAT_DIFF
3399 && filler_todo <= 0
3400 #endif
3403 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3404 wp->w_p_rl);
3405 /* Pretend we have finished updating the window. Except when
3406 * 'cursorcolumn' is set. */
3407 #ifdef FEAT_SYN_HL
3408 if (wp->w_p_cuc)
3409 row = wp->w_cline_row + wp->w_cline_height;
3410 else
3411 #endif
3412 row = wp->w_height;
3413 break;
3416 if (draw_state == WL_LINE && area_highlighting)
3418 /* handle Visual or match highlighting in this line */
3419 if (vcol == fromcol
3420 #ifdef FEAT_MBYTE
3421 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3422 && (*mb_ptr2cells)(ptr) > 1)
3423 #endif
3424 || ((int)vcol_prev == fromcol_prev
3425 && vcol_prev < vcol /* not at margin */
3426 && vcol < tocol))
3427 area_attr = attr; /* start highlighting */
3428 else if (area_attr != 0
3429 && (vcol == tocol
3430 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
3431 area_attr = 0; /* stop highlighting */
3433 #ifdef FEAT_SEARCH_EXTRA
3434 if (!n_extra)
3437 * Check for start/end of search pattern match.
3438 * After end, check for start/end of next match.
3439 * When another match, have to check for start again.
3440 * Watch out for matching an empty string!
3441 * Do this for 'search_hl' and the match list (ordered by
3442 * priority).
3444 v = (long)(ptr - line);
3445 cur = wp->w_match_head;
3446 shl_flag = FALSE;
3447 while (cur != NULL || shl_flag == FALSE)
3449 if (shl_flag == FALSE
3450 && ((cur != NULL
3451 && cur->priority > SEARCH_HL_PRIORITY)
3452 || cur == NULL))
3454 shl = &search_hl;
3455 shl_flag = TRUE;
3457 else
3458 shl = &cur->hl;
3459 while (shl->rm.regprog != NULL)
3461 if (shl->startcol != MAXCOL
3462 && v >= (long)shl->startcol
3463 && v < (long)shl->endcol)
3465 shl->attr_cur = shl->attr;
3467 else if (v == (long)shl->endcol)
3469 shl->attr_cur = 0;
3471 next_search_hl(wp, shl, lnum, (colnr_T)v);
3473 /* Need to get the line again, a multi-line regexp
3474 * may have made it invalid. */
3475 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3476 ptr = line + v;
3478 if (shl->lnum == lnum)
3480 shl->startcol = shl->rm.startpos[0].col;
3481 if (shl->rm.endpos[0].lnum == 0)
3482 shl->endcol = shl->rm.endpos[0].col;
3483 else
3484 shl->endcol = MAXCOL;
3486 if (shl->startcol == shl->endcol)
3488 /* highlight empty match, try again after
3489 * it */
3490 #ifdef FEAT_MBYTE
3491 if (has_mbyte)
3492 shl->endcol += (*mb_ptr2len)(line
3493 + shl->endcol);
3494 else
3495 #endif
3496 ++shl->endcol;
3499 /* Loop to check if the match starts at the
3500 * current position */
3501 continue;
3504 break;
3506 if (shl != &search_hl && cur != NULL)
3507 cur = cur->next;
3510 /* Use attributes from match with highest priority among
3511 * 'search_hl' and the match list. */
3512 search_attr = search_hl.attr_cur;
3513 cur = wp->w_match_head;
3514 shl_flag = FALSE;
3515 while (cur != NULL || shl_flag == FALSE)
3517 if (shl_flag == FALSE
3518 && ((cur != NULL
3519 && cur->priority > SEARCH_HL_PRIORITY)
3520 || cur == NULL))
3522 shl = &search_hl;
3523 shl_flag = TRUE;
3525 else
3526 shl = &cur->hl;
3527 if (shl->attr_cur != 0)
3528 search_attr = shl->attr_cur;
3529 if (shl != &search_hl && cur != NULL)
3530 cur = cur->next;
3533 #endif
3535 #ifdef FEAT_DIFF
3536 if (diff_hlf != (hlf_T)0)
3538 if (diff_hlf == HLF_CHD && ptr - line >= change_start
3539 && n_extra == 0)
3540 diff_hlf = HLF_TXD; /* changed text */
3541 if (diff_hlf == HLF_TXD && ptr - line > change_end
3542 && n_extra == 0)
3543 diff_hlf = HLF_CHD; /* changed line */
3544 line_attr = hl_attr(diff_hlf);
3546 #endif
3548 /* Decide which of the highlight attributes to use. */
3549 attr_pri = TRUE;
3550 if (area_attr != 0)
3551 char_attr = area_attr;
3552 else if (search_attr != 0)
3553 char_attr = search_attr;
3554 #ifdef LINE_ATTR
3555 /* Use line_attr when not in the Visual or 'incsearch' area
3556 * (area_attr may be 0 when "noinvcur" is set). */
3557 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
3558 || vcol < fromcol || vcol_prev < fromcol_prev
3559 || vcol >= tocol))
3560 char_attr = line_attr;
3561 #endif
3562 else
3564 attr_pri = FALSE;
3565 #ifdef FEAT_SYN_HL
3566 if (has_syntax)
3567 char_attr = syntax_attr;
3568 else
3569 #endif
3570 char_attr = 0;
3575 * Get the next character to put on the screen.
3578 * The "p_extra" points to the extra stuff that is inserted to
3579 * represent special characters (non-printable stuff) and other
3580 * things. When all characters are the same, c_extra is used.
3581 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
3582 * "p_extra[n_extra]".
3583 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
3585 if (n_extra > 0)
3587 if (c_extra != NUL)
3589 c = c_extra;
3590 #ifdef FEAT_MBYTE
3591 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
3592 if (enc_utf8 && (*mb_char2len)(c) > 1)
3594 mb_utf8 = TRUE;
3595 u8cc[0] = 0;
3596 c = 0xc0;
3598 else
3599 mb_utf8 = FALSE;
3600 #endif
3602 else
3604 c = *p_extra;
3605 #ifdef FEAT_MBYTE
3606 if (has_mbyte)
3608 mb_c = c;
3609 if (enc_utf8)
3611 /* If the UTF-8 character is more than one byte:
3612 * Decode it into "mb_c". */
3613 mb_l = (*mb_ptr2len)(p_extra);
3614 mb_utf8 = FALSE;
3615 if (mb_l > n_extra)
3616 mb_l = 1;
3617 else if (mb_l > 1)
3619 mb_c = utfc_ptr2char(p_extra, u8cc);
3620 mb_utf8 = TRUE;
3621 c = 0xc0;
3624 else
3626 /* if this is a DBCS character, put it in "mb_c" */
3627 mb_l = MB_BYTE2LEN(c);
3628 if (mb_l >= n_extra)
3629 mb_l = 1;
3630 else if (mb_l > 1)
3631 mb_c = (c << 8) + p_extra[1];
3633 if (mb_l == 0) /* at the NUL at end-of-line */
3634 mb_l = 1;
3636 /* If a double-width char doesn't fit display a '>' in the
3637 * last column. */
3638 if ((
3639 # ifdef FEAT_RIGHTLEFT
3640 wp->w_p_rl ? (col <= 0) :
3641 # endif
3642 (col >= W_WIDTH(wp) - 1))
3643 && (*mb_char2cells)(mb_c) == 2)
3645 c = '>';
3646 mb_c = c;
3647 mb_l = 1;
3648 mb_utf8 = FALSE;
3649 multi_attr = hl_attr(HLF_AT);
3650 /* put the pointer back to output the double-width
3651 * character at the start of the next line. */
3652 ++n_extra;
3653 --p_extra;
3655 else
3657 n_extra -= mb_l - 1;
3658 p_extra += mb_l - 1;
3661 #endif
3662 ++p_extra;
3664 --n_extra;
3666 else
3669 * Get a character from the line itself.
3671 c = *ptr;
3672 #ifdef FEAT_MBYTE
3673 if (has_mbyte)
3675 mb_c = c;
3676 if (enc_utf8)
3678 /* If the UTF-8 character is more than one byte: Decode it
3679 * into "mb_c". */
3680 mb_l = (*mb_ptr2len)(ptr);
3681 mb_utf8 = FALSE;
3682 if (mb_l > 1)
3684 mb_c = utfc_ptr2char(ptr, u8cc);
3685 /* Overlong encoded ASCII or ASCII with composing char
3686 * is displayed normally, except a NUL. */
3687 if (mb_c < 0x80)
3688 c = mb_c;
3689 mb_utf8 = TRUE;
3691 /* At start of the line we can have a composing char.
3692 * Draw it as a space with a composing char. */
3693 if (utf_iscomposing(mb_c))
3695 int i;
3697 for (i = Screen_mco - 1; i > 0; --i)
3698 u8cc[i] = u8cc[i - 1];
3699 u8cc[0] = mb_c;
3700 mb_c = ' ';
3704 if ((mb_l == 1 && c >= 0x80)
3705 || (mb_l >= 1 && mb_c == 0)
3706 || (mb_l > 1 && (!vim_isprintc(mb_c)
3707 # ifdef UNICODE16
3708 || mb_c >= 0x10000
3709 # endif
3713 * Illegal UTF-8 byte: display as <xx>.
3714 * Non-BMP character : display as ? or fullwidth ?.
3716 # ifdef UNICODE16
3717 if (mb_c < 0x10000)
3718 # endif
3720 transchar_hex(extra, mb_c);
3721 # ifdef FEAT_RIGHTLEFT
3722 if (wp->w_p_rl) /* reverse */
3723 rl_mirror(extra);
3724 # endif
3726 # ifdef UNICODE16
3727 else if (utf_char2cells(mb_c) != 2)
3728 STRCPY(extra, "?");
3729 else
3730 /* 0xff1f in UTF-8: full-width '?' */
3731 STRCPY(extra, "\357\274\237");
3732 # endif
3734 p_extra = extra;
3735 c = *p_extra;
3736 mb_c = mb_ptr2char_adv(&p_extra);
3737 mb_utf8 = (c >= 0x80);
3738 n_extra = (int)STRLEN(p_extra);
3739 c_extra = NUL;
3740 if (area_attr == 0 && search_attr == 0)
3742 n_attr = n_extra + 1;
3743 extra_attr = hl_attr(HLF_8);
3744 saved_attr2 = char_attr; /* save current attr */
3747 else if (mb_l == 0) /* at the NUL at end-of-line */
3748 mb_l = 1;
3749 #ifdef FEAT_ARABIC
3750 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
3752 /* Do Arabic shaping. */
3753 int pc, pc1, nc;
3754 int pcc[MAX_MCO];
3756 /* The idea of what is the previous and next
3757 * character depends on 'rightleft'. */
3758 if (wp->w_p_rl)
3760 pc = prev_c;
3761 pc1 = prev_c1;
3762 nc = utf_ptr2char(ptr + mb_l);
3763 prev_c1 = u8cc[0];
3765 else
3767 pc = utfc_ptr2char(ptr + mb_l, pcc);
3768 nc = prev_c;
3769 pc1 = pcc[0];
3771 prev_c = mb_c;
3773 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
3775 else
3776 prev_c = mb_c;
3777 #endif
3779 else /* enc_dbcs */
3781 mb_l = MB_BYTE2LEN(c);
3782 if (mb_l == 0) /* at the NUL at end-of-line */
3783 mb_l = 1;
3784 else if (mb_l > 1)
3786 /* We assume a second byte below 32 is illegal.
3787 * Hopefully this is OK for all double-byte encodings!
3789 if (ptr[1] >= 32)
3790 mb_c = (c << 8) + ptr[1];
3791 else
3793 if (ptr[1] == NUL)
3795 /* head byte at end of line */
3796 mb_l = 1;
3797 transchar_nonprint(extra, c);
3799 else
3801 /* illegal tail byte */
3802 mb_l = 2;
3803 STRCPY(extra, "XX");
3805 p_extra = extra;
3806 n_extra = (int)STRLEN(extra) - 1;
3807 c_extra = NUL;
3808 c = *p_extra++;
3809 if (area_attr == 0 && search_attr == 0)
3811 n_attr = n_extra + 1;
3812 extra_attr = hl_attr(HLF_8);
3813 saved_attr2 = char_attr; /* save current attr */
3815 mb_c = c;
3819 /* If a double-width char doesn't fit display a '>' in the
3820 * last column; the character is displayed at the start of the
3821 * next line. */
3822 if ((
3823 # ifdef FEAT_RIGHTLEFT
3824 wp->w_p_rl ? (col <= 0) :
3825 # endif
3826 (col >= W_WIDTH(wp) - 1))
3827 && (*mb_char2cells)(mb_c) == 2)
3829 c = '>';
3830 mb_c = c;
3831 mb_utf8 = FALSE;
3832 mb_l = 1;
3833 multi_attr = hl_attr(HLF_AT);
3834 /* Put pointer back so that the character will be
3835 * displayed at the start of the next line. */
3836 --ptr;
3838 else if (*ptr != NUL)
3839 ptr += mb_l - 1;
3841 /* If a double-width char doesn't fit at the left side display
3842 * a '<' in the first column. */
3843 if (n_skip > 0 && mb_l > 1)
3845 n_extra = 1;
3846 c_extra = '<';
3847 c = ' ';
3848 if (area_attr == 0 && search_attr == 0)
3850 n_attr = n_extra + 1;
3851 extra_attr = hl_attr(HLF_AT);
3852 saved_attr2 = char_attr; /* save current attr */
3854 mb_c = c;
3855 mb_utf8 = FALSE;
3856 mb_l = 1;
3860 #endif
3861 ++ptr;
3863 /* 'list' : change char 160 to lcs_nbsp. */
3864 if (wp->w_p_list && (c == 160
3865 #ifdef FEAT_MBYTE
3866 || (mb_utf8 && mb_c == 160)
3867 #endif
3868 ) && lcs_nbsp)
3870 c = lcs_nbsp;
3871 if (area_attr == 0 && search_attr == 0)
3873 n_attr = 1;
3874 extra_attr = hl_attr(HLF_8);
3875 saved_attr2 = char_attr; /* save current attr */
3877 #ifdef FEAT_MBYTE
3878 mb_c = c;
3879 if (enc_utf8 && (*mb_char2len)(c) > 1)
3881 mb_utf8 = TRUE;
3882 u8cc[0] = 0;
3883 c = 0xc0;
3885 else
3886 mb_utf8 = FALSE;
3887 #endif
3890 if (extra_check)
3892 #ifdef FEAT_SPELL
3893 int can_spell = TRUE;
3894 #endif
3896 #ifdef FEAT_SYN_HL
3897 /* Get syntax attribute, unless still at the start of the line
3898 * (double-wide char that doesn't fit). */
3899 v = (long)(ptr - line);
3900 if (has_syntax && v > 0)
3902 /* Get the syntax attribute for the character. If there
3903 * is an error, disable syntax highlighting. */
3904 save_did_emsg = did_emsg;
3905 did_emsg = FALSE;
3907 syntax_attr = get_syntax_attr((colnr_T)v - 1,
3908 # ifdef FEAT_SPELL
3909 has_spell ? &can_spell :
3910 # endif
3911 NULL, FALSE);
3913 if (did_emsg)
3915 wp->w_buffer->b_syn_error = TRUE;
3916 has_syntax = FALSE;
3918 else
3919 did_emsg = save_did_emsg;
3921 /* Need to get the line again, a multi-line regexp may
3922 * have made it invalid. */
3923 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3924 ptr = line + v;
3926 if (!attr_pri)
3927 char_attr = syntax_attr;
3928 else
3929 char_attr = hl_combine_attr(syntax_attr, char_attr);
3931 #endif
3933 #ifdef FEAT_SPELL
3934 /* Check spelling (unless at the end of the line).
3935 * Only do this when there is no syntax highlighting, the
3936 * @Spell cluster is not used or the current syntax item
3937 * contains the @Spell cluster. */
3938 if (has_spell && v >= word_end && v > cur_checked_col)
3940 spell_attr = 0;
3941 # ifdef FEAT_SYN_HL
3942 if (!attr_pri)
3943 char_attr = syntax_attr;
3944 # endif
3945 if (c != 0 && (
3946 # ifdef FEAT_SYN_HL
3947 !has_syntax ||
3948 # endif
3949 can_spell))
3951 char_u *prev_ptr, *p;
3952 int len;
3953 hlf_T spell_hlf = HLF_COUNT;
3954 # ifdef FEAT_MBYTE
3955 if (has_mbyte)
3957 prev_ptr = ptr - mb_l;
3958 v -= mb_l - 1;
3960 else
3961 # endif
3962 prev_ptr = ptr - 1;
3964 /* Use nextline[] if possible, it has the start of the
3965 * next line concatenated. */
3966 if ((prev_ptr - line) - nextlinecol >= 0)
3967 p = nextline + (prev_ptr - line) - nextlinecol;
3968 else
3969 p = prev_ptr;
3970 cap_col -= (int)(prev_ptr - line);
3971 len = spell_check(wp, p, &spell_hlf, &cap_col,
3972 nochange);
3973 word_end = v + len;
3975 /* In Insert mode only highlight a word that
3976 * doesn't touch the cursor. */
3977 if (spell_hlf != HLF_COUNT
3978 && (State & INSERT) != 0
3979 && wp->w_cursor.lnum == lnum
3980 && wp->w_cursor.col >=
3981 (colnr_T)(prev_ptr - line)
3982 && wp->w_cursor.col < (colnr_T)word_end)
3984 spell_hlf = HLF_COUNT;
3985 spell_redraw_lnum = lnum;
3988 if (spell_hlf == HLF_COUNT && p != prev_ptr
3989 && (p - nextline) + len > nextline_idx)
3991 /* Remember that the good word continues at the
3992 * start of the next line. */
3993 checked_lnum = lnum + 1;
3994 checked_col = (int)((p - nextline) + len - nextline_idx);
3997 /* Turn index into actual attributes. */
3998 if (spell_hlf != HLF_COUNT)
3999 spell_attr = highlight_attr[spell_hlf];
4001 if (cap_col > 0)
4003 if (p != prev_ptr
4004 && (p - nextline) + cap_col >= nextline_idx)
4006 /* Remember that the word in the next line
4007 * must start with a capital. */
4008 capcol_lnum = lnum + 1;
4009 cap_col = (int)((p - nextline) + cap_col
4010 - nextline_idx);
4012 else
4013 /* Compute the actual column. */
4014 cap_col += (int)(prev_ptr - line);
4018 if (spell_attr != 0)
4020 if (!attr_pri)
4021 char_attr = hl_combine_attr(char_attr, spell_attr);
4022 else
4023 char_attr = hl_combine_attr(spell_attr, char_attr);
4025 #endif
4026 #ifdef FEAT_LINEBREAK
4028 * Found last space before word: check for line break.
4030 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr)
4031 && !wp->w_p_list)
4033 n_extra = win_lbr_chartabsize(wp, ptr - (
4034 # ifdef FEAT_MBYTE
4035 has_mbyte ? mb_l :
4036 # endif
4037 1), (colnr_T)vcol, NULL) - 1;
4038 c_extra = ' ';
4039 if (vim_iswhite(c))
4040 c = ' ';
4042 #endif
4044 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4046 c = lcs_trail;
4047 if (!attr_pri)
4049 n_attr = 1;
4050 extra_attr = hl_attr(HLF_8);
4051 saved_attr2 = char_attr; /* save current attr */
4053 #ifdef FEAT_MBYTE
4054 mb_c = c;
4055 if (enc_utf8 && (*mb_char2len)(c) > 1)
4057 mb_utf8 = TRUE;
4058 u8cc[0] = 0;
4059 c = 0xc0;
4061 else
4062 mb_utf8 = FALSE;
4063 #endif
4068 * Handling of non-printable characters.
4070 if (!(chartab[c & 0xff] & CT_PRINT_CHAR))
4073 * when getting a character from the file, we may have to
4074 * turn it into something else on the way to putting it
4075 * into "ScreenLines".
4077 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4079 /* tab amount depends on current column */
4080 n_extra = (int)wp->w_buffer->b_p_ts
4081 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4082 #ifdef FEAT_MBYTE
4083 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4084 #endif
4085 if (wp->w_p_list)
4087 c = lcs_tab1;
4088 c_extra = lcs_tab2;
4089 n_attr = n_extra + 1;
4090 extra_attr = hl_attr(HLF_8);
4091 saved_attr2 = char_attr; /* save current attr */
4092 #ifdef FEAT_MBYTE
4093 mb_c = c;
4094 if (enc_utf8 && (*mb_char2len)(c) > 1)
4096 mb_utf8 = TRUE;
4097 u8cc[0] = 0;
4098 c = 0xc0;
4100 #endif
4102 else
4104 c_extra = ' ';
4105 c = ' ';
4108 else if (c == NUL
4109 && ((wp->w_p_list && lcs_eol > 0)
4110 || ((fromcol >= 0 || fromcol_prev >= 0)
4111 && tocol > vcol
4112 #ifdef FEAT_VISUAL
4113 && VIsual_mode != Ctrl_V
4114 #endif
4115 && (
4116 # ifdef FEAT_RIGHTLEFT
4117 wp->w_p_rl ? (col >= 0) :
4118 # endif
4119 (col < W_WIDTH(wp)))
4120 && !(noinvcur
4121 && (colnr_T)vcol == wp->w_virtcol)))
4122 && lcs_eol_one >= 0)
4124 /* Display a '$' after the line or highlight an extra
4125 * character if the line break is included. */
4126 #if defined(FEAT_DIFF) || defined(LINE_ATTR)
4127 /* For a diff line the highlighting continues after the
4128 * "$". */
4129 if (
4130 # ifdef FEAT_DIFF
4131 diff_hlf == (hlf_T)0
4132 # ifdef LINE_ATTR
4134 # endif
4135 # endif
4136 # ifdef LINE_ATTR
4137 line_attr == 0
4138 # endif
4140 #endif
4142 #ifdef FEAT_VIRTUALEDIT
4143 /* In virtualedit, visual selections may extend
4144 * beyond end of line. */
4145 if (area_highlighting && virtual_active()
4146 && tocol != MAXCOL && vcol < tocol)
4147 n_extra = 0;
4148 else
4149 #endif
4151 p_extra = at_end_str;
4152 n_extra = 1;
4153 c_extra = NUL;
4156 if (wp->w_p_list)
4157 c = lcs_eol;
4158 else
4159 c = ' ';
4160 lcs_eol_one = -1;
4161 --ptr; /* put it back at the NUL */
4162 if (!attr_pri)
4164 extra_attr = hl_attr(HLF_AT);
4165 n_attr = 1;
4167 #ifdef FEAT_MBYTE
4168 mb_c = c;
4169 if (enc_utf8 && (*mb_char2len)(c) > 1)
4171 mb_utf8 = TRUE;
4172 u8cc[0] = 0;
4173 c = 0xc0;
4175 else
4176 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4177 #endif
4179 else if (c != NUL)
4181 p_extra = transchar(c);
4182 #ifdef FEAT_RIGHTLEFT
4183 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4184 rl_mirror(p_extra); /* reverse "<12>" */
4185 #endif
4186 n_extra = byte2cells(c) - 1;
4187 c_extra = NUL;
4188 c = *p_extra++;
4189 if (!attr_pri)
4191 n_attr = n_extra + 1;
4192 extra_attr = hl_attr(HLF_8);
4193 saved_attr2 = char_attr; /* save current attr */
4195 #ifdef FEAT_MBYTE
4196 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4197 #endif
4199 #ifdef FEAT_VIRTUALEDIT
4200 else if (VIsual_active
4201 && (VIsual_mode == Ctrl_V
4202 || VIsual_mode == 'v')
4203 && virtual_active()
4204 && tocol != MAXCOL
4205 && vcol < tocol
4206 && (
4207 # ifdef FEAT_RIGHTLEFT
4208 wp->w_p_rl ? (col >= 0) :
4209 # endif
4210 (col < W_WIDTH(wp))))
4212 c = ' ';
4213 --ptr; /* put it back at the NUL */
4215 #endif
4216 #if defined(LINE_ATTR)
4217 else if ((
4218 # ifdef FEAT_DIFF
4219 diff_hlf != (hlf_T)0 ||
4220 # endif
4221 line_attr != 0
4222 ) && (
4223 # ifdef FEAT_RIGHTLEFT
4224 wp->w_p_rl ? (col >= 0) :
4225 # endif
4226 (col < W_WIDTH(wp))))
4228 /* Highlight until the right side of the window */
4229 c = ' ';
4230 --ptr; /* put it back at the NUL */
4232 /* Remember we do the char for line highlighting. */
4233 ++did_line_attr;
4235 /* don't do search HL for the rest of the line */
4236 if (line_attr != 0 && char_attr == search_attr && col > 0)
4237 char_attr = line_attr;
4238 # ifdef FEAT_DIFF
4239 if (diff_hlf == HLF_TXD)
4241 diff_hlf = HLF_CHD;
4242 if (attr == 0 || char_attr != attr)
4243 char_attr = hl_attr(diff_hlf);
4245 # endif
4247 #endif
4251 /* Don't override visual selection highlighting. */
4252 if (n_attr > 0
4253 && draw_state == WL_LINE
4254 && !attr_pri)
4255 char_attr = extra_attr;
4257 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
4258 /* XIM don't send preedit_start and preedit_end, but they send
4259 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
4260 * im_is_preediting() here. */
4261 if (xic != NULL
4262 && lnum == curwin->w_cursor.lnum
4263 && (State & INSERT)
4264 && !p_imdisable
4265 && im_is_preediting()
4266 && draw_state == WL_LINE)
4268 colnr_T tcol;
4270 if (preedit_end_col == MAXCOL)
4271 getvcol(curwin, &(curwin->w_cursor), &tcol, NULL, NULL);
4272 else
4273 tcol = preedit_end_col;
4274 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
4276 if (feedback_old_attr < 0)
4278 feedback_col = 0;
4279 feedback_old_attr = char_attr;
4281 char_attr = im_get_feedback_attr(feedback_col);
4282 if (char_attr < 0)
4283 char_attr = feedback_old_attr;
4284 feedback_col++;
4286 else if (feedback_old_attr >= 0)
4288 char_attr = feedback_old_attr;
4289 feedback_old_attr = -1;
4290 feedback_col = 0;
4293 #endif
4295 * Handle the case where we are in column 0 but not on the first
4296 * character of the line and the user wants us to show us a
4297 * special character (via 'listchars' option "precedes:<char>".
4299 if (lcs_prec_todo != NUL
4300 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
4301 #ifdef FEAT_DIFF
4302 && filler_todo <= 0
4303 #endif
4304 && draw_state > WL_NR
4305 && c != NUL)
4307 c = lcs_prec;
4308 lcs_prec_todo = NUL;
4309 #ifdef FEAT_MBYTE
4310 mb_c = c;
4311 if (enc_utf8 && (*mb_char2len)(c) > 1)
4313 mb_utf8 = TRUE;
4314 u8cc[0] = 0;
4315 c = 0xc0;
4317 else
4318 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4319 #endif
4320 if (!attr_pri)
4322 saved_attr3 = char_attr; /* save current attr */
4323 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
4324 n_attr3 = 1;
4329 * At end of the text line or just after the last character.
4331 if (c == NUL
4332 #if defined(LINE_ATTR)
4333 || did_line_attr == 1
4334 #endif
4337 #ifdef FEAT_SEARCH_EXTRA
4338 long prevcol = (long)(ptr - line) - (c == NUL);
4340 /* we're not really at that column when skipping some text */
4341 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
4342 ++prevcol;
4343 #endif
4345 /* invert at least one char, used for Visual and empty line or
4346 * highlight match at end of line. If it's beyond the last
4347 * char on the screen, just overwrite that one (tricky!) Not
4348 * needed when a '$' was displayed for 'list'. */
4349 #ifdef FEAT_SEARCH_EXTRA
4350 prevcol_hl_flag = FALSE;
4351 if (prevcol == (long)search_hl.startcol)
4352 prevcol_hl_flag = TRUE;
4353 else
4355 cur = wp->w_match_head;
4356 while (cur != NULL)
4358 if (prevcol == (long)cur->hl.startcol)
4360 prevcol_hl_flag = TRUE;
4361 break;
4363 cur = cur->next;
4366 #endif
4367 if (lcs_eol == lcs_eol_one
4368 && ((area_attr != 0 && vcol == fromcol && c == NUL)
4369 #ifdef FEAT_SEARCH_EXTRA
4370 /* highlight 'hlsearch' match at end of line */
4371 || (prevcol_hl_flag == TRUE
4372 # if defined(LINE_ATTR)
4373 && did_line_attr <= 1
4374 # endif
4376 #endif
4379 int n = 0;
4381 #ifdef FEAT_RIGHTLEFT
4382 if (wp->w_p_rl)
4384 if (col < 0)
4385 n = 1;
4387 else
4388 #endif
4390 if (col >= W_WIDTH(wp))
4391 n = -1;
4393 if (n != 0)
4395 /* At the window boundary, highlight the last character
4396 * instead (better than nothing). */
4397 off += n;
4398 col += n;
4400 else
4402 /* Add a blank character to highlight. */
4403 ScreenLines[off] = ' ';
4404 #ifdef FEAT_MBYTE
4405 if (enc_utf8)
4406 ScreenLinesUC[off] = 0;
4407 #endif
4409 #ifdef FEAT_SEARCH_EXTRA
4410 if (area_attr == 0)
4412 /* Use attributes from match with highest priority among
4413 * 'search_hl' and the match list. */
4414 char_attr = search_hl.attr;
4415 cur = wp->w_match_head;
4416 shl_flag = FALSE;
4417 while (cur != NULL || shl_flag == FALSE)
4419 if (shl_flag == FALSE
4420 && ((cur != NULL
4421 && cur->priority > SEARCH_HL_PRIORITY)
4422 || cur == NULL))
4424 shl = &search_hl;
4425 shl_flag = TRUE;
4427 else
4428 shl = &cur->hl;
4429 if ((ptr - line) - 1 == (long)shl->startcol)
4430 char_attr = shl->attr;
4431 if (shl != &search_hl && cur != NULL)
4432 cur = cur->next;
4435 #endif
4436 ScreenAttrs[off] = char_attr;
4437 #ifdef FEAT_RIGHTLEFT
4438 if (wp->w_p_rl)
4440 --col;
4441 --off;
4443 else
4444 #endif
4446 ++col;
4447 ++off;
4449 ++vcol;
4450 #ifdef FEAT_SYN_HL
4451 eol_hl_off = 1;
4452 #endif
4457 * At end of the text line.
4459 if (c == NUL)
4461 #ifdef FEAT_SYN_HL
4462 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol)
4464 /* highlight last char after line */
4465 --col;
4466 --off;
4467 --vcol;
4470 /* Highlight 'cursorcolumn' past end of the line. */
4471 if (wp->w_p_wrap)
4472 v = wp->w_skipcol;
4473 else
4474 v = wp->w_leftcol;
4475 /* check if line ends before left margin */
4476 if (vcol < v + col - win_col_off(wp))
4478 vcol = v + col - win_col_off(wp);
4479 if (wp->w_p_cuc
4480 && (int)wp->w_virtcol >= vcol - eol_hl_off
4481 && (int)wp->w_virtcol < W_WIDTH(wp) * (row - startrow + 1)
4483 && lnum != wp->w_cursor.lnum
4484 # ifdef FEAT_RIGHTLEFT
4485 && !wp->w_p_rl
4486 # endif
4489 while (col < W_WIDTH(wp))
4491 ScreenLines[off] = ' ';
4492 #ifdef FEAT_MBYTE
4493 if (enc_utf8)
4494 ScreenLinesUC[off] = 0;
4495 #endif
4496 ++col;
4497 if (vcol == (long)wp->w_virtcol)
4499 ScreenAttrs[off] = hl_attr(HLF_CUC);
4500 break;
4502 ScreenAttrs[off++] = 0;
4503 ++vcol;
4506 #endif
4508 SCREEN_LINE(screen_row, W_WINCOL(wp), col, (int)W_WIDTH(wp),
4509 wp->w_p_rl);
4510 row++;
4513 * Update w_cline_height and w_cline_folded if the cursor line was
4514 * updated (saves a call to plines() later).
4516 if (wp == curwin && lnum == curwin->w_cursor.lnum)
4518 curwin->w_cline_row = startrow;
4519 curwin->w_cline_height = row - startrow;
4520 #ifdef FEAT_FOLDING
4521 curwin->w_cline_folded = FALSE;
4522 #endif
4523 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
4526 break;
4529 /* line continues beyond line end */
4530 if (lcs_ext
4531 && !wp->w_p_wrap
4532 #ifdef FEAT_DIFF
4533 && filler_todo <= 0
4534 #endif
4535 && (
4536 #ifdef FEAT_RIGHTLEFT
4537 wp->w_p_rl ? col == 0 :
4538 #endif
4539 col == W_WIDTH(wp) - 1)
4540 && (*ptr != NUL
4541 || (wp->w_p_list && lcs_eol_one > 0)
4542 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
4544 c = lcs_ext;
4545 char_attr = hl_attr(HLF_AT);
4546 #ifdef FEAT_MBYTE
4547 mb_c = c;
4548 if (enc_utf8 && (*mb_char2len)(c) > 1)
4550 mb_utf8 = TRUE;
4551 u8cc[0] = 0;
4552 c = 0xc0;
4554 else
4555 mb_utf8 = FALSE;
4556 #endif
4559 #ifdef FEAT_SYN_HL
4560 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
4561 * highlight the cursor position itself. */
4562 if (wp->w_p_cuc && vcol == (long)wp->w_virtcol
4563 && lnum != wp->w_cursor.lnum
4564 && draw_state == WL_LINE
4565 && !lnum_in_visual_area)
4567 vcol_save_attr = char_attr;
4568 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
4570 else
4571 vcol_save_attr = -1;
4572 #endif
4575 * Store character to be displayed.
4576 * Skip characters that are left of the screen for 'nowrap'.
4578 vcol_prev = vcol;
4579 if (draw_state < WL_LINE || n_skip <= 0)
4582 * Store the character.
4584 #if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
4585 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
4587 /* A double-wide character is: put first halve in left cell. */
4588 --off;
4589 --col;
4591 #endif
4592 ScreenLines[off] = c;
4593 #ifdef FEAT_MBYTE
4594 if (enc_dbcs == DBCS_JPNU)
4595 ScreenLines2[off] = mb_c & 0xff;
4596 else if (enc_utf8)
4598 if (mb_utf8)
4600 int i;
4602 ScreenLinesUC[off] = mb_c;
4603 if ((c & 0xff) == 0)
4604 ScreenLines[off] = 0x80; /* avoid storing zero */
4605 for (i = 0; i < Screen_mco; ++i)
4607 ScreenLinesC[i][off] = u8cc[i];
4608 if (u8cc[i] == 0)
4609 break;
4612 else
4613 ScreenLinesUC[off] = 0;
4615 if (multi_attr)
4617 ScreenAttrs[off] = multi_attr;
4618 multi_attr = 0;
4620 else
4621 #endif
4622 ScreenAttrs[off] = char_attr;
4624 #ifdef FEAT_MBYTE
4625 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
4627 /* Need to fill two screen columns. */
4628 ++off;
4629 ++col;
4630 if (enc_utf8)
4631 /* UTF-8: Put a 0 in the second screen char. */
4632 ScreenLines[off] = 0;
4633 else
4634 /* DBCS: Put second byte in the second screen char. */
4635 ScreenLines[off] = mb_c & 0xff;
4636 ++vcol;
4637 /* When "tocol" is halfway a character, set it to the end of
4638 * the character, otherwise highlighting won't stop. */
4639 if (tocol == vcol)
4640 ++tocol;
4641 #ifdef FEAT_RIGHTLEFT
4642 if (wp->w_p_rl)
4644 /* now it's time to backup one cell */
4645 --off;
4646 --col;
4648 #endif
4650 #endif
4651 #ifdef FEAT_RIGHTLEFT
4652 if (wp->w_p_rl)
4654 --off;
4655 --col;
4657 else
4658 #endif
4660 ++off;
4661 ++col;
4664 else
4665 --n_skip;
4667 /* Only advance the "vcol" when after the 'number' column. */
4668 if (draw_state > WL_NR
4669 #ifdef FEAT_DIFF
4670 && filler_todo <= 0
4671 #endif
4673 ++vcol;
4675 #ifdef FEAT_SYN_HL
4676 if (vcol_save_attr >= 0)
4677 char_attr = vcol_save_attr;
4678 #endif
4680 /* restore attributes after "predeces" in 'listchars' */
4681 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
4682 char_attr = saved_attr3;
4684 /* restore attributes after last 'listchars' or 'number' char */
4685 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
4686 char_attr = saved_attr2;
4689 * At end of screen line and there is more to come: Display the line
4690 * so far. If there is no more to display it is caught above.
4692 if ((
4693 #ifdef FEAT_RIGHTLEFT
4694 wp->w_p_rl ? (col < 0) :
4695 #endif
4696 (col >= W_WIDTH(wp)))
4697 && (*ptr != NUL
4698 #ifdef FEAT_DIFF
4699 || filler_todo > 0
4700 #endif
4701 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
4702 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
4705 SCREEN_LINE(screen_row, W_WINCOL(wp), col, (int)W_WIDTH(wp),
4706 wp->w_p_rl);
4707 ++row;
4708 ++screen_row;
4710 /* When not wrapping and finished diff lines, or when displayed
4711 * '$' and highlighting until last column, break here. */
4712 if ((!wp->w_p_wrap
4713 #ifdef FEAT_DIFF
4714 && filler_todo <= 0
4715 #endif
4716 ) || lcs_eol_one == -1)
4717 break;
4719 /* When the window is too narrow draw all "@" lines. */
4720 if (draw_state != WL_LINE
4721 #ifdef FEAT_DIFF
4722 && filler_todo <= 0
4723 #endif
4726 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
4727 #ifdef FEAT_VERTSPLIT
4728 draw_vsep_win(wp, row);
4729 #endif
4730 row = endrow;
4733 /* When line got too long for screen break here. */
4734 if (row == endrow)
4736 ++row;
4737 break;
4740 if (screen_cur_row == screen_row - 1
4741 #ifdef FEAT_DIFF
4742 && filler_todo <= 0
4743 #endif
4744 && W_WIDTH(wp) == Columns)
4746 /* Remember that the line wraps, used for modeless copy. */
4747 LineWraps[screen_row - 1] = TRUE;
4750 * Special trick to make copy/paste of wrapped lines work with
4751 * xterm/screen: write an extra character beyond the end of
4752 * the line. This will work with all terminal types
4753 * (regardless of the xn,am settings).
4754 * Only do this on a fast tty.
4755 * Only do this if the cursor is on the current line
4756 * (something has been written in it).
4757 * Don't do this for the GUI.
4758 * Don't do this for double-width characters.
4759 * Don't do this for a window not at the right screen border.
4761 if (p_tf
4762 #ifdef FEAT_GUI
4763 && !gui.in_use
4764 #endif
4765 #ifdef FEAT_MBYTE
4766 && !(has_mbyte
4767 && ((*mb_off2cells)(LineOffset[screen_row],
4768 LineOffset[screen_row] + screen_Columns)
4769 == 2
4770 || (*mb_off2cells)(LineOffset[screen_row - 1]
4771 + (int)Columns - 2,
4772 LineOffset[screen_row] + screen_Columns)
4773 == 2))
4774 #endif
4777 /* First make sure we are at the end of the screen line,
4778 * then output the same character again to let the
4779 * terminal know about the wrap. If the terminal doesn't
4780 * auto-wrap, we overwrite the character. */
4781 if (screen_cur_col != W_WIDTH(wp))
4782 screen_char(LineOffset[screen_row - 1]
4783 + (unsigned)Columns - 1,
4784 screen_row - 1, (int)(Columns - 1));
4786 #ifdef FEAT_MBYTE
4787 /* When there is a multi-byte character, just output a
4788 * space to keep it simple. */
4789 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
4790 screen_row - 1] + (Columns - 1)]) > 1)
4791 out_char(' ');
4792 else
4793 #endif
4794 out_char(ScreenLines[LineOffset[screen_row - 1]
4795 + (Columns - 1)]);
4796 /* force a redraw of the first char on the next line */
4797 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
4798 screen_start(); /* don't know where cursor is now */
4802 col = 0;
4803 off = (unsigned)(current_ScreenLine - ScreenLines);
4804 #ifdef FEAT_RIGHTLEFT
4805 if (wp->w_p_rl)
4807 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
4808 off += col;
4810 #endif
4812 /* reset the drawing state for the start of a wrapped line */
4813 draw_state = WL_START;
4814 saved_n_extra = n_extra;
4815 saved_p_extra = p_extra;
4816 saved_c_extra = c_extra;
4817 saved_char_attr = char_attr;
4818 n_extra = 0;
4819 lcs_prec_todo = lcs_prec;
4820 #ifdef FEAT_LINEBREAK
4821 # ifdef FEAT_DIFF
4822 if (filler_todo <= 0)
4823 # endif
4824 need_showbreak = TRUE;
4825 #endif
4826 #ifdef FEAT_DIFF
4827 --filler_todo;
4828 /* When the filler lines are actually below the last line of the
4829 * file, don't draw the line itself, break here. */
4830 if (filler_todo == 0 && wp->w_botfill)
4831 break;
4832 #endif
4835 } /* for every character in the line */
4837 #ifdef FEAT_SPELL
4838 /* After an empty line check first word for capital. */
4839 if (*skipwhite(line) == NUL)
4841 capcol_lnum = lnum + 1;
4842 cap_col = 0;
4844 #endif
4846 return row;
4849 #ifdef FEAT_MBYTE
4850 static int comp_char_differs __ARGS((int, int));
4853 * Return if the composing characters at "off_from" and "off_to" differ.
4855 static int
4856 comp_char_differs(off_from, off_to)
4857 int off_from;
4858 int off_to;
4860 int i;
4862 for (i = 0; i < Screen_mco; ++i)
4864 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
4865 return TRUE;
4866 if (ScreenLinesC[i][off_from] == 0)
4867 break;
4869 return FALSE;
4871 #endif
4874 * Check whether the given character needs redrawing:
4875 * - the (first byte of the) character is different
4876 * - the attributes are different
4877 * - the character is multi-byte and the next byte is different
4878 * - the character is two cells wide and the second cell differs.
4880 static int
4881 char_needs_redraw(off_from, off_to, cols)
4882 int off_from;
4883 int off_to;
4884 int cols;
4886 if (cols > 0
4887 && ((ScreenLines[off_from] != ScreenLines[off_to]
4888 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
4890 #ifdef FEAT_MBYTE
4891 || (enc_dbcs != 0
4892 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
4893 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
4894 ? ScreenLines2[off_from] != ScreenLines2[off_to]
4895 : (cols > 1 && ScreenLines[off_from + 1]
4896 != ScreenLines[off_to + 1])))
4897 || (enc_utf8
4898 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
4899 || (ScreenLinesUC[off_from] != 0
4900 && comp_char_differs(off_from, off_to))
4901 || (cols > 1 && ScreenLines[off_from + 1]
4902 != ScreenLines[off_to + 1])))
4903 #endif
4905 return TRUE;
4906 return FALSE;
4910 * Move one "cooked" screen line to the screen, but only the characters that
4911 * have actually changed. Handle insert/delete character.
4912 * "coloff" gives the first column on the screen for this line.
4913 * "endcol" gives the columns where valid characters are.
4914 * "clear_width" is the width of the window. It's > 0 if the rest of the line
4915 * needs to be cleared, negative otherwise.
4916 * "rlflag" is TRUE in a rightleft window:
4917 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
4918 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
4920 static void
4921 screen_line(row, coloff, endcol, clear_width
4922 #ifdef FEAT_RIGHTLEFT
4923 , rlflag
4924 #endif
4926 int row;
4927 int coloff;
4928 int endcol;
4929 int clear_width;
4930 #ifdef FEAT_RIGHTLEFT
4931 int rlflag;
4932 #endif
4934 unsigned off_from;
4935 unsigned off_to;
4936 #ifdef FEAT_MBYTE
4937 unsigned max_off_from;
4938 unsigned max_off_to;
4939 #endif
4940 int col = 0;
4941 #if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_VERTSPLIT)
4942 int hl;
4943 #endif
4944 int force = FALSE; /* force update rest of the line */
4945 int redraw_this /* bool: does character need redraw? */
4946 #ifdef FEAT_GUI
4947 = TRUE /* For GUI when while-loop empty */
4948 #endif
4950 int redraw_next; /* redraw_this for next character */
4951 #ifdef FEAT_MBYTE
4952 int clear_next = FALSE;
4953 int char_cells; /* 1: normal char */
4954 /* 2: occupies two display cells */
4955 # define CHAR_CELLS char_cells
4956 #else
4957 # define CHAR_CELLS 1
4958 #endif
4960 # ifdef FEAT_CLIPBOARD
4961 clip_may_clear_selection(row, row);
4962 # endif
4964 off_from = (unsigned)(current_ScreenLine - ScreenLines);
4965 off_to = LineOffset[row] + coloff;
4966 #ifdef FEAT_MBYTE
4967 max_off_from = off_from + screen_Columns;
4968 max_off_to = LineOffset[row] + screen_Columns;
4969 #endif
4971 #ifdef FEAT_RIGHTLEFT
4972 if (rlflag)
4974 /* Clear rest first, because it's left of the text. */
4975 if (clear_width > 0)
4977 while (col <= endcol && ScreenLines[off_to] == ' '
4978 && ScreenAttrs[off_to] == 0
4979 # ifdef FEAT_MBYTE
4980 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
4981 # endif
4984 ++off_to;
4985 ++col;
4987 if (col <= endcol)
4988 screen_fill(row, row + 1, col + coloff,
4989 endcol + coloff + 1, ' ', ' ', 0);
4991 col = endcol + 1;
4992 off_to = LineOffset[row] + col + coloff;
4993 off_from += col;
4994 endcol = (clear_width > 0 ? clear_width : -clear_width);
4996 #endif /* FEAT_RIGHTLEFT */
4998 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
5000 while (col < endcol)
5002 #ifdef FEAT_MBYTE
5003 if (has_mbyte && (col + 1 < endcol))
5004 char_cells = (*mb_off2cells)(off_from, max_off_from);
5005 else
5006 char_cells = 1;
5007 #endif
5009 redraw_this = redraw_next;
5010 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
5011 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
5013 #ifdef FEAT_GUI
5014 /* If the next character was bold, then redraw the current character to
5015 * remove any pixels that might have spilt over into us. This only
5016 * happens in the GUI.
5018 if (redraw_next && gui.in_use)
5020 hl = ScreenAttrs[off_to + CHAR_CELLS];
5021 if (hl > HL_ALL)
5022 hl = syn_attr2attr(hl);
5023 if (hl & HL_BOLD)
5024 redraw_this = TRUE;
5026 #endif
5028 if (redraw_this)
5031 * Special handling when 'xs' termcap flag set (hpterm):
5032 * Attributes for characters are stored at the position where the
5033 * cursor is when writing the highlighting code. The
5034 * start-highlighting code must be written with the cursor on the
5035 * first highlighted character. The stop-highlighting code must
5036 * be written with the cursor just after the last highlighted
5037 * character.
5038 * Overwriting a character doesn't remove it's highlighting. Need
5039 * to clear the rest of the line, and force redrawing it
5040 * completely.
5042 if ( p_wiv
5043 && !force
5044 #ifdef FEAT_GUI
5045 && !gui.in_use
5046 #endif
5047 && ScreenAttrs[off_to] != 0
5048 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
5051 * Need to remove highlighting attributes here.
5053 windgoto(row, col + coloff);
5054 out_str(T_CE); /* clear rest of this screen line */
5055 screen_start(); /* don't know where cursor is now */
5056 force = TRUE; /* force redraw of rest of the line */
5057 redraw_next = TRUE; /* or else next char would miss out */
5060 * If the previous character was highlighted, need to stop
5061 * highlighting at this character.
5063 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
5065 screen_attr = ScreenAttrs[off_to - 1];
5066 term_windgoto(row, col + coloff);
5067 screen_stop_highlight();
5069 else
5070 screen_attr = 0; /* highlighting has stopped */
5072 #ifdef FEAT_MBYTE
5073 if (enc_dbcs != 0)
5075 /* Check if overwriting a double-byte with a single-byte or
5076 * the other way around requires another character to be
5077 * redrawn. For UTF-8 this isn't needed, because comparing
5078 * ScreenLinesUC[] is sufficient. */
5079 if (char_cells == 1
5080 && col + 1 < endcol
5081 && (*mb_off2cells)(off_to, max_off_to) > 1)
5083 /* Writing a single-cell character over a double-cell
5084 * character: need to redraw the next cell. */
5085 ScreenLines[off_to + 1] = 0;
5086 redraw_next = TRUE;
5088 else if (char_cells == 2
5089 && col + 2 < endcol
5090 && (*mb_off2cells)(off_to, max_off_to) == 1
5091 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
5093 /* Writing the second half of a double-cell character over
5094 * a double-cell character: need to redraw the second
5095 * cell. */
5096 ScreenLines[off_to + 2] = 0;
5097 redraw_next = TRUE;
5100 if (enc_dbcs == DBCS_JPNU)
5101 ScreenLines2[off_to] = ScreenLines2[off_from];
5103 /* When writing a single-width character over a double-width
5104 * character and at the end of the redrawn text, need to clear out
5105 * the right halve of the old character.
5106 * Also required when writing the right halve of a double-width
5107 * char over the left halve of an existing one. */
5108 if (has_mbyte && col + char_cells == endcol
5109 && ((char_cells == 1
5110 && (*mb_off2cells)(off_to, max_off_to) > 1)
5111 || (char_cells == 2
5112 && (*mb_off2cells)(off_to, max_off_to) == 1
5113 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
5114 clear_next = TRUE;
5115 #endif
5117 ScreenLines[off_to] = ScreenLines[off_from];
5118 #ifdef FEAT_MBYTE
5119 if (enc_utf8)
5121 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
5122 if (ScreenLinesUC[off_from] != 0)
5124 int i;
5126 for (i = 0; i < Screen_mco; ++i)
5127 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
5130 if (char_cells == 2)
5131 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
5132 #endif
5134 #if defined(FEAT_GUI) || defined(UNIX)
5135 /* The bold trick makes a single column of pixels appear in the
5136 * next character. When a bold character is removed, the next
5137 * character should be redrawn too. This happens for our own GUI
5138 * and for some xterms. */
5139 if (
5140 # ifdef FEAT_GUI
5141 gui.in_use
5142 # endif
5143 # if defined(FEAT_GUI) && defined(UNIX)
5145 # endif
5146 # ifdef UNIX
5147 term_is_xterm
5148 # endif
5151 hl = ScreenAttrs[off_to];
5152 if (hl > HL_ALL)
5153 hl = syn_attr2attr(hl);
5154 if (hl & HL_BOLD)
5155 redraw_next = TRUE;
5157 #endif
5158 ScreenAttrs[off_to] = ScreenAttrs[off_from];
5159 #ifdef FEAT_MBYTE
5160 /* For simplicity set the attributes of second half of a
5161 * double-wide character equal to the first half. */
5162 if (char_cells == 2)
5163 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
5165 if (enc_dbcs != 0 && char_cells == 2)
5166 screen_char_2(off_to, row, col + coloff);
5167 else
5168 #endif
5169 screen_char(off_to, row, col + coloff);
5171 else if ( p_wiv
5172 #ifdef FEAT_GUI
5173 && !gui.in_use
5174 #endif
5175 && col + coloff > 0)
5177 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
5180 * Don't output stop-highlight when moving the cursor, it will
5181 * stop the highlighting when it should continue.
5183 screen_attr = 0;
5185 else if (screen_attr != 0)
5186 screen_stop_highlight();
5189 off_to += CHAR_CELLS;
5190 off_from += CHAR_CELLS;
5191 col += CHAR_CELLS;
5194 #ifdef FEAT_MBYTE
5195 if (clear_next)
5197 /* Clear the second half of a double-wide character of which the left
5198 * half was overwritten with a single-wide character. */
5199 ScreenLines[off_to] = ' ';
5200 if (enc_utf8)
5201 ScreenLinesUC[off_to] = 0;
5202 screen_char(off_to, row, col + coloff);
5204 #endif
5206 if (clear_width > 0
5207 #ifdef FEAT_RIGHTLEFT
5208 && !rlflag
5209 #endif
5212 #ifdef FEAT_GUI
5213 int startCol = col;
5214 #endif
5216 /* blank out the rest of the line */
5217 while (col < clear_width && ScreenLines[off_to] == ' '
5218 && ScreenAttrs[off_to] == 0
5219 #ifdef FEAT_MBYTE
5220 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5221 #endif
5224 ++off_to;
5225 ++col;
5227 if (col < clear_width)
5229 #ifdef FEAT_GUI
5231 * In the GUI, clearing the rest of the line may leave pixels
5232 * behind if the first character cleared was bold. Some bold
5233 * fonts spill over the left. In this case we redraw the previous
5234 * character too. If we didn't skip any blanks above, then we
5235 * only redraw if the character wasn't already redrawn anyway.
5237 if (gui.in_use && (col > startCol || !redraw_this))
5239 hl = ScreenAttrs[off_to];
5240 if (hl > HL_ALL || (hl & HL_BOLD))
5242 int prev_cells = 1;
5243 # ifdef FEAT_MBYTE
5244 if (enc_utf8)
5245 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
5246 * that its width is 2. */
5247 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
5248 else if (enc_dbcs != 0)
5250 /* find previous character by counting from first
5251 * column and get its width. */
5252 unsigned off = LineOffset[row];
5253 unsigned max_off = LineOffset[row] + screen_Columns;
5255 while (off < off_to)
5257 prev_cells = (*mb_off2cells)(off, max_off);
5258 off += prev_cells;
5262 if (enc_dbcs != 0 && prev_cells > 1)
5263 screen_char_2(off_to - prev_cells, row,
5264 col + coloff - prev_cells);
5265 else
5266 # endif
5267 screen_char(off_to - prev_cells, row,
5268 col + coloff - prev_cells);
5271 #endif
5272 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
5273 ' ', ' ', 0);
5274 #ifdef FEAT_VERTSPLIT
5275 off_to += clear_width - col;
5276 col = clear_width;
5277 #endif
5281 if (clear_width > 0)
5283 #ifdef FEAT_VERTSPLIT
5284 /* For a window that's left of another, draw the separator char. */
5285 if (col + coloff < Columns)
5287 int c;
5289 c = fillchar_vsep(&hl);
5290 if (ScreenLines[off_to] != c
5291 # ifdef FEAT_MBYTE
5292 || (enc_utf8 && (int)ScreenLinesUC[off_to]
5293 != (c >= 0x80 ? c : 0))
5294 # endif
5295 || ScreenAttrs[off_to] != hl)
5297 ScreenLines[off_to] = c;
5298 ScreenAttrs[off_to] = hl;
5299 # ifdef FEAT_MBYTE
5300 if (enc_utf8)
5302 if (c >= 0x80)
5304 ScreenLinesUC[off_to] = c;
5305 ScreenLinesC[0][off_to] = 0;
5307 else
5308 ScreenLinesUC[off_to] = 0;
5310 # endif
5311 screen_char(off_to, row, col + coloff);
5314 else
5315 #endif
5316 LineWraps[row] = FALSE;
5320 #if defined(FEAT_RIGHTLEFT) || defined(PROTO)
5322 * Mirror text "str" for right-left displaying.
5323 * Only works for single-byte characters (e.g., numbers).
5325 void
5326 rl_mirror(str)
5327 char_u *str;
5329 char_u *p1, *p2;
5330 int t;
5332 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
5334 t = *p1;
5335 *p1 = *p2;
5336 *p2 = t;
5339 #endif
5341 #if defined(FEAT_WINDOWS) || defined(PROTO)
5343 * mark all status lines for redraw; used after first :cd
5345 void
5346 status_redraw_all()
5348 win_T *wp;
5350 for (wp = firstwin; wp; wp = wp->w_next)
5351 if (wp->w_status_height)
5353 wp->w_redr_status = TRUE;
5354 redraw_later(VALID);
5359 * mark all status lines of the current buffer for redraw
5361 void
5362 status_redraw_curbuf()
5364 win_T *wp;
5366 for (wp = firstwin; wp; wp = wp->w_next)
5367 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
5369 wp->w_redr_status = TRUE;
5370 redraw_later(VALID);
5375 * Redraw all status lines that need to be redrawn.
5377 void
5378 redraw_statuslines()
5380 win_T *wp;
5382 for (wp = firstwin; wp; wp = wp->w_next)
5383 if (wp->w_redr_status)
5384 win_redr_status(wp);
5385 if (redraw_tabline)
5386 draw_tabline();
5388 #endif
5390 #if (defined(FEAT_WILDMENU) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
5392 * Redraw all status lines at the bottom of frame "frp".
5394 void
5395 win_redraw_last_status(frp)
5396 frame_T *frp;
5398 if (frp->fr_layout == FR_LEAF)
5399 frp->fr_win->w_redr_status = TRUE;
5400 else if (frp->fr_layout == FR_ROW)
5402 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
5403 win_redraw_last_status(frp);
5405 else /* frp->fr_layout == FR_COL */
5407 frp = frp->fr_child;
5408 while (frp->fr_next != NULL)
5409 frp = frp->fr_next;
5410 win_redraw_last_status(frp);
5413 #endif
5415 #ifdef FEAT_VERTSPLIT
5417 * Draw the verticap separator right of window "wp" starting with line "row".
5419 static void
5420 draw_vsep_win(wp, row)
5421 win_T *wp;
5422 int row;
5424 int hl;
5425 int c;
5427 if (wp->w_vsep_width)
5429 /* draw the vertical separator right of this window */
5430 c = fillchar_vsep(&hl);
5431 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
5432 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
5433 c, ' ', hl);
5436 #endif
5438 #ifdef FEAT_WILDMENU
5439 static int status_match_len __ARGS((expand_T *xp, char_u *s));
5440 static int skip_status_match_char __ARGS((expand_T *xp, char_u *s));
5443 * Get the length of an item as it will be shown in the status line.
5445 static int
5446 status_match_len(xp, s)
5447 expand_T *xp;
5448 char_u *s;
5450 int len = 0;
5452 #ifdef FEAT_MENU
5453 int emenu = (xp->xp_context == EXPAND_MENUS
5454 || xp->xp_context == EXPAND_MENUNAMES);
5456 /* Check for menu separators - replace with '|'. */
5457 if (emenu && menu_is_separator(s))
5458 return 1;
5459 #endif
5461 while (*s != NUL)
5463 s += skip_status_match_char(xp, s);
5464 len += ptr2cells(s);
5465 mb_ptr_adv(s);
5468 return len;
5472 * Return the number of characters that should be skipped in a status match.
5473 * These are backslashes used for escaping. Do show backslashes in help tags.
5475 static int
5476 skip_status_match_char(xp, s)
5477 expand_T *xp;
5478 char_u *s;
5480 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
5481 #ifdef FEAT_MENU
5482 || ((xp->xp_context == EXPAND_MENUS
5483 || xp->xp_context == EXPAND_MENUNAMES)
5484 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
5485 #endif
5488 #ifndef BACKSLASH_IN_FILENAME
5489 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
5490 return 2;
5491 #endif
5492 return 1;
5494 return 0;
5498 * Show wildchar matches in the status line.
5499 * Show at least the "match" item.
5500 * We start at item 'first_match' in the list and show all matches that fit.
5502 * If inversion is possible we use it. Else '=' characters are used.
5504 void
5505 win_redr_status_matches(xp, num_matches, matches, match, showtail)
5506 expand_T *xp;
5507 int num_matches;
5508 char_u **matches; /* list of matches */
5509 int match;
5510 int showtail;
5512 #define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
5513 int row;
5514 char_u *buf;
5515 int len;
5516 int clen; /* length in screen cells */
5517 int fillchar;
5518 int attr;
5519 int i;
5520 int highlight = TRUE;
5521 char_u *selstart = NULL;
5522 int selstart_col = 0;
5523 char_u *selend = NULL;
5524 static int first_match = 0;
5525 int add_left = FALSE;
5526 char_u *s;
5527 #ifdef FEAT_MENU
5528 int emenu;
5529 #endif
5530 #if defined(FEAT_MBYTE) || defined(FEAT_MENU)
5531 int l;
5532 #endif
5534 if (matches == NULL) /* interrupted completion? */
5535 return;
5537 #ifdef FEAT_MBYTE
5538 if (has_mbyte)
5539 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
5540 else
5541 #endif
5542 buf = alloc((unsigned)Columns + 1);
5543 if (buf == NULL)
5544 return;
5546 if (match == -1) /* don't show match but original text */
5548 match = 0;
5549 highlight = FALSE;
5551 /* count 1 for the ending ">" */
5552 clen = status_match_len(xp, L_MATCH(match)) + 3;
5553 if (match == 0)
5554 first_match = 0;
5555 else if (match < first_match)
5557 /* jumping left, as far as we can go */
5558 first_match = match;
5559 add_left = TRUE;
5561 else
5563 /* check if match fits on the screen */
5564 for (i = first_match; i < match; ++i)
5565 clen += status_match_len(xp, L_MATCH(i)) + 2;
5566 if (first_match > 0)
5567 clen += 2;
5568 /* jumping right, put match at the left */
5569 if ((long)clen > Columns)
5571 first_match = match;
5572 /* if showing the last match, we can add some on the left */
5573 clen = 2;
5574 for (i = match; i < num_matches; ++i)
5576 clen += status_match_len(xp, L_MATCH(i)) + 2;
5577 if ((long)clen >= Columns)
5578 break;
5580 if (i == num_matches)
5581 add_left = TRUE;
5584 if (add_left)
5585 while (first_match > 0)
5587 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
5588 if ((long)clen >= Columns)
5589 break;
5590 --first_match;
5593 fillchar = fillchar_status(&attr, TRUE);
5595 if (first_match == 0)
5597 *buf = NUL;
5598 len = 0;
5600 else
5602 STRCPY(buf, "< ");
5603 len = 2;
5605 clen = len;
5607 i = first_match;
5608 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
5610 if (i == match)
5612 selstart = buf + len;
5613 selstart_col = clen;
5616 s = L_MATCH(i);
5617 /* Check for menu separators - replace with '|' */
5618 #ifdef FEAT_MENU
5619 emenu = (xp->xp_context == EXPAND_MENUS
5620 || xp->xp_context == EXPAND_MENUNAMES);
5621 if (emenu && menu_is_separator(s))
5623 STRCPY(buf + len, transchar('|'));
5624 l = (int)STRLEN(buf + len);
5625 len += l;
5626 clen += l;
5628 else
5629 #endif
5630 for ( ; *s != NUL; ++s)
5632 s += skip_status_match_char(xp, s);
5633 clen += ptr2cells(s);
5634 #ifdef FEAT_MBYTE
5635 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
5637 STRNCPY(buf + len, s, l);
5638 s += l - 1;
5639 len += l;
5641 else
5642 #endif
5644 STRCPY(buf + len, transchar_byte(*s));
5645 len += (int)STRLEN(buf + len);
5648 if (i == match)
5649 selend = buf + len;
5651 *(buf + len++) = ' ';
5652 *(buf + len++) = ' ';
5653 clen += 2;
5654 if (++i == num_matches)
5655 break;
5658 if (i != num_matches)
5660 *(buf + len++) = '>';
5661 ++clen;
5664 buf[len] = NUL;
5666 row = cmdline_row - 1;
5667 if (row >= 0)
5669 if (wild_menu_showing == 0)
5671 if (msg_scrolled > 0)
5673 /* Put the wildmenu just above the command line. If there is
5674 * no room, scroll the screen one line up. */
5675 if (cmdline_row == Rows - 1)
5677 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
5678 ++msg_scrolled;
5680 else
5682 ++cmdline_row;
5683 ++row;
5685 wild_menu_showing = WM_SCROLLED;
5687 else
5689 /* Create status line if needed by setting 'laststatus' to 2.
5690 * Set 'winminheight' to zero to avoid that the window is
5691 * resized. */
5692 if (lastwin->w_status_height == 0)
5694 save_p_ls = p_ls;
5695 save_p_wmh = p_wmh;
5696 p_ls = 2;
5697 p_wmh = 0;
5698 last_status(FALSE);
5700 wild_menu_showing = WM_SHOWN;
5704 screen_puts(buf, row, 0, attr);
5705 if (selstart != NULL && highlight)
5707 *selend = NUL;
5708 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
5711 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
5714 #ifdef FEAT_VERTSPLIT
5715 win_redraw_last_status(topframe);
5716 #else
5717 lastwin->w_redr_status = TRUE;
5718 #endif
5719 vim_free(buf);
5721 #endif
5723 #if defined(FEAT_WINDOWS) || defined(PROTO)
5725 * Redraw the status line of window wp.
5727 * If inversion is possible we use it. Else '=' characters are used.
5729 void
5730 win_redr_status(wp)
5731 win_T *wp;
5733 int row;
5734 char_u *p;
5735 int len;
5736 int fillchar;
5737 int attr;
5738 int this_ru_col;
5740 wp->w_redr_status = FALSE;
5741 if (wp->w_status_height == 0)
5743 /* no status line, can only be last window */
5744 redraw_cmdline = TRUE;
5746 else if (!redrawing()
5747 #ifdef FEAT_INS_EXPAND
5748 /* don't update status line when popup menu is visible and may be
5749 * drawn over it */
5750 || pum_visible()
5751 #endif
5754 /* Don't redraw right now, do it later. */
5755 wp->w_redr_status = TRUE;
5757 #ifdef FEAT_STL_OPT
5758 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
5760 /* redraw custom status line */
5761 redraw_custum_statusline(wp);
5763 #endif
5764 else
5766 fillchar = fillchar_status(&attr, wp == curwin);
5768 get_trans_bufname(wp->w_buffer);
5769 p = NameBuff;
5770 len = (int)STRLEN(p);
5772 if (wp->w_buffer->b_help
5773 #ifdef FEAT_QUICKFIX
5774 || wp->w_p_pvw
5775 #endif
5776 || bufIsChanged(wp->w_buffer)
5777 || wp->w_buffer->b_p_ro)
5778 *(p + len++) = ' ';
5779 if (wp->w_buffer->b_help)
5781 STRCPY(p + len, _("[Help]"));
5782 len += (int)STRLEN(p + len);
5784 #ifdef FEAT_QUICKFIX
5785 if (wp->w_p_pvw)
5787 STRCPY(p + len, _("[Preview]"));
5788 len += (int)STRLEN(p + len);
5790 #endif
5791 if (bufIsChanged(wp->w_buffer))
5793 STRCPY(p + len, "[+]");
5794 len += 3;
5796 if (wp->w_buffer->b_p_ro)
5798 STRCPY(p + len, "[RO]");
5799 len += 4;
5802 #ifndef FEAT_VERTSPLIT
5803 this_ru_col = ru_col;
5804 if (this_ru_col < (Columns + 1) / 2)
5805 this_ru_col = (Columns + 1) / 2;
5806 #else
5807 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
5808 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
5809 this_ru_col = (W_WIDTH(wp) + 1) / 2;
5810 if (this_ru_col <= 1)
5812 p = (char_u *)"<"; /* No room for file name! */
5813 len = 1;
5815 else
5816 #endif
5817 #ifdef FEAT_MBYTE
5818 if (has_mbyte)
5820 int clen = 0, i;
5822 /* Count total number of display cells. */
5823 for (i = 0; p[i] != NUL; i += (*mb_ptr2len)(p + i))
5824 clen += (*mb_ptr2cells)(p + i);
5825 /* Find first character that will fit.
5826 * Going from start to end is much faster for DBCS. */
5827 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
5828 i += (*mb_ptr2len)(p + i))
5829 clen -= (*mb_ptr2cells)(p + i);
5830 len = clen;
5831 if (i > 0)
5833 p = p + i - 1;
5834 *p = '<';
5835 ++len;
5839 else
5840 #endif
5841 if (len > this_ru_col - 1)
5843 p += len - (this_ru_col - 1);
5844 *p = '<';
5845 len = this_ru_col - 1;
5848 row = W_WINROW(wp) + wp->w_height;
5849 screen_puts(p, row, W_WINCOL(wp), attr);
5850 screen_fill(row, row + 1, len + W_WINCOL(wp),
5851 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
5853 if (get_keymap_str(wp, NameBuff, MAXPATHL)
5854 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
5855 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
5856 - 1 + W_WINCOL(wp)), attr);
5858 #ifdef FEAT_CMDL_INFO
5859 win_redr_ruler(wp, TRUE);
5860 #endif
5863 #ifdef FEAT_VERTSPLIT
5865 * May need to draw the character below the vertical separator.
5867 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
5869 if (stl_connected(wp))
5870 fillchar = fillchar_status(&attr, wp == curwin);
5871 else
5872 fillchar = fillchar_vsep(&attr);
5873 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
5874 attr);
5876 #endif
5879 #ifdef FEAT_STL_OPT
5881 * Redraw the status line according to 'statusline' and take care of any
5882 * errors encountered.
5884 static void
5885 redraw_custum_statusline(wp)
5886 win_T *wp;
5888 int save_called_emsg = called_emsg;
5890 called_emsg = FALSE;
5891 win_redr_custom(wp, FALSE);
5892 if (called_emsg)
5893 set_string_option_direct((char_u *)"statusline", -1,
5894 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
5895 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
5896 called_emsg |= save_called_emsg;
5898 #endif
5900 # ifdef FEAT_VERTSPLIT
5902 * Return TRUE if the status line of window "wp" is connected to the status
5903 * line of the window right of it. If not, then it's a vertical separator.
5904 * Only call if (wp->w_vsep_width != 0).
5907 stl_connected(wp)
5908 win_T *wp;
5910 frame_T *fr;
5912 fr = wp->w_frame;
5913 while (fr->fr_parent != NULL)
5915 if (fr->fr_parent->fr_layout == FR_COL)
5917 if (fr->fr_next != NULL)
5918 break;
5920 else
5922 if (fr->fr_next != NULL)
5923 return TRUE;
5925 fr = fr->fr_parent;
5927 return FALSE;
5929 # endif
5931 #endif /* FEAT_WINDOWS */
5933 #if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
5935 * Get the value to show for the language mappings, active 'keymap'.
5938 get_keymap_str(wp, buf, len)
5939 win_T *wp;
5940 char_u *buf; /* buffer for the result */
5941 int len; /* length of buffer */
5943 char_u *p;
5945 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
5946 return FALSE;
5949 #ifdef FEAT_EVAL
5950 buf_T *old_curbuf = curbuf;
5951 win_T *old_curwin = curwin;
5952 char_u *s;
5954 curbuf = wp->w_buffer;
5955 curwin = wp;
5956 STRCPY(buf, "b:keymap_name"); /* must be writable */
5957 ++emsg_skip;
5958 s = p = eval_to_string(buf, NULL, FALSE);
5959 --emsg_skip;
5960 curbuf = old_curbuf;
5961 curwin = old_curwin;
5962 if (p == NULL || *p == NUL)
5963 #endif
5965 #ifdef FEAT_KEYMAP
5966 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
5967 p = wp->w_buffer->b_p_keymap;
5968 else
5969 #endif
5970 p = (char_u *)"lang";
5972 if ((int)(STRLEN(p) + 3) < len)
5973 sprintf((char *)buf, "<%s>", p);
5974 else
5975 buf[0] = NUL;
5976 #ifdef FEAT_EVAL
5977 vim_free(s);
5978 #endif
5980 return buf[0] != NUL;
5982 #endif
5984 #if defined(FEAT_STL_OPT) || defined(PROTO)
5986 * Redraw the status line or ruler of window "wp".
5987 * When "wp" is NULL redraw the tab pages line from 'tabline'.
5989 static void
5990 win_redr_custom(wp, draw_ruler)
5991 win_T *wp;
5992 int draw_ruler; /* TRUE or FALSE */
5994 int attr;
5995 int curattr;
5996 int row;
5997 int col = 0;
5998 int maxwidth;
5999 int width;
6000 int n;
6001 int len;
6002 int fillchar;
6003 char_u buf[MAXPATHL];
6004 char_u *p;
6005 struct stl_hlrec hltab[STL_MAX_ITEM];
6006 struct stl_hlrec tabtab[STL_MAX_ITEM];
6007 int use_sandbox = FALSE;
6009 /* setup environment for the task at hand */
6010 if (wp == NULL)
6012 /* Use 'tabline'. Always at the first line of the screen. */
6013 p = p_tal;
6014 row = 0;
6015 fillchar = ' ';
6016 attr = hl_attr(HLF_TPF);
6017 maxwidth = Columns;
6018 # ifdef FEAT_EVAL
6019 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
6020 # endif
6022 else
6024 row = W_WINROW(wp) + wp->w_height;
6025 fillchar = fillchar_status(&attr, wp == curwin);
6026 maxwidth = W_WIDTH(wp);
6028 if (draw_ruler)
6030 p = p_ruf;
6031 /* advance past any leading group spec - implicit in ru_col */
6032 if (*p == '%')
6034 if (*++p == '-')
6035 p++;
6036 if (atoi((char *) p))
6037 while (VIM_ISDIGIT(*p))
6038 p++;
6039 if (*p++ != '(')
6040 p = p_ruf;
6042 #ifdef FEAT_VERTSPLIT
6043 col = ru_col - (Columns - W_WIDTH(wp));
6044 if (col < (W_WIDTH(wp) + 1) / 2)
6045 col = (W_WIDTH(wp) + 1) / 2;
6046 #else
6047 col = ru_col;
6048 if (col > (Columns + 1) / 2)
6049 col = (Columns + 1) / 2;
6050 #endif
6051 maxwidth = W_WIDTH(wp) - col;
6052 #ifdef FEAT_WINDOWS
6053 if (!wp->w_status_height)
6054 #endif
6056 row = Rows - 1;
6057 --maxwidth; /* writing in last column may cause scrolling */
6058 fillchar = ' ';
6059 attr = 0;
6062 # ifdef FEAT_EVAL
6063 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
6064 # endif
6066 else
6068 if (*wp->w_p_stl != NUL)
6069 p = wp->w_p_stl;
6070 else
6071 p = p_stl;
6072 # ifdef FEAT_EVAL
6073 use_sandbox = was_set_insecurely((char_u *)"statusline",
6074 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
6075 # endif
6078 #ifdef FEAT_VERTSPLIT
6079 col += W_WINCOL(wp);
6080 #endif
6083 if (maxwidth <= 0)
6084 return;
6086 width = build_stl_str_hl(wp == NULL ? curwin : wp,
6087 buf, sizeof(buf),
6088 p, use_sandbox,
6089 fillchar, maxwidth, hltab, tabtab);
6090 len = (int)STRLEN(buf);
6092 while (width < maxwidth && len < sizeof(buf) - 1)
6094 #ifdef FEAT_MBYTE
6095 len += (*mb_char2bytes)(fillchar, buf + len);
6096 #else
6097 buf[len++] = fillchar;
6098 #endif
6099 ++width;
6101 buf[len] = NUL;
6104 * Draw each snippet with the specified highlighting.
6106 curattr = attr;
6107 p = buf;
6108 for (n = 0; hltab[n].start != NULL; n++)
6110 len = (int)(hltab[n].start - p);
6111 screen_puts_len(p, len, row, col, curattr);
6112 col += vim_strnsize(p, len);
6113 p = hltab[n].start;
6115 if (hltab[n].userhl == 0)
6116 curattr = attr;
6117 else if (hltab[n].userhl < 0)
6118 curattr = syn_id2attr(-hltab[n].userhl);
6119 #ifdef FEAT_WINDOWS
6120 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
6121 curattr = highlight_stlnc[hltab[n].userhl - 1];
6122 #endif
6123 else
6124 curattr = highlight_user[hltab[n].userhl - 1];
6126 screen_puts(p, row, col, curattr);
6128 if (wp == NULL)
6130 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
6131 col = 0;
6132 len = 0;
6133 p = buf;
6134 fillchar = 0;
6135 for (n = 0; tabtab[n].start != NULL; n++)
6137 len += vim_strnsize(p, (int)(tabtab[n].start - p));
6138 while (col < len)
6139 TabPageIdxs[col++] = fillchar;
6140 p = tabtab[n].start;
6141 fillchar = tabtab[n].userhl;
6143 while (col < Columns)
6144 TabPageIdxs[col++] = fillchar;
6148 #endif /* FEAT_STL_OPT */
6151 * Output a single character directly to the screen and update ScreenLines.
6153 void
6154 screen_putchar(c, row, col, attr)
6155 int c;
6156 int row, col;
6157 int attr;
6159 #ifdef FEAT_MBYTE
6160 char_u buf[MB_MAXBYTES + 1];
6162 buf[(*mb_char2bytes)(c, buf)] = NUL;
6163 #else
6164 char_u buf[2];
6166 buf[0] = c;
6167 buf[1] = NUL;
6168 #endif
6169 screen_puts(buf, row, col, attr);
6173 * Get a single character directly from ScreenLines into "bytes[]".
6174 * Also return its attribute in *attrp;
6176 void
6177 screen_getbytes(row, col, bytes, attrp)
6178 int row, col;
6179 char_u *bytes;
6180 int *attrp;
6182 unsigned off;
6184 /* safety check */
6185 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
6187 off = LineOffset[row] + col;
6188 *attrp = ScreenAttrs[off];
6189 bytes[0] = ScreenLines[off];
6190 bytes[1] = NUL;
6192 #ifdef FEAT_MBYTE
6193 if (enc_utf8 && ScreenLinesUC[off] != 0)
6194 bytes[utfc_char2bytes(off, bytes)] = NUL;
6195 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6197 bytes[0] = ScreenLines[off];
6198 bytes[1] = ScreenLines2[off];
6199 bytes[2] = NUL;
6201 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
6203 bytes[1] = ScreenLines[off + 1];
6204 bytes[2] = NUL;
6206 #endif
6210 #ifdef FEAT_MBYTE
6211 static int screen_comp_differs __ARGS((int, int*));
6214 * Return TRUE if composing characters for screen posn "off" differs from
6215 * composing characters in "u8cc".
6217 static int
6218 screen_comp_differs(off, u8cc)
6219 int off;
6220 int *u8cc;
6222 int i;
6224 for (i = 0; i < Screen_mco; ++i)
6226 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
6227 return TRUE;
6228 if (u8cc[i] == 0)
6229 break;
6231 return FALSE;
6233 #endif
6236 * Put string '*text' on the screen at position 'row' and 'col', with
6237 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
6238 * Note: only outputs within one row, message is truncated at screen boundary!
6239 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
6241 void
6242 screen_puts(text, row, col, attr)
6243 char_u *text;
6244 int row;
6245 int col;
6246 int attr;
6248 screen_puts_len(text, -1, row, col, attr);
6252 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
6253 * a NUL.
6255 void
6256 screen_puts_len(text, len, row, col, attr)
6257 char_u *text;
6258 int len;
6259 int row;
6260 int col;
6261 int attr;
6263 unsigned off;
6264 char_u *ptr = text;
6265 int c;
6266 #ifdef FEAT_MBYTE
6267 unsigned max_off;
6268 int mbyte_blen = 1;
6269 int mbyte_cells = 1;
6270 int u8c = 0;
6271 int u8cc[MAX_MCO];
6272 int clear_next_cell = FALSE;
6273 # ifdef FEAT_ARABIC
6274 int prev_c = 0; /* previous Arabic character */
6275 int pc, nc, nc1;
6276 int pcc[MAX_MCO];
6277 # endif
6278 #endif
6279 #if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6280 int force_redraw_this;
6281 int force_redraw_next = FALSE;
6282 #endif
6283 int need_redraw;
6285 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
6286 return;
6287 off = LineOffset[row] + col;
6289 #ifdef FEAT_MBYTE
6290 /* When drawing over the right halve of a double-wide char clear out the
6291 * left halve. Only needed in a terminal. */
6292 if (has_mbyte && col > 0 && col < screen_Columns
6293 # ifdef FEAT_GUI
6294 && !gui.in_use
6295 # endif
6296 && mb_fix_col(col, row) != col)
6298 ScreenLines[off - 1] = ' ';
6299 ScreenAttrs[off - 1] = 0;
6300 if (enc_utf8)
6302 ScreenLinesUC[off - 1] = 0;
6303 ScreenLinesC[0][off - 1] = 0;
6305 /* redraw the previous cell, make it empty */
6306 screen_char(off - 1, row, col - 1);
6307 /* force the cell at "col" to be redrawn */
6308 force_redraw_next = TRUE;
6310 #endif
6312 #ifdef FEAT_MBYTE
6313 max_off = LineOffset[row] + screen_Columns;
6314 #endif
6315 while (col < screen_Columns
6316 && (len < 0 || (int)(ptr - text) < len)
6317 && *ptr != NUL)
6319 c = *ptr;
6320 #ifdef FEAT_MBYTE
6321 /* check if this is the first byte of a multibyte */
6322 if (has_mbyte)
6324 if (enc_utf8 && len > 0)
6325 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
6326 else
6327 mbyte_blen = (*mb_ptr2len)(ptr);
6328 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6329 mbyte_cells = 1;
6330 else if (enc_dbcs != 0)
6331 mbyte_cells = mbyte_blen;
6332 else /* enc_utf8 */
6334 if (len >= 0)
6335 u8c = utfc_ptr2char_len(ptr, u8cc,
6336 (int)((text + len) - ptr));
6337 else
6338 u8c = utfc_ptr2char(ptr, u8cc);
6339 mbyte_cells = utf_char2cells(u8c);
6340 # ifdef UNICODE16
6341 /* Non-BMP character: display as ? or fullwidth ?. */
6342 if (u8c >= 0x10000)
6344 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
6345 if (attr == 0)
6346 attr = hl_attr(HLF_8);
6348 # endif
6349 # ifdef FEAT_ARABIC
6350 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
6352 /* Do Arabic shaping. */
6353 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
6355 /* Past end of string to be displayed. */
6356 nc = NUL;
6357 nc1 = NUL;
6359 else
6361 nc = utfc_ptr2char(ptr + mbyte_blen, pcc);
6362 nc1 = pcc[0];
6364 pc = prev_c;
6365 prev_c = u8c;
6366 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
6368 else
6369 prev_c = u8c;
6370 # endif
6373 #endif
6375 #if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6376 force_redraw_this = force_redraw_next;
6377 force_redraw_next = FALSE;
6378 #endif
6380 need_redraw = ScreenLines[off] != c
6381 #ifdef FEAT_MBYTE
6382 || (mbyte_cells == 2
6383 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
6384 || (enc_dbcs == DBCS_JPNU
6385 && c == 0x8e
6386 && ScreenLines2[off] != ptr[1])
6387 || (enc_utf8
6388 && (ScreenLinesUC[off] != (u8char_T)(c >= 0x80 ? u8c : 0)
6389 || screen_comp_differs(off, u8cc)))
6390 #endif
6391 || ScreenAttrs[off] != attr
6392 || exmode_active;
6394 if (need_redraw
6395 #if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6396 || force_redraw_this
6397 #endif
6400 #if defined(FEAT_GUI) || defined(UNIX)
6401 /* The bold trick makes a single row of pixels appear in the next
6402 * character. When a bold character is removed, the next
6403 * character should be redrawn too. This happens for our own GUI
6404 * and for some xterms. */
6405 if (need_redraw && ScreenLines[off] != ' ' && (
6406 # ifdef FEAT_GUI
6407 gui.in_use
6408 # endif
6409 # if defined(FEAT_GUI) && defined(UNIX)
6411 # endif
6412 # ifdef UNIX
6413 term_is_xterm
6414 # endif
6417 int n = ScreenAttrs[off];
6419 if (n > HL_ALL)
6420 n = syn_attr2attr(n);
6421 if (n & HL_BOLD)
6422 force_redraw_next = TRUE;
6424 #endif
6425 #ifdef FEAT_MBYTE
6426 /* When at the end of the text and overwriting a two-cell
6427 * character with a one-cell character, need to clear the next
6428 * cell. Also when overwriting the left halve of a two-cell char
6429 * with the right halve of a two-cell char. Do this only once
6430 * (mb_off2cells() may return 2 on the right halve). */
6431 if (clear_next_cell)
6432 clear_next_cell = FALSE;
6433 else if (has_mbyte
6434 && (len < 0 ? ptr[mbyte_blen] == NUL
6435 : ptr + mbyte_blen >= text + len)
6436 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
6437 || (mbyte_cells == 2
6438 && (*mb_off2cells)(off, max_off) == 1
6439 && (*mb_off2cells)(off + 1, max_off) > 1)))
6440 clear_next_cell = TRUE;
6442 /* Make sure we never leave a second byte of a double-byte behind,
6443 * it confuses mb_off2cells(). */
6444 if (enc_dbcs
6445 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
6446 || (mbyte_cells == 2
6447 && (*mb_off2cells)(off, max_off) == 1
6448 && (*mb_off2cells)(off + 1, max_off) > 1)))
6449 ScreenLines[off + mbyte_blen] = 0;
6450 #endif
6451 ScreenLines[off] = c;
6452 ScreenAttrs[off] = attr;
6453 #ifdef FEAT_MBYTE
6454 if (enc_utf8)
6456 if (c < 0x80 && u8cc[0] == 0)
6457 ScreenLinesUC[off] = 0;
6458 else
6460 int i;
6462 ScreenLinesUC[off] = u8c;
6463 for (i = 0; i < Screen_mco; ++i)
6465 ScreenLinesC[i][off] = u8cc[i];
6466 if (u8cc[i] == 0)
6467 break;
6470 if (mbyte_cells == 2)
6472 ScreenLines[off + 1] = 0;
6473 ScreenAttrs[off + 1] = attr;
6475 screen_char(off, row, col);
6477 else if (mbyte_cells == 2)
6479 ScreenLines[off + 1] = ptr[1];
6480 ScreenAttrs[off + 1] = attr;
6481 screen_char_2(off, row, col);
6483 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6485 ScreenLines2[off] = ptr[1];
6486 screen_char(off, row, col);
6488 else
6489 #endif
6490 screen_char(off, row, col);
6492 #ifdef FEAT_MBYTE
6493 if (has_mbyte)
6495 off += mbyte_cells;
6496 col += mbyte_cells;
6497 ptr += mbyte_blen;
6498 if (clear_next_cell)
6499 ptr = (char_u *)" ";
6501 else
6502 #endif
6504 ++off;
6505 ++col;
6506 ++ptr;
6510 #if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6511 /* If we detected the next character needs to be redrawn, but the text
6512 * doesn't extend up to there, update the character here. */
6513 if (force_redraw_next && col < screen_Columns)
6515 # ifdef FEAT_MBYTE
6516 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
6517 screen_char_2(off, row, col);
6518 else
6519 # endif
6520 screen_char(off, row, col);
6522 #endif
6525 #ifdef FEAT_SEARCH_EXTRA
6527 * Prepare for 'hlsearch' highlighting.
6529 static void
6530 start_search_hl()
6532 if (p_hls && !no_hlsearch)
6534 last_pat_prog(&search_hl.rm);
6535 search_hl.attr = hl_attr(HLF_L);
6536 # ifdef FEAT_RELTIME
6537 /* Set the time limit to 'redrawtime'. */
6538 profile_setlimit(p_rdt, &search_hl.tm);
6539 # endif
6544 * Clean up for 'hlsearch' highlighting.
6546 static void
6547 end_search_hl()
6549 if (search_hl.rm.regprog != NULL)
6551 vim_free(search_hl.rm.regprog);
6552 search_hl.rm.regprog = NULL;
6557 * Advance to the match in window "wp" line "lnum" or past it.
6559 static void
6560 prepare_search_hl(wp, lnum)
6561 win_T *wp;
6562 linenr_T lnum;
6564 matchitem_T *cur; /* points to the match list */
6565 match_T *shl; /* points to search_hl or a match */
6566 int shl_flag; /* flag to indicate whether search_hl
6567 has been processed or not */
6568 int n;
6571 * When using a multi-line pattern, start searching at the top
6572 * of the window or just after a closed fold.
6573 * Do this both for search_hl and the match list.
6575 cur = wp->w_match_head;
6576 shl_flag = FALSE;
6577 while (cur != NULL || shl_flag == FALSE)
6579 if (shl_flag == FALSE)
6581 shl = &search_hl;
6582 shl_flag = TRUE;
6584 else
6585 shl = &cur->hl;
6586 if (shl->rm.regprog != NULL
6587 && shl->lnum == 0
6588 && re_multiline(shl->rm.regprog))
6590 if (shl->first_lnum == 0)
6592 # ifdef FEAT_FOLDING
6593 for (shl->first_lnum = lnum;
6594 shl->first_lnum > wp->w_topline; --shl->first_lnum)
6595 if (hasFoldingWin(wp, shl->first_lnum - 1,
6596 NULL, NULL, TRUE, NULL))
6597 break;
6598 # else
6599 shl->first_lnum = wp->w_topline;
6600 # endif
6602 n = 0;
6603 while (shl->first_lnum < lnum && shl->rm.regprog != NULL)
6605 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n);
6606 if (shl->lnum != 0)
6608 shl->first_lnum = shl->lnum
6609 + shl->rm.endpos[0].lnum
6610 - shl->rm.startpos[0].lnum;
6611 n = shl->rm.endpos[0].col;
6613 else
6615 ++shl->first_lnum;
6616 n = 0;
6620 if (shl != &search_hl && cur != NULL)
6621 cur = cur->next;
6626 * Search for a next 'hlsearch' or match.
6627 * Uses shl->buf.
6628 * Sets shl->lnum and shl->rm contents.
6629 * Note: Assumes a previous match is always before "lnum", unless
6630 * shl->lnum is zero.
6631 * Careful: Any pointers for buffer lines will become invalid.
6633 static void
6634 next_search_hl(win, shl, lnum, mincol)
6635 win_T *win;
6636 match_T *shl; /* points to search_hl or a match */
6637 linenr_T lnum;
6638 colnr_T mincol; /* minimal column for a match */
6640 linenr_T l;
6641 colnr_T matchcol;
6642 long nmatched;
6644 if (shl->lnum != 0)
6646 /* Check for three situations:
6647 * 1. If the "lnum" is below a previous match, start a new search.
6648 * 2. If the previous match includes "mincol", use it.
6649 * 3. Continue after the previous match.
6651 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
6652 if (lnum > l)
6653 shl->lnum = 0;
6654 else if (lnum < l || shl->rm.endpos[0].col > mincol)
6655 return;
6659 * Repeat searching for a match until one is found that includes "mincol"
6660 * or none is found in this line.
6662 called_emsg = FALSE;
6663 for (;;)
6665 #ifdef FEAT_RELTIME
6666 /* Stop searching after passing the time limit. */
6667 if (profile_passed_limit(&(shl->tm)))
6669 shl->lnum = 0; /* no match found in time */
6670 break;
6672 #endif
6673 /* Three situations:
6674 * 1. No useful previous match: search from start of line.
6675 * 2. Not Vi compatible or empty match: continue at next character.
6676 * Break the loop if this is beyond the end of the line.
6677 * 3. Vi compatible searching: continue at end of previous match.
6679 if (shl->lnum == 0)
6680 matchcol = 0;
6681 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
6682 || (shl->rm.endpos[0].lnum == 0
6683 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
6685 char_u *ml;
6687 matchcol = shl->rm.startpos[0].col;
6688 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
6689 if (*ml == NUL)
6691 ++matchcol;
6692 shl->lnum = 0;
6693 break;
6695 #ifdef FEAT_MBYTE
6696 if (has_mbyte)
6697 matchcol += mb_ptr2len(ml);
6698 else
6699 #endif
6700 ++matchcol;
6702 else
6703 matchcol = shl->rm.endpos[0].col;
6705 shl->lnum = lnum;
6706 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum, matchcol,
6707 #ifdef FEAT_RELTIME
6708 &(shl->tm)
6709 #else
6710 NULL
6711 #endif
6713 if (called_emsg)
6715 /* Error while handling regexp: stop using this regexp. */
6716 if (shl == &search_hl)
6718 /* don't free regprog in the match list, it's a copy */
6719 vim_free(shl->rm.regprog);
6720 no_hlsearch = TRUE;
6722 shl->rm.regprog = NULL;
6723 shl->lnum = 0;
6724 got_int = FALSE; /* avoid the "Type :quit to exit Vim" message */
6725 break;
6727 if (nmatched == 0)
6729 shl->lnum = 0; /* no match found */
6730 break;
6732 if (shl->rm.startpos[0].lnum > 0
6733 || shl->rm.startpos[0].col >= mincol
6734 || nmatched > 1
6735 || shl->rm.endpos[0].col > mincol)
6737 shl->lnum += shl->rm.startpos[0].lnum;
6738 break; /* useful match found */
6742 #endif
6744 static void
6745 screen_start_highlight(attr)
6746 int attr;
6748 attrentry_T *aep = NULL;
6750 screen_attr = attr;
6751 if (full_screen
6752 #ifdef WIN3264
6753 && termcap_active
6754 #endif
6757 #ifdef FEAT_GUI
6758 if (gui.in_use)
6760 char buf[20];
6762 /* The GUI handles this internally. */
6763 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
6764 OUT_STR(buf);
6766 else
6767 #endif
6769 if (attr > HL_ALL) /* special HL attr. */
6771 if (t_colors > 1)
6772 aep = syn_cterm_attr2entry(attr);
6773 else
6774 aep = syn_term_attr2entry(attr);
6775 if (aep == NULL) /* did ":syntax clear" */
6776 attr = 0;
6777 else
6778 attr = aep->ae_attr;
6780 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
6781 out_str(T_MD);
6782 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
6783 && cterm_normal_fg_bold)
6784 /* If the Normal FG color has BOLD attribute and the new HL
6785 * has a FG color defined, clear BOLD. */
6786 out_str(T_ME);
6787 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
6788 out_str(T_SO);
6789 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
6790 /* underline or undercurl */
6791 out_str(T_US);
6792 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
6793 out_str(T_CZH);
6794 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
6795 out_str(T_MR);
6798 * Output the color or start string after bold etc., in case the
6799 * bold etc. override the color setting.
6801 if (aep != NULL)
6803 if (t_colors > 1)
6805 if (aep->ae_u.cterm.fg_color)
6806 term_fg_color(aep->ae_u.cterm.fg_color - 1);
6807 if (aep->ae_u.cterm.bg_color)
6808 term_bg_color(aep->ae_u.cterm.bg_color - 1);
6810 else
6812 if (aep->ae_u.term.start != NULL)
6813 out_str(aep->ae_u.term.start);
6820 void
6821 screen_stop_highlight()
6823 int do_ME = FALSE; /* output T_ME code */
6825 if (screen_attr != 0
6826 #ifdef WIN3264
6827 && termcap_active
6828 #endif
6831 #ifdef FEAT_GUI
6832 if (gui.in_use)
6834 char buf[20];
6836 /* use internal GUI code */
6837 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
6838 OUT_STR(buf);
6840 else
6841 #endif
6843 if (screen_attr > HL_ALL) /* special HL attr. */
6845 attrentry_T *aep;
6847 if (t_colors > 1)
6850 * Assume that t_me restores the original colors!
6852 aep = syn_cterm_attr2entry(screen_attr);
6853 if (aep != NULL && (aep->ae_u.cterm.fg_color
6854 || aep->ae_u.cterm.bg_color))
6855 do_ME = TRUE;
6857 else
6859 aep = syn_term_attr2entry(screen_attr);
6860 if (aep != NULL && aep->ae_u.term.stop != NULL)
6862 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
6863 do_ME = TRUE;
6864 else
6865 out_str(aep->ae_u.term.stop);
6868 if (aep == NULL) /* did ":syntax clear" */
6869 screen_attr = 0;
6870 else
6871 screen_attr = aep->ae_attr;
6875 * Often all ending-codes are equal to T_ME. Avoid outputting the
6876 * same sequence several times.
6878 if (screen_attr & HL_STANDOUT)
6880 if (STRCMP(T_SE, T_ME) == 0)
6881 do_ME = TRUE;
6882 else
6883 out_str(T_SE);
6885 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
6887 if (STRCMP(T_UE, T_ME) == 0)
6888 do_ME = TRUE;
6889 else
6890 out_str(T_UE);
6892 if (screen_attr & HL_ITALIC)
6894 if (STRCMP(T_CZR, T_ME) == 0)
6895 do_ME = TRUE;
6896 else
6897 out_str(T_CZR);
6899 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
6900 out_str(T_ME);
6902 if (t_colors > 1)
6904 /* set Normal cterm colors */
6905 if (cterm_normal_fg_color != 0)
6906 term_fg_color(cterm_normal_fg_color - 1);
6907 if (cterm_normal_bg_color != 0)
6908 term_bg_color(cterm_normal_bg_color - 1);
6909 if (cterm_normal_fg_bold)
6910 out_str(T_MD);
6914 screen_attr = 0;
6918 * Reset the colors for a cterm. Used when leaving Vim.
6919 * The machine specific code may override this again.
6921 void
6922 reset_cterm_colors()
6924 if (t_colors > 1)
6926 /* set Normal cterm colors */
6927 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
6929 out_str(T_OP);
6930 screen_attr = -1;
6932 if (cterm_normal_fg_bold)
6934 out_str(T_ME);
6935 screen_attr = -1;
6941 * Put character ScreenLines["off"] on the screen at position "row" and "col",
6942 * using the attributes from ScreenAttrs["off"].
6944 static void
6945 screen_char(off, row, col)
6946 unsigned off;
6947 int row;
6948 int col;
6950 int attr;
6952 /* Check for illegal values, just in case (could happen just after
6953 * resizing). */
6954 if (row >= screen_Rows || col >= screen_Columns)
6955 return;
6957 /* Outputting the last character on the screen may scrollup the screen.
6958 * Don't to it! Mark the character invalid (update it when scrolled up) */
6959 if (row == screen_Rows - 1 && col == screen_Columns - 1
6960 #ifdef FEAT_RIGHTLEFT
6961 /* account for first command-line character in rightleft mode */
6962 && !cmdmsg_rl
6963 #endif
6966 ScreenAttrs[off] = (sattr_T)-1;
6967 return;
6971 * Stop highlighting first, so it's easier to move the cursor.
6973 #if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
6974 if (screen_char_attr != 0)
6975 attr = screen_char_attr;
6976 else
6977 #endif
6978 attr = ScreenAttrs[off];
6979 if (screen_attr != attr)
6980 screen_stop_highlight();
6982 windgoto(row, col);
6984 if (screen_attr != attr)
6985 screen_start_highlight(attr);
6987 #ifdef FEAT_MBYTE
6988 if (enc_utf8 && ScreenLinesUC[off] != 0)
6990 char_u buf[MB_MAXBYTES + 1];
6992 /* Convert UTF-8 character to bytes and write it. */
6994 buf[utfc_char2bytes(off, buf)] = NUL;
6996 out_str(buf);
6997 if (utf_char2cells(ScreenLinesUC[off]) > 1)
6998 ++screen_cur_col;
7000 else
7001 #endif
7003 #ifdef FEAT_MBYTE
7004 out_flush_check();
7005 #endif
7006 out_char(ScreenLines[off]);
7007 #ifdef FEAT_MBYTE
7008 /* double-byte character in single-width cell */
7009 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7010 out_char(ScreenLines2[off]);
7011 #endif
7014 screen_cur_col++;
7017 #ifdef FEAT_MBYTE
7020 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
7021 * on the screen at position 'row' and 'col'.
7022 * The attributes of the first byte is used for all. This is required to
7023 * output the two bytes of a double-byte character with nothing in between.
7025 static void
7026 screen_char_2(off, row, col)
7027 unsigned off;
7028 int row;
7029 int col;
7031 /* Check for illegal values (could be wrong when screen was resized). */
7032 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
7033 return;
7035 /* Outputting the last character on the screen may scrollup the screen.
7036 * Don't to it! Mark the character invalid (update it when scrolled up) */
7037 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
7039 ScreenAttrs[off] = (sattr_T)-1;
7040 return;
7043 /* Output the first byte normally (positions the cursor), then write the
7044 * second byte directly. */
7045 screen_char(off, row, col);
7046 out_char(ScreenLines[off + 1]);
7047 ++screen_cur_col;
7049 #endif
7051 #if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
7053 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
7054 * This uses the contents of ScreenLines[] and doesn't change it.
7056 void
7057 screen_draw_rectangle(row, col, height, width, invert)
7058 int row;
7059 int col;
7060 int height;
7061 int width;
7062 int invert;
7064 int r, c;
7065 int off;
7066 #ifdef FEAT_MBYTE
7067 int max_off;
7068 #endif
7070 /* Can't use ScreenLines unless initialized */
7071 if (ScreenLines == NULL)
7072 return;
7074 if (invert)
7075 screen_char_attr = HL_INVERSE;
7076 for (r = row; r < row + height; ++r)
7078 off = LineOffset[r];
7079 #ifdef FEAT_MBYTE
7080 max_off = off + screen_Columns;
7081 #endif
7082 for (c = col; c < col + width; ++c)
7084 #ifdef FEAT_MBYTE
7085 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
7087 screen_char_2(off + c, r, c);
7088 ++c;
7090 else
7091 #endif
7093 screen_char(off + c, r, c);
7094 #ifdef FEAT_MBYTE
7095 if (utf_off2cells(off + c, max_off) > 1)
7096 ++c;
7097 #endif
7101 screen_char_attr = 0;
7103 #endif
7105 #ifdef FEAT_VERTSPLIT
7107 * Redraw the characters for a vertically split window.
7109 static void
7110 redraw_block(row, end, wp)
7111 int row;
7112 int end;
7113 win_T *wp;
7115 int col;
7116 int width;
7118 # ifdef FEAT_CLIPBOARD
7119 clip_may_clear_selection(row, end - 1);
7120 # endif
7122 if (wp == NULL)
7124 col = 0;
7125 width = Columns;
7127 else
7129 col = wp->w_wincol;
7130 width = wp->w_width;
7132 screen_draw_rectangle(row, col, end - row, width, FALSE);
7134 #endif
7137 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
7138 * with character 'c1' in first column followed by 'c2' in the other columns.
7139 * Use attributes 'attr'.
7141 void
7142 screen_fill(start_row, end_row, start_col, end_col, c1, c2, attr)
7143 int start_row, end_row;
7144 int start_col, end_col;
7145 int c1, c2;
7146 int attr;
7148 int row;
7149 int col;
7150 int off;
7151 int end_off;
7152 int did_delete;
7153 int c;
7154 int norm_term;
7155 #if defined(FEAT_GUI) || defined(UNIX)
7156 int force_next = FALSE;
7157 #endif
7159 if (end_row > screen_Rows) /* safety check */
7160 end_row = screen_Rows;
7161 if (end_col > screen_Columns) /* safety check */
7162 end_col = screen_Columns;
7163 if (ScreenLines == NULL
7164 || start_row >= end_row
7165 || start_col >= end_col) /* nothing to do */
7166 return;
7168 /* it's a "normal" terminal when not in a GUI or cterm */
7169 norm_term = (
7170 #ifdef FEAT_GUI
7171 !gui.in_use &&
7172 #endif
7173 t_colors <= 1);
7174 for (row = start_row; row < end_row; ++row)
7176 #ifdef FEAT_MBYTE
7177 if (has_mbyte
7178 # ifdef FEAT_GUI
7179 && !gui.in_use
7180 # endif
7183 /* When drawing over the right halve of a double-wide char clear
7184 * out the left halve. When drawing over the left halve of a
7185 * double wide-char clear out the right halve. Only needed in a
7186 * terminal. */
7187 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
7188 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
7189 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
7190 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
7192 #endif
7194 * Try to use delete-line termcap code, when no attributes or in a
7195 * "normal" terminal, where a bold/italic space is just a
7196 * space.
7198 did_delete = FALSE;
7199 if (c2 == ' '
7200 && end_col == Columns
7201 && can_clear(T_CE)
7202 && (attr == 0
7203 || (norm_term
7204 && attr <= HL_ALL
7205 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
7208 * check if we really need to clear something
7210 col = start_col;
7211 if (c1 != ' ') /* don't clear first char */
7212 ++col;
7214 off = LineOffset[row] + col;
7215 end_off = LineOffset[row] + end_col;
7217 /* skip blanks (used often, keep it fast!) */
7218 #ifdef FEAT_MBYTE
7219 if (enc_utf8)
7220 while (off < end_off && ScreenLines[off] == ' '
7221 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
7222 ++off;
7223 else
7224 #endif
7225 while (off < end_off && ScreenLines[off] == ' '
7226 && ScreenAttrs[off] == 0)
7227 ++off;
7228 if (off < end_off) /* something to be cleared */
7230 col = off - LineOffset[row];
7231 screen_stop_highlight();
7232 term_windgoto(row, col);/* clear rest of this screen line */
7233 out_str(T_CE);
7234 screen_start(); /* don't know where cursor is now */
7235 col = end_col - col;
7236 while (col--) /* clear chars in ScreenLines */
7238 ScreenLines[off] = ' ';
7239 #ifdef FEAT_MBYTE
7240 if (enc_utf8)
7241 ScreenLinesUC[off] = 0;
7242 #endif
7243 ScreenAttrs[off] = 0;
7244 ++off;
7247 did_delete = TRUE; /* the chars are cleared now */
7250 off = LineOffset[row] + start_col;
7251 c = c1;
7252 for (col = start_col; col < end_col; ++col)
7254 if (ScreenLines[off] != c
7255 #ifdef FEAT_MBYTE
7256 || (enc_utf8 && (int)ScreenLinesUC[off]
7257 != (c >= 0x80 ? c : 0))
7258 #endif
7259 || ScreenAttrs[off] != attr
7260 #if defined(FEAT_GUI) || defined(UNIX)
7261 || force_next
7262 #endif
7265 #if defined(FEAT_GUI) || defined(UNIX)
7266 /* The bold trick may make a single row of pixels appear in
7267 * the next character. When a bold character is removed, the
7268 * next character should be redrawn too. This happens for our
7269 * own GUI and for some xterms. */
7270 if (
7271 # ifdef FEAT_GUI
7272 gui.in_use
7273 # endif
7274 # if defined(FEAT_GUI) && defined(UNIX)
7276 # endif
7277 # ifdef UNIX
7278 term_is_xterm
7279 # endif
7282 if (ScreenLines[off] != ' '
7283 && (ScreenAttrs[off] > HL_ALL
7284 || ScreenAttrs[off] & HL_BOLD))
7285 force_next = TRUE;
7286 else
7287 force_next = FALSE;
7289 #endif
7290 ScreenLines[off] = c;
7291 #ifdef FEAT_MBYTE
7292 if (enc_utf8)
7294 if (c >= 0x80)
7296 ScreenLinesUC[off] = c;
7297 ScreenLinesC[0][off] = 0;
7299 else
7300 ScreenLinesUC[off] = 0;
7302 #endif
7303 ScreenAttrs[off] = attr;
7304 if (!did_delete || c != ' ')
7305 screen_char(off, row, col);
7307 ++off;
7308 if (col == start_col)
7310 if (did_delete)
7311 break;
7312 c = c2;
7315 if (end_col == Columns)
7316 LineWraps[row] = FALSE;
7317 if (row == Rows - 1) /* overwritten the command line */
7319 redraw_cmdline = TRUE;
7320 if (c1 == ' ' && c2 == ' ')
7321 clear_cmdline = FALSE; /* command line has been cleared */
7322 if (start_col == 0)
7323 mode_displayed = FALSE; /* mode cleared or overwritten */
7329 * Check if there should be a delay. Used before clearing or redrawing the
7330 * screen or the command line.
7332 void
7333 check_for_delay(check_msg_scroll)
7334 int check_msg_scroll;
7336 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
7337 && !did_wait_return
7338 && emsg_silent == 0)
7340 out_flush();
7341 ui_delay(1000L, TRUE);
7342 emsg_on_display = FALSE;
7343 if (check_msg_scroll)
7344 msg_scroll = FALSE;
7349 * screen_valid - allocate screen buffers if size changed
7350 * If "clear" is TRUE: clear screen if it has been resized.
7351 * Returns TRUE if there is a valid screen to write to.
7352 * Returns FALSE when starting up and screen not initialized yet.
7355 screen_valid(clear)
7356 int clear;
7358 screenalloc(clear); /* allocate screen buffers if size changed */
7359 return (ScreenLines != NULL);
7363 * Resize the shell to Rows and Columns.
7364 * Allocate ScreenLines[] and associated items.
7366 * There may be some time between setting Rows and Columns and (re)allocating
7367 * ScreenLines[]. This happens when starting up and when (manually) changing
7368 * the shell size. Always use screen_Rows and screen_Columns to access items
7369 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
7370 * final size of the shell is needed.
7372 void
7373 screenalloc(clear)
7374 int clear;
7376 int new_row, old_row;
7377 #ifdef FEAT_GUI
7378 int old_Rows;
7379 #endif
7380 win_T *wp;
7381 int outofmem = FALSE;
7382 int len;
7383 schar_T *new_ScreenLines;
7384 #ifdef FEAT_MBYTE
7385 u8char_T *new_ScreenLinesUC = NULL;
7386 u8char_T *new_ScreenLinesC[MAX_MCO];
7387 schar_T *new_ScreenLines2 = NULL;
7388 int i;
7389 #endif
7390 sattr_T *new_ScreenAttrs;
7391 unsigned *new_LineOffset;
7392 char_u *new_LineWraps;
7393 #ifdef FEAT_WINDOWS
7394 short *new_TabPageIdxs;
7395 tabpage_T *tp;
7396 #endif
7397 static int entered = FALSE; /* avoid recursiveness */
7398 static int done_outofmem_msg = FALSE; /* did outofmem message */
7399 #ifdef FEAT_AUTOCMD
7400 int retry_count = 0;
7402 retry:
7403 #endif
7405 * Allocation of the screen buffers is done only when the size changes and
7406 * when Rows and Columns have been set and we have started doing full
7407 * screen stuff.
7409 if ((ScreenLines != NULL
7410 && Rows == screen_Rows
7411 && Columns == screen_Columns
7412 #ifdef FEAT_MBYTE
7413 && enc_utf8 == (ScreenLinesUC != NULL)
7414 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
7415 && p_mco == Screen_mco
7416 #endif
7418 || Rows == 0
7419 || Columns == 0
7420 || (!full_screen && ScreenLines == NULL))
7421 return;
7424 * It's possible that we produce an out-of-memory message below, which
7425 * will cause this function to be called again. To break the loop, just
7426 * return here.
7428 if (entered)
7429 return;
7430 entered = TRUE;
7433 * Note that the window sizes are updated before reallocating the arrays,
7434 * thus we must not redraw here!
7436 ++RedrawingDisabled;
7438 win_new_shellsize(); /* fit the windows in the new sized shell */
7440 comp_col(); /* recompute columns for shown command and ruler */
7443 * We're changing the size of the screen.
7444 * - Allocate new arrays for ScreenLines and ScreenAttrs.
7445 * - Move lines from the old arrays into the new arrays, clear extra
7446 * lines (unless the screen is going to be cleared).
7447 * - Free the old arrays.
7449 * If anything fails, make ScreenLines NULL, so we don't do anything!
7450 * Continuing with the old ScreenLines may result in a crash, because the
7451 * size is wrong.
7453 FOR_ALL_TAB_WINDOWS(tp, wp)
7454 win_free_lsize(wp);
7456 new_ScreenLines = (schar_T *)lalloc((long_u)(
7457 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7458 #ifdef FEAT_MBYTE
7459 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T) * MAX_MCO);
7460 if (enc_utf8)
7462 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
7463 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
7464 for (i = 0; i < p_mco; ++i)
7465 new_ScreenLinesC[i] = (u8char_T *)lalloc((long_u)(
7466 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
7468 if (enc_dbcs == DBCS_JPNU)
7469 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
7470 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7471 #endif
7472 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
7473 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
7474 new_LineOffset = (unsigned *)lalloc((long_u)(
7475 Rows * sizeof(unsigned)), FALSE);
7476 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
7477 #ifdef FEAT_WINDOWS
7478 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
7479 #endif
7481 FOR_ALL_TAB_WINDOWS(tp, wp)
7483 if (win_alloc_lines(wp) == FAIL)
7485 outofmem = TRUE;
7486 #ifdef FEAT_WINDOWS
7487 goto give_up;
7488 #endif
7491 #ifdef FEAT_WINDOWS
7492 give_up:
7493 #endif
7495 #ifdef FEAT_MBYTE
7496 for (i = 0; i < p_mco; ++i)
7497 if (new_ScreenLinesC[i] == NULL)
7498 break;
7499 #endif
7500 if (new_ScreenLines == NULL
7501 #ifdef FEAT_MBYTE
7502 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
7503 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
7504 #endif
7505 || new_ScreenAttrs == NULL
7506 || new_LineOffset == NULL
7507 || new_LineWraps == NULL
7508 #ifdef FEAT_WINDOWS
7509 || new_TabPageIdxs == NULL
7510 #endif
7511 || outofmem)
7513 if (ScreenLines != NULL || !done_outofmem_msg)
7515 /* guess the size */
7516 do_outofmem_msg((long_u)((Rows + 1) * Columns));
7518 /* Remember we did this to avoid getting outofmem messages over
7519 * and over again. */
7520 done_outofmem_msg = TRUE;
7522 vim_free(new_ScreenLines);
7523 new_ScreenLines = NULL;
7524 #ifdef FEAT_MBYTE
7525 vim_free(new_ScreenLinesUC);
7526 new_ScreenLinesUC = NULL;
7527 for (i = 0; i < p_mco; ++i)
7529 vim_free(new_ScreenLinesC[i]);
7530 new_ScreenLinesC[i] = NULL;
7532 vim_free(new_ScreenLines2);
7533 new_ScreenLines2 = NULL;
7534 #endif
7535 vim_free(new_ScreenAttrs);
7536 new_ScreenAttrs = NULL;
7537 vim_free(new_LineOffset);
7538 new_LineOffset = NULL;
7539 vim_free(new_LineWraps);
7540 new_LineWraps = NULL;
7541 #ifdef FEAT_WINDOWS
7542 vim_free(new_TabPageIdxs);
7543 new_TabPageIdxs = NULL;
7544 #endif
7546 else
7548 done_outofmem_msg = FALSE;
7550 for (new_row = 0; new_row < Rows; ++new_row)
7552 new_LineOffset[new_row] = new_row * Columns;
7553 new_LineWraps[new_row] = FALSE;
7556 * If the screen is not going to be cleared, copy as much as
7557 * possible from the old screen to the new one and clear the rest
7558 * (used when resizing the window at the "--more--" prompt or when
7559 * executing an external command, for the GUI).
7561 if (!clear)
7563 (void)vim_memset(new_ScreenLines + new_row * Columns,
7564 ' ', (size_t)Columns * sizeof(schar_T));
7565 #ifdef FEAT_MBYTE
7566 if (enc_utf8)
7568 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
7569 0, (size_t)Columns * sizeof(u8char_T));
7570 for (i = 0; i < p_mco; ++i)
7571 (void)vim_memset(new_ScreenLinesC[i]
7572 + new_row * Columns,
7573 0, (size_t)Columns * sizeof(u8char_T));
7575 if (enc_dbcs == DBCS_JPNU)
7576 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
7577 0, (size_t)Columns * sizeof(schar_T));
7578 #endif
7579 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
7580 0, (size_t)Columns * sizeof(sattr_T));
7581 old_row = new_row + (screen_Rows - Rows);
7582 if (old_row >= 0 && ScreenLines != NULL)
7584 if (screen_Columns < Columns)
7585 len = screen_Columns;
7586 else
7587 len = Columns;
7588 #ifdef FEAT_MBYTE
7589 /* When switching to utf-8 don't copy characters, they
7590 * may be invalid now. Also when p_mco changes. */
7591 if (!(enc_utf8 && ScreenLinesUC == NULL)
7592 && p_mco == Screen_mco)
7593 #endif
7594 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
7595 ScreenLines + LineOffset[old_row],
7596 (size_t)len * sizeof(schar_T));
7597 #ifdef FEAT_MBYTE
7598 if (enc_utf8 && ScreenLinesUC != NULL
7599 && p_mco == Screen_mco)
7601 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
7602 ScreenLinesUC + LineOffset[old_row],
7603 (size_t)len * sizeof(u8char_T));
7604 for (i = 0; i < p_mco; ++i)
7605 mch_memmove(new_ScreenLinesC[i]
7606 + new_LineOffset[new_row],
7607 ScreenLinesC[i] + LineOffset[old_row],
7608 (size_t)len * sizeof(u8char_T));
7610 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
7611 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
7612 ScreenLines2 + LineOffset[old_row],
7613 (size_t)len * sizeof(schar_T));
7614 #endif
7615 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
7616 ScreenAttrs + LineOffset[old_row],
7617 (size_t)len * sizeof(sattr_T));
7621 /* Use the last line of the screen for the current line. */
7622 current_ScreenLine = new_ScreenLines + Rows * Columns;
7625 free_screenlines();
7627 ScreenLines = new_ScreenLines;
7628 #ifdef FEAT_MBYTE
7629 ScreenLinesUC = new_ScreenLinesUC;
7630 for (i = 0; i < p_mco; ++i)
7631 ScreenLinesC[i] = new_ScreenLinesC[i];
7632 Screen_mco = p_mco;
7633 ScreenLines2 = new_ScreenLines2;
7634 #endif
7635 ScreenAttrs = new_ScreenAttrs;
7636 LineOffset = new_LineOffset;
7637 LineWraps = new_LineWraps;
7638 #ifdef FEAT_WINDOWS
7639 TabPageIdxs = new_TabPageIdxs;
7640 #endif
7642 /* It's important that screen_Rows and screen_Columns reflect the actual
7643 * size of ScreenLines[]. Set them before calling anything. */
7644 #ifdef FEAT_GUI
7645 old_Rows = screen_Rows;
7646 #endif
7647 screen_Rows = Rows;
7648 screen_Columns = Columns;
7650 must_redraw = CLEAR; /* need to clear the screen later */
7651 if (clear)
7652 screenclear2();
7654 #ifdef FEAT_GUI
7655 else if (gui.in_use
7656 && !gui.starting
7657 && ScreenLines != NULL
7658 && old_Rows != Rows)
7660 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
7662 * Adjust the position of the cursor, for when executing an external
7663 * command.
7665 if (msg_row >= Rows) /* Rows got smaller */
7666 msg_row = Rows - 1; /* put cursor at last row */
7667 else if (Rows > old_Rows) /* Rows got bigger */
7668 msg_row += Rows - old_Rows; /* put cursor in same place */
7669 if (msg_col >= Columns) /* Columns got smaller */
7670 msg_col = Columns - 1; /* put cursor at last column */
7672 #endif
7674 entered = FALSE;
7675 --RedrawingDisabled;
7677 #ifdef FEAT_AUTOCMD
7679 * Do not apply autocommands more than 3 times to avoid an endless loop
7680 * in case applying autocommands always changes Rows or Columns.
7682 if (starting == 0 && ++retry_count <= 3)
7684 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
7685 /* In rare cases, autocommands may have altered Rows or Columns,
7686 * jump back to check if we need to allocate the screen again. */
7687 goto retry;
7689 #endif
7692 void
7693 free_screenlines()
7695 #ifdef FEAT_MBYTE
7696 int i;
7698 vim_free(ScreenLinesUC);
7699 for (i = 0; i < Screen_mco; ++i)
7700 vim_free(ScreenLinesC[i]);
7701 vim_free(ScreenLines2);
7702 #endif
7703 vim_free(ScreenLines);
7704 vim_free(ScreenAttrs);
7705 vim_free(LineOffset);
7706 vim_free(LineWraps);
7707 #ifdef FEAT_WINDOWS
7708 vim_free(TabPageIdxs);
7709 #endif
7712 void
7713 screenclear()
7715 check_for_delay(FALSE);
7716 screenalloc(FALSE); /* allocate screen buffers if size changed */
7717 screenclear2(); /* clear the screen */
7720 static void
7721 screenclear2()
7723 int i;
7725 if (starting == NO_SCREEN || ScreenLines == NULL
7726 #ifdef FEAT_GUI
7727 || (gui.in_use && gui.starting)
7728 #endif
7730 return;
7732 #ifdef FEAT_GUI
7733 if (!gui.in_use)
7734 #endif
7735 screen_attr = -1; /* force setting the Normal colors */
7736 screen_stop_highlight(); /* don't want highlighting here */
7738 #ifdef FEAT_CLIPBOARD
7739 /* disable selection without redrawing it */
7740 clip_scroll_selection(9999);
7741 #endif
7743 /* blank out ScreenLines */
7744 for (i = 0; i < Rows; ++i)
7746 lineclear(LineOffset[i], (int)Columns);
7747 LineWraps[i] = FALSE;
7750 if (can_clear(T_CL))
7752 out_str(T_CL); /* clear the display */
7753 clear_cmdline = FALSE;
7754 mode_displayed = FALSE;
7756 else
7758 /* can't clear the screen, mark all chars with invalid attributes */
7759 for (i = 0; i < Rows; ++i)
7760 lineinvalid(LineOffset[i], (int)Columns);
7761 clear_cmdline = TRUE;
7764 screen_cleared = TRUE; /* can use contents of ScreenLines now */
7766 win_rest_invalid(firstwin);
7767 redraw_cmdline = TRUE;
7768 #ifdef FEAT_WINDOWS
7769 redraw_tabline = TRUE;
7770 #endif
7771 if (must_redraw == CLEAR) /* no need to clear again */
7772 must_redraw = NOT_VALID;
7773 compute_cmdrow();
7774 msg_row = cmdline_row; /* put cursor on last line for messages */
7775 msg_col = 0;
7776 screen_start(); /* don't know where cursor is now */
7777 msg_scrolled = 0; /* can't scroll back */
7778 msg_didany = FALSE;
7779 msg_didout = FALSE;
7783 * Clear one line in ScreenLines.
7785 static void
7786 lineclear(off, width)
7787 unsigned off;
7788 int width;
7790 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
7791 #ifdef FEAT_MBYTE
7792 if (enc_utf8)
7793 (void)vim_memset(ScreenLinesUC + off, 0,
7794 (size_t)width * sizeof(u8char_T));
7795 #endif
7796 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
7800 * Mark one line in ScreenLines invalid by setting the attributes to an
7801 * invalid value.
7803 static void
7804 lineinvalid(off, width)
7805 unsigned off;
7806 int width;
7808 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
7811 #ifdef FEAT_VERTSPLIT
7813 * Copy part of a Screenline for vertically split window "wp".
7815 static void
7816 linecopy(to, from, wp)
7817 int to;
7818 int from;
7819 win_T *wp;
7821 unsigned off_to = LineOffset[to] + wp->w_wincol;
7822 unsigned off_from = LineOffset[from] + wp->w_wincol;
7824 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
7825 wp->w_width * sizeof(schar_T));
7826 # ifdef FEAT_MBYTE
7827 if (enc_utf8)
7829 int i;
7831 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
7832 wp->w_width * sizeof(u8char_T));
7833 for (i = 0; i < p_mco; ++i)
7834 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
7835 wp->w_width * sizeof(u8char_T));
7837 if (enc_dbcs == DBCS_JPNU)
7838 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
7839 wp->w_width * sizeof(schar_T));
7840 # endif
7841 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
7842 wp->w_width * sizeof(sattr_T));
7844 #endif
7847 * Return TRUE if clearing with term string "p" would work.
7848 * It can't work when the string is empty or it won't set the right background.
7851 can_clear(p)
7852 char_u *p;
7854 return (*p != NUL && (t_colors <= 1
7855 #ifdef FEAT_GUI
7856 || gui.in_use
7857 #endif
7858 || cterm_normal_bg_color == 0 || *T_UT != NUL));
7862 * Reset cursor position. Use whenever cursor was moved because of outputting
7863 * something directly to the screen (shell commands) or a terminal control
7864 * code.
7866 void
7867 screen_start()
7869 screen_cur_row = screen_cur_col = 9999;
7873 * Move the cursor to position "row","col" in the screen.
7874 * This tries to find the most efficient way to move, minimizing the number of
7875 * characters sent to the terminal.
7877 void
7878 windgoto(row, col)
7879 int row;
7880 int col;
7882 sattr_T *p;
7883 int i;
7884 int plan;
7885 int cost;
7886 int wouldbe_col;
7887 int noinvcurs;
7888 char_u *bs;
7889 int goto_cost;
7890 int attr;
7892 #define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
7893 #define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
7895 #define PLAN_LE 1
7896 #define PLAN_CR 2
7897 #define PLAN_NL 3
7898 #define PLAN_WRITE 4
7899 /* Can't use ScreenLines unless initialized */
7900 if (ScreenLines == NULL)
7901 return;
7903 if (col != screen_cur_col || row != screen_cur_row)
7905 /* Check for valid position. */
7906 if (row < 0) /* window without text lines? */
7907 row = 0;
7908 if (row >= screen_Rows)
7909 row = screen_Rows - 1;
7910 if (col >= screen_Columns)
7911 col = screen_Columns - 1;
7913 /* check if no cursor movement is allowed in highlight mode */
7914 if (screen_attr && *T_MS == NUL)
7915 noinvcurs = HIGHL_COST;
7916 else
7917 noinvcurs = 0;
7918 goto_cost = GOTO_COST + noinvcurs;
7921 * Plan how to do the positioning:
7922 * 1. Use CR to move it to column 0, same row.
7923 * 2. Use T_LE to move it a few columns to the left.
7924 * 3. Use NL to move a few lines down, column 0.
7925 * 4. Move a few columns to the right with T_ND or by writing chars.
7927 * Don't do this if the cursor went beyond the last column, the cursor
7928 * position is unknown then (some terminals wrap, some don't )
7930 * First check if the highlighting attributes allow us to write
7931 * characters to move the cursor to the right.
7933 if (row >= screen_cur_row && screen_cur_col < Columns)
7936 * If the cursor is in the same row, bigger col, we can use CR
7937 * or T_LE.
7939 bs = NULL; /* init for GCC */
7940 attr = screen_attr;
7941 if (row == screen_cur_row && col < screen_cur_col)
7943 /* "le" is preferred over "bc", because "bc" is obsolete */
7944 if (*T_LE)
7945 bs = T_LE; /* "cursor left" */
7946 else
7947 bs = T_BC; /* "backspace character (old) */
7948 if (*bs)
7949 cost = (screen_cur_col - col) * (int)STRLEN(bs);
7950 else
7951 cost = 999;
7952 if (col + 1 < cost) /* using CR is less characters */
7954 plan = PLAN_CR;
7955 wouldbe_col = 0;
7956 cost = 1; /* CR is just one character */
7958 else
7960 plan = PLAN_LE;
7961 wouldbe_col = col;
7963 if (noinvcurs) /* will stop highlighting */
7965 cost += noinvcurs;
7966 attr = 0;
7971 * If the cursor is above where we want to be, we can use CR LF.
7973 else if (row > screen_cur_row)
7975 plan = PLAN_NL;
7976 wouldbe_col = 0;
7977 cost = (row - screen_cur_row) * 2; /* CR LF */
7978 if (noinvcurs) /* will stop highlighting */
7980 cost += noinvcurs;
7981 attr = 0;
7986 * If the cursor is in the same row, smaller col, just use write.
7988 else
7990 plan = PLAN_WRITE;
7991 wouldbe_col = screen_cur_col;
7992 cost = 0;
7996 * Check if any characters that need to be written have the
7997 * correct attributes. Also avoid UTF-8 characters.
7999 i = col - wouldbe_col;
8000 if (i > 0)
8001 cost += i;
8002 if (cost < goto_cost && i > 0)
8005 * Check if the attributes are correct without additionally
8006 * stopping highlighting.
8008 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
8009 while (i && *p++ == attr)
8010 --i;
8011 if (i != 0)
8014 * Try if it works when highlighting is stopped here.
8016 if (*--p == 0)
8018 cost += noinvcurs;
8019 while (i && *p++ == 0)
8020 --i;
8022 if (i != 0)
8023 cost = 999; /* different attributes, don't do it */
8025 #ifdef FEAT_MBYTE
8026 if (enc_utf8)
8028 /* Don't use an UTF-8 char for positioning, it's slow. */
8029 for (i = wouldbe_col; i < col; ++i)
8030 if (ScreenLinesUC[LineOffset[row] + i] != 0)
8032 cost = 999;
8033 break;
8036 #endif
8040 * We can do it without term_windgoto()!
8042 if (cost < goto_cost)
8044 if (plan == PLAN_LE)
8046 if (noinvcurs)
8047 screen_stop_highlight();
8048 while (screen_cur_col > col)
8050 out_str(bs);
8051 --screen_cur_col;
8054 else if (plan == PLAN_CR)
8056 if (noinvcurs)
8057 screen_stop_highlight();
8058 out_char('\r');
8059 screen_cur_col = 0;
8061 else if (plan == PLAN_NL)
8063 if (noinvcurs)
8064 screen_stop_highlight();
8065 while (screen_cur_row < row)
8067 out_char('\n');
8068 ++screen_cur_row;
8070 screen_cur_col = 0;
8073 i = col - screen_cur_col;
8074 if (i > 0)
8077 * Use cursor-right if it's one character only. Avoids
8078 * removing a line of pixels from the last bold char, when
8079 * using the bold trick in the GUI.
8081 if (T_ND[0] != NUL && T_ND[1] == NUL)
8083 while (i-- > 0)
8084 out_char(*T_ND);
8086 else
8088 int off;
8090 off = LineOffset[row] + screen_cur_col;
8091 while (i-- > 0)
8093 if (ScreenAttrs[off] != screen_attr)
8094 screen_stop_highlight();
8095 #ifdef FEAT_MBYTE
8096 out_flush_check();
8097 #endif
8098 out_char(ScreenLines[off]);
8099 #ifdef FEAT_MBYTE
8100 if (enc_dbcs == DBCS_JPNU
8101 && ScreenLines[off] == 0x8e)
8102 out_char(ScreenLines2[off]);
8103 #endif
8104 ++off;
8110 else
8111 cost = 999;
8113 if (cost >= goto_cost)
8115 if (noinvcurs)
8116 screen_stop_highlight();
8117 if (row == screen_cur_row && (col > screen_cur_col) &&
8118 *T_CRI != NUL)
8119 term_cursor_right(col - screen_cur_col);
8120 else
8121 term_windgoto(row, col);
8123 screen_cur_row = row;
8124 screen_cur_col = col;
8129 * Set cursor to its position in the current window.
8131 void
8132 setcursor()
8134 if (redrawing())
8136 validate_cursor();
8137 windgoto(W_WINROW(curwin) + curwin->w_wrow,
8138 W_WINCOL(curwin) + (
8139 #ifdef FEAT_RIGHTLEFT
8140 /* With 'rightleft' set and the cursor on a double-wide
8141 * character, position it on the leftmost column. */
8142 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
8143 # ifdef FEAT_MBYTE
8144 (has_mbyte
8145 && (*mb_ptr2cells)(ml_get_cursor()) == 2
8146 && vim_isprintc(gchar_cursor())) ? 2 :
8147 # endif
8148 1)) :
8149 #endif
8150 curwin->w_wcol));
8156 * insert 'line_count' lines at 'row' in window 'wp'
8157 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
8158 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
8159 * scrolling.
8160 * Returns FAIL if the lines are not inserted, OK for success.
8163 win_ins_lines(wp, row, line_count, invalid, mayclear)
8164 win_T *wp;
8165 int row;
8166 int line_count;
8167 int invalid;
8168 int mayclear;
8170 int did_delete;
8171 int nextrow;
8172 int lastrow;
8173 int retval;
8175 if (invalid)
8176 wp->w_lines_valid = 0;
8178 if (wp->w_height < 5)
8179 return FAIL;
8181 if (line_count > wp->w_height - row)
8182 line_count = wp->w_height - row;
8184 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
8185 if (retval != MAYBE)
8186 return retval;
8189 * If there is a next window or a status line, we first try to delete the
8190 * lines at the bottom to avoid messing what is after the window.
8191 * If this fails and there are following windows, don't do anything to avoid
8192 * messing up those windows, better just redraw.
8194 did_delete = FALSE;
8195 #ifdef FEAT_WINDOWS
8196 if (wp->w_next != NULL || wp->w_status_height)
8198 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8199 line_count, (int)Rows, FALSE, NULL) == OK)
8200 did_delete = TRUE;
8201 else if (wp->w_next)
8202 return FAIL;
8204 #endif
8206 * if no lines deleted, blank the lines that will end up below the window
8208 if (!did_delete)
8210 #ifdef FEAT_WINDOWS
8211 wp->w_redr_status = TRUE;
8212 #endif
8213 redraw_cmdline = TRUE;
8214 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
8215 lastrow = nextrow + line_count;
8216 if (lastrow > Rows)
8217 lastrow = Rows;
8218 screen_fill(nextrow - line_count, lastrow - line_count,
8219 W_WINCOL(wp), (int)W_ENDCOL(wp),
8220 ' ', ' ', 0);
8223 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
8224 == FAIL)
8226 /* deletion will have messed up other windows */
8227 if (did_delete)
8229 #ifdef FEAT_WINDOWS
8230 wp->w_redr_status = TRUE;
8231 #endif
8232 win_rest_invalid(W_NEXT(wp));
8234 return FAIL;
8237 return OK;
8241 * delete "line_count" window lines at "row" in window "wp"
8242 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
8243 * If "mayclear" is TRUE the screen will be cleared if it is faster than
8244 * scrolling
8245 * Return OK for success, FAIL if the lines are not deleted.
8248 win_del_lines(wp, row, line_count, invalid, mayclear)
8249 win_T *wp;
8250 int row;
8251 int line_count;
8252 int invalid;
8253 int mayclear;
8255 int retval;
8257 if (invalid)
8258 wp->w_lines_valid = 0;
8260 if (line_count > wp->w_height - row)
8261 line_count = wp->w_height - row;
8263 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
8264 if (retval != MAYBE)
8265 return retval;
8267 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
8268 (int)Rows, FALSE, NULL) == FAIL)
8269 return FAIL;
8271 #ifdef FEAT_WINDOWS
8273 * If there are windows or status lines below, try to put them at the
8274 * correct place. If we can't do that, they have to be redrawn.
8276 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
8278 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8279 line_count, (int)Rows, NULL) == FAIL)
8281 wp->w_redr_status = TRUE;
8282 win_rest_invalid(wp->w_next);
8286 * If this is the last window and there is no status line, redraw the
8287 * command line later.
8289 else
8290 #endif
8291 redraw_cmdline = TRUE;
8292 return OK;
8296 * Common code for win_ins_lines() and win_del_lines().
8297 * Returns OK or FAIL when the work has been done.
8298 * Returns MAYBE when not finished yet.
8300 static int
8301 win_do_lines(wp, row, line_count, mayclear, del)
8302 win_T *wp;
8303 int row;
8304 int line_count;
8305 int mayclear;
8306 int del;
8308 int retval;
8310 if (!redrawing() || line_count <= 0)
8311 return FAIL;
8313 /* only a few lines left: redraw is faster */
8314 if (mayclear && Rows - line_count < 5
8315 #ifdef FEAT_VERTSPLIT
8316 && wp->w_width == Columns
8317 #endif
8320 screenclear(); /* will set wp->w_lines_valid to 0 */
8321 return FAIL;
8325 * Delete all remaining lines
8327 if (row + line_count >= wp->w_height)
8329 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
8330 W_WINCOL(wp), (int)W_ENDCOL(wp),
8331 ' ', ' ', 0);
8332 return OK;
8336 * when scrolling, the message on the command line should be cleared,
8337 * otherwise it will stay there forever.
8339 clear_cmdline = TRUE;
8342 * If the terminal can set a scroll region, use that.
8343 * Always do this in a vertically split window. This will redraw from
8344 * ScreenLines[] when t_CV isn't defined. That's faster than using
8345 * win_line().
8346 * Don't use a scroll region when we are going to redraw the text, writing
8347 * a character in the lower right corner of the scroll region causes a
8348 * scroll-up in the DJGPP version.
8350 if (scroll_region
8351 #ifdef FEAT_VERTSPLIT
8352 || W_WIDTH(wp) != Columns
8353 #endif
8356 #ifdef FEAT_VERTSPLIT
8357 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8358 #endif
8359 scroll_region_set(wp, row);
8360 if (del)
8361 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
8362 wp->w_height - row, FALSE, wp);
8363 else
8364 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
8365 wp->w_height - row, wp);
8366 #ifdef FEAT_VERTSPLIT
8367 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8368 #endif
8369 scroll_region_reset();
8370 return retval;
8373 #ifdef FEAT_WINDOWS
8374 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
8375 return FAIL;
8376 #endif
8378 return MAYBE;
8382 * window 'wp' and everything after it is messed up, mark it for redraw
8384 static void
8385 win_rest_invalid(wp)
8386 win_T *wp;
8388 #ifdef FEAT_WINDOWS
8389 while (wp != NULL)
8390 #else
8391 if (wp != NULL)
8392 #endif
8394 redraw_win_later(wp, NOT_VALID);
8395 #ifdef FEAT_WINDOWS
8396 wp->w_redr_status = TRUE;
8397 wp = wp->w_next;
8398 #endif
8400 redraw_cmdline = TRUE;
8404 * The rest of the routines in this file perform screen manipulations. The
8405 * given operation is performed physically on the screen. The corresponding
8406 * change is also made to the internal screen image. In this way, the editor
8407 * anticipates the effect of editing changes on the appearance of the screen.
8408 * That way, when we call screenupdate a complete redraw isn't usually
8409 * necessary. Another advantage is that we can keep adding code to anticipate
8410 * screen changes, and in the meantime, everything still works.
8414 * types for inserting or deleting lines
8416 #define USE_T_CAL 1
8417 #define USE_T_CDL 2
8418 #define USE_T_AL 3
8419 #define USE_T_CE 4
8420 #define USE_T_DL 5
8421 #define USE_T_SR 6
8422 #define USE_NL 7
8423 #define USE_T_CD 8
8424 #define USE_REDRAW 9
8427 * insert lines on the screen and update ScreenLines[]
8428 * 'end' is the line after the scrolled part. Normally it is Rows.
8429 * When scrolling region used 'off' is the offset from the top for the region.
8430 * 'row' and 'end' are relative to the start of the region.
8432 * return FAIL for failure, OK for success.
8435 screen_ins_lines(off, row, line_count, end, wp)
8436 int off;
8437 int row;
8438 int line_count;
8439 int end;
8440 win_T *wp; /* NULL or window to use width from */
8442 int i;
8443 int j;
8444 unsigned temp;
8445 int cursor_row;
8446 int type;
8447 int result_empty;
8448 int can_ce = can_clear(T_CE);
8451 * FAIL if
8452 * - there is no valid screen
8453 * - the screen has to be redrawn completely
8454 * - the line count is less than one
8455 * - the line count is more than 'ttyscroll'
8457 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
8458 return FAIL;
8461 * There are seven ways to insert lines:
8462 * 0. When in a vertically split window and t_CV isn't set, redraw the
8463 * characters from ScreenLines[].
8464 * 1. Use T_CD (clear to end of display) if it exists and the result of
8465 * the insert is just empty lines
8466 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
8467 * present or line_count > 1. It looks better if we do all the inserts
8468 * at once.
8469 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
8470 * insert is just empty lines and T_CE is not present or line_count >
8471 * 1.
8472 * 4. Use T_AL (insert line) if it exists.
8473 * 5. Use T_CE (erase line) if it exists and the result of the insert is
8474 * just empty lines.
8475 * 6. Use T_DL (delete line) if it exists and the result of the insert is
8476 * just empty lines.
8477 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
8478 * the 'da' flag is not set or we have clear line capability.
8479 * 8. redraw the characters from ScreenLines[].
8481 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
8482 * the scrollbar for the window. It does have insert line, use that if it
8483 * exists.
8485 result_empty = (row + line_count >= end);
8486 #ifdef FEAT_VERTSPLIT
8487 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
8488 type = USE_REDRAW;
8489 else
8490 #endif
8491 if (can_clear(T_CD) && result_empty)
8492 type = USE_T_CD;
8493 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
8494 type = USE_T_CAL;
8495 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
8496 type = USE_T_CDL;
8497 else if (*T_AL != NUL)
8498 type = USE_T_AL;
8499 else if (can_ce && result_empty)
8500 type = USE_T_CE;
8501 else if (*T_DL != NUL && result_empty)
8502 type = USE_T_DL;
8503 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
8504 type = USE_T_SR;
8505 else
8506 return FAIL;
8509 * For clearing the lines screen_del_lines() is used. This will also take
8510 * care of t_db if necessary.
8512 if (type == USE_T_CD || type == USE_T_CDL ||
8513 type == USE_T_CE || type == USE_T_DL)
8514 return screen_del_lines(off, row, line_count, end, FALSE, wp);
8517 * If text is retained below the screen, first clear or delete as many
8518 * lines at the bottom of the window as are about to be inserted so that
8519 * the deleted lines won't later surface during a screen_del_lines.
8521 if (*T_DB)
8522 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
8524 #ifdef FEAT_CLIPBOARD
8525 /* Remove a modeless selection when inserting lines halfway the screen
8526 * or not the full width of the screen. */
8527 if (off + row > 0
8528 # ifdef FEAT_VERTSPLIT
8529 || (wp != NULL && wp->w_width != Columns)
8530 # endif
8532 clip_clear_selection();
8533 else
8534 clip_scroll_selection(-line_count);
8535 #endif
8537 #ifdef FEAT_GUI
8538 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
8539 * scrolling is actually carried out. */
8540 gui_dont_update_cursor();
8541 #endif
8543 if (*T_CCS != NUL) /* cursor relative to region */
8544 cursor_row = row;
8545 else
8546 cursor_row = row + off;
8549 * Shift LineOffset[] line_count down to reflect the inserted lines.
8550 * Clear the inserted lines in ScreenLines[].
8552 row += off;
8553 end += off;
8554 for (i = 0; i < line_count; ++i)
8556 #ifdef FEAT_VERTSPLIT
8557 if (wp != NULL && wp->w_width != Columns)
8559 /* need to copy part of a line */
8560 j = end - 1 - i;
8561 while ((j -= line_count) >= row)
8562 linecopy(j + line_count, j, wp);
8563 j += line_count;
8564 if (can_clear((char_u *)" "))
8565 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
8566 else
8567 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
8568 LineWraps[j] = FALSE;
8570 else
8571 #endif
8573 j = end - 1 - i;
8574 temp = LineOffset[j];
8575 while ((j -= line_count) >= row)
8577 LineOffset[j + line_count] = LineOffset[j];
8578 LineWraps[j + line_count] = LineWraps[j];
8580 LineOffset[j + line_count] = temp;
8581 LineWraps[j + line_count] = FALSE;
8582 if (can_clear((char_u *)" "))
8583 lineclear(temp, (int)Columns);
8584 else
8585 lineinvalid(temp, (int)Columns);
8589 screen_stop_highlight();
8590 windgoto(cursor_row, 0);
8592 #ifdef FEAT_VERTSPLIT
8593 /* redraw the characters */
8594 if (type == USE_REDRAW)
8595 redraw_block(row, end, wp);
8596 else
8597 #endif
8598 if (type == USE_T_CAL)
8600 term_append_lines(line_count);
8601 screen_start(); /* don't know where cursor is now */
8603 else
8605 for (i = 0; i < line_count; i++)
8607 if (type == USE_T_AL)
8609 if (i && cursor_row != 0)
8610 windgoto(cursor_row, 0);
8611 out_str(T_AL);
8613 else /* type == USE_T_SR */
8614 out_str(T_SR);
8615 screen_start(); /* don't know where cursor is now */
8620 * With scroll-reverse and 'da' flag set we need to clear the lines that
8621 * have been scrolled down into the region.
8623 if (type == USE_T_SR && *T_DA)
8625 for (i = 0; i < line_count; ++i)
8627 windgoto(off + i, 0);
8628 out_str(T_CE);
8629 screen_start(); /* don't know where cursor is now */
8633 #ifdef FEAT_GUI
8634 gui_can_update_cursor();
8635 if (gui.in_use)
8636 out_flush(); /* always flush after a scroll */
8637 #endif
8638 return OK;
8642 * delete lines on the screen and update ScreenLines[]
8643 * 'end' is the line after the scrolled part. Normally it is Rows.
8644 * When scrolling region used 'off' is the offset from the top for the region.
8645 * 'row' and 'end' are relative to the start of the region.
8647 * Return OK for success, FAIL if the lines are not deleted.
8649 /*ARGSUSED*/
8651 screen_del_lines(off, row, line_count, end, force, wp)
8652 int off;
8653 int row;
8654 int line_count;
8655 int end;
8656 int force; /* even when line_count > p_ttyscroll */
8657 win_T *wp; /* NULL or window to use width from */
8659 int j;
8660 int i;
8661 unsigned temp;
8662 int cursor_row;
8663 int cursor_end;
8664 int result_empty; /* result is empty until end of region */
8665 int can_delete; /* deleting line codes can be used */
8666 int type;
8669 * FAIL if
8670 * - there is no valid screen
8671 * - the screen has to be redrawn completely
8672 * - the line count is less than one
8673 * - the line count is more than 'ttyscroll'
8675 if (!screen_valid(TRUE) || line_count <= 0 ||
8676 (!force && line_count > p_ttyscroll))
8677 return FAIL;
8680 * Check if the rest of the current region will become empty.
8682 result_empty = row + line_count >= end;
8685 * We can delete lines only when 'db' flag not set or when 'ce' option
8686 * available.
8688 can_delete = (*T_DB == NUL || can_clear(T_CE));
8691 * There are six ways to delete lines:
8692 * 0. When in a vertically split window and t_CV isn't set, redraw the
8693 * characters from ScreenLines[].
8694 * 1. Use T_CD if it exists and the result is empty.
8695 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
8696 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
8697 * none of the other ways work.
8698 * 4. Use T_CE (erase line) if the result is empty.
8699 * 5. Use T_DL (delete line) if it exists.
8700 * 6. redraw the characters from ScreenLines[].
8702 #ifdef FEAT_VERTSPLIT
8703 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
8704 type = USE_REDRAW;
8705 else
8706 #endif
8707 if (can_clear(T_CD) && result_empty)
8708 type = USE_T_CD;
8709 #if defined(__BEOS__) && defined(BEOS_DR8)
8711 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
8712 * its internal termcap... this works okay for tests which test *T_DB !=
8713 * NUL. It has the disadvantage that the user cannot use any :set t_*
8714 * command to get T_DB (back) to empty_option, only :set term=... will do
8715 * the trick...
8716 * Anyway, this hack will hopefully go away with the next OS release.
8717 * (Olaf Seibert)
8719 else if (row == 0 && T_DB == empty_option
8720 && (line_count == 1 || *T_CDL == NUL))
8721 #else
8722 else if (row == 0 && (
8723 #ifndef AMIGA
8724 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
8725 * up, so use delete-line command */
8726 line_count == 1 ||
8727 #endif
8728 *T_CDL == NUL))
8729 #endif
8730 type = USE_NL;
8731 else if (*T_CDL != NUL && line_count > 1 && can_delete)
8732 type = USE_T_CDL;
8733 else if (can_clear(T_CE) && result_empty
8734 #ifdef FEAT_VERTSPLIT
8735 && (wp == NULL || wp->w_width == Columns)
8736 #endif
8738 type = USE_T_CE;
8739 else if (*T_DL != NUL && can_delete)
8740 type = USE_T_DL;
8741 else if (*T_CDL != NUL && can_delete)
8742 type = USE_T_CDL;
8743 else
8744 return FAIL;
8746 #ifdef FEAT_CLIPBOARD
8747 /* Remove a modeless selection when deleting lines halfway the screen or
8748 * not the full width of the screen. */
8749 if (off + row > 0
8750 # ifdef FEAT_VERTSPLIT
8751 || (wp != NULL && wp->w_width != Columns)
8752 # endif
8754 clip_clear_selection();
8755 else
8756 clip_scroll_selection(line_count);
8757 #endif
8759 #ifdef FEAT_GUI
8760 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
8761 * scrolling is actually carried out. */
8762 gui_dont_update_cursor();
8763 #endif
8765 if (*T_CCS != NUL) /* cursor relative to region */
8767 cursor_row = row;
8768 cursor_end = end;
8770 else
8772 cursor_row = row + off;
8773 cursor_end = end + off;
8777 * Now shift LineOffset[] line_count up to reflect the deleted lines.
8778 * Clear the inserted lines in ScreenLines[].
8780 row += off;
8781 end += off;
8782 for (i = 0; i < line_count; ++i)
8784 #ifdef FEAT_VERTSPLIT
8785 if (wp != NULL && wp->w_width != Columns)
8787 /* need to copy part of a line */
8788 j = row + i;
8789 while ((j += line_count) <= end - 1)
8790 linecopy(j - line_count, j, wp);
8791 j -= line_count;
8792 if (can_clear((char_u *)" "))
8793 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
8794 else
8795 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
8796 LineWraps[j] = FALSE;
8798 else
8799 #endif
8801 /* whole width, moving the line pointers is faster */
8802 j = row + i;
8803 temp = LineOffset[j];
8804 while ((j += line_count) <= end - 1)
8806 LineOffset[j - line_count] = LineOffset[j];
8807 LineWraps[j - line_count] = LineWraps[j];
8809 LineOffset[j - line_count] = temp;
8810 LineWraps[j - line_count] = FALSE;
8811 if (can_clear((char_u *)" "))
8812 lineclear(temp, (int)Columns);
8813 else
8814 lineinvalid(temp, (int)Columns);
8818 screen_stop_highlight();
8820 #ifdef FEAT_VERTSPLIT
8821 /* redraw the characters */
8822 if (type == USE_REDRAW)
8823 redraw_block(row, end, wp);
8824 else
8825 #endif
8826 if (type == USE_T_CD) /* delete the lines */
8828 windgoto(cursor_row, 0);
8829 out_str(T_CD);
8830 screen_start(); /* don't know where cursor is now */
8832 else if (type == USE_T_CDL)
8834 windgoto(cursor_row, 0);
8835 term_delete_lines(line_count);
8836 screen_start(); /* don't know where cursor is now */
8839 * Deleting lines at top of the screen or scroll region: Just scroll
8840 * the whole screen (scroll region) up by outputting newlines on the
8841 * last line.
8843 else if (type == USE_NL)
8845 windgoto(cursor_end - 1, 0);
8846 for (i = line_count; --i >= 0; )
8847 out_char('\n'); /* cursor will remain on same line */
8849 else
8851 for (i = line_count; --i >= 0; )
8853 if (type == USE_T_DL)
8855 windgoto(cursor_row, 0);
8856 out_str(T_DL); /* delete a line */
8858 else /* type == USE_T_CE */
8860 windgoto(cursor_row + i, 0);
8861 out_str(T_CE); /* erase a line */
8863 screen_start(); /* don't know where cursor is now */
8868 * If the 'db' flag is set, we need to clear the lines that have been
8869 * scrolled up at the bottom of the region.
8871 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
8873 for (i = line_count; i > 0; --i)
8875 windgoto(cursor_end - i, 0);
8876 out_str(T_CE); /* erase a line */
8877 screen_start(); /* don't know where cursor is now */
8881 #ifdef FEAT_GUI
8882 gui_can_update_cursor();
8883 if (gui.in_use)
8884 out_flush(); /* always flush after a scroll */
8885 #endif
8887 return OK;
8891 * show the current mode and ruler
8893 * If clear_cmdline is TRUE, clear the rest of the cmdline.
8894 * If clear_cmdline is FALSE there may be a message there that needs to be
8895 * cleared only if a mode is shown.
8896 * Return the length of the message (0 if no message).
8899 showmode()
8901 int need_clear;
8902 int length = 0;
8903 int do_mode;
8904 int attr;
8905 int nwr_save;
8906 #ifdef FEAT_INS_EXPAND
8907 int sub_attr;
8908 #endif
8910 do_mode = ((p_smd && msg_silent == 0)
8911 && ((State & INSERT)
8912 || restart_edit
8913 #ifdef FEAT_VISUAL
8914 || VIsual_active
8915 #endif
8917 if (do_mode || Recording)
8920 * Don't show mode right now, when not redrawing or inside a mapping.
8921 * Call char_avail() only when we are going to show something, because
8922 * it takes a bit of time.
8924 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
8926 redraw_cmdline = TRUE; /* show mode later */
8927 return 0;
8930 nwr_save = need_wait_return;
8932 /* wait a bit before overwriting an important message */
8933 check_for_delay(FALSE);
8935 /* if the cmdline is more than one line high, erase top lines */
8936 need_clear = clear_cmdline;
8937 if (clear_cmdline && cmdline_row < Rows - 1)
8938 msg_clr_cmdline(); /* will reset clear_cmdline */
8940 /* Position on the last line in the window, column 0 */
8941 msg_pos_mode();
8942 cursor_off();
8943 attr = hl_attr(HLF_CM); /* Highlight mode */
8944 if (do_mode)
8946 MSG_PUTS_ATTR("--", attr);
8947 #if defined(FEAT_XIM)
8948 # if 0 /* old version, changed by SungHyun Nam July 2008 */
8949 if (xic != NULL && im_get_status() && !p_imdisable
8950 && curbuf->b_p_iminsert == B_IMODE_IM)
8951 # else
8952 if (
8953 # ifdef HAVE_GTK2
8954 preedit_get_status()
8955 # else
8956 im_get_status()
8957 # endif
8959 # endif
8960 # ifdef HAVE_GTK2 /* most of the time, it's not XIM being used */
8961 MSG_PUTS_ATTR(" IM", attr);
8962 # else
8963 MSG_PUTS_ATTR(" XIM", attr);
8964 # endif
8965 #endif
8966 #if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
8967 if (gui.in_use)
8969 if (hangul_input_state_get())
8970 MSG_PUTS_ATTR(" \307\321\261\333", attr); /* HANGUL */
8972 #endif
8973 #ifdef FEAT_INS_EXPAND
8974 if (edit_submode != NULL) /* CTRL-X in Insert mode */
8976 /* These messages can get long, avoid a wrap in a narrow
8977 * window. Prefer showing edit_submode_extra. */
8978 length = (Rows - msg_row) * Columns - 3;
8979 if (edit_submode_extra != NULL)
8980 length -= vim_strsize(edit_submode_extra);
8981 if (length > 0)
8983 if (edit_submode_pre != NULL)
8984 length -= vim_strsize(edit_submode_pre);
8985 if (length - vim_strsize(edit_submode) > 0)
8987 if (edit_submode_pre != NULL)
8988 msg_puts_attr(edit_submode_pre, attr);
8989 msg_puts_attr(edit_submode, attr);
8991 if (edit_submode_extra != NULL)
8993 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
8994 if ((int)edit_submode_highl < (int)HLF_COUNT)
8995 sub_attr = hl_attr(edit_submode_highl);
8996 else
8997 sub_attr = attr;
8998 msg_puts_attr(edit_submode_extra, sub_attr);
9001 length = 0;
9003 else
9004 #endif
9006 #ifdef FEAT_VREPLACE
9007 if (State & VREPLACE_FLAG)
9008 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
9009 else
9010 #endif
9011 if (State & REPLACE_FLAG)
9012 MSG_PUTS_ATTR(_(" REPLACE"), attr);
9013 else if (State & INSERT)
9015 #ifdef FEAT_RIGHTLEFT
9016 if (p_ri)
9017 MSG_PUTS_ATTR(_(" REVERSE"), attr);
9018 #endif
9019 MSG_PUTS_ATTR(_(" INSERT"), attr);
9021 else if (restart_edit == 'I')
9022 MSG_PUTS_ATTR(_(" (insert)"), attr);
9023 else if (restart_edit == 'R')
9024 MSG_PUTS_ATTR(_(" (replace)"), attr);
9025 else if (restart_edit == 'V')
9026 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
9027 #ifdef FEAT_RIGHTLEFT
9028 if (p_hkmap)
9029 MSG_PUTS_ATTR(_(" Hebrew"), attr);
9030 # ifdef FEAT_FKMAP
9031 if (p_fkmap)
9032 MSG_PUTS_ATTR(farsi_text_5, attr);
9033 # endif
9034 #endif
9035 #ifdef FEAT_KEYMAP
9036 if (State & LANGMAP)
9038 # ifdef FEAT_ARABIC
9039 if (curwin->w_p_arab)
9040 MSG_PUTS_ATTR(_(" Arabic"), attr);
9041 else
9042 # endif
9043 MSG_PUTS_ATTR(_(" (lang)"), attr);
9045 #endif
9046 if ((State & INSERT) && p_paste)
9047 MSG_PUTS_ATTR(_(" (paste)"), attr);
9049 #ifdef FEAT_VISUAL
9050 if (VIsual_active)
9052 char *p;
9054 /* Don't concatenate separate words to avoid translation
9055 * problems. */
9056 switch ((VIsual_select ? 4 : 0)
9057 + (VIsual_mode == Ctrl_V) * 2
9058 + (VIsual_mode == 'V'))
9060 case 0: p = N_(" VISUAL"); break;
9061 case 1: p = N_(" VISUAL LINE"); break;
9062 case 2: p = N_(" VISUAL BLOCK"); break;
9063 case 4: p = N_(" SELECT"); break;
9064 case 5: p = N_(" SELECT LINE"); break;
9065 default: p = N_(" SELECT BLOCK"); break;
9067 MSG_PUTS_ATTR(_(p), attr);
9069 #endif
9070 MSG_PUTS_ATTR(" --", attr);
9073 need_clear = TRUE;
9075 if (Recording
9076 #ifdef FEAT_INS_EXPAND
9077 && edit_submode == NULL /* otherwise it gets too long */
9078 #endif
9081 MSG_PUTS_ATTR(_("recording"), attr);
9082 need_clear = TRUE;
9085 mode_displayed = TRUE;
9086 if (need_clear || clear_cmdline)
9087 msg_clr_eos();
9088 msg_didout = FALSE; /* overwrite this message */
9089 length = msg_col;
9090 msg_col = 0;
9091 need_wait_return = nwr_save; /* never ask for hit-return for this */
9093 else if (clear_cmdline && msg_silent == 0)
9094 /* Clear the whole command line. Will reset "clear_cmdline". */
9095 msg_clr_cmdline();
9097 #ifdef FEAT_CMDL_INFO
9098 # ifdef FEAT_VISUAL
9099 /* In Visual mode the size of the selected area must be redrawn. */
9100 if (VIsual_active)
9101 clear_showcmd();
9102 # endif
9104 /* If the last window has no status line, the ruler is after the mode
9105 * message and must be redrawn */
9106 if (redrawing()
9107 # ifdef FEAT_WINDOWS
9108 && lastwin->w_status_height == 0
9109 # endif
9111 win_redr_ruler(lastwin, TRUE);
9112 #endif
9113 redraw_cmdline = FALSE;
9114 clear_cmdline = FALSE;
9116 return length;
9120 * Position for a mode message.
9122 static void
9123 msg_pos_mode()
9125 msg_col = 0;
9126 msg_row = Rows - 1;
9130 * Delete mode message. Used when ESC is typed which is expected to end
9131 * Insert mode (but Insert mode didn't end yet!).
9132 * Caller should check "mode_displayed".
9134 void
9135 unshowmode(force)
9136 int force;
9139 * Don't delete it right now, when not redrawing or insided a mapping.
9141 if (!redrawing() || (!force && char_avail() && !KeyTyped))
9142 redraw_cmdline = TRUE; /* delete mode later */
9143 else
9145 msg_pos_mode();
9146 if (Recording)
9147 MSG_PUTS_ATTR(_("recording"), hl_attr(HLF_CM));
9148 msg_clr_eos();
9152 #if defined(FEAT_WINDOWS)
9154 * Draw the tab pages line at the top of the Vim window.
9156 static void
9157 draw_tabline()
9159 int tabcount = 0;
9160 tabpage_T *tp;
9161 int tabwidth;
9162 int col = 0;
9163 int scol = 0;
9164 int attr;
9165 win_T *wp;
9166 win_T *cwp;
9167 int wincount;
9168 int modified;
9169 int c;
9170 int len;
9171 int attr_sel = hl_attr(HLF_TPS);
9172 int attr_nosel = hl_attr(HLF_TP);
9173 int attr_fill = hl_attr(HLF_TPF);
9174 char_u *p;
9175 int room;
9176 int use_sep_chars = (t_colors < 8
9177 #ifdef FEAT_GUI
9178 && !gui.in_use
9179 #endif
9182 redraw_tabline = FALSE;
9184 #ifdef FEAT_GUI_TABLINE
9185 /* Take care of a GUI tabline. */
9186 if (gui_use_tabline())
9188 gui_update_tabline();
9189 return;
9191 #endif
9193 if (tabline_height() < 1)
9194 return;
9196 #if defined(FEAT_STL_OPT)
9198 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
9199 for (scol = 0; scol < Columns; ++scol)
9200 TabPageIdxs[scol] = 0;
9202 /* Use the 'tabline' option if it's set. */
9203 if (*p_tal != NUL)
9205 int save_called_emsg = called_emsg;
9207 /* Check for an error. If there is one we would loop in redrawing the
9208 * screen. Avoid that by making 'tabline' empty. */
9209 called_emsg = FALSE;
9210 win_redr_custom(NULL, FALSE);
9211 if (called_emsg)
9212 set_string_option_direct((char_u *)"tabline", -1,
9213 (char_u *)"", OPT_FREE, SID_ERROR);
9214 called_emsg |= save_called_emsg;
9216 else
9217 #endif
9219 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
9220 ++tabcount;
9222 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
9223 if (tabwidth < 6)
9224 tabwidth = 6;
9226 attr = attr_nosel;
9227 tabcount = 0;
9228 scol = 0;
9229 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
9230 tp = tp->tp_next)
9232 scol = col;
9234 if (tp->tp_topframe == topframe)
9235 attr = attr_sel;
9236 if (use_sep_chars && col > 0)
9237 screen_putchar('|', 0, col++, attr);
9239 if (tp->tp_topframe != topframe)
9240 attr = attr_nosel;
9242 screen_putchar(' ', 0, col++, attr);
9244 if (tp == curtab)
9246 cwp = curwin;
9247 wp = firstwin;
9249 else
9251 cwp = tp->tp_curwin;
9252 wp = tp->tp_firstwin;
9255 modified = FALSE;
9256 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
9257 if (bufIsChanged(wp->w_buffer))
9258 modified = TRUE;
9259 if (modified || wincount > 1)
9261 if (wincount > 1)
9263 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
9264 len = (int)STRLEN(NameBuff);
9265 if (col + len >= Columns - 3)
9266 break;
9267 screen_puts_len(NameBuff, len, 0, col,
9268 #if defined(FEAT_SYN_HL)
9269 hl_combine_attr(attr, hl_attr(HLF_T))
9270 #else
9271 attr
9272 #endif
9274 col += len;
9276 if (modified)
9277 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
9278 screen_putchar(' ', 0, col++, attr);
9281 room = scol - col + tabwidth - 1;
9282 if (room > 0)
9284 /* Get buffer name in NameBuff[] */
9285 get_trans_bufname(cwp->w_buffer);
9286 shorten_dir(NameBuff);
9287 len = vim_strsize(NameBuff);
9288 p = NameBuff;
9289 #ifdef FEAT_MBYTE
9290 if (has_mbyte)
9291 while (len > room)
9293 len -= ptr2cells(p);
9294 mb_ptr_adv(p);
9296 else
9297 #endif
9298 if (len > room)
9300 p += len - room;
9301 len = room;
9303 if (len > Columns - col - 1)
9304 len = Columns - col - 1;
9306 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
9307 col += len;
9309 screen_putchar(' ', 0, col++, attr);
9311 /* Store the tab page number in TabPageIdxs[], so that
9312 * jump_to_mouse() knows where each one is. */
9313 ++tabcount;
9314 while (scol < col)
9315 TabPageIdxs[scol++] = tabcount;
9318 if (use_sep_chars)
9319 c = '_';
9320 else
9321 c = ' ';
9322 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
9324 /* Put an "X" for closing the current tab if there are several. */
9325 if (first_tabpage->tp_next != NULL)
9327 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
9328 TabPageIdxs[Columns - 1] = -999;
9332 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
9333 * set. */
9334 redraw_tabline = FALSE;
9338 * Get buffer name for "buf" into NameBuff[].
9339 * Takes care of special buffer names and translates special characters.
9341 void
9342 get_trans_bufname(buf)
9343 buf_T *buf;
9345 if (buf_spname(buf) != NULL)
9346 STRCPY(NameBuff, buf_spname(buf));
9347 else
9348 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
9349 trans_characters(NameBuff, MAXPATHL);
9351 #endif
9353 #if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
9355 * Get the character to use in a status line. Get its attributes in "*attr".
9357 static int
9358 fillchar_status(attr, is_curwin)
9359 int *attr;
9360 int is_curwin;
9362 int fill;
9363 if (is_curwin)
9365 *attr = hl_attr(HLF_S);
9366 fill = fill_stl;
9368 else
9370 *attr = hl_attr(HLF_SNC);
9371 fill = fill_stlnc;
9373 /* Use fill when there is highlighting, and highlighting of current
9374 * window differs, or the fillchars differ, or this is not the
9375 * current window */
9376 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
9377 || !is_curwin || firstwin == lastwin)
9378 || (fill_stl != fill_stlnc)))
9379 return fill;
9380 if (is_curwin)
9381 return '^';
9382 return '=';
9384 #endif
9386 #ifdef FEAT_VERTSPLIT
9388 * Get the character to use in a separator between vertically split windows.
9389 * Get its attributes in "*attr".
9391 static int
9392 fillchar_vsep(attr)
9393 int *attr;
9395 *attr = hl_attr(HLF_C);
9396 if (*attr == 0 && fill_vert == ' ')
9397 return '|';
9398 else
9399 return fill_vert;
9401 #endif
9404 * Return TRUE if redrawing should currently be done.
9407 redrawing()
9409 return (!RedrawingDisabled
9410 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
9414 * Return TRUE if printing messages should currently be done.
9417 messaging()
9419 return (!(p_lz && char_avail() && !KeyTyped));
9423 * Show current status info in ruler and various other places
9424 * If always is FALSE, only show ruler if position has changed.
9426 void
9427 showruler(always)
9428 int always;
9430 if (!always && !redrawing())
9431 return;
9432 #ifdef FEAT_INS_EXPAND
9433 if (pum_visible())
9435 # ifdef FEAT_WINDOWS
9436 /* Don't redraw right now, do it later. */
9437 curwin->w_redr_status = TRUE;
9438 # endif
9439 return;
9441 #endif
9442 #if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
9443 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
9445 redraw_custum_statusline(curwin);
9447 else
9448 #endif
9449 #ifdef FEAT_CMDL_INFO
9450 win_redr_ruler(curwin, always);
9451 #endif
9453 #ifdef FEAT_TITLE
9454 if (need_maketitle
9455 # ifdef FEAT_STL_OPT
9456 || (p_icon && (stl_syntax & STL_IN_ICON))
9457 || (p_title && (stl_syntax & STL_IN_TITLE))
9458 # endif
9460 maketitle();
9461 #endif
9462 #ifdef FEAT_WINDOWS
9463 /* Redraw the tab pages line if needed. */
9464 if (redraw_tabline)
9465 draw_tabline();
9466 #endif
9469 #ifdef FEAT_CMDL_INFO
9470 static void
9471 win_redr_ruler(wp, always)
9472 win_T *wp;
9473 int always;
9475 char_u buffer[70];
9476 int row;
9477 int fillchar;
9478 int attr;
9479 int empty_line = FALSE;
9480 colnr_T virtcol;
9481 int i;
9482 int o;
9483 #ifdef FEAT_VERTSPLIT
9484 int this_ru_col;
9485 int off = 0;
9486 int width = Columns;
9487 # define WITH_OFF(x) x
9488 # define WITH_WIDTH(x) x
9489 #else
9490 # define WITH_OFF(x) 0
9491 # define WITH_WIDTH(x) Columns
9492 # define this_ru_col ru_col
9493 #endif
9495 /* If 'ruler' off or redrawing disabled, don't do anything */
9496 if (!p_ru)
9497 return;
9500 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
9501 * after deleting lines, before cursor.lnum is corrected.
9503 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
9504 return;
9506 #ifdef FEAT_INS_EXPAND
9507 /* Don't draw the ruler while doing insert-completion, it might overwrite
9508 * the (long) mode message. */
9509 # ifdef FEAT_WINDOWS
9510 if (wp == lastwin && lastwin->w_status_height == 0)
9511 # endif
9512 if (edit_submode != NULL)
9513 return;
9514 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
9515 if (pum_visible())
9516 return;
9517 #endif
9519 #ifdef FEAT_STL_OPT
9520 if (*p_ruf)
9522 int save_called_emsg = called_emsg;
9524 called_emsg = FALSE;
9525 win_redr_custom(wp, TRUE);
9526 if (called_emsg)
9527 set_string_option_direct((char_u *)"rulerformat", -1,
9528 (char_u *)"", OPT_FREE, SID_ERROR);
9529 called_emsg |= save_called_emsg;
9530 return;
9532 #endif
9535 * Check if not in Insert mode and the line is empty (will show "0-1").
9537 if (!(State & INSERT)
9538 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
9539 empty_line = TRUE;
9542 * Only draw the ruler when something changed.
9544 validate_virtcol_win(wp);
9545 if ( redraw_cmdline
9546 || always
9547 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
9548 || wp->w_cursor.col != wp->w_ru_cursor.col
9549 || wp->w_virtcol != wp->w_ru_virtcol
9550 #ifdef FEAT_VIRTUALEDIT
9551 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
9552 #endif
9553 || wp->w_topline != wp->w_ru_topline
9554 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
9555 #ifdef FEAT_DIFF
9556 || wp->w_topfill != wp->w_ru_topfill
9557 #endif
9558 || empty_line != wp->w_ru_empty)
9560 cursor_off();
9561 #ifdef FEAT_WINDOWS
9562 if (wp->w_status_height)
9564 row = W_WINROW(wp) + wp->w_height;
9565 fillchar = fillchar_status(&attr, wp == curwin);
9566 # ifdef FEAT_VERTSPLIT
9567 off = W_WINCOL(wp);
9568 width = W_WIDTH(wp);
9569 # endif
9571 else
9572 #endif
9574 row = Rows - 1;
9575 fillchar = ' ';
9576 attr = 0;
9577 #ifdef FEAT_VERTSPLIT
9578 width = Columns;
9579 off = 0;
9580 #endif
9583 /* In list mode virtcol needs to be recomputed */
9584 virtcol = wp->w_virtcol;
9585 if (wp->w_p_list && lcs_tab1 == NUL)
9587 wp->w_p_list = FALSE;
9588 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
9589 wp->w_p_list = TRUE;
9593 * Some sprintfs return the length, some return a pointer.
9594 * To avoid portability problems we use strlen() here.
9596 sprintf((char *)buffer, "%ld,",
9597 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
9598 ? 0L
9599 : (long)(wp->w_cursor.lnum));
9600 col_print(buffer + STRLEN(buffer),
9601 empty_line ? 0 : (int)wp->w_cursor.col + 1,
9602 (int)virtcol + 1);
9605 * Add a "50%" if there is room for it.
9606 * On the last line, don't print in the last column (scrolls the
9607 * screen up on some terminals).
9609 i = (int)STRLEN(buffer);
9610 get_rel_pos(wp, buffer + i + 1);
9611 o = i + vim_strsize(buffer + i + 1);
9612 #ifdef FEAT_WINDOWS
9613 if (wp->w_status_height == 0) /* can't use last char of screen */
9614 #endif
9615 ++o;
9616 #ifdef FEAT_VERTSPLIT
9617 this_ru_col = ru_col - (Columns - width);
9618 if (this_ru_col < 0)
9619 this_ru_col = 0;
9620 #endif
9621 /* Never use more than half the window/screen width, leave the other
9622 * half for the filename. */
9623 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
9624 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
9625 if (this_ru_col + o < WITH_WIDTH(width))
9627 while (this_ru_col + o < WITH_WIDTH(width))
9629 #ifdef FEAT_MBYTE
9630 if (has_mbyte)
9631 i += (*mb_char2bytes)(fillchar, buffer + i);
9632 else
9633 #endif
9634 buffer[i++] = fillchar;
9635 ++o;
9637 get_rel_pos(wp, buffer + i);
9639 /* Truncate at window boundary. */
9640 #ifdef FEAT_MBYTE
9641 if (has_mbyte)
9643 o = 0;
9644 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
9646 o += (*mb_ptr2cells)(buffer + i);
9647 if (this_ru_col + o > WITH_WIDTH(width))
9649 buffer[i] = NUL;
9650 break;
9654 else
9655 #endif
9656 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
9657 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
9659 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
9660 i = redraw_cmdline;
9661 screen_fill(row, row + 1,
9662 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
9663 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
9664 fillchar, fillchar, attr);
9665 /* don't redraw the cmdline because of showing the ruler */
9666 redraw_cmdline = i;
9667 wp->w_ru_cursor = wp->w_cursor;
9668 wp->w_ru_virtcol = wp->w_virtcol;
9669 wp->w_ru_empty = empty_line;
9670 wp->w_ru_topline = wp->w_topline;
9671 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
9672 #ifdef FEAT_DIFF
9673 wp->w_ru_topfill = wp->w_topfill;
9674 #endif
9677 #endif
9679 #if defined(FEAT_LINEBREAK) || defined(PROTO)
9681 * Return the width of the 'number' column.
9682 * Caller may need to check if 'number' is set.
9683 * Otherwise it depends on 'numberwidth' and the line count.
9686 number_width(wp)
9687 win_T *wp;
9689 int n;
9690 linenr_T lnum;
9692 lnum = wp->w_buffer->b_ml.ml_line_count;
9693 if (lnum == wp->w_nrwidth_line_count)
9694 return wp->w_nrwidth_width;
9695 wp->w_nrwidth_line_count = lnum;
9697 n = 0;
9700 lnum /= 10;
9701 ++n;
9702 } while (lnum > 0);
9704 /* 'numberwidth' gives the minimal width plus one */
9705 if (n < wp->w_p_nuw - 1)
9706 n = wp->w_p_nuw - 1;
9708 wp->w_nrwidth_width = n;
9709 return n;
9711 #endif