Merge branch 'feat/tagfunc'
[vim_extended.git] / src / screen.c
blobf9b1816aa21885f254217f116e73594f591473ce
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 and
29 * ScreenLinesC[][] is not used. When the character occupies two display
30 * cells the next byte in ScreenLines[] is 0.
31 * ScreenLinesC[][] contain up to 'maxcombine' composing characters
32 * (drawn on top of the first character). There is 0 after the last one used.
33 * ScreenLines2[] is only used for euc-jp to store the second byte if the
34 * first byte is 0x8e (single-width character).
36 * The screen_*() functions write to the screen and handle updating
37 * ScreenLines[].
39 * update_screen() is the function that updates all windows and status lines.
40 * It is called form the main loop when must_redraw is non-zero. It may be
41 * called from other places when an immediate screen update is needed.
43 * The part of the buffer that is displayed in a window is set with:
44 * - w_topline (first buffer line in window)
45 * - w_topfill (filler line above the first line)
46 * - w_leftcol (leftmost window cell in window),
47 * - w_skipcol (skipped window cells of first line)
49 * Commands that only move the cursor around in a window, do not need to take
50 * action to update the display. The main loop will check if w_topline is
51 * valid and update it (scroll the window) when needed.
53 * Commands that scroll a window change w_topline and must call
54 * check_cursor() to move the cursor into the visible part of the window, and
55 * call redraw_later(VALID) to have the window displayed by update_screen()
56 * later.
58 * Commands that change text in the buffer must call changed_bytes() or
59 * changed_lines() to mark the area that changed and will require updating
60 * later. The main loop will call update_screen(), which will update each
61 * window that shows the changed buffer. This assumes text above the change
62 * can remain displayed as it is. Text after the change may need updating for
63 * scrolling, folding and syntax highlighting.
65 * Commands that change how a window is displayed (e.g., setting 'list') or
66 * invalidate the contents of a window in another way (e.g., change fold
67 * settings), must call redraw_later(NOT_VALID) to have the whole window
68 * redisplayed by update_screen() later.
70 * Commands that change how a buffer is displayed (e.g., setting 'tabstop')
71 * must call redraw_curbuf_later(NOT_VALID) to have all the windows for the
72 * buffer redisplayed by update_screen() later.
74 * Commands that change highlighting and possibly cause a scroll too must call
75 * redraw_later(SOME_VALID) to update the whole window but still use scrolling
76 * to avoid redrawing everything. But the length of displayed lines must not
77 * change, use NOT_VALID then.
79 * Commands that move the window position must call redraw_later(NOT_VALID).
80 * TODO: should minimize redrawing by scrolling when possible.
82 * Commands that change everything (e.g., resizing the screen) must call
83 * redraw_all_later(NOT_VALID) or redraw_all_later(CLEAR).
85 * Things that are handled indirectly:
86 * - When messages scroll the screen up, msg_scrolled will be set and
87 * update_screen() called to redraw.
90 #include "vim.h"
93 * The attributes that are actually active for writing to the screen.
95 static int screen_attr = 0;
98 * Positioning the cursor is reduced by remembering the last position.
99 * Mostly used by windgoto() and screen_char().
101 static int screen_cur_row, screen_cur_col; /* last known cursor position */
103 #ifdef FEAT_SEARCH_EXTRA
104 static match_T search_hl; /* used for 'hlsearch' highlight matching */
105 #endif
107 #ifdef FEAT_FOLDING
108 static foldinfo_T win_foldinfo; /* info for 'foldcolumn' */
109 #endif
112 * Buffer for one screen line (characters and attributes).
114 static schar_T *current_ScreenLine;
116 static void win_update __ARGS((win_T *wp));
117 static void win_draw_end __ARGS((win_T *wp, int c1, int c2, int row, int endrow, hlf_T hl));
118 #ifdef FEAT_FOLDING
119 static void fold_line __ARGS((win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row));
120 static void fill_foldcolumn __ARGS((char_u *p, win_T *wp, int closed, linenr_T lnum));
121 static void copy_text_attr __ARGS((int off, char_u *buf, int len, int attr));
122 #endif
123 static int win_line __ARGS((win_T *, linenr_T, int, int, int nochange));
124 static int char_needs_redraw __ARGS((int off_from, int off_to, int cols));
125 #ifdef FEAT_RIGHTLEFT
126 static void screen_line __ARGS((int row, int coloff, int endcol, int clear_width, int rlflag));
127 # define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c), (rl))
128 #else
129 static void screen_line __ARGS((int row, int coloff, int endcol, int clear_width));
130 # define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c))
131 #endif
132 #ifdef FEAT_VERTSPLIT
133 static void draw_vsep_win __ARGS((win_T *wp, int row));
134 #endif
135 #ifdef FEAT_STL_OPT
136 static void redraw_custom_statusline __ARGS((win_T *wp));
137 #endif
138 #ifdef FEAT_SEARCH_EXTRA
139 #define SEARCH_HL_PRIORITY 0
140 static void start_search_hl __ARGS((void));
141 static void end_search_hl __ARGS((void));
142 static void prepare_search_hl __ARGS((win_T *wp, linenr_T lnum));
143 static void next_search_hl __ARGS((win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol));
144 #endif
145 static void screen_start_highlight __ARGS((int attr));
146 static void screen_char __ARGS((unsigned off, int row, int col));
147 #ifdef FEAT_MBYTE
148 static void screen_char_2 __ARGS((unsigned off, int row, int col));
149 #endif
150 static void screenclear2 __ARGS((void));
151 static void lineclear __ARGS((unsigned off, int width));
152 static void lineinvalid __ARGS((unsigned off, int width));
153 #ifdef FEAT_VERTSPLIT
154 static void linecopy __ARGS((int to, int from, win_T *wp));
155 static void redraw_block __ARGS((int row, int end, win_T *wp));
156 #endif
157 static int win_do_lines __ARGS((win_T *wp, int row, int line_count, int mayclear, int del));
158 static void win_rest_invalid __ARGS((win_T *wp));
159 static void msg_pos_mode __ARGS((void));
160 #if defined(FEAT_WINDOWS)
161 static void draw_tabline __ARGS((void));
162 #endif
163 #if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
164 static int fillchar_status __ARGS((int *attr, int is_curwin));
165 #endif
166 #ifdef FEAT_VERTSPLIT
167 static int fillchar_vsep __ARGS((int *attr));
168 #endif
169 #ifdef FEAT_STL_OPT
170 static void win_redr_custom __ARGS((win_T *wp, int draw_ruler));
171 #endif
172 #ifdef FEAT_CMDL_INFO
173 static void win_redr_ruler __ARGS((win_T *wp, int always));
174 #endif
176 #if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
177 /* Ugly global: overrule attribute used by screen_char() */
178 static int screen_char_attr = 0;
179 #endif
182 * Redraw the current window later, with update_screen(type).
183 * Set must_redraw only if not already set to a higher value.
184 * e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
186 void
187 redraw_later(type)
188 int type;
190 redraw_win_later(curwin, type);
193 void
194 redraw_win_later(wp, type)
195 win_T *wp;
196 int type;
198 if (wp->w_redr_type < type)
200 wp->w_redr_type = type;
201 if (type >= NOT_VALID)
202 wp->w_lines_valid = 0;
203 if (must_redraw < type) /* must_redraw is the maximum of all windows */
204 must_redraw = type;
209 * Force a complete redraw later. Also resets the highlighting. To be used
210 * after executing a shell command that messes up the screen.
212 void
213 redraw_later_clear()
215 redraw_all_later(CLEAR);
216 #ifdef FEAT_GUI
217 if (gui.in_use)
218 /* Use a code that will reset gui.highlight_mask in
219 * gui_stop_highlight(). */
220 screen_attr = HL_ALL + 1;
221 else
222 #endif
223 /* Use attributes that is very unlikely to appear in text. */
224 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE;
228 * Mark all windows to be redrawn later.
230 void
231 redraw_all_later(type)
232 int type;
234 win_T *wp;
236 FOR_ALL_WINDOWS(wp)
238 redraw_win_later(wp, type);
243 * Mark all windows that are editing the current buffer to be updated later.
245 void
246 redraw_curbuf_later(type)
247 int type;
249 redraw_buf_later(curbuf, type);
252 void
253 redraw_buf_later(buf, type)
254 buf_T *buf;
255 int type;
257 win_T *wp;
259 FOR_ALL_WINDOWS(wp)
261 if (wp->w_buffer == buf)
262 redraw_win_later(wp, type);
267 * Changed something in the current window, at buffer line "lnum", that
268 * requires that line and possibly other lines to be redrawn.
269 * Used when entering/leaving Insert mode with the cursor on a folded line.
270 * Used to remove the "$" from a change command.
271 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
272 * may become invalid and the whole window will have to be redrawn.
274 void
275 redrawWinline(lnum, invalid)
276 linenr_T lnum;
277 int invalid UNUSED; /* 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 /* Don't do anything if the screen structures are (not yet) valid. */
328 if (!screen_valid(TRUE))
329 return;
331 if (must_redraw)
333 if (type < must_redraw) /* use maximal type */
334 type = must_redraw;
336 /* must_redraw is reset here, so that when we run into some weird
337 * reason to redraw while busy redrawing (e.g., asynchronous
338 * scrolling), or update_topline() in win_update() will cause a
339 * scroll, the screen will be redrawn later or in win_update(). */
340 must_redraw = 0;
343 /* Need to update w_lines[]. */
344 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
345 type = NOT_VALID;
347 /* Postpone the redrawing when it's not needed and when being called
348 * recursively. */
349 if (!redrawing() || updating_screen)
351 redraw_later(type); /* remember type for next time */
352 must_redraw = type;
353 if (type > INVERTED_ALL)
354 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
355 return;
358 updating_screen = TRUE;
359 #ifdef FEAT_SYN_HL
360 ++display_tick; /* let syntax code know we're in a next round of
361 * display updating */
362 #endif
365 * if the screen was scrolled up when displaying a message, scroll it down
367 if (msg_scrolled)
369 clear_cmdline = TRUE;
370 if (msg_scrolled > Rows - 5) /* clearing is faster */
371 type = CLEAR;
372 else if (type != CLEAR)
374 check_for_delay(FALSE);
375 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, NULL) == FAIL)
376 type = CLEAR;
377 FOR_ALL_WINDOWS(wp)
379 if (W_WINROW(wp) < msg_scrolled)
381 if (W_WINROW(wp) + wp->w_height > msg_scrolled
382 && wp->w_redr_type < REDRAW_TOP
383 && wp->w_lines_valid > 0
384 && wp->w_topline == wp->w_lines[0].wl_lnum)
386 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
387 wp->w_redr_type = REDRAW_TOP;
389 else
391 wp->w_redr_type = NOT_VALID;
392 #ifdef FEAT_WINDOWS
393 if (W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp)
394 <= msg_scrolled)
395 wp->w_redr_status = TRUE;
396 #endif
400 redraw_cmdline = TRUE;
401 #ifdef FEAT_WINDOWS
402 redraw_tabline = TRUE;
403 #endif
405 msg_scrolled = 0;
406 need_wait_return = FALSE;
409 /* reset cmdline_row now (may have been changed temporarily) */
410 compute_cmdrow();
412 /* Check for changed highlighting */
413 if (need_highlight_changed)
414 highlight_changed();
416 if (type == CLEAR) /* first clear screen */
418 screenclear(); /* will reset clear_cmdline */
419 type = NOT_VALID;
422 if (clear_cmdline) /* going to clear cmdline (done below) */
423 check_for_delay(FALSE);
425 #ifdef FEAT_LINEBREAK
426 /* Force redraw when width of 'number' or 'relativenumber' column
427 * changes. */
428 if (curwin->w_redr_type < NOT_VALID
429 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
430 ? number_width(curwin) : 0))
431 curwin->w_redr_type = NOT_VALID;
432 #endif
435 * Only start redrawing if there is really something to do.
437 if (type == INVERTED)
438 update_curswant();
439 if (curwin->w_redr_type < type
440 && !((type == VALID
441 && curwin->w_lines[0].wl_valid
442 #ifdef FEAT_DIFF
443 && curwin->w_topfill == curwin->w_old_topfill
444 && curwin->w_botfill == curwin->w_old_botfill
445 #endif
446 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
447 #ifdef FEAT_VISUAL
448 || (type == INVERTED
449 && VIsual_active
450 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
451 && curwin->w_old_visual_mode == VIsual_mode
452 && (curwin->w_valid & VALID_VIRTCOL)
453 && curwin->w_old_curswant == curwin->w_curswant)
454 #endif
456 curwin->w_redr_type = type;
458 #ifdef FEAT_WINDOWS
459 /* Redraw the tab pages line if needed. */
460 if (redraw_tabline || type >= NOT_VALID)
461 draw_tabline();
462 #endif
464 #ifdef FEAT_SYN_HL
466 * Correct stored syntax highlighting info for changes in each displayed
467 * buffer. Each buffer must only be done once.
469 FOR_ALL_WINDOWS(wp)
471 if (wp->w_buffer->b_mod_set)
473 # ifdef FEAT_WINDOWS
474 win_T *wwp;
476 /* Check if we already did this buffer. */
477 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
478 if (wwp->w_buffer == wp->w_buffer)
479 break;
480 # endif
481 if (
482 # ifdef FEAT_WINDOWS
483 wwp == wp &&
484 # endif
485 syntax_present(wp->w_buffer))
486 syn_stack_apply_changes(wp->w_buffer);
489 #endif
492 * Go from top to bottom through the windows, redrawing the ones that need
493 * it.
495 #if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
496 did_one = FALSE;
497 #endif
498 #ifdef FEAT_SEARCH_EXTRA
499 search_hl.rm.regprog = NULL;
500 #endif
501 FOR_ALL_WINDOWS(wp)
503 if (wp->w_redr_type != 0)
505 cursor_off();
506 #if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
507 if (!did_one)
509 did_one = TRUE;
510 # ifdef FEAT_SEARCH_EXTRA
511 start_search_hl();
512 # endif
513 # ifdef FEAT_CLIPBOARD
514 /* When Visual area changed, may have to update selection. */
515 if (clip_star.available && clip_isautosel())
516 clip_update_selection();
517 # endif
518 #ifdef FEAT_GUI
519 /* Remove the cursor before starting to do anything, because
520 * scrolling may make it difficult to redraw the text under
521 * it. */
522 if (gui.in_use)
523 gui_undraw_cursor();
524 #endif
526 #endif
527 win_update(wp);
530 #ifdef FEAT_WINDOWS
531 /* redraw status line after the window to minimize cursor movement */
532 if (wp->w_redr_status)
534 cursor_off();
535 win_redr_status(wp);
537 #endif
539 #if defined(FEAT_SEARCH_EXTRA)
540 end_search_hl();
541 #endif
543 #ifdef FEAT_WINDOWS
544 /* Reset b_mod_set flags. Going through all windows is probably faster
545 * than going through all buffers (there could be many buffers). */
546 for (wp = firstwin; wp != NULL; wp = wp->w_next)
547 wp->w_buffer->b_mod_set = FALSE;
548 #else
549 curbuf->b_mod_set = FALSE;
550 #endif
552 updating_screen = FALSE;
553 #ifdef FEAT_GUI
554 gui_may_resize_shell();
555 #endif
557 /* Clear or redraw the command line. Done last, because scrolling may
558 * mess up the command line. */
559 if (clear_cmdline || redraw_cmdline)
560 showmode();
562 /* May put up an introductory message when not editing a file */
563 if (!did_intro && bufempty()
564 && curbuf->b_fname == NULL
565 #ifdef FEAT_WINDOWS
566 && firstwin->w_next == NULL
567 #endif
568 && vim_strchr(p_shm, SHM_INTRO) == NULL)
569 intro_message(FALSE);
570 did_intro = TRUE;
572 #ifdef FEAT_GUI
573 /* Redraw the cursor and update the scrollbars when all screen updating is
574 * done. */
575 if (gui.in_use)
577 out_flush(); /* required before updating the cursor */
578 if (did_one)
579 gui_update_cursor(FALSE, FALSE);
580 gui_update_scrollbars(FALSE);
582 #endif
585 #if defined(FEAT_SIGNS) || defined(FEAT_GUI)
586 static void update_prepare __ARGS((void));
587 static void update_finish __ARGS((void));
590 * Prepare for updating one or more windows.
591 * Caller must check for "updating_screen" already set to avoid recursiveness.
593 static void
594 update_prepare()
596 cursor_off();
597 updating_screen = TRUE;
598 #ifdef FEAT_GUI
599 /* Remove the cursor before starting to do anything, because scrolling may
600 * make it difficult to redraw the text under it. */
601 if (gui.in_use)
602 gui_undraw_cursor();
603 #endif
604 #ifdef FEAT_SEARCH_EXTRA
605 start_search_hl();
606 #endif
610 * Finish updating one or more windows.
612 static void
613 update_finish()
615 if (redraw_cmdline)
616 showmode();
618 # ifdef FEAT_SEARCH_EXTRA
619 end_search_hl();
620 # endif
622 updating_screen = FALSE;
624 # ifdef FEAT_GUI
625 gui_may_resize_shell();
627 /* Redraw the cursor and update the scrollbars when all screen updating is
628 * done. */
629 if (gui.in_use)
631 out_flush(); /* required before updating the cursor */
632 gui_update_cursor(FALSE, FALSE);
633 gui_update_scrollbars(FALSE);
635 # endif
637 #endif
639 #if defined(FEAT_SIGNS) || defined(PROTO)
640 void
641 update_debug_sign(buf, lnum)
642 buf_T *buf;
643 linenr_T lnum;
645 win_T *wp;
646 int doit = FALSE;
648 # ifdef FEAT_FOLDING
649 win_foldinfo.fi_level = 0;
650 # endif
652 /* update/delete a specific mark */
653 FOR_ALL_WINDOWS(wp)
655 if (buf != NULL && lnum > 0)
657 if (wp->w_buffer == buf && lnum >= wp->w_topline
658 && lnum < wp->w_botline)
660 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
661 wp->w_redraw_top = lnum;
662 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
663 wp->w_redraw_bot = lnum;
664 redraw_win_later(wp, VALID);
667 else
668 redraw_win_later(wp, VALID);
669 if (wp->w_redr_type != 0)
670 doit = TRUE;
673 /* Return when there is nothing to do or screen updating already
674 * happening. */
675 if (!doit || updating_screen)
676 return;
678 /* update all windows that need updating */
679 update_prepare();
681 # ifdef FEAT_WINDOWS
682 for (wp = firstwin; wp; wp = wp->w_next)
684 if (wp->w_redr_type != 0)
685 win_update(wp);
686 if (wp->w_redr_status)
687 win_redr_status(wp);
689 # else
690 if (curwin->w_redr_type != 0)
691 win_update(curwin);
692 # endif
694 update_finish();
696 #endif
699 #if defined(FEAT_GUI) || defined(PROTO)
701 * Update a single window, its status line and maybe the command line msg.
702 * Used for the GUI scrollbar.
704 void
705 updateWindow(wp)
706 win_T *wp;
708 /* return if already busy updating */
709 if (updating_screen)
710 return;
712 update_prepare();
714 #ifdef FEAT_CLIPBOARD
715 /* When Visual area changed, may have to update selection. */
716 if (clip_star.available && clip_isautosel())
717 clip_update_selection();
718 #endif
720 win_update(wp);
722 #ifdef FEAT_WINDOWS
723 /* When the screen was cleared redraw the tab pages line. */
724 if (redraw_tabline)
725 draw_tabline();
727 if (wp->w_redr_status
728 # ifdef FEAT_CMDL_INFO
729 || p_ru
730 # endif
731 # ifdef FEAT_STL_OPT
732 || *p_stl != NUL || *wp->w_p_stl != NUL
733 # endif
735 win_redr_status(wp);
736 #endif
738 update_finish();
740 #endif
743 * Update a single window.
745 * This may cause the windows below it also to be redrawn (when clearing the
746 * screen or scrolling lines).
748 * How the window is redrawn depends on wp->w_redr_type. Each type also
749 * implies the one below it.
750 * NOT_VALID redraw the whole window
751 * SOME_VALID redraw the whole window but do scroll when possible
752 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
753 * INVERTED redraw the changed part of the Visual area
754 * INVERTED_ALL redraw the whole Visual area
755 * VALID 1. scroll up/down to adjust for a changed w_topline
756 * 2. update lines at the top when scrolled down
757 * 3. redraw changed text:
758 * - if wp->w_buffer->b_mod_set set, update lines between
759 * b_mod_top and b_mod_bot.
760 * - if wp->w_redraw_top non-zero, redraw lines between
761 * wp->w_redraw_top and wp->w_redr_bot.
762 * - continue redrawing when syntax status is invalid.
763 * 4. if scrolled up, update lines at the bottom.
764 * This results in three areas that may need updating:
765 * top: from first row to top_end (when scrolled down)
766 * mid: from mid_start to mid_end (update inversion or changed text)
767 * bot: from bot_start to last row (when scrolled up)
769 static void
770 win_update(wp)
771 win_T *wp;
773 buf_T *buf = wp->w_buffer;
774 int type;
775 int top_end = 0; /* Below last row of the top area that needs
776 updating. 0 when no top area updating. */
777 int mid_start = 999;/* first row of the mid area that needs
778 updating. 999 when no mid area updating. */
779 int mid_end = 0; /* Below last row of the mid area that needs
780 updating. 0 when no mid area updating. */
781 int bot_start = 999;/* first row of the bot area that needs
782 updating. 999 when no bot area updating */
783 #ifdef FEAT_VISUAL
784 int scrolled_down = FALSE; /* TRUE when scrolled down when
785 w_topline got smaller a bit */
786 #endif
787 #ifdef FEAT_SEARCH_EXTRA
788 matchitem_T *cur; /* points to the match list */
789 int top_to_mod = FALSE; /* redraw above mod_top */
790 #endif
792 int row; /* current window row to display */
793 linenr_T lnum; /* current buffer lnum to display */
794 int idx; /* current index in w_lines[] */
795 int srow; /* starting row of the current line */
797 int eof = FALSE; /* if TRUE, we hit the end of the file */
798 int didline = FALSE; /* if TRUE, we finished the last line */
799 int i;
800 long j;
801 static int recursive = FALSE; /* being called recursively */
802 int old_botline = wp->w_botline;
803 #ifdef FEAT_FOLDING
804 long fold_count;
805 #endif
806 #ifdef FEAT_SYN_HL
807 /* remember what happened to the previous line, to know if
808 * check_visual_highlight() can be used */
809 #define DID_NONE 1 /* didn't update a line */
810 #define DID_LINE 2 /* updated a normal line */
811 #define DID_FOLD 3 /* updated a folded line */
812 int did_update = DID_NONE;
813 linenr_T syntax_last_parsed = 0; /* last parsed text line */
814 #endif
815 linenr_T mod_top = 0;
816 linenr_T mod_bot = 0;
817 #if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
818 int save_got_int;
819 #endif
821 type = wp->w_redr_type;
823 if (type == NOT_VALID)
825 #ifdef FEAT_WINDOWS
826 wp->w_redr_status = TRUE;
827 #endif
828 wp->w_lines_valid = 0;
831 /* Window is zero-height: nothing to draw. */
832 if (wp->w_height == 0)
834 wp->w_redr_type = 0;
835 return;
838 #ifdef FEAT_VERTSPLIT
839 /* Window is zero-width: Only need to draw the separator. */
840 if (wp->w_width == 0)
842 /* draw the vertical separator right of this window */
843 draw_vsep_win(wp, 0);
844 wp->w_redr_type = 0;
845 return;
847 #endif
849 #ifdef FEAT_SEARCH_EXTRA
850 /* Setup for match and 'hlsearch' highlighting. Disable any previous
851 * match */
852 cur = wp->w_match_head;
853 while (cur != NULL)
855 cur->hl.rm = cur->match;
856 if (cur->hlg_id == 0)
857 cur->hl.attr = 0;
858 else
859 cur->hl.attr = syn_id2attr(cur->hlg_id);
860 cur->hl.buf = buf;
861 cur->hl.lnum = 0;
862 cur->hl.first_lnum = 0;
863 # ifdef FEAT_RELTIME
864 /* Set the time limit to 'redrawtime'. */
865 profile_setlimit(p_rdt, &(cur->hl.tm));
866 # endif
867 cur = cur->next;
869 search_hl.buf = buf;
870 search_hl.lnum = 0;
871 search_hl.first_lnum = 0;
872 /* time limit is set at the toplevel, for all windows */
873 #endif
875 #ifdef FEAT_LINEBREAK
876 /* Force redraw when width of 'number' or 'relativenumber' column
877 * changes. */
878 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
879 if (wp->w_nrwidth != i)
881 type = NOT_VALID;
882 wp->w_nrwidth = i;
884 else
885 #endif
887 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
890 * When there are both inserted/deleted lines and specific lines to be
891 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
892 * everything (only happens when redrawing is off for while).
894 type = NOT_VALID;
896 else
899 * Set mod_top to the first line that needs displaying because of
900 * changes. Set mod_bot to the first line after the changes.
902 mod_top = wp->w_redraw_top;
903 if (wp->w_redraw_bot != 0)
904 mod_bot = wp->w_redraw_bot + 1;
905 else
906 mod_bot = 0;
907 wp->w_redraw_top = 0; /* reset for next time */
908 wp->w_redraw_bot = 0;
909 if (buf->b_mod_set)
911 if (mod_top == 0 || mod_top > buf->b_mod_top)
913 mod_top = buf->b_mod_top;
914 #ifdef FEAT_SYN_HL
915 /* Need to redraw lines above the change that may be included
916 * in a pattern match. */
917 if (syntax_present(buf))
919 mod_top -= buf->b_syn_sync_linebreaks;
920 if (mod_top < 1)
921 mod_top = 1;
923 #endif
925 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
926 mod_bot = buf->b_mod_bot;
928 #ifdef FEAT_SEARCH_EXTRA
929 /* When 'hlsearch' is on and using a multi-line search pattern, a
930 * change in one line may make the Search highlighting in a
931 * previous line invalid. Simple solution: redraw all visible
932 * lines above the change.
933 * Same for a match pattern.
935 if (search_hl.rm.regprog != NULL
936 && re_multiline(search_hl.rm.regprog))
937 top_to_mod = TRUE;
938 else
940 cur = wp->w_match_head;
941 while (cur != NULL)
943 if (cur->match.regprog != NULL
944 && re_multiline(cur->match.regprog))
946 top_to_mod = TRUE;
947 break;
949 cur = cur->next;
952 #endif
954 #ifdef FEAT_FOLDING
955 if (mod_top != 0 && hasAnyFolding(wp))
957 linenr_T lnumt, lnumb;
960 * A change in a line can cause lines above it to become folded or
961 * unfolded. Find the top most buffer line that may be affected.
962 * If the line was previously folded and displayed, get the first
963 * line of that fold. If the line is folded now, get the first
964 * folded line. Use the minimum of these two.
967 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
968 * the line below it. If there is no valid entry, use w_topline.
969 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
970 * to this line. If there is no valid entry, use MAXLNUM. */
971 lnumt = wp->w_topline;
972 lnumb = MAXLNUM;
973 for (i = 0; i < wp->w_lines_valid; ++i)
974 if (wp->w_lines[i].wl_valid)
976 if (wp->w_lines[i].wl_lastlnum < mod_top)
977 lnumt = wp->w_lines[i].wl_lastlnum + 1;
978 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
980 lnumb = wp->w_lines[i].wl_lnum;
981 /* When there is a fold column it might need updating
982 * in the next line ("J" just above an open fold). */
983 if (wp->w_p_fdc > 0)
984 ++lnumb;
988 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
989 if (mod_top > lnumt)
990 mod_top = lnumt;
992 /* Now do the same for the bottom line (one above mod_bot). */
993 --mod_bot;
994 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
995 ++mod_bot;
996 if (mod_bot < lnumb)
997 mod_bot = lnumb;
999 #endif
1001 /* When a change starts above w_topline and the end is below
1002 * w_topline, start redrawing at w_topline.
1003 * If the end of the change is above w_topline: do like no change was
1004 * made, but redraw the first line to find changes in syntax. */
1005 if (mod_top != 0 && mod_top < wp->w_topline)
1007 if (mod_bot > wp->w_topline)
1008 mod_top = wp->w_topline;
1009 #ifdef FEAT_SYN_HL
1010 else if (syntax_present(buf))
1011 top_end = 1;
1012 #endif
1015 /* When line numbers are displayed need to redraw all lines below
1016 * inserted/deleted lines. */
1017 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1018 mod_bot = MAXLNUM;
1022 * When only displaying the lines at the top, set top_end. Used when
1023 * window has scrolled down for msg_scrolled.
1025 if (type == REDRAW_TOP)
1027 j = 0;
1028 for (i = 0; i < wp->w_lines_valid; ++i)
1030 j += wp->w_lines[i].wl_size;
1031 if (j >= wp->w_upd_rows)
1033 top_end = j;
1034 break;
1037 if (top_end == 0)
1038 /* not found (cannot happen?): redraw everything */
1039 type = NOT_VALID;
1040 else
1041 /* top area defined, the rest is VALID */
1042 type = VALID;
1045 /* Trick: we want to avoid clearing the screen twice. screenclear() will
1046 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1047 * non-zero and thus not FALSE) will indicate that screenclear() was not
1048 * called. */
1049 if (screen_cleared)
1050 screen_cleared = MAYBE;
1053 * If there are no changes on the screen that require a complete redraw,
1054 * handle three cases:
1055 * 1: we are off the top of the screen by a few lines: scroll down
1056 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1057 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1058 * w_lines[] that needs updating.
1060 if ((type == VALID || type == SOME_VALID
1061 || type == INVERTED || type == INVERTED_ALL)
1062 #ifdef FEAT_DIFF
1063 && !wp->w_botfill && !wp->w_old_botfill
1064 #endif
1067 if (mod_top != 0 && wp->w_topline == mod_top)
1070 * w_topline is the first changed line, the scrolling will be done
1071 * further down.
1074 else if (wp->w_lines[0].wl_valid
1075 && (wp->w_topline < wp->w_lines[0].wl_lnum
1076 #ifdef FEAT_DIFF
1077 || (wp->w_topline == wp->w_lines[0].wl_lnum
1078 && wp->w_topfill > wp->w_old_topfill)
1079 #endif
1083 * New topline is above old topline: May scroll down.
1085 #ifdef FEAT_FOLDING
1086 if (hasAnyFolding(wp))
1088 linenr_T ln;
1090 /* count the number of lines we are off, counting a sequence
1091 * of folded lines as one */
1092 j = 0;
1093 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1095 ++j;
1096 if (j >= wp->w_height - 2)
1097 break;
1098 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1101 else
1102 #endif
1103 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1104 if (j < wp->w_height - 2) /* not too far off */
1106 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1107 #ifdef FEAT_DIFF
1108 /* insert extra lines for previously invisible filler lines */
1109 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1110 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1111 - wp->w_old_topfill;
1112 #endif
1113 if (i < wp->w_height - 2) /* less than a screen off */
1116 * Try to insert the correct number of lines.
1117 * If not the last window, delete the lines at the bottom.
1118 * win_ins_lines may fail when the terminal can't do it.
1120 if (i > 0)
1121 check_for_delay(FALSE);
1122 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1124 if (wp->w_lines_valid != 0)
1126 /* Need to update rows that are new, stop at the
1127 * first one that scrolled down. */
1128 top_end = i;
1129 #ifdef FEAT_VISUAL
1130 scrolled_down = TRUE;
1131 #endif
1133 /* Move the entries that were scrolled, disable
1134 * the entries for the lines to be redrawn. */
1135 if ((wp->w_lines_valid += j) > wp->w_height)
1136 wp->w_lines_valid = wp->w_height;
1137 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1138 wp->w_lines[idx] = wp->w_lines[idx - j];
1139 while (idx >= 0)
1140 wp->w_lines[idx--].wl_valid = FALSE;
1143 else
1144 mid_start = 0; /* redraw all lines */
1146 else
1147 mid_start = 0; /* redraw all lines */
1149 else
1150 mid_start = 0; /* redraw all lines */
1152 else
1155 * New topline is at or below old topline: May scroll up.
1156 * When topline didn't change, find first entry in w_lines[] that
1157 * needs updating.
1160 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1161 j = -1;
1162 row = 0;
1163 for (i = 0; i < wp->w_lines_valid; i++)
1165 if (wp->w_lines[i].wl_valid
1166 && wp->w_lines[i].wl_lnum == wp->w_topline)
1168 j = i;
1169 break;
1171 row += wp->w_lines[i].wl_size;
1173 if (j == -1)
1175 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1176 * lines */
1177 mid_start = 0;
1179 else
1182 * Try to delete the correct number of lines.
1183 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1185 #ifdef FEAT_DIFF
1186 /* If the topline didn't change, delete old filler lines,
1187 * otherwise delete filler lines of the new topline... */
1188 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1189 row += wp->w_old_topfill;
1190 else
1191 row += diff_check_fill(wp, wp->w_topline);
1192 /* ... but don't delete new filler lines. */
1193 row -= wp->w_topfill;
1194 #endif
1195 if (row > 0)
1197 check_for_delay(FALSE);
1198 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1199 bot_start = wp->w_height - row;
1200 else
1201 mid_start = 0; /* redraw all lines */
1203 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1206 * Skip the lines (below the deleted lines) that are still
1207 * valid and don't need redrawing. Copy their info
1208 * upwards, to compensate for the deleted lines. Set
1209 * bot_start to the first row that needs redrawing.
1211 bot_start = 0;
1212 idx = 0;
1213 for (;;)
1215 wp->w_lines[idx] = wp->w_lines[j];
1216 /* stop at line that didn't fit, unless it is still
1217 * valid (no lines deleted) */
1218 if (row > 0 && bot_start + row
1219 + (int)wp->w_lines[j].wl_size > wp->w_height)
1221 wp->w_lines_valid = idx + 1;
1222 break;
1224 bot_start += wp->w_lines[idx++].wl_size;
1226 /* stop at the last valid entry in w_lines[].wl_size */
1227 if (++j >= wp->w_lines_valid)
1229 wp->w_lines_valid = idx;
1230 break;
1233 #ifdef FEAT_DIFF
1234 /* Correct the first entry for filler lines at the top
1235 * when it won't get updated below. */
1236 if (wp->w_p_diff && bot_start > 0)
1237 wp->w_lines[0].wl_size =
1238 plines_win_nofill(wp, wp->w_topline, TRUE)
1239 + wp->w_topfill;
1240 #endif
1245 /* When starting redraw in the first line, redraw all lines. When
1246 * there is only one window it's probably faster to clear the screen
1247 * first. */
1248 if (mid_start == 0)
1250 mid_end = wp->w_height;
1251 if (lastwin == firstwin)
1253 /* Clear the screen when it was not done by win_del_lines() or
1254 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1255 * then. */
1256 if (screen_cleared != TRUE)
1257 screenclear();
1258 #ifdef FEAT_WINDOWS
1259 /* The screen was cleared, redraw the tab pages line. */
1260 if (redraw_tabline)
1261 draw_tabline();
1262 #endif
1266 /* When win_del_lines() or win_ins_lines() caused the screen to be
1267 * cleared (only happens for the first window) or when screenclear()
1268 * was called directly above, "must_redraw" will have been set to
1269 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1270 if (screen_cleared == TRUE)
1271 must_redraw = 0;
1273 else
1275 /* Not VALID or INVERTED: redraw all lines. */
1276 mid_start = 0;
1277 mid_end = wp->w_height;
1280 if (type == SOME_VALID)
1282 /* SOME_VALID: redraw all lines. */
1283 mid_start = 0;
1284 mid_end = wp->w_height;
1285 type = NOT_VALID;
1288 #ifdef FEAT_VISUAL
1289 /* check if we are updating or removing the inverted part */
1290 if ((VIsual_active && buf == curwin->w_buffer)
1291 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1293 linenr_T from, to;
1295 if (VIsual_active)
1297 if (VIsual_active
1298 && (VIsual_mode != wp->w_old_visual_mode
1299 || type == INVERTED_ALL))
1302 * If the type of Visual selection changed, redraw the whole
1303 * selection. Also when the ownership of the X selection is
1304 * gained or lost.
1306 if (curwin->w_cursor.lnum < VIsual.lnum)
1308 from = curwin->w_cursor.lnum;
1309 to = VIsual.lnum;
1311 else
1313 from = VIsual.lnum;
1314 to = curwin->w_cursor.lnum;
1316 /* redraw more when the cursor moved as well */
1317 if (wp->w_old_cursor_lnum < from)
1318 from = wp->w_old_cursor_lnum;
1319 if (wp->w_old_cursor_lnum > to)
1320 to = wp->w_old_cursor_lnum;
1321 if (wp->w_old_visual_lnum < from)
1322 from = wp->w_old_visual_lnum;
1323 if (wp->w_old_visual_lnum > to)
1324 to = wp->w_old_visual_lnum;
1326 else
1329 * Find the line numbers that need to be updated: The lines
1330 * between the old cursor position and the current cursor
1331 * position. Also check if the Visual position changed.
1333 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1335 from = curwin->w_cursor.lnum;
1336 to = wp->w_old_cursor_lnum;
1338 else
1340 from = wp->w_old_cursor_lnum;
1341 to = curwin->w_cursor.lnum;
1342 if (from == 0) /* Visual mode just started */
1343 from = to;
1346 if (VIsual.lnum != wp->w_old_visual_lnum
1347 || VIsual.col != wp->w_old_visual_col)
1349 if (wp->w_old_visual_lnum < from
1350 && wp->w_old_visual_lnum != 0)
1351 from = wp->w_old_visual_lnum;
1352 if (wp->w_old_visual_lnum > to)
1353 to = wp->w_old_visual_lnum;
1354 if (VIsual.lnum < from)
1355 from = VIsual.lnum;
1356 if (VIsual.lnum > to)
1357 to = VIsual.lnum;
1362 * If in block mode and changed column or curwin->w_curswant:
1363 * update all lines.
1364 * First compute the actual start and end column.
1366 if (VIsual_mode == Ctrl_V)
1368 colnr_T fromc, toc;
1370 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
1371 ++toc;
1372 if (curwin->w_curswant == MAXCOL)
1373 toc = MAXCOL;
1375 if (fromc != wp->w_old_cursor_fcol
1376 || toc != wp->w_old_cursor_lcol)
1378 if (from > VIsual.lnum)
1379 from = VIsual.lnum;
1380 if (to < VIsual.lnum)
1381 to = VIsual.lnum;
1383 wp->w_old_cursor_fcol = fromc;
1384 wp->w_old_cursor_lcol = toc;
1387 else
1389 /* Use the line numbers of the old Visual area. */
1390 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1392 from = wp->w_old_cursor_lnum;
1393 to = wp->w_old_visual_lnum;
1395 else
1397 from = wp->w_old_visual_lnum;
1398 to = wp->w_old_cursor_lnum;
1403 * There is no need to update lines above the top of the window.
1405 if (from < wp->w_topline)
1406 from = wp->w_topline;
1409 * If we know the value of w_botline, use it to restrict the update to
1410 * the lines that are visible in the window.
1412 if (wp->w_valid & VALID_BOTLINE)
1414 if (from >= wp->w_botline)
1415 from = wp->w_botline - 1;
1416 if (to >= wp->w_botline)
1417 to = wp->w_botline - 1;
1421 * Find the minimal part to be updated.
1422 * Watch out for scrolling that made entries in w_lines[] invalid.
1423 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1424 * top_end; need to redraw from top_end to the "to" line.
1425 * A middle mouse click with a Visual selection may change the text
1426 * above the Visual area and reset wl_valid, do count these for
1427 * mid_end (in srow).
1429 if (mid_start > 0)
1431 lnum = wp->w_topline;
1432 idx = 0;
1433 srow = 0;
1434 if (scrolled_down)
1435 mid_start = top_end;
1436 else
1437 mid_start = 0;
1438 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1440 if (wp->w_lines[idx].wl_valid)
1441 mid_start += wp->w_lines[idx].wl_size;
1442 else if (!scrolled_down)
1443 srow += wp->w_lines[idx].wl_size;
1444 ++idx;
1445 # ifdef FEAT_FOLDING
1446 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1447 lnum = wp->w_lines[idx].wl_lnum;
1448 else
1449 # endif
1450 ++lnum;
1452 srow += mid_start;
1453 mid_end = wp->w_height;
1454 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1456 if (wp->w_lines[idx].wl_valid
1457 && wp->w_lines[idx].wl_lnum >= to + 1)
1459 /* Only update until first row of this line */
1460 mid_end = srow;
1461 break;
1463 srow += wp->w_lines[idx].wl_size;
1468 if (VIsual_active && buf == curwin->w_buffer)
1470 wp->w_old_visual_mode = VIsual_mode;
1471 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1472 wp->w_old_visual_lnum = VIsual.lnum;
1473 wp->w_old_visual_col = VIsual.col;
1474 wp->w_old_curswant = curwin->w_curswant;
1476 else
1478 wp->w_old_visual_mode = 0;
1479 wp->w_old_cursor_lnum = 0;
1480 wp->w_old_visual_lnum = 0;
1481 wp->w_old_visual_col = 0;
1483 #endif /* FEAT_VISUAL */
1485 #if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1486 /* reset got_int, otherwise regexp won't work */
1487 save_got_int = got_int;
1488 got_int = 0;
1489 #endif
1490 #ifdef FEAT_FOLDING
1491 win_foldinfo.fi_level = 0;
1492 #endif
1495 * Update all the window rows.
1497 idx = 0; /* first entry in w_lines[].wl_size */
1498 row = 0;
1499 srow = 0;
1500 lnum = wp->w_topline; /* first line shown in window */
1501 for (;;)
1503 /* stop updating when reached the end of the window (check for _past_
1504 * the end of the window is at the end of the loop) */
1505 if (row == wp->w_height)
1507 didline = TRUE;
1508 break;
1511 /* stop updating when hit the end of the file */
1512 if (lnum > buf->b_ml.ml_line_count)
1514 eof = TRUE;
1515 break;
1518 /* Remember the starting row of the line that is going to be dealt
1519 * with. It is used further down when the line doesn't fit. */
1520 srow = row;
1523 * Update a line when it is in an area that needs updating, when it
1524 * has changes or w_lines[idx] is invalid.
1525 * bot_start may be halfway a wrapped line after using
1526 * win_del_lines(), check if the current line includes it.
1527 * When syntax folding is being used, the saved syntax states will
1528 * already have been updated, we can't see where the syntax state is
1529 * the same again, just update until the end of the window.
1531 if (row < top_end
1532 || (row >= mid_start && row < mid_end)
1533 #ifdef FEAT_SEARCH_EXTRA
1534 || top_to_mod
1535 #endif
1536 || idx >= wp->w_lines_valid
1537 || (row + wp->w_lines[idx].wl_size > bot_start)
1538 || (mod_top != 0
1539 && (lnum == mod_top
1540 || (lnum >= mod_top
1541 && (lnum < mod_bot
1542 #ifdef FEAT_SYN_HL
1543 || did_update == DID_FOLD
1544 || (did_update == DID_LINE
1545 && syntax_present(buf)
1546 && (
1547 # ifdef FEAT_FOLDING
1548 (foldmethodIsSyntax(wp)
1549 && hasAnyFolding(wp)) ||
1550 # endif
1551 syntax_check_changed(lnum)))
1552 #endif
1553 )))))
1555 #ifdef FEAT_SEARCH_EXTRA
1556 if (lnum == mod_top)
1557 top_to_mod = FALSE;
1558 #endif
1561 * When at start of changed lines: May scroll following lines
1562 * up or down to minimize redrawing.
1563 * Don't do this when the change continues until the end.
1564 * Don't scroll when dollar_vcol is non-zero, keep the "$".
1566 if (lnum == mod_top
1567 && mod_bot != MAXLNUM
1568 && !(dollar_vcol != 0 && mod_bot == mod_top + 1))
1570 int old_rows = 0;
1571 int new_rows = 0;
1572 int xtra_rows;
1573 linenr_T l;
1575 /* Count the old number of window rows, using w_lines[], which
1576 * should still contain the sizes for the lines as they are
1577 * currently displayed. */
1578 for (i = idx; i < wp->w_lines_valid; ++i)
1580 /* Only valid lines have a meaningful wl_lnum. Invalid
1581 * lines are part of the changed area. */
1582 if (wp->w_lines[i].wl_valid
1583 && wp->w_lines[i].wl_lnum == mod_bot)
1584 break;
1585 old_rows += wp->w_lines[i].wl_size;
1586 #ifdef FEAT_FOLDING
1587 if (wp->w_lines[i].wl_valid
1588 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1590 /* Must have found the last valid entry above mod_bot.
1591 * Add following invalid entries. */
1592 ++i;
1593 while (i < wp->w_lines_valid
1594 && !wp->w_lines[i].wl_valid)
1595 old_rows += wp->w_lines[i++].wl_size;
1596 break;
1598 #endif
1601 if (i >= wp->w_lines_valid)
1603 /* We can't find a valid line below the changed lines,
1604 * need to redraw until the end of the window.
1605 * Inserting/deleting lines has no use. */
1606 bot_start = 0;
1608 else
1610 /* Able to count old number of rows: Count new window
1611 * rows, and may insert/delete lines */
1612 j = idx;
1613 for (l = lnum; l < mod_bot; ++l)
1615 #ifdef FEAT_FOLDING
1616 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1617 ++new_rows;
1618 else
1619 #endif
1620 #ifdef FEAT_DIFF
1621 if (l == wp->w_topline)
1622 new_rows += plines_win_nofill(wp, l, TRUE)
1623 + wp->w_topfill;
1624 else
1625 #endif
1626 new_rows += plines_win(wp, l, TRUE);
1627 ++j;
1628 if (new_rows > wp->w_height - row - 2)
1630 /* it's getting too much, must redraw the rest */
1631 new_rows = 9999;
1632 break;
1635 xtra_rows = new_rows - old_rows;
1636 if (xtra_rows < 0)
1638 /* May scroll text up. If there is not enough
1639 * remaining text or scrolling fails, must redraw the
1640 * rest. If scrolling works, must redraw the text
1641 * below the scrolled text. */
1642 if (row - xtra_rows >= wp->w_height - 2)
1643 mod_bot = MAXLNUM;
1644 else
1646 check_for_delay(FALSE);
1647 if (win_del_lines(wp, row,
1648 -xtra_rows, FALSE, FALSE) == FAIL)
1649 mod_bot = MAXLNUM;
1650 else
1651 bot_start = wp->w_height + xtra_rows;
1654 else if (xtra_rows > 0)
1656 /* May scroll text down. If there is not enough
1657 * remaining text of scrolling fails, must redraw the
1658 * rest. */
1659 if (row + xtra_rows >= wp->w_height - 2)
1660 mod_bot = MAXLNUM;
1661 else
1663 check_for_delay(FALSE);
1664 if (win_ins_lines(wp, row + old_rows,
1665 xtra_rows, FALSE, FALSE) == FAIL)
1666 mod_bot = MAXLNUM;
1667 else if (top_end > row + old_rows)
1668 /* Scrolled the part at the top that requires
1669 * updating down. */
1670 top_end += xtra_rows;
1674 /* When not updating the rest, may need to move w_lines[]
1675 * entries. */
1676 if (mod_bot != MAXLNUM && i != j)
1678 if (j < i)
1680 int x = row + new_rows;
1682 /* move entries in w_lines[] upwards */
1683 for (;;)
1685 /* stop at last valid entry in w_lines[] */
1686 if (i >= wp->w_lines_valid)
1688 wp->w_lines_valid = j;
1689 break;
1691 wp->w_lines[j] = wp->w_lines[i];
1692 /* stop at a line that won't fit */
1693 if (x + (int)wp->w_lines[j].wl_size
1694 > wp->w_height)
1696 wp->w_lines_valid = j + 1;
1697 break;
1699 x += wp->w_lines[j++].wl_size;
1700 ++i;
1702 if (bot_start > x)
1703 bot_start = x;
1705 else /* j > i */
1707 /* move entries in w_lines[] downwards */
1708 j -= i;
1709 wp->w_lines_valid += j;
1710 if (wp->w_lines_valid > wp->w_height)
1711 wp->w_lines_valid = wp->w_height;
1712 for (i = wp->w_lines_valid; i - j >= idx; --i)
1713 wp->w_lines[i] = wp->w_lines[i - j];
1715 /* The w_lines[] entries for inserted lines are
1716 * now invalid, but wl_size may be used above.
1717 * Reset to zero. */
1718 while (i >= idx)
1720 wp->w_lines[i].wl_size = 0;
1721 wp->w_lines[i--].wl_valid = FALSE;
1728 #ifdef FEAT_FOLDING
1730 * When lines are folded, display one line for all of them.
1731 * Otherwise, display normally (can be several display lines when
1732 * 'wrap' is on).
1734 fold_count = foldedCount(wp, lnum, &win_foldinfo);
1735 if (fold_count != 0)
1737 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
1738 ++row;
1739 --fold_count;
1740 wp->w_lines[idx].wl_folded = TRUE;
1741 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
1742 # ifdef FEAT_SYN_HL
1743 did_update = DID_FOLD;
1744 # endif
1746 else
1747 #endif
1748 if (idx < wp->w_lines_valid
1749 && wp->w_lines[idx].wl_valid
1750 && wp->w_lines[idx].wl_lnum == lnum
1751 && lnum > wp->w_topline
1752 && !(dy_flags & DY_LASTLINE)
1753 && srow + wp->w_lines[idx].wl_size > wp->w_height
1754 #ifdef FEAT_DIFF
1755 && diff_check_fill(wp, lnum) == 0
1756 #endif
1759 /* This line is not going to fit. Don't draw anything here,
1760 * will draw "@ " lines below. */
1761 row = wp->w_height + 1;
1763 else
1765 #ifdef FEAT_SEARCH_EXTRA
1766 prepare_search_hl(wp, lnum);
1767 #endif
1768 #ifdef FEAT_SYN_HL
1769 /* Let the syntax stuff know we skipped a few lines. */
1770 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
1771 && syntax_present(buf))
1772 syntax_end_parsing(syntax_last_parsed + 1);
1773 #endif
1776 * Display one line.
1778 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
1780 #ifdef FEAT_FOLDING
1781 wp->w_lines[idx].wl_folded = FALSE;
1782 wp->w_lines[idx].wl_lastlnum = lnum;
1783 #endif
1784 #ifdef FEAT_SYN_HL
1785 did_update = DID_LINE;
1786 syntax_last_parsed = lnum;
1787 #endif
1790 wp->w_lines[idx].wl_lnum = lnum;
1791 wp->w_lines[idx].wl_valid = TRUE;
1792 if (row > wp->w_height) /* past end of screen */
1794 /* we may need the size of that too long line later on */
1795 if (dollar_vcol == 0)
1796 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
1797 ++idx;
1798 break;
1800 if (dollar_vcol == 0)
1801 wp->w_lines[idx].wl_size = row - srow;
1802 ++idx;
1803 #ifdef FEAT_FOLDING
1804 lnum += fold_count + 1;
1805 #else
1806 ++lnum;
1807 #endif
1809 else
1811 /* This line does not need updating, advance to the next one */
1812 row += wp->w_lines[idx++].wl_size;
1813 if (row > wp->w_height) /* past end of screen */
1814 break;
1815 #ifdef FEAT_FOLDING
1816 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
1817 #else
1818 ++lnum;
1819 #endif
1820 #ifdef FEAT_SYN_HL
1821 did_update = DID_NONE;
1822 #endif
1825 if (lnum > buf->b_ml.ml_line_count)
1827 eof = TRUE;
1828 break;
1832 * End of loop over all window lines.
1836 if (idx > wp->w_lines_valid)
1837 wp->w_lines_valid = idx;
1839 #ifdef FEAT_SYN_HL
1841 * Let the syntax stuff know we stop parsing here.
1843 if (syntax_last_parsed != 0 && syntax_present(buf))
1844 syntax_end_parsing(syntax_last_parsed + 1);
1845 #endif
1848 * If we didn't hit the end of the file, and we didn't finish the last
1849 * line we were working on, then the line didn't fit.
1851 wp->w_empty_rows = 0;
1852 #ifdef FEAT_DIFF
1853 wp->w_filler_rows = 0;
1854 #endif
1855 if (!eof && !didline)
1857 if (lnum == wp->w_topline)
1860 * Single line that does not fit!
1861 * Don't overwrite it, it can be edited.
1863 wp->w_botline = lnum + 1;
1865 #ifdef FEAT_DIFF
1866 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
1868 /* Window ends in filler lines. */
1869 wp->w_botline = lnum;
1870 wp->w_filler_rows = wp->w_height - srow;
1872 #endif
1873 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
1876 * Last line isn't finished: Display "@@@" at the end.
1878 screen_fill(W_WINROW(wp) + wp->w_height - 1,
1879 W_WINROW(wp) + wp->w_height,
1880 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
1881 '@', '@', hl_attr(HLF_AT));
1882 set_empty_rows(wp, srow);
1883 wp->w_botline = lnum;
1885 else
1887 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
1888 wp->w_botline = lnum;
1891 else
1893 #ifdef FEAT_VERTSPLIT
1894 draw_vsep_win(wp, row);
1895 #endif
1896 if (eof) /* we hit the end of the file */
1898 wp->w_botline = buf->b_ml.ml_line_count + 1;
1899 #ifdef FEAT_DIFF
1900 j = diff_check_fill(wp, wp->w_botline);
1901 if (j > 0 && !wp->w_botfill)
1904 * Display filler lines at the end of the file
1906 if (char2cells(fill_diff) > 1)
1907 i = '-';
1908 else
1909 i = fill_diff;
1910 if (row + j > wp->w_height)
1911 j = wp->w_height - row;
1912 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
1913 row += j;
1915 #endif
1917 else if (dollar_vcol == 0)
1918 wp->w_botline = lnum;
1920 /* make sure the rest of the screen is blank */
1921 /* put '~'s on rows that aren't part of the file. */
1922 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_AT);
1925 /* Reset the type of redrawing required, the window has been updated. */
1926 wp->w_redr_type = 0;
1927 #ifdef FEAT_DIFF
1928 wp->w_old_topfill = wp->w_topfill;
1929 wp->w_old_botfill = wp->w_botfill;
1930 #endif
1932 if (dollar_vcol == 0)
1935 * There is a trick with w_botline. If we invalidate it on each
1936 * change that might modify it, this will cause a lot of expensive
1937 * calls to plines() in update_topline() each time. Therefore the
1938 * value of w_botline is often approximated, and this value is used to
1939 * compute the value of w_topline. If the value of w_botline was
1940 * wrong, check that the value of w_topline is correct (cursor is on
1941 * the visible part of the text). If it's not, we need to redraw
1942 * again. Mostly this just means scrolling up a few lines, so it
1943 * doesn't look too bad. Only do this for the current window (where
1944 * changes are relevant).
1946 wp->w_valid |= VALID_BOTLINE;
1947 if (wp == curwin && wp->w_botline != old_botline && !recursive)
1949 recursive = TRUE;
1950 curwin->w_valid &= ~VALID_TOPLINE;
1951 update_topline(); /* may invalidate w_botline again */
1952 if (must_redraw != 0)
1954 /* Don't update for changes in buffer again. */
1955 i = curbuf->b_mod_set;
1956 curbuf->b_mod_set = FALSE;
1957 win_update(curwin);
1958 must_redraw = 0;
1959 curbuf->b_mod_set = i;
1961 recursive = FALSE;
1965 #if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1966 /* restore got_int, unless CTRL-C was hit while redrawing */
1967 if (!got_int)
1968 got_int = save_got_int;
1969 #endif
1972 #ifdef FEAT_SIGNS
1973 static int draw_signcolumn __ARGS((win_T *wp));
1976 * Return TRUE when window "wp" has a column to draw signs in.
1978 static int
1979 draw_signcolumn(wp)
1980 win_T *wp;
1982 return (wp->w_buffer->b_signlist != NULL
1983 # ifdef FEAT_NETBEANS_INTG
1984 || usingNetbeans
1985 # endif
1988 #endif
1991 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
1992 * as the filler character.
1994 static void
1995 win_draw_end(wp, c1, c2, row, endrow, hl)
1996 win_T *wp;
1997 int c1;
1998 int c2;
1999 int row;
2000 int endrow;
2001 hlf_T hl;
2003 #if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2004 int n = 0;
2005 # define FDC_OFF n
2006 #else
2007 # define FDC_OFF 0
2008 #endif
2010 #ifdef FEAT_RIGHTLEFT
2011 if (wp->w_p_rl)
2013 /* No check for cmdline window: should never be right-left. */
2014 # ifdef FEAT_FOLDING
2015 n = wp->w_p_fdc;
2017 if (n > 0)
2019 /* draw the fold column at the right */
2020 if (n > W_WIDTH(wp))
2021 n = W_WIDTH(wp);
2022 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2023 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
2024 ' ', ' ', hl_attr(HLF_FC));
2026 # endif
2027 # ifdef FEAT_SIGNS
2028 if (draw_signcolumn(wp))
2030 int nn = n + 2;
2032 /* draw the sign column left of the fold column */
2033 if (nn > W_WIDTH(wp))
2034 nn = W_WIDTH(wp);
2035 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2036 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
2037 ' ', ' ', hl_attr(HLF_SC));
2038 n = nn;
2040 # endif
2041 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2042 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
2043 c2, c2, hl_attr(hl));
2044 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2045 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
2046 c1, c2, hl_attr(hl));
2048 else
2049 #endif
2051 #ifdef FEAT_CMDWIN
2052 if (cmdwin_type != 0 && wp == curwin)
2054 /* draw the cmdline character in the leftmost column */
2055 n = 1;
2056 if (n > wp->w_width)
2057 n = wp->w_width;
2058 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2059 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
2060 cmdwin_type, ' ', hl_attr(HLF_AT));
2062 #endif
2063 #ifdef FEAT_FOLDING
2064 if (wp->w_p_fdc > 0)
2066 int nn = n + wp->w_p_fdc;
2068 /* draw the fold column at the left */
2069 if (nn > W_WIDTH(wp))
2070 nn = W_WIDTH(wp);
2071 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2072 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2073 ' ', ' ', hl_attr(HLF_FC));
2074 n = nn;
2076 #endif
2077 #ifdef FEAT_SIGNS
2078 if (draw_signcolumn(wp))
2080 int nn = n + 2;
2082 /* draw the sign column after the fold column */
2083 if (nn > W_WIDTH(wp))
2084 nn = W_WIDTH(wp);
2085 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2086 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2087 ' ', ' ', hl_attr(HLF_SC));
2088 n = nn;
2090 #endif
2091 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2092 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
2093 c1, c2, hl_attr(hl));
2095 set_empty_rows(wp, row);
2098 #ifdef FEAT_FOLDING
2100 * Display one folded line.
2102 static void
2103 fold_line(wp, fold_count, foldinfo, lnum, row)
2104 win_T *wp;
2105 long fold_count;
2106 foldinfo_T *foldinfo;
2107 linenr_T lnum;
2108 int row;
2110 char_u buf[51];
2111 pos_T *top, *bot;
2112 linenr_T lnume = lnum + fold_count - 1;
2113 int len;
2114 char_u *text;
2115 int fdc;
2116 int col;
2117 int txtcol;
2118 int off = (int)(current_ScreenLine - ScreenLines);
2119 int ri;
2121 /* Build the fold line:
2122 * 1. Add the cmdwin_type for the command-line window
2123 * 2. Add the 'foldcolumn'
2124 * 3. Add the 'number' or 'relativenumber' column
2125 * 4. Compose the text
2126 * 5. Add the text
2127 * 6. set highlighting for the Visual area an other text
2129 col = 0;
2132 * 1. Add the cmdwin_type for the command-line window
2133 * Ignores 'rightleft', this window is never right-left.
2135 #ifdef FEAT_CMDWIN
2136 if (cmdwin_type != 0 && wp == curwin)
2138 ScreenLines[off] = cmdwin_type;
2139 ScreenAttrs[off] = hl_attr(HLF_AT);
2140 #ifdef FEAT_MBYTE
2141 if (enc_utf8)
2142 ScreenLinesUC[off] = 0;
2143 #endif
2144 ++col;
2146 #endif
2149 * 2. Add the 'foldcolumn'
2151 fdc = wp->w_p_fdc;
2152 if (fdc > W_WIDTH(wp) - col)
2153 fdc = W_WIDTH(wp) - col;
2154 if (fdc > 0)
2156 fill_foldcolumn(buf, wp, TRUE, lnum);
2157 #ifdef FEAT_RIGHTLEFT
2158 if (wp->w_p_rl)
2160 int i;
2162 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
2163 hl_attr(HLF_FC));
2164 /* reverse the fold column */
2165 for (i = 0; i < fdc; ++i)
2166 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2168 else
2169 #endif
2170 copy_text_attr(off + col, buf, fdc, hl_attr(HLF_FC));
2171 col += fdc;
2174 #ifdef FEAT_RIGHTLEFT
2175 # define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2176 for (ri = 0; ri < l; ++ri) \
2177 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2178 else \
2179 for (ri = 0; ri < l; ++ri) \
2180 ScreenAttrs[off + (p) + ri] = v
2181 #else
2182 # define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2183 ScreenAttrs[off + (p) + ri] = v
2184 #endif
2186 /* Set all attributes of the 'number' or 'relativenumber' column and the
2187 * text */
2188 RL_MEMSET(col, hl_attr(HLF_FL), W_WIDTH(wp) - col);
2190 #ifdef FEAT_SIGNS
2191 /* If signs are being displayed, add two spaces. */
2192 if (draw_signcolumn(wp))
2194 len = W_WIDTH(wp) - col;
2195 if (len > 0)
2197 if (len > 2)
2198 len = 2;
2199 # ifdef FEAT_RIGHTLEFT
2200 if (wp->w_p_rl)
2201 /* the line number isn't reversed */
2202 copy_text_attr(off + W_WIDTH(wp) - len - col,
2203 (char_u *)" ", len, hl_attr(HLF_FL));
2204 else
2205 # endif
2206 copy_text_attr(off + col, (char_u *)" ", len, hl_attr(HLF_FL));
2207 col += len;
2210 #endif
2213 * 3. Add the 'number' or 'relativenumber' column
2215 if (wp->w_p_nu || wp->w_p_rnu)
2217 len = W_WIDTH(wp) - col;
2218 if (len > 0)
2220 int w = number_width(wp);
2221 long num;
2223 if (len > w + 1)
2224 len = w + 1;
2226 if (wp->w_p_nu)
2227 /* 'number' */
2228 num = (long)lnum;
2229 else
2230 /* 'relativenumber', don't use negative numbers */
2231 num = (long)abs((int)get_cursor_rel_lnum(wp, lnum));
2233 sprintf((char *)buf, "%*ld ", w, num);
2234 #ifdef FEAT_RIGHTLEFT
2235 if (wp->w_p_rl)
2236 /* the line number isn't reversed */
2237 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
2238 hl_attr(HLF_FL));
2239 else
2240 #endif
2241 copy_text_attr(off + col, buf, len, hl_attr(HLF_FL));
2242 col += len;
2247 * 4. Compose the folded-line string with 'foldtext', if set.
2249 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
2251 txtcol = col; /* remember where text starts */
2254 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2255 * Right-left text is put in columns 0 - number-col, normal text is put
2256 * in columns number-col - window-width.
2258 #ifdef FEAT_MBYTE
2259 if (has_mbyte)
2261 int cells;
2262 int u8c, u8cc[MAX_MCO];
2263 int i;
2264 int idx;
2265 int c_len;
2266 char_u *p;
2267 # ifdef FEAT_ARABIC
2268 int prev_c = 0; /* previous Arabic character */
2269 int prev_c1 = 0; /* first composing char for prev_c */
2270 # endif
2272 # ifdef FEAT_RIGHTLEFT
2273 if (wp->w_p_rl)
2274 idx = off;
2275 else
2276 # endif
2277 idx = off + col;
2279 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2280 for (p = text; *p != NUL; )
2282 cells = (*mb_ptr2cells)(p);
2283 c_len = (*mb_ptr2len)(p);
2284 if (col + cells > W_WIDTH(wp)
2285 # ifdef FEAT_RIGHTLEFT
2286 - (wp->w_p_rl ? col : 0)
2287 # endif
2289 break;
2290 ScreenLines[idx] = *p;
2291 if (enc_utf8)
2293 u8c = utfc_ptr2char(p, u8cc);
2294 if (*p < 0x80 && u8cc[0] == 0)
2296 ScreenLinesUC[idx] = 0;
2297 #ifdef FEAT_ARABIC
2298 prev_c = u8c;
2299 #endif
2301 else
2303 #ifdef FEAT_ARABIC
2304 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2306 /* Do Arabic shaping. */
2307 int pc, pc1, nc;
2308 int pcc[MAX_MCO];
2309 int firstbyte = *p;
2311 /* The idea of what is the previous and next
2312 * character depends on 'rightleft'. */
2313 if (wp->w_p_rl)
2315 pc = prev_c;
2316 pc1 = prev_c1;
2317 nc = utf_ptr2char(p + c_len);
2318 prev_c1 = u8cc[0];
2320 else
2322 pc = utfc_ptr2char(p + c_len, pcc);
2323 nc = prev_c;
2324 pc1 = pcc[0];
2326 prev_c = u8c;
2328 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2329 pc, pc1, nc);
2330 ScreenLines[idx] = firstbyte;
2332 else
2333 prev_c = u8c;
2334 #endif
2335 /* Non-BMP character: display as ? or fullwidth ?. */
2336 #ifdef UNICODE16
2337 if (u8c >= 0x10000)
2338 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2339 else
2340 #endif
2341 ScreenLinesUC[idx] = u8c;
2342 for (i = 0; i < Screen_mco; ++i)
2344 ScreenLinesC[i][idx] = u8cc[i];
2345 if (u8cc[i] == 0)
2346 break;
2349 if (cells > 1)
2350 ScreenLines[idx + 1] = 0;
2352 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2353 /* double-byte single width character */
2354 ScreenLines2[idx] = p[1];
2355 else if (cells > 1)
2356 /* double-width character */
2357 ScreenLines[idx + 1] = p[1];
2358 col += cells;
2359 idx += cells;
2360 p += c_len;
2363 else
2364 #endif
2366 len = (int)STRLEN(text);
2367 if (len > W_WIDTH(wp) - col)
2368 len = W_WIDTH(wp) - col;
2369 if (len > 0)
2371 #ifdef FEAT_RIGHTLEFT
2372 if (wp->w_p_rl)
2373 STRNCPY(current_ScreenLine, text, len);
2374 else
2375 #endif
2376 STRNCPY(current_ScreenLine + col, text, len);
2377 col += len;
2381 /* Fill the rest of the line with the fold filler */
2382 #ifdef FEAT_RIGHTLEFT
2383 if (wp->w_p_rl)
2384 col -= txtcol;
2385 #endif
2386 while (col < W_WIDTH(wp)
2387 #ifdef FEAT_RIGHTLEFT
2388 - (wp->w_p_rl ? txtcol : 0)
2389 #endif
2392 #ifdef FEAT_MBYTE
2393 if (enc_utf8)
2395 if (fill_fold >= 0x80)
2397 ScreenLinesUC[off + col] = fill_fold;
2398 ScreenLinesC[0][off + col] = 0;
2400 else
2401 ScreenLinesUC[off + col] = 0;
2403 #endif
2404 ScreenLines[off + col++] = fill_fold;
2407 if (text != buf)
2408 vim_free(text);
2411 * 6. set highlighting for the Visual area an other text.
2412 * If all folded lines are in the Visual area, highlight the line.
2414 #ifdef FEAT_VISUAL
2415 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2417 if (ltoreq(curwin->w_cursor, VIsual))
2419 /* Visual is after curwin->w_cursor */
2420 top = &curwin->w_cursor;
2421 bot = &VIsual;
2423 else
2425 /* Visual is before curwin->w_cursor */
2426 top = &VIsual;
2427 bot = &curwin->w_cursor;
2429 if (lnum >= top->lnum
2430 && lnume <= bot->lnum
2431 && (VIsual_mode != 'v'
2432 || ((lnum > top->lnum
2433 || (lnum == top->lnum
2434 && top->col == 0))
2435 && (lnume < bot->lnum
2436 || (lnume == bot->lnum
2437 && (bot->col - (*p_sel == 'e'))
2438 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
2440 if (VIsual_mode == Ctrl_V)
2442 /* Visual block mode: highlight the chars part of the block */
2443 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2445 if (wp->w_old_cursor_lcol + txtcol < (colnr_T)W_WIDTH(wp))
2446 len = wp->w_old_cursor_lcol;
2447 else
2448 len = W_WIDTH(wp) - txtcol;
2449 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, hl_attr(HLF_V),
2450 len - (int)wp->w_old_cursor_fcol);
2453 else
2455 /* Set all attributes of the text */
2456 RL_MEMSET(txtcol, hl_attr(HLF_V), W_WIDTH(wp) - txtcol);
2460 #endif
2462 #ifdef FEAT_SYN_HL
2463 /* Show 'cursorcolumn' in the fold line. */
2464 if (wp->w_p_cuc)
2466 txtcol += wp->w_virtcol;
2467 if (wp->w_p_wrap)
2468 txtcol -= wp->w_skipcol;
2469 else
2470 txtcol -= wp->w_leftcol;
2471 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2472 ScreenAttrs[off + txtcol] = hl_combine_attr(
2473 ScreenAttrs[off + txtcol], hl_attr(HLF_CUC));
2475 #endif
2477 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2478 (int)W_WIDTH(wp), FALSE);
2481 * Update w_cline_height and w_cline_folded if the cursor line was
2482 * updated (saves a call to plines() later).
2484 if (wp == curwin
2485 && lnum <= curwin->w_cursor.lnum
2486 && lnume >= curwin->w_cursor.lnum)
2488 curwin->w_cline_row = row;
2489 curwin->w_cline_height = 1;
2490 curwin->w_cline_folded = TRUE;
2491 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2496 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2498 static void
2499 copy_text_attr(off, buf, len, attr)
2500 int off;
2501 char_u *buf;
2502 int len;
2503 int attr;
2505 int i;
2507 mch_memmove(ScreenLines + off, buf, (size_t)len);
2508 # ifdef FEAT_MBYTE
2509 if (enc_utf8)
2510 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2511 # endif
2512 for (i = 0; i < len; ++i)
2513 ScreenAttrs[off + i] = attr;
2517 * Fill the foldcolumn at "p" for window "wp".
2518 * Only to be called when 'foldcolumn' > 0.
2520 static void
2521 fill_foldcolumn(p, wp, closed, lnum)
2522 char_u *p;
2523 win_T *wp;
2524 int closed; /* TRUE of FALSE */
2525 linenr_T lnum; /* current line number */
2527 int i = 0;
2528 int level;
2529 int first_level;
2530 int empty;
2532 /* Init to all spaces. */
2533 copy_spaces(p, (size_t)wp->w_p_fdc);
2535 level = win_foldinfo.fi_level;
2536 if (level > 0)
2538 /* If there is only one column put more info in it. */
2539 empty = (wp->w_p_fdc == 1) ? 0 : 1;
2541 /* If the column is too narrow, we start at the lowest level that
2542 * fits and use numbers to indicated the depth. */
2543 first_level = level - wp->w_p_fdc - closed + 1 + empty;
2544 if (first_level < 1)
2545 first_level = 1;
2547 for (i = 0; i + empty < wp->w_p_fdc; ++i)
2549 if (win_foldinfo.fi_lnum == lnum
2550 && first_level + i >= win_foldinfo.fi_low_level)
2551 p[i] = '-';
2552 else if (first_level == 1)
2553 p[i] = '|';
2554 else if (first_level + i <= 9)
2555 p[i] = '0' + first_level + i;
2556 else
2557 p[i] = '>';
2558 if (first_level + i == level)
2559 break;
2562 if (closed)
2563 p[i >= wp->w_p_fdc ? i - 1 : i] = '+';
2565 #endif /* FEAT_FOLDING */
2568 * Display line "lnum" of window 'wp' on the screen.
2569 * Start at row "startrow", stop when "endrow" is reached.
2570 * wp->w_virtcol needs to be valid.
2572 * Return the number of last row the line occupies.
2574 static int
2575 win_line(wp, lnum, startrow, endrow, nochange)
2576 win_T *wp;
2577 linenr_T lnum;
2578 int startrow;
2579 int endrow;
2580 int nochange UNUSED; /* not updating for changed text */
2582 int col; /* visual column on screen */
2583 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2584 int c = 0; /* init for GCC */
2585 long vcol = 0; /* virtual column (for tabs) */
2586 long vcol_prev = -1; /* "vcol" of previous character */
2587 char_u *line; /* current line */
2588 char_u *ptr; /* current position in "line" */
2589 int row; /* row in the window, excl w_winrow */
2590 int screen_row; /* row on the screen, incl w_winrow */
2592 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2593 int n_extra = 0; /* number of extra chars */
2594 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
2595 int c_extra = NUL; /* extra chars, all the same */
2596 int extra_attr = 0; /* attributes when n_extra != 0 */
2597 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2598 displaying lcs_eol at end-of-line */
2599 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2600 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2602 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2603 int saved_n_extra = 0;
2604 char_u *saved_p_extra = NULL;
2605 int saved_c_extra = 0;
2606 int saved_char_attr = 0;
2608 int n_attr = 0; /* chars with special attr */
2609 int saved_attr2 = 0; /* char_attr saved for n_attr */
2610 int n_attr3 = 0; /* chars with overruling special attr */
2611 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2613 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2615 int fromcol, tocol; /* start/end of inverting */
2616 int fromcol_prev = -2; /* start of inverting after cursor */
2617 int noinvcur = FALSE; /* don't invert the cursor */
2618 #ifdef FEAT_VISUAL
2619 pos_T *top, *bot;
2620 int lnum_in_visual_area = FALSE;
2621 #endif
2622 pos_T pos;
2623 long v;
2625 int char_attr = 0; /* attributes for next character */
2626 int attr_pri = FALSE; /* char_attr has priority */
2627 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2628 in this line */
2629 int attr = 0; /* attributes for area highlighting */
2630 int area_attr = 0; /* attributes desired by highlighting */
2631 int search_attr = 0; /* attributes desired by 'hlsearch' */
2632 #ifdef FEAT_SYN_HL
2633 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
2634 int syntax_attr = 0; /* attributes desired by syntax */
2635 int has_syntax = FALSE; /* this buffer has syntax highl. */
2636 int save_did_emsg;
2637 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
2638 #endif
2639 #ifdef FEAT_SPELL
2640 int has_spell = FALSE; /* this buffer has spell checking */
2641 # define SPWORDLEN 150
2642 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
2643 int nextlinecol = 0; /* column where nextline[] starts */
2644 int nextline_idx = 0; /* index in nextline[] where next line
2645 starts */
2646 int spell_attr = 0; /* attributes desired by spelling */
2647 int word_end = 0; /* last byte with same spell_attr */
2648 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2649 static int checked_col = 0; /* column in "checked_lnum" up to which
2650 * there are no spell errors */
2651 static int cap_col = -1; /* column to check for Cap word */
2652 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
2653 int cur_checked_col = 0; /* checked column for current line */
2654 #endif
2655 int extra_check; /* has syntax or linebreak */
2656 #ifdef FEAT_MBYTE
2657 int multi_attr = 0; /* attributes desired by multibyte */
2658 int mb_l = 1; /* multi-byte byte length */
2659 int mb_c = 0; /* decoded multi-byte character */
2660 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
2661 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
2662 #endif
2663 #ifdef FEAT_DIFF
2664 int filler_lines; /* nr of filler lines to be drawn */
2665 int filler_todo; /* nr of filler lines still to do + 1 */
2666 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
2667 int change_start = MAXCOL; /* first col of changed area */
2668 int change_end = -1; /* last col of changed area */
2669 #endif
2670 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
2671 #ifdef FEAT_LINEBREAK
2672 int need_showbreak = FALSE;
2673 #endif
2674 #if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
2675 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
2676 # define LINE_ATTR
2677 int line_attr = 0; /* attribute for the whole line */
2678 #endif
2679 #ifdef FEAT_SEARCH_EXTRA
2680 matchitem_T *cur; /* points to the match list */
2681 match_T *shl; /* points to search_hl or a match */
2682 int shl_flag; /* flag to indicate whether search_hl
2683 has been processed or not */
2684 int prevcol_hl_flag; /* flag to indicate whether prevcol
2685 equals startcol of search_hl or one
2686 of the matches */
2687 #endif
2688 #ifdef FEAT_ARABIC
2689 int prev_c = 0; /* previous Arabic character */
2690 int prev_c1 = 0; /* first composing char for prev_c */
2691 #endif
2692 #if defined(LINE_ATTR)
2693 int did_line_attr = 0;
2694 #endif
2696 /* draw_state: items that are drawn in sequence: */
2697 #define WL_START 0 /* nothing done yet */
2698 #ifdef FEAT_CMDWIN
2699 # define WL_CMDLINE WL_START + 1 /* cmdline window column */
2700 #else
2701 # define WL_CMDLINE WL_START
2702 #endif
2703 #ifdef FEAT_FOLDING
2704 # define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
2705 #else
2706 # define WL_FOLD WL_CMDLINE
2707 #endif
2708 #ifdef FEAT_SIGNS
2709 # define WL_SIGN WL_FOLD + 1 /* column for signs */
2710 #else
2711 # define WL_SIGN WL_FOLD /* column for signs */
2712 #endif
2713 #define WL_NR WL_SIGN + 1 /* line number */
2714 #ifdef FEAT_LINEBREAK
2715 # define WL_BRI WL_NR + 1 /* 'breakindent' */
2716 #else
2717 # define WL_BRI WL_NR
2718 #endif
2719 #if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
2720 # define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
2721 #else
2722 # define WL_SBR WL_BRI
2723 #endif
2724 #define WL_LINE WL_SBR + 1 /* text in the line */
2725 int draw_state = WL_START; /* what to draw next */
2726 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2727 int feedback_col = 0;
2728 int feedback_old_attr = -1;
2729 #endif
2732 if (startrow > endrow) /* past the end already! */
2733 return startrow;
2735 row = startrow;
2736 screen_row = row + W_WINROW(wp);
2739 * To speed up the loop below, set extra_check when there is linebreak,
2740 * trailing white space and/or syntax processing to be done.
2742 #ifdef FEAT_LINEBREAK
2743 extra_check = wp->w_p_lbr;
2744 #else
2745 extra_check = 0;
2746 #endif
2747 #ifdef FEAT_SYN_HL
2748 if (syntax_present(wp->w_buffer) && !wp->w_buffer->b_syn_error)
2750 /* Prepare for syntax highlighting in this line. When there is an
2751 * error, stop syntax highlighting. */
2752 save_did_emsg = did_emsg;
2753 did_emsg = FALSE;
2754 syntax_start(wp, lnum);
2755 if (did_emsg)
2756 wp->w_buffer->b_syn_error = TRUE;
2757 else
2759 did_emsg = save_did_emsg;
2760 has_syntax = TRUE;
2761 extra_check = TRUE;
2764 #endif
2766 #ifdef FEAT_SPELL
2767 if (wp->w_p_spell
2768 && *wp->w_buffer->b_p_spl != NUL
2769 && wp->w_buffer->b_langp.ga_len > 0
2770 && *(char **)(wp->w_buffer->b_langp.ga_data) != NULL)
2772 /* Prepare for spell checking. */
2773 has_spell = TRUE;
2774 extra_check = TRUE;
2776 /* Get the start of the next line, so that words that wrap to the next
2777 * line are found too: "et<line-break>al.".
2778 * Trick: skip a few chars for C/shell/Vim comments */
2779 nextline[SPWORDLEN] = NUL;
2780 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2782 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
2783 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
2786 /* When a word wrapped from the previous line the start of the current
2787 * line is valid. */
2788 if (lnum == checked_lnum)
2789 cur_checked_col = checked_col;
2790 checked_lnum = 0;
2792 /* When there was a sentence end in the previous line may require a
2793 * word starting with capital in this line. In line 1 always check
2794 * the first word. */
2795 if (lnum != capcol_lnum)
2796 cap_col = -1;
2797 if (lnum == 1)
2798 cap_col = 0;
2799 capcol_lnum = 0;
2801 #endif
2804 * handle visual active in this window
2806 fromcol = -10;
2807 tocol = MAXCOL;
2808 #ifdef FEAT_VISUAL
2809 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2811 /* Visual is after curwin->w_cursor */
2812 if (ltoreq(curwin->w_cursor, VIsual))
2814 top = &curwin->w_cursor;
2815 bot = &VIsual;
2817 else /* Visual is before curwin->w_cursor */
2819 top = &VIsual;
2820 bot = &curwin->w_cursor;
2822 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
2823 if (VIsual_mode == Ctrl_V) /* block mode */
2825 if (lnum_in_visual_area)
2827 fromcol = wp->w_old_cursor_fcol;
2828 tocol = wp->w_old_cursor_lcol;
2831 else /* non-block mode */
2833 if (lnum > top->lnum && lnum <= bot->lnum)
2834 fromcol = 0;
2835 else if (lnum == top->lnum)
2837 if (VIsual_mode == 'V') /* linewise */
2838 fromcol = 0;
2839 else
2841 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
2842 if (gchar_pos(top) == NUL)
2843 tocol = fromcol + 1;
2846 if (VIsual_mode != 'V' && lnum == bot->lnum)
2848 if (*p_sel == 'e' && bot->col == 0
2849 #ifdef FEAT_VIRTUALEDIT
2850 && bot->coladd == 0
2851 #endif
2854 fromcol = -10;
2855 tocol = MAXCOL;
2857 else if (bot->col == MAXCOL)
2858 tocol = MAXCOL;
2859 else
2861 pos = *bot;
2862 if (*p_sel == 'e')
2863 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
2864 else
2866 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
2867 ++tocol;
2873 #ifndef MSDOS
2874 /* Check if the character under the cursor should not be inverted */
2875 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
2876 # ifdef FEAT_GUI
2877 && !gui.in_use
2878 # endif
2880 noinvcur = TRUE;
2881 #endif
2883 /* if inverting in this line set area_highlighting */
2884 if (fromcol >= 0)
2886 area_highlighting = TRUE;
2887 attr = hl_attr(HLF_V);
2888 #if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
2889 if (clip_star.available && !clip_star.owned && clip_isautosel())
2890 attr = hl_attr(HLF_VNC);
2891 #endif
2896 * handle 'incsearch' and ":s///c" highlighting
2898 else
2899 #endif /* FEAT_VISUAL */
2900 if (highlight_match
2901 && wp == curwin
2902 && lnum >= curwin->w_cursor.lnum
2903 && lnum <= curwin->w_cursor.lnum + search_match_lines)
2905 if (lnum == curwin->w_cursor.lnum)
2906 getvcol(curwin, &(curwin->w_cursor),
2907 (colnr_T *)&fromcol, NULL, NULL);
2908 else
2909 fromcol = 0;
2910 if (lnum == curwin->w_cursor.lnum + search_match_lines)
2912 pos.lnum = lnum;
2913 pos.col = search_match_endcol;
2914 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
2916 else
2917 tocol = MAXCOL;
2918 /* do at least one character; happens when past end of line */
2919 if (fromcol == tocol)
2920 tocol = fromcol + 1;
2921 area_highlighting = TRUE;
2922 attr = hl_attr(HLF_I);
2925 #ifdef FEAT_DIFF
2926 filler_lines = diff_check(wp, lnum);
2927 if (filler_lines < 0)
2929 if (filler_lines == -1)
2931 if (diff_find_change(wp, lnum, &change_start, &change_end))
2932 diff_hlf = HLF_ADD; /* added line */
2933 else if (change_start == 0)
2934 diff_hlf = HLF_TXD; /* changed text */
2935 else
2936 diff_hlf = HLF_CHD; /* changed line */
2938 else
2939 diff_hlf = HLF_ADD; /* added line */
2940 filler_lines = 0;
2941 area_highlighting = TRUE;
2943 if (lnum == wp->w_topline)
2944 filler_lines = wp->w_topfill;
2945 filler_todo = filler_lines;
2946 #endif
2948 #ifdef LINE_ATTR
2949 # ifdef FEAT_SIGNS
2950 /* If this line has a sign with line highlighting set line_attr. */
2951 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
2952 if (v != 0)
2953 line_attr = sign_get_attr((int)v, TRUE);
2954 # endif
2955 # if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
2956 /* Highlight the current line in the quickfix window. */
2957 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
2958 line_attr = hl_attr(HLF_L);
2959 # endif
2960 if (line_attr != 0)
2961 area_highlighting = TRUE;
2962 #endif
2964 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
2965 ptr = line;
2967 #ifdef FEAT_SPELL
2968 if (has_spell)
2970 /* For checking first word with a capital skip white space. */
2971 if (cap_col == 0)
2972 cap_col = (int)(skipwhite(line) - line);
2974 /* To be able to spell-check over line boundaries copy the end of the
2975 * current line into nextline[]. Above the start of the next line was
2976 * copied to nextline[SPWORDLEN]. */
2977 if (nextline[SPWORDLEN] == NUL)
2979 /* No next line or it is empty. */
2980 nextlinecol = MAXCOL;
2981 nextline_idx = 0;
2983 else
2985 v = (long)STRLEN(line);
2986 if (v < SPWORDLEN)
2988 /* Short line, use it completely and append the start of the
2989 * next line. */
2990 nextlinecol = 0;
2991 mch_memmove(nextline, line, (size_t)v);
2992 STRMOVE(nextline + v, nextline + SPWORDLEN);
2993 nextline_idx = v + 1;
2995 else
2997 /* Long line, use only the last SPWORDLEN bytes. */
2998 nextlinecol = v - SPWORDLEN;
2999 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3000 nextline_idx = SPWORDLEN + 1;
3004 #endif
3006 /* find start of trailing whitespace */
3007 if (wp->w_p_list && lcs_trail)
3009 trailcol = (colnr_T)STRLEN(ptr);
3010 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
3011 --trailcol;
3012 trailcol += (colnr_T) (ptr - line);
3013 extra_check = TRUE;
3017 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3018 * first character to be displayed.
3020 if (wp->w_p_wrap)
3021 v = wp->w_skipcol;
3022 else
3023 v = wp->w_leftcol;
3024 if (v > 0)
3026 #ifdef FEAT_MBYTE
3027 char_u *prev_ptr = ptr;
3028 #endif
3029 while (vcol < v && *ptr != NUL)
3031 c = win_lbr_chartabsize(wp, ptr, (colnr_T)vcol, NULL, lnum);
3032 vcol += c;
3033 #ifdef FEAT_MBYTE
3034 prev_ptr = ptr;
3035 #endif
3036 mb_ptr_adv(ptr);
3039 #if defined(FEAT_SYN_HL) || defined(FEAT_VIRTUALEDIT) || defined(FEAT_VISUAL)
3040 /* When:
3041 * - 'cuc' is set, or
3042 * - 'virtualedit' is set, or
3043 * - the visual mode is active,
3044 * the end of the line may be before the start of the displayed part.
3046 if (vcol < v && (
3047 # ifdef FEAT_SYN_HL
3048 wp->w_p_cuc
3049 # if defined(FEAT_VIRTUALEDIT) || defined(FEAT_VISUAL)
3051 # endif
3052 # endif
3053 # ifdef FEAT_VIRTUALEDIT
3054 virtual_active()
3055 # ifdef FEAT_VISUAL
3057 # endif
3058 # endif
3059 # ifdef FEAT_VISUAL
3060 (VIsual_active && wp->w_buffer == curwin->w_buffer)
3061 # endif
3064 vcol = v;
3066 #endif
3068 /* Handle a character that's not completely on the screen: Put ptr at
3069 * that character but skip the first few screen characters. */
3070 if (vcol > v)
3072 vcol -= c;
3073 #ifdef FEAT_MBYTE
3074 ptr = prev_ptr;
3075 #else
3076 --ptr;
3077 #endif
3078 n_skip = v - vcol;
3082 * Adjust for when the inverted text is before the screen,
3083 * and when the start of the inverted text is before the screen.
3085 if (tocol <= vcol)
3086 fromcol = 0;
3087 else if (fromcol >= 0 && fromcol < vcol)
3088 fromcol = vcol;
3090 #ifdef FEAT_LINEBREAK
3091 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3092 if (wp->w_p_wrap)
3093 need_showbreak = TRUE;
3094 #endif
3095 #ifdef FEAT_SPELL
3096 /* When spell checking a word we need to figure out the start of the
3097 * word and if it's badly spelled or not. */
3098 if (has_spell)
3100 int len;
3101 colnr_T linecol = (colnr_T)(ptr - line);
3102 hlf_T spell_hlf = HLF_COUNT;
3104 pos = wp->w_cursor;
3105 wp->w_cursor.lnum = lnum;
3106 wp->w_cursor.col = linecol;
3107 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
3109 /* spell_move_to() may call ml_get() and make "line" invalid */
3110 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3111 ptr = line + linecol;
3113 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
3115 /* no bad word found at line start, don't check until end of a
3116 * word */
3117 spell_hlf = HLF_COUNT;
3118 word_end = (int)(spell_to_word_end(ptr, wp->w_buffer)
3119 - line + 1);
3121 else
3123 /* bad word found, use attributes until end of word */
3124 word_end = wp->w_cursor.col + len + 1;
3126 /* Turn index into actual attributes. */
3127 if (spell_hlf != HLF_COUNT)
3128 spell_attr = highlight_attr[spell_hlf];
3130 wp->w_cursor = pos;
3132 # ifdef FEAT_SYN_HL
3133 /* Need to restart syntax highlighting for this line. */
3134 if (has_syntax)
3135 syntax_start(wp, lnum);
3136 # endif
3138 #endif
3142 * Correct highlighting for cursor that can't be disabled.
3143 * Avoids having to check this for each character.
3145 if (fromcol >= 0)
3147 if (noinvcur)
3149 if ((colnr_T)fromcol == wp->w_virtcol)
3151 /* highlighting starts at cursor, let it start just after the
3152 * cursor */
3153 fromcol_prev = fromcol;
3154 fromcol = -1;
3156 else if ((colnr_T)fromcol < wp->w_virtcol)
3157 /* restart highlighting after the cursor */
3158 fromcol_prev = wp->w_virtcol;
3160 if (fromcol >= tocol)
3161 fromcol = -1;
3164 #ifdef FEAT_SEARCH_EXTRA
3166 * Handle highlighting the last used search pattern and matches.
3167 * Do this for both search_hl and the match list.
3169 cur = wp->w_match_head;
3170 shl_flag = FALSE;
3171 while (cur != NULL || shl_flag == FALSE)
3173 if (shl_flag == FALSE)
3175 shl = &search_hl;
3176 shl_flag = TRUE;
3178 else
3179 shl = &cur->hl;
3180 shl->startcol = MAXCOL;
3181 shl->endcol = MAXCOL;
3182 shl->attr_cur = 0;
3183 if (shl->rm.regprog != NULL)
3185 v = (long)(ptr - line);
3186 next_search_hl(wp, shl, lnum, (colnr_T)v);
3188 /* Need to get the line again, a multi-line regexp may have made it
3189 * invalid. */
3190 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3191 ptr = line + v;
3193 if (shl->lnum != 0 && shl->lnum <= lnum)
3195 if (shl->lnum == lnum)
3196 shl->startcol = shl->rm.startpos[0].col;
3197 else
3198 shl->startcol = 0;
3199 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3200 - shl->rm.startpos[0].lnum)
3201 shl->endcol = shl->rm.endpos[0].col;
3202 else
3203 shl->endcol = MAXCOL;
3204 /* Highlight one character for an empty match. */
3205 if (shl->startcol == shl->endcol)
3207 #ifdef FEAT_MBYTE
3208 if (has_mbyte && line[shl->endcol] != NUL)
3209 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3210 else
3211 #endif
3212 ++shl->endcol;
3214 if ((long)shl->startcol < v) /* match at leftcol */
3216 shl->attr_cur = shl->attr;
3217 search_attr = shl->attr;
3219 area_highlighting = TRUE;
3222 if (shl != &search_hl && cur != NULL)
3223 cur = cur->next;
3225 #endif
3227 #ifdef FEAT_SYN_HL
3228 /* Cursor line highlighting for 'cursorline'. Not when Visual mode is
3229 * active, because it's not clear what is selected then. */
3230 if (wp->w_p_cul && lnum == wp->w_cursor.lnum && !VIsual_active)
3232 line_attr = hl_attr(HLF_CUL);
3233 area_highlighting = TRUE;
3235 #endif
3237 off = (unsigned)(current_ScreenLine - ScreenLines);
3238 col = 0;
3239 #ifdef FEAT_RIGHTLEFT
3240 if (wp->w_p_rl)
3242 /* Rightleft window: process the text in the normal direction, but put
3243 * it in current_ScreenLine[] from right to left. Start at the
3244 * rightmost column of the window. */
3245 col = W_WIDTH(wp) - 1;
3246 off += col;
3248 #endif
3251 * Repeat for the whole displayed line.
3253 for (;;)
3255 /* Skip this quickly when working on the text. */
3256 if (draw_state != WL_LINE)
3258 #ifdef FEAT_CMDWIN
3259 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3261 draw_state = WL_CMDLINE;
3262 if (cmdwin_type != 0 && wp == curwin)
3264 /* Draw the cmdline character. */
3265 n_extra = 1;
3266 c_extra = cmdwin_type;
3267 char_attr = hl_attr(HLF_AT);
3270 #endif
3272 #ifdef FEAT_FOLDING
3273 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3275 draw_state = WL_FOLD;
3276 if (wp->w_p_fdc > 0)
3278 /* Draw the 'foldcolumn'. */
3279 fill_foldcolumn(extra, wp, FALSE, lnum);
3280 n_extra = wp->w_p_fdc;
3281 p_extra = extra;
3282 p_extra[n_extra] = NUL;
3283 c_extra = NUL;
3284 char_attr = hl_attr(HLF_FC);
3287 #endif
3289 #ifdef FEAT_SIGNS
3290 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3292 draw_state = WL_SIGN;
3293 /* Show the sign column when there are any signs in this
3294 * buffer or when using Netbeans. */
3295 if (draw_signcolumn(wp)
3296 # ifdef FEAT_DIFF
3297 && filler_todo <= 0
3298 # endif
3301 int_u text_sign;
3302 # ifdef FEAT_SIGN_ICONS
3303 int_u icon_sign;
3304 # endif
3306 /* Draw two cells with the sign value or blank. */
3307 c_extra = ' ';
3308 char_attr = hl_attr(HLF_SC);
3309 n_extra = 2;
3311 if (row == startrow)
3313 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3314 SIGN_TEXT);
3315 # ifdef FEAT_SIGN_ICONS
3316 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3317 SIGN_ICON);
3318 if (gui.in_use && icon_sign != 0)
3320 /* Use the image in this position. */
3321 c_extra = SIGN_BYTE;
3322 # ifdef FEAT_NETBEANS_INTG
3323 if (buf_signcount(wp->w_buffer, lnum) > 1)
3324 c_extra = MULTISIGN_BYTE;
3325 # endif
3326 char_attr = icon_sign;
3328 else
3329 # endif
3330 if (text_sign != 0)
3332 p_extra = sign_get_text(text_sign);
3333 if (p_extra != NULL)
3335 c_extra = NUL;
3336 n_extra = (int)STRLEN(p_extra);
3338 char_attr = sign_get_attr(text_sign, FALSE);
3343 #endif
3345 if (draw_state == WL_NR - 1 && n_extra == 0)
3347 draw_state = WL_NR;
3348 /* Display the absolute or relative line number. After the
3349 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3350 if ((wp->w_p_nu || wp->w_p_rnu)
3351 && (row == startrow
3352 #ifdef FEAT_DIFF
3353 + filler_lines
3354 #endif
3355 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3357 /* Draw the line number (empty space after wrapping). */
3358 if (row == startrow
3359 #ifdef FEAT_DIFF
3360 + filler_lines
3361 #endif
3364 long num;
3366 if (wp->w_p_nu)
3367 /* 'number' */
3368 num = (long)lnum;
3369 else
3370 /* 'relativenumber', don't use negative numbers */
3371 num = (long)abs((int)get_cursor_rel_lnum(wp,
3372 lnum));
3374 sprintf((char *)extra, "%*ld ",
3375 number_width(wp), num);
3376 if (wp->w_skipcol > 0)
3377 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3378 *p_extra = '-';
3379 #ifdef FEAT_RIGHTLEFT
3380 if (wp->w_p_rl) /* reverse line numbers */
3381 rl_mirror(extra);
3382 #endif
3383 p_extra = extra;
3384 c_extra = NUL;
3386 else
3387 c_extra = ' ';
3388 n_extra = number_width(wp) + 1;
3389 char_attr = hl_attr(HLF_N);
3390 #ifdef FEAT_SYN_HL
3391 /* When 'cursorline' is set highlight the line number of
3392 * the current line differently. */
3393 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3394 char_attr = hl_combine_attr(hl_attr(HLF_CUL), char_attr);
3395 #endif
3399 #ifdef FEAT_LINEBREAK
3400 /* draw 'breakindent': indent wrapped text accordingly */
3401 if (draw_state == WL_BRI -1 && n_extra == 0){
3402 draw_state = WL_BRI;
3403 # ifdef FEAT_DIFF
3404 /* FIXME: handle (filler_todo > 0): or modify showbreak so that ---- lines are shorter by the amount needed? */
3405 # endif
3406 if (wp->w_p_bri && row != startrow){ /* FIXME: what is startrow? Don't we need it as well?? */
3407 p_extra = NUL;
3408 c_extra = ' ';
3409 n_extra = get_breakindent_win(wp,lnum);
3410 char_attr = 0; /* was: hl_attr(HLF_AT); */
3411 /* FIXME: why do we need to adjust vcol if showbreak does not?? */
3412 vcol += n_extra;
3413 /* FIXME: is this relevant here? copied shamelessly from showbreak */
3414 /* Correct end of highlighted area for 'breakindent',
3415 * required when 'linebreak' is also set. */
3416 if (tocol == vcol)
3417 tocol += n_extra;
3420 #endif
3423 #if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3424 if (draw_state == WL_SBR - 1 && n_extra == 0)
3426 draw_state = WL_SBR;
3427 # ifdef FEAT_DIFF
3428 if (filler_todo > 0)
3430 /* Draw "deleted" diff line(s). */
3431 if (char2cells(fill_diff) > 1)
3432 c_extra = '-';
3433 else
3434 c_extra = fill_diff;
3435 # ifdef FEAT_RIGHTLEFT
3436 if (wp->w_p_rl)
3437 n_extra = col + 1;
3438 else
3439 # endif
3440 n_extra = W_WIDTH(wp) - col;
3441 char_attr = hl_attr(HLF_DED);
3443 # endif
3444 # ifdef FEAT_LINEBREAK
3445 if (*p_sbr != NUL && need_showbreak)
3447 /* Draw 'showbreak' at the start of each broken line. */
3448 p_extra = p_sbr;
3449 c_extra = NUL;
3450 n_extra = (int)STRLEN(p_sbr);
3451 char_attr = hl_attr(HLF_AT);
3452 need_showbreak = FALSE;
3453 /* Correct end of highlighted area for 'showbreak',
3454 * required when 'linebreak' is also set. */
3455 if (tocol == vcol)
3456 tocol += n_extra;
3458 # endif
3460 #endif
3462 if (draw_state == WL_LINE - 1 && n_extra == 0)
3464 draw_state = WL_LINE;
3465 if (saved_n_extra)
3467 /* Continue item from end of wrapped line. */
3468 n_extra = saved_n_extra;
3469 c_extra = saved_c_extra;
3470 p_extra = saved_p_extra;
3471 char_attr = saved_char_attr;
3473 else
3474 char_attr = 0;
3478 /* When still displaying '$' of change command, stop at cursor */
3479 if (dollar_vcol != 0 && wp == curwin
3480 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
3481 #ifdef FEAT_DIFF
3482 && filler_todo <= 0
3483 #endif
3486 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3487 wp->w_p_rl);
3488 /* Pretend we have finished updating the window. Except when
3489 * 'cursorcolumn' is set. */
3490 #ifdef FEAT_SYN_HL
3491 if (wp->w_p_cuc)
3492 row = wp->w_cline_row + wp->w_cline_height;
3493 else
3494 #endif
3495 row = wp->w_height;
3496 break;
3499 if (draw_state == WL_LINE && area_highlighting)
3501 /* handle Visual or match highlighting in this line */
3502 if (vcol == fromcol
3503 #ifdef FEAT_MBYTE
3504 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3505 && (*mb_ptr2cells)(ptr) > 1)
3506 #endif
3507 || ((int)vcol_prev == fromcol_prev
3508 && vcol_prev < vcol /* not at margin */
3509 && vcol < tocol))
3510 area_attr = attr; /* start highlighting */
3511 else if (area_attr != 0
3512 && (vcol == tocol
3513 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
3514 area_attr = 0; /* stop highlighting */
3516 #ifdef FEAT_SEARCH_EXTRA
3517 if (!n_extra)
3520 * Check for start/end of search pattern match.
3521 * After end, check for start/end of next match.
3522 * When another match, have to check for start again.
3523 * Watch out for matching an empty string!
3524 * Do this for 'search_hl' and the match list (ordered by
3525 * priority).
3527 v = (long)(ptr - line);
3528 cur = wp->w_match_head;
3529 shl_flag = FALSE;
3530 while (cur != NULL || shl_flag == FALSE)
3532 if (shl_flag == FALSE
3533 && ((cur != NULL
3534 && cur->priority > SEARCH_HL_PRIORITY)
3535 || cur == NULL))
3537 shl = &search_hl;
3538 shl_flag = TRUE;
3540 else
3541 shl = &cur->hl;
3542 while (shl->rm.regprog != NULL)
3544 if (shl->startcol != MAXCOL
3545 && v >= (long)shl->startcol
3546 && v < (long)shl->endcol)
3548 shl->attr_cur = shl->attr;
3550 else if (v == (long)shl->endcol)
3552 shl->attr_cur = 0;
3554 next_search_hl(wp, shl, lnum, (colnr_T)v);
3556 /* Need to get the line again, a multi-line regexp
3557 * may have made it invalid. */
3558 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3559 ptr = line + v;
3561 if (shl->lnum == lnum)
3563 shl->startcol = shl->rm.startpos[0].col;
3564 if (shl->rm.endpos[0].lnum == 0)
3565 shl->endcol = shl->rm.endpos[0].col;
3566 else
3567 shl->endcol = MAXCOL;
3569 if (shl->startcol == shl->endcol)
3571 /* highlight empty match, try again after
3572 * it */
3573 #ifdef FEAT_MBYTE
3574 if (has_mbyte)
3575 shl->endcol += (*mb_ptr2len)(line
3576 + shl->endcol);
3577 else
3578 #endif
3579 ++shl->endcol;
3582 /* Loop to check if the match starts at the
3583 * current position */
3584 continue;
3587 break;
3589 if (shl != &search_hl && cur != NULL)
3590 cur = cur->next;
3593 /* Use attributes from match with highest priority among
3594 * 'search_hl' and the match list. */
3595 search_attr = search_hl.attr_cur;
3596 cur = wp->w_match_head;
3597 shl_flag = FALSE;
3598 while (cur != NULL || shl_flag == FALSE)
3600 if (shl_flag == FALSE
3601 && ((cur != NULL
3602 && cur->priority > SEARCH_HL_PRIORITY)
3603 || cur == NULL))
3605 shl = &search_hl;
3606 shl_flag = TRUE;
3608 else
3609 shl = &cur->hl;
3610 if (shl->attr_cur != 0)
3611 search_attr = shl->attr_cur;
3612 if (shl != &search_hl && cur != NULL)
3613 cur = cur->next;
3616 #endif
3618 #ifdef FEAT_DIFF
3619 if (diff_hlf != (hlf_T)0)
3621 if (diff_hlf == HLF_CHD && ptr - line >= change_start
3622 && n_extra == 0)
3623 diff_hlf = HLF_TXD; /* changed text */
3624 if (diff_hlf == HLF_TXD && ptr - line > change_end
3625 && n_extra == 0)
3626 diff_hlf = HLF_CHD; /* changed line */
3627 line_attr = hl_attr(diff_hlf);
3629 #endif
3631 /* Decide which of the highlight attributes to use. */
3632 attr_pri = TRUE;
3633 if (area_attr != 0)
3634 char_attr = area_attr;
3635 else if (search_attr != 0)
3636 char_attr = search_attr;
3637 #ifdef LINE_ATTR
3638 /* Use line_attr when not in the Visual or 'incsearch' area
3639 * (area_attr may be 0 when "noinvcur" is set). */
3640 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
3641 || vcol < fromcol || vcol_prev < fromcol_prev
3642 || vcol >= tocol))
3643 char_attr = line_attr;
3644 #endif
3645 else
3647 attr_pri = FALSE;
3648 #ifdef FEAT_SYN_HL
3649 if (has_syntax)
3650 char_attr = syntax_attr;
3651 else
3652 #endif
3653 char_attr = 0;
3658 * Get the next character to put on the screen.
3661 * The "p_extra" points to the extra stuff that is inserted to
3662 * represent special characters (non-printable stuff) and other
3663 * things. When all characters are the same, c_extra is used.
3664 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
3665 * "p_extra[n_extra]".
3666 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
3668 if (n_extra > 0)
3670 if (c_extra != NUL)
3672 c = c_extra;
3673 #ifdef FEAT_MBYTE
3674 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
3675 if (enc_utf8 && (*mb_char2len)(c) > 1)
3677 mb_utf8 = TRUE;
3678 u8cc[0] = 0;
3679 c = 0xc0;
3681 else
3682 mb_utf8 = FALSE;
3683 #endif
3685 else
3687 c = *p_extra;
3688 #ifdef FEAT_MBYTE
3689 if (has_mbyte)
3691 mb_c = c;
3692 if (enc_utf8)
3694 /* If the UTF-8 character is more than one byte:
3695 * Decode it into "mb_c". */
3696 mb_l = (*mb_ptr2len)(p_extra);
3697 mb_utf8 = FALSE;
3698 if (mb_l > n_extra)
3699 mb_l = 1;
3700 else if (mb_l > 1)
3702 mb_c = utfc_ptr2char(p_extra, u8cc);
3703 mb_utf8 = TRUE;
3704 c = 0xc0;
3707 else
3709 /* if this is a DBCS character, put it in "mb_c" */
3710 mb_l = MB_BYTE2LEN(c);
3711 if (mb_l >= n_extra)
3712 mb_l = 1;
3713 else if (mb_l > 1)
3714 mb_c = (c << 8) + p_extra[1];
3716 if (mb_l == 0) /* at the NUL at end-of-line */
3717 mb_l = 1;
3719 /* If a double-width char doesn't fit display a '>' in the
3720 * last column. */
3721 if ((
3722 # ifdef FEAT_RIGHTLEFT
3723 wp->w_p_rl ? (col <= 0) :
3724 # endif
3725 (col >= W_WIDTH(wp) - 1))
3726 && (*mb_char2cells)(mb_c) == 2)
3728 c = '>';
3729 mb_c = c;
3730 mb_l = 1;
3731 mb_utf8 = FALSE;
3732 multi_attr = hl_attr(HLF_AT);
3733 /* put the pointer back to output the double-width
3734 * character at the start of the next line. */
3735 ++n_extra;
3736 --p_extra;
3738 else
3740 n_extra -= mb_l - 1;
3741 p_extra += mb_l - 1;
3744 #endif
3745 ++p_extra;
3747 --n_extra;
3749 else
3752 * Get a character from the line itself.
3754 c = *ptr;
3755 #ifdef FEAT_MBYTE
3756 if (has_mbyte)
3758 mb_c = c;
3759 if (enc_utf8)
3761 /* If the UTF-8 character is more than one byte: Decode it
3762 * into "mb_c". */
3763 mb_l = (*mb_ptr2len)(ptr);
3764 mb_utf8 = FALSE;
3765 if (mb_l > 1)
3767 mb_c = utfc_ptr2char(ptr, u8cc);
3768 /* Overlong encoded ASCII or ASCII with composing char
3769 * is displayed normally, except a NUL. */
3770 if (mb_c < 0x80)
3771 c = mb_c;
3772 mb_utf8 = TRUE;
3774 /* At start of the line we can have a composing char.
3775 * Draw it as a space with a composing char. */
3776 if (utf_iscomposing(mb_c))
3778 int i;
3780 for (i = Screen_mco - 1; i > 0; --i)
3781 u8cc[i] = u8cc[i - 1];
3782 u8cc[0] = mb_c;
3783 mb_c = ' ';
3787 if ((mb_l == 1 && c >= 0x80)
3788 || (mb_l >= 1 && mb_c == 0)
3789 || (mb_l > 1 && (!vim_isprintc(mb_c)
3790 # ifdef UNICODE16
3791 || mb_c >= 0x10000
3792 # endif
3796 * Illegal UTF-8 byte: display as <xx>.
3797 * Non-BMP character : display as ? or fullwidth ?.
3799 # ifdef UNICODE16
3800 if (mb_c < 0x10000)
3801 # endif
3803 transchar_hex(extra, mb_c);
3804 # ifdef FEAT_RIGHTLEFT
3805 if (wp->w_p_rl) /* reverse */
3806 rl_mirror(extra);
3807 # endif
3809 # ifdef UNICODE16
3810 else if (utf_char2cells(mb_c) != 2)
3811 STRCPY(extra, "?");
3812 else
3813 /* 0xff1f in UTF-8: full-width '?' */
3814 STRCPY(extra, "\357\274\237");
3815 # endif
3817 p_extra = extra;
3818 c = *p_extra;
3819 mb_c = mb_ptr2char_adv(&p_extra);
3820 mb_utf8 = (c >= 0x80);
3821 n_extra = (int)STRLEN(p_extra);
3822 c_extra = NUL;
3823 if (area_attr == 0 && search_attr == 0)
3825 n_attr = n_extra + 1;
3826 extra_attr = hl_attr(HLF_8);
3827 saved_attr2 = char_attr; /* save current attr */
3830 else if (mb_l == 0) /* at the NUL at end-of-line */
3831 mb_l = 1;
3832 #ifdef FEAT_ARABIC
3833 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
3835 /* Do Arabic shaping. */
3836 int pc, pc1, nc;
3837 int pcc[MAX_MCO];
3839 /* The idea of what is the previous and next
3840 * character depends on 'rightleft'. */
3841 if (wp->w_p_rl)
3843 pc = prev_c;
3844 pc1 = prev_c1;
3845 nc = utf_ptr2char(ptr + mb_l);
3846 prev_c1 = u8cc[0];
3848 else
3850 pc = utfc_ptr2char(ptr + mb_l, pcc);
3851 nc = prev_c;
3852 pc1 = pcc[0];
3854 prev_c = mb_c;
3856 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
3858 else
3859 prev_c = mb_c;
3860 #endif
3862 else /* enc_dbcs */
3864 mb_l = MB_BYTE2LEN(c);
3865 if (mb_l == 0) /* at the NUL at end-of-line */
3866 mb_l = 1;
3867 else if (mb_l > 1)
3869 /* We assume a second byte below 32 is illegal.
3870 * Hopefully this is OK for all double-byte encodings!
3872 if (ptr[1] >= 32)
3873 mb_c = (c << 8) + ptr[1];
3874 else
3876 if (ptr[1] == NUL)
3878 /* head byte at end of line */
3879 mb_l = 1;
3880 transchar_nonprint(extra, c);
3882 else
3884 /* illegal tail byte */
3885 mb_l = 2;
3886 STRCPY(extra, "XX");
3888 p_extra = extra;
3889 n_extra = (int)STRLEN(extra) - 1;
3890 c_extra = NUL;
3891 c = *p_extra++;
3892 if (area_attr == 0 && search_attr == 0)
3894 n_attr = n_extra + 1;
3895 extra_attr = hl_attr(HLF_8);
3896 saved_attr2 = char_attr; /* save current attr */
3898 mb_c = c;
3902 /* If a double-width char doesn't fit display a '>' in the
3903 * last column; the character is displayed at the start of the
3904 * next line. */
3905 if ((
3906 # ifdef FEAT_RIGHTLEFT
3907 wp->w_p_rl ? (col <= 0) :
3908 # endif
3909 (col >= W_WIDTH(wp) - 1))
3910 && (*mb_char2cells)(mb_c) == 2)
3912 c = '>';
3913 mb_c = c;
3914 mb_utf8 = FALSE;
3915 mb_l = 1;
3916 multi_attr = hl_attr(HLF_AT);
3917 /* Put pointer back so that the character will be
3918 * displayed at the start of the next line. */
3919 --ptr;
3921 else if (*ptr != NUL)
3922 ptr += mb_l - 1;
3924 /* If a double-width char doesn't fit at the left side display
3925 * a '<' in the first column. */
3926 if (n_skip > 0 && mb_l > 1)
3928 n_extra = 1;
3929 c_extra = '<';
3930 c = ' ';
3931 if (area_attr == 0 && search_attr == 0)
3933 n_attr = n_extra + 1;
3934 extra_attr = hl_attr(HLF_AT);
3935 saved_attr2 = char_attr; /* save current attr */
3937 mb_c = c;
3938 mb_utf8 = FALSE;
3939 mb_l = 1;
3943 #endif
3944 ++ptr;
3946 /* 'list' : change char 160 to lcs_nbsp. */
3947 if (wp->w_p_list && (c == 160
3948 #ifdef FEAT_MBYTE
3949 || (mb_utf8 && mb_c == 160)
3950 #endif
3951 ) && lcs_nbsp)
3953 c = lcs_nbsp;
3954 if (area_attr == 0 && search_attr == 0)
3956 n_attr = 1;
3957 extra_attr = hl_attr(HLF_8);
3958 saved_attr2 = char_attr; /* save current attr */
3960 #ifdef FEAT_MBYTE
3961 mb_c = c;
3962 if (enc_utf8 && (*mb_char2len)(c) > 1)
3964 mb_utf8 = TRUE;
3965 u8cc[0] = 0;
3966 c = 0xc0;
3968 else
3969 mb_utf8 = FALSE;
3970 #endif
3973 if (extra_check)
3975 #ifdef FEAT_SPELL
3976 int can_spell = TRUE;
3977 #endif
3979 #ifdef FEAT_SYN_HL
3980 /* Get syntax attribute, unless still at the start of the line
3981 * (double-wide char that doesn't fit). */
3982 v = (long)(ptr - line);
3983 if (has_syntax && v > 0)
3985 /* Get the syntax attribute for the character. If there
3986 * is an error, disable syntax highlighting. */
3987 save_did_emsg = did_emsg;
3988 did_emsg = FALSE;
3990 syntax_attr = get_syntax_attr((colnr_T)v - 1,
3991 # ifdef FEAT_SPELL
3992 has_spell ? &can_spell :
3993 # endif
3994 NULL, FALSE);
3996 if (did_emsg)
3998 wp->w_buffer->b_syn_error = TRUE;
3999 has_syntax = FALSE;
4001 else
4002 did_emsg = save_did_emsg;
4004 /* Need to get the line again, a multi-line regexp may
4005 * have made it invalid. */
4006 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4007 ptr = line + v;
4009 if (!attr_pri)
4010 char_attr = syntax_attr;
4011 else
4012 char_attr = hl_combine_attr(syntax_attr, char_attr);
4014 #endif
4016 #ifdef FEAT_SPELL
4017 /* Check spelling (unless at the end of the line).
4018 * Only do this when there is no syntax highlighting, the
4019 * @Spell cluster is not used or the current syntax item
4020 * contains the @Spell cluster. */
4021 if (has_spell && v >= word_end && v > cur_checked_col)
4023 spell_attr = 0;
4024 # ifdef FEAT_SYN_HL
4025 if (!attr_pri)
4026 char_attr = syntax_attr;
4027 # endif
4028 if (c != 0 && (
4029 # ifdef FEAT_SYN_HL
4030 !has_syntax ||
4031 # endif
4032 can_spell))
4034 char_u *prev_ptr, *p;
4035 int len;
4036 hlf_T spell_hlf = HLF_COUNT;
4037 # ifdef FEAT_MBYTE
4038 if (has_mbyte)
4040 prev_ptr = ptr - mb_l;
4041 v -= mb_l - 1;
4043 else
4044 # endif
4045 prev_ptr = ptr - 1;
4047 /* Use nextline[] if possible, it has the start of the
4048 * next line concatenated. */
4049 if ((prev_ptr - line) - nextlinecol >= 0)
4050 p = nextline + (prev_ptr - line) - nextlinecol;
4051 else
4052 p = prev_ptr;
4053 cap_col -= (int)(prev_ptr - line);
4054 len = spell_check(wp, p, &spell_hlf, &cap_col,
4055 nochange);
4056 word_end = v + len;
4058 /* In Insert mode only highlight a word that
4059 * doesn't touch the cursor. */
4060 if (spell_hlf != HLF_COUNT
4061 && (State & INSERT) != 0
4062 && wp->w_cursor.lnum == lnum
4063 && wp->w_cursor.col >=
4064 (colnr_T)(prev_ptr - line)
4065 && wp->w_cursor.col < (colnr_T)word_end)
4067 spell_hlf = HLF_COUNT;
4068 spell_redraw_lnum = lnum;
4071 if (spell_hlf == HLF_COUNT && p != prev_ptr
4072 && (p - nextline) + len > nextline_idx)
4074 /* Remember that the good word continues at the
4075 * start of the next line. */
4076 checked_lnum = lnum + 1;
4077 checked_col = (int)((p - nextline) + len - nextline_idx);
4080 /* Turn index into actual attributes. */
4081 if (spell_hlf != HLF_COUNT)
4082 spell_attr = highlight_attr[spell_hlf];
4084 if (cap_col > 0)
4086 if (p != prev_ptr
4087 && (p - nextline) + cap_col >= nextline_idx)
4089 /* Remember that the word in the next line
4090 * must start with a capital. */
4091 capcol_lnum = lnum + 1;
4092 cap_col = (int)((p - nextline) + cap_col
4093 - nextline_idx);
4095 else
4096 /* Compute the actual column. */
4097 cap_col += (int)(prev_ptr - line);
4101 if (spell_attr != 0)
4103 if (!attr_pri)
4104 char_attr = hl_combine_attr(char_attr, spell_attr);
4105 else
4106 char_attr = hl_combine_attr(spell_attr, char_attr);
4108 #endif
4109 #ifdef FEAT_LINEBREAK
4111 * Found last space before word: check for line break.
4113 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr)
4114 && !wp->w_p_list)
4116 n_extra = win_lbr_chartabsize(wp, ptr - (
4117 # ifdef FEAT_MBYTE
4118 has_mbyte ? mb_l :
4119 # endif
4120 1), (colnr_T)vcol, NULL, lnum) - 1;
4121 c_extra = ' ';
4122 if (vim_iswhite(c))
4123 c = ' ';
4125 #endif
4127 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4129 c = lcs_trail;
4130 if (!attr_pri)
4132 n_attr = 1;
4133 extra_attr = hl_attr(HLF_8);
4134 saved_attr2 = char_attr; /* save current attr */
4136 #ifdef FEAT_MBYTE
4137 mb_c = c;
4138 if (enc_utf8 && (*mb_char2len)(c) > 1)
4140 mb_utf8 = TRUE;
4141 u8cc[0] = 0;
4142 c = 0xc0;
4144 else
4145 mb_utf8 = FALSE;
4146 #endif
4151 * Handling of non-printable characters.
4153 if (!(chartab[c & 0xff] & CT_PRINT_CHAR))
4156 * when getting a character from the file, we may have to
4157 * turn it into something else on the way to putting it
4158 * into "ScreenLines".
4160 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4162 /* tab amount depends on current column */
4163 #ifdef FEAT_VARTABS
4164 n_extra = tabstop_padding(vcol, wp->w_buffer->b_p_ts,
4165 wp->w_buffer->b_p_vts_ary) - 1;
4166 #else
4167 n_extra = (int)wp->w_buffer->b_p_ts
4168 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4169 #endif
4170 #ifdef FEAT_MBYTE
4171 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4172 #endif
4173 if (wp->w_p_list)
4175 c = lcs_tab1;
4176 c_extra = lcs_tab2;
4177 n_attr = n_extra + 1;
4178 extra_attr = hl_attr(HLF_8);
4179 saved_attr2 = char_attr; /* save current attr */
4180 #ifdef FEAT_MBYTE
4181 mb_c = c;
4182 if (enc_utf8 && (*mb_char2len)(c) > 1)
4184 mb_utf8 = TRUE;
4185 u8cc[0] = 0;
4186 c = 0xc0;
4188 #endif
4190 else
4192 c_extra = ' ';
4193 c = ' ';
4196 else if (c == NUL
4197 && ((wp->w_p_list && lcs_eol > 0)
4198 || ((fromcol >= 0 || fromcol_prev >= 0)
4199 && tocol > vcol
4200 #ifdef FEAT_VISUAL
4201 && VIsual_mode != Ctrl_V
4202 #endif
4203 && (
4204 # ifdef FEAT_RIGHTLEFT
4205 wp->w_p_rl ? (col >= 0) :
4206 # endif
4207 (col < W_WIDTH(wp)))
4208 && !(noinvcur
4209 && lnum == wp->w_cursor.lnum
4210 && (colnr_T)vcol == wp->w_virtcol)))
4211 && lcs_eol_one >= 0)
4213 /* Display a '$' after the line or highlight an extra
4214 * character if the line break is included. */
4215 #if defined(FEAT_DIFF) || defined(LINE_ATTR)
4216 /* For a diff line the highlighting continues after the
4217 * "$". */
4218 if (
4219 # ifdef FEAT_DIFF
4220 diff_hlf == (hlf_T)0
4221 # ifdef LINE_ATTR
4223 # endif
4224 # endif
4225 # ifdef LINE_ATTR
4226 line_attr == 0
4227 # endif
4229 #endif
4231 #ifdef FEAT_VIRTUALEDIT
4232 /* In virtualedit, visual selections may extend
4233 * beyond end of line. */
4234 if (area_highlighting && virtual_active()
4235 && tocol != MAXCOL && vcol < tocol)
4236 n_extra = 0;
4237 else
4238 #endif
4240 p_extra = at_end_str;
4241 n_extra = 1;
4242 c_extra = NUL;
4245 if (wp->w_p_list)
4246 c = lcs_eol;
4247 else
4248 c = ' ';
4249 lcs_eol_one = -1;
4250 --ptr; /* put it back at the NUL */
4251 if (!attr_pri)
4253 extra_attr = hl_attr(HLF_AT);
4254 n_attr = 1;
4256 #ifdef FEAT_MBYTE
4257 mb_c = c;
4258 if (enc_utf8 && (*mb_char2len)(c) > 1)
4260 mb_utf8 = TRUE;
4261 u8cc[0] = 0;
4262 c = 0xc0;
4264 else
4265 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4266 #endif
4268 else if (c != NUL)
4270 p_extra = transchar(c);
4271 #ifdef FEAT_RIGHTLEFT
4272 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4273 rl_mirror(p_extra); /* reverse "<12>" */
4274 #endif
4275 n_extra = byte2cells(c) - 1;
4276 c_extra = NUL;
4277 c = *p_extra++;
4278 if (!attr_pri)
4280 n_attr = n_extra + 1;
4281 extra_attr = hl_attr(HLF_8);
4282 saved_attr2 = char_attr; /* save current attr */
4284 #ifdef FEAT_MBYTE
4285 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4286 #endif
4288 #ifdef FEAT_VIRTUALEDIT
4289 else if (VIsual_active
4290 && (VIsual_mode == Ctrl_V
4291 || VIsual_mode == 'v')
4292 && virtual_active()
4293 && tocol != MAXCOL
4294 && vcol < tocol
4295 && (
4296 # ifdef FEAT_RIGHTLEFT
4297 wp->w_p_rl ? (col >= 0) :
4298 # endif
4299 (col < W_WIDTH(wp))))
4301 c = ' ';
4302 --ptr; /* put it back at the NUL */
4304 #endif
4305 #if defined(LINE_ATTR)
4306 else if ((
4307 # ifdef FEAT_DIFF
4308 diff_hlf != (hlf_T)0 ||
4309 # endif
4310 line_attr != 0
4311 ) && (
4312 # ifdef FEAT_RIGHTLEFT
4313 wp->w_p_rl ? (col >= 0) :
4314 # endif
4315 (col < W_WIDTH(wp))))
4317 /* Highlight until the right side of the window */
4318 c = ' ';
4319 --ptr; /* put it back at the NUL */
4321 /* Remember we do the char for line highlighting. */
4322 ++did_line_attr;
4324 /* don't do search HL for the rest of the line */
4325 if (line_attr != 0 && char_attr == search_attr && col > 0)
4326 char_attr = line_attr;
4327 # ifdef FEAT_DIFF
4328 if (diff_hlf == HLF_TXD)
4330 diff_hlf = HLF_CHD;
4331 if (attr == 0 || char_attr != attr)
4332 char_attr = hl_attr(diff_hlf);
4334 # endif
4336 #endif
4340 /* Don't override visual selection highlighting. */
4341 if (n_attr > 0
4342 && draw_state == WL_LINE
4343 && !attr_pri)
4344 char_attr = extra_attr;
4346 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
4347 /* XIM don't send preedit_start and preedit_end, but they send
4348 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
4349 * im_is_preediting() here. */
4350 if (xic != NULL
4351 && lnum == wp->w_cursor.lnum
4352 && (State & INSERT)
4353 && !p_imdisable
4354 && im_is_preediting()
4355 && draw_state == WL_LINE)
4357 colnr_T tcol;
4359 if (preedit_end_col == MAXCOL)
4360 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
4361 else
4362 tcol = preedit_end_col;
4363 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
4365 if (feedback_old_attr < 0)
4367 feedback_col = 0;
4368 feedback_old_attr = char_attr;
4370 char_attr = im_get_feedback_attr(feedback_col);
4371 if (char_attr < 0)
4372 char_attr = feedback_old_attr;
4373 feedback_col++;
4375 else if (feedback_old_attr >= 0)
4377 char_attr = feedback_old_attr;
4378 feedback_old_attr = -1;
4379 feedback_col = 0;
4382 #endif
4384 * Handle the case where we are in column 0 but not on the first
4385 * character of the line and the user wants us to show us a
4386 * special character (via 'listchars' option "precedes:<char>".
4388 if (lcs_prec_todo != NUL
4389 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
4390 #ifdef FEAT_DIFF
4391 && filler_todo <= 0
4392 #endif
4393 && draw_state > WL_NR
4394 && c != NUL)
4396 c = lcs_prec;
4397 lcs_prec_todo = NUL;
4398 #ifdef FEAT_MBYTE
4399 mb_c = c;
4400 if (enc_utf8 && (*mb_char2len)(c) > 1)
4402 mb_utf8 = TRUE;
4403 u8cc[0] = 0;
4404 c = 0xc0;
4406 else
4407 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4408 #endif
4409 if (!attr_pri)
4411 saved_attr3 = char_attr; /* save current attr */
4412 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
4413 n_attr3 = 1;
4418 * At end of the text line or just after the last character.
4420 if (c == NUL
4421 #if defined(LINE_ATTR)
4422 || did_line_attr == 1
4423 #endif
4426 #ifdef FEAT_SEARCH_EXTRA
4427 long prevcol = (long)(ptr - line) - (c == NUL);
4429 /* we're not really at that column when skipping some text */
4430 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
4431 ++prevcol;
4432 #endif
4434 /* invert at least one char, used for Visual and empty line or
4435 * highlight match at end of line. If it's beyond the last
4436 * char on the screen, just overwrite that one (tricky!) Not
4437 * needed when a '$' was displayed for 'list'. */
4438 #ifdef FEAT_SEARCH_EXTRA
4439 prevcol_hl_flag = FALSE;
4440 if (prevcol == (long)search_hl.startcol)
4441 prevcol_hl_flag = TRUE;
4442 else
4444 cur = wp->w_match_head;
4445 while (cur != NULL)
4447 if (prevcol == (long)cur->hl.startcol)
4449 prevcol_hl_flag = TRUE;
4450 break;
4452 cur = cur->next;
4455 #endif
4456 if (lcs_eol == lcs_eol_one
4457 && ((area_attr != 0 && vcol == fromcol
4458 #ifdef FEAT_VISUAL
4459 && (VIsual_mode != Ctrl_V
4460 || lnum == VIsual.lnum
4461 || lnum == curwin->w_cursor.lnum)
4462 #endif
4463 && c == NUL)
4464 #ifdef FEAT_SEARCH_EXTRA
4465 /* highlight 'hlsearch' match at end of line */
4466 || (prevcol_hl_flag == TRUE
4467 # if defined(LINE_ATTR)
4468 && did_line_attr <= 1
4469 # endif
4471 #endif
4474 int n = 0;
4476 #ifdef FEAT_RIGHTLEFT
4477 if (wp->w_p_rl)
4479 if (col < 0)
4480 n = 1;
4482 else
4483 #endif
4485 if (col >= W_WIDTH(wp))
4486 n = -1;
4488 if (n != 0)
4490 /* At the window boundary, highlight the last character
4491 * instead (better than nothing). */
4492 off += n;
4493 col += n;
4495 else
4497 /* Add a blank character to highlight. */
4498 ScreenLines[off] = ' ';
4499 #ifdef FEAT_MBYTE
4500 if (enc_utf8)
4501 ScreenLinesUC[off] = 0;
4502 #endif
4504 #ifdef FEAT_SEARCH_EXTRA
4505 if (area_attr == 0)
4507 /* Use attributes from match with highest priority among
4508 * 'search_hl' and the match list. */
4509 char_attr = search_hl.attr;
4510 cur = wp->w_match_head;
4511 shl_flag = FALSE;
4512 while (cur != NULL || shl_flag == FALSE)
4514 if (shl_flag == FALSE
4515 && ((cur != NULL
4516 && cur->priority > SEARCH_HL_PRIORITY)
4517 || cur == NULL))
4519 shl = &search_hl;
4520 shl_flag = TRUE;
4522 else
4523 shl = &cur->hl;
4524 if ((ptr - line) - 1 == (long)shl->startcol)
4525 char_attr = shl->attr;
4526 if (shl != &search_hl && cur != NULL)
4527 cur = cur->next;
4530 #endif
4531 ScreenAttrs[off] = char_attr;
4532 #ifdef FEAT_RIGHTLEFT
4533 if (wp->w_p_rl)
4535 --col;
4536 --off;
4538 else
4539 #endif
4541 ++col;
4542 ++off;
4544 ++vcol;
4545 #ifdef FEAT_SYN_HL
4546 eol_hl_off = 1;
4547 #endif
4552 * At end of the text line.
4554 if (c == NUL)
4556 #ifdef FEAT_SYN_HL
4557 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
4558 && lnum == wp->w_cursor.lnum)
4560 /* highlight last char after line */
4561 --col;
4562 --off;
4563 --vcol;
4566 /* Highlight 'cursorcolumn' past end of the line. */
4567 if (wp->w_p_wrap)
4568 v = wp->w_skipcol;
4569 else
4570 v = wp->w_leftcol;
4571 /* check if line ends before left margin */
4572 if (vcol < v + col - win_col_off(wp))
4574 vcol = v + col - win_col_off(wp);
4575 if (wp->w_p_cuc
4576 && (int)wp->w_virtcol >= vcol - eol_hl_off
4577 && (int)wp->w_virtcol < W_WIDTH(wp) * (row - startrow + 1)
4579 && lnum != wp->w_cursor.lnum
4580 # ifdef FEAT_RIGHTLEFT
4581 && !wp->w_p_rl
4582 # endif
4585 while (col < W_WIDTH(wp))
4587 ScreenLines[off] = ' ';
4588 #ifdef FEAT_MBYTE
4589 if (enc_utf8)
4590 ScreenLinesUC[off] = 0;
4591 #endif
4592 ++col;
4593 if (vcol == (long)wp->w_virtcol)
4595 ScreenAttrs[off] = hl_attr(HLF_CUC);
4596 break;
4598 ScreenAttrs[off++] = 0;
4599 ++vcol;
4602 #endif
4604 SCREEN_LINE(screen_row, W_WINCOL(wp), col, (int)W_WIDTH(wp),
4605 wp->w_p_rl);
4606 row++;
4609 * Update w_cline_height and w_cline_folded if the cursor line was
4610 * updated (saves a call to plines() later).
4612 if (wp == curwin && lnum == curwin->w_cursor.lnum)
4614 curwin->w_cline_row = startrow;
4615 curwin->w_cline_height = row - startrow;
4616 #ifdef FEAT_FOLDING
4617 curwin->w_cline_folded = FALSE;
4618 #endif
4619 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
4622 break;
4625 /* line continues beyond line end */
4626 if (lcs_ext
4627 && !wp->w_p_wrap
4628 #ifdef FEAT_DIFF
4629 && filler_todo <= 0
4630 #endif
4631 && (
4632 #ifdef FEAT_RIGHTLEFT
4633 wp->w_p_rl ? col == 0 :
4634 #endif
4635 col == W_WIDTH(wp) - 1)
4636 && (*ptr != NUL
4637 || (wp->w_p_list && lcs_eol_one > 0)
4638 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
4640 c = lcs_ext;
4641 char_attr = hl_attr(HLF_AT);
4642 #ifdef FEAT_MBYTE
4643 mb_c = c;
4644 if (enc_utf8 && (*mb_char2len)(c) > 1)
4646 mb_utf8 = TRUE;
4647 u8cc[0] = 0;
4648 c = 0xc0;
4650 else
4651 mb_utf8 = FALSE;
4652 #endif
4655 #ifdef FEAT_SYN_HL
4656 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
4657 * highlight the cursor position itself. */
4658 if (wp->w_p_cuc && vcol == (long)wp->w_virtcol
4659 && lnum != wp->w_cursor.lnum
4660 && draw_state == WL_LINE
4661 && !lnum_in_visual_area)
4663 vcol_save_attr = char_attr;
4664 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
4666 else
4667 vcol_save_attr = -1;
4668 #endif
4671 * Store character to be displayed.
4672 * Skip characters that are left of the screen for 'nowrap'.
4674 vcol_prev = vcol;
4675 if (draw_state < WL_LINE || n_skip <= 0)
4678 * Store the character.
4680 #if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
4681 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
4683 /* A double-wide character is: put first halve in left cell. */
4684 --off;
4685 --col;
4687 #endif
4688 ScreenLines[off] = c;
4689 #ifdef FEAT_MBYTE
4690 if (enc_dbcs == DBCS_JPNU)
4692 if ((mb_c & 0xff00) == 0x8e00)
4693 ScreenLines[off] = 0x8e;
4694 ScreenLines2[off] = mb_c & 0xff;
4696 else if (enc_utf8)
4698 if (mb_utf8)
4700 int i;
4702 ScreenLinesUC[off] = mb_c;
4703 if ((c & 0xff) == 0)
4704 ScreenLines[off] = 0x80; /* avoid storing zero */
4705 for (i = 0; i < Screen_mco; ++i)
4707 ScreenLinesC[i][off] = u8cc[i];
4708 if (u8cc[i] == 0)
4709 break;
4712 else
4713 ScreenLinesUC[off] = 0;
4715 if (multi_attr)
4717 ScreenAttrs[off] = multi_attr;
4718 multi_attr = 0;
4720 else
4721 #endif
4722 ScreenAttrs[off] = char_attr;
4724 #ifdef FEAT_MBYTE
4725 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
4727 /* Need to fill two screen columns. */
4728 ++off;
4729 ++col;
4730 if (enc_utf8)
4731 /* UTF-8: Put a 0 in the second screen char. */
4732 ScreenLines[off] = 0;
4733 else
4734 /* DBCS: Put second byte in the second screen char. */
4735 ScreenLines[off] = mb_c & 0xff;
4736 ++vcol;
4737 /* When "tocol" is halfway a character, set it to the end of
4738 * the character, otherwise highlighting won't stop. */
4739 if (tocol == vcol)
4740 ++tocol;
4741 #ifdef FEAT_RIGHTLEFT
4742 if (wp->w_p_rl)
4744 /* now it's time to backup one cell */
4745 --off;
4746 --col;
4748 #endif
4750 #endif
4751 #ifdef FEAT_RIGHTLEFT
4752 if (wp->w_p_rl)
4754 --off;
4755 --col;
4757 else
4758 #endif
4760 ++off;
4761 ++col;
4764 else
4765 --n_skip;
4767 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
4768 * column. */
4769 if (draw_state > WL_NR
4770 #ifdef FEAT_DIFF
4771 && filler_todo <= 0
4772 #endif
4774 ++vcol;
4776 #ifdef FEAT_SYN_HL
4777 if (vcol_save_attr >= 0)
4778 char_attr = vcol_save_attr;
4779 #endif
4781 /* restore attributes after "predeces" in 'listchars' */
4782 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
4783 char_attr = saved_attr3;
4785 /* restore attributes after last 'listchars' or 'number' char */
4786 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
4787 char_attr = saved_attr2;
4790 * At end of screen line and there is more to come: Display the line
4791 * so far. If there is no more to display it is caught above.
4793 if ((
4794 #ifdef FEAT_RIGHTLEFT
4795 wp->w_p_rl ? (col < 0) :
4796 #endif
4797 (col >= W_WIDTH(wp)))
4798 && (*ptr != NUL
4799 #ifdef FEAT_DIFF
4800 || filler_todo > 0
4801 #endif
4802 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
4803 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
4806 SCREEN_LINE(screen_row, W_WINCOL(wp), col, (int)W_WIDTH(wp),
4807 wp->w_p_rl);
4808 ++row;
4809 ++screen_row;
4811 /* When not wrapping and finished diff lines, or when displayed
4812 * '$' and highlighting until last column, break here. */
4813 if ((!wp->w_p_wrap
4814 #ifdef FEAT_DIFF
4815 && filler_todo <= 0
4816 #endif
4817 ) || lcs_eol_one == -1)
4818 break;
4820 /* When the window is too narrow draw all "@" lines. */
4821 if (draw_state != WL_LINE
4822 #ifdef FEAT_DIFF
4823 && filler_todo <= 0
4824 #endif
4827 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
4828 #ifdef FEAT_VERTSPLIT
4829 draw_vsep_win(wp, row);
4830 #endif
4831 row = endrow;
4834 /* When line got too long for screen break here. */
4835 if (row == endrow)
4837 ++row;
4838 break;
4841 if (screen_cur_row == screen_row - 1
4842 #ifdef FEAT_DIFF
4843 && filler_todo <= 0
4844 #endif
4845 && W_WIDTH(wp) == Columns)
4847 /* Remember that the line wraps, used for modeless copy. */
4848 LineWraps[screen_row - 1] = TRUE;
4851 * Special trick to make copy/paste of wrapped lines work with
4852 * xterm/screen: write an extra character beyond the end of
4853 * the line. This will work with all terminal types
4854 * (regardless of the xn,am settings).
4855 * Only do this on a fast tty.
4856 * Only do this if the cursor is on the current line
4857 * (something has been written in it).
4858 * Don't do this for the GUI.
4859 * Don't do this for double-width characters.
4860 * Don't do this for a window not at the right screen border.
4862 if (p_tf
4863 #ifdef FEAT_GUI
4864 && !gui.in_use
4865 #endif
4866 #ifdef FEAT_MBYTE
4867 && !(has_mbyte
4868 && ((*mb_off2cells)(LineOffset[screen_row],
4869 LineOffset[screen_row] + screen_Columns)
4870 == 2
4871 || (*mb_off2cells)(LineOffset[screen_row - 1]
4872 + (int)Columns - 2,
4873 LineOffset[screen_row] + screen_Columns)
4874 == 2))
4875 #endif
4878 /* First make sure we are at the end of the screen line,
4879 * then output the same character again to let the
4880 * terminal know about the wrap. If the terminal doesn't
4881 * auto-wrap, we overwrite the character. */
4882 if (screen_cur_col != W_WIDTH(wp))
4883 screen_char(LineOffset[screen_row - 1]
4884 + (unsigned)Columns - 1,
4885 screen_row - 1, (int)(Columns - 1));
4887 #ifdef FEAT_MBYTE
4888 /* When there is a multi-byte character, just output a
4889 * space to keep it simple. */
4890 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
4891 screen_row - 1] + (Columns - 1)]) > 1)
4892 out_char(' ');
4893 else
4894 #endif
4895 out_char(ScreenLines[LineOffset[screen_row - 1]
4896 + (Columns - 1)]);
4897 /* force a redraw of the first char on the next line */
4898 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
4899 screen_start(); /* don't know where cursor is now */
4903 col = 0;
4904 off = (unsigned)(current_ScreenLine - ScreenLines);
4905 #ifdef FEAT_RIGHTLEFT
4906 if (wp->w_p_rl)
4908 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
4909 off += col;
4911 #endif
4913 /* reset the drawing state for the start of a wrapped line */
4914 draw_state = WL_START;
4915 saved_n_extra = n_extra;
4916 saved_p_extra = p_extra;
4917 saved_c_extra = c_extra;
4918 saved_char_attr = char_attr;
4919 n_extra = 0;
4920 lcs_prec_todo = lcs_prec;
4921 #ifdef FEAT_LINEBREAK
4922 # ifdef FEAT_DIFF
4923 if (filler_todo <= 0)
4924 # endif
4925 need_showbreak = TRUE;
4926 #endif
4927 #ifdef FEAT_DIFF
4928 --filler_todo;
4929 /* When the filler lines are actually below the last line of the
4930 * file, don't draw the line itself, break here. */
4931 if (filler_todo == 0 && wp->w_botfill)
4932 break;
4933 #endif
4936 } /* for every character in the line */
4938 #ifdef FEAT_SPELL
4939 /* After an empty line check first word for capital. */
4940 if (*skipwhite(line) == NUL)
4942 capcol_lnum = lnum + 1;
4943 cap_col = 0;
4945 #endif
4947 return row;
4950 #ifdef FEAT_MBYTE
4951 static int comp_char_differs __ARGS((int, int));
4954 * Return if the composing characters at "off_from" and "off_to" differ.
4955 * Only to be used when ScreenLinesUC[off_from] != 0.
4957 static int
4958 comp_char_differs(off_from, off_to)
4959 int off_from;
4960 int off_to;
4962 int i;
4964 for (i = 0; i < Screen_mco; ++i)
4966 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
4967 return TRUE;
4968 if (ScreenLinesC[i][off_from] == 0)
4969 break;
4971 return FALSE;
4973 #endif
4976 * Check whether the given character needs redrawing:
4977 * - the (first byte of the) character is different
4978 * - the attributes are different
4979 * - the character is multi-byte and the next byte is different
4980 * - the character is two cells wide and the second cell differs.
4982 static int
4983 char_needs_redraw(off_from, off_to, cols)
4984 int off_from;
4985 int off_to;
4986 int cols;
4988 if (cols > 0
4989 && ((ScreenLines[off_from] != ScreenLines[off_to]
4990 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
4992 #ifdef FEAT_MBYTE
4993 || (enc_dbcs != 0
4994 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
4995 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
4996 ? ScreenLines2[off_from] != ScreenLines2[off_to]
4997 : (cols > 1 && ScreenLines[off_from + 1]
4998 != ScreenLines[off_to + 1])))
4999 || (enc_utf8
5000 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
5001 || (ScreenLinesUC[off_from] != 0
5002 && comp_char_differs(off_from, off_to))
5003 || (cols > 1 && ScreenLines[off_from + 1]
5004 != ScreenLines[off_to + 1])))
5005 #endif
5007 return TRUE;
5008 return FALSE;
5012 * Move one "cooked" screen line to the screen, but only the characters that
5013 * have actually changed. Handle insert/delete character.
5014 * "coloff" gives the first column on the screen for this line.
5015 * "endcol" gives the columns where valid characters are.
5016 * "clear_width" is the width of the window. It's > 0 if the rest of the line
5017 * needs to be cleared, negative otherwise.
5018 * "rlflag" is TRUE in a rightleft window:
5019 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
5020 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
5022 static void
5023 screen_line(row, coloff, endcol, clear_width
5024 #ifdef FEAT_RIGHTLEFT
5025 , rlflag
5026 #endif
5028 int row;
5029 int coloff;
5030 int endcol;
5031 int clear_width;
5032 #ifdef FEAT_RIGHTLEFT
5033 int rlflag;
5034 #endif
5036 unsigned off_from;
5037 unsigned off_to;
5038 #ifdef FEAT_MBYTE
5039 unsigned max_off_from;
5040 unsigned max_off_to;
5041 #endif
5042 int col = 0;
5043 #if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_VERTSPLIT)
5044 int hl;
5045 #endif
5046 int force = FALSE; /* force update rest of the line */
5047 int redraw_this /* bool: does character need redraw? */
5048 #ifdef FEAT_GUI
5049 = TRUE /* For GUI when while-loop empty */
5050 #endif
5052 int redraw_next; /* redraw_this for next character */
5053 #ifdef FEAT_MBYTE
5054 int clear_next = FALSE;
5055 int char_cells; /* 1: normal char */
5056 /* 2: occupies two display cells */
5057 # define CHAR_CELLS char_cells
5058 #else
5059 # define CHAR_CELLS 1
5060 #endif
5062 # ifdef FEAT_CLIPBOARD
5063 clip_may_clear_selection(row, row);
5064 # endif
5066 off_from = (unsigned)(current_ScreenLine - ScreenLines);
5067 off_to = LineOffset[row] + coloff;
5068 #ifdef FEAT_MBYTE
5069 max_off_from = off_from + screen_Columns;
5070 max_off_to = LineOffset[row] + screen_Columns;
5071 #endif
5073 #ifdef FEAT_RIGHTLEFT
5074 if (rlflag)
5076 /* Clear rest first, because it's left of the text. */
5077 if (clear_width > 0)
5079 while (col <= endcol && ScreenLines[off_to] == ' '
5080 && ScreenAttrs[off_to] == 0
5081 # ifdef FEAT_MBYTE
5082 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5083 # endif
5086 ++off_to;
5087 ++col;
5089 if (col <= endcol)
5090 screen_fill(row, row + 1, col + coloff,
5091 endcol + coloff + 1, ' ', ' ', 0);
5093 col = endcol + 1;
5094 off_to = LineOffset[row] + col + coloff;
5095 off_from += col;
5096 endcol = (clear_width > 0 ? clear_width : -clear_width);
5098 #endif /* FEAT_RIGHTLEFT */
5100 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
5102 while (col < endcol)
5104 #ifdef FEAT_MBYTE
5105 if (has_mbyte && (col + 1 < endcol))
5106 char_cells = (*mb_off2cells)(off_from, max_off_from);
5107 else
5108 char_cells = 1;
5109 #endif
5111 redraw_this = redraw_next;
5112 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
5113 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
5115 #ifdef FEAT_GUI
5116 /* If the next character was bold, then redraw the current character to
5117 * remove any pixels that might have spilt over into us. This only
5118 * happens in the GUI.
5120 if (redraw_next && gui.in_use)
5122 hl = ScreenAttrs[off_to + CHAR_CELLS];
5123 if (hl > HL_ALL)
5124 hl = syn_attr2attr(hl);
5125 if (hl & HL_BOLD)
5126 redraw_this = TRUE;
5128 #endif
5130 if (redraw_this)
5133 * Special handling when 'xs' termcap flag set (hpterm):
5134 * Attributes for characters are stored at the position where the
5135 * cursor is when writing the highlighting code. The
5136 * start-highlighting code must be written with the cursor on the
5137 * first highlighted character. The stop-highlighting code must
5138 * be written with the cursor just after the last highlighted
5139 * character.
5140 * Overwriting a character doesn't remove it's highlighting. Need
5141 * to clear the rest of the line, and force redrawing it
5142 * completely.
5144 if ( p_wiv
5145 && !force
5146 #ifdef FEAT_GUI
5147 && !gui.in_use
5148 #endif
5149 && ScreenAttrs[off_to] != 0
5150 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
5153 * Need to remove highlighting attributes here.
5155 windgoto(row, col + coloff);
5156 out_str(T_CE); /* clear rest of this screen line */
5157 screen_start(); /* don't know where cursor is now */
5158 force = TRUE; /* force redraw of rest of the line */
5159 redraw_next = TRUE; /* or else next char would miss out */
5162 * If the previous character was highlighted, need to stop
5163 * highlighting at this character.
5165 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
5167 screen_attr = ScreenAttrs[off_to - 1];
5168 term_windgoto(row, col + coloff);
5169 screen_stop_highlight();
5171 else
5172 screen_attr = 0; /* highlighting has stopped */
5174 #ifdef FEAT_MBYTE
5175 if (enc_dbcs != 0)
5177 /* Check if overwriting a double-byte with a single-byte or
5178 * the other way around requires another character to be
5179 * redrawn. For UTF-8 this isn't needed, because comparing
5180 * ScreenLinesUC[] is sufficient. */
5181 if (char_cells == 1
5182 && col + 1 < endcol
5183 && (*mb_off2cells)(off_to, max_off_to) > 1)
5185 /* Writing a single-cell character over a double-cell
5186 * character: need to redraw the next cell. */
5187 ScreenLines[off_to + 1] = 0;
5188 redraw_next = TRUE;
5190 else if (char_cells == 2
5191 && col + 2 < endcol
5192 && (*mb_off2cells)(off_to, max_off_to) == 1
5193 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
5195 /* Writing the second half of a double-cell character over
5196 * a double-cell character: need to redraw the second
5197 * cell. */
5198 ScreenLines[off_to + 2] = 0;
5199 redraw_next = TRUE;
5202 if (enc_dbcs == DBCS_JPNU)
5203 ScreenLines2[off_to] = ScreenLines2[off_from];
5205 /* When writing a single-width character over a double-width
5206 * character and at the end of the redrawn text, need to clear out
5207 * the right halve of the old character.
5208 * Also required when writing the right halve of a double-width
5209 * char over the left halve of an existing one. */
5210 if (has_mbyte && col + char_cells == endcol
5211 && ((char_cells == 1
5212 && (*mb_off2cells)(off_to, max_off_to) > 1)
5213 || (char_cells == 2
5214 && (*mb_off2cells)(off_to, max_off_to) == 1
5215 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
5216 clear_next = TRUE;
5217 #endif
5219 ScreenLines[off_to] = ScreenLines[off_from];
5220 #ifdef FEAT_MBYTE
5221 if (enc_utf8)
5223 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
5224 if (ScreenLinesUC[off_from] != 0)
5226 int i;
5228 for (i = 0; i < Screen_mco; ++i)
5229 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
5232 if (char_cells == 2)
5233 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
5234 #endif
5236 #if defined(FEAT_GUI) || defined(UNIX)
5237 /* The bold trick makes a single column of pixels appear in the
5238 * next character. When a bold character is removed, the next
5239 * character should be redrawn too. This happens for our own GUI
5240 * and for some xterms. */
5241 if (
5242 # ifdef FEAT_GUI
5243 gui.in_use
5244 # endif
5245 # if defined(FEAT_GUI) && defined(UNIX)
5247 # endif
5248 # ifdef UNIX
5249 term_is_xterm
5250 # endif
5253 hl = ScreenAttrs[off_to];
5254 if (hl > HL_ALL)
5255 hl = syn_attr2attr(hl);
5256 if (hl & HL_BOLD)
5257 redraw_next = TRUE;
5259 #endif
5260 ScreenAttrs[off_to] = ScreenAttrs[off_from];
5261 #ifdef FEAT_MBYTE
5262 /* For simplicity set the attributes of second half of a
5263 * double-wide character equal to the first half. */
5264 if (char_cells == 2)
5265 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
5267 if (enc_dbcs != 0 && char_cells == 2)
5268 screen_char_2(off_to, row, col + coloff);
5269 else
5270 #endif
5271 screen_char(off_to, row, col + coloff);
5273 else if ( p_wiv
5274 #ifdef FEAT_GUI
5275 && !gui.in_use
5276 #endif
5277 && col + coloff > 0)
5279 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
5282 * Don't output stop-highlight when moving the cursor, it will
5283 * stop the highlighting when it should continue.
5285 screen_attr = 0;
5287 else if (screen_attr != 0)
5288 screen_stop_highlight();
5291 off_to += CHAR_CELLS;
5292 off_from += CHAR_CELLS;
5293 col += CHAR_CELLS;
5296 #ifdef FEAT_MBYTE
5297 if (clear_next)
5299 /* Clear the second half of a double-wide character of which the left
5300 * half was overwritten with a single-wide character. */
5301 ScreenLines[off_to] = ' ';
5302 if (enc_utf8)
5303 ScreenLinesUC[off_to] = 0;
5304 screen_char(off_to, row, col + coloff);
5306 #endif
5308 if (clear_width > 0
5309 #ifdef FEAT_RIGHTLEFT
5310 && !rlflag
5311 #endif
5314 #ifdef FEAT_GUI
5315 int startCol = col;
5316 #endif
5318 /* blank out the rest of the line */
5319 while (col < clear_width && ScreenLines[off_to] == ' '
5320 && ScreenAttrs[off_to] == 0
5321 #ifdef FEAT_MBYTE
5322 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5323 #endif
5326 ++off_to;
5327 ++col;
5329 if (col < clear_width)
5331 #ifdef FEAT_GUI
5333 * In the GUI, clearing the rest of the line may leave pixels
5334 * behind if the first character cleared was bold. Some bold
5335 * fonts spill over the left. In this case we redraw the previous
5336 * character too. If we didn't skip any blanks above, then we
5337 * only redraw if the character wasn't already redrawn anyway.
5339 if (gui.in_use && (col > startCol || !redraw_this))
5341 hl = ScreenAttrs[off_to];
5342 if (hl > HL_ALL || (hl & HL_BOLD))
5344 int prev_cells = 1;
5345 # ifdef FEAT_MBYTE
5346 if (enc_utf8)
5347 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
5348 * that its width is 2. */
5349 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
5350 else if (enc_dbcs != 0)
5352 /* find previous character by counting from first
5353 * column and get its width. */
5354 unsigned off = LineOffset[row];
5355 unsigned max_off = LineOffset[row] + screen_Columns;
5357 while (off < off_to)
5359 prev_cells = (*mb_off2cells)(off, max_off);
5360 off += prev_cells;
5364 if (enc_dbcs != 0 && prev_cells > 1)
5365 screen_char_2(off_to - prev_cells, row,
5366 col + coloff - prev_cells);
5367 else
5368 # endif
5369 screen_char(off_to - prev_cells, row,
5370 col + coloff - prev_cells);
5373 #endif
5374 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
5375 ' ', ' ', 0);
5376 #ifdef FEAT_VERTSPLIT
5377 off_to += clear_width - col;
5378 col = clear_width;
5379 #endif
5383 if (clear_width > 0)
5385 #ifdef FEAT_VERTSPLIT
5386 /* For a window that's left of another, draw the separator char. */
5387 if (col + coloff < Columns)
5389 int c;
5391 c = fillchar_vsep(&hl);
5392 if (ScreenLines[off_to] != c
5393 # ifdef FEAT_MBYTE
5394 || (enc_utf8 && (int)ScreenLinesUC[off_to]
5395 != (c >= 0x80 ? c : 0))
5396 # endif
5397 || ScreenAttrs[off_to] != hl)
5399 ScreenLines[off_to] = c;
5400 ScreenAttrs[off_to] = hl;
5401 # ifdef FEAT_MBYTE
5402 if (enc_utf8)
5404 if (c >= 0x80)
5406 ScreenLinesUC[off_to] = c;
5407 ScreenLinesC[0][off_to] = 0;
5409 else
5410 ScreenLinesUC[off_to] = 0;
5412 # endif
5413 screen_char(off_to, row, col + coloff);
5416 else
5417 #endif
5418 LineWraps[row] = FALSE;
5422 #if defined(FEAT_RIGHTLEFT) || defined(PROTO)
5424 * Mirror text "str" for right-left displaying.
5425 * Only works for single-byte characters (e.g., numbers).
5427 void
5428 rl_mirror(str)
5429 char_u *str;
5431 char_u *p1, *p2;
5432 int t;
5434 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
5436 t = *p1;
5437 *p1 = *p2;
5438 *p2 = t;
5441 #endif
5443 #if defined(FEAT_WINDOWS) || defined(PROTO)
5445 * mark all status lines for redraw; used after first :cd
5447 void
5448 status_redraw_all()
5450 win_T *wp;
5452 for (wp = firstwin; wp; wp = wp->w_next)
5453 if (wp->w_status_height)
5455 wp->w_redr_status = TRUE;
5456 redraw_later(VALID);
5461 * mark all status lines of the current buffer for redraw
5463 void
5464 status_redraw_curbuf()
5466 win_T *wp;
5468 for (wp = firstwin; wp; wp = wp->w_next)
5469 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
5471 wp->w_redr_status = TRUE;
5472 redraw_later(VALID);
5477 * Redraw all status lines that need to be redrawn.
5479 void
5480 redraw_statuslines()
5482 win_T *wp;
5484 for (wp = firstwin; wp; wp = wp->w_next)
5485 if (wp->w_redr_status)
5486 win_redr_status(wp);
5487 if (redraw_tabline)
5488 draw_tabline();
5490 #endif
5492 #if (defined(FEAT_WILDMENU) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
5494 * Redraw all status lines at the bottom of frame "frp".
5496 void
5497 win_redraw_last_status(frp)
5498 frame_T *frp;
5500 if (frp->fr_layout == FR_LEAF)
5501 frp->fr_win->w_redr_status = TRUE;
5502 else if (frp->fr_layout == FR_ROW)
5504 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
5505 win_redraw_last_status(frp);
5507 else /* frp->fr_layout == FR_COL */
5509 frp = frp->fr_child;
5510 while (frp->fr_next != NULL)
5511 frp = frp->fr_next;
5512 win_redraw_last_status(frp);
5515 #endif
5517 #ifdef FEAT_VERTSPLIT
5519 * Draw the verticap separator right of window "wp" starting with line "row".
5521 static void
5522 draw_vsep_win(wp, row)
5523 win_T *wp;
5524 int row;
5526 int hl;
5527 int c;
5529 if (wp->w_vsep_width)
5531 /* draw the vertical separator right of this window */
5532 c = fillchar_vsep(&hl);
5533 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
5534 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
5535 c, ' ', hl);
5538 #endif
5540 #ifdef FEAT_WILDMENU
5541 static int status_match_len __ARGS((expand_T *xp, char_u *s));
5542 static int skip_status_match_char __ARGS((expand_T *xp, char_u *s));
5545 * Get the length of an item as it will be shown in the status line.
5547 static int
5548 status_match_len(xp, s)
5549 expand_T *xp;
5550 char_u *s;
5552 int len = 0;
5554 #ifdef FEAT_MENU
5555 int emenu = (xp->xp_context == EXPAND_MENUS
5556 || xp->xp_context == EXPAND_MENUNAMES);
5558 /* Check for menu separators - replace with '|'. */
5559 if (emenu && menu_is_separator(s))
5560 return 1;
5561 #endif
5563 while (*s != NUL)
5565 s += skip_status_match_char(xp, s);
5566 len += ptr2cells(s);
5567 mb_ptr_adv(s);
5570 return len;
5574 * Return the number of characters that should be skipped in a status match.
5575 * These are backslashes used for escaping. Do show backslashes in help tags.
5577 static int
5578 skip_status_match_char(xp, s)
5579 expand_T *xp;
5580 char_u *s;
5582 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
5583 #ifdef FEAT_MENU
5584 || ((xp->xp_context == EXPAND_MENUS
5585 || xp->xp_context == EXPAND_MENUNAMES)
5586 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
5587 #endif
5590 #ifndef BACKSLASH_IN_FILENAME
5591 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
5592 return 2;
5593 #endif
5594 return 1;
5596 return 0;
5600 * Show wildchar matches in the status line.
5601 * Show at least the "match" item.
5602 * We start at item 'first_match' in the list and show all matches that fit.
5604 * If inversion is possible we use it. Else '=' characters are used.
5606 void
5607 win_redr_status_matches(xp, num_matches, matches, match, showtail)
5608 expand_T *xp;
5609 int num_matches;
5610 char_u **matches; /* list of matches */
5611 int match;
5612 int showtail;
5614 #define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
5615 int row;
5616 char_u *buf;
5617 int len;
5618 int clen; /* length in screen cells */
5619 int fillchar;
5620 int attr;
5621 int i;
5622 int highlight = TRUE;
5623 char_u *selstart = NULL;
5624 int selstart_col = 0;
5625 char_u *selend = NULL;
5626 static int first_match = 0;
5627 int add_left = FALSE;
5628 char_u *s;
5629 #ifdef FEAT_MENU
5630 int emenu;
5631 #endif
5632 #if defined(FEAT_MBYTE) || defined(FEAT_MENU)
5633 int l;
5634 #endif
5636 if (matches == NULL) /* interrupted completion? */
5637 return;
5639 #ifdef FEAT_MBYTE
5640 if (has_mbyte)
5641 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
5642 else
5643 #endif
5644 buf = alloc((unsigned)Columns + 1);
5645 if (buf == NULL)
5646 return;
5648 if (match == -1) /* don't show match but original text */
5650 match = 0;
5651 highlight = FALSE;
5653 /* count 1 for the ending ">" */
5654 clen = status_match_len(xp, L_MATCH(match)) + 3;
5655 if (match == 0)
5656 first_match = 0;
5657 else if (match < first_match)
5659 /* jumping left, as far as we can go */
5660 first_match = match;
5661 add_left = TRUE;
5663 else
5665 /* check if match fits on the screen */
5666 for (i = first_match; i < match; ++i)
5667 clen += status_match_len(xp, L_MATCH(i)) + 2;
5668 if (first_match > 0)
5669 clen += 2;
5670 /* jumping right, put match at the left */
5671 if ((long)clen > Columns)
5673 first_match = match;
5674 /* if showing the last match, we can add some on the left */
5675 clen = 2;
5676 for (i = match; i < num_matches; ++i)
5678 clen += status_match_len(xp, L_MATCH(i)) + 2;
5679 if ((long)clen >= Columns)
5680 break;
5682 if (i == num_matches)
5683 add_left = TRUE;
5686 if (add_left)
5687 while (first_match > 0)
5689 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
5690 if ((long)clen >= Columns)
5691 break;
5692 --first_match;
5695 fillchar = fillchar_status(&attr, TRUE);
5697 if (first_match == 0)
5699 *buf = NUL;
5700 len = 0;
5702 else
5704 STRCPY(buf, "< ");
5705 len = 2;
5707 clen = len;
5709 i = first_match;
5710 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
5712 if (i == match)
5714 selstart = buf + len;
5715 selstart_col = clen;
5718 s = L_MATCH(i);
5719 /* Check for menu separators - replace with '|' */
5720 #ifdef FEAT_MENU
5721 emenu = (xp->xp_context == EXPAND_MENUS
5722 || xp->xp_context == EXPAND_MENUNAMES);
5723 if (emenu && menu_is_separator(s))
5725 STRCPY(buf + len, transchar('|'));
5726 l = (int)STRLEN(buf + len);
5727 len += l;
5728 clen += l;
5730 else
5731 #endif
5732 for ( ; *s != NUL; ++s)
5734 s += skip_status_match_char(xp, s);
5735 clen += ptr2cells(s);
5736 #ifdef FEAT_MBYTE
5737 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
5739 STRNCPY(buf + len, s, l);
5740 s += l - 1;
5741 len += l;
5743 else
5744 #endif
5746 STRCPY(buf + len, transchar_byte(*s));
5747 len += (int)STRLEN(buf + len);
5750 if (i == match)
5751 selend = buf + len;
5753 *(buf + len++) = ' ';
5754 *(buf + len++) = ' ';
5755 clen += 2;
5756 if (++i == num_matches)
5757 break;
5760 if (i != num_matches)
5762 *(buf + len++) = '>';
5763 ++clen;
5766 buf[len] = NUL;
5768 row = cmdline_row - 1;
5769 if (row >= 0)
5771 if (wild_menu_showing == 0)
5773 if (msg_scrolled > 0)
5775 /* Put the wildmenu just above the command line. If there is
5776 * no room, scroll the screen one line up. */
5777 if (cmdline_row == Rows - 1)
5779 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
5780 ++msg_scrolled;
5782 else
5784 ++cmdline_row;
5785 ++row;
5787 wild_menu_showing = WM_SCROLLED;
5789 else
5791 /* Create status line if needed by setting 'laststatus' to 2.
5792 * Set 'winminheight' to zero to avoid that the window is
5793 * resized. */
5794 if (lastwin->w_status_height == 0)
5796 save_p_ls = p_ls;
5797 save_p_wmh = p_wmh;
5798 p_ls = 2;
5799 p_wmh = 0;
5800 last_status(FALSE);
5802 wild_menu_showing = WM_SHOWN;
5806 screen_puts(buf, row, 0, attr);
5807 if (selstart != NULL && highlight)
5809 *selend = NUL;
5810 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
5813 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
5816 #ifdef FEAT_VERTSPLIT
5817 win_redraw_last_status(topframe);
5818 #else
5819 lastwin->w_redr_status = TRUE;
5820 #endif
5821 vim_free(buf);
5823 #endif
5825 #if defined(FEAT_WINDOWS) || defined(PROTO)
5827 * Redraw the status line of window wp.
5829 * If inversion is possible we use it. Else '=' characters are used.
5831 void
5832 win_redr_status(wp)
5833 win_T *wp;
5835 int row;
5836 char_u *p;
5837 int len;
5838 int fillchar;
5839 int attr;
5840 int this_ru_col;
5841 static int busy = FALSE;
5843 /* It's possible to get here recursively when 'statusline' (indirectly)
5844 * invokes ":redrawstatus". Simply ignore the call then. */
5845 if (busy)
5846 return;
5847 busy = TRUE;
5849 wp->w_redr_status = FALSE;
5850 if (wp->w_status_height == 0)
5852 /* no status line, can only be last window */
5853 redraw_cmdline = TRUE;
5855 else if (!redrawing()
5856 #ifdef FEAT_INS_EXPAND
5857 /* don't update status line when popup menu is visible and may be
5858 * drawn over it */
5859 || pum_visible()
5860 #endif
5863 /* Don't redraw right now, do it later. */
5864 wp->w_redr_status = TRUE;
5866 #ifdef FEAT_STL_OPT
5867 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
5869 /* redraw custom status line */
5870 redraw_custom_statusline(wp);
5872 #endif
5873 else
5875 fillchar = fillchar_status(&attr, wp == curwin);
5877 get_trans_bufname(wp->w_buffer);
5878 p = NameBuff;
5879 len = (int)STRLEN(p);
5881 if (wp->w_buffer->b_help
5882 #ifdef FEAT_QUICKFIX
5883 || wp->w_p_pvw
5884 #endif
5885 || bufIsChanged(wp->w_buffer)
5886 || wp->w_buffer->b_p_ro)
5887 *(p + len++) = ' ';
5888 if (wp->w_buffer->b_help)
5890 STRCPY(p + len, _("[Help]"));
5891 len += (int)STRLEN(p + len);
5893 #ifdef FEAT_QUICKFIX
5894 if (wp->w_p_pvw)
5896 STRCPY(p + len, _("[Preview]"));
5897 len += (int)STRLEN(p + len);
5899 #endif
5900 if (bufIsChanged(wp->w_buffer))
5902 STRCPY(p + len, "[+]");
5903 len += 3;
5905 if (wp->w_buffer->b_p_ro)
5907 STRCPY(p + len, "[RO]");
5908 len += 4;
5911 #ifndef FEAT_VERTSPLIT
5912 this_ru_col = ru_col;
5913 if (this_ru_col < (Columns + 1) / 2)
5914 this_ru_col = (Columns + 1) / 2;
5915 #else
5916 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
5917 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
5918 this_ru_col = (W_WIDTH(wp) + 1) / 2;
5919 if (this_ru_col <= 1)
5921 p = (char_u *)"<"; /* No room for file name! */
5922 len = 1;
5924 else
5925 #endif
5926 #ifdef FEAT_MBYTE
5927 if (has_mbyte)
5929 int clen = 0, i;
5931 /* Count total number of display cells. */
5932 for (i = 0; p[i] != NUL; i += (*mb_ptr2len)(p + i))
5933 clen += (*mb_ptr2cells)(p + i);
5934 /* Find first character that will fit.
5935 * Going from start to end is much faster for DBCS. */
5936 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
5937 i += (*mb_ptr2len)(p + i))
5938 clen -= (*mb_ptr2cells)(p + i);
5939 len = clen;
5940 if (i > 0)
5942 p = p + i - 1;
5943 *p = '<';
5944 ++len;
5948 else
5949 #endif
5950 if (len > this_ru_col - 1)
5952 p += len - (this_ru_col - 1);
5953 *p = '<';
5954 len = this_ru_col - 1;
5957 row = W_WINROW(wp) + wp->w_height;
5958 screen_puts(p, row, W_WINCOL(wp), attr);
5959 screen_fill(row, row + 1, len + W_WINCOL(wp),
5960 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
5962 if (get_keymap_str(wp, NameBuff, MAXPATHL)
5963 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
5964 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
5965 - 1 + W_WINCOL(wp)), attr);
5967 #ifdef FEAT_CMDL_INFO
5968 win_redr_ruler(wp, TRUE);
5969 #endif
5972 #ifdef FEAT_VERTSPLIT
5974 * May need to draw the character below the vertical separator.
5976 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
5978 if (stl_connected(wp))
5979 fillchar = fillchar_status(&attr, wp == curwin);
5980 else
5981 fillchar = fillchar_vsep(&attr);
5982 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
5983 attr);
5985 #endif
5986 busy = FALSE;
5989 #ifdef FEAT_STL_OPT
5991 * Redraw the status line according to 'statusline' and take care of any
5992 * errors encountered.
5994 static void
5995 redraw_custom_statusline(wp)
5996 win_T *wp;
5998 static int entered = FALSE;
5999 int save_called_emsg = called_emsg;
6001 /* When called recursively return. This can happen when the statusline
6002 * contains an expression that triggers a redraw. */
6003 if (entered)
6004 return;
6005 entered = TRUE;
6007 called_emsg = FALSE;
6008 win_redr_custom(wp, FALSE);
6009 if (called_emsg)
6011 /* When there is an error disable the statusline, otherwise the
6012 * display is messed up with errors and a redraw triggers the problem
6013 * again and again. */
6014 set_string_option_direct((char_u *)"statusline", -1,
6015 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
6016 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
6018 called_emsg |= save_called_emsg;
6019 entered = FALSE;
6021 #endif
6023 # ifdef FEAT_VERTSPLIT
6025 * Return TRUE if the status line of window "wp" is connected to the status
6026 * line of the window right of it. If not, then it's a vertical separator.
6027 * Only call if (wp->w_vsep_width != 0).
6030 stl_connected(wp)
6031 win_T *wp;
6033 frame_T *fr;
6035 fr = wp->w_frame;
6036 while (fr->fr_parent != NULL)
6038 if (fr->fr_parent->fr_layout == FR_COL)
6040 if (fr->fr_next != NULL)
6041 break;
6043 else
6045 if (fr->fr_next != NULL)
6046 return TRUE;
6048 fr = fr->fr_parent;
6050 return FALSE;
6052 # endif
6054 #endif /* FEAT_WINDOWS */
6056 #if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
6058 * Get the value to show for the language mappings, active 'keymap'.
6061 get_keymap_str(wp, buf, len)
6062 win_T *wp;
6063 char_u *buf; /* buffer for the result */
6064 int len; /* length of buffer */
6066 char_u *p;
6068 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
6069 return FALSE;
6072 #ifdef FEAT_EVAL
6073 buf_T *old_curbuf = curbuf;
6074 win_T *old_curwin = curwin;
6075 char_u *s;
6077 curbuf = wp->w_buffer;
6078 curwin = wp;
6079 STRCPY(buf, "b:keymap_name"); /* must be writable */
6080 ++emsg_skip;
6081 s = p = eval_to_string(buf, NULL, FALSE);
6082 --emsg_skip;
6083 curbuf = old_curbuf;
6084 curwin = old_curwin;
6085 if (p == NULL || *p == NUL)
6086 #endif
6088 #ifdef FEAT_KEYMAP
6089 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
6090 p = wp->w_buffer->b_p_keymap;
6091 else
6092 #endif
6093 p = (char_u *)"lang";
6095 if ((int)(STRLEN(p) + 3) < len)
6096 sprintf((char *)buf, "<%s>", p);
6097 else
6098 buf[0] = NUL;
6099 #ifdef FEAT_EVAL
6100 vim_free(s);
6101 #endif
6103 return buf[0] != NUL;
6105 #endif
6107 #if defined(FEAT_STL_OPT) || defined(PROTO)
6109 * Redraw the status line or ruler of window "wp".
6110 * When "wp" is NULL redraw the tab pages line from 'tabline'.
6112 static void
6113 win_redr_custom(wp, draw_ruler)
6114 win_T *wp;
6115 int draw_ruler; /* TRUE or FALSE */
6117 int attr;
6118 int curattr;
6119 int row;
6120 int col = 0;
6121 int maxwidth;
6122 int width;
6123 int n;
6124 int len;
6125 int fillchar;
6126 char_u buf[MAXPATHL];
6127 char_u *stl;
6128 char_u *p;
6129 struct stl_hlrec hltab[STL_MAX_ITEM];
6130 struct stl_hlrec tabtab[STL_MAX_ITEM];
6131 int use_sandbox = FALSE;
6133 /* setup environment for the task at hand */
6134 if (wp == NULL)
6136 /* Use 'tabline'. Always at the first line of the screen. */
6137 stl = p_tal;
6138 row = 0;
6139 fillchar = ' ';
6140 attr = hl_attr(HLF_TPF);
6141 maxwidth = Columns;
6142 # ifdef FEAT_EVAL
6143 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
6144 # endif
6146 else
6148 row = W_WINROW(wp) + wp->w_height;
6149 fillchar = fillchar_status(&attr, wp == curwin);
6150 maxwidth = W_WIDTH(wp);
6152 if (draw_ruler)
6154 stl = p_ruf;
6155 /* advance past any leading group spec - implicit in ru_col */
6156 if (*stl == '%')
6158 if (*++stl == '-')
6159 stl++;
6160 if (atoi((char *)stl))
6161 while (VIM_ISDIGIT(*stl))
6162 stl++;
6163 if (*stl++ != '(')
6164 stl = p_ruf;
6166 #ifdef FEAT_VERTSPLIT
6167 col = ru_col - (Columns - W_WIDTH(wp));
6168 if (col < (W_WIDTH(wp) + 1) / 2)
6169 col = (W_WIDTH(wp) + 1) / 2;
6170 #else
6171 col = ru_col;
6172 if (col > (Columns + 1) / 2)
6173 col = (Columns + 1) / 2;
6174 #endif
6175 maxwidth = W_WIDTH(wp) - col;
6176 #ifdef FEAT_WINDOWS
6177 if (!wp->w_status_height)
6178 #endif
6180 row = Rows - 1;
6181 --maxwidth; /* writing in last column may cause scrolling */
6182 fillchar = ' ';
6183 attr = 0;
6186 # ifdef FEAT_EVAL
6187 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
6188 # endif
6190 else
6192 if (*wp->w_p_stl != NUL)
6193 stl = wp->w_p_stl;
6194 else
6195 stl = p_stl;
6196 # ifdef FEAT_EVAL
6197 use_sandbox = was_set_insecurely((char_u *)"statusline",
6198 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
6199 # endif
6202 #ifdef FEAT_VERTSPLIT
6203 col += W_WINCOL(wp);
6204 #endif
6207 if (maxwidth <= 0)
6208 return;
6210 /* Make a copy, because the statusline may include a function call that
6211 * might change the option value and free the memory. */
6212 stl = vim_strsave(stl);
6213 width = build_stl_str_hl(wp == NULL ? curwin : wp,
6214 buf, sizeof(buf),
6215 stl, use_sandbox,
6216 fillchar, maxwidth, hltab, tabtab);
6217 vim_free(stl);
6218 len = (int)STRLEN(buf);
6220 while (width < maxwidth && len < (int)sizeof(buf) - 1)
6222 #ifdef FEAT_MBYTE
6223 len += (*mb_char2bytes)(fillchar, buf + len);
6224 #else
6225 buf[len++] = fillchar;
6226 #endif
6227 ++width;
6229 buf[len] = NUL;
6232 * Draw each snippet with the specified highlighting.
6234 curattr = attr;
6235 p = buf;
6236 for (n = 0; hltab[n].start != NULL; n++)
6238 len = (int)(hltab[n].start - p);
6239 screen_puts_len(p, len, row, col, curattr);
6240 col += vim_strnsize(p, len);
6241 p = hltab[n].start;
6243 if (hltab[n].userhl == 0)
6244 curattr = attr;
6245 else if (hltab[n].userhl < 0)
6246 curattr = syn_id2attr(-hltab[n].userhl);
6247 #ifdef FEAT_WINDOWS
6248 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
6249 curattr = highlight_stlnc[hltab[n].userhl - 1];
6250 #endif
6251 else
6252 curattr = highlight_user[hltab[n].userhl - 1];
6254 screen_puts(p, row, col, curattr);
6256 if (wp == NULL)
6258 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
6259 col = 0;
6260 len = 0;
6261 p = buf;
6262 fillchar = 0;
6263 for (n = 0; tabtab[n].start != NULL; n++)
6265 len += vim_strnsize(p, (int)(tabtab[n].start - p));
6266 while (col < len)
6267 TabPageIdxs[col++] = fillchar;
6268 p = tabtab[n].start;
6269 fillchar = tabtab[n].userhl;
6271 while (col < Columns)
6272 TabPageIdxs[col++] = fillchar;
6276 #endif /* FEAT_STL_OPT */
6279 * Output a single character directly to the screen and update ScreenLines.
6281 void
6282 screen_putchar(c, row, col, attr)
6283 int c;
6284 int row, col;
6285 int attr;
6287 #ifdef FEAT_MBYTE
6288 char_u buf[MB_MAXBYTES + 1];
6290 buf[(*mb_char2bytes)(c, buf)] = NUL;
6291 #else
6292 char_u buf[2];
6294 buf[0] = c;
6295 buf[1] = NUL;
6296 #endif
6297 screen_puts(buf, row, col, attr);
6301 * Get a single character directly from ScreenLines into "bytes[]".
6302 * Also return its attribute in *attrp;
6304 void
6305 screen_getbytes(row, col, bytes, attrp)
6306 int row, col;
6307 char_u *bytes;
6308 int *attrp;
6310 unsigned off;
6312 /* safety check */
6313 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
6315 off = LineOffset[row] + col;
6316 *attrp = ScreenAttrs[off];
6317 bytes[0] = ScreenLines[off];
6318 bytes[1] = NUL;
6320 #ifdef FEAT_MBYTE
6321 if (enc_utf8 && ScreenLinesUC[off] != 0)
6322 bytes[utfc_char2bytes(off, bytes)] = NUL;
6323 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6325 bytes[0] = ScreenLines[off];
6326 bytes[1] = ScreenLines2[off];
6327 bytes[2] = NUL;
6329 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
6331 bytes[1] = ScreenLines[off + 1];
6332 bytes[2] = NUL;
6334 #endif
6338 #ifdef FEAT_MBYTE
6339 static int screen_comp_differs __ARGS((int, int*));
6342 * Return TRUE if composing characters for screen posn "off" differs from
6343 * composing characters in "u8cc".
6344 * Only to be used when ScreenLinesUC[off] != 0.
6346 static int
6347 screen_comp_differs(off, u8cc)
6348 int off;
6349 int *u8cc;
6351 int i;
6353 for (i = 0; i < Screen_mco; ++i)
6355 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
6356 return TRUE;
6357 if (u8cc[i] == 0)
6358 break;
6360 return FALSE;
6362 #endif
6365 * Put string '*text' on the screen at position 'row' and 'col', with
6366 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
6367 * Note: only outputs within one row, message is truncated at screen boundary!
6368 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
6370 void
6371 screen_puts(text, row, col, attr)
6372 char_u *text;
6373 int row;
6374 int col;
6375 int attr;
6377 screen_puts_len(text, -1, row, col, attr);
6381 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
6382 * a NUL.
6384 void
6385 screen_puts_len(text, len, row, col, attr)
6386 char_u *text;
6387 int len;
6388 int row;
6389 int col;
6390 int attr;
6392 unsigned off;
6393 char_u *ptr = text;
6394 int c;
6395 #ifdef FEAT_MBYTE
6396 unsigned max_off;
6397 int mbyte_blen = 1;
6398 int mbyte_cells = 1;
6399 int u8c = 0;
6400 int u8cc[MAX_MCO];
6401 int clear_next_cell = FALSE;
6402 # ifdef FEAT_ARABIC
6403 int prev_c = 0; /* previous Arabic character */
6404 int pc, nc, nc1;
6405 int pcc[MAX_MCO];
6406 # endif
6407 #endif
6408 #if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6409 int force_redraw_this;
6410 int force_redraw_next = FALSE;
6411 #endif
6412 int need_redraw;
6414 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
6415 return;
6416 off = LineOffset[row] + col;
6418 #ifdef FEAT_MBYTE
6419 /* When drawing over the right halve of a double-wide char clear out the
6420 * left halve. Only needed in a terminal. */
6421 if (has_mbyte && col > 0 && col < screen_Columns
6422 # ifdef FEAT_GUI
6423 && !gui.in_use
6424 # endif
6425 && mb_fix_col(col, row) != col)
6427 ScreenLines[off - 1] = ' ';
6428 ScreenAttrs[off - 1] = 0;
6429 if (enc_utf8)
6431 ScreenLinesUC[off - 1] = 0;
6432 ScreenLinesC[0][off - 1] = 0;
6434 /* redraw the previous cell, make it empty */
6435 screen_char(off - 1, row, col - 1);
6436 /* force the cell at "col" to be redrawn */
6437 force_redraw_next = TRUE;
6439 #endif
6441 #ifdef FEAT_MBYTE
6442 max_off = LineOffset[row] + screen_Columns;
6443 #endif
6444 while (col < screen_Columns
6445 && (len < 0 || (int)(ptr - text) < len)
6446 && *ptr != NUL)
6448 c = *ptr;
6449 #ifdef FEAT_MBYTE
6450 /* check if this is the first byte of a multibyte */
6451 if (has_mbyte)
6453 if (enc_utf8 && len > 0)
6454 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
6455 else
6456 mbyte_blen = (*mb_ptr2len)(ptr);
6457 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6458 mbyte_cells = 1;
6459 else if (enc_dbcs != 0)
6460 mbyte_cells = mbyte_blen;
6461 else /* enc_utf8 */
6463 if (len >= 0)
6464 u8c = utfc_ptr2char_len(ptr, u8cc,
6465 (int)((text + len) - ptr));
6466 else
6467 u8c = utfc_ptr2char(ptr, u8cc);
6468 mbyte_cells = utf_char2cells(u8c);
6469 # ifdef UNICODE16
6470 /* Non-BMP character: display as ? or fullwidth ?. */
6471 if (u8c >= 0x10000)
6473 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
6474 if (attr == 0)
6475 attr = hl_attr(HLF_8);
6477 # endif
6478 # ifdef FEAT_ARABIC
6479 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
6481 /* Do Arabic shaping. */
6482 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
6484 /* Past end of string to be displayed. */
6485 nc = NUL;
6486 nc1 = NUL;
6488 else
6490 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
6491 (int)((text + len) - ptr - mbyte_blen));
6492 nc1 = pcc[0];
6494 pc = prev_c;
6495 prev_c = u8c;
6496 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
6498 else
6499 prev_c = u8c;
6500 # endif
6501 if (col + mbyte_cells > screen_Columns)
6503 /* Only 1 cell left, but character requires 2 cells:
6504 * display a '>' in the last column to avoid wrapping. */
6505 c = '>';
6506 mbyte_cells = 1;
6510 #endif
6512 #if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6513 force_redraw_this = force_redraw_next;
6514 force_redraw_next = FALSE;
6515 #endif
6517 need_redraw = ScreenLines[off] != c
6518 #ifdef FEAT_MBYTE
6519 || (mbyte_cells == 2
6520 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
6521 || (enc_dbcs == DBCS_JPNU
6522 && c == 0x8e
6523 && ScreenLines2[off] != ptr[1])
6524 || (enc_utf8
6525 && (ScreenLinesUC[off] !=
6526 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
6527 || (ScreenLinesUC[off] != 0
6528 && screen_comp_differs(off, u8cc))))
6529 #endif
6530 || ScreenAttrs[off] != attr
6531 || exmode_active;
6533 if (need_redraw
6534 #if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6535 || force_redraw_this
6536 #endif
6539 #if defined(FEAT_GUI) || defined(UNIX)
6540 /* The bold trick makes a single row of pixels appear in the next
6541 * character. When a bold character is removed, the next
6542 * character should be redrawn too. This happens for our own GUI
6543 * and for some xterms. */
6544 if (need_redraw && ScreenLines[off] != ' ' && (
6545 # ifdef FEAT_GUI
6546 gui.in_use
6547 # endif
6548 # if defined(FEAT_GUI) && defined(UNIX)
6550 # endif
6551 # ifdef UNIX
6552 term_is_xterm
6553 # endif
6556 int n = ScreenAttrs[off];
6558 if (n > HL_ALL)
6559 n = syn_attr2attr(n);
6560 if (n & HL_BOLD)
6561 force_redraw_next = TRUE;
6563 #endif
6564 #ifdef FEAT_MBYTE
6565 /* When at the end of the text and overwriting a two-cell
6566 * character with a one-cell character, need to clear the next
6567 * cell. Also when overwriting the left halve of a two-cell char
6568 * with the right halve of a two-cell char. Do this only once
6569 * (mb_off2cells() may return 2 on the right halve). */
6570 if (clear_next_cell)
6571 clear_next_cell = FALSE;
6572 else if (has_mbyte
6573 && (len < 0 ? ptr[mbyte_blen] == NUL
6574 : ptr + mbyte_blen >= text + len)
6575 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
6576 || (mbyte_cells == 2
6577 && (*mb_off2cells)(off, max_off) == 1
6578 && (*mb_off2cells)(off + 1, max_off) > 1)))
6579 clear_next_cell = TRUE;
6581 /* Make sure we never leave a second byte of a double-byte behind,
6582 * it confuses mb_off2cells(). */
6583 if (enc_dbcs
6584 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
6585 || (mbyte_cells == 2
6586 && (*mb_off2cells)(off, max_off) == 1
6587 && (*mb_off2cells)(off + 1, max_off) > 1)))
6588 ScreenLines[off + mbyte_blen] = 0;
6589 #endif
6590 ScreenLines[off] = c;
6591 ScreenAttrs[off] = attr;
6592 #ifdef FEAT_MBYTE
6593 if (enc_utf8)
6595 if (c < 0x80 && u8cc[0] == 0)
6596 ScreenLinesUC[off] = 0;
6597 else
6599 int i;
6601 ScreenLinesUC[off] = u8c;
6602 for (i = 0; i < Screen_mco; ++i)
6604 ScreenLinesC[i][off] = u8cc[i];
6605 if (u8cc[i] == 0)
6606 break;
6609 if (mbyte_cells == 2)
6611 ScreenLines[off + 1] = 0;
6612 ScreenAttrs[off + 1] = attr;
6614 screen_char(off, row, col);
6616 else if (mbyte_cells == 2)
6618 ScreenLines[off + 1] = ptr[1];
6619 ScreenAttrs[off + 1] = attr;
6620 screen_char_2(off, row, col);
6622 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6624 ScreenLines2[off] = ptr[1];
6625 screen_char(off, row, col);
6627 else
6628 #endif
6629 screen_char(off, row, col);
6631 #ifdef FEAT_MBYTE
6632 if (has_mbyte)
6634 off += mbyte_cells;
6635 col += mbyte_cells;
6636 ptr += mbyte_blen;
6637 if (clear_next_cell)
6638 ptr = (char_u *)" ";
6640 else
6641 #endif
6643 ++off;
6644 ++col;
6645 ++ptr;
6649 #if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6650 /* If we detected the next character needs to be redrawn, but the text
6651 * doesn't extend up to there, update the character here. */
6652 if (force_redraw_next && col < screen_Columns)
6654 # ifdef FEAT_MBYTE
6655 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
6656 screen_char_2(off, row, col);
6657 else
6658 # endif
6659 screen_char(off, row, col);
6661 #endif
6664 #ifdef FEAT_SEARCH_EXTRA
6666 * Prepare for 'hlsearch' highlighting.
6668 static void
6669 start_search_hl()
6671 if (p_hls && !no_hlsearch)
6673 last_pat_prog(&search_hl.rm);
6674 search_hl.attr = hl_attr(HLF_L);
6675 # ifdef FEAT_RELTIME
6676 /* Set the time limit to 'redrawtime'. */
6677 profile_setlimit(p_rdt, &search_hl.tm);
6678 # endif
6683 * Clean up for 'hlsearch' highlighting.
6685 static void
6686 end_search_hl()
6688 if (search_hl.rm.regprog != NULL)
6690 vim_free(search_hl.rm.regprog);
6691 search_hl.rm.regprog = NULL;
6696 * Advance to the match in window "wp" line "lnum" or past it.
6698 static void
6699 prepare_search_hl(wp, lnum)
6700 win_T *wp;
6701 linenr_T lnum;
6703 matchitem_T *cur; /* points to the match list */
6704 match_T *shl; /* points to search_hl or a match */
6705 int shl_flag; /* flag to indicate whether search_hl
6706 has been processed or not */
6707 int n;
6710 * When using a multi-line pattern, start searching at the top
6711 * of the window or just after a closed fold.
6712 * Do this both for search_hl and the match list.
6714 cur = wp->w_match_head;
6715 shl_flag = FALSE;
6716 while (cur != NULL || shl_flag == FALSE)
6718 if (shl_flag == FALSE)
6720 shl = &search_hl;
6721 shl_flag = TRUE;
6723 else
6724 shl = &cur->hl;
6725 if (shl->rm.regprog != NULL
6726 && shl->lnum == 0
6727 && re_multiline(shl->rm.regprog))
6729 if (shl->first_lnum == 0)
6731 # ifdef FEAT_FOLDING
6732 for (shl->first_lnum = lnum;
6733 shl->first_lnum > wp->w_topline; --shl->first_lnum)
6734 if (hasFoldingWin(wp, shl->first_lnum - 1,
6735 NULL, NULL, TRUE, NULL))
6736 break;
6737 # else
6738 shl->first_lnum = wp->w_topline;
6739 # endif
6741 n = 0;
6742 while (shl->first_lnum < lnum && shl->rm.regprog != NULL)
6744 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n);
6745 if (shl->lnum != 0)
6747 shl->first_lnum = shl->lnum
6748 + shl->rm.endpos[0].lnum
6749 - shl->rm.startpos[0].lnum;
6750 n = shl->rm.endpos[0].col;
6752 else
6754 ++shl->first_lnum;
6755 n = 0;
6759 if (shl != &search_hl && cur != NULL)
6760 cur = cur->next;
6765 * Search for a next 'hlsearch' or match.
6766 * Uses shl->buf.
6767 * Sets shl->lnum and shl->rm contents.
6768 * Note: Assumes a previous match is always before "lnum", unless
6769 * shl->lnum is zero.
6770 * Careful: Any pointers for buffer lines will become invalid.
6772 static void
6773 next_search_hl(win, shl, lnum, mincol)
6774 win_T *win;
6775 match_T *shl; /* points to search_hl or a match */
6776 linenr_T lnum;
6777 colnr_T mincol; /* minimal column for a match */
6779 linenr_T l;
6780 colnr_T matchcol;
6781 long nmatched;
6783 if (shl->lnum != 0)
6785 /* Check for three situations:
6786 * 1. If the "lnum" is below a previous match, start a new search.
6787 * 2. If the previous match includes "mincol", use it.
6788 * 3. Continue after the previous match.
6790 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
6791 if (lnum > l)
6792 shl->lnum = 0;
6793 else if (lnum < l || shl->rm.endpos[0].col > mincol)
6794 return;
6798 * Repeat searching for a match until one is found that includes "mincol"
6799 * or none is found in this line.
6801 called_emsg = FALSE;
6802 for (;;)
6804 #ifdef FEAT_RELTIME
6805 /* Stop searching after passing the time limit. */
6806 if (profile_passed_limit(&(shl->tm)))
6808 shl->lnum = 0; /* no match found in time */
6809 break;
6811 #endif
6812 /* Three situations:
6813 * 1. No useful previous match: search from start of line.
6814 * 2. Not Vi compatible or empty match: continue at next character.
6815 * Break the loop if this is beyond the end of the line.
6816 * 3. Vi compatible searching: continue at end of previous match.
6818 if (shl->lnum == 0)
6819 matchcol = 0;
6820 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
6821 || (shl->rm.endpos[0].lnum == 0
6822 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
6824 char_u *ml;
6826 matchcol = shl->rm.startpos[0].col;
6827 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
6828 if (*ml == NUL)
6830 ++matchcol;
6831 shl->lnum = 0;
6832 break;
6834 #ifdef FEAT_MBYTE
6835 if (has_mbyte)
6836 matchcol += mb_ptr2len(ml);
6837 else
6838 #endif
6839 ++matchcol;
6841 else
6842 matchcol = shl->rm.endpos[0].col;
6844 shl->lnum = lnum;
6845 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum, matchcol,
6846 #ifdef FEAT_RELTIME
6847 &(shl->tm)
6848 #else
6849 NULL
6850 #endif
6852 if (called_emsg)
6854 /* Error while handling regexp: stop using this regexp. */
6855 if (shl == &search_hl)
6857 /* don't free regprog in the match list, it's a copy */
6858 vim_free(shl->rm.regprog);
6859 no_hlsearch = TRUE;
6861 shl->rm.regprog = NULL;
6862 shl->lnum = 0;
6863 got_int = FALSE; /* avoid the "Type :quit to exit Vim" message */
6864 break;
6866 if (nmatched == 0)
6868 shl->lnum = 0; /* no match found */
6869 break;
6871 if (shl->rm.startpos[0].lnum > 0
6872 || shl->rm.startpos[0].col >= mincol
6873 || nmatched > 1
6874 || shl->rm.endpos[0].col > mincol)
6876 shl->lnum += shl->rm.startpos[0].lnum;
6877 break; /* useful match found */
6881 #endif
6883 static void
6884 screen_start_highlight(attr)
6885 int attr;
6887 attrentry_T *aep = NULL;
6889 screen_attr = attr;
6890 if (full_screen
6891 #ifdef WIN3264
6892 && termcap_active
6893 #endif
6896 #ifdef FEAT_GUI
6897 if (gui.in_use)
6899 char buf[20];
6901 /* The GUI handles this internally. */
6902 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
6903 OUT_STR(buf);
6905 else
6906 #endif
6908 if (attr > HL_ALL) /* special HL attr. */
6910 if (t_colors > 1)
6911 aep = syn_cterm_attr2entry(attr);
6912 else
6913 aep = syn_term_attr2entry(attr);
6914 if (aep == NULL) /* did ":syntax clear" */
6915 attr = 0;
6916 else
6917 attr = aep->ae_attr;
6919 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
6920 out_str(T_MD);
6921 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
6922 && cterm_normal_fg_bold)
6923 /* If the Normal FG color has BOLD attribute and the new HL
6924 * has a FG color defined, clear BOLD. */
6925 out_str(T_ME);
6926 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
6927 out_str(T_SO);
6928 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
6929 /* underline or undercurl */
6930 out_str(T_US);
6931 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
6932 out_str(T_CZH);
6933 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
6934 out_str(T_MR);
6937 * Output the color or start string after bold etc., in case the
6938 * bold etc. override the color setting.
6940 if (aep != NULL)
6942 if (t_colors > 1)
6944 if (aep->ae_u.cterm.fg_color)
6945 term_fg_color(aep->ae_u.cterm.fg_color - 1);
6946 if (aep->ae_u.cterm.bg_color)
6947 term_bg_color(aep->ae_u.cterm.bg_color - 1);
6949 else
6951 if (aep->ae_u.term.start != NULL)
6952 out_str(aep->ae_u.term.start);
6959 void
6960 screen_stop_highlight()
6962 int do_ME = FALSE; /* output T_ME code */
6964 if (screen_attr != 0
6965 #ifdef WIN3264
6966 && termcap_active
6967 #endif
6970 #ifdef FEAT_GUI
6971 if (gui.in_use)
6973 char buf[20];
6975 /* use internal GUI code */
6976 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
6977 OUT_STR(buf);
6979 else
6980 #endif
6982 if (screen_attr > HL_ALL) /* special HL attr. */
6984 attrentry_T *aep;
6986 if (t_colors > 1)
6989 * Assume that t_me restores the original colors!
6991 aep = syn_cterm_attr2entry(screen_attr);
6992 if (aep != NULL && (aep->ae_u.cterm.fg_color
6993 || aep->ae_u.cterm.bg_color))
6994 do_ME = TRUE;
6996 else
6998 aep = syn_term_attr2entry(screen_attr);
6999 if (aep != NULL && aep->ae_u.term.stop != NULL)
7001 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
7002 do_ME = TRUE;
7003 else
7004 out_str(aep->ae_u.term.stop);
7007 if (aep == NULL) /* did ":syntax clear" */
7008 screen_attr = 0;
7009 else
7010 screen_attr = aep->ae_attr;
7014 * Often all ending-codes are equal to T_ME. Avoid outputting the
7015 * same sequence several times.
7017 if (screen_attr & HL_STANDOUT)
7019 if (STRCMP(T_SE, T_ME) == 0)
7020 do_ME = TRUE;
7021 else
7022 out_str(T_SE);
7024 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
7026 if (STRCMP(T_UE, T_ME) == 0)
7027 do_ME = TRUE;
7028 else
7029 out_str(T_UE);
7031 if (screen_attr & HL_ITALIC)
7033 if (STRCMP(T_CZR, T_ME) == 0)
7034 do_ME = TRUE;
7035 else
7036 out_str(T_CZR);
7038 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
7039 out_str(T_ME);
7041 if (t_colors > 1)
7043 /* set Normal cterm colors */
7044 if (cterm_normal_fg_color != 0)
7045 term_fg_color(cterm_normal_fg_color - 1);
7046 if (cterm_normal_bg_color != 0)
7047 term_bg_color(cterm_normal_bg_color - 1);
7048 if (cterm_normal_fg_bold)
7049 out_str(T_MD);
7053 screen_attr = 0;
7057 * Reset the colors for a cterm. Used when leaving Vim.
7058 * The machine specific code may override this again.
7060 void
7061 reset_cterm_colors()
7063 if (t_colors > 1)
7065 /* set Normal cterm colors */
7066 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
7068 out_str(T_OP);
7069 screen_attr = -1;
7071 if (cterm_normal_fg_bold)
7073 out_str(T_ME);
7074 screen_attr = -1;
7080 * Put character ScreenLines["off"] on the screen at position "row" and "col",
7081 * using the attributes from ScreenAttrs["off"].
7083 static void
7084 screen_char(off, row, col)
7085 unsigned off;
7086 int row;
7087 int col;
7089 int attr;
7091 /* Check for illegal values, just in case (could happen just after
7092 * resizing). */
7093 if (row >= screen_Rows || col >= screen_Columns)
7094 return;
7096 /* Outputting the last character on the screen may scrollup the screen.
7097 * Don't to it! Mark the character invalid (update it when scrolled up) */
7098 if (row == screen_Rows - 1 && col == screen_Columns - 1
7099 #ifdef FEAT_RIGHTLEFT
7100 /* account for first command-line character in rightleft mode */
7101 && !cmdmsg_rl
7102 #endif
7105 ScreenAttrs[off] = (sattr_T)-1;
7106 return;
7110 * Stop highlighting first, so it's easier to move the cursor.
7112 #if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
7113 if (screen_char_attr != 0)
7114 attr = screen_char_attr;
7115 else
7116 #endif
7117 attr = ScreenAttrs[off];
7118 if (screen_attr != attr)
7119 screen_stop_highlight();
7121 windgoto(row, col);
7123 if (screen_attr != attr)
7124 screen_start_highlight(attr);
7126 #ifdef FEAT_MBYTE
7127 if (enc_utf8 && ScreenLinesUC[off] != 0)
7129 char_u buf[MB_MAXBYTES + 1];
7131 /* Convert UTF-8 character to bytes and write it. */
7133 buf[utfc_char2bytes(off, buf)] = NUL;
7135 out_str(buf);
7136 if (utf_char2cells(ScreenLinesUC[off]) > 1)
7137 ++screen_cur_col;
7139 else
7140 #endif
7142 #ifdef FEAT_MBYTE
7143 out_flush_check();
7144 #endif
7145 out_char(ScreenLines[off]);
7146 #ifdef FEAT_MBYTE
7147 /* double-byte character in single-width cell */
7148 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7149 out_char(ScreenLines2[off]);
7150 #endif
7153 screen_cur_col++;
7156 #ifdef FEAT_MBYTE
7159 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
7160 * on the screen at position 'row' and 'col'.
7161 * The attributes of the first byte is used for all. This is required to
7162 * output the two bytes of a double-byte character with nothing in between.
7164 static void
7165 screen_char_2(off, row, col)
7166 unsigned off;
7167 int row;
7168 int col;
7170 /* Check for illegal values (could be wrong when screen was resized). */
7171 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
7172 return;
7174 /* Outputting the last character on the screen may scrollup the screen.
7175 * Don't to it! Mark the character invalid (update it when scrolled up) */
7176 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
7178 ScreenAttrs[off] = (sattr_T)-1;
7179 return;
7182 /* Output the first byte normally (positions the cursor), then write the
7183 * second byte directly. */
7184 screen_char(off, row, col);
7185 out_char(ScreenLines[off + 1]);
7186 ++screen_cur_col;
7188 #endif
7190 #if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
7192 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
7193 * This uses the contents of ScreenLines[] and doesn't change it.
7195 void
7196 screen_draw_rectangle(row, col, height, width, invert)
7197 int row;
7198 int col;
7199 int height;
7200 int width;
7201 int invert;
7203 int r, c;
7204 int off;
7205 #ifdef FEAT_MBYTE
7206 int max_off;
7207 #endif
7209 /* Can't use ScreenLines unless initialized */
7210 if (ScreenLines == NULL)
7211 return;
7213 if (invert)
7214 screen_char_attr = HL_INVERSE;
7215 for (r = row; r < row + height; ++r)
7217 off = LineOffset[r];
7218 #ifdef FEAT_MBYTE
7219 max_off = off + screen_Columns;
7220 #endif
7221 for (c = col; c < col + width; ++c)
7223 #ifdef FEAT_MBYTE
7224 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
7226 screen_char_2(off + c, r, c);
7227 ++c;
7229 else
7230 #endif
7232 screen_char(off + c, r, c);
7233 #ifdef FEAT_MBYTE
7234 if (utf_off2cells(off + c, max_off) > 1)
7235 ++c;
7236 #endif
7240 screen_char_attr = 0;
7242 #endif
7244 #ifdef FEAT_VERTSPLIT
7246 * Redraw the characters for a vertically split window.
7248 static void
7249 redraw_block(row, end, wp)
7250 int row;
7251 int end;
7252 win_T *wp;
7254 int col;
7255 int width;
7257 # ifdef FEAT_CLIPBOARD
7258 clip_may_clear_selection(row, end - 1);
7259 # endif
7261 if (wp == NULL)
7263 col = 0;
7264 width = Columns;
7266 else
7268 col = wp->w_wincol;
7269 width = wp->w_width;
7271 screen_draw_rectangle(row, col, end - row, width, FALSE);
7273 #endif
7276 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
7277 * with character 'c1' in first column followed by 'c2' in the other columns.
7278 * Use attributes 'attr'.
7280 void
7281 screen_fill(start_row, end_row, start_col, end_col, c1, c2, attr)
7282 int start_row, end_row;
7283 int start_col, end_col;
7284 int c1, c2;
7285 int attr;
7287 int row;
7288 int col;
7289 int off;
7290 int end_off;
7291 int did_delete;
7292 int c;
7293 int norm_term;
7294 #if defined(FEAT_GUI) || defined(UNIX)
7295 int force_next = FALSE;
7296 #endif
7298 if (end_row > screen_Rows) /* safety check */
7299 end_row = screen_Rows;
7300 if (end_col > screen_Columns) /* safety check */
7301 end_col = screen_Columns;
7302 if (ScreenLines == NULL
7303 || start_row >= end_row
7304 || start_col >= end_col) /* nothing to do */
7305 return;
7307 /* it's a "normal" terminal when not in a GUI or cterm */
7308 norm_term = (
7309 #ifdef FEAT_GUI
7310 !gui.in_use &&
7311 #endif
7312 t_colors <= 1);
7313 for (row = start_row; row < end_row; ++row)
7315 #ifdef FEAT_MBYTE
7316 if (has_mbyte
7317 # ifdef FEAT_GUI
7318 && !gui.in_use
7319 # endif
7322 /* When drawing over the right halve of a double-wide char clear
7323 * out the left halve. When drawing over the left halve of a
7324 * double wide-char clear out the right halve. Only needed in a
7325 * terminal. */
7326 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
7327 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
7328 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
7329 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
7331 #endif
7333 * Try to use delete-line termcap code, when no attributes or in a
7334 * "normal" terminal, where a bold/italic space is just a
7335 * space.
7337 did_delete = FALSE;
7338 if (c2 == ' '
7339 && end_col == Columns
7340 && can_clear(T_CE)
7341 && (attr == 0
7342 || (norm_term
7343 && attr <= HL_ALL
7344 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
7347 * check if we really need to clear something
7349 col = start_col;
7350 if (c1 != ' ') /* don't clear first char */
7351 ++col;
7353 off = LineOffset[row] + col;
7354 end_off = LineOffset[row] + end_col;
7356 /* skip blanks (used often, keep it fast!) */
7357 #ifdef FEAT_MBYTE
7358 if (enc_utf8)
7359 while (off < end_off && ScreenLines[off] == ' '
7360 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
7361 ++off;
7362 else
7363 #endif
7364 while (off < end_off && ScreenLines[off] == ' '
7365 && ScreenAttrs[off] == 0)
7366 ++off;
7367 if (off < end_off) /* something to be cleared */
7369 col = off - LineOffset[row];
7370 screen_stop_highlight();
7371 term_windgoto(row, col);/* clear rest of this screen line */
7372 out_str(T_CE);
7373 screen_start(); /* don't know where cursor is now */
7374 col = end_col - col;
7375 while (col--) /* clear chars in ScreenLines */
7377 ScreenLines[off] = ' ';
7378 #ifdef FEAT_MBYTE
7379 if (enc_utf8)
7380 ScreenLinesUC[off] = 0;
7381 #endif
7382 ScreenAttrs[off] = 0;
7383 ++off;
7386 did_delete = TRUE; /* the chars are cleared now */
7389 off = LineOffset[row] + start_col;
7390 c = c1;
7391 for (col = start_col; col < end_col; ++col)
7393 if (ScreenLines[off] != c
7394 #ifdef FEAT_MBYTE
7395 || (enc_utf8 && (int)ScreenLinesUC[off]
7396 != (c >= 0x80 ? c : 0))
7397 #endif
7398 || ScreenAttrs[off] != attr
7399 #if defined(FEAT_GUI) || defined(UNIX)
7400 || force_next
7401 #endif
7404 #if defined(FEAT_GUI) || defined(UNIX)
7405 /* The bold trick may make a single row of pixels appear in
7406 * the next character. When a bold character is removed, the
7407 * next character should be redrawn too. This happens for our
7408 * own GUI and for some xterms. */
7409 if (
7410 # ifdef FEAT_GUI
7411 gui.in_use
7412 # endif
7413 # if defined(FEAT_GUI) && defined(UNIX)
7415 # endif
7416 # ifdef UNIX
7417 term_is_xterm
7418 # endif
7421 if (ScreenLines[off] != ' '
7422 && (ScreenAttrs[off] > HL_ALL
7423 || ScreenAttrs[off] & HL_BOLD))
7424 force_next = TRUE;
7425 else
7426 force_next = FALSE;
7428 #endif
7429 ScreenLines[off] = c;
7430 #ifdef FEAT_MBYTE
7431 if (enc_utf8)
7433 if (c >= 0x80)
7435 ScreenLinesUC[off] = c;
7436 ScreenLinesC[0][off] = 0;
7438 else
7439 ScreenLinesUC[off] = 0;
7441 #endif
7442 ScreenAttrs[off] = attr;
7443 if (!did_delete || c != ' ')
7444 screen_char(off, row, col);
7446 ++off;
7447 if (col == start_col)
7449 if (did_delete)
7450 break;
7451 c = c2;
7454 if (end_col == Columns)
7455 LineWraps[row] = FALSE;
7456 if (row == Rows - 1) /* overwritten the command line */
7458 redraw_cmdline = TRUE;
7459 if (c1 == ' ' && c2 == ' ')
7460 clear_cmdline = FALSE; /* command line has been cleared */
7461 if (start_col == 0)
7462 mode_displayed = FALSE; /* mode cleared or overwritten */
7468 * Check if there should be a delay. Used before clearing or redrawing the
7469 * screen or the command line.
7471 void
7472 check_for_delay(check_msg_scroll)
7473 int check_msg_scroll;
7475 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
7476 && !did_wait_return
7477 && emsg_silent == 0)
7479 out_flush();
7480 ui_delay(1000L, TRUE);
7481 emsg_on_display = FALSE;
7482 if (check_msg_scroll)
7483 msg_scroll = FALSE;
7488 * screen_valid - allocate screen buffers if size changed
7489 * If "clear" is TRUE: clear screen if it has been resized.
7490 * Returns TRUE if there is a valid screen to write to.
7491 * Returns FALSE when starting up and screen not initialized yet.
7494 screen_valid(clear)
7495 int clear;
7497 screenalloc(clear); /* allocate screen buffers if size changed */
7498 return (ScreenLines != NULL);
7502 * Resize the shell to Rows and Columns.
7503 * Allocate ScreenLines[] and associated items.
7505 * There may be some time between setting Rows and Columns and (re)allocating
7506 * ScreenLines[]. This happens when starting up and when (manually) changing
7507 * the shell size. Always use screen_Rows and screen_Columns to access items
7508 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
7509 * final size of the shell is needed.
7511 void
7512 screenalloc(clear)
7513 int clear;
7515 int new_row, old_row;
7516 #ifdef FEAT_GUI
7517 int old_Rows;
7518 #endif
7519 win_T *wp;
7520 int outofmem = FALSE;
7521 int len;
7522 schar_T *new_ScreenLines;
7523 #ifdef FEAT_MBYTE
7524 u8char_T *new_ScreenLinesUC = NULL;
7525 u8char_T *new_ScreenLinesC[MAX_MCO];
7526 schar_T *new_ScreenLines2 = NULL;
7527 int i;
7528 #endif
7529 sattr_T *new_ScreenAttrs;
7530 unsigned *new_LineOffset;
7531 char_u *new_LineWraps;
7532 #ifdef FEAT_WINDOWS
7533 short *new_TabPageIdxs;
7534 tabpage_T *tp;
7535 #endif
7536 static int entered = FALSE; /* avoid recursiveness */
7537 static int done_outofmem_msg = FALSE; /* did outofmem message */
7538 #ifdef FEAT_AUTOCMD
7539 int retry_count = 0;
7541 retry:
7542 #endif
7544 * Allocation of the screen buffers is done only when the size changes and
7545 * when Rows and Columns have been set and we have started doing full
7546 * screen stuff.
7548 if ((ScreenLines != NULL
7549 && Rows == screen_Rows
7550 && Columns == screen_Columns
7551 #ifdef FEAT_MBYTE
7552 && enc_utf8 == (ScreenLinesUC != NULL)
7553 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
7554 && p_mco == Screen_mco
7555 #endif
7557 || Rows == 0
7558 || Columns == 0
7559 || (!full_screen && ScreenLines == NULL))
7560 return;
7563 * It's possible that we produce an out-of-memory message below, which
7564 * will cause this function to be called again. To break the loop, just
7565 * return here.
7567 if (entered)
7568 return;
7569 entered = TRUE;
7572 * Note that the window sizes are updated before reallocating the arrays,
7573 * thus we must not redraw here!
7575 ++RedrawingDisabled;
7577 win_new_shellsize(); /* fit the windows in the new sized shell */
7579 comp_col(); /* recompute columns for shown command and ruler */
7582 * We're changing the size of the screen.
7583 * - Allocate new arrays for ScreenLines and ScreenAttrs.
7584 * - Move lines from the old arrays into the new arrays, clear extra
7585 * lines (unless the screen is going to be cleared).
7586 * - Free the old arrays.
7588 * If anything fails, make ScreenLines NULL, so we don't do anything!
7589 * Continuing with the old ScreenLines may result in a crash, because the
7590 * size is wrong.
7592 FOR_ALL_TAB_WINDOWS(tp, wp)
7593 win_free_lsize(wp);
7594 #ifdef FEAT_AUTOCMD
7595 if (aucmd_win != NULL)
7596 win_free_lsize(aucmd_win);
7597 #endif
7599 new_ScreenLines = (schar_T *)lalloc((long_u)(
7600 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7601 #ifdef FEAT_MBYTE
7602 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
7603 if (enc_utf8)
7605 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
7606 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
7607 for (i = 0; i < p_mco; ++i)
7608 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
7609 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
7611 if (enc_dbcs == DBCS_JPNU)
7612 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
7613 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7614 #endif
7615 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
7616 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
7617 new_LineOffset = (unsigned *)lalloc((long_u)(
7618 Rows * sizeof(unsigned)), FALSE);
7619 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
7620 #ifdef FEAT_WINDOWS
7621 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
7622 #endif
7624 FOR_ALL_TAB_WINDOWS(tp, wp)
7626 if (win_alloc_lines(wp) == FAIL)
7628 outofmem = TRUE;
7629 #ifdef FEAT_WINDOWS
7630 goto give_up;
7631 #endif
7634 #ifdef FEAT_AUTOCMD
7635 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
7636 && win_alloc_lines(aucmd_win) == FAIL)
7637 outofmem = TRUE;
7638 #endif
7639 #ifdef FEAT_WINDOWS
7640 give_up:
7641 #endif
7643 #ifdef FEAT_MBYTE
7644 for (i = 0; i < p_mco; ++i)
7645 if (new_ScreenLinesC[i] == NULL)
7646 break;
7647 #endif
7648 if (new_ScreenLines == NULL
7649 #ifdef FEAT_MBYTE
7650 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
7651 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
7652 #endif
7653 || new_ScreenAttrs == NULL
7654 || new_LineOffset == NULL
7655 || new_LineWraps == NULL
7656 #ifdef FEAT_WINDOWS
7657 || new_TabPageIdxs == NULL
7658 #endif
7659 || outofmem)
7661 if (ScreenLines != NULL || !done_outofmem_msg)
7663 /* guess the size */
7664 do_outofmem_msg((long_u)((Rows + 1) * Columns));
7666 /* Remember we did this to avoid getting outofmem messages over
7667 * and over again. */
7668 done_outofmem_msg = TRUE;
7670 vim_free(new_ScreenLines);
7671 new_ScreenLines = NULL;
7672 #ifdef FEAT_MBYTE
7673 vim_free(new_ScreenLinesUC);
7674 new_ScreenLinesUC = NULL;
7675 for (i = 0; i < p_mco; ++i)
7677 vim_free(new_ScreenLinesC[i]);
7678 new_ScreenLinesC[i] = NULL;
7680 vim_free(new_ScreenLines2);
7681 new_ScreenLines2 = NULL;
7682 #endif
7683 vim_free(new_ScreenAttrs);
7684 new_ScreenAttrs = NULL;
7685 vim_free(new_LineOffset);
7686 new_LineOffset = NULL;
7687 vim_free(new_LineWraps);
7688 new_LineWraps = NULL;
7689 #ifdef FEAT_WINDOWS
7690 vim_free(new_TabPageIdxs);
7691 new_TabPageIdxs = NULL;
7692 #endif
7694 else
7696 done_outofmem_msg = FALSE;
7698 for (new_row = 0; new_row < Rows; ++new_row)
7700 new_LineOffset[new_row] = new_row * Columns;
7701 new_LineWraps[new_row] = FALSE;
7704 * If the screen is not going to be cleared, copy as much as
7705 * possible from the old screen to the new one and clear the rest
7706 * (used when resizing the window at the "--more--" prompt or when
7707 * executing an external command, for the GUI).
7709 if (!clear)
7711 (void)vim_memset(new_ScreenLines + new_row * Columns,
7712 ' ', (size_t)Columns * sizeof(schar_T));
7713 #ifdef FEAT_MBYTE
7714 if (enc_utf8)
7716 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
7717 0, (size_t)Columns * sizeof(u8char_T));
7718 for (i = 0; i < p_mco; ++i)
7719 (void)vim_memset(new_ScreenLinesC[i]
7720 + new_row * Columns,
7721 0, (size_t)Columns * sizeof(u8char_T));
7723 if (enc_dbcs == DBCS_JPNU)
7724 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
7725 0, (size_t)Columns * sizeof(schar_T));
7726 #endif
7727 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
7728 0, (size_t)Columns * sizeof(sattr_T));
7729 old_row = new_row + (screen_Rows - Rows);
7730 if (old_row >= 0 && ScreenLines != NULL)
7732 if (screen_Columns < Columns)
7733 len = screen_Columns;
7734 else
7735 len = Columns;
7736 #ifdef FEAT_MBYTE
7737 /* When switching to utf-8 don't copy characters, they
7738 * may be invalid now. Also when p_mco changes. */
7739 if (!(enc_utf8 && ScreenLinesUC == NULL)
7740 && p_mco == Screen_mco)
7741 #endif
7742 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
7743 ScreenLines + LineOffset[old_row],
7744 (size_t)len * sizeof(schar_T));
7745 #ifdef FEAT_MBYTE
7746 if (enc_utf8 && ScreenLinesUC != NULL
7747 && p_mco == Screen_mco)
7749 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
7750 ScreenLinesUC + LineOffset[old_row],
7751 (size_t)len * sizeof(u8char_T));
7752 for (i = 0; i < p_mco; ++i)
7753 mch_memmove(new_ScreenLinesC[i]
7754 + new_LineOffset[new_row],
7755 ScreenLinesC[i] + LineOffset[old_row],
7756 (size_t)len * sizeof(u8char_T));
7758 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
7759 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
7760 ScreenLines2 + LineOffset[old_row],
7761 (size_t)len * sizeof(schar_T));
7762 #endif
7763 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
7764 ScreenAttrs + LineOffset[old_row],
7765 (size_t)len * sizeof(sattr_T));
7769 /* Use the last line of the screen for the current line. */
7770 current_ScreenLine = new_ScreenLines + Rows * Columns;
7773 free_screenlines();
7775 ScreenLines = new_ScreenLines;
7776 #ifdef FEAT_MBYTE
7777 ScreenLinesUC = new_ScreenLinesUC;
7778 for (i = 0; i < p_mco; ++i)
7779 ScreenLinesC[i] = new_ScreenLinesC[i];
7780 Screen_mco = p_mco;
7781 ScreenLines2 = new_ScreenLines2;
7782 #endif
7783 ScreenAttrs = new_ScreenAttrs;
7784 LineOffset = new_LineOffset;
7785 LineWraps = new_LineWraps;
7786 #ifdef FEAT_WINDOWS
7787 TabPageIdxs = new_TabPageIdxs;
7788 #endif
7790 /* It's important that screen_Rows and screen_Columns reflect the actual
7791 * size of ScreenLines[]. Set them before calling anything. */
7792 #ifdef FEAT_GUI
7793 old_Rows = screen_Rows;
7794 #endif
7795 screen_Rows = Rows;
7796 screen_Columns = Columns;
7798 must_redraw = CLEAR; /* need to clear the screen later */
7799 if (clear)
7800 screenclear2();
7802 #ifdef FEAT_GUI
7803 else if (gui.in_use
7804 && !gui.starting
7805 && ScreenLines != NULL
7806 && old_Rows != Rows)
7808 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
7810 * Adjust the position of the cursor, for when executing an external
7811 * command.
7813 if (msg_row >= Rows) /* Rows got smaller */
7814 msg_row = Rows - 1; /* put cursor at last row */
7815 else if (Rows > old_Rows) /* Rows got bigger */
7816 msg_row += Rows - old_Rows; /* put cursor in same place */
7817 if (msg_col >= Columns) /* Columns got smaller */
7818 msg_col = Columns - 1; /* put cursor at last column */
7820 #endif
7822 entered = FALSE;
7823 --RedrawingDisabled;
7825 #ifdef FEAT_AUTOCMD
7827 * Do not apply autocommands more than 3 times to avoid an endless loop
7828 * in case applying autocommands always changes Rows or Columns.
7830 if (starting == 0 && ++retry_count <= 3)
7832 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
7833 /* In rare cases, autocommands may have altered Rows or Columns,
7834 * jump back to check if we need to allocate the screen again. */
7835 goto retry;
7837 #endif
7840 void
7841 free_screenlines()
7843 #ifdef FEAT_MBYTE
7844 int i;
7846 vim_free(ScreenLinesUC);
7847 for (i = 0; i < Screen_mco; ++i)
7848 vim_free(ScreenLinesC[i]);
7849 vim_free(ScreenLines2);
7850 #endif
7851 vim_free(ScreenLines);
7852 vim_free(ScreenAttrs);
7853 vim_free(LineOffset);
7854 vim_free(LineWraps);
7855 #ifdef FEAT_WINDOWS
7856 vim_free(TabPageIdxs);
7857 #endif
7860 void
7861 screenclear()
7863 check_for_delay(FALSE);
7864 screenalloc(FALSE); /* allocate screen buffers if size changed */
7865 screenclear2(); /* clear the screen */
7868 static void
7869 screenclear2()
7871 int i;
7873 if (starting == NO_SCREEN || ScreenLines == NULL
7874 #ifdef FEAT_GUI
7875 || (gui.in_use && gui.starting)
7876 #endif
7878 return;
7880 #ifdef FEAT_GUI
7881 if (!gui.in_use)
7882 #endif
7883 screen_attr = -1; /* force setting the Normal colors */
7884 screen_stop_highlight(); /* don't want highlighting here */
7886 #ifdef FEAT_CLIPBOARD
7887 /* disable selection without redrawing it */
7888 clip_scroll_selection(9999);
7889 #endif
7891 /* blank out ScreenLines */
7892 for (i = 0; i < Rows; ++i)
7894 lineclear(LineOffset[i], (int)Columns);
7895 LineWraps[i] = FALSE;
7898 if (can_clear(T_CL))
7900 out_str(T_CL); /* clear the display */
7901 clear_cmdline = FALSE;
7902 mode_displayed = FALSE;
7904 else
7906 /* can't clear the screen, mark all chars with invalid attributes */
7907 for (i = 0; i < Rows; ++i)
7908 lineinvalid(LineOffset[i], (int)Columns);
7909 clear_cmdline = TRUE;
7912 screen_cleared = TRUE; /* can use contents of ScreenLines now */
7914 win_rest_invalid(firstwin);
7915 redraw_cmdline = TRUE;
7916 #ifdef FEAT_WINDOWS
7917 redraw_tabline = TRUE;
7918 #endif
7919 if (must_redraw == CLEAR) /* no need to clear again */
7920 must_redraw = NOT_VALID;
7921 compute_cmdrow();
7922 msg_row = cmdline_row; /* put cursor on last line for messages */
7923 msg_col = 0;
7924 screen_start(); /* don't know where cursor is now */
7925 msg_scrolled = 0; /* can't scroll back */
7926 msg_didany = FALSE;
7927 msg_didout = FALSE;
7931 * Clear one line in ScreenLines.
7933 static void
7934 lineclear(off, width)
7935 unsigned off;
7936 int width;
7938 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
7939 #ifdef FEAT_MBYTE
7940 if (enc_utf8)
7941 (void)vim_memset(ScreenLinesUC + off, 0,
7942 (size_t)width * sizeof(u8char_T));
7943 #endif
7944 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
7948 * Mark one line in ScreenLines invalid by setting the attributes to an
7949 * invalid value.
7951 static void
7952 lineinvalid(off, width)
7953 unsigned off;
7954 int width;
7956 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
7959 #ifdef FEAT_VERTSPLIT
7961 * Copy part of a Screenline for vertically split window "wp".
7963 static void
7964 linecopy(to, from, wp)
7965 int to;
7966 int from;
7967 win_T *wp;
7969 unsigned off_to = LineOffset[to] + wp->w_wincol;
7970 unsigned off_from = LineOffset[from] + wp->w_wincol;
7972 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
7973 wp->w_width * sizeof(schar_T));
7974 # ifdef FEAT_MBYTE
7975 if (enc_utf8)
7977 int i;
7979 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
7980 wp->w_width * sizeof(u8char_T));
7981 for (i = 0; i < p_mco; ++i)
7982 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
7983 wp->w_width * sizeof(u8char_T));
7985 if (enc_dbcs == DBCS_JPNU)
7986 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
7987 wp->w_width * sizeof(schar_T));
7988 # endif
7989 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
7990 wp->w_width * sizeof(sattr_T));
7992 #endif
7995 * Return TRUE if clearing with term string "p" would work.
7996 * It can't work when the string is empty or it won't set the right background.
7999 can_clear(p)
8000 char_u *p;
8002 return (*p != NUL && (t_colors <= 1
8003 #ifdef FEAT_GUI
8004 || gui.in_use
8005 #endif
8006 || cterm_normal_bg_color == 0 || *T_UT != NUL));
8010 * Reset cursor position. Use whenever cursor was moved because of outputting
8011 * something directly to the screen (shell commands) or a terminal control
8012 * code.
8014 void
8015 screen_start()
8017 screen_cur_row = screen_cur_col = 9999;
8021 * Move the cursor to position "row","col" in the screen.
8022 * This tries to find the most efficient way to move, minimizing the number of
8023 * characters sent to the terminal.
8025 void
8026 windgoto(row, col)
8027 int row;
8028 int col;
8030 sattr_T *p;
8031 int i;
8032 int plan;
8033 int cost;
8034 int wouldbe_col;
8035 int noinvcurs;
8036 char_u *bs;
8037 int goto_cost;
8038 int attr;
8040 #define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
8041 #define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
8043 #define PLAN_LE 1
8044 #define PLAN_CR 2
8045 #define PLAN_NL 3
8046 #define PLAN_WRITE 4
8047 /* Can't use ScreenLines unless initialized */
8048 if (ScreenLines == NULL)
8049 return;
8051 if (col != screen_cur_col || row != screen_cur_row)
8053 /* Check for valid position. */
8054 if (row < 0) /* window without text lines? */
8055 row = 0;
8056 if (row >= screen_Rows)
8057 row = screen_Rows - 1;
8058 if (col >= screen_Columns)
8059 col = screen_Columns - 1;
8061 /* check if no cursor movement is allowed in highlight mode */
8062 if (screen_attr && *T_MS == NUL)
8063 noinvcurs = HIGHL_COST;
8064 else
8065 noinvcurs = 0;
8066 goto_cost = GOTO_COST + noinvcurs;
8069 * Plan how to do the positioning:
8070 * 1. Use CR to move it to column 0, same row.
8071 * 2. Use T_LE to move it a few columns to the left.
8072 * 3. Use NL to move a few lines down, column 0.
8073 * 4. Move a few columns to the right with T_ND or by writing chars.
8075 * Don't do this if the cursor went beyond the last column, the cursor
8076 * position is unknown then (some terminals wrap, some don't )
8078 * First check if the highlighting attributes allow us to write
8079 * characters to move the cursor to the right.
8081 if (row >= screen_cur_row && screen_cur_col < Columns)
8084 * If the cursor is in the same row, bigger col, we can use CR
8085 * or T_LE.
8087 bs = NULL; /* init for GCC */
8088 attr = screen_attr;
8089 if (row == screen_cur_row && col < screen_cur_col)
8091 /* "le" is preferred over "bc", because "bc" is obsolete */
8092 if (*T_LE)
8093 bs = T_LE; /* "cursor left" */
8094 else
8095 bs = T_BC; /* "backspace character (old) */
8096 if (*bs)
8097 cost = (screen_cur_col - col) * (int)STRLEN(bs);
8098 else
8099 cost = 999;
8100 if (col + 1 < cost) /* using CR is less characters */
8102 plan = PLAN_CR;
8103 wouldbe_col = 0;
8104 cost = 1; /* CR is just one character */
8106 else
8108 plan = PLAN_LE;
8109 wouldbe_col = col;
8111 if (noinvcurs) /* will stop highlighting */
8113 cost += noinvcurs;
8114 attr = 0;
8119 * If the cursor is above where we want to be, we can use CR LF.
8121 else if (row > screen_cur_row)
8123 plan = PLAN_NL;
8124 wouldbe_col = 0;
8125 cost = (row - screen_cur_row) * 2; /* CR LF */
8126 if (noinvcurs) /* will stop highlighting */
8128 cost += noinvcurs;
8129 attr = 0;
8134 * If the cursor is in the same row, smaller col, just use write.
8136 else
8138 plan = PLAN_WRITE;
8139 wouldbe_col = screen_cur_col;
8140 cost = 0;
8144 * Check if any characters that need to be written have the
8145 * correct attributes. Also avoid UTF-8 characters.
8147 i = col - wouldbe_col;
8148 if (i > 0)
8149 cost += i;
8150 if (cost < goto_cost && i > 0)
8153 * Check if the attributes are correct without additionally
8154 * stopping highlighting.
8156 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
8157 while (i && *p++ == attr)
8158 --i;
8159 if (i != 0)
8162 * Try if it works when highlighting is stopped here.
8164 if (*--p == 0)
8166 cost += noinvcurs;
8167 while (i && *p++ == 0)
8168 --i;
8170 if (i != 0)
8171 cost = 999; /* different attributes, don't do it */
8173 #ifdef FEAT_MBYTE
8174 if (enc_utf8)
8176 /* Don't use an UTF-8 char for positioning, it's slow. */
8177 for (i = wouldbe_col; i < col; ++i)
8178 if (ScreenLinesUC[LineOffset[row] + i] != 0)
8180 cost = 999;
8181 break;
8184 #endif
8188 * We can do it without term_windgoto()!
8190 if (cost < goto_cost)
8192 if (plan == PLAN_LE)
8194 if (noinvcurs)
8195 screen_stop_highlight();
8196 while (screen_cur_col > col)
8198 out_str(bs);
8199 --screen_cur_col;
8202 else if (plan == PLAN_CR)
8204 if (noinvcurs)
8205 screen_stop_highlight();
8206 out_char('\r');
8207 screen_cur_col = 0;
8209 else if (plan == PLAN_NL)
8211 if (noinvcurs)
8212 screen_stop_highlight();
8213 while (screen_cur_row < row)
8215 out_char('\n');
8216 ++screen_cur_row;
8218 screen_cur_col = 0;
8221 i = col - screen_cur_col;
8222 if (i > 0)
8225 * Use cursor-right if it's one character only. Avoids
8226 * removing a line of pixels from the last bold char, when
8227 * using the bold trick in the GUI.
8229 if (T_ND[0] != NUL && T_ND[1] == NUL)
8231 while (i-- > 0)
8232 out_char(*T_ND);
8234 else
8236 int off;
8238 off = LineOffset[row] + screen_cur_col;
8239 while (i-- > 0)
8241 if (ScreenAttrs[off] != screen_attr)
8242 screen_stop_highlight();
8243 #ifdef FEAT_MBYTE
8244 out_flush_check();
8245 #endif
8246 out_char(ScreenLines[off]);
8247 #ifdef FEAT_MBYTE
8248 if (enc_dbcs == DBCS_JPNU
8249 && ScreenLines[off] == 0x8e)
8250 out_char(ScreenLines2[off]);
8251 #endif
8252 ++off;
8258 else
8259 cost = 999;
8261 if (cost >= goto_cost)
8263 if (noinvcurs)
8264 screen_stop_highlight();
8265 if (row == screen_cur_row && (col > screen_cur_col) &&
8266 *T_CRI != NUL)
8267 term_cursor_right(col - screen_cur_col);
8268 else
8269 term_windgoto(row, col);
8271 screen_cur_row = row;
8272 screen_cur_col = col;
8277 * Set cursor to its position in the current window.
8279 void
8280 setcursor()
8282 if (redrawing())
8284 validate_cursor();
8285 windgoto(W_WINROW(curwin) + curwin->w_wrow,
8286 W_WINCOL(curwin) + (
8287 #ifdef FEAT_RIGHTLEFT
8288 /* With 'rightleft' set and the cursor on a double-wide
8289 * character, position it on the leftmost column. */
8290 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
8291 # ifdef FEAT_MBYTE
8292 (has_mbyte
8293 && (*mb_ptr2cells)(ml_get_cursor()) == 2
8294 && vim_isprintc(gchar_cursor())) ? 2 :
8295 # endif
8296 1)) :
8297 #endif
8298 curwin->w_wcol));
8304 * insert 'line_count' lines at 'row' in window 'wp'
8305 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
8306 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
8307 * scrolling.
8308 * Returns FAIL if the lines are not inserted, OK for success.
8311 win_ins_lines(wp, row, line_count, invalid, mayclear)
8312 win_T *wp;
8313 int row;
8314 int line_count;
8315 int invalid;
8316 int mayclear;
8318 int did_delete;
8319 int nextrow;
8320 int lastrow;
8321 int retval;
8323 if (invalid)
8324 wp->w_lines_valid = 0;
8326 if (wp->w_height < 5)
8327 return FAIL;
8329 if (line_count > wp->w_height - row)
8330 line_count = wp->w_height - row;
8332 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
8333 if (retval != MAYBE)
8334 return retval;
8337 * If there is a next window or a status line, we first try to delete the
8338 * lines at the bottom to avoid messing what is after the window.
8339 * If this fails and there are following windows, don't do anything to avoid
8340 * messing up those windows, better just redraw.
8342 did_delete = FALSE;
8343 #ifdef FEAT_WINDOWS
8344 if (wp->w_next != NULL || wp->w_status_height)
8346 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8347 line_count, (int)Rows, FALSE, NULL) == OK)
8348 did_delete = TRUE;
8349 else if (wp->w_next)
8350 return FAIL;
8352 #endif
8354 * if no lines deleted, blank the lines that will end up below the window
8356 if (!did_delete)
8358 #ifdef FEAT_WINDOWS
8359 wp->w_redr_status = TRUE;
8360 #endif
8361 redraw_cmdline = TRUE;
8362 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
8363 lastrow = nextrow + line_count;
8364 if (lastrow > Rows)
8365 lastrow = Rows;
8366 screen_fill(nextrow - line_count, lastrow - line_count,
8367 W_WINCOL(wp), (int)W_ENDCOL(wp),
8368 ' ', ' ', 0);
8371 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
8372 == FAIL)
8374 /* deletion will have messed up other windows */
8375 if (did_delete)
8377 #ifdef FEAT_WINDOWS
8378 wp->w_redr_status = TRUE;
8379 #endif
8380 win_rest_invalid(W_NEXT(wp));
8382 return FAIL;
8385 return OK;
8389 * delete "line_count" window lines at "row" in window "wp"
8390 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
8391 * If "mayclear" is TRUE the screen will be cleared if it is faster than
8392 * scrolling
8393 * Return OK for success, FAIL if the lines are not deleted.
8396 win_del_lines(wp, row, line_count, invalid, mayclear)
8397 win_T *wp;
8398 int row;
8399 int line_count;
8400 int invalid;
8401 int mayclear;
8403 int retval;
8405 if (invalid)
8406 wp->w_lines_valid = 0;
8408 if (line_count > wp->w_height - row)
8409 line_count = wp->w_height - row;
8411 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
8412 if (retval != MAYBE)
8413 return retval;
8415 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
8416 (int)Rows, FALSE, NULL) == FAIL)
8417 return FAIL;
8419 #ifdef FEAT_WINDOWS
8421 * If there are windows or status lines below, try to put them at the
8422 * correct place. If we can't do that, they have to be redrawn.
8424 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
8426 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8427 line_count, (int)Rows, NULL) == FAIL)
8429 wp->w_redr_status = TRUE;
8430 win_rest_invalid(wp->w_next);
8434 * If this is the last window and there is no status line, redraw the
8435 * command line later.
8437 else
8438 #endif
8439 redraw_cmdline = TRUE;
8440 return OK;
8444 * Common code for win_ins_lines() and win_del_lines().
8445 * Returns OK or FAIL when the work has been done.
8446 * Returns MAYBE when not finished yet.
8448 static int
8449 win_do_lines(wp, row, line_count, mayclear, del)
8450 win_T *wp;
8451 int row;
8452 int line_count;
8453 int mayclear;
8454 int del;
8456 int retval;
8458 if (!redrawing() || line_count <= 0)
8459 return FAIL;
8461 /* only a few lines left: redraw is faster */
8462 if (mayclear && Rows - line_count < 5
8463 #ifdef FEAT_VERTSPLIT
8464 && wp->w_width == Columns
8465 #endif
8468 screenclear(); /* will set wp->w_lines_valid to 0 */
8469 return FAIL;
8473 * Delete all remaining lines
8475 if (row + line_count >= wp->w_height)
8477 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
8478 W_WINCOL(wp), (int)W_ENDCOL(wp),
8479 ' ', ' ', 0);
8480 return OK;
8484 * when scrolling, the message on the command line should be cleared,
8485 * otherwise it will stay there forever.
8487 clear_cmdline = TRUE;
8490 * If the terminal can set a scroll region, use that.
8491 * Always do this in a vertically split window. This will redraw from
8492 * ScreenLines[] when t_CV isn't defined. That's faster than using
8493 * win_line().
8494 * Don't use a scroll region when we are going to redraw the text, writing
8495 * a character in the lower right corner of the scroll region causes a
8496 * scroll-up in the DJGPP version.
8498 if (scroll_region
8499 #ifdef FEAT_VERTSPLIT
8500 || W_WIDTH(wp) != Columns
8501 #endif
8504 #ifdef FEAT_VERTSPLIT
8505 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8506 #endif
8507 scroll_region_set(wp, row);
8508 if (del)
8509 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
8510 wp->w_height - row, FALSE, wp);
8511 else
8512 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
8513 wp->w_height - row, wp);
8514 #ifdef FEAT_VERTSPLIT
8515 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8516 #endif
8517 scroll_region_reset();
8518 return retval;
8521 #ifdef FEAT_WINDOWS
8522 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
8523 return FAIL;
8524 #endif
8526 return MAYBE;
8530 * window 'wp' and everything after it is messed up, mark it for redraw
8532 static void
8533 win_rest_invalid(wp)
8534 win_T *wp;
8536 #ifdef FEAT_WINDOWS
8537 while (wp != NULL)
8538 #else
8539 if (wp != NULL)
8540 #endif
8542 redraw_win_later(wp, NOT_VALID);
8543 #ifdef FEAT_WINDOWS
8544 wp->w_redr_status = TRUE;
8545 wp = wp->w_next;
8546 #endif
8548 redraw_cmdline = TRUE;
8552 * The rest of the routines in this file perform screen manipulations. The
8553 * given operation is performed physically on the screen. The corresponding
8554 * change is also made to the internal screen image. In this way, the editor
8555 * anticipates the effect of editing changes on the appearance of the screen.
8556 * That way, when we call screenupdate a complete redraw isn't usually
8557 * necessary. Another advantage is that we can keep adding code to anticipate
8558 * screen changes, and in the meantime, everything still works.
8562 * types for inserting or deleting lines
8564 #define USE_T_CAL 1
8565 #define USE_T_CDL 2
8566 #define USE_T_AL 3
8567 #define USE_T_CE 4
8568 #define USE_T_DL 5
8569 #define USE_T_SR 6
8570 #define USE_NL 7
8571 #define USE_T_CD 8
8572 #define USE_REDRAW 9
8575 * insert lines on the screen and update ScreenLines[]
8576 * 'end' is the line after the scrolled part. Normally it is Rows.
8577 * When scrolling region used 'off' is the offset from the top for the region.
8578 * 'row' and 'end' are relative to the start of the region.
8580 * return FAIL for failure, OK for success.
8583 screen_ins_lines(off, row, line_count, end, wp)
8584 int off;
8585 int row;
8586 int line_count;
8587 int end;
8588 win_T *wp; /* NULL or window to use width from */
8590 int i;
8591 int j;
8592 unsigned temp;
8593 int cursor_row;
8594 int type;
8595 int result_empty;
8596 int can_ce = can_clear(T_CE);
8599 * FAIL if
8600 * - there is no valid screen
8601 * - the screen has to be redrawn completely
8602 * - the line count is less than one
8603 * - the line count is more than 'ttyscroll'
8605 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
8606 return FAIL;
8609 * There are seven ways to insert lines:
8610 * 0. When in a vertically split window and t_CV isn't set, redraw the
8611 * characters from ScreenLines[].
8612 * 1. Use T_CD (clear to end of display) if it exists and the result of
8613 * the insert is just empty lines
8614 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
8615 * present or line_count > 1. It looks better if we do all the inserts
8616 * at once.
8617 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
8618 * insert is just empty lines and T_CE is not present or line_count >
8619 * 1.
8620 * 4. Use T_AL (insert line) if it exists.
8621 * 5. Use T_CE (erase line) if it exists and the result of the insert is
8622 * just empty lines.
8623 * 6. Use T_DL (delete line) if it exists and the result of the insert is
8624 * just empty lines.
8625 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
8626 * the 'da' flag is not set or we have clear line capability.
8627 * 8. redraw the characters from ScreenLines[].
8629 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
8630 * the scrollbar for the window. It does have insert line, use that if it
8631 * exists.
8633 result_empty = (row + line_count >= end);
8634 #ifdef FEAT_VERTSPLIT
8635 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
8636 type = USE_REDRAW;
8637 else
8638 #endif
8639 if (can_clear(T_CD) && result_empty)
8640 type = USE_T_CD;
8641 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
8642 type = USE_T_CAL;
8643 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
8644 type = USE_T_CDL;
8645 else if (*T_AL != NUL)
8646 type = USE_T_AL;
8647 else if (can_ce && result_empty)
8648 type = USE_T_CE;
8649 else if (*T_DL != NUL && result_empty)
8650 type = USE_T_DL;
8651 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
8652 type = USE_T_SR;
8653 else
8654 return FAIL;
8657 * For clearing the lines screen_del_lines() is used. This will also take
8658 * care of t_db if necessary.
8660 if (type == USE_T_CD || type == USE_T_CDL ||
8661 type == USE_T_CE || type == USE_T_DL)
8662 return screen_del_lines(off, row, line_count, end, FALSE, wp);
8665 * If text is retained below the screen, first clear or delete as many
8666 * lines at the bottom of the window as are about to be inserted so that
8667 * the deleted lines won't later surface during a screen_del_lines.
8669 if (*T_DB)
8670 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
8672 #ifdef FEAT_CLIPBOARD
8673 /* Remove a modeless selection when inserting lines halfway the screen
8674 * or not the full width of the screen. */
8675 if (off + row > 0
8676 # ifdef FEAT_VERTSPLIT
8677 || (wp != NULL && wp->w_width != Columns)
8678 # endif
8680 clip_clear_selection();
8681 else
8682 clip_scroll_selection(-line_count);
8683 #endif
8685 #ifdef FEAT_GUI
8686 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
8687 * scrolling is actually carried out. */
8688 gui_dont_update_cursor();
8689 #endif
8691 if (*T_CCS != NUL) /* cursor relative to region */
8692 cursor_row = row;
8693 else
8694 cursor_row = row + off;
8697 * Shift LineOffset[] line_count down to reflect the inserted lines.
8698 * Clear the inserted lines in ScreenLines[].
8700 row += off;
8701 end += off;
8702 for (i = 0; i < line_count; ++i)
8704 #ifdef FEAT_VERTSPLIT
8705 if (wp != NULL && wp->w_width != Columns)
8707 /* need to copy part of a line */
8708 j = end - 1 - i;
8709 while ((j -= line_count) >= row)
8710 linecopy(j + line_count, j, wp);
8711 j += line_count;
8712 if (can_clear((char_u *)" "))
8713 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
8714 else
8715 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
8716 LineWraps[j] = FALSE;
8718 else
8719 #endif
8721 j = end - 1 - i;
8722 temp = LineOffset[j];
8723 while ((j -= line_count) >= row)
8725 LineOffset[j + line_count] = LineOffset[j];
8726 LineWraps[j + line_count] = LineWraps[j];
8728 LineOffset[j + line_count] = temp;
8729 LineWraps[j + line_count] = FALSE;
8730 if (can_clear((char_u *)" "))
8731 lineclear(temp, (int)Columns);
8732 else
8733 lineinvalid(temp, (int)Columns);
8737 screen_stop_highlight();
8738 windgoto(cursor_row, 0);
8740 #ifdef FEAT_VERTSPLIT
8741 /* redraw the characters */
8742 if (type == USE_REDRAW)
8743 redraw_block(row, end, wp);
8744 else
8745 #endif
8746 if (type == USE_T_CAL)
8748 term_append_lines(line_count);
8749 screen_start(); /* don't know where cursor is now */
8751 else
8753 for (i = 0; i < line_count; i++)
8755 if (type == USE_T_AL)
8757 if (i && cursor_row != 0)
8758 windgoto(cursor_row, 0);
8759 out_str(T_AL);
8761 else /* type == USE_T_SR */
8762 out_str(T_SR);
8763 screen_start(); /* don't know where cursor is now */
8768 * With scroll-reverse and 'da' flag set we need to clear the lines that
8769 * have been scrolled down into the region.
8771 if (type == USE_T_SR && *T_DA)
8773 for (i = 0; i < line_count; ++i)
8775 windgoto(off + i, 0);
8776 out_str(T_CE);
8777 screen_start(); /* don't know where cursor is now */
8781 #ifdef FEAT_GUI
8782 gui_can_update_cursor();
8783 if (gui.in_use)
8784 out_flush(); /* always flush after a scroll */
8785 #endif
8786 return OK;
8790 * delete lines on the screen and update ScreenLines[]
8791 * 'end' is the line after the scrolled part. Normally it is Rows.
8792 * When scrolling region used 'off' is the offset from the top for the region.
8793 * 'row' and 'end' are relative to the start of the region.
8795 * Return OK for success, FAIL if the lines are not deleted.
8798 screen_del_lines(off, row, line_count, end, force, wp)
8799 int off;
8800 int row;
8801 int line_count;
8802 int end;
8803 int force; /* even when line_count > p_ttyscroll */
8804 win_T *wp UNUSED; /* NULL or window to use width from */
8806 int j;
8807 int i;
8808 unsigned temp;
8809 int cursor_row;
8810 int cursor_end;
8811 int result_empty; /* result is empty until end of region */
8812 int can_delete; /* deleting line codes can be used */
8813 int type;
8816 * FAIL if
8817 * - there is no valid screen
8818 * - the screen has to be redrawn completely
8819 * - the line count is less than one
8820 * - the line count is more than 'ttyscroll'
8822 if (!screen_valid(TRUE) || line_count <= 0 ||
8823 (!force && line_count > p_ttyscroll))
8824 return FAIL;
8827 * Check if the rest of the current region will become empty.
8829 result_empty = row + line_count >= end;
8832 * We can delete lines only when 'db' flag not set or when 'ce' option
8833 * available.
8835 can_delete = (*T_DB == NUL || can_clear(T_CE));
8838 * There are six ways to delete lines:
8839 * 0. When in a vertically split window and t_CV isn't set, redraw the
8840 * characters from ScreenLines[].
8841 * 1. Use T_CD if it exists and the result is empty.
8842 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
8843 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
8844 * none of the other ways work.
8845 * 4. Use T_CE (erase line) if the result is empty.
8846 * 5. Use T_DL (delete line) if it exists.
8847 * 6. redraw the characters from ScreenLines[].
8849 #ifdef FEAT_VERTSPLIT
8850 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
8851 type = USE_REDRAW;
8852 else
8853 #endif
8854 if (can_clear(T_CD) && result_empty)
8855 type = USE_T_CD;
8856 #if defined(__BEOS__) && defined(BEOS_DR8)
8858 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
8859 * its internal termcap... this works okay for tests which test *T_DB !=
8860 * NUL. It has the disadvantage that the user cannot use any :set t_*
8861 * command to get T_DB (back) to empty_option, only :set term=... will do
8862 * the trick...
8863 * Anyway, this hack will hopefully go away with the next OS release.
8864 * (Olaf Seibert)
8866 else if (row == 0 && T_DB == empty_option
8867 && (line_count == 1 || *T_CDL == NUL))
8868 #else
8869 else if (row == 0 && (
8870 #ifndef AMIGA
8871 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
8872 * up, so use delete-line command */
8873 line_count == 1 ||
8874 #endif
8875 *T_CDL == NUL))
8876 #endif
8877 type = USE_NL;
8878 else if (*T_CDL != NUL && line_count > 1 && can_delete)
8879 type = USE_T_CDL;
8880 else if (can_clear(T_CE) && result_empty
8881 #ifdef FEAT_VERTSPLIT
8882 && (wp == NULL || wp->w_width == Columns)
8883 #endif
8885 type = USE_T_CE;
8886 else if (*T_DL != NUL && can_delete)
8887 type = USE_T_DL;
8888 else if (*T_CDL != NUL && can_delete)
8889 type = USE_T_CDL;
8890 else
8891 return FAIL;
8893 #ifdef FEAT_CLIPBOARD
8894 /* Remove a modeless selection when deleting lines halfway the screen or
8895 * not the full width of the screen. */
8896 if (off + row > 0
8897 # ifdef FEAT_VERTSPLIT
8898 || (wp != NULL && wp->w_width != Columns)
8899 # endif
8901 clip_clear_selection();
8902 else
8903 clip_scroll_selection(line_count);
8904 #endif
8906 #ifdef FEAT_GUI
8907 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
8908 * scrolling is actually carried out. */
8909 gui_dont_update_cursor();
8910 #endif
8912 if (*T_CCS != NUL) /* cursor relative to region */
8914 cursor_row = row;
8915 cursor_end = end;
8917 else
8919 cursor_row = row + off;
8920 cursor_end = end + off;
8924 * Now shift LineOffset[] line_count up to reflect the deleted lines.
8925 * Clear the inserted lines in ScreenLines[].
8927 row += off;
8928 end += off;
8929 for (i = 0; i < line_count; ++i)
8931 #ifdef FEAT_VERTSPLIT
8932 if (wp != NULL && wp->w_width != Columns)
8934 /* need to copy part of a line */
8935 j = row + i;
8936 while ((j += line_count) <= end - 1)
8937 linecopy(j - line_count, j, wp);
8938 j -= line_count;
8939 if (can_clear((char_u *)" "))
8940 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
8941 else
8942 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
8943 LineWraps[j] = FALSE;
8945 else
8946 #endif
8948 /* whole width, moving the line pointers is faster */
8949 j = row + i;
8950 temp = LineOffset[j];
8951 while ((j += line_count) <= end - 1)
8953 LineOffset[j - line_count] = LineOffset[j];
8954 LineWraps[j - line_count] = LineWraps[j];
8956 LineOffset[j - line_count] = temp;
8957 LineWraps[j - line_count] = FALSE;
8958 if (can_clear((char_u *)" "))
8959 lineclear(temp, (int)Columns);
8960 else
8961 lineinvalid(temp, (int)Columns);
8965 screen_stop_highlight();
8967 #ifdef FEAT_VERTSPLIT
8968 /* redraw the characters */
8969 if (type == USE_REDRAW)
8970 redraw_block(row, end, wp);
8971 else
8972 #endif
8973 if (type == USE_T_CD) /* delete the lines */
8975 windgoto(cursor_row, 0);
8976 out_str(T_CD);
8977 screen_start(); /* don't know where cursor is now */
8979 else if (type == USE_T_CDL)
8981 windgoto(cursor_row, 0);
8982 term_delete_lines(line_count);
8983 screen_start(); /* don't know where cursor is now */
8986 * Deleting lines at top of the screen or scroll region: Just scroll
8987 * the whole screen (scroll region) up by outputting newlines on the
8988 * last line.
8990 else if (type == USE_NL)
8992 windgoto(cursor_end - 1, 0);
8993 for (i = line_count; --i >= 0; )
8994 out_char('\n'); /* cursor will remain on same line */
8996 else
8998 for (i = line_count; --i >= 0; )
9000 if (type == USE_T_DL)
9002 windgoto(cursor_row, 0);
9003 out_str(T_DL); /* delete a line */
9005 else /* type == USE_T_CE */
9007 windgoto(cursor_row + i, 0);
9008 out_str(T_CE); /* erase a line */
9010 screen_start(); /* don't know where cursor is now */
9015 * If the 'db' flag is set, we need to clear the lines that have been
9016 * scrolled up at the bottom of the region.
9018 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
9020 for (i = line_count; i > 0; --i)
9022 windgoto(cursor_end - i, 0);
9023 out_str(T_CE); /* erase a line */
9024 screen_start(); /* don't know where cursor is now */
9028 #ifdef FEAT_GUI
9029 gui_can_update_cursor();
9030 if (gui.in_use)
9031 out_flush(); /* always flush after a scroll */
9032 #endif
9034 return OK;
9038 * show the current mode and ruler
9040 * If clear_cmdline is TRUE, clear the rest of the cmdline.
9041 * If clear_cmdline is FALSE there may be a message there that needs to be
9042 * cleared only if a mode is shown.
9043 * Return the length of the message (0 if no message).
9046 showmode()
9048 int need_clear;
9049 int length = 0;
9050 int do_mode;
9051 int attr;
9052 int nwr_save;
9053 #ifdef FEAT_INS_EXPAND
9054 int sub_attr;
9055 #endif
9057 do_mode = ((p_smd && msg_silent == 0)
9058 && ((State & INSERT)
9059 || restart_edit
9060 #ifdef FEAT_VISUAL
9061 || VIsual_active
9062 #endif
9064 if (do_mode || Recording)
9067 * Don't show mode right now, when not redrawing or inside a mapping.
9068 * Call char_avail() only when we are going to show something, because
9069 * it takes a bit of time.
9071 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
9073 redraw_cmdline = TRUE; /* show mode later */
9074 return 0;
9077 nwr_save = need_wait_return;
9079 /* wait a bit before overwriting an important message */
9080 check_for_delay(FALSE);
9082 /* if the cmdline is more than one line high, erase top lines */
9083 need_clear = clear_cmdline;
9084 if (clear_cmdline && cmdline_row < Rows - 1)
9085 msg_clr_cmdline(); /* will reset clear_cmdline */
9087 /* Position on the last line in the window, column 0 */
9088 msg_pos_mode();
9089 cursor_off();
9090 attr = hl_attr(HLF_CM); /* Highlight mode */
9091 if (do_mode)
9093 MSG_PUTS_ATTR("--", attr);
9094 #if defined(FEAT_XIM)
9095 # if 0 /* old version, changed by SungHyun Nam July 2008 */
9096 if (xic != NULL && im_get_status() && !p_imdisable
9097 && curbuf->b_p_iminsert == B_IMODE_IM)
9098 # else
9099 if (
9100 # ifdef HAVE_GTK2
9101 preedit_get_status()
9102 # else
9103 im_get_status()
9104 # endif
9106 # endif
9107 # ifdef HAVE_GTK2 /* most of the time, it's not XIM being used */
9108 MSG_PUTS_ATTR(" IM", attr);
9109 # else
9110 MSG_PUTS_ATTR(" XIM", attr);
9111 # endif
9112 #endif
9113 #if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
9114 if (gui.in_use)
9116 if (hangul_input_state_get())
9117 MSG_PUTS_ATTR(" \307\321\261\333", attr); /* HANGUL */
9119 #endif
9120 #ifdef FEAT_INS_EXPAND
9121 if (edit_submode != NULL) /* CTRL-X in Insert mode */
9123 /* These messages can get long, avoid a wrap in a narrow
9124 * window. Prefer showing edit_submode_extra. */
9125 length = (Rows - msg_row) * Columns - 3;
9126 if (edit_submode_extra != NULL)
9127 length -= vim_strsize(edit_submode_extra);
9128 if (length > 0)
9130 if (edit_submode_pre != NULL)
9131 length -= vim_strsize(edit_submode_pre);
9132 if (length - vim_strsize(edit_submode) > 0)
9134 if (edit_submode_pre != NULL)
9135 msg_puts_attr(edit_submode_pre, attr);
9136 msg_puts_attr(edit_submode, attr);
9138 if (edit_submode_extra != NULL)
9140 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
9141 if ((int)edit_submode_highl < (int)HLF_COUNT)
9142 sub_attr = hl_attr(edit_submode_highl);
9143 else
9144 sub_attr = attr;
9145 msg_puts_attr(edit_submode_extra, sub_attr);
9148 length = 0;
9150 else
9151 #endif
9153 #ifdef FEAT_VREPLACE
9154 if (State & VREPLACE_FLAG)
9155 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
9156 else
9157 #endif
9158 if (State & REPLACE_FLAG)
9159 MSG_PUTS_ATTR(_(" REPLACE"), attr);
9160 else if (State & INSERT)
9162 #ifdef FEAT_RIGHTLEFT
9163 if (p_ri)
9164 MSG_PUTS_ATTR(_(" REVERSE"), attr);
9165 #endif
9166 MSG_PUTS_ATTR(_(" INSERT"), attr);
9168 else if (restart_edit == 'I')
9169 MSG_PUTS_ATTR(_(" (insert)"), attr);
9170 else if (restart_edit == 'R')
9171 MSG_PUTS_ATTR(_(" (replace)"), attr);
9172 else if (restart_edit == 'V')
9173 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
9174 #ifdef FEAT_RIGHTLEFT
9175 if (p_hkmap)
9176 MSG_PUTS_ATTR(_(" Hebrew"), attr);
9177 # ifdef FEAT_FKMAP
9178 if (p_fkmap)
9179 MSG_PUTS_ATTR(farsi_text_5, attr);
9180 # endif
9181 #endif
9182 #ifdef FEAT_KEYMAP
9183 if (State & LANGMAP)
9185 # ifdef FEAT_ARABIC
9186 if (curwin->w_p_arab)
9187 MSG_PUTS_ATTR(_(" Arabic"), attr);
9188 else
9189 # endif
9190 MSG_PUTS_ATTR(_(" (lang)"), attr);
9192 #endif
9193 if ((State & INSERT) && p_paste)
9194 MSG_PUTS_ATTR(_(" (paste)"), attr);
9196 #ifdef FEAT_VISUAL
9197 if (VIsual_active)
9199 char *p;
9201 /* Don't concatenate separate words to avoid translation
9202 * problems. */
9203 switch ((VIsual_select ? 4 : 0)
9204 + (VIsual_mode == Ctrl_V) * 2
9205 + (VIsual_mode == 'V'))
9207 case 0: p = N_(" VISUAL"); break;
9208 case 1: p = N_(" VISUAL LINE"); break;
9209 case 2: p = N_(" VISUAL BLOCK"); break;
9210 case 4: p = N_(" SELECT"); break;
9211 case 5: p = N_(" SELECT LINE"); break;
9212 default: p = N_(" SELECT BLOCK"); break;
9214 MSG_PUTS_ATTR(_(p), attr);
9216 #endif
9217 MSG_PUTS_ATTR(" --", attr);
9220 need_clear = TRUE;
9222 if (Recording
9223 #ifdef FEAT_INS_EXPAND
9224 && edit_submode == NULL /* otherwise it gets too long */
9225 #endif
9228 MSG_PUTS_ATTR(_("recording"), attr);
9229 need_clear = TRUE;
9232 mode_displayed = TRUE;
9233 if (need_clear || clear_cmdline)
9234 msg_clr_eos();
9235 msg_didout = FALSE; /* overwrite this message */
9236 length = msg_col;
9237 msg_col = 0;
9238 need_wait_return = nwr_save; /* never ask for hit-return for this */
9240 else if (clear_cmdline && msg_silent == 0)
9241 /* Clear the whole command line. Will reset "clear_cmdline". */
9242 msg_clr_cmdline();
9244 #ifdef FEAT_CMDL_INFO
9245 # ifdef FEAT_VISUAL
9246 /* In Visual mode the size of the selected area must be redrawn. */
9247 if (VIsual_active)
9248 clear_showcmd();
9249 # endif
9251 /* If the last window has no status line, the ruler is after the mode
9252 * message and must be redrawn */
9253 if (redrawing()
9254 # ifdef FEAT_WINDOWS
9255 && lastwin->w_status_height == 0
9256 # endif
9258 win_redr_ruler(lastwin, TRUE);
9259 #endif
9260 redraw_cmdline = FALSE;
9261 clear_cmdline = FALSE;
9263 return length;
9267 * Position for a mode message.
9269 static void
9270 msg_pos_mode()
9272 msg_col = 0;
9273 msg_row = Rows - 1;
9277 * Delete mode message. Used when ESC is typed which is expected to end
9278 * Insert mode (but Insert mode didn't end yet!).
9279 * Caller should check "mode_displayed".
9281 void
9282 unshowmode(force)
9283 int force;
9286 * Don't delete it right now, when not redrawing or inside a mapping.
9288 if (!redrawing() || (!force && char_avail() && !KeyTyped))
9289 redraw_cmdline = TRUE; /* delete mode later */
9290 else
9292 msg_pos_mode();
9293 if (Recording)
9294 MSG_PUTS_ATTR(_("recording"), hl_attr(HLF_CM));
9295 msg_clr_eos();
9299 #if defined(FEAT_WINDOWS)
9301 * Draw the tab pages line at the top of the Vim window.
9303 static void
9304 draw_tabline()
9306 int tabcount = 0;
9307 tabpage_T *tp;
9308 int tabwidth;
9309 int col = 0;
9310 int scol = 0;
9311 int attr;
9312 win_T *wp;
9313 win_T *cwp;
9314 int wincount;
9315 int modified;
9316 int c;
9317 int len;
9318 int attr_sel = hl_attr(HLF_TPS);
9319 int attr_nosel = hl_attr(HLF_TP);
9320 int attr_fill = hl_attr(HLF_TPF);
9321 char_u *p;
9322 int room;
9323 int use_sep_chars = (t_colors < 8
9324 #ifdef FEAT_GUI
9325 && !gui.in_use
9326 #endif
9329 redraw_tabline = FALSE;
9331 #ifdef FEAT_GUI_TABLINE
9332 /* Take care of a GUI tabline. */
9333 if (gui_use_tabline())
9335 gui_update_tabline();
9336 return;
9338 #endif
9340 if (tabline_height() < 1)
9341 return;
9343 #if defined(FEAT_STL_OPT)
9345 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
9346 for (scol = 0; scol < Columns; ++scol)
9347 TabPageIdxs[scol] = 0;
9349 /* Use the 'tabline' option if it's set. */
9350 if (*p_tal != NUL)
9352 int save_called_emsg = called_emsg;
9354 /* Check for an error. If there is one we would loop in redrawing the
9355 * screen. Avoid that by making 'tabline' empty. */
9356 called_emsg = FALSE;
9357 win_redr_custom(NULL, FALSE);
9358 if (called_emsg)
9359 set_string_option_direct((char_u *)"tabline", -1,
9360 (char_u *)"", OPT_FREE, SID_ERROR);
9361 called_emsg |= save_called_emsg;
9363 else
9364 #endif
9366 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
9367 ++tabcount;
9369 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
9370 if (tabwidth < 6)
9371 tabwidth = 6;
9373 attr = attr_nosel;
9374 tabcount = 0;
9375 scol = 0;
9376 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
9377 tp = tp->tp_next)
9379 scol = col;
9381 if (tp->tp_topframe == topframe)
9382 attr = attr_sel;
9383 if (use_sep_chars && col > 0)
9384 screen_putchar('|', 0, col++, attr);
9386 if (tp->tp_topframe != topframe)
9387 attr = attr_nosel;
9389 screen_putchar(' ', 0, col++, attr);
9391 if (tp == curtab)
9393 cwp = curwin;
9394 wp = firstwin;
9396 else
9398 cwp = tp->tp_curwin;
9399 wp = tp->tp_firstwin;
9402 modified = FALSE;
9403 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
9404 if (bufIsChanged(wp->w_buffer))
9405 modified = TRUE;
9406 if (modified || wincount > 1)
9408 if (wincount > 1)
9410 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
9411 len = (int)STRLEN(NameBuff);
9412 if (col + len >= Columns - 3)
9413 break;
9414 screen_puts_len(NameBuff, len, 0, col,
9415 #if defined(FEAT_SYN_HL)
9416 hl_combine_attr(attr, hl_attr(HLF_T))
9417 #else
9418 attr
9419 #endif
9421 col += len;
9423 if (modified)
9424 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
9425 screen_putchar(' ', 0, col++, attr);
9428 room = scol - col + tabwidth - 1;
9429 if (room > 0)
9431 /* Get buffer name in NameBuff[] */
9432 get_trans_bufname(cwp->w_buffer);
9433 shorten_dir(NameBuff);
9434 len = vim_strsize(NameBuff);
9435 p = NameBuff;
9436 #ifdef FEAT_MBYTE
9437 if (has_mbyte)
9438 while (len > room)
9440 len -= ptr2cells(p);
9441 mb_ptr_adv(p);
9443 else
9444 #endif
9445 if (len > room)
9447 p += len - room;
9448 len = room;
9450 if (len > Columns - col - 1)
9451 len = Columns - col - 1;
9453 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
9454 col += len;
9456 screen_putchar(' ', 0, col++, attr);
9458 /* Store the tab page number in TabPageIdxs[], so that
9459 * jump_to_mouse() knows where each one is. */
9460 ++tabcount;
9461 while (scol < col)
9462 TabPageIdxs[scol++] = tabcount;
9465 if (use_sep_chars)
9466 c = '_';
9467 else
9468 c = ' ';
9469 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
9471 /* Put an "X" for closing the current tab if there are several. */
9472 if (first_tabpage->tp_next != NULL)
9474 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
9475 TabPageIdxs[Columns - 1] = -999;
9479 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
9480 * set. */
9481 redraw_tabline = FALSE;
9485 * Get buffer name for "buf" into NameBuff[].
9486 * Takes care of special buffer names and translates special characters.
9488 void
9489 get_trans_bufname(buf)
9490 buf_T *buf;
9492 char_u *bname = (char_u *)buf_spname(buf);
9493 if (bname)
9495 vim_strncpy(NameBuff, bname, MAXPATHL - 1);
9496 vim_free(bname);
9498 else
9499 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
9500 trans_characters(NameBuff, MAXPATHL);
9502 #endif
9504 #if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
9506 * Get the character to use in a status line. Get its attributes in "*attr".
9508 static int
9509 fillchar_status(attr, is_curwin)
9510 int *attr;
9511 int is_curwin;
9513 int fill;
9514 if (is_curwin)
9516 *attr = hl_attr(HLF_S);
9517 fill = fill_stl;
9519 else
9521 *attr = hl_attr(HLF_SNC);
9522 fill = fill_stlnc;
9524 /* Use fill when there is highlighting, and highlighting of current
9525 * window differs, or the fillchars differ, or this is not the
9526 * current window */
9527 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
9528 || !is_curwin || firstwin == lastwin)
9529 || (fill_stl != fill_stlnc)))
9530 return fill;
9531 if (is_curwin)
9532 return '^';
9533 return '=';
9535 #endif
9537 #ifdef FEAT_VERTSPLIT
9539 * Get the character to use in a separator between vertically split windows.
9540 * Get its attributes in "*attr".
9542 static int
9543 fillchar_vsep(attr)
9544 int *attr;
9546 *attr = hl_attr(HLF_C);
9547 if (*attr == 0 && fill_vert == ' ')
9548 return '|';
9549 else
9550 return fill_vert;
9552 #endif
9555 * Return TRUE if redrawing should currently be done.
9558 redrawing()
9560 return (!RedrawingDisabled
9561 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
9565 * Return TRUE if printing messages should currently be done.
9568 messaging()
9570 return (!(p_lz && char_avail() && !KeyTyped));
9574 * Show current status info in ruler and various other places
9575 * If always is FALSE, only show ruler if position has changed.
9577 void
9578 showruler(always)
9579 int always;
9581 if (!always && !redrawing())
9582 return;
9583 #ifdef FEAT_INS_EXPAND
9584 if (pum_visible())
9586 # ifdef FEAT_WINDOWS
9587 /* Don't redraw right now, do it later. */
9588 curwin->w_redr_status = TRUE;
9589 # endif
9590 return;
9592 #endif
9593 #if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
9594 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
9596 redraw_custom_statusline(curwin);
9598 else
9599 #endif
9600 #ifdef FEAT_CMDL_INFO
9601 win_redr_ruler(curwin, always);
9602 #endif
9604 #ifdef FEAT_TITLE
9605 if (need_maketitle
9606 # ifdef FEAT_STL_OPT
9607 || (p_icon && (stl_syntax & STL_IN_ICON))
9608 || (p_title && (stl_syntax & STL_IN_TITLE))
9609 # endif
9611 maketitle();
9612 #endif
9613 #ifdef FEAT_WINDOWS
9614 /* Redraw the tab pages line if needed. */
9615 if (redraw_tabline)
9616 draw_tabline();
9617 #endif
9620 #ifdef FEAT_CMDL_INFO
9621 static void
9622 win_redr_ruler(wp, always)
9623 win_T *wp;
9624 int always;
9626 #define RULER_BUF_LEN 70
9627 char_u buffer[RULER_BUF_LEN];
9628 int row;
9629 int fillchar;
9630 int attr;
9631 int empty_line = FALSE;
9632 colnr_T virtcol;
9633 int i;
9634 size_t len;
9635 int o;
9636 #ifdef FEAT_VERTSPLIT
9637 int this_ru_col;
9638 int off = 0;
9639 int width = Columns;
9640 # define WITH_OFF(x) x
9641 # define WITH_WIDTH(x) x
9642 #else
9643 # define WITH_OFF(x) 0
9644 # define WITH_WIDTH(x) Columns
9645 # define this_ru_col ru_col
9646 #endif
9648 /* If 'ruler' off or redrawing disabled, don't do anything */
9649 if (!p_ru)
9650 return;
9653 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
9654 * after deleting lines, before cursor.lnum is corrected.
9656 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
9657 return;
9659 #ifdef FEAT_INS_EXPAND
9660 /* Don't draw the ruler while doing insert-completion, it might overwrite
9661 * the (long) mode message. */
9662 # ifdef FEAT_WINDOWS
9663 if (wp == lastwin && lastwin->w_status_height == 0)
9664 # endif
9665 if (edit_submode != NULL)
9666 return;
9667 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
9668 if (pum_visible())
9669 return;
9670 #endif
9672 #ifdef FEAT_STL_OPT
9673 if (*p_ruf)
9675 int save_called_emsg = called_emsg;
9677 called_emsg = FALSE;
9678 win_redr_custom(wp, TRUE);
9679 if (called_emsg)
9680 set_string_option_direct((char_u *)"rulerformat", -1,
9681 (char_u *)"", OPT_FREE, SID_ERROR);
9682 called_emsg |= save_called_emsg;
9683 return;
9685 #endif
9688 * Check if not in Insert mode and the line is empty (will show "0-1").
9690 if (!(State & INSERT)
9691 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
9692 empty_line = TRUE;
9695 * Only draw the ruler when something changed.
9697 validate_virtcol_win(wp);
9698 if ( redraw_cmdline
9699 || always
9700 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
9701 || wp->w_cursor.col != wp->w_ru_cursor.col
9702 || wp->w_virtcol != wp->w_ru_virtcol
9703 #ifdef FEAT_VIRTUALEDIT
9704 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
9705 #endif
9706 || wp->w_topline != wp->w_ru_topline
9707 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
9708 #ifdef FEAT_DIFF
9709 || wp->w_topfill != wp->w_ru_topfill
9710 #endif
9711 || empty_line != wp->w_ru_empty)
9713 cursor_off();
9714 #ifdef FEAT_WINDOWS
9715 if (wp->w_status_height)
9717 row = W_WINROW(wp) + wp->w_height;
9718 fillchar = fillchar_status(&attr, wp == curwin);
9719 # ifdef FEAT_VERTSPLIT
9720 off = W_WINCOL(wp);
9721 width = W_WIDTH(wp);
9722 # endif
9724 else
9725 #endif
9727 row = Rows - 1;
9728 fillchar = ' ';
9729 attr = 0;
9730 #ifdef FEAT_VERTSPLIT
9731 width = Columns;
9732 off = 0;
9733 #endif
9736 /* In list mode virtcol needs to be recomputed */
9737 virtcol = wp->w_virtcol;
9738 if (wp->w_p_list && lcs_tab1 == NUL)
9740 wp->w_p_list = FALSE;
9741 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
9742 wp->w_p_list = TRUE;
9746 * Some sprintfs return the length, some return a pointer.
9747 * To avoid portability problems we use strlen() here.
9749 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
9750 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
9751 ? 0L
9752 : (long)(wp->w_cursor.lnum));
9753 len = STRLEN(buffer);
9754 col_print(buffer + len, RULER_BUF_LEN - len,
9755 empty_line ? 0 : (int)wp->w_cursor.col + 1,
9756 (int)virtcol + 1);
9759 * Add a "50%" if there is room for it.
9760 * On the last line, don't print in the last column (scrolls the
9761 * screen up on some terminals).
9763 i = (int)STRLEN(buffer);
9764 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
9765 o = i + vim_strsize(buffer + i + 1);
9766 #ifdef FEAT_WINDOWS
9767 if (wp->w_status_height == 0) /* can't use last char of screen */
9768 #endif
9769 ++o;
9770 #ifdef FEAT_VERTSPLIT
9771 this_ru_col = ru_col - (Columns - width);
9772 if (this_ru_col < 0)
9773 this_ru_col = 0;
9774 #endif
9775 /* Never use more than half the window/screen width, leave the other
9776 * half for the filename. */
9777 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
9778 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
9779 if (this_ru_col + o < WITH_WIDTH(width))
9781 while (this_ru_col + o < WITH_WIDTH(width))
9783 #ifdef FEAT_MBYTE
9784 if (has_mbyte)
9785 i += (*mb_char2bytes)(fillchar, buffer + i);
9786 else
9787 #endif
9788 buffer[i++] = fillchar;
9789 ++o;
9791 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
9793 /* Truncate at window boundary. */
9794 #ifdef FEAT_MBYTE
9795 if (has_mbyte)
9797 o = 0;
9798 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
9800 o += (*mb_ptr2cells)(buffer + i);
9801 if (this_ru_col + o > WITH_WIDTH(width))
9803 buffer[i] = NUL;
9804 break;
9808 else
9809 #endif
9810 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
9811 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
9813 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
9814 i = redraw_cmdline;
9815 screen_fill(row, row + 1,
9816 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
9817 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
9818 fillchar, fillchar, attr);
9819 /* don't redraw the cmdline because of showing the ruler */
9820 redraw_cmdline = i;
9821 wp->w_ru_cursor = wp->w_cursor;
9822 wp->w_ru_virtcol = wp->w_virtcol;
9823 wp->w_ru_empty = empty_line;
9824 wp->w_ru_topline = wp->w_topline;
9825 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
9826 #ifdef FEAT_DIFF
9827 wp->w_ru_topfill = wp->w_topfill;
9828 #endif
9831 #endif
9833 #if defined(FEAT_LINEBREAK) || defined(PROTO)
9835 * Return the width of the 'number' and 'relativenumber' column.
9836 * Caller may need to check if 'number' or 'relativenumber' is set.
9837 * Otherwise it depends on 'numberwidth' and the line count.
9840 number_width(wp)
9841 win_T *wp;
9843 int n;
9844 linenr_T lnum;
9846 if (wp->w_p_nu)
9847 /* 'number' */
9848 lnum = wp->w_buffer->b_ml.ml_line_count;
9849 else
9850 /* 'relativenumber' */
9851 lnum = wp->w_height;
9853 if (lnum == wp->w_nrwidth_line_count)
9854 return wp->w_nrwidth_width;
9855 wp->w_nrwidth_line_count = lnum;
9857 n = 0;
9860 lnum /= 10;
9861 ++n;
9862 } while (lnum > 0);
9864 /* 'numberwidth' gives the minimal width plus one */
9865 if (n < wp->w_p_nuw - 1)
9866 n = wp->w_p_nuw - 1;
9868 wp->w_nrwidth_width = n;
9869 return n;
9871 #endif