Merge branch 'vim' into feat/rel-line-numbers
[vim_extended.git] / src / screen.c
blob305f220c44388e7da1023d342a83e5368b39b528
1 /* vi:set ts=8 sts=4 sw=4:
3 * VIM - Vi IMproved by Bram Moolenaar
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
11 * screen.c: code for displaying on the screen
13 * Output to the screen (console, terminal emulator or GUI window) is minimized
14 * by remembering what is already on the screen, and only updating the parts
15 * that changed.
17 * ScreenLines[off] Contains a copy of the whole screen, as it is currently
18 * displayed (excluding text written by external commands).
19 * ScreenAttrs[off] Contains the associated attributes.
20 * LineOffset[row] Contains the offset into ScreenLines*[] and ScreenAttrs[]
21 * for each line.
22 * LineWraps[row] Flag for each line whether it wraps to the next line.
24 * For double-byte characters, two consecutive bytes in ScreenLines[] can form
25 * one character which occupies two display cells.
26 * For UTF-8 a multi-byte character is converted to Unicode and stored in
27 * ScreenLinesUC[]. ScreenLines[] contains the first byte only. For an ASCII
28 * character without composing chars ScreenLinesUC[] will be 0. When the
29 * character occupies two display cells the next byte in ScreenLines[] is 0.
30 * ScreenLinesC[][] contain up to 'maxcombine' composing characters
31 * (drawn on top of the first character). They are 0 when not used.
32 * ScreenLines2[] is only used for euc-jp to store the second byte if the
33 * first byte is 0x8e (single-width character).
35 * The screen_*() functions write to the screen and handle updating
36 * ScreenLines[].
38 * update_screen() is the function that updates all windows and status lines.
39 * It is called form the main loop when must_redraw is non-zero. It may be
40 * called from other places when an immediate screen update is needed.
42 * The part of the buffer that is displayed in a window is set with:
43 * - w_topline (first buffer line in window)
44 * - w_topfill (filler line above the first line)
45 * - w_leftcol (leftmost window cell in window),
46 * - w_skipcol (skipped window cells of first line)
48 * Commands that only move the cursor around in a window, do not need to take
49 * action to update the display. The main loop will check if w_topline is
50 * valid and update it (scroll the window) when needed.
52 * Commands that scroll a window change w_topline and must call
53 * check_cursor() to move the cursor into the visible part of the window, and
54 * call redraw_later(VALID) to have the window displayed by update_screen()
55 * later.
57 * Commands that change text in the buffer must call changed_bytes() or
58 * changed_lines() to mark the area that changed and will require updating
59 * later. The main loop will call update_screen(), which will update each
60 * window that shows the changed buffer. This assumes text above the change
61 * can remain displayed as it is. Text after the change may need updating for
62 * scrolling, folding and syntax highlighting.
64 * Commands that change how a window is displayed (e.g., setting 'list') or
65 * invalidate the contents of a window in another way (e.g., change fold
66 * settings), must call redraw_later(NOT_VALID) to have the whole window
67 * redisplayed by update_screen() later.
69 * Commands that change how a buffer is displayed (e.g., setting 'tabstop')
70 * must call redraw_curbuf_later(NOT_VALID) to have all the windows for the
71 * buffer redisplayed by update_screen() later.
73 * Commands that change highlighting and possibly cause a scroll too must call
74 * redraw_later(SOME_VALID) to update the whole window but still use scrolling
75 * to avoid redrawing everything. But the length of displayed lines must not
76 * change, use NOT_VALID then.
78 * Commands that move the window position must call redraw_later(NOT_VALID).
79 * TODO: should minimize redrawing by scrolling when possible.
81 * Commands that change everything (e.g., resizing the screen) must call
82 * redraw_all_later(NOT_VALID) or redraw_all_later(CLEAR).
84 * Things that are handled indirectly:
85 * - When messages scroll the screen up, msg_scrolled will be set and
86 * update_screen() called to redraw.
89 #include "vim.h"
92 * The attributes that are actually active for writing to the screen.
94 static int screen_attr = 0;
97 * Positioning the cursor is reduced by remembering the last position.
98 * Mostly used by windgoto() and screen_char().
100 static int screen_cur_row, screen_cur_col; /* last known cursor position */
102 #ifdef FEAT_SEARCH_EXTRA
103 static match_T search_hl; /* used for 'hlsearch' highlight matching */
104 #endif
106 #ifdef FEAT_FOLDING
107 static foldinfo_T win_foldinfo; /* info for 'foldcolumn' */
108 #endif
111 * Buffer for one screen line (characters and attributes).
113 static schar_T *current_ScreenLine;
115 static void win_update __ARGS((win_T *wp));
116 static void win_draw_end __ARGS((win_T *wp, int c1, int c2, int row, int endrow, hlf_T hl));
117 #ifdef FEAT_FOLDING
118 static void fold_line __ARGS((win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row));
119 static void fill_foldcolumn __ARGS((char_u *p, win_T *wp, int closed, linenr_T lnum));
120 static void copy_text_attr __ARGS((int off, char_u *buf, int len, int attr));
121 #endif
122 static int win_line __ARGS((win_T *, linenr_T, int, int, int nochange));
123 static int char_needs_redraw __ARGS((int off_from, int off_to, int cols));
124 #ifdef FEAT_RIGHTLEFT
125 static void screen_line __ARGS((int row, int coloff, int endcol, int clear_width, int rlflag));
126 # define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c), (rl))
127 #else
128 static void screen_line __ARGS((int row, int coloff, int endcol, int clear_width));
129 # define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c))
130 #endif
131 #ifdef FEAT_VERTSPLIT
132 static void draw_vsep_win __ARGS((win_T *wp, int row));
133 #endif
134 #ifdef FEAT_STL_OPT
135 static void redraw_custum_statusline __ARGS((win_T *wp));
136 #endif
137 #ifdef FEAT_SEARCH_EXTRA
138 #define SEARCH_HL_PRIORITY 0
139 static void start_search_hl __ARGS((void));
140 static void end_search_hl __ARGS((void));
141 static void prepare_search_hl __ARGS((win_T *wp, linenr_T lnum));
142 static void next_search_hl __ARGS((win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol));
143 #endif
144 static void screen_start_highlight __ARGS((int attr));
145 static void screen_char __ARGS((unsigned off, int row, int col));
146 #ifdef FEAT_MBYTE
147 static void screen_char_2 __ARGS((unsigned off, int row, int col));
148 #endif
149 static void screenclear2 __ARGS((void));
150 static void lineclear __ARGS((unsigned off, int width));
151 static void lineinvalid __ARGS((unsigned off, int width));
152 #ifdef FEAT_VERTSPLIT
153 static void linecopy __ARGS((int to, int from, win_T *wp));
154 static void redraw_block __ARGS((int row, int end, win_T *wp));
155 #endif
156 static int win_do_lines __ARGS((win_T *wp, int row, int line_count, int mayclear, int del));
157 static void win_rest_invalid __ARGS((win_T *wp));
158 static void msg_pos_mode __ARGS((void));
159 #if defined(FEAT_WINDOWS)
160 static void draw_tabline __ARGS((void));
161 #endif
162 #if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
163 static int fillchar_status __ARGS((int *attr, int is_curwin));
164 #endif
165 #ifdef FEAT_VERTSPLIT
166 static int fillchar_vsep __ARGS((int *attr));
167 #endif
168 #ifdef FEAT_STL_OPT
169 static void win_redr_custom __ARGS((win_T *wp, int draw_ruler));
170 #endif
171 #ifdef FEAT_CMDL_INFO
172 static void win_redr_ruler __ARGS((win_T *wp, int always));
173 #endif
175 #if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
176 /* Ugly global: overrule attribute used by screen_char() */
177 static int screen_char_attr = 0;
178 #endif
181 * Redraw the current window later, with update_screen(type).
182 * Set must_redraw only if not already set to a higher value.
183 * e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
185 void
186 redraw_later(type)
187 int type;
189 redraw_win_later(curwin, type);
192 void
193 redraw_win_later(wp, type)
194 win_T *wp;
195 int type;
197 if (wp->w_redr_type < type)
199 wp->w_redr_type = type;
200 if (type >= NOT_VALID)
201 wp->w_lines_valid = 0;
202 if (must_redraw < type) /* must_redraw is the maximum of all windows */
203 must_redraw = type;
208 * Force a complete redraw later. Also resets the highlighting. To be used
209 * after executing a shell command that messes up the screen.
211 void
212 redraw_later_clear()
214 redraw_all_later(CLEAR);
215 #ifdef FEAT_GUI
216 if (gui.in_use)
217 /* Use a code that will reset gui.highlight_mask in
218 * gui_stop_highlight(). */
219 screen_attr = HL_ALL + 1;
220 else
221 #endif
222 /* Use attributes that is very unlikely to appear in text. */
223 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE;
227 * Mark all windows to be redrawn later.
229 void
230 redraw_all_later(type)
231 int type;
233 win_T *wp;
235 FOR_ALL_WINDOWS(wp)
237 redraw_win_later(wp, type);
242 * Mark all windows that are editing the current buffer to be updated later.
244 void
245 redraw_curbuf_later(type)
246 int type;
248 redraw_buf_later(curbuf, type);
251 void
252 redraw_buf_later(buf, type)
253 buf_T *buf;
254 int type;
256 win_T *wp;
258 FOR_ALL_WINDOWS(wp)
260 if (wp->w_buffer == buf)
261 redraw_win_later(wp, type);
266 * Changed something in the current window, at buffer line "lnum", that
267 * requires that line and possibly other lines to be redrawn.
268 * Used when entering/leaving Insert mode with the cursor on a folded line.
269 * Used to remove the "$" from a change command.
270 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
271 * may become invalid and the whole window will have to be redrawn.
273 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' or 'relativenumber' column
423 * changes. */
424 if (curwin->w_redr_type < NOT_VALID
425 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
426 ? number_width(curwin) : 0))
427 curwin->w_redr_type = NOT_VALID;
428 #endif
431 * Only start redrawing if there is really something to do.
433 if (type == INVERTED)
434 update_curswant();
435 if (curwin->w_redr_type < type
436 && !((type == VALID
437 && curwin->w_lines[0].wl_valid
438 #ifdef FEAT_DIFF
439 && curwin->w_topfill == curwin->w_old_topfill
440 && curwin->w_botfill == curwin->w_old_botfill
441 #endif
442 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
443 #ifdef FEAT_VISUAL
444 || (type == INVERTED
445 && VIsual_active
446 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
447 && curwin->w_old_visual_mode == VIsual_mode
448 && (curwin->w_valid & VALID_VIRTCOL)
449 && curwin->w_old_curswant == curwin->w_curswant)
450 #endif
452 curwin->w_redr_type = type;
454 #ifdef FEAT_WINDOWS
455 /* Redraw the tab pages line if needed. */
456 if (redraw_tabline || type >= NOT_VALID)
457 draw_tabline();
458 #endif
460 #ifdef FEAT_SYN_HL
462 * Correct stored syntax highlighting info for changes in each displayed
463 * buffer. Each buffer must only be done once.
465 FOR_ALL_WINDOWS(wp)
467 if (wp->w_buffer->b_mod_set)
469 # ifdef FEAT_WINDOWS
470 win_T *wwp;
472 /* Check if we already did this buffer. */
473 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
474 if (wwp->w_buffer == wp->w_buffer)
475 break;
476 # endif
477 if (
478 # ifdef FEAT_WINDOWS
479 wwp == wp &&
480 # endif
481 syntax_present(wp->w_buffer))
482 syn_stack_apply_changes(wp->w_buffer);
485 #endif
488 * Go from top to bottom through the windows, redrawing the ones that need
489 * it.
491 #if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
492 did_one = FALSE;
493 #endif
494 #ifdef FEAT_SEARCH_EXTRA
495 search_hl.rm.regprog = NULL;
496 #endif
497 FOR_ALL_WINDOWS(wp)
499 if (wp->w_redr_type != 0)
501 cursor_off();
502 #if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
503 if (!did_one)
505 did_one = TRUE;
506 # ifdef FEAT_SEARCH_EXTRA
507 start_search_hl();
508 # endif
509 # ifdef FEAT_CLIPBOARD
510 /* When Visual area changed, may have to update selection. */
511 if (clip_star.available && clip_isautosel())
512 clip_update_selection();
513 # endif
514 #ifdef FEAT_GUI
515 /* Remove the cursor before starting to do anything, because
516 * scrolling may make it difficult to redraw the text under
517 * it. */
518 if (gui.in_use)
519 gui_undraw_cursor();
520 #endif
522 #endif
523 win_update(wp);
526 #ifdef FEAT_WINDOWS
527 /* redraw status line after the window to minimize cursor movement */
528 if (wp->w_redr_status)
530 cursor_off();
531 win_redr_status(wp);
533 #endif
535 #if defined(FEAT_SEARCH_EXTRA)
536 end_search_hl();
537 #endif
539 #ifdef FEAT_WINDOWS
540 /* Reset b_mod_set flags. Going through all windows is probably faster
541 * than going through all buffers (there could be many buffers). */
542 for (wp = firstwin; wp != NULL; wp = wp->w_next)
543 wp->w_buffer->b_mod_set = FALSE;
544 #else
545 curbuf->b_mod_set = FALSE;
546 #endif
548 updating_screen = FALSE;
549 #ifdef FEAT_GUI
550 gui_may_resize_shell();
551 #endif
553 /* Clear or redraw the command line. Done last, because scrolling may
554 * mess up the command line. */
555 if (clear_cmdline || redraw_cmdline)
556 showmode();
558 /* May put up an introductory message when not editing a file */
559 if (!did_intro && bufempty()
560 && curbuf->b_fname == NULL
561 #ifdef FEAT_WINDOWS
562 && firstwin->w_next == NULL
563 #endif
564 && vim_strchr(p_shm, SHM_INTRO) == NULL)
565 intro_message(FALSE);
566 did_intro = TRUE;
568 #ifdef FEAT_GUI
569 /* Redraw the cursor and update the scrollbars when all screen updating is
570 * done. */
571 if (gui.in_use)
573 out_flush(); /* required before updating the cursor */
574 if (did_one)
575 gui_update_cursor(FALSE, FALSE);
576 gui_update_scrollbars(FALSE);
578 #endif
581 #if defined(FEAT_SIGNS) || defined(FEAT_GUI)
582 static void update_prepare __ARGS((void));
583 static void update_finish __ARGS((void));
586 * Prepare for updating one or more windows.
588 static void
589 update_prepare()
591 cursor_off();
592 updating_screen = TRUE;
593 #ifdef FEAT_GUI
594 /* Remove the cursor before starting to do anything, because scrolling may
595 * make it difficult to redraw the text under it. */
596 if (gui.in_use)
597 gui_undraw_cursor();
598 #endif
599 #ifdef FEAT_SEARCH_EXTRA
600 start_search_hl();
601 #endif
605 * Finish updating one or more windows.
607 static void
608 update_finish()
610 if (redraw_cmdline)
611 showmode();
613 # ifdef FEAT_SEARCH_EXTRA
614 end_search_hl();
615 # endif
617 updating_screen = FALSE;
619 # ifdef FEAT_GUI
620 gui_may_resize_shell();
622 /* Redraw the cursor and update the scrollbars when all screen updating is
623 * done. */
624 if (gui.in_use)
626 out_flush(); /* required before updating the cursor */
627 gui_update_cursor(FALSE, FALSE);
628 gui_update_scrollbars(FALSE);
630 # endif
632 #endif
634 #if defined(FEAT_SIGNS) || defined(PROTO)
635 void
636 update_debug_sign(buf, lnum)
637 buf_T *buf;
638 linenr_T lnum;
640 win_T *wp;
641 int doit = FALSE;
643 # ifdef FEAT_FOLDING
644 win_foldinfo.fi_level = 0;
645 # endif
647 /* update/delete a specific mark */
648 FOR_ALL_WINDOWS(wp)
650 if (buf != NULL && lnum > 0)
652 if (wp->w_buffer == buf && lnum >= wp->w_topline
653 && lnum < wp->w_botline)
655 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
656 wp->w_redraw_top = lnum;
657 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
658 wp->w_redraw_bot = lnum;
659 redraw_win_later(wp, VALID);
662 else
663 redraw_win_later(wp, VALID);
664 if (wp->w_redr_type != 0)
665 doit = TRUE;
668 if (!doit)
669 return;
671 /* update all windows that need updating */
672 update_prepare();
674 # ifdef FEAT_WINDOWS
675 for (wp = firstwin; wp; wp = wp->w_next)
677 if (wp->w_redr_type != 0)
678 win_update(wp);
679 if (wp->w_redr_status)
680 win_redr_status(wp);
682 # else
683 if (curwin->w_redr_type != 0)
684 win_update(curwin);
685 # endif
687 update_finish();
689 #endif
692 #if defined(FEAT_GUI) || defined(PROTO)
694 * Update a single window, its status line and maybe the command line msg.
695 * Used for the GUI scrollbar.
697 void
698 updateWindow(wp)
699 win_T *wp;
701 update_prepare();
703 #ifdef FEAT_CLIPBOARD
704 /* When Visual area changed, may have to update selection. */
705 if (clip_star.available && clip_isautosel())
706 clip_update_selection();
707 #endif
709 win_update(wp);
711 #ifdef FEAT_WINDOWS
712 /* When the screen was cleared redraw the tab pages line. */
713 if (redraw_tabline)
714 draw_tabline();
716 if (wp->w_redr_status
717 # ifdef FEAT_CMDL_INFO
718 || p_ru
719 # endif
720 # ifdef FEAT_STL_OPT
721 || *p_stl != NUL || *wp->w_p_stl != NUL
722 # endif
724 win_redr_status(wp);
725 #endif
727 update_finish();
729 #endif
732 * Update a single window.
734 * This may cause the windows below it also to be redrawn (when clearing the
735 * screen or scrolling lines).
737 * How the window is redrawn depends on wp->w_redr_type. Each type also
738 * implies the one below it.
739 * NOT_VALID redraw the whole window
740 * SOME_VALID redraw the whole window but do scroll when possible
741 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
742 * INVERTED redraw the changed part of the Visual area
743 * INVERTED_ALL redraw the whole Visual area
744 * VALID 1. scroll up/down to adjust for a changed w_topline
745 * 2. update lines at the top when scrolled down
746 * 3. redraw changed text:
747 * - if wp->w_buffer->b_mod_set set, update lines between
748 * b_mod_top and b_mod_bot.
749 * - if wp->w_redraw_top non-zero, redraw lines between
750 * wp->w_redraw_top and wp->w_redr_bot.
751 * - continue redrawing when syntax status is invalid.
752 * 4. if scrolled up, update lines at the bottom.
753 * This results in three areas that may need updating:
754 * top: from first row to top_end (when scrolled down)
755 * mid: from mid_start to mid_end (update inversion or changed text)
756 * bot: from bot_start to last row (when scrolled up)
758 static void
759 win_update(wp)
760 win_T *wp;
762 buf_T *buf = wp->w_buffer;
763 int type;
764 int top_end = 0; /* Below last row of the top area that needs
765 updating. 0 when no top area updating. */
766 int mid_start = 999;/* first row of the mid area that needs
767 updating. 999 when no mid area updating. */
768 int mid_end = 0; /* Below last row of the mid area that needs
769 updating. 0 when no mid area updating. */
770 int bot_start = 999;/* first row of the bot area that needs
771 updating. 999 when no bot area updating */
772 #ifdef FEAT_VISUAL
773 int scrolled_down = FALSE; /* TRUE when scrolled down when
774 w_topline got smaller a bit */
775 #endif
776 #ifdef FEAT_SEARCH_EXTRA
777 matchitem_T *cur; /* points to the match list */
778 int top_to_mod = FALSE; /* redraw above mod_top */
779 #endif
781 int row; /* current window row to display */
782 linenr_T lnum; /* current buffer lnum to display */
783 int idx; /* current index in w_lines[] */
784 int srow; /* starting row of the current line */
786 int eof = FALSE; /* if TRUE, we hit the end of the file */
787 int didline = FALSE; /* if TRUE, we finished the last line */
788 int i;
789 long j;
790 static int recursive = FALSE; /* being called recursively */
791 int old_botline = wp->w_botline;
792 #ifdef FEAT_FOLDING
793 long fold_count;
794 #endif
795 #ifdef FEAT_SYN_HL
796 /* remember what happened to the previous line, to know if
797 * check_visual_highlight() can be used */
798 #define DID_NONE 1 /* didn't update a line */
799 #define DID_LINE 2 /* updated a normal line */
800 #define DID_FOLD 3 /* updated a folded line */
801 int did_update = DID_NONE;
802 linenr_T syntax_last_parsed = 0; /* last parsed text line */
803 #endif
804 linenr_T mod_top = 0;
805 linenr_T mod_bot = 0;
806 #if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
807 int save_got_int;
808 #endif
810 type = wp->w_redr_type;
812 if (type == NOT_VALID)
814 #ifdef FEAT_WINDOWS
815 wp->w_redr_status = TRUE;
816 #endif
817 wp->w_lines_valid = 0;
820 /* Window is zero-height: nothing to draw. */
821 if (wp->w_height == 0)
823 wp->w_redr_type = 0;
824 return;
827 #ifdef FEAT_VERTSPLIT
828 /* Window is zero-width: Only need to draw the separator. */
829 if (wp->w_width == 0)
831 /* draw the vertical separator right of this window */
832 draw_vsep_win(wp, 0);
833 wp->w_redr_type = 0;
834 return;
836 #endif
838 #ifdef FEAT_SEARCH_EXTRA
839 /* Setup for match and 'hlsearch' highlighting. Disable any previous
840 * match */
841 cur = wp->w_match_head;
842 while (cur != NULL)
844 cur->hl.rm = cur->match;
845 if (cur->hlg_id == 0)
846 cur->hl.attr = 0;
847 else
848 cur->hl.attr = syn_id2attr(cur->hlg_id);
849 cur->hl.buf = buf;
850 cur->hl.lnum = 0;
851 cur->hl.first_lnum = 0;
852 # ifdef FEAT_RELTIME
853 /* Set the time limit to 'redrawtime'. */
854 profile_setlimit(p_rdt, &(cur->hl.tm));
855 # endif
856 cur = cur->next;
858 search_hl.buf = buf;
859 search_hl.lnum = 0;
860 search_hl.first_lnum = 0;
861 /* time limit is set at the toplevel, for all windows */
862 #endif
864 #ifdef FEAT_LINEBREAK
865 /* Force redraw when width of 'number' or 'relativenumber' column
866 * changes. */
867 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
868 if (wp->w_nrwidth != i)
870 type = NOT_VALID;
871 wp->w_nrwidth = i;
873 else
874 #endif
876 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
879 * When there are both inserted/deleted lines and specific lines to be
880 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
881 * everything (only happens when redrawing is off for while).
883 type = NOT_VALID;
885 else
888 * Set mod_top to the first line that needs displaying because of
889 * changes. Set mod_bot to the first line after the changes.
891 mod_top = wp->w_redraw_top;
892 if (wp->w_redraw_bot != 0)
893 mod_bot = wp->w_redraw_bot + 1;
894 else
895 mod_bot = 0;
896 wp->w_redraw_top = 0; /* reset for next time */
897 wp->w_redraw_bot = 0;
898 if (buf->b_mod_set)
900 if (mod_top == 0 || mod_top > buf->b_mod_top)
902 mod_top = buf->b_mod_top;
903 #ifdef FEAT_SYN_HL
904 /* Need to redraw lines above the change that may be included
905 * in a pattern match. */
906 if (syntax_present(buf))
908 mod_top -= buf->b_syn_sync_linebreaks;
909 if (mod_top < 1)
910 mod_top = 1;
912 #endif
914 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
915 mod_bot = buf->b_mod_bot;
917 #ifdef FEAT_SEARCH_EXTRA
918 /* When 'hlsearch' is on and using a multi-line search pattern, a
919 * change in one line may make the Search highlighting in a
920 * previous line invalid. Simple solution: redraw all visible
921 * lines above the change.
922 * Same for a match pattern.
924 if (search_hl.rm.regprog != NULL
925 && re_multiline(search_hl.rm.regprog))
926 top_to_mod = TRUE;
927 else
929 cur = wp->w_match_head;
930 while (cur != NULL)
932 if (cur->match.regprog != NULL
933 && re_multiline(cur->match.regprog))
935 top_to_mod = TRUE;
936 break;
938 cur = cur->next;
941 #endif
943 #ifdef FEAT_FOLDING
944 if (mod_top != 0 && hasAnyFolding(wp))
946 linenr_T lnumt, lnumb;
949 * A change in a line can cause lines above it to become folded or
950 * unfolded. Find the top most buffer line that may be affected.
951 * If the line was previously folded and displayed, get the first
952 * line of that fold. If the line is folded now, get the first
953 * folded line. Use the minimum of these two.
956 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
957 * the line below it. If there is no valid entry, use w_topline.
958 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
959 * to this line. If there is no valid entry, use MAXLNUM. */
960 lnumt = wp->w_topline;
961 lnumb = MAXLNUM;
962 for (i = 0; i < wp->w_lines_valid; ++i)
963 if (wp->w_lines[i].wl_valid)
965 if (wp->w_lines[i].wl_lastlnum < mod_top)
966 lnumt = wp->w_lines[i].wl_lastlnum + 1;
967 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
969 lnumb = wp->w_lines[i].wl_lnum;
970 /* When there is a fold column it might need updating
971 * in the next line ("J" just above an open fold). */
972 if (wp->w_p_fdc > 0)
973 ++lnumb;
977 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
978 if (mod_top > lnumt)
979 mod_top = lnumt;
981 /* Now do the same for the bottom line (one above mod_bot). */
982 --mod_bot;
983 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
984 ++mod_bot;
985 if (mod_bot < lnumb)
986 mod_bot = lnumb;
988 #endif
990 /* When a change starts above w_topline and the end is below
991 * w_topline, start redrawing at w_topline.
992 * If the end of the change is above w_topline: do like no change was
993 * made, but redraw the first line to find changes in syntax. */
994 if (mod_top != 0 && mod_top < wp->w_topline)
996 if (mod_bot > wp->w_topline)
997 mod_top = wp->w_topline;
998 #ifdef FEAT_SYN_HL
999 else if (syntax_present(buf))
1000 top_end = 1;
1001 #endif
1004 /* When line numbers are displayed need to redraw all lines below
1005 * inserted/deleted lines. */
1006 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1007 mod_bot = MAXLNUM;
1011 * When only displaying the lines at the top, set top_end. Used when
1012 * window has scrolled down for msg_scrolled.
1014 if (type == REDRAW_TOP)
1016 j = 0;
1017 for (i = 0; i < wp->w_lines_valid; ++i)
1019 j += wp->w_lines[i].wl_size;
1020 if (j >= wp->w_upd_rows)
1022 top_end = j;
1023 break;
1026 if (top_end == 0)
1027 /* not found (cannot happen?): redraw everything */
1028 type = NOT_VALID;
1029 else
1030 /* top area defined, the rest is VALID */
1031 type = VALID;
1034 /* Trick: we want to avoid clearing the screen twice. screenclear() will
1035 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1036 * non-zero and thus not FALSE) will indicate that screenclear() was not
1037 * called. */
1038 if (screen_cleared)
1039 screen_cleared = MAYBE;
1042 * If there are no changes on the screen that require a complete redraw,
1043 * handle three cases:
1044 * 1: we are off the top of the screen by a few lines: scroll down
1045 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1046 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1047 * w_lines[] that needs updating.
1049 if ((type == VALID || type == SOME_VALID
1050 || type == INVERTED || type == INVERTED_ALL)
1051 #ifdef FEAT_DIFF
1052 && !wp->w_botfill && !wp->w_old_botfill
1053 #endif
1056 if (mod_top != 0 && wp->w_topline == mod_top)
1059 * w_topline is the first changed line, the scrolling will be done
1060 * further down.
1063 else if (wp->w_lines[0].wl_valid
1064 && (wp->w_topline < wp->w_lines[0].wl_lnum
1065 #ifdef FEAT_DIFF
1066 || (wp->w_topline == wp->w_lines[0].wl_lnum
1067 && wp->w_topfill > wp->w_old_topfill)
1068 #endif
1072 * New topline is above old topline: May scroll down.
1074 #ifdef FEAT_FOLDING
1075 if (hasAnyFolding(wp))
1077 linenr_T ln;
1079 /* count the number of lines we are off, counting a sequence
1080 * of folded lines as one */
1081 j = 0;
1082 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1084 ++j;
1085 if (j >= wp->w_height - 2)
1086 break;
1087 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1090 else
1091 #endif
1092 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1093 if (j < wp->w_height - 2) /* not too far off */
1095 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1096 #ifdef FEAT_DIFF
1097 /* insert extra lines for previously invisible filler lines */
1098 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1099 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1100 - wp->w_old_topfill;
1101 #endif
1102 if (i < wp->w_height - 2) /* less than a screen off */
1105 * Try to insert the correct number of lines.
1106 * If not the last window, delete the lines at the bottom.
1107 * win_ins_lines may fail when the terminal can't do it.
1109 if (i > 0)
1110 check_for_delay(FALSE);
1111 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1113 if (wp->w_lines_valid != 0)
1115 /* Need to update rows that are new, stop at the
1116 * first one that scrolled down. */
1117 top_end = i;
1118 #ifdef FEAT_VISUAL
1119 scrolled_down = TRUE;
1120 #endif
1122 /* Move the entries that were scrolled, disable
1123 * the entries for the lines to be redrawn. */
1124 if ((wp->w_lines_valid += j) > wp->w_height)
1125 wp->w_lines_valid = wp->w_height;
1126 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1127 wp->w_lines[idx] = wp->w_lines[idx - j];
1128 while (idx >= 0)
1129 wp->w_lines[idx--].wl_valid = FALSE;
1132 else
1133 mid_start = 0; /* redraw all lines */
1135 else
1136 mid_start = 0; /* redraw all lines */
1138 else
1139 mid_start = 0; /* redraw all lines */
1141 else
1144 * New topline is at or below old topline: May scroll up.
1145 * When topline didn't change, find first entry in w_lines[] that
1146 * needs updating.
1149 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1150 j = -1;
1151 row = 0;
1152 for (i = 0; i < wp->w_lines_valid; i++)
1154 if (wp->w_lines[i].wl_valid
1155 && wp->w_lines[i].wl_lnum == wp->w_topline)
1157 j = i;
1158 break;
1160 row += wp->w_lines[i].wl_size;
1162 if (j == -1)
1164 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1165 * lines */
1166 mid_start = 0;
1168 else
1171 * Try to delete the correct number of lines.
1172 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1174 #ifdef FEAT_DIFF
1175 /* If the topline didn't change, delete old filler lines,
1176 * otherwise delete filler lines of the new topline... */
1177 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1178 row += wp->w_old_topfill;
1179 else
1180 row += diff_check_fill(wp, wp->w_topline);
1181 /* ... but don't delete new filler lines. */
1182 row -= wp->w_topfill;
1183 #endif
1184 if (row > 0)
1186 check_for_delay(FALSE);
1187 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1188 bot_start = wp->w_height - row;
1189 else
1190 mid_start = 0; /* redraw all lines */
1192 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1195 * Skip the lines (below the deleted lines) that are still
1196 * valid and don't need redrawing. Copy their info
1197 * upwards, to compensate for the deleted lines. Set
1198 * bot_start to the first row that needs redrawing.
1200 bot_start = 0;
1201 idx = 0;
1202 for (;;)
1204 wp->w_lines[idx] = wp->w_lines[j];
1205 /* stop at line that didn't fit, unless it is still
1206 * valid (no lines deleted) */
1207 if (row > 0 && bot_start + row
1208 + (int)wp->w_lines[j].wl_size > wp->w_height)
1210 wp->w_lines_valid = idx + 1;
1211 break;
1213 bot_start += wp->w_lines[idx++].wl_size;
1215 /* stop at the last valid entry in w_lines[].wl_size */
1216 if (++j >= wp->w_lines_valid)
1218 wp->w_lines_valid = idx;
1219 break;
1222 #ifdef FEAT_DIFF
1223 /* Correct the first entry for filler lines at the top
1224 * when it won't get updated below. */
1225 if (wp->w_p_diff && bot_start > 0)
1226 wp->w_lines[0].wl_size =
1227 plines_win_nofill(wp, wp->w_topline, TRUE)
1228 + wp->w_topfill;
1229 #endif
1234 /* When starting redraw in the first line, redraw all lines. When
1235 * there is only one window it's probably faster to clear the screen
1236 * first. */
1237 if (mid_start == 0)
1239 mid_end = wp->w_height;
1240 if (lastwin == firstwin)
1242 /* Clear the screen when it was not done by win_del_lines() or
1243 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1244 * then. */
1245 if (screen_cleared != TRUE)
1246 screenclear();
1247 #ifdef FEAT_WINDOWS
1248 /* The screen was cleared, redraw the tab pages line. */
1249 if (redraw_tabline)
1250 draw_tabline();
1251 #endif
1255 /* When win_del_lines() or win_ins_lines() caused the screen to be
1256 * cleared (only happens for the first window) or when screenclear()
1257 * was called directly above, "must_redraw" will have been set to
1258 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1259 if (screen_cleared == TRUE)
1260 must_redraw = 0;
1262 else
1264 /* Not VALID or INVERTED: redraw all lines. */
1265 mid_start = 0;
1266 mid_end = wp->w_height;
1269 if (type == SOME_VALID)
1271 /* SOME_VALID: redraw all lines. */
1272 mid_start = 0;
1273 mid_end = wp->w_height;
1274 type = NOT_VALID;
1277 #ifdef FEAT_VISUAL
1278 /* check if we are updating or removing the inverted part */
1279 if ((VIsual_active && buf == curwin->w_buffer)
1280 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1282 linenr_T from, to;
1284 if (VIsual_active)
1286 if (VIsual_active
1287 && (VIsual_mode != wp->w_old_visual_mode
1288 || type == INVERTED_ALL))
1291 * If the type of Visual selection changed, redraw the whole
1292 * selection. Also when the ownership of the X selection is
1293 * gained or lost.
1295 if (curwin->w_cursor.lnum < VIsual.lnum)
1297 from = curwin->w_cursor.lnum;
1298 to = VIsual.lnum;
1300 else
1302 from = VIsual.lnum;
1303 to = curwin->w_cursor.lnum;
1305 /* redraw more when the cursor moved as well */
1306 if (wp->w_old_cursor_lnum < from)
1307 from = wp->w_old_cursor_lnum;
1308 if (wp->w_old_cursor_lnum > to)
1309 to = wp->w_old_cursor_lnum;
1310 if (wp->w_old_visual_lnum < from)
1311 from = wp->w_old_visual_lnum;
1312 if (wp->w_old_visual_lnum > to)
1313 to = wp->w_old_visual_lnum;
1315 else
1318 * Find the line numbers that need to be updated: The lines
1319 * between the old cursor position and the current cursor
1320 * position. Also check if the Visual position changed.
1322 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1324 from = curwin->w_cursor.lnum;
1325 to = wp->w_old_cursor_lnum;
1327 else
1329 from = wp->w_old_cursor_lnum;
1330 to = curwin->w_cursor.lnum;
1331 if (from == 0) /* Visual mode just started */
1332 from = to;
1335 if (VIsual.lnum != wp->w_old_visual_lnum
1336 || VIsual.col != wp->w_old_visual_col)
1338 if (wp->w_old_visual_lnum < from
1339 && wp->w_old_visual_lnum != 0)
1340 from = wp->w_old_visual_lnum;
1341 if (wp->w_old_visual_lnum > to)
1342 to = wp->w_old_visual_lnum;
1343 if (VIsual.lnum < from)
1344 from = VIsual.lnum;
1345 if (VIsual.lnum > to)
1346 to = VIsual.lnum;
1351 * If in block mode and changed column or curwin->w_curswant:
1352 * update all lines.
1353 * First compute the actual start and end column.
1355 if (VIsual_mode == Ctrl_V)
1357 colnr_T fromc, toc;
1359 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
1360 ++toc;
1361 if (curwin->w_curswant == MAXCOL)
1362 toc = MAXCOL;
1364 if (fromc != wp->w_old_cursor_fcol
1365 || toc != wp->w_old_cursor_lcol)
1367 if (from > VIsual.lnum)
1368 from = VIsual.lnum;
1369 if (to < VIsual.lnum)
1370 to = VIsual.lnum;
1372 wp->w_old_cursor_fcol = fromc;
1373 wp->w_old_cursor_lcol = toc;
1376 else
1378 /* Use the line numbers of the old Visual area. */
1379 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1381 from = wp->w_old_cursor_lnum;
1382 to = wp->w_old_visual_lnum;
1384 else
1386 from = wp->w_old_visual_lnum;
1387 to = wp->w_old_cursor_lnum;
1392 * There is no need to update lines above the top of the window.
1394 if (from < wp->w_topline)
1395 from = wp->w_topline;
1398 * If we know the value of w_botline, use it to restrict the update to
1399 * the lines that are visible in the window.
1401 if (wp->w_valid & VALID_BOTLINE)
1403 if (from >= wp->w_botline)
1404 from = wp->w_botline - 1;
1405 if (to >= wp->w_botline)
1406 to = wp->w_botline - 1;
1410 * Find the minimal part to be updated.
1411 * Watch out for scrolling that made entries in w_lines[] invalid.
1412 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1413 * top_end; need to redraw from top_end to the "to" line.
1414 * A middle mouse click with a Visual selection may change the text
1415 * above the Visual area and reset wl_valid, do count these for
1416 * mid_end (in srow).
1418 if (mid_start > 0)
1420 lnum = wp->w_topline;
1421 idx = 0;
1422 srow = 0;
1423 if (scrolled_down)
1424 mid_start = top_end;
1425 else
1426 mid_start = 0;
1427 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1429 if (wp->w_lines[idx].wl_valid)
1430 mid_start += wp->w_lines[idx].wl_size;
1431 else if (!scrolled_down)
1432 srow += wp->w_lines[idx].wl_size;
1433 ++idx;
1434 # ifdef FEAT_FOLDING
1435 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1436 lnum = wp->w_lines[idx].wl_lnum;
1437 else
1438 # endif
1439 ++lnum;
1441 srow += mid_start;
1442 mid_end = wp->w_height;
1443 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1445 if (wp->w_lines[idx].wl_valid
1446 && wp->w_lines[idx].wl_lnum >= to + 1)
1448 /* Only update until first row of this line */
1449 mid_end = srow;
1450 break;
1452 srow += wp->w_lines[idx].wl_size;
1457 if (VIsual_active && buf == curwin->w_buffer)
1459 wp->w_old_visual_mode = VIsual_mode;
1460 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1461 wp->w_old_visual_lnum = VIsual.lnum;
1462 wp->w_old_visual_col = VIsual.col;
1463 wp->w_old_curswant = curwin->w_curswant;
1465 else
1467 wp->w_old_visual_mode = 0;
1468 wp->w_old_cursor_lnum = 0;
1469 wp->w_old_visual_lnum = 0;
1470 wp->w_old_visual_col = 0;
1472 #endif /* FEAT_VISUAL */
1474 #if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1475 /* reset got_int, otherwise regexp won't work */
1476 save_got_int = got_int;
1477 got_int = 0;
1478 #endif
1479 #ifdef FEAT_FOLDING
1480 win_foldinfo.fi_level = 0;
1481 #endif
1484 * Update all the window rows.
1486 idx = 0; /* first entry in w_lines[].wl_size */
1487 row = 0;
1488 srow = 0;
1489 lnum = wp->w_topline; /* first line shown in window */
1490 for (;;)
1492 /* stop updating when reached the end of the window (check for _past_
1493 * the end of the window is at the end of the loop) */
1494 if (row == wp->w_height)
1496 didline = TRUE;
1497 break;
1500 /* stop updating when hit the end of the file */
1501 if (lnum > buf->b_ml.ml_line_count)
1503 eof = TRUE;
1504 break;
1507 /* Remember the starting row of the line that is going to be dealt
1508 * with. It is used further down when the line doesn't fit. */
1509 srow = row;
1512 * Update a line when it is in an area that needs updating, when it
1513 * has changes or w_lines[idx] is invalid.
1514 * bot_start may be halfway a wrapped line after using
1515 * win_del_lines(), check if the current line includes it.
1516 * When syntax folding is being used, the saved syntax states will
1517 * already have been updated, we can't see where the syntax state is
1518 * the same again, just update until the end of the window.
1520 if (row < top_end
1521 || (row >= mid_start && row < mid_end)
1522 #ifdef FEAT_SEARCH_EXTRA
1523 || top_to_mod
1524 #endif
1525 || idx >= wp->w_lines_valid
1526 || (row + wp->w_lines[idx].wl_size > bot_start)
1527 || (mod_top != 0
1528 && (lnum == mod_top
1529 || (lnum >= mod_top
1530 && (lnum < mod_bot
1531 #ifdef FEAT_SYN_HL
1532 || did_update == DID_FOLD
1533 || (did_update == DID_LINE
1534 && syntax_present(buf)
1535 && (
1536 # ifdef FEAT_FOLDING
1537 (foldmethodIsSyntax(wp)
1538 && hasAnyFolding(wp)) ||
1539 # endif
1540 syntax_check_changed(lnum)))
1541 #endif
1542 )))))
1544 #ifdef FEAT_SEARCH_EXTRA
1545 if (lnum == mod_top)
1546 top_to_mod = FALSE;
1547 #endif
1550 * When at start of changed lines: May scroll following lines
1551 * up or down to minimize redrawing.
1552 * Don't do this when the change continues until the end.
1553 * Don't scroll when dollar_vcol is non-zero, keep the "$".
1555 if (lnum == mod_top
1556 && mod_bot != MAXLNUM
1557 && !(dollar_vcol != 0 && mod_bot == mod_top + 1))
1559 int old_rows = 0;
1560 int new_rows = 0;
1561 int xtra_rows;
1562 linenr_T l;
1564 /* Count the old number of window rows, using w_lines[], which
1565 * should still contain the sizes for the lines as they are
1566 * currently displayed. */
1567 for (i = idx; i < wp->w_lines_valid; ++i)
1569 /* Only valid lines have a meaningful wl_lnum. Invalid
1570 * lines are part of the changed area. */
1571 if (wp->w_lines[i].wl_valid
1572 && wp->w_lines[i].wl_lnum == mod_bot)
1573 break;
1574 old_rows += wp->w_lines[i].wl_size;
1575 #ifdef FEAT_FOLDING
1576 if (wp->w_lines[i].wl_valid
1577 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1579 /* Must have found the last valid entry above mod_bot.
1580 * Add following invalid entries. */
1581 ++i;
1582 while (i < wp->w_lines_valid
1583 && !wp->w_lines[i].wl_valid)
1584 old_rows += wp->w_lines[i++].wl_size;
1585 break;
1587 #endif
1590 if (i >= wp->w_lines_valid)
1592 /* We can't find a valid line below the changed lines,
1593 * need to redraw until the end of the window.
1594 * Inserting/deleting lines has no use. */
1595 bot_start = 0;
1597 else
1599 /* Able to count old number of rows: Count new window
1600 * rows, and may insert/delete lines */
1601 j = idx;
1602 for (l = lnum; l < mod_bot; ++l)
1604 #ifdef FEAT_FOLDING
1605 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1606 ++new_rows;
1607 else
1608 #endif
1609 #ifdef FEAT_DIFF
1610 if (l == wp->w_topline)
1611 new_rows += plines_win_nofill(wp, l, TRUE)
1612 + wp->w_topfill;
1613 else
1614 #endif
1615 new_rows += plines_win(wp, l, TRUE);
1616 ++j;
1617 if (new_rows > wp->w_height - row - 2)
1619 /* it's getting too much, must redraw the rest */
1620 new_rows = 9999;
1621 break;
1624 xtra_rows = new_rows - old_rows;
1625 if (xtra_rows < 0)
1627 /* May scroll text up. If there is not enough
1628 * remaining text or scrolling fails, must redraw the
1629 * rest. If scrolling works, must redraw the text
1630 * below the scrolled text. */
1631 if (row - xtra_rows >= wp->w_height - 2)
1632 mod_bot = MAXLNUM;
1633 else
1635 check_for_delay(FALSE);
1636 if (win_del_lines(wp, row,
1637 -xtra_rows, FALSE, FALSE) == FAIL)
1638 mod_bot = MAXLNUM;
1639 else
1640 bot_start = wp->w_height + xtra_rows;
1643 else if (xtra_rows > 0)
1645 /* May scroll text down. If there is not enough
1646 * remaining text of scrolling fails, must redraw the
1647 * rest. */
1648 if (row + xtra_rows >= wp->w_height - 2)
1649 mod_bot = MAXLNUM;
1650 else
1652 check_for_delay(FALSE);
1653 if (win_ins_lines(wp, row + old_rows,
1654 xtra_rows, FALSE, FALSE) == FAIL)
1655 mod_bot = MAXLNUM;
1656 else if (top_end > row + old_rows)
1657 /* Scrolled the part at the top that requires
1658 * updating down. */
1659 top_end += xtra_rows;
1663 /* When not updating the rest, may need to move w_lines[]
1664 * entries. */
1665 if (mod_bot != MAXLNUM && i != j)
1667 if (j < i)
1669 int x = row + new_rows;
1671 /* move entries in w_lines[] upwards */
1672 for (;;)
1674 /* stop at last valid entry in w_lines[] */
1675 if (i >= wp->w_lines_valid)
1677 wp->w_lines_valid = j;
1678 break;
1680 wp->w_lines[j] = wp->w_lines[i];
1681 /* stop at a line that won't fit */
1682 if (x + (int)wp->w_lines[j].wl_size
1683 > wp->w_height)
1685 wp->w_lines_valid = j + 1;
1686 break;
1688 x += wp->w_lines[j++].wl_size;
1689 ++i;
1691 if (bot_start > x)
1692 bot_start = x;
1694 else /* j > i */
1696 /* move entries in w_lines[] downwards */
1697 j -= i;
1698 wp->w_lines_valid += j;
1699 if (wp->w_lines_valid > wp->w_height)
1700 wp->w_lines_valid = wp->w_height;
1701 for (i = wp->w_lines_valid; i - j >= idx; --i)
1702 wp->w_lines[i] = wp->w_lines[i - j];
1704 /* The w_lines[] entries for inserted lines are
1705 * now invalid, but wl_size may be used above.
1706 * Reset to zero. */
1707 while (i >= idx)
1709 wp->w_lines[i].wl_size = 0;
1710 wp->w_lines[i--].wl_valid = FALSE;
1717 #ifdef FEAT_FOLDING
1719 * When lines are folded, display one line for all of them.
1720 * Otherwise, display normally (can be several display lines when
1721 * 'wrap' is on).
1723 fold_count = foldedCount(wp, lnum, &win_foldinfo);
1724 if (fold_count != 0)
1726 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
1727 ++row;
1728 --fold_count;
1729 wp->w_lines[idx].wl_folded = TRUE;
1730 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
1731 # ifdef FEAT_SYN_HL
1732 did_update = DID_FOLD;
1733 # endif
1735 else
1736 #endif
1737 if (idx < wp->w_lines_valid
1738 && wp->w_lines[idx].wl_valid
1739 && wp->w_lines[idx].wl_lnum == lnum
1740 && lnum > wp->w_topline
1741 && !(dy_flags & DY_LASTLINE)
1742 && srow + wp->w_lines[idx].wl_size > wp->w_height
1743 #ifdef FEAT_DIFF
1744 && diff_check_fill(wp, lnum) == 0
1745 #endif
1748 /* This line is not going to fit. Don't draw anything here,
1749 * will draw "@ " lines below. */
1750 row = wp->w_height + 1;
1752 else
1754 #ifdef FEAT_SEARCH_EXTRA
1755 prepare_search_hl(wp, lnum);
1756 #endif
1757 #ifdef FEAT_SYN_HL
1758 /* Let the syntax stuff know we skipped a few lines. */
1759 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
1760 && syntax_present(buf))
1761 syntax_end_parsing(syntax_last_parsed + 1);
1762 #endif
1765 * Display one line.
1767 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
1769 #ifdef FEAT_FOLDING
1770 wp->w_lines[idx].wl_folded = FALSE;
1771 wp->w_lines[idx].wl_lastlnum = lnum;
1772 #endif
1773 #ifdef FEAT_SYN_HL
1774 did_update = DID_LINE;
1775 syntax_last_parsed = lnum;
1776 #endif
1779 wp->w_lines[idx].wl_lnum = lnum;
1780 wp->w_lines[idx].wl_valid = TRUE;
1781 if (row > wp->w_height) /* past end of screen */
1783 /* we may need the size of that too long line later on */
1784 if (dollar_vcol == 0)
1785 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
1786 ++idx;
1787 break;
1789 if (dollar_vcol == 0)
1790 wp->w_lines[idx].wl_size = row - srow;
1791 ++idx;
1792 #ifdef FEAT_FOLDING
1793 lnum += fold_count + 1;
1794 #else
1795 ++lnum;
1796 #endif
1798 else
1800 /* This line does not need updating, advance to the next one */
1801 row += wp->w_lines[idx++].wl_size;
1802 if (row > wp->w_height) /* past end of screen */
1803 break;
1804 #ifdef FEAT_FOLDING
1805 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
1806 #else
1807 ++lnum;
1808 #endif
1809 #ifdef FEAT_SYN_HL
1810 did_update = DID_NONE;
1811 #endif
1814 if (lnum > buf->b_ml.ml_line_count)
1816 eof = TRUE;
1817 break;
1821 * End of loop over all window lines.
1825 if (idx > wp->w_lines_valid)
1826 wp->w_lines_valid = idx;
1828 #ifdef FEAT_SYN_HL
1830 * Let the syntax stuff know we stop parsing here.
1832 if (syntax_last_parsed != 0 && syntax_present(buf))
1833 syntax_end_parsing(syntax_last_parsed + 1);
1834 #endif
1837 * If we didn't hit the end of the file, and we didn't finish the last
1838 * line we were working on, then the line didn't fit.
1840 wp->w_empty_rows = 0;
1841 #ifdef FEAT_DIFF
1842 wp->w_filler_rows = 0;
1843 #endif
1844 if (!eof && !didline)
1846 if (lnum == wp->w_topline)
1849 * Single line that does not fit!
1850 * Don't overwrite it, it can be edited.
1852 wp->w_botline = lnum + 1;
1854 #ifdef FEAT_DIFF
1855 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
1857 /* Window ends in filler lines. */
1858 wp->w_botline = lnum;
1859 wp->w_filler_rows = wp->w_height - srow;
1861 #endif
1862 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
1865 * Last line isn't finished: Display "@@@" at the end.
1867 screen_fill(W_WINROW(wp) + wp->w_height - 1,
1868 W_WINROW(wp) + wp->w_height,
1869 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
1870 '@', '@', hl_attr(HLF_AT));
1871 set_empty_rows(wp, srow);
1872 wp->w_botline = lnum;
1874 else
1876 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
1877 wp->w_botline = lnum;
1880 else
1882 #ifdef FEAT_VERTSPLIT
1883 draw_vsep_win(wp, row);
1884 #endif
1885 if (eof) /* we hit the end of the file */
1887 wp->w_botline = buf->b_ml.ml_line_count + 1;
1888 #ifdef FEAT_DIFF
1889 j = diff_check_fill(wp, wp->w_botline);
1890 if (j > 0 && !wp->w_botfill)
1893 * Display filler lines at the end of the file
1895 if (char2cells(fill_diff) > 1)
1896 i = '-';
1897 else
1898 i = fill_diff;
1899 if (row + j > wp->w_height)
1900 j = wp->w_height - row;
1901 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
1902 row += j;
1904 #endif
1906 else if (dollar_vcol == 0)
1907 wp->w_botline = lnum;
1909 /* make sure the rest of the screen is blank */
1910 /* put '~'s on rows that aren't part of the file. */
1911 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_AT);
1914 /* Reset the type of redrawing required, the window has been updated. */
1915 wp->w_redr_type = 0;
1916 #ifdef FEAT_DIFF
1917 wp->w_old_topfill = wp->w_topfill;
1918 wp->w_old_botfill = wp->w_botfill;
1919 #endif
1921 if (dollar_vcol == 0)
1924 * There is a trick with w_botline. If we invalidate it on each
1925 * change that might modify it, this will cause a lot of expensive
1926 * calls to plines() in update_topline() each time. Therefore the
1927 * value of w_botline is often approximated, and this value is used to
1928 * compute the value of w_topline. If the value of w_botline was
1929 * wrong, check that the value of w_topline is correct (cursor is on
1930 * the visible part of the text). If it's not, we need to redraw
1931 * again. Mostly this just means scrolling up a few lines, so it
1932 * doesn't look too bad. Only do this for the current window (where
1933 * changes are relevant).
1935 wp->w_valid |= VALID_BOTLINE;
1936 if (wp == curwin && wp->w_botline != old_botline && !recursive)
1938 recursive = TRUE;
1939 curwin->w_valid &= ~VALID_TOPLINE;
1940 update_topline(); /* may invalidate w_botline again */
1941 if (must_redraw != 0)
1943 /* Don't update for changes in buffer again. */
1944 i = curbuf->b_mod_set;
1945 curbuf->b_mod_set = FALSE;
1946 win_update(curwin);
1947 must_redraw = 0;
1948 curbuf->b_mod_set = i;
1950 recursive = FALSE;
1954 #if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1955 /* restore got_int, unless CTRL-C was hit while redrawing */
1956 if (!got_int)
1957 got_int = save_got_int;
1958 #endif
1961 #ifdef FEAT_SIGNS
1962 static int draw_signcolumn __ARGS((win_T *wp));
1965 * Return TRUE when window "wp" has a column to draw signs in.
1967 static int
1968 draw_signcolumn(wp)
1969 win_T *wp;
1971 return (wp->w_buffer->b_signlist != NULL
1972 # ifdef FEAT_NETBEANS_INTG
1973 || usingNetbeans
1974 # endif
1977 #endif
1980 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
1981 * as the filler character.
1983 static void
1984 win_draw_end(wp, c1, c2, row, endrow, hl)
1985 win_T *wp;
1986 int c1;
1987 int c2;
1988 int row;
1989 int endrow;
1990 hlf_T hl;
1992 #if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
1993 int n = 0;
1994 # define FDC_OFF n
1995 #else
1996 # define FDC_OFF 0
1997 #endif
1999 #ifdef FEAT_RIGHTLEFT
2000 if (wp->w_p_rl)
2002 /* No check for cmdline window: should never be right-left. */
2003 # ifdef FEAT_FOLDING
2004 n = wp->w_p_fdc;
2006 if (n > 0)
2008 /* draw the fold column at the right */
2009 if (n > W_WIDTH(wp))
2010 n = W_WIDTH(wp);
2011 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2012 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
2013 ' ', ' ', hl_attr(HLF_FC));
2015 # endif
2016 # ifdef FEAT_SIGNS
2017 if (draw_signcolumn(wp))
2019 int nn = n + 2;
2021 /* draw the sign column left of the fold column */
2022 if (nn > W_WIDTH(wp))
2023 nn = W_WIDTH(wp);
2024 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2025 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
2026 ' ', ' ', hl_attr(HLF_SC));
2027 n = nn;
2029 # endif
2030 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2031 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
2032 c2, c2, hl_attr(hl));
2033 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2034 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
2035 c1, c2, hl_attr(hl));
2037 else
2038 #endif
2040 #ifdef FEAT_CMDWIN
2041 if (cmdwin_type != 0 && wp == curwin)
2043 /* draw the cmdline character in the leftmost column */
2044 n = 1;
2045 if (n > wp->w_width)
2046 n = wp->w_width;
2047 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2048 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
2049 cmdwin_type, ' ', hl_attr(HLF_AT));
2051 #endif
2052 #ifdef FEAT_FOLDING
2053 if (wp->w_p_fdc > 0)
2055 int nn = n + wp->w_p_fdc;
2057 /* draw the fold column at the left */
2058 if (nn > W_WIDTH(wp))
2059 nn = W_WIDTH(wp);
2060 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2061 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2062 ' ', ' ', hl_attr(HLF_FC));
2063 n = nn;
2065 #endif
2066 #ifdef FEAT_SIGNS
2067 if (draw_signcolumn(wp))
2069 int nn = n + 2;
2071 /* draw the sign column after the fold column */
2072 if (nn > W_WIDTH(wp))
2073 nn = W_WIDTH(wp);
2074 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2075 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2076 ' ', ' ', hl_attr(HLF_SC));
2077 n = nn;
2079 #endif
2080 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2081 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
2082 c1, c2, hl_attr(hl));
2084 set_empty_rows(wp, row);
2087 #ifdef FEAT_FOLDING
2089 * Display one folded line.
2091 static void
2092 fold_line(wp, fold_count, foldinfo, lnum, row)
2093 win_T *wp;
2094 long fold_count;
2095 foldinfo_T *foldinfo;
2096 linenr_T lnum;
2097 int row;
2099 char_u buf[51];
2100 pos_T *top, *bot;
2101 linenr_T lnume = lnum + fold_count - 1;
2102 int len;
2103 char_u *text;
2104 int fdc;
2105 int col;
2106 int txtcol;
2107 int off = (int)(current_ScreenLine - ScreenLines);
2108 int ri;
2110 /* Build the fold line:
2111 * 1. Add the cmdwin_type for the command-line window
2112 * 2. Add the 'foldcolumn'
2113 * 3. Add the 'number' or 'relativenumber' column
2114 * 4. Compose the text
2115 * 5. Add the text
2116 * 6. set highlighting for the Visual area an other text
2118 col = 0;
2121 * 1. Add the cmdwin_type for the command-line window
2122 * Ignores 'rightleft', this window is never right-left.
2124 #ifdef FEAT_CMDWIN
2125 if (cmdwin_type != 0 && wp == curwin)
2127 ScreenLines[off] = cmdwin_type;
2128 ScreenAttrs[off] = hl_attr(HLF_AT);
2129 #ifdef FEAT_MBYTE
2130 if (enc_utf8)
2131 ScreenLinesUC[off] = 0;
2132 #endif
2133 ++col;
2135 #endif
2138 * 2. Add the 'foldcolumn'
2140 fdc = wp->w_p_fdc;
2141 if (fdc > W_WIDTH(wp) - col)
2142 fdc = W_WIDTH(wp) - col;
2143 if (fdc > 0)
2145 fill_foldcolumn(buf, wp, TRUE, lnum);
2146 #ifdef FEAT_RIGHTLEFT
2147 if (wp->w_p_rl)
2149 int i;
2151 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
2152 hl_attr(HLF_FC));
2153 /* reverse the fold column */
2154 for (i = 0; i < fdc; ++i)
2155 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2157 else
2158 #endif
2159 copy_text_attr(off + col, buf, fdc, hl_attr(HLF_FC));
2160 col += fdc;
2163 #ifdef FEAT_RIGHTLEFT
2164 # define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2165 for (ri = 0; ri < l; ++ri) \
2166 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2167 else \
2168 for (ri = 0; ri < l; ++ri) \
2169 ScreenAttrs[off + (p) + ri] = v
2170 #else
2171 # define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2172 ScreenAttrs[off + (p) + ri] = v
2173 #endif
2175 /* Set all attributes of the 'number' or 'relativenumber' column and the
2176 * text */
2177 RL_MEMSET(col, hl_attr(HLF_FL), W_WIDTH(wp) - col);
2179 #ifdef FEAT_SIGNS
2180 /* If signs are being displayed, add two spaces. */
2181 if (draw_signcolumn(wp))
2183 len = W_WIDTH(wp) - col;
2184 if (len > 0)
2186 if (len > 2)
2187 len = 2;
2188 # ifdef FEAT_RIGHTLEFT
2189 if (wp->w_p_rl)
2190 /* the line number isn't reversed */
2191 copy_text_attr(off + W_WIDTH(wp) - len - col,
2192 (char_u *)" ", len, hl_attr(HLF_FL));
2193 else
2194 # endif
2195 copy_text_attr(off + col, (char_u *)" ", len, hl_attr(HLF_FL));
2196 col += len;
2199 #endif
2202 * 3. Add the 'number' or 'relativenumber' column
2204 if (wp->w_p_nu || wp->w_p_rnu)
2206 len = W_WIDTH(wp) - col;
2207 if (len > 0)
2209 int w = number_width(wp);
2210 long num;
2212 if (len > w + 1)
2213 len = w + 1;
2215 if (wp->w_p_nu)
2216 /* 'number' */
2217 num = (long)lnum;
2218 else
2219 /* 'relativenumber', don't use negative numbers */
2220 num = (long)abs((int)get_cursor_rel_lnum(wp, lnum));
2222 sprintf((char *)buf, "%*ld ", w, num);
2223 #ifdef FEAT_RIGHTLEFT
2224 if (wp->w_p_rl)
2225 /* the line number isn't reversed */
2226 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
2227 hl_attr(HLF_FL));
2228 else
2229 #endif
2230 copy_text_attr(off + col, buf, len, hl_attr(HLF_FL));
2231 col += len;
2236 * 4. Compose the folded-line string with 'foldtext', if set.
2238 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
2240 txtcol = col; /* remember where text starts */
2243 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2244 * Right-left text is put in columns 0 - number-col, normal text is put
2245 * in columns number-col - window-width.
2247 #ifdef FEAT_MBYTE
2248 if (has_mbyte)
2250 int cells;
2251 int u8c, u8cc[MAX_MCO];
2252 int i;
2253 int idx;
2254 int c_len;
2255 char_u *p;
2256 # ifdef FEAT_ARABIC
2257 int prev_c = 0; /* previous Arabic character */
2258 int prev_c1 = 0; /* first composing char for prev_c */
2259 # endif
2261 # ifdef FEAT_RIGHTLEFT
2262 if (wp->w_p_rl)
2263 idx = off;
2264 else
2265 # endif
2266 idx = off + col;
2268 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2269 for (p = text; *p != NUL; )
2271 cells = (*mb_ptr2cells)(p);
2272 c_len = (*mb_ptr2len)(p);
2273 if (col + cells > W_WIDTH(wp)
2274 # ifdef FEAT_RIGHTLEFT
2275 - (wp->w_p_rl ? col : 0)
2276 # endif
2278 break;
2279 ScreenLines[idx] = *p;
2280 if (enc_utf8)
2282 u8c = utfc_ptr2char(p, u8cc);
2283 if (*p < 0x80 && u8cc[0] == 0)
2285 ScreenLinesUC[idx] = 0;
2286 #ifdef FEAT_ARABIC
2287 prev_c = u8c;
2288 #endif
2290 else
2292 #ifdef FEAT_ARABIC
2293 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2295 /* Do Arabic shaping. */
2296 int pc, pc1, nc;
2297 int pcc[MAX_MCO];
2298 int firstbyte = *p;
2300 /* The idea of what is the previous and next
2301 * character depends on 'rightleft'. */
2302 if (wp->w_p_rl)
2304 pc = prev_c;
2305 pc1 = prev_c1;
2306 nc = utf_ptr2char(p + c_len);
2307 prev_c1 = u8cc[0];
2309 else
2311 pc = utfc_ptr2char(p + c_len, pcc);
2312 nc = prev_c;
2313 pc1 = pcc[0];
2315 prev_c = u8c;
2317 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2318 pc, pc1, nc);
2319 ScreenLines[idx] = firstbyte;
2321 else
2322 prev_c = u8c;
2323 #endif
2324 /* Non-BMP character: display as ? or fullwidth ?. */
2325 #ifdef UNICODE16
2326 if (u8c >= 0x10000)
2327 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2328 else
2329 #endif
2330 ScreenLinesUC[idx] = u8c;
2331 for (i = 0; i < Screen_mco; ++i)
2333 ScreenLinesC[i][idx] = u8cc[i];
2334 if (u8cc[i] == 0)
2335 break;
2338 if (cells > 1)
2339 ScreenLines[idx + 1] = 0;
2341 else if (cells > 1) /* double-byte character */
2343 if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2344 ScreenLines2[idx] = p[1];
2345 else
2346 ScreenLines[idx + 1] = p[1];
2348 col += cells;
2349 idx += cells;
2350 p += c_len;
2353 else
2354 #endif
2356 len = (int)STRLEN(text);
2357 if (len > W_WIDTH(wp) - col)
2358 len = W_WIDTH(wp) - col;
2359 if (len > 0)
2361 #ifdef FEAT_RIGHTLEFT
2362 if (wp->w_p_rl)
2363 STRNCPY(current_ScreenLine, text, len);
2364 else
2365 #endif
2366 STRNCPY(current_ScreenLine + col, text, len);
2367 col += len;
2371 /* Fill the rest of the line with the fold filler */
2372 #ifdef FEAT_RIGHTLEFT
2373 if (wp->w_p_rl)
2374 col -= txtcol;
2375 #endif
2376 while (col < W_WIDTH(wp)
2377 #ifdef FEAT_RIGHTLEFT
2378 - (wp->w_p_rl ? txtcol : 0)
2379 #endif
2382 #ifdef FEAT_MBYTE
2383 if (enc_utf8)
2385 if (fill_fold >= 0x80)
2387 ScreenLinesUC[off + col] = fill_fold;
2388 ScreenLinesC[0][off + col] = 0;
2390 else
2391 ScreenLinesUC[off + col] = 0;
2393 #endif
2394 ScreenLines[off + col++] = fill_fold;
2397 if (text != buf)
2398 vim_free(text);
2401 * 6. set highlighting for the Visual area an other text.
2402 * If all folded lines are in the Visual area, highlight the line.
2404 #ifdef FEAT_VISUAL
2405 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2407 if (ltoreq(curwin->w_cursor, VIsual))
2409 /* Visual is after curwin->w_cursor */
2410 top = &curwin->w_cursor;
2411 bot = &VIsual;
2413 else
2415 /* Visual is before curwin->w_cursor */
2416 top = &VIsual;
2417 bot = &curwin->w_cursor;
2419 if (lnum >= top->lnum
2420 && lnume <= bot->lnum
2421 && (VIsual_mode != 'v'
2422 || ((lnum > top->lnum
2423 || (lnum == top->lnum
2424 && top->col == 0))
2425 && (lnume < bot->lnum
2426 || (lnume == bot->lnum
2427 && (bot->col - (*p_sel == 'e'))
2428 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
2430 if (VIsual_mode == Ctrl_V)
2432 /* Visual block mode: highlight the chars part of the block */
2433 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2435 if (wp->w_old_cursor_lcol + txtcol < (colnr_T)W_WIDTH(wp))
2436 len = wp->w_old_cursor_lcol;
2437 else
2438 len = W_WIDTH(wp) - txtcol;
2439 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, hl_attr(HLF_V),
2440 len - (int)wp->w_old_cursor_fcol);
2443 else
2445 /* Set all attributes of the text */
2446 RL_MEMSET(txtcol, hl_attr(HLF_V), W_WIDTH(wp) - txtcol);
2450 #endif
2452 #ifdef FEAT_SYN_HL
2453 /* Show 'cursorcolumn' in the fold line. */
2454 if (wp->w_p_cuc)
2456 txtcol += wp->w_virtcol;
2457 if (wp->w_p_wrap)
2458 txtcol -= wp->w_skipcol;
2459 else
2460 txtcol -= wp->w_leftcol;
2461 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2462 ScreenAttrs[off + txtcol] = hl_combine_attr(
2463 ScreenAttrs[off + txtcol], hl_attr(HLF_CUC));
2465 #endif
2467 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2468 (int)W_WIDTH(wp), FALSE);
2471 * Update w_cline_height and w_cline_folded if the cursor line was
2472 * updated (saves a call to plines() later).
2474 if (wp == curwin
2475 && lnum <= curwin->w_cursor.lnum
2476 && lnume >= curwin->w_cursor.lnum)
2478 curwin->w_cline_row = row;
2479 curwin->w_cline_height = 1;
2480 curwin->w_cline_folded = TRUE;
2481 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2486 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2488 static void
2489 copy_text_attr(off, buf, len, attr)
2490 int off;
2491 char_u *buf;
2492 int len;
2493 int attr;
2495 int i;
2497 mch_memmove(ScreenLines + off, buf, (size_t)len);
2498 # ifdef FEAT_MBYTE
2499 if (enc_utf8)
2500 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2501 # endif
2502 for (i = 0; i < len; ++i)
2503 ScreenAttrs[off + i] = attr;
2507 * Fill the foldcolumn at "p" for window "wp".
2508 * Only to be called when 'foldcolumn' > 0.
2510 static void
2511 fill_foldcolumn(p, wp, closed, lnum)
2512 char_u *p;
2513 win_T *wp;
2514 int closed; /* TRUE of FALSE */
2515 linenr_T lnum; /* current line number */
2517 int i = 0;
2518 int level;
2519 int first_level;
2520 int empty;
2522 /* Init to all spaces. */
2523 copy_spaces(p, (size_t)wp->w_p_fdc);
2525 level = win_foldinfo.fi_level;
2526 if (level > 0)
2528 /* If there is only one column put more info in it. */
2529 empty = (wp->w_p_fdc == 1) ? 0 : 1;
2531 /* If the column is too narrow, we start at the lowest level that
2532 * fits and use numbers to indicated the depth. */
2533 first_level = level - wp->w_p_fdc - closed + 1 + empty;
2534 if (first_level < 1)
2535 first_level = 1;
2537 for (i = 0; i + empty < wp->w_p_fdc; ++i)
2539 if (win_foldinfo.fi_lnum == lnum
2540 && first_level + i >= win_foldinfo.fi_low_level)
2541 p[i] = '-';
2542 else if (first_level == 1)
2543 p[i] = '|';
2544 else if (first_level + i <= 9)
2545 p[i] = '0' + first_level + i;
2546 else
2547 p[i] = '>';
2548 if (first_level + i == level)
2549 break;
2552 if (closed)
2553 p[i >= wp->w_p_fdc ? i - 1 : i] = '+';
2555 #endif /* FEAT_FOLDING */
2558 * Display line "lnum" of window 'wp' on the screen.
2559 * Start at row "startrow", stop when "endrow" is reached.
2560 * wp->w_virtcol needs to be valid.
2562 * Return the number of last row the line occupies.
2564 static int
2565 win_line(wp, lnum, startrow, endrow, nochange)
2566 win_T *wp;
2567 linenr_T lnum;
2568 int startrow;
2569 int endrow;
2570 int nochange UNUSED; /* not updating for changed text */
2572 int col; /* visual column on screen */
2573 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2574 int c = 0; /* init for GCC */
2575 long vcol = 0; /* virtual column (for tabs) */
2576 long vcol_prev = -1; /* "vcol" of previous character */
2577 char_u *line; /* current line */
2578 char_u *ptr; /* current position in "line" */
2579 int row; /* row in the window, excl w_winrow */
2580 int screen_row; /* row on the screen, incl w_winrow */
2582 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2583 int n_extra = 0; /* number of extra chars */
2584 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
2585 int c_extra = NUL; /* extra chars, all the same */
2586 int extra_attr = 0; /* attributes when n_extra != 0 */
2587 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2588 displaying lcs_eol at end-of-line */
2589 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2590 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2592 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2593 int saved_n_extra = 0;
2594 char_u *saved_p_extra = NULL;
2595 int saved_c_extra = 0;
2596 int saved_char_attr = 0;
2598 int n_attr = 0; /* chars with special attr */
2599 int saved_attr2 = 0; /* char_attr saved for n_attr */
2600 int n_attr3 = 0; /* chars with overruling special attr */
2601 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2603 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2605 int fromcol, tocol; /* start/end of inverting */
2606 int fromcol_prev = -2; /* start of inverting after cursor */
2607 int noinvcur = FALSE; /* don't invert the cursor */
2608 #ifdef FEAT_VISUAL
2609 pos_T *top, *bot;
2610 int lnum_in_visual_area = FALSE;
2611 #endif
2612 pos_T pos;
2613 long v;
2615 int char_attr = 0; /* attributes for next character */
2616 int attr_pri = FALSE; /* char_attr has priority */
2617 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2618 in this line */
2619 int attr = 0; /* attributes for area highlighting */
2620 int area_attr = 0; /* attributes desired by highlighting */
2621 int search_attr = 0; /* attributes desired by 'hlsearch' */
2622 #ifdef FEAT_SYN_HL
2623 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
2624 int syntax_attr = 0; /* attributes desired by syntax */
2625 int has_syntax = FALSE; /* this buffer has syntax highl. */
2626 int save_did_emsg;
2627 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
2628 #endif
2629 #ifdef FEAT_SPELL
2630 int has_spell = FALSE; /* this buffer has spell checking */
2631 # define SPWORDLEN 150
2632 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
2633 int nextlinecol = 0; /* column where nextline[] starts */
2634 int nextline_idx = 0; /* index in nextline[] where next line
2635 starts */
2636 int spell_attr = 0; /* attributes desired by spelling */
2637 int word_end = 0; /* last byte with same spell_attr */
2638 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2639 static int checked_col = 0; /* column in "checked_lnum" up to which
2640 * there are no spell errors */
2641 static int cap_col = -1; /* column to check for Cap word */
2642 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
2643 int cur_checked_col = 0; /* checked column for current line */
2644 #endif
2645 int extra_check; /* has syntax or linebreak */
2646 #ifdef FEAT_MBYTE
2647 int multi_attr = 0; /* attributes desired by multibyte */
2648 int mb_l = 1; /* multi-byte byte length */
2649 int mb_c = 0; /* decoded multi-byte character */
2650 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
2651 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
2652 #endif
2653 #ifdef FEAT_DIFF
2654 int filler_lines; /* nr of filler lines to be drawn */
2655 int filler_todo; /* nr of filler lines still to do + 1 */
2656 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
2657 int change_start = MAXCOL; /* first col of changed area */
2658 int change_end = -1; /* last col of changed area */
2659 #endif
2660 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
2661 #ifdef FEAT_LINEBREAK
2662 int need_showbreak = FALSE;
2663 #endif
2664 #if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
2665 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
2666 # define LINE_ATTR
2667 int line_attr = 0; /* attribute for the whole line */
2668 #endif
2669 #ifdef FEAT_SEARCH_EXTRA
2670 matchitem_T *cur; /* points to the match list */
2671 match_T *shl; /* points to search_hl or a match */
2672 int shl_flag; /* flag to indicate whether search_hl
2673 has been processed or not */
2674 int prevcol_hl_flag; /* flag to indicate whether prevcol
2675 equals startcol of search_hl or one
2676 of the matches */
2677 #endif
2678 #ifdef FEAT_ARABIC
2679 int prev_c = 0; /* previous Arabic character */
2680 int prev_c1 = 0; /* first composing char for prev_c */
2681 #endif
2682 #if defined(LINE_ATTR)
2683 int did_line_attr = 0;
2684 #endif
2686 /* draw_state: items that are drawn in sequence: */
2687 #define WL_START 0 /* nothing done yet */
2688 #ifdef FEAT_CMDWIN
2689 # define WL_CMDLINE WL_START + 1 /* cmdline window column */
2690 #else
2691 # define WL_CMDLINE WL_START
2692 #endif
2693 #ifdef FEAT_FOLDING
2694 # define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
2695 #else
2696 # define WL_FOLD WL_CMDLINE
2697 #endif
2698 #ifdef FEAT_SIGNS
2699 # define WL_SIGN WL_FOLD + 1 /* column for signs */
2700 #else
2701 # define WL_SIGN WL_FOLD /* column for signs */
2702 #endif
2703 #define WL_NR WL_SIGN + 1 /* line number */
2704 #if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
2705 # define WL_SBR WL_NR + 1 /* 'showbreak' or 'diff' */
2706 #else
2707 # define WL_SBR WL_NR
2708 #endif
2709 #define WL_LINE WL_SBR + 1 /* text in the line */
2710 int draw_state = WL_START; /* what to draw next */
2711 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2712 int feedback_col = 0;
2713 int feedback_old_attr = -1;
2714 #endif
2717 if (startrow > endrow) /* past the end already! */
2718 return startrow;
2720 row = startrow;
2721 screen_row = row + W_WINROW(wp);
2724 * To speed up the loop below, set extra_check when there is linebreak,
2725 * trailing white space and/or syntax processing to be done.
2727 #ifdef FEAT_LINEBREAK
2728 extra_check = wp->w_p_lbr;
2729 #else
2730 extra_check = 0;
2731 #endif
2732 #ifdef FEAT_SYN_HL
2733 if (syntax_present(wp->w_buffer) && !wp->w_buffer->b_syn_error)
2735 /* Prepare for syntax highlighting in this line. When there is an
2736 * error, stop syntax highlighting. */
2737 save_did_emsg = did_emsg;
2738 did_emsg = FALSE;
2739 syntax_start(wp, lnum);
2740 if (did_emsg)
2741 wp->w_buffer->b_syn_error = TRUE;
2742 else
2744 did_emsg = save_did_emsg;
2745 has_syntax = TRUE;
2746 extra_check = TRUE;
2749 #endif
2751 #ifdef FEAT_SPELL
2752 if (wp->w_p_spell
2753 && *wp->w_buffer->b_p_spl != NUL
2754 && wp->w_buffer->b_langp.ga_len > 0
2755 && *(char **)(wp->w_buffer->b_langp.ga_data) != NULL)
2757 /* Prepare for spell checking. */
2758 has_spell = TRUE;
2759 extra_check = TRUE;
2761 /* Get the start of the next line, so that words that wrap to the next
2762 * line are found too: "et<line-break>al.".
2763 * Trick: skip a few chars for C/shell/Vim comments */
2764 nextline[SPWORDLEN] = NUL;
2765 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2767 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
2768 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
2771 /* When a word wrapped from the previous line the start of the current
2772 * line is valid. */
2773 if (lnum == checked_lnum)
2774 cur_checked_col = checked_col;
2775 checked_lnum = 0;
2777 /* When there was a sentence end in the previous line may require a
2778 * word starting with capital in this line. In line 1 always check
2779 * the first word. */
2780 if (lnum != capcol_lnum)
2781 cap_col = -1;
2782 if (lnum == 1)
2783 cap_col = 0;
2784 capcol_lnum = 0;
2786 #endif
2789 * handle visual active in this window
2791 fromcol = -10;
2792 tocol = MAXCOL;
2793 #ifdef FEAT_VISUAL
2794 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2796 /* Visual is after curwin->w_cursor */
2797 if (ltoreq(curwin->w_cursor, VIsual))
2799 top = &curwin->w_cursor;
2800 bot = &VIsual;
2802 else /* Visual is before curwin->w_cursor */
2804 top = &VIsual;
2805 bot = &curwin->w_cursor;
2807 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
2808 if (VIsual_mode == Ctrl_V) /* block mode */
2810 if (lnum_in_visual_area)
2812 fromcol = wp->w_old_cursor_fcol;
2813 tocol = wp->w_old_cursor_lcol;
2816 else /* non-block mode */
2818 if (lnum > top->lnum && lnum <= bot->lnum)
2819 fromcol = 0;
2820 else if (lnum == top->lnum)
2822 if (VIsual_mode == 'V') /* linewise */
2823 fromcol = 0;
2824 else
2826 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
2827 if (gchar_pos(top) == NUL)
2828 tocol = fromcol + 1;
2831 if (VIsual_mode != 'V' && lnum == bot->lnum)
2833 if (*p_sel == 'e' && bot->col == 0
2834 #ifdef FEAT_VIRTUALEDIT
2835 && bot->coladd == 0
2836 #endif
2839 fromcol = -10;
2840 tocol = MAXCOL;
2842 else if (bot->col == MAXCOL)
2843 tocol = MAXCOL;
2844 else
2846 pos = *bot;
2847 if (*p_sel == 'e')
2848 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
2849 else
2851 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
2852 ++tocol;
2858 #ifndef MSDOS
2859 /* Check if the character under the cursor should not be inverted */
2860 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
2861 # ifdef FEAT_GUI
2862 && !gui.in_use
2863 # endif
2865 noinvcur = TRUE;
2866 #endif
2868 /* if inverting in this line set area_highlighting */
2869 if (fromcol >= 0)
2871 area_highlighting = TRUE;
2872 attr = hl_attr(HLF_V);
2873 #if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
2874 if (clip_star.available && !clip_star.owned && clip_isautosel())
2875 attr = hl_attr(HLF_VNC);
2876 #endif
2881 * handle 'incsearch' and ":s///c" highlighting
2883 else
2884 #endif /* FEAT_VISUAL */
2885 if (highlight_match
2886 && wp == curwin
2887 && lnum >= curwin->w_cursor.lnum
2888 && lnum <= curwin->w_cursor.lnum + search_match_lines)
2890 if (lnum == curwin->w_cursor.lnum)
2891 getvcol(curwin, &(curwin->w_cursor),
2892 (colnr_T *)&fromcol, NULL, NULL);
2893 else
2894 fromcol = 0;
2895 if (lnum == curwin->w_cursor.lnum + search_match_lines)
2897 pos.lnum = lnum;
2898 pos.col = search_match_endcol;
2899 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
2901 else
2902 tocol = MAXCOL;
2903 /* do at least one character; happens when past end of line */
2904 if (fromcol == tocol)
2905 tocol = fromcol + 1;
2906 area_highlighting = TRUE;
2907 attr = hl_attr(HLF_I);
2910 #ifdef FEAT_DIFF
2911 filler_lines = diff_check(wp, lnum);
2912 if (filler_lines < 0)
2914 if (filler_lines == -1)
2916 if (diff_find_change(wp, lnum, &change_start, &change_end))
2917 diff_hlf = HLF_ADD; /* added line */
2918 else if (change_start == 0)
2919 diff_hlf = HLF_TXD; /* changed text */
2920 else
2921 diff_hlf = HLF_CHD; /* changed line */
2923 else
2924 diff_hlf = HLF_ADD; /* added line */
2925 filler_lines = 0;
2926 area_highlighting = TRUE;
2928 if (lnum == wp->w_topline)
2929 filler_lines = wp->w_topfill;
2930 filler_todo = filler_lines;
2931 #endif
2933 #ifdef LINE_ATTR
2934 # ifdef FEAT_SIGNS
2935 /* If this line has a sign with line highlighting set line_attr. */
2936 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
2937 if (v != 0)
2938 line_attr = sign_get_attr((int)v, TRUE);
2939 # endif
2940 # if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
2941 /* Highlight the current line in the quickfix window. */
2942 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
2943 line_attr = hl_attr(HLF_L);
2944 # endif
2945 if (line_attr != 0)
2946 area_highlighting = TRUE;
2947 #endif
2949 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
2950 ptr = line;
2952 #ifdef FEAT_SPELL
2953 if (has_spell)
2955 /* For checking first word with a capital skip white space. */
2956 if (cap_col == 0)
2957 cap_col = (int)(skipwhite(line) - line);
2959 /* To be able to spell-check over line boundaries copy the end of the
2960 * current line into nextline[]. Above the start of the next line was
2961 * copied to nextline[SPWORDLEN]. */
2962 if (nextline[SPWORDLEN] == NUL)
2964 /* No next line or it is empty. */
2965 nextlinecol = MAXCOL;
2966 nextline_idx = 0;
2968 else
2970 v = (long)STRLEN(line);
2971 if (v < SPWORDLEN)
2973 /* Short line, use it completely and append the start of the
2974 * next line. */
2975 nextlinecol = 0;
2976 mch_memmove(nextline, line, (size_t)v);
2977 STRMOVE(nextline + v, nextline + SPWORDLEN);
2978 nextline_idx = v + 1;
2980 else
2982 /* Long line, use only the last SPWORDLEN bytes. */
2983 nextlinecol = v - SPWORDLEN;
2984 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
2985 nextline_idx = SPWORDLEN + 1;
2989 #endif
2991 /* find start of trailing whitespace */
2992 if (wp->w_p_list && lcs_trail)
2994 trailcol = (colnr_T)STRLEN(ptr);
2995 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
2996 --trailcol;
2997 trailcol += (colnr_T) (ptr - line);
2998 extra_check = TRUE;
3002 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3003 * first character to be displayed.
3005 if (wp->w_p_wrap)
3006 v = wp->w_skipcol;
3007 else
3008 v = wp->w_leftcol;
3009 if (v > 0)
3011 #ifdef FEAT_MBYTE
3012 char_u *prev_ptr = ptr;
3013 #endif
3014 while (vcol < v && *ptr != NUL)
3016 c = win_lbr_chartabsize(wp, ptr, (colnr_T)vcol, NULL);
3017 vcol += c;
3018 #ifdef FEAT_MBYTE
3019 prev_ptr = ptr;
3020 #endif
3021 mb_ptr_adv(ptr);
3024 #ifdef FEAT_VIRTUALEDIT
3025 /* When 'virtualedit' is set the end of the line may be before the
3026 * start of the displayed part. */
3027 if (vcol < v && *ptr == NUL && virtual_active())
3028 vcol = v;
3029 #endif
3031 /* Handle a character that's not completely on the screen: Put ptr at
3032 * that character but skip the first few screen characters. */
3033 if (vcol > v)
3035 vcol -= c;
3036 #ifdef FEAT_MBYTE
3037 ptr = prev_ptr;
3038 #else
3039 --ptr;
3040 #endif
3041 n_skip = v - vcol;
3045 * Adjust for when the inverted text is before the screen,
3046 * and when the start of the inverted text is before the screen.
3048 if (tocol <= vcol)
3049 fromcol = 0;
3050 else if (fromcol >= 0 && fromcol < vcol)
3051 fromcol = vcol;
3053 #ifdef FEAT_LINEBREAK
3054 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3055 if (wp->w_p_wrap)
3056 need_showbreak = TRUE;
3057 #endif
3058 #ifdef FEAT_SPELL
3059 /* When spell checking a word we need to figure out the start of the
3060 * word and if it's badly spelled or not. */
3061 if (has_spell)
3063 int len;
3064 colnr_T linecol = (colnr_T)(ptr - line);
3065 hlf_T spell_hlf = HLF_COUNT;
3067 pos = wp->w_cursor;
3068 wp->w_cursor.lnum = lnum;
3069 wp->w_cursor.col = linecol;
3070 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
3072 /* spell_move_to() may call ml_get() and make "line" invalid */
3073 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3074 ptr = line + linecol;
3076 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
3078 /* no bad word found at line start, don't check until end of a
3079 * word */
3080 spell_hlf = HLF_COUNT;
3081 word_end = (int)(spell_to_word_end(ptr, wp->w_buffer)
3082 - line + 1);
3084 else
3086 /* bad word found, use attributes until end of word */
3087 word_end = wp->w_cursor.col + len + 1;
3089 /* Turn index into actual attributes. */
3090 if (spell_hlf != HLF_COUNT)
3091 spell_attr = highlight_attr[spell_hlf];
3093 wp->w_cursor = pos;
3095 # ifdef FEAT_SYN_HL
3096 /* Need to restart syntax highlighting for this line. */
3097 if (has_syntax)
3098 syntax_start(wp, lnum);
3099 # endif
3101 #endif
3105 * Correct highlighting for cursor that can't be disabled.
3106 * Avoids having to check this for each character.
3108 if (fromcol >= 0)
3110 if (noinvcur)
3112 if ((colnr_T)fromcol == wp->w_virtcol)
3114 /* highlighting starts at cursor, let it start just after the
3115 * cursor */
3116 fromcol_prev = fromcol;
3117 fromcol = -1;
3119 else if ((colnr_T)fromcol < wp->w_virtcol)
3120 /* restart highlighting after the cursor */
3121 fromcol_prev = wp->w_virtcol;
3123 if (fromcol >= tocol)
3124 fromcol = -1;
3127 #ifdef FEAT_SEARCH_EXTRA
3129 * Handle highlighting the last used search pattern and matches.
3130 * Do this for both search_hl and the match list.
3132 cur = wp->w_match_head;
3133 shl_flag = FALSE;
3134 while (cur != NULL || shl_flag == FALSE)
3136 if (shl_flag == FALSE)
3138 shl = &search_hl;
3139 shl_flag = TRUE;
3141 else
3142 shl = &cur->hl;
3143 shl->startcol = MAXCOL;
3144 shl->endcol = MAXCOL;
3145 shl->attr_cur = 0;
3146 if (shl->rm.regprog != NULL)
3148 v = (long)(ptr - line);
3149 next_search_hl(wp, shl, lnum, (colnr_T)v);
3151 /* Need to get the line again, a multi-line regexp may have made it
3152 * invalid. */
3153 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3154 ptr = line + v;
3156 if (shl->lnum != 0 && shl->lnum <= lnum)
3158 if (shl->lnum == lnum)
3159 shl->startcol = shl->rm.startpos[0].col;
3160 else
3161 shl->startcol = 0;
3162 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3163 - shl->rm.startpos[0].lnum)
3164 shl->endcol = shl->rm.endpos[0].col;
3165 else
3166 shl->endcol = MAXCOL;
3167 /* Highlight one character for an empty match. */
3168 if (shl->startcol == shl->endcol)
3170 #ifdef FEAT_MBYTE
3171 if (has_mbyte && line[shl->endcol] != NUL)
3172 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3173 else
3174 #endif
3175 ++shl->endcol;
3177 if ((long)shl->startcol < v) /* match at leftcol */
3179 shl->attr_cur = shl->attr;
3180 search_attr = shl->attr;
3182 area_highlighting = TRUE;
3185 if (shl != &search_hl && cur != NULL)
3186 cur = cur->next;
3188 #endif
3190 #ifdef FEAT_SYN_HL
3191 /* Cursor line highlighting for 'cursorline'. Not when Visual mode is
3192 * active, because it's not clear what is selected then. */
3193 if (wp->w_p_cul && lnum == wp->w_cursor.lnum && !VIsual_active)
3195 line_attr = hl_attr(HLF_CUL);
3196 area_highlighting = TRUE;
3198 #endif
3200 off = (unsigned)(current_ScreenLine - ScreenLines);
3201 col = 0;
3202 #ifdef FEAT_RIGHTLEFT
3203 if (wp->w_p_rl)
3205 /* Rightleft window: process the text in the normal direction, but put
3206 * it in current_ScreenLine[] from right to left. Start at the
3207 * rightmost column of the window. */
3208 col = W_WIDTH(wp) - 1;
3209 off += col;
3211 #endif
3214 * Repeat for the whole displayed line.
3216 for (;;)
3218 /* Skip this quickly when working on the text. */
3219 if (draw_state != WL_LINE)
3221 #ifdef FEAT_CMDWIN
3222 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3224 draw_state = WL_CMDLINE;
3225 if (cmdwin_type != 0 && wp == curwin)
3227 /* Draw the cmdline character. */
3228 n_extra = 1;
3229 c_extra = cmdwin_type;
3230 char_attr = hl_attr(HLF_AT);
3233 #endif
3235 #ifdef FEAT_FOLDING
3236 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3238 draw_state = WL_FOLD;
3239 if (wp->w_p_fdc > 0)
3241 /* Draw the 'foldcolumn'. */
3242 fill_foldcolumn(extra, wp, FALSE, lnum);
3243 n_extra = wp->w_p_fdc;
3244 p_extra = extra;
3245 p_extra[n_extra] = NUL;
3246 c_extra = NUL;
3247 char_attr = hl_attr(HLF_FC);
3250 #endif
3252 #ifdef FEAT_SIGNS
3253 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3255 draw_state = WL_SIGN;
3256 /* Show the sign column when there are any signs in this
3257 * buffer or when using Netbeans. */
3258 if (draw_signcolumn(wp)
3259 # ifdef FEAT_DIFF
3260 && filler_todo <= 0
3261 # endif
3264 int_u text_sign;
3265 # ifdef FEAT_SIGN_ICONS
3266 int_u icon_sign;
3267 # endif
3269 /* Draw two cells with the sign value or blank. */
3270 c_extra = ' ';
3271 char_attr = hl_attr(HLF_SC);
3272 n_extra = 2;
3274 if (row == startrow)
3276 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3277 SIGN_TEXT);
3278 # ifdef FEAT_SIGN_ICONS
3279 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3280 SIGN_ICON);
3281 if (gui.in_use && icon_sign != 0)
3283 /* Use the image in this position. */
3284 c_extra = SIGN_BYTE;
3285 # ifdef FEAT_NETBEANS_INTG
3286 if (buf_signcount(wp->w_buffer, lnum) > 1)
3287 c_extra = MULTISIGN_BYTE;
3288 # endif
3289 char_attr = icon_sign;
3291 else
3292 # endif
3293 if (text_sign != 0)
3295 p_extra = sign_get_text(text_sign);
3296 if (p_extra != NULL)
3298 c_extra = NUL;
3299 n_extra = (int)STRLEN(p_extra);
3301 char_attr = sign_get_attr(text_sign, FALSE);
3306 #endif
3308 if (draw_state == WL_NR - 1 && n_extra == 0)
3310 draw_state = WL_NR;
3311 /* Display the absolute or relative line number. After the
3312 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3313 if ((wp->w_p_nu || wp->w_p_rnu)
3314 && (row == startrow
3315 #ifdef FEAT_DIFF
3316 + filler_lines
3317 #endif
3318 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3320 /* Draw the line number (empty space after wrapping). */
3321 if (row == startrow
3322 #ifdef FEAT_DIFF
3323 + filler_lines
3324 #endif
3327 long num;
3329 if (wp->w_p_nu)
3330 /* 'number' */
3331 num = (long)lnum;
3332 else
3333 /* 'relativenumber', don't use negative numbers */
3334 num = (long)abs((int)get_cursor_rel_lnum(wp,
3335 lnum));
3337 sprintf((char *)extra, "%*ld ",
3338 number_width(wp), num);
3339 if (wp->w_skipcol > 0)
3340 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3341 *p_extra = '-';
3342 #ifdef FEAT_RIGHTLEFT
3343 if (wp->w_p_rl) /* reverse line numbers */
3344 rl_mirror(extra);
3345 #endif
3346 p_extra = extra;
3347 c_extra = NUL;
3349 else
3350 c_extra = ' ';
3351 n_extra = number_width(wp) + 1;
3352 char_attr = hl_attr(HLF_N);
3353 #ifdef FEAT_SYN_HL
3354 /* When 'cursorline' is set highlight the line number of
3355 * the current line differently. */
3356 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3357 char_attr = hl_combine_attr(hl_attr(HLF_CUL), char_attr);
3358 #endif
3362 #if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3363 if (draw_state == WL_SBR - 1 && n_extra == 0)
3365 draw_state = WL_SBR;
3366 # ifdef FEAT_DIFF
3367 if (filler_todo > 0)
3369 /* Draw "deleted" diff line(s). */
3370 if (char2cells(fill_diff) > 1)
3371 c_extra = '-';
3372 else
3373 c_extra = fill_diff;
3374 # ifdef FEAT_RIGHTLEFT
3375 if (wp->w_p_rl)
3376 n_extra = col + 1;
3377 else
3378 # endif
3379 n_extra = W_WIDTH(wp) - col;
3380 char_attr = hl_attr(HLF_DED);
3382 # endif
3383 # ifdef FEAT_LINEBREAK
3384 if (*p_sbr != NUL && need_showbreak)
3386 /* Draw 'showbreak' at the start of each broken line. */
3387 p_extra = p_sbr;
3388 c_extra = NUL;
3389 n_extra = (int)STRLEN(p_sbr);
3390 char_attr = hl_attr(HLF_AT);
3391 need_showbreak = FALSE;
3392 /* Correct end of highlighted area for 'showbreak',
3393 * required when 'linebreak' is also set. */
3394 if (tocol == vcol)
3395 tocol += n_extra;
3397 # endif
3399 #endif
3401 if (draw_state == WL_LINE - 1 && n_extra == 0)
3403 draw_state = WL_LINE;
3404 if (saved_n_extra)
3406 /* Continue item from end of wrapped line. */
3407 n_extra = saved_n_extra;
3408 c_extra = saved_c_extra;
3409 p_extra = saved_p_extra;
3410 char_attr = saved_char_attr;
3412 else
3413 char_attr = 0;
3417 /* When still displaying '$' of change command, stop at cursor */
3418 if (dollar_vcol != 0 && wp == curwin
3419 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
3420 #ifdef FEAT_DIFF
3421 && filler_todo <= 0
3422 #endif
3425 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3426 wp->w_p_rl);
3427 /* Pretend we have finished updating the window. Except when
3428 * 'cursorcolumn' is set. */
3429 #ifdef FEAT_SYN_HL
3430 if (wp->w_p_cuc)
3431 row = wp->w_cline_row + wp->w_cline_height;
3432 else
3433 #endif
3434 row = wp->w_height;
3435 break;
3438 if (draw_state == WL_LINE && area_highlighting)
3440 /* handle Visual or match highlighting in this line */
3441 if (vcol == fromcol
3442 #ifdef FEAT_MBYTE
3443 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3444 && (*mb_ptr2cells)(ptr) > 1)
3445 #endif
3446 || ((int)vcol_prev == fromcol_prev
3447 && vcol_prev < vcol /* not at margin */
3448 && vcol < tocol))
3449 area_attr = attr; /* start highlighting */
3450 else if (area_attr != 0
3451 && (vcol == tocol
3452 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
3453 area_attr = 0; /* stop highlighting */
3455 #ifdef FEAT_SEARCH_EXTRA
3456 if (!n_extra)
3459 * Check for start/end of search pattern match.
3460 * After end, check for start/end of next match.
3461 * When another match, have to check for start again.
3462 * Watch out for matching an empty string!
3463 * Do this for 'search_hl' and the match list (ordered by
3464 * priority).
3466 v = (long)(ptr - line);
3467 cur = wp->w_match_head;
3468 shl_flag = FALSE;
3469 while (cur != NULL || shl_flag == FALSE)
3471 if (shl_flag == FALSE
3472 && ((cur != NULL
3473 && cur->priority > SEARCH_HL_PRIORITY)
3474 || cur == NULL))
3476 shl = &search_hl;
3477 shl_flag = TRUE;
3479 else
3480 shl = &cur->hl;
3481 while (shl->rm.regprog != NULL)
3483 if (shl->startcol != MAXCOL
3484 && v >= (long)shl->startcol
3485 && v < (long)shl->endcol)
3487 shl->attr_cur = shl->attr;
3489 else if (v == (long)shl->endcol)
3491 shl->attr_cur = 0;
3493 next_search_hl(wp, shl, lnum, (colnr_T)v);
3495 /* Need to get the line again, a multi-line regexp
3496 * may have made it invalid. */
3497 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3498 ptr = line + v;
3500 if (shl->lnum == lnum)
3502 shl->startcol = shl->rm.startpos[0].col;
3503 if (shl->rm.endpos[0].lnum == 0)
3504 shl->endcol = shl->rm.endpos[0].col;
3505 else
3506 shl->endcol = MAXCOL;
3508 if (shl->startcol == shl->endcol)
3510 /* highlight empty match, try again after
3511 * it */
3512 #ifdef FEAT_MBYTE
3513 if (has_mbyte)
3514 shl->endcol += (*mb_ptr2len)(line
3515 + shl->endcol);
3516 else
3517 #endif
3518 ++shl->endcol;
3521 /* Loop to check if the match starts at the
3522 * current position */
3523 continue;
3526 break;
3528 if (shl != &search_hl && cur != NULL)
3529 cur = cur->next;
3532 /* Use attributes from match with highest priority among
3533 * 'search_hl' and the match list. */
3534 search_attr = search_hl.attr_cur;
3535 cur = wp->w_match_head;
3536 shl_flag = FALSE;
3537 while (cur != NULL || shl_flag == FALSE)
3539 if (shl_flag == FALSE
3540 && ((cur != NULL
3541 && cur->priority > SEARCH_HL_PRIORITY)
3542 || cur == NULL))
3544 shl = &search_hl;
3545 shl_flag = TRUE;
3547 else
3548 shl = &cur->hl;
3549 if (shl->attr_cur != 0)
3550 search_attr = shl->attr_cur;
3551 if (shl != &search_hl && cur != NULL)
3552 cur = cur->next;
3555 #endif
3557 #ifdef FEAT_DIFF
3558 if (diff_hlf != (hlf_T)0)
3560 if (diff_hlf == HLF_CHD && ptr - line >= change_start
3561 && n_extra == 0)
3562 diff_hlf = HLF_TXD; /* changed text */
3563 if (diff_hlf == HLF_TXD && ptr - line > change_end
3564 && n_extra == 0)
3565 diff_hlf = HLF_CHD; /* changed line */
3566 line_attr = hl_attr(diff_hlf);
3568 #endif
3570 /* Decide which of the highlight attributes to use. */
3571 attr_pri = TRUE;
3572 if (area_attr != 0)
3573 char_attr = area_attr;
3574 else if (search_attr != 0)
3575 char_attr = search_attr;
3576 #ifdef LINE_ATTR
3577 /* Use line_attr when not in the Visual or 'incsearch' area
3578 * (area_attr may be 0 when "noinvcur" is set). */
3579 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
3580 || vcol < fromcol || vcol_prev < fromcol_prev
3581 || vcol >= tocol))
3582 char_attr = line_attr;
3583 #endif
3584 else
3586 attr_pri = FALSE;
3587 #ifdef FEAT_SYN_HL
3588 if (has_syntax)
3589 char_attr = syntax_attr;
3590 else
3591 #endif
3592 char_attr = 0;
3597 * Get the next character to put on the screen.
3600 * The "p_extra" points to the extra stuff that is inserted to
3601 * represent special characters (non-printable stuff) and other
3602 * things. When all characters are the same, c_extra is used.
3603 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
3604 * "p_extra[n_extra]".
3605 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
3607 if (n_extra > 0)
3609 if (c_extra != NUL)
3611 c = c_extra;
3612 #ifdef FEAT_MBYTE
3613 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
3614 if (enc_utf8 && (*mb_char2len)(c) > 1)
3616 mb_utf8 = TRUE;
3617 u8cc[0] = 0;
3618 c = 0xc0;
3620 else
3621 mb_utf8 = FALSE;
3622 #endif
3624 else
3626 c = *p_extra;
3627 #ifdef FEAT_MBYTE
3628 if (has_mbyte)
3630 mb_c = c;
3631 if (enc_utf8)
3633 /* If the UTF-8 character is more than one byte:
3634 * Decode it into "mb_c". */
3635 mb_l = (*mb_ptr2len)(p_extra);
3636 mb_utf8 = FALSE;
3637 if (mb_l > n_extra)
3638 mb_l = 1;
3639 else if (mb_l > 1)
3641 mb_c = utfc_ptr2char(p_extra, u8cc);
3642 mb_utf8 = TRUE;
3643 c = 0xc0;
3646 else
3648 /* if this is a DBCS character, put it in "mb_c" */
3649 mb_l = MB_BYTE2LEN(c);
3650 if (mb_l >= n_extra)
3651 mb_l = 1;
3652 else if (mb_l > 1)
3653 mb_c = (c << 8) + p_extra[1];
3655 if (mb_l == 0) /* at the NUL at end-of-line */
3656 mb_l = 1;
3658 /* If a double-width char doesn't fit display a '>' in the
3659 * last column. */
3660 if ((
3661 # ifdef FEAT_RIGHTLEFT
3662 wp->w_p_rl ? (col <= 0) :
3663 # endif
3664 (col >= W_WIDTH(wp) - 1))
3665 && (*mb_char2cells)(mb_c) == 2)
3667 c = '>';
3668 mb_c = c;
3669 mb_l = 1;
3670 mb_utf8 = FALSE;
3671 multi_attr = hl_attr(HLF_AT);
3672 /* put the pointer back to output the double-width
3673 * character at the start of the next line. */
3674 ++n_extra;
3675 --p_extra;
3677 else
3679 n_extra -= mb_l - 1;
3680 p_extra += mb_l - 1;
3683 #endif
3684 ++p_extra;
3686 --n_extra;
3688 else
3691 * Get a character from the line itself.
3693 c = *ptr;
3694 #ifdef FEAT_MBYTE
3695 if (has_mbyte)
3697 mb_c = c;
3698 if (enc_utf8)
3700 /* If the UTF-8 character is more than one byte: Decode it
3701 * into "mb_c". */
3702 mb_l = (*mb_ptr2len)(ptr);
3703 mb_utf8 = FALSE;
3704 if (mb_l > 1)
3706 mb_c = utfc_ptr2char(ptr, u8cc);
3707 /* Overlong encoded ASCII or ASCII with composing char
3708 * is displayed normally, except a NUL. */
3709 if (mb_c < 0x80)
3710 c = mb_c;
3711 mb_utf8 = TRUE;
3713 /* At start of the line we can have a composing char.
3714 * Draw it as a space with a composing char. */
3715 if (utf_iscomposing(mb_c))
3717 int i;
3719 for (i = Screen_mco - 1; i > 0; --i)
3720 u8cc[i] = u8cc[i - 1];
3721 u8cc[0] = mb_c;
3722 mb_c = ' ';
3726 if ((mb_l == 1 && c >= 0x80)
3727 || (mb_l >= 1 && mb_c == 0)
3728 || (mb_l > 1 && (!vim_isprintc(mb_c)
3729 # ifdef UNICODE16
3730 || mb_c >= 0x10000
3731 # endif
3735 * Illegal UTF-8 byte: display as <xx>.
3736 * Non-BMP character : display as ? or fullwidth ?.
3738 # ifdef UNICODE16
3739 if (mb_c < 0x10000)
3740 # endif
3742 transchar_hex(extra, mb_c);
3743 # ifdef FEAT_RIGHTLEFT
3744 if (wp->w_p_rl) /* reverse */
3745 rl_mirror(extra);
3746 # endif
3748 # ifdef UNICODE16
3749 else if (utf_char2cells(mb_c) != 2)
3750 STRCPY(extra, "?");
3751 else
3752 /* 0xff1f in UTF-8: full-width '?' */
3753 STRCPY(extra, "\357\274\237");
3754 # endif
3756 p_extra = extra;
3757 c = *p_extra;
3758 mb_c = mb_ptr2char_adv(&p_extra);
3759 mb_utf8 = (c >= 0x80);
3760 n_extra = (int)STRLEN(p_extra);
3761 c_extra = NUL;
3762 if (area_attr == 0 && search_attr == 0)
3764 n_attr = n_extra + 1;
3765 extra_attr = hl_attr(HLF_8);
3766 saved_attr2 = char_attr; /* save current attr */
3769 else if (mb_l == 0) /* at the NUL at end-of-line */
3770 mb_l = 1;
3771 #ifdef FEAT_ARABIC
3772 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
3774 /* Do Arabic shaping. */
3775 int pc, pc1, nc;
3776 int pcc[MAX_MCO];
3778 /* The idea of what is the previous and next
3779 * character depends on 'rightleft'. */
3780 if (wp->w_p_rl)
3782 pc = prev_c;
3783 pc1 = prev_c1;
3784 nc = utf_ptr2char(ptr + mb_l);
3785 prev_c1 = u8cc[0];
3787 else
3789 pc = utfc_ptr2char(ptr + mb_l, pcc);
3790 nc = prev_c;
3791 pc1 = pcc[0];
3793 prev_c = mb_c;
3795 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
3797 else
3798 prev_c = mb_c;
3799 #endif
3801 else /* enc_dbcs */
3803 mb_l = MB_BYTE2LEN(c);
3804 if (mb_l == 0) /* at the NUL at end-of-line */
3805 mb_l = 1;
3806 else if (mb_l > 1)
3808 /* We assume a second byte below 32 is illegal.
3809 * Hopefully this is OK for all double-byte encodings!
3811 if (ptr[1] >= 32)
3812 mb_c = (c << 8) + ptr[1];
3813 else
3815 if (ptr[1] == NUL)
3817 /* head byte at end of line */
3818 mb_l = 1;
3819 transchar_nonprint(extra, c);
3821 else
3823 /* illegal tail byte */
3824 mb_l = 2;
3825 STRCPY(extra, "XX");
3827 p_extra = extra;
3828 n_extra = (int)STRLEN(extra) - 1;
3829 c_extra = NUL;
3830 c = *p_extra++;
3831 if (area_attr == 0 && search_attr == 0)
3833 n_attr = n_extra + 1;
3834 extra_attr = hl_attr(HLF_8);
3835 saved_attr2 = char_attr; /* save current attr */
3837 mb_c = c;
3841 /* If a double-width char doesn't fit display a '>' in the
3842 * last column; the character is displayed at the start of the
3843 * next line. */
3844 if ((
3845 # ifdef FEAT_RIGHTLEFT
3846 wp->w_p_rl ? (col <= 0) :
3847 # endif
3848 (col >= W_WIDTH(wp) - 1))
3849 && (*mb_char2cells)(mb_c) == 2)
3851 c = '>';
3852 mb_c = c;
3853 mb_utf8 = FALSE;
3854 mb_l = 1;
3855 multi_attr = hl_attr(HLF_AT);
3856 /* Put pointer back so that the character will be
3857 * displayed at the start of the next line. */
3858 --ptr;
3860 else if (*ptr != NUL)
3861 ptr += mb_l - 1;
3863 /* If a double-width char doesn't fit at the left side display
3864 * a '<' in the first column. */
3865 if (n_skip > 0 && mb_l > 1)
3867 n_extra = 1;
3868 c_extra = '<';
3869 c = ' ';
3870 if (area_attr == 0 && search_attr == 0)
3872 n_attr = n_extra + 1;
3873 extra_attr = hl_attr(HLF_AT);
3874 saved_attr2 = char_attr; /* save current attr */
3876 mb_c = c;
3877 mb_utf8 = FALSE;
3878 mb_l = 1;
3882 #endif
3883 ++ptr;
3885 /* 'list' : change char 160 to lcs_nbsp. */
3886 if (wp->w_p_list && (c == 160
3887 #ifdef FEAT_MBYTE
3888 || (mb_utf8 && mb_c == 160)
3889 #endif
3890 ) && lcs_nbsp)
3892 c = lcs_nbsp;
3893 if (area_attr == 0 && search_attr == 0)
3895 n_attr = 1;
3896 extra_attr = hl_attr(HLF_8);
3897 saved_attr2 = char_attr; /* save current attr */
3899 #ifdef FEAT_MBYTE
3900 mb_c = c;
3901 if (enc_utf8 && (*mb_char2len)(c) > 1)
3903 mb_utf8 = TRUE;
3904 u8cc[0] = 0;
3905 c = 0xc0;
3907 else
3908 mb_utf8 = FALSE;
3909 #endif
3912 if (extra_check)
3914 #ifdef FEAT_SPELL
3915 int can_spell = TRUE;
3916 #endif
3918 #ifdef FEAT_SYN_HL
3919 /* Get syntax attribute, unless still at the start of the line
3920 * (double-wide char that doesn't fit). */
3921 v = (long)(ptr - line);
3922 if (has_syntax && v > 0)
3924 /* Get the syntax attribute for the character. If there
3925 * is an error, disable syntax highlighting. */
3926 save_did_emsg = did_emsg;
3927 did_emsg = FALSE;
3929 syntax_attr = get_syntax_attr((colnr_T)v - 1,
3930 # ifdef FEAT_SPELL
3931 has_spell ? &can_spell :
3932 # endif
3933 NULL, FALSE);
3935 if (did_emsg)
3937 wp->w_buffer->b_syn_error = TRUE;
3938 has_syntax = FALSE;
3940 else
3941 did_emsg = save_did_emsg;
3943 /* Need to get the line again, a multi-line regexp may
3944 * have made it invalid. */
3945 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3946 ptr = line + v;
3948 if (!attr_pri)
3949 char_attr = syntax_attr;
3950 else
3951 char_attr = hl_combine_attr(syntax_attr, char_attr);
3953 #endif
3955 #ifdef FEAT_SPELL
3956 /* Check spelling (unless at the end of the line).
3957 * Only do this when there is no syntax highlighting, the
3958 * @Spell cluster is not used or the current syntax item
3959 * contains the @Spell cluster. */
3960 if (has_spell && v >= word_end && v > cur_checked_col)
3962 spell_attr = 0;
3963 # ifdef FEAT_SYN_HL
3964 if (!attr_pri)
3965 char_attr = syntax_attr;
3966 # endif
3967 if (c != 0 && (
3968 # ifdef FEAT_SYN_HL
3969 !has_syntax ||
3970 # endif
3971 can_spell))
3973 char_u *prev_ptr, *p;
3974 int len;
3975 hlf_T spell_hlf = HLF_COUNT;
3976 # ifdef FEAT_MBYTE
3977 if (has_mbyte)
3979 prev_ptr = ptr - mb_l;
3980 v -= mb_l - 1;
3982 else
3983 # endif
3984 prev_ptr = ptr - 1;
3986 /* Use nextline[] if possible, it has the start of the
3987 * next line concatenated. */
3988 if ((prev_ptr - line) - nextlinecol >= 0)
3989 p = nextline + (prev_ptr - line) - nextlinecol;
3990 else
3991 p = prev_ptr;
3992 cap_col -= (int)(prev_ptr - line);
3993 len = spell_check(wp, p, &spell_hlf, &cap_col,
3994 nochange);
3995 word_end = v + len;
3997 /* In Insert mode only highlight a word that
3998 * doesn't touch the cursor. */
3999 if (spell_hlf != HLF_COUNT
4000 && (State & INSERT) != 0
4001 && wp->w_cursor.lnum == lnum
4002 && wp->w_cursor.col >=
4003 (colnr_T)(prev_ptr - line)
4004 && wp->w_cursor.col < (colnr_T)word_end)
4006 spell_hlf = HLF_COUNT;
4007 spell_redraw_lnum = lnum;
4010 if (spell_hlf == HLF_COUNT && p != prev_ptr
4011 && (p - nextline) + len > nextline_idx)
4013 /* Remember that the good word continues at the
4014 * start of the next line. */
4015 checked_lnum = lnum + 1;
4016 checked_col = (int)((p - nextline) + len - nextline_idx);
4019 /* Turn index into actual attributes. */
4020 if (spell_hlf != HLF_COUNT)
4021 spell_attr = highlight_attr[spell_hlf];
4023 if (cap_col > 0)
4025 if (p != prev_ptr
4026 && (p - nextline) + cap_col >= nextline_idx)
4028 /* Remember that the word in the next line
4029 * must start with a capital. */
4030 capcol_lnum = lnum + 1;
4031 cap_col = (int)((p - nextline) + cap_col
4032 - nextline_idx);
4034 else
4035 /* Compute the actual column. */
4036 cap_col += (int)(prev_ptr - line);
4040 if (spell_attr != 0)
4042 if (!attr_pri)
4043 char_attr = hl_combine_attr(char_attr, spell_attr);
4044 else
4045 char_attr = hl_combine_attr(spell_attr, char_attr);
4047 #endif
4048 #ifdef FEAT_LINEBREAK
4050 * Found last space before word: check for line break.
4052 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr)
4053 && !wp->w_p_list)
4055 n_extra = win_lbr_chartabsize(wp, ptr - (
4056 # ifdef FEAT_MBYTE
4057 has_mbyte ? mb_l :
4058 # endif
4059 1), (colnr_T)vcol, NULL) - 1;
4060 c_extra = ' ';
4061 if (vim_iswhite(c))
4062 c = ' ';
4064 #endif
4066 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4068 c = lcs_trail;
4069 if (!attr_pri)
4071 n_attr = 1;
4072 extra_attr = hl_attr(HLF_8);
4073 saved_attr2 = char_attr; /* save current attr */
4075 #ifdef FEAT_MBYTE
4076 mb_c = c;
4077 if (enc_utf8 && (*mb_char2len)(c) > 1)
4079 mb_utf8 = TRUE;
4080 u8cc[0] = 0;
4081 c = 0xc0;
4083 else
4084 mb_utf8 = FALSE;
4085 #endif
4090 * Handling of non-printable characters.
4092 if (!(chartab[c & 0xff] & CT_PRINT_CHAR))
4095 * when getting a character from the file, we may have to
4096 * turn it into something else on the way to putting it
4097 * into "ScreenLines".
4099 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4101 /* tab amount depends on current column */
4102 n_extra = (int)wp->w_buffer->b_p_ts
4103 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4104 #ifdef FEAT_MBYTE
4105 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4106 #endif
4107 if (wp->w_p_list)
4109 c = lcs_tab1;
4110 c_extra = lcs_tab2;
4111 n_attr = n_extra + 1;
4112 extra_attr = hl_attr(HLF_8);
4113 saved_attr2 = char_attr; /* save current attr */
4114 #ifdef FEAT_MBYTE
4115 mb_c = c;
4116 if (enc_utf8 && (*mb_char2len)(c) > 1)
4118 mb_utf8 = TRUE;
4119 u8cc[0] = 0;
4120 c = 0xc0;
4122 #endif
4124 else
4126 c_extra = ' ';
4127 c = ' ';
4130 else if (c == NUL
4131 && ((wp->w_p_list && lcs_eol > 0)
4132 || ((fromcol >= 0 || fromcol_prev >= 0)
4133 && tocol > vcol
4134 #ifdef FEAT_VISUAL
4135 && VIsual_mode != Ctrl_V
4136 #endif
4137 && (
4138 # ifdef FEAT_RIGHTLEFT
4139 wp->w_p_rl ? (col >= 0) :
4140 # endif
4141 (col < W_WIDTH(wp)))
4142 && !(noinvcur
4143 && lnum == wp->w_cursor.lnum
4144 && (colnr_T)vcol == wp->w_virtcol)))
4145 && lcs_eol_one >= 0)
4147 /* Display a '$' after the line or highlight an extra
4148 * character if the line break is included. */
4149 #if defined(FEAT_DIFF) || defined(LINE_ATTR)
4150 /* For a diff line the highlighting continues after the
4151 * "$". */
4152 if (
4153 # ifdef FEAT_DIFF
4154 diff_hlf == (hlf_T)0
4155 # ifdef LINE_ATTR
4157 # endif
4158 # endif
4159 # ifdef LINE_ATTR
4160 line_attr == 0
4161 # endif
4163 #endif
4165 #ifdef FEAT_VIRTUALEDIT
4166 /* In virtualedit, visual selections may extend
4167 * beyond end of line. */
4168 if (area_highlighting && virtual_active()
4169 && tocol != MAXCOL && vcol < tocol)
4170 n_extra = 0;
4171 else
4172 #endif
4174 p_extra = at_end_str;
4175 n_extra = 1;
4176 c_extra = NUL;
4179 if (wp->w_p_list)
4180 c = lcs_eol;
4181 else
4182 c = ' ';
4183 lcs_eol_one = -1;
4184 --ptr; /* put it back at the NUL */
4185 if (!attr_pri)
4187 extra_attr = hl_attr(HLF_AT);
4188 n_attr = 1;
4190 #ifdef FEAT_MBYTE
4191 mb_c = c;
4192 if (enc_utf8 && (*mb_char2len)(c) > 1)
4194 mb_utf8 = TRUE;
4195 u8cc[0] = 0;
4196 c = 0xc0;
4198 else
4199 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4200 #endif
4202 else if (c != NUL)
4204 p_extra = transchar(c);
4205 #ifdef FEAT_RIGHTLEFT
4206 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4207 rl_mirror(p_extra); /* reverse "<12>" */
4208 #endif
4209 n_extra = byte2cells(c) - 1;
4210 c_extra = NUL;
4211 c = *p_extra++;
4212 if (!attr_pri)
4214 n_attr = n_extra + 1;
4215 extra_attr = hl_attr(HLF_8);
4216 saved_attr2 = char_attr; /* save current attr */
4218 #ifdef FEAT_MBYTE
4219 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4220 #endif
4222 #ifdef FEAT_VIRTUALEDIT
4223 else if (VIsual_active
4224 && (VIsual_mode == Ctrl_V
4225 || VIsual_mode == 'v')
4226 && virtual_active()
4227 && tocol != MAXCOL
4228 && vcol < tocol
4229 && (
4230 # ifdef FEAT_RIGHTLEFT
4231 wp->w_p_rl ? (col >= 0) :
4232 # endif
4233 (col < W_WIDTH(wp))))
4235 c = ' ';
4236 --ptr; /* put it back at the NUL */
4238 #endif
4239 #if defined(LINE_ATTR)
4240 else if ((
4241 # ifdef FEAT_DIFF
4242 diff_hlf != (hlf_T)0 ||
4243 # endif
4244 line_attr != 0
4245 ) && (
4246 # ifdef FEAT_RIGHTLEFT
4247 wp->w_p_rl ? (col >= 0) :
4248 # endif
4249 (col < W_WIDTH(wp))))
4251 /* Highlight until the right side of the window */
4252 c = ' ';
4253 --ptr; /* put it back at the NUL */
4255 /* Remember we do the char for line highlighting. */
4256 ++did_line_attr;
4258 /* don't do search HL for the rest of the line */
4259 if (line_attr != 0 && char_attr == search_attr && col > 0)
4260 char_attr = line_attr;
4261 # ifdef FEAT_DIFF
4262 if (diff_hlf == HLF_TXD)
4264 diff_hlf = HLF_CHD;
4265 if (attr == 0 || char_attr != attr)
4266 char_attr = hl_attr(diff_hlf);
4268 # endif
4270 #endif
4274 /* Don't override visual selection highlighting. */
4275 if (n_attr > 0
4276 && draw_state == WL_LINE
4277 && !attr_pri)
4278 char_attr = extra_attr;
4280 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
4281 /* XIM don't send preedit_start and preedit_end, but they send
4282 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
4283 * im_is_preediting() here. */
4284 if (xic != NULL
4285 && lnum == wp->w_cursor.lnum
4286 && (State & INSERT)
4287 && !p_imdisable
4288 && im_is_preediting()
4289 && draw_state == WL_LINE)
4291 colnr_T tcol;
4293 if (preedit_end_col == MAXCOL)
4294 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
4295 else
4296 tcol = preedit_end_col;
4297 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
4299 if (feedback_old_attr < 0)
4301 feedback_col = 0;
4302 feedback_old_attr = char_attr;
4304 char_attr = im_get_feedback_attr(feedback_col);
4305 if (char_attr < 0)
4306 char_attr = feedback_old_attr;
4307 feedback_col++;
4309 else if (feedback_old_attr >= 0)
4311 char_attr = feedback_old_attr;
4312 feedback_old_attr = -1;
4313 feedback_col = 0;
4316 #endif
4318 * Handle the case where we are in column 0 but not on the first
4319 * character of the line and the user wants us to show us a
4320 * special character (via 'listchars' option "precedes:<char>".
4322 if (lcs_prec_todo != NUL
4323 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
4324 #ifdef FEAT_DIFF
4325 && filler_todo <= 0
4326 #endif
4327 && draw_state > WL_NR
4328 && c != NUL)
4330 c = lcs_prec;
4331 lcs_prec_todo = NUL;
4332 #ifdef FEAT_MBYTE
4333 mb_c = c;
4334 if (enc_utf8 && (*mb_char2len)(c) > 1)
4336 mb_utf8 = TRUE;
4337 u8cc[0] = 0;
4338 c = 0xc0;
4340 else
4341 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4342 #endif
4343 if (!attr_pri)
4345 saved_attr3 = char_attr; /* save current attr */
4346 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
4347 n_attr3 = 1;
4352 * At end of the text line or just after the last character.
4354 if (c == NUL
4355 #if defined(LINE_ATTR)
4356 || did_line_attr == 1
4357 #endif
4360 #ifdef FEAT_SEARCH_EXTRA
4361 long prevcol = (long)(ptr - line) - (c == NUL);
4363 /* we're not really at that column when skipping some text */
4364 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
4365 ++prevcol;
4366 #endif
4368 /* invert at least one char, used for Visual and empty line or
4369 * highlight match at end of line. If it's beyond the last
4370 * char on the screen, just overwrite that one (tricky!) Not
4371 * needed when a '$' was displayed for 'list'. */
4372 #ifdef FEAT_SEARCH_EXTRA
4373 prevcol_hl_flag = FALSE;
4374 if (prevcol == (long)search_hl.startcol)
4375 prevcol_hl_flag = TRUE;
4376 else
4378 cur = wp->w_match_head;
4379 while (cur != NULL)
4381 if (prevcol == (long)cur->hl.startcol)
4383 prevcol_hl_flag = TRUE;
4384 break;
4386 cur = cur->next;
4389 #endif
4390 if (lcs_eol == lcs_eol_one
4391 && ((area_attr != 0 && vcol == fromcol
4392 #ifdef FEAT_VISUAL
4393 && (VIsual_mode != Ctrl_V
4394 || lnum == VIsual.lnum
4395 || lnum == curwin->w_cursor.lnum)
4396 #endif
4397 && c == NUL)
4398 #ifdef FEAT_SEARCH_EXTRA
4399 /* highlight 'hlsearch' match at end of line */
4400 || (prevcol_hl_flag == TRUE
4401 # if defined(LINE_ATTR)
4402 && did_line_attr <= 1
4403 # endif
4405 #endif
4408 int n = 0;
4410 #ifdef FEAT_RIGHTLEFT
4411 if (wp->w_p_rl)
4413 if (col < 0)
4414 n = 1;
4416 else
4417 #endif
4419 if (col >= W_WIDTH(wp))
4420 n = -1;
4422 if (n != 0)
4424 /* At the window boundary, highlight the last character
4425 * instead (better than nothing). */
4426 off += n;
4427 col += n;
4429 else
4431 /* Add a blank character to highlight. */
4432 ScreenLines[off] = ' ';
4433 #ifdef FEAT_MBYTE
4434 if (enc_utf8)
4435 ScreenLinesUC[off] = 0;
4436 #endif
4438 #ifdef FEAT_SEARCH_EXTRA
4439 if (area_attr == 0)
4441 /* Use attributes from match with highest priority among
4442 * 'search_hl' and the match list. */
4443 char_attr = search_hl.attr;
4444 cur = wp->w_match_head;
4445 shl_flag = FALSE;
4446 while (cur != NULL || shl_flag == FALSE)
4448 if (shl_flag == FALSE
4449 && ((cur != NULL
4450 && cur->priority > SEARCH_HL_PRIORITY)
4451 || cur == NULL))
4453 shl = &search_hl;
4454 shl_flag = TRUE;
4456 else
4457 shl = &cur->hl;
4458 if ((ptr - line) - 1 == (long)shl->startcol)
4459 char_attr = shl->attr;
4460 if (shl != &search_hl && cur != NULL)
4461 cur = cur->next;
4464 #endif
4465 ScreenAttrs[off] = char_attr;
4466 #ifdef FEAT_RIGHTLEFT
4467 if (wp->w_p_rl)
4469 --col;
4470 --off;
4472 else
4473 #endif
4475 ++col;
4476 ++off;
4478 ++vcol;
4479 #ifdef FEAT_SYN_HL
4480 eol_hl_off = 1;
4481 #endif
4486 * At end of the text line.
4488 if (c == NUL)
4490 #ifdef FEAT_SYN_HL
4491 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
4492 && lnum == wp->w_cursor.lnum)
4494 /* highlight last char after line */
4495 --col;
4496 --off;
4497 --vcol;
4500 /* Highlight 'cursorcolumn' past end of the line. */
4501 if (wp->w_p_wrap)
4502 v = wp->w_skipcol;
4503 else
4504 v = wp->w_leftcol;
4505 /* check if line ends before left margin */
4506 if (vcol < v + col - win_col_off(wp))
4508 vcol = v + col - win_col_off(wp);
4509 if (wp->w_p_cuc
4510 && (int)wp->w_virtcol >= vcol - eol_hl_off
4511 && (int)wp->w_virtcol < W_WIDTH(wp) * (row - startrow + 1)
4513 && lnum != wp->w_cursor.lnum
4514 # ifdef FEAT_RIGHTLEFT
4515 && !wp->w_p_rl
4516 # endif
4519 while (col < W_WIDTH(wp))
4521 ScreenLines[off] = ' ';
4522 #ifdef FEAT_MBYTE
4523 if (enc_utf8)
4524 ScreenLinesUC[off] = 0;
4525 #endif
4526 ++col;
4527 if (vcol == (long)wp->w_virtcol)
4529 ScreenAttrs[off] = hl_attr(HLF_CUC);
4530 break;
4532 ScreenAttrs[off++] = 0;
4533 ++vcol;
4536 #endif
4538 SCREEN_LINE(screen_row, W_WINCOL(wp), col, (int)W_WIDTH(wp),
4539 wp->w_p_rl);
4540 row++;
4543 * Update w_cline_height and w_cline_folded if the cursor line was
4544 * updated (saves a call to plines() later).
4546 if (wp == curwin && lnum == curwin->w_cursor.lnum)
4548 curwin->w_cline_row = startrow;
4549 curwin->w_cline_height = row - startrow;
4550 #ifdef FEAT_FOLDING
4551 curwin->w_cline_folded = FALSE;
4552 #endif
4553 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
4556 break;
4559 /* line continues beyond line end */
4560 if (lcs_ext
4561 && !wp->w_p_wrap
4562 #ifdef FEAT_DIFF
4563 && filler_todo <= 0
4564 #endif
4565 && (
4566 #ifdef FEAT_RIGHTLEFT
4567 wp->w_p_rl ? col == 0 :
4568 #endif
4569 col == W_WIDTH(wp) - 1)
4570 && (*ptr != NUL
4571 || (wp->w_p_list && lcs_eol_one > 0)
4572 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
4574 c = lcs_ext;
4575 char_attr = hl_attr(HLF_AT);
4576 #ifdef FEAT_MBYTE
4577 mb_c = c;
4578 if (enc_utf8 && (*mb_char2len)(c) > 1)
4580 mb_utf8 = TRUE;
4581 u8cc[0] = 0;
4582 c = 0xc0;
4584 else
4585 mb_utf8 = FALSE;
4586 #endif
4589 #ifdef FEAT_SYN_HL
4590 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
4591 * highlight the cursor position itself. */
4592 if (wp->w_p_cuc && vcol == (long)wp->w_virtcol
4593 && lnum != wp->w_cursor.lnum
4594 && draw_state == WL_LINE
4595 && !lnum_in_visual_area)
4597 vcol_save_attr = char_attr;
4598 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
4600 else
4601 vcol_save_attr = -1;
4602 #endif
4605 * Store character to be displayed.
4606 * Skip characters that are left of the screen for 'nowrap'.
4608 vcol_prev = vcol;
4609 if (draw_state < WL_LINE || n_skip <= 0)
4612 * Store the character.
4614 #if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
4615 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
4617 /* A double-wide character is: put first halve in left cell. */
4618 --off;
4619 --col;
4621 #endif
4622 ScreenLines[off] = c;
4623 #ifdef FEAT_MBYTE
4624 if (enc_dbcs == DBCS_JPNU)
4625 ScreenLines2[off] = mb_c & 0xff;
4626 else if (enc_utf8)
4628 if (mb_utf8)
4630 int i;
4632 ScreenLinesUC[off] = mb_c;
4633 if ((c & 0xff) == 0)
4634 ScreenLines[off] = 0x80; /* avoid storing zero */
4635 for (i = 0; i < Screen_mco; ++i)
4637 ScreenLinesC[i][off] = u8cc[i];
4638 if (u8cc[i] == 0)
4639 break;
4642 else
4643 ScreenLinesUC[off] = 0;
4645 if (multi_attr)
4647 ScreenAttrs[off] = multi_attr;
4648 multi_attr = 0;
4650 else
4651 #endif
4652 ScreenAttrs[off] = char_attr;
4654 #ifdef FEAT_MBYTE
4655 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
4657 /* Need to fill two screen columns. */
4658 ++off;
4659 ++col;
4660 if (enc_utf8)
4661 /* UTF-8: Put a 0 in the second screen char. */
4662 ScreenLines[off] = 0;
4663 else
4664 /* DBCS: Put second byte in the second screen char. */
4665 ScreenLines[off] = mb_c & 0xff;
4666 ++vcol;
4667 /* When "tocol" is halfway a character, set it to the end of
4668 * the character, otherwise highlighting won't stop. */
4669 if (tocol == vcol)
4670 ++tocol;
4671 #ifdef FEAT_RIGHTLEFT
4672 if (wp->w_p_rl)
4674 /* now it's time to backup one cell */
4675 --off;
4676 --col;
4678 #endif
4680 #endif
4681 #ifdef FEAT_RIGHTLEFT
4682 if (wp->w_p_rl)
4684 --off;
4685 --col;
4687 else
4688 #endif
4690 ++off;
4691 ++col;
4694 else
4695 --n_skip;
4697 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
4698 * column. */
4699 if (draw_state > WL_NR
4700 #ifdef FEAT_DIFF
4701 && filler_todo <= 0
4702 #endif
4704 ++vcol;
4706 #ifdef FEAT_SYN_HL
4707 if (vcol_save_attr >= 0)
4708 char_attr = vcol_save_attr;
4709 #endif
4711 /* restore attributes after "predeces" in 'listchars' */
4712 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
4713 char_attr = saved_attr3;
4715 /* restore attributes after last 'listchars' or 'number' char */
4716 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
4717 char_attr = saved_attr2;
4720 * At end of screen line and there is more to come: Display the line
4721 * so far. If there is no more to display it is caught above.
4723 if ((
4724 #ifdef FEAT_RIGHTLEFT
4725 wp->w_p_rl ? (col < 0) :
4726 #endif
4727 (col >= W_WIDTH(wp)))
4728 && (*ptr != NUL
4729 #ifdef FEAT_DIFF
4730 || filler_todo > 0
4731 #endif
4732 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
4733 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
4736 SCREEN_LINE(screen_row, W_WINCOL(wp), col, (int)W_WIDTH(wp),
4737 wp->w_p_rl);
4738 ++row;
4739 ++screen_row;
4741 /* When not wrapping and finished diff lines, or when displayed
4742 * '$' and highlighting until last column, break here. */
4743 if ((!wp->w_p_wrap
4744 #ifdef FEAT_DIFF
4745 && filler_todo <= 0
4746 #endif
4747 ) || lcs_eol_one == -1)
4748 break;
4750 /* When the window is too narrow draw all "@" lines. */
4751 if (draw_state != WL_LINE
4752 #ifdef FEAT_DIFF
4753 && filler_todo <= 0
4754 #endif
4757 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
4758 #ifdef FEAT_VERTSPLIT
4759 draw_vsep_win(wp, row);
4760 #endif
4761 row = endrow;
4764 /* When line got too long for screen break here. */
4765 if (row == endrow)
4767 ++row;
4768 break;
4771 if (screen_cur_row == screen_row - 1
4772 #ifdef FEAT_DIFF
4773 && filler_todo <= 0
4774 #endif
4775 && W_WIDTH(wp) == Columns)
4777 /* Remember that the line wraps, used for modeless copy. */
4778 LineWraps[screen_row - 1] = TRUE;
4781 * Special trick to make copy/paste of wrapped lines work with
4782 * xterm/screen: write an extra character beyond the end of
4783 * the line. This will work with all terminal types
4784 * (regardless of the xn,am settings).
4785 * Only do this on a fast tty.
4786 * Only do this if the cursor is on the current line
4787 * (something has been written in it).
4788 * Don't do this for the GUI.
4789 * Don't do this for double-width characters.
4790 * Don't do this for a window not at the right screen border.
4792 if (p_tf
4793 #ifdef FEAT_GUI
4794 && !gui.in_use
4795 #endif
4796 #ifdef FEAT_MBYTE
4797 && !(has_mbyte
4798 && ((*mb_off2cells)(LineOffset[screen_row],
4799 LineOffset[screen_row] + screen_Columns)
4800 == 2
4801 || (*mb_off2cells)(LineOffset[screen_row - 1]
4802 + (int)Columns - 2,
4803 LineOffset[screen_row] + screen_Columns)
4804 == 2))
4805 #endif
4808 /* First make sure we are at the end of the screen line,
4809 * then output the same character again to let the
4810 * terminal know about the wrap. If the terminal doesn't
4811 * auto-wrap, we overwrite the character. */
4812 if (screen_cur_col != W_WIDTH(wp))
4813 screen_char(LineOffset[screen_row - 1]
4814 + (unsigned)Columns - 1,
4815 screen_row - 1, (int)(Columns - 1));
4817 #ifdef FEAT_MBYTE
4818 /* When there is a multi-byte character, just output a
4819 * space to keep it simple. */
4820 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
4821 screen_row - 1] + (Columns - 1)]) > 1)
4822 out_char(' ');
4823 else
4824 #endif
4825 out_char(ScreenLines[LineOffset[screen_row - 1]
4826 + (Columns - 1)]);
4827 /* force a redraw of the first char on the next line */
4828 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
4829 screen_start(); /* don't know where cursor is now */
4833 col = 0;
4834 off = (unsigned)(current_ScreenLine - ScreenLines);
4835 #ifdef FEAT_RIGHTLEFT
4836 if (wp->w_p_rl)
4838 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
4839 off += col;
4841 #endif
4843 /* reset the drawing state for the start of a wrapped line */
4844 draw_state = WL_START;
4845 saved_n_extra = n_extra;
4846 saved_p_extra = p_extra;
4847 saved_c_extra = c_extra;
4848 saved_char_attr = char_attr;
4849 n_extra = 0;
4850 lcs_prec_todo = lcs_prec;
4851 #ifdef FEAT_LINEBREAK
4852 # ifdef FEAT_DIFF
4853 if (filler_todo <= 0)
4854 # endif
4855 need_showbreak = TRUE;
4856 #endif
4857 #ifdef FEAT_DIFF
4858 --filler_todo;
4859 /* When the filler lines are actually below the last line of the
4860 * file, don't draw the line itself, break here. */
4861 if (filler_todo == 0 && wp->w_botfill)
4862 break;
4863 #endif
4866 } /* for every character in the line */
4868 #ifdef FEAT_SPELL
4869 /* After an empty line check first word for capital. */
4870 if (*skipwhite(line) == NUL)
4872 capcol_lnum = lnum + 1;
4873 cap_col = 0;
4875 #endif
4877 return row;
4880 #ifdef FEAT_MBYTE
4881 static int comp_char_differs __ARGS((int, int));
4884 * Return if the composing characters at "off_from" and "off_to" differ.
4886 static int
4887 comp_char_differs(off_from, off_to)
4888 int off_from;
4889 int off_to;
4891 int i;
4893 for (i = 0; i < Screen_mco; ++i)
4895 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
4896 return TRUE;
4897 if (ScreenLinesC[i][off_from] == 0)
4898 break;
4900 return FALSE;
4902 #endif
4905 * Check whether the given character needs redrawing:
4906 * - the (first byte of the) character is different
4907 * - the attributes are different
4908 * - the character is multi-byte and the next byte is different
4909 * - the character is two cells wide and the second cell differs.
4911 static int
4912 char_needs_redraw(off_from, off_to, cols)
4913 int off_from;
4914 int off_to;
4915 int cols;
4917 if (cols > 0
4918 && ((ScreenLines[off_from] != ScreenLines[off_to]
4919 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
4921 #ifdef FEAT_MBYTE
4922 || (enc_dbcs != 0
4923 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
4924 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
4925 ? ScreenLines2[off_from] != ScreenLines2[off_to]
4926 : (cols > 1 && ScreenLines[off_from + 1]
4927 != ScreenLines[off_to + 1])))
4928 || (enc_utf8
4929 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
4930 || (ScreenLinesUC[off_from] != 0
4931 && comp_char_differs(off_from, off_to))
4932 || (cols > 1 && ScreenLines[off_from + 1]
4933 != ScreenLines[off_to + 1])))
4934 #endif
4936 return TRUE;
4937 return FALSE;
4941 * Move one "cooked" screen line to the screen, but only the characters that
4942 * have actually changed. Handle insert/delete character.
4943 * "coloff" gives the first column on the screen for this line.
4944 * "endcol" gives the columns where valid characters are.
4945 * "clear_width" is the width of the window. It's > 0 if the rest of the line
4946 * needs to be cleared, negative otherwise.
4947 * "rlflag" is TRUE in a rightleft window:
4948 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
4949 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
4951 static void
4952 screen_line(row, coloff, endcol, clear_width
4953 #ifdef FEAT_RIGHTLEFT
4954 , rlflag
4955 #endif
4957 int row;
4958 int coloff;
4959 int endcol;
4960 int clear_width;
4961 #ifdef FEAT_RIGHTLEFT
4962 int rlflag;
4963 #endif
4965 unsigned off_from;
4966 unsigned off_to;
4967 #ifdef FEAT_MBYTE
4968 unsigned max_off_from;
4969 unsigned max_off_to;
4970 #endif
4971 int col = 0;
4972 #if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_VERTSPLIT)
4973 int hl;
4974 #endif
4975 int force = FALSE; /* force update rest of the line */
4976 int redraw_this /* bool: does character need redraw? */
4977 #ifdef FEAT_GUI
4978 = TRUE /* For GUI when while-loop empty */
4979 #endif
4981 int redraw_next; /* redraw_this for next character */
4982 #ifdef FEAT_MBYTE
4983 int clear_next = FALSE;
4984 int char_cells; /* 1: normal char */
4985 /* 2: occupies two display cells */
4986 # define CHAR_CELLS char_cells
4987 #else
4988 # define CHAR_CELLS 1
4989 #endif
4991 # ifdef FEAT_CLIPBOARD
4992 clip_may_clear_selection(row, row);
4993 # endif
4995 off_from = (unsigned)(current_ScreenLine - ScreenLines);
4996 off_to = LineOffset[row] + coloff;
4997 #ifdef FEAT_MBYTE
4998 max_off_from = off_from + screen_Columns;
4999 max_off_to = LineOffset[row] + screen_Columns;
5000 #endif
5002 #ifdef FEAT_RIGHTLEFT
5003 if (rlflag)
5005 /* Clear rest first, because it's left of the text. */
5006 if (clear_width > 0)
5008 while (col <= endcol && ScreenLines[off_to] == ' '
5009 && ScreenAttrs[off_to] == 0
5010 # ifdef FEAT_MBYTE
5011 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5012 # endif
5015 ++off_to;
5016 ++col;
5018 if (col <= endcol)
5019 screen_fill(row, row + 1, col + coloff,
5020 endcol + coloff + 1, ' ', ' ', 0);
5022 col = endcol + 1;
5023 off_to = LineOffset[row] + col + coloff;
5024 off_from += col;
5025 endcol = (clear_width > 0 ? clear_width : -clear_width);
5027 #endif /* FEAT_RIGHTLEFT */
5029 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
5031 while (col < endcol)
5033 #ifdef FEAT_MBYTE
5034 if (has_mbyte && (col + 1 < endcol))
5035 char_cells = (*mb_off2cells)(off_from, max_off_from);
5036 else
5037 char_cells = 1;
5038 #endif
5040 redraw_this = redraw_next;
5041 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
5042 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
5044 #ifdef FEAT_GUI
5045 /* If the next character was bold, then redraw the current character to
5046 * remove any pixels that might have spilt over into us. This only
5047 * happens in the GUI.
5049 if (redraw_next && gui.in_use)
5051 hl = ScreenAttrs[off_to + CHAR_CELLS];
5052 if (hl > HL_ALL)
5053 hl = syn_attr2attr(hl);
5054 if (hl & HL_BOLD)
5055 redraw_this = TRUE;
5057 #endif
5059 if (redraw_this)
5062 * Special handling when 'xs' termcap flag set (hpterm):
5063 * Attributes for characters are stored at the position where the
5064 * cursor is when writing the highlighting code. The
5065 * start-highlighting code must be written with the cursor on the
5066 * first highlighted character. The stop-highlighting code must
5067 * be written with the cursor just after the last highlighted
5068 * character.
5069 * Overwriting a character doesn't remove it's highlighting. Need
5070 * to clear the rest of the line, and force redrawing it
5071 * completely.
5073 if ( p_wiv
5074 && !force
5075 #ifdef FEAT_GUI
5076 && !gui.in_use
5077 #endif
5078 && ScreenAttrs[off_to] != 0
5079 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
5082 * Need to remove highlighting attributes here.
5084 windgoto(row, col + coloff);
5085 out_str(T_CE); /* clear rest of this screen line */
5086 screen_start(); /* don't know where cursor is now */
5087 force = TRUE; /* force redraw of rest of the line */
5088 redraw_next = TRUE; /* or else next char would miss out */
5091 * If the previous character was highlighted, need to stop
5092 * highlighting at this character.
5094 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
5096 screen_attr = ScreenAttrs[off_to - 1];
5097 term_windgoto(row, col + coloff);
5098 screen_stop_highlight();
5100 else
5101 screen_attr = 0; /* highlighting has stopped */
5103 #ifdef FEAT_MBYTE
5104 if (enc_dbcs != 0)
5106 /* Check if overwriting a double-byte with a single-byte or
5107 * the other way around requires another character to be
5108 * redrawn. For UTF-8 this isn't needed, because comparing
5109 * ScreenLinesUC[] is sufficient. */
5110 if (char_cells == 1
5111 && col + 1 < endcol
5112 && (*mb_off2cells)(off_to, max_off_to) > 1)
5114 /* Writing a single-cell character over a double-cell
5115 * character: need to redraw the next cell. */
5116 ScreenLines[off_to + 1] = 0;
5117 redraw_next = TRUE;
5119 else if (char_cells == 2
5120 && col + 2 < endcol
5121 && (*mb_off2cells)(off_to, max_off_to) == 1
5122 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
5124 /* Writing the second half of a double-cell character over
5125 * a double-cell character: need to redraw the second
5126 * cell. */
5127 ScreenLines[off_to + 2] = 0;
5128 redraw_next = TRUE;
5131 if (enc_dbcs == DBCS_JPNU)
5132 ScreenLines2[off_to] = ScreenLines2[off_from];
5134 /* When writing a single-width character over a double-width
5135 * character and at the end of the redrawn text, need to clear out
5136 * the right halve of the old character.
5137 * Also required when writing the right halve of a double-width
5138 * char over the left halve of an existing one. */
5139 if (has_mbyte && col + char_cells == endcol
5140 && ((char_cells == 1
5141 && (*mb_off2cells)(off_to, max_off_to) > 1)
5142 || (char_cells == 2
5143 && (*mb_off2cells)(off_to, max_off_to) == 1
5144 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
5145 clear_next = TRUE;
5146 #endif
5148 ScreenLines[off_to] = ScreenLines[off_from];
5149 #ifdef FEAT_MBYTE
5150 if (enc_utf8)
5152 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
5153 if (ScreenLinesUC[off_from] != 0)
5155 int i;
5157 for (i = 0; i < Screen_mco; ++i)
5158 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
5161 if (char_cells == 2)
5162 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
5163 #endif
5165 #if defined(FEAT_GUI) || defined(UNIX)
5166 /* The bold trick makes a single column of pixels appear in the
5167 * next character. When a bold character is removed, the next
5168 * character should be redrawn too. This happens for our own GUI
5169 * and for some xterms. */
5170 if (
5171 # ifdef FEAT_GUI
5172 gui.in_use
5173 # endif
5174 # if defined(FEAT_GUI) && defined(UNIX)
5176 # endif
5177 # ifdef UNIX
5178 term_is_xterm
5179 # endif
5182 hl = ScreenAttrs[off_to];
5183 if (hl > HL_ALL)
5184 hl = syn_attr2attr(hl);
5185 if (hl & HL_BOLD)
5186 redraw_next = TRUE;
5188 #endif
5189 ScreenAttrs[off_to] = ScreenAttrs[off_from];
5190 #ifdef FEAT_MBYTE
5191 /* For simplicity set the attributes of second half of a
5192 * double-wide character equal to the first half. */
5193 if (char_cells == 2)
5194 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
5196 if (enc_dbcs != 0 && char_cells == 2)
5197 screen_char_2(off_to, row, col + coloff);
5198 else
5199 #endif
5200 screen_char(off_to, row, col + coloff);
5202 else if ( p_wiv
5203 #ifdef FEAT_GUI
5204 && !gui.in_use
5205 #endif
5206 && col + coloff > 0)
5208 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
5211 * Don't output stop-highlight when moving the cursor, it will
5212 * stop the highlighting when it should continue.
5214 screen_attr = 0;
5216 else if (screen_attr != 0)
5217 screen_stop_highlight();
5220 off_to += CHAR_CELLS;
5221 off_from += CHAR_CELLS;
5222 col += CHAR_CELLS;
5225 #ifdef FEAT_MBYTE
5226 if (clear_next)
5228 /* Clear the second half of a double-wide character of which the left
5229 * half was overwritten with a single-wide character. */
5230 ScreenLines[off_to] = ' ';
5231 if (enc_utf8)
5232 ScreenLinesUC[off_to] = 0;
5233 screen_char(off_to, row, col + coloff);
5235 #endif
5237 if (clear_width > 0
5238 #ifdef FEAT_RIGHTLEFT
5239 && !rlflag
5240 #endif
5243 #ifdef FEAT_GUI
5244 int startCol = col;
5245 #endif
5247 /* blank out the rest of the line */
5248 while (col < clear_width && ScreenLines[off_to] == ' '
5249 && ScreenAttrs[off_to] == 0
5250 #ifdef FEAT_MBYTE
5251 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5252 #endif
5255 ++off_to;
5256 ++col;
5258 if (col < clear_width)
5260 #ifdef FEAT_GUI
5262 * In the GUI, clearing the rest of the line may leave pixels
5263 * behind if the first character cleared was bold. Some bold
5264 * fonts spill over the left. In this case we redraw the previous
5265 * character too. If we didn't skip any blanks above, then we
5266 * only redraw if the character wasn't already redrawn anyway.
5268 if (gui.in_use && (col > startCol || !redraw_this))
5270 hl = ScreenAttrs[off_to];
5271 if (hl > HL_ALL || (hl & HL_BOLD))
5273 int prev_cells = 1;
5274 # ifdef FEAT_MBYTE
5275 if (enc_utf8)
5276 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
5277 * that its width is 2. */
5278 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
5279 else if (enc_dbcs != 0)
5281 /* find previous character by counting from first
5282 * column and get its width. */
5283 unsigned off = LineOffset[row];
5284 unsigned max_off = LineOffset[row] + screen_Columns;
5286 while (off < off_to)
5288 prev_cells = (*mb_off2cells)(off, max_off);
5289 off += prev_cells;
5293 if (enc_dbcs != 0 && prev_cells > 1)
5294 screen_char_2(off_to - prev_cells, row,
5295 col + coloff - prev_cells);
5296 else
5297 # endif
5298 screen_char(off_to - prev_cells, row,
5299 col + coloff - prev_cells);
5302 #endif
5303 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
5304 ' ', ' ', 0);
5305 #ifdef FEAT_VERTSPLIT
5306 off_to += clear_width - col;
5307 col = clear_width;
5308 #endif
5312 if (clear_width > 0)
5314 #ifdef FEAT_VERTSPLIT
5315 /* For a window that's left of another, draw the separator char. */
5316 if (col + coloff < Columns)
5318 int c;
5320 c = fillchar_vsep(&hl);
5321 if (ScreenLines[off_to] != c
5322 # ifdef FEAT_MBYTE
5323 || (enc_utf8 && (int)ScreenLinesUC[off_to]
5324 != (c >= 0x80 ? c : 0))
5325 # endif
5326 || ScreenAttrs[off_to] != hl)
5328 ScreenLines[off_to] = c;
5329 ScreenAttrs[off_to] = hl;
5330 # ifdef FEAT_MBYTE
5331 if (enc_utf8)
5333 if (c >= 0x80)
5335 ScreenLinesUC[off_to] = c;
5336 ScreenLinesC[0][off_to] = 0;
5338 else
5339 ScreenLinesUC[off_to] = 0;
5341 # endif
5342 screen_char(off_to, row, col + coloff);
5345 else
5346 #endif
5347 LineWraps[row] = FALSE;
5351 #if defined(FEAT_RIGHTLEFT) || defined(PROTO)
5353 * Mirror text "str" for right-left displaying.
5354 * Only works for single-byte characters (e.g., numbers).
5356 void
5357 rl_mirror(str)
5358 char_u *str;
5360 char_u *p1, *p2;
5361 int t;
5363 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
5365 t = *p1;
5366 *p1 = *p2;
5367 *p2 = t;
5370 #endif
5372 #if defined(FEAT_WINDOWS) || defined(PROTO)
5374 * mark all status lines for redraw; used after first :cd
5376 void
5377 status_redraw_all()
5379 win_T *wp;
5381 for (wp = firstwin; wp; wp = wp->w_next)
5382 if (wp->w_status_height)
5384 wp->w_redr_status = TRUE;
5385 redraw_later(VALID);
5390 * mark all status lines of the current buffer for redraw
5392 void
5393 status_redraw_curbuf()
5395 win_T *wp;
5397 for (wp = firstwin; wp; wp = wp->w_next)
5398 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
5400 wp->w_redr_status = TRUE;
5401 redraw_later(VALID);
5406 * Redraw all status lines that need to be redrawn.
5408 void
5409 redraw_statuslines()
5411 win_T *wp;
5413 for (wp = firstwin; wp; wp = wp->w_next)
5414 if (wp->w_redr_status)
5415 win_redr_status(wp);
5416 if (redraw_tabline)
5417 draw_tabline();
5419 #endif
5421 #if (defined(FEAT_WILDMENU) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
5423 * Redraw all status lines at the bottom of frame "frp".
5425 void
5426 win_redraw_last_status(frp)
5427 frame_T *frp;
5429 if (frp->fr_layout == FR_LEAF)
5430 frp->fr_win->w_redr_status = TRUE;
5431 else if (frp->fr_layout == FR_ROW)
5433 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
5434 win_redraw_last_status(frp);
5436 else /* frp->fr_layout == FR_COL */
5438 frp = frp->fr_child;
5439 while (frp->fr_next != NULL)
5440 frp = frp->fr_next;
5441 win_redraw_last_status(frp);
5444 #endif
5446 #ifdef FEAT_VERTSPLIT
5448 * Draw the verticap separator right of window "wp" starting with line "row".
5450 static void
5451 draw_vsep_win(wp, row)
5452 win_T *wp;
5453 int row;
5455 int hl;
5456 int c;
5458 if (wp->w_vsep_width)
5460 /* draw the vertical separator right of this window */
5461 c = fillchar_vsep(&hl);
5462 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
5463 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
5464 c, ' ', hl);
5467 #endif
5469 #ifdef FEAT_WILDMENU
5470 static int status_match_len __ARGS((expand_T *xp, char_u *s));
5471 static int skip_status_match_char __ARGS((expand_T *xp, char_u *s));
5474 * Get the length of an item as it will be shown in the status line.
5476 static int
5477 status_match_len(xp, s)
5478 expand_T *xp;
5479 char_u *s;
5481 int len = 0;
5483 #ifdef FEAT_MENU
5484 int emenu = (xp->xp_context == EXPAND_MENUS
5485 || xp->xp_context == EXPAND_MENUNAMES);
5487 /* Check for menu separators - replace with '|'. */
5488 if (emenu && menu_is_separator(s))
5489 return 1;
5490 #endif
5492 while (*s != NUL)
5494 s += skip_status_match_char(xp, s);
5495 len += ptr2cells(s);
5496 mb_ptr_adv(s);
5499 return len;
5503 * Return the number of characters that should be skipped in a status match.
5504 * These are backslashes used for escaping. Do show backslashes in help tags.
5506 static int
5507 skip_status_match_char(xp, s)
5508 expand_T *xp;
5509 char_u *s;
5511 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
5512 #ifdef FEAT_MENU
5513 || ((xp->xp_context == EXPAND_MENUS
5514 || xp->xp_context == EXPAND_MENUNAMES)
5515 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
5516 #endif
5519 #ifndef BACKSLASH_IN_FILENAME
5520 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
5521 return 2;
5522 #endif
5523 return 1;
5525 return 0;
5529 * Show wildchar matches in the status line.
5530 * Show at least the "match" item.
5531 * We start at item 'first_match' in the list and show all matches that fit.
5533 * If inversion is possible we use it. Else '=' characters are used.
5535 void
5536 win_redr_status_matches(xp, num_matches, matches, match, showtail)
5537 expand_T *xp;
5538 int num_matches;
5539 char_u **matches; /* list of matches */
5540 int match;
5541 int showtail;
5543 #define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
5544 int row;
5545 char_u *buf;
5546 int len;
5547 int clen; /* length in screen cells */
5548 int fillchar;
5549 int attr;
5550 int i;
5551 int highlight = TRUE;
5552 char_u *selstart = NULL;
5553 int selstart_col = 0;
5554 char_u *selend = NULL;
5555 static int first_match = 0;
5556 int add_left = FALSE;
5557 char_u *s;
5558 #ifdef FEAT_MENU
5559 int emenu;
5560 #endif
5561 #if defined(FEAT_MBYTE) || defined(FEAT_MENU)
5562 int l;
5563 #endif
5565 if (matches == NULL) /* interrupted completion? */
5566 return;
5568 #ifdef FEAT_MBYTE
5569 if (has_mbyte)
5570 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
5571 else
5572 #endif
5573 buf = alloc((unsigned)Columns + 1);
5574 if (buf == NULL)
5575 return;
5577 if (match == -1) /* don't show match but original text */
5579 match = 0;
5580 highlight = FALSE;
5582 /* count 1 for the ending ">" */
5583 clen = status_match_len(xp, L_MATCH(match)) + 3;
5584 if (match == 0)
5585 first_match = 0;
5586 else if (match < first_match)
5588 /* jumping left, as far as we can go */
5589 first_match = match;
5590 add_left = TRUE;
5592 else
5594 /* check if match fits on the screen */
5595 for (i = first_match; i < match; ++i)
5596 clen += status_match_len(xp, L_MATCH(i)) + 2;
5597 if (first_match > 0)
5598 clen += 2;
5599 /* jumping right, put match at the left */
5600 if ((long)clen > Columns)
5602 first_match = match;
5603 /* if showing the last match, we can add some on the left */
5604 clen = 2;
5605 for (i = match; i < num_matches; ++i)
5607 clen += status_match_len(xp, L_MATCH(i)) + 2;
5608 if ((long)clen >= Columns)
5609 break;
5611 if (i == num_matches)
5612 add_left = TRUE;
5615 if (add_left)
5616 while (first_match > 0)
5618 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
5619 if ((long)clen >= Columns)
5620 break;
5621 --first_match;
5624 fillchar = fillchar_status(&attr, TRUE);
5626 if (first_match == 0)
5628 *buf = NUL;
5629 len = 0;
5631 else
5633 STRCPY(buf, "< ");
5634 len = 2;
5636 clen = len;
5638 i = first_match;
5639 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
5641 if (i == match)
5643 selstart = buf + len;
5644 selstart_col = clen;
5647 s = L_MATCH(i);
5648 /* Check for menu separators - replace with '|' */
5649 #ifdef FEAT_MENU
5650 emenu = (xp->xp_context == EXPAND_MENUS
5651 || xp->xp_context == EXPAND_MENUNAMES);
5652 if (emenu && menu_is_separator(s))
5654 STRCPY(buf + len, transchar('|'));
5655 l = (int)STRLEN(buf + len);
5656 len += l;
5657 clen += l;
5659 else
5660 #endif
5661 for ( ; *s != NUL; ++s)
5663 s += skip_status_match_char(xp, s);
5664 clen += ptr2cells(s);
5665 #ifdef FEAT_MBYTE
5666 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
5668 STRNCPY(buf + len, s, l);
5669 s += l - 1;
5670 len += l;
5672 else
5673 #endif
5675 STRCPY(buf + len, transchar_byte(*s));
5676 len += (int)STRLEN(buf + len);
5679 if (i == match)
5680 selend = buf + len;
5682 *(buf + len++) = ' ';
5683 *(buf + len++) = ' ';
5684 clen += 2;
5685 if (++i == num_matches)
5686 break;
5689 if (i != num_matches)
5691 *(buf + len++) = '>';
5692 ++clen;
5695 buf[len] = NUL;
5697 row = cmdline_row - 1;
5698 if (row >= 0)
5700 if (wild_menu_showing == 0)
5702 if (msg_scrolled > 0)
5704 /* Put the wildmenu just above the command line. If there is
5705 * no room, scroll the screen one line up. */
5706 if (cmdline_row == Rows - 1)
5708 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
5709 ++msg_scrolled;
5711 else
5713 ++cmdline_row;
5714 ++row;
5716 wild_menu_showing = WM_SCROLLED;
5718 else
5720 /* Create status line if needed by setting 'laststatus' to 2.
5721 * Set 'winminheight' to zero to avoid that the window is
5722 * resized. */
5723 if (lastwin->w_status_height == 0)
5725 save_p_ls = p_ls;
5726 save_p_wmh = p_wmh;
5727 p_ls = 2;
5728 p_wmh = 0;
5729 last_status(FALSE);
5731 wild_menu_showing = WM_SHOWN;
5735 screen_puts(buf, row, 0, attr);
5736 if (selstart != NULL && highlight)
5738 *selend = NUL;
5739 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
5742 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
5745 #ifdef FEAT_VERTSPLIT
5746 win_redraw_last_status(topframe);
5747 #else
5748 lastwin->w_redr_status = TRUE;
5749 #endif
5750 vim_free(buf);
5752 #endif
5754 #if defined(FEAT_WINDOWS) || defined(PROTO)
5756 * Redraw the status line of window wp.
5758 * If inversion is possible we use it. Else '=' characters are used.
5760 void
5761 win_redr_status(wp)
5762 win_T *wp;
5764 int row;
5765 char_u *p;
5766 int len;
5767 int fillchar;
5768 int attr;
5769 int this_ru_col;
5771 wp->w_redr_status = FALSE;
5772 if (wp->w_status_height == 0)
5774 /* no status line, can only be last window */
5775 redraw_cmdline = TRUE;
5777 else if (!redrawing()
5778 #ifdef FEAT_INS_EXPAND
5779 /* don't update status line when popup menu is visible and may be
5780 * drawn over it */
5781 || pum_visible()
5782 #endif
5785 /* Don't redraw right now, do it later. */
5786 wp->w_redr_status = TRUE;
5788 #ifdef FEAT_STL_OPT
5789 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
5791 /* redraw custom status line */
5792 redraw_custum_statusline(wp);
5794 #endif
5795 else
5797 fillchar = fillchar_status(&attr, wp == curwin);
5799 get_trans_bufname(wp->w_buffer);
5800 p = NameBuff;
5801 len = (int)STRLEN(p);
5803 if (wp->w_buffer->b_help
5804 #ifdef FEAT_QUICKFIX
5805 || wp->w_p_pvw
5806 #endif
5807 || bufIsChanged(wp->w_buffer)
5808 || wp->w_buffer->b_p_ro)
5809 *(p + len++) = ' ';
5810 if (wp->w_buffer->b_help)
5812 STRCPY(p + len, _("[Help]"));
5813 len += (int)STRLEN(p + len);
5815 #ifdef FEAT_QUICKFIX
5816 if (wp->w_p_pvw)
5818 STRCPY(p + len, _("[Preview]"));
5819 len += (int)STRLEN(p + len);
5821 #endif
5822 if (bufIsChanged(wp->w_buffer))
5824 STRCPY(p + len, "[+]");
5825 len += 3;
5827 if (wp->w_buffer->b_p_ro)
5829 STRCPY(p + len, "[RO]");
5830 len += 4;
5833 #ifndef FEAT_VERTSPLIT
5834 this_ru_col = ru_col;
5835 if (this_ru_col < (Columns + 1) / 2)
5836 this_ru_col = (Columns + 1) / 2;
5837 #else
5838 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
5839 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
5840 this_ru_col = (W_WIDTH(wp) + 1) / 2;
5841 if (this_ru_col <= 1)
5843 p = (char_u *)"<"; /* No room for file name! */
5844 len = 1;
5846 else
5847 #endif
5848 #ifdef FEAT_MBYTE
5849 if (has_mbyte)
5851 int clen = 0, i;
5853 /* Count total number of display cells. */
5854 for (i = 0; p[i] != NUL; i += (*mb_ptr2len)(p + i))
5855 clen += (*mb_ptr2cells)(p + i);
5856 /* Find first character that will fit.
5857 * Going from start to end is much faster for DBCS. */
5858 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
5859 i += (*mb_ptr2len)(p + i))
5860 clen -= (*mb_ptr2cells)(p + i);
5861 len = clen;
5862 if (i > 0)
5864 p = p + i - 1;
5865 *p = '<';
5866 ++len;
5870 else
5871 #endif
5872 if (len > this_ru_col - 1)
5874 p += len - (this_ru_col - 1);
5875 *p = '<';
5876 len = this_ru_col - 1;
5879 row = W_WINROW(wp) + wp->w_height;
5880 screen_puts(p, row, W_WINCOL(wp), attr);
5881 screen_fill(row, row + 1, len + W_WINCOL(wp),
5882 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
5884 if (get_keymap_str(wp, NameBuff, MAXPATHL)
5885 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
5886 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
5887 - 1 + W_WINCOL(wp)), attr);
5889 #ifdef FEAT_CMDL_INFO
5890 win_redr_ruler(wp, TRUE);
5891 #endif
5894 #ifdef FEAT_VERTSPLIT
5896 * May need to draw the character below the vertical separator.
5898 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
5900 if (stl_connected(wp))
5901 fillchar = fillchar_status(&attr, wp == curwin);
5902 else
5903 fillchar = fillchar_vsep(&attr);
5904 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
5905 attr);
5907 #endif
5910 #ifdef FEAT_STL_OPT
5912 * Redraw the status line according to 'statusline' and take care of any
5913 * errors encountered.
5915 static void
5916 redraw_custum_statusline(wp)
5917 win_T *wp;
5919 int save_called_emsg = called_emsg;
5921 called_emsg = FALSE;
5922 win_redr_custom(wp, FALSE);
5923 if (called_emsg)
5924 set_string_option_direct((char_u *)"statusline", -1,
5925 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
5926 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
5927 called_emsg |= save_called_emsg;
5929 #endif
5931 # ifdef FEAT_VERTSPLIT
5933 * Return TRUE if the status line of window "wp" is connected to the status
5934 * line of the window right of it. If not, then it's a vertical separator.
5935 * Only call if (wp->w_vsep_width != 0).
5938 stl_connected(wp)
5939 win_T *wp;
5941 frame_T *fr;
5943 fr = wp->w_frame;
5944 while (fr->fr_parent != NULL)
5946 if (fr->fr_parent->fr_layout == FR_COL)
5948 if (fr->fr_next != NULL)
5949 break;
5951 else
5953 if (fr->fr_next != NULL)
5954 return TRUE;
5956 fr = fr->fr_parent;
5958 return FALSE;
5960 # endif
5962 #endif /* FEAT_WINDOWS */
5964 #if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
5966 * Get the value to show for the language mappings, active 'keymap'.
5969 get_keymap_str(wp, buf, len)
5970 win_T *wp;
5971 char_u *buf; /* buffer for the result */
5972 int len; /* length of buffer */
5974 char_u *p;
5976 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
5977 return FALSE;
5980 #ifdef FEAT_EVAL
5981 buf_T *old_curbuf = curbuf;
5982 win_T *old_curwin = curwin;
5983 char_u *s;
5985 curbuf = wp->w_buffer;
5986 curwin = wp;
5987 STRCPY(buf, "b:keymap_name"); /* must be writable */
5988 ++emsg_skip;
5989 s = p = eval_to_string(buf, NULL, FALSE);
5990 --emsg_skip;
5991 curbuf = old_curbuf;
5992 curwin = old_curwin;
5993 if (p == NULL || *p == NUL)
5994 #endif
5996 #ifdef FEAT_KEYMAP
5997 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
5998 p = wp->w_buffer->b_p_keymap;
5999 else
6000 #endif
6001 p = (char_u *)"lang";
6003 if ((int)(STRLEN(p) + 3) < len)
6004 sprintf((char *)buf, "<%s>", p);
6005 else
6006 buf[0] = NUL;
6007 #ifdef FEAT_EVAL
6008 vim_free(s);
6009 #endif
6011 return buf[0] != NUL;
6013 #endif
6015 #if defined(FEAT_STL_OPT) || defined(PROTO)
6017 * Redraw the status line or ruler of window "wp".
6018 * When "wp" is NULL redraw the tab pages line from 'tabline'.
6020 static void
6021 win_redr_custom(wp, draw_ruler)
6022 win_T *wp;
6023 int draw_ruler; /* TRUE or FALSE */
6025 int attr;
6026 int curattr;
6027 int row;
6028 int col = 0;
6029 int maxwidth;
6030 int width;
6031 int n;
6032 int len;
6033 int fillchar;
6034 char_u buf[MAXPATHL];
6035 char_u *p;
6036 struct stl_hlrec hltab[STL_MAX_ITEM];
6037 struct stl_hlrec tabtab[STL_MAX_ITEM];
6038 int use_sandbox = FALSE;
6040 /* setup environment for the task at hand */
6041 if (wp == NULL)
6043 /* Use 'tabline'. Always at the first line of the screen. */
6044 p = p_tal;
6045 row = 0;
6046 fillchar = ' ';
6047 attr = hl_attr(HLF_TPF);
6048 maxwidth = Columns;
6049 # ifdef FEAT_EVAL
6050 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
6051 # endif
6053 else
6055 row = W_WINROW(wp) + wp->w_height;
6056 fillchar = fillchar_status(&attr, wp == curwin);
6057 maxwidth = W_WIDTH(wp);
6059 if (draw_ruler)
6061 p = p_ruf;
6062 /* advance past any leading group spec - implicit in ru_col */
6063 if (*p == '%')
6065 if (*++p == '-')
6066 p++;
6067 if (atoi((char *) p))
6068 while (VIM_ISDIGIT(*p))
6069 p++;
6070 if (*p++ != '(')
6071 p = p_ruf;
6073 #ifdef FEAT_VERTSPLIT
6074 col = ru_col - (Columns - W_WIDTH(wp));
6075 if (col < (W_WIDTH(wp) + 1) / 2)
6076 col = (W_WIDTH(wp) + 1) / 2;
6077 #else
6078 col = ru_col;
6079 if (col > (Columns + 1) / 2)
6080 col = (Columns + 1) / 2;
6081 #endif
6082 maxwidth = W_WIDTH(wp) - col;
6083 #ifdef FEAT_WINDOWS
6084 if (!wp->w_status_height)
6085 #endif
6087 row = Rows - 1;
6088 --maxwidth; /* writing in last column may cause scrolling */
6089 fillchar = ' ';
6090 attr = 0;
6093 # ifdef FEAT_EVAL
6094 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
6095 # endif
6097 else
6099 if (*wp->w_p_stl != NUL)
6100 p = wp->w_p_stl;
6101 else
6102 p = p_stl;
6103 # ifdef FEAT_EVAL
6104 use_sandbox = was_set_insecurely((char_u *)"statusline",
6105 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
6106 # endif
6109 #ifdef FEAT_VERTSPLIT
6110 col += W_WINCOL(wp);
6111 #endif
6114 if (maxwidth <= 0)
6115 return;
6117 width = build_stl_str_hl(wp == NULL ? curwin : wp,
6118 buf, sizeof(buf),
6119 p, use_sandbox,
6120 fillchar, maxwidth, hltab, tabtab);
6121 len = (int)STRLEN(buf);
6123 while (width < maxwidth && len < (int)sizeof(buf) - 1)
6125 #ifdef FEAT_MBYTE
6126 len += (*mb_char2bytes)(fillchar, buf + len);
6127 #else
6128 buf[len++] = fillchar;
6129 #endif
6130 ++width;
6132 buf[len] = NUL;
6135 * Draw each snippet with the specified highlighting.
6137 curattr = attr;
6138 p = buf;
6139 for (n = 0; hltab[n].start != NULL; n++)
6141 len = (int)(hltab[n].start - p);
6142 screen_puts_len(p, len, row, col, curattr);
6143 col += vim_strnsize(p, len);
6144 p = hltab[n].start;
6146 if (hltab[n].userhl == 0)
6147 curattr = attr;
6148 else if (hltab[n].userhl < 0)
6149 curattr = syn_id2attr(-hltab[n].userhl);
6150 #ifdef FEAT_WINDOWS
6151 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
6152 curattr = highlight_stlnc[hltab[n].userhl - 1];
6153 #endif
6154 else
6155 curattr = highlight_user[hltab[n].userhl - 1];
6157 screen_puts(p, row, col, curattr);
6159 if (wp == NULL)
6161 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
6162 col = 0;
6163 len = 0;
6164 p = buf;
6165 fillchar = 0;
6166 for (n = 0; tabtab[n].start != NULL; n++)
6168 len += vim_strnsize(p, (int)(tabtab[n].start - p));
6169 while (col < len)
6170 TabPageIdxs[col++] = fillchar;
6171 p = tabtab[n].start;
6172 fillchar = tabtab[n].userhl;
6174 while (col < Columns)
6175 TabPageIdxs[col++] = fillchar;
6179 #endif /* FEAT_STL_OPT */
6182 * Output a single character directly to the screen and update ScreenLines.
6184 void
6185 screen_putchar(c, row, col, attr)
6186 int c;
6187 int row, col;
6188 int attr;
6190 #ifdef FEAT_MBYTE
6191 char_u buf[MB_MAXBYTES + 1];
6193 buf[(*mb_char2bytes)(c, buf)] = NUL;
6194 #else
6195 char_u buf[2];
6197 buf[0] = c;
6198 buf[1] = NUL;
6199 #endif
6200 screen_puts(buf, row, col, attr);
6204 * Get a single character directly from ScreenLines into "bytes[]".
6205 * Also return its attribute in *attrp;
6207 void
6208 screen_getbytes(row, col, bytes, attrp)
6209 int row, col;
6210 char_u *bytes;
6211 int *attrp;
6213 unsigned off;
6215 /* safety check */
6216 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
6218 off = LineOffset[row] + col;
6219 *attrp = ScreenAttrs[off];
6220 bytes[0] = ScreenLines[off];
6221 bytes[1] = NUL;
6223 #ifdef FEAT_MBYTE
6224 if (enc_utf8 && ScreenLinesUC[off] != 0)
6225 bytes[utfc_char2bytes(off, bytes)] = NUL;
6226 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6228 bytes[0] = ScreenLines[off];
6229 bytes[1] = ScreenLines2[off];
6230 bytes[2] = NUL;
6232 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
6234 bytes[1] = ScreenLines[off + 1];
6235 bytes[2] = NUL;
6237 #endif
6241 #ifdef FEAT_MBYTE
6242 static int screen_comp_differs __ARGS((int, int*));
6245 * Return TRUE if composing characters for screen posn "off" differs from
6246 * composing characters in "u8cc".
6248 static int
6249 screen_comp_differs(off, u8cc)
6250 int off;
6251 int *u8cc;
6253 int i;
6255 for (i = 0; i < Screen_mco; ++i)
6257 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
6258 return TRUE;
6259 if (u8cc[i] == 0)
6260 break;
6262 return FALSE;
6264 #endif
6267 * Put string '*text' on the screen at position 'row' and 'col', with
6268 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
6269 * Note: only outputs within one row, message is truncated at screen boundary!
6270 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
6272 void
6273 screen_puts(text, row, col, attr)
6274 char_u *text;
6275 int row;
6276 int col;
6277 int attr;
6279 screen_puts_len(text, -1, row, col, attr);
6283 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
6284 * a NUL.
6286 void
6287 screen_puts_len(text, len, row, col, attr)
6288 char_u *text;
6289 int len;
6290 int row;
6291 int col;
6292 int attr;
6294 unsigned off;
6295 char_u *ptr = text;
6296 int c;
6297 #ifdef FEAT_MBYTE
6298 unsigned max_off;
6299 int mbyte_blen = 1;
6300 int mbyte_cells = 1;
6301 int u8c = 0;
6302 int u8cc[MAX_MCO];
6303 int clear_next_cell = FALSE;
6304 # ifdef FEAT_ARABIC
6305 int prev_c = 0; /* previous Arabic character */
6306 int pc, nc, nc1;
6307 int pcc[MAX_MCO];
6308 # endif
6309 #endif
6310 #if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6311 int force_redraw_this;
6312 int force_redraw_next = FALSE;
6313 #endif
6314 int need_redraw;
6316 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
6317 return;
6318 off = LineOffset[row] + col;
6320 #ifdef FEAT_MBYTE
6321 /* When drawing over the right halve of a double-wide char clear out the
6322 * left halve. Only needed in a terminal. */
6323 if (has_mbyte && col > 0 && col < screen_Columns
6324 # ifdef FEAT_GUI
6325 && !gui.in_use
6326 # endif
6327 && mb_fix_col(col, row) != col)
6329 ScreenLines[off - 1] = ' ';
6330 ScreenAttrs[off - 1] = 0;
6331 if (enc_utf8)
6333 ScreenLinesUC[off - 1] = 0;
6334 ScreenLinesC[0][off - 1] = 0;
6336 /* redraw the previous cell, make it empty */
6337 screen_char(off - 1, row, col - 1);
6338 /* force the cell at "col" to be redrawn */
6339 force_redraw_next = TRUE;
6341 #endif
6343 #ifdef FEAT_MBYTE
6344 max_off = LineOffset[row] + screen_Columns;
6345 #endif
6346 while (col < screen_Columns
6347 && (len < 0 || (int)(ptr - text) < len)
6348 && *ptr != NUL)
6350 c = *ptr;
6351 #ifdef FEAT_MBYTE
6352 /* check if this is the first byte of a multibyte */
6353 if (has_mbyte)
6355 if (enc_utf8 && len > 0)
6356 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
6357 else
6358 mbyte_blen = (*mb_ptr2len)(ptr);
6359 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6360 mbyte_cells = 1;
6361 else if (enc_dbcs != 0)
6362 mbyte_cells = mbyte_blen;
6363 else /* enc_utf8 */
6365 if (len >= 0)
6366 u8c = utfc_ptr2char_len(ptr, u8cc,
6367 (int)((text + len) - ptr));
6368 else
6369 u8c = utfc_ptr2char(ptr, u8cc);
6370 mbyte_cells = utf_char2cells(u8c);
6371 # ifdef UNICODE16
6372 /* Non-BMP character: display as ? or fullwidth ?. */
6373 if (u8c >= 0x10000)
6375 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
6376 if (attr == 0)
6377 attr = hl_attr(HLF_8);
6379 # endif
6380 # ifdef FEAT_ARABIC
6381 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
6383 /* Do Arabic shaping. */
6384 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
6386 /* Past end of string to be displayed. */
6387 nc = NUL;
6388 nc1 = NUL;
6390 else
6392 nc = utfc_ptr2char(ptr + mbyte_blen, pcc);
6393 nc1 = pcc[0];
6395 pc = prev_c;
6396 prev_c = u8c;
6397 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
6399 else
6400 prev_c = u8c;
6401 # endif
6404 #endif
6406 #if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6407 force_redraw_this = force_redraw_next;
6408 force_redraw_next = FALSE;
6409 #endif
6411 need_redraw = ScreenLines[off] != c
6412 #ifdef FEAT_MBYTE
6413 || (mbyte_cells == 2
6414 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
6415 || (enc_dbcs == DBCS_JPNU
6416 && c == 0x8e
6417 && ScreenLines2[off] != ptr[1])
6418 || (enc_utf8
6419 && (ScreenLinesUC[off] != (u8char_T)(c >= 0x80 ? u8c : 0)
6420 || screen_comp_differs(off, u8cc)))
6421 #endif
6422 || ScreenAttrs[off] != attr
6423 || exmode_active;
6425 if (need_redraw
6426 #if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6427 || force_redraw_this
6428 #endif
6431 #if defined(FEAT_GUI) || defined(UNIX)
6432 /* The bold trick makes a single row of pixels appear in the next
6433 * character. When a bold character is removed, the next
6434 * character should be redrawn too. This happens for our own GUI
6435 * and for some xterms. */
6436 if (need_redraw && ScreenLines[off] != ' ' && (
6437 # ifdef FEAT_GUI
6438 gui.in_use
6439 # endif
6440 # if defined(FEAT_GUI) && defined(UNIX)
6442 # endif
6443 # ifdef UNIX
6444 term_is_xterm
6445 # endif
6448 int n = ScreenAttrs[off];
6450 if (n > HL_ALL)
6451 n = syn_attr2attr(n);
6452 if (n & HL_BOLD)
6453 force_redraw_next = TRUE;
6455 #endif
6456 #ifdef FEAT_MBYTE
6457 /* When at the end of the text and overwriting a two-cell
6458 * character with a one-cell character, need to clear the next
6459 * cell. Also when overwriting the left halve of a two-cell char
6460 * with the right halve of a two-cell char. Do this only once
6461 * (mb_off2cells() may return 2 on the right halve). */
6462 if (clear_next_cell)
6463 clear_next_cell = FALSE;
6464 else if (has_mbyte
6465 && (len < 0 ? ptr[mbyte_blen] == NUL
6466 : ptr + mbyte_blen >= text + len)
6467 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
6468 || (mbyte_cells == 2
6469 && (*mb_off2cells)(off, max_off) == 1
6470 && (*mb_off2cells)(off + 1, max_off) > 1)))
6471 clear_next_cell = TRUE;
6473 /* Make sure we never leave a second byte of a double-byte behind,
6474 * it confuses mb_off2cells(). */
6475 if (enc_dbcs
6476 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
6477 || (mbyte_cells == 2
6478 && (*mb_off2cells)(off, max_off) == 1
6479 && (*mb_off2cells)(off + 1, max_off) > 1)))
6480 ScreenLines[off + mbyte_blen] = 0;
6481 #endif
6482 ScreenLines[off] = c;
6483 ScreenAttrs[off] = attr;
6484 #ifdef FEAT_MBYTE
6485 if (enc_utf8)
6487 if (c < 0x80 && u8cc[0] == 0)
6488 ScreenLinesUC[off] = 0;
6489 else
6491 int i;
6493 ScreenLinesUC[off] = u8c;
6494 for (i = 0; i < Screen_mco; ++i)
6496 ScreenLinesC[i][off] = u8cc[i];
6497 if (u8cc[i] == 0)
6498 break;
6501 if (mbyte_cells == 2)
6503 ScreenLines[off + 1] = 0;
6504 ScreenAttrs[off + 1] = attr;
6506 screen_char(off, row, col);
6508 else if (mbyte_cells == 2)
6510 ScreenLines[off + 1] = ptr[1];
6511 ScreenAttrs[off + 1] = attr;
6512 screen_char_2(off, row, col);
6514 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6516 ScreenLines2[off] = ptr[1];
6517 screen_char(off, row, col);
6519 else
6520 #endif
6521 screen_char(off, row, col);
6523 #ifdef FEAT_MBYTE
6524 if (has_mbyte)
6526 off += mbyte_cells;
6527 col += mbyte_cells;
6528 ptr += mbyte_blen;
6529 if (clear_next_cell)
6530 ptr = (char_u *)" ";
6532 else
6533 #endif
6535 ++off;
6536 ++col;
6537 ++ptr;
6541 #if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6542 /* If we detected the next character needs to be redrawn, but the text
6543 * doesn't extend up to there, update the character here. */
6544 if (force_redraw_next && col < screen_Columns)
6546 # ifdef FEAT_MBYTE
6547 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
6548 screen_char_2(off, row, col);
6549 else
6550 # endif
6551 screen_char(off, row, col);
6553 #endif
6556 #ifdef FEAT_SEARCH_EXTRA
6558 * Prepare for 'hlsearch' highlighting.
6560 static void
6561 start_search_hl()
6563 if (p_hls && !no_hlsearch)
6565 last_pat_prog(&search_hl.rm);
6566 search_hl.attr = hl_attr(HLF_L);
6567 # ifdef FEAT_RELTIME
6568 /* Set the time limit to 'redrawtime'. */
6569 profile_setlimit(p_rdt, &search_hl.tm);
6570 # endif
6575 * Clean up for 'hlsearch' highlighting.
6577 static void
6578 end_search_hl()
6580 if (search_hl.rm.regprog != NULL)
6582 vim_free(search_hl.rm.regprog);
6583 search_hl.rm.regprog = NULL;
6588 * Advance to the match in window "wp" line "lnum" or past it.
6590 static void
6591 prepare_search_hl(wp, lnum)
6592 win_T *wp;
6593 linenr_T lnum;
6595 matchitem_T *cur; /* points to the match list */
6596 match_T *shl; /* points to search_hl or a match */
6597 int shl_flag; /* flag to indicate whether search_hl
6598 has been processed or not */
6599 int n;
6602 * When using a multi-line pattern, start searching at the top
6603 * of the window or just after a closed fold.
6604 * Do this both for search_hl and the match list.
6606 cur = wp->w_match_head;
6607 shl_flag = FALSE;
6608 while (cur != NULL || shl_flag == FALSE)
6610 if (shl_flag == FALSE)
6612 shl = &search_hl;
6613 shl_flag = TRUE;
6615 else
6616 shl = &cur->hl;
6617 if (shl->rm.regprog != NULL
6618 && shl->lnum == 0
6619 && re_multiline(shl->rm.regprog))
6621 if (shl->first_lnum == 0)
6623 # ifdef FEAT_FOLDING
6624 for (shl->first_lnum = lnum;
6625 shl->first_lnum > wp->w_topline; --shl->first_lnum)
6626 if (hasFoldingWin(wp, shl->first_lnum - 1,
6627 NULL, NULL, TRUE, NULL))
6628 break;
6629 # else
6630 shl->first_lnum = wp->w_topline;
6631 # endif
6633 n = 0;
6634 while (shl->first_lnum < lnum && shl->rm.regprog != NULL)
6636 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n);
6637 if (shl->lnum != 0)
6639 shl->first_lnum = shl->lnum
6640 + shl->rm.endpos[0].lnum
6641 - shl->rm.startpos[0].lnum;
6642 n = shl->rm.endpos[0].col;
6644 else
6646 ++shl->first_lnum;
6647 n = 0;
6651 if (shl != &search_hl && cur != NULL)
6652 cur = cur->next;
6657 * Search for a next 'hlsearch' or match.
6658 * Uses shl->buf.
6659 * Sets shl->lnum and shl->rm contents.
6660 * Note: Assumes a previous match is always before "lnum", unless
6661 * shl->lnum is zero.
6662 * Careful: Any pointers for buffer lines will become invalid.
6664 static void
6665 next_search_hl(win, shl, lnum, mincol)
6666 win_T *win;
6667 match_T *shl; /* points to search_hl or a match */
6668 linenr_T lnum;
6669 colnr_T mincol; /* minimal column for a match */
6671 linenr_T l;
6672 colnr_T matchcol;
6673 long nmatched;
6675 if (shl->lnum != 0)
6677 /* Check for three situations:
6678 * 1. If the "lnum" is below a previous match, start a new search.
6679 * 2. If the previous match includes "mincol", use it.
6680 * 3. Continue after the previous match.
6682 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
6683 if (lnum > l)
6684 shl->lnum = 0;
6685 else if (lnum < l || shl->rm.endpos[0].col > mincol)
6686 return;
6690 * Repeat searching for a match until one is found that includes "mincol"
6691 * or none is found in this line.
6693 called_emsg = FALSE;
6694 for (;;)
6696 #ifdef FEAT_RELTIME
6697 /* Stop searching after passing the time limit. */
6698 if (profile_passed_limit(&(shl->tm)))
6700 shl->lnum = 0; /* no match found in time */
6701 break;
6703 #endif
6704 /* Three situations:
6705 * 1. No useful previous match: search from start of line.
6706 * 2. Not Vi compatible or empty match: continue at next character.
6707 * Break the loop if this is beyond the end of the line.
6708 * 3. Vi compatible searching: continue at end of previous match.
6710 if (shl->lnum == 0)
6711 matchcol = 0;
6712 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
6713 || (shl->rm.endpos[0].lnum == 0
6714 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
6716 char_u *ml;
6718 matchcol = shl->rm.startpos[0].col;
6719 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
6720 if (*ml == NUL)
6722 ++matchcol;
6723 shl->lnum = 0;
6724 break;
6726 #ifdef FEAT_MBYTE
6727 if (has_mbyte)
6728 matchcol += mb_ptr2len(ml);
6729 else
6730 #endif
6731 ++matchcol;
6733 else
6734 matchcol = shl->rm.endpos[0].col;
6736 shl->lnum = lnum;
6737 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum, matchcol,
6738 #ifdef FEAT_RELTIME
6739 &(shl->tm)
6740 #else
6741 NULL
6742 #endif
6744 if (called_emsg)
6746 /* Error while handling regexp: stop using this regexp. */
6747 if (shl == &search_hl)
6749 /* don't free regprog in the match list, it's a copy */
6750 vim_free(shl->rm.regprog);
6751 no_hlsearch = TRUE;
6753 shl->rm.regprog = NULL;
6754 shl->lnum = 0;
6755 got_int = FALSE; /* avoid the "Type :quit to exit Vim" message */
6756 break;
6758 if (nmatched == 0)
6760 shl->lnum = 0; /* no match found */
6761 break;
6763 if (shl->rm.startpos[0].lnum > 0
6764 || shl->rm.startpos[0].col >= mincol
6765 || nmatched > 1
6766 || shl->rm.endpos[0].col > mincol)
6768 shl->lnum += shl->rm.startpos[0].lnum;
6769 break; /* useful match found */
6773 #endif
6775 static void
6776 screen_start_highlight(attr)
6777 int attr;
6779 attrentry_T *aep = NULL;
6781 screen_attr = attr;
6782 if (full_screen
6783 #ifdef WIN3264
6784 && termcap_active
6785 #endif
6788 #ifdef FEAT_GUI
6789 if (gui.in_use)
6791 char buf[20];
6793 /* The GUI handles this internally. */
6794 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
6795 OUT_STR(buf);
6797 else
6798 #endif
6800 if (attr > HL_ALL) /* special HL attr. */
6802 if (t_colors > 1)
6803 aep = syn_cterm_attr2entry(attr);
6804 else
6805 aep = syn_term_attr2entry(attr);
6806 if (aep == NULL) /* did ":syntax clear" */
6807 attr = 0;
6808 else
6809 attr = aep->ae_attr;
6811 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
6812 out_str(T_MD);
6813 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
6814 && cterm_normal_fg_bold)
6815 /* If the Normal FG color has BOLD attribute and the new HL
6816 * has a FG color defined, clear BOLD. */
6817 out_str(T_ME);
6818 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
6819 out_str(T_SO);
6820 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
6821 /* underline or undercurl */
6822 out_str(T_US);
6823 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
6824 out_str(T_CZH);
6825 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
6826 out_str(T_MR);
6829 * Output the color or start string after bold etc., in case the
6830 * bold etc. override the color setting.
6832 if (aep != NULL)
6834 if (t_colors > 1)
6836 if (aep->ae_u.cterm.fg_color)
6837 term_fg_color(aep->ae_u.cterm.fg_color - 1);
6838 if (aep->ae_u.cterm.bg_color)
6839 term_bg_color(aep->ae_u.cterm.bg_color - 1);
6841 else
6843 if (aep->ae_u.term.start != NULL)
6844 out_str(aep->ae_u.term.start);
6851 void
6852 screen_stop_highlight()
6854 int do_ME = FALSE; /* output T_ME code */
6856 if (screen_attr != 0
6857 #ifdef WIN3264
6858 && termcap_active
6859 #endif
6862 #ifdef FEAT_GUI
6863 if (gui.in_use)
6865 char buf[20];
6867 /* use internal GUI code */
6868 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
6869 OUT_STR(buf);
6871 else
6872 #endif
6874 if (screen_attr > HL_ALL) /* special HL attr. */
6876 attrentry_T *aep;
6878 if (t_colors > 1)
6881 * Assume that t_me restores the original colors!
6883 aep = syn_cterm_attr2entry(screen_attr);
6884 if (aep != NULL && (aep->ae_u.cterm.fg_color
6885 || aep->ae_u.cterm.bg_color))
6886 do_ME = TRUE;
6888 else
6890 aep = syn_term_attr2entry(screen_attr);
6891 if (aep != NULL && aep->ae_u.term.stop != NULL)
6893 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
6894 do_ME = TRUE;
6895 else
6896 out_str(aep->ae_u.term.stop);
6899 if (aep == NULL) /* did ":syntax clear" */
6900 screen_attr = 0;
6901 else
6902 screen_attr = aep->ae_attr;
6906 * Often all ending-codes are equal to T_ME. Avoid outputting the
6907 * same sequence several times.
6909 if (screen_attr & HL_STANDOUT)
6911 if (STRCMP(T_SE, T_ME) == 0)
6912 do_ME = TRUE;
6913 else
6914 out_str(T_SE);
6916 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
6918 if (STRCMP(T_UE, T_ME) == 0)
6919 do_ME = TRUE;
6920 else
6921 out_str(T_UE);
6923 if (screen_attr & HL_ITALIC)
6925 if (STRCMP(T_CZR, T_ME) == 0)
6926 do_ME = TRUE;
6927 else
6928 out_str(T_CZR);
6930 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
6931 out_str(T_ME);
6933 if (t_colors > 1)
6935 /* set Normal cterm colors */
6936 if (cterm_normal_fg_color != 0)
6937 term_fg_color(cterm_normal_fg_color - 1);
6938 if (cterm_normal_bg_color != 0)
6939 term_bg_color(cterm_normal_bg_color - 1);
6940 if (cterm_normal_fg_bold)
6941 out_str(T_MD);
6945 screen_attr = 0;
6949 * Reset the colors for a cterm. Used when leaving Vim.
6950 * The machine specific code may override this again.
6952 void
6953 reset_cterm_colors()
6955 if (t_colors > 1)
6957 /* set Normal cterm colors */
6958 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
6960 out_str(T_OP);
6961 screen_attr = -1;
6963 if (cterm_normal_fg_bold)
6965 out_str(T_ME);
6966 screen_attr = -1;
6972 * Put character ScreenLines["off"] on the screen at position "row" and "col",
6973 * using the attributes from ScreenAttrs["off"].
6975 static void
6976 screen_char(off, row, col)
6977 unsigned off;
6978 int row;
6979 int col;
6981 int attr;
6983 /* Check for illegal values, just in case (could happen just after
6984 * resizing). */
6985 if (row >= screen_Rows || col >= screen_Columns)
6986 return;
6988 /* Outputting the last character on the screen may scrollup the screen.
6989 * Don't to it! Mark the character invalid (update it when scrolled up) */
6990 if (row == screen_Rows - 1 && col == screen_Columns - 1
6991 #ifdef FEAT_RIGHTLEFT
6992 /* account for first command-line character in rightleft mode */
6993 && !cmdmsg_rl
6994 #endif
6997 ScreenAttrs[off] = (sattr_T)-1;
6998 return;
7002 * Stop highlighting first, so it's easier to move the cursor.
7004 #if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
7005 if (screen_char_attr != 0)
7006 attr = screen_char_attr;
7007 else
7008 #endif
7009 attr = ScreenAttrs[off];
7010 if (screen_attr != attr)
7011 screen_stop_highlight();
7013 windgoto(row, col);
7015 if (screen_attr != attr)
7016 screen_start_highlight(attr);
7018 #ifdef FEAT_MBYTE
7019 if (enc_utf8 && ScreenLinesUC[off] != 0)
7021 char_u buf[MB_MAXBYTES + 1];
7023 /* Convert UTF-8 character to bytes and write it. */
7025 buf[utfc_char2bytes(off, buf)] = NUL;
7027 out_str(buf);
7028 if (utf_char2cells(ScreenLinesUC[off]) > 1)
7029 ++screen_cur_col;
7031 else
7032 #endif
7034 #ifdef FEAT_MBYTE
7035 out_flush_check();
7036 #endif
7037 out_char(ScreenLines[off]);
7038 #ifdef FEAT_MBYTE
7039 /* double-byte character in single-width cell */
7040 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7041 out_char(ScreenLines2[off]);
7042 #endif
7045 screen_cur_col++;
7048 #ifdef FEAT_MBYTE
7051 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
7052 * on the screen at position 'row' and 'col'.
7053 * The attributes of the first byte is used for all. This is required to
7054 * output the two bytes of a double-byte character with nothing in between.
7056 static void
7057 screen_char_2(off, row, col)
7058 unsigned off;
7059 int row;
7060 int col;
7062 /* Check for illegal values (could be wrong when screen was resized). */
7063 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
7064 return;
7066 /* Outputting the last character on the screen may scrollup the screen.
7067 * Don't to it! Mark the character invalid (update it when scrolled up) */
7068 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
7070 ScreenAttrs[off] = (sattr_T)-1;
7071 return;
7074 /* Output the first byte normally (positions the cursor), then write the
7075 * second byte directly. */
7076 screen_char(off, row, col);
7077 out_char(ScreenLines[off + 1]);
7078 ++screen_cur_col;
7080 #endif
7082 #if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
7084 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
7085 * This uses the contents of ScreenLines[] and doesn't change it.
7087 void
7088 screen_draw_rectangle(row, col, height, width, invert)
7089 int row;
7090 int col;
7091 int height;
7092 int width;
7093 int invert;
7095 int r, c;
7096 int off;
7097 #ifdef FEAT_MBYTE
7098 int max_off;
7099 #endif
7101 /* Can't use ScreenLines unless initialized */
7102 if (ScreenLines == NULL)
7103 return;
7105 if (invert)
7106 screen_char_attr = HL_INVERSE;
7107 for (r = row; r < row + height; ++r)
7109 off = LineOffset[r];
7110 #ifdef FEAT_MBYTE
7111 max_off = off + screen_Columns;
7112 #endif
7113 for (c = col; c < col + width; ++c)
7115 #ifdef FEAT_MBYTE
7116 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
7118 screen_char_2(off + c, r, c);
7119 ++c;
7121 else
7122 #endif
7124 screen_char(off + c, r, c);
7125 #ifdef FEAT_MBYTE
7126 if (utf_off2cells(off + c, max_off) > 1)
7127 ++c;
7128 #endif
7132 screen_char_attr = 0;
7134 #endif
7136 #ifdef FEAT_VERTSPLIT
7138 * Redraw the characters for a vertically split window.
7140 static void
7141 redraw_block(row, end, wp)
7142 int row;
7143 int end;
7144 win_T *wp;
7146 int col;
7147 int width;
7149 # ifdef FEAT_CLIPBOARD
7150 clip_may_clear_selection(row, end - 1);
7151 # endif
7153 if (wp == NULL)
7155 col = 0;
7156 width = Columns;
7158 else
7160 col = wp->w_wincol;
7161 width = wp->w_width;
7163 screen_draw_rectangle(row, col, end - row, width, FALSE);
7165 #endif
7168 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
7169 * with character 'c1' in first column followed by 'c2' in the other columns.
7170 * Use attributes 'attr'.
7172 void
7173 screen_fill(start_row, end_row, start_col, end_col, c1, c2, attr)
7174 int start_row, end_row;
7175 int start_col, end_col;
7176 int c1, c2;
7177 int attr;
7179 int row;
7180 int col;
7181 int off;
7182 int end_off;
7183 int did_delete;
7184 int c;
7185 int norm_term;
7186 #if defined(FEAT_GUI) || defined(UNIX)
7187 int force_next = FALSE;
7188 #endif
7190 if (end_row > screen_Rows) /* safety check */
7191 end_row = screen_Rows;
7192 if (end_col > screen_Columns) /* safety check */
7193 end_col = screen_Columns;
7194 if (ScreenLines == NULL
7195 || start_row >= end_row
7196 || start_col >= end_col) /* nothing to do */
7197 return;
7199 /* it's a "normal" terminal when not in a GUI or cterm */
7200 norm_term = (
7201 #ifdef FEAT_GUI
7202 !gui.in_use &&
7203 #endif
7204 t_colors <= 1);
7205 for (row = start_row; row < end_row; ++row)
7207 #ifdef FEAT_MBYTE
7208 if (has_mbyte
7209 # ifdef FEAT_GUI
7210 && !gui.in_use
7211 # endif
7214 /* When drawing over the right halve of a double-wide char clear
7215 * out the left halve. When drawing over the left halve of a
7216 * double wide-char clear out the right halve. Only needed in a
7217 * terminal. */
7218 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
7219 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
7220 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
7221 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
7223 #endif
7225 * Try to use delete-line termcap code, when no attributes or in a
7226 * "normal" terminal, where a bold/italic space is just a
7227 * space.
7229 did_delete = FALSE;
7230 if (c2 == ' '
7231 && end_col == Columns
7232 && can_clear(T_CE)
7233 && (attr == 0
7234 || (norm_term
7235 && attr <= HL_ALL
7236 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
7239 * check if we really need to clear something
7241 col = start_col;
7242 if (c1 != ' ') /* don't clear first char */
7243 ++col;
7245 off = LineOffset[row] + col;
7246 end_off = LineOffset[row] + end_col;
7248 /* skip blanks (used often, keep it fast!) */
7249 #ifdef FEAT_MBYTE
7250 if (enc_utf8)
7251 while (off < end_off && ScreenLines[off] == ' '
7252 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
7253 ++off;
7254 else
7255 #endif
7256 while (off < end_off && ScreenLines[off] == ' '
7257 && ScreenAttrs[off] == 0)
7258 ++off;
7259 if (off < end_off) /* something to be cleared */
7261 col = off - LineOffset[row];
7262 screen_stop_highlight();
7263 term_windgoto(row, col);/* clear rest of this screen line */
7264 out_str(T_CE);
7265 screen_start(); /* don't know where cursor is now */
7266 col = end_col - col;
7267 while (col--) /* clear chars in ScreenLines */
7269 ScreenLines[off] = ' ';
7270 #ifdef FEAT_MBYTE
7271 if (enc_utf8)
7272 ScreenLinesUC[off] = 0;
7273 #endif
7274 ScreenAttrs[off] = 0;
7275 ++off;
7278 did_delete = TRUE; /* the chars are cleared now */
7281 off = LineOffset[row] + start_col;
7282 c = c1;
7283 for (col = start_col; col < end_col; ++col)
7285 if (ScreenLines[off] != c
7286 #ifdef FEAT_MBYTE
7287 || (enc_utf8 && (int)ScreenLinesUC[off]
7288 != (c >= 0x80 ? c : 0))
7289 #endif
7290 || ScreenAttrs[off] != attr
7291 #if defined(FEAT_GUI) || defined(UNIX)
7292 || force_next
7293 #endif
7296 #if defined(FEAT_GUI) || defined(UNIX)
7297 /* The bold trick may make a single row of pixels appear in
7298 * the next character. When a bold character is removed, the
7299 * next character should be redrawn too. This happens for our
7300 * own GUI and for some xterms. */
7301 if (
7302 # ifdef FEAT_GUI
7303 gui.in_use
7304 # endif
7305 # if defined(FEAT_GUI) && defined(UNIX)
7307 # endif
7308 # ifdef UNIX
7309 term_is_xterm
7310 # endif
7313 if (ScreenLines[off] != ' '
7314 && (ScreenAttrs[off] > HL_ALL
7315 || ScreenAttrs[off] & HL_BOLD))
7316 force_next = TRUE;
7317 else
7318 force_next = FALSE;
7320 #endif
7321 ScreenLines[off] = c;
7322 #ifdef FEAT_MBYTE
7323 if (enc_utf8)
7325 if (c >= 0x80)
7327 ScreenLinesUC[off] = c;
7328 ScreenLinesC[0][off] = 0;
7330 else
7331 ScreenLinesUC[off] = 0;
7333 #endif
7334 ScreenAttrs[off] = attr;
7335 if (!did_delete || c != ' ')
7336 screen_char(off, row, col);
7338 ++off;
7339 if (col == start_col)
7341 if (did_delete)
7342 break;
7343 c = c2;
7346 if (end_col == Columns)
7347 LineWraps[row] = FALSE;
7348 if (row == Rows - 1) /* overwritten the command line */
7350 redraw_cmdline = TRUE;
7351 if (c1 == ' ' && c2 == ' ')
7352 clear_cmdline = FALSE; /* command line has been cleared */
7353 if (start_col == 0)
7354 mode_displayed = FALSE; /* mode cleared or overwritten */
7360 * Check if there should be a delay. Used before clearing or redrawing the
7361 * screen or the command line.
7363 void
7364 check_for_delay(check_msg_scroll)
7365 int check_msg_scroll;
7367 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
7368 && !did_wait_return
7369 && emsg_silent == 0)
7371 out_flush();
7372 ui_delay(1000L, TRUE);
7373 emsg_on_display = FALSE;
7374 if (check_msg_scroll)
7375 msg_scroll = FALSE;
7380 * screen_valid - allocate screen buffers if size changed
7381 * If "clear" is TRUE: clear screen if it has been resized.
7382 * Returns TRUE if there is a valid screen to write to.
7383 * Returns FALSE when starting up and screen not initialized yet.
7386 screen_valid(clear)
7387 int clear;
7389 screenalloc(clear); /* allocate screen buffers if size changed */
7390 return (ScreenLines != NULL);
7394 * Resize the shell to Rows and Columns.
7395 * Allocate ScreenLines[] and associated items.
7397 * There may be some time between setting Rows and Columns and (re)allocating
7398 * ScreenLines[]. This happens when starting up and when (manually) changing
7399 * the shell size. Always use screen_Rows and screen_Columns to access items
7400 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
7401 * final size of the shell is needed.
7403 void
7404 screenalloc(clear)
7405 int clear;
7407 int new_row, old_row;
7408 #ifdef FEAT_GUI
7409 int old_Rows;
7410 #endif
7411 win_T *wp;
7412 int outofmem = FALSE;
7413 int len;
7414 schar_T *new_ScreenLines;
7415 #ifdef FEAT_MBYTE
7416 u8char_T *new_ScreenLinesUC = NULL;
7417 u8char_T *new_ScreenLinesC[MAX_MCO];
7418 schar_T *new_ScreenLines2 = NULL;
7419 int i;
7420 #endif
7421 sattr_T *new_ScreenAttrs;
7422 unsigned *new_LineOffset;
7423 char_u *new_LineWraps;
7424 #ifdef FEAT_WINDOWS
7425 short *new_TabPageIdxs;
7426 tabpage_T *tp;
7427 #endif
7428 static int entered = FALSE; /* avoid recursiveness */
7429 static int done_outofmem_msg = FALSE; /* did outofmem message */
7430 #ifdef FEAT_AUTOCMD
7431 int retry_count = 0;
7433 retry:
7434 #endif
7436 * Allocation of the screen buffers is done only when the size changes and
7437 * when Rows and Columns have been set and we have started doing full
7438 * screen stuff.
7440 if ((ScreenLines != NULL
7441 && Rows == screen_Rows
7442 && Columns == screen_Columns
7443 #ifdef FEAT_MBYTE
7444 && enc_utf8 == (ScreenLinesUC != NULL)
7445 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
7446 && p_mco == Screen_mco
7447 #endif
7449 || Rows == 0
7450 || Columns == 0
7451 || (!full_screen && ScreenLines == NULL))
7452 return;
7455 * It's possible that we produce an out-of-memory message below, which
7456 * will cause this function to be called again. To break the loop, just
7457 * return here.
7459 if (entered)
7460 return;
7461 entered = TRUE;
7464 * Note that the window sizes are updated before reallocating the arrays,
7465 * thus we must not redraw here!
7467 ++RedrawingDisabled;
7469 win_new_shellsize(); /* fit the windows in the new sized shell */
7471 comp_col(); /* recompute columns for shown command and ruler */
7474 * We're changing the size of the screen.
7475 * - Allocate new arrays for ScreenLines and ScreenAttrs.
7476 * - Move lines from the old arrays into the new arrays, clear extra
7477 * lines (unless the screen is going to be cleared).
7478 * - Free the old arrays.
7480 * If anything fails, make ScreenLines NULL, so we don't do anything!
7481 * Continuing with the old ScreenLines may result in a crash, because the
7482 * size is wrong.
7484 FOR_ALL_TAB_WINDOWS(tp, wp)
7485 win_free_lsize(wp);
7487 new_ScreenLines = (schar_T *)lalloc((long_u)(
7488 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7489 #ifdef FEAT_MBYTE
7490 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T) * MAX_MCO);
7491 if (enc_utf8)
7493 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
7494 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
7495 for (i = 0; i < p_mco; ++i)
7496 new_ScreenLinesC[i] = (u8char_T *)lalloc((long_u)(
7497 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
7499 if (enc_dbcs == DBCS_JPNU)
7500 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
7501 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7502 #endif
7503 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
7504 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
7505 new_LineOffset = (unsigned *)lalloc((long_u)(
7506 Rows * sizeof(unsigned)), FALSE);
7507 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
7508 #ifdef FEAT_WINDOWS
7509 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
7510 #endif
7512 FOR_ALL_TAB_WINDOWS(tp, wp)
7514 if (win_alloc_lines(wp) == FAIL)
7516 outofmem = TRUE;
7517 #ifdef FEAT_WINDOWS
7518 goto give_up;
7519 #endif
7522 #ifdef FEAT_WINDOWS
7523 give_up:
7524 #endif
7526 #ifdef FEAT_MBYTE
7527 for (i = 0; i < p_mco; ++i)
7528 if (new_ScreenLinesC[i] == NULL)
7529 break;
7530 #endif
7531 if (new_ScreenLines == NULL
7532 #ifdef FEAT_MBYTE
7533 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
7534 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
7535 #endif
7536 || new_ScreenAttrs == NULL
7537 || new_LineOffset == NULL
7538 || new_LineWraps == NULL
7539 #ifdef FEAT_WINDOWS
7540 || new_TabPageIdxs == NULL
7541 #endif
7542 || outofmem)
7544 if (ScreenLines != NULL || !done_outofmem_msg)
7546 /* guess the size */
7547 do_outofmem_msg((long_u)((Rows + 1) * Columns));
7549 /* Remember we did this to avoid getting outofmem messages over
7550 * and over again. */
7551 done_outofmem_msg = TRUE;
7553 vim_free(new_ScreenLines);
7554 new_ScreenLines = NULL;
7555 #ifdef FEAT_MBYTE
7556 vim_free(new_ScreenLinesUC);
7557 new_ScreenLinesUC = NULL;
7558 for (i = 0; i < p_mco; ++i)
7560 vim_free(new_ScreenLinesC[i]);
7561 new_ScreenLinesC[i] = NULL;
7563 vim_free(new_ScreenLines2);
7564 new_ScreenLines2 = NULL;
7565 #endif
7566 vim_free(new_ScreenAttrs);
7567 new_ScreenAttrs = NULL;
7568 vim_free(new_LineOffset);
7569 new_LineOffset = NULL;
7570 vim_free(new_LineWraps);
7571 new_LineWraps = NULL;
7572 #ifdef FEAT_WINDOWS
7573 vim_free(new_TabPageIdxs);
7574 new_TabPageIdxs = NULL;
7575 #endif
7577 else
7579 done_outofmem_msg = FALSE;
7581 for (new_row = 0; new_row < Rows; ++new_row)
7583 new_LineOffset[new_row] = new_row * Columns;
7584 new_LineWraps[new_row] = FALSE;
7587 * If the screen is not going to be cleared, copy as much as
7588 * possible from the old screen to the new one and clear the rest
7589 * (used when resizing the window at the "--more--" prompt or when
7590 * executing an external command, for the GUI).
7592 if (!clear)
7594 (void)vim_memset(new_ScreenLines + new_row * Columns,
7595 ' ', (size_t)Columns * sizeof(schar_T));
7596 #ifdef FEAT_MBYTE
7597 if (enc_utf8)
7599 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
7600 0, (size_t)Columns * sizeof(u8char_T));
7601 for (i = 0; i < p_mco; ++i)
7602 (void)vim_memset(new_ScreenLinesC[i]
7603 + new_row * Columns,
7604 0, (size_t)Columns * sizeof(u8char_T));
7606 if (enc_dbcs == DBCS_JPNU)
7607 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
7608 0, (size_t)Columns * sizeof(schar_T));
7609 #endif
7610 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
7611 0, (size_t)Columns * sizeof(sattr_T));
7612 old_row = new_row + (screen_Rows - Rows);
7613 if (old_row >= 0 && ScreenLines != NULL)
7615 if (screen_Columns < Columns)
7616 len = screen_Columns;
7617 else
7618 len = Columns;
7619 #ifdef FEAT_MBYTE
7620 /* When switching to utf-8 don't copy characters, they
7621 * may be invalid now. Also when p_mco changes. */
7622 if (!(enc_utf8 && ScreenLinesUC == NULL)
7623 && p_mco == Screen_mco)
7624 #endif
7625 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
7626 ScreenLines + LineOffset[old_row],
7627 (size_t)len * sizeof(schar_T));
7628 #ifdef FEAT_MBYTE
7629 if (enc_utf8 && ScreenLinesUC != NULL
7630 && p_mco == Screen_mco)
7632 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
7633 ScreenLinesUC + LineOffset[old_row],
7634 (size_t)len * sizeof(u8char_T));
7635 for (i = 0; i < p_mco; ++i)
7636 mch_memmove(new_ScreenLinesC[i]
7637 + new_LineOffset[new_row],
7638 ScreenLinesC[i] + LineOffset[old_row],
7639 (size_t)len * sizeof(u8char_T));
7641 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
7642 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
7643 ScreenLines2 + LineOffset[old_row],
7644 (size_t)len * sizeof(schar_T));
7645 #endif
7646 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
7647 ScreenAttrs + LineOffset[old_row],
7648 (size_t)len * sizeof(sattr_T));
7652 /* Use the last line of the screen for the current line. */
7653 current_ScreenLine = new_ScreenLines + Rows * Columns;
7656 free_screenlines();
7658 ScreenLines = new_ScreenLines;
7659 #ifdef FEAT_MBYTE
7660 ScreenLinesUC = new_ScreenLinesUC;
7661 for (i = 0; i < p_mco; ++i)
7662 ScreenLinesC[i] = new_ScreenLinesC[i];
7663 Screen_mco = p_mco;
7664 ScreenLines2 = new_ScreenLines2;
7665 #endif
7666 ScreenAttrs = new_ScreenAttrs;
7667 LineOffset = new_LineOffset;
7668 LineWraps = new_LineWraps;
7669 #ifdef FEAT_WINDOWS
7670 TabPageIdxs = new_TabPageIdxs;
7671 #endif
7673 /* It's important that screen_Rows and screen_Columns reflect the actual
7674 * size of ScreenLines[]. Set them before calling anything. */
7675 #ifdef FEAT_GUI
7676 old_Rows = screen_Rows;
7677 #endif
7678 screen_Rows = Rows;
7679 screen_Columns = Columns;
7681 must_redraw = CLEAR; /* need to clear the screen later */
7682 if (clear)
7683 screenclear2();
7685 #ifdef FEAT_GUI
7686 else if (gui.in_use
7687 && !gui.starting
7688 && ScreenLines != NULL
7689 && old_Rows != Rows)
7691 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
7693 * Adjust the position of the cursor, for when executing an external
7694 * command.
7696 if (msg_row >= Rows) /* Rows got smaller */
7697 msg_row = Rows - 1; /* put cursor at last row */
7698 else if (Rows > old_Rows) /* Rows got bigger */
7699 msg_row += Rows - old_Rows; /* put cursor in same place */
7700 if (msg_col >= Columns) /* Columns got smaller */
7701 msg_col = Columns - 1; /* put cursor at last column */
7703 #endif
7705 entered = FALSE;
7706 --RedrawingDisabled;
7708 #ifdef FEAT_AUTOCMD
7710 * Do not apply autocommands more than 3 times to avoid an endless loop
7711 * in case applying autocommands always changes Rows or Columns.
7713 if (starting == 0 && ++retry_count <= 3)
7715 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
7716 /* In rare cases, autocommands may have altered Rows or Columns,
7717 * jump back to check if we need to allocate the screen again. */
7718 goto retry;
7720 #endif
7723 void
7724 free_screenlines()
7726 #ifdef FEAT_MBYTE
7727 int i;
7729 vim_free(ScreenLinesUC);
7730 for (i = 0; i < Screen_mco; ++i)
7731 vim_free(ScreenLinesC[i]);
7732 vim_free(ScreenLines2);
7733 #endif
7734 vim_free(ScreenLines);
7735 vim_free(ScreenAttrs);
7736 vim_free(LineOffset);
7737 vim_free(LineWraps);
7738 #ifdef FEAT_WINDOWS
7739 vim_free(TabPageIdxs);
7740 #endif
7743 void
7744 screenclear()
7746 check_for_delay(FALSE);
7747 screenalloc(FALSE); /* allocate screen buffers if size changed */
7748 screenclear2(); /* clear the screen */
7751 static void
7752 screenclear2()
7754 int i;
7756 if (starting == NO_SCREEN || ScreenLines == NULL
7757 #ifdef FEAT_GUI
7758 || (gui.in_use && gui.starting)
7759 #endif
7761 return;
7763 #ifdef FEAT_GUI
7764 if (!gui.in_use)
7765 #endif
7766 screen_attr = -1; /* force setting the Normal colors */
7767 screen_stop_highlight(); /* don't want highlighting here */
7769 #ifdef FEAT_CLIPBOARD
7770 /* disable selection without redrawing it */
7771 clip_scroll_selection(9999);
7772 #endif
7774 /* blank out ScreenLines */
7775 for (i = 0; i < Rows; ++i)
7777 lineclear(LineOffset[i], (int)Columns);
7778 LineWraps[i] = FALSE;
7781 if (can_clear(T_CL))
7783 out_str(T_CL); /* clear the display */
7784 clear_cmdline = FALSE;
7785 mode_displayed = FALSE;
7787 else
7789 /* can't clear the screen, mark all chars with invalid attributes */
7790 for (i = 0; i < Rows; ++i)
7791 lineinvalid(LineOffset[i], (int)Columns);
7792 clear_cmdline = TRUE;
7795 screen_cleared = TRUE; /* can use contents of ScreenLines now */
7797 win_rest_invalid(firstwin);
7798 redraw_cmdline = TRUE;
7799 #ifdef FEAT_WINDOWS
7800 redraw_tabline = TRUE;
7801 #endif
7802 if (must_redraw == CLEAR) /* no need to clear again */
7803 must_redraw = NOT_VALID;
7804 compute_cmdrow();
7805 msg_row = cmdline_row; /* put cursor on last line for messages */
7806 msg_col = 0;
7807 screen_start(); /* don't know where cursor is now */
7808 msg_scrolled = 0; /* can't scroll back */
7809 msg_didany = FALSE;
7810 msg_didout = FALSE;
7814 * Clear one line in ScreenLines.
7816 static void
7817 lineclear(off, width)
7818 unsigned off;
7819 int width;
7821 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
7822 #ifdef FEAT_MBYTE
7823 if (enc_utf8)
7824 (void)vim_memset(ScreenLinesUC + off, 0,
7825 (size_t)width * sizeof(u8char_T));
7826 #endif
7827 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
7831 * Mark one line in ScreenLines invalid by setting the attributes to an
7832 * invalid value.
7834 static void
7835 lineinvalid(off, width)
7836 unsigned off;
7837 int width;
7839 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
7842 #ifdef FEAT_VERTSPLIT
7844 * Copy part of a Screenline for vertically split window "wp".
7846 static void
7847 linecopy(to, from, wp)
7848 int to;
7849 int from;
7850 win_T *wp;
7852 unsigned off_to = LineOffset[to] + wp->w_wincol;
7853 unsigned off_from = LineOffset[from] + wp->w_wincol;
7855 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
7856 wp->w_width * sizeof(schar_T));
7857 # ifdef FEAT_MBYTE
7858 if (enc_utf8)
7860 int i;
7862 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
7863 wp->w_width * sizeof(u8char_T));
7864 for (i = 0; i < p_mco; ++i)
7865 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
7866 wp->w_width * sizeof(u8char_T));
7868 if (enc_dbcs == DBCS_JPNU)
7869 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
7870 wp->w_width * sizeof(schar_T));
7871 # endif
7872 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
7873 wp->w_width * sizeof(sattr_T));
7875 #endif
7878 * Return TRUE if clearing with term string "p" would work.
7879 * It can't work when the string is empty or it won't set the right background.
7882 can_clear(p)
7883 char_u *p;
7885 return (*p != NUL && (t_colors <= 1
7886 #ifdef FEAT_GUI
7887 || gui.in_use
7888 #endif
7889 || cterm_normal_bg_color == 0 || *T_UT != NUL));
7893 * Reset cursor position. Use whenever cursor was moved because of outputting
7894 * something directly to the screen (shell commands) or a terminal control
7895 * code.
7897 void
7898 screen_start()
7900 screen_cur_row = screen_cur_col = 9999;
7904 * Move the cursor to position "row","col" in the screen.
7905 * This tries to find the most efficient way to move, minimizing the number of
7906 * characters sent to the terminal.
7908 void
7909 windgoto(row, col)
7910 int row;
7911 int col;
7913 sattr_T *p;
7914 int i;
7915 int plan;
7916 int cost;
7917 int wouldbe_col;
7918 int noinvcurs;
7919 char_u *bs;
7920 int goto_cost;
7921 int attr;
7923 #define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
7924 #define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
7926 #define PLAN_LE 1
7927 #define PLAN_CR 2
7928 #define PLAN_NL 3
7929 #define PLAN_WRITE 4
7930 /* Can't use ScreenLines unless initialized */
7931 if (ScreenLines == NULL)
7932 return;
7934 if (col != screen_cur_col || row != screen_cur_row)
7936 /* Check for valid position. */
7937 if (row < 0) /* window without text lines? */
7938 row = 0;
7939 if (row >= screen_Rows)
7940 row = screen_Rows - 1;
7941 if (col >= screen_Columns)
7942 col = screen_Columns - 1;
7944 /* check if no cursor movement is allowed in highlight mode */
7945 if (screen_attr && *T_MS == NUL)
7946 noinvcurs = HIGHL_COST;
7947 else
7948 noinvcurs = 0;
7949 goto_cost = GOTO_COST + noinvcurs;
7952 * Plan how to do the positioning:
7953 * 1. Use CR to move it to column 0, same row.
7954 * 2. Use T_LE to move it a few columns to the left.
7955 * 3. Use NL to move a few lines down, column 0.
7956 * 4. Move a few columns to the right with T_ND or by writing chars.
7958 * Don't do this if the cursor went beyond the last column, the cursor
7959 * position is unknown then (some terminals wrap, some don't )
7961 * First check if the highlighting attributes allow us to write
7962 * characters to move the cursor to the right.
7964 if (row >= screen_cur_row && screen_cur_col < Columns)
7967 * If the cursor is in the same row, bigger col, we can use CR
7968 * or T_LE.
7970 bs = NULL; /* init for GCC */
7971 attr = screen_attr;
7972 if (row == screen_cur_row && col < screen_cur_col)
7974 /* "le" is preferred over "bc", because "bc" is obsolete */
7975 if (*T_LE)
7976 bs = T_LE; /* "cursor left" */
7977 else
7978 bs = T_BC; /* "backspace character (old) */
7979 if (*bs)
7980 cost = (screen_cur_col - col) * (int)STRLEN(bs);
7981 else
7982 cost = 999;
7983 if (col + 1 < cost) /* using CR is less characters */
7985 plan = PLAN_CR;
7986 wouldbe_col = 0;
7987 cost = 1; /* CR is just one character */
7989 else
7991 plan = PLAN_LE;
7992 wouldbe_col = col;
7994 if (noinvcurs) /* will stop highlighting */
7996 cost += noinvcurs;
7997 attr = 0;
8002 * If the cursor is above where we want to be, we can use CR LF.
8004 else if (row > screen_cur_row)
8006 plan = PLAN_NL;
8007 wouldbe_col = 0;
8008 cost = (row - screen_cur_row) * 2; /* CR LF */
8009 if (noinvcurs) /* will stop highlighting */
8011 cost += noinvcurs;
8012 attr = 0;
8017 * If the cursor is in the same row, smaller col, just use write.
8019 else
8021 plan = PLAN_WRITE;
8022 wouldbe_col = screen_cur_col;
8023 cost = 0;
8027 * Check if any characters that need to be written have the
8028 * correct attributes. Also avoid UTF-8 characters.
8030 i = col - wouldbe_col;
8031 if (i > 0)
8032 cost += i;
8033 if (cost < goto_cost && i > 0)
8036 * Check if the attributes are correct without additionally
8037 * stopping highlighting.
8039 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
8040 while (i && *p++ == attr)
8041 --i;
8042 if (i != 0)
8045 * Try if it works when highlighting is stopped here.
8047 if (*--p == 0)
8049 cost += noinvcurs;
8050 while (i && *p++ == 0)
8051 --i;
8053 if (i != 0)
8054 cost = 999; /* different attributes, don't do it */
8056 #ifdef FEAT_MBYTE
8057 if (enc_utf8)
8059 /* Don't use an UTF-8 char for positioning, it's slow. */
8060 for (i = wouldbe_col; i < col; ++i)
8061 if (ScreenLinesUC[LineOffset[row] + i] != 0)
8063 cost = 999;
8064 break;
8067 #endif
8071 * We can do it without term_windgoto()!
8073 if (cost < goto_cost)
8075 if (plan == PLAN_LE)
8077 if (noinvcurs)
8078 screen_stop_highlight();
8079 while (screen_cur_col > col)
8081 out_str(bs);
8082 --screen_cur_col;
8085 else if (plan == PLAN_CR)
8087 if (noinvcurs)
8088 screen_stop_highlight();
8089 out_char('\r');
8090 screen_cur_col = 0;
8092 else if (plan == PLAN_NL)
8094 if (noinvcurs)
8095 screen_stop_highlight();
8096 while (screen_cur_row < row)
8098 out_char('\n');
8099 ++screen_cur_row;
8101 screen_cur_col = 0;
8104 i = col - screen_cur_col;
8105 if (i > 0)
8108 * Use cursor-right if it's one character only. Avoids
8109 * removing a line of pixels from the last bold char, when
8110 * using the bold trick in the GUI.
8112 if (T_ND[0] != NUL && T_ND[1] == NUL)
8114 while (i-- > 0)
8115 out_char(*T_ND);
8117 else
8119 int off;
8121 off = LineOffset[row] + screen_cur_col;
8122 while (i-- > 0)
8124 if (ScreenAttrs[off] != screen_attr)
8125 screen_stop_highlight();
8126 #ifdef FEAT_MBYTE
8127 out_flush_check();
8128 #endif
8129 out_char(ScreenLines[off]);
8130 #ifdef FEAT_MBYTE
8131 if (enc_dbcs == DBCS_JPNU
8132 && ScreenLines[off] == 0x8e)
8133 out_char(ScreenLines2[off]);
8134 #endif
8135 ++off;
8141 else
8142 cost = 999;
8144 if (cost >= goto_cost)
8146 if (noinvcurs)
8147 screen_stop_highlight();
8148 if (row == screen_cur_row && (col > screen_cur_col) &&
8149 *T_CRI != NUL)
8150 term_cursor_right(col - screen_cur_col);
8151 else
8152 term_windgoto(row, col);
8154 screen_cur_row = row;
8155 screen_cur_col = col;
8160 * Set cursor to its position in the current window.
8162 void
8163 setcursor()
8165 if (redrawing())
8167 validate_cursor();
8168 windgoto(W_WINROW(curwin) + curwin->w_wrow,
8169 W_WINCOL(curwin) + (
8170 #ifdef FEAT_RIGHTLEFT
8171 /* With 'rightleft' set and the cursor on a double-wide
8172 * character, position it on the leftmost column. */
8173 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
8174 # ifdef FEAT_MBYTE
8175 (has_mbyte
8176 && (*mb_ptr2cells)(ml_get_cursor()) == 2
8177 && vim_isprintc(gchar_cursor())) ? 2 :
8178 # endif
8179 1)) :
8180 #endif
8181 curwin->w_wcol));
8187 * insert 'line_count' lines at 'row' in window 'wp'
8188 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
8189 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
8190 * scrolling.
8191 * Returns FAIL if the lines are not inserted, OK for success.
8194 win_ins_lines(wp, row, line_count, invalid, mayclear)
8195 win_T *wp;
8196 int row;
8197 int line_count;
8198 int invalid;
8199 int mayclear;
8201 int did_delete;
8202 int nextrow;
8203 int lastrow;
8204 int retval;
8206 if (invalid)
8207 wp->w_lines_valid = 0;
8209 if (wp->w_height < 5)
8210 return FAIL;
8212 if (line_count > wp->w_height - row)
8213 line_count = wp->w_height - row;
8215 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
8216 if (retval != MAYBE)
8217 return retval;
8220 * If there is a next window or a status line, we first try to delete the
8221 * lines at the bottom to avoid messing what is after the window.
8222 * If this fails and there are following windows, don't do anything to avoid
8223 * messing up those windows, better just redraw.
8225 did_delete = FALSE;
8226 #ifdef FEAT_WINDOWS
8227 if (wp->w_next != NULL || wp->w_status_height)
8229 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8230 line_count, (int)Rows, FALSE, NULL) == OK)
8231 did_delete = TRUE;
8232 else if (wp->w_next)
8233 return FAIL;
8235 #endif
8237 * if no lines deleted, blank the lines that will end up below the window
8239 if (!did_delete)
8241 #ifdef FEAT_WINDOWS
8242 wp->w_redr_status = TRUE;
8243 #endif
8244 redraw_cmdline = TRUE;
8245 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
8246 lastrow = nextrow + line_count;
8247 if (lastrow > Rows)
8248 lastrow = Rows;
8249 screen_fill(nextrow - line_count, lastrow - line_count,
8250 W_WINCOL(wp), (int)W_ENDCOL(wp),
8251 ' ', ' ', 0);
8254 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
8255 == FAIL)
8257 /* deletion will have messed up other windows */
8258 if (did_delete)
8260 #ifdef FEAT_WINDOWS
8261 wp->w_redr_status = TRUE;
8262 #endif
8263 win_rest_invalid(W_NEXT(wp));
8265 return FAIL;
8268 return OK;
8272 * delete "line_count" window lines at "row" in window "wp"
8273 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
8274 * If "mayclear" is TRUE the screen will be cleared if it is faster than
8275 * scrolling
8276 * Return OK for success, FAIL if the lines are not deleted.
8279 win_del_lines(wp, row, line_count, invalid, mayclear)
8280 win_T *wp;
8281 int row;
8282 int line_count;
8283 int invalid;
8284 int mayclear;
8286 int retval;
8288 if (invalid)
8289 wp->w_lines_valid = 0;
8291 if (line_count > wp->w_height - row)
8292 line_count = wp->w_height - row;
8294 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
8295 if (retval != MAYBE)
8296 return retval;
8298 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
8299 (int)Rows, FALSE, NULL) == FAIL)
8300 return FAIL;
8302 #ifdef FEAT_WINDOWS
8304 * If there are windows or status lines below, try to put them at the
8305 * correct place. If we can't do that, they have to be redrawn.
8307 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
8309 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8310 line_count, (int)Rows, NULL) == FAIL)
8312 wp->w_redr_status = TRUE;
8313 win_rest_invalid(wp->w_next);
8317 * If this is the last window and there is no status line, redraw the
8318 * command line later.
8320 else
8321 #endif
8322 redraw_cmdline = TRUE;
8323 return OK;
8327 * Common code for win_ins_lines() and win_del_lines().
8328 * Returns OK or FAIL when the work has been done.
8329 * Returns MAYBE when not finished yet.
8331 static int
8332 win_do_lines(wp, row, line_count, mayclear, del)
8333 win_T *wp;
8334 int row;
8335 int line_count;
8336 int mayclear;
8337 int del;
8339 int retval;
8341 if (!redrawing() || line_count <= 0)
8342 return FAIL;
8344 /* only a few lines left: redraw is faster */
8345 if (mayclear && Rows - line_count < 5
8346 #ifdef FEAT_VERTSPLIT
8347 && wp->w_width == Columns
8348 #endif
8351 screenclear(); /* will set wp->w_lines_valid to 0 */
8352 return FAIL;
8356 * Delete all remaining lines
8358 if (row + line_count >= wp->w_height)
8360 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
8361 W_WINCOL(wp), (int)W_ENDCOL(wp),
8362 ' ', ' ', 0);
8363 return OK;
8367 * when scrolling, the message on the command line should be cleared,
8368 * otherwise it will stay there forever.
8370 clear_cmdline = TRUE;
8373 * If the terminal can set a scroll region, use that.
8374 * Always do this in a vertically split window. This will redraw from
8375 * ScreenLines[] when t_CV isn't defined. That's faster than using
8376 * win_line().
8377 * Don't use a scroll region when we are going to redraw the text, writing
8378 * a character in the lower right corner of the scroll region causes a
8379 * scroll-up in the DJGPP version.
8381 if (scroll_region
8382 #ifdef FEAT_VERTSPLIT
8383 || W_WIDTH(wp) != Columns
8384 #endif
8387 #ifdef FEAT_VERTSPLIT
8388 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8389 #endif
8390 scroll_region_set(wp, row);
8391 if (del)
8392 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
8393 wp->w_height - row, FALSE, wp);
8394 else
8395 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
8396 wp->w_height - row, wp);
8397 #ifdef FEAT_VERTSPLIT
8398 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8399 #endif
8400 scroll_region_reset();
8401 return retval;
8404 #ifdef FEAT_WINDOWS
8405 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
8406 return FAIL;
8407 #endif
8409 return MAYBE;
8413 * window 'wp' and everything after it is messed up, mark it for redraw
8415 static void
8416 win_rest_invalid(wp)
8417 win_T *wp;
8419 #ifdef FEAT_WINDOWS
8420 while (wp != NULL)
8421 #else
8422 if (wp != NULL)
8423 #endif
8425 redraw_win_later(wp, NOT_VALID);
8426 #ifdef FEAT_WINDOWS
8427 wp->w_redr_status = TRUE;
8428 wp = wp->w_next;
8429 #endif
8431 redraw_cmdline = TRUE;
8435 * The rest of the routines in this file perform screen manipulations. The
8436 * given operation is performed physically on the screen. The corresponding
8437 * change is also made to the internal screen image. In this way, the editor
8438 * anticipates the effect of editing changes on the appearance of the screen.
8439 * That way, when we call screenupdate a complete redraw isn't usually
8440 * necessary. Another advantage is that we can keep adding code to anticipate
8441 * screen changes, and in the meantime, everything still works.
8445 * types for inserting or deleting lines
8447 #define USE_T_CAL 1
8448 #define USE_T_CDL 2
8449 #define USE_T_AL 3
8450 #define USE_T_CE 4
8451 #define USE_T_DL 5
8452 #define USE_T_SR 6
8453 #define USE_NL 7
8454 #define USE_T_CD 8
8455 #define USE_REDRAW 9
8458 * insert lines on the screen and update ScreenLines[]
8459 * 'end' is the line after the scrolled part. Normally it is Rows.
8460 * When scrolling region used 'off' is the offset from the top for the region.
8461 * 'row' and 'end' are relative to the start of the region.
8463 * return FAIL for failure, OK for success.
8466 screen_ins_lines(off, row, line_count, end, wp)
8467 int off;
8468 int row;
8469 int line_count;
8470 int end;
8471 win_T *wp; /* NULL or window to use width from */
8473 int i;
8474 int j;
8475 unsigned temp;
8476 int cursor_row;
8477 int type;
8478 int result_empty;
8479 int can_ce = can_clear(T_CE);
8482 * FAIL if
8483 * - there is no valid screen
8484 * - the screen has to be redrawn completely
8485 * - the line count is less than one
8486 * - the line count is more than 'ttyscroll'
8488 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
8489 return FAIL;
8492 * There are seven ways to insert lines:
8493 * 0. When in a vertically split window and t_CV isn't set, redraw the
8494 * characters from ScreenLines[].
8495 * 1. Use T_CD (clear to end of display) if it exists and the result of
8496 * the insert is just empty lines
8497 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
8498 * present or line_count > 1. It looks better if we do all the inserts
8499 * at once.
8500 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
8501 * insert is just empty lines and T_CE is not present or line_count >
8502 * 1.
8503 * 4. Use T_AL (insert line) if it exists.
8504 * 5. Use T_CE (erase line) if it exists and the result of the insert is
8505 * just empty lines.
8506 * 6. Use T_DL (delete line) if it exists and the result of the insert is
8507 * just empty lines.
8508 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
8509 * the 'da' flag is not set or we have clear line capability.
8510 * 8. redraw the characters from ScreenLines[].
8512 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
8513 * the scrollbar for the window. It does have insert line, use that if it
8514 * exists.
8516 result_empty = (row + line_count >= end);
8517 #ifdef FEAT_VERTSPLIT
8518 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
8519 type = USE_REDRAW;
8520 else
8521 #endif
8522 if (can_clear(T_CD) && result_empty)
8523 type = USE_T_CD;
8524 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
8525 type = USE_T_CAL;
8526 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
8527 type = USE_T_CDL;
8528 else if (*T_AL != NUL)
8529 type = USE_T_AL;
8530 else if (can_ce && result_empty)
8531 type = USE_T_CE;
8532 else if (*T_DL != NUL && result_empty)
8533 type = USE_T_DL;
8534 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
8535 type = USE_T_SR;
8536 else
8537 return FAIL;
8540 * For clearing the lines screen_del_lines() is used. This will also take
8541 * care of t_db if necessary.
8543 if (type == USE_T_CD || type == USE_T_CDL ||
8544 type == USE_T_CE || type == USE_T_DL)
8545 return screen_del_lines(off, row, line_count, end, FALSE, wp);
8548 * If text is retained below the screen, first clear or delete as many
8549 * lines at the bottom of the window as are about to be inserted so that
8550 * the deleted lines won't later surface during a screen_del_lines.
8552 if (*T_DB)
8553 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
8555 #ifdef FEAT_CLIPBOARD
8556 /* Remove a modeless selection when inserting lines halfway the screen
8557 * or not the full width of the screen. */
8558 if (off + row > 0
8559 # ifdef FEAT_VERTSPLIT
8560 || (wp != NULL && wp->w_width != Columns)
8561 # endif
8563 clip_clear_selection();
8564 else
8565 clip_scroll_selection(-line_count);
8566 #endif
8568 #ifdef FEAT_GUI
8569 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
8570 * scrolling is actually carried out. */
8571 gui_dont_update_cursor();
8572 #endif
8574 if (*T_CCS != NUL) /* cursor relative to region */
8575 cursor_row = row;
8576 else
8577 cursor_row = row + off;
8580 * Shift LineOffset[] line_count down to reflect the inserted lines.
8581 * Clear the inserted lines in ScreenLines[].
8583 row += off;
8584 end += off;
8585 for (i = 0; i < line_count; ++i)
8587 #ifdef FEAT_VERTSPLIT
8588 if (wp != NULL && wp->w_width != Columns)
8590 /* need to copy part of a line */
8591 j = end - 1 - i;
8592 while ((j -= line_count) >= row)
8593 linecopy(j + line_count, j, wp);
8594 j += line_count;
8595 if (can_clear((char_u *)" "))
8596 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
8597 else
8598 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
8599 LineWraps[j] = FALSE;
8601 else
8602 #endif
8604 j = end - 1 - i;
8605 temp = LineOffset[j];
8606 while ((j -= line_count) >= row)
8608 LineOffset[j + line_count] = LineOffset[j];
8609 LineWraps[j + line_count] = LineWraps[j];
8611 LineOffset[j + line_count] = temp;
8612 LineWraps[j + line_count] = FALSE;
8613 if (can_clear((char_u *)" "))
8614 lineclear(temp, (int)Columns);
8615 else
8616 lineinvalid(temp, (int)Columns);
8620 screen_stop_highlight();
8621 windgoto(cursor_row, 0);
8623 #ifdef FEAT_VERTSPLIT
8624 /* redraw the characters */
8625 if (type == USE_REDRAW)
8626 redraw_block(row, end, wp);
8627 else
8628 #endif
8629 if (type == USE_T_CAL)
8631 term_append_lines(line_count);
8632 screen_start(); /* don't know where cursor is now */
8634 else
8636 for (i = 0; i < line_count; i++)
8638 if (type == USE_T_AL)
8640 if (i && cursor_row != 0)
8641 windgoto(cursor_row, 0);
8642 out_str(T_AL);
8644 else /* type == USE_T_SR */
8645 out_str(T_SR);
8646 screen_start(); /* don't know where cursor is now */
8651 * With scroll-reverse and 'da' flag set we need to clear the lines that
8652 * have been scrolled down into the region.
8654 if (type == USE_T_SR && *T_DA)
8656 for (i = 0; i < line_count; ++i)
8658 windgoto(off + i, 0);
8659 out_str(T_CE);
8660 screen_start(); /* don't know where cursor is now */
8664 #ifdef FEAT_GUI
8665 gui_can_update_cursor();
8666 if (gui.in_use)
8667 out_flush(); /* always flush after a scroll */
8668 #endif
8669 return OK;
8673 * delete lines on the screen and update ScreenLines[]
8674 * 'end' is the line after the scrolled part. Normally it is Rows.
8675 * When scrolling region used 'off' is the offset from the top for the region.
8676 * 'row' and 'end' are relative to the start of the region.
8678 * Return OK for success, FAIL if the lines are not deleted.
8681 screen_del_lines(off, row, line_count, end, force, wp)
8682 int off;
8683 int row;
8684 int line_count;
8685 int end;
8686 int force; /* even when line_count > p_ttyscroll */
8687 win_T *wp UNUSED; /* NULL or window to use width from */
8689 int j;
8690 int i;
8691 unsigned temp;
8692 int cursor_row;
8693 int cursor_end;
8694 int result_empty; /* result is empty until end of region */
8695 int can_delete; /* deleting line codes can be used */
8696 int type;
8699 * FAIL if
8700 * - there is no valid screen
8701 * - the screen has to be redrawn completely
8702 * - the line count is less than one
8703 * - the line count is more than 'ttyscroll'
8705 if (!screen_valid(TRUE) || line_count <= 0 ||
8706 (!force && line_count > p_ttyscroll))
8707 return FAIL;
8710 * Check if the rest of the current region will become empty.
8712 result_empty = row + line_count >= end;
8715 * We can delete lines only when 'db' flag not set or when 'ce' option
8716 * available.
8718 can_delete = (*T_DB == NUL || can_clear(T_CE));
8721 * There are six ways to delete lines:
8722 * 0. When in a vertically split window and t_CV isn't set, redraw the
8723 * characters from ScreenLines[].
8724 * 1. Use T_CD if it exists and the result is empty.
8725 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
8726 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
8727 * none of the other ways work.
8728 * 4. Use T_CE (erase line) if the result is empty.
8729 * 5. Use T_DL (delete line) if it exists.
8730 * 6. redraw the characters from ScreenLines[].
8732 #ifdef FEAT_VERTSPLIT
8733 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
8734 type = USE_REDRAW;
8735 else
8736 #endif
8737 if (can_clear(T_CD) && result_empty)
8738 type = USE_T_CD;
8739 #if defined(__BEOS__) && defined(BEOS_DR8)
8741 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
8742 * its internal termcap... this works okay for tests which test *T_DB !=
8743 * NUL. It has the disadvantage that the user cannot use any :set t_*
8744 * command to get T_DB (back) to empty_option, only :set term=... will do
8745 * the trick...
8746 * Anyway, this hack will hopefully go away with the next OS release.
8747 * (Olaf Seibert)
8749 else if (row == 0 && T_DB == empty_option
8750 && (line_count == 1 || *T_CDL == NUL))
8751 #else
8752 else if (row == 0 && (
8753 #ifndef AMIGA
8754 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
8755 * up, so use delete-line command */
8756 line_count == 1 ||
8757 #endif
8758 *T_CDL == NUL))
8759 #endif
8760 type = USE_NL;
8761 else if (*T_CDL != NUL && line_count > 1 && can_delete)
8762 type = USE_T_CDL;
8763 else if (can_clear(T_CE) && result_empty
8764 #ifdef FEAT_VERTSPLIT
8765 && (wp == NULL || wp->w_width == Columns)
8766 #endif
8768 type = USE_T_CE;
8769 else if (*T_DL != NUL && can_delete)
8770 type = USE_T_DL;
8771 else if (*T_CDL != NUL && can_delete)
8772 type = USE_T_CDL;
8773 else
8774 return FAIL;
8776 #ifdef FEAT_CLIPBOARD
8777 /* Remove a modeless selection when deleting lines halfway the screen or
8778 * not the full width of the screen. */
8779 if (off + row > 0
8780 # ifdef FEAT_VERTSPLIT
8781 || (wp != NULL && wp->w_width != Columns)
8782 # endif
8784 clip_clear_selection();
8785 else
8786 clip_scroll_selection(line_count);
8787 #endif
8789 #ifdef FEAT_GUI
8790 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
8791 * scrolling is actually carried out. */
8792 gui_dont_update_cursor();
8793 #endif
8795 if (*T_CCS != NUL) /* cursor relative to region */
8797 cursor_row = row;
8798 cursor_end = end;
8800 else
8802 cursor_row = row + off;
8803 cursor_end = end + off;
8807 * Now shift LineOffset[] line_count up to reflect the deleted lines.
8808 * Clear the inserted lines in ScreenLines[].
8810 row += off;
8811 end += off;
8812 for (i = 0; i < line_count; ++i)
8814 #ifdef FEAT_VERTSPLIT
8815 if (wp != NULL && wp->w_width != Columns)
8817 /* need to copy part of a line */
8818 j = row + i;
8819 while ((j += line_count) <= end - 1)
8820 linecopy(j - line_count, j, wp);
8821 j -= line_count;
8822 if (can_clear((char_u *)" "))
8823 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
8824 else
8825 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
8826 LineWraps[j] = FALSE;
8828 else
8829 #endif
8831 /* whole width, moving the line pointers is faster */
8832 j = row + i;
8833 temp = LineOffset[j];
8834 while ((j += line_count) <= end - 1)
8836 LineOffset[j - line_count] = LineOffset[j];
8837 LineWraps[j - line_count] = LineWraps[j];
8839 LineOffset[j - line_count] = temp;
8840 LineWraps[j - line_count] = FALSE;
8841 if (can_clear((char_u *)" "))
8842 lineclear(temp, (int)Columns);
8843 else
8844 lineinvalid(temp, (int)Columns);
8848 screen_stop_highlight();
8850 #ifdef FEAT_VERTSPLIT
8851 /* redraw the characters */
8852 if (type == USE_REDRAW)
8853 redraw_block(row, end, wp);
8854 else
8855 #endif
8856 if (type == USE_T_CD) /* delete the lines */
8858 windgoto(cursor_row, 0);
8859 out_str(T_CD);
8860 screen_start(); /* don't know where cursor is now */
8862 else if (type == USE_T_CDL)
8864 windgoto(cursor_row, 0);
8865 term_delete_lines(line_count);
8866 screen_start(); /* don't know where cursor is now */
8869 * Deleting lines at top of the screen or scroll region: Just scroll
8870 * the whole screen (scroll region) up by outputting newlines on the
8871 * last line.
8873 else if (type == USE_NL)
8875 windgoto(cursor_end - 1, 0);
8876 for (i = line_count; --i >= 0; )
8877 out_char('\n'); /* cursor will remain on same line */
8879 else
8881 for (i = line_count; --i >= 0; )
8883 if (type == USE_T_DL)
8885 windgoto(cursor_row, 0);
8886 out_str(T_DL); /* delete a line */
8888 else /* type == USE_T_CE */
8890 windgoto(cursor_row + i, 0);
8891 out_str(T_CE); /* erase a line */
8893 screen_start(); /* don't know where cursor is now */
8898 * If the 'db' flag is set, we need to clear the lines that have been
8899 * scrolled up at the bottom of the region.
8901 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
8903 for (i = line_count; i > 0; --i)
8905 windgoto(cursor_end - i, 0);
8906 out_str(T_CE); /* erase a line */
8907 screen_start(); /* don't know where cursor is now */
8911 #ifdef FEAT_GUI
8912 gui_can_update_cursor();
8913 if (gui.in_use)
8914 out_flush(); /* always flush after a scroll */
8915 #endif
8917 return OK;
8921 * show the current mode and ruler
8923 * If clear_cmdline is TRUE, clear the rest of the cmdline.
8924 * If clear_cmdline is FALSE there may be a message there that needs to be
8925 * cleared only if a mode is shown.
8926 * Return the length of the message (0 if no message).
8929 showmode()
8931 int need_clear;
8932 int length = 0;
8933 int do_mode;
8934 int attr;
8935 int nwr_save;
8936 #ifdef FEAT_INS_EXPAND
8937 int sub_attr;
8938 #endif
8940 do_mode = ((p_smd && msg_silent == 0)
8941 && ((State & INSERT)
8942 || restart_edit
8943 #ifdef FEAT_VISUAL
8944 || VIsual_active
8945 #endif
8947 if (do_mode || Recording)
8950 * Don't show mode right now, when not redrawing or inside a mapping.
8951 * Call char_avail() only when we are going to show something, because
8952 * it takes a bit of time.
8954 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
8956 redraw_cmdline = TRUE; /* show mode later */
8957 return 0;
8960 nwr_save = need_wait_return;
8962 /* wait a bit before overwriting an important message */
8963 check_for_delay(FALSE);
8965 /* if the cmdline is more than one line high, erase top lines */
8966 need_clear = clear_cmdline;
8967 if (clear_cmdline && cmdline_row < Rows - 1)
8968 msg_clr_cmdline(); /* will reset clear_cmdline */
8970 /* Position on the last line in the window, column 0 */
8971 msg_pos_mode();
8972 cursor_off();
8973 attr = hl_attr(HLF_CM); /* Highlight mode */
8974 if (do_mode)
8976 MSG_PUTS_ATTR("--", attr);
8977 #if defined(FEAT_XIM)
8978 # if 0 /* old version, changed by SungHyun Nam July 2008 */
8979 if (xic != NULL && im_get_status() && !p_imdisable
8980 && curbuf->b_p_iminsert == B_IMODE_IM)
8981 # else
8982 if (
8983 # ifdef HAVE_GTK2
8984 preedit_get_status()
8985 # else
8986 im_get_status()
8987 # endif
8989 # endif
8990 # ifdef HAVE_GTK2 /* most of the time, it's not XIM being used */
8991 MSG_PUTS_ATTR(" IM", attr);
8992 # else
8993 MSG_PUTS_ATTR(" XIM", attr);
8994 # endif
8995 #endif
8996 #if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
8997 if (gui.in_use)
8999 if (hangul_input_state_get())
9000 MSG_PUTS_ATTR(" \307\321\261\333", attr); /* HANGUL */
9002 #endif
9003 #ifdef FEAT_INS_EXPAND
9004 if (edit_submode != NULL) /* CTRL-X in Insert mode */
9006 /* These messages can get long, avoid a wrap in a narrow
9007 * window. Prefer showing edit_submode_extra. */
9008 length = (Rows - msg_row) * Columns - 3;
9009 if (edit_submode_extra != NULL)
9010 length -= vim_strsize(edit_submode_extra);
9011 if (length > 0)
9013 if (edit_submode_pre != NULL)
9014 length -= vim_strsize(edit_submode_pre);
9015 if (length - vim_strsize(edit_submode) > 0)
9017 if (edit_submode_pre != NULL)
9018 msg_puts_attr(edit_submode_pre, attr);
9019 msg_puts_attr(edit_submode, attr);
9021 if (edit_submode_extra != NULL)
9023 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
9024 if ((int)edit_submode_highl < (int)HLF_COUNT)
9025 sub_attr = hl_attr(edit_submode_highl);
9026 else
9027 sub_attr = attr;
9028 msg_puts_attr(edit_submode_extra, sub_attr);
9031 length = 0;
9033 else
9034 #endif
9036 #ifdef FEAT_VREPLACE
9037 if (State & VREPLACE_FLAG)
9038 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
9039 else
9040 #endif
9041 if (State & REPLACE_FLAG)
9042 MSG_PUTS_ATTR(_(" REPLACE"), attr);
9043 else if (State & INSERT)
9045 #ifdef FEAT_RIGHTLEFT
9046 if (p_ri)
9047 MSG_PUTS_ATTR(_(" REVERSE"), attr);
9048 #endif
9049 MSG_PUTS_ATTR(_(" INSERT"), attr);
9051 else if (restart_edit == 'I')
9052 MSG_PUTS_ATTR(_(" (insert)"), attr);
9053 else if (restart_edit == 'R')
9054 MSG_PUTS_ATTR(_(" (replace)"), attr);
9055 else if (restart_edit == 'V')
9056 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
9057 #ifdef FEAT_RIGHTLEFT
9058 if (p_hkmap)
9059 MSG_PUTS_ATTR(_(" Hebrew"), attr);
9060 # ifdef FEAT_FKMAP
9061 if (p_fkmap)
9062 MSG_PUTS_ATTR(farsi_text_5, attr);
9063 # endif
9064 #endif
9065 #ifdef FEAT_KEYMAP
9066 if (State & LANGMAP)
9068 # ifdef FEAT_ARABIC
9069 if (curwin->w_p_arab)
9070 MSG_PUTS_ATTR(_(" Arabic"), attr);
9071 else
9072 # endif
9073 MSG_PUTS_ATTR(_(" (lang)"), attr);
9075 #endif
9076 if ((State & INSERT) && p_paste)
9077 MSG_PUTS_ATTR(_(" (paste)"), attr);
9079 #ifdef FEAT_VISUAL
9080 if (VIsual_active)
9082 char *p;
9084 /* Don't concatenate separate words to avoid translation
9085 * problems. */
9086 switch ((VIsual_select ? 4 : 0)
9087 + (VIsual_mode == Ctrl_V) * 2
9088 + (VIsual_mode == 'V'))
9090 case 0: p = N_(" VISUAL"); break;
9091 case 1: p = N_(" VISUAL LINE"); break;
9092 case 2: p = N_(" VISUAL BLOCK"); break;
9093 case 4: p = N_(" SELECT"); break;
9094 case 5: p = N_(" SELECT LINE"); break;
9095 default: p = N_(" SELECT BLOCK"); break;
9097 MSG_PUTS_ATTR(_(p), attr);
9099 #endif
9100 MSG_PUTS_ATTR(" --", attr);
9103 need_clear = TRUE;
9105 if (Recording
9106 #ifdef FEAT_INS_EXPAND
9107 && edit_submode == NULL /* otherwise it gets too long */
9108 #endif
9111 MSG_PUTS_ATTR(_("recording"), attr);
9112 need_clear = TRUE;
9115 mode_displayed = TRUE;
9116 if (need_clear || clear_cmdline)
9117 msg_clr_eos();
9118 msg_didout = FALSE; /* overwrite this message */
9119 length = msg_col;
9120 msg_col = 0;
9121 need_wait_return = nwr_save; /* never ask for hit-return for this */
9123 else if (clear_cmdline && msg_silent == 0)
9124 /* Clear the whole command line. Will reset "clear_cmdline". */
9125 msg_clr_cmdline();
9127 #ifdef FEAT_CMDL_INFO
9128 # ifdef FEAT_VISUAL
9129 /* In Visual mode the size of the selected area must be redrawn. */
9130 if (VIsual_active)
9131 clear_showcmd();
9132 # endif
9134 /* If the last window has no status line, the ruler is after the mode
9135 * message and must be redrawn */
9136 if (redrawing()
9137 # ifdef FEAT_WINDOWS
9138 && lastwin->w_status_height == 0
9139 # endif
9141 win_redr_ruler(lastwin, TRUE);
9142 #endif
9143 redraw_cmdline = FALSE;
9144 clear_cmdline = FALSE;
9146 return length;
9150 * Position for a mode message.
9152 static void
9153 msg_pos_mode()
9155 msg_col = 0;
9156 msg_row = Rows - 1;
9160 * Delete mode message. Used when ESC is typed which is expected to end
9161 * Insert mode (but Insert mode didn't end yet!).
9162 * Caller should check "mode_displayed".
9164 void
9165 unshowmode(force)
9166 int force;
9169 * Don't delete it right now, when not redrawing or insided a mapping.
9171 if (!redrawing() || (!force && char_avail() && !KeyTyped))
9172 redraw_cmdline = TRUE; /* delete mode later */
9173 else
9175 msg_pos_mode();
9176 if (Recording)
9177 MSG_PUTS_ATTR(_("recording"), hl_attr(HLF_CM));
9178 msg_clr_eos();
9182 #if defined(FEAT_WINDOWS)
9184 * Draw the tab pages line at the top of the Vim window.
9186 static void
9187 draw_tabline()
9189 int tabcount = 0;
9190 tabpage_T *tp;
9191 int tabwidth;
9192 int col = 0;
9193 int scol = 0;
9194 int attr;
9195 win_T *wp;
9196 win_T *cwp;
9197 int wincount;
9198 int modified;
9199 int c;
9200 int len;
9201 int attr_sel = hl_attr(HLF_TPS);
9202 int attr_nosel = hl_attr(HLF_TP);
9203 int attr_fill = hl_attr(HLF_TPF);
9204 char_u *p;
9205 int room;
9206 int use_sep_chars = (t_colors < 8
9207 #ifdef FEAT_GUI
9208 && !gui.in_use
9209 #endif
9212 redraw_tabline = FALSE;
9214 #ifdef FEAT_GUI_TABLINE
9215 /* Take care of a GUI tabline. */
9216 if (gui_use_tabline())
9218 gui_update_tabline();
9219 return;
9221 #endif
9223 if (tabline_height() < 1)
9224 return;
9226 #if defined(FEAT_STL_OPT)
9228 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
9229 for (scol = 0; scol < Columns; ++scol)
9230 TabPageIdxs[scol] = 0;
9232 /* Use the 'tabline' option if it's set. */
9233 if (*p_tal != NUL)
9235 int save_called_emsg = called_emsg;
9237 /* Check for an error. If there is one we would loop in redrawing the
9238 * screen. Avoid that by making 'tabline' empty. */
9239 called_emsg = FALSE;
9240 win_redr_custom(NULL, FALSE);
9241 if (called_emsg)
9242 set_string_option_direct((char_u *)"tabline", -1,
9243 (char_u *)"", OPT_FREE, SID_ERROR);
9244 called_emsg |= save_called_emsg;
9246 else
9247 #endif
9249 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
9250 ++tabcount;
9252 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
9253 if (tabwidth < 6)
9254 tabwidth = 6;
9256 attr = attr_nosel;
9257 tabcount = 0;
9258 scol = 0;
9259 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
9260 tp = tp->tp_next)
9262 scol = col;
9264 if (tp->tp_topframe == topframe)
9265 attr = attr_sel;
9266 if (use_sep_chars && col > 0)
9267 screen_putchar('|', 0, col++, attr);
9269 if (tp->tp_topframe != topframe)
9270 attr = attr_nosel;
9272 screen_putchar(' ', 0, col++, attr);
9274 if (tp == curtab)
9276 cwp = curwin;
9277 wp = firstwin;
9279 else
9281 cwp = tp->tp_curwin;
9282 wp = tp->tp_firstwin;
9285 modified = FALSE;
9286 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
9287 if (bufIsChanged(wp->w_buffer))
9288 modified = TRUE;
9289 if (modified || wincount > 1)
9291 if (wincount > 1)
9293 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
9294 len = (int)STRLEN(NameBuff);
9295 if (col + len >= Columns - 3)
9296 break;
9297 screen_puts_len(NameBuff, len, 0, col,
9298 #if defined(FEAT_SYN_HL)
9299 hl_combine_attr(attr, hl_attr(HLF_T))
9300 #else
9301 attr
9302 #endif
9304 col += len;
9306 if (modified)
9307 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
9308 screen_putchar(' ', 0, col++, attr);
9311 room = scol - col + tabwidth - 1;
9312 if (room > 0)
9314 /* Get buffer name in NameBuff[] */
9315 get_trans_bufname(cwp->w_buffer);
9316 shorten_dir(NameBuff);
9317 len = vim_strsize(NameBuff);
9318 p = NameBuff;
9319 #ifdef FEAT_MBYTE
9320 if (has_mbyte)
9321 while (len > room)
9323 len -= ptr2cells(p);
9324 mb_ptr_adv(p);
9326 else
9327 #endif
9328 if (len > room)
9330 p += len - room;
9331 len = room;
9333 if (len > Columns - col - 1)
9334 len = Columns - col - 1;
9336 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
9337 col += len;
9339 screen_putchar(' ', 0, col++, attr);
9341 /* Store the tab page number in TabPageIdxs[], so that
9342 * jump_to_mouse() knows where each one is. */
9343 ++tabcount;
9344 while (scol < col)
9345 TabPageIdxs[scol++] = tabcount;
9348 if (use_sep_chars)
9349 c = '_';
9350 else
9351 c = ' ';
9352 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
9354 /* Put an "X" for closing the current tab if there are several. */
9355 if (first_tabpage->tp_next != NULL)
9357 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
9358 TabPageIdxs[Columns - 1] = -999;
9362 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
9363 * set. */
9364 redraw_tabline = FALSE;
9368 * Get buffer name for "buf" into NameBuff[].
9369 * Takes care of special buffer names and translates special characters.
9371 void
9372 get_trans_bufname(buf)
9373 buf_T *buf;
9375 if (buf_spname(buf) != NULL)
9376 STRCPY(NameBuff, buf_spname(buf));
9377 else
9378 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
9379 trans_characters(NameBuff, MAXPATHL);
9381 #endif
9383 #if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
9385 * Get the character to use in a status line. Get its attributes in "*attr".
9387 static int
9388 fillchar_status(attr, is_curwin)
9389 int *attr;
9390 int is_curwin;
9392 int fill;
9393 if (is_curwin)
9395 *attr = hl_attr(HLF_S);
9396 fill = fill_stl;
9398 else
9400 *attr = hl_attr(HLF_SNC);
9401 fill = fill_stlnc;
9403 /* Use fill when there is highlighting, and highlighting of current
9404 * window differs, or the fillchars differ, or this is not the
9405 * current window */
9406 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
9407 || !is_curwin || firstwin == lastwin)
9408 || (fill_stl != fill_stlnc)))
9409 return fill;
9410 if (is_curwin)
9411 return '^';
9412 return '=';
9414 #endif
9416 #ifdef FEAT_VERTSPLIT
9418 * Get the character to use in a separator between vertically split windows.
9419 * Get its attributes in "*attr".
9421 static int
9422 fillchar_vsep(attr)
9423 int *attr;
9425 *attr = hl_attr(HLF_C);
9426 if (*attr == 0 && fill_vert == ' ')
9427 return '|';
9428 else
9429 return fill_vert;
9431 #endif
9434 * Return TRUE if redrawing should currently be done.
9437 redrawing()
9439 return (!RedrawingDisabled
9440 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
9444 * Return TRUE if printing messages should currently be done.
9447 messaging()
9449 return (!(p_lz && char_avail() && !KeyTyped));
9453 * Show current status info in ruler and various other places
9454 * If always is FALSE, only show ruler if position has changed.
9456 void
9457 showruler(always)
9458 int always;
9460 if (!always && !redrawing())
9461 return;
9462 #ifdef FEAT_INS_EXPAND
9463 if (pum_visible())
9465 # ifdef FEAT_WINDOWS
9466 /* Don't redraw right now, do it later. */
9467 curwin->w_redr_status = TRUE;
9468 # endif
9469 return;
9471 #endif
9472 #if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
9473 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
9475 redraw_custum_statusline(curwin);
9477 else
9478 #endif
9479 #ifdef FEAT_CMDL_INFO
9480 win_redr_ruler(curwin, always);
9481 #endif
9483 #ifdef FEAT_TITLE
9484 if (need_maketitle
9485 # ifdef FEAT_STL_OPT
9486 || (p_icon && (stl_syntax & STL_IN_ICON))
9487 || (p_title && (stl_syntax & STL_IN_TITLE))
9488 # endif
9490 maketitle();
9491 #endif
9492 #ifdef FEAT_WINDOWS
9493 /* Redraw the tab pages line if needed. */
9494 if (redraw_tabline)
9495 draw_tabline();
9496 #endif
9499 #ifdef FEAT_CMDL_INFO
9500 static void
9501 win_redr_ruler(wp, always)
9502 win_T *wp;
9503 int always;
9505 #define RULER_BUF_LEN 70
9506 char_u buffer[RULER_BUF_LEN];
9507 int row;
9508 int fillchar;
9509 int attr;
9510 int empty_line = FALSE;
9511 colnr_T virtcol;
9512 int i;
9513 size_t len;
9514 int o;
9515 #ifdef FEAT_VERTSPLIT
9516 int this_ru_col;
9517 int off = 0;
9518 int width = Columns;
9519 # define WITH_OFF(x) x
9520 # define WITH_WIDTH(x) x
9521 #else
9522 # define WITH_OFF(x) 0
9523 # define WITH_WIDTH(x) Columns
9524 # define this_ru_col ru_col
9525 #endif
9527 /* If 'ruler' off or redrawing disabled, don't do anything */
9528 if (!p_ru)
9529 return;
9532 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
9533 * after deleting lines, before cursor.lnum is corrected.
9535 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
9536 return;
9538 #ifdef FEAT_INS_EXPAND
9539 /* Don't draw the ruler while doing insert-completion, it might overwrite
9540 * the (long) mode message. */
9541 # ifdef FEAT_WINDOWS
9542 if (wp == lastwin && lastwin->w_status_height == 0)
9543 # endif
9544 if (edit_submode != NULL)
9545 return;
9546 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
9547 if (pum_visible())
9548 return;
9549 #endif
9551 #ifdef FEAT_STL_OPT
9552 if (*p_ruf)
9554 int save_called_emsg = called_emsg;
9556 called_emsg = FALSE;
9557 win_redr_custom(wp, TRUE);
9558 if (called_emsg)
9559 set_string_option_direct((char_u *)"rulerformat", -1,
9560 (char_u *)"", OPT_FREE, SID_ERROR);
9561 called_emsg |= save_called_emsg;
9562 return;
9564 #endif
9567 * Check if not in Insert mode and the line is empty (will show "0-1").
9569 if (!(State & INSERT)
9570 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
9571 empty_line = TRUE;
9574 * Only draw the ruler when something changed.
9576 validate_virtcol_win(wp);
9577 if ( redraw_cmdline
9578 || always
9579 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
9580 || wp->w_cursor.col != wp->w_ru_cursor.col
9581 || wp->w_virtcol != wp->w_ru_virtcol
9582 #ifdef FEAT_VIRTUALEDIT
9583 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
9584 #endif
9585 || wp->w_topline != wp->w_ru_topline
9586 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
9587 #ifdef FEAT_DIFF
9588 || wp->w_topfill != wp->w_ru_topfill
9589 #endif
9590 || empty_line != wp->w_ru_empty)
9592 cursor_off();
9593 #ifdef FEAT_WINDOWS
9594 if (wp->w_status_height)
9596 row = W_WINROW(wp) + wp->w_height;
9597 fillchar = fillchar_status(&attr, wp == curwin);
9598 # ifdef FEAT_VERTSPLIT
9599 off = W_WINCOL(wp);
9600 width = W_WIDTH(wp);
9601 # endif
9603 else
9604 #endif
9606 row = Rows - 1;
9607 fillchar = ' ';
9608 attr = 0;
9609 #ifdef FEAT_VERTSPLIT
9610 width = Columns;
9611 off = 0;
9612 #endif
9615 /* In list mode virtcol needs to be recomputed */
9616 virtcol = wp->w_virtcol;
9617 if (wp->w_p_list && lcs_tab1 == NUL)
9619 wp->w_p_list = FALSE;
9620 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
9621 wp->w_p_list = TRUE;
9625 * Some sprintfs return the length, some return a pointer.
9626 * To avoid portability problems we use strlen() here.
9628 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
9629 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
9630 ? 0L
9631 : (long)(wp->w_cursor.lnum));
9632 len = STRLEN(buffer);
9633 col_print(buffer + len, RULER_BUF_LEN - len,
9634 empty_line ? 0 : (int)wp->w_cursor.col + 1,
9635 (int)virtcol + 1);
9638 * Add a "50%" if there is room for it.
9639 * On the last line, don't print in the last column (scrolls the
9640 * screen up on some terminals).
9642 i = (int)STRLEN(buffer);
9643 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
9644 o = i + vim_strsize(buffer + i + 1);
9645 #ifdef FEAT_WINDOWS
9646 if (wp->w_status_height == 0) /* can't use last char of screen */
9647 #endif
9648 ++o;
9649 #ifdef FEAT_VERTSPLIT
9650 this_ru_col = ru_col - (Columns - width);
9651 if (this_ru_col < 0)
9652 this_ru_col = 0;
9653 #endif
9654 /* Never use more than half the window/screen width, leave the other
9655 * half for the filename. */
9656 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
9657 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
9658 if (this_ru_col + o < WITH_WIDTH(width))
9660 while (this_ru_col + o < WITH_WIDTH(width))
9662 #ifdef FEAT_MBYTE
9663 if (has_mbyte)
9664 i += (*mb_char2bytes)(fillchar, buffer + i);
9665 else
9666 #endif
9667 buffer[i++] = fillchar;
9668 ++o;
9670 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
9672 /* Truncate at window boundary. */
9673 #ifdef FEAT_MBYTE
9674 if (has_mbyte)
9676 o = 0;
9677 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
9679 o += (*mb_ptr2cells)(buffer + i);
9680 if (this_ru_col + o > WITH_WIDTH(width))
9682 buffer[i] = NUL;
9683 break;
9687 else
9688 #endif
9689 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
9690 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
9692 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
9693 i = redraw_cmdline;
9694 screen_fill(row, row + 1,
9695 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
9696 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
9697 fillchar, fillchar, attr);
9698 /* don't redraw the cmdline because of showing the ruler */
9699 redraw_cmdline = i;
9700 wp->w_ru_cursor = wp->w_cursor;
9701 wp->w_ru_virtcol = wp->w_virtcol;
9702 wp->w_ru_empty = empty_line;
9703 wp->w_ru_topline = wp->w_topline;
9704 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
9705 #ifdef FEAT_DIFF
9706 wp->w_ru_topfill = wp->w_topfill;
9707 #endif
9710 #endif
9712 #if defined(FEAT_LINEBREAK) || defined(PROTO)
9714 * Return the width of the 'number' and 'relativenumber' column.
9715 * Caller may need to check if 'number' or 'relativenumber' is set.
9716 * Otherwise it depends on 'numberwidth' and the line count.
9719 number_width(wp)
9720 win_T *wp;
9722 int n;
9723 linenr_T lnum;
9725 if (wp->w_p_nu)
9726 /* 'number' */
9727 lnum = wp->w_buffer->b_ml.ml_line_count;
9728 else
9729 /* 'relativenumber' */
9730 lnum = wp->w_height;
9732 if (lnum == wp->w_nrwidth_line_count)
9733 return wp->w_nrwidth_width;
9734 wp->w_nrwidth_line_count = lnum;
9736 n = 0;
9739 lnum /= 10;
9740 ++n;
9741 } while (lnum > 0);
9743 /* 'numberwidth' gives the minimal width plus one */
9744 if (n < wp->w_p_nuw - 1)
9745 n = wp->w_p_nuw - 1;
9747 wp->w_nrwidth_width = n;
9748 return n;
9750 #endif