Merged from the latest developing branch.
[MacVim/KaoriYa.git] / src / screen.c
blobf85bd082543f0e0b3cdb183a8774a8f2d088be15
1 /* vi:set ts=8 sts=4 sw=4:
3 * VIM - Vi IMproved by Bram Moolenaar
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
11 * screen.c: code for displaying on the screen
13 * Output to the screen (console, terminal emulator or GUI window) is minimized
14 * by remembering what is already on the screen, and only updating the parts
15 * that changed.
17 * ScreenLines[off] Contains a copy of the whole screen, as it is currently
18 * displayed (excluding text written by external commands).
19 * ScreenAttrs[off] Contains the associated attributes.
20 * LineOffset[row] Contains the offset into ScreenLines*[] and ScreenAttrs[]
21 * for each line.
22 * LineWraps[row] Flag for each line whether it wraps to the next line.
24 * For double-byte characters, two consecutive bytes in ScreenLines[] can form
25 * one character which occupies two display cells.
26 * For UTF-8 a multi-byte character is converted to Unicode and stored in
27 * ScreenLinesUC[]. ScreenLines[] contains the first byte only. For an ASCII
28 * character without composing chars ScreenLinesUC[] will be 0. When the
29 * character occupies two display cells the next byte in ScreenLines[] is 0.
30 * ScreenLinesC[][] contain up to 'maxcombine' composing characters
31 * (drawn on top of the first character). They are 0 when not used.
32 * ScreenLines2[] is only used for euc-jp to store the second byte if the
33 * first byte is 0x8e (single-width character).
35 * The screen_*() functions write to the screen and handle updating
36 * ScreenLines[].
38 * update_screen() is the function that updates all windows and status lines.
39 * It is called form the main loop when must_redraw is non-zero. It may be
40 * called from other places when an immediate screen update is needed.
42 * The part of the buffer that is displayed in a window is set with:
43 * - w_topline (first buffer line in window)
44 * - w_topfill (filler line above the first line)
45 * - w_leftcol (leftmost window cell in window),
46 * - w_skipcol (skipped window cells of first line)
48 * Commands that only move the cursor around in a window, do not need to take
49 * action to update the display. The main loop will check if w_topline is
50 * valid and update it (scroll the window) when needed.
52 * Commands that scroll a window change w_topline and must call
53 * check_cursor() to move the cursor into the visible part of the window, and
54 * call redraw_later(VALID) to have the window displayed by update_screen()
55 * later.
57 * Commands that change text in the buffer must call changed_bytes() or
58 * changed_lines() to mark the area that changed and will require updating
59 * later. The main loop will call update_screen(), which will update each
60 * window that shows the changed buffer. This assumes text above the change
61 * can remain displayed as it is. Text after the change may need updating for
62 * scrolling, folding and syntax highlighting.
64 * Commands that change how a window is displayed (e.g., setting 'list') or
65 * invalidate the contents of a window in another way (e.g., change fold
66 * settings), must call redraw_later(NOT_VALID) to have the whole window
67 * redisplayed by update_screen() later.
69 * Commands that change how a buffer is displayed (e.g., setting 'tabstop')
70 * must call redraw_curbuf_later(NOT_VALID) to have all the windows for the
71 * buffer redisplayed by update_screen() later.
73 * Commands that change highlighting and possibly cause a scroll too must call
74 * redraw_later(SOME_VALID) to update the whole window but still use scrolling
75 * to avoid redrawing everything. But the length of displayed lines must not
76 * change, use NOT_VALID then.
78 * Commands that move the window position must call redraw_later(NOT_VALID).
79 * TODO: should minimize redrawing by scrolling when possible.
81 * Commands that change everything (e.g., resizing the screen) must call
82 * redraw_all_later(NOT_VALID) or redraw_all_later(CLEAR).
84 * Things that are handled indirectly:
85 * - When messages scroll the screen up, msg_scrolled will be set and
86 * update_screen() called to redraw.
89 #include "vim.h"
92 * The attributes that are actually active for writing to the screen.
94 static int screen_attr = 0;
97 * Positioning the cursor is reduced by remembering the last position.
98 * Mostly used by windgoto() and screen_char().
100 static int screen_cur_row, screen_cur_col; /* last known cursor position */
102 #ifdef FEAT_SEARCH_EXTRA
103 static match_T search_hl; /* used for 'hlsearch' highlight matching */
104 #endif
106 #ifdef FEAT_FOLDING
107 static foldinfo_T win_foldinfo; /* info for 'foldcolumn' */
108 #endif
111 * Buffer for one screen line (characters and attributes).
113 static schar_T *current_ScreenLine;
115 static void win_update __ARGS((win_T *wp));
116 static void win_draw_end __ARGS((win_T *wp, int c1, int c2, int row, int endrow, hlf_T hl));
117 #ifdef FEAT_FOLDING
118 static void fold_line __ARGS((win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row));
119 static void fill_foldcolumn __ARGS((char_u *p, win_T *wp, int closed, linenr_T lnum));
120 static void copy_text_attr __ARGS((int off, char_u *buf, int len, int attr));
121 #endif
122 static int win_line __ARGS((win_T *, linenr_T, int, int, int nochange));
123 static int char_needs_redraw __ARGS((int off_from, int off_to, int cols));
124 #ifdef FEAT_RIGHTLEFT
125 static void screen_line __ARGS((int row, int coloff, int endcol, int clear_width, int rlflag));
126 # define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c), (rl))
127 #else
128 static void screen_line __ARGS((int row, int coloff, int endcol, int clear_width));
129 # define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c))
130 #endif
131 #ifdef FEAT_VERTSPLIT
132 static void draw_vsep_win __ARGS((win_T *wp, int row));
133 #endif
134 #ifdef FEAT_STL_OPT
135 static void redraw_custum_statusline __ARGS((win_T *wp));
136 #endif
137 #ifdef FEAT_SEARCH_EXTRA
138 #define SEARCH_HL_PRIORITY 0
139 static void start_search_hl __ARGS((void));
140 static void end_search_hl __ARGS((void));
141 static void prepare_search_hl __ARGS((win_T *wp, linenr_T lnum));
142 static void next_search_hl __ARGS((win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol));
143 #endif
144 static void screen_start_highlight __ARGS((int attr));
145 static void screen_char __ARGS((unsigned off, int row, int col));
146 #ifdef FEAT_MBYTE
147 static void screen_char_2 __ARGS((unsigned off, int row, int col));
148 #endif
149 static void screenclear2 __ARGS((void));
150 static void lineclear __ARGS((unsigned off, int width));
151 static void lineinvalid __ARGS((unsigned off, int width));
152 #ifdef FEAT_VERTSPLIT
153 static void linecopy __ARGS((int to, int from, win_T *wp));
154 static void redraw_block __ARGS((int row, int end, win_T *wp));
155 #endif
156 static int win_do_lines __ARGS((win_T *wp, int row, int line_count, int mayclear, int del));
157 static void win_rest_invalid __ARGS((win_T *wp));
158 static void msg_pos_mode __ARGS((void));
159 #if defined(FEAT_WINDOWS)
160 static void draw_tabline __ARGS((void));
161 #endif
162 #if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
163 static int fillchar_status __ARGS((int *attr, int is_curwin));
164 #endif
165 #ifdef FEAT_VERTSPLIT
166 static int fillchar_vsep __ARGS((int *attr));
167 #endif
168 #ifdef FEAT_STL_OPT
169 static void win_redr_custom __ARGS((win_T *wp, int draw_ruler));
170 #endif
171 #ifdef FEAT_CMDL_INFO
172 static void win_redr_ruler __ARGS((win_T *wp, int always));
173 #endif
175 #if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
176 /* Ugly global: overrule attribute used by screen_char() */
177 static int screen_char_attr = 0;
178 #endif
181 * Redraw the current window later, with update_screen(type).
182 * Set must_redraw only if not already set to a higher value.
183 * e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
185 void
186 redraw_later(type)
187 int type;
189 redraw_win_later(curwin, type);
192 void
193 redraw_win_later(wp, type)
194 win_T *wp;
195 int type;
197 if (wp->w_redr_type < type)
199 wp->w_redr_type = type;
200 if (type >= NOT_VALID)
201 wp->w_lines_valid = 0;
202 if (must_redraw < type) /* must_redraw is the maximum of all windows */
203 must_redraw = type;
208 * Force a complete redraw later. Also resets the highlighting. To be used
209 * after executing a shell command that messes up the screen.
211 void
212 redraw_later_clear()
214 redraw_all_later(CLEAR);
215 #ifdef FEAT_GUI
216 if (gui.in_use)
217 /* Use a code that will reset gui.highlight_mask in
218 * gui_stop_highlight(). */
219 screen_attr = HL_ALL + 1;
220 else
221 #endif
222 /* Use attributes that is very unlikely to appear in text. */
223 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE;
227 * Mark all windows to be redrawn later.
229 void
230 redraw_all_later(type)
231 int type;
233 win_T *wp;
235 FOR_ALL_WINDOWS(wp)
237 redraw_win_later(wp, type);
242 * Mark all windows that are editing the current buffer to be updated later.
244 void
245 redraw_curbuf_later(type)
246 int type;
248 redraw_buf_later(curbuf, type);
251 void
252 redraw_buf_later(buf, type)
253 buf_T *buf;
254 int type;
256 win_T *wp;
258 FOR_ALL_WINDOWS(wp)
260 if (wp->w_buffer == buf)
261 redraw_win_later(wp, type);
266 * Changed something in the current window, at buffer line "lnum", that
267 * requires that line and possibly other lines to be redrawn.
268 * Used when entering/leaving Insert mode with the cursor on a folded line.
269 * Used to remove the "$" from a change command.
270 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
271 * may become invalid and the whole window will have to be redrawn.
273 /*ARGSUSED*/
274 void
275 redrawWinline(lnum, invalid)
276 linenr_T lnum;
277 int invalid; /* window line height is invalid now */
279 #ifdef FEAT_FOLDING
280 int i;
281 #endif
283 if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
284 curwin->w_redraw_top = lnum;
285 if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
286 curwin->w_redraw_bot = lnum;
287 redraw_later(VALID);
289 #ifdef FEAT_FOLDING
290 if (invalid)
292 /* A w_lines[] entry for this lnum has become invalid. */
293 i = find_wl_entry(curwin, lnum);
294 if (i >= 0)
295 curwin->w_lines[i].wl_valid = FALSE;
297 #endif
301 * update all windows that are editing the current buffer
303 void
304 update_curbuf(type)
305 int type;
307 redraw_curbuf_later(type);
308 update_screen(type);
312 * update_screen()
314 * Based on the current value of curwin->w_topline, transfer a screenfull
315 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
317 void
318 update_screen(type)
319 int type;
321 win_T *wp;
322 static int did_intro = FALSE;
323 #if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
324 int did_one;
325 #endif
327 if (!screen_valid(TRUE))
328 return;
330 if (must_redraw)
332 if (type < must_redraw) /* use maximal type */
333 type = must_redraw;
335 /* must_redraw is reset here, so that when we run into some weird
336 * reason to redraw while busy redrawing (e.g., asynchronous
337 * scrolling), or update_topline() in win_update() will cause a
338 * scroll, the screen will be redrawn later or in win_update(). */
339 must_redraw = 0;
342 /* Need to update w_lines[]. */
343 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
344 type = NOT_VALID;
346 if (!redrawing())
348 redraw_later(type); /* remember type for next time */
349 must_redraw = type;
350 if (type > INVERTED_ALL)
351 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
352 return;
355 updating_screen = TRUE;
356 #ifdef FEAT_SYN_HL
357 ++display_tick; /* let syntax code know we're in a next round of
358 * display updating */
359 #endif
362 * if the screen was scrolled up when displaying a message, scroll it down
364 if (msg_scrolled)
366 clear_cmdline = TRUE;
367 if (msg_scrolled > Rows - 5) /* clearing is faster */
368 type = CLEAR;
369 else if (type != CLEAR)
371 check_for_delay(FALSE);
372 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, NULL) == FAIL)
373 type = CLEAR;
374 FOR_ALL_WINDOWS(wp)
376 if (W_WINROW(wp) < msg_scrolled)
378 if (W_WINROW(wp) + wp->w_height > msg_scrolled
379 && wp->w_redr_type < REDRAW_TOP
380 && wp->w_lines_valid > 0
381 && wp->w_topline == wp->w_lines[0].wl_lnum)
383 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
384 wp->w_redr_type = REDRAW_TOP;
386 else
388 wp->w_redr_type = NOT_VALID;
389 #ifdef FEAT_WINDOWS
390 if (W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp)
391 <= msg_scrolled)
392 wp->w_redr_status = TRUE;
393 #endif
397 redraw_cmdline = TRUE;
398 #ifdef FEAT_WINDOWS
399 redraw_tabline = TRUE;
400 #endif
402 msg_scrolled = 0;
403 need_wait_return = FALSE;
406 /* reset cmdline_row now (may have been changed temporarily) */
407 compute_cmdrow();
409 /* Check for changed highlighting */
410 if (need_highlight_changed)
411 highlight_changed();
413 if (type == CLEAR) /* first clear screen */
415 screenclear(); /* will reset clear_cmdline */
416 type = NOT_VALID;
419 if (clear_cmdline) /* going to clear cmdline (done below) */
420 check_for_delay(FALSE);
422 #ifdef FEAT_LINEBREAK
423 /* Force redraw when width of 'number' column changes. */
424 if (curwin->w_redr_type < NOT_VALID
425 && curwin->w_nrwidth != (curwin->w_p_nu ? number_width(curwin) : 0))
426 curwin->w_redr_type = NOT_VALID;
427 #endif
430 * Only start redrawing if there is really something to do.
432 if (type == INVERTED)
433 update_curswant();
434 if (curwin->w_redr_type < type
435 && !((type == VALID
436 && curwin->w_lines[0].wl_valid
437 #ifdef FEAT_DIFF
438 && curwin->w_topfill == curwin->w_old_topfill
439 && curwin->w_botfill == curwin->w_old_botfill
440 #endif
441 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
442 #ifdef FEAT_VISUAL
443 || (type == INVERTED
444 && VIsual_active
445 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
446 && curwin->w_old_visual_mode == VIsual_mode
447 && (curwin->w_valid & VALID_VIRTCOL)
448 && curwin->w_old_curswant == curwin->w_curswant)
449 #endif
451 curwin->w_redr_type = type;
453 #ifdef FEAT_WINDOWS
454 /* Redraw the tab pages line if needed. */
455 if (redraw_tabline || type >= NOT_VALID)
456 draw_tabline();
457 #endif
459 #ifdef FEAT_SYN_HL
461 * Correct stored syntax highlighting info for changes in each displayed
462 * buffer. Each buffer must only be done once.
464 FOR_ALL_WINDOWS(wp)
466 if (wp->w_buffer->b_mod_set)
468 # ifdef FEAT_WINDOWS
469 win_T *wwp;
471 /* Check if we already did this buffer. */
472 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
473 if (wwp->w_buffer == wp->w_buffer)
474 break;
475 # endif
476 if (
477 # ifdef FEAT_WINDOWS
478 wwp == wp &&
479 # endif
480 syntax_present(wp->w_buffer))
481 syn_stack_apply_changes(wp->w_buffer);
484 #endif
487 * Go from top to bottom through the windows, redrawing the ones that need
488 * it.
490 #if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
491 did_one = FALSE;
492 #endif
493 #ifdef FEAT_SEARCH_EXTRA
494 search_hl.rm.regprog = NULL;
495 #endif
496 FOR_ALL_WINDOWS(wp)
498 if (wp->w_redr_type != 0)
500 cursor_off();
501 #if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
502 if (!did_one)
504 did_one = TRUE;
505 # ifdef FEAT_SEARCH_EXTRA
506 start_search_hl();
507 # endif
508 # ifdef FEAT_CLIPBOARD
509 /* When Visual area changed, may have to update selection. */
510 if (clip_star.available && clip_isautosel())
511 clip_update_selection();
512 # endif
513 #ifdef FEAT_GUI
514 /* Remove the cursor before starting to do anything, because
515 * scrolling may make it difficult to redraw the text under
516 * it. */
517 if (gui.in_use)
518 gui_undraw_cursor();
519 #endif
521 #endif
522 win_update(wp);
525 #ifdef FEAT_WINDOWS
526 /* redraw status line after the window to minimize cursor movement */
527 if (wp->w_redr_status)
529 cursor_off();
530 win_redr_status(wp);
532 #endif
534 #if defined(FEAT_SEARCH_EXTRA)
535 end_search_hl();
536 #endif
538 #ifdef FEAT_WINDOWS
539 /* Reset b_mod_set flags. Going through all windows is probably faster
540 * than going through all buffers (there could be many buffers). */
541 for (wp = firstwin; wp != NULL; wp = wp->w_next)
542 wp->w_buffer->b_mod_set = FALSE;
543 #else
544 curbuf->b_mod_set = FALSE;
545 #endif
547 updating_screen = FALSE;
548 #ifdef FEAT_GUI
549 gui_may_resize_shell();
550 #endif
552 /* Clear or redraw the command line. Done last, because scrolling may
553 * mess up the command line. */
554 if (clear_cmdline || redraw_cmdline)
555 showmode();
557 /* May put up an introductory message when not editing a file */
558 if (!did_intro && bufempty()
559 && curbuf->b_fname == NULL
560 #ifdef FEAT_WINDOWS
561 && firstwin->w_next == NULL
562 #endif
563 && vim_strchr(p_shm, SHM_INTRO) == NULL)
564 intro_message(FALSE);
565 did_intro = TRUE;
567 #ifdef FEAT_GUI
568 /* Redraw the cursor and update the scrollbars when all screen updating is
569 * done. */
570 if (gui.in_use)
572 out_flush(); /* required before updating the cursor */
573 if (did_one)
574 gui_update_cursor(FALSE, FALSE);
575 gui_update_scrollbars(FALSE);
577 #endif
580 #if defined(FEAT_SIGNS) || defined(FEAT_GUI)
581 static void update_prepare __ARGS((void));
582 static void update_finish __ARGS((void));
585 * Prepare for updating one or more windows.
587 static void
588 update_prepare()
590 cursor_off();
591 updating_screen = TRUE;
592 #ifdef FEAT_GUI
593 /* Remove the cursor before starting to do anything, because scrolling may
594 * make it difficult to redraw the text under it. */
595 if (gui.in_use)
596 gui_undraw_cursor();
597 #endif
598 #ifdef FEAT_SEARCH_EXTRA
599 start_search_hl();
600 #endif
604 * Finish updating one or more windows.
606 static void
607 update_finish()
609 if (redraw_cmdline)
610 showmode();
612 # ifdef FEAT_SEARCH_EXTRA
613 end_search_hl();
614 # endif
616 updating_screen = FALSE;
618 # ifdef FEAT_GUI
619 gui_may_resize_shell();
621 /* Redraw the cursor and update the scrollbars when all screen updating is
622 * done. */
623 if (gui.in_use)
625 out_flush(); /* required before updating the cursor */
626 gui_update_cursor(FALSE, FALSE);
627 gui_update_scrollbars(FALSE);
629 # endif
631 #endif
633 #if defined(FEAT_SIGNS) || defined(PROTO)
634 void
635 update_debug_sign(buf, lnum)
636 buf_T *buf;
637 linenr_T lnum;
639 win_T *wp;
640 int doit = FALSE;
642 # ifdef FEAT_FOLDING
643 win_foldinfo.fi_level = 0;
644 # endif
646 /* update/delete a specific mark */
647 FOR_ALL_WINDOWS(wp)
649 if (buf != NULL && lnum > 0)
651 if (wp->w_buffer == buf && lnum >= wp->w_topline
652 && lnum < wp->w_botline)
654 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
655 wp->w_redraw_top = lnum;
656 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
657 wp->w_redraw_bot = lnum;
658 redraw_win_later(wp, VALID);
661 else
662 redraw_win_later(wp, VALID);
663 if (wp->w_redr_type != 0)
664 doit = TRUE;
667 if (!doit)
668 return;
670 /* update all windows that need updating */
671 update_prepare();
673 # ifdef FEAT_WINDOWS
674 for (wp = firstwin; wp; wp = wp->w_next)
676 if (wp->w_redr_type != 0)
677 win_update(wp);
678 if (wp->w_redr_status)
679 win_redr_status(wp);
681 # else
682 if (curwin->w_redr_type != 0)
683 win_update(curwin);
684 # endif
686 update_finish();
688 #endif
691 #if defined(FEAT_GUI) || defined(PROTO)
693 * Update a single window, its status line and maybe the command line msg.
694 * Used for the GUI scrollbar.
696 void
697 updateWindow(wp)
698 win_T *wp;
700 update_prepare();
702 #ifdef FEAT_CLIPBOARD
703 /* When Visual area changed, may have to update selection. */
704 if (clip_star.available && clip_isautosel())
705 clip_update_selection();
706 #endif
708 win_update(wp);
710 #ifdef FEAT_WINDOWS
711 /* When the screen was cleared redraw the tab pages line. */
712 if (redraw_tabline)
713 draw_tabline();
715 if (wp->w_redr_status
716 # ifdef FEAT_CMDL_INFO
717 || p_ru
718 # endif
719 # ifdef FEAT_STL_OPT
720 || *p_stl != NUL || *wp->w_p_stl != NUL
721 # endif
723 win_redr_status(wp);
724 #endif
726 update_finish();
728 #endif
731 * Update a single window.
733 * This may cause the windows below it also to be redrawn (when clearing the
734 * screen or scrolling lines).
736 * How the window is redrawn depends on wp->w_redr_type. Each type also
737 * implies the one below it.
738 * NOT_VALID redraw the whole window
739 * SOME_VALID redraw the whole window but do scroll when possible
740 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
741 * INVERTED redraw the changed part of the Visual area
742 * INVERTED_ALL redraw the whole Visual area
743 * VALID 1. scroll up/down to adjust for a changed w_topline
744 * 2. update lines at the top when scrolled down
745 * 3. redraw changed text:
746 * - if wp->w_buffer->b_mod_set set, update lines between
747 * b_mod_top and b_mod_bot.
748 * - if wp->w_redraw_top non-zero, redraw lines between
749 * wp->w_redraw_top and wp->w_redr_bot.
750 * - continue redrawing when syntax status is invalid.
751 * 4. if scrolled up, update lines at the bottom.
752 * This results in three areas that may need updating:
753 * top: from first row to top_end (when scrolled down)
754 * mid: from mid_start to mid_end (update inversion or changed text)
755 * bot: from bot_start to last row (when scrolled up)
757 static void
758 win_update(wp)
759 win_T *wp;
761 buf_T *buf = wp->w_buffer;
762 int type;
763 int top_end = 0; /* Below last row of the top area that needs
764 updating. 0 when no top area updating. */
765 int mid_start = 999;/* first row of the mid area that needs
766 updating. 999 when no mid area updating. */
767 int mid_end = 0; /* Below last row of the mid area that needs
768 updating. 0 when no mid area updating. */
769 int bot_start = 999;/* first row of the bot area that needs
770 updating. 999 when no bot area updating */
771 #ifdef FEAT_VISUAL
772 int scrolled_down = FALSE; /* TRUE when scrolled down when
773 w_topline got smaller a bit */
774 #endif
775 #ifdef FEAT_SEARCH_EXTRA
776 matchitem_T *cur; /* points to the match list */
777 int top_to_mod = FALSE; /* redraw above mod_top */
778 #endif
780 int row; /* current window row to display */
781 linenr_T lnum; /* current buffer lnum to display */
782 int idx; /* current index in w_lines[] */
783 int srow; /* starting row of the current line */
785 int eof = FALSE; /* if TRUE, we hit the end of the file */
786 int didline = FALSE; /* if TRUE, we finished the last line */
787 int i;
788 long j;
789 static int recursive = FALSE; /* being called recursively */
790 int old_botline = wp->w_botline;
791 #ifdef FEAT_FOLDING
792 long fold_count;
793 #endif
794 #ifdef FEAT_SYN_HL
795 /* remember what happened to the previous line, to know if
796 * check_visual_highlight() can be used */
797 #define DID_NONE 1 /* didn't update a line */
798 #define DID_LINE 2 /* updated a normal line */
799 #define DID_FOLD 3 /* updated a folded line */
800 int did_update = DID_NONE;
801 linenr_T syntax_last_parsed = 0; /* last parsed text line */
802 #endif
803 linenr_T mod_top = 0;
804 linenr_T mod_bot = 0;
805 #if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
806 int save_got_int;
807 #endif
809 type = wp->w_redr_type;
811 if (type == NOT_VALID)
813 #ifdef FEAT_WINDOWS
814 wp->w_redr_status = TRUE;
815 #endif
816 wp->w_lines_valid = 0;
819 /* Window is zero-height: nothing to draw. */
820 if (wp->w_height == 0)
822 wp->w_redr_type = 0;
823 return;
826 #ifdef FEAT_VERTSPLIT
827 /* Window is zero-width: Only need to draw the separator. */
828 if (wp->w_width == 0)
830 /* draw the vertical separator right of this window */
831 draw_vsep_win(wp, 0);
832 wp->w_redr_type = 0;
833 return;
835 #endif
837 #ifdef FEAT_SEARCH_EXTRA
838 /* Setup for match and 'hlsearch' highlighting. Disable any previous
839 * match */
840 cur = wp->w_match_head;
841 while (cur != NULL)
843 cur->hl.rm = cur->match;
844 if (cur->hlg_id == 0)
845 cur->hl.attr = 0;
846 else
847 cur->hl.attr = syn_id2attr(cur->hlg_id);
848 cur->hl.buf = buf;
849 cur->hl.lnum = 0;
850 cur->hl.first_lnum = 0;
851 cur = cur->next;
853 search_hl.buf = buf;
854 search_hl.lnum = 0;
855 search_hl.first_lnum = 0;
856 #endif
858 #ifdef FEAT_LINEBREAK
859 /* Force redraw when width of 'number' column changes. */
860 i = wp->w_p_nu ? number_width(wp) : 0;
861 if (wp->w_nrwidth != i)
863 type = NOT_VALID;
864 wp->w_nrwidth = i;
866 else
867 #endif
869 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
872 * When there are both inserted/deleted lines and specific lines to be
873 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
874 * everything (only happens when redrawing is off for while).
876 type = NOT_VALID;
878 else
881 * Set mod_top to the first line that needs displaying because of
882 * changes. Set mod_bot to the first line after the changes.
884 mod_top = wp->w_redraw_top;
885 if (wp->w_redraw_bot != 0)
886 mod_bot = wp->w_redraw_bot + 1;
887 else
888 mod_bot = 0;
889 wp->w_redraw_top = 0; /* reset for next time */
890 wp->w_redraw_bot = 0;
891 if (buf->b_mod_set)
893 if (mod_top == 0 || mod_top > buf->b_mod_top)
895 mod_top = buf->b_mod_top;
896 #ifdef FEAT_SYN_HL
897 /* Need to redraw lines above the change that may be included
898 * in a pattern match. */
899 if (syntax_present(buf))
901 mod_top -= buf->b_syn_sync_linebreaks;
902 if (mod_top < 1)
903 mod_top = 1;
905 #endif
907 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
908 mod_bot = buf->b_mod_bot;
910 #ifdef FEAT_SEARCH_EXTRA
911 /* When 'hlsearch' is on and using a multi-line search pattern, a
912 * change in one line may make the Search highlighting in a
913 * previous line invalid. Simple solution: redraw all visible
914 * lines above the change.
915 * Same for a match pattern.
917 if (search_hl.rm.regprog != NULL
918 && re_multiline(search_hl.rm.regprog))
919 top_to_mod = TRUE;
920 else
922 cur = wp->w_match_head;
923 while (cur != NULL)
925 if (cur->match.regprog != NULL
926 && re_multiline(cur->match.regprog))
928 top_to_mod = TRUE;
929 break;
931 cur = cur->next;
934 #endif
936 #ifdef FEAT_FOLDING
937 if (mod_top != 0 && hasAnyFolding(wp))
939 linenr_T lnumt, lnumb;
942 * A change in a line can cause lines above it to become folded or
943 * unfolded. Find the top most buffer line that may be affected.
944 * If the line was previously folded and displayed, get the first
945 * line of that fold. If the line is folded now, get the first
946 * folded line. Use the minimum of these two.
949 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
950 * the line below it. If there is no valid entry, use w_topline.
951 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
952 * to this line. If there is no valid entry, use MAXLNUM. */
953 lnumt = wp->w_topline;
954 lnumb = MAXLNUM;
955 for (i = 0; i < wp->w_lines_valid; ++i)
956 if (wp->w_lines[i].wl_valid)
958 if (wp->w_lines[i].wl_lastlnum < mod_top)
959 lnumt = wp->w_lines[i].wl_lastlnum + 1;
960 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
962 lnumb = wp->w_lines[i].wl_lnum;
963 /* When there is a fold column it might need updating
964 * in the next line ("J" just above an open fold). */
965 if (wp->w_p_fdc > 0)
966 ++lnumb;
970 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
971 if (mod_top > lnumt)
972 mod_top = lnumt;
974 /* Now do the same for the bottom line (one above mod_bot). */
975 --mod_bot;
976 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
977 ++mod_bot;
978 if (mod_bot < lnumb)
979 mod_bot = lnumb;
981 #endif
983 /* When a change starts above w_topline and the end is below
984 * w_topline, start redrawing at w_topline.
985 * If the end of the change is above w_topline: do like no change was
986 * made, but redraw the first line to find changes in syntax. */
987 if (mod_top != 0 && mod_top < wp->w_topline)
989 if (mod_bot > wp->w_topline)
990 mod_top = wp->w_topline;
991 #ifdef FEAT_SYN_HL
992 else if (syntax_present(buf))
993 top_end = 1;
994 #endif
997 /* When line numbers are displayed need to redraw all lines below
998 * inserted/deleted lines. */
999 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1000 mod_bot = MAXLNUM;
1004 * When only displaying the lines at the top, set top_end. Used when
1005 * window has scrolled down for msg_scrolled.
1007 if (type == REDRAW_TOP)
1009 j = 0;
1010 for (i = 0; i < wp->w_lines_valid; ++i)
1012 j += wp->w_lines[i].wl_size;
1013 if (j >= wp->w_upd_rows)
1015 top_end = j;
1016 break;
1019 if (top_end == 0)
1020 /* not found (cannot happen?): redraw everything */
1021 type = NOT_VALID;
1022 else
1023 /* top area defined, the rest is VALID */
1024 type = VALID;
1027 /* Trick: we want to avoid clearing the screen twice. screenclear() will
1028 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1029 * non-zero and thus not FALSE) will indicate that screenclear() was not
1030 * called. */
1031 if (screen_cleared)
1032 screen_cleared = MAYBE;
1035 * If there are no changes on the screen that require a complete redraw,
1036 * handle three cases:
1037 * 1: we are off the top of the screen by a few lines: scroll down
1038 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1039 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1040 * w_lines[] that needs updating.
1042 if ((type == VALID || type == SOME_VALID
1043 || type == INVERTED || type == INVERTED_ALL)
1044 #ifdef FEAT_DIFF
1045 && !wp->w_botfill && !wp->w_old_botfill
1046 #endif
1049 if (mod_top != 0 && wp->w_topline == mod_top)
1052 * w_topline is the first changed line, the scrolling will be done
1053 * further down.
1056 else if (wp->w_lines[0].wl_valid
1057 && (wp->w_topline < wp->w_lines[0].wl_lnum
1058 #ifdef FEAT_DIFF
1059 || (wp->w_topline == wp->w_lines[0].wl_lnum
1060 && wp->w_topfill > wp->w_old_topfill)
1061 #endif
1065 * New topline is above old topline: May scroll down.
1067 #ifdef FEAT_FOLDING
1068 if (hasAnyFolding(wp))
1070 linenr_T ln;
1072 /* count the number of lines we are off, counting a sequence
1073 * of folded lines as one */
1074 j = 0;
1075 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1077 ++j;
1078 if (j >= wp->w_height - 2)
1079 break;
1080 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1083 else
1084 #endif
1085 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1086 if (j < wp->w_height - 2) /* not too far off */
1088 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1089 #ifdef FEAT_DIFF
1090 /* insert extra lines for previously invisible filler lines */
1091 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1092 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1093 - wp->w_old_topfill;
1094 #endif
1095 if (i < wp->w_height - 2) /* less than a screen off */
1098 * Try to insert the correct number of lines.
1099 * If not the last window, delete the lines at the bottom.
1100 * win_ins_lines may fail when the terminal can't do it.
1102 if (i > 0)
1103 check_for_delay(FALSE);
1104 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1106 if (wp->w_lines_valid != 0)
1108 /* Need to update rows that are new, stop at the
1109 * first one that scrolled down. */
1110 top_end = i;
1111 #ifdef FEAT_VISUAL
1112 scrolled_down = TRUE;
1113 #endif
1115 /* Move the entries that were scrolled, disable
1116 * the entries for the lines to be redrawn. */
1117 if ((wp->w_lines_valid += j) > wp->w_height)
1118 wp->w_lines_valid = wp->w_height;
1119 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1120 wp->w_lines[idx] = wp->w_lines[idx - j];
1121 while (idx >= 0)
1122 wp->w_lines[idx--].wl_valid = FALSE;
1125 else
1126 mid_start = 0; /* redraw all lines */
1128 else
1129 mid_start = 0; /* redraw all lines */
1131 else
1132 mid_start = 0; /* redraw all lines */
1134 else
1137 * New topline is at or below old topline: May scroll up.
1138 * When topline didn't change, find first entry in w_lines[] that
1139 * needs updating.
1142 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1143 j = -1;
1144 row = 0;
1145 for (i = 0; i < wp->w_lines_valid; i++)
1147 if (wp->w_lines[i].wl_valid
1148 && wp->w_lines[i].wl_lnum == wp->w_topline)
1150 j = i;
1151 break;
1153 row += wp->w_lines[i].wl_size;
1155 if (j == -1)
1157 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1158 * lines */
1159 mid_start = 0;
1161 else
1164 * Try to delete the correct number of lines.
1165 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1167 #ifdef FEAT_DIFF
1168 /* If the topline didn't change, delete old filler lines,
1169 * otherwise delete filler lines of the new topline... */
1170 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1171 row += wp->w_old_topfill;
1172 else
1173 row += diff_check_fill(wp, wp->w_topline);
1174 /* ... but don't delete new filler lines. */
1175 row -= wp->w_topfill;
1176 #endif
1177 if (row > 0)
1179 check_for_delay(FALSE);
1180 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1181 bot_start = wp->w_height - row;
1182 else
1183 mid_start = 0; /* redraw all lines */
1185 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1188 * Skip the lines (below the deleted lines) that are still
1189 * valid and don't need redrawing. Copy their info
1190 * upwards, to compensate for the deleted lines. Set
1191 * bot_start to the first row that needs redrawing.
1193 bot_start = 0;
1194 idx = 0;
1195 for (;;)
1197 wp->w_lines[idx] = wp->w_lines[j];
1198 /* stop at line that didn't fit, unless it is still
1199 * valid (no lines deleted) */
1200 if (row > 0 && bot_start + row
1201 + (int)wp->w_lines[j].wl_size > wp->w_height)
1203 wp->w_lines_valid = idx + 1;
1204 break;
1206 bot_start += wp->w_lines[idx++].wl_size;
1208 /* stop at the last valid entry in w_lines[].wl_size */
1209 if (++j >= wp->w_lines_valid)
1211 wp->w_lines_valid = idx;
1212 break;
1215 #ifdef FEAT_DIFF
1216 /* Correct the first entry for filler lines at the top
1217 * when it won't get updated below. */
1218 if (wp->w_p_diff && bot_start > 0)
1219 wp->w_lines[0].wl_size =
1220 plines_win_nofill(wp, wp->w_topline, TRUE)
1221 + wp->w_topfill;
1222 #endif
1227 /* When starting redraw in the first line, redraw all lines. When
1228 * there is only one window it's probably faster to clear the screen
1229 * first. */
1230 if (mid_start == 0)
1232 mid_end = wp->w_height;
1233 if (lastwin == firstwin)
1235 /* Clear the screen when it was not done by win_del_lines() or
1236 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1237 * then. */
1238 if (screen_cleared != TRUE)
1239 screenclear();
1240 #ifdef FEAT_WINDOWS
1241 /* The screen was cleared, redraw the tab pages line. */
1242 if (redraw_tabline)
1243 draw_tabline();
1244 #endif
1248 /* When win_del_lines() or win_ins_lines() caused the screen to be
1249 * cleared (only happens for the first window) or when screenclear()
1250 * was called directly above, "must_redraw" will have been set to
1251 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1252 if (screen_cleared == TRUE)
1253 must_redraw = 0;
1255 else
1257 /* Not VALID or INVERTED: redraw all lines. */
1258 mid_start = 0;
1259 mid_end = wp->w_height;
1262 if (type == SOME_VALID)
1264 /* SOME_VALID: redraw all lines. */
1265 mid_start = 0;
1266 mid_end = wp->w_height;
1267 type = NOT_VALID;
1270 #ifdef FEAT_VISUAL
1271 /* check if we are updating or removing the inverted part */
1272 if ((VIsual_active && buf == curwin->w_buffer)
1273 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1275 linenr_T from, to;
1277 if (VIsual_active)
1279 if (VIsual_active
1280 && (VIsual_mode != wp->w_old_visual_mode
1281 || type == INVERTED_ALL))
1284 * If the type of Visual selection changed, redraw the whole
1285 * selection. Also when the ownership of the X selection is
1286 * gained or lost.
1288 if (curwin->w_cursor.lnum < VIsual.lnum)
1290 from = curwin->w_cursor.lnum;
1291 to = VIsual.lnum;
1293 else
1295 from = VIsual.lnum;
1296 to = curwin->w_cursor.lnum;
1298 /* redraw more when the cursor moved as well */
1299 if (wp->w_old_cursor_lnum < from)
1300 from = wp->w_old_cursor_lnum;
1301 if (wp->w_old_cursor_lnum > to)
1302 to = wp->w_old_cursor_lnum;
1303 if (wp->w_old_visual_lnum < from)
1304 from = wp->w_old_visual_lnum;
1305 if (wp->w_old_visual_lnum > to)
1306 to = wp->w_old_visual_lnum;
1308 else
1311 * Find the line numbers that need to be updated: The lines
1312 * between the old cursor position and the current cursor
1313 * position. Also check if the Visual position changed.
1315 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1317 from = curwin->w_cursor.lnum;
1318 to = wp->w_old_cursor_lnum;
1320 else
1322 from = wp->w_old_cursor_lnum;
1323 to = curwin->w_cursor.lnum;
1324 if (from == 0) /* Visual mode just started */
1325 from = to;
1328 if (VIsual.lnum != wp->w_old_visual_lnum
1329 || VIsual.col != wp->w_old_visual_col)
1331 if (wp->w_old_visual_lnum < from
1332 && wp->w_old_visual_lnum != 0)
1333 from = wp->w_old_visual_lnum;
1334 if (wp->w_old_visual_lnum > to)
1335 to = wp->w_old_visual_lnum;
1336 if (VIsual.lnum < from)
1337 from = VIsual.lnum;
1338 if (VIsual.lnum > to)
1339 to = VIsual.lnum;
1344 * If in block mode and changed column or curwin->w_curswant:
1345 * update all lines.
1346 * First compute the actual start and end column.
1348 if (VIsual_mode == Ctrl_V)
1350 colnr_T fromc, toc;
1352 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
1353 ++toc;
1354 if (curwin->w_curswant == MAXCOL)
1355 toc = MAXCOL;
1357 if (fromc != wp->w_old_cursor_fcol
1358 || toc != wp->w_old_cursor_lcol)
1360 if (from > VIsual.lnum)
1361 from = VIsual.lnum;
1362 if (to < VIsual.lnum)
1363 to = VIsual.lnum;
1365 wp->w_old_cursor_fcol = fromc;
1366 wp->w_old_cursor_lcol = toc;
1369 else
1371 /* Use the line numbers of the old Visual area. */
1372 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1374 from = wp->w_old_cursor_lnum;
1375 to = wp->w_old_visual_lnum;
1377 else
1379 from = wp->w_old_visual_lnum;
1380 to = wp->w_old_cursor_lnum;
1385 * There is no need to update lines above the top of the window.
1387 if (from < wp->w_topline)
1388 from = wp->w_topline;
1391 * If we know the value of w_botline, use it to restrict the update to
1392 * the lines that are visible in the window.
1394 if (wp->w_valid & VALID_BOTLINE)
1396 if (from >= wp->w_botline)
1397 from = wp->w_botline - 1;
1398 if (to >= wp->w_botline)
1399 to = wp->w_botline - 1;
1403 * Find the minimal part to be updated.
1404 * Watch out for scrolling that made entries in w_lines[] invalid.
1405 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1406 * top_end; need to redraw from top_end to the "to" line.
1407 * A middle mouse click with a Visual selection may change the text
1408 * above the Visual area and reset wl_valid, do count these for
1409 * mid_end (in srow).
1411 if (mid_start > 0)
1413 lnum = wp->w_topline;
1414 idx = 0;
1415 srow = 0;
1416 if (scrolled_down)
1417 mid_start = top_end;
1418 else
1419 mid_start = 0;
1420 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1422 if (wp->w_lines[idx].wl_valid)
1423 mid_start += wp->w_lines[idx].wl_size;
1424 else if (!scrolled_down)
1425 srow += wp->w_lines[idx].wl_size;
1426 ++idx;
1427 # ifdef FEAT_FOLDING
1428 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1429 lnum = wp->w_lines[idx].wl_lnum;
1430 else
1431 # endif
1432 ++lnum;
1434 srow += mid_start;
1435 mid_end = wp->w_height;
1436 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1438 if (wp->w_lines[idx].wl_valid
1439 && wp->w_lines[idx].wl_lnum >= to + 1)
1441 /* Only update until first row of this line */
1442 mid_end = srow;
1443 break;
1445 srow += wp->w_lines[idx].wl_size;
1450 if (VIsual_active && buf == curwin->w_buffer)
1452 wp->w_old_visual_mode = VIsual_mode;
1453 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1454 wp->w_old_visual_lnum = VIsual.lnum;
1455 wp->w_old_visual_col = VIsual.col;
1456 wp->w_old_curswant = curwin->w_curswant;
1458 else
1460 wp->w_old_visual_mode = 0;
1461 wp->w_old_cursor_lnum = 0;
1462 wp->w_old_visual_lnum = 0;
1463 wp->w_old_visual_col = 0;
1465 #endif /* FEAT_VISUAL */
1467 #if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1468 /* reset got_int, otherwise regexp won't work */
1469 save_got_int = got_int;
1470 got_int = 0;
1471 #endif
1472 #ifdef FEAT_FOLDING
1473 win_foldinfo.fi_level = 0;
1474 #endif
1477 * Update all the window rows.
1479 idx = 0; /* first entry in w_lines[].wl_size */
1480 row = 0;
1481 srow = 0;
1482 lnum = wp->w_topline; /* first line shown in window */
1483 for (;;)
1485 /* stop updating when reached the end of the window (check for _past_
1486 * the end of the window is at the end of the loop) */
1487 if (row == wp->w_height)
1489 didline = TRUE;
1490 break;
1493 /* stop updating when hit the end of the file */
1494 if (lnum > buf->b_ml.ml_line_count)
1496 eof = TRUE;
1497 break;
1500 /* Remember the starting row of the line that is going to be dealt
1501 * with. It is used further down when the line doesn't fit. */
1502 srow = row;
1505 * Update a line when it is in an area that needs updating, when it
1506 * has changes or w_lines[idx] is invalid.
1507 * bot_start may be halfway a wrapped line after using
1508 * win_del_lines(), check if the current line includes it.
1509 * When syntax folding is being used, the saved syntax states will
1510 * already have been updated, we can't see where the syntax state is
1511 * the same again, just update until the end of the window.
1513 if (row < top_end
1514 || (row >= mid_start && row < mid_end)
1515 #ifdef FEAT_SEARCH_EXTRA
1516 || top_to_mod
1517 #endif
1518 || idx >= wp->w_lines_valid
1519 || (row + wp->w_lines[idx].wl_size > bot_start)
1520 || (mod_top != 0
1521 && (lnum == mod_top
1522 || (lnum >= mod_top
1523 && (lnum < mod_bot
1524 #ifdef FEAT_SYN_HL
1525 || did_update == DID_FOLD
1526 || (did_update == DID_LINE
1527 && syntax_present(buf)
1528 && (
1529 # ifdef FEAT_FOLDING
1530 (foldmethodIsSyntax(wp)
1531 && hasAnyFolding(wp)) ||
1532 # endif
1533 syntax_check_changed(lnum)))
1534 #endif
1535 )))))
1537 #ifdef FEAT_SEARCH_EXTRA
1538 if (lnum == mod_top)
1539 top_to_mod = FALSE;
1540 #endif
1543 * When at start of changed lines: May scroll following lines
1544 * up or down to minimize redrawing.
1545 * Don't do this when the change continues until the end.
1546 * Don't scroll when dollar_vcol is non-zero, keep the "$".
1548 if (lnum == mod_top
1549 && mod_bot != MAXLNUM
1550 && !(dollar_vcol != 0 && mod_bot == mod_top + 1))
1552 int old_rows = 0;
1553 int new_rows = 0;
1554 int xtra_rows;
1555 linenr_T l;
1557 /* Count the old number of window rows, using w_lines[], which
1558 * should still contain the sizes for the lines as they are
1559 * currently displayed. */
1560 for (i = idx; i < wp->w_lines_valid; ++i)
1562 /* Only valid lines have a meaningful wl_lnum. Invalid
1563 * lines are part of the changed area. */
1564 if (wp->w_lines[i].wl_valid
1565 && wp->w_lines[i].wl_lnum == mod_bot)
1566 break;
1567 old_rows += wp->w_lines[i].wl_size;
1568 #ifdef FEAT_FOLDING
1569 if (wp->w_lines[i].wl_valid
1570 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1572 /* Must have found the last valid entry above mod_bot.
1573 * Add following invalid entries. */
1574 ++i;
1575 while (i < wp->w_lines_valid
1576 && !wp->w_lines[i].wl_valid)
1577 old_rows += wp->w_lines[i++].wl_size;
1578 break;
1580 #endif
1583 if (i >= wp->w_lines_valid)
1585 /* We can't find a valid line below the changed lines,
1586 * need to redraw until the end of the window.
1587 * Inserting/deleting lines has no use. */
1588 bot_start = 0;
1590 else
1592 /* Able to count old number of rows: Count new window
1593 * rows, and may insert/delete lines */
1594 j = idx;
1595 for (l = lnum; l < mod_bot; ++l)
1597 #ifdef FEAT_FOLDING
1598 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1599 ++new_rows;
1600 else
1601 #endif
1602 #ifdef FEAT_DIFF
1603 if (l == wp->w_topline)
1604 new_rows += plines_win_nofill(wp, l, TRUE)
1605 + wp->w_topfill;
1606 else
1607 #endif
1608 new_rows += plines_win(wp, l, TRUE);
1609 ++j;
1610 if (new_rows > wp->w_height - row - 2)
1612 /* it's getting too much, must redraw the rest */
1613 new_rows = 9999;
1614 break;
1617 xtra_rows = new_rows - old_rows;
1618 if (xtra_rows < 0)
1620 /* May scroll text up. If there is not enough
1621 * remaining text or scrolling fails, must redraw the
1622 * rest. If scrolling works, must redraw the text
1623 * below the scrolled text. */
1624 if (row - xtra_rows >= wp->w_height - 2)
1625 mod_bot = MAXLNUM;
1626 else
1628 check_for_delay(FALSE);
1629 if (win_del_lines(wp, row,
1630 -xtra_rows, FALSE, FALSE) == FAIL)
1631 mod_bot = MAXLNUM;
1632 else
1633 bot_start = wp->w_height + xtra_rows;
1636 else if (xtra_rows > 0)
1638 /* May scroll text down. If there is not enough
1639 * remaining text of scrolling fails, must redraw the
1640 * rest. */
1641 if (row + xtra_rows >= wp->w_height - 2)
1642 mod_bot = MAXLNUM;
1643 else
1645 check_for_delay(FALSE);
1646 if (win_ins_lines(wp, row + old_rows,
1647 xtra_rows, FALSE, FALSE) == FAIL)
1648 mod_bot = MAXLNUM;
1649 else if (top_end > row + old_rows)
1650 /* Scrolled the part at the top that requires
1651 * updating down. */
1652 top_end += xtra_rows;
1656 /* When not updating the rest, may need to move w_lines[]
1657 * entries. */
1658 if (mod_bot != MAXLNUM && i != j)
1660 if (j < i)
1662 int x = row + new_rows;
1664 /* move entries in w_lines[] upwards */
1665 for (;;)
1667 /* stop at last valid entry in w_lines[] */
1668 if (i >= wp->w_lines_valid)
1670 wp->w_lines_valid = j;
1671 break;
1673 wp->w_lines[j] = wp->w_lines[i];
1674 /* stop at a line that won't fit */
1675 if (x + (int)wp->w_lines[j].wl_size
1676 > wp->w_height)
1678 wp->w_lines_valid = j + 1;
1679 break;
1681 x += wp->w_lines[j++].wl_size;
1682 ++i;
1684 if (bot_start > x)
1685 bot_start = x;
1687 else /* j > i */
1689 /* move entries in w_lines[] downwards */
1690 j -= i;
1691 wp->w_lines_valid += j;
1692 if (wp->w_lines_valid > wp->w_height)
1693 wp->w_lines_valid = wp->w_height;
1694 for (i = wp->w_lines_valid; i - j >= idx; --i)
1695 wp->w_lines[i] = wp->w_lines[i - j];
1697 /* The w_lines[] entries for inserted lines are
1698 * now invalid, but wl_size may be used above.
1699 * Reset to zero. */
1700 while (i >= idx)
1702 wp->w_lines[i].wl_size = 0;
1703 wp->w_lines[i--].wl_valid = FALSE;
1710 #ifdef FEAT_FOLDING
1712 * When lines are folded, display one line for all of them.
1713 * Otherwise, display normally (can be several display lines when
1714 * 'wrap' is on).
1716 fold_count = foldedCount(wp, lnum, &win_foldinfo);
1717 if (fold_count != 0)
1719 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
1720 ++row;
1721 --fold_count;
1722 wp->w_lines[idx].wl_folded = TRUE;
1723 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
1724 # ifdef FEAT_SYN_HL
1725 did_update = DID_FOLD;
1726 # endif
1728 else
1729 #endif
1730 if (idx < wp->w_lines_valid
1731 && wp->w_lines[idx].wl_valid
1732 && wp->w_lines[idx].wl_lnum == lnum
1733 && lnum > wp->w_topline
1734 && !(dy_flags & DY_LASTLINE)
1735 && srow + wp->w_lines[idx].wl_size > wp->w_height
1736 #ifdef FEAT_DIFF
1737 && diff_check_fill(wp, lnum) == 0
1738 #endif
1741 /* This line is not going to fit. Don't draw anything here,
1742 * will draw "@ " lines below. */
1743 row = wp->w_height + 1;
1745 else
1747 #ifdef FEAT_SEARCH_EXTRA
1748 prepare_search_hl(wp, lnum);
1749 #endif
1750 #ifdef FEAT_SYN_HL
1751 /* Let the syntax stuff know we skipped a few lines. */
1752 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
1753 && syntax_present(buf))
1754 syntax_end_parsing(syntax_last_parsed + 1);
1755 #endif
1758 * Display one line.
1760 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
1762 #ifdef FEAT_FOLDING
1763 wp->w_lines[idx].wl_folded = FALSE;
1764 wp->w_lines[idx].wl_lastlnum = lnum;
1765 #endif
1766 #ifdef FEAT_SYN_HL
1767 did_update = DID_LINE;
1768 syntax_last_parsed = lnum;
1769 #endif
1772 wp->w_lines[idx].wl_lnum = lnum;
1773 wp->w_lines[idx].wl_valid = TRUE;
1774 if (row > wp->w_height) /* past end of screen */
1776 /* we may need the size of that too long line later on */
1777 if (dollar_vcol == 0)
1778 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
1779 ++idx;
1780 break;
1782 if (dollar_vcol == 0)
1783 wp->w_lines[idx].wl_size = row - srow;
1784 ++idx;
1785 #ifdef FEAT_FOLDING
1786 lnum += fold_count + 1;
1787 #else
1788 ++lnum;
1789 #endif
1791 else
1793 /* This line does not need updating, advance to the next one */
1794 row += wp->w_lines[idx++].wl_size;
1795 if (row > wp->w_height) /* past end of screen */
1796 break;
1797 #ifdef FEAT_FOLDING
1798 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
1799 #else
1800 ++lnum;
1801 #endif
1802 #ifdef FEAT_SYN_HL
1803 did_update = DID_NONE;
1804 #endif
1807 if (lnum > buf->b_ml.ml_line_count)
1809 eof = TRUE;
1810 break;
1814 * End of loop over all window lines.
1818 if (idx > wp->w_lines_valid)
1819 wp->w_lines_valid = idx;
1821 #ifdef FEAT_SYN_HL
1823 * Let the syntax stuff know we stop parsing here.
1825 if (syntax_last_parsed != 0 && syntax_present(buf))
1826 syntax_end_parsing(syntax_last_parsed + 1);
1827 #endif
1830 * If we didn't hit the end of the file, and we didn't finish the last
1831 * line we were working on, then the line didn't fit.
1833 wp->w_empty_rows = 0;
1834 #ifdef FEAT_DIFF
1835 wp->w_filler_rows = 0;
1836 #endif
1837 if (!eof && !didline)
1839 if (lnum == wp->w_topline)
1842 * Single line that does not fit!
1843 * Don't overwrite it, it can be edited.
1845 wp->w_botline = lnum + 1;
1847 #ifdef FEAT_DIFF
1848 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
1850 /* Window ends in filler lines. */
1851 wp->w_botline = lnum;
1852 wp->w_filler_rows = wp->w_height - srow;
1854 #endif
1855 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
1858 * Last line isn't finished: Display "@@@" at the end.
1860 screen_fill(W_WINROW(wp) + wp->w_height - 1,
1861 W_WINROW(wp) + wp->w_height,
1862 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
1863 '@', '@', hl_attr(HLF_AT));
1864 set_empty_rows(wp, srow);
1865 wp->w_botline = lnum;
1867 else
1869 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
1870 wp->w_botline = lnum;
1873 else
1875 #ifdef FEAT_VERTSPLIT
1876 draw_vsep_win(wp, row);
1877 #endif
1878 if (eof) /* we hit the end of the file */
1880 wp->w_botline = buf->b_ml.ml_line_count + 1;
1881 #ifdef FEAT_DIFF
1882 j = diff_check_fill(wp, wp->w_botline);
1883 if (j > 0 && !wp->w_botfill)
1886 * Display filler lines at the end of the file
1888 if (char2cells(fill_diff) > 1)
1889 i = '-';
1890 else
1891 i = fill_diff;
1892 if (row + j > wp->w_height)
1893 j = wp->w_height - row;
1894 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
1895 row += j;
1897 #endif
1899 else if (dollar_vcol == 0)
1900 wp->w_botline = lnum;
1902 /* make sure the rest of the screen is blank */
1903 /* put '~'s on rows that aren't part of the file. */
1904 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_AT);
1907 /* Reset the type of redrawing required, the window has been updated. */
1908 wp->w_redr_type = 0;
1909 #ifdef FEAT_DIFF
1910 wp->w_old_topfill = wp->w_topfill;
1911 wp->w_old_botfill = wp->w_botfill;
1912 #endif
1914 if (dollar_vcol == 0)
1917 * There is a trick with w_botline. If we invalidate it on each
1918 * change that might modify it, this will cause a lot of expensive
1919 * calls to plines() in update_topline() each time. Therefore the
1920 * value of w_botline is often approximated, and this value is used to
1921 * compute the value of w_topline. If the value of w_botline was
1922 * wrong, check that the value of w_topline is correct (cursor is on
1923 * the visible part of the text). If it's not, we need to redraw
1924 * again. Mostly this just means scrolling up a few lines, so it
1925 * doesn't look too bad. Only do this for the current window (where
1926 * changes are relevant).
1928 wp->w_valid |= VALID_BOTLINE;
1929 if (wp == curwin && wp->w_botline != old_botline && !recursive)
1931 recursive = TRUE;
1932 curwin->w_valid &= ~VALID_TOPLINE;
1933 update_topline(); /* may invalidate w_botline again */
1934 if (must_redraw != 0)
1936 /* Don't update for changes in buffer again. */
1937 i = curbuf->b_mod_set;
1938 curbuf->b_mod_set = FALSE;
1939 win_update(curwin);
1940 must_redraw = 0;
1941 curbuf->b_mod_set = i;
1943 recursive = FALSE;
1947 #if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1948 /* restore got_int, unless CTRL-C was hit while redrawing */
1949 if (!got_int)
1950 got_int = save_got_int;
1951 #endif
1954 #ifdef FEAT_SIGNS
1955 static int draw_signcolumn __ARGS((win_T *wp));
1958 * Return TRUE when window "wp" has a column to draw signs in.
1960 static int
1961 draw_signcolumn(wp)
1962 win_T *wp;
1964 return (wp->w_buffer->b_signlist != NULL
1965 # ifdef FEAT_NETBEANS_INTG
1966 || usingNetbeans
1967 # endif
1970 #endif
1973 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
1974 * as the filler character.
1976 static void
1977 win_draw_end(wp, c1, c2, row, endrow, hl)
1978 win_T *wp;
1979 int c1;
1980 int c2;
1981 int row;
1982 int endrow;
1983 hlf_T hl;
1985 #if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
1986 int n = 0;
1987 # define FDC_OFF n
1988 #else
1989 # define FDC_OFF 0
1990 #endif
1992 #ifdef FEAT_RIGHTLEFT
1993 if (wp->w_p_rl)
1995 /* No check for cmdline window: should never be right-left. */
1996 # ifdef FEAT_FOLDING
1997 n = wp->w_p_fdc;
1999 if (n > 0)
2001 /* draw the fold column at the right */
2002 if (n > W_WIDTH(wp))
2003 n = W_WIDTH(wp);
2004 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2005 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
2006 ' ', ' ', hl_attr(HLF_FC));
2008 # endif
2009 # ifdef FEAT_SIGNS
2010 if (draw_signcolumn(wp))
2012 int nn = n + 2;
2014 /* draw the sign column left of the fold column */
2015 if (nn > W_WIDTH(wp))
2016 nn = W_WIDTH(wp);
2017 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2018 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
2019 ' ', ' ', hl_attr(HLF_SC));
2020 n = nn;
2022 # endif
2023 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2024 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
2025 c2, c2, hl_attr(hl));
2026 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2027 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
2028 c1, c2, hl_attr(hl));
2030 else
2031 #endif
2033 #ifdef FEAT_CMDWIN
2034 if (cmdwin_type != 0 && wp == curwin)
2036 /* draw the cmdline character in the leftmost column */
2037 n = 1;
2038 if (n > wp->w_width)
2039 n = wp->w_width;
2040 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2041 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
2042 cmdwin_type, ' ', hl_attr(HLF_AT));
2044 #endif
2045 #ifdef FEAT_FOLDING
2046 if (wp->w_p_fdc > 0)
2048 int nn = n + wp->w_p_fdc;
2050 /* draw the fold column at the left */
2051 if (nn > W_WIDTH(wp))
2052 nn = W_WIDTH(wp);
2053 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2054 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2055 ' ', ' ', hl_attr(HLF_FC));
2056 n = nn;
2058 #endif
2059 #ifdef FEAT_SIGNS
2060 if (draw_signcolumn(wp))
2062 int nn = n + 2;
2064 /* draw the sign column after the fold column */
2065 if (nn > W_WIDTH(wp))
2066 nn = W_WIDTH(wp);
2067 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2068 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2069 ' ', ' ', hl_attr(HLF_SC));
2070 n = nn;
2072 #endif
2073 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2074 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
2075 c1, c2, hl_attr(hl));
2077 set_empty_rows(wp, row);
2080 #ifdef FEAT_FOLDING
2082 * Display one folded line.
2084 static void
2085 fold_line(wp, fold_count, foldinfo, lnum, row)
2086 win_T *wp;
2087 long fold_count;
2088 foldinfo_T *foldinfo;
2089 linenr_T lnum;
2090 int row;
2092 char_u buf[51];
2093 pos_T *top, *bot;
2094 linenr_T lnume = lnum + fold_count - 1;
2095 int len;
2096 char_u *text;
2097 int fdc;
2098 int col;
2099 int txtcol;
2100 int off = (int)(current_ScreenLine - ScreenLines);
2101 int ri;
2103 /* Build the fold line:
2104 * 1. Add the cmdwin_type for the command-line window
2105 * 2. Add the 'foldcolumn'
2106 * 3. Add the 'number' column
2107 * 4. Compose the text
2108 * 5. Add the text
2109 * 6. set highlighting for the Visual area an other text
2111 col = 0;
2114 * 1. Add the cmdwin_type for the command-line window
2115 * Ignores 'rightleft', this window is never right-left.
2117 #ifdef FEAT_CMDWIN
2118 if (cmdwin_type != 0 && wp == curwin)
2120 ScreenLines[off] = cmdwin_type;
2121 ScreenAttrs[off] = hl_attr(HLF_AT);
2122 #ifdef FEAT_MBYTE
2123 if (enc_utf8)
2124 ScreenLinesUC[off] = 0;
2125 #endif
2126 ++col;
2128 #endif
2131 * 2. Add the 'foldcolumn'
2133 fdc = wp->w_p_fdc;
2134 if (fdc > W_WIDTH(wp) - col)
2135 fdc = W_WIDTH(wp) - col;
2136 if (fdc > 0)
2138 fill_foldcolumn(buf, wp, TRUE, lnum);
2139 #ifdef FEAT_RIGHTLEFT
2140 if (wp->w_p_rl)
2142 int i;
2144 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
2145 hl_attr(HLF_FC));
2146 /* reverse the fold column */
2147 for (i = 0; i < fdc; ++i)
2148 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2150 else
2151 #endif
2152 copy_text_attr(off + col, buf, fdc, hl_attr(HLF_FC));
2153 col += fdc;
2156 #ifdef FEAT_RIGHTLEFT
2157 # define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2158 for (ri = 0; ri < l; ++ri) \
2159 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2160 else \
2161 for (ri = 0; ri < l; ++ri) \
2162 ScreenAttrs[off + (p) + ri] = v
2163 #else
2164 # define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2165 ScreenAttrs[off + (p) + ri] = v
2166 #endif
2168 /* Set all attributes of the 'number' column and the text */
2169 RL_MEMSET(col, hl_attr(HLF_FL), W_WIDTH(wp) - col);
2171 #ifdef FEAT_SIGNS
2172 /* If signs are being displayed, add two spaces. */
2173 if (draw_signcolumn(wp))
2175 len = W_WIDTH(wp) - col;
2176 if (len > 0)
2178 if (len > 2)
2179 len = 2;
2180 # ifdef FEAT_RIGHTLEFT
2181 if (wp->w_p_rl)
2182 /* the line number isn't reversed */
2183 copy_text_attr(off + W_WIDTH(wp) - len - col,
2184 (char_u *)" ", len, hl_attr(HLF_FL));
2185 else
2186 # endif
2187 copy_text_attr(off + col, (char_u *)" ", len, hl_attr(HLF_FL));
2188 col += len;
2191 #endif
2194 * 3. Add the 'number' column
2196 if (wp->w_p_nu)
2198 len = W_WIDTH(wp) - col;
2199 if (len > 0)
2201 int w = number_width(wp);
2203 if (len > w + 1)
2204 len = w + 1;
2205 sprintf((char *)buf, "%*ld ", w, (long)lnum);
2206 #ifdef FEAT_RIGHTLEFT
2207 if (wp->w_p_rl)
2208 /* the line number isn't reversed */
2209 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
2210 hl_attr(HLF_FL));
2211 else
2212 #endif
2213 copy_text_attr(off + col, buf, len, hl_attr(HLF_FL));
2214 col += len;
2219 * 4. Compose the folded-line string with 'foldtext', if set.
2221 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
2223 txtcol = col; /* remember where text starts */
2226 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2227 * Right-left text is put in columns 0 - number-col, normal text is put
2228 * in columns number-col - window-width.
2230 #ifdef FEAT_MBYTE
2231 if (has_mbyte)
2233 int cells;
2234 int u8c, u8cc[MAX_MCO];
2235 int i;
2236 int idx;
2237 int c_len;
2238 char_u *p;
2239 # ifdef FEAT_ARABIC
2240 int prev_c = 0; /* previous Arabic character */
2241 int prev_c1 = 0; /* first composing char for prev_c */
2242 # endif
2244 # ifdef FEAT_RIGHTLEFT
2245 if (wp->w_p_rl)
2246 idx = off;
2247 else
2248 # endif
2249 idx = off + col;
2251 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2252 for (p = text; *p != NUL; )
2254 cells = (*mb_ptr2cells)(p);
2255 c_len = (*mb_ptr2len)(p);
2256 if (col + cells > W_WIDTH(wp)
2257 # ifdef FEAT_RIGHTLEFT
2258 - (wp->w_p_rl ? col : 0)
2259 # endif
2261 break;
2262 ScreenLines[idx] = *p;
2263 if (enc_utf8)
2265 u8c = utfc_ptr2char(p, u8cc);
2266 if (*p < 0x80 && u8cc[0] == 0)
2268 ScreenLinesUC[idx] = 0;
2269 #ifdef FEAT_ARABIC
2270 prev_c = u8c;
2271 #endif
2273 else
2275 #ifdef FEAT_ARABIC
2276 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2278 /* Do Arabic shaping. */
2279 int pc, pc1, nc;
2280 int pcc[MAX_MCO];
2281 int firstbyte = *p;
2283 /* The idea of what is the previous and next
2284 * character depends on 'rightleft'. */
2285 if (wp->w_p_rl)
2287 pc = prev_c;
2288 pc1 = prev_c1;
2289 nc = utf_ptr2char(p + c_len);
2290 prev_c1 = u8cc[0];
2292 else
2294 pc = utfc_ptr2char(p + c_len, pcc);
2295 nc = prev_c;
2296 pc1 = pcc[0];
2298 prev_c = u8c;
2300 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2301 pc, pc1, nc);
2302 ScreenLines[idx] = firstbyte;
2304 else
2305 prev_c = u8c;
2306 #endif
2307 /* Non-BMP character: display as ? or fullwidth ?. */
2308 #ifdef UNICODE16
2309 if (u8c >= 0x10000)
2310 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2311 else
2312 #endif
2313 ScreenLinesUC[idx] = u8c;
2314 for (i = 0; i < Screen_mco; ++i)
2316 ScreenLinesC[i][idx] = u8cc[i];
2317 if (u8cc[i] == 0)
2318 break;
2321 if (cells > 1)
2322 ScreenLines[idx + 1] = 0;
2324 else if (cells > 1) /* double-byte character */
2326 if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2327 ScreenLines2[idx] = p[1];
2328 else
2329 ScreenLines[idx + 1] = p[1];
2331 col += cells;
2332 idx += cells;
2333 p += c_len;
2336 else
2337 #endif
2339 len = (int)STRLEN(text);
2340 if (len > W_WIDTH(wp) - col)
2341 len = W_WIDTH(wp) - col;
2342 if (len > 0)
2344 #ifdef FEAT_RIGHTLEFT
2345 if (wp->w_p_rl)
2346 STRNCPY(current_ScreenLine, text, len);
2347 else
2348 #endif
2349 STRNCPY(current_ScreenLine + col, text, len);
2350 col += len;
2354 /* Fill the rest of the line with the fold filler */
2355 #ifdef FEAT_RIGHTLEFT
2356 if (wp->w_p_rl)
2357 col -= txtcol;
2358 #endif
2359 while (col < W_WIDTH(wp)
2360 #ifdef FEAT_RIGHTLEFT
2361 - (wp->w_p_rl ? txtcol : 0)
2362 #endif
2365 #ifdef FEAT_MBYTE
2366 if (enc_utf8)
2368 if (fill_fold >= 0x80)
2370 ScreenLinesUC[off + col] = fill_fold;
2371 ScreenLinesC[0][off + col] = 0;
2373 else
2374 ScreenLinesUC[off + col] = 0;
2376 #endif
2377 ScreenLines[off + col++] = fill_fold;
2380 if (text != buf)
2381 vim_free(text);
2384 * 6. set highlighting for the Visual area an other text.
2385 * If all folded lines are in the Visual area, highlight the line.
2387 #ifdef FEAT_VISUAL
2388 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2390 if (ltoreq(curwin->w_cursor, VIsual))
2392 /* Visual is after curwin->w_cursor */
2393 top = &curwin->w_cursor;
2394 bot = &VIsual;
2396 else
2398 /* Visual is before curwin->w_cursor */
2399 top = &VIsual;
2400 bot = &curwin->w_cursor;
2402 if (lnum >= top->lnum
2403 && lnume <= bot->lnum
2404 && (VIsual_mode != 'v'
2405 || ((lnum > top->lnum
2406 || (lnum == top->lnum
2407 && top->col == 0))
2408 && (lnume < bot->lnum
2409 || (lnume == bot->lnum
2410 && (bot->col - (*p_sel == 'e'))
2411 >= STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
2413 if (VIsual_mode == Ctrl_V)
2415 /* Visual block mode: highlight the chars part of the block */
2416 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2418 if (wp->w_old_cursor_lcol + txtcol < (colnr_T)W_WIDTH(wp))
2419 len = wp->w_old_cursor_lcol;
2420 else
2421 len = W_WIDTH(wp) - txtcol;
2422 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, hl_attr(HLF_V),
2423 len - (int)wp->w_old_cursor_fcol);
2426 else
2428 /* Set all attributes of the text */
2429 RL_MEMSET(txtcol, hl_attr(HLF_V), W_WIDTH(wp) - txtcol);
2433 #endif
2435 #ifdef FEAT_SYN_HL
2436 /* Show 'cursorcolumn' in the fold line. */
2437 if (wp->w_p_cuc && (int)wp->w_virtcol + txtcol < W_WIDTH(wp))
2438 ScreenAttrs[off + wp->w_virtcol + txtcol] = hl_combine_attr(
2439 ScreenAttrs[off + wp->w_virtcol + txtcol], hl_attr(HLF_CUC));
2440 #endif
2442 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2443 (int)W_WIDTH(wp), FALSE);
2446 * Update w_cline_height and w_cline_folded if the cursor line was
2447 * updated (saves a call to plines() later).
2449 if (wp == curwin
2450 && lnum <= curwin->w_cursor.lnum
2451 && lnume >= curwin->w_cursor.lnum)
2453 curwin->w_cline_row = row;
2454 curwin->w_cline_height = 1;
2455 curwin->w_cline_folded = TRUE;
2456 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2461 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2463 static void
2464 copy_text_attr(off, buf, len, attr)
2465 int off;
2466 char_u *buf;
2467 int len;
2468 int attr;
2470 int i;
2472 mch_memmove(ScreenLines + off, buf, (size_t)len);
2473 # ifdef FEAT_MBYTE
2474 if (enc_utf8)
2475 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2476 # endif
2477 for (i = 0; i < len; ++i)
2478 ScreenAttrs[off + i] = attr;
2482 * Fill the foldcolumn at "p" for window "wp".
2483 * Only to be called when 'foldcolumn' > 0.
2485 static void
2486 fill_foldcolumn(p, wp, closed, lnum)
2487 char_u *p;
2488 win_T *wp;
2489 int closed; /* TRUE of FALSE */
2490 linenr_T lnum; /* current line number */
2492 int i = 0;
2493 int level;
2494 int first_level;
2495 int empty;
2497 /* Init to all spaces. */
2498 copy_spaces(p, (size_t)wp->w_p_fdc);
2500 level = win_foldinfo.fi_level;
2501 if (level > 0)
2503 /* If there is only one column put more info in it. */
2504 empty = (wp->w_p_fdc == 1) ? 0 : 1;
2506 /* If the column is too narrow, we start at the lowest level that
2507 * fits and use numbers to indicated the depth. */
2508 first_level = level - wp->w_p_fdc - closed + 1 + empty;
2509 if (first_level < 1)
2510 first_level = 1;
2512 for (i = 0; i + empty < wp->w_p_fdc; ++i)
2514 if (win_foldinfo.fi_lnum == lnum
2515 && first_level + i >= win_foldinfo.fi_low_level)
2516 p[i] = '-';
2517 else if (first_level == 1)
2518 p[i] = '|';
2519 else if (first_level + i <= 9)
2520 p[i] = '0' + first_level + i;
2521 else
2522 p[i] = '>';
2523 if (first_level + i == level)
2524 break;
2527 if (closed)
2528 p[i >= wp->w_p_fdc ? i - 1 : i] = '+';
2530 #endif /* FEAT_FOLDING */
2533 * Display line "lnum" of window 'wp' on the screen.
2534 * Start at row "startrow", stop when "endrow" is reached.
2535 * wp->w_virtcol needs to be valid.
2537 * Return the number of last row the line occupies.
2539 /* ARGSUSED */
2540 static int
2541 win_line(wp, lnum, startrow, endrow, nochange)
2542 win_T *wp;
2543 linenr_T lnum;
2544 int startrow;
2545 int endrow;
2546 int nochange; /* not updating for changed text */
2548 int col; /* visual column on screen */
2549 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2550 int c = 0; /* init for GCC */
2551 long vcol = 0; /* virtual column (for tabs) */
2552 long vcol_prev = -1; /* "vcol" of previous character */
2553 char_u *line; /* current line */
2554 char_u *ptr; /* current position in "line" */
2555 int row; /* row in the window, excl w_winrow */
2556 int screen_row; /* row on the screen, incl w_winrow */
2558 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2559 int n_extra = 0; /* number of extra chars */
2560 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
2561 int c_extra = NUL; /* extra chars, all the same */
2562 int extra_attr = 0; /* attributes when n_extra != 0 */
2563 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2564 displaying lcs_eol at end-of-line */
2565 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2566 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2568 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2569 int saved_n_extra = 0;
2570 char_u *saved_p_extra = NULL;
2571 int saved_c_extra = 0;
2572 int saved_char_attr = 0;
2574 int n_attr = 0; /* chars with special attr */
2575 int saved_attr2 = 0; /* char_attr saved for n_attr */
2576 int n_attr3 = 0; /* chars with overruling special attr */
2577 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2579 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2581 int fromcol, tocol; /* start/end of inverting */
2582 int fromcol_prev = -2; /* start of inverting after cursor */
2583 int noinvcur = FALSE; /* don't invert the cursor */
2584 #ifdef FEAT_VISUAL
2585 pos_T *top, *bot;
2586 #endif
2587 pos_T pos;
2588 long v;
2590 int char_attr = 0; /* attributes for next character */
2591 int attr_pri = FALSE; /* char_attr has priority */
2592 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2593 in this line */
2594 int attr = 0; /* attributes for area highlighting */
2595 int area_attr = 0; /* attributes desired by highlighting */
2596 int search_attr = 0; /* attributes desired by 'hlsearch' */
2597 #ifdef FEAT_SYN_HL
2598 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
2599 int syntax_attr = 0; /* attributes desired by syntax */
2600 int has_syntax = FALSE; /* this buffer has syntax highl. */
2601 int save_did_emsg;
2602 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
2603 #endif
2604 #ifdef FEAT_SPELL
2605 int has_spell = FALSE; /* this buffer has spell checking */
2606 # define SPWORDLEN 150
2607 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
2608 int nextlinecol = 0; /* column where nextline[] starts */
2609 int nextline_idx = 0; /* index in nextline[] where next line
2610 starts */
2611 int spell_attr = 0; /* attributes desired by spelling */
2612 int word_end = 0; /* last byte with same spell_attr */
2613 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2614 static int checked_col = 0; /* column in "checked_lnum" up to which
2615 * there are no spell errors */
2616 static int cap_col = -1; /* column to check for Cap word */
2617 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
2618 int cur_checked_col = 0; /* checked column for current line */
2619 #endif
2620 int extra_check; /* has syntax or linebreak */
2621 #ifdef FEAT_MBYTE
2622 int multi_attr = 0; /* attributes desired by multibyte */
2623 int mb_l = 1; /* multi-byte byte length */
2624 int mb_c = 0; /* decoded multi-byte character */
2625 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
2626 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
2627 #endif
2628 #ifdef FEAT_DIFF
2629 int filler_lines; /* nr of filler lines to be drawn */
2630 int filler_todo; /* nr of filler lines still to do + 1 */
2631 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
2632 int change_start = MAXCOL; /* first col of changed area */
2633 int change_end = -1; /* last col of changed area */
2634 #endif
2635 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
2636 #ifdef FEAT_LINEBREAK
2637 int need_showbreak = FALSE;
2638 #endif
2639 #if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
2640 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
2641 # define LINE_ATTR
2642 int line_attr = 0; /* atrribute for the whole line */
2643 #endif
2644 #ifdef FEAT_SEARCH_EXTRA
2645 matchitem_T *cur; /* points to the match list */
2646 match_T *shl; /* points to search_hl or a match */
2647 int shl_flag; /* flag to indicate whether search_hl
2648 has been processed or not */
2649 int prevcol_hl_flag; /* flag to indicate whether prevcol
2650 equals startcol of search_hl or one
2651 of the matches */
2652 #endif
2653 #ifdef FEAT_ARABIC
2654 int prev_c = 0; /* previous Arabic character */
2655 int prev_c1 = 0; /* first composing char for prev_c */
2656 #endif
2657 #if defined(LINE_ATTR)
2658 int did_line_attr = 0;
2659 #endif
2661 /* draw_state: items that are drawn in sequence: */
2662 #define WL_START 0 /* nothing done yet */
2663 #ifdef FEAT_CMDWIN
2664 # define WL_CMDLINE WL_START + 1 /* cmdline window column */
2665 #else
2666 # define WL_CMDLINE WL_START
2667 #endif
2668 #ifdef FEAT_FOLDING
2669 # define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
2670 #else
2671 # define WL_FOLD WL_CMDLINE
2672 #endif
2673 #ifdef FEAT_SIGNS
2674 # define WL_SIGN WL_FOLD + 1 /* column for signs */
2675 #else
2676 # define WL_SIGN WL_FOLD /* column for signs */
2677 #endif
2678 #define WL_NR WL_SIGN + 1 /* line number */
2679 #if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
2680 # define WL_SBR WL_NR + 1 /* 'showbreak' or 'diff' */
2681 #else
2682 # define WL_SBR WL_NR
2683 #endif
2684 #define WL_LINE WL_SBR + 1 /* text in the line */
2685 int draw_state = WL_START; /* what to draw next */
2686 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2687 int feedback_col = 0;
2688 int feedback_old_attr = -1;
2689 #endif
2692 if (startrow > endrow) /* past the end already! */
2693 return startrow;
2695 row = startrow;
2696 screen_row = row + W_WINROW(wp);
2699 * To speed up the loop below, set extra_check when there is linebreak,
2700 * trailing white space and/or syntax processing to be done.
2702 #ifdef FEAT_LINEBREAK
2703 extra_check = wp->w_p_lbr;
2704 #else
2705 extra_check = 0;
2706 #endif
2707 #ifdef FEAT_SYN_HL
2708 if (syntax_present(wp->w_buffer) && !wp->w_buffer->b_syn_error)
2710 /* Prepare for syntax highlighting in this line. When there is an
2711 * error, stop syntax highlighting. */
2712 save_did_emsg = did_emsg;
2713 did_emsg = FALSE;
2714 syntax_start(wp, lnum);
2715 if (did_emsg)
2716 wp->w_buffer->b_syn_error = TRUE;
2717 else
2719 did_emsg = save_did_emsg;
2720 has_syntax = TRUE;
2721 extra_check = TRUE;
2724 #endif
2726 #ifdef FEAT_SPELL
2727 if (wp->w_p_spell
2728 && *wp->w_buffer->b_p_spl != NUL
2729 && wp->w_buffer->b_langp.ga_len > 0
2730 && *(char **)(wp->w_buffer->b_langp.ga_data) != NULL)
2732 /* Prepare for spell checking. */
2733 has_spell = TRUE;
2734 extra_check = TRUE;
2736 /* Get the start of the next line, so that words that wrap to the next
2737 * line are found too: "et<line-break>al.".
2738 * Trick: skip a few chars for C/shell/Vim comments */
2739 nextline[SPWORDLEN] = NUL;
2740 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2742 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
2743 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
2746 /* When a word wrapped from the previous line the start of the current
2747 * line is valid. */
2748 if (lnum == checked_lnum)
2749 cur_checked_col = checked_col;
2750 checked_lnum = 0;
2752 /* When there was a sentence end in the previous line may require a
2753 * word starting with capital in this line. In line 1 always check
2754 * the first word. */
2755 if (lnum != capcol_lnum)
2756 cap_col = -1;
2757 if (lnum == 1)
2758 cap_col = 0;
2759 capcol_lnum = 0;
2761 #endif
2764 * handle visual active in this window
2766 fromcol = -10;
2767 tocol = MAXCOL;
2768 #ifdef FEAT_VISUAL
2769 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2771 /* Visual is after curwin->w_cursor */
2772 if (ltoreq(curwin->w_cursor, VIsual))
2774 top = &curwin->w_cursor;
2775 bot = &VIsual;
2777 else /* Visual is before curwin->w_cursor */
2779 top = &VIsual;
2780 bot = &curwin->w_cursor;
2782 if (VIsual_mode == Ctrl_V) /* block mode */
2784 if (lnum >= top->lnum && lnum <= bot->lnum)
2786 fromcol = wp->w_old_cursor_fcol;
2787 tocol = wp->w_old_cursor_lcol;
2790 else /* non-block mode */
2792 if (lnum > top->lnum && lnum <= bot->lnum)
2793 fromcol = 0;
2794 else if (lnum == top->lnum)
2796 if (VIsual_mode == 'V') /* linewise */
2797 fromcol = 0;
2798 else
2800 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
2801 if (gchar_pos(top) == NUL)
2802 tocol = fromcol + 1;
2805 if (VIsual_mode != 'V' && lnum == bot->lnum)
2807 if (*p_sel == 'e' && bot->col == 0
2808 #ifdef FEAT_VIRTUALEDIT
2809 && bot->coladd == 0
2810 #endif
2813 fromcol = -10;
2814 tocol = MAXCOL;
2816 else if (bot->col == MAXCOL)
2817 tocol = MAXCOL;
2818 else
2820 pos = *bot;
2821 if (*p_sel == 'e')
2822 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
2823 else
2825 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
2826 ++tocol;
2832 #ifndef MSDOS
2833 /* Check if the character under the cursor should not be inverted */
2834 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
2835 # ifdef FEAT_GUI
2836 && !gui.in_use
2837 # endif
2839 noinvcur = TRUE;
2840 #endif
2842 /* if inverting in this line set area_highlighting */
2843 if (fromcol >= 0)
2845 area_highlighting = TRUE;
2846 attr = hl_attr(HLF_V);
2847 #if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
2848 if (clip_star.available && !clip_star.owned && clip_isautosel())
2849 attr = hl_attr(HLF_VNC);
2850 #endif
2855 * handle 'incsearch' and ":s///c" highlighting
2857 else
2858 #endif /* FEAT_VISUAL */
2859 if (highlight_match
2860 && wp == curwin
2861 && lnum >= curwin->w_cursor.lnum
2862 && lnum <= curwin->w_cursor.lnum + search_match_lines)
2864 if (lnum == curwin->w_cursor.lnum)
2865 getvcol(curwin, &(curwin->w_cursor),
2866 (colnr_T *)&fromcol, NULL, NULL);
2867 else
2868 fromcol = 0;
2869 if (lnum == curwin->w_cursor.lnum + search_match_lines)
2871 pos.lnum = lnum;
2872 pos.col = search_match_endcol;
2873 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
2875 else
2876 tocol = MAXCOL;
2877 if (fromcol == tocol) /* do at least one character */
2878 tocol = fromcol + 1; /* happens when past end of line */
2879 area_highlighting = TRUE;
2880 attr = hl_attr(HLF_I);
2883 #ifdef FEAT_DIFF
2884 filler_lines = diff_check(wp, lnum);
2885 if (filler_lines < 0)
2887 if (filler_lines == -1)
2889 if (diff_find_change(wp, lnum, &change_start, &change_end))
2890 diff_hlf = HLF_ADD; /* added line */
2891 else if (change_start == 0)
2892 diff_hlf = HLF_TXD; /* changed text */
2893 else
2894 diff_hlf = HLF_CHD; /* changed line */
2896 else
2897 diff_hlf = HLF_ADD; /* added line */
2898 filler_lines = 0;
2899 area_highlighting = TRUE;
2901 if (lnum == wp->w_topline)
2902 filler_lines = wp->w_topfill;
2903 filler_todo = filler_lines;
2904 #endif
2906 #ifdef LINE_ATTR
2907 # ifdef FEAT_SIGNS
2908 /* If this line has a sign with line highlighting set line_attr. */
2909 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
2910 if (v != 0)
2911 line_attr = sign_get_attr((int)v, TRUE);
2912 # endif
2913 # if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
2914 /* Highlight the current line in the quickfix window. */
2915 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
2916 line_attr = hl_attr(HLF_L);
2917 # endif
2918 if (line_attr != 0)
2919 area_highlighting = TRUE;
2920 #endif
2922 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
2923 ptr = line;
2925 #ifdef FEAT_SPELL
2926 if (has_spell)
2928 /* For checking first word with a capital skip white space. */
2929 if (cap_col == 0)
2930 cap_col = (int)(skipwhite(line) - line);
2932 /* To be able to spell-check over line boundaries copy the end of the
2933 * current line into nextline[]. Above the start of the next line was
2934 * copied to nextline[SPWORDLEN]. */
2935 if (nextline[SPWORDLEN] == NUL)
2937 /* No next line or it is empty. */
2938 nextlinecol = MAXCOL;
2939 nextline_idx = 0;
2941 else
2943 v = (long)STRLEN(line);
2944 if (v < SPWORDLEN)
2946 /* Short line, use it completely and append the start of the
2947 * next line. */
2948 nextlinecol = 0;
2949 mch_memmove(nextline, line, (size_t)v);
2950 mch_memmove(nextline + v, nextline + SPWORDLEN,
2951 STRLEN(nextline + SPWORDLEN) + 1);
2952 nextline_idx = v + 1;
2954 else
2956 /* Long line, use only the last SPWORDLEN bytes. */
2957 nextlinecol = v - SPWORDLEN;
2958 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
2959 nextline_idx = SPWORDLEN + 1;
2963 #endif
2965 /* find start of trailing whitespace */
2966 if (wp->w_p_list && lcs_trail)
2968 trailcol = (colnr_T)STRLEN(ptr);
2969 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
2970 --trailcol;
2971 trailcol += (colnr_T) (ptr - line);
2972 extra_check = TRUE;
2976 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
2977 * first character to be displayed.
2979 if (wp->w_p_wrap)
2980 v = wp->w_skipcol;
2981 else
2982 v = wp->w_leftcol;
2983 if (v > 0)
2985 #ifdef FEAT_MBYTE
2986 char_u *prev_ptr = ptr;
2987 #endif
2988 while (vcol < v && *ptr != NUL)
2990 c = win_lbr_chartabsize(wp, ptr, (colnr_T)vcol, NULL);
2991 vcol += c;
2992 #ifdef FEAT_MBYTE
2993 prev_ptr = ptr;
2994 #endif
2995 mb_ptr_adv(ptr);
2998 #ifdef FEAT_VIRTUALEDIT
2999 /* When 'virtualedit' is set the end of the line may be before the
3000 * start of the displayed part. */
3001 if (vcol < v && *ptr == NUL && virtual_active())
3002 vcol = v;
3003 #endif
3005 /* Handle a character that's not completely on the screen: Put ptr at
3006 * that character but skip the first few screen characters. */
3007 if (vcol > v)
3009 vcol -= c;
3010 #ifdef FEAT_MBYTE
3011 ptr = prev_ptr;
3012 #else
3013 --ptr;
3014 #endif
3015 n_skip = v - vcol;
3019 * Adjust for when the inverted text is before the screen,
3020 * and when the start of the inverted text is before the screen.
3022 if (tocol <= vcol)
3023 fromcol = 0;
3024 else if (fromcol >= 0 && fromcol < vcol)
3025 fromcol = vcol;
3027 #ifdef FEAT_LINEBREAK
3028 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3029 if (wp->w_p_wrap)
3030 need_showbreak = TRUE;
3031 #endif
3032 #ifdef FEAT_SPELL
3033 /* When spell checking a word we need to figure out the start of the
3034 * word and if it's badly spelled or not. */
3035 if (has_spell)
3037 int len;
3038 hlf_T spell_hlf = HLF_COUNT;
3040 pos = wp->w_cursor;
3041 wp->w_cursor.lnum = lnum;
3042 wp->w_cursor.col = (colnr_T)(ptr - line);
3043 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
3044 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
3046 /* no bad word found at line start, don't check until end of a
3047 * word */
3048 spell_hlf = HLF_COUNT;
3049 word_end = (int)(spell_to_word_end(ptr, wp->w_buffer) - line + 1);
3051 else
3053 /* bad word found, use attributes until end of word */
3054 word_end = wp->w_cursor.col + len + 1;
3056 /* Turn index into actual attributes. */
3057 if (spell_hlf != HLF_COUNT)
3058 spell_attr = highlight_attr[spell_hlf];
3060 wp->w_cursor = pos;
3062 # ifdef FEAT_SYN_HL
3063 /* Need to restart syntax highlighting for this line. */
3064 if (has_syntax)
3065 syntax_start(wp, lnum);
3066 # endif
3068 #endif
3072 * Correct highlighting for cursor that can't be disabled.
3073 * Avoids having to check this for each character.
3075 if (fromcol >= 0)
3077 if (noinvcur)
3079 if ((colnr_T)fromcol == wp->w_virtcol)
3081 /* highlighting starts at cursor, let it start just after the
3082 * cursor */
3083 fromcol_prev = fromcol;
3084 fromcol = -1;
3086 else if ((colnr_T)fromcol < wp->w_virtcol)
3087 /* restart highlighting after the cursor */
3088 fromcol_prev = wp->w_virtcol;
3090 if (fromcol >= tocol)
3091 fromcol = -1;
3094 #ifdef FEAT_SEARCH_EXTRA
3096 * Handle highlighting the last used search pattern and matches.
3097 * Do this for both search_hl and the match list.
3099 cur = wp->w_match_head;
3100 shl_flag = FALSE;
3101 while (cur != NULL || shl_flag == FALSE)
3103 if (shl_flag == FALSE)
3105 shl = &search_hl;
3106 shl_flag = TRUE;
3108 else
3109 shl = &cur->hl;
3110 shl->startcol = MAXCOL;
3111 shl->endcol = MAXCOL;
3112 shl->attr_cur = 0;
3113 if (shl->rm.regprog != NULL)
3115 v = (long)(ptr - line);
3116 next_search_hl(wp, shl, lnum, (colnr_T)v);
3118 /* Need to get the line again, a multi-line regexp may have made it
3119 * invalid. */
3120 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3121 ptr = line + v;
3123 if (shl->lnum != 0 && shl->lnum <= lnum)
3125 if (shl->lnum == lnum)
3126 shl->startcol = shl->rm.startpos[0].col;
3127 else
3128 shl->startcol = 0;
3129 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3130 - shl->rm.startpos[0].lnum)
3131 shl->endcol = shl->rm.endpos[0].col;
3132 else
3133 shl->endcol = MAXCOL;
3134 /* Highlight one character for an empty match. */
3135 if (shl->startcol == shl->endcol)
3137 #ifdef FEAT_MBYTE
3138 if (has_mbyte && line[shl->endcol] != NUL)
3139 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3140 else
3141 #endif
3142 ++shl->endcol;
3144 if ((long)shl->startcol < v) /* match at leftcol */
3146 shl->attr_cur = shl->attr;
3147 search_attr = shl->attr;
3149 area_highlighting = TRUE;
3152 if (shl != &search_hl && cur != NULL)
3153 cur = cur->next;
3155 #endif
3157 #ifdef FEAT_SYN_HL
3158 /* Cursor line highlighting for 'cursorline'. Not when Visual mode is
3159 * active, because it's not clear what is selected then. */
3160 if (wp->w_p_cul && lnum == wp->w_cursor.lnum && !VIsual_active)
3162 line_attr = hl_attr(HLF_CUL);
3163 area_highlighting = TRUE;
3165 #endif
3167 off = (unsigned)(current_ScreenLine - ScreenLines);
3168 col = 0;
3169 #ifdef FEAT_RIGHTLEFT
3170 if (wp->w_p_rl)
3172 /* Rightleft window: process the text in the normal direction, but put
3173 * it in current_ScreenLine[] from right to left. Start at the
3174 * rightmost column of the window. */
3175 col = W_WIDTH(wp) - 1;
3176 off += col;
3178 #endif
3181 * Repeat for the whole displayed line.
3183 for (;;)
3185 /* Skip this quickly when working on the text. */
3186 if (draw_state != WL_LINE)
3188 #ifdef FEAT_CMDWIN
3189 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3191 draw_state = WL_CMDLINE;
3192 if (cmdwin_type != 0 && wp == curwin)
3194 /* Draw the cmdline character. */
3195 n_extra = 1;
3196 c_extra = cmdwin_type;
3197 char_attr = hl_attr(HLF_AT);
3200 #endif
3202 #ifdef FEAT_FOLDING
3203 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3205 draw_state = WL_FOLD;
3206 if (wp->w_p_fdc > 0)
3208 /* Draw the 'foldcolumn'. */
3209 fill_foldcolumn(extra, wp, FALSE, lnum);
3210 n_extra = wp->w_p_fdc;
3211 p_extra = extra;
3212 p_extra[n_extra] = NUL;
3213 c_extra = NUL;
3214 char_attr = hl_attr(HLF_FC);
3217 #endif
3219 #ifdef FEAT_SIGNS
3220 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3222 draw_state = WL_SIGN;
3223 /* Show the sign column when there are any signs in this
3224 * buffer or when using Netbeans. */
3225 if (draw_signcolumn(wp)
3226 # ifdef FEAT_DIFF
3227 && filler_todo <= 0
3228 # endif
3231 int_u text_sign;
3232 # ifdef FEAT_SIGN_ICONS
3233 int_u icon_sign;
3234 # endif
3236 /* Draw two cells with the sign value or blank. */
3237 c_extra = ' ';
3238 char_attr = hl_attr(HLF_SC);
3239 n_extra = 2;
3241 if (row == startrow)
3243 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3244 SIGN_TEXT);
3245 # ifdef FEAT_SIGN_ICONS
3246 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3247 SIGN_ICON);
3248 if (gui.in_use && icon_sign != 0)
3250 /* Use the image in this position. */
3251 c_extra = SIGN_BYTE;
3252 # ifdef FEAT_NETBEANS_INTG
3253 if (buf_signcount(wp->w_buffer, lnum) > 1)
3254 c_extra = MULTISIGN_BYTE;
3255 # endif
3256 char_attr = icon_sign;
3258 else
3259 # endif
3260 if (text_sign != 0)
3262 p_extra = sign_get_text(text_sign);
3263 if (p_extra != NULL)
3265 c_extra = NUL;
3266 n_extra = (int)STRLEN(p_extra);
3268 char_attr = sign_get_attr(text_sign, FALSE);
3273 #endif
3275 if (draw_state == WL_NR - 1 && n_extra == 0)
3277 draw_state = WL_NR;
3278 /* Display the line number. After the first fill with blanks
3279 * when the 'n' flag isn't in 'cpo' */
3280 if (wp->w_p_nu
3281 && (row == startrow
3282 #ifdef FEAT_DIFF
3283 + filler_lines
3284 #endif
3285 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3287 /* Draw the line number (empty space after wrapping). */
3288 if (row == startrow
3289 #ifdef FEAT_DIFF
3290 + filler_lines
3291 #endif
3294 sprintf((char *)extra, "%*ld ",
3295 number_width(wp), (long)lnum);
3296 if (wp->w_skipcol > 0)
3297 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3298 *p_extra = '-';
3299 #ifdef FEAT_RIGHTLEFT
3300 if (wp->w_p_rl) /* reverse line numbers */
3301 rl_mirror(extra);
3302 #endif
3303 p_extra = extra;
3304 c_extra = NUL;
3306 else
3307 c_extra = ' ';
3308 n_extra = number_width(wp) + 1;
3309 char_attr = hl_attr(HLF_N);
3310 #ifdef FEAT_SYN_HL
3311 /* When 'cursorline' is set highlight the line number of
3312 * the current line differently. */
3313 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3314 char_attr = hl_combine_attr(hl_attr(HLF_CUL), char_attr);
3315 #endif
3319 #if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3320 if (draw_state == WL_SBR - 1 && n_extra == 0)
3322 draw_state = WL_SBR;
3323 # ifdef FEAT_DIFF
3324 if (filler_todo > 0)
3326 /* Draw "deleted" diff line(s). */
3327 if (char2cells(fill_diff) > 1)
3328 c_extra = '-';
3329 else
3330 c_extra = fill_diff;
3331 # ifdef FEAT_RIGHTLEFT
3332 if (wp->w_p_rl)
3333 n_extra = col + 1;
3334 else
3335 # endif
3336 n_extra = W_WIDTH(wp) - col;
3337 char_attr = hl_attr(HLF_DED);
3339 # endif
3340 # ifdef FEAT_LINEBREAK
3341 if (*p_sbr != NUL && need_showbreak)
3343 /* Draw 'showbreak' at the start of each broken line. */
3344 p_extra = p_sbr;
3345 c_extra = NUL;
3346 n_extra = (int)STRLEN(p_sbr);
3347 char_attr = hl_attr(HLF_AT);
3348 need_showbreak = FALSE;
3349 /* Correct end of highlighted area for 'showbreak',
3350 * required when 'linebreak' is also set. */
3351 if (tocol == vcol)
3352 tocol += n_extra;
3354 # endif
3356 #endif
3358 if (draw_state == WL_LINE - 1 && n_extra == 0)
3360 draw_state = WL_LINE;
3361 if (saved_n_extra)
3363 /* Continue item from end of wrapped line. */
3364 n_extra = saved_n_extra;
3365 c_extra = saved_c_extra;
3366 p_extra = saved_p_extra;
3367 char_attr = saved_char_attr;
3369 else
3370 char_attr = 0;
3374 /* When still displaying '$' of change command, stop at cursor */
3375 if (dollar_vcol != 0 && wp == curwin
3376 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
3377 #ifdef FEAT_DIFF
3378 && filler_todo <= 0
3379 #endif
3382 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3383 wp->w_p_rl);
3384 /* Pretend we have finished updating the window. Except when
3385 * 'cursorcolumn' is set. */
3386 #ifdef FEAT_SYN_HL
3387 if (wp->w_p_cuc)
3388 row = wp->w_cline_row + wp->w_cline_height;
3389 else
3390 #endif
3391 row = wp->w_height;
3392 break;
3395 if (draw_state == WL_LINE && area_highlighting)
3397 /* handle Visual or match highlighting in this line */
3398 if (vcol == fromcol
3399 #ifdef FEAT_MBYTE
3400 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3401 && (*mb_ptr2cells)(ptr) > 1)
3402 #endif
3403 || ((int)vcol_prev == fromcol_prev
3404 && vcol < tocol))
3405 area_attr = attr; /* start highlighting */
3406 else if (area_attr != 0
3407 && (vcol == tocol
3408 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
3409 area_attr = 0; /* stop highlighting */
3411 #ifdef FEAT_SEARCH_EXTRA
3412 if (!n_extra)
3415 * Check for start/end of search pattern match.
3416 * After end, check for start/end of next match.
3417 * When another match, have to check for start again.
3418 * Watch out for matching an empty string!
3419 * Do this for 'search_hl' and the match list (ordered by
3420 * priority).
3422 v = (long)(ptr - line);
3423 cur = wp->w_match_head;
3424 shl_flag = FALSE;
3425 while (cur != NULL || shl_flag == FALSE)
3427 if (shl_flag == FALSE
3428 && ((cur != NULL
3429 && cur->priority > SEARCH_HL_PRIORITY)
3430 || cur == NULL))
3432 shl = &search_hl;
3433 shl_flag = TRUE;
3435 else
3436 shl = &cur->hl;
3437 while (shl->rm.regprog != NULL)
3439 if (shl->startcol != MAXCOL
3440 && v >= (long)shl->startcol
3441 && v < (long)shl->endcol)
3443 shl->attr_cur = shl->attr;
3445 else if (v == (long)shl->endcol)
3447 shl->attr_cur = 0;
3449 next_search_hl(wp, shl, lnum, (colnr_T)v);
3451 /* Need to get the line again, a multi-line regexp
3452 * may have made it invalid. */
3453 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3454 ptr = line + v;
3456 if (shl->lnum == lnum)
3458 shl->startcol = shl->rm.startpos[0].col;
3459 if (shl->rm.endpos[0].lnum == 0)
3460 shl->endcol = shl->rm.endpos[0].col;
3461 else
3462 shl->endcol = MAXCOL;
3464 if (shl->startcol == shl->endcol)
3466 /* highlight empty match, try again after
3467 * it */
3468 #ifdef FEAT_MBYTE
3469 if (has_mbyte)
3470 shl->endcol += (*mb_ptr2len)(line
3471 + shl->endcol);
3472 else
3473 #endif
3474 ++shl->endcol;
3477 /* Loop to check if the match starts at the
3478 * current position */
3479 continue;
3482 break;
3484 if (shl != &search_hl && cur != NULL)
3485 cur = cur->next;
3488 /* Use attributes from match with highest priority among
3489 * 'search_hl' and the match list. */
3490 search_attr = search_hl.attr_cur;
3491 cur = wp->w_match_head;
3492 shl_flag = FALSE;
3493 while (cur != NULL || shl_flag == FALSE)
3495 if (shl_flag == FALSE
3496 && ((cur != NULL
3497 && cur->priority > SEARCH_HL_PRIORITY)
3498 || cur == NULL))
3500 shl = &search_hl;
3501 shl_flag = TRUE;
3503 else
3504 shl = &cur->hl;
3505 if (shl->attr_cur != 0)
3506 search_attr = shl->attr_cur;
3507 if (shl != &search_hl && cur != NULL)
3508 cur = cur->next;
3511 #endif
3513 #ifdef FEAT_DIFF
3514 if (diff_hlf != (hlf_T)0)
3516 if (diff_hlf == HLF_CHD && ptr - line >= change_start
3517 && n_extra == 0)
3518 diff_hlf = HLF_TXD; /* changed text */
3519 if (diff_hlf == HLF_TXD && ptr - line > change_end
3520 && n_extra == 0)
3521 diff_hlf = HLF_CHD; /* changed line */
3522 line_attr = hl_attr(diff_hlf);
3524 #endif
3526 /* Decide which of the highlight attributes to use. */
3527 attr_pri = TRUE;
3528 if (area_attr != 0)
3529 char_attr = area_attr;
3530 else if (search_attr != 0)
3531 char_attr = search_attr;
3532 #ifdef LINE_ATTR
3533 /* Use line_attr when not in the Visual or 'incsearch' area
3534 * (area_attr may be 0 when "noinvcur" is set). */
3535 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
3536 || (vcol < fromcol || vcol >= tocol)))
3537 char_attr = line_attr;
3538 #endif
3539 else
3541 attr_pri = FALSE;
3542 #ifdef FEAT_SYN_HL
3543 if (has_syntax)
3544 char_attr = syntax_attr;
3545 else
3546 #endif
3547 char_attr = 0;
3552 * Get the next character to put on the screen.
3555 * The "p_extra" points to the extra stuff that is inserted to
3556 * represent special characters (non-printable stuff) and other
3557 * things. When all characters are the same, c_extra is used.
3558 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
3559 * "p_extra[n_extra]".
3560 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
3562 if (n_extra > 0)
3564 if (c_extra != NUL)
3566 c = c_extra;
3567 #ifdef FEAT_MBYTE
3568 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
3569 if (enc_utf8 && (*mb_char2len)(c) > 1)
3571 mb_utf8 = TRUE;
3572 u8cc[0] = 0;
3573 c = 0xc0;
3575 else
3576 mb_utf8 = FALSE;
3577 #endif
3579 else
3581 c = *p_extra;
3582 #ifdef FEAT_MBYTE
3583 if (has_mbyte)
3585 mb_c = c;
3586 if (enc_utf8)
3588 /* If the UTF-8 character is more than one byte:
3589 * Decode it into "mb_c". */
3590 mb_l = (*mb_ptr2len)(p_extra);
3591 mb_utf8 = FALSE;
3592 if (mb_l > n_extra)
3593 mb_l = 1;
3594 else if (mb_l > 1)
3596 mb_c = utfc_ptr2char(p_extra, u8cc);
3597 mb_utf8 = TRUE;
3598 c = 0xc0;
3601 else
3603 /* if this is a DBCS character, put it in "mb_c" */
3604 mb_l = MB_BYTE2LEN(c);
3605 if (mb_l >= n_extra)
3606 mb_l = 1;
3607 else if (mb_l > 1)
3608 mb_c = (c << 8) + p_extra[1];
3610 if (mb_l == 0) /* at the NUL at end-of-line */
3611 mb_l = 1;
3613 /* If a double-width char doesn't fit display a '>' in the
3614 * last column. */
3615 if ((
3616 # ifdef FEAT_RIGHTLEFT
3617 wp->w_p_rl ? (col <= 0) :
3618 # endif
3619 (col >= W_WIDTH(wp) - 1))
3620 && (*mb_char2cells)(mb_c) == 2)
3622 c = '>';
3623 mb_c = c;
3624 mb_l = 1;
3625 mb_utf8 = FALSE;
3626 multi_attr = hl_attr(HLF_AT);
3627 /* put the pointer back to output the double-width
3628 * character at the start of the next line. */
3629 ++n_extra;
3630 --p_extra;
3632 else
3634 n_extra -= mb_l - 1;
3635 p_extra += mb_l - 1;
3638 #endif
3639 ++p_extra;
3641 --n_extra;
3643 else
3646 * Get a character from the line itself.
3648 c = *ptr;
3649 #ifdef FEAT_MBYTE
3650 if (has_mbyte)
3652 mb_c = c;
3653 if (enc_utf8)
3655 /* If the UTF-8 character is more than one byte: Decode it
3656 * into "mb_c". */
3657 mb_l = (*mb_ptr2len)(ptr);
3658 mb_utf8 = FALSE;
3659 if (mb_l > 1)
3661 mb_c = utfc_ptr2char(ptr, u8cc);
3662 /* Overlong encoded ASCII or ASCII with composing char
3663 * is displayed normally, except a NUL. */
3664 if (mb_c < 0x80)
3665 c = mb_c;
3666 mb_utf8 = TRUE;
3668 /* At start of the line we can have a composing char.
3669 * Draw it as a space with a composing char. */
3670 if (utf_iscomposing(mb_c))
3672 int i;
3674 for (i = Screen_mco - 1; i > 0; --i)
3675 u8cc[i] = u8cc[i - 1];
3676 u8cc[0] = mb_c;
3677 mb_c = ' ';
3681 if ((mb_l == 1 && c >= 0x80)
3682 || (mb_l >= 1 && mb_c == 0)
3683 || (mb_l > 1 && (!vim_isprintc(mb_c)
3684 # ifdef UNICODE16
3685 || mb_c >= 0x10000
3686 # endif
3690 * Illegal UTF-8 byte: display as <xx>.
3691 * Non-BMP character : display as ? or fullwidth ?.
3693 # ifdef UNICODE16
3694 if (mb_c < 0x10000)
3695 # endif
3697 transchar_hex(extra, mb_c);
3698 # ifdef FEAT_RIGHTLEFT
3699 if (wp->w_p_rl) /* reverse */
3700 rl_mirror(extra);
3701 # endif
3703 # ifdef UNICODE16
3704 else if (utf_char2cells(mb_c) != 2)
3705 STRCPY(extra, "?");
3706 else
3707 /* 0xff1f in UTF-8: full-width '?' */
3708 STRCPY(extra, "\357\274\237");
3709 # endif
3711 p_extra = extra;
3712 c = *p_extra;
3713 mb_c = mb_ptr2char_adv(&p_extra);
3714 mb_utf8 = (c >= 0x80);
3715 n_extra = (int)STRLEN(p_extra);
3716 c_extra = NUL;
3717 if (area_attr == 0 && search_attr == 0)
3719 n_attr = n_extra + 1;
3720 extra_attr = hl_attr(HLF_8);
3721 saved_attr2 = char_attr; /* save current attr */
3724 else if (mb_l == 0) /* at the NUL at end-of-line */
3725 mb_l = 1;
3726 #ifdef FEAT_ARABIC
3727 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
3729 /* Do Arabic shaping. */
3730 int pc, pc1, nc;
3731 int pcc[MAX_MCO];
3733 /* The idea of what is the previous and next
3734 * character depends on 'rightleft'. */
3735 if (wp->w_p_rl)
3737 pc = prev_c;
3738 pc1 = prev_c1;
3739 nc = utf_ptr2char(ptr + mb_l);
3740 prev_c1 = u8cc[0];
3742 else
3744 pc = utfc_ptr2char(ptr + mb_l, pcc);
3745 nc = prev_c;
3746 pc1 = pcc[0];
3748 prev_c = mb_c;
3750 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
3752 else
3753 prev_c = mb_c;
3754 #endif
3756 else /* enc_dbcs */
3758 mb_l = MB_BYTE2LEN(c);
3759 if (mb_l == 0) /* at the NUL at end-of-line */
3760 mb_l = 1;
3761 else if (mb_l > 1)
3763 /* We assume a second byte below 32 is illegal.
3764 * Hopefully this is OK for all double-byte encodings!
3766 if (ptr[1] >= 32)
3767 mb_c = (c << 8) + ptr[1];
3768 else
3770 if (ptr[1] == NUL)
3772 /* head byte at end of line */
3773 mb_l = 1;
3774 transchar_nonprint(extra, c);
3776 else
3778 /* illegal tail byte */
3779 mb_l = 2;
3780 STRCPY(extra, "XX");
3782 p_extra = extra;
3783 n_extra = (int)STRLEN(extra) - 1;
3784 c_extra = NUL;
3785 c = *p_extra++;
3786 if (area_attr == 0 && search_attr == 0)
3788 n_attr = n_extra + 1;
3789 extra_attr = hl_attr(HLF_8);
3790 saved_attr2 = char_attr; /* save current attr */
3792 mb_c = c;
3796 /* If a double-width char doesn't fit display a '>' in the
3797 * last column; the character is displayed at the start of the
3798 * next line. */
3799 if ((
3800 # ifdef FEAT_RIGHTLEFT
3801 wp->w_p_rl ? (col <= 0) :
3802 # endif
3803 (col >= W_WIDTH(wp) - 1))
3804 && (*mb_char2cells)(mb_c) == 2)
3806 c = '>';
3807 mb_c = c;
3808 mb_utf8 = FALSE;
3809 mb_l = 1;
3810 multi_attr = hl_attr(HLF_AT);
3811 /* Put pointer back so that the character will be
3812 * displayed at the start of the next line. */
3813 --ptr;
3815 else if (*ptr != NUL)
3816 ptr += mb_l - 1;
3818 /* If a double-width char doesn't fit at the left side display
3819 * a '<' in the first column. */
3820 if (n_skip > 0 && mb_l > 1)
3822 n_extra = 1;
3823 c_extra = '<';
3824 c = ' ';
3825 if (area_attr == 0 && search_attr == 0)
3827 n_attr = n_extra + 1;
3828 extra_attr = hl_attr(HLF_AT);
3829 saved_attr2 = char_attr; /* save current attr */
3831 mb_c = c;
3832 mb_utf8 = FALSE;
3833 mb_l = 1;
3837 #endif
3838 ++ptr;
3840 /* 'list' : change char 160 to lcs_nbsp. */
3841 if (wp->w_p_list && (c == 160
3842 #ifdef FEAT_MBYTE
3843 || (mb_utf8 && mb_c == 160)
3844 #endif
3845 ) && lcs_nbsp)
3847 c = lcs_nbsp;
3848 if (area_attr == 0 && search_attr == 0)
3850 n_attr = 1;
3851 extra_attr = hl_attr(HLF_8);
3852 saved_attr2 = char_attr; /* save current attr */
3854 #ifdef FEAT_MBYTE
3855 mb_c = c;
3856 if (enc_utf8 && (*mb_char2len)(c) > 1)
3858 mb_utf8 = TRUE;
3859 u8cc[0] = 0;
3860 c = 0xc0;
3862 else
3863 mb_utf8 = FALSE;
3864 #endif
3867 if (extra_check)
3869 #ifdef FEAT_SPELL
3870 int can_spell = TRUE;
3871 #endif
3873 #ifdef FEAT_SYN_HL
3874 /* Get syntax attribute, unless still at the start of the line
3875 * (double-wide char that doesn't fit). */
3876 v = (long)(ptr - line);
3877 if (has_syntax && v > 0)
3879 /* Get the syntax attribute for the character. If there
3880 * is an error, disable syntax highlighting. */
3881 save_did_emsg = did_emsg;
3882 did_emsg = FALSE;
3884 syntax_attr = get_syntax_attr((colnr_T)v - 1,
3885 # ifdef FEAT_SPELL
3886 has_spell ? &can_spell :
3887 # endif
3888 NULL);
3890 if (did_emsg)
3892 wp->w_buffer->b_syn_error = TRUE;
3893 has_syntax = FALSE;
3895 else
3896 did_emsg = save_did_emsg;
3898 /* Need to get the line again, a multi-line regexp may
3899 * have made it invalid. */
3900 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3901 ptr = line + v;
3903 if (!attr_pri)
3904 char_attr = syntax_attr;
3905 else
3906 char_attr = hl_combine_attr(syntax_attr, char_attr);
3908 #endif
3910 #ifdef FEAT_SPELL
3911 /* Check spelling (unless at the end of the line).
3912 * Only do this when there is no syntax highlighting, the
3913 * @Spell cluster is not used or the current syntax item
3914 * contains the @Spell cluster. */
3915 if (has_spell && v >= word_end && v > cur_checked_col)
3917 spell_attr = 0;
3918 # ifdef FEAT_SYN_HL
3919 if (!attr_pri)
3920 char_attr = syntax_attr;
3921 # endif
3922 if (c != 0 && (
3923 # ifdef FEAT_SYN_HL
3924 !has_syntax ||
3925 # endif
3926 can_spell))
3928 char_u *prev_ptr, *p;
3929 int len;
3930 hlf_T spell_hlf = HLF_COUNT;
3931 # ifdef FEAT_MBYTE
3932 if (has_mbyte)
3934 prev_ptr = ptr - mb_l;
3935 v -= mb_l - 1;
3937 else
3938 # endif
3939 prev_ptr = ptr - 1;
3941 /* Use nextline[] if possible, it has the start of the
3942 * next line concatenated. */
3943 if ((prev_ptr - line) - nextlinecol >= 0)
3944 p = nextline + (prev_ptr - line) - nextlinecol;
3945 else
3946 p = prev_ptr;
3947 cap_col -= (int)(prev_ptr - line);
3948 len = spell_check(wp, p, &spell_hlf, &cap_col,
3949 nochange);
3950 word_end = v + len;
3952 /* In Insert mode only highlight a word that
3953 * doesn't touch the cursor. */
3954 if (spell_hlf != HLF_COUNT
3955 && (State & INSERT) != 0
3956 && wp->w_cursor.lnum == lnum
3957 && wp->w_cursor.col >=
3958 (colnr_T)(prev_ptr - line)
3959 && wp->w_cursor.col < (colnr_T)word_end)
3961 spell_hlf = HLF_COUNT;
3962 spell_redraw_lnum = lnum;
3965 if (spell_hlf == HLF_COUNT && p != prev_ptr
3966 && (p - nextline) + len > nextline_idx)
3968 /* Remember that the good word continues at the
3969 * start of the next line. */
3970 checked_lnum = lnum + 1;
3971 checked_col = (int)((p - nextline) + len - nextline_idx);
3974 /* Turn index into actual attributes. */
3975 if (spell_hlf != HLF_COUNT)
3976 spell_attr = highlight_attr[spell_hlf];
3978 if (cap_col > 0)
3980 if (p != prev_ptr
3981 && (p - nextline) + cap_col >= nextline_idx)
3983 /* Remember that the word in the next line
3984 * must start with a capital. */
3985 capcol_lnum = lnum + 1;
3986 cap_col = (int)((p - nextline) + cap_col
3987 - nextline_idx);
3989 else
3990 /* Compute the actual column. */
3991 cap_col += (int)(prev_ptr - line);
3995 if (spell_attr != 0)
3997 if (!attr_pri)
3998 char_attr = hl_combine_attr(char_attr, spell_attr);
3999 else
4000 char_attr = hl_combine_attr(spell_attr, char_attr);
4002 #endif
4003 #ifdef FEAT_LINEBREAK
4005 * Found last space before word: check for line break.
4007 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr)
4008 && !wp->w_p_list)
4010 n_extra = win_lbr_chartabsize(wp, ptr - (
4011 # ifdef FEAT_MBYTE
4012 has_mbyte ? mb_l :
4013 # endif
4014 1), (colnr_T)vcol, NULL) - 1;
4015 c_extra = ' ';
4016 if (vim_iswhite(c))
4017 c = ' ';
4019 #endif
4021 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4023 c = lcs_trail;
4024 if (!attr_pri)
4026 n_attr = 1;
4027 extra_attr = hl_attr(HLF_8);
4028 saved_attr2 = char_attr; /* save current attr */
4030 #ifdef FEAT_MBYTE
4031 mb_c = c;
4032 if (enc_utf8 && (*mb_char2len)(c) > 1)
4034 mb_utf8 = TRUE;
4035 u8cc[0] = 0;
4036 c = 0xc0;
4038 else
4039 mb_utf8 = FALSE;
4040 #endif
4045 * Handling of non-printable characters.
4047 if (!(chartab[c & 0xff] & CT_PRINT_CHAR))
4050 * when getting a character from the file, we may have to
4051 * turn it into something else on the way to putting it
4052 * into "ScreenLines".
4054 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4056 /* tab amount depends on current column */
4057 n_extra = (int)wp->w_buffer->b_p_ts
4058 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4059 #ifdef FEAT_MBYTE
4060 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4061 #endif
4062 if (wp->w_p_list)
4064 c = lcs_tab1;
4065 c_extra = lcs_tab2;
4066 n_attr = n_extra + 1;
4067 extra_attr = hl_attr(HLF_8);
4068 saved_attr2 = char_attr; /* save current attr */
4069 #ifdef FEAT_MBYTE
4070 mb_c = c;
4071 if (enc_utf8 && (*mb_char2len)(c) > 1)
4073 mb_utf8 = TRUE;
4074 u8cc[0] = 0;
4075 c = 0xc0;
4077 #endif
4079 else
4081 c_extra = ' ';
4082 c = ' ';
4085 else if (c == NUL
4086 && ((wp->w_p_list && lcs_eol > 0)
4087 || ((fromcol >= 0 || fromcol_prev >= 0)
4088 && tocol > vcol
4089 #ifdef FEAT_VISUAL
4090 && VIsual_mode != Ctrl_V
4091 #endif
4092 && (
4093 # ifdef FEAT_RIGHTLEFT
4094 wp->w_p_rl ? (col >= 0) :
4095 # endif
4096 (col < W_WIDTH(wp)))
4097 && !(noinvcur
4098 && (colnr_T)vcol == wp->w_virtcol)))
4099 && lcs_eol_one >= 0)
4101 /* Display a '$' after the line or highlight an extra
4102 * character if the line break is included. */
4103 #if defined(FEAT_DIFF) || defined(LINE_ATTR)
4104 /* For a diff line the highlighting continues after the
4105 * "$". */
4106 if (
4107 # ifdef FEAT_DIFF
4108 diff_hlf == (hlf_T)0
4109 # ifdef LINE_ATTR
4111 # endif
4112 # endif
4113 # ifdef LINE_ATTR
4114 line_attr == 0
4115 # endif
4117 #endif
4119 #ifdef FEAT_VIRTUALEDIT
4120 /* In virtualedit, visual selections may extend
4121 * beyond end of line. */
4122 if (area_highlighting && virtual_active()
4123 && tocol != MAXCOL && vcol < tocol)
4124 n_extra = 0;
4125 else
4126 #endif
4128 p_extra = at_end_str;
4129 n_extra = 1;
4130 c_extra = NUL;
4133 if (wp->w_p_list)
4134 c = lcs_eol;
4135 else
4136 c = ' ';
4137 lcs_eol_one = -1;
4138 --ptr; /* put it back at the NUL */
4139 if (!attr_pri)
4141 extra_attr = hl_attr(HLF_AT);
4142 n_attr = 1;
4144 #ifdef FEAT_MBYTE
4145 mb_c = c;
4146 if (enc_utf8 && (*mb_char2len)(c) > 1)
4148 mb_utf8 = TRUE;
4149 u8cc[0] = 0;
4150 c = 0xc0;
4152 else
4153 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4154 #endif
4156 else if (c != NUL)
4158 p_extra = transchar(c);
4159 #ifdef FEAT_RIGHTLEFT
4160 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4161 rl_mirror(p_extra); /* reverse "<12>" */
4162 #endif
4163 n_extra = byte2cells(c) - 1;
4164 c_extra = NUL;
4165 c = *p_extra++;
4166 if (!attr_pri)
4168 n_attr = n_extra + 1;
4169 extra_attr = hl_attr(HLF_8);
4170 saved_attr2 = char_attr; /* save current attr */
4172 #ifdef FEAT_MBYTE
4173 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4174 #endif
4176 #ifdef FEAT_VIRTUALEDIT
4177 else if (VIsual_active
4178 && (VIsual_mode == Ctrl_V
4179 || VIsual_mode == 'v')
4180 && virtual_active()
4181 && tocol != MAXCOL
4182 && vcol < tocol
4183 && (
4184 # ifdef FEAT_RIGHTLEFT
4185 wp->w_p_rl ? (col >= 0) :
4186 # endif
4187 (col < W_WIDTH(wp))))
4189 c = ' ';
4190 --ptr; /* put it back at the NUL */
4192 #endif
4193 #if defined(LINE_ATTR)
4194 else if ((
4195 # ifdef FEAT_DIFF
4196 diff_hlf != (hlf_T)0 ||
4197 # endif
4198 line_attr != 0
4199 ) && (
4200 # ifdef FEAT_RIGHTLEFT
4201 wp->w_p_rl ? (col >= 0) :
4202 # endif
4203 (col < W_WIDTH(wp))))
4205 /* Highlight until the right side of the window */
4206 c = ' ';
4207 --ptr; /* put it back at the NUL */
4209 /* Remember we do the char for line highlighting. */
4210 ++did_line_attr;
4212 /* don't do search HL for the rest of the line */
4213 if (line_attr != 0 && char_attr == search_attr && col > 0)
4214 char_attr = line_attr;
4215 # ifdef FEAT_DIFF
4216 if (diff_hlf == HLF_TXD)
4218 diff_hlf = HLF_CHD;
4219 if (attr == 0 || char_attr != attr)
4220 char_attr = hl_attr(diff_hlf);
4222 # endif
4224 #endif
4228 /* Don't override visual selection highlighting. */
4229 if (n_attr > 0
4230 && draw_state == WL_LINE
4231 && !attr_pri)
4232 char_attr = extra_attr;
4234 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
4235 /* XIM don't send preedit_start and preedit_end, but they send
4236 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
4237 * im_is_preediting() here. */
4238 if (xic != NULL
4239 && lnum == curwin->w_cursor.lnum
4240 && (State & INSERT)
4241 && !p_imdisable
4242 && im_is_preediting()
4243 && draw_state == WL_LINE)
4245 colnr_T tcol;
4247 if (preedit_end_col == MAXCOL)
4248 getvcol(curwin, &(curwin->w_cursor), &tcol, NULL, NULL);
4249 else
4250 tcol = preedit_end_col;
4251 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
4253 if (feedback_old_attr < 0)
4255 feedback_col = 0;
4256 feedback_old_attr = char_attr;
4258 char_attr = im_get_feedback_attr(feedback_col);
4259 if (char_attr < 0)
4260 char_attr = feedback_old_attr;
4261 feedback_col++;
4263 else if (feedback_old_attr >= 0)
4265 char_attr = feedback_old_attr;
4266 feedback_old_attr = -1;
4267 feedback_col = 0;
4270 #endif
4272 * Handle the case where we are in column 0 but not on the first
4273 * character of the line and the user wants us to show us a
4274 * special character (via 'listchars' option "precedes:<char>".
4276 if (lcs_prec_todo != NUL
4277 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
4278 #ifdef FEAT_DIFF
4279 && filler_todo <= 0
4280 #endif
4281 && draw_state > WL_NR
4282 && c != NUL)
4284 c = lcs_prec;
4285 lcs_prec_todo = NUL;
4286 #ifdef FEAT_MBYTE
4287 mb_c = c;
4288 if (enc_utf8 && (*mb_char2len)(c) > 1)
4290 mb_utf8 = TRUE;
4291 u8cc[0] = 0;
4292 c = 0xc0;
4294 else
4295 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4296 #endif
4297 if (!attr_pri)
4299 saved_attr3 = char_attr; /* save current attr */
4300 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
4301 n_attr3 = 1;
4306 * At end of the text line or just after the last character.
4308 if (c == NUL
4309 #if defined(LINE_ATTR)
4310 || did_line_attr == 1
4311 #endif
4314 #ifdef FEAT_SEARCH_EXTRA
4315 long prevcol = (long)(ptr - line) - (c == NUL);
4317 /* we're not really at that column when skipping some text */
4318 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
4319 ++prevcol;
4320 #endif
4322 /* invert at least one char, used for Visual and empty line or
4323 * highlight match at end of line. If it's beyond the last
4324 * char on the screen, just overwrite that one (tricky!) Not
4325 * needed when a '$' was displayed for 'list'. */
4326 #ifdef FEAT_SEARCH_EXTRA
4327 prevcol_hl_flag = FALSE;
4328 if (prevcol == (long)search_hl.startcol)
4329 prevcol_hl_flag = TRUE;
4330 else
4332 cur = wp->w_match_head;
4333 while (cur != NULL)
4335 if (prevcol == (long)cur->hl.startcol)
4337 prevcol_hl_flag = TRUE;
4338 break;
4340 cur = cur->next;
4343 #endif
4344 if (lcs_eol == lcs_eol_one
4345 && ((area_attr != 0 && vcol == fromcol && c == NUL)
4346 #ifdef FEAT_SEARCH_EXTRA
4347 /* highlight 'hlsearch' match at end of line */
4348 || (prevcol_hl_flag == TRUE
4349 # if defined(LINE_ATTR)
4350 && did_line_attr <= 1
4351 # endif
4353 #endif
4356 int n = 0;
4358 #ifdef FEAT_RIGHTLEFT
4359 if (wp->w_p_rl)
4361 if (col < 0)
4362 n = 1;
4364 else
4365 #endif
4367 if (col >= W_WIDTH(wp))
4368 n = -1;
4370 if (n != 0)
4372 /* At the window boundary, highlight the last character
4373 * instead (better than nothing). */
4374 off += n;
4375 col += n;
4377 else
4379 /* Add a blank character to highlight. */
4380 ScreenLines[off] = ' ';
4381 #ifdef FEAT_MBYTE
4382 if (enc_utf8)
4383 ScreenLinesUC[off] = 0;
4384 #endif
4386 #ifdef FEAT_SEARCH_EXTRA
4387 if (area_attr == 0)
4389 /* Use attributes from match with highest priority among
4390 * 'search_hl' and the match list. */
4391 char_attr = search_hl.attr;
4392 cur = wp->w_match_head;
4393 shl_flag = FALSE;
4394 while (cur != NULL || shl_flag == FALSE)
4396 if (shl_flag == FALSE
4397 && ((cur != NULL
4398 && cur->priority > SEARCH_HL_PRIORITY)
4399 || cur == NULL))
4401 shl = &search_hl;
4402 shl_flag = TRUE;
4404 else
4405 shl = &cur->hl;
4406 if ((ptr - line) - 1 == (long)shl->startcol)
4407 char_attr = shl->attr;
4408 if (shl != &search_hl && cur != NULL)
4409 cur = cur->next;
4412 #endif
4413 ScreenAttrs[off] = char_attr;
4414 #ifdef FEAT_RIGHTLEFT
4415 if (wp->w_p_rl)
4417 --col;
4418 --off;
4420 else
4421 #endif
4423 ++col;
4424 ++off;
4426 ++vcol;
4427 #ifdef FEAT_SYN_HL
4428 eol_hl_off = 1;
4429 #endif
4434 * At end of the text line.
4436 if (c == NUL)
4438 #ifdef FEAT_SYN_HL
4439 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol)
4441 /* highlight last char after line */
4442 --col;
4443 --off;
4444 --vcol;
4447 /* Highlight 'cursorcolumn' past end of the line. */
4448 if (wp->w_p_wrap)
4449 v = wp->w_skipcol;
4450 else
4451 v = wp->w_leftcol;
4452 /* check if line ends before left margin */
4453 if (vcol < v + col - win_col_off(wp))
4455 vcol = v + col - win_col_off(wp);
4456 if (wp->w_p_cuc
4457 && (int)wp->w_virtcol >= vcol - eol_hl_off
4458 && (int)wp->w_virtcol < W_WIDTH(wp) * (row - startrow + 1)
4460 && lnum != wp->w_cursor.lnum
4461 # ifdef FEAT_RIGHTLEFT
4462 && !wp->w_p_rl
4463 # endif
4466 while (col < W_WIDTH(wp))
4468 ScreenLines[off] = ' ';
4469 #ifdef FEAT_MBYTE
4470 if (enc_utf8)
4471 ScreenLinesUC[off] = 0;
4472 #endif
4473 ++col;
4474 if (vcol == (long)wp->w_virtcol)
4476 ScreenAttrs[off] = hl_attr(HLF_CUC);
4477 break;
4479 ScreenAttrs[off++] = 0;
4480 ++vcol;
4483 #endif
4485 SCREEN_LINE(screen_row, W_WINCOL(wp), col, (int)W_WIDTH(wp),
4486 wp->w_p_rl);
4487 row++;
4490 * Update w_cline_height and w_cline_folded if the cursor line was
4491 * updated (saves a call to plines() later).
4493 if (wp == curwin && lnum == curwin->w_cursor.lnum)
4495 curwin->w_cline_row = startrow;
4496 curwin->w_cline_height = row - startrow;
4497 #ifdef FEAT_FOLDING
4498 curwin->w_cline_folded = FALSE;
4499 #endif
4500 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
4503 break;
4506 /* line continues beyond line end */
4507 if (lcs_ext
4508 && !wp->w_p_wrap
4509 #ifdef FEAT_DIFF
4510 && filler_todo <= 0
4511 #endif
4512 && (
4513 #ifdef FEAT_RIGHTLEFT
4514 wp->w_p_rl ? col == 0 :
4515 #endif
4516 col == W_WIDTH(wp) - 1)
4517 && (*ptr != NUL
4518 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
4519 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
4521 c = lcs_ext;
4522 char_attr = hl_attr(HLF_AT);
4523 #ifdef FEAT_MBYTE
4524 mb_c = c;
4525 if (enc_utf8 && (*mb_char2len)(c) > 1)
4527 mb_utf8 = TRUE;
4528 u8cc[0] = 0;
4529 c = 0xc0;
4531 else
4532 mb_utf8 = FALSE;
4533 #endif
4536 #ifdef FEAT_SYN_HL
4537 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
4538 * highlight the cursor position itself. */
4539 if (wp->w_p_cuc && vcol == (long)wp->w_virtcol
4540 && lnum != wp->w_cursor.lnum
4541 && draw_state == WL_LINE)
4543 vcol_save_attr = char_attr;
4544 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
4546 else
4547 vcol_save_attr = -1;
4548 #endif
4551 * Store character to be displayed.
4552 * Skip characters that are left of the screen for 'nowrap'.
4554 vcol_prev = vcol;
4555 if (draw_state < WL_LINE || n_skip <= 0)
4558 * Store the character.
4560 #if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
4561 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
4563 /* A double-wide character is: put first halve in left cell. */
4564 --off;
4565 --col;
4567 #endif
4568 ScreenLines[off] = c;
4569 #ifdef FEAT_MBYTE
4570 if (enc_dbcs == DBCS_JPNU)
4571 ScreenLines2[off] = mb_c & 0xff;
4572 else if (enc_utf8)
4574 if (mb_utf8)
4576 int i;
4578 ScreenLinesUC[off] = mb_c;
4579 if ((c & 0xff) == 0)
4580 ScreenLines[off] = 0x80; /* avoid storing zero */
4581 for (i = 0; i < Screen_mco; ++i)
4583 ScreenLinesC[i][off] = u8cc[i];
4584 if (u8cc[i] == 0)
4585 break;
4588 else
4589 ScreenLinesUC[off] = 0;
4591 if (multi_attr)
4593 ScreenAttrs[off] = multi_attr;
4594 multi_attr = 0;
4596 else
4597 #endif
4598 ScreenAttrs[off] = char_attr;
4600 #ifdef FEAT_MBYTE
4601 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
4603 /* Need to fill two screen columns. */
4604 ++off;
4605 ++col;
4606 if (enc_utf8)
4607 /* UTF-8: Put a 0 in the second screen char. */
4608 ScreenLines[off] = 0;
4609 else
4610 /* DBCS: Put second byte in the second screen char. */
4611 ScreenLines[off] = mb_c & 0xff;
4612 ++vcol;
4613 /* When "tocol" is halfway a character, set it to the end of
4614 * the character, otherwise highlighting won't stop. */
4615 if (tocol == vcol)
4616 ++tocol;
4617 #ifdef FEAT_RIGHTLEFT
4618 if (wp->w_p_rl)
4620 /* now it's time to backup one cell */
4621 --off;
4622 --col;
4624 #endif
4626 #endif
4627 #ifdef FEAT_RIGHTLEFT
4628 if (wp->w_p_rl)
4630 --off;
4631 --col;
4633 else
4634 #endif
4636 ++off;
4637 ++col;
4640 else
4641 --n_skip;
4643 /* Only advance the "vcol" when after the 'number' column. */
4644 if (draw_state >= WL_SBR
4645 #ifdef FEAT_DIFF
4646 && filler_todo <= 0
4647 #endif
4649 ++vcol;
4651 #ifdef FEAT_SYN_HL
4652 if (vcol_save_attr >= 0)
4653 char_attr = vcol_save_attr;
4654 #endif
4656 /* restore attributes after "predeces" in 'listchars' */
4657 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
4658 char_attr = saved_attr3;
4660 /* restore attributes after last 'listchars' or 'number' char */
4661 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
4662 char_attr = saved_attr2;
4665 * At end of screen line and there is more to come: Display the line
4666 * so far. If there is no more to display it is caught above.
4668 if ((
4669 #ifdef FEAT_RIGHTLEFT
4670 wp->w_p_rl ? (col < 0) :
4671 #endif
4672 (col >= W_WIDTH(wp)))
4673 && (*ptr != NUL
4674 #ifdef FEAT_DIFF
4675 || filler_todo > 0
4676 #endif
4677 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
4678 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
4681 SCREEN_LINE(screen_row, W_WINCOL(wp), col, (int)W_WIDTH(wp),
4682 wp->w_p_rl);
4683 ++row;
4684 ++screen_row;
4686 /* When not wrapping and finished diff lines, or when displayed
4687 * '$' and highlighting until last column, break here. */
4688 if ((!wp->w_p_wrap
4689 #ifdef FEAT_DIFF
4690 && filler_todo <= 0
4691 #endif
4692 ) || lcs_eol_one == -1)
4693 break;
4695 /* When the window is too narrow draw all "@" lines. */
4696 if (draw_state != WL_LINE
4697 #ifdef FEAT_DIFF
4698 && filler_todo <= 0
4699 #endif
4702 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
4703 #ifdef FEAT_VERTSPLIT
4704 draw_vsep_win(wp, row);
4705 #endif
4706 row = endrow;
4709 /* When line got too long for screen break here. */
4710 if (row == endrow)
4712 ++row;
4713 break;
4716 if (screen_cur_row == screen_row - 1
4717 #ifdef FEAT_DIFF
4718 && filler_todo <= 0
4719 #endif
4720 && W_WIDTH(wp) == Columns)
4722 /* Remember that the line wraps, used for modeless copy. */
4723 LineWraps[screen_row - 1] = TRUE;
4726 * Special trick to make copy/paste of wrapped lines work with
4727 * xterm/screen: write an extra character beyond the end of
4728 * the line. This will work with all terminal types
4729 * (regardless of the xn,am settings).
4730 * Only do this on a fast tty.
4731 * Only do this if the cursor is on the current line
4732 * (something has been written in it).
4733 * Don't do this for the GUI.
4734 * Don't do this for double-width characters.
4735 * Don't do this for a window not at the right screen border.
4737 if (p_tf
4738 #ifdef FEAT_GUI
4739 && !gui.in_use
4740 #endif
4741 #ifdef FEAT_MBYTE
4742 && !(has_mbyte
4743 && ((*mb_off2cells)(LineOffset[screen_row],
4744 LineOffset[screen_row] + screen_Columns)
4745 == 2
4746 || (*mb_off2cells)(LineOffset[screen_row - 1]
4747 + (int)Columns - 2,
4748 LineOffset[screen_row] + screen_Columns)
4749 == 2))
4750 #endif
4753 /* First make sure we are at the end of the screen line,
4754 * then output the same character again to let the
4755 * terminal know about the wrap. If the terminal doesn't
4756 * auto-wrap, we overwrite the character. */
4757 if (screen_cur_col != W_WIDTH(wp))
4758 screen_char(LineOffset[screen_row - 1]
4759 + (unsigned)Columns - 1,
4760 screen_row - 1, (int)(Columns - 1));
4762 #ifdef FEAT_MBYTE
4763 /* When there is a multi-byte character, just output a
4764 * space to keep it simple. */
4765 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
4766 screen_row - 1] + (Columns - 1)]) > 1)
4767 out_char(' ');
4768 else
4769 #endif
4770 out_char(ScreenLines[LineOffset[screen_row - 1]
4771 + (Columns - 1)]);
4772 /* force a redraw of the first char on the next line */
4773 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
4774 screen_start(); /* don't know where cursor is now */
4778 col = 0;
4779 off = (unsigned)(current_ScreenLine - ScreenLines);
4780 #ifdef FEAT_RIGHTLEFT
4781 if (wp->w_p_rl)
4783 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
4784 off += col;
4786 #endif
4788 /* reset the drawing state for the start of a wrapped line */
4789 draw_state = WL_START;
4790 saved_n_extra = n_extra;
4791 saved_p_extra = p_extra;
4792 saved_c_extra = c_extra;
4793 saved_char_attr = char_attr;
4794 n_extra = 0;
4795 lcs_prec_todo = lcs_prec;
4796 #ifdef FEAT_LINEBREAK
4797 # ifdef FEAT_DIFF
4798 if (filler_todo <= 0)
4799 # endif
4800 need_showbreak = TRUE;
4801 #endif
4802 #ifdef FEAT_DIFF
4803 --filler_todo;
4804 /* When the filler lines are actually below the last line of the
4805 * file, don't draw the line itself, break here. */
4806 if (filler_todo == 0 && wp->w_botfill)
4807 break;
4808 #endif
4811 } /* for every character in the line */
4813 #ifdef FEAT_SPELL
4814 /* After an empty line check first word for capital. */
4815 if (*skipwhite(line) == NUL)
4817 capcol_lnum = lnum + 1;
4818 cap_col = 0;
4820 #endif
4822 return row;
4825 #ifdef FEAT_MBYTE
4826 static int comp_char_differs __ARGS((int, int));
4829 * Return if the composing characters at "off_from" and "off_to" differ.
4831 static int
4832 comp_char_differs(off_from, off_to)
4833 int off_from;
4834 int off_to;
4836 int i;
4838 for (i = 0; i < Screen_mco; ++i)
4840 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
4841 return TRUE;
4842 if (ScreenLinesC[i][off_from] == 0)
4843 break;
4845 return FALSE;
4847 #endif
4850 * Check whether the given character needs redrawing:
4851 * - the (first byte of the) character is different
4852 * - the attributes are different
4853 * - the character is multi-byte and the next byte is different
4855 static int
4856 char_needs_redraw(off_from, off_to, cols)
4857 int off_from;
4858 int off_to;
4859 int cols;
4861 if (cols > 0
4862 && ((ScreenLines[off_from] != ScreenLines[off_to]
4863 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
4865 #ifdef FEAT_MBYTE
4866 || (enc_dbcs != 0
4867 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
4868 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
4869 ? ScreenLines2[off_from] != ScreenLines2[off_to]
4870 : (cols > 1 && ScreenLines[off_from + 1]
4871 != ScreenLines[off_to + 1])))
4872 || (enc_utf8
4873 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
4874 || (ScreenLinesUC[off_from] != 0
4875 && comp_char_differs(off_from, off_to))))
4876 #endif
4878 return TRUE;
4879 return FALSE;
4883 * Move one "cooked" screen line to the screen, but only the characters that
4884 * have actually changed. Handle insert/delete character.
4885 * "coloff" gives the first column on the screen for this line.
4886 * "endcol" gives the columns where valid characters are.
4887 * "clear_width" is the width of the window. It's > 0 if the rest of the line
4888 * needs to be cleared, negative otherwise.
4889 * "rlflag" is TRUE in a rightleft window:
4890 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
4891 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
4893 static void
4894 screen_line(row, coloff, endcol, clear_width
4895 #ifdef FEAT_RIGHTLEFT
4896 , rlflag
4897 #endif
4899 int row;
4900 int coloff;
4901 int endcol;
4902 int clear_width;
4903 #ifdef FEAT_RIGHTLEFT
4904 int rlflag;
4905 #endif
4907 unsigned off_from;
4908 unsigned off_to;
4909 #ifdef FEAT_MBYTE
4910 unsigned max_off_from;
4911 unsigned max_off_to;
4912 #endif
4913 int col = 0;
4914 #if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_VERTSPLIT)
4915 int hl;
4916 #endif
4917 int force = FALSE; /* force update rest of the line */
4918 int redraw_this /* bool: does character need redraw? */
4919 #ifdef FEAT_GUI
4920 = TRUE /* For GUI when while-loop empty */
4921 #endif
4923 int redraw_next; /* redraw_this for next character */
4924 #ifdef FEAT_MBYTE
4925 int clear_next = FALSE;
4926 int char_cells; /* 1: normal char */
4927 /* 2: occupies two display cells */
4928 # define CHAR_CELLS char_cells
4929 #else
4930 # define CHAR_CELLS 1
4931 #endif
4933 # ifdef FEAT_CLIPBOARD
4934 clip_may_clear_selection(row, row);
4935 # endif
4937 off_from = (unsigned)(current_ScreenLine - ScreenLines);
4938 off_to = LineOffset[row] + coloff;
4939 #ifdef FEAT_MBYTE
4940 max_off_from = off_from + screen_Columns;
4941 max_off_to = LineOffset[row] + screen_Columns;
4942 #endif
4944 #ifdef FEAT_RIGHTLEFT
4945 if (rlflag)
4947 /* Clear rest first, because it's left of the text. */
4948 if (clear_width > 0)
4950 while (col <= endcol && ScreenLines[off_to] == ' '
4951 && ScreenAttrs[off_to] == 0
4952 # ifdef FEAT_MBYTE
4953 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
4954 # endif
4957 ++off_to;
4958 ++col;
4960 if (col <= endcol)
4961 screen_fill(row, row + 1, col + coloff,
4962 endcol + coloff + 1, ' ', ' ', 0);
4964 col = endcol + 1;
4965 off_to = LineOffset[row] + col + coloff;
4966 off_from += col;
4967 endcol = (clear_width > 0 ? clear_width : -clear_width);
4969 #endif /* FEAT_RIGHTLEFT */
4971 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
4973 while (col < endcol)
4975 #ifdef FEAT_MBYTE
4976 if (has_mbyte && (col + 1 < endcol))
4977 char_cells = (*mb_off2cells)(off_from, max_off_from);
4978 else
4979 char_cells = 1;
4980 #endif
4982 redraw_this = redraw_next;
4983 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
4984 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
4986 #ifdef FEAT_GUI
4987 /* If the next character was bold, then redraw the current character to
4988 * remove any pixels that might have spilt over into us. This only
4989 * happens in the GUI.
4991 if (redraw_next && gui.in_use)
4993 hl = ScreenAttrs[off_to + CHAR_CELLS];
4994 if (hl > HL_ALL)
4995 hl = syn_attr2attr(hl);
4996 if (hl & HL_BOLD)
4997 redraw_this = TRUE;
4999 #endif
5001 if (redraw_this)
5004 * Special handling when 'xs' termcap flag set (hpterm):
5005 * Attributes for characters are stored at the position where the
5006 * cursor is when writing the highlighting code. The
5007 * start-highlighting code must be written with the cursor on the
5008 * first highlighted character. The stop-highlighting code must
5009 * be written with the cursor just after the last highlighted
5010 * character.
5011 * Overwriting a character doesn't remove it's highlighting. Need
5012 * to clear the rest of the line, and force redrawing it
5013 * completely.
5015 if ( p_wiv
5016 && !force
5017 #ifdef FEAT_GUI
5018 && !gui.in_use
5019 #endif
5020 && ScreenAttrs[off_to] != 0
5021 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
5024 * Need to remove highlighting attributes here.
5026 windgoto(row, col + coloff);
5027 out_str(T_CE); /* clear rest of this screen line */
5028 screen_start(); /* don't know where cursor is now */
5029 force = TRUE; /* force redraw of rest of the line */
5030 redraw_next = TRUE; /* or else next char would miss out */
5033 * If the previous character was highlighted, need to stop
5034 * highlighting at this character.
5036 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
5038 screen_attr = ScreenAttrs[off_to - 1];
5039 term_windgoto(row, col + coloff);
5040 screen_stop_highlight();
5042 else
5043 screen_attr = 0; /* highlighting has stopped */
5045 #ifdef FEAT_MBYTE
5046 if (enc_dbcs != 0)
5048 /* Check if overwriting a double-byte with a single-byte or
5049 * the other way around requires another character to be
5050 * redrawn. For UTF-8 this isn't needed, because comparing
5051 * ScreenLinesUC[] is sufficient. */
5052 if (char_cells == 1
5053 && col + 1 < endcol
5054 && (*mb_off2cells)(off_to, max_off_to) > 1)
5056 /* Writing a single-cell character over a double-cell
5057 * character: need to redraw the next cell. */
5058 ScreenLines[off_to + 1] = 0;
5059 redraw_next = TRUE;
5061 else if (char_cells == 2
5062 && col + 2 < endcol
5063 && (*mb_off2cells)(off_to, max_off_to) == 1
5064 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
5066 /* Writing the second half of a double-cell character over
5067 * a double-cell character: need to redraw the second
5068 * cell. */
5069 ScreenLines[off_to + 2] = 0;
5070 redraw_next = TRUE;
5073 if (enc_dbcs == DBCS_JPNU)
5074 ScreenLines2[off_to] = ScreenLines2[off_from];
5076 /* When writing a single-width character over a double-width
5077 * character and at the end of the redrawn text, need to clear out
5078 * the right halve of the old character.
5079 * Also required when writing the right halve of a double-width
5080 * char over the left halve of an existing one. */
5081 if (has_mbyte && col + char_cells == endcol
5082 && ((char_cells == 1
5083 && (*mb_off2cells)(off_to, max_off_to) > 1)
5084 || (char_cells == 2
5085 && (*mb_off2cells)(off_to, max_off_to) == 1
5086 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
5087 clear_next = TRUE;
5088 #endif
5090 ScreenLines[off_to] = ScreenLines[off_from];
5091 #ifdef FEAT_MBYTE
5092 if (enc_utf8)
5094 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
5095 if (ScreenLinesUC[off_from] != 0)
5097 int i;
5099 for (i = 0; i < Screen_mco; ++i)
5100 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
5103 if (char_cells == 2)
5104 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
5105 #endif
5107 #if defined(FEAT_GUI) || defined(UNIX)
5108 /* The bold trick makes a single row of pixels appear in the next
5109 * character. When a bold character is removed, the next
5110 * character should be redrawn too. This happens for our own GUI
5111 * and for some xterms. */
5112 if (
5113 # ifdef FEAT_GUI
5114 gui.in_use
5115 # endif
5116 # if defined(FEAT_GUI) && defined(UNIX)
5118 # endif
5119 # ifdef UNIX
5120 term_is_xterm
5121 # endif
5124 hl = ScreenAttrs[off_to];
5125 if (hl > HL_ALL)
5126 hl = syn_attr2attr(hl);
5127 if (hl & HL_BOLD)
5128 redraw_next = TRUE;
5130 #endif
5131 ScreenAttrs[off_to] = ScreenAttrs[off_from];
5132 #ifdef FEAT_MBYTE
5133 /* For simplicity set the attributes of second half of a
5134 * double-wide character equal to the first half. */
5135 if (char_cells == 2)
5136 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
5138 if (enc_dbcs != 0 && char_cells == 2)
5139 screen_char_2(off_to, row, col + coloff);
5140 else
5141 #endif
5142 screen_char(off_to, row, col + coloff);
5144 else if ( p_wiv
5145 #ifdef FEAT_GUI
5146 && !gui.in_use
5147 #endif
5148 && col + coloff > 0)
5150 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
5153 * Don't output stop-highlight when moving the cursor, it will
5154 * stop the highlighting when it should continue.
5156 screen_attr = 0;
5158 else if (screen_attr != 0)
5159 screen_stop_highlight();
5162 off_to += CHAR_CELLS;
5163 off_from += CHAR_CELLS;
5164 col += CHAR_CELLS;
5167 #ifdef FEAT_MBYTE
5168 if (clear_next)
5170 /* Clear the second half of a double-wide character of which the left
5171 * half was overwritten with a single-wide character. */
5172 ScreenLines[off_to] = ' ';
5173 if (enc_utf8)
5174 ScreenLinesUC[off_to] = 0;
5175 screen_char(off_to, row, col + coloff);
5177 #endif
5179 if (clear_width > 0
5180 #ifdef FEAT_RIGHTLEFT
5181 && !rlflag
5182 #endif
5185 #ifdef FEAT_GUI
5186 int startCol = col;
5187 #endif
5189 /* blank out the rest of the line */
5190 while (col < clear_width && ScreenLines[off_to] == ' '
5191 && ScreenAttrs[off_to] == 0
5192 #ifdef FEAT_MBYTE
5193 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5194 #endif
5197 ++off_to;
5198 ++col;
5200 if (col < clear_width)
5202 #ifdef FEAT_GUI
5204 * In the GUI, clearing the rest of the line may leave pixels
5205 * behind if the first character cleared was bold. Some bold
5206 * fonts spill over the left. In this case we redraw the previous
5207 * character too. If we didn't skip any blanks above, then we
5208 * only redraw if the character wasn't already redrawn anyway.
5210 if (gui.in_use && (col > startCol || !redraw_this))
5212 hl = ScreenAttrs[off_to];
5213 if (hl > HL_ALL || (hl & HL_BOLD))
5215 int prev_cells = 1;
5216 # ifdef FEAT_MBYTE
5217 if (enc_utf8)
5218 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
5219 * that its width is 2. */
5220 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
5221 else if (enc_dbcs != 0)
5223 /* find previous character by counting from first
5224 * column and get its width. */
5225 unsigned off = LineOffset[row];
5226 unsigned max_off = LineOffset[row] + screen_Columns;
5228 while (off < off_to)
5230 prev_cells = (*mb_off2cells)(off, max_off);
5231 off += prev_cells;
5235 if (enc_dbcs != 0 && prev_cells > 1)
5236 screen_char_2(off_to - prev_cells, row,
5237 col + coloff - prev_cells);
5238 else
5239 # endif
5240 screen_char(off_to - prev_cells, row,
5241 col + coloff - prev_cells);
5244 #endif
5245 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
5246 ' ', ' ', 0);
5247 #ifdef FEAT_VERTSPLIT
5248 off_to += clear_width - col;
5249 col = clear_width;
5250 #endif
5254 if (clear_width > 0)
5256 #ifdef FEAT_VERTSPLIT
5257 /* For a window that's left of another, draw the separator char. */
5258 if (col + coloff < Columns)
5260 int c;
5262 c = fillchar_vsep(&hl);
5263 if (ScreenLines[off_to] != c
5264 # ifdef FEAT_MBYTE
5265 || (enc_utf8 && (int)ScreenLinesUC[off_to]
5266 != (c >= 0x80 ? c : 0))
5267 # endif
5268 || ScreenAttrs[off_to] != hl)
5270 ScreenLines[off_to] = c;
5271 ScreenAttrs[off_to] = hl;
5272 # ifdef FEAT_MBYTE
5273 if (enc_utf8)
5275 if (c >= 0x80)
5277 ScreenLinesUC[off_to] = c;
5278 ScreenLinesC[0][off_to] = 0;
5280 else
5281 ScreenLinesUC[off_to] = 0;
5283 # endif
5284 screen_char(off_to, row, col + coloff);
5287 else
5288 #endif
5289 LineWraps[row] = FALSE;
5293 #if defined(FEAT_RIGHTLEFT) || defined(PROTO)
5295 * Mirror text "str" for right-left displaying.
5296 * Only works for single-byte characters (e.g., numbers).
5298 void
5299 rl_mirror(str)
5300 char_u *str;
5302 char_u *p1, *p2;
5303 int t;
5305 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
5307 t = *p1;
5308 *p1 = *p2;
5309 *p2 = t;
5312 #endif
5314 #if defined(FEAT_WINDOWS) || defined(PROTO)
5316 * mark all status lines for redraw; used after first :cd
5318 void
5319 status_redraw_all()
5321 win_T *wp;
5323 for (wp = firstwin; wp; wp = wp->w_next)
5324 if (wp->w_status_height)
5326 wp->w_redr_status = TRUE;
5327 redraw_later(VALID);
5332 * mark all status lines of the current buffer for redraw
5334 void
5335 status_redraw_curbuf()
5337 win_T *wp;
5339 for (wp = firstwin; wp; wp = wp->w_next)
5340 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
5342 wp->w_redr_status = TRUE;
5343 redraw_later(VALID);
5348 * Redraw all status lines that need to be redrawn.
5350 void
5351 redraw_statuslines()
5353 win_T *wp;
5355 for (wp = firstwin; wp; wp = wp->w_next)
5356 if (wp->w_redr_status)
5357 win_redr_status(wp);
5358 if (redraw_tabline)
5359 draw_tabline();
5361 #endif
5363 #if (defined(FEAT_WILDMENU) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
5365 * Redraw all status lines at the bottom of frame "frp".
5367 void
5368 win_redraw_last_status(frp)
5369 frame_T *frp;
5371 if (frp->fr_layout == FR_LEAF)
5372 frp->fr_win->w_redr_status = TRUE;
5373 else if (frp->fr_layout == FR_ROW)
5375 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
5376 win_redraw_last_status(frp);
5378 else /* frp->fr_layout == FR_COL */
5380 frp = frp->fr_child;
5381 while (frp->fr_next != NULL)
5382 frp = frp->fr_next;
5383 win_redraw_last_status(frp);
5386 #endif
5388 #ifdef FEAT_VERTSPLIT
5390 * Draw the verticap separator right of window "wp" starting with line "row".
5392 static void
5393 draw_vsep_win(wp, row)
5394 win_T *wp;
5395 int row;
5397 int hl;
5398 int c;
5400 if (wp->w_vsep_width)
5402 /* draw the vertical separator right of this window */
5403 c = fillchar_vsep(&hl);
5404 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
5405 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
5406 c, ' ', hl);
5409 #endif
5411 #ifdef FEAT_WILDMENU
5412 static int status_match_len __ARGS((expand_T *xp, char_u *s));
5413 static int skip_status_match_char __ARGS((expand_T *xp, char_u *s));
5416 * Get the length of an item as it will be shown in the status line.
5418 static int
5419 status_match_len(xp, s)
5420 expand_T *xp;
5421 char_u *s;
5423 int len = 0;
5425 #ifdef FEAT_MENU
5426 int emenu = (xp->xp_context == EXPAND_MENUS
5427 || xp->xp_context == EXPAND_MENUNAMES);
5429 /* Check for menu separators - replace with '|'. */
5430 if (emenu && menu_is_separator(s))
5431 return 1;
5432 #endif
5434 while (*s != NUL)
5436 if (skip_status_match_char(xp, s))
5437 ++s;
5438 len += ptr2cells(s);
5439 mb_ptr_adv(s);
5442 return len;
5446 * Return TRUE for characters that are not displayed in a status match.
5447 * These are backslashes used for escaping. Do show backslashes in help tags.
5449 static int
5450 skip_status_match_char(xp, s)
5451 expand_T *xp;
5452 char_u *s;
5454 return ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
5455 #ifdef FEAT_MENU
5456 || ((xp->xp_context == EXPAND_MENUS
5457 || xp->xp_context == EXPAND_MENUNAMES)
5458 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
5459 #endif
5464 * Show wildchar matches in the status line.
5465 * Show at least the "match" item.
5466 * We start at item 'first_match' in the list and show all matches that fit.
5468 * If inversion is possible we use it. Else '=' characters are used.
5470 void
5471 win_redr_status_matches(xp, num_matches, matches, match, showtail)
5472 expand_T *xp;
5473 int num_matches;
5474 char_u **matches; /* list of matches */
5475 int match;
5476 int showtail;
5478 #define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
5479 int row;
5480 char_u *buf;
5481 int len;
5482 int clen; /* length in screen cells */
5483 int fillchar;
5484 int attr;
5485 int i;
5486 int highlight = TRUE;
5487 char_u *selstart = NULL;
5488 int selstart_col = 0;
5489 char_u *selend = NULL;
5490 static int first_match = 0;
5491 int add_left = FALSE;
5492 char_u *s;
5493 #ifdef FEAT_MENU
5494 int emenu;
5495 #endif
5496 #if defined(FEAT_MBYTE) || defined(FEAT_MENU)
5497 int l;
5498 #endif
5500 if (matches == NULL) /* interrupted completion? */
5501 return;
5503 #ifdef FEAT_MBYTE
5504 if (has_mbyte)
5505 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
5506 else
5507 #endif
5508 buf = alloc((unsigned)Columns + 1);
5509 if (buf == NULL)
5510 return;
5512 if (match == -1) /* don't show match but original text */
5514 match = 0;
5515 highlight = FALSE;
5517 /* count 1 for the ending ">" */
5518 clen = status_match_len(xp, L_MATCH(match)) + 3;
5519 if (match == 0)
5520 first_match = 0;
5521 else if (match < first_match)
5523 /* jumping left, as far as we can go */
5524 first_match = match;
5525 add_left = TRUE;
5527 else
5529 /* check if match fits on the screen */
5530 for (i = first_match; i < match; ++i)
5531 clen += status_match_len(xp, L_MATCH(i)) + 2;
5532 if (first_match > 0)
5533 clen += 2;
5534 /* jumping right, put match at the left */
5535 if ((long)clen > Columns)
5537 first_match = match;
5538 /* if showing the last match, we can add some on the left */
5539 clen = 2;
5540 for (i = match; i < num_matches; ++i)
5542 clen += status_match_len(xp, L_MATCH(i)) + 2;
5543 if ((long)clen >= Columns)
5544 break;
5546 if (i == num_matches)
5547 add_left = TRUE;
5550 if (add_left)
5551 while (first_match > 0)
5553 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
5554 if ((long)clen >= Columns)
5555 break;
5556 --first_match;
5559 fillchar = fillchar_status(&attr, TRUE);
5561 if (first_match == 0)
5563 *buf = NUL;
5564 len = 0;
5566 else
5568 STRCPY(buf, "< ");
5569 len = 2;
5571 clen = len;
5573 i = first_match;
5574 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
5576 if (i == match)
5578 selstart = buf + len;
5579 selstart_col = clen;
5582 s = L_MATCH(i);
5583 /* Check for menu separators - replace with '|' */
5584 #ifdef FEAT_MENU
5585 emenu = (xp->xp_context == EXPAND_MENUS
5586 || xp->xp_context == EXPAND_MENUNAMES);
5587 if (emenu && menu_is_separator(s))
5589 STRCPY(buf + len, transchar('|'));
5590 l = (int)STRLEN(buf + len);
5591 len += l;
5592 clen += l;
5594 else
5595 #endif
5596 for ( ; *s != NUL; ++s)
5598 if (skip_status_match_char(xp, s))
5599 ++s;
5600 clen += ptr2cells(s);
5601 #ifdef FEAT_MBYTE
5602 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
5604 STRNCPY(buf + len, s, l);
5605 s += l - 1;
5606 len += l;
5608 else
5609 #endif
5611 STRCPY(buf + len, transchar_byte(*s));
5612 len += (int)STRLEN(buf + len);
5615 if (i == match)
5616 selend = buf + len;
5618 *(buf + len++) = ' ';
5619 *(buf + len++) = ' ';
5620 clen += 2;
5621 if (++i == num_matches)
5622 break;
5625 if (i != num_matches)
5627 *(buf + len++) = '>';
5628 ++clen;
5631 buf[len] = NUL;
5633 row = cmdline_row - 1;
5634 if (row >= 0)
5636 if (wild_menu_showing == 0)
5638 if (msg_scrolled > 0)
5640 /* Put the wildmenu just above the command line. If there is
5641 * no room, scroll the screen one line up. */
5642 if (cmdline_row == Rows - 1)
5644 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
5645 ++msg_scrolled;
5647 else
5649 ++cmdline_row;
5650 ++row;
5652 wild_menu_showing = WM_SCROLLED;
5654 else
5656 /* Create status line if needed by setting 'laststatus' to 2.
5657 * Set 'winminheight' to zero to avoid that the window is
5658 * resized. */
5659 if (lastwin->w_status_height == 0)
5661 save_p_ls = p_ls;
5662 save_p_wmh = p_wmh;
5663 p_ls = 2;
5664 p_wmh = 0;
5665 last_status(FALSE);
5667 wild_menu_showing = WM_SHOWN;
5671 screen_puts(buf, row, 0, attr);
5672 if (selstart != NULL && highlight)
5674 *selend = NUL;
5675 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
5678 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
5681 #ifdef FEAT_VERTSPLIT
5682 win_redraw_last_status(topframe);
5683 #else
5684 lastwin->w_redr_status = TRUE;
5685 #endif
5686 vim_free(buf);
5688 #endif
5690 #if defined(FEAT_WINDOWS) || defined(PROTO)
5692 * Redraw the status line of window wp.
5694 * If inversion is possible we use it. Else '=' characters are used.
5696 void
5697 win_redr_status(wp)
5698 win_T *wp;
5700 int row;
5701 char_u *p;
5702 int len;
5703 int fillchar;
5704 int attr;
5705 int this_ru_col;
5707 wp->w_redr_status = FALSE;
5708 if (wp->w_status_height == 0)
5710 /* no status line, can only be last window */
5711 redraw_cmdline = TRUE;
5713 else if (!redrawing()
5714 #ifdef FEAT_INS_EXPAND
5715 /* don't update status line when popup menu is visible and may be
5716 * drawn over it */
5717 || pum_visible()
5718 #endif
5721 /* Don't redraw right now, do it later. */
5722 wp->w_redr_status = TRUE;
5724 #ifdef FEAT_STL_OPT
5725 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
5727 /* redraw custom status line */
5728 redraw_custum_statusline(wp);
5730 #endif
5731 else
5733 fillchar = fillchar_status(&attr, wp == curwin);
5735 get_trans_bufname(wp->w_buffer);
5736 p = NameBuff;
5737 len = (int)STRLEN(p);
5739 if (wp->w_buffer->b_help
5740 #ifdef FEAT_QUICKFIX
5741 || wp->w_p_pvw
5742 #endif
5743 || bufIsChanged(wp->w_buffer)
5744 || wp->w_buffer->b_p_ro)
5745 *(p + len++) = ' ';
5746 if (wp->w_buffer->b_help)
5748 STRCPY(p + len, _("[Help]"));
5749 len += (int)STRLEN(p + len);
5751 #ifdef FEAT_QUICKFIX
5752 if (wp->w_p_pvw)
5754 STRCPY(p + len, _("[Preview]"));
5755 len += (int)STRLEN(p + len);
5757 #endif
5758 if (bufIsChanged(wp->w_buffer))
5760 STRCPY(p + len, "[+]");
5761 len += 3;
5763 if (wp->w_buffer->b_p_ro)
5765 STRCPY(p + len, "[RO]");
5766 len += 4;
5769 #ifndef FEAT_VERTSPLIT
5770 this_ru_col = ru_col;
5771 if (this_ru_col < (Columns + 1) / 2)
5772 this_ru_col = (Columns + 1) / 2;
5773 #else
5774 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
5775 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
5776 this_ru_col = (W_WIDTH(wp) + 1) / 2;
5777 if (this_ru_col <= 1)
5779 p = (char_u *)"<"; /* No room for file name! */
5780 len = 1;
5782 else
5783 #endif
5784 #ifdef FEAT_MBYTE
5785 if (has_mbyte)
5787 int clen = 0, i;
5789 /* Count total number of display cells. */
5790 for (i = 0; p[i] != NUL; i += (*mb_ptr2len)(p + i))
5791 clen += (*mb_ptr2cells)(p + i);
5792 /* Find first character that will fit.
5793 * Going from start to end is much faster for DBCS. */
5794 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
5795 i += (*mb_ptr2len)(p + i))
5796 clen -= (*mb_ptr2cells)(p + i);
5797 len = clen;
5798 if (i > 0)
5800 p = p + i - 1;
5801 *p = '<';
5802 ++len;
5806 else
5807 #endif
5808 if (len > this_ru_col - 1)
5810 p += len - (this_ru_col - 1);
5811 *p = '<';
5812 len = this_ru_col - 1;
5815 row = W_WINROW(wp) + wp->w_height;
5816 screen_puts(p, row, W_WINCOL(wp), attr);
5817 screen_fill(row, row + 1, len + W_WINCOL(wp),
5818 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
5820 if (get_keymap_str(wp, NameBuff, MAXPATHL)
5821 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
5822 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
5823 - 1 + W_WINCOL(wp)), attr);
5825 #ifdef FEAT_CMDL_INFO
5826 win_redr_ruler(wp, TRUE);
5827 #endif
5830 #ifdef FEAT_VERTSPLIT
5832 * May need to draw the character below the vertical separator.
5834 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
5836 if (stl_connected(wp))
5837 fillchar = fillchar_status(&attr, wp == curwin);
5838 else
5839 fillchar = fillchar_vsep(&attr);
5840 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
5841 attr);
5843 #endif
5846 #ifdef FEAT_STL_OPT
5848 * Redraw the status line according to 'statusline' and take care of any
5849 * errors encountered.
5851 static void
5852 redraw_custum_statusline(wp)
5853 win_T *wp;
5855 int save_called_emsg = called_emsg;
5857 called_emsg = FALSE;
5858 win_redr_custom(wp, FALSE);
5859 if (called_emsg)
5860 set_string_option_direct((char_u *)"statusline", -1,
5861 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
5862 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
5863 called_emsg |= save_called_emsg;
5865 #endif
5867 # ifdef FEAT_VERTSPLIT
5869 * Return TRUE if the status line of window "wp" is connected to the status
5870 * line of the window right of it. If not, then it's a vertical separator.
5871 * Only call if (wp->w_vsep_width != 0).
5874 stl_connected(wp)
5875 win_T *wp;
5877 frame_T *fr;
5879 fr = wp->w_frame;
5880 while (fr->fr_parent != NULL)
5882 if (fr->fr_parent->fr_layout == FR_COL)
5884 if (fr->fr_next != NULL)
5885 break;
5887 else
5889 if (fr->fr_next != NULL)
5890 return TRUE;
5892 fr = fr->fr_parent;
5894 return FALSE;
5896 # endif
5898 #endif /* FEAT_WINDOWS */
5900 #if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
5902 * Get the value to show for the language mappings, active 'keymap'.
5905 get_keymap_str(wp, buf, len)
5906 win_T *wp;
5907 char_u *buf; /* buffer for the result */
5908 int len; /* length of buffer */
5910 char_u *p;
5912 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
5913 return FALSE;
5916 #ifdef FEAT_EVAL
5917 buf_T *old_curbuf = curbuf;
5918 win_T *old_curwin = curwin;
5919 char_u *s;
5921 curbuf = wp->w_buffer;
5922 curwin = wp;
5923 STRCPY(buf, "b:keymap_name"); /* must be writable */
5924 ++emsg_skip;
5925 s = p = eval_to_string(buf, NULL, FALSE);
5926 --emsg_skip;
5927 curbuf = old_curbuf;
5928 curwin = old_curwin;
5929 if (p == NULL || *p == NUL)
5930 #endif
5932 #ifdef FEAT_KEYMAP
5933 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
5934 p = wp->w_buffer->b_p_keymap;
5935 else
5936 #endif
5937 p = (char_u *)"lang";
5939 if ((int)(STRLEN(p) + 3) < len)
5940 sprintf((char *)buf, "<%s>", p);
5941 else
5942 buf[0] = NUL;
5943 #ifdef FEAT_EVAL
5944 vim_free(s);
5945 #endif
5947 return buf[0] != NUL;
5949 #endif
5951 #if defined(FEAT_STL_OPT) || defined(PROTO)
5953 * Redraw the status line or ruler of window "wp".
5954 * When "wp" is NULL redraw the tab pages line from 'tabline'.
5956 static void
5957 win_redr_custom(wp, draw_ruler)
5958 win_T *wp;
5959 int draw_ruler; /* TRUE or FALSE */
5961 int attr;
5962 int curattr;
5963 int row;
5964 int col = 0;
5965 int maxwidth;
5966 int width;
5967 int n;
5968 int len;
5969 int fillchar;
5970 char_u buf[MAXPATHL];
5971 char_u *p;
5972 struct stl_hlrec hltab[STL_MAX_ITEM];
5973 struct stl_hlrec tabtab[STL_MAX_ITEM];
5974 int use_sandbox = FALSE;
5976 /* setup environment for the task at hand */
5977 if (wp == NULL)
5979 /* Use 'tabline'. Always at the first line of the screen. */
5980 p = p_tal;
5981 row = 0;
5982 fillchar = ' ';
5983 attr = hl_attr(HLF_TPF);
5984 maxwidth = Columns;
5985 # ifdef FEAT_EVAL
5986 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
5987 # endif
5989 else
5991 row = W_WINROW(wp) + wp->w_height;
5992 fillchar = fillchar_status(&attr, wp == curwin);
5993 maxwidth = W_WIDTH(wp);
5995 if (draw_ruler)
5997 p = p_ruf;
5998 /* advance past any leading group spec - implicit in ru_col */
5999 if (*p == '%')
6001 if (*++p == '-')
6002 p++;
6003 if (atoi((char *) p))
6004 while (VIM_ISDIGIT(*p))
6005 p++;
6006 if (*p++ != '(')
6007 p = p_ruf;
6009 #ifdef FEAT_VERTSPLIT
6010 col = ru_col - (Columns - W_WIDTH(wp));
6011 if (col < (W_WIDTH(wp) + 1) / 2)
6012 col = (W_WIDTH(wp) + 1) / 2;
6013 #else
6014 col = ru_col;
6015 if (col > (Columns + 1) / 2)
6016 col = (Columns + 1) / 2;
6017 #endif
6018 maxwidth = W_WIDTH(wp) - col;
6019 #ifdef FEAT_WINDOWS
6020 if (!wp->w_status_height)
6021 #endif
6023 row = Rows - 1;
6024 --maxwidth; /* writing in last column may cause scrolling */
6025 fillchar = ' ';
6026 attr = 0;
6029 # ifdef FEAT_EVAL
6030 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
6031 # endif
6033 else
6035 if (*wp->w_p_stl != NUL)
6036 p = wp->w_p_stl;
6037 else
6038 p = p_stl;
6039 # ifdef FEAT_EVAL
6040 use_sandbox = was_set_insecurely((char_u *)"statusline",
6041 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
6042 # endif
6045 #ifdef FEAT_VERTSPLIT
6046 col += W_WINCOL(wp);
6047 #endif
6050 if (maxwidth <= 0)
6051 return;
6053 width = build_stl_str_hl(wp == NULL ? curwin : wp,
6054 buf, sizeof(buf),
6055 p, use_sandbox,
6056 fillchar, maxwidth, hltab, tabtab);
6057 len = (int)STRLEN(buf);
6059 while (width < maxwidth && len < sizeof(buf) - 1)
6061 #ifdef FEAT_MBYTE
6062 len += (*mb_char2bytes)(fillchar, buf + len);
6063 #else
6064 buf[len++] = fillchar;
6065 #endif
6066 ++width;
6068 buf[len] = NUL;
6071 * Draw each snippet with the specified highlighting.
6073 curattr = attr;
6074 p = buf;
6075 for (n = 0; hltab[n].start != NULL; n++)
6077 len = (int)(hltab[n].start - p);
6078 screen_puts_len(p, len, row, col, curattr);
6079 col += vim_strnsize(p, len);
6080 p = hltab[n].start;
6082 if (hltab[n].userhl == 0)
6083 curattr = attr;
6084 else if (hltab[n].userhl < 0)
6085 curattr = syn_id2attr(-hltab[n].userhl);
6086 #ifdef FEAT_WINDOWS
6087 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
6088 curattr = highlight_stlnc[hltab[n].userhl - 1];
6089 #endif
6090 else
6091 curattr = highlight_user[hltab[n].userhl - 1];
6093 screen_puts(p, row, col, curattr);
6095 if (wp == NULL)
6097 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
6098 col = 0;
6099 len = 0;
6100 p = buf;
6101 fillchar = 0;
6102 for (n = 0; tabtab[n].start != NULL; n++)
6104 len += vim_strnsize(p, (int)(tabtab[n].start - p));
6105 while (col < len)
6106 TabPageIdxs[col++] = fillchar;
6107 p = tabtab[n].start;
6108 fillchar = tabtab[n].userhl;
6110 while (col < Columns)
6111 TabPageIdxs[col++] = fillchar;
6115 #endif /* FEAT_STL_OPT */
6118 * Output a single character directly to the screen and update ScreenLines.
6120 void
6121 screen_putchar(c, row, col, attr)
6122 int c;
6123 int row, col;
6124 int attr;
6126 #ifdef FEAT_MBYTE
6127 char_u buf[MB_MAXBYTES + 1];
6129 buf[(*mb_char2bytes)(c, buf)] = NUL;
6130 #else
6131 char_u buf[2];
6133 buf[0] = c;
6134 buf[1] = NUL;
6135 #endif
6136 screen_puts(buf, row, col, attr);
6140 * Get a single character directly from ScreenLines into "bytes[]".
6141 * Also return its attribute in *attrp;
6143 void
6144 screen_getbytes(row, col, bytes, attrp)
6145 int row, col;
6146 char_u *bytes;
6147 int *attrp;
6149 unsigned off;
6151 /* safety check */
6152 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
6154 off = LineOffset[row] + col;
6155 *attrp = ScreenAttrs[off];
6156 bytes[0] = ScreenLines[off];
6157 bytes[1] = NUL;
6159 #ifdef FEAT_MBYTE
6160 if (enc_utf8 && ScreenLinesUC[off] != 0)
6161 bytes[utfc_char2bytes(off, bytes)] = NUL;
6162 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6164 bytes[0] = ScreenLines[off];
6165 bytes[1] = ScreenLines2[off];
6166 bytes[2] = NUL;
6168 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
6170 bytes[1] = ScreenLines[off + 1];
6171 bytes[2] = NUL;
6173 #endif
6177 #ifdef FEAT_MBYTE
6178 static int screen_comp_differs __ARGS((int, int*));
6181 * Return TRUE if composing characters for screen posn "off" differs from
6182 * composing characters in "u8cc".
6184 static int
6185 screen_comp_differs(off, u8cc)
6186 int off;
6187 int *u8cc;
6189 int i;
6191 for (i = 0; i < Screen_mco; ++i)
6193 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
6194 return TRUE;
6195 if (u8cc[i] == 0)
6196 break;
6198 return FALSE;
6200 #endif
6203 * Put string '*text' on the screen at position 'row' and 'col', with
6204 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
6205 * Note: only outputs within one row, message is truncated at screen boundary!
6206 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
6208 void
6209 screen_puts(text, row, col, attr)
6210 char_u *text;
6211 int row;
6212 int col;
6213 int attr;
6215 screen_puts_len(text, -1, row, col, attr);
6219 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
6220 * a NUL.
6222 void
6223 screen_puts_len(text, len, row, col, attr)
6224 char_u *text;
6225 int len;
6226 int row;
6227 int col;
6228 int attr;
6230 unsigned off;
6231 char_u *ptr = text;
6232 int c;
6233 #ifdef FEAT_MBYTE
6234 unsigned max_off;
6235 int mbyte_blen = 1;
6236 int mbyte_cells = 1;
6237 int u8c = 0;
6238 int u8cc[MAX_MCO];
6239 int clear_next_cell = FALSE;
6240 # ifdef FEAT_ARABIC
6241 int prev_c = 0; /* previous Arabic character */
6242 int pc, nc, nc1;
6243 int pcc[MAX_MCO];
6244 # endif
6245 #endif
6247 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
6248 return;
6250 off = LineOffset[row] + col;
6251 #ifdef FEAT_MBYTE
6252 max_off = LineOffset[row] + screen_Columns;
6253 #endif
6254 while (col < screen_Columns
6255 && (len < 0 || (int)(ptr - text) < len)
6256 && *ptr != NUL)
6258 c = *ptr;
6259 #ifdef FEAT_MBYTE
6260 /* check if this is the first byte of a multibyte */
6261 if (has_mbyte)
6263 if (enc_utf8 && len > 0)
6264 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
6265 else
6266 mbyte_blen = (*mb_ptr2len)(ptr);
6267 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6268 mbyte_cells = 1;
6269 else if (enc_dbcs != 0)
6270 mbyte_cells = mbyte_blen;
6271 else /* enc_utf8 */
6273 if (len >= 0)
6274 u8c = utfc_ptr2char_len(ptr, u8cc,
6275 (int)((text + len) - ptr));
6276 else
6277 u8c = utfc_ptr2char(ptr, u8cc);
6278 mbyte_cells = utf_char2cells(u8c);
6279 # ifdef UNICODE16
6280 /* Non-BMP character: display as ? or fullwidth ?. */
6281 if (u8c >= 0x10000)
6283 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
6284 if (attr == 0)
6285 attr = hl_attr(HLF_8);
6287 # endif
6288 # ifdef FEAT_ARABIC
6289 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
6291 /* Do Arabic shaping. */
6292 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
6294 /* Past end of string to be displayed. */
6295 nc = NUL;
6296 nc1 = NUL;
6298 else
6300 nc = utfc_ptr2char(ptr + mbyte_blen, pcc);
6301 nc1 = pcc[0];
6303 pc = prev_c;
6304 prev_c = u8c;
6305 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
6307 else
6308 prev_c = u8c;
6309 # endif
6312 #endif
6314 if (ScreenLines[off] != c
6315 #ifdef FEAT_MBYTE
6316 || (mbyte_cells == 2
6317 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
6318 || (enc_dbcs == DBCS_JPNU
6319 && c == 0x8e
6320 && ScreenLines2[off] != ptr[1])
6321 || (enc_utf8
6322 && (ScreenLinesUC[off] != (u8char_T)u8c
6323 || screen_comp_differs(off, u8cc)))
6324 #endif
6325 || ScreenAttrs[off] != attr
6326 || exmode_active
6329 #if defined(FEAT_GUI) || defined(UNIX)
6330 /* The bold trick makes a single row of pixels appear in the next
6331 * character. When a bold character is removed, the next
6332 * character should be redrawn too. This happens for our own GUI
6333 * and for some xterms.
6334 * Force the redraw by setting the attribute to a different value
6335 * than "attr", the contents of ScreenLines[] may be needed by
6336 * mb_off2cells() further on.
6337 * Don't do this for the last drawn character, because the next
6338 * character may not be redrawn. */
6339 if (
6340 # ifdef FEAT_GUI
6341 gui.in_use
6342 # endif
6343 # if defined(FEAT_GUI) && defined(UNIX)
6345 # endif
6346 # ifdef UNIX
6347 term_is_xterm
6348 # endif
6351 int n;
6353 n = ScreenAttrs[off];
6354 # ifdef FEAT_MBYTE
6355 if (col + mbyte_cells < screen_Columns
6356 && (n > HL_ALL || (n & HL_BOLD))
6357 && (len < 0 ? ptr[mbyte_blen] != NUL
6358 : ptr + mbyte_blen < text + len))
6359 ScreenAttrs[off + mbyte_cells] = attr + 1;
6360 # else
6361 if (col + 1 < screen_Columns
6362 && (n > HL_ALL || (n & HL_BOLD))
6363 && (len < 0 ? ptr[1] != NUL : ptr + 1 < text + len))
6364 ScreenLines[off + 1] = 0;
6365 # endif
6367 #endif
6368 #ifdef FEAT_MBYTE
6369 /* When at the end of the text and overwriting a two-cell
6370 * character with a one-cell character, need to clear the next
6371 * cell. Also when overwriting the left halve of a two-cell char
6372 * with the right halve of a two-cell char. Do this only once
6373 * (mb_off2cells() may return 2 on the right halve). */
6374 if (clear_next_cell)
6375 clear_next_cell = FALSE;
6376 else if (has_mbyte
6377 && (len < 0 ? ptr[mbyte_blen] == NUL
6378 : ptr + mbyte_blen >= text + len)
6379 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
6380 || (mbyte_cells == 2
6381 && (*mb_off2cells)(off, max_off) == 1
6382 && (*mb_off2cells)(off + 1, max_off) > 1)))
6383 clear_next_cell = TRUE;
6385 /* Make sure we never leave a second byte of a double-byte behind,
6386 * it confuses mb_off2cells(). */
6387 if (enc_dbcs
6388 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
6389 || (mbyte_cells == 2
6390 && (*mb_off2cells)(off, max_off) == 1
6391 && (*mb_off2cells)(off + 1, max_off) > 1)))
6392 ScreenLines[off + mbyte_blen] = 0;
6393 #endif
6394 ScreenLines[off] = c;
6395 ScreenAttrs[off] = attr;
6396 #ifdef FEAT_MBYTE
6397 if (enc_utf8)
6399 if (c < 0x80 && u8cc[0] == 0)
6400 ScreenLinesUC[off] = 0;
6401 else
6403 int i;
6405 ScreenLinesUC[off] = u8c;
6406 for (i = 0; i < Screen_mco; ++i)
6408 ScreenLinesC[i][off] = u8cc[i];
6409 if (u8cc[i] == 0)
6410 break;
6413 if (mbyte_cells == 2)
6415 ScreenLines[off + 1] = 0;
6416 ScreenAttrs[off + 1] = attr;
6418 screen_char(off, row, col);
6420 else if (mbyte_cells == 2)
6422 ScreenLines[off + 1] = ptr[1];
6423 ScreenAttrs[off + 1] = attr;
6424 screen_char_2(off, row, col);
6426 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6428 ScreenLines2[off] = ptr[1];
6429 screen_char(off, row, col);
6431 else
6432 #endif
6433 screen_char(off, row, col);
6435 #ifdef FEAT_MBYTE
6436 if (has_mbyte)
6438 off += mbyte_cells;
6439 col += mbyte_cells;
6440 ptr += mbyte_blen;
6441 if (clear_next_cell)
6442 ptr = (char_u *)" ";
6444 else
6445 #endif
6447 ++off;
6448 ++col;
6449 ++ptr;
6454 #ifdef FEAT_SEARCH_EXTRA
6456 * Prepare for 'hlsearch' highlighting.
6458 static void
6459 start_search_hl()
6461 if (p_hls && !no_hlsearch)
6463 last_pat_prog(&search_hl.rm);
6464 search_hl.attr = hl_attr(HLF_L);
6469 * Clean up for 'hlsearch' highlighting.
6471 static void
6472 end_search_hl()
6474 if (search_hl.rm.regprog != NULL)
6476 vim_free(search_hl.rm.regprog);
6477 search_hl.rm.regprog = NULL;
6482 * Advance to the match in window "wp" line "lnum" or past it.
6484 static void
6485 prepare_search_hl(wp, lnum)
6486 win_T *wp;
6487 linenr_T lnum;
6489 matchitem_T *cur; /* points to the match list */
6490 match_T *shl; /* points to search_hl or a match */
6491 int shl_flag; /* flag to indicate whether search_hl
6492 has been processed or not */
6493 int n;
6496 * When using a multi-line pattern, start searching at the top
6497 * of the window or just after a closed fold.
6498 * Do this both for search_hl and the match list.
6500 cur = wp->w_match_head;
6501 shl_flag = FALSE;
6502 while (cur != NULL || shl_flag == FALSE)
6504 if (shl_flag == FALSE)
6506 shl = &search_hl;
6507 shl_flag = TRUE;
6509 else
6510 shl = &cur->hl;
6511 if (shl->rm.regprog != NULL
6512 && shl->lnum == 0
6513 && re_multiline(shl->rm.regprog))
6515 if (shl->first_lnum == 0)
6517 # ifdef FEAT_FOLDING
6518 for (shl->first_lnum = lnum;
6519 shl->first_lnum > wp->w_topline; --shl->first_lnum)
6520 if (hasFoldingWin(wp, shl->first_lnum - 1,
6521 NULL, NULL, TRUE, NULL))
6522 break;
6523 # else
6524 shl->first_lnum = wp->w_topline;
6525 # endif
6527 n = 0;
6528 while (shl->first_lnum < lnum && shl->rm.regprog != NULL)
6530 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n);
6531 if (shl->lnum != 0)
6533 shl->first_lnum = shl->lnum
6534 + shl->rm.endpos[0].lnum
6535 - shl->rm.startpos[0].lnum;
6536 n = shl->rm.endpos[0].col;
6538 else
6540 ++shl->first_lnum;
6541 n = 0;
6545 if (shl != &search_hl && cur != NULL)
6546 cur = cur->next;
6551 * Search for a next 'hlsearch' or match.
6552 * Uses shl->buf.
6553 * Sets shl->lnum and shl->rm contents.
6554 * Note: Assumes a previous match is always before "lnum", unless
6555 * shl->lnum is zero.
6556 * Careful: Any pointers for buffer lines will become invalid.
6558 static void
6559 next_search_hl(win, shl, lnum, mincol)
6560 win_T *win;
6561 match_T *shl; /* points to search_hl or a match */
6562 linenr_T lnum;
6563 colnr_T mincol; /* minimal column for a match */
6565 linenr_T l;
6566 colnr_T matchcol;
6567 long nmatched;
6569 if (shl->lnum != 0)
6571 /* Check for three situations:
6572 * 1. If the "lnum" is below a previous match, start a new search.
6573 * 2. If the previous match includes "mincol", use it.
6574 * 3. Continue after the previous match.
6576 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
6577 if (lnum > l)
6578 shl->lnum = 0;
6579 else if (lnum < l || shl->rm.endpos[0].col > mincol)
6580 return;
6584 * Repeat searching for a match until one is found that includes "mincol"
6585 * or none is found in this line.
6587 called_emsg = FALSE;
6588 for (;;)
6590 /* Three situations:
6591 * 1. No useful previous match: search from start of line.
6592 * 2. Not Vi compatible or empty match: continue at next character.
6593 * Break the loop if this is beyond the end of the line.
6594 * 3. Vi compatible searching: continue at end of previous match.
6596 if (shl->lnum == 0)
6597 matchcol = 0;
6598 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
6599 || (shl->rm.endpos[0].lnum == 0
6600 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
6602 char_u *ml;
6604 matchcol = shl->rm.startpos[0].col;
6605 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
6606 if (*ml == NUL)
6608 ++matchcol;
6609 shl->lnum = 0;
6610 break;
6612 #ifdef FEAT_MBYTE
6613 if (has_mbyte)
6614 matchcol += mb_ptr2len(ml);
6615 else
6616 #endif
6617 ++matchcol;
6619 else
6620 matchcol = shl->rm.endpos[0].col;
6622 shl->lnum = lnum;
6623 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum, matchcol);
6624 if (called_emsg)
6626 /* Error while handling regexp: stop using this regexp. */
6627 if (shl == &search_hl)
6629 /* don't free regprog in the match list, it's a copy */
6630 vim_free(shl->rm.regprog);
6631 no_hlsearch = TRUE;
6633 shl->rm.regprog = NULL;
6634 shl->lnum = 0;
6635 got_int = FALSE; /* avoid the "Type :quit to exit Vim" message */
6636 break;
6638 if (nmatched == 0)
6640 shl->lnum = 0; /* no match found */
6641 break;
6643 if (shl->rm.startpos[0].lnum > 0
6644 || shl->rm.startpos[0].col >= mincol
6645 || nmatched > 1
6646 || shl->rm.endpos[0].col > mincol)
6648 shl->lnum += shl->rm.startpos[0].lnum;
6649 break; /* useful match found */
6653 #endif
6655 static void
6656 screen_start_highlight(attr)
6657 int attr;
6659 attrentry_T *aep = NULL;
6661 screen_attr = attr;
6662 if (full_screen
6663 #ifdef WIN3264
6664 && termcap_active
6665 #endif
6668 #ifdef FEAT_GUI
6669 if (gui.in_use)
6671 char buf[20];
6673 /* The GUI handles this internally. */
6674 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
6675 OUT_STR(buf);
6677 else
6678 #endif
6680 if (attr > HL_ALL) /* special HL attr. */
6682 if (t_colors > 1)
6683 aep = syn_cterm_attr2entry(attr);
6684 else
6685 aep = syn_term_attr2entry(attr);
6686 if (aep == NULL) /* did ":syntax clear" */
6687 attr = 0;
6688 else
6689 attr = aep->ae_attr;
6691 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
6692 out_str(T_MD);
6693 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
6694 && cterm_normal_fg_bold)
6695 /* If the Normal FG color has BOLD attribute and the new HL
6696 * has a FG color defined, clear BOLD. */
6697 out_str(T_ME);
6698 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
6699 out_str(T_SO);
6700 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
6701 /* underline or undercurl */
6702 out_str(T_US);
6703 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
6704 out_str(T_CZH);
6705 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
6706 out_str(T_MR);
6709 * Output the color or start string after bold etc., in case the
6710 * bold etc. override the color setting.
6712 if (aep != NULL)
6714 if (t_colors > 1)
6716 if (aep->ae_u.cterm.fg_color)
6717 term_fg_color(aep->ae_u.cterm.fg_color - 1);
6718 if (aep->ae_u.cterm.bg_color)
6719 term_bg_color(aep->ae_u.cterm.bg_color - 1);
6721 else
6723 if (aep->ae_u.term.start != NULL)
6724 out_str(aep->ae_u.term.start);
6731 void
6732 screen_stop_highlight()
6734 int do_ME = FALSE; /* output T_ME code */
6736 if (screen_attr != 0
6737 #ifdef WIN3264
6738 && termcap_active
6739 #endif
6742 #ifdef FEAT_GUI
6743 if (gui.in_use)
6745 char buf[20];
6747 /* use internal GUI code */
6748 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
6749 OUT_STR(buf);
6751 else
6752 #endif
6754 if (screen_attr > HL_ALL) /* special HL attr. */
6756 attrentry_T *aep;
6758 if (t_colors > 1)
6761 * Assume that t_me restores the original colors!
6763 aep = syn_cterm_attr2entry(screen_attr);
6764 if (aep != NULL && (aep->ae_u.cterm.fg_color
6765 || aep->ae_u.cterm.bg_color))
6766 do_ME = TRUE;
6768 else
6770 aep = syn_term_attr2entry(screen_attr);
6771 if (aep != NULL && aep->ae_u.term.stop != NULL)
6773 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
6774 do_ME = TRUE;
6775 else
6776 out_str(aep->ae_u.term.stop);
6779 if (aep == NULL) /* did ":syntax clear" */
6780 screen_attr = 0;
6781 else
6782 screen_attr = aep->ae_attr;
6786 * Often all ending-codes are equal to T_ME. Avoid outputting the
6787 * same sequence several times.
6789 if (screen_attr & HL_STANDOUT)
6791 if (STRCMP(T_SE, T_ME) == 0)
6792 do_ME = TRUE;
6793 else
6794 out_str(T_SE);
6796 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
6798 if (STRCMP(T_UE, T_ME) == 0)
6799 do_ME = TRUE;
6800 else
6801 out_str(T_UE);
6803 if (screen_attr & HL_ITALIC)
6805 if (STRCMP(T_CZR, T_ME) == 0)
6806 do_ME = TRUE;
6807 else
6808 out_str(T_CZR);
6810 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
6811 out_str(T_ME);
6813 if (t_colors > 1)
6815 /* set Normal cterm colors */
6816 if (cterm_normal_fg_color != 0)
6817 term_fg_color(cterm_normal_fg_color - 1);
6818 if (cterm_normal_bg_color != 0)
6819 term_bg_color(cterm_normal_bg_color - 1);
6820 if (cterm_normal_fg_bold)
6821 out_str(T_MD);
6825 screen_attr = 0;
6829 * Reset the colors for a cterm. Used when leaving Vim.
6830 * The machine specific code may override this again.
6832 void
6833 reset_cterm_colors()
6835 if (t_colors > 1)
6837 /* set Normal cterm colors */
6838 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
6840 out_str(T_OP);
6841 screen_attr = -1;
6843 if (cterm_normal_fg_bold)
6845 out_str(T_ME);
6846 screen_attr = -1;
6852 * Put character ScreenLines["off"] on the screen at position "row" and "col",
6853 * using the attributes from ScreenAttrs["off"].
6855 static void
6856 screen_char(off, row, col)
6857 unsigned off;
6858 int row;
6859 int col;
6861 int attr;
6863 /* Check for illegal values, just in case (could happen just after
6864 * resizing). */
6865 if (row >= screen_Rows || col >= screen_Columns)
6866 return;
6868 /* Outputting the last character on the screen may scrollup the screen.
6869 * Don't to it! Mark the character invalid (update it when scrolled up) */
6870 if (row == screen_Rows - 1 && col == screen_Columns - 1
6871 #ifdef FEAT_RIGHTLEFT
6872 /* account for first command-line character in rightleft mode */
6873 && !cmdmsg_rl
6874 #endif
6877 ScreenAttrs[off] = (sattr_T)-1;
6878 return;
6882 * Stop highlighting first, so it's easier to move the cursor.
6884 #if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
6885 if (screen_char_attr != 0)
6886 attr = screen_char_attr;
6887 else
6888 #endif
6889 attr = ScreenAttrs[off];
6890 if (screen_attr != attr)
6891 screen_stop_highlight();
6893 windgoto(row, col);
6895 if (screen_attr != attr)
6896 screen_start_highlight(attr);
6898 #ifdef FEAT_MBYTE
6899 if (enc_utf8 && ScreenLinesUC[off] != 0)
6901 char_u buf[MB_MAXBYTES + 1];
6903 /* Convert UTF-8 character to bytes and write it. */
6905 buf[utfc_char2bytes(off, buf)] = NUL;
6907 out_str(buf);
6908 if (utf_char2cells(ScreenLinesUC[off]) > 1)
6909 ++screen_cur_col;
6911 else
6912 #endif
6914 #ifdef FEAT_MBYTE
6915 out_flush_check();
6916 #endif
6917 out_char(ScreenLines[off]);
6918 #ifdef FEAT_MBYTE
6919 /* double-byte character in single-width cell */
6920 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6921 out_char(ScreenLines2[off]);
6922 #endif
6925 screen_cur_col++;
6928 #ifdef FEAT_MBYTE
6931 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
6932 * on the screen at position 'row' and 'col'.
6933 * The attributes of the first byte is used for all. This is required to
6934 * output the two bytes of a double-byte character with nothing in between.
6936 static void
6937 screen_char_2(off, row, col)
6938 unsigned off;
6939 int row;
6940 int col;
6942 /* Check for illegal values (could be wrong when screen was resized). */
6943 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
6944 return;
6946 /* Outputting the last character on the screen may scrollup the screen.
6947 * Don't to it! Mark the character invalid (update it when scrolled up) */
6948 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
6950 ScreenAttrs[off] = (sattr_T)-1;
6951 return;
6954 /* Output the first byte normally (positions the cursor), then write the
6955 * second byte directly. */
6956 screen_char(off, row, col);
6957 out_char(ScreenLines[off + 1]);
6958 ++screen_cur_col;
6960 #endif
6962 #if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
6964 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
6965 * This uses the contents of ScreenLines[] and doesn't change it.
6967 void
6968 screen_draw_rectangle(row, col, height, width, invert)
6969 int row;
6970 int col;
6971 int height;
6972 int width;
6973 int invert;
6975 int r, c;
6976 int off;
6977 #ifdef FEAT_MBYTE
6978 int max_off;
6979 #endif
6981 /* Can't use ScreenLines unless initialized */
6982 if (ScreenLines == NULL)
6983 return;
6985 if (invert)
6986 screen_char_attr = HL_INVERSE;
6987 for (r = row; r < row + height; ++r)
6989 off = LineOffset[r];
6990 #ifdef FEAT_MBYTE
6991 max_off = off + screen_Columns;
6992 #endif
6993 for (c = col; c < col + width; ++c)
6995 #ifdef FEAT_MBYTE
6996 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
6998 screen_char_2(off + c, r, c);
6999 ++c;
7001 else
7002 #endif
7004 screen_char(off + c, r, c);
7005 #ifdef FEAT_MBYTE
7006 if (utf_off2cells(off + c, max_off) > 1)
7007 ++c;
7008 #endif
7012 screen_char_attr = 0;
7014 #endif
7016 #ifdef FEAT_VERTSPLIT
7018 * Redraw the characters for a vertically split window.
7020 static void
7021 redraw_block(row, end, wp)
7022 int row;
7023 int end;
7024 win_T *wp;
7026 int col;
7027 int width;
7029 # ifdef FEAT_CLIPBOARD
7030 clip_may_clear_selection(row, end - 1);
7031 # endif
7033 if (wp == NULL)
7035 col = 0;
7036 width = Columns;
7038 else
7040 col = wp->w_wincol;
7041 width = wp->w_width;
7043 screen_draw_rectangle(row, col, end - row, width, FALSE);
7045 #endif
7048 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
7049 * with character 'c1' in first column followed by 'c2' in the other columns.
7050 * Use attributes 'attr'.
7052 void
7053 screen_fill(start_row, end_row, start_col, end_col, c1, c2, attr)
7054 int start_row, end_row;
7055 int start_col, end_col;
7056 int c1, c2;
7057 int attr;
7059 int row;
7060 int col;
7061 int off;
7062 int end_off;
7063 int did_delete;
7064 int c;
7065 int norm_term;
7066 #if defined(FEAT_GUI) || defined(UNIX)
7067 int force_next = FALSE;
7068 #endif
7070 if (end_row > screen_Rows) /* safety check */
7071 end_row = screen_Rows;
7072 if (end_col > screen_Columns) /* safety check */
7073 end_col = screen_Columns;
7074 if (ScreenLines == NULL
7075 || start_row >= end_row
7076 || start_col >= end_col) /* nothing to do */
7077 return;
7079 /* it's a "normal" terminal when not in a GUI or cterm */
7080 norm_term = (
7081 #ifdef FEAT_GUI
7082 !gui.in_use &&
7083 #endif
7084 t_colors <= 1);
7085 for (row = start_row; row < end_row; ++row)
7088 * Try to use delete-line termcap code, when no attributes or in a
7089 * "normal" terminal, where a bold/italic space is just a
7090 * space.
7092 did_delete = FALSE;
7093 if (c2 == ' '
7094 && end_col == Columns
7095 && can_clear(T_CE)
7096 && (attr == 0
7097 || (norm_term
7098 && attr <= HL_ALL
7099 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
7102 * check if we really need to clear something
7104 col = start_col;
7105 if (c1 != ' ') /* don't clear first char */
7106 ++col;
7108 off = LineOffset[row] + col;
7109 end_off = LineOffset[row] + end_col;
7111 /* skip blanks (used often, keep it fast!) */
7112 #ifdef FEAT_MBYTE
7113 if (enc_utf8)
7114 while (off < end_off && ScreenLines[off] == ' '
7115 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
7116 ++off;
7117 else
7118 #endif
7119 while (off < end_off && ScreenLines[off] == ' '
7120 && ScreenAttrs[off] == 0)
7121 ++off;
7122 if (off < end_off) /* something to be cleared */
7124 col = off - LineOffset[row];
7125 screen_stop_highlight();
7126 term_windgoto(row, col);/* clear rest of this screen line */
7127 out_str(T_CE);
7128 screen_start(); /* don't know where cursor is now */
7129 col = end_col - col;
7130 while (col--) /* clear chars in ScreenLines */
7132 ScreenLines[off] = ' ';
7133 #ifdef FEAT_MBYTE
7134 if (enc_utf8)
7135 ScreenLinesUC[off] = 0;
7136 #endif
7137 ScreenAttrs[off] = 0;
7138 ++off;
7141 did_delete = TRUE; /* the chars are cleared now */
7144 off = LineOffset[row] + start_col;
7145 c = c1;
7146 for (col = start_col; col < end_col; ++col)
7148 if (ScreenLines[off] != c
7149 #ifdef FEAT_MBYTE
7150 || (enc_utf8 && (int)ScreenLinesUC[off]
7151 != (c >= 0x80 ? c : 0))
7152 #endif
7153 || ScreenAttrs[off] != attr
7154 #if defined(FEAT_GUI) || defined(UNIX)
7155 || force_next
7156 #endif
7159 #if defined(FEAT_GUI) || defined(UNIX)
7160 /* The bold trick may make a single row of pixels appear in
7161 * the next character. When a bold character is removed, the
7162 * next character should be redrawn too. This happens for our
7163 * own GUI and for some xterms. */
7164 if (
7165 # ifdef FEAT_GUI
7166 gui.in_use
7167 # endif
7168 # if defined(FEAT_GUI) && defined(UNIX)
7170 # endif
7171 # ifdef UNIX
7172 term_is_xterm
7173 # endif
7176 if (ScreenLines[off] != ' '
7177 && (ScreenAttrs[off] > HL_ALL
7178 || ScreenAttrs[off] & HL_BOLD))
7179 force_next = TRUE;
7180 else
7181 force_next = FALSE;
7183 #endif
7184 ScreenLines[off] = c;
7185 #ifdef FEAT_MBYTE
7186 if (enc_utf8)
7188 if (c >= 0x80)
7190 ScreenLinesUC[off] = c;
7191 ScreenLinesC[0][off] = 0;
7193 else
7194 ScreenLinesUC[off] = 0;
7196 #endif
7197 ScreenAttrs[off] = attr;
7198 if (!did_delete || c != ' ')
7199 screen_char(off, row, col);
7201 ++off;
7202 if (col == start_col)
7204 if (did_delete)
7205 break;
7206 c = c2;
7209 if (end_col == Columns)
7210 LineWraps[row] = FALSE;
7211 if (row == Rows - 1) /* overwritten the command line */
7213 redraw_cmdline = TRUE;
7214 if (c1 == ' ' && c2 == ' ')
7215 clear_cmdline = FALSE; /* command line has been cleared */
7216 if (start_col == 0)
7217 mode_displayed = FALSE; /* mode cleared or overwritten */
7223 * Check if there should be a delay. Used before clearing or redrawing the
7224 * screen or the command line.
7226 void
7227 check_for_delay(check_msg_scroll)
7228 int check_msg_scroll;
7230 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
7231 && !did_wait_return
7232 && emsg_silent == 0)
7234 out_flush();
7235 ui_delay(1000L, TRUE);
7236 emsg_on_display = FALSE;
7237 if (check_msg_scroll)
7238 msg_scroll = FALSE;
7243 * screen_valid - allocate screen buffers if size changed
7244 * If "clear" is TRUE: clear screen if it has been resized.
7245 * Returns TRUE if there is a valid screen to write to.
7246 * Returns FALSE when starting up and screen not initialized yet.
7249 screen_valid(clear)
7250 int clear;
7252 screenalloc(clear); /* allocate screen buffers if size changed */
7253 return (ScreenLines != NULL);
7257 * Resize the shell to Rows and Columns.
7258 * Allocate ScreenLines[] and associated items.
7260 * There may be some time between setting Rows and Columns and (re)allocating
7261 * ScreenLines[]. This happens when starting up and when (manually) changing
7262 * the shell size. Always use screen_Rows and screen_Columns to access items
7263 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
7264 * final size of the shell is needed.
7266 void
7267 screenalloc(clear)
7268 int clear;
7270 int new_row, old_row;
7271 #ifdef FEAT_GUI
7272 int old_Rows;
7273 #endif
7274 win_T *wp;
7275 int outofmem = FALSE;
7276 int len;
7277 schar_T *new_ScreenLines;
7278 #ifdef FEAT_MBYTE
7279 u8char_T *new_ScreenLinesUC = NULL;
7280 u8char_T *new_ScreenLinesC[MAX_MCO];
7281 schar_T *new_ScreenLines2 = NULL;
7282 int i;
7283 #endif
7284 sattr_T *new_ScreenAttrs;
7285 unsigned *new_LineOffset;
7286 char_u *new_LineWraps;
7287 #ifdef FEAT_WINDOWS
7288 short *new_TabPageIdxs;
7289 tabpage_T *tp;
7290 #endif
7291 static int entered = FALSE; /* avoid recursiveness */
7292 static int done_outofmem_msg = FALSE; /* did outofmem message */
7295 * Allocation of the screen buffers is done only when the size changes and
7296 * when Rows and Columns have been set and we have started doing full
7297 * screen stuff.
7299 if ((ScreenLines != NULL
7300 && Rows == screen_Rows
7301 && Columns == screen_Columns
7302 #ifdef FEAT_MBYTE
7303 && enc_utf8 == (ScreenLinesUC != NULL)
7304 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
7305 && p_mco == Screen_mco
7306 #endif
7308 || Rows == 0
7309 || Columns == 0
7310 || (!full_screen && ScreenLines == NULL))
7311 return;
7314 * It's possible that we produce an out-of-memory message below, which
7315 * will cause this function to be called again. To break the loop, just
7316 * return here.
7318 if (entered)
7319 return;
7320 entered = TRUE;
7323 * Note that the window sizes are updated before reallocating the arrays,
7324 * thus we must not redraw here!
7326 ++RedrawingDisabled;
7328 win_new_shellsize(); /* fit the windows in the new sized shell */
7330 comp_col(); /* recompute columns for shown command and ruler */
7333 * We're changing the size of the screen.
7334 * - Allocate new arrays for ScreenLines and ScreenAttrs.
7335 * - Move lines from the old arrays into the new arrays, clear extra
7336 * lines (unless the screen is going to be cleared).
7337 * - Free the old arrays.
7339 * If anything fails, make ScreenLines NULL, so we don't do anything!
7340 * Continuing with the old ScreenLines may result in a crash, because the
7341 * size is wrong.
7343 FOR_ALL_TAB_WINDOWS(tp, wp)
7344 win_free_lsize(wp);
7346 new_ScreenLines = (schar_T *)lalloc((long_u)(
7347 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7348 #ifdef FEAT_MBYTE
7349 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T) * MAX_MCO);
7350 if (enc_utf8)
7352 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
7353 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
7354 for (i = 0; i < p_mco; ++i)
7355 new_ScreenLinesC[i] = (u8char_T *)lalloc((long_u)(
7356 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
7358 if (enc_dbcs == DBCS_JPNU)
7359 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
7360 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7361 #endif
7362 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
7363 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
7364 new_LineOffset = (unsigned *)lalloc((long_u)(
7365 Rows * sizeof(unsigned)), FALSE);
7366 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
7367 #ifdef FEAT_WINDOWS
7368 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
7369 #endif
7371 FOR_ALL_TAB_WINDOWS(tp, wp)
7373 if (win_alloc_lines(wp) == FAIL)
7375 outofmem = TRUE;
7376 #ifdef FEAT_WINDOWS
7377 break;
7378 #endif
7382 #ifdef FEAT_MBYTE
7383 for (i = 0; i < p_mco; ++i)
7384 if (new_ScreenLinesC[i] == NULL)
7385 break;
7386 #endif
7387 if (new_ScreenLines == NULL
7388 #ifdef FEAT_MBYTE
7389 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
7390 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
7391 #endif
7392 || new_ScreenAttrs == NULL
7393 || new_LineOffset == NULL
7394 || new_LineWraps == NULL
7395 #ifdef FEAT_WINDOWS
7396 || new_TabPageIdxs == NULL
7397 #endif
7398 || outofmem)
7400 if (ScreenLines != NULL || !done_outofmem_msg)
7402 /* guess the size */
7403 do_outofmem_msg((long_u)((Rows + 1) * Columns));
7405 /* Remember we did this to avoid getting outofmem messages over
7406 * and over again. */
7407 done_outofmem_msg = TRUE;
7409 vim_free(new_ScreenLines);
7410 new_ScreenLines = NULL;
7411 #ifdef FEAT_MBYTE
7412 vim_free(new_ScreenLinesUC);
7413 new_ScreenLinesUC = NULL;
7414 for (i = 0; i < p_mco; ++i)
7416 vim_free(new_ScreenLinesC[i]);
7417 new_ScreenLinesC[i] = NULL;
7419 vim_free(new_ScreenLines2);
7420 new_ScreenLines2 = NULL;
7421 #endif
7422 vim_free(new_ScreenAttrs);
7423 new_ScreenAttrs = NULL;
7424 vim_free(new_LineOffset);
7425 new_LineOffset = NULL;
7426 vim_free(new_LineWraps);
7427 new_LineWraps = NULL;
7428 #ifdef FEAT_WINDOWS
7429 vim_free(new_TabPageIdxs);
7430 new_TabPageIdxs = NULL;
7431 #endif
7433 else
7435 done_outofmem_msg = FALSE;
7437 for (new_row = 0; new_row < Rows; ++new_row)
7439 new_LineOffset[new_row] = new_row * Columns;
7440 new_LineWraps[new_row] = FALSE;
7443 * If the screen is not going to be cleared, copy as much as
7444 * possible from the old screen to the new one and clear the rest
7445 * (used when resizing the window at the "--more--" prompt or when
7446 * executing an external command, for the GUI).
7448 if (!clear)
7450 (void)vim_memset(new_ScreenLines + new_row * Columns,
7451 ' ', (size_t)Columns * sizeof(schar_T));
7452 #ifdef FEAT_MBYTE
7453 if (enc_utf8)
7455 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
7456 0, (size_t)Columns * sizeof(u8char_T));
7457 for (i = 0; i < p_mco; ++i)
7458 (void)vim_memset(new_ScreenLinesC[i]
7459 + new_row * Columns,
7460 0, (size_t)Columns * sizeof(u8char_T));
7462 if (enc_dbcs == DBCS_JPNU)
7463 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
7464 0, (size_t)Columns * sizeof(schar_T));
7465 #endif
7466 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
7467 0, (size_t)Columns * sizeof(sattr_T));
7468 old_row = new_row + (screen_Rows - Rows);
7469 if (old_row >= 0 && ScreenLines != NULL)
7471 if (screen_Columns < Columns)
7472 len = screen_Columns;
7473 else
7474 len = Columns;
7475 #ifdef FEAT_MBYTE
7476 /* When switching to utf-8 don't copy characters, they
7477 * may be invalid now. Also when p_mco changes. */
7478 if (!(enc_utf8 && ScreenLinesUC == NULL)
7479 && p_mco == Screen_mco)
7480 #endif
7481 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
7482 ScreenLines + LineOffset[old_row],
7483 (size_t)len * sizeof(schar_T));
7484 #ifdef FEAT_MBYTE
7485 if (enc_utf8 && ScreenLinesUC != NULL
7486 && p_mco == Screen_mco)
7488 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
7489 ScreenLinesUC + LineOffset[old_row],
7490 (size_t)len * sizeof(u8char_T));
7491 for (i = 0; i < p_mco; ++i)
7492 mch_memmove(new_ScreenLinesC[i]
7493 + new_LineOffset[new_row],
7494 ScreenLinesC[i] + LineOffset[old_row],
7495 (size_t)len * sizeof(u8char_T));
7497 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
7498 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
7499 ScreenLines2 + LineOffset[old_row],
7500 (size_t)len * sizeof(schar_T));
7501 #endif
7502 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
7503 ScreenAttrs + LineOffset[old_row],
7504 (size_t)len * sizeof(sattr_T));
7508 /* Use the last line of the screen for the current line. */
7509 current_ScreenLine = new_ScreenLines + Rows * Columns;
7512 free_screenlines();
7514 ScreenLines = new_ScreenLines;
7515 #ifdef FEAT_MBYTE
7516 ScreenLinesUC = new_ScreenLinesUC;
7517 for (i = 0; i < p_mco; ++i)
7518 ScreenLinesC[i] = new_ScreenLinesC[i];
7519 Screen_mco = p_mco;
7520 ScreenLines2 = new_ScreenLines2;
7521 #endif
7522 ScreenAttrs = new_ScreenAttrs;
7523 LineOffset = new_LineOffset;
7524 LineWraps = new_LineWraps;
7525 #ifdef FEAT_WINDOWS
7526 TabPageIdxs = new_TabPageIdxs;
7527 #endif
7529 /* It's important that screen_Rows and screen_Columns reflect the actual
7530 * size of ScreenLines[]. Set them before calling anything. */
7531 #ifdef FEAT_GUI
7532 old_Rows = screen_Rows;
7533 #endif
7534 screen_Rows = Rows;
7535 screen_Columns = Columns;
7537 must_redraw = CLEAR; /* need to clear the screen later */
7538 if (clear)
7539 screenclear2();
7541 #ifdef FEAT_GUI
7542 else if (gui.in_use
7543 && !gui.starting
7544 && ScreenLines != NULL
7545 && old_Rows != Rows)
7547 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
7549 * Adjust the position of the cursor, for when executing an external
7550 * command.
7552 if (msg_row >= Rows) /* Rows got smaller */
7553 msg_row = Rows - 1; /* put cursor at last row */
7554 else if (Rows > old_Rows) /* Rows got bigger */
7555 msg_row += Rows - old_Rows; /* put cursor in same place */
7556 if (msg_col >= Columns) /* Columns got smaller */
7557 msg_col = Columns - 1; /* put cursor at last column */
7559 #endif
7561 entered = FALSE;
7562 --RedrawingDisabled;
7564 #ifdef FEAT_AUTOCMD
7565 if (starting == 0)
7566 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
7567 #endif
7570 void
7571 free_screenlines()
7573 #ifdef FEAT_MBYTE
7574 int i;
7576 vim_free(ScreenLinesUC);
7577 for (i = 0; i < Screen_mco; ++i)
7578 vim_free(ScreenLinesC[i]);
7579 vim_free(ScreenLines2);
7580 #endif
7581 vim_free(ScreenLines);
7582 vim_free(ScreenAttrs);
7583 vim_free(LineOffset);
7584 vim_free(LineWraps);
7585 #ifdef FEAT_WINDOWS
7586 vim_free(TabPageIdxs);
7587 #endif
7590 void
7591 screenclear()
7593 check_for_delay(FALSE);
7594 screenalloc(FALSE); /* allocate screen buffers if size changed */
7595 screenclear2(); /* clear the screen */
7598 static void
7599 screenclear2()
7601 int i;
7603 if (starting == NO_SCREEN || ScreenLines == NULL
7604 #ifdef FEAT_GUI
7605 || (gui.in_use && gui.starting)
7606 #endif
7608 return;
7610 #ifdef FEAT_GUI
7611 if (!gui.in_use)
7612 #endif
7613 screen_attr = -1; /* force setting the Normal colors */
7614 screen_stop_highlight(); /* don't want highlighting here */
7616 #ifdef FEAT_CLIPBOARD
7617 /* disable selection without redrawing it */
7618 clip_scroll_selection(9999);
7619 #endif
7621 /* blank out ScreenLines */
7622 for (i = 0; i < Rows; ++i)
7624 lineclear(LineOffset[i], (int)Columns);
7625 LineWraps[i] = FALSE;
7628 if (can_clear(T_CL))
7630 out_str(T_CL); /* clear the display */
7631 clear_cmdline = FALSE;
7632 mode_displayed = FALSE;
7634 else
7636 /* can't clear the screen, mark all chars with invalid attributes */
7637 for (i = 0; i < Rows; ++i)
7638 lineinvalid(LineOffset[i], (int)Columns);
7639 clear_cmdline = TRUE;
7642 screen_cleared = TRUE; /* can use contents of ScreenLines now */
7644 win_rest_invalid(firstwin);
7645 redraw_cmdline = TRUE;
7646 #ifdef FEAT_WINDOWS
7647 redraw_tabline = TRUE;
7648 #endif
7649 if (must_redraw == CLEAR) /* no need to clear again */
7650 must_redraw = NOT_VALID;
7651 compute_cmdrow();
7652 msg_row = cmdline_row; /* put cursor on last line for messages */
7653 msg_col = 0;
7654 screen_start(); /* don't know where cursor is now */
7655 msg_scrolled = 0; /* can't scroll back */
7656 msg_didany = FALSE;
7657 msg_didout = FALSE;
7661 * Clear one line in ScreenLines.
7663 static void
7664 lineclear(off, width)
7665 unsigned off;
7666 int width;
7668 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
7669 #ifdef FEAT_MBYTE
7670 if (enc_utf8)
7671 (void)vim_memset(ScreenLinesUC + off, 0,
7672 (size_t)width * sizeof(u8char_T));
7673 #endif
7674 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
7678 * Mark one line in ScreenLines invalid by setting the attributes to an
7679 * invalid value.
7681 static void
7682 lineinvalid(off, width)
7683 unsigned off;
7684 int width;
7686 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
7689 #ifdef FEAT_VERTSPLIT
7691 * Copy part of a Screenline for vertically split window "wp".
7693 static void
7694 linecopy(to, from, wp)
7695 int to;
7696 int from;
7697 win_T *wp;
7699 unsigned off_to = LineOffset[to] + wp->w_wincol;
7700 unsigned off_from = LineOffset[from] + wp->w_wincol;
7702 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
7703 wp->w_width * sizeof(schar_T));
7704 # ifdef FEAT_MBYTE
7705 if (enc_utf8)
7707 int i;
7709 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
7710 wp->w_width * sizeof(u8char_T));
7711 for (i = 0; i < p_mco; ++i)
7712 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
7713 wp->w_width * sizeof(u8char_T));
7715 if (enc_dbcs == DBCS_JPNU)
7716 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
7717 wp->w_width * sizeof(schar_T));
7718 # endif
7719 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
7720 wp->w_width * sizeof(sattr_T));
7722 #endif
7725 * Return TRUE if clearing with term string "p" would work.
7726 * It can't work when the string is empty or it won't set the right background.
7729 can_clear(p)
7730 char_u *p;
7732 return (*p != NUL && (t_colors <= 1
7733 #ifdef FEAT_GUI
7734 || gui.in_use
7735 #endif
7736 || cterm_normal_bg_color == 0 || *T_UT != NUL));
7740 * Reset cursor position. Use whenever cursor was moved because of outputting
7741 * something directly to the screen (shell commands) or a terminal control
7742 * code.
7744 void
7745 screen_start()
7747 screen_cur_row = screen_cur_col = 9999;
7751 * Move the cursor to position "row","col" in the screen.
7752 * This tries to find the most efficient way to move, minimizing the number of
7753 * characters sent to the terminal.
7755 void
7756 windgoto(row, col)
7757 int row;
7758 int col;
7760 sattr_T *p;
7761 int i;
7762 int plan;
7763 int cost;
7764 int wouldbe_col;
7765 int noinvcurs;
7766 char_u *bs;
7767 int goto_cost;
7768 int attr;
7770 #define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
7771 #define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
7773 #define PLAN_LE 1
7774 #define PLAN_CR 2
7775 #define PLAN_NL 3
7776 #define PLAN_WRITE 4
7777 /* Can't use ScreenLines unless initialized */
7778 if (ScreenLines == NULL)
7779 return;
7781 if (col != screen_cur_col || row != screen_cur_row)
7783 /* Check for valid position. */
7784 if (row < 0) /* window without text lines? */
7785 row = 0;
7786 if (row >= screen_Rows)
7787 row = screen_Rows - 1;
7788 if (col >= screen_Columns)
7789 col = screen_Columns - 1;
7791 /* check if no cursor movement is allowed in highlight mode */
7792 if (screen_attr && *T_MS == NUL)
7793 noinvcurs = HIGHL_COST;
7794 else
7795 noinvcurs = 0;
7796 goto_cost = GOTO_COST + noinvcurs;
7799 * Plan how to do the positioning:
7800 * 1. Use CR to move it to column 0, same row.
7801 * 2. Use T_LE to move it a few columns to the left.
7802 * 3. Use NL to move a few lines down, column 0.
7803 * 4. Move a few columns to the right with T_ND or by writing chars.
7805 * Don't do this if the cursor went beyond the last column, the cursor
7806 * position is unknown then (some terminals wrap, some don't )
7808 * First check if the highlighting attributes allow us to write
7809 * characters to move the cursor to the right.
7811 if (row >= screen_cur_row && screen_cur_col < Columns)
7814 * If the cursor is in the same row, bigger col, we can use CR
7815 * or T_LE.
7817 bs = NULL; /* init for GCC */
7818 attr = screen_attr;
7819 if (row == screen_cur_row && col < screen_cur_col)
7821 /* "le" is preferred over "bc", because "bc" is obsolete */
7822 if (*T_LE)
7823 bs = T_LE; /* "cursor left" */
7824 else
7825 bs = T_BC; /* "backspace character (old) */
7826 if (*bs)
7827 cost = (screen_cur_col - col) * (int)STRLEN(bs);
7828 else
7829 cost = 999;
7830 if (col + 1 < cost) /* using CR is less characters */
7832 plan = PLAN_CR;
7833 wouldbe_col = 0;
7834 cost = 1; /* CR is just one character */
7836 else
7838 plan = PLAN_LE;
7839 wouldbe_col = col;
7841 if (noinvcurs) /* will stop highlighting */
7843 cost += noinvcurs;
7844 attr = 0;
7849 * If the cursor is above where we want to be, we can use CR LF.
7851 else if (row > screen_cur_row)
7853 plan = PLAN_NL;
7854 wouldbe_col = 0;
7855 cost = (row - screen_cur_row) * 2; /* CR LF */
7856 if (noinvcurs) /* will stop highlighting */
7858 cost += noinvcurs;
7859 attr = 0;
7864 * If the cursor is in the same row, smaller col, just use write.
7866 else
7868 plan = PLAN_WRITE;
7869 wouldbe_col = screen_cur_col;
7870 cost = 0;
7874 * Check if any characters that need to be written have the
7875 * correct attributes. Also avoid UTF-8 characters.
7877 i = col - wouldbe_col;
7878 if (i > 0)
7879 cost += i;
7880 if (cost < goto_cost && i > 0)
7883 * Check if the attributes are correct without additionally
7884 * stopping highlighting.
7886 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
7887 while (i && *p++ == attr)
7888 --i;
7889 if (i != 0)
7892 * Try if it works when highlighting is stopped here.
7894 if (*--p == 0)
7896 cost += noinvcurs;
7897 while (i && *p++ == 0)
7898 --i;
7900 if (i != 0)
7901 cost = 999; /* different attributes, don't do it */
7903 #ifdef FEAT_MBYTE
7904 if (enc_utf8)
7906 /* Don't use an UTF-8 char for positioning, it's slow. */
7907 for (i = wouldbe_col; i < col; ++i)
7908 if (ScreenLinesUC[LineOffset[row] + i] != 0)
7910 cost = 999;
7911 break;
7914 #endif
7918 * We can do it without term_windgoto()!
7920 if (cost < goto_cost)
7922 if (plan == PLAN_LE)
7924 if (noinvcurs)
7925 screen_stop_highlight();
7926 while (screen_cur_col > col)
7928 out_str(bs);
7929 --screen_cur_col;
7932 else if (plan == PLAN_CR)
7934 if (noinvcurs)
7935 screen_stop_highlight();
7936 out_char('\r');
7937 screen_cur_col = 0;
7939 else if (plan == PLAN_NL)
7941 if (noinvcurs)
7942 screen_stop_highlight();
7943 while (screen_cur_row < row)
7945 out_char('\n');
7946 ++screen_cur_row;
7948 screen_cur_col = 0;
7951 i = col - screen_cur_col;
7952 if (i > 0)
7955 * Use cursor-right if it's one character only. Avoids
7956 * removing a line of pixels from the last bold char, when
7957 * using the bold trick in the GUI.
7959 if (T_ND[0] != NUL && T_ND[1] == NUL)
7961 while (i-- > 0)
7962 out_char(*T_ND);
7964 else
7966 int off;
7968 off = LineOffset[row] + screen_cur_col;
7969 while (i-- > 0)
7971 if (ScreenAttrs[off] != screen_attr)
7972 screen_stop_highlight();
7973 #ifdef FEAT_MBYTE
7974 out_flush_check();
7975 #endif
7976 out_char(ScreenLines[off]);
7977 #ifdef FEAT_MBYTE
7978 if (enc_dbcs == DBCS_JPNU
7979 && ScreenLines[off] == 0x8e)
7980 out_char(ScreenLines2[off]);
7981 #endif
7982 ++off;
7988 else
7989 cost = 999;
7991 if (cost >= goto_cost)
7993 if (noinvcurs)
7994 screen_stop_highlight();
7995 if (row == screen_cur_row && (col > screen_cur_col) &&
7996 *T_CRI != NUL)
7997 term_cursor_right(col - screen_cur_col);
7998 else
7999 term_windgoto(row, col);
8001 screen_cur_row = row;
8002 screen_cur_col = col;
8007 * Set cursor to its position in the current window.
8009 void
8010 setcursor()
8012 if (redrawing())
8014 validate_cursor();
8015 windgoto(W_WINROW(curwin) + curwin->w_wrow,
8016 W_WINCOL(curwin) + (
8017 #ifdef FEAT_RIGHTLEFT
8018 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
8019 # ifdef FEAT_MBYTE
8020 has_mbyte ? (*mb_ptr2cells)(ml_get_cursor()) :
8021 # endif
8022 1)) :
8023 #endif
8024 curwin->w_wcol));
8030 * insert 'line_count' lines at 'row' in window 'wp'
8031 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
8032 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
8033 * scrolling.
8034 * Returns FAIL if the lines are not inserted, OK for success.
8037 win_ins_lines(wp, row, line_count, invalid, mayclear)
8038 win_T *wp;
8039 int row;
8040 int line_count;
8041 int invalid;
8042 int mayclear;
8044 int did_delete;
8045 int nextrow;
8046 int lastrow;
8047 int retval;
8049 if (invalid)
8050 wp->w_lines_valid = 0;
8052 if (wp->w_height < 5)
8053 return FAIL;
8055 if (line_count > wp->w_height - row)
8056 line_count = wp->w_height - row;
8058 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
8059 if (retval != MAYBE)
8060 return retval;
8063 * If there is a next window or a status line, we first try to delete the
8064 * lines at the bottom to avoid messing what is after the window.
8065 * If this fails and there are following windows, don't do anything to avoid
8066 * messing up those windows, better just redraw.
8068 did_delete = FALSE;
8069 #ifdef FEAT_WINDOWS
8070 if (wp->w_next != NULL || wp->w_status_height)
8072 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8073 line_count, (int)Rows, FALSE, NULL) == OK)
8074 did_delete = TRUE;
8075 else if (wp->w_next)
8076 return FAIL;
8078 #endif
8080 * if no lines deleted, blank the lines that will end up below the window
8082 if (!did_delete)
8084 #ifdef FEAT_WINDOWS
8085 wp->w_redr_status = TRUE;
8086 #endif
8087 redraw_cmdline = TRUE;
8088 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
8089 lastrow = nextrow + line_count;
8090 if (lastrow > Rows)
8091 lastrow = Rows;
8092 screen_fill(nextrow - line_count, lastrow - line_count,
8093 W_WINCOL(wp), (int)W_ENDCOL(wp),
8094 ' ', ' ', 0);
8097 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
8098 == FAIL)
8100 /* deletion will have messed up other windows */
8101 if (did_delete)
8103 #ifdef FEAT_WINDOWS
8104 wp->w_redr_status = TRUE;
8105 #endif
8106 win_rest_invalid(W_NEXT(wp));
8108 return FAIL;
8111 return OK;
8115 * delete "line_count" window lines at "row" in window "wp"
8116 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
8117 * If "mayclear" is TRUE the screen will be cleared if it is faster than
8118 * scrolling
8119 * Return OK for success, FAIL if the lines are not deleted.
8122 win_del_lines(wp, row, line_count, invalid, mayclear)
8123 win_T *wp;
8124 int row;
8125 int line_count;
8126 int invalid;
8127 int mayclear;
8129 int retval;
8131 if (invalid)
8132 wp->w_lines_valid = 0;
8134 if (line_count > wp->w_height - row)
8135 line_count = wp->w_height - row;
8137 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
8138 if (retval != MAYBE)
8139 return retval;
8141 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
8142 (int)Rows, FALSE, NULL) == FAIL)
8143 return FAIL;
8145 #ifdef FEAT_WINDOWS
8147 * If there are windows or status lines below, try to put them at the
8148 * correct place. If we can't do that, they have to be redrawn.
8150 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
8152 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8153 line_count, (int)Rows, NULL) == FAIL)
8155 wp->w_redr_status = TRUE;
8156 win_rest_invalid(wp->w_next);
8160 * If this is the last window and there is no status line, redraw the
8161 * command line later.
8163 else
8164 #endif
8165 redraw_cmdline = TRUE;
8166 return OK;
8170 * Common code for win_ins_lines() and win_del_lines().
8171 * Returns OK or FAIL when the work has been done.
8172 * Returns MAYBE when not finished yet.
8174 static int
8175 win_do_lines(wp, row, line_count, mayclear, del)
8176 win_T *wp;
8177 int row;
8178 int line_count;
8179 int mayclear;
8180 int del;
8182 int retval;
8184 if (!redrawing() || line_count <= 0)
8185 return FAIL;
8187 /* only a few lines left: redraw is faster */
8188 if (mayclear && Rows - line_count < 5
8189 #ifdef FEAT_VERTSPLIT
8190 && wp->w_width == Columns
8191 #endif
8194 screenclear(); /* will set wp->w_lines_valid to 0 */
8195 return FAIL;
8199 * Delete all remaining lines
8201 if (row + line_count >= wp->w_height)
8203 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
8204 W_WINCOL(wp), (int)W_ENDCOL(wp),
8205 ' ', ' ', 0);
8206 return OK;
8210 * when scrolling, the message on the command line should be cleared,
8211 * otherwise it will stay there forever.
8213 clear_cmdline = TRUE;
8216 * If the terminal can set a scroll region, use that.
8217 * Always do this in a vertically split window. This will redraw from
8218 * ScreenLines[] when t_CV isn't defined. That's faster than using
8219 * win_line().
8220 * Don't use a scroll region when we are going to redraw the text, writing
8221 * a character in the lower right corner of the scroll region causes a
8222 * scroll-up in the DJGPP version.
8224 if (scroll_region
8225 #ifdef FEAT_VERTSPLIT
8226 || W_WIDTH(wp) != Columns
8227 #endif
8230 #ifdef FEAT_VERTSPLIT
8231 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8232 #endif
8233 scroll_region_set(wp, row);
8234 if (del)
8235 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
8236 wp->w_height - row, FALSE, wp);
8237 else
8238 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
8239 wp->w_height - row, wp);
8240 #ifdef FEAT_VERTSPLIT
8241 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8242 #endif
8243 scroll_region_reset();
8244 return retval;
8247 #ifdef FEAT_WINDOWS
8248 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
8249 return FAIL;
8250 #endif
8252 return MAYBE;
8256 * window 'wp' and everything after it is messed up, mark it for redraw
8258 static void
8259 win_rest_invalid(wp)
8260 win_T *wp;
8262 #ifdef FEAT_WINDOWS
8263 while (wp != NULL)
8264 #else
8265 if (wp != NULL)
8266 #endif
8268 redraw_win_later(wp, NOT_VALID);
8269 #ifdef FEAT_WINDOWS
8270 wp->w_redr_status = TRUE;
8271 wp = wp->w_next;
8272 #endif
8274 redraw_cmdline = TRUE;
8278 * The rest of the routines in this file perform screen manipulations. The
8279 * given operation is performed physically on the screen. The corresponding
8280 * change is also made to the internal screen image. In this way, the editor
8281 * anticipates the effect of editing changes on the appearance of the screen.
8282 * That way, when we call screenupdate a complete redraw isn't usually
8283 * necessary. Another advantage is that we can keep adding code to anticipate
8284 * screen changes, and in the meantime, everything still works.
8288 * types for inserting or deleting lines
8290 #define USE_T_CAL 1
8291 #define USE_T_CDL 2
8292 #define USE_T_AL 3
8293 #define USE_T_CE 4
8294 #define USE_T_DL 5
8295 #define USE_T_SR 6
8296 #define USE_NL 7
8297 #define USE_T_CD 8
8298 #define USE_REDRAW 9
8301 * insert lines on the screen and update ScreenLines[]
8302 * 'end' is the line after the scrolled part. Normally it is Rows.
8303 * When scrolling region used 'off' is the offset from the top for the region.
8304 * 'row' and 'end' are relative to the start of the region.
8306 * return FAIL for failure, OK for success.
8309 screen_ins_lines(off, row, line_count, end, wp)
8310 int off;
8311 int row;
8312 int line_count;
8313 int end;
8314 win_T *wp; /* NULL or window to use width from */
8316 int i;
8317 int j;
8318 unsigned temp;
8319 int cursor_row;
8320 int type;
8321 int result_empty;
8322 int can_ce = can_clear(T_CE);
8325 * FAIL if
8326 * - there is no valid screen
8327 * - the screen has to be redrawn completely
8328 * - the line count is less than one
8329 * - the line count is more than 'ttyscroll'
8331 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
8332 return FAIL;
8335 * There are seven ways to insert lines:
8336 * 0. When in a vertically split window and t_CV isn't set, redraw the
8337 * characters from ScreenLines[].
8338 * 1. Use T_CD (clear to end of display) if it exists and the result of
8339 * the insert is just empty lines
8340 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
8341 * present or line_count > 1. It looks better if we do all the inserts
8342 * at once.
8343 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
8344 * insert is just empty lines and T_CE is not present or line_count >
8345 * 1.
8346 * 4. Use T_AL (insert line) if it exists.
8347 * 5. Use T_CE (erase line) if it exists and the result of the insert is
8348 * just empty lines.
8349 * 6. Use T_DL (delete line) if it exists and the result of the insert is
8350 * just empty lines.
8351 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
8352 * the 'da' flag is not set or we have clear line capability.
8353 * 8. redraw the characters from ScreenLines[].
8355 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
8356 * the scrollbar for the window. It does have insert line, use that if it
8357 * exists.
8359 result_empty = (row + line_count >= end);
8360 #ifdef FEAT_VERTSPLIT
8361 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
8362 type = USE_REDRAW;
8363 else
8364 #endif
8365 if (can_clear(T_CD) && result_empty)
8366 type = USE_T_CD;
8367 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
8368 type = USE_T_CAL;
8369 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
8370 type = USE_T_CDL;
8371 else if (*T_AL != NUL)
8372 type = USE_T_AL;
8373 else if (can_ce && result_empty)
8374 type = USE_T_CE;
8375 else if (*T_DL != NUL && result_empty)
8376 type = USE_T_DL;
8377 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
8378 type = USE_T_SR;
8379 else
8380 return FAIL;
8383 * For clearing the lines screen_del_lines() is used. This will also take
8384 * care of t_db if necessary.
8386 if (type == USE_T_CD || type == USE_T_CDL ||
8387 type == USE_T_CE || type == USE_T_DL)
8388 return screen_del_lines(off, row, line_count, end, FALSE, wp);
8391 * If text is retained below the screen, first clear or delete as many
8392 * lines at the bottom of the window as are about to be inserted so that
8393 * the deleted lines won't later surface during a screen_del_lines.
8395 if (*T_DB)
8396 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
8398 #ifdef FEAT_CLIPBOARD
8399 /* Remove a modeless selection when inserting lines halfway the screen
8400 * or not the full width of the screen. */
8401 if (off + row > 0
8402 # ifdef FEAT_VERTSPLIT
8403 || (wp != NULL && wp->w_width != Columns)
8404 # endif
8406 clip_clear_selection();
8407 else
8408 clip_scroll_selection(-line_count);
8409 #endif
8411 #ifdef FEAT_GUI
8412 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
8413 * scrolling is actually carried out. */
8414 gui_dont_update_cursor();
8415 #endif
8417 if (*T_CCS != NUL) /* cursor relative to region */
8418 cursor_row = row;
8419 else
8420 cursor_row = row + off;
8423 * Shift LineOffset[] line_count down to reflect the inserted lines.
8424 * Clear the inserted lines in ScreenLines[].
8426 row += off;
8427 end += off;
8428 for (i = 0; i < line_count; ++i)
8430 #ifdef FEAT_VERTSPLIT
8431 if (wp != NULL && wp->w_width != Columns)
8433 /* need to copy part of a line */
8434 j = end - 1 - i;
8435 while ((j -= line_count) >= row)
8436 linecopy(j + line_count, j, wp);
8437 j += line_count;
8438 if (can_clear((char_u *)" "))
8439 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
8440 else
8441 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
8442 LineWraps[j] = FALSE;
8444 else
8445 #endif
8447 j = end - 1 - i;
8448 temp = LineOffset[j];
8449 while ((j -= line_count) >= row)
8451 LineOffset[j + line_count] = LineOffset[j];
8452 LineWraps[j + line_count] = LineWraps[j];
8454 LineOffset[j + line_count] = temp;
8455 LineWraps[j + line_count] = FALSE;
8456 if (can_clear((char_u *)" "))
8457 lineclear(temp, (int)Columns);
8458 else
8459 lineinvalid(temp, (int)Columns);
8463 screen_stop_highlight();
8464 windgoto(cursor_row, 0);
8466 #ifdef FEAT_VERTSPLIT
8467 /* redraw the characters */
8468 if (type == USE_REDRAW)
8469 redraw_block(row, end, wp);
8470 else
8471 #endif
8472 if (type == USE_T_CAL)
8474 term_append_lines(line_count);
8475 screen_start(); /* don't know where cursor is now */
8477 else
8479 for (i = 0; i < line_count; i++)
8481 if (type == USE_T_AL)
8483 if (i && cursor_row != 0)
8484 windgoto(cursor_row, 0);
8485 out_str(T_AL);
8487 else /* type == USE_T_SR */
8488 out_str(T_SR);
8489 screen_start(); /* don't know where cursor is now */
8494 * With scroll-reverse and 'da' flag set we need to clear the lines that
8495 * have been scrolled down into the region.
8497 if (type == USE_T_SR && *T_DA)
8499 for (i = 0; i < line_count; ++i)
8501 windgoto(off + i, 0);
8502 out_str(T_CE);
8503 screen_start(); /* don't know where cursor is now */
8507 #ifdef FEAT_GUI
8508 gui_can_update_cursor();
8509 if (gui.in_use)
8510 out_flush(); /* always flush after a scroll */
8511 #endif
8512 return OK;
8516 * delete lines on the screen and update ScreenLines[]
8517 * 'end' is the line after the scrolled part. Normally it is Rows.
8518 * When scrolling region used 'off' is the offset from the top for the region.
8519 * 'row' and 'end' are relative to the start of the region.
8521 * Return OK for success, FAIL if the lines are not deleted.
8523 /*ARGSUSED*/
8525 screen_del_lines(off, row, line_count, end, force, wp)
8526 int off;
8527 int row;
8528 int line_count;
8529 int end;
8530 int force; /* even when line_count > p_ttyscroll */
8531 win_T *wp; /* NULL or window to use width from */
8533 int j;
8534 int i;
8535 unsigned temp;
8536 int cursor_row;
8537 int cursor_end;
8538 int result_empty; /* result is empty until end of region */
8539 int can_delete; /* deleting line codes can be used */
8540 int type;
8543 * FAIL if
8544 * - there is no valid screen
8545 * - the screen has to be redrawn completely
8546 * - the line count is less than one
8547 * - the line count is more than 'ttyscroll'
8549 if (!screen_valid(TRUE) || line_count <= 0 ||
8550 (!force && line_count > p_ttyscroll))
8551 return FAIL;
8554 * Check if the rest of the current region will become empty.
8556 result_empty = row + line_count >= end;
8559 * We can delete lines only when 'db' flag not set or when 'ce' option
8560 * available.
8562 can_delete = (*T_DB == NUL || can_clear(T_CE));
8565 * There are six ways to delete lines:
8566 * 0. When in a vertically split window and t_CV isn't set, redraw the
8567 * characters from ScreenLines[].
8568 * 1. Use T_CD if it exists and the result is empty.
8569 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
8570 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
8571 * none of the other ways work.
8572 * 4. Use T_CE (erase line) if the result is empty.
8573 * 5. Use T_DL (delete line) if it exists.
8574 * 6. redraw the characters from ScreenLines[].
8576 #ifdef FEAT_VERTSPLIT
8577 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
8578 type = USE_REDRAW;
8579 else
8580 #endif
8581 if (can_clear(T_CD) && result_empty)
8582 type = USE_T_CD;
8583 #if defined(__BEOS__) && defined(BEOS_DR8)
8585 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
8586 * its internal termcap... this works okay for tests which test *T_DB !=
8587 * NUL. It has the disadvantage that the user cannot use any :set t_*
8588 * command to get T_DB (back) to empty_option, only :set term=... will do
8589 * the trick...
8590 * Anyway, this hack will hopefully go away with the next OS release.
8591 * (Olaf Seibert)
8593 else if (row == 0 && T_DB == empty_option
8594 && (line_count == 1 || *T_CDL == NUL))
8595 #else
8596 else if (row == 0 && (
8597 #ifndef AMIGA
8598 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
8599 * up, so use delete-line command */
8600 line_count == 1 ||
8601 #endif
8602 *T_CDL == NUL))
8603 #endif
8604 type = USE_NL;
8605 else if (*T_CDL != NUL && line_count > 1 && can_delete)
8606 type = USE_T_CDL;
8607 else if (can_clear(T_CE) && result_empty
8608 #ifdef FEAT_VERTSPLIT
8609 && (wp == NULL || wp->w_width == Columns)
8610 #endif
8612 type = USE_T_CE;
8613 else if (*T_DL != NUL && can_delete)
8614 type = USE_T_DL;
8615 else if (*T_CDL != NUL && can_delete)
8616 type = USE_T_CDL;
8617 else
8618 return FAIL;
8620 #ifdef FEAT_CLIPBOARD
8621 /* Remove a modeless selection when deleting lines halfway the screen or
8622 * not the full width of the screen. */
8623 if (off + row > 0
8624 # ifdef FEAT_VERTSPLIT
8625 || (wp != NULL && wp->w_width != Columns)
8626 # endif
8628 clip_clear_selection();
8629 else
8630 clip_scroll_selection(line_count);
8631 #endif
8633 #ifdef FEAT_GUI
8634 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
8635 * scrolling is actually carried out. */
8636 gui_dont_update_cursor();
8637 #endif
8639 if (*T_CCS != NUL) /* cursor relative to region */
8641 cursor_row = row;
8642 cursor_end = end;
8644 else
8646 cursor_row = row + off;
8647 cursor_end = end + off;
8651 * Now shift LineOffset[] line_count up to reflect the deleted lines.
8652 * Clear the inserted lines in ScreenLines[].
8654 row += off;
8655 end += off;
8656 for (i = 0; i < line_count; ++i)
8658 #ifdef FEAT_VERTSPLIT
8659 if (wp != NULL && wp->w_width != Columns)
8661 /* need to copy part of a line */
8662 j = row + i;
8663 while ((j += line_count) <= end - 1)
8664 linecopy(j - line_count, j, wp);
8665 j -= line_count;
8666 if (can_clear((char_u *)" "))
8667 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
8668 else
8669 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
8670 LineWraps[j] = FALSE;
8672 else
8673 #endif
8675 /* whole width, moving the line pointers is faster */
8676 j = row + i;
8677 temp = LineOffset[j];
8678 while ((j += line_count) <= end - 1)
8680 LineOffset[j - line_count] = LineOffset[j];
8681 LineWraps[j - line_count] = LineWraps[j];
8683 LineOffset[j - line_count] = temp;
8684 LineWraps[j - line_count] = FALSE;
8685 if (can_clear((char_u *)" "))
8686 lineclear(temp, (int)Columns);
8687 else
8688 lineinvalid(temp, (int)Columns);
8692 screen_stop_highlight();
8694 #ifdef FEAT_VERTSPLIT
8695 /* redraw the characters */
8696 if (type == USE_REDRAW)
8697 redraw_block(row, end, wp);
8698 else
8699 #endif
8700 if (type == USE_T_CD) /* delete the lines */
8702 windgoto(cursor_row, 0);
8703 out_str(T_CD);
8704 screen_start(); /* don't know where cursor is now */
8706 else if (type == USE_T_CDL)
8708 windgoto(cursor_row, 0);
8709 term_delete_lines(line_count);
8710 screen_start(); /* don't know where cursor is now */
8713 * Deleting lines at top of the screen or scroll region: Just scroll
8714 * the whole screen (scroll region) up by outputting newlines on the
8715 * last line.
8717 else if (type == USE_NL)
8719 windgoto(cursor_end - 1, 0);
8720 for (i = line_count; --i >= 0; )
8721 out_char('\n'); /* cursor will remain on same line */
8723 else
8725 for (i = line_count; --i >= 0; )
8727 if (type == USE_T_DL)
8729 windgoto(cursor_row, 0);
8730 out_str(T_DL); /* delete a line */
8732 else /* type == USE_T_CE */
8734 windgoto(cursor_row + i, 0);
8735 out_str(T_CE); /* erase a line */
8737 screen_start(); /* don't know where cursor is now */
8742 * If the 'db' flag is set, we need to clear the lines that have been
8743 * scrolled up at the bottom of the region.
8745 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
8747 for (i = line_count; i > 0; --i)
8749 windgoto(cursor_end - i, 0);
8750 out_str(T_CE); /* erase a line */
8751 screen_start(); /* don't know where cursor is now */
8755 #ifdef FEAT_GUI
8756 gui_can_update_cursor();
8757 if (gui.in_use)
8758 out_flush(); /* always flush after a scroll */
8759 #endif
8761 return OK;
8765 * show the current mode and ruler
8767 * If clear_cmdline is TRUE, clear the rest of the cmdline.
8768 * If clear_cmdline is FALSE there may be a message there that needs to be
8769 * cleared only if a mode is shown.
8770 * Return the length of the message (0 if no message).
8773 showmode()
8775 int need_clear;
8776 int length = 0;
8777 int do_mode;
8778 int attr;
8779 int nwr_save;
8780 #ifdef FEAT_INS_EXPAND
8781 int sub_attr;
8782 #endif
8784 do_mode = ((p_smd && msg_silent == 0)
8785 && ((State & INSERT)
8786 || restart_edit
8787 #ifdef FEAT_VISUAL
8788 || VIsual_active
8789 #endif
8791 if (do_mode || Recording)
8794 * Don't show mode right now, when not redrawing or inside a mapping.
8795 * Call char_avail() only when we are going to show something, because
8796 * it takes a bit of time.
8798 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
8800 redraw_cmdline = TRUE; /* show mode later */
8801 return 0;
8804 nwr_save = need_wait_return;
8806 /* wait a bit before overwriting an important message */
8807 check_for_delay(FALSE);
8809 /* if the cmdline is more than one line high, erase top lines */
8810 need_clear = clear_cmdline;
8811 if (clear_cmdline && cmdline_row < Rows - 1)
8812 msg_clr_cmdline(); /* will reset clear_cmdline */
8814 /* Position on the last line in the window, column 0 */
8815 msg_pos_mode();
8816 cursor_off();
8817 attr = hl_attr(HLF_CM); /* Highlight mode */
8818 if (do_mode)
8820 MSG_PUTS_ATTR("--", attr);
8821 #if defined(FEAT_XIM)
8822 if (xic != NULL && im_get_status() && !p_imdisable
8823 && curbuf->b_p_iminsert == B_IMODE_IM)
8824 # ifdef HAVE_GTK2 /* most of the time, it's not XIM being used */
8825 MSG_PUTS_ATTR(" IM", attr);
8826 # else
8827 MSG_PUTS_ATTR(" XIM", attr);
8828 # endif
8829 #endif
8830 #if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
8831 if (gui.in_use)
8833 if (hangul_input_state_get())
8834 MSG_PUTS_ATTR(" \307\321\261\333", attr); /* HANGUL */
8836 #endif
8837 #ifdef FEAT_INS_EXPAND
8838 if (edit_submode != NULL) /* CTRL-X in Insert mode */
8840 /* These messages can get long, avoid a wrap in a narrow
8841 * window. Prefer showing edit_submode_extra. */
8842 length = (Rows - msg_row) * Columns - 3;
8843 if (edit_submode_extra != NULL)
8844 length -= vim_strsize(edit_submode_extra);
8845 if (length > 0)
8847 if (edit_submode_pre != NULL)
8848 length -= vim_strsize(edit_submode_pre);
8849 if (length - vim_strsize(edit_submode) > 0)
8851 if (edit_submode_pre != NULL)
8852 msg_puts_attr(edit_submode_pre, attr);
8853 msg_puts_attr(edit_submode, attr);
8855 if (edit_submode_extra != NULL)
8857 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
8858 if ((int)edit_submode_highl < (int)HLF_COUNT)
8859 sub_attr = hl_attr(edit_submode_highl);
8860 else
8861 sub_attr = attr;
8862 msg_puts_attr(edit_submode_extra, sub_attr);
8865 length = 0;
8867 else
8868 #endif
8870 #ifdef FEAT_VREPLACE
8871 if (State & VREPLACE_FLAG)
8872 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
8873 else
8874 #endif
8875 if (State & REPLACE_FLAG)
8876 MSG_PUTS_ATTR(_(" REPLACE"), attr);
8877 else if (State & INSERT)
8879 #ifdef FEAT_RIGHTLEFT
8880 if (p_ri)
8881 MSG_PUTS_ATTR(_(" REVERSE"), attr);
8882 #endif
8883 MSG_PUTS_ATTR(_(" INSERT"), attr);
8885 else if (restart_edit == 'I')
8886 MSG_PUTS_ATTR(_(" (insert)"), attr);
8887 else if (restart_edit == 'R')
8888 MSG_PUTS_ATTR(_(" (replace)"), attr);
8889 else if (restart_edit == 'V')
8890 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
8891 #ifdef FEAT_RIGHTLEFT
8892 if (p_hkmap)
8893 MSG_PUTS_ATTR(_(" Hebrew"), attr);
8894 # ifdef FEAT_FKMAP
8895 if (p_fkmap)
8896 MSG_PUTS_ATTR(farsi_text_5, attr);
8897 # endif
8898 #endif
8899 #ifdef FEAT_KEYMAP
8900 if (State & LANGMAP)
8902 # ifdef FEAT_ARABIC
8903 if (curwin->w_p_arab)
8904 MSG_PUTS_ATTR(_(" Arabic"), attr);
8905 else
8906 # endif
8907 MSG_PUTS_ATTR(_(" (lang)"), attr);
8909 #endif
8910 if ((State & INSERT) && p_paste)
8911 MSG_PUTS_ATTR(_(" (paste)"), attr);
8913 #ifdef FEAT_VISUAL
8914 if (VIsual_active)
8916 char *p;
8918 /* Don't concatenate separate words to avoid translation
8919 * problems. */
8920 switch ((VIsual_select ? 4 : 0)
8921 + (VIsual_mode == Ctrl_V) * 2
8922 + (VIsual_mode == 'V'))
8924 case 0: p = N_(" VISUAL"); break;
8925 case 1: p = N_(" VISUAL LINE"); break;
8926 case 2: p = N_(" VISUAL BLOCK"); break;
8927 case 4: p = N_(" SELECT"); break;
8928 case 5: p = N_(" SELECT LINE"); break;
8929 default: p = N_(" SELECT BLOCK"); break;
8931 MSG_PUTS_ATTR(_(p), attr);
8933 #endif
8934 MSG_PUTS_ATTR(" --", attr);
8937 need_clear = TRUE;
8939 if (Recording
8940 #ifdef FEAT_INS_EXPAND
8941 && edit_submode == NULL /* otherwise it gets too long */
8942 #endif
8945 MSG_PUTS_ATTR(_("recording"), attr);
8946 need_clear = TRUE;
8949 mode_displayed = TRUE;
8950 if (need_clear || clear_cmdline)
8951 msg_clr_eos();
8952 msg_didout = FALSE; /* overwrite this message */
8953 length = msg_col;
8954 msg_col = 0;
8955 need_wait_return = nwr_save; /* never ask for hit-return for this */
8957 else if (clear_cmdline && msg_silent == 0)
8958 /* Clear the whole command line. Will reset "clear_cmdline". */
8959 msg_clr_cmdline();
8961 #ifdef FEAT_CMDL_INFO
8962 # ifdef FEAT_VISUAL
8963 /* In Visual mode the size of the selected area must be redrawn. */
8964 if (VIsual_active)
8965 clear_showcmd();
8966 # endif
8968 /* If the last window has no status line, the ruler is after the mode
8969 * message and must be redrawn */
8970 if (redrawing()
8971 # ifdef FEAT_WINDOWS
8972 && lastwin->w_status_height == 0
8973 # endif
8975 win_redr_ruler(lastwin, TRUE);
8976 #endif
8977 redraw_cmdline = FALSE;
8978 clear_cmdline = FALSE;
8980 return length;
8984 * Position for a mode message.
8986 static void
8987 msg_pos_mode()
8989 msg_col = 0;
8990 msg_row = Rows - 1;
8994 * Delete mode message. Used when ESC is typed which is expected to end
8995 * Insert mode (but Insert mode didn't end yet!).
8996 * Caller should check "mode_displayed".
8998 void
8999 unshowmode(force)
9000 int force;
9003 * Don't delete it right now, when not redrawing or insided a mapping.
9005 if (!redrawing() || (!force && char_avail() && !KeyTyped))
9006 redraw_cmdline = TRUE; /* delete mode later */
9007 else
9009 msg_pos_mode();
9010 if (Recording)
9011 MSG_PUTS_ATTR(_("recording"), hl_attr(HLF_CM));
9012 msg_clr_eos();
9016 #if defined(FEAT_WINDOWS)
9018 * Draw the tab pages line at the top of the Vim window.
9020 static void
9021 draw_tabline()
9023 int tabcount = 0;
9024 tabpage_T *tp;
9025 int tabwidth;
9026 int col = 0;
9027 int scol = 0;
9028 int attr;
9029 win_T *wp;
9030 win_T *cwp;
9031 int wincount;
9032 int modified;
9033 int c;
9034 int len;
9035 int attr_sel = hl_attr(HLF_TPS);
9036 int attr_nosel = hl_attr(HLF_TP);
9037 int attr_fill = hl_attr(HLF_TPF);
9038 char_u *p;
9039 int room;
9040 int use_sep_chars = (t_colors < 8
9041 #ifdef FEAT_GUI
9042 && !gui.in_use
9043 #endif
9046 redraw_tabline = FALSE;
9048 #ifdef FEAT_GUI_TABLINE
9049 /* Take care of a GUI tabline. */
9050 if (gui_use_tabline())
9052 gui_update_tabline();
9053 return;
9055 #endif
9057 if (tabline_height() < 1)
9058 return;
9060 #if defined(FEAT_STL_OPT)
9062 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
9063 for (scol = 0; scol < Columns; ++scol)
9064 TabPageIdxs[scol] = 0;
9066 /* Use the 'tabline' option if it's set. */
9067 if (*p_tal != NUL)
9069 int save_called_emsg = called_emsg;
9071 /* Check for an error. If there is one we would loop in redrawing the
9072 * screen. Avoid that by making 'tabline' empty. */
9073 called_emsg = FALSE;
9074 win_redr_custom(NULL, FALSE);
9075 if (called_emsg)
9076 set_string_option_direct((char_u *)"tabline", -1,
9077 (char_u *)"", OPT_FREE, SID_ERROR);
9078 called_emsg |= save_called_emsg;
9080 else
9081 #endif
9083 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
9084 ++tabcount;
9086 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
9087 if (tabwidth < 6)
9088 tabwidth = 6;
9090 attr = attr_nosel;
9091 tabcount = 0;
9092 scol = 0;
9093 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
9094 tp = tp->tp_next)
9096 scol = col;
9098 if (tp->tp_topframe == topframe)
9099 attr = attr_sel;
9100 if (use_sep_chars && col > 0)
9101 screen_putchar('|', 0, col++, attr);
9103 if (tp->tp_topframe != topframe)
9104 attr = attr_nosel;
9106 screen_putchar(' ', 0, col++, attr);
9108 if (tp == curtab)
9110 cwp = curwin;
9111 wp = firstwin;
9113 else
9115 cwp = tp->tp_curwin;
9116 wp = tp->tp_firstwin;
9119 modified = FALSE;
9120 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
9121 if (bufIsChanged(wp->w_buffer))
9122 modified = TRUE;
9123 if (modified || wincount > 1)
9125 if (wincount > 1)
9127 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
9128 len = (int)STRLEN(NameBuff);
9129 if (col + len >= Columns - 3)
9130 break;
9131 screen_puts_len(NameBuff, len, 0, col,
9132 #if defined(FEAT_SYN_HL)
9133 hl_combine_attr(attr, hl_attr(HLF_T))
9134 #else
9135 attr
9136 #endif
9138 col += len;
9140 if (modified)
9141 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
9142 screen_putchar(' ', 0, col++, attr);
9145 room = scol - col + tabwidth - 1;
9146 if (room > 0)
9148 /* Get buffer name in NameBuff[] */
9149 get_trans_bufname(cwp->w_buffer);
9150 shorten_dir(NameBuff);
9151 len = vim_strsize(NameBuff);
9152 p = NameBuff;
9153 #ifdef FEAT_MBYTE
9154 if (has_mbyte)
9155 while (len > room)
9157 len -= ptr2cells(p);
9158 mb_ptr_adv(p);
9160 else
9161 #endif
9162 if (len > room)
9164 p += len - room;
9165 len = room;
9167 if (len > Columns - col - 1)
9168 len = Columns - col - 1;
9170 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
9171 col += len;
9173 screen_putchar(' ', 0, col++, attr);
9175 /* Store the tab page number in TabPageIdxs[], so that
9176 * jump_to_mouse() knows where each one is. */
9177 ++tabcount;
9178 while (scol < col)
9179 TabPageIdxs[scol++] = tabcount;
9182 if (use_sep_chars)
9183 c = '_';
9184 else
9185 c = ' ';
9186 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
9188 /* Put an "X" for closing the current tab if there are several. */
9189 if (first_tabpage->tp_next != NULL)
9191 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
9192 TabPageIdxs[Columns - 1] = -999;
9196 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
9197 * set. */
9198 redraw_tabline = FALSE;
9202 * Get buffer name for "buf" into NameBuff[].
9203 * Takes care of special buffer names and translates special characters.
9205 void
9206 get_trans_bufname(buf)
9207 buf_T *buf;
9209 if (buf_spname(buf) != NULL)
9210 STRCPY(NameBuff, buf_spname(buf));
9211 else
9212 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
9213 trans_characters(NameBuff, MAXPATHL);
9215 #endif
9217 #if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
9219 * Get the character to use in a status line. Get its attributes in "*attr".
9221 static int
9222 fillchar_status(attr, is_curwin)
9223 int *attr;
9224 int is_curwin;
9226 int fill;
9227 if (is_curwin)
9229 *attr = hl_attr(HLF_S);
9230 fill = fill_stl;
9232 else
9234 *attr = hl_attr(HLF_SNC);
9235 fill = fill_stlnc;
9237 /* Use fill when there is highlighting, and highlighting of current
9238 * window differs, or the fillchars differ, or this is not the
9239 * current window */
9240 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
9241 || !is_curwin || firstwin == lastwin)
9242 || (fill_stl != fill_stlnc)))
9243 return fill;
9244 if (is_curwin)
9245 return '^';
9246 return '=';
9248 #endif
9250 #ifdef FEAT_VERTSPLIT
9252 * Get the character to use in a separator between vertically split windows.
9253 * Get its attributes in "*attr".
9255 static int
9256 fillchar_vsep(attr)
9257 int *attr;
9259 *attr = hl_attr(HLF_C);
9260 if (*attr == 0 && fill_vert == ' ')
9261 return '|';
9262 else
9263 return fill_vert;
9265 #endif
9268 * Return TRUE if redrawing should currently be done.
9271 redrawing()
9273 return (!RedrawingDisabled
9274 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
9278 * Return TRUE if printing messages should currently be done.
9281 messaging()
9283 return (!(p_lz && char_avail() && !KeyTyped));
9287 * Show current status info in ruler and various other places
9288 * If always is FALSE, only show ruler if position has changed.
9290 void
9291 showruler(always)
9292 int always;
9294 if (!always && !redrawing())
9295 return;
9296 #ifdef FEAT_INS_EXPAND
9297 if (pum_visible())
9299 # ifdef FEAT_WINDOWS
9300 /* Don't redraw right now, do it later. */
9301 curwin->w_redr_status = TRUE;
9302 # endif
9303 return;
9305 #endif
9306 #if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
9307 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
9309 redraw_custum_statusline(curwin);
9311 else
9312 #endif
9313 #ifdef FEAT_CMDL_INFO
9314 win_redr_ruler(curwin, always);
9315 #endif
9317 #ifdef FEAT_TITLE
9318 if (need_maketitle
9319 # ifdef FEAT_STL_OPT
9320 || (p_icon && (stl_syntax & STL_IN_ICON))
9321 || (p_title && (stl_syntax & STL_IN_TITLE))
9322 # endif
9324 maketitle();
9325 #endif
9328 #ifdef FEAT_CMDL_INFO
9329 static void
9330 win_redr_ruler(wp, always)
9331 win_T *wp;
9332 int always;
9334 char_u buffer[70];
9335 int row;
9336 int fillchar;
9337 int attr;
9338 int empty_line = FALSE;
9339 colnr_T virtcol;
9340 int i;
9341 int o;
9342 #ifdef FEAT_VERTSPLIT
9343 int this_ru_col;
9344 int off = 0;
9345 int width = Columns;
9346 # define WITH_OFF(x) x
9347 # define WITH_WIDTH(x) x
9348 #else
9349 # define WITH_OFF(x) 0
9350 # define WITH_WIDTH(x) Columns
9351 # define this_ru_col ru_col
9352 #endif
9354 /* If 'ruler' off or redrawing disabled, don't do anything */
9355 if (!p_ru)
9356 return;
9359 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
9360 * after deleting lines, before cursor.lnum is corrected.
9362 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
9363 return;
9365 #ifdef FEAT_INS_EXPAND
9366 /* Don't draw the ruler while doing insert-completion, it might overwrite
9367 * the (long) mode message. */
9368 # ifdef FEAT_WINDOWS
9369 if (wp == lastwin && lastwin->w_status_height == 0)
9370 # endif
9371 if (edit_submode != NULL)
9372 return;
9373 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
9374 if (pum_visible())
9375 return;
9376 #endif
9378 #ifdef FEAT_STL_OPT
9379 if (*p_ruf)
9381 int save_called_emsg = called_emsg;
9383 called_emsg = FALSE;
9384 win_redr_custom(wp, TRUE);
9385 if (called_emsg)
9386 set_string_option_direct((char_u *)"rulerformat", -1,
9387 (char_u *)"", OPT_FREE, SID_ERROR);
9388 called_emsg |= save_called_emsg;
9389 return;
9391 #endif
9394 * Check if not in Insert mode and the line is empty (will show "0-1").
9396 if (!(State & INSERT)
9397 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
9398 empty_line = TRUE;
9401 * Only draw the ruler when something changed.
9403 validate_virtcol_win(wp);
9404 if ( redraw_cmdline
9405 || always
9406 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
9407 || wp->w_cursor.col != wp->w_ru_cursor.col
9408 || wp->w_virtcol != wp->w_ru_virtcol
9409 #ifdef FEAT_VIRTUALEDIT
9410 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
9411 #endif
9412 || wp->w_topline != wp->w_ru_topline
9413 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
9414 #ifdef FEAT_DIFF
9415 || wp->w_topfill != wp->w_ru_topfill
9416 #endif
9417 || empty_line != wp->w_ru_empty)
9419 cursor_off();
9420 #ifdef FEAT_WINDOWS
9421 if (wp->w_status_height)
9423 row = W_WINROW(wp) + wp->w_height;
9424 fillchar = fillchar_status(&attr, wp == curwin);
9425 # ifdef FEAT_VERTSPLIT
9426 off = W_WINCOL(wp);
9427 width = W_WIDTH(wp);
9428 # endif
9430 else
9431 #endif
9433 row = Rows - 1;
9434 fillchar = ' ';
9435 attr = 0;
9436 #ifdef FEAT_VERTSPLIT
9437 width = Columns;
9438 off = 0;
9439 #endif
9442 /* In list mode virtcol needs to be recomputed */
9443 virtcol = wp->w_virtcol;
9444 if (wp->w_p_list && lcs_tab1 == NUL)
9446 wp->w_p_list = FALSE;
9447 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
9448 wp->w_p_list = TRUE;
9452 * Some sprintfs return the length, some return a pointer.
9453 * To avoid portability problems we use strlen() here.
9455 sprintf((char *)buffer, "%ld,",
9456 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
9457 ? 0L
9458 : (long)(wp->w_cursor.lnum));
9459 col_print(buffer + STRLEN(buffer),
9460 empty_line ? 0 : (int)wp->w_cursor.col + 1,
9461 (int)virtcol + 1);
9464 * Add a "50%" if there is room for it.
9465 * On the last line, don't print in the last column (scrolls the
9466 * screen up on some terminals).
9468 i = (int)STRLEN(buffer);
9469 get_rel_pos(wp, buffer + i + 1);
9470 o = i + vim_strsize(buffer + i + 1);
9471 #ifdef FEAT_WINDOWS
9472 if (wp->w_status_height == 0) /* can't use last char of screen */
9473 #endif
9474 ++o;
9475 #ifdef FEAT_VERTSPLIT
9476 this_ru_col = ru_col - (Columns - width);
9477 if (this_ru_col < 0)
9478 this_ru_col = 0;
9479 #endif
9480 /* Never use more than half the window/screen width, leave the other
9481 * half for the filename. */
9482 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
9483 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
9484 if (this_ru_col + o < WITH_WIDTH(width))
9486 while (this_ru_col + o < WITH_WIDTH(width))
9488 #ifdef FEAT_MBYTE
9489 if (has_mbyte)
9490 i += (*mb_char2bytes)(fillchar, buffer + i);
9491 else
9492 #endif
9493 buffer[i++] = fillchar;
9494 ++o;
9496 get_rel_pos(wp, buffer + i);
9498 /* Truncate at window boundary. */
9499 #ifdef FEAT_MBYTE
9500 if (has_mbyte)
9502 o = 0;
9503 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
9505 o += (*mb_ptr2cells)(buffer + i);
9506 if (this_ru_col + o > WITH_WIDTH(width))
9508 buffer[i] = NUL;
9509 break;
9513 else
9514 #endif
9515 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
9516 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
9518 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
9519 i = redraw_cmdline;
9520 screen_fill(row, row + 1,
9521 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
9522 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
9523 fillchar, fillchar, attr);
9524 /* don't redraw the cmdline because of showing the ruler */
9525 redraw_cmdline = i;
9526 wp->w_ru_cursor = wp->w_cursor;
9527 wp->w_ru_virtcol = wp->w_virtcol;
9528 wp->w_ru_empty = empty_line;
9529 wp->w_ru_topline = wp->w_topline;
9530 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
9531 #ifdef FEAT_DIFF
9532 wp->w_ru_topfill = wp->w_topfill;
9533 #endif
9536 #endif
9538 #if defined(FEAT_LINEBREAK) || defined(PROTO)
9540 * Return the width of the 'number' column.
9541 * Caller may need to check if 'number' is set.
9542 * Otherwise it depends on 'numberwidth' and the line count.
9545 number_width(wp)
9546 win_T *wp;
9548 int n;
9549 linenr_T lnum;
9551 lnum = wp->w_buffer->b_ml.ml_line_count;
9552 if (lnum == wp->w_nrwidth_line_count)
9553 return wp->w_nrwidth_width;
9554 wp->w_nrwidth_line_count = lnum;
9556 n = 0;
9559 lnum /= 10;
9560 ++n;
9561 } while (lnum > 0);
9563 /* 'numberwidth' gives the minimal width plus one */
9564 if (n < wp->w_p_nuw - 1)
9565 n = wp->w_p_nuw - 1;
9567 wp->w_nrwidth_width = n;
9568 return n;
9570 #endif