Merge branch 'vim' into feat/code-check
[vim_extended.git] / src / screen.c
blob239339ae9107471a92eca6df6c08dc67824533f3
1 /* vi:set ts=8 sts=4 sw=4:
3 * VIM - Vi IMproved by Bram Moolenaar
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
11 * screen.c: code for displaying on the screen
13 * Output to the screen (console, terminal emulator or GUI window) is minimized
14 * by remembering what is already on the screen, and only updating the parts
15 * that changed.
17 * ScreenLines[off] Contains a copy of the whole screen, as it is currently
18 * displayed (excluding text written by external commands).
19 * ScreenAttrs[off] Contains the associated attributes.
20 * LineOffset[row] Contains the offset into ScreenLines*[] and ScreenAttrs[]
21 * for each line.
22 * LineWraps[row] Flag for each line whether it wraps to the next line.
24 * For double-byte characters, two consecutive bytes in ScreenLines[] can form
25 * one character which occupies two display cells.
26 * For UTF-8 a multi-byte character is converted to Unicode and stored in
27 * ScreenLinesUC[]. ScreenLines[] contains the first byte only. For an ASCII
28 * character without composing chars ScreenLinesUC[] will be 0. When the
29 * character occupies two display cells the next byte in ScreenLines[] is 0.
30 * ScreenLinesC[][] contain up to 'maxcombine' composing characters
31 * (drawn on top of the first character). They are 0 when not used.
32 * ScreenLines2[] is only used for euc-jp to store the second byte if the
33 * first byte is 0x8e (single-width character).
35 * The screen_*() functions write to the screen and handle updating
36 * ScreenLines[].
38 * update_screen() is the function that updates all windows and status lines.
39 * It is called form the main loop when must_redraw is non-zero. It may be
40 * called from other places when an immediate screen update is needed.
42 * The part of the buffer that is displayed in a window is set with:
43 * - w_topline (first buffer line in window)
44 * - w_topfill (filler line above the first line)
45 * - w_leftcol (leftmost window cell in window),
46 * - w_skipcol (skipped window cells of first line)
48 * Commands that only move the cursor around in a window, do not need to take
49 * action to update the display. The main loop will check if w_topline is
50 * valid and update it (scroll the window) when needed.
52 * Commands that scroll a window change w_topline and must call
53 * check_cursor() to move the cursor into the visible part of the window, and
54 * call redraw_later(VALID) to have the window displayed by update_screen()
55 * later.
57 * Commands that change text in the buffer must call changed_bytes() or
58 * changed_lines() to mark the area that changed and will require updating
59 * later. The main loop will call update_screen(), which will update each
60 * window that shows the changed buffer. This assumes text above the change
61 * can remain displayed as it is. Text after the change may need updating for
62 * scrolling, folding and syntax highlighting.
64 * Commands that change how a window is displayed (e.g., setting 'list') or
65 * invalidate the contents of a window in another way (e.g., change fold
66 * settings), must call redraw_later(NOT_VALID) to have the whole window
67 * redisplayed by update_screen() later.
69 * Commands that change how a buffer is displayed (e.g., setting 'tabstop')
70 * must call redraw_curbuf_later(NOT_VALID) to have all the windows for the
71 * buffer redisplayed by update_screen() later.
73 * Commands that change highlighting and possibly cause a scroll too must call
74 * redraw_later(SOME_VALID) to update the whole window but still use scrolling
75 * to avoid redrawing everything. But the length of displayed lines must not
76 * change, use NOT_VALID then.
78 * Commands that move the window position must call redraw_later(NOT_VALID).
79 * TODO: should minimize redrawing by scrolling when possible.
81 * Commands that change everything (e.g., resizing the screen) must call
82 * redraw_all_later(NOT_VALID) or redraw_all_later(CLEAR).
84 * Things that are handled indirectly:
85 * - When messages scroll the screen up, msg_scrolled will be set and
86 * update_screen() called to redraw.
89 #include "vim.h"
92 * The attributes that are actually active for writing to the screen.
94 static int screen_attr = 0;
97 * Positioning the cursor is reduced by remembering the last position.
98 * Mostly used by windgoto() and screen_char().
100 static int screen_cur_row, screen_cur_col; /* last known cursor position */
102 #ifdef FEAT_SEARCH_EXTRA
103 static match_T search_hl; /* used for 'hlsearch' highlight matching */
104 #endif
106 #ifdef FEAT_FOLDING
107 static foldinfo_T win_foldinfo; /* info for 'foldcolumn' */
108 #endif
111 * Buffer for one screen line (characters and attributes).
113 static schar_T *current_ScreenLine;
115 static void win_update __ARGS((win_T *wp));
116 static void win_draw_end __ARGS((win_T *wp, int c1, int c2, int row, int endrow, hlf_T hl));
117 #ifdef FEAT_FOLDING
118 static void fold_line __ARGS((win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row));
119 static void fill_foldcolumn __ARGS((char_u *p, win_T *wp, int closed, linenr_T lnum));
120 static void copy_text_attr __ARGS((int off, char_u *buf, int len, int attr));
121 #endif
122 static int win_line __ARGS((win_T *, linenr_T, int, int, int nochange));
123 static int char_needs_redraw __ARGS((int off_from, int off_to, int cols));
124 #ifdef FEAT_RIGHTLEFT
125 static void screen_line __ARGS((int row, int coloff, int endcol, int clear_width, int rlflag));
126 # define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c), (rl))
127 #else
128 static void screen_line __ARGS((int row, int coloff, int endcol, int clear_width));
129 # define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c))
130 #endif
131 #ifdef FEAT_VERTSPLIT
132 static void draw_vsep_win __ARGS((win_T *wp, int row));
133 #endif
134 #ifdef FEAT_STL_OPT
135 static void redraw_custom_statusline __ARGS((win_T *wp));
136 #endif
137 #ifdef FEAT_SEARCH_EXTRA
138 #define SEARCH_HL_PRIORITY 0
139 static void start_search_hl __ARGS((void));
140 static void end_search_hl __ARGS((void));
141 static void prepare_search_hl __ARGS((win_T *wp, linenr_T lnum));
142 static void next_search_hl __ARGS((win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol));
143 #endif
144 static void screen_start_highlight __ARGS((int attr));
145 static void screen_char __ARGS((unsigned off, int row, int col));
146 #ifdef FEAT_MBYTE
147 static void screen_char_2 __ARGS((unsigned off, int row, int col));
148 #endif
149 static void screenclear2 __ARGS((void));
150 static void lineclear __ARGS((unsigned off, int width));
151 static void lineinvalid __ARGS((unsigned off, int width));
152 #ifdef FEAT_VERTSPLIT
153 static void linecopy __ARGS((int to, int from, win_T *wp));
154 static void redraw_block __ARGS((int row, int end, win_T *wp));
155 #endif
156 static int win_do_lines __ARGS((win_T *wp, int row, int line_count, int mayclear, int del));
157 static void win_rest_invalid __ARGS((win_T *wp));
158 static void msg_pos_mode __ARGS((void));
159 #if defined(FEAT_WINDOWS)
160 static void draw_tabline __ARGS((void));
161 #endif
162 #if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
163 static int fillchar_status __ARGS((int *attr, int is_curwin));
164 #endif
165 #ifdef FEAT_VERTSPLIT
166 static int fillchar_vsep __ARGS((int *attr));
167 #endif
168 #ifdef FEAT_STL_OPT
169 static void win_redr_custom __ARGS((win_T *wp, int draw_ruler));
170 #endif
171 #ifdef FEAT_CMDL_INFO
172 static void win_redr_ruler __ARGS((win_T *wp, int always));
173 #endif
175 #if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
176 /* Ugly global: overrule attribute used by screen_char() */
177 static int screen_char_attr = 0;
178 #endif
181 * Redraw the current window later, with update_screen(type).
182 * Set must_redraw only if not already set to a higher value.
183 * e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
185 void
186 redraw_later(type)
187 int type;
189 redraw_win_later(curwin, type);
192 void
193 redraw_win_later(wp, type)
194 win_T *wp;
195 int type;
197 if (wp->w_redr_type < type)
199 wp->w_redr_type = type;
200 if (type >= NOT_VALID)
201 wp->w_lines_valid = 0;
202 if (must_redraw < type) /* must_redraw is the maximum of all windows */
203 must_redraw = type;
208 * Force a complete redraw later. Also resets the highlighting. To be used
209 * after executing a shell command that messes up the screen.
211 void
212 redraw_later_clear()
214 redraw_all_later(CLEAR);
215 #ifdef FEAT_GUI
216 if (gui.in_use)
217 /* Use a code that will reset gui.highlight_mask in
218 * gui_stop_highlight(). */
219 screen_attr = HL_ALL + 1;
220 else
221 #endif
222 /* Use attributes that is very unlikely to appear in text. */
223 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE;
227 * Mark all windows to be redrawn later.
229 void
230 redraw_all_later(type)
231 int type;
233 win_T *wp;
235 FOR_ALL_WINDOWS(wp)
237 redraw_win_later(wp, type);
242 * Mark all windows that are editing the current buffer to be updated later.
244 void
245 redraw_curbuf_later(type)
246 int type;
248 redraw_buf_later(curbuf, type);
251 void
252 redraw_buf_later(buf, type)
253 buf_T *buf;
254 int type;
256 win_T *wp;
258 FOR_ALL_WINDOWS(wp)
260 if (wp->w_buffer == buf)
261 redraw_win_later(wp, type);
266 * Changed something in the current window, at buffer line "lnum", that
267 * requires that line and possibly other lines to be redrawn.
268 * Used when entering/leaving Insert mode with the cursor on a folded line.
269 * Used to remove the "$" from a change command.
270 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
271 * may become invalid and the whole window will have to be redrawn.
273 void
274 redrawWinline(lnum, invalid)
275 linenr_T lnum;
276 int invalid UNUSED; /* window line height is invalid now */
278 #ifdef FEAT_FOLDING
279 int i;
280 #endif
282 if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
283 curwin->w_redraw_top = lnum;
284 if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
285 curwin->w_redraw_bot = lnum;
286 redraw_later(VALID);
288 #ifdef FEAT_FOLDING
289 if (invalid)
291 /* A w_lines[] entry for this lnum has become invalid. */
292 i = find_wl_entry(curwin, lnum);
293 if (i >= 0)
294 curwin->w_lines[i].wl_valid = FALSE;
296 #endif
300 * update all windows that are editing the current buffer
302 void
303 update_curbuf(type)
304 int type;
306 redraw_curbuf_later(type);
307 update_screen(type);
311 * update_screen()
313 * Based on the current value of curwin->w_topline, transfer a screenfull
314 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
316 void
317 update_screen(type)
318 int type;
320 win_T *wp;
321 static int did_intro = FALSE;
322 #if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
323 int did_one;
324 #endif
326 if (!screen_valid(TRUE))
327 return;
329 if (must_redraw)
331 if (type < must_redraw) /* use maximal type */
332 type = must_redraw;
334 /* must_redraw is reset here, so that when we run into some weird
335 * reason to redraw while busy redrawing (e.g., asynchronous
336 * scrolling), or update_topline() in win_update() will cause a
337 * scroll, the screen will be redrawn later or in win_update(). */
338 must_redraw = 0;
341 /* Need to update w_lines[]. */
342 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
343 type = NOT_VALID;
345 if (!redrawing())
347 redraw_later(type); /* remember type for next time */
348 must_redraw = type;
349 if (type > INVERTED_ALL)
350 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
351 return;
354 updating_screen = TRUE;
355 #ifdef FEAT_SYN_HL
356 ++display_tick; /* let syntax code know we're in a next round of
357 * display updating */
358 #endif
361 * if the screen was scrolled up when displaying a message, scroll it down
363 if (msg_scrolled)
365 clear_cmdline = TRUE;
366 if (msg_scrolled > Rows - 5) /* clearing is faster */
367 type = CLEAR;
368 else if (type != CLEAR)
370 check_for_delay(FALSE);
371 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, NULL) == FAIL)
372 type = CLEAR;
373 FOR_ALL_WINDOWS(wp)
375 if (W_WINROW(wp) < msg_scrolled)
377 if (W_WINROW(wp) + wp->w_height > msg_scrolled
378 && wp->w_redr_type < REDRAW_TOP
379 && wp->w_lines_valid > 0
380 && wp->w_topline == wp->w_lines[0].wl_lnum)
382 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
383 wp->w_redr_type = REDRAW_TOP;
385 else
387 wp->w_redr_type = NOT_VALID;
388 #ifdef FEAT_WINDOWS
389 if (W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp)
390 <= msg_scrolled)
391 wp->w_redr_status = TRUE;
392 #endif
396 redraw_cmdline = TRUE;
397 #ifdef FEAT_WINDOWS
398 redraw_tabline = TRUE;
399 #endif
401 msg_scrolled = 0;
402 need_wait_return = FALSE;
405 /* reset cmdline_row now (may have been changed temporarily) */
406 compute_cmdrow();
408 /* Check for changed highlighting */
409 if (need_highlight_changed)
410 highlight_changed();
412 if (type == CLEAR) /* first clear screen */
414 screenclear(); /* will reset clear_cmdline */
415 type = NOT_VALID;
418 if (clear_cmdline) /* going to clear cmdline (done below) */
419 check_for_delay(FALSE);
421 #ifdef FEAT_LINEBREAK
422 /* Force redraw when width of 'number' column changes. */
423 if (curwin->w_redr_type < NOT_VALID
424 && curwin->w_nrwidth != (curwin->w_p_nu ? number_width(curwin) : 0))
425 curwin->w_redr_type = NOT_VALID;
426 #endif
429 * Only start redrawing if there is really something to do.
431 if (type == INVERTED)
432 update_curswant();
433 if (curwin->w_redr_type < type
434 && !((type == VALID
435 && curwin->w_lines[0].wl_valid
436 #ifdef FEAT_DIFF
437 && curwin->w_topfill == curwin->w_old_topfill
438 && curwin->w_botfill == curwin->w_old_botfill
439 #endif
440 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
441 #ifdef FEAT_VISUAL
442 || (type == INVERTED
443 && VIsual_active
444 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
445 && curwin->w_old_visual_mode == VIsual_mode
446 && (curwin->w_valid & VALID_VIRTCOL)
447 && curwin->w_old_curswant == curwin->w_curswant)
448 #endif
450 curwin->w_redr_type = type;
452 #ifdef FEAT_WINDOWS
453 /* Redraw the tab pages line if needed. */
454 if (redraw_tabline || type >= NOT_VALID)
455 draw_tabline();
456 #endif
458 #ifdef FEAT_SYN_HL
460 * Correct stored syntax highlighting info for changes in each displayed
461 * buffer. Each buffer must only be done once.
463 FOR_ALL_WINDOWS(wp)
465 if (wp->w_buffer->b_mod_set)
467 # ifdef FEAT_WINDOWS
468 win_T *wwp;
470 /* Check if we already did this buffer. */
471 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
472 if (wwp->w_buffer == wp->w_buffer)
473 break;
474 # endif
475 if (
476 # ifdef FEAT_WINDOWS
477 wwp == wp &&
478 # endif
479 syntax_present(wp->w_buffer))
480 syn_stack_apply_changes(wp->w_buffer);
483 #endif
486 * Go from top to bottom through the windows, redrawing the ones that need
487 * it.
489 #if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
490 did_one = FALSE;
491 #endif
492 #ifdef FEAT_SEARCH_EXTRA
493 search_hl.rm.regprog = NULL;
494 #endif
495 FOR_ALL_WINDOWS(wp)
497 if (wp->w_redr_type != 0)
499 cursor_off();
500 #if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
501 if (!did_one)
503 did_one = TRUE;
504 # ifdef FEAT_SEARCH_EXTRA
505 start_search_hl();
506 # endif
507 # ifdef FEAT_CLIPBOARD
508 /* When Visual area changed, may have to update selection. */
509 if (clip_star.available && clip_isautosel())
510 clip_update_selection();
511 # endif
512 #ifdef FEAT_GUI
513 /* Remove the cursor before starting to do anything, because
514 * scrolling may make it difficult to redraw the text under
515 * it. */
516 if (gui.in_use)
517 gui_undraw_cursor();
518 #endif
520 #endif
521 win_update(wp);
524 #ifdef FEAT_WINDOWS
525 /* redraw status line after the window to minimize cursor movement */
526 if (wp->w_redr_status)
528 cursor_off();
529 win_redr_status(wp);
531 #endif
533 #if defined(FEAT_SEARCH_EXTRA)
534 end_search_hl();
535 #endif
537 #ifdef FEAT_WINDOWS
538 /* Reset b_mod_set flags. Going through all windows is probably faster
539 * than going through all buffers (there could be many buffers). */
540 for (wp = firstwin; wp != NULL; wp = wp->w_next)
541 wp->w_buffer->b_mod_set = FALSE;
542 #else
543 curbuf->b_mod_set = FALSE;
544 #endif
546 updating_screen = FALSE;
547 #ifdef FEAT_GUI
548 gui_may_resize_shell();
549 #endif
551 /* Clear or redraw the command line. Done last, because scrolling may
552 * mess up the command line. */
553 if (clear_cmdline || redraw_cmdline)
554 showmode();
556 /* May put up an introductory message when not editing a file */
557 if (!did_intro && bufempty()
558 && curbuf->b_fname == NULL
559 #ifdef FEAT_WINDOWS
560 && firstwin->w_next == NULL
561 #endif
562 && vim_strchr(p_shm, SHM_INTRO) == NULL)
563 intro_message(FALSE);
564 did_intro = TRUE;
566 #ifdef FEAT_GUI
567 /* Redraw the cursor and update the scrollbars when all screen updating is
568 * done. */
569 if (gui.in_use)
571 out_flush(); /* required before updating the cursor */
572 if (did_one)
573 gui_update_cursor(FALSE, FALSE);
574 gui_update_scrollbars(FALSE);
576 #endif
579 #if defined(FEAT_SIGNS) || defined(FEAT_GUI)
580 static void update_prepare __ARGS((void));
581 static void update_finish __ARGS((void));
584 * Prepare for updating one or more windows.
586 static void
587 update_prepare()
589 cursor_off();
590 updating_screen = TRUE;
591 #ifdef FEAT_GUI
592 /* Remove the cursor before starting to do anything, because scrolling may
593 * make it difficult to redraw the text under it. */
594 if (gui.in_use)
595 gui_undraw_cursor();
596 #endif
597 #ifdef FEAT_SEARCH_EXTRA
598 start_search_hl();
599 #endif
603 * Finish updating one or more windows.
605 static void
606 update_finish()
608 if (redraw_cmdline)
609 showmode();
611 # ifdef FEAT_SEARCH_EXTRA
612 end_search_hl();
613 # endif
615 updating_screen = FALSE;
617 # ifdef FEAT_GUI
618 gui_may_resize_shell();
620 /* Redraw the cursor and update the scrollbars when all screen updating is
621 * done. */
622 if (gui.in_use)
624 out_flush(); /* required before updating the cursor */
625 gui_update_cursor(FALSE, FALSE);
626 gui_update_scrollbars(FALSE);
628 # endif
630 #endif
632 #if defined(FEAT_SIGNS) || defined(PROTO)
633 void
634 update_debug_sign(buf, lnum)
635 buf_T *buf;
636 linenr_T lnum;
638 win_T *wp;
639 int doit = FALSE;
641 # ifdef FEAT_FOLDING
642 win_foldinfo.fi_level = 0;
643 # endif
645 /* update/delete a specific mark */
646 FOR_ALL_WINDOWS(wp)
648 if (buf != NULL && lnum > 0)
650 if (wp->w_buffer == buf && lnum >= wp->w_topline
651 && lnum < wp->w_botline)
653 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
654 wp->w_redraw_top = lnum;
655 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
656 wp->w_redraw_bot = lnum;
657 redraw_win_later(wp, VALID);
660 else
661 redraw_win_later(wp, VALID);
662 if (wp->w_redr_type != 0)
663 doit = TRUE;
666 if (!doit)
667 return;
669 /* update all windows that need updating */
670 update_prepare();
672 # ifdef FEAT_WINDOWS
673 for (wp = firstwin; wp; wp = wp->w_next)
675 if (wp->w_redr_type != 0)
676 win_update(wp);
677 if (wp->w_redr_status)
678 win_redr_status(wp);
680 # else
681 if (curwin->w_redr_type != 0)
682 win_update(curwin);
683 # endif
685 update_finish();
687 #endif
690 #if defined(FEAT_GUI) || defined(PROTO)
692 * Update a single window, its status line and maybe the command line msg.
693 * Used for the GUI scrollbar.
695 void
696 updateWindow(wp)
697 win_T *wp;
699 update_prepare();
701 #ifdef FEAT_CLIPBOARD
702 /* When Visual area changed, may have to update selection. */
703 if (clip_star.available && clip_isautosel())
704 clip_update_selection();
705 #endif
707 win_update(wp);
709 #ifdef FEAT_WINDOWS
710 /* When the screen was cleared redraw the tab pages line. */
711 if (redraw_tabline)
712 draw_tabline();
714 if (wp->w_redr_status
715 # ifdef FEAT_CMDL_INFO
716 || p_ru
717 # endif
718 # ifdef FEAT_STL_OPT
719 || *p_stl != NUL || *wp->w_p_stl != NUL
720 # endif
722 win_redr_status(wp);
723 #endif
725 update_finish();
727 #endif
730 * Update a single window.
732 * This may cause the windows below it also to be redrawn (when clearing the
733 * screen or scrolling lines).
735 * How the window is redrawn depends on wp->w_redr_type. Each type also
736 * implies the one below it.
737 * NOT_VALID redraw the whole window
738 * SOME_VALID redraw the whole window but do scroll when possible
739 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
740 * INVERTED redraw the changed part of the Visual area
741 * INVERTED_ALL redraw the whole Visual area
742 * VALID 1. scroll up/down to adjust for a changed w_topline
743 * 2. update lines at the top when scrolled down
744 * 3. redraw changed text:
745 * - if wp->w_buffer->b_mod_set set, update lines between
746 * b_mod_top and b_mod_bot.
747 * - if wp->w_redraw_top non-zero, redraw lines between
748 * wp->w_redraw_top and wp->w_redr_bot.
749 * - continue redrawing when syntax status is invalid.
750 * 4. if scrolled up, update lines at the bottom.
751 * This results in three areas that may need updating:
752 * top: from first row to top_end (when scrolled down)
753 * mid: from mid_start to mid_end (update inversion or changed text)
754 * bot: from bot_start to last row (when scrolled up)
756 static void
757 win_update(wp)
758 win_T *wp;
760 buf_T *buf = wp->w_buffer;
761 int type;
762 int top_end = 0; /* Below last row of the top area that needs
763 updating. 0 when no top area updating. */
764 int mid_start = 999;/* first row of the mid area that needs
765 updating. 999 when no mid area updating. */
766 int mid_end = 0; /* Below last row of the mid area that needs
767 updating. 0 when no mid area updating. */
768 int bot_start = 999;/* first row of the bot area that needs
769 updating. 999 when no bot area updating */
770 #ifdef FEAT_VISUAL
771 int scrolled_down = FALSE; /* TRUE when scrolled down when
772 w_topline got smaller a bit */
773 #endif
774 #ifdef FEAT_SEARCH_EXTRA
775 matchitem_T *cur; /* points to the match list */
776 int top_to_mod = FALSE; /* redraw above mod_top */
777 #endif
779 int row; /* current window row to display */
780 linenr_T lnum; /* current buffer lnum to display */
781 int idx; /* current index in w_lines[] */
782 int srow; /* starting row of the current line */
784 int eof = FALSE; /* if TRUE, we hit the end of the file */
785 int didline = FALSE; /* if TRUE, we finished the last line */
786 int i;
787 long j;
788 static int recursive = FALSE; /* being called recursively */
789 int old_botline = wp->w_botline;
790 #ifdef FEAT_FOLDING
791 long fold_count;
792 #endif
793 #ifdef FEAT_SYN_HL
794 /* remember what happened to the previous line, to know if
795 * check_visual_highlight() can be used */
796 #define DID_NONE 1 /* didn't update a line */
797 #define DID_LINE 2 /* updated a normal line */
798 #define DID_FOLD 3 /* updated a folded line */
799 int did_update = DID_NONE;
800 linenr_T syntax_last_parsed = 0; /* last parsed text line */
801 #endif
802 linenr_T mod_top = 0;
803 linenr_T mod_bot = 0;
804 #if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
805 int save_got_int;
806 #endif
808 type = wp->w_redr_type;
810 if (type == NOT_VALID)
812 #ifdef FEAT_WINDOWS
813 wp->w_redr_status = TRUE;
814 #endif
815 wp->w_lines_valid = 0;
818 /* Window is zero-height: nothing to draw. */
819 if (wp->w_height == 0)
821 wp->w_redr_type = 0;
822 return;
825 #ifdef FEAT_VERTSPLIT
826 /* Window is zero-width: Only need to draw the separator. */
827 if (wp->w_width == 0)
829 /* draw the vertical separator right of this window */
830 draw_vsep_win(wp, 0);
831 wp->w_redr_type = 0;
832 return;
834 #endif
836 #ifdef FEAT_SEARCH_EXTRA
837 /* Setup for match and 'hlsearch' highlighting. Disable any previous
838 * match */
839 cur = wp->w_match_head;
840 while (cur != NULL)
842 cur->hl.rm = cur->match;
843 if (cur->hlg_id == 0)
844 cur->hl.attr = 0;
845 else
846 cur->hl.attr = syn_id2attr(cur->hlg_id);
847 cur->hl.buf = buf;
848 cur->hl.lnum = 0;
849 cur->hl.first_lnum = 0;
850 # ifdef FEAT_RELTIME
851 /* Set the time limit to 'redrawtime'. */
852 profile_setlimit(p_rdt, &(cur->hl.tm));
853 # endif
854 cur = cur->next;
856 search_hl.buf = buf;
857 search_hl.lnum = 0;
858 search_hl.first_lnum = 0;
859 /* time limit is set at the toplevel, for all windows */
860 #endif
862 #ifdef FEAT_LINEBREAK
863 /* Force redraw when width of 'number' column changes. */
864 i = wp->w_p_nu ? number_width(wp) : 0;
865 if (wp->w_nrwidth != i)
867 type = NOT_VALID;
868 wp->w_nrwidth = i;
870 else
871 #endif
873 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
876 * When there are both inserted/deleted lines and specific lines to be
877 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
878 * everything (only happens when redrawing is off for while).
880 type = NOT_VALID;
882 else
885 * Set mod_top to the first line that needs displaying because of
886 * changes. Set mod_bot to the first line after the changes.
888 mod_top = wp->w_redraw_top;
889 if (wp->w_redraw_bot != 0)
890 mod_bot = wp->w_redraw_bot + 1;
891 else
892 mod_bot = 0;
893 wp->w_redraw_top = 0; /* reset for next time */
894 wp->w_redraw_bot = 0;
895 if (buf->b_mod_set)
897 if (mod_top == 0 || mod_top > buf->b_mod_top)
899 mod_top = buf->b_mod_top;
900 #ifdef FEAT_SYN_HL
901 /* Need to redraw lines above the change that may be included
902 * in a pattern match. */
903 if (syntax_present(buf))
905 mod_top -= buf->b_syn_sync_linebreaks;
906 if (mod_top < 1)
907 mod_top = 1;
909 #endif
911 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
912 mod_bot = buf->b_mod_bot;
914 #ifdef FEAT_SEARCH_EXTRA
915 /* When 'hlsearch' is on and using a multi-line search pattern, a
916 * change in one line may make the Search highlighting in a
917 * previous line invalid. Simple solution: redraw all visible
918 * lines above the change.
919 * Same for a match pattern.
921 if (search_hl.rm.regprog != NULL
922 && re_multiline(search_hl.rm.regprog))
923 top_to_mod = TRUE;
924 else
926 cur = wp->w_match_head;
927 while (cur != NULL)
929 if (cur->match.regprog != NULL
930 && re_multiline(cur->match.regprog))
932 top_to_mod = TRUE;
933 break;
935 cur = cur->next;
938 #endif
940 #ifdef FEAT_FOLDING
941 if (mod_top != 0 && hasAnyFolding(wp))
943 linenr_T lnumt, lnumb;
946 * A change in a line can cause lines above it to become folded or
947 * unfolded. Find the top most buffer line that may be affected.
948 * If the line was previously folded and displayed, get the first
949 * line of that fold. If the line is folded now, get the first
950 * folded line. Use the minimum of these two.
953 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
954 * the line below it. If there is no valid entry, use w_topline.
955 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
956 * to this line. If there is no valid entry, use MAXLNUM. */
957 lnumt = wp->w_topline;
958 lnumb = MAXLNUM;
959 for (i = 0; i < wp->w_lines_valid; ++i)
960 if (wp->w_lines[i].wl_valid)
962 if (wp->w_lines[i].wl_lastlnum < mod_top)
963 lnumt = wp->w_lines[i].wl_lastlnum + 1;
964 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
966 lnumb = wp->w_lines[i].wl_lnum;
967 /* When there is a fold column it might need updating
968 * in the next line ("J" just above an open fold). */
969 if (wp->w_p_fdc > 0)
970 ++lnumb;
974 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
975 if (mod_top > lnumt)
976 mod_top = lnumt;
978 /* Now do the same for the bottom line (one above mod_bot). */
979 --mod_bot;
980 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
981 ++mod_bot;
982 if (mod_bot < lnumb)
983 mod_bot = lnumb;
985 #endif
987 /* When a change starts above w_topline and the end is below
988 * w_topline, start redrawing at w_topline.
989 * If the end of the change is above w_topline: do like no change was
990 * made, but redraw the first line to find changes in syntax. */
991 if (mod_top != 0 && mod_top < wp->w_topline)
993 if (mod_bot > wp->w_topline)
994 mod_top = wp->w_topline;
995 #ifdef FEAT_SYN_HL
996 else if (syntax_present(buf))
997 top_end = 1;
998 #endif
1001 /* When line numbers are displayed need to redraw all lines below
1002 * inserted/deleted lines. */
1003 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1004 mod_bot = MAXLNUM;
1008 * When only displaying the lines at the top, set top_end. Used when
1009 * window has scrolled down for msg_scrolled.
1011 if (type == REDRAW_TOP)
1013 j = 0;
1014 for (i = 0; i < wp->w_lines_valid; ++i)
1016 j += wp->w_lines[i].wl_size;
1017 if (j >= wp->w_upd_rows)
1019 top_end = j;
1020 break;
1023 if (top_end == 0)
1024 /* not found (cannot happen?): redraw everything */
1025 type = NOT_VALID;
1026 else
1027 /* top area defined, the rest is VALID */
1028 type = VALID;
1031 /* Trick: we want to avoid clearing the screen twice. screenclear() will
1032 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1033 * non-zero and thus not FALSE) will indicate that screenclear() was not
1034 * called. */
1035 if (screen_cleared)
1036 screen_cleared = MAYBE;
1039 * If there are no changes on the screen that require a complete redraw,
1040 * handle three cases:
1041 * 1: we are off the top of the screen by a few lines: scroll down
1042 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1043 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1044 * w_lines[] that needs updating.
1046 if ((type == VALID || type == SOME_VALID
1047 || type == INVERTED || type == INVERTED_ALL)
1048 #ifdef FEAT_DIFF
1049 && !wp->w_botfill && !wp->w_old_botfill
1050 #endif
1053 if (mod_top != 0 && wp->w_topline == mod_top)
1056 * w_topline is the first changed line, the scrolling will be done
1057 * further down.
1060 else if (wp->w_lines[0].wl_valid
1061 && (wp->w_topline < wp->w_lines[0].wl_lnum
1062 #ifdef FEAT_DIFF
1063 || (wp->w_topline == wp->w_lines[0].wl_lnum
1064 && wp->w_topfill > wp->w_old_topfill)
1065 #endif
1069 * New topline is above old topline: May scroll down.
1071 #ifdef FEAT_FOLDING
1072 if (hasAnyFolding(wp))
1074 linenr_T ln;
1076 /* count the number of lines we are off, counting a sequence
1077 * of folded lines as one */
1078 j = 0;
1079 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1081 ++j;
1082 if (j >= wp->w_height - 2)
1083 break;
1084 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1087 else
1088 #endif
1089 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1090 if (j < wp->w_height - 2) /* not too far off */
1092 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1093 #ifdef FEAT_DIFF
1094 /* insert extra lines for previously invisible filler lines */
1095 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1096 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1097 - wp->w_old_topfill;
1098 #endif
1099 if (i < wp->w_height - 2) /* less than a screen off */
1102 * Try to insert the correct number of lines.
1103 * If not the last window, delete the lines at the bottom.
1104 * win_ins_lines may fail when the terminal can't do it.
1106 if (i > 0)
1107 check_for_delay(FALSE);
1108 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1110 if (wp->w_lines_valid != 0)
1112 /* Need to update rows that are new, stop at the
1113 * first one that scrolled down. */
1114 top_end = i;
1115 #ifdef FEAT_VISUAL
1116 scrolled_down = TRUE;
1117 #endif
1119 /* Move the entries that were scrolled, disable
1120 * the entries for the lines to be redrawn. */
1121 if ((wp->w_lines_valid += j) > wp->w_height)
1122 wp->w_lines_valid = wp->w_height;
1123 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1124 wp->w_lines[idx] = wp->w_lines[idx - j];
1125 while (idx >= 0)
1126 wp->w_lines[idx--].wl_valid = FALSE;
1129 else
1130 mid_start = 0; /* redraw all lines */
1132 else
1133 mid_start = 0; /* redraw all lines */
1135 else
1136 mid_start = 0; /* redraw all lines */
1138 else
1141 * New topline is at or below old topline: May scroll up.
1142 * When topline didn't change, find first entry in w_lines[] that
1143 * needs updating.
1146 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1147 j = -1;
1148 row = 0;
1149 for (i = 0; i < wp->w_lines_valid; i++)
1151 if (wp->w_lines[i].wl_valid
1152 && wp->w_lines[i].wl_lnum == wp->w_topline)
1154 j = i;
1155 break;
1157 row += wp->w_lines[i].wl_size;
1159 if (j == -1)
1161 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1162 * lines */
1163 mid_start = 0;
1165 else
1168 * Try to delete the correct number of lines.
1169 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1171 #ifdef FEAT_DIFF
1172 /* If the topline didn't change, delete old filler lines,
1173 * otherwise delete filler lines of the new topline... */
1174 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1175 row += wp->w_old_topfill;
1176 else
1177 row += diff_check_fill(wp, wp->w_topline);
1178 /* ... but don't delete new filler lines. */
1179 row -= wp->w_topfill;
1180 #endif
1181 if (row > 0)
1183 check_for_delay(FALSE);
1184 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1185 bot_start = wp->w_height - row;
1186 else
1187 mid_start = 0; /* redraw all lines */
1189 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1192 * Skip the lines (below the deleted lines) that are still
1193 * valid and don't need redrawing. Copy their info
1194 * upwards, to compensate for the deleted lines. Set
1195 * bot_start to the first row that needs redrawing.
1197 bot_start = 0;
1198 idx = 0;
1199 for (;;)
1201 wp->w_lines[idx] = wp->w_lines[j];
1202 /* stop at line that didn't fit, unless it is still
1203 * valid (no lines deleted) */
1204 if (row > 0 && bot_start + row
1205 + (int)wp->w_lines[j].wl_size > wp->w_height)
1207 wp->w_lines_valid = idx + 1;
1208 break;
1210 bot_start += wp->w_lines[idx++].wl_size;
1212 /* stop at the last valid entry in w_lines[].wl_size */
1213 if (++j >= wp->w_lines_valid)
1215 wp->w_lines_valid = idx;
1216 break;
1219 #ifdef FEAT_DIFF
1220 /* Correct the first entry for filler lines at the top
1221 * when it won't get updated below. */
1222 if (wp->w_p_diff && bot_start > 0)
1223 wp->w_lines[0].wl_size =
1224 plines_win_nofill(wp, wp->w_topline, TRUE)
1225 + wp->w_topfill;
1226 #endif
1231 /* When starting redraw in the first line, redraw all lines. When
1232 * there is only one window it's probably faster to clear the screen
1233 * first. */
1234 if (mid_start == 0)
1236 mid_end = wp->w_height;
1237 if (lastwin == firstwin)
1239 /* Clear the screen when it was not done by win_del_lines() or
1240 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1241 * then. */
1242 if (screen_cleared != TRUE)
1243 screenclear();
1244 #ifdef FEAT_WINDOWS
1245 /* The screen was cleared, redraw the tab pages line. */
1246 if (redraw_tabline)
1247 draw_tabline();
1248 #endif
1252 /* When win_del_lines() or win_ins_lines() caused the screen to be
1253 * cleared (only happens for the first window) or when screenclear()
1254 * was called directly above, "must_redraw" will have been set to
1255 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1256 if (screen_cleared == TRUE)
1257 must_redraw = 0;
1259 else
1261 /* Not VALID or INVERTED: redraw all lines. */
1262 mid_start = 0;
1263 mid_end = wp->w_height;
1266 if (type == SOME_VALID)
1268 /* SOME_VALID: redraw all lines. */
1269 mid_start = 0;
1270 mid_end = wp->w_height;
1271 type = NOT_VALID;
1274 #ifdef FEAT_VISUAL
1275 /* check if we are updating or removing the inverted part */
1276 if ((VIsual_active && buf == curwin->w_buffer)
1277 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1279 linenr_T from, to;
1281 if (VIsual_active)
1283 if (VIsual_active
1284 && (VIsual_mode != wp->w_old_visual_mode
1285 || type == INVERTED_ALL))
1288 * If the type of Visual selection changed, redraw the whole
1289 * selection. Also when the ownership of the X selection is
1290 * gained or lost.
1292 if (curwin->w_cursor.lnum < VIsual.lnum)
1294 from = curwin->w_cursor.lnum;
1295 to = VIsual.lnum;
1297 else
1299 from = VIsual.lnum;
1300 to = curwin->w_cursor.lnum;
1302 /* redraw more when the cursor moved as well */
1303 if (wp->w_old_cursor_lnum < from)
1304 from = wp->w_old_cursor_lnum;
1305 if (wp->w_old_cursor_lnum > to)
1306 to = wp->w_old_cursor_lnum;
1307 if (wp->w_old_visual_lnum < from)
1308 from = wp->w_old_visual_lnum;
1309 if (wp->w_old_visual_lnum > to)
1310 to = wp->w_old_visual_lnum;
1312 else
1315 * Find the line numbers that need to be updated: The lines
1316 * between the old cursor position and the current cursor
1317 * position. Also check if the Visual position changed.
1319 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1321 from = curwin->w_cursor.lnum;
1322 to = wp->w_old_cursor_lnum;
1324 else
1326 from = wp->w_old_cursor_lnum;
1327 to = curwin->w_cursor.lnum;
1328 if (from == 0) /* Visual mode just started */
1329 from = to;
1332 if (VIsual.lnum != wp->w_old_visual_lnum
1333 || VIsual.col != wp->w_old_visual_col)
1335 if (wp->w_old_visual_lnum < from
1336 && wp->w_old_visual_lnum != 0)
1337 from = wp->w_old_visual_lnum;
1338 if (wp->w_old_visual_lnum > to)
1339 to = wp->w_old_visual_lnum;
1340 if (VIsual.lnum < from)
1341 from = VIsual.lnum;
1342 if (VIsual.lnum > to)
1343 to = VIsual.lnum;
1348 * If in block mode and changed column or curwin->w_curswant:
1349 * update all lines.
1350 * First compute the actual start and end column.
1352 if (VIsual_mode == Ctrl_V)
1354 colnr_T fromc, toc;
1356 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
1357 ++toc;
1358 if (curwin->w_curswant == MAXCOL)
1359 toc = MAXCOL;
1361 if (fromc != wp->w_old_cursor_fcol
1362 || toc != wp->w_old_cursor_lcol)
1364 if (from > VIsual.lnum)
1365 from = VIsual.lnum;
1366 if (to < VIsual.lnum)
1367 to = VIsual.lnum;
1369 wp->w_old_cursor_fcol = fromc;
1370 wp->w_old_cursor_lcol = toc;
1373 else
1375 /* Use the line numbers of the old Visual area. */
1376 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1378 from = wp->w_old_cursor_lnum;
1379 to = wp->w_old_visual_lnum;
1381 else
1383 from = wp->w_old_visual_lnum;
1384 to = wp->w_old_cursor_lnum;
1389 * There is no need to update lines above the top of the window.
1391 if (from < wp->w_topline)
1392 from = wp->w_topline;
1395 * If we know the value of w_botline, use it to restrict the update to
1396 * the lines that are visible in the window.
1398 if (wp->w_valid & VALID_BOTLINE)
1400 if (from >= wp->w_botline)
1401 from = wp->w_botline - 1;
1402 if (to >= wp->w_botline)
1403 to = wp->w_botline - 1;
1407 * Find the minimal part to be updated.
1408 * Watch out for scrolling that made entries in w_lines[] invalid.
1409 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1410 * top_end; need to redraw from top_end to the "to" line.
1411 * A middle mouse click with a Visual selection may change the text
1412 * above the Visual area and reset wl_valid, do count these for
1413 * mid_end (in srow).
1415 if (mid_start > 0)
1417 lnum = wp->w_topline;
1418 idx = 0;
1419 srow = 0;
1420 if (scrolled_down)
1421 mid_start = top_end;
1422 else
1423 mid_start = 0;
1424 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1426 if (wp->w_lines[idx].wl_valid)
1427 mid_start += wp->w_lines[idx].wl_size;
1428 else if (!scrolled_down)
1429 srow += wp->w_lines[idx].wl_size;
1430 ++idx;
1431 # ifdef FEAT_FOLDING
1432 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1433 lnum = wp->w_lines[idx].wl_lnum;
1434 else
1435 # endif
1436 ++lnum;
1438 srow += mid_start;
1439 mid_end = wp->w_height;
1440 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1442 if (wp->w_lines[idx].wl_valid
1443 && wp->w_lines[idx].wl_lnum >= to + 1)
1445 /* Only update until first row of this line */
1446 mid_end = srow;
1447 break;
1449 srow += wp->w_lines[idx].wl_size;
1454 if (VIsual_active && buf == curwin->w_buffer)
1456 wp->w_old_visual_mode = VIsual_mode;
1457 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1458 wp->w_old_visual_lnum = VIsual.lnum;
1459 wp->w_old_visual_col = VIsual.col;
1460 wp->w_old_curswant = curwin->w_curswant;
1462 else
1464 wp->w_old_visual_mode = 0;
1465 wp->w_old_cursor_lnum = 0;
1466 wp->w_old_visual_lnum = 0;
1467 wp->w_old_visual_col = 0;
1469 #endif /* FEAT_VISUAL */
1471 #if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1472 /* reset got_int, otherwise regexp won't work */
1473 save_got_int = got_int;
1474 got_int = 0;
1475 #endif
1476 #ifdef FEAT_FOLDING
1477 win_foldinfo.fi_level = 0;
1478 #endif
1481 * Update all the window rows.
1483 idx = 0; /* first entry in w_lines[].wl_size */
1484 row = 0;
1485 srow = 0;
1486 lnum = wp->w_topline; /* first line shown in window */
1487 for (;;)
1489 /* stop updating when reached the end of the window (check for _past_
1490 * the end of the window is at the end of the loop) */
1491 if (row == wp->w_height)
1493 didline = TRUE;
1494 break;
1497 /* stop updating when hit the end of the file */
1498 if (lnum > buf->b_ml.ml_line_count)
1500 eof = TRUE;
1501 break;
1504 /* Remember the starting row of the line that is going to be dealt
1505 * with. It is used further down when the line doesn't fit. */
1506 srow = row;
1509 * Update a line when it is in an area that needs updating, when it
1510 * has changes or w_lines[idx] is invalid.
1511 * bot_start may be halfway a wrapped line after using
1512 * win_del_lines(), check if the current line includes it.
1513 * When syntax folding is being used, the saved syntax states will
1514 * already have been updated, we can't see where the syntax state is
1515 * the same again, just update until the end of the window.
1517 if (row < top_end
1518 || (row >= mid_start && row < mid_end)
1519 #ifdef FEAT_SEARCH_EXTRA
1520 || top_to_mod
1521 #endif
1522 || idx >= wp->w_lines_valid
1523 || (row + wp->w_lines[idx].wl_size > bot_start)
1524 || (mod_top != 0
1525 && (lnum == mod_top
1526 || (lnum >= mod_top
1527 && (lnum < mod_bot
1528 #ifdef FEAT_SYN_HL
1529 || did_update == DID_FOLD
1530 || (did_update == DID_LINE
1531 && syntax_present(buf)
1532 && (
1533 # ifdef FEAT_FOLDING
1534 (foldmethodIsSyntax(wp)
1535 && hasAnyFolding(wp)) ||
1536 # endif
1537 syntax_check_changed(lnum)))
1538 #endif
1539 )))))
1541 #ifdef FEAT_SEARCH_EXTRA
1542 if (lnum == mod_top)
1543 top_to_mod = FALSE;
1544 #endif
1547 * When at start of changed lines: May scroll following lines
1548 * up or down to minimize redrawing.
1549 * Don't do this when the change continues until the end.
1550 * Don't scroll when dollar_vcol is non-zero, keep the "$".
1552 if (lnum == mod_top
1553 && mod_bot != MAXLNUM
1554 && !(dollar_vcol != 0 && mod_bot == mod_top + 1))
1556 int old_rows = 0;
1557 int new_rows = 0;
1558 int xtra_rows;
1559 linenr_T l;
1561 /* Count the old number of window rows, using w_lines[], which
1562 * should still contain the sizes for the lines as they are
1563 * currently displayed. */
1564 for (i = idx; i < wp->w_lines_valid; ++i)
1566 /* Only valid lines have a meaningful wl_lnum. Invalid
1567 * lines are part of the changed area. */
1568 if (wp->w_lines[i].wl_valid
1569 && wp->w_lines[i].wl_lnum == mod_bot)
1570 break;
1571 old_rows += wp->w_lines[i].wl_size;
1572 #ifdef FEAT_FOLDING
1573 if (wp->w_lines[i].wl_valid
1574 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1576 /* Must have found the last valid entry above mod_bot.
1577 * Add following invalid entries. */
1578 ++i;
1579 while (i < wp->w_lines_valid
1580 && !wp->w_lines[i].wl_valid)
1581 old_rows += wp->w_lines[i++].wl_size;
1582 break;
1584 #endif
1587 if (i >= wp->w_lines_valid)
1589 /* We can't find a valid line below the changed lines,
1590 * need to redraw until the end of the window.
1591 * Inserting/deleting lines has no use. */
1592 bot_start = 0;
1594 else
1596 /* Able to count old number of rows: Count new window
1597 * rows, and may insert/delete lines */
1598 j = idx;
1599 for (l = lnum; l < mod_bot; ++l)
1601 #ifdef FEAT_FOLDING
1602 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1603 ++new_rows;
1604 else
1605 #endif
1606 #ifdef FEAT_DIFF
1607 if (l == wp->w_topline)
1608 new_rows += plines_win_nofill(wp, l, TRUE)
1609 + wp->w_topfill;
1610 else
1611 #endif
1612 new_rows += plines_win(wp, l, TRUE);
1613 ++j;
1614 if (new_rows > wp->w_height - row - 2)
1616 /* it's getting too much, must redraw the rest */
1617 new_rows = 9999;
1618 break;
1621 xtra_rows = new_rows - old_rows;
1622 if (xtra_rows < 0)
1624 /* May scroll text up. If there is not enough
1625 * remaining text or scrolling fails, must redraw the
1626 * rest. If scrolling works, must redraw the text
1627 * below the scrolled text. */
1628 if (row - xtra_rows >= wp->w_height - 2)
1629 mod_bot = MAXLNUM;
1630 else
1632 check_for_delay(FALSE);
1633 if (win_del_lines(wp, row,
1634 -xtra_rows, FALSE, FALSE) == FAIL)
1635 mod_bot = MAXLNUM;
1636 else
1637 bot_start = wp->w_height + xtra_rows;
1640 else if (xtra_rows > 0)
1642 /* May scroll text down. If there is not enough
1643 * remaining text of scrolling fails, must redraw the
1644 * rest. */
1645 if (row + xtra_rows >= wp->w_height - 2)
1646 mod_bot = MAXLNUM;
1647 else
1649 check_for_delay(FALSE);
1650 if (win_ins_lines(wp, row + old_rows,
1651 xtra_rows, FALSE, FALSE) == FAIL)
1652 mod_bot = MAXLNUM;
1653 else if (top_end > row + old_rows)
1654 /* Scrolled the part at the top that requires
1655 * updating down. */
1656 top_end += xtra_rows;
1660 /* When not updating the rest, may need to move w_lines[]
1661 * entries. */
1662 if (mod_bot != MAXLNUM && i != j)
1664 if (j < i)
1666 int x = row + new_rows;
1668 /* move entries in w_lines[] upwards */
1669 for (;;)
1671 /* stop at last valid entry in w_lines[] */
1672 if (i >= wp->w_lines_valid)
1674 wp->w_lines_valid = j;
1675 break;
1677 wp->w_lines[j] = wp->w_lines[i];
1678 /* stop at a line that won't fit */
1679 if (x + (int)wp->w_lines[j].wl_size
1680 > wp->w_height)
1682 wp->w_lines_valid = j + 1;
1683 break;
1685 x += wp->w_lines[j++].wl_size;
1686 ++i;
1688 if (bot_start > x)
1689 bot_start = x;
1691 else /* j > i */
1693 /* move entries in w_lines[] downwards */
1694 j -= i;
1695 wp->w_lines_valid += j;
1696 if (wp->w_lines_valid > wp->w_height)
1697 wp->w_lines_valid = wp->w_height;
1698 for (i = wp->w_lines_valid; i - j >= idx; --i)
1699 wp->w_lines[i] = wp->w_lines[i - j];
1701 /* The w_lines[] entries for inserted lines are
1702 * now invalid, but wl_size may be used above.
1703 * Reset to zero. */
1704 while (i >= idx)
1706 wp->w_lines[i].wl_size = 0;
1707 wp->w_lines[i--].wl_valid = FALSE;
1714 #ifdef FEAT_FOLDING
1716 * When lines are folded, display one line for all of them.
1717 * Otherwise, display normally (can be several display lines when
1718 * 'wrap' is on).
1720 fold_count = foldedCount(wp, lnum, &win_foldinfo);
1721 if (fold_count != 0)
1723 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
1724 ++row;
1725 --fold_count;
1726 wp->w_lines[idx].wl_folded = TRUE;
1727 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
1728 # ifdef FEAT_SYN_HL
1729 did_update = DID_FOLD;
1730 # endif
1732 else
1733 #endif
1734 if (idx < wp->w_lines_valid
1735 && wp->w_lines[idx].wl_valid
1736 && wp->w_lines[idx].wl_lnum == lnum
1737 && lnum > wp->w_topline
1738 && !(dy_flags & DY_LASTLINE)
1739 && srow + wp->w_lines[idx].wl_size > wp->w_height
1740 #ifdef FEAT_DIFF
1741 && diff_check_fill(wp, lnum) == 0
1742 #endif
1745 /* This line is not going to fit. Don't draw anything here,
1746 * will draw "@ " lines below. */
1747 row = wp->w_height + 1;
1749 else
1751 #ifdef FEAT_SEARCH_EXTRA
1752 prepare_search_hl(wp, lnum);
1753 #endif
1754 #ifdef FEAT_SYN_HL
1755 /* Let the syntax stuff know we skipped a few lines. */
1756 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
1757 && syntax_present(buf))
1758 syntax_end_parsing(syntax_last_parsed + 1);
1759 #endif
1762 * Display one line.
1764 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
1766 #ifdef FEAT_FOLDING
1767 wp->w_lines[idx].wl_folded = FALSE;
1768 wp->w_lines[idx].wl_lastlnum = lnum;
1769 #endif
1770 #ifdef FEAT_SYN_HL
1771 did_update = DID_LINE;
1772 syntax_last_parsed = lnum;
1773 #endif
1776 wp->w_lines[idx].wl_lnum = lnum;
1777 wp->w_lines[idx].wl_valid = TRUE;
1778 if (row > wp->w_height) /* past end of screen */
1780 /* we may need the size of that too long line later on */
1781 if (dollar_vcol == 0)
1782 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
1783 ++idx;
1784 break;
1786 if (dollar_vcol == 0)
1787 wp->w_lines[idx].wl_size = row - srow;
1788 ++idx;
1789 #ifdef FEAT_FOLDING
1790 lnum += fold_count + 1;
1791 #else
1792 ++lnum;
1793 #endif
1795 else
1797 /* This line does not need updating, advance to the next one */
1798 row += wp->w_lines[idx++].wl_size;
1799 if (row > wp->w_height) /* past end of screen */
1800 break;
1801 #ifdef FEAT_FOLDING
1802 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
1803 #else
1804 ++lnum;
1805 #endif
1806 #ifdef FEAT_SYN_HL
1807 did_update = DID_NONE;
1808 #endif
1811 if (lnum > buf->b_ml.ml_line_count)
1813 eof = TRUE;
1814 break;
1818 * End of loop over all window lines.
1822 if (idx > wp->w_lines_valid)
1823 wp->w_lines_valid = idx;
1825 #ifdef FEAT_SYN_HL
1827 * Let the syntax stuff know we stop parsing here.
1829 if (syntax_last_parsed != 0 && syntax_present(buf))
1830 syntax_end_parsing(syntax_last_parsed + 1);
1831 #endif
1834 * If we didn't hit the end of the file, and we didn't finish the last
1835 * line we were working on, then the line didn't fit.
1837 wp->w_empty_rows = 0;
1838 #ifdef FEAT_DIFF
1839 wp->w_filler_rows = 0;
1840 #endif
1841 if (!eof && !didline)
1843 if (lnum == wp->w_topline)
1846 * Single line that does not fit!
1847 * Don't overwrite it, it can be edited.
1849 wp->w_botline = lnum + 1;
1851 #ifdef FEAT_DIFF
1852 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
1854 /* Window ends in filler lines. */
1855 wp->w_botline = lnum;
1856 wp->w_filler_rows = wp->w_height - srow;
1858 #endif
1859 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
1862 * Last line isn't finished: Display "@@@" at the end.
1864 screen_fill(W_WINROW(wp) + wp->w_height - 1,
1865 W_WINROW(wp) + wp->w_height,
1866 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
1867 '@', '@', hl_attr(HLF_AT));
1868 set_empty_rows(wp, srow);
1869 wp->w_botline = lnum;
1871 else
1873 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
1874 wp->w_botline = lnum;
1877 else
1879 #ifdef FEAT_VERTSPLIT
1880 draw_vsep_win(wp, row);
1881 #endif
1882 if (eof) /* we hit the end of the file */
1884 wp->w_botline = buf->b_ml.ml_line_count + 1;
1885 #ifdef FEAT_DIFF
1886 j = diff_check_fill(wp, wp->w_botline);
1887 if (j > 0 && !wp->w_botfill)
1890 * Display filler lines at the end of the file
1892 if (char2cells(fill_diff) > 1)
1893 i = '-';
1894 else
1895 i = fill_diff;
1896 if (row + j > wp->w_height)
1897 j = wp->w_height - row;
1898 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
1899 row += j;
1901 #endif
1903 else if (dollar_vcol == 0)
1904 wp->w_botline = lnum;
1906 /* make sure the rest of the screen is blank */
1907 /* put '~'s on rows that aren't part of the file. */
1908 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_AT);
1911 /* Reset the type of redrawing required, the window has been updated. */
1912 wp->w_redr_type = 0;
1913 #ifdef FEAT_DIFF
1914 wp->w_old_topfill = wp->w_topfill;
1915 wp->w_old_botfill = wp->w_botfill;
1916 #endif
1918 if (dollar_vcol == 0)
1921 * There is a trick with w_botline. If we invalidate it on each
1922 * change that might modify it, this will cause a lot of expensive
1923 * calls to plines() in update_topline() each time. Therefore the
1924 * value of w_botline is often approximated, and this value is used to
1925 * compute the value of w_topline. If the value of w_botline was
1926 * wrong, check that the value of w_topline is correct (cursor is on
1927 * the visible part of the text). If it's not, we need to redraw
1928 * again. Mostly this just means scrolling up a few lines, so it
1929 * doesn't look too bad. Only do this for the current window (where
1930 * changes are relevant).
1932 wp->w_valid |= VALID_BOTLINE;
1933 if (wp == curwin && wp->w_botline != old_botline && !recursive)
1935 recursive = TRUE;
1936 curwin->w_valid &= ~VALID_TOPLINE;
1937 update_topline(); /* may invalidate w_botline again */
1938 if (must_redraw != 0)
1940 /* Don't update for changes in buffer again. */
1941 i = curbuf->b_mod_set;
1942 curbuf->b_mod_set = FALSE;
1943 win_update(curwin);
1944 must_redraw = 0;
1945 curbuf->b_mod_set = i;
1947 recursive = FALSE;
1951 #if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1952 /* restore got_int, unless CTRL-C was hit while redrawing */
1953 if (!got_int)
1954 got_int = save_got_int;
1955 #endif
1958 #ifdef FEAT_SIGNS
1959 static int draw_signcolumn __ARGS((win_T *wp));
1962 * Return TRUE when window "wp" has a column to draw signs in.
1964 static int
1965 draw_signcolumn(wp)
1966 win_T *wp;
1968 return (wp->w_buffer->b_signlist != NULL
1969 # ifdef FEAT_NETBEANS_INTG
1970 || usingNetbeans
1971 # endif
1974 #endif
1977 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
1978 * as the filler character.
1980 static void
1981 win_draw_end(wp, c1, c2, row, endrow, hl)
1982 win_T *wp;
1983 int c1;
1984 int c2;
1985 int row;
1986 int endrow;
1987 hlf_T hl;
1989 #if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
1990 int n = 0;
1991 # define FDC_OFF n
1992 #else
1993 # define FDC_OFF 0
1994 #endif
1996 #ifdef FEAT_RIGHTLEFT
1997 if (wp->w_p_rl)
1999 /* No check for cmdline window: should never be right-left. */
2000 # ifdef FEAT_FOLDING
2001 n = wp->w_p_fdc;
2003 if (n > 0)
2005 /* draw the fold column at the right */
2006 if (n > W_WIDTH(wp))
2007 n = W_WIDTH(wp);
2008 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2009 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
2010 ' ', ' ', hl_attr(HLF_FC));
2012 # endif
2013 # ifdef FEAT_SIGNS
2014 if (draw_signcolumn(wp))
2016 int nn = n + 2;
2018 /* draw the sign column left of the fold column */
2019 if (nn > W_WIDTH(wp))
2020 nn = W_WIDTH(wp);
2021 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2022 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
2023 ' ', ' ', hl_attr(HLF_SC));
2024 n = nn;
2026 # endif
2027 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2028 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
2029 c2, c2, hl_attr(hl));
2030 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2031 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
2032 c1, c2, hl_attr(hl));
2034 else
2035 #endif
2037 #ifdef FEAT_CMDWIN
2038 if (cmdwin_type != 0 && wp == curwin)
2040 /* draw the cmdline character in the leftmost column */
2041 n = 1;
2042 if (n > wp->w_width)
2043 n = wp->w_width;
2044 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2045 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
2046 cmdwin_type, ' ', hl_attr(HLF_AT));
2048 #endif
2049 #ifdef FEAT_FOLDING
2050 if (wp->w_p_fdc > 0)
2052 int nn = n + wp->w_p_fdc;
2054 /* draw the fold column at the left */
2055 if (nn > W_WIDTH(wp))
2056 nn = W_WIDTH(wp);
2057 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2058 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2059 ' ', ' ', hl_attr(HLF_FC));
2060 n = nn;
2062 #endif
2063 #ifdef FEAT_SIGNS
2064 if (draw_signcolumn(wp))
2066 int nn = n + 2;
2068 /* draw the sign column after the fold column */
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_SC));
2074 n = nn;
2076 #endif
2077 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2078 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
2079 c1, c2, hl_attr(hl));
2081 set_empty_rows(wp, row);
2084 #ifdef FEAT_FOLDING
2086 * Display one folded line.
2088 static void
2089 fold_line(wp, fold_count, foldinfo, lnum, row)
2090 win_T *wp;
2091 long fold_count;
2092 foldinfo_T *foldinfo;
2093 linenr_T lnum;
2094 int row;
2096 char_u buf[51];
2097 pos_T *top, *bot;
2098 linenr_T lnume = lnum + fold_count - 1;
2099 int len;
2100 char_u *text;
2101 int fdc;
2102 int col;
2103 int txtcol;
2104 int off = (int)(current_ScreenLine - ScreenLines);
2105 int ri;
2107 /* Build the fold line:
2108 * 1. Add the cmdwin_type for the command-line window
2109 * 2. Add the 'foldcolumn'
2110 * 3. Add the 'number' column
2111 * 4. Compose the text
2112 * 5. Add the text
2113 * 6. set highlighting for the Visual area an other text
2115 col = 0;
2118 * 1. Add the cmdwin_type for the command-line window
2119 * Ignores 'rightleft', this window is never right-left.
2121 #ifdef FEAT_CMDWIN
2122 if (cmdwin_type != 0 && wp == curwin)
2124 ScreenLines[off] = cmdwin_type;
2125 ScreenAttrs[off] = hl_attr(HLF_AT);
2126 #ifdef FEAT_MBYTE
2127 if (enc_utf8)
2128 ScreenLinesUC[off] = 0;
2129 #endif
2130 ++col;
2132 #endif
2135 * 2. Add the 'foldcolumn'
2137 fdc = wp->w_p_fdc;
2138 if (fdc > W_WIDTH(wp) - col)
2139 fdc = W_WIDTH(wp) - col;
2140 if (fdc > 0)
2142 fill_foldcolumn(buf, wp, TRUE, lnum);
2143 #ifdef FEAT_RIGHTLEFT
2144 if (wp->w_p_rl)
2146 int i;
2148 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
2149 hl_attr(HLF_FC));
2150 /* reverse the fold column */
2151 for (i = 0; i < fdc; ++i)
2152 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2154 else
2155 #endif
2156 copy_text_attr(off + col, buf, fdc, hl_attr(HLF_FC));
2157 col += fdc;
2160 #ifdef FEAT_RIGHTLEFT
2161 # define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2162 for (ri = 0; ri < l; ++ri) \
2163 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2164 else \
2165 for (ri = 0; ri < l; ++ri) \
2166 ScreenAttrs[off + (p) + ri] = v
2167 #else
2168 # define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2169 ScreenAttrs[off + (p) + ri] = v
2170 #endif
2172 /* Set all attributes of the 'number' column and the text */
2173 RL_MEMSET(col, hl_attr(HLF_FL), W_WIDTH(wp) - col);
2175 #ifdef FEAT_SIGNS
2176 /* If signs are being displayed, add two spaces. */
2177 if (draw_signcolumn(wp))
2179 len = W_WIDTH(wp) - col;
2180 if (len > 0)
2182 if (len > 2)
2183 len = 2;
2184 # ifdef FEAT_RIGHTLEFT
2185 if (wp->w_p_rl)
2186 /* the line number isn't reversed */
2187 copy_text_attr(off + W_WIDTH(wp) - len - col,
2188 (char_u *)" ", len, hl_attr(HLF_FL));
2189 else
2190 # endif
2191 copy_text_attr(off + col, (char_u *)" ", len, hl_attr(HLF_FL));
2192 col += len;
2195 #endif
2198 * 3. Add the 'number' column
2200 if (wp->w_p_nu)
2202 len = W_WIDTH(wp) - col;
2203 if (len > 0)
2205 int w = number_width(wp);
2207 if (len > w + 1)
2208 len = w + 1;
2209 sprintf((char *)buf, "%*ld ", w, (long)lnum);
2210 #ifdef FEAT_RIGHTLEFT
2211 if (wp->w_p_rl)
2212 /* the line number isn't reversed */
2213 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
2214 hl_attr(HLF_FL));
2215 else
2216 #endif
2217 copy_text_attr(off + col, buf, len, hl_attr(HLF_FL));
2218 col += len;
2223 * 4. Compose the folded-line string with 'foldtext', if set.
2225 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
2227 txtcol = col; /* remember where text starts */
2230 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2231 * Right-left text is put in columns 0 - number-col, normal text is put
2232 * in columns number-col - window-width.
2234 #ifdef FEAT_MBYTE
2235 if (has_mbyte)
2237 int cells;
2238 int u8c, u8cc[MAX_MCO];
2239 int i;
2240 int idx;
2241 int c_len;
2242 char_u *p;
2243 # ifdef FEAT_ARABIC
2244 int prev_c = 0; /* previous Arabic character */
2245 int prev_c1 = 0; /* first composing char for prev_c */
2246 # endif
2248 # ifdef FEAT_RIGHTLEFT
2249 if (wp->w_p_rl)
2250 idx = off;
2251 else
2252 # endif
2253 idx = off + col;
2255 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2256 for (p = text; *p != NUL; )
2258 cells = (*mb_ptr2cells)(p);
2259 c_len = (*mb_ptr2len)(p);
2260 if (col + cells > W_WIDTH(wp)
2261 # ifdef FEAT_RIGHTLEFT
2262 - (wp->w_p_rl ? col : 0)
2263 # endif
2265 break;
2266 ScreenLines[idx] = *p;
2267 if (enc_utf8)
2269 u8c = utfc_ptr2char(p, u8cc);
2270 if (*p < 0x80 && u8cc[0] == 0)
2272 ScreenLinesUC[idx] = 0;
2273 #ifdef FEAT_ARABIC
2274 prev_c = u8c;
2275 #endif
2277 else
2279 #ifdef FEAT_ARABIC
2280 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2282 /* Do Arabic shaping. */
2283 int pc, pc1, nc;
2284 int pcc[MAX_MCO];
2285 int firstbyte = *p;
2287 /* The idea of what is the previous and next
2288 * character depends on 'rightleft'. */
2289 if (wp->w_p_rl)
2291 pc = prev_c;
2292 pc1 = prev_c1;
2293 nc = utf_ptr2char(p + c_len);
2294 prev_c1 = u8cc[0];
2296 else
2298 pc = utfc_ptr2char(p + c_len, pcc);
2299 nc = prev_c;
2300 pc1 = pcc[0];
2302 prev_c = u8c;
2304 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2305 pc, pc1, nc);
2306 ScreenLines[idx] = firstbyte;
2308 else
2309 prev_c = u8c;
2310 #endif
2311 /* Non-BMP character: display as ? or fullwidth ?. */
2312 #ifdef UNICODE16
2313 if (u8c >= 0x10000)
2314 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2315 else
2316 #endif
2317 ScreenLinesUC[idx] = u8c;
2318 for (i = 0; i < Screen_mco; ++i)
2320 ScreenLinesC[i][idx] = u8cc[i];
2321 if (u8cc[i] == 0)
2322 break;
2325 if (cells > 1)
2326 ScreenLines[idx + 1] = 0;
2328 else if (cells > 1) /* double-byte character */
2330 if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2331 ScreenLines2[idx] = p[1];
2332 else
2333 ScreenLines[idx + 1] = p[1];
2335 col += cells;
2336 idx += cells;
2337 p += c_len;
2340 else
2341 #endif
2343 len = (int)STRLEN(text);
2344 if (len > W_WIDTH(wp) - col)
2345 len = W_WIDTH(wp) - col;
2346 if (len > 0)
2348 #ifdef FEAT_RIGHTLEFT
2349 if (wp->w_p_rl)
2350 STRNCPY(current_ScreenLine, text, len);
2351 else
2352 #endif
2353 STRNCPY(current_ScreenLine + col, text, len);
2354 col += len;
2358 /* Fill the rest of the line with the fold filler */
2359 #ifdef FEAT_RIGHTLEFT
2360 if (wp->w_p_rl)
2361 col -= txtcol;
2362 #endif
2363 while (col < W_WIDTH(wp)
2364 #ifdef FEAT_RIGHTLEFT
2365 - (wp->w_p_rl ? txtcol : 0)
2366 #endif
2369 #ifdef FEAT_MBYTE
2370 if (enc_utf8)
2372 if (fill_fold >= 0x80)
2374 ScreenLinesUC[off + col] = fill_fold;
2375 ScreenLinesC[0][off + col] = 0;
2377 else
2378 ScreenLinesUC[off + col] = 0;
2380 #endif
2381 ScreenLines[off + col++] = fill_fold;
2384 if (text != buf)
2385 vim_free(text);
2388 * 6. set highlighting for the Visual area an other text.
2389 * If all folded lines are in the Visual area, highlight the line.
2391 #ifdef FEAT_VISUAL
2392 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2394 if (ltoreq(curwin->w_cursor, VIsual))
2396 /* Visual is after curwin->w_cursor */
2397 top = &curwin->w_cursor;
2398 bot = &VIsual;
2400 else
2402 /* Visual is before curwin->w_cursor */
2403 top = &VIsual;
2404 bot = &curwin->w_cursor;
2406 if (lnum >= top->lnum
2407 && lnume <= bot->lnum
2408 && (VIsual_mode != 'v'
2409 || ((lnum > top->lnum
2410 || (lnum == top->lnum
2411 && top->col == 0))
2412 && (lnume < bot->lnum
2413 || (lnume == bot->lnum
2414 && (bot->col - (*p_sel == 'e'))
2415 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
2417 if (VIsual_mode == Ctrl_V)
2419 /* Visual block mode: highlight the chars part of the block */
2420 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2422 if (wp->w_old_cursor_lcol + txtcol < (colnr_T)W_WIDTH(wp))
2423 len = wp->w_old_cursor_lcol;
2424 else
2425 len = W_WIDTH(wp) - txtcol;
2426 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, hl_attr(HLF_V),
2427 len - (int)wp->w_old_cursor_fcol);
2430 else
2432 /* Set all attributes of the text */
2433 RL_MEMSET(txtcol, hl_attr(HLF_V), W_WIDTH(wp) - txtcol);
2437 #endif
2439 #ifdef FEAT_SYN_HL
2440 /* Show 'cursorcolumn' in the fold line. */
2441 if (wp->w_p_cuc)
2443 txtcol += wp->w_virtcol;
2444 if (wp->w_p_wrap)
2445 txtcol -= wp->w_skipcol;
2446 else
2447 txtcol -= wp->w_leftcol;
2448 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2449 ScreenAttrs[off + txtcol] = hl_combine_attr(
2450 ScreenAttrs[off + txtcol], hl_attr(HLF_CUC));
2452 #endif
2454 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2455 (int)W_WIDTH(wp), FALSE);
2458 * Update w_cline_height and w_cline_folded if the cursor line was
2459 * updated (saves a call to plines() later).
2461 if (wp == curwin
2462 && lnum <= curwin->w_cursor.lnum
2463 && lnume >= curwin->w_cursor.lnum)
2465 curwin->w_cline_row = row;
2466 curwin->w_cline_height = 1;
2467 curwin->w_cline_folded = TRUE;
2468 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2473 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2475 static void
2476 copy_text_attr(off, buf, len, attr)
2477 int off;
2478 char_u *buf;
2479 int len;
2480 int attr;
2482 int i;
2484 mch_memmove(ScreenLines + off, buf, (size_t)len);
2485 # ifdef FEAT_MBYTE
2486 if (enc_utf8)
2487 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2488 # endif
2489 for (i = 0; i < len; ++i)
2490 ScreenAttrs[off + i] = attr;
2494 * Fill the foldcolumn at "p" for window "wp".
2495 * Only to be called when 'foldcolumn' > 0.
2497 static void
2498 fill_foldcolumn(p, wp, closed, lnum)
2499 char_u *p;
2500 win_T *wp;
2501 int closed; /* TRUE of FALSE */
2502 linenr_T lnum; /* current line number */
2504 int i = 0;
2505 int level;
2506 int first_level;
2507 int empty;
2509 /* Init to all spaces. */
2510 copy_spaces(p, (size_t)wp->w_p_fdc);
2512 level = win_foldinfo.fi_level;
2513 if (level > 0)
2515 /* If there is only one column put more info in it. */
2516 empty = (wp->w_p_fdc == 1) ? 0 : 1;
2518 /* If the column is too narrow, we start at the lowest level that
2519 * fits and use numbers to indicated the depth. */
2520 first_level = level - wp->w_p_fdc - closed + 1 + empty;
2521 if (first_level < 1)
2522 first_level = 1;
2524 for (i = 0; i + empty < wp->w_p_fdc; ++i)
2526 if (win_foldinfo.fi_lnum == lnum
2527 && first_level + i >= win_foldinfo.fi_low_level)
2528 p[i] = '-';
2529 else if (first_level == 1)
2530 p[i] = '|';
2531 else if (first_level + i <= 9)
2532 p[i] = '0' + first_level + i;
2533 else
2534 p[i] = '>';
2535 if (first_level + i == level)
2536 break;
2539 if (closed)
2540 p[i >= wp->w_p_fdc ? i - 1 : i] = '+';
2542 #endif /* FEAT_FOLDING */
2545 * Display line "lnum" of window 'wp' on the screen.
2546 * Start at row "startrow", stop when "endrow" is reached.
2547 * wp->w_virtcol needs to be valid.
2549 * Return the number of last row the line occupies.
2551 static int
2552 win_line(wp, lnum, startrow, endrow, nochange)
2553 win_T *wp;
2554 linenr_T lnum;
2555 int startrow;
2556 int endrow;
2557 int nochange UNUSED; /* not updating for changed text */
2559 int col; /* visual column on screen */
2560 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2561 int c = 0; /* init for GCC */
2562 long vcol = 0; /* virtual column (for tabs) */
2563 long vcol_prev = -1; /* "vcol" of previous character */
2564 char_u *line; /* current line */
2565 char_u *ptr; /* current position in "line" */
2566 int row; /* row in the window, excl w_winrow */
2567 int screen_row; /* row on the screen, incl w_winrow */
2569 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2570 int n_extra = 0; /* number of extra chars */
2571 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
2572 int c_extra = NUL; /* extra chars, all the same */
2573 int extra_attr = 0; /* attributes when n_extra != 0 */
2574 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2575 displaying lcs_eol at end-of-line */
2576 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2577 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2579 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2580 int saved_n_extra = 0;
2581 char_u *saved_p_extra = NULL;
2582 int saved_c_extra = 0;
2583 int saved_char_attr = 0;
2585 int n_attr = 0; /* chars with special attr */
2586 int saved_attr2 = 0; /* char_attr saved for n_attr */
2587 int n_attr3 = 0; /* chars with overruling special attr */
2588 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2590 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2592 int fromcol, tocol; /* start/end of inverting */
2593 int fromcol_prev = -2; /* start of inverting after cursor */
2594 int noinvcur = FALSE; /* don't invert the cursor */
2595 #ifdef FEAT_VISUAL
2596 pos_T *top, *bot;
2597 int lnum_in_visual_area = FALSE;
2598 #endif
2599 pos_T pos;
2600 long v;
2602 int char_attr = 0; /* attributes for next character */
2603 int attr_pri = FALSE; /* char_attr has priority */
2604 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2605 in this line */
2606 int attr = 0; /* attributes for area highlighting */
2607 int area_attr = 0; /* attributes desired by highlighting */
2608 int search_attr = 0; /* attributes desired by 'hlsearch' */
2609 #ifdef FEAT_SYN_HL
2610 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
2611 int syntax_attr = 0; /* attributes desired by syntax */
2612 int has_syntax = FALSE; /* this buffer has syntax highl. */
2613 int save_did_emsg;
2614 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
2615 #endif
2616 #ifdef FEAT_SPELL
2617 int has_spell = FALSE; /* this buffer has spell checking */
2618 # define SPWORDLEN 150
2619 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
2620 int nextlinecol = 0; /* column where nextline[] starts */
2621 int nextline_idx = 0; /* index in nextline[] where next line
2622 starts */
2623 int spell_attr = 0; /* attributes desired by spelling */
2624 int word_end = 0; /* last byte with same spell_attr */
2625 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2626 static int checked_col = 0; /* column in "checked_lnum" up to which
2627 * there are no spell errors */
2628 static int cap_col = -1; /* column to check for Cap word */
2629 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
2630 int cur_checked_col = 0; /* checked column for current line */
2631 #endif
2632 int extra_check; /* has syntax or linebreak */
2633 #ifdef FEAT_MBYTE
2634 int multi_attr = 0; /* attributes desired by multibyte */
2635 int mb_l = 1; /* multi-byte byte length */
2636 int mb_c = 0; /* decoded multi-byte character */
2637 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
2638 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
2639 #endif
2640 #ifdef FEAT_DIFF
2641 int filler_lines; /* nr of filler lines to be drawn */
2642 int filler_todo; /* nr of filler lines still to do + 1 */
2643 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
2644 int change_start = MAXCOL; /* first col of changed area */
2645 int change_end = -1; /* last col of changed area */
2646 #endif
2647 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
2648 #ifdef FEAT_LINEBREAK
2649 int need_showbreak = FALSE;
2650 #endif
2651 #if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
2652 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
2653 # define LINE_ATTR
2654 int line_attr = 0; /* attribute for the whole line */
2655 #endif
2656 #ifdef FEAT_SEARCH_EXTRA
2657 matchitem_T *cur; /* points to the match list */
2658 match_T *shl; /* points to search_hl or a match */
2659 int shl_flag; /* flag to indicate whether search_hl
2660 has been processed or not */
2661 int prevcol_hl_flag; /* flag to indicate whether prevcol
2662 equals startcol of search_hl or one
2663 of the matches */
2664 #endif
2665 #ifdef FEAT_ARABIC
2666 int prev_c = 0; /* previous Arabic character */
2667 int prev_c1 = 0; /* first composing char for prev_c */
2668 #endif
2669 #if defined(LINE_ATTR)
2670 int did_line_attr = 0;
2671 #endif
2673 /* draw_state: items that are drawn in sequence: */
2674 #define WL_START 0 /* nothing done yet */
2675 #ifdef FEAT_CMDWIN
2676 # define WL_CMDLINE WL_START + 1 /* cmdline window column */
2677 #else
2678 # define WL_CMDLINE WL_START
2679 #endif
2680 #ifdef FEAT_FOLDING
2681 # define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
2682 #else
2683 # define WL_FOLD WL_CMDLINE
2684 #endif
2685 #ifdef FEAT_SIGNS
2686 # define WL_SIGN WL_FOLD + 1 /* column for signs */
2687 #else
2688 # define WL_SIGN WL_FOLD /* column for signs */
2689 #endif
2690 #define WL_NR WL_SIGN + 1 /* line number */
2691 #if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
2692 # define WL_SBR WL_NR + 1 /* 'showbreak' or 'diff' */
2693 #else
2694 # define WL_SBR WL_NR
2695 #endif
2696 #define WL_LINE WL_SBR + 1 /* text in the line */
2697 int draw_state = WL_START; /* what to draw next */
2698 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2699 int feedback_col = 0;
2700 int feedback_old_attr = -1;
2701 #endif
2704 if (startrow > endrow) /* past the end already! */
2705 return startrow;
2707 row = startrow;
2708 screen_row = row + W_WINROW(wp);
2711 * To speed up the loop below, set extra_check when there is linebreak,
2712 * trailing white space and/or syntax processing to be done.
2714 #ifdef FEAT_LINEBREAK
2715 extra_check = wp->w_p_lbr;
2716 #else
2717 extra_check = 0;
2718 #endif
2721 * Highlight the line if this buffer is in the code_check.c watchlist,
2722 * and there is an associated error in the corresponding error/warning
2723 * list.
2725 if (cc_is_buf_watched(curbuf)) {
2726 switch (cc_get_ew_type(curbuf, lnum)) {
2727 case /*CC_WARNING*/1:
2728 line_attr = hl_attr(HLF_WM);
2729 break;
2730 case /*CC_ERROR*/2:
2731 line_attr = hl_attr(HLF_E);
2732 break;
2733 default:
2734 line_attr = 0;
2735 break;
2739 #ifdef FEAT_SYN_HL
2740 if (syntax_present(wp->w_buffer) && !wp->w_buffer->b_syn_error)
2742 /* Prepare for syntax highlighting in this line. When there is an
2743 * error, stop syntax highlighting. */
2744 save_did_emsg = did_emsg;
2745 did_emsg = FALSE;
2746 syntax_start(wp, lnum);
2747 if (did_emsg)
2748 wp->w_buffer->b_syn_error = TRUE;
2749 else
2751 did_emsg = save_did_emsg;
2752 has_syntax = TRUE;
2753 extra_check = TRUE;
2756 #endif
2758 #ifdef FEAT_SPELL
2759 if (wp->w_p_spell
2760 && *wp->w_buffer->b_p_spl != NUL
2761 && wp->w_buffer->b_langp.ga_len > 0
2762 && *(char **)(wp->w_buffer->b_langp.ga_data) != NULL)
2764 /* Prepare for spell checking. */
2765 has_spell = TRUE;
2766 extra_check = TRUE;
2768 /* Get the start of the next line, so that words that wrap to the next
2769 * line are found too: "et<line-break>al.".
2770 * Trick: skip a few chars for C/shell/Vim comments */
2771 nextline[SPWORDLEN] = NUL;
2772 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2774 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
2775 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
2778 /* When a word wrapped from the previous line the start of the current
2779 * line is valid. */
2780 if (lnum == checked_lnum)
2781 cur_checked_col = checked_col;
2782 checked_lnum = 0;
2784 /* When there was a sentence end in the previous line may require a
2785 * word starting with capital in this line. In line 1 always check
2786 * the first word. */
2787 if (lnum != capcol_lnum)
2788 cap_col = -1;
2789 if (lnum == 1)
2790 cap_col = 0;
2791 capcol_lnum = 0;
2793 #endif
2796 * handle visual active in this window
2798 fromcol = -10;
2799 tocol = MAXCOL;
2800 #ifdef FEAT_VISUAL
2801 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2803 /* Visual is after curwin->w_cursor */
2804 if (ltoreq(curwin->w_cursor, VIsual))
2806 top = &curwin->w_cursor;
2807 bot = &VIsual;
2809 else /* Visual is before curwin->w_cursor */
2811 top = &VIsual;
2812 bot = &curwin->w_cursor;
2814 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
2815 if (VIsual_mode == Ctrl_V) /* block mode */
2817 if (lnum_in_visual_area)
2819 fromcol = wp->w_old_cursor_fcol;
2820 tocol = wp->w_old_cursor_lcol;
2823 else /* non-block mode */
2825 if (lnum > top->lnum && lnum <= bot->lnum)
2826 fromcol = 0;
2827 else if (lnum == top->lnum)
2829 if (VIsual_mode == 'V') /* linewise */
2830 fromcol = 0;
2831 else
2833 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
2834 if (gchar_pos(top) == NUL)
2835 tocol = fromcol + 1;
2838 if (VIsual_mode != 'V' && lnum == bot->lnum)
2840 if (*p_sel == 'e' && bot->col == 0
2841 #ifdef FEAT_VIRTUALEDIT
2842 && bot->coladd == 0
2843 #endif
2846 fromcol = -10;
2847 tocol = MAXCOL;
2849 else if (bot->col == MAXCOL)
2850 tocol = MAXCOL;
2851 else
2853 pos = *bot;
2854 if (*p_sel == 'e')
2855 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
2856 else
2858 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
2859 ++tocol;
2865 #ifndef MSDOS
2866 /* Check if the character under the cursor should not be inverted */
2867 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
2868 # ifdef FEAT_GUI
2869 && !gui.in_use
2870 # endif
2872 noinvcur = TRUE;
2873 #endif
2875 /* if inverting in this line set area_highlighting */
2876 if (fromcol >= 0)
2878 area_highlighting = TRUE;
2879 attr = hl_attr(HLF_V);
2880 #if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
2881 if (clip_star.available && !clip_star.owned && clip_isautosel())
2882 attr = hl_attr(HLF_VNC);
2883 #endif
2888 * handle 'incsearch' and ":s///c" highlighting
2890 else
2891 #endif /* FEAT_VISUAL */
2892 if (highlight_match
2893 && wp == curwin
2894 && lnum >= curwin->w_cursor.lnum
2895 && lnum <= curwin->w_cursor.lnum + search_match_lines)
2897 if (lnum == curwin->w_cursor.lnum)
2898 getvcol(curwin, &(curwin->w_cursor),
2899 (colnr_T *)&fromcol, NULL, NULL);
2900 else
2901 fromcol = 0;
2902 if (lnum == curwin->w_cursor.lnum + search_match_lines)
2904 pos.lnum = lnum;
2905 pos.col = search_match_endcol;
2906 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
2908 else
2909 tocol = MAXCOL;
2910 /* do at least one character; happens when past end of line */
2911 if (fromcol == tocol)
2912 tocol = fromcol + 1;
2913 area_highlighting = TRUE;
2914 attr = hl_attr(HLF_I);
2917 #ifdef FEAT_DIFF
2918 filler_lines = diff_check(wp, lnum);
2919 if (filler_lines < 0)
2921 if (filler_lines == -1)
2923 if (diff_find_change(wp, lnum, &change_start, &change_end))
2924 diff_hlf = HLF_ADD; /* added line */
2925 else if (change_start == 0)
2926 diff_hlf = HLF_TXD; /* changed text */
2927 else
2928 diff_hlf = HLF_CHD; /* changed line */
2930 else
2931 diff_hlf = HLF_ADD; /* added line */
2932 filler_lines = 0;
2933 area_highlighting = TRUE;
2935 if (lnum == wp->w_topline)
2936 filler_lines = wp->w_topfill;
2937 filler_todo = filler_lines;
2938 #endif
2940 #ifdef LINE_ATTR
2941 # ifdef FEAT_SIGNS
2942 /* If this line has a sign with line highlighting set line_attr. */
2943 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
2944 if (v != 0)
2945 line_attr = sign_get_attr((int)v, TRUE);
2946 # endif
2947 # if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
2948 /* Highlight the current line in the quickfix window. */
2949 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
2950 line_attr = hl_attr(HLF_L);
2951 # endif
2952 if (line_attr != 0)
2953 area_highlighting = TRUE;
2954 #endif
2956 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
2957 ptr = line;
2959 #ifdef FEAT_SPELL
2960 if (has_spell)
2962 /* For checking first word with a capital skip white space. */
2963 if (cap_col == 0)
2964 cap_col = (int)(skipwhite(line) - line);
2966 /* To be able to spell-check over line boundaries copy the end of the
2967 * current line into nextline[]. Above the start of the next line was
2968 * copied to nextline[SPWORDLEN]. */
2969 if (nextline[SPWORDLEN] == NUL)
2971 /* No next line or it is empty. */
2972 nextlinecol = MAXCOL;
2973 nextline_idx = 0;
2975 else
2977 v = (long)STRLEN(line);
2978 if (v < SPWORDLEN)
2980 /* Short line, use it completely and append the start of the
2981 * next line. */
2982 nextlinecol = 0;
2983 mch_memmove(nextline, line, (size_t)v);
2984 STRMOVE(nextline + v, nextline + SPWORDLEN);
2985 nextline_idx = v + 1;
2987 else
2989 /* Long line, use only the last SPWORDLEN bytes. */
2990 nextlinecol = v - SPWORDLEN;
2991 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
2992 nextline_idx = SPWORDLEN + 1;
2996 #endif
2998 /* find start of trailing whitespace */
2999 if (wp->w_p_list && lcs_trail)
3001 trailcol = (colnr_T)STRLEN(ptr);
3002 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
3003 --trailcol;
3004 trailcol += (colnr_T) (ptr - line);
3005 extra_check = TRUE;
3009 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3010 * first character to be displayed.
3012 if (wp->w_p_wrap)
3013 v = wp->w_skipcol;
3014 else
3015 v = wp->w_leftcol;
3016 if (v > 0)
3018 #ifdef FEAT_MBYTE
3019 char_u *prev_ptr = ptr;
3020 #endif
3021 while (vcol < v && *ptr != NUL)
3023 c = win_lbr_chartabsize(wp, ptr, (colnr_T)vcol, NULL);
3024 vcol += c;
3025 #ifdef FEAT_MBYTE
3026 prev_ptr = ptr;
3027 #endif
3028 mb_ptr_adv(ptr);
3031 #if defined(FEAT_SYN_HL) || defined(FEAT_VIRTUALEDIT) || defined(FEAT_VISUAL)
3032 /* When:
3033 * - 'cuc' is set, or
3034 * - 'virtualedit' is set, or
3035 * - the visual mode is active,
3036 * the end of the line may be before the start of the displayed part.
3038 if (vcol < v && (
3039 # ifdef FEAT_SYN_HL
3040 wp->w_p_cuc
3041 # if defined(FEAT_VIRTUALEDIT) || defined(FEAT_VISUAL)
3043 # endif
3044 # endif
3045 # ifdef FEAT_VIRTUALEDIT
3046 virtual_active()
3047 # ifdef FEAT_VISUAL
3049 # endif
3050 # endif
3051 # ifdef FEAT_VISUAL
3052 (VIsual_active && wp->w_buffer == curwin->w_buffer)
3053 # endif
3056 vcol = v;
3058 #endif
3060 /* Handle a character that's not completely on the screen: Put ptr at
3061 * that character but skip the first few screen characters. */
3062 if (vcol > v)
3064 vcol -= c;
3065 #ifdef FEAT_MBYTE
3066 ptr = prev_ptr;
3067 #else
3068 --ptr;
3069 #endif
3070 n_skip = v - vcol;
3074 * Adjust for when the inverted text is before the screen,
3075 * and when the start of the inverted text is before the screen.
3077 if (tocol <= vcol)
3078 fromcol = 0;
3079 else if (fromcol >= 0 && fromcol < vcol)
3080 fromcol = vcol;
3082 #ifdef FEAT_LINEBREAK
3083 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3084 if (wp->w_p_wrap)
3085 need_showbreak = TRUE;
3086 #endif
3087 #ifdef FEAT_SPELL
3088 /* When spell checking a word we need to figure out the start of the
3089 * word and if it's badly spelled or not. */
3090 if (has_spell)
3092 int len;
3093 colnr_T linecol = (colnr_T)(ptr - line);
3094 hlf_T spell_hlf = HLF_COUNT;
3096 pos = wp->w_cursor;
3097 wp->w_cursor.lnum = lnum;
3098 wp->w_cursor.col = linecol;
3099 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
3101 /* spell_move_to() may call ml_get() and make "line" invalid */
3102 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3103 ptr = line + linecol;
3105 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
3107 /* no bad word found at line start, don't check until end of a
3108 * word */
3109 spell_hlf = HLF_COUNT;
3110 word_end = (int)(spell_to_word_end(ptr, wp->w_buffer)
3111 - line + 1);
3113 else
3115 /* bad word found, use attributes until end of word */
3116 word_end = wp->w_cursor.col + len + 1;
3118 /* Turn index into actual attributes. */
3119 if (spell_hlf != HLF_COUNT)
3120 spell_attr = highlight_attr[spell_hlf];
3122 wp->w_cursor = pos;
3124 # ifdef FEAT_SYN_HL
3125 /* Need to restart syntax highlighting for this line. */
3126 if (has_syntax)
3127 syntax_start(wp, lnum);
3128 # endif
3130 #endif
3134 * Correct highlighting for cursor that can't be disabled.
3135 * Avoids having to check this for each character.
3137 if (fromcol >= 0)
3139 if (noinvcur)
3141 if ((colnr_T)fromcol == wp->w_virtcol)
3143 /* highlighting starts at cursor, let it start just after the
3144 * cursor */
3145 fromcol_prev = fromcol;
3146 fromcol = -1;
3148 else if ((colnr_T)fromcol < wp->w_virtcol)
3149 /* restart highlighting after the cursor */
3150 fromcol_prev = wp->w_virtcol;
3152 if (fromcol >= tocol)
3153 fromcol = -1;
3156 #ifdef FEAT_SEARCH_EXTRA
3158 * Handle highlighting the last used search pattern and matches.
3159 * Do this for both search_hl and the match list.
3161 cur = wp->w_match_head;
3162 shl_flag = FALSE;
3163 while (cur != NULL || shl_flag == FALSE)
3165 if (shl_flag == FALSE)
3167 shl = &search_hl;
3168 shl_flag = TRUE;
3170 else
3171 shl = &cur->hl;
3172 shl->startcol = MAXCOL;
3173 shl->endcol = MAXCOL;
3174 shl->attr_cur = 0;
3175 if (shl->rm.regprog != NULL)
3177 v = (long)(ptr - line);
3178 next_search_hl(wp, shl, lnum, (colnr_T)v);
3180 /* Need to get the line again, a multi-line regexp may have made it
3181 * invalid. */
3182 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3183 ptr = line + v;
3185 if (shl->lnum != 0 && shl->lnum <= lnum)
3187 if (shl->lnum == lnum)
3188 shl->startcol = shl->rm.startpos[0].col;
3189 else
3190 shl->startcol = 0;
3191 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3192 - shl->rm.startpos[0].lnum)
3193 shl->endcol = shl->rm.endpos[0].col;
3194 else
3195 shl->endcol = MAXCOL;
3196 /* Highlight one character for an empty match. */
3197 if (shl->startcol == shl->endcol)
3199 #ifdef FEAT_MBYTE
3200 if (has_mbyte && line[shl->endcol] != NUL)
3201 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3202 else
3203 #endif
3204 ++shl->endcol;
3206 if ((long)shl->startcol < v) /* match at leftcol */
3208 shl->attr_cur = shl->attr;
3209 search_attr = shl->attr;
3211 area_highlighting = TRUE;
3214 if (shl != &search_hl && cur != NULL)
3215 cur = cur->next;
3217 #endif
3219 #ifdef FEAT_SYN_HL
3220 /* Cursor line highlighting for 'cursorline'. Not when Visual mode is
3221 * active, because it's not clear what is selected then. */
3222 if (wp->w_p_cul && lnum == wp->w_cursor.lnum && !VIsual_active)
3224 line_attr = hl_attr(HLF_CUL);
3225 area_highlighting = TRUE;
3227 #endif
3229 off = (unsigned)(current_ScreenLine - ScreenLines);
3230 col = 0;
3231 #ifdef FEAT_RIGHTLEFT
3232 if (wp->w_p_rl)
3234 /* Rightleft window: process the text in the normal direction, but put
3235 * it in current_ScreenLine[] from right to left. Start at the
3236 * rightmost column of the window. */
3237 col = W_WIDTH(wp) - 1;
3238 off += col;
3240 #endif
3243 * Repeat for the whole displayed line.
3245 for (;;)
3247 /* Skip this quickly when working on the text. */
3248 if (draw_state != WL_LINE)
3250 #ifdef FEAT_CMDWIN
3251 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3253 draw_state = WL_CMDLINE;
3254 if (cmdwin_type != 0 && wp == curwin)
3256 /* Draw the cmdline character. */
3257 n_extra = 1;
3258 c_extra = cmdwin_type;
3259 char_attr = hl_attr(HLF_AT);
3262 #endif
3264 #ifdef FEAT_FOLDING
3265 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3267 draw_state = WL_FOLD;
3268 if (wp->w_p_fdc > 0)
3270 /* Draw the 'foldcolumn'. */
3271 fill_foldcolumn(extra, wp, FALSE, lnum);
3272 n_extra = wp->w_p_fdc;
3273 p_extra = extra;
3274 p_extra[n_extra] = NUL;
3275 c_extra = NUL;
3276 char_attr = hl_attr(HLF_FC);
3279 #endif
3281 #ifdef FEAT_SIGNS
3282 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3284 draw_state = WL_SIGN;
3285 /* Show the sign column when there are any signs in this
3286 * buffer or when using Netbeans. */
3287 if (draw_signcolumn(wp)
3288 # ifdef FEAT_DIFF
3289 && filler_todo <= 0
3290 # endif
3293 int_u text_sign;
3294 # ifdef FEAT_SIGN_ICONS
3295 int_u icon_sign;
3296 # endif
3298 /* Draw two cells with the sign value or blank. */
3299 c_extra = ' ';
3300 char_attr = hl_attr(HLF_SC);
3301 n_extra = 2;
3303 if (row == startrow)
3305 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3306 SIGN_TEXT);
3307 # ifdef FEAT_SIGN_ICONS
3308 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3309 SIGN_ICON);
3310 if (gui.in_use && icon_sign != 0)
3312 /* Use the image in this position. */
3313 c_extra = SIGN_BYTE;
3314 # ifdef FEAT_NETBEANS_INTG
3315 if (buf_signcount(wp->w_buffer, lnum) > 1)
3316 c_extra = MULTISIGN_BYTE;
3317 # endif
3318 char_attr = icon_sign;
3320 else
3321 # endif
3322 if (text_sign != 0)
3324 p_extra = sign_get_text(text_sign);
3325 if (p_extra != NULL)
3327 c_extra = NUL;
3328 n_extra = (int)STRLEN(p_extra);
3330 char_attr = sign_get_attr(text_sign, FALSE);
3335 #endif
3337 if (draw_state == WL_NR - 1 && n_extra == 0)
3339 draw_state = WL_NR;
3340 /* Display the line number. After the first fill with blanks
3341 * when the 'n' flag isn't in 'cpo' */
3342 if (wp->w_p_nu
3343 && (row == startrow
3344 #ifdef FEAT_DIFF
3345 + filler_lines
3346 #endif
3347 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3349 /* Draw the line number (empty space after wrapping). */
3350 if (row == startrow
3351 #ifdef FEAT_DIFF
3352 + filler_lines
3353 #endif
3356 sprintf((char *)extra, "%*ld ",
3357 number_width(wp), (long)lnum);
3358 if (wp->w_skipcol > 0)
3359 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3360 *p_extra = '-';
3361 #ifdef FEAT_RIGHTLEFT
3362 if (wp->w_p_rl) /* reverse line numbers */
3363 rl_mirror(extra);
3364 #endif
3365 p_extra = extra;
3366 c_extra = NUL;
3368 else
3369 c_extra = ' ';
3370 n_extra = number_width(wp) + 1;
3371 char_attr = hl_attr(HLF_N);
3372 #ifdef FEAT_SYN_HL
3373 /* When 'cursorline' is set highlight the line number of
3374 * the current line differently. */
3375 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3376 char_attr = hl_combine_attr(hl_attr(HLF_CUL), char_attr);
3377 #endif
3381 #if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3382 if (draw_state == WL_SBR - 1 && n_extra == 0)
3384 draw_state = WL_SBR;
3385 # ifdef FEAT_DIFF
3386 if (filler_todo > 0)
3388 /* Draw "deleted" diff line(s). */
3389 if (char2cells(fill_diff) > 1)
3390 c_extra = '-';
3391 else
3392 c_extra = fill_diff;
3393 # ifdef FEAT_RIGHTLEFT
3394 if (wp->w_p_rl)
3395 n_extra = col + 1;
3396 else
3397 # endif
3398 n_extra = W_WIDTH(wp) - col;
3399 char_attr = hl_attr(HLF_DED);
3401 # endif
3402 # ifdef FEAT_LINEBREAK
3403 if (*p_sbr != NUL && need_showbreak)
3405 /* Draw 'showbreak' at the start of each broken line. */
3406 p_extra = p_sbr;
3407 c_extra = NUL;
3408 n_extra = (int)STRLEN(p_sbr);
3409 char_attr = hl_attr(HLF_AT);
3410 need_showbreak = FALSE;
3411 /* Correct end of highlighted area for 'showbreak',
3412 * required when 'linebreak' is also set. */
3413 if (tocol == vcol)
3414 tocol += n_extra;
3416 # endif
3418 #endif
3420 if (draw_state == WL_LINE - 1 && n_extra == 0)
3422 draw_state = WL_LINE;
3423 if (saved_n_extra)
3425 /* Continue item from end of wrapped line. */
3426 n_extra = saved_n_extra;
3427 c_extra = saved_c_extra;
3428 p_extra = saved_p_extra;
3429 char_attr = saved_char_attr;
3431 else
3432 char_attr = 0;
3436 /* When still displaying '$' of change command, stop at cursor */
3437 if (dollar_vcol != 0 && wp == curwin
3438 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
3439 #ifdef FEAT_DIFF
3440 && filler_todo <= 0
3441 #endif
3444 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3445 wp->w_p_rl);
3446 /* Pretend we have finished updating the window. Except when
3447 * 'cursorcolumn' is set. */
3448 #ifdef FEAT_SYN_HL
3449 if (wp->w_p_cuc)
3450 row = wp->w_cline_row + wp->w_cline_height;
3451 else
3452 #endif
3453 row = wp->w_height;
3454 break;
3457 if (draw_state == WL_LINE && area_highlighting)
3459 /* handle Visual or match highlighting in this line */
3460 if (vcol == fromcol
3461 #ifdef FEAT_MBYTE
3462 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3463 && (*mb_ptr2cells)(ptr) > 1)
3464 #endif
3465 || ((int)vcol_prev == fromcol_prev
3466 && vcol_prev < vcol /* not at margin */
3467 && vcol < tocol))
3468 area_attr = attr; /* start highlighting */
3469 else if (area_attr != 0
3470 && (vcol == tocol
3471 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
3472 area_attr = 0; /* stop highlighting */
3474 #ifdef FEAT_SEARCH_EXTRA
3475 if (!n_extra)
3478 * Check for start/end of search pattern match.
3479 * After end, check for start/end of next match.
3480 * When another match, have to check for start again.
3481 * Watch out for matching an empty string!
3482 * Do this for 'search_hl' and the match list (ordered by
3483 * priority).
3485 v = (long)(ptr - line);
3486 cur = wp->w_match_head;
3487 shl_flag = FALSE;
3488 while (cur != NULL || shl_flag == FALSE)
3490 if (shl_flag == FALSE
3491 && ((cur != NULL
3492 && cur->priority > SEARCH_HL_PRIORITY)
3493 || cur == NULL))
3495 shl = &search_hl;
3496 shl_flag = TRUE;
3498 else
3499 shl = &cur->hl;
3500 while (shl->rm.regprog != NULL)
3502 if (shl->startcol != MAXCOL
3503 && v >= (long)shl->startcol
3504 && v < (long)shl->endcol)
3506 shl->attr_cur = shl->attr;
3508 else if (v == (long)shl->endcol)
3510 shl->attr_cur = 0;
3512 next_search_hl(wp, shl, lnum, (colnr_T)v);
3514 /* Need to get the line again, a multi-line regexp
3515 * may have made it invalid. */
3516 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3517 ptr = line + v;
3519 if (shl->lnum == lnum)
3521 shl->startcol = shl->rm.startpos[0].col;
3522 if (shl->rm.endpos[0].lnum == 0)
3523 shl->endcol = shl->rm.endpos[0].col;
3524 else
3525 shl->endcol = MAXCOL;
3527 if (shl->startcol == shl->endcol)
3529 /* highlight empty match, try again after
3530 * it */
3531 #ifdef FEAT_MBYTE
3532 if (has_mbyte)
3533 shl->endcol += (*mb_ptr2len)(line
3534 + shl->endcol);
3535 else
3536 #endif
3537 ++shl->endcol;
3540 /* Loop to check if the match starts at the
3541 * current position */
3542 continue;
3545 break;
3547 if (shl != &search_hl && cur != NULL)
3548 cur = cur->next;
3551 /* Use attributes from match with highest priority among
3552 * 'search_hl' and the match list. */
3553 search_attr = search_hl.attr_cur;
3554 cur = wp->w_match_head;
3555 shl_flag = FALSE;
3556 while (cur != NULL || shl_flag == FALSE)
3558 if (shl_flag == FALSE
3559 && ((cur != NULL
3560 && cur->priority > SEARCH_HL_PRIORITY)
3561 || cur == NULL))
3563 shl = &search_hl;
3564 shl_flag = TRUE;
3566 else
3567 shl = &cur->hl;
3568 if (shl->attr_cur != 0)
3569 search_attr = shl->attr_cur;
3570 if (shl != &search_hl && cur != NULL)
3571 cur = cur->next;
3574 #endif
3576 #ifdef FEAT_DIFF
3577 if (diff_hlf != (hlf_T)0)
3579 if (diff_hlf == HLF_CHD && ptr - line >= change_start
3580 && n_extra == 0)
3581 diff_hlf = HLF_TXD; /* changed text */
3582 if (diff_hlf == HLF_TXD && ptr - line > change_end
3583 && n_extra == 0)
3584 diff_hlf = HLF_CHD; /* changed line */
3585 line_attr = hl_attr(diff_hlf);
3587 #endif
3589 /* Decide which of the highlight attributes to use. */
3590 attr_pri = TRUE;
3591 if (area_attr != 0)
3592 char_attr = area_attr;
3593 else if (search_attr != 0)
3594 char_attr = search_attr;
3595 #ifdef LINE_ATTR
3596 /* Use line_attr when not in the Visual or 'incsearch' area
3597 * (area_attr may be 0 when "noinvcur" is set). */
3598 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
3599 || vcol < fromcol || vcol_prev < fromcol_prev
3600 || vcol >= tocol))
3601 char_attr = line_attr;
3602 #endif
3603 else
3605 attr_pri = FALSE;
3606 #ifdef FEAT_SYN_HL
3607 if (has_syntax)
3608 char_attr = syntax_attr;
3609 else
3610 #endif
3611 char_attr = 0;
3616 * Get the next character to put on the screen.
3619 * The "p_extra" points to the extra stuff that is inserted to
3620 * represent special characters (non-printable stuff) and other
3621 * things. When all characters are the same, c_extra is used.
3622 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
3623 * "p_extra[n_extra]".
3624 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
3626 if (n_extra > 0)
3628 if (c_extra != NUL)
3630 c = c_extra;
3631 #ifdef FEAT_MBYTE
3632 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
3633 if (enc_utf8 && (*mb_char2len)(c) > 1)
3635 mb_utf8 = TRUE;
3636 u8cc[0] = 0;
3637 c = 0xc0;
3639 else
3640 mb_utf8 = FALSE;
3641 #endif
3643 else
3645 c = *p_extra;
3646 #ifdef FEAT_MBYTE
3647 if (has_mbyte)
3649 mb_c = c;
3650 if (enc_utf8)
3652 /* If the UTF-8 character is more than one byte:
3653 * Decode it into "mb_c". */
3654 mb_l = (*mb_ptr2len)(p_extra);
3655 mb_utf8 = FALSE;
3656 if (mb_l > n_extra)
3657 mb_l = 1;
3658 else if (mb_l > 1)
3660 mb_c = utfc_ptr2char(p_extra, u8cc);
3661 mb_utf8 = TRUE;
3662 c = 0xc0;
3665 else
3667 /* if this is a DBCS character, put it in "mb_c" */
3668 mb_l = MB_BYTE2LEN(c);
3669 if (mb_l >= n_extra)
3670 mb_l = 1;
3671 else if (mb_l > 1)
3672 mb_c = (c << 8) + p_extra[1];
3674 if (mb_l == 0) /* at the NUL at end-of-line */
3675 mb_l = 1;
3677 /* If a double-width char doesn't fit display a '>' in the
3678 * last column. */
3679 if ((
3680 # ifdef FEAT_RIGHTLEFT
3681 wp->w_p_rl ? (col <= 0) :
3682 # endif
3683 (col >= W_WIDTH(wp) - 1))
3684 && (*mb_char2cells)(mb_c) == 2)
3686 c = '>';
3687 mb_c = c;
3688 mb_l = 1;
3689 mb_utf8 = FALSE;
3690 multi_attr = hl_attr(HLF_AT);
3691 /* put the pointer back to output the double-width
3692 * character at the start of the next line. */
3693 ++n_extra;
3694 --p_extra;
3696 else
3698 n_extra -= mb_l - 1;
3699 p_extra += mb_l - 1;
3702 #endif
3703 ++p_extra;
3705 --n_extra;
3707 else
3710 * Get a character from the line itself.
3712 c = *ptr;
3713 #ifdef FEAT_MBYTE
3714 if (has_mbyte)
3716 mb_c = c;
3717 if (enc_utf8)
3719 /* If the UTF-8 character is more than one byte: Decode it
3720 * into "mb_c". */
3721 mb_l = (*mb_ptr2len)(ptr);
3722 mb_utf8 = FALSE;
3723 if (mb_l > 1)
3725 mb_c = utfc_ptr2char(ptr, u8cc);
3726 /* Overlong encoded ASCII or ASCII with composing char
3727 * is displayed normally, except a NUL. */
3728 if (mb_c < 0x80)
3729 c = mb_c;
3730 mb_utf8 = TRUE;
3732 /* At start of the line we can have a composing char.
3733 * Draw it as a space with a composing char. */
3734 if (utf_iscomposing(mb_c))
3736 int i;
3738 for (i = Screen_mco - 1; i > 0; --i)
3739 u8cc[i] = u8cc[i - 1];
3740 u8cc[0] = mb_c;
3741 mb_c = ' ';
3745 if ((mb_l == 1 && c >= 0x80)
3746 || (mb_l >= 1 && mb_c == 0)
3747 || (mb_l > 1 && (!vim_isprintc(mb_c)
3748 # ifdef UNICODE16
3749 || mb_c >= 0x10000
3750 # endif
3754 * Illegal UTF-8 byte: display as <xx>.
3755 * Non-BMP character : display as ? or fullwidth ?.
3757 # ifdef UNICODE16
3758 if (mb_c < 0x10000)
3759 # endif
3761 transchar_hex(extra, mb_c);
3762 # ifdef FEAT_RIGHTLEFT
3763 if (wp->w_p_rl) /* reverse */
3764 rl_mirror(extra);
3765 # endif
3767 # ifdef UNICODE16
3768 else if (utf_char2cells(mb_c) != 2)
3769 STRCPY(extra, "?");
3770 else
3771 /* 0xff1f in UTF-8: full-width '?' */
3772 STRCPY(extra, "\357\274\237");
3773 # endif
3775 p_extra = extra;
3776 c = *p_extra;
3777 mb_c = mb_ptr2char_adv(&p_extra);
3778 mb_utf8 = (c >= 0x80);
3779 n_extra = (int)STRLEN(p_extra);
3780 c_extra = NUL;
3781 if (area_attr == 0 && search_attr == 0)
3783 n_attr = n_extra + 1;
3784 extra_attr = hl_attr(HLF_8);
3785 saved_attr2 = char_attr; /* save current attr */
3788 else if (mb_l == 0) /* at the NUL at end-of-line */
3789 mb_l = 1;
3790 #ifdef FEAT_ARABIC
3791 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
3793 /* Do Arabic shaping. */
3794 int pc, pc1, nc;
3795 int pcc[MAX_MCO];
3797 /* The idea of what is the previous and next
3798 * character depends on 'rightleft'. */
3799 if (wp->w_p_rl)
3801 pc = prev_c;
3802 pc1 = prev_c1;
3803 nc = utf_ptr2char(ptr + mb_l);
3804 prev_c1 = u8cc[0];
3806 else
3808 pc = utfc_ptr2char(ptr + mb_l, pcc);
3809 nc = prev_c;
3810 pc1 = pcc[0];
3812 prev_c = mb_c;
3814 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
3816 else
3817 prev_c = mb_c;
3818 #endif
3820 else /* enc_dbcs */
3822 mb_l = MB_BYTE2LEN(c);
3823 if (mb_l == 0) /* at the NUL at end-of-line */
3824 mb_l = 1;
3825 else if (mb_l > 1)
3827 /* We assume a second byte below 32 is illegal.
3828 * Hopefully this is OK for all double-byte encodings!
3830 if (ptr[1] >= 32)
3831 mb_c = (c << 8) + ptr[1];
3832 else
3834 if (ptr[1] == NUL)
3836 /* head byte at end of line */
3837 mb_l = 1;
3838 transchar_nonprint(extra, c);
3840 else
3842 /* illegal tail byte */
3843 mb_l = 2;
3844 STRCPY(extra, "XX");
3846 p_extra = extra;
3847 n_extra = (int)STRLEN(extra) - 1;
3848 c_extra = NUL;
3849 c = *p_extra++;
3850 if (area_attr == 0 && search_attr == 0)
3852 n_attr = n_extra + 1;
3853 extra_attr = hl_attr(HLF_8);
3854 saved_attr2 = char_attr; /* save current attr */
3856 mb_c = c;
3860 /* If a double-width char doesn't fit display a '>' in the
3861 * last column; the character is displayed at the start of the
3862 * next line. */
3863 if ((
3864 # ifdef FEAT_RIGHTLEFT
3865 wp->w_p_rl ? (col <= 0) :
3866 # endif
3867 (col >= W_WIDTH(wp) - 1))
3868 && (*mb_char2cells)(mb_c) == 2)
3870 c = '>';
3871 mb_c = c;
3872 mb_utf8 = FALSE;
3873 mb_l = 1;
3874 multi_attr = hl_attr(HLF_AT);
3875 /* Put pointer back so that the character will be
3876 * displayed at the start of the next line. */
3877 --ptr;
3879 else if (*ptr != NUL)
3880 ptr += mb_l - 1;
3882 /* If a double-width char doesn't fit at the left side display
3883 * a '<' in the first column. */
3884 if (n_skip > 0 && mb_l > 1)
3886 n_extra = 1;
3887 c_extra = '<';
3888 c = ' ';
3889 if (area_attr == 0 && search_attr == 0)
3891 n_attr = n_extra + 1;
3892 extra_attr = hl_attr(HLF_AT);
3893 saved_attr2 = char_attr; /* save current attr */
3895 mb_c = c;
3896 mb_utf8 = FALSE;
3897 mb_l = 1;
3901 #endif
3902 ++ptr;
3904 /* 'list' : change char 160 to lcs_nbsp. */
3905 if (wp->w_p_list && (c == 160
3906 #ifdef FEAT_MBYTE
3907 || (mb_utf8 && mb_c == 160)
3908 #endif
3909 ) && lcs_nbsp)
3911 c = lcs_nbsp;
3912 if (area_attr == 0 && search_attr == 0)
3914 n_attr = 1;
3915 extra_attr = hl_attr(HLF_8);
3916 saved_attr2 = char_attr; /* save current attr */
3918 #ifdef FEAT_MBYTE
3919 mb_c = c;
3920 if (enc_utf8 && (*mb_char2len)(c) > 1)
3922 mb_utf8 = TRUE;
3923 u8cc[0] = 0;
3924 c = 0xc0;
3926 else
3927 mb_utf8 = FALSE;
3928 #endif
3931 if (extra_check)
3933 #ifdef FEAT_SPELL
3934 int can_spell = TRUE;
3935 #endif
3937 #ifdef FEAT_SYN_HL
3938 /* Get syntax attribute, unless still at the start of the line
3939 * (double-wide char that doesn't fit). */
3940 v = (long)(ptr - line);
3941 if (has_syntax && v > 0)
3943 /* Get the syntax attribute for the character. If there
3944 * is an error, disable syntax highlighting. */
3945 save_did_emsg = did_emsg;
3946 did_emsg = FALSE;
3948 syntax_attr = get_syntax_attr((colnr_T)v - 1,
3949 # ifdef FEAT_SPELL
3950 has_spell ? &can_spell :
3951 # endif
3952 NULL, FALSE);
3954 if (did_emsg)
3956 wp->w_buffer->b_syn_error = TRUE;
3957 has_syntax = FALSE;
3959 else
3960 did_emsg = save_did_emsg;
3962 /* Need to get the line again, a multi-line regexp may
3963 * have made it invalid. */
3964 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3965 ptr = line + v;
3967 if (!attr_pri)
3968 char_attr = syntax_attr;
3969 else
3970 char_attr = hl_combine_attr(syntax_attr, char_attr);
3972 #endif
3974 #ifdef FEAT_SPELL
3975 /* Check spelling (unless at the end of the line).
3976 * Only do this when there is no syntax highlighting, the
3977 * @Spell cluster is not used or the current syntax item
3978 * contains the @Spell cluster. */
3979 if (has_spell && v >= word_end && v > cur_checked_col)
3981 spell_attr = 0;
3982 # ifdef FEAT_SYN_HL
3983 if (!attr_pri)
3984 char_attr = syntax_attr;
3985 # endif
3986 if (c != 0 && (
3987 # ifdef FEAT_SYN_HL
3988 !has_syntax ||
3989 # endif
3990 can_spell))
3992 char_u *prev_ptr, *p;
3993 int len;
3994 hlf_T spell_hlf = HLF_COUNT;
3995 # ifdef FEAT_MBYTE
3996 if (has_mbyte)
3998 prev_ptr = ptr - mb_l;
3999 v -= mb_l - 1;
4001 else
4002 # endif
4003 prev_ptr = ptr - 1;
4005 /* Use nextline[] if possible, it has the start of the
4006 * next line concatenated. */
4007 if ((prev_ptr - line) - nextlinecol >= 0)
4008 p = nextline + (prev_ptr - line) - nextlinecol;
4009 else
4010 p = prev_ptr;
4011 cap_col -= (int)(prev_ptr - line);
4012 len = spell_check(wp, p, &spell_hlf, &cap_col,
4013 nochange);
4014 word_end = v + len;
4016 /* In Insert mode only highlight a word that
4017 * doesn't touch the cursor. */
4018 if (spell_hlf != HLF_COUNT
4019 && (State & INSERT) != 0
4020 && wp->w_cursor.lnum == lnum
4021 && wp->w_cursor.col >=
4022 (colnr_T)(prev_ptr - line)
4023 && wp->w_cursor.col < (colnr_T)word_end)
4025 spell_hlf = HLF_COUNT;
4026 spell_redraw_lnum = lnum;
4029 if (spell_hlf == HLF_COUNT && p != prev_ptr
4030 && (p - nextline) + len > nextline_idx)
4032 /* Remember that the good word continues at the
4033 * start of the next line. */
4034 checked_lnum = lnum + 1;
4035 checked_col = (int)((p - nextline) + len - nextline_idx);
4038 /* Turn index into actual attributes. */
4039 if (spell_hlf != HLF_COUNT)
4040 spell_attr = highlight_attr[spell_hlf];
4042 if (cap_col > 0)
4044 if (p != prev_ptr
4045 && (p - nextline) + cap_col >= nextline_idx)
4047 /* Remember that the word in the next line
4048 * must start with a capital. */
4049 capcol_lnum = lnum + 1;
4050 cap_col = (int)((p - nextline) + cap_col
4051 - nextline_idx);
4053 else
4054 /* Compute the actual column. */
4055 cap_col += (int)(prev_ptr - line);
4059 if (spell_attr != 0)
4061 if (!attr_pri)
4062 char_attr = hl_combine_attr(char_attr, spell_attr);
4063 else
4064 char_attr = hl_combine_attr(spell_attr, char_attr);
4066 #endif
4067 #ifdef FEAT_LINEBREAK
4069 * Found last space before word: check for line break.
4071 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr)
4072 && !wp->w_p_list)
4074 n_extra = win_lbr_chartabsize(wp, ptr - (
4075 # ifdef FEAT_MBYTE
4076 has_mbyte ? mb_l :
4077 # endif
4078 1), (colnr_T)vcol, NULL) - 1;
4079 c_extra = ' ';
4080 if (vim_iswhite(c))
4081 c = ' ';
4083 #endif
4085 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4087 c = lcs_trail;
4088 if (!attr_pri)
4090 n_attr = 1;
4091 extra_attr = hl_attr(HLF_8);
4092 saved_attr2 = char_attr; /* save current attr */
4094 #ifdef FEAT_MBYTE
4095 mb_c = c;
4096 if (enc_utf8 && (*mb_char2len)(c) > 1)
4098 mb_utf8 = TRUE;
4099 u8cc[0] = 0;
4100 c = 0xc0;
4102 else
4103 mb_utf8 = FALSE;
4104 #endif
4109 * Handling of non-printable characters.
4111 if (!(chartab[c & 0xff] & CT_PRINT_CHAR))
4114 * when getting a character from the file, we may have to
4115 * turn it into something else on the way to putting it
4116 * into "ScreenLines".
4118 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4120 /* tab amount depends on current column */
4121 n_extra = (int)wp->w_buffer->b_p_ts
4122 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4123 #ifdef FEAT_MBYTE
4124 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4125 #endif
4126 if (wp->w_p_list)
4128 c = lcs_tab1;
4129 c_extra = lcs_tab2;
4130 n_attr = n_extra + 1;
4131 extra_attr = hl_attr(HLF_8);
4132 saved_attr2 = char_attr; /* save current attr */
4133 #ifdef FEAT_MBYTE
4134 mb_c = c;
4135 if (enc_utf8 && (*mb_char2len)(c) > 1)
4137 mb_utf8 = TRUE;
4138 u8cc[0] = 0;
4139 c = 0xc0;
4141 #endif
4143 else
4145 c_extra = ' ';
4146 c = ' ';
4149 else if (c == NUL
4150 && ((wp->w_p_list && lcs_eol > 0)
4151 || ((fromcol >= 0 || fromcol_prev >= 0)
4152 && tocol > vcol
4153 #ifdef FEAT_VISUAL
4154 && VIsual_mode != Ctrl_V
4155 #endif
4156 && (
4157 # ifdef FEAT_RIGHTLEFT
4158 wp->w_p_rl ? (col >= 0) :
4159 # endif
4160 (col < W_WIDTH(wp)))
4161 && !(noinvcur
4162 && lnum == wp->w_cursor.lnum
4163 && (colnr_T)vcol == wp->w_virtcol)))
4164 && lcs_eol_one >= 0)
4166 /* Display a '$' after the line or highlight an extra
4167 * character if the line break is included. */
4168 #if defined(FEAT_DIFF) || defined(LINE_ATTR)
4169 /* For a diff line the highlighting continues after the
4170 * "$". */
4171 if (
4172 # ifdef FEAT_DIFF
4173 diff_hlf == (hlf_T)0
4174 # ifdef LINE_ATTR
4176 # endif
4177 # endif
4178 # ifdef LINE_ATTR
4179 line_attr == 0
4180 # endif
4182 #endif
4184 #ifdef FEAT_VIRTUALEDIT
4185 /* In virtualedit, visual selections may extend
4186 * beyond end of line. */
4187 if (area_highlighting && virtual_active()
4188 && tocol != MAXCOL && vcol < tocol)
4189 n_extra = 0;
4190 else
4191 #endif
4193 p_extra = at_end_str;
4194 n_extra = 1;
4195 c_extra = NUL;
4198 if (wp->w_p_list)
4199 c = lcs_eol;
4200 else
4201 c = ' ';
4202 lcs_eol_one = -1;
4203 --ptr; /* put it back at the NUL */
4204 if (!attr_pri)
4206 extra_attr = hl_attr(HLF_AT);
4207 n_attr = 1;
4209 #ifdef FEAT_MBYTE
4210 mb_c = c;
4211 if (enc_utf8 && (*mb_char2len)(c) > 1)
4213 mb_utf8 = TRUE;
4214 u8cc[0] = 0;
4215 c = 0xc0;
4217 else
4218 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4219 #endif
4221 else if (c != NUL)
4223 p_extra = transchar(c);
4224 #ifdef FEAT_RIGHTLEFT
4225 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4226 rl_mirror(p_extra); /* reverse "<12>" */
4227 #endif
4228 n_extra = byte2cells(c) - 1;
4229 c_extra = NUL;
4230 c = *p_extra++;
4231 if (!attr_pri)
4233 n_attr = n_extra + 1;
4234 extra_attr = hl_attr(HLF_8);
4235 saved_attr2 = char_attr; /* save current attr */
4237 #ifdef FEAT_MBYTE
4238 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4239 #endif
4241 #ifdef FEAT_VIRTUALEDIT
4242 else if (VIsual_active
4243 && (VIsual_mode == Ctrl_V
4244 || VIsual_mode == 'v')
4245 && virtual_active()
4246 && tocol != MAXCOL
4247 && vcol < tocol
4248 && (
4249 # ifdef FEAT_RIGHTLEFT
4250 wp->w_p_rl ? (col >= 0) :
4251 # endif
4252 (col < W_WIDTH(wp))))
4254 c = ' ';
4255 --ptr; /* put it back at the NUL */
4257 #endif
4258 #if defined(LINE_ATTR)
4259 else if ((
4260 # ifdef FEAT_DIFF
4261 diff_hlf != (hlf_T)0 ||
4262 # endif
4263 line_attr != 0
4264 ) && (
4265 # ifdef FEAT_RIGHTLEFT
4266 wp->w_p_rl ? (col >= 0) :
4267 # endif
4268 (col < W_WIDTH(wp))))
4270 /* Highlight until the right side of the window */
4271 c = ' ';
4272 --ptr; /* put it back at the NUL */
4274 /* Remember we do the char for line highlighting. */
4275 ++did_line_attr;
4277 /* don't do search HL for the rest of the line */
4278 if (line_attr != 0 && char_attr == search_attr && col > 0)
4279 char_attr = line_attr;
4280 # ifdef FEAT_DIFF
4281 if (diff_hlf == HLF_TXD)
4283 diff_hlf = HLF_CHD;
4284 if (attr == 0 || char_attr != attr)
4285 char_attr = hl_attr(diff_hlf);
4287 # endif
4289 #endif
4293 /* Don't override visual selection highlighting. */
4294 if (n_attr > 0
4295 && draw_state == WL_LINE
4296 && !attr_pri)
4297 char_attr = extra_attr;
4299 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
4300 /* XIM don't send preedit_start and preedit_end, but they send
4301 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
4302 * im_is_preediting() here. */
4303 if (xic != NULL
4304 && lnum == wp->w_cursor.lnum
4305 && (State & INSERT)
4306 && !p_imdisable
4307 && im_is_preediting()
4308 && draw_state == WL_LINE)
4310 colnr_T tcol;
4312 if (preedit_end_col == MAXCOL)
4313 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
4314 else
4315 tcol = preedit_end_col;
4316 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
4318 if (feedback_old_attr < 0)
4320 feedback_col = 0;
4321 feedback_old_attr = char_attr;
4323 char_attr = im_get_feedback_attr(feedback_col);
4324 if (char_attr < 0)
4325 char_attr = feedback_old_attr;
4326 feedback_col++;
4328 else if (feedback_old_attr >= 0)
4330 char_attr = feedback_old_attr;
4331 feedback_old_attr = -1;
4332 feedback_col = 0;
4335 #endif
4337 * Handle the case where we are in column 0 but not on the first
4338 * character of the line and the user wants us to show us a
4339 * special character (via 'listchars' option "precedes:<char>".
4341 if (lcs_prec_todo != NUL
4342 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
4343 #ifdef FEAT_DIFF
4344 && filler_todo <= 0
4345 #endif
4346 && draw_state > WL_NR
4347 && c != NUL)
4349 c = lcs_prec;
4350 lcs_prec_todo = NUL;
4351 #ifdef FEAT_MBYTE
4352 mb_c = c;
4353 if (enc_utf8 && (*mb_char2len)(c) > 1)
4355 mb_utf8 = TRUE;
4356 u8cc[0] = 0;
4357 c = 0xc0;
4359 else
4360 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4361 #endif
4362 if (!attr_pri)
4364 saved_attr3 = char_attr; /* save current attr */
4365 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
4366 n_attr3 = 1;
4371 * At end of the text line or just after the last character.
4373 if (c == NUL
4374 #if defined(LINE_ATTR)
4375 || did_line_attr == 1
4376 #endif
4379 #ifdef FEAT_SEARCH_EXTRA
4380 long prevcol = (long)(ptr - line) - (c == NUL);
4382 /* we're not really at that column when skipping some text */
4383 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
4384 ++prevcol;
4385 #endif
4387 /* invert at least one char, used for Visual and empty line or
4388 * highlight match at end of line. If it's beyond the last
4389 * char on the screen, just overwrite that one (tricky!) Not
4390 * needed when a '$' was displayed for 'list'. */
4391 #ifdef FEAT_SEARCH_EXTRA
4392 prevcol_hl_flag = FALSE;
4393 if (prevcol == (long)search_hl.startcol)
4394 prevcol_hl_flag = TRUE;
4395 else
4397 cur = wp->w_match_head;
4398 while (cur != NULL)
4400 if (prevcol == (long)cur->hl.startcol)
4402 prevcol_hl_flag = TRUE;
4403 break;
4405 cur = cur->next;
4408 #endif
4409 if (lcs_eol == lcs_eol_one
4410 && ((area_attr != 0 && vcol == fromcol
4411 #ifdef FEAT_VISUAL
4412 && (VIsual_mode != Ctrl_V
4413 || lnum == VIsual.lnum
4414 || lnum == curwin->w_cursor.lnum)
4415 #endif
4416 && c == NUL)
4417 #ifdef FEAT_SEARCH_EXTRA
4418 /* highlight 'hlsearch' match at end of line */
4419 || (prevcol_hl_flag == TRUE
4420 # if defined(LINE_ATTR)
4421 && did_line_attr <= 1
4422 # endif
4424 #endif
4427 int n = 0;
4429 #ifdef FEAT_RIGHTLEFT
4430 if (wp->w_p_rl)
4432 if (col < 0)
4433 n = 1;
4435 else
4436 #endif
4438 if (col >= W_WIDTH(wp))
4439 n = -1;
4441 if (n != 0)
4443 /* At the window boundary, highlight the last character
4444 * instead (better than nothing). */
4445 off += n;
4446 col += n;
4448 else
4450 /* Add a blank character to highlight. */
4451 ScreenLines[off] = ' ';
4452 #ifdef FEAT_MBYTE
4453 if (enc_utf8)
4454 ScreenLinesUC[off] = 0;
4455 #endif
4457 #ifdef FEAT_SEARCH_EXTRA
4458 if (area_attr == 0)
4460 /* Use attributes from match with highest priority among
4461 * 'search_hl' and the match list. */
4462 char_attr = search_hl.attr;
4463 cur = wp->w_match_head;
4464 shl_flag = FALSE;
4465 while (cur != NULL || shl_flag == FALSE)
4467 if (shl_flag == FALSE
4468 && ((cur != NULL
4469 && cur->priority > SEARCH_HL_PRIORITY)
4470 || cur == NULL))
4472 shl = &search_hl;
4473 shl_flag = TRUE;
4475 else
4476 shl = &cur->hl;
4477 if ((ptr - line) - 1 == (long)shl->startcol)
4478 char_attr = shl->attr;
4479 if (shl != &search_hl && cur != NULL)
4480 cur = cur->next;
4483 #endif
4484 ScreenAttrs[off] = char_attr;
4485 #ifdef FEAT_RIGHTLEFT
4486 if (wp->w_p_rl)
4488 --col;
4489 --off;
4491 else
4492 #endif
4494 ++col;
4495 ++off;
4497 ++vcol;
4498 #ifdef FEAT_SYN_HL
4499 eol_hl_off = 1;
4500 #endif
4505 * At end of the text line.
4507 if (c == NUL)
4509 #ifdef FEAT_SYN_HL
4510 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
4511 && lnum == wp->w_cursor.lnum)
4513 /* highlight last char after line */
4514 --col;
4515 --off;
4516 --vcol;
4519 /* Highlight 'cursorcolumn' past end of the line. */
4520 if (wp->w_p_wrap)
4521 v = wp->w_skipcol;
4522 else
4523 v = wp->w_leftcol;
4524 /* check if line ends before left margin */
4525 if (vcol < v + col - win_col_off(wp))
4527 vcol = v + col - win_col_off(wp);
4528 if (wp->w_p_cuc
4529 && (int)wp->w_virtcol >= vcol - eol_hl_off
4530 && (int)wp->w_virtcol < W_WIDTH(wp) * (row - startrow + 1)
4532 && lnum != wp->w_cursor.lnum
4533 # ifdef FEAT_RIGHTLEFT
4534 && !wp->w_p_rl
4535 # endif
4538 while (col < W_WIDTH(wp))
4540 ScreenLines[off] = ' ';
4541 #ifdef FEAT_MBYTE
4542 if (enc_utf8)
4543 ScreenLinesUC[off] = 0;
4544 #endif
4545 ++col;
4546 if (vcol == (long)wp->w_virtcol)
4548 ScreenAttrs[off] = hl_attr(HLF_CUC);
4549 break;
4551 ScreenAttrs[off++] = 0;
4552 ++vcol;
4555 #endif
4557 SCREEN_LINE(screen_row, W_WINCOL(wp), col, (int)W_WIDTH(wp),
4558 wp->w_p_rl);
4559 row++;
4562 * Update w_cline_height and w_cline_folded if the cursor line was
4563 * updated (saves a call to plines() later).
4565 if (wp == curwin && lnum == curwin->w_cursor.lnum)
4567 curwin->w_cline_row = startrow;
4568 curwin->w_cline_height = row - startrow;
4569 #ifdef FEAT_FOLDING
4570 curwin->w_cline_folded = FALSE;
4571 #endif
4572 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
4575 break;
4578 /* line continues beyond line end */
4579 if (lcs_ext
4580 && !wp->w_p_wrap
4581 #ifdef FEAT_DIFF
4582 && filler_todo <= 0
4583 #endif
4584 && (
4585 #ifdef FEAT_RIGHTLEFT
4586 wp->w_p_rl ? col == 0 :
4587 #endif
4588 col == W_WIDTH(wp) - 1)
4589 && (*ptr != NUL
4590 || (wp->w_p_list && lcs_eol_one > 0)
4591 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
4593 c = lcs_ext;
4594 char_attr = hl_attr(HLF_AT);
4595 #ifdef FEAT_MBYTE
4596 mb_c = c;
4597 if (enc_utf8 && (*mb_char2len)(c) > 1)
4599 mb_utf8 = TRUE;
4600 u8cc[0] = 0;
4601 c = 0xc0;
4603 else
4604 mb_utf8 = FALSE;
4605 #endif
4608 #ifdef FEAT_SYN_HL
4609 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
4610 * highlight the cursor position itself. */
4611 if (wp->w_p_cuc && vcol == (long)wp->w_virtcol
4612 && lnum != wp->w_cursor.lnum
4613 && draw_state == WL_LINE
4614 && !lnum_in_visual_area)
4616 vcol_save_attr = char_attr;
4617 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
4619 else
4620 vcol_save_attr = -1;
4621 #endif
4624 * Store character to be displayed.
4625 * Skip characters that are left of the screen for 'nowrap'.
4627 vcol_prev = vcol;
4628 if (draw_state < WL_LINE || n_skip <= 0)
4631 * Store the character.
4633 #if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
4634 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
4636 /* A double-wide character is: put first halve in left cell. */
4637 --off;
4638 --col;
4640 #endif
4641 ScreenLines[off] = c;
4642 #ifdef FEAT_MBYTE
4643 if (enc_dbcs == DBCS_JPNU)
4644 ScreenLines2[off] = mb_c & 0xff;
4645 else if (enc_utf8)
4647 if (mb_utf8)
4649 int i;
4651 ScreenLinesUC[off] = mb_c;
4652 if ((c & 0xff) == 0)
4653 ScreenLines[off] = 0x80; /* avoid storing zero */
4654 for (i = 0; i < Screen_mco; ++i)
4656 ScreenLinesC[i][off] = u8cc[i];
4657 if (u8cc[i] == 0)
4658 break;
4661 else
4662 ScreenLinesUC[off] = 0;
4664 if (multi_attr)
4666 ScreenAttrs[off] = multi_attr;
4667 multi_attr = 0;
4669 else
4670 #endif
4671 ScreenAttrs[off] = char_attr;
4673 #ifdef FEAT_MBYTE
4674 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
4676 /* Need to fill two screen columns. */
4677 ++off;
4678 ++col;
4679 if (enc_utf8)
4680 /* UTF-8: Put a 0 in the second screen char. */
4681 ScreenLines[off] = 0;
4682 else
4683 /* DBCS: Put second byte in the second screen char. */
4684 ScreenLines[off] = mb_c & 0xff;
4685 ++vcol;
4686 /* When "tocol" is halfway a character, set it to the end of
4687 * the character, otherwise highlighting won't stop. */
4688 if (tocol == vcol)
4689 ++tocol;
4690 #ifdef FEAT_RIGHTLEFT
4691 if (wp->w_p_rl)
4693 /* now it's time to backup one cell */
4694 --off;
4695 --col;
4697 #endif
4699 #endif
4700 #ifdef FEAT_RIGHTLEFT
4701 if (wp->w_p_rl)
4703 --off;
4704 --col;
4706 else
4707 #endif
4709 ++off;
4710 ++col;
4713 else
4714 --n_skip;
4716 /* Only advance the "vcol" when after the 'number' column. */
4717 if (draw_state > WL_NR
4718 #ifdef FEAT_DIFF
4719 && filler_todo <= 0
4720 #endif
4722 ++vcol;
4724 #ifdef FEAT_SYN_HL
4725 if (vcol_save_attr >= 0)
4726 char_attr = vcol_save_attr;
4727 #endif
4729 /* restore attributes after "predeces" in 'listchars' */
4730 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
4731 char_attr = saved_attr3;
4733 /* restore attributes after last 'listchars' or 'number' char */
4734 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
4735 char_attr = saved_attr2;
4738 * At end of screen line and there is more to come: Display the line
4739 * so far. If there is no more to display it is caught above.
4741 if ((
4742 #ifdef FEAT_RIGHTLEFT
4743 wp->w_p_rl ? (col < 0) :
4744 #endif
4745 (col >= W_WIDTH(wp)))
4746 && (*ptr != NUL
4747 #ifdef FEAT_DIFF
4748 || filler_todo > 0
4749 #endif
4750 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
4751 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
4754 SCREEN_LINE(screen_row, W_WINCOL(wp), col, (int)W_WIDTH(wp),
4755 wp->w_p_rl);
4756 ++row;
4757 ++screen_row;
4759 /* When not wrapping and finished diff lines, or when displayed
4760 * '$' and highlighting until last column, break here. */
4761 if ((!wp->w_p_wrap
4762 #ifdef FEAT_DIFF
4763 && filler_todo <= 0
4764 #endif
4765 ) || lcs_eol_one == -1)
4766 break;
4768 /* When the window is too narrow draw all "@" lines. */
4769 if (draw_state != WL_LINE
4770 #ifdef FEAT_DIFF
4771 && filler_todo <= 0
4772 #endif
4775 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
4776 #ifdef FEAT_VERTSPLIT
4777 draw_vsep_win(wp, row);
4778 #endif
4779 row = endrow;
4782 /* When line got too long for screen break here. */
4783 if (row == endrow)
4785 ++row;
4786 break;
4789 if (screen_cur_row == screen_row - 1
4790 #ifdef FEAT_DIFF
4791 && filler_todo <= 0
4792 #endif
4793 && W_WIDTH(wp) == Columns)
4795 /* Remember that the line wraps, used for modeless copy. */
4796 LineWraps[screen_row - 1] = TRUE;
4799 * Special trick to make copy/paste of wrapped lines work with
4800 * xterm/screen: write an extra character beyond the end of
4801 * the line. This will work with all terminal types
4802 * (regardless of the xn,am settings).
4803 * Only do this on a fast tty.
4804 * Only do this if the cursor is on the current line
4805 * (something has been written in it).
4806 * Don't do this for the GUI.
4807 * Don't do this for double-width characters.
4808 * Don't do this for a window not at the right screen border.
4810 if (p_tf
4811 #ifdef FEAT_GUI
4812 && !gui.in_use
4813 #endif
4814 #ifdef FEAT_MBYTE
4815 && !(has_mbyte
4816 && ((*mb_off2cells)(LineOffset[screen_row],
4817 LineOffset[screen_row] + screen_Columns)
4818 == 2
4819 || (*mb_off2cells)(LineOffset[screen_row - 1]
4820 + (int)Columns - 2,
4821 LineOffset[screen_row] + screen_Columns)
4822 == 2))
4823 #endif
4826 /* First make sure we are at the end of the screen line,
4827 * then output the same character again to let the
4828 * terminal know about the wrap. If the terminal doesn't
4829 * auto-wrap, we overwrite the character. */
4830 if (screen_cur_col != W_WIDTH(wp))
4831 screen_char(LineOffset[screen_row - 1]
4832 + (unsigned)Columns - 1,
4833 screen_row - 1, (int)(Columns - 1));
4835 #ifdef FEAT_MBYTE
4836 /* When there is a multi-byte character, just output a
4837 * space to keep it simple. */
4838 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
4839 screen_row - 1] + (Columns - 1)]) > 1)
4840 out_char(' ');
4841 else
4842 #endif
4843 out_char(ScreenLines[LineOffset[screen_row - 1]
4844 + (Columns - 1)]);
4845 /* force a redraw of the first char on the next line */
4846 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
4847 screen_start(); /* don't know where cursor is now */
4851 col = 0;
4852 off = (unsigned)(current_ScreenLine - ScreenLines);
4853 #ifdef FEAT_RIGHTLEFT
4854 if (wp->w_p_rl)
4856 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
4857 off += col;
4859 #endif
4861 /* reset the drawing state for the start of a wrapped line */
4862 draw_state = WL_START;
4863 saved_n_extra = n_extra;
4864 saved_p_extra = p_extra;
4865 saved_c_extra = c_extra;
4866 saved_char_attr = char_attr;
4867 n_extra = 0;
4868 lcs_prec_todo = lcs_prec;
4869 #ifdef FEAT_LINEBREAK
4870 # ifdef FEAT_DIFF
4871 if (filler_todo <= 0)
4872 # endif
4873 need_showbreak = TRUE;
4874 #endif
4875 #ifdef FEAT_DIFF
4876 --filler_todo;
4877 /* When the filler lines are actually below the last line of the
4878 * file, don't draw the line itself, break here. */
4879 if (filler_todo == 0 && wp->w_botfill)
4880 break;
4881 #endif
4884 } /* for every character in the line */
4886 #ifdef FEAT_SPELL
4887 /* After an empty line check first word for capital. */
4888 if (*skipwhite(line) == NUL)
4890 capcol_lnum = lnum + 1;
4891 cap_col = 0;
4893 #endif
4895 return row;
4898 #ifdef FEAT_MBYTE
4899 static int comp_char_differs __ARGS((int, int));
4902 * Return if the composing characters at "off_from" and "off_to" differ.
4904 static int
4905 comp_char_differs(off_from, off_to)
4906 int off_from;
4907 int off_to;
4909 int i;
4911 for (i = 0; i < Screen_mco; ++i)
4913 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
4914 return TRUE;
4915 if (ScreenLinesC[i][off_from] == 0)
4916 break;
4918 return FALSE;
4920 #endif
4923 * Check whether the given character needs redrawing:
4924 * - the (first byte of the) character is different
4925 * - the attributes are different
4926 * - the character is multi-byte and the next byte is different
4927 * - the character is two cells wide and the second cell differs.
4929 static int
4930 char_needs_redraw(off_from, off_to, cols)
4931 int off_from;
4932 int off_to;
4933 int cols;
4935 if (cols > 0
4936 && ((ScreenLines[off_from] != ScreenLines[off_to]
4937 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
4939 #ifdef FEAT_MBYTE
4940 || (enc_dbcs != 0
4941 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
4942 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
4943 ? ScreenLines2[off_from] != ScreenLines2[off_to]
4944 : (cols > 1 && ScreenLines[off_from + 1]
4945 != ScreenLines[off_to + 1])))
4946 || (enc_utf8
4947 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
4948 || (ScreenLinesUC[off_from] != 0
4949 && comp_char_differs(off_from, off_to))
4950 || (cols > 1 && ScreenLines[off_from + 1]
4951 != ScreenLines[off_to + 1])))
4952 #endif
4954 return TRUE;
4955 return FALSE;
4959 * Move one "cooked" screen line to the screen, but only the characters that
4960 * have actually changed. Handle insert/delete character.
4961 * "coloff" gives the first column on the screen for this line.
4962 * "endcol" gives the columns where valid characters are.
4963 * "clear_width" is the width of the window. It's > 0 if the rest of the line
4964 * needs to be cleared, negative otherwise.
4965 * "rlflag" is TRUE in a rightleft window:
4966 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
4967 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
4969 static void
4970 screen_line(row, coloff, endcol, clear_width
4971 #ifdef FEAT_RIGHTLEFT
4972 , rlflag
4973 #endif
4975 int row;
4976 int coloff;
4977 int endcol;
4978 int clear_width;
4979 #ifdef FEAT_RIGHTLEFT
4980 int rlflag;
4981 #endif
4983 unsigned off_from;
4984 unsigned off_to;
4985 #ifdef FEAT_MBYTE
4986 unsigned max_off_from;
4987 unsigned max_off_to;
4988 #endif
4989 int col = 0;
4990 #if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_VERTSPLIT)
4991 int hl;
4992 #endif
4993 int force = FALSE; /* force update rest of the line */
4994 int redraw_this /* bool: does character need redraw? */
4995 #ifdef FEAT_GUI
4996 = TRUE /* For GUI when while-loop empty */
4997 #endif
4999 int redraw_next; /* redraw_this for next character */
5000 #ifdef FEAT_MBYTE
5001 int clear_next = FALSE;
5002 int char_cells; /* 1: normal char */
5003 /* 2: occupies two display cells */
5004 # define CHAR_CELLS char_cells
5005 #else
5006 # define CHAR_CELLS 1
5007 #endif
5009 # ifdef FEAT_CLIPBOARD
5010 clip_may_clear_selection(row, row);
5011 # endif
5013 off_from = (unsigned)(current_ScreenLine - ScreenLines);
5014 off_to = LineOffset[row] + coloff;
5015 #ifdef FEAT_MBYTE
5016 max_off_from = off_from + screen_Columns;
5017 max_off_to = LineOffset[row] + screen_Columns;
5018 #endif
5020 #ifdef FEAT_RIGHTLEFT
5021 if (rlflag)
5023 /* Clear rest first, because it's left of the text. */
5024 if (clear_width > 0)
5026 while (col <= endcol && ScreenLines[off_to] == ' '
5027 && ScreenAttrs[off_to] == 0
5028 # ifdef FEAT_MBYTE
5029 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5030 # endif
5033 ++off_to;
5034 ++col;
5036 if (col <= endcol)
5037 screen_fill(row, row + 1, col + coloff,
5038 endcol + coloff + 1, ' ', ' ', 0);
5040 col = endcol + 1;
5041 off_to = LineOffset[row] + col + coloff;
5042 off_from += col;
5043 endcol = (clear_width > 0 ? clear_width : -clear_width);
5045 #endif /* FEAT_RIGHTLEFT */
5047 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
5049 while (col < endcol)
5051 #ifdef FEAT_MBYTE
5052 if (has_mbyte && (col + 1 < endcol))
5053 char_cells = (*mb_off2cells)(off_from, max_off_from);
5054 else
5055 char_cells = 1;
5056 #endif
5058 redraw_this = redraw_next;
5059 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
5060 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
5062 #ifdef FEAT_GUI
5063 /* If the next character was bold, then redraw the current character to
5064 * remove any pixels that might have spilt over into us. This only
5065 * happens in the GUI.
5067 if (redraw_next && gui.in_use)
5069 hl = ScreenAttrs[off_to + CHAR_CELLS];
5070 if (hl > HL_ALL)
5071 hl = syn_attr2attr(hl);
5072 if (hl & HL_BOLD)
5073 redraw_this = TRUE;
5075 #endif
5077 if (redraw_this)
5080 * Special handling when 'xs' termcap flag set (hpterm):
5081 * Attributes for characters are stored at the position where the
5082 * cursor is when writing the highlighting code. The
5083 * start-highlighting code must be written with the cursor on the
5084 * first highlighted character. The stop-highlighting code must
5085 * be written with the cursor just after the last highlighted
5086 * character.
5087 * Overwriting a character doesn't remove it's highlighting. Need
5088 * to clear the rest of the line, and force redrawing it
5089 * completely.
5091 if ( p_wiv
5092 && !force
5093 #ifdef FEAT_GUI
5094 && !gui.in_use
5095 #endif
5096 && ScreenAttrs[off_to] != 0
5097 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
5100 * Need to remove highlighting attributes here.
5102 windgoto(row, col + coloff);
5103 out_str(T_CE); /* clear rest of this screen line */
5104 screen_start(); /* don't know where cursor is now */
5105 force = TRUE; /* force redraw of rest of the line */
5106 redraw_next = TRUE; /* or else next char would miss out */
5109 * If the previous character was highlighted, need to stop
5110 * highlighting at this character.
5112 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
5114 screen_attr = ScreenAttrs[off_to - 1];
5115 term_windgoto(row, col + coloff);
5116 screen_stop_highlight();
5118 else
5119 screen_attr = 0; /* highlighting has stopped */
5121 #ifdef FEAT_MBYTE
5122 if (enc_dbcs != 0)
5124 /* Check if overwriting a double-byte with a single-byte or
5125 * the other way around requires another character to be
5126 * redrawn. For UTF-8 this isn't needed, because comparing
5127 * ScreenLinesUC[] is sufficient. */
5128 if (char_cells == 1
5129 && col + 1 < endcol
5130 && (*mb_off2cells)(off_to, max_off_to) > 1)
5132 /* Writing a single-cell character over a double-cell
5133 * character: need to redraw the next cell. */
5134 ScreenLines[off_to + 1] = 0;
5135 redraw_next = TRUE;
5137 else if (char_cells == 2
5138 && col + 2 < endcol
5139 && (*mb_off2cells)(off_to, max_off_to) == 1
5140 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
5142 /* Writing the second half of a double-cell character over
5143 * a double-cell character: need to redraw the second
5144 * cell. */
5145 ScreenLines[off_to + 2] = 0;
5146 redraw_next = TRUE;
5149 if (enc_dbcs == DBCS_JPNU)
5150 ScreenLines2[off_to] = ScreenLines2[off_from];
5152 /* When writing a single-width character over a double-width
5153 * character and at the end of the redrawn text, need to clear out
5154 * the right halve of the old character.
5155 * Also required when writing the right halve of a double-width
5156 * char over the left halve of an existing one. */
5157 if (has_mbyte && col + char_cells == endcol
5158 && ((char_cells == 1
5159 && (*mb_off2cells)(off_to, max_off_to) > 1)
5160 || (char_cells == 2
5161 && (*mb_off2cells)(off_to, max_off_to) == 1
5162 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
5163 clear_next = TRUE;
5164 #endif
5166 ScreenLines[off_to] = ScreenLines[off_from];
5167 #ifdef FEAT_MBYTE
5168 if (enc_utf8)
5170 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
5171 if (ScreenLinesUC[off_from] != 0)
5173 int i;
5175 for (i = 0; i < Screen_mco; ++i)
5176 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
5179 if (char_cells == 2)
5180 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
5181 #endif
5183 #if defined(FEAT_GUI) || defined(UNIX)
5184 /* The bold trick makes a single column of pixels appear in the
5185 * next character. When a bold character is removed, the next
5186 * character should be redrawn too. This happens for our own GUI
5187 * and for some xterms. */
5188 if (
5189 # ifdef FEAT_GUI
5190 gui.in_use
5191 # endif
5192 # if defined(FEAT_GUI) && defined(UNIX)
5194 # endif
5195 # ifdef UNIX
5196 term_is_xterm
5197 # endif
5200 hl = ScreenAttrs[off_to];
5201 if (hl > HL_ALL)
5202 hl = syn_attr2attr(hl);
5203 if (hl & HL_BOLD)
5204 redraw_next = TRUE;
5206 #endif
5207 ScreenAttrs[off_to] = ScreenAttrs[off_from];
5208 #ifdef FEAT_MBYTE
5209 /* For simplicity set the attributes of second half of a
5210 * double-wide character equal to the first half. */
5211 if (char_cells == 2)
5212 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
5214 if (enc_dbcs != 0 && char_cells == 2)
5215 screen_char_2(off_to, row, col + coloff);
5216 else
5217 #endif
5218 screen_char(off_to, row, col + coloff);
5220 else if ( p_wiv
5221 #ifdef FEAT_GUI
5222 && !gui.in_use
5223 #endif
5224 && col + coloff > 0)
5226 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
5229 * Don't output stop-highlight when moving the cursor, it will
5230 * stop the highlighting when it should continue.
5232 screen_attr = 0;
5234 else if (screen_attr != 0)
5235 screen_stop_highlight();
5238 off_to += CHAR_CELLS;
5239 off_from += CHAR_CELLS;
5240 col += CHAR_CELLS;
5243 #ifdef FEAT_MBYTE
5244 if (clear_next)
5246 /* Clear the second half of a double-wide character of which the left
5247 * half was overwritten with a single-wide character. */
5248 ScreenLines[off_to] = ' ';
5249 if (enc_utf8)
5250 ScreenLinesUC[off_to] = 0;
5251 screen_char(off_to, row, col + coloff);
5253 #endif
5255 if (clear_width > 0
5256 #ifdef FEAT_RIGHTLEFT
5257 && !rlflag
5258 #endif
5261 #ifdef FEAT_GUI
5262 int startCol = col;
5263 #endif
5265 /* blank out the rest of the line */
5266 while (col < clear_width && ScreenLines[off_to] == ' '
5267 && ScreenAttrs[off_to] == 0
5268 #ifdef FEAT_MBYTE
5269 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5270 #endif
5273 ++off_to;
5274 ++col;
5276 if (col < clear_width)
5278 #ifdef FEAT_GUI
5280 * In the GUI, clearing the rest of the line may leave pixels
5281 * behind if the first character cleared was bold. Some bold
5282 * fonts spill over the left. In this case we redraw the previous
5283 * character too. If we didn't skip any blanks above, then we
5284 * only redraw if the character wasn't already redrawn anyway.
5286 if (gui.in_use && (col > startCol || !redraw_this))
5288 hl = ScreenAttrs[off_to];
5289 if (hl > HL_ALL || (hl & HL_BOLD))
5291 int prev_cells = 1;
5292 # ifdef FEAT_MBYTE
5293 if (enc_utf8)
5294 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
5295 * that its width is 2. */
5296 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
5297 else if (enc_dbcs != 0)
5299 /* find previous character by counting from first
5300 * column and get its width. */
5301 unsigned off = LineOffset[row];
5302 unsigned max_off = LineOffset[row] + screen_Columns;
5304 while (off < off_to)
5306 prev_cells = (*mb_off2cells)(off, max_off);
5307 off += prev_cells;
5311 if (enc_dbcs != 0 && prev_cells > 1)
5312 screen_char_2(off_to - prev_cells, row,
5313 col + coloff - prev_cells);
5314 else
5315 # endif
5316 screen_char(off_to - prev_cells, row,
5317 col + coloff - prev_cells);
5320 #endif
5321 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
5322 ' ', ' ', 0);
5323 #ifdef FEAT_VERTSPLIT
5324 off_to += clear_width - col;
5325 col = clear_width;
5326 #endif
5330 if (clear_width > 0)
5332 #ifdef FEAT_VERTSPLIT
5333 /* For a window that's left of another, draw the separator char. */
5334 if (col + coloff < Columns)
5336 int c;
5338 c = fillchar_vsep(&hl);
5339 if (ScreenLines[off_to] != c
5340 # ifdef FEAT_MBYTE
5341 || (enc_utf8 && (int)ScreenLinesUC[off_to]
5342 != (c >= 0x80 ? c : 0))
5343 # endif
5344 || ScreenAttrs[off_to] != hl)
5346 ScreenLines[off_to] = c;
5347 ScreenAttrs[off_to] = hl;
5348 # ifdef FEAT_MBYTE
5349 if (enc_utf8)
5351 if (c >= 0x80)
5353 ScreenLinesUC[off_to] = c;
5354 ScreenLinesC[0][off_to] = 0;
5356 else
5357 ScreenLinesUC[off_to] = 0;
5359 # endif
5360 screen_char(off_to, row, col + coloff);
5363 else
5364 #endif
5365 LineWraps[row] = FALSE;
5369 #if defined(FEAT_RIGHTLEFT) || defined(PROTO)
5371 * Mirror text "str" for right-left displaying.
5372 * Only works for single-byte characters (e.g., numbers).
5374 void
5375 rl_mirror(str)
5376 char_u *str;
5378 char_u *p1, *p2;
5379 int t;
5381 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
5383 t = *p1;
5384 *p1 = *p2;
5385 *p2 = t;
5388 #endif
5390 #if defined(FEAT_WINDOWS) || defined(PROTO)
5392 * mark all status lines for redraw; used after first :cd
5394 void
5395 status_redraw_all()
5397 win_T *wp;
5399 for (wp = firstwin; wp; wp = wp->w_next)
5400 if (wp->w_status_height)
5402 wp->w_redr_status = TRUE;
5403 redraw_later(VALID);
5408 * mark all status lines of the current buffer for redraw
5410 void
5411 status_redraw_curbuf()
5413 win_T *wp;
5415 for (wp = firstwin; wp; wp = wp->w_next)
5416 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
5418 wp->w_redr_status = TRUE;
5419 redraw_later(VALID);
5424 * Redraw all status lines that need to be redrawn.
5426 void
5427 redraw_statuslines()
5429 win_T *wp;
5431 for (wp = firstwin; wp; wp = wp->w_next)
5432 if (wp->w_redr_status)
5433 win_redr_status(wp);
5434 if (redraw_tabline)
5435 draw_tabline();
5437 #endif
5439 #if (defined(FEAT_WILDMENU) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
5441 * Redraw all status lines at the bottom of frame "frp".
5443 void
5444 win_redraw_last_status(frp)
5445 frame_T *frp;
5447 if (frp->fr_layout == FR_LEAF)
5448 frp->fr_win->w_redr_status = TRUE;
5449 else if (frp->fr_layout == FR_ROW)
5451 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
5452 win_redraw_last_status(frp);
5454 else /* frp->fr_layout == FR_COL */
5456 frp = frp->fr_child;
5457 while (frp->fr_next != NULL)
5458 frp = frp->fr_next;
5459 win_redraw_last_status(frp);
5462 #endif
5464 #ifdef FEAT_VERTSPLIT
5466 * Draw the verticap separator right of window "wp" starting with line "row".
5468 static void
5469 draw_vsep_win(wp, row)
5470 win_T *wp;
5471 int row;
5473 int hl;
5474 int c;
5476 if (wp->w_vsep_width)
5478 /* draw the vertical separator right of this window */
5479 c = fillchar_vsep(&hl);
5480 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
5481 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
5482 c, ' ', hl);
5485 #endif
5487 #ifdef FEAT_WILDMENU
5488 static int status_match_len __ARGS((expand_T *xp, char_u *s));
5489 static int skip_status_match_char __ARGS((expand_T *xp, char_u *s));
5492 * Get the length of an item as it will be shown in the status line.
5494 static int
5495 status_match_len(xp, s)
5496 expand_T *xp;
5497 char_u *s;
5499 int len = 0;
5501 #ifdef FEAT_MENU
5502 int emenu = (xp->xp_context == EXPAND_MENUS
5503 || xp->xp_context == EXPAND_MENUNAMES);
5505 /* Check for menu separators - replace with '|'. */
5506 if (emenu && menu_is_separator(s))
5507 return 1;
5508 #endif
5510 while (*s != NUL)
5512 s += skip_status_match_char(xp, s);
5513 len += ptr2cells(s);
5514 mb_ptr_adv(s);
5517 return len;
5521 * Return the number of characters that should be skipped in a status match.
5522 * These are backslashes used for escaping. Do show backslashes in help tags.
5524 static int
5525 skip_status_match_char(xp, s)
5526 expand_T *xp;
5527 char_u *s;
5529 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
5530 #ifdef FEAT_MENU
5531 || ((xp->xp_context == EXPAND_MENUS
5532 || xp->xp_context == EXPAND_MENUNAMES)
5533 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
5534 #endif
5537 #ifndef BACKSLASH_IN_FILENAME
5538 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
5539 return 2;
5540 #endif
5541 return 1;
5543 return 0;
5547 * Show wildchar matches in the status line.
5548 * Show at least the "match" item.
5549 * We start at item 'first_match' in the list and show all matches that fit.
5551 * If inversion is possible we use it. Else '=' characters are used.
5553 void
5554 win_redr_status_matches(xp, num_matches, matches, match, showtail)
5555 expand_T *xp;
5556 int num_matches;
5557 char_u **matches; /* list of matches */
5558 int match;
5559 int showtail;
5561 #define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
5562 int row;
5563 char_u *buf;
5564 int len;
5565 int clen; /* length in screen cells */
5566 int fillchar;
5567 int attr;
5568 int i;
5569 int highlight = TRUE;
5570 char_u *selstart = NULL;
5571 int selstart_col = 0;
5572 char_u *selend = NULL;
5573 static int first_match = 0;
5574 int add_left = FALSE;
5575 char_u *s;
5576 #ifdef FEAT_MENU
5577 int emenu;
5578 #endif
5579 #if defined(FEAT_MBYTE) || defined(FEAT_MENU)
5580 int l;
5581 #endif
5583 if (matches == NULL) /* interrupted completion? */
5584 return;
5586 #ifdef FEAT_MBYTE
5587 if (has_mbyte)
5588 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
5589 else
5590 #endif
5591 buf = alloc((unsigned)Columns + 1);
5592 if (buf == NULL)
5593 return;
5595 if (match == -1) /* don't show match but original text */
5597 match = 0;
5598 highlight = FALSE;
5600 /* count 1 for the ending ">" */
5601 clen = status_match_len(xp, L_MATCH(match)) + 3;
5602 if (match == 0)
5603 first_match = 0;
5604 else if (match < first_match)
5606 /* jumping left, as far as we can go */
5607 first_match = match;
5608 add_left = TRUE;
5610 else
5612 /* check if match fits on the screen */
5613 for (i = first_match; i < match; ++i)
5614 clen += status_match_len(xp, L_MATCH(i)) + 2;
5615 if (first_match > 0)
5616 clen += 2;
5617 /* jumping right, put match at the left */
5618 if ((long)clen > Columns)
5620 first_match = match;
5621 /* if showing the last match, we can add some on the left */
5622 clen = 2;
5623 for (i = match; i < num_matches; ++i)
5625 clen += status_match_len(xp, L_MATCH(i)) + 2;
5626 if ((long)clen >= Columns)
5627 break;
5629 if (i == num_matches)
5630 add_left = TRUE;
5633 if (add_left)
5634 while (first_match > 0)
5636 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
5637 if ((long)clen >= Columns)
5638 break;
5639 --first_match;
5642 fillchar = fillchar_status(&attr, TRUE);
5644 if (first_match == 0)
5646 *buf = NUL;
5647 len = 0;
5649 else
5651 STRCPY(buf, "< ");
5652 len = 2;
5654 clen = len;
5656 i = first_match;
5657 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
5659 if (i == match)
5661 selstart = buf + len;
5662 selstart_col = clen;
5665 s = L_MATCH(i);
5666 /* Check for menu separators - replace with '|' */
5667 #ifdef FEAT_MENU
5668 emenu = (xp->xp_context == EXPAND_MENUS
5669 || xp->xp_context == EXPAND_MENUNAMES);
5670 if (emenu && menu_is_separator(s))
5672 STRCPY(buf + len, transchar('|'));
5673 l = (int)STRLEN(buf + len);
5674 len += l;
5675 clen += l;
5677 else
5678 #endif
5679 for ( ; *s != NUL; ++s)
5681 s += skip_status_match_char(xp, s);
5682 clen += ptr2cells(s);
5683 #ifdef FEAT_MBYTE
5684 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
5686 STRNCPY(buf + len, s, l);
5687 s += l - 1;
5688 len += l;
5690 else
5691 #endif
5693 STRCPY(buf + len, transchar_byte(*s));
5694 len += (int)STRLEN(buf + len);
5697 if (i == match)
5698 selend = buf + len;
5700 *(buf + len++) = ' ';
5701 *(buf + len++) = ' ';
5702 clen += 2;
5703 if (++i == num_matches)
5704 break;
5707 if (i != num_matches)
5709 *(buf + len++) = '>';
5710 ++clen;
5713 buf[len] = NUL;
5715 row = cmdline_row - 1;
5716 if (row >= 0)
5718 if (wild_menu_showing == 0)
5720 if (msg_scrolled > 0)
5722 /* Put the wildmenu just above the command line. If there is
5723 * no room, scroll the screen one line up. */
5724 if (cmdline_row == Rows - 1)
5726 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
5727 ++msg_scrolled;
5729 else
5731 ++cmdline_row;
5732 ++row;
5734 wild_menu_showing = WM_SCROLLED;
5736 else
5738 /* Create status line if needed by setting 'laststatus' to 2.
5739 * Set 'winminheight' to zero to avoid that the window is
5740 * resized. */
5741 if (lastwin->w_status_height == 0)
5743 save_p_ls = p_ls;
5744 save_p_wmh = p_wmh;
5745 p_ls = 2;
5746 p_wmh = 0;
5747 last_status(FALSE);
5749 wild_menu_showing = WM_SHOWN;
5753 screen_puts(buf, row, 0, attr);
5754 if (selstart != NULL && highlight)
5756 *selend = NUL;
5757 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
5760 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
5763 #ifdef FEAT_VERTSPLIT
5764 win_redraw_last_status(topframe);
5765 #else
5766 lastwin->w_redr_status = TRUE;
5767 #endif
5768 vim_free(buf);
5770 #endif
5772 #if defined(FEAT_WINDOWS) || defined(PROTO)
5774 * Redraw the status line of window wp.
5776 * If inversion is possible we use it. Else '=' characters are used.
5778 void
5779 win_redr_status(wp)
5780 win_T *wp;
5782 int row;
5783 char_u *p;
5784 int len;
5785 int fillchar;
5786 int attr;
5787 int this_ru_col;
5788 static int busy = FALSE;
5790 /* It's possible to get here recursively when 'statusline' (indirectly)
5791 * invokes ":redrawstatus". Simply ignore the call then. */
5792 if (busy)
5793 return;
5794 busy = TRUE;
5796 wp->w_redr_status = FALSE;
5797 if (wp->w_status_height == 0)
5799 /* no status line, can only be last window */
5800 redraw_cmdline = TRUE;
5802 else if (!redrawing()
5803 #ifdef FEAT_INS_EXPAND
5804 /* don't update status line when popup menu is visible and may be
5805 * drawn over it */
5806 || pum_visible()
5807 #endif
5810 /* Don't redraw right now, do it later. */
5811 wp->w_redr_status = TRUE;
5813 #ifdef FEAT_STL_OPT
5814 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
5816 /* redraw custom status line */
5817 redraw_custom_statusline(wp);
5819 #endif
5820 else
5822 fillchar = fillchar_status(&attr, wp == curwin);
5824 get_trans_bufname(wp->w_buffer);
5825 p = NameBuff;
5826 len = (int)STRLEN(p);
5828 if (wp->w_buffer->b_help
5829 #ifdef FEAT_QUICKFIX
5830 || wp->w_p_pvw
5831 #endif
5832 || bufIsChanged(wp->w_buffer)
5833 || wp->w_buffer->b_p_ro)
5834 *(p + len++) = ' ';
5835 if (wp->w_buffer->b_help)
5837 STRCPY(p + len, _("[Help]"));
5838 len += (int)STRLEN(p + len);
5840 #ifdef FEAT_QUICKFIX
5841 if (wp->w_p_pvw)
5843 STRCPY(p + len, _("[Preview]"));
5844 len += (int)STRLEN(p + len);
5846 #endif
5847 if (bufIsChanged(wp->w_buffer))
5849 STRCPY(p + len, "[+]");
5850 len += 3;
5852 if (wp->w_buffer->b_p_ro)
5854 STRCPY(p + len, "[RO]");
5855 len += 4;
5858 #ifndef FEAT_VERTSPLIT
5859 this_ru_col = ru_col;
5860 if (this_ru_col < (Columns + 1) / 2)
5861 this_ru_col = (Columns + 1) / 2;
5862 #else
5863 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
5864 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
5865 this_ru_col = (W_WIDTH(wp) + 1) / 2;
5866 if (this_ru_col <= 1)
5868 p = (char_u *)"<"; /* No room for file name! */
5869 len = 1;
5871 else
5872 #endif
5873 #ifdef FEAT_MBYTE
5874 if (has_mbyte)
5876 int clen = 0, i;
5878 /* Count total number of display cells. */
5879 for (i = 0; p[i] != NUL; i += (*mb_ptr2len)(p + i))
5880 clen += (*mb_ptr2cells)(p + i);
5881 /* Find first character that will fit.
5882 * Going from start to end is much faster for DBCS. */
5883 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
5884 i += (*mb_ptr2len)(p + i))
5885 clen -= (*mb_ptr2cells)(p + i);
5886 len = clen;
5887 if (i > 0)
5889 p = p + i - 1;
5890 *p = '<';
5891 ++len;
5895 else
5896 #endif
5897 if (len > this_ru_col - 1)
5899 p += len - (this_ru_col - 1);
5900 *p = '<';
5901 len = this_ru_col - 1;
5904 row = W_WINROW(wp) + wp->w_height;
5905 screen_puts(p, row, W_WINCOL(wp), attr);
5906 screen_fill(row, row + 1, len + W_WINCOL(wp),
5907 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
5909 if (get_keymap_str(wp, NameBuff, MAXPATHL)
5910 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
5911 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
5912 - 1 + W_WINCOL(wp)), attr);
5914 #ifdef FEAT_CMDL_INFO
5915 win_redr_ruler(wp, TRUE);
5916 #endif
5919 #ifdef FEAT_VERTSPLIT
5921 * May need to draw the character below the vertical separator.
5923 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
5925 if (stl_connected(wp))
5926 fillchar = fillchar_status(&attr, wp == curwin);
5927 else
5928 fillchar = fillchar_vsep(&attr);
5929 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
5930 attr);
5932 #endif
5933 busy = FALSE;
5936 #ifdef FEAT_STL_OPT
5938 * Redraw the status line according to 'statusline' and take care of any
5939 * errors encountered.
5941 static void
5942 redraw_custom_statusline(wp)
5943 win_T *wp;
5945 static int entered = FALSE;
5946 int save_called_emsg = called_emsg;
5948 /* When called recursively return. This can happen when the statusline
5949 * contains an expression that triggers a redraw. */
5950 if (entered)
5951 return;
5952 entered = TRUE;
5954 called_emsg = FALSE;
5955 win_redr_custom(wp, FALSE);
5956 if (called_emsg)
5958 /* When there is an error disable the statusline, otherwise the
5959 * display is messed up with errors and a redraw triggers the problem
5960 * again and again. */
5961 set_string_option_direct((char_u *)"statusline", -1,
5962 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
5963 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
5965 called_emsg |= save_called_emsg;
5966 entered = FALSE;
5968 #endif
5970 # ifdef FEAT_VERTSPLIT
5972 * Return TRUE if the status line of window "wp" is connected to the status
5973 * line of the window right of it. If not, then it's a vertical separator.
5974 * Only call if (wp->w_vsep_width != 0).
5977 stl_connected(wp)
5978 win_T *wp;
5980 frame_T *fr;
5982 fr = wp->w_frame;
5983 while (fr->fr_parent != NULL)
5985 if (fr->fr_parent->fr_layout == FR_COL)
5987 if (fr->fr_next != NULL)
5988 break;
5990 else
5992 if (fr->fr_next != NULL)
5993 return TRUE;
5995 fr = fr->fr_parent;
5997 return FALSE;
5999 # endif
6001 #endif /* FEAT_WINDOWS */
6003 #if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
6005 * Get the value to show for the language mappings, active 'keymap'.
6008 get_keymap_str(wp, buf, len)
6009 win_T *wp;
6010 char_u *buf; /* buffer for the result */
6011 int len; /* length of buffer */
6013 char_u *p;
6015 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
6016 return FALSE;
6019 #ifdef FEAT_EVAL
6020 buf_T *old_curbuf = curbuf;
6021 win_T *old_curwin = curwin;
6022 char_u *s;
6024 curbuf = wp->w_buffer;
6025 curwin = wp;
6026 STRCPY(buf, "b:keymap_name"); /* must be writable */
6027 ++emsg_skip;
6028 s = p = eval_to_string(buf, NULL, FALSE);
6029 --emsg_skip;
6030 curbuf = old_curbuf;
6031 curwin = old_curwin;
6032 if (p == NULL || *p == NUL)
6033 #endif
6035 #ifdef FEAT_KEYMAP
6036 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
6037 p = wp->w_buffer->b_p_keymap;
6038 else
6039 #endif
6040 p = (char_u *)"lang";
6042 if ((int)(STRLEN(p) + 3) < len)
6043 sprintf((char *)buf, "<%s>", p);
6044 else
6045 buf[0] = NUL;
6046 #ifdef FEAT_EVAL
6047 vim_free(s);
6048 #endif
6050 return buf[0] != NUL;
6052 #endif
6054 #if defined(FEAT_STL_OPT) || defined(PROTO)
6056 * Redraw the status line or ruler of window "wp".
6057 * When "wp" is NULL redraw the tab pages line from 'tabline'.
6059 static void
6060 win_redr_custom(wp, draw_ruler)
6061 win_T *wp;
6062 int draw_ruler; /* TRUE or FALSE */
6064 int attr;
6065 int curattr;
6066 int row;
6067 int col = 0;
6068 int maxwidth;
6069 int width;
6070 int n;
6071 int len;
6072 int fillchar;
6073 char_u buf[MAXPATHL];
6074 char_u *stl;
6075 char_u *p;
6076 struct stl_hlrec hltab[STL_MAX_ITEM];
6077 struct stl_hlrec tabtab[STL_MAX_ITEM];
6078 int use_sandbox = FALSE;
6080 /* setup environment for the task at hand */
6081 if (wp == NULL)
6083 /* Use 'tabline'. Always at the first line of the screen. */
6084 stl = p_tal;
6085 row = 0;
6086 fillchar = ' ';
6087 attr = hl_attr(HLF_TPF);
6088 maxwidth = Columns;
6089 # ifdef FEAT_EVAL
6090 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
6091 # endif
6093 else
6095 row = W_WINROW(wp) + wp->w_height;
6096 fillchar = fillchar_status(&attr, wp == curwin);
6097 maxwidth = W_WIDTH(wp);
6099 if (draw_ruler)
6101 stl = p_ruf;
6102 /* advance past any leading group spec - implicit in ru_col */
6103 if (*stl == '%')
6105 if (*++stl == '-')
6106 stl++;
6107 if (atoi((char *)stl))
6108 while (VIM_ISDIGIT(*stl))
6109 stl++;
6110 if (*stl++ != '(')
6111 stl = p_ruf;
6113 #ifdef FEAT_VERTSPLIT
6114 col = ru_col - (Columns - W_WIDTH(wp));
6115 if (col < (W_WIDTH(wp) + 1) / 2)
6116 col = (W_WIDTH(wp) + 1) / 2;
6117 #else
6118 col = ru_col;
6119 if (col > (Columns + 1) / 2)
6120 col = (Columns + 1) / 2;
6121 #endif
6122 maxwidth = W_WIDTH(wp) - col;
6123 #ifdef FEAT_WINDOWS
6124 if (!wp->w_status_height)
6125 #endif
6127 row = Rows - 1;
6128 --maxwidth; /* writing in last column may cause scrolling */
6129 fillchar = ' ';
6130 attr = 0;
6133 # ifdef FEAT_EVAL
6134 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
6135 # endif
6137 else
6139 if (*wp->w_p_stl != NUL)
6140 stl = wp->w_p_stl;
6141 else
6142 stl = p_stl;
6143 # ifdef FEAT_EVAL
6144 use_sandbox = was_set_insecurely((char_u *)"statusline",
6145 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
6146 # endif
6149 #ifdef FEAT_VERTSPLIT
6150 col += W_WINCOL(wp);
6151 #endif
6154 if (maxwidth <= 0)
6155 return;
6157 /* Make a copy, because the statusline may include a function call that
6158 * might change the option value and free the memory. */
6159 stl = vim_strsave(stl);
6160 width = build_stl_str_hl(wp == NULL ? curwin : wp,
6161 buf, sizeof(buf),
6162 stl, use_sandbox,
6163 fillchar, maxwidth, hltab, tabtab);
6164 vim_free(stl);
6165 len = (int)STRLEN(buf);
6167 while (width < maxwidth && len < (int)sizeof(buf) - 1)
6169 #ifdef FEAT_MBYTE
6170 len += (*mb_char2bytes)(fillchar, buf + len);
6171 #else
6172 buf[len++] = fillchar;
6173 #endif
6174 ++width;
6176 buf[len] = NUL;
6179 * Draw each snippet with the specified highlighting.
6181 curattr = attr;
6182 p = buf;
6183 for (n = 0; hltab[n].start != NULL; n++)
6185 len = (int)(hltab[n].start - p);
6186 screen_puts_len(p, len, row, col, curattr);
6187 col += vim_strnsize(p, len);
6188 p = hltab[n].start;
6190 if (hltab[n].userhl == 0)
6191 curattr = attr;
6192 else if (hltab[n].userhl < 0)
6193 curattr = syn_id2attr(-hltab[n].userhl);
6194 #ifdef FEAT_WINDOWS
6195 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
6196 curattr = highlight_stlnc[hltab[n].userhl - 1];
6197 #endif
6198 else
6199 curattr = highlight_user[hltab[n].userhl - 1];
6201 screen_puts(p, row, col, curattr);
6203 if (wp == NULL)
6205 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
6206 col = 0;
6207 len = 0;
6208 p = buf;
6209 fillchar = 0;
6210 for (n = 0; tabtab[n].start != NULL; n++)
6212 len += vim_strnsize(p, (int)(tabtab[n].start - p));
6213 while (col < len)
6214 TabPageIdxs[col++] = fillchar;
6215 p = tabtab[n].start;
6216 fillchar = tabtab[n].userhl;
6218 while (col < Columns)
6219 TabPageIdxs[col++] = fillchar;
6223 #endif /* FEAT_STL_OPT */
6226 * Output a single character directly to the screen and update ScreenLines.
6228 void
6229 screen_putchar(c, row, col, attr)
6230 int c;
6231 int row, col;
6232 int attr;
6234 #ifdef FEAT_MBYTE
6235 char_u buf[MB_MAXBYTES + 1];
6237 buf[(*mb_char2bytes)(c, buf)] = NUL;
6238 #else
6239 char_u buf[2];
6241 buf[0] = c;
6242 buf[1] = NUL;
6243 #endif
6244 screen_puts(buf, row, col, attr);
6248 * Get a single character directly from ScreenLines into "bytes[]".
6249 * Also return its attribute in *attrp;
6251 void
6252 screen_getbytes(row, col, bytes, attrp)
6253 int row, col;
6254 char_u *bytes;
6255 int *attrp;
6257 unsigned off;
6259 /* safety check */
6260 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
6262 off = LineOffset[row] + col;
6263 *attrp = ScreenAttrs[off];
6264 bytes[0] = ScreenLines[off];
6265 bytes[1] = NUL;
6267 #ifdef FEAT_MBYTE
6268 if (enc_utf8 && ScreenLinesUC[off] != 0)
6269 bytes[utfc_char2bytes(off, bytes)] = NUL;
6270 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6272 bytes[0] = ScreenLines[off];
6273 bytes[1] = ScreenLines2[off];
6274 bytes[2] = NUL;
6276 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
6278 bytes[1] = ScreenLines[off + 1];
6279 bytes[2] = NUL;
6281 #endif
6285 #ifdef FEAT_MBYTE
6286 static int screen_comp_differs __ARGS((int, int*));
6289 * Return TRUE if composing characters for screen posn "off" differs from
6290 * composing characters in "u8cc".
6292 static int
6293 screen_comp_differs(off, u8cc)
6294 int off;
6295 int *u8cc;
6297 int i;
6299 for (i = 0; i < Screen_mco; ++i)
6301 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
6302 return TRUE;
6303 if (u8cc[i] == 0)
6304 break;
6306 return FALSE;
6308 #endif
6311 * Put string '*text' on the screen at position 'row' and 'col', with
6312 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
6313 * Note: only outputs within one row, message is truncated at screen boundary!
6314 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
6316 void
6317 screen_puts(text, row, col, attr)
6318 char_u *text;
6319 int row;
6320 int col;
6321 int attr;
6323 screen_puts_len(text, -1, row, col, attr);
6327 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
6328 * a NUL.
6330 void
6331 screen_puts_len(text, len, row, col, attr)
6332 char_u *text;
6333 int len;
6334 int row;
6335 int col;
6336 int attr;
6338 unsigned off;
6339 char_u *ptr = text;
6340 int c;
6341 #ifdef FEAT_MBYTE
6342 unsigned max_off;
6343 int mbyte_blen = 1;
6344 int mbyte_cells = 1;
6345 int u8c = 0;
6346 int u8cc[MAX_MCO];
6347 int clear_next_cell = FALSE;
6348 # ifdef FEAT_ARABIC
6349 int prev_c = 0; /* previous Arabic character */
6350 int pc, nc, nc1;
6351 int pcc[MAX_MCO];
6352 # endif
6353 #endif
6354 #if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6355 int force_redraw_this;
6356 int force_redraw_next = FALSE;
6357 #endif
6358 int need_redraw;
6360 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
6361 return;
6362 off = LineOffset[row] + col;
6364 #ifdef FEAT_MBYTE
6365 /* When drawing over the right halve of a double-wide char clear out the
6366 * left halve. Only needed in a terminal. */
6367 if (has_mbyte && col > 0 && col < screen_Columns
6368 # ifdef FEAT_GUI
6369 && !gui.in_use
6370 # endif
6371 && mb_fix_col(col, row) != col)
6373 ScreenLines[off - 1] = ' ';
6374 ScreenAttrs[off - 1] = 0;
6375 if (enc_utf8)
6377 ScreenLinesUC[off - 1] = 0;
6378 ScreenLinesC[0][off - 1] = 0;
6380 /* redraw the previous cell, make it empty */
6381 screen_char(off - 1, row, col - 1);
6382 /* force the cell at "col" to be redrawn */
6383 force_redraw_next = TRUE;
6385 #endif
6387 #ifdef FEAT_MBYTE
6388 max_off = LineOffset[row] + screen_Columns;
6389 #endif
6390 while (col < screen_Columns
6391 && (len < 0 || (int)(ptr - text) < len)
6392 && *ptr != NUL)
6394 c = *ptr;
6395 #ifdef FEAT_MBYTE
6396 /* check if this is the first byte of a multibyte */
6397 if (has_mbyte)
6399 if (enc_utf8 && len > 0)
6400 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
6401 else
6402 mbyte_blen = (*mb_ptr2len)(ptr);
6403 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6404 mbyte_cells = 1;
6405 else if (enc_dbcs != 0)
6406 mbyte_cells = mbyte_blen;
6407 else /* enc_utf8 */
6409 if (len >= 0)
6410 u8c = utfc_ptr2char_len(ptr, u8cc,
6411 (int)((text + len) - ptr));
6412 else
6413 u8c = utfc_ptr2char(ptr, u8cc);
6414 mbyte_cells = utf_char2cells(u8c);
6415 # ifdef UNICODE16
6416 /* Non-BMP character: display as ? or fullwidth ?. */
6417 if (u8c >= 0x10000)
6419 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
6420 if (attr == 0)
6421 attr = hl_attr(HLF_8);
6423 # endif
6424 # ifdef FEAT_ARABIC
6425 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
6427 /* Do Arabic shaping. */
6428 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
6430 /* Past end of string to be displayed. */
6431 nc = NUL;
6432 nc1 = NUL;
6434 else
6436 nc = utfc_ptr2char(ptr + mbyte_blen, pcc);
6437 nc1 = pcc[0];
6439 pc = prev_c;
6440 prev_c = u8c;
6441 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
6443 else
6444 prev_c = u8c;
6445 # endif
6448 #endif
6450 #if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6451 force_redraw_this = force_redraw_next;
6452 force_redraw_next = FALSE;
6453 #endif
6455 need_redraw = ScreenLines[off] != c
6456 #ifdef FEAT_MBYTE
6457 || (mbyte_cells == 2
6458 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
6459 || (enc_dbcs == DBCS_JPNU
6460 && c == 0x8e
6461 && ScreenLines2[off] != ptr[1])
6462 || (enc_utf8
6463 && (ScreenLinesUC[off] != (u8char_T)(c >= 0x80 ? u8c : 0)
6464 || screen_comp_differs(off, u8cc)))
6465 #endif
6466 || ScreenAttrs[off] != attr
6467 || exmode_active;
6469 if (need_redraw
6470 #if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6471 || force_redraw_this
6472 #endif
6475 #if defined(FEAT_GUI) || defined(UNIX)
6476 /* The bold trick makes a single row of pixels appear in the next
6477 * character. When a bold character is removed, the next
6478 * character should be redrawn too. This happens for our own GUI
6479 * and for some xterms. */
6480 if (need_redraw && ScreenLines[off] != ' ' && (
6481 # ifdef FEAT_GUI
6482 gui.in_use
6483 # endif
6484 # if defined(FEAT_GUI) && defined(UNIX)
6486 # endif
6487 # ifdef UNIX
6488 term_is_xterm
6489 # endif
6492 int n = ScreenAttrs[off];
6494 if (n > HL_ALL)
6495 n = syn_attr2attr(n);
6496 if (n & HL_BOLD)
6497 force_redraw_next = TRUE;
6499 #endif
6500 #ifdef FEAT_MBYTE
6501 /* When at the end of the text and overwriting a two-cell
6502 * character with a one-cell character, need to clear the next
6503 * cell. Also when overwriting the left halve of a two-cell char
6504 * with the right halve of a two-cell char. Do this only once
6505 * (mb_off2cells() may return 2 on the right halve). */
6506 if (clear_next_cell)
6507 clear_next_cell = FALSE;
6508 else if (has_mbyte
6509 && (len < 0 ? ptr[mbyte_blen] == NUL
6510 : ptr + mbyte_blen >= text + len)
6511 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
6512 || (mbyte_cells == 2
6513 && (*mb_off2cells)(off, max_off) == 1
6514 && (*mb_off2cells)(off + 1, max_off) > 1)))
6515 clear_next_cell = TRUE;
6517 /* Make sure we never leave a second byte of a double-byte behind,
6518 * it confuses mb_off2cells(). */
6519 if (enc_dbcs
6520 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
6521 || (mbyte_cells == 2
6522 && (*mb_off2cells)(off, max_off) == 1
6523 && (*mb_off2cells)(off + 1, max_off) > 1)))
6524 ScreenLines[off + mbyte_blen] = 0;
6525 #endif
6526 ScreenLines[off] = c;
6527 ScreenAttrs[off] = attr;
6528 #ifdef FEAT_MBYTE
6529 if (enc_utf8)
6531 if (c < 0x80 && u8cc[0] == 0)
6532 ScreenLinesUC[off] = 0;
6533 else
6535 int i;
6537 ScreenLinesUC[off] = u8c;
6538 for (i = 0; i < Screen_mco; ++i)
6540 ScreenLinesC[i][off] = u8cc[i];
6541 if (u8cc[i] == 0)
6542 break;
6545 if (mbyte_cells == 2)
6547 ScreenLines[off + 1] = 0;
6548 ScreenAttrs[off + 1] = attr;
6550 screen_char(off, row, col);
6552 else if (mbyte_cells == 2)
6554 ScreenLines[off + 1] = ptr[1];
6555 ScreenAttrs[off + 1] = attr;
6556 screen_char_2(off, row, col);
6558 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6560 ScreenLines2[off] = ptr[1];
6561 screen_char(off, row, col);
6563 else
6564 #endif
6565 screen_char(off, row, col);
6567 #ifdef FEAT_MBYTE
6568 if (has_mbyte)
6570 off += mbyte_cells;
6571 col += mbyte_cells;
6572 ptr += mbyte_blen;
6573 if (clear_next_cell)
6574 ptr = (char_u *)" ";
6576 else
6577 #endif
6579 ++off;
6580 ++col;
6581 ++ptr;
6585 #if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6586 /* If we detected the next character needs to be redrawn, but the text
6587 * doesn't extend up to there, update the character here. */
6588 if (force_redraw_next && col < screen_Columns)
6590 # ifdef FEAT_MBYTE
6591 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
6592 screen_char_2(off, row, col);
6593 else
6594 # endif
6595 screen_char(off, row, col);
6597 #endif
6600 #ifdef FEAT_SEARCH_EXTRA
6602 * Prepare for 'hlsearch' highlighting.
6604 static void
6605 start_search_hl()
6607 if (p_hls && !no_hlsearch)
6609 last_pat_prog(&search_hl.rm);
6610 search_hl.attr = hl_attr(HLF_L);
6611 # ifdef FEAT_RELTIME
6612 /* Set the time limit to 'redrawtime'. */
6613 profile_setlimit(p_rdt, &search_hl.tm);
6614 # endif
6619 * Clean up for 'hlsearch' highlighting.
6621 static void
6622 end_search_hl()
6624 if (search_hl.rm.regprog != NULL)
6626 vim_free(search_hl.rm.regprog);
6627 search_hl.rm.regprog = NULL;
6632 * Advance to the match in window "wp" line "lnum" or past it.
6634 static void
6635 prepare_search_hl(wp, lnum)
6636 win_T *wp;
6637 linenr_T lnum;
6639 matchitem_T *cur; /* points to the match list */
6640 match_T *shl; /* points to search_hl or a match */
6641 int shl_flag; /* flag to indicate whether search_hl
6642 has been processed or not */
6643 int n;
6646 * When using a multi-line pattern, start searching at the top
6647 * of the window or just after a closed fold.
6648 * Do this both for search_hl and the match list.
6650 cur = wp->w_match_head;
6651 shl_flag = FALSE;
6652 while (cur != NULL || shl_flag == FALSE)
6654 if (shl_flag == FALSE)
6656 shl = &search_hl;
6657 shl_flag = TRUE;
6659 else
6660 shl = &cur->hl;
6661 if (shl->rm.regprog != NULL
6662 && shl->lnum == 0
6663 && re_multiline(shl->rm.regprog))
6665 if (shl->first_lnum == 0)
6667 # ifdef FEAT_FOLDING
6668 for (shl->first_lnum = lnum;
6669 shl->first_lnum > wp->w_topline; --shl->first_lnum)
6670 if (hasFoldingWin(wp, shl->first_lnum - 1,
6671 NULL, NULL, TRUE, NULL))
6672 break;
6673 # else
6674 shl->first_lnum = wp->w_topline;
6675 # endif
6677 n = 0;
6678 while (shl->first_lnum < lnum && shl->rm.regprog != NULL)
6680 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n);
6681 if (shl->lnum != 0)
6683 shl->first_lnum = shl->lnum
6684 + shl->rm.endpos[0].lnum
6685 - shl->rm.startpos[0].lnum;
6686 n = shl->rm.endpos[0].col;
6688 else
6690 ++shl->first_lnum;
6691 n = 0;
6695 if (shl != &search_hl && cur != NULL)
6696 cur = cur->next;
6701 * Search for a next 'hlsearch' or match.
6702 * Uses shl->buf.
6703 * Sets shl->lnum and shl->rm contents.
6704 * Note: Assumes a previous match is always before "lnum", unless
6705 * shl->lnum is zero.
6706 * Careful: Any pointers for buffer lines will become invalid.
6708 static void
6709 next_search_hl(win, shl, lnum, mincol)
6710 win_T *win;
6711 match_T *shl; /* points to search_hl or a match */
6712 linenr_T lnum;
6713 colnr_T mincol; /* minimal column for a match */
6715 linenr_T l;
6716 colnr_T matchcol;
6717 long nmatched;
6719 if (shl->lnum != 0)
6721 /* Check for three situations:
6722 * 1. If the "lnum" is below a previous match, start a new search.
6723 * 2. If the previous match includes "mincol", use it.
6724 * 3. Continue after the previous match.
6726 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
6727 if (lnum > l)
6728 shl->lnum = 0;
6729 else if (lnum < l || shl->rm.endpos[0].col > mincol)
6730 return;
6734 * Repeat searching for a match until one is found that includes "mincol"
6735 * or none is found in this line.
6737 called_emsg = FALSE;
6738 for (;;)
6740 #ifdef FEAT_RELTIME
6741 /* Stop searching after passing the time limit. */
6742 if (profile_passed_limit(&(shl->tm)))
6744 shl->lnum = 0; /* no match found in time */
6745 break;
6747 #endif
6748 /* Three situations:
6749 * 1. No useful previous match: search from start of line.
6750 * 2. Not Vi compatible or empty match: continue at next character.
6751 * Break the loop if this is beyond the end of the line.
6752 * 3. Vi compatible searching: continue at end of previous match.
6754 if (shl->lnum == 0)
6755 matchcol = 0;
6756 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
6757 || (shl->rm.endpos[0].lnum == 0
6758 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
6760 char_u *ml;
6762 matchcol = shl->rm.startpos[0].col;
6763 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
6764 if (*ml == NUL)
6766 ++matchcol;
6767 shl->lnum = 0;
6768 break;
6770 #ifdef FEAT_MBYTE
6771 if (has_mbyte)
6772 matchcol += mb_ptr2len(ml);
6773 else
6774 #endif
6775 ++matchcol;
6777 else
6778 matchcol = shl->rm.endpos[0].col;
6780 shl->lnum = lnum;
6781 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum, matchcol,
6782 #ifdef FEAT_RELTIME
6783 &(shl->tm)
6784 #else
6785 NULL
6786 #endif
6788 if (called_emsg)
6790 /* Error while handling regexp: stop using this regexp. */
6791 if (shl == &search_hl)
6793 /* don't free regprog in the match list, it's a copy */
6794 vim_free(shl->rm.regprog);
6795 no_hlsearch = TRUE;
6797 shl->rm.regprog = NULL;
6798 shl->lnum = 0;
6799 got_int = FALSE; /* avoid the "Type :quit to exit Vim" message */
6800 break;
6802 if (nmatched == 0)
6804 shl->lnum = 0; /* no match found */
6805 break;
6807 if (shl->rm.startpos[0].lnum > 0
6808 || shl->rm.startpos[0].col >= mincol
6809 || nmatched > 1
6810 || shl->rm.endpos[0].col > mincol)
6812 shl->lnum += shl->rm.startpos[0].lnum;
6813 break; /* useful match found */
6817 #endif
6819 static void
6820 screen_start_highlight(attr)
6821 int attr;
6823 attrentry_T *aep = NULL;
6825 screen_attr = attr;
6826 if (full_screen
6827 #ifdef WIN3264
6828 && termcap_active
6829 #endif
6832 #ifdef FEAT_GUI
6833 if (gui.in_use)
6835 char buf[20];
6837 /* The GUI handles this internally. */
6838 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
6839 OUT_STR(buf);
6841 else
6842 #endif
6844 if (attr > HL_ALL) /* special HL attr. */
6846 if (t_colors > 1)
6847 aep = syn_cterm_attr2entry(attr);
6848 else
6849 aep = syn_term_attr2entry(attr);
6850 if (aep == NULL) /* did ":syntax clear" */
6851 attr = 0;
6852 else
6853 attr = aep->ae_attr;
6855 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
6856 out_str(T_MD);
6857 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
6858 && cterm_normal_fg_bold)
6859 /* If the Normal FG color has BOLD attribute and the new HL
6860 * has a FG color defined, clear BOLD. */
6861 out_str(T_ME);
6862 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
6863 out_str(T_SO);
6864 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
6865 /* underline or undercurl */
6866 out_str(T_US);
6867 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
6868 out_str(T_CZH);
6869 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
6870 out_str(T_MR);
6873 * Output the color or start string after bold etc., in case the
6874 * bold etc. override the color setting.
6876 if (aep != NULL)
6878 if (t_colors > 1)
6880 if (aep->ae_u.cterm.fg_color)
6881 term_fg_color(aep->ae_u.cterm.fg_color - 1);
6882 if (aep->ae_u.cterm.bg_color)
6883 term_bg_color(aep->ae_u.cterm.bg_color - 1);
6885 else
6887 if (aep->ae_u.term.start != NULL)
6888 out_str(aep->ae_u.term.start);
6895 void
6896 screen_stop_highlight()
6898 int do_ME = FALSE; /* output T_ME code */
6900 if (screen_attr != 0
6901 #ifdef WIN3264
6902 && termcap_active
6903 #endif
6906 #ifdef FEAT_GUI
6907 if (gui.in_use)
6909 char buf[20];
6911 /* use internal GUI code */
6912 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
6913 OUT_STR(buf);
6915 else
6916 #endif
6918 if (screen_attr > HL_ALL) /* special HL attr. */
6920 attrentry_T *aep;
6922 if (t_colors > 1)
6925 * Assume that t_me restores the original colors!
6927 aep = syn_cterm_attr2entry(screen_attr);
6928 if (aep != NULL && (aep->ae_u.cterm.fg_color
6929 || aep->ae_u.cterm.bg_color))
6930 do_ME = TRUE;
6932 else
6934 aep = syn_term_attr2entry(screen_attr);
6935 if (aep != NULL && aep->ae_u.term.stop != NULL)
6937 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
6938 do_ME = TRUE;
6939 else
6940 out_str(aep->ae_u.term.stop);
6943 if (aep == NULL) /* did ":syntax clear" */
6944 screen_attr = 0;
6945 else
6946 screen_attr = aep->ae_attr;
6950 * Often all ending-codes are equal to T_ME. Avoid outputting the
6951 * same sequence several times.
6953 if (screen_attr & HL_STANDOUT)
6955 if (STRCMP(T_SE, T_ME) == 0)
6956 do_ME = TRUE;
6957 else
6958 out_str(T_SE);
6960 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
6962 if (STRCMP(T_UE, T_ME) == 0)
6963 do_ME = TRUE;
6964 else
6965 out_str(T_UE);
6967 if (screen_attr & HL_ITALIC)
6969 if (STRCMP(T_CZR, T_ME) == 0)
6970 do_ME = TRUE;
6971 else
6972 out_str(T_CZR);
6974 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
6975 out_str(T_ME);
6977 if (t_colors > 1)
6979 /* set Normal cterm colors */
6980 if (cterm_normal_fg_color != 0)
6981 term_fg_color(cterm_normal_fg_color - 1);
6982 if (cterm_normal_bg_color != 0)
6983 term_bg_color(cterm_normal_bg_color - 1);
6984 if (cterm_normal_fg_bold)
6985 out_str(T_MD);
6989 screen_attr = 0;
6993 * Reset the colors for a cterm. Used when leaving Vim.
6994 * The machine specific code may override this again.
6996 void
6997 reset_cterm_colors()
6999 if (t_colors > 1)
7001 /* set Normal cterm colors */
7002 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
7004 out_str(T_OP);
7005 screen_attr = -1;
7007 if (cterm_normal_fg_bold)
7009 out_str(T_ME);
7010 screen_attr = -1;
7016 * Put character ScreenLines["off"] on the screen at position "row" and "col",
7017 * using the attributes from ScreenAttrs["off"].
7019 static void
7020 screen_char(off, row, col)
7021 unsigned off;
7022 int row;
7023 int col;
7025 int attr;
7027 /* Check for illegal values, just in case (could happen just after
7028 * resizing). */
7029 if (row >= screen_Rows || col >= screen_Columns)
7030 return;
7032 /* Outputting the last character on the screen may scrollup the screen.
7033 * Don't to it! Mark the character invalid (update it when scrolled up) */
7034 if (row == screen_Rows - 1 && col == screen_Columns - 1
7035 #ifdef FEAT_RIGHTLEFT
7036 /* account for first command-line character in rightleft mode */
7037 && !cmdmsg_rl
7038 #endif
7041 ScreenAttrs[off] = (sattr_T)-1;
7042 return;
7046 * Stop highlighting first, so it's easier to move the cursor.
7048 #if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
7049 if (screen_char_attr != 0)
7050 attr = screen_char_attr;
7051 else
7052 #endif
7053 attr = ScreenAttrs[off];
7054 if (screen_attr != attr)
7055 screen_stop_highlight();
7057 windgoto(row, col);
7059 if (screen_attr != attr)
7060 screen_start_highlight(attr);
7062 #ifdef FEAT_MBYTE
7063 if (enc_utf8 && ScreenLinesUC[off] != 0)
7065 char_u buf[MB_MAXBYTES + 1];
7067 /* Convert UTF-8 character to bytes and write it. */
7069 buf[utfc_char2bytes(off, buf)] = NUL;
7071 out_str(buf);
7072 if (utf_char2cells(ScreenLinesUC[off]) > 1)
7073 ++screen_cur_col;
7075 else
7076 #endif
7078 #ifdef FEAT_MBYTE
7079 out_flush_check();
7080 #endif
7081 out_char(ScreenLines[off]);
7082 #ifdef FEAT_MBYTE
7083 /* double-byte character in single-width cell */
7084 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7085 out_char(ScreenLines2[off]);
7086 #endif
7089 screen_cur_col++;
7092 #ifdef FEAT_MBYTE
7095 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
7096 * on the screen at position 'row' and 'col'.
7097 * The attributes of the first byte is used for all. This is required to
7098 * output the two bytes of a double-byte character with nothing in between.
7100 static void
7101 screen_char_2(off, row, col)
7102 unsigned off;
7103 int row;
7104 int col;
7106 /* Check for illegal values (could be wrong when screen was resized). */
7107 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
7108 return;
7110 /* Outputting the last character on the screen may scrollup the screen.
7111 * Don't to it! Mark the character invalid (update it when scrolled up) */
7112 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
7114 ScreenAttrs[off] = (sattr_T)-1;
7115 return;
7118 /* Output the first byte normally (positions the cursor), then write the
7119 * second byte directly. */
7120 screen_char(off, row, col);
7121 out_char(ScreenLines[off + 1]);
7122 ++screen_cur_col;
7124 #endif
7126 #if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
7128 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
7129 * This uses the contents of ScreenLines[] and doesn't change it.
7131 void
7132 screen_draw_rectangle(row, col, height, width, invert)
7133 int row;
7134 int col;
7135 int height;
7136 int width;
7137 int invert;
7139 int r, c;
7140 int off;
7141 #ifdef FEAT_MBYTE
7142 int max_off;
7143 #endif
7145 /* Can't use ScreenLines unless initialized */
7146 if (ScreenLines == NULL)
7147 return;
7149 if (invert)
7150 screen_char_attr = HL_INVERSE;
7151 for (r = row; r < row + height; ++r)
7153 off = LineOffset[r];
7154 #ifdef FEAT_MBYTE
7155 max_off = off + screen_Columns;
7156 #endif
7157 for (c = col; c < col + width; ++c)
7159 #ifdef FEAT_MBYTE
7160 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
7162 screen_char_2(off + c, r, c);
7163 ++c;
7165 else
7166 #endif
7168 screen_char(off + c, r, c);
7169 #ifdef FEAT_MBYTE
7170 if (utf_off2cells(off + c, max_off) > 1)
7171 ++c;
7172 #endif
7176 screen_char_attr = 0;
7178 #endif
7180 #ifdef FEAT_VERTSPLIT
7182 * Redraw the characters for a vertically split window.
7184 static void
7185 redraw_block(row, end, wp)
7186 int row;
7187 int end;
7188 win_T *wp;
7190 int col;
7191 int width;
7193 # ifdef FEAT_CLIPBOARD
7194 clip_may_clear_selection(row, end - 1);
7195 # endif
7197 if (wp == NULL)
7199 col = 0;
7200 width = Columns;
7202 else
7204 col = wp->w_wincol;
7205 width = wp->w_width;
7207 screen_draw_rectangle(row, col, end - row, width, FALSE);
7209 #endif
7212 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
7213 * with character 'c1' in first column followed by 'c2' in the other columns.
7214 * Use attributes 'attr'.
7216 void
7217 screen_fill(start_row, end_row, start_col, end_col, c1, c2, attr)
7218 int start_row, end_row;
7219 int start_col, end_col;
7220 int c1, c2;
7221 int attr;
7223 int row;
7224 int col;
7225 int off;
7226 int end_off;
7227 int did_delete;
7228 int c;
7229 int norm_term;
7230 #if defined(FEAT_GUI) || defined(UNIX)
7231 int force_next = FALSE;
7232 #endif
7234 if (end_row > screen_Rows) /* safety check */
7235 end_row = screen_Rows;
7236 if (end_col > screen_Columns) /* safety check */
7237 end_col = screen_Columns;
7238 if (ScreenLines == NULL
7239 || start_row >= end_row
7240 || start_col >= end_col) /* nothing to do */
7241 return;
7243 /* it's a "normal" terminal when not in a GUI or cterm */
7244 norm_term = (
7245 #ifdef FEAT_GUI
7246 !gui.in_use &&
7247 #endif
7248 t_colors <= 1);
7249 for (row = start_row; row < end_row; ++row)
7251 #ifdef FEAT_MBYTE
7252 if (has_mbyte
7253 # ifdef FEAT_GUI
7254 && !gui.in_use
7255 # endif
7258 /* When drawing over the right halve of a double-wide char clear
7259 * out the left halve. When drawing over the left halve of a
7260 * double wide-char clear out the right halve. Only needed in a
7261 * terminal. */
7262 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
7263 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
7264 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
7265 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
7267 #endif
7269 * Try to use delete-line termcap code, when no attributes or in a
7270 * "normal" terminal, where a bold/italic space is just a
7271 * space.
7273 did_delete = FALSE;
7274 if (c2 == ' '
7275 && end_col == Columns
7276 && can_clear(T_CE)
7277 && (attr == 0
7278 || (norm_term
7279 && attr <= HL_ALL
7280 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
7283 * check if we really need to clear something
7285 col = start_col;
7286 if (c1 != ' ') /* don't clear first char */
7287 ++col;
7289 off = LineOffset[row] + col;
7290 end_off = LineOffset[row] + end_col;
7292 /* skip blanks (used often, keep it fast!) */
7293 #ifdef FEAT_MBYTE
7294 if (enc_utf8)
7295 while (off < end_off && ScreenLines[off] == ' '
7296 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
7297 ++off;
7298 else
7299 #endif
7300 while (off < end_off && ScreenLines[off] == ' '
7301 && ScreenAttrs[off] == 0)
7302 ++off;
7303 if (off < end_off) /* something to be cleared */
7305 col = off - LineOffset[row];
7306 screen_stop_highlight();
7307 term_windgoto(row, col);/* clear rest of this screen line */
7308 out_str(T_CE);
7309 screen_start(); /* don't know where cursor is now */
7310 col = end_col - col;
7311 while (col--) /* clear chars in ScreenLines */
7313 ScreenLines[off] = ' ';
7314 #ifdef FEAT_MBYTE
7315 if (enc_utf8)
7316 ScreenLinesUC[off] = 0;
7317 #endif
7318 ScreenAttrs[off] = 0;
7319 ++off;
7322 did_delete = TRUE; /* the chars are cleared now */
7325 off = LineOffset[row] + start_col;
7326 c = c1;
7327 for (col = start_col; col < end_col; ++col)
7329 if (ScreenLines[off] != c
7330 #ifdef FEAT_MBYTE
7331 || (enc_utf8 && (int)ScreenLinesUC[off]
7332 != (c >= 0x80 ? c : 0))
7333 #endif
7334 || ScreenAttrs[off] != attr
7335 #if defined(FEAT_GUI) || defined(UNIX)
7336 || force_next
7337 #endif
7340 #if defined(FEAT_GUI) || defined(UNIX)
7341 /* The bold trick may make a single row of pixels appear in
7342 * the next character. When a bold character is removed, the
7343 * next character should be redrawn too. This happens for our
7344 * own GUI and for some xterms. */
7345 if (
7346 # ifdef FEAT_GUI
7347 gui.in_use
7348 # endif
7349 # if defined(FEAT_GUI) && defined(UNIX)
7351 # endif
7352 # ifdef UNIX
7353 term_is_xterm
7354 # endif
7357 if (ScreenLines[off] != ' '
7358 && (ScreenAttrs[off] > HL_ALL
7359 || ScreenAttrs[off] & HL_BOLD))
7360 force_next = TRUE;
7361 else
7362 force_next = FALSE;
7364 #endif
7365 ScreenLines[off] = c;
7366 #ifdef FEAT_MBYTE
7367 if (enc_utf8)
7369 if (c >= 0x80)
7371 ScreenLinesUC[off] = c;
7372 ScreenLinesC[0][off] = 0;
7374 else
7375 ScreenLinesUC[off] = 0;
7377 #endif
7378 ScreenAttrs[off] = attr;
7379 if (!did_delete || c != ' ')
7380 screen_char(off, row, col);
7382 ++off;
7383 if (col == start_col)
7385 if (did_delete)
7386 break;
7387 c = c2;
7390 if (end_col == Columns)
7391 LineWraps[row] = FALSE;
7392 if (row == Rows - 1) /* overwritten the command line */
7394 redraw_cmdline = TRUE;
7395 if (c1 == ' ' && c2 == ' ')
7396 clear_cmdline = FALSE; /* command line has been cleared */
7397 if (start_col == 0)
7398 mode_displayed = FALSE; /* mode cleared or overwritten */
7404 * Check if there should be a delay. Used before clearing or redrawing the
7405 * screen or the command line.
7407 void
7408 check_for_delay(check_msg_scroll)
7409 int check_msg_scroll;
7411 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
7412 && !did_wait_return
7413 && emsg_silent == 0)
7415 out_flush();
7416 ui_delay(1000L, TRUE);
7417 emsg_on_display = FALSE;
7418 if (check_msg_scroll)
7419 msg_scroll = FALSE;
7424 * screen_valid - allocate screen buffers if size changed
7425 * If "clear" is TRUE: clear screen if it has been resized.
7426 * Returns TRUE if there is a valid screen to write to.
7427 * Returns FALSE when starting up and screen not initialized yet.
7430 screen_valid(clear)
7431 int clear;
7433 screenalloc(clear); /* allocate screen buffers if size changed */
7434 return (ScreenLines != NULL);
7438 * Resize the shell to Rows and Columns.
7439 * Allocate ScreenLines[] and associated items.
7441 * There may be some time between setting Rows and Columns and (re)allocating
7442 * ScreenLines[]. This happens when starting up and when (manually) changing
7443 * the shell size. Always use screen_Rows and screen_Columns to access items
7444 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
7445 * final size of the shell is needed.
7447 void
7448 screenalloc(clear)
7449 int clear;
7451 int new_row, old_row;
7452 #ifdef FEAT_GUI
7453 int old_Rows;
7454 #endif
7455 win_T *wp;
7456 int outofmem = FALSE;
7457 int len;
7458 schar_T *new_ScreenLines;
7459 #ifdef FEAT_MBYTE
7460 u8char_T *new_ScreenLinesUC = NULL;
7461 u8char_T *new_ScreenLinesC[MAX_MCO];
7462 schar_T *new_ScreenLines2 = NULL;
7463 int i;
7464 #endif
7465 sattr_T *new_ScreenAttrs;
7466 unsigned *new_LineOffset;
7467 char_u *new_LineWraps;
7468 #ifdef FEAT_WINDOWS
7469 short *new_TabPageIdxs;
7470 tabpage_T *tp;
7471 #endif
7472 static int entered = FALSE; /* avoid recursiveness */
7473 static int done_outofmem_msg = FALSE; /* did outofmem message */
7474 #ifdef FEAT_AUTOCMD
7475 int retry_count = 0;
7477 retry:
7478 #endif
7480 * Allocation of the screen buffers is done only when the size changes and
7481 * when Rows and Columns have been set and we have started doing full
7482 * screen stuff.
7484 if ((ScreenLines != NULL
7485 && Rows == screen_Rows
7486 && Columns == screen_Columns
7487 #ifdef FEAT_MBYTE
7488 && enc_utf8 == (ScreenLinesUC != NULL)
7489 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
7490 && p_mco == Screen_mco
7491 #endif
7493 || Rows == 0
7494 || Columns == 0
7495 || (!full_screen && ScreenLines == NULL))
7496 return;
7499 * It's possible that we produce an out-of-memory message below, which
7500 * will cause this function to be called again. To break the loop, just
7501 * return here.
7503 if (entered)
7504 return;
7505 entered = TRUE;
7508 * Note that the window sizes are updated before reallocating the arrays,
7509 * thus we must not redraw here!
7511 ++RedrawingDisabled;
7513 win_new_shellsize(); /* fit the windows in the new sized shell */
7515 comp_col(); /* recompute columns for shown command and ruler */
7518 * We're changing the size of the screen.
7519 * - Allocate new arrays for ScreenLines and ScreenAttrs.
7520 * - Move lines from the old arrays into the new arrays, clear extra
7521 * lines (unless the screen is going to be cleared).
7522 * - Free the old arrays.
7524 * If anything fails, make ScreenLines NULL, so we don't do anything!
7525 * Continuing with the old ScreenLines may result in a crash, because the
7526 * size is wrong.
7528 FOR_ALL_TAB_WINDOWS(tp, wp)
7529 win_free_lsize(wp);
7530 #ifdef FEAT_AUTOCMD
7531 if (aucmd_win != NULL)
7532 win_free_lsize(aucmd_win);
7533 #endif
7535 new_ScreenLines = (schar_T *)lalloc((long_u)(
7536 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7537 #ifdef FEAT_MBYTE
7538 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T) * MAX_MCO);
7539 if (enc_utf8)
7541 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
7542 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
7543 for (i = 0; i < p_mco; ++i)
7544 new_ScreenLinesC[i] = (u8char_T *)lalloc((long_u)(
7545 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
7547 if (enc_dbcs == DBCS_JPNU)
7548 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
7549 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7550 #endif
7551 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
7552 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
7553 new_LineOffset = (unsigned *)lalloc((long_u)(
7554 Rows * sizeof(unsigned)), FALSE);
7555 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
7556 #ifdef FEAT_WINDOWS
7557 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
7558 #endif
7560 FOR_ALL_TAB_WINDOWS(tp, wp)
7562 if (win_alloc_lines(wp) == FAIL)
7564 outofmem = TRUE;
7565 #ifdef FEAT_WINDOWS
7566 goto give_up;
7567 #endif
7570 #ifdef FEAT_AUTOCMD
7571 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
7572 && win_alloc_lines(aucmd_win) == FAIL)
7573 outofmem = TRUE;
7574 #endif
7575 #ifdef FEAT_WINDOWS
7576 give_up:
7577 #endif
7579 #ifdef FEAT_MBYTE
7580 for (i = 0; i < p_mco; ++i)
7581 if (new_ScreenLinesC[i] == NULL)
7582 break;
7583 #endif
7584 if (new_ScreenLines == NULL
7585 #ifdef FEAT_MBYTE
7586 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
7587 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
7588 #endif
7589 || new_ScreenAttrs == NULL
7590 || new_LineOffset == NULL
7591 || new_LineWraps == NULL
7592 #ifdef FEAT_WINDOWS
7593 || new_TabPageIdxs == NULL
7594 #endif
7595 || outofmem)
7597 if (ScreenLines != NULL || !done_outofmem_msg)
7599 /* guess the size */
7600 do_outofmem_msg((long_u)((Rows + 1) * Columns));
7602 /* Remember we did this to avoid getting outofmem messages over
7603 * and over again. */
7604 done_outofmem_msg = TRUE;
7606 vim_free(new_ScreenLines);
7607 new_ScreenLines = NULL;
7608 #ifdef FEAT_MBYTE
7609 vim_free(new_ScreenLinesUC);
7610 new_ScreenLinesUC = NULL;
7611 for (i = 0; i < p_mco; ++i)
7613 vim_free(new_ScreenLinesC[i]);
7614 new_ScreenLinesC[i] = NULL;
7616 vim_free(new_ScreenLines2);
7617 new_ScreenLines2 = NULL;
7618 #endif
7619 vim_free(new_ScreenAttrs);
7620 new_ScreenAttrs = NULL;
7621 vim_free(new_LineOffset);
7622 new_LineOffset = NULL;
7623 vim_free(new_LineWraps);
7624 new_LineWraps = NULL;
7625 #ifdef FEAT_WINDOWS
7626 vim_free(new_TabPageIdxs);
7627 new_TabPageIdxs = NULL;
7628 #endif
7630 else
7632 done_outofmem_msg = FALSE;
7634 for (new_row = 0; new_row < Rows; ++new_row)
7636 new_LineOffset[new_row] = new_row * Columns;
7637 new_LineWraps[new_row] = FALSE;
7640 * If the screen is not going to be cleared, copy as much as
7641 * possible from the old screen to the new one and clear the rest
7642 * (used when resizing the window at the "--more--" prompt or when
7643 * executing an external command, for the GUI).
7645 if (!clear)
7647 (void)vim_memset(new_ScreenLines + new_row * Columns,
7648 ' ', (size_t)Columns * sizeof(schar_T));
7649 #ifdef FEAT_MBYTE
7650 if (enc_utf8)
7652 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
7653 0, (size_t)Columns * sizeof(u8char_T));
7654 for (i = 0; i < p_mco; ++i)
7655 (void)vim_memset(new_ScreenLinesC[i]
7656 + new_row * Columns,
7657 0, (size_t)Columns * sizeof(u8char_T));
7659 if (enc_dbcs == DBCS_JPNU)
7660 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
7661 0, (size_t)Columns * sizeof(schar_T));
7662 #endif
7663 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
7664 0, (size_t)Columns * sizeof(sattr_T));
7665 old_row = new_row + (screen_Rows - Rows);
7666 if (old_row >= 0 && ScreenLines != NULL)
7668 if (screen_Columns < Columns)
7669 len = screen_Columns;
7670 else
7671 len = Columns;
7672 #ifdef FEAT_MBYTE
7673 /* When switching to utf-8 don't copy characters, they
7674 * may be invalid now. Also when p_mco changes. */
7675 if (!(enc_utf8 && ScreenLinesUC == NULL)
7676 && p_mco == Screen_mco)
7677 #endif
7678 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
7679 ScreenLines + LineOffset[old_row],
7680 (size_t)len * sizeof(schar_T));
7681 #ifdef FEAT_MBYTE
7682 if (enc_utf8 && ScreenLinesUC != NULL
7683 && p_mco == Screen_mco)
7685 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
7686 ScreenLinesUC + LineOffset[old_row],
7687 (size_t)len * sizeof(u8char_T));
7688 for (i = 0; i < p_mco; ++i)
7689 mch_memmove(new_ScreenLinesC[i]
7690 + new_LineOffset[new_row],
7691 ScreenLinesC[i] + LineOffset[old_row],
7692 (size_t)len * sizeof(u8char_T));
7694 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
7695 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
7696 ScreenLines2 + LineOffset[old_row],
7697 (size_t)len * sizeof(schar_T));
7698 #endif
7699 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
7700 ScreenAttrs + LineOffset[old_row],
7701 (size_t)len * sizeof(sattr_T));
7705 /* Use the last line of the screen for the current line. */
7706 current_ScreenLine = new_ScreenLines + Rows * Columns;
7709 free_screenlines();
7711 ScreenLines = new_ScreenLines;
7712 #ifdef FEAT_MBYTE
7713 ScreenLinesUC = new_ScreenLinesUC;
7714 for (i = 0; i < p_mco; ++i)
7715 ScreenLinesC[i] = new_ScreenLinesC[i];
7716 Screen_mco = p_mco;
7717 ScreenLines2 = new_ScreenLines2;
7718 #endif
7719 ScreenAttrs = new_ScreenAttrs;
7720 LineOffset = new_LineOffset;
7721 LineWraps = new_LineWraps;
7722 #ifdef FEAT_WINDOWS
7723 TabPageIdxs = new_TabPageIdxs;
7724 #endif
7726 /* It's important that screen_Rows and screen_Columns reflect the actual
7727 * size of ScreenLines[]. Set them before calling anything. */
7728 #ifdef FEAT_GUI
7729 old_Rows = screen_Rows;
7730 #endif
7731 screen_Rows = Rows;
7732 screen_Columns = Columns;
7734 must_redraw = CLEAR; /* need to clear the screen later */
7735 if (clear)
7736 screenclear2();
7738 #ifdef FEAT_GUI
7739 else if (gui.in_use
7740 && !gui.starting
7741 && ScreenLines != NULL
7742 && old_Rows != Rows)
7744 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
7746 * Adjust the position of the cursor, for when executing an external
7747 * command.
7749 if (msg_row >= Rows) /* Rows got smaller */
7750 msg_row = Rows - 1; /* put cursor at last row */
7751 else if (Rows > old_Rows) /* Rows got bigger */
7752 msg_row += Rows - old_Rows; /* put cursor in same place */
7753 if (msg_col >= Columns) /* Columns got smaller */
7754 msg_col = Columns - 1; /* put cursor at last column */
7756 #endif
7758 entered = FALSE;
7759 --RedrawingDisabled;
7761 #ifdef FEAT_AUTOCMD
7763 * Do not apply autocommands more than 3 times to avoid an endless loop
7764 * in case applying autocommands always changes Rows or Columns.
7766 if (starting == 0 && ++retry_count <= 3)
7768 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
7769 /* In rare cases, autocommands may have altered Rows or Columns,
7770 * jump back to check if we need to allocate the screen again. */
7771 goto retry;
7773 #endif
7776 void
7777 free_screenlines()
7779 #ifdef FEAT_MBYTE
7780 int i;
7782 vim_free(ScreenLinesUC);
7783 for (i = 0; i < Screen_mco; ++i)
7784 vim_free(ScreenLinesC[i]);
7785 vim_free(ScreenLines2);
7786 #endif
7787 vim_free(ScreenLines);
7788 vim_free(ScreenAttrs);
7789 vim_free(LineOffset);
7790 vim_free(LineWraps);
7791 #ifdef FEAT_WINDOWS
7792 vim_free(TabPageIdxs);
7793 #endif
7796 void
7797 screenclear()
7799 check_for_delay(FALSE);
7800 screenalloc(FALSE); /* allocate screen buffers if size changed */
7801 screenclear2(); /* clear the screen */
7804 static void
7805 screenclear2()
7807 int i;
7809 if (starting == NO_SCREEN || ScreenLines == NULL
7810 #ifdef FEAT_GUI
7811 || (gui.in_use && gui.starting)
7812 #endif
7814 return;
7816 #ifdef FEAT_GUI
7817 if (!gui.in_use)
7818 #endif
7819 screen_attr = -1; /* force setting the Normal colors */
7820 screen_stop_highlight(); /* don't want highlighting here */
7822 #ifdef FEAT_CLIPBOARD
7823 /* disable selection without redrawing it */
7824 clip_scroll_selection(9999);
7825 #endif
7827 /* blank out ScreenLines */
7828 for (i = 0; i < Rows; ++i)
7830 lineclear(LineOffset[i], (int)Columns);
7831 LineWraps[i] = FALSE;
7834 if (can_clear(T_CL))
7836 out_str(T_CL); /* clear the display */
7837 clear_cmdline = FALSE;
7838 mode_displayed = FALSE;
7840 else
7842 /* can't clear the screen, mark all chars with invalid attributes */
7843 for (i = 0; i < Rows; ++i)
7844 lineinvalid(LineOffset[i], (int)Columns);
7845 clear_cmdline = TRUE;
7848 screen_cleared = TRUE; /* can use contents of ScreenLines now */
7850 win_rest_invalid(firstwin);
7851 redraw_cmdline = TRUE;
7852 #ifdef FEAT_WINDOWS
7853 redraw_tabline = TRUE;
7854 #endif
7855 if (must_redraw == CLEAR) /* no need to clear again */
7856 must_redraw = NOT_VALID;
7857 compute_cmdrow();
7858 msg_row = cmdline_row; /* put cursor on last line for messages */
7859 msg_col = 0;
7860 screen_start(); /* don't know where cursor is now */
7861 msg_scrolled = 0; /* can't scroll back */
7862 msg_didany = FALSE;
7863 msg_didout = FALSE;
7867 * Clear one line in ScreenLines.
7869 static void
7870 lineclear(off, width)
7871 unsigned off;
7872 int width;
7874 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
7875 #ifdef FEAT_MBYTE
7876 if (enc_utf8)
7877 (void)vim_memset(ScreenLinesUC + off, 0,
7878 (size_t)width * sizeof(u8char_T));
7879 #endif
7880 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
7884 * Mark one line in ScreenLines invalid by setting the attributes to an
7885 * invalid value.
7887 static void
7888 lineinvalid(off, width)
7889 unsigned off;
7890 int width;
7892 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
7895 #ifdef FEAT_VERTSPLIT
7897 * Copy part of a Screenline for vertically split window "wp".
7899 static void
7900 linecopy(to, from, wp)
7901 int to;
7902 int from;
7903 win_T *wp;
7905 unsigned off_to = LineOffset[to] + wp->w_wincol;
7906 unsigned off_from = LineOffset[from] + wp->w_wincol;
7908 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
7909 wp->w_width * sizeof(schar_T));
7910 # ifdef FEAT_MBYTE
7911 if (enc_utf8)
7913 int i;
7915 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
7916 wp->w_width * sizeof(u8char_T));
7917 for (i = 0; i < p_mco; ++i)
7918 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
7919 wp->w_width * sizeof(u8char_T));
7921 if (enc_dbcs == DBCS_JPNU)
7922 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
7923 wp->w_width * sizeof(schar_T));
7924 # endif
7925 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
7926 wp->w_width * sizeof(sattr_T));
7928 #endif
7931 * Return TRUE if clearing with term string "p" would work.
7932 * It can't work when the string is empty or it won't set the right background.
7935 can_clear(p)
7936 char_u *p;
7938 return (*p != NUL && (t_colors <= 1
7939 #ifdef FEAT_GUI
7940 || gui.in_use
7941 #endif
7942 || cterm_normal_bg_color == 0 || *T_UT != NUL));
7946 * Reset cursor position. Use whenever cursor was moved because of outputting
7947 * something directly to the screen (shell commands) or a terminal control
7948 * code.
7950 void
7951 screen_start()
7953 screen_cur_row = screen_cur_col = 9999;
7957 * Move the cursor to position "row","col" in the screen.
7958 * This tries to find the most efficient way to move, minimizing the number of
7959 * characters sent to the terminal.
7961 void
7962 windgoto(row, col)
7963 int row;
7964 int col;
7966 sattr_T *p;
7967 int i;
7968 int plan;
7969 int cost;
7970 int wouldbe_col;
7971 int noinvcurs;
7972 char_u *bs;
7973 int goto_cost;
7974 int attr;
7976 #define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
7977 #define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
7979 #define PLAN_LE 1
7980 #define PLAN_CR 2
7981 #define PLAN_NL 3
7982 #define PLAN_WRITE 4
7983 /* Can't use ScreenLines unless initialized */
7984 if (ScreenLines == NULL)
7985 return;
7987 if (col != screen_cur_col || row != screen_cur_row)
7989 /* Check for valid position. */
7990 if (row < 0) /* window without text lines? */
7991 row = 0;
7992 if (row >= screen_Rows)
7993 row = screen_Rows - 1;
7994 if (col >= screen_Columns)
7995 col = screen_Columns - 1;
7997 /* check if no cursor movement is allowed in highlight mode */
7998 if (screen_attr && *T_MS == NUL)
7999 noinvcurs = HIGHL_COST;
8000 else
8001 noinvcurs = 0;
8002 goto_cost = GOTO_COST + noinvcurs;
8005 * Plan how to do the positioning:
8006 * 1. Use CR to move it to column 0, same row.
8007 * 2. Use T_LE to move it a few columns to the left.
8008 * 3. Use NL to move a few lines down, column 0.
8009 * 4. Move a few columns to the right with T_ND or by writing chars.
8011 * Don't do this if the cursor went beyond the last column, the cursor
8012 * position is unknown then (some terminals wrap, some don't )
8014 * First check if the highlighting attributes allow us to write
8015 * characters to move the cursor to the right.
8017 if (row >= screen_cur_row && screen_cur_col < Columns)
8020 * If the cursor is in the same row, bigger col, we can use CR
8021 * or T_LE.
8023 bs = NULL; /* init for GCC */
8024 attr = screen_attr;
8025 if (row == screen_cur_row && col < screen_cur_col)
8027 /* "le" is preferred over "bc", because "bc" is obsolete */
8028 if (*T_LE)
8029 bs = T_LE; /* "cursor left" */
8030 else
8031 bs = T_BC; /* "backspace character (old) */
8032 if (*bs)
8033 cost = (screen_cur_col - col) * (int)STRLEN(bs);
8034 else
8035 cost = 999;
8036 if (col + 1 < cost) /* using CR is less characters */
8038 plan = PLAN_CR;
8039 wouldbe_col = 0;
8040 cost = 1; /* CR is just one character */
8042 else
8044 plan = PLAN_LE;
8045 wouldbe_col = col;
8047 if (noinvcurs) /* will stop highlighting */
8049 cost += noinvcurs;
8050 attr = 0;
8055 * If the cursor is above where we want to be, we can use CR LF.
8057 else if (row > screen_cur_row)
8059 plan = PLAN_NL;
8060 wouldbe_col = 0;
8061 cost = (row - screen_cur_row) * 2; /* CR LF */
8062 if (noinvcurs) /* will stop highlighting */
8064 cost += noinvcurs;
8065 attr = 0;
8070 * If the cursor is in the same row, smaller col, just use write.
8072 else
8074 plan = PLAN_WRITE;
8075 wouldbe_col = screen_cur_col;
8076 cost = 0;
8080 * Check if any characters that need to be written have the
8081 * correct attributes. Also avoid UTF-8 characters.
8083 i = col - wouldbe_col;
8084 if (i > 0)
8085 cost += i;
8086 if (cost < goto_cost && i > 0)
8089 * Check if the attributes are correct without additionally
8090 * stopping highlighting.
8092 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
8093 while (i && *p++ == attr)
8094 --i;
8095 if (i != 0)
8098 * Try if it works when highlighting is stopped here.
8100 if (*--p == 0)
8102 cost += noinvcurs;
8103 while (i && *p++ == 0)
8104 --i;
8106 if (i != 0)
8107 cost = 999; /* different attributes, don't do it */
8109 #ifdef FEAT_MBYTE
8110 if (enc_utf8)
8112 /* Don't use an UTF-8 char for positioning, it's slow. */
8113 for (i = wouldbe_col; i < col; ++i)
8114 if (ScreenLinesUC[LineOffset[row] + i] != 0)
8116 cost = 999;
8117 break;
8120 #endif
8124 * We can do it without term_windgoto()!
8126 if (cost < goto_cost)
8128 if (plan == PLAN_LE)
8130 if (noinvcurs)
8131 screen_stop_highlight();
8132 while (screen_cur_col > col)
8134 out_str(bs);
8135 --screen_cur_col;
8138 else if (plan == PLAN_CR)
8140 if (noinvcurs)
8141 screen_stop_highlight();
8142 out_char('\r');
8143 screen_cur_col = 0;
8145 else if (plan == PLAN_NL)
8147 if (noinvcurs)
8148 screen_stop_highlight();
8149 while (screen_cur_row < row)
8151 out_char('\n');
8152 ++screen_cur_row;
8154 screen_cur_col = 0;
8157 i = col - screen_cur_col;
8158 if (i > 0)
8161 * Use cursor-right if it's one character only. Avoids
8162 * removing a line of pixels from the last bold char, when
8163 * using the bold trick in the GUI.
8165 if (T_ND[0] != NUL && T_ND[1] == NUL)
8167 while (i-- > 0)
8168 out_char(*T_ND);
8170 else
8172 int off;
8174 off = LineOffset[row] + screen_cur_col;
8175 while (i-- > 0)
8177 if (ScreenAttrs[off] != screen_attr)
8178 screen_stop_highlight();
8179 #ifdef FEAT_MBYTE
8180 out_flush_check();
8181 #endif
8182 out_char(ScreenLines[off]);
8183 #ifdef FEAT_MBYTE
8184 if (enc_dbcs == DBCS_JPNU
8185 && ScreenLines[off] == 0x8e)
8186 out_char(ScreenLines2[off]);
8187 #endif
8188 ++off;
8194 else
8195 cost = 999;
8197 if (cost >= goto_cost)
8199 if (noinvcurs)
8200 screen_stop_highlight();
8201 if (row == screen_cur_row && (col > screen_cur_col) &&
8202 *T_CRI != NUL)
8203 term_cursor_right(col - screen_cur_col);
8204 else
8205 term_windgoto(row, col);
8207 screen_cur_row = row;
8208 screen_cur_col = col;
8213 * Set cursor to its position in the current window.
8215 void
8216 setcursor()
8218 if (redrawing())
8220 validate_cursor();
8221 windgoto(W_WINROW(curwin) + curwin->w_wrow,
8222 W_WINCOL(curwin) + (
8223 #ifdef FEAT_RIGHTLEFT
8224 /* With 'rightleft' set and the cursor on a double-wide
8225 * character, position it on the leftmost column. */
8226 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
8227 # ifdef FEAT_MBYTE
8228 (has_mbyte
8229 && (*mb_ptr2cells)(ml_get_cursor()) == 2
8230 && vim_isprintc(gchar_cursor())) ? 2 :
8231 # endif
8232 1)) :
8233 #endif
8234 curwin->w_wcol));
8240 * insert 'line_count' lines at 'row' in window 'wp'
8241 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
8242 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
8243 * scrolling.
8244 * Returns FAIL if the lines are not inserted, OK for success.
8247 win_ins_lines(wp, row, line_count, invalid, mayclear)
8248 win_T *wp;
8249 int row;
8250 int line_count;
8251 int invalid;
8252 int mayclear;
8254 int did_delete;
8255 int nextrow;
8256 int lastrow;
8257 int retval;
8259 if (invalid)
8260 wp->w_lines_valid = 0;
8262 if (wp->w_height < 5)
8263 return FAIL;
8265 if (line_count > wp->w_height - row)
8266 line_count = wp->w_height - row;
8268 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
8269 if (retval != MAYBE)
8270 return retval;
8273 * If there is a next window or a status line, we first try to delete the
8274 * lines at the bottom to avoid messing what is after the window.
8275 * If this fails and there are following windows, don't do anything to avoid
8276 * messing up those windows, better just redraw.
8278 did_delete = FALSE;
8279 #ifdef FEAT_WINDOWS
8280 if (wp->w_next != NULL || wp->w_status_height)
8282 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8283 line_count, (int)Rows, FALSE, NULL) == OK)
8284 did_delete = TRUE;
8285 else if (wp->w_next)
8286 return FAIL;
8288 #endif
8290 * if no lines deleted, blank the lines that will end up below the window
8292 if (!did_delete)
8294 #ifdef FEAT_WINDOWS
8295 wp->w_redr_status = TRUE;
8296 #endif
8297 redraw_cmdline = TRUE;
8298 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
8299 lastrow = nextrow + line_count;
8300 if (lastrow > Rows)
8301 lastrow = Rows;
8302 screen_fill(nextrow - line_count, lastrow - line_count,
8303 W_WINCOL(wp), (int)W_ENDCOL(wp),
8304 ' ', ' ', 0);
8307 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
8308 == FAIL)
8310 /* deletion will have messed up other windows */
8311 if (did_delete)
8313 #ifdef FEAT_WINDOWS
8314 wp->w_redr_status = TRUE;
8315 #endif
8316 win_rest_invalid(W_NEXT(wp));
8318 return FAIL;
8321 return OK;
8325 * delete "line_count" window lines at "row" in window "wp"
8326 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
8327 * If "mayclear" is TRUE the screen will be cleared if it is faster than
8328 * scrolling
8329 * Return OK for success, FAIL if the lines are not deleted.
8332 win_del_lines(wp, row, line_count, invalid, mayclear)
8333 win_T *wp;
8334 int row;
8335 int line_count;
8336 int invalid;
8337 int mayclear;
8339 int retval;
8341 if (invalid)
8342 wp->w_lines_valid = 0;
8344 if (line_count > wp->w_height - row)
8345 line_count = wp->w_height - row;
8347 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
8348 if (retval != MAYBE)
8349 return retval;
8351 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
8352 (int)Rows, FALSE, NULL) == FAIL)
8353 return FAIL;
8355 #ifdef FEAT_WINDOWS
8357 * If there are windows or status lines below, try to put them at the
8358 * correct place. If we can't do that, they have to be redrawn.
8360 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
8362 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8363 line_count, (int)Rows, NULL) == FAIL)
8365 wp->w_redr_status = TRUE;
8366 win_rest_invalid(wp->w_next);
8370 * If this is the last window and there is no status line, redraw the
8371 * command line later.
8373 else
8374 #endif
8375 redraw_cmdline = TRUE;
8376 return OK;
8380 * Common code for win_ins_lines() and win_del_lines().
8381 * Returns OK or FAIL when the work has been done.
8382 * Returns MAYBE when not finished yet.
8384 static int
8385 win_do_lines(wp, row, line_count, mayclear, del)
8386 win_T *wp;
8387 int row;
8388 int line_count;
8389 int mayclear;
8390 int del;
8392 int retval;
8394 if (!redrawing() || line_count <= 0)
8395 return FAIL;
8397 /* only a few lines left: redraw is faster */
8398 if (mayclear && Rows - line_count < 5
8399 #ifdef FEAT_VERTSPLIT
8400 && wp->w_width == Columns
8401 #endif
8404 screenclear(); /* will set wp->w_lines_valid to 0 */
8405 return FAIL;
8409 * Delete all remaining lines
8411 if (row + line_count >= wp->w_height)
8413 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
8414 W_WINCOL(wp), (int)W_ENDCOL(wp),
8415 ' ', ' ', 0);
8416 return OK;
8420 * when scrolling, the message on the command line should be cleared,
8421 * otherwise it will stay there forever.
8423 clear_cmdline = TRUE;
8426 * If the terminal can set a scroll region, use that.
8427 * Always do this in a vertically split window. This will redraw from
8428 * ScreenLines[] when t_CV isn't defined. That's faster than using
8429 * win_line().
8430 * Don't use a scroll region when we are going to redraw the text, writing
8431 * a character in the lower right corner of the scroll region causes a
8432 * scroll-up in the DJGPP version.
8434 if (scroll_region
8435 #ifdef FEAT_VERTSPLIT
8436 || W_WIDTH(wp) != Columns
8437 #endif
8440 #ifdef FEAT_VERTSPLIT
8441 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8442 #endif
8443 scroll_region_set(wp, row);
8444 if (del)
8445 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
8446 wp->w_height - row, FALSE, wp);
8447 else
8448 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
8449 wp->w_height - row, wp);
8450 #ifdef FEAT_VERTSPLIT
8451 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8452 #endif
8453 scroll_region_reset();
8454 return retval;
8457 #ifdef FEAT_WINDOWS
8458 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
8459 return FAIL;
8460 #endif
8462 return MAYBE;
8466 * window 'wp' and everything after it is messed up, mark it for redraw
8468 static void
8469 win_rest_invalid(wp)
8470 win_T *wp;
8472 #ifdef FEAT_WINDOWS
8473 while (wp != NULL)
8474 #else
8475 if (wp != NULL)
8476 #endif
8478 redraw_win_later(wp, NOT_VALID);
8479 #ifdef FEAT_WINDOWS
8480 wp->w_redr_status = TRUE;
8481 wp = wp->w_next;
8482 #endif
8484 redraw_cmdline = TRUE;
8488 * The rest of the routines in this file perform screen manipulations. The
8489 * given operation is performed physically on the screen. The corresponding
8490 * change is also made to the internal screen image. In this way, the editor
8491 * anticipates the effect of editing changes on the appearance of the screen.
8492 * That way, when we call screenupdate a complete redraw isn't usually
8493 * necessary. Another advantage is that we can keep adding code to anticipate
8494 * screen changes, and in the meantime, everything still works.
8498 * types for inserting or deleting lines
8500 #define USE_T_CAL 1
8501 #define USE_T_CDL 2
8502 #define USE_T_AL 3
8503 #define USE_T_CE 4
8504 #define USE_T_DL 5
8505 #define USE_T_SR 6
8506 #define USE_NL 7
8507 #define USE_T_CD 8
8508 #define USE_REDRAW 9
8511 * insert lines on the screen and update ScreenLines[]
8512 * 'end' is the line after the scrolled part. Normally it is Rows.
8513 * When scrolling region used 'off' is the offset from the top for the region.
8514 * 'row' and 'end' are relative to the start of the region.
8516 * return FAIL for failure, OK for success.
8519 screen_ins_lines(off, row, line_count, end, wp)
8520 int off;
8521 int row;
8522 int line_count;
8523 int end;
8524 win_T *wp; /* NULL or window to use width from */
8526 int i;
8527 int j;
8528 unsigned temp;
8529 int cursor_row;
8530 int type;
8531 int result_empty;
8532 int can_ce = can_clear(T_CE);
8535 * FAIL if
8536 * - there is no valid screen
8537 * - the screen has to be redrawn completely
8538 * - the line count is less than one
8539 * - the line count is more than 'ttyscroll'
8541 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
8542 return FAIL;
8545 * There are seven ways to insert lines:
8546 * 0. When in a vertically split window and t_CV isn't set, redraw the
8547 * characters from ScreenLines[].
8548 * 1. Use T_CD (clear to end of display) if it exists and the result of
8549 * the insert is just empty lines
8550 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
8551 * present or line_count > 1. It looks better if we do all the inserts
8552 * at once.
8553 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
8554 * insert is just empty lines and T_CE is not present or line_count >
8555 * 1.
8556 * 4. Use T_AL (insert line) if it exists.
8557 * 5. Use T_CE (erase line) if it exists and the result of the insert is
8558 * just empty lines.
8559 * 6. Use T_DL (delete line) if it exists and the result of the insert is
8560 * just empty lines.
8561 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
8562 * the 'da' flag is not set or we have clear line capability.
8563 * 8. redraw the characters from ScreenLines[].
8565 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
8566 * the scrollbar for the window. It does have insert line, use that if it
8567 * exists.
8569 result_empty = (row + line_count >= end);
8570 #ifdef FEAT_VERTSPLIT
8571 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
8572 type = USE_REDRAW;
8573 else
8574 #endif
8575 if (can_clear(T_CD) && result_empty)
8576 type = USE_T_CD;
8577 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
8578 type = USE_T_CAL;
8579 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
8580 type = USE_T_CDL;
8581 else if (*T_AL != NUL)
8582 type = USE_T_AL;
8583 else if (can_ce && result_empty)
8584 type = USE_T_CE;
8585 else if (*T_DL != NUL && result_empty)
8586 type = USE_T_DL;
8587 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
8588 type = USE_T_SR;
8589 else
8590 return FAIL;
8593 * For clearing the lines screen_del_lines() is used. This will also take
8594 * care of t_db if necessary.
8596 if (type == USE_T_CD || type == USE_T_CDL ||
8597 type == USE_T_CE || type == USE_T_DL)
8598 return screen_del_lines(off, row, line_count, end, FALSE, wp);
8601 * If text is retained below the screen, first clear or delete as many
8602 * lines at the bottom of the window as are about to be inserted so that
8603 * the deleted lines won't later surface during a screen_del_lines.
8605 if (*T_DB)
8606 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
8608 #ifdef FEAT_CLIPBOARD
8609 /* Remove a modeless selection when inserting lines halfway the screen
8610 * or not the full width of the screen. */
8611 if (off + row > 0
8612 # ifdef FEAT_VERTSPLIT
8613 || (wp != NULL && wp->w_width != Columns)
8614 # endif
8616 clip_clear_selection();
8617 else
8618 clip_scroll_selection(-line_count);
8619 #endif
8621 #ifdef FEAT_GUI
8622 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
8623 * scrolling is actually carried out. */
8624 gui_dont_update_cursor();
8625 #endif
8627 if (*T_CCS != NUL) /* cursor relative to region */
8628 cursor_row = row;
8629 else
8630 cursor_row = row + off;
8633 * Shift LineOffset[] line_count down to reflect the inserted lines.
8634 * Clear the inserted lines in ScreenLines[].
8636 row += off;
8637 end += off;
8638 for (i = 0; i < line_count; ++i)
8640 #ifdef FEAT_VERTSPLIT
8641 if (wp != NULL && wp->w_width != Columns)
8643 /* need to copy part of a line */
8644 j = end - 1 - i;
8645 while ((j -= line_count) >= row)
8646 linecopy(j + line_count, j, wp);
8647 j += line_count;
8648 if (can_clear((char_u *)" "))
8649 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
8650 else
8651 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
8652 LineWraps[j] = FALSE;
8654 else
8655 #endif
8657 j = end - 1 - i;
8658 temp = LineOffset[j];
8659 while ((j -= line_count) >= row)
8661 LineOffset[j + line_count] = LineOffset[j];
8662 LineWraps[j + line_count] = LineWraps[j];
8664 LineOffset[j + line_count] = temp;
8665 LineWraps[j + line_count] = FALSE;
8666 if (can_clear((char_u *)" "))
8667 lineclear(temp, (int)Columns);
8668 else
8669 lineinvalid(temp, (int)Columns);
8673 screen_stop_highlight();
8674 windgoto(cursor_row, 0);
8676 #ifdef FEAT_VERTSPLIT
8677 /* redraw the characters */
8678 if (type == USE_REDRAW)
8679 redraw_block(row, end, wp);
8680 else
8681 #endif
8682 if (type == USE_T_CAL)
8684 term_append_lines(line_count);
8685 screen_start(); /* don't know where cursor is now */
8687 else
8689 for (i = 0; i < line_count; i++)
8691 if (type == USE_T_AL)
8693 if (i && cursor_row != 0)
8694 windgoto(cursor_row, 0);
8695 out_str(T_AL);
8697 else /* type == USE_T_SR */
8698 out_str(T_SR);
8699 screen_start(); /* don't know where cursor is now */
8704 * With scroll-reverse and 'da' flag set we need to clear the lines that
8705 * have been scrolled down into the region.
8707 if (type == USE_T_SR && *T_DA)
8709 for (i = 0; i < line_count; ++i)
8711 windgoto(off + i, 0);
8712 out_str(T_CE);
8713 screen_start(); /* don't know where cursor is now */
8717 #ifdef FEAT_GUI
8718 gui_can_update_cursor();
8719 if (gui.in_use)
8720 out_flush(); /* always flush after a scroll */
8721 #endif
8722 return OK;
8726 * delete lines on the screen and update ScreenLines[]
8727 * 'end' is the line after the scrolled part. Normally it is Rows.
8728 * When scrolling region used 'off' is the offset from the top for the region.
8729 * 'row' and 'end' are relative to the start of the region.
8731 * Return OK for success, FAIL if the lines are not deleted.
8734 screen_del_lines(off, row, line_count, end, force, wp)
8735 int off;
8736 int row;
8737 int line_count;
8738 int end;
8739 int force; /* even when line_count > p_ttyscroll */
8740 win_T *wp UNUSED; /* NULL or window to use width from */
8742 int j;
8743 int i;
8744 unsigned temp;
8745 int cursor_row;
8746 int cursor_end;
8747 int result_empty; /* result is empty until end of region */
8748 int can_delete; /* deleting line codes can be used */
8749 int type;
8752 * FAIL if
8753 * - there is no valid screen
8754 * - the screen has to be redrawn completely
8755 * - the line count is less than one
8756 * - the line count is more than 'ttyscroll'
8758 if (!screen_valid(TRUE) || line_count <= 0 ||
8759 (!force && line_count > p_ttyscroll))
8760 return FAIL;
8763 * Check if the rest of the current region will become empty.
8765 result_empty = row + line_count >= end;
8768 * We can delete lines only when 'db' flag not set or when 'ce' option
8769 * available.
8771 can_delete = (*T_DB == NUL || can_clear(T_CE));
8774 * There are six ways to delete lines:
8775 * 0. When in a vertically split window and t_CV isn't set, redraw the
8776 * characters from ScreenLines[].
8777 * 1. Use T_CD if it exists and the result is empty.
8778 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
8779 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
8780 * none of the other ways work.
8781 * 4. Use T_CE (erase line) if the result is empty.
8782 * 5. Use T_DL (delete line) if it exists.
8783 * 6. redraw the characters from ScreenLines[].
8785 #ifdef FEAT_VERTSPLIT
8786 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
8787 type = USE_REDRAW;
8788 else
8789 #endif
8790 if (can_clear(T_CD) && result_empty)
8791 type = USE_T_CD;
8792 #if defined(__BEOS__) && defined(BEOS_DR8)
8794 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
8795 * its internal termcap... this works okay for tests which test *T_DB !=
8796 * NUL. It has the disadvantage that the user cannot use any :set t_*
8797 * command to get T_DB (back) to empty_option, only :set term=... will do
8798 * the trick...
8799 * Anyway, this hack will hopefully go away with the next OS release.
8800 * (Olaf Seibert)
8802 else if (row == 0 && T_DB == empty_option
8803 && (line_count == 1 || *T_CDL == NUL))
8804 #else
8805 else if (row == 0 && (
8806 #ifndef AMIGA
8807 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
8808 * up, so use delete-line command */
8809 line_count == 1 ||
8810 #endif
8811 *T_CDL == NUL))
8812 #endif
8813 type = USE_NL;
8814 else if (*T_CDL != NUL && line_count > 1 && can_delete)
8815 type = USE_T_CDL;
8816 else if (can_clear(T_CE) && result_empty
8817 #ifdef FEAT_VERTSPLIT
8818 && (wp == NULL || wp->w_width == Columns)
8819 #endif
8821 type = USE_T_CE;
8822 else if (*T_DL != NUL && can_delete)
8823 type = USE_T_DL;
8824 else if (*T_CDL != NUL && can_delete)
8825 type = USE_T_CDL;
8826 else
8827 return FAIL;
8829 #ifdef FEAT_CLIPBOARD
8830 /* Remove a modeless selection when deleting lines halfway the screen or
8831 * not the full width of the screen. */
8832 if (off + row > 0
8833 # ifdef FEAT_VERTSPLIT
8834 || (wp != NULL && wp->w_width != Columns)
8835 # endif
8837 clip_clear_selection();
8838 else
8839 clip_scroll_selection(line_count);
8840 #endif
8842 #ifdef FEAT_GUI
8843 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
8844 * scrolling is actually carried out. */
8845 gui_dont_update_cursor();
8846 #endif
8848 if (*T_CCS != NUL) /* cursor relative to region */
8850 cursor_row = row;
8851 cursor_end = end;
8853 else
8855 cursor_row = row + off;
8856 cursor_end = end + off;
8860 * Now shift LineOffset[] line_count up to reflect the deleted lines.
8861 * Clear the inserted lines in ScreenLines[].
8863 row += off;
8864 end += off;
8865 for (i = 0; i < line_count; ++i)
8867 #ifdef FEAT_VERTSPLIT
8868 if (wp != NULL && wp->w_width != Columns)
8870 /* need to copy part of a line */
8871 j = row + i;
8872 while ((j += line_count) <= end - 1)
8873 linecopy(j - line_count, j, wp);
8874 j -= line_count;
8875 if (can_clear((char_u *)" "))
8876 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
8877 else
8878 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
8879 LineWraps[j] = FALSE;
8881 else
8882 #endif
8884 /* whole width, moving the line pointers is faster */
8885 j = row + i;
8886 temp = LineOffset[j];
8887 while ((j += line_count) <= end - 1)
8889 LineOffset[j - line_count] = LineOffset[j];
8890 LineWraps[j - line_count] = LineWraps[j];
8892 LineOffset[j - line_count] = temp;
8893 LineWraps[j - line_count] = FALSE;
8894 if (can_clear((char_u *)" "))
8895 lineclear(temp, (int)Columns);
8896 else
8897 lineinvalid(temp, (int)Columns);
8901 screen_stop_highlight();
8903 #ifdef FEAT_VERTSPLIT
8904 /* redraw the characters */
8905 if (type == USE_REDRAW)
8906 redraw_block(row, end, wp);
8907 else
8908 #endif
8909 if (type == USE_T_CD) /* delete the lines */
8911 windgoto(cursor_row, 0);
8912 out_str(T_CD);
8913 screen_start(); /* don't know where cursor is now */
8915 else if (type == USE_T_CDL)
8917 windgoto(cursor_row, 0);
8918 term_delete_lines(line_count);
8919 screen_start(); /* don't know where cursor is now */
8922 * Deleting lines at top of the screen or scroll region: Just scroll
8923 * the whole screen (scroll region) up by outputting newlines on the
8924 * last line.
8926 else if (type == USE_NL)
8928 windgoto(cursor_end - 1, 0);
8929 for (i = line_count; --i >= 0; )
8930 out_char('\n'); /* cursor will remain on same line */
8932 else
8934 for (i = line_count; --i >= 0; )
8936 if (type == USE_T_DL)
8938 windgoto(cursor_row, 0);
8939 out_str(T_DL); /* delete a line */
8941 else /* type == USE_T_CE */
8943 windgoto(cursor_row + i, 0);
8944 out_str(T_CE); /* erase a line */
8946 screen_start(); /* don't know where cursor is now */
8951 * If the 'db' flag is set, we need to clear the lines that have been
8952 * scrolled up at the bottom of the region.
8954 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
8956 for (i = line_count; i > 0; --i)
8958 windgoto(cursor_end - i, 0);
8959 out_str(T_CE); /* erase a line */
8960 screen_start(); /* don't know where cursor is now */
8964 #ifdef FEAT_GUI
8965 gui_can_update_cursor();
8966 if (gui.in_use)
8967 out_flush(); /* always flush after a scroll */
8968 #endif
8970 return OK;
8974 * show the current mode and ruler
8976 * If clear_cmdline is TRUE, clear the rest of the cmdline.
8977 * If clear_cmdline is FALSE there may be a message there that needs to be
8978 * cleared only if a mode is shown.
8979 * Return the length of the message (0 if no message).
8982 showmode()
8984 int need_clear;
8985 int length = 0;
8986 int do_mode;
8987 int attr;
8988 int nwr_save;
8989 #ifdef FEAT_INS_EXPAND
8990 int sub_attr;
8991 #endif
8993 do_mode = ((p_smd && msg_silent == 0)
8994 && ((State & INSERT)
8995 || restart_edit
8996 #ifdef FEAT_VISUAL
8997 || VIsual_active
8998 #endif
9000 if (do_mode || Recording)
9003 * Don't show mode right now, when not redrawing or inside a mapping.
9004 * Call char_avail() only when we are going to show something, because
9005 * it takes a bit of time.
9007 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
9009 redraw_cmdline = TRUE; /* show mode later */
9010 return 0;
9013 nwr_save = need_wait_return;
9015 /* wait a bit before overwriting an important message */
9016 check_for_delay(FALSE);
9018 /* if the cmdline is more than one line high, erase top lines */
9019 need_clear = clear_cmdline;
9020 if (clear_cmdline && cmdline_row < Rows - 1)
9021 msg_clr_cmdline(); /* will reset clear_cmdline */
9023 /* Position on the last line in the window, column 0 */
9024 msg_pos_mode();
9025 cursor_off();
9026 attr = hl_attr(HLF_CM); /* Highlight mode */
9027 if (do_mode)
9029 MSG_PUTS_ATTR("--", attr);
9030 #if defined(FEAT_XIM)
9031 # if 0 /* old version, changed by SungHyun Nam July 2008 */
9032 if (xic != NULL && im_get_status() && !p_imdisable
9033 && curbuf->b_p_iminsert == B_IMODE_IM)
9034 # else
9035 if (
9036 # ifdef HAVE_GTK2
9037 preedit_get_status()
9038 # else
9039 im_get_status()
9040 # endif
9042 # endif
9043 # ifdef HAVE_GTK2 /* most of the time, it's not XIM being used */
9044 MSG_PUTS_ATTR(" IM", attr);
9045 # else
9046 MSG_PUTS_ATTR(" XIM", attr);
9047 # endif
9048 #endif
9049 #if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
9050 if (gui.in_use)
9052 if (hangul_input_state_get())
9053 MSG_PUTS_ATTR(" \307\321\261\333", attr); /* HANGUL */
9055 #endif
9056 #ifdef FEAT_INS_EXPAND
9057 if (edit_submode != NULL) /* CTRL-X in Insert mode */
9059 /* These messages can get long, avoid a wrap in a narrow
9060 * window. Prefer showing edit_submode_extra. */
9061 length = (Rows - msg_row) * Columns - 3;
9062 if (edit_submode_extra != NULL)
9063 length -= vim_strsize(edit_submode_extra);
9064 if (length > 0)
9066 if (edit_submode_pre != NULL)
9067 length -= vim_strsize(edit_submode_pre);
9068 if (length - vim_strsize(edit_submode) > 0)
9070 if (edit_submode_pre != NULL)
9071 msg_puts_attr(edit_submode_pre, attr);
9072 msg_puts_attr(edit_submode, attr);
9074 if (edit_submode_extra != NULL)
9076 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
9077 if ((int)edit_submode_highl < (int)HLF_COUNT)
9078 sub_attr = hl_attr(edit_submode_highl);
9079 else
9080 sub_attr = attr;
9081 msg_puts_attr(edit_submode_extra, sub_attr);
9084 length = 0;
9086 else
9087 #endif
9089 #ifdef FEAT_VREPLACE
9090 if (State & VREPLACE_FLAG)
9091 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
9092 else
9093 #endif
9094 if (State & REPLACE_FLAG)
9095 MSG_PUTS_ATTR(_(" REPLACE"), attr);
9096 else if (State & INSERT)
9098 #ifdef FEAT_RIGHTLEFT
9099 if (p_ri)
9100 MSG_PUTS_ATTR(_(" REVERSE"), attr);
9101 #endif
9102 MSG_PUTS_ATTR(_(" INSERT"), attr);
9104 else if (restart_edit == 'I')
9105 MSG_PUTS_ATTR(_(" (insert)"), attr);
9106 else if (restart_edit == 'R')
9107 MSG_PUTS_ATTR(_(" (replace)"), attr);
9108 else if (restart_edit == 'V')
9109 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
9110 #ifdef FEAT_RIGHTLEFT
9111 if (p_hkmap)
9112 MSG_PUTS_ATTR(_(" Hebrew"), attr);
9113 # ifdef FEAT_FKMAP
9114 if (p_fkmap)
9115 MSG_PUTS_ATTR(farsi_text_5, attr);
9116 # endif
9117 #endif
9118 #ifdef FEAT_KEYMAP
9119 if (State & LANGMAP)
9121 # ifdef FEAT_ARABIC
9122 if (curwin->w_p_arab)
9123 MSG_PUTS_ATTR(_(" Arabic"), attr);
9124 else
9125 # endif
9126 MSG_PUTS_ATTR(_(" (lang)"), attr);
9128 #endif
9129 if ((State & INSERT) && p_paste)
9130 MSG_PUTS_ATTR(_(" (paste)"), attr);
9132 #ifdef FEAT_VISUAL
9133 if (VIsual_active)
9135 char *p;
9137 /* Don't concatenate separate words to avoid translation
9138 * problems. */
9139 switch ((VIsual_select ? 4 : 0)
9140 + (VIsual_mode == Ctrl_V) * 2
9141 + (VIsual_mode == 'V'))
9143 case 0: p = N_(" VISUAL"); break;
9144 case 1: p = N_(" VISUAL LINE"); break;
9145 case 2: p = N_(" VISUAL BLOCK"); break;
9146 case 4: p = N_(" SELECT"); break;
9147 case 5: p = N_(" SELECT LINE"); break;
9148 default: p = N_(" SELECT BLOCK"); break;
9150 MSG_PUTS_ATTR(_(p), attr);
9152 #endif
9153 MSG_PUTS_ATTR(" --", attr);
9156 need_clear = TRUE;
9158 if (Recording
9159 #ifdef FEAT_INS_EXPAND
9160 && edit_submode == NULL /* otherwise it gets too long */
9161 #endif
9164 MSG_PUTS_ATTR(_("recording"), attr);
9165 need_clear = TRUE;
9168 mode_displayed = TRUE;
9169 if (need_clear || clear_cmdline)
9170 msg_clr_eos();
9171 msg_didout = FALSE; /* overwrite this message */
9172 length = msg_col;
9173 msg_col = 0;
9174 need_wait_return = nwr_save; /* never ask for hit-return for this */
9176 else if (clear_cmdline && msg_silent == 0)
9177 /* Clear the whole command line. Will reset "clear_cmdline". */
9178 msg_clr_cmdline();
9180 #ifdef FEAT_CMDL_INFO
9181 # ifdef FEAT_VISUAL
9182 /* In Visual mode the size of the selected area must be redrawn. */
9183 if (VIsual_active)
9184 clear_showcmd();
9185 # endif
9187 /* If the last window has no status line, the ruler is after the mode
9188 * message and must be redrawn */
9189 if (redrawing()
9190 # ifdef FEAT_WINDOWS
9191 && lastwin->w_status_height == 0
9192 # endif
9194 win_redr_ruler(lastwin, TRUE);
9195 #endif
9196 redraw_cmdline = FALSE;
9197 clear_cmdline = FALSE;
9199 return length;
9203 * Position for a mode message.
9205 static void
9206 msg_pos_mode()
9208 msg_col = 0;
9209 msg_row = Rows - 1;
9213 * Delete mode message. Used when ESC is typed which is expected to end
9214 * Insert mode (but Insert mode didn't end yet!).
9215 * Caller should check "mode_displayed".
9217 void
9218 unshowmode(force)
9219 int force;
9222 * Don't delete it right now, when not redrawing or insided a mapping.
9224 if (!redrawing() || (!force && char_avail() && !KeyTyped))
9225 redraw_cmdline = TRUE; /* delete mode later */
9226 else
9228 msg_pos_mode();
9229 if (Recording)
9230 MSG_PUTS_ATTR(_("recording"), hl_attr(HLF_CM));
9231 msg_clr_eos();
9235 #if defined(FEAT_WINDOWS)
9237 * Draw the tab pages line at the top of the Vim window.
9239 static void
9240 draw_tabline()
9242 int tabcount = 0;
9243 tabpage_T *tp;
9244 int tabwidth;
9245 int col = 0;
9246 int scol = 0;
9247 int attr;
9248 win_T *wp;
9249 win_T *cwp;
9250 int wincount;
9251 int modified;
9252 int c;
9253 int len;
9254 int attr_sel = hl_attr(HLF_TPS);
9255 int attr_nosel = hl_attr(HLF_TP);
9256 int attr_fill = hl_attr(HLF_TPF);
9257 char_u *p;
9258 int room;
9259 int use_sep_chars = (t_colors < 8
9260 #ifdef FEAT_GUI
9261 && !gui.in_use
9262 #endif
9265 redraw_tabline = FALSE;
9267 #ifdef FEAT_GUI_TABLINE
9268 /* Take care of a GUI tabline. */
9269 if (gui_use_tabline())
9271 gui_update_tabline();
9272 return;
9274 #endif
9276 if (tabline_height() < 1)
9277 return;
9279 #if defined(FEAT_STL_OPT)
9281 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
9282 for (scol = 0; scol < Columns; ++scol)
9283 TabPageIdxs[scol] = 0;
9285 /* Use the 'tabline' option if it's set. */
9286 if (*p_tal != NUL)
9288 int save_called_emsg = called_emsg;
9290 /* Check for an error. If there is one we would loop in redrawing the
9291 * screen. Avoid that by making 'tabline' empty. */
9292 called_emsg = FALSE;
9293 win_redr_custom(NULL, FALSE);
9294 if (called_emsg)
9295 set_string_option_direct((char_u *)"tabline", -1,
9296 (char_u *)"", OPT_FREE, SID_ERROR);
9297 called_emsg |= save_called_emsg;
9299 else
9300 #endif
9302 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
9303 ++tabcount;
9305 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
9306 if (tabwidth < 6)
9307 tabwidth = 6;
9309 attr = attr_nosel;
9310 tabcount = 0;
9311 scol = 0;
9312 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
9313 tp = tp->tp_next)
9315 scol = col;
9317 if (tp->tp_topframe == topframe)
9318 attr = attr_sel;
9319 if (use_sep_chars && col > 0)
9320 screen_putchar('|', 0, col++, attr);
9322 if (tp->tp_topframe != topframe)
9323 attr = attr_nosel;
9325 screen_putchar(' ', 0, col++, attr);
9327 if (tp == curtab)
9329 cwp = curwin;
9330 wp = firstwin;
9332 else
9334 cwp = tp->tp_curwin;
9335 wp = tp->tp_firstwin;
9338 modified = FALSE;
9339 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
9340 if (bufIsChanged(wp->w_buffer))
9341 modified = TRUE;
9342 if (modified || wincount > 1)
9344 if (wincount > 1)
9346 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
9347 len = (int)STRLEN(NameBuff);
9348 if (col + len >= Columns - 3)
9349 break;
9350 screen_puts_len(NameBuff, len, 0, col,
9351 #if defined(FEAT_SYN_HL)
9352 hl_combine_attr(attr, hl_attr(HLF_T))
9353 #else
9354 attr
9355 #endif
9357 col += len;
9359 if (modified)
9360 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
9361 screen_putchar(' ', 0, col++, attr);
9364 room = scol - col + tabwidth - 1;
9365 if (room > 0)
9367 /* Get buffer name in NameBuff[] */
9368 get_trans_bufname(cwp->w_buffer);
9369 shorten_dir(NameBuff);
9370 len = vim_strsize(NameBuff);
9371 p = NameBuff;
9372 #ifdef FEAT_MBYTE
9373 if (has_mbyte)
9374 while (len > room)
9376 len -= ptr2cells(p);
9377 mb_ptr_adv(p);
9379 else
9380 #endif
9381 if (len > room)
9383 p += len - room;
9384 len = room;
9386 if (len > Columns - col - 1)
9387 len = Columns - col - 1;
9389 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
9390 col += len;
9392 screen_putchar(' ', 0, col++, attr);
9394 /* Store the tab page number in TabPageIdxs[], so that
9395 * jump_to_mouse() knows where each one is. */
9396 ++tabcount;
9397 while (scol < col)
9398 TabPageIdxs[scol++] = tabcount;
9401 if (use_sep_chars)
9402 c = '_';
9403 else
9404 c = ' ';
9405 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
9407 /* Put an "X" for closing the current tab if there are several. */
9408 if (first_tabpage->tp_next != NULL)
9410 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
9411 TabPageIdxs[Columns - 1] = -999;
9415 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
9416 * set. */
9417 redraw_tabline = FALSE;
9421 * Get buffer name for "buf" into NameBuff[].
9422 * Takes care of special buffer names and translates special characters.
9424 void
9425 get_trans_bufname(buf)
9426 buf_T *buf;
9428 if (buf_spname(buf) != NULL)
9429 STRCPY(NameBuff, buf_spname(buf));
9430 else
9431 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
9432 trans_characters(NameBuff, MAXPATHL);
9434 #endif
9436 #if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
9438 * Get the character to use in a status line. Get its attributes in "*attr".
9440 static int
9441 fillchar_status(attr, is_curwin)
9442 int *attr;
9443 int is_curwin;
9445 int fill;
9446 if (is_curwin)
9448 *attr = hl_attr(HLF_S);
9449 fill = fill_stl;
9451 else
9453 *attr = hl_attr(HLF_SNC);
9454 fill = fill_stlnc;
9456 /* Use fill when there is highlighting, and highlighting of current
9457 * window differs, or the fillchars differ, or this is not the
9458 * current window */
9459 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
9460 || !is_curwin || firstwin == lastwin)
9461 || (fill_stl != fill_stlnc)))
9462 return fill;
9463 if (is_curwin)
9464 return '^';
9465 return '=';
9467 #endif
9469 #ifdef FEAT_VERTSPLIT
9471 * Get the character to use in a separator between vertically split windows.
9472 * Get its attributes in "*attr".
9474 static int
9475 fillchar_vsep(attr)
9476 int *attr;
9478 *attr = hl_attr(HLF_C);
9479 if (*attr == 0 && fill_vert == ' ')
9480 return '|';
9481 else
9482 return fill_vert;
9484 #endif
9487 * Return TRUE if redrawing should currently be done.
9490 redrawing()
9492 return (!RedrawingDisabled
9493 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
9497 * Return TRUE if printing messages should currently be done.
9500 messaging()
9502 return (!(p_lz && char_avail() && !KeyTyped));
9506 * Show current status info in ruler and various other places
9507 * If always is FALSE, only show ruler if position has changed.
9509 void
9510 showruler(always)
9511 int always;
9513 if (!always && !redrawing())
9514 return;
9515 #ifdef FEAT_INS_EXPAND
9516 if (pum_visible())
9518 # ifdef FEAT_WINDOWS
9519 /* Don't redraw right now, do it later. */
9520 curwin->w_redr_status = TRUE;
9521 # endif
9522 return;
9524 #endif
9525 #if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
9526 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
9528 redraw_custom_statusline(curwin);
9530 else
9531 #endif
9532 #ifdef FEAT_CMDL_INFO
9533 win_redr_ruler(curwin, always);
9534 #endif
9536 #ifdef FEAT_TITLE
9537 if (need_maketitle
9538 # ifdef FEAT_STL_OPT
9539 || (p_icon && (stl_syntax & STL_IN_ICON))
9540 || (p_title && (stl_syntax & STL_IN_TITLE))
9541 # endif
9543 maketitle();
9544 #endif
9545 #ifdef FEAT_WINDOWS
9546 /* Redraw the tab pages line if needed. */
9547 if (redraw_tabline)
9548 draw_tabline();
9549 #endif
9552 #ifdef FEAT_CMDL_INFO
9553 static void
9554 win_redr_ruler(wp, always)
9555 win_T *wp;
9556 int always;
9558 #define RULER_BUF_LEN 70
9559 char_u buffer[RULER_BUF_LEN];
9560 int row;
9561 int fillchar;
9562 int attr;
9563 int empty_line = FALSE;
9564 colnr_T virtcol;
9565 int i;
9566 size_t len;
9567 int o;
9568 #ifdef FEAT_VERTSPLIT
9569 int this_ru_col;
9570 int off = 0;
9571 int width = Columns;
9572 # define WITH_OFF(x) x
9573 # define WITH_WIDTH(x) x
9574 #else
9575 # define WITH_OFF(x) 0
9576 # define WITH_WIDTH(x) Columns
9577 # define this_ru_col ru_col
9578 #endif
9580 /* If 'ruler' off or redrawing disabled, don't do anything */
9581 if (!p_ru)
9582 return;
9585 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
9586 * after deleting lines, before cursor.lnum is corrected.
9588 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
9589 return;
9591 #ifdef FEAT_INS_EXPAND
9592 /* Don't draw the ruler while doing insert-completion, it might overwrite
9593 * the (long) mode message. */
9594 # ifdef FEAT_WINDOWS
9595 if (wp == lastwin && lastwin->w_status_height == 0)
9596 # endif
9597 if (edit_submode != NULL)
9598 return;
9599 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
9600 if (pum_visible())
9601 return;
9602 #endif
9604 #ifdef FEAT_STL_OPT
9605 if (*p_ruf)
9607 int save_called_emsg = called_emsg;
9609 called_emsg = FALSE;
9610 win_redr_custom(wp, TRUE);
9611 if (called_emsg)
9612 set_string_option_direct((char_u *)"rulerformat", -1,
9613 (char_u *)"", OPT_FREE, SID_ERROR);
9614 called_emsg |= save_called_emsg;
9615 return;
9617 #endif
9620 * Check if not in Insert mode and the line is empty (will show "0-1").
9622 if (!(State & INSERT)
9623 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
9624 empty_line = TRUE;
9627 * Only draw the ruler when something changed.
9629 validate_virtcol_win(wp);
9630 if ( redraw_cmdline
9631 || always
9632 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
9633 || wp->w_cursor.col != wp->w_ru_cursor.col
9634 || wp->w_virtcol != wp->w_ru_virtcol
9635 #ifdef FEAT_VIRTUALEDIT
9636 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
9637 #endif
9638 || wp->w_topline != wp->w_ru_topline
9639 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
9640 #ifdef FEAT_DIFF
9641 || wp->w_topfill != wp->w_ru_topfill
9642 #endif
9643 || empty_line != wp->w_ru_empty)
9645 cursor_off();
9646 #ifdef FEAT_WINDOWS
9647 if (wp->w_status_height)
9649 row = W_WINROW(wp) + wp->w_height;
9650 fillchar = fillchar_status(&attr, wp == curwin);
9651 # ifdef FEAT_VERTSPLIT
9652 off = W_WINCOL(wp);
9653 width = W_WIDTH(wp);
9654 # endif
9656 else
9657 #endif
9659 row = Rows - 1;
9660 fillchar = ' ';
9661 attr = 0;
9662 #ifdef FEAT_VERTSPLIT
9663 width = Columns;
9664 off = 0;
9665 #endif
9668 /* In list mode virtcol needs to be recomputed */
9669 virtcol = wp->w_virtcol;
9670 if (wp->w_p_list && lcs_tab1 == NUL)
9672 wp->w_p_list = FALSE;
9673 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
9674 wp->w_p_list = TRUE;
9678 * Some sprintfs return the length, some return a pointer.
9679 * To avoid portability problems we use strlen() here.
9681 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
9682 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
9683 ? 0L
9684 : (long)(wp->w_cursor.lnum));
9685 len = STRLEN(buffer);
9686 col_print(buffer + len, RULER_BUF_LEN - len,
9687 empty_line ? 0 : (int)wp->w_cursor.col + 1,
9688 (int)virtcol + 1);
9691 * Add a "50%" if there is room for it.
9692 * On the last line, don't print in the last column (scrolls the
9693 * screen up on some terminals).
9695 i = (int)STRLEN(buffer);
9696 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
9697 o = i + vim_strsize(buffer + i + 1);
9698 #ifdef FEAT_WINDOWS
9699 if (wp->w_status_height == 0) /* can't use last char of screen */
9700 #endif
9701 ++o;
9702 #ifdef FEAT_VERTSPLIT
9703 this_ru_col = ru_col - (Columns - width);
9704 if (this_ru_col < 0)
9705 this_ru_col = 0;
9706 #endif
9707 /* Never use more than half the window/screen width, leave the other
9708 * half for the filename. */
9709 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
9710 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
9711 if (this_ru_col + o < WITH_WIDTH(width))
9713 while (this_ru_col + o < WITH_WIDTH(width))
9715 #ifdef FEAT_MBYTE
9716 if (has_mbyte)
9717 i += (*mb_char2bytes)(fillchar, buffer + i);
9718 else
9719 #endif
9720 buffer[i++] = fillchar;
9721 ++o;
9723 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
9725 /* Truncate at window boundary. */
9726 #ifdef FEAT_MBYTE
9727 if (has_mbyte)
9729 o = 0;
9730 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
9732 o += (*mb_ptr2cells)(buffer + i);
9733 if (this_ru_col + o > WITH_WIDTH(width))
9735 buffer[i] = NUL;
9736 break;
9740 else
9741 #endif
9742 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
9743 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
9745 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
9746 i = redraw_cmdline;
9747 screen_fill(row, row + 1,
9748 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
9749 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
9750 fillchar, fillchar, attr);
9751 /* don't redraw the cmdline because of showing the ruler */
9752 redraw_cmdline = i;
9753 wp->w_ru_cursor = wp->w_cursor;
9754 wp->w_ru_virtcol = wp->w_virtcol;
9755 wp->w_ru_empty = empty_line;
9756 wp->w_ru_topline = wp->w_topline;
9757 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
9758 #ifdef FEAT_DIFF
9759 wp->w_ru_topfill = wp->w_topfill;
9760 #endif
9763 #endif
9765 #if defined(FEAT_LINEBREAK) || defined(PROTO)
9767 * Return the width of the 'number' column.
9768 * Caller may need to check if 'number' is set.
9769 * Otherwise it depends on 'numberwidth' and the line count.
9772 number_width(wp)
9773 win_T *wp;
9775 int n;
9776 linenr_T lnum;
9778 lnum = wp->w_buffer->b_ml.ml_line_count;
9779 if (lnum == wp->w_nrwidth_line_count)
9780 return wp->w_nrwidth_width;
9781 wp->w_nrwidth_line_count = lnum;
9783 n = 0;
9786 lnum /= 10;
9787 ++n;
9788 } while (lnum > 0);
9790 /* 'numberwidth' gives the minimal width plus one */
9791 if (n < wp->w_p_nuw - 1)
9792 n = wp->w_p_nuw - 1;
9794 wp->w_nrwidth_width = n;
9795 return n;
9797 #endif