blob: df5489f8bffed74aa66f5bdd61289d49faf09041 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
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 */
9
10/*
11 * screen.c: code for displaying on the screen
12 *
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.
16 *
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.
23 *
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
Bram Moolenaar70c49c12010-03-23 15:36:35 +010028 * character without composing chars ScreenLinesUC[] will be 0 and
29 * ScreenLinesC[][] is not used. When the character occupies two display
30 * cells the next byte in ScreenLines[] is 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +000031 * ScreenLinesC[][] contain up to 'maxcombine' composing characters
Bram Moolenaar70c49c12010-03-23 15:36:35 +010032 * (drawn on top of the first character). There is 0 after the last one used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000033 * ScreenLines2[] is only used for euc-jp to store the second byte if the
34 * first byte is 0x8e (single-width character).
35 *
36 * The screen_*() functions write to the screen and handle updating
37 * ScreenLines[].
38 *
39 * update_screen() is the function that updates all windows and status lines.
40 * It is called form the main loop when must_redraw is non-zero. It may be
Bram Moolenaar2c7a7632007-05-10 18:19:11 +000041 * called from other places when an immediate screen update is needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +000042 *
43 * The part of the buffer that is displayed in a window is set with:
44 * - w_topline (first buffer line in window)
45 * - w_topfill (filler line above the first line)
46 * - w_leftcol (leftmost window cell in window),
47 * - w_skipcol (skipped window cells of first line)
48 *
49 * Commands that only move the cursor around in a window, do not need to take
50 * action to update the display. The main loop will check if w_topline is
51 * valid and update it (scroll the window) when needed.
52 *
53 * Commands that scroll a window change w_topline and must call
54 * check_cursor() to move the cursor into the visible part of the window, and
55 * call redraw_later(VALID) to have the window displayed by update_screen()
56 * later.
57 *
58 * Commands that change text in the buffer must call changed_bytes() or
59 * changed_lines() to mark the area that changed and will require updating
60 * later. The main loop will call update_screen(), which will update each
61 * window that shows the changed buffer. This assumes text above the change
62 * can remain displayed as it is. Text after the change may need updating for
63 * scrolling, folding and syntax highlighting.
64 *
65 * Commands that change how a window is displayed (e.g., setting 'list') or
66 * invalidate the contents of a window in another way (e.g., change fold
67 * settings), must call redraw_later(NOT_VALID) to have the whole window
68 * redisplayed by update_screen() later.
69 *
70 * Commands that change how a buffer is displayed (e.g., setting 'tabstop')
71 * must call redraw_curbuf_later(NOT_VALID) to have all the windows for the
72 * buffer redisplayed by update_screen() later.
73 *
Bram Moolenaar600dddc2006-03-12 22:05:10 +000074 * Commands that change highlighting and possibly cause a scroll too must call
75 * redraw_later(SOME_VALID) to update the whole window but still use scrolling
76 * to avoid redrawing everything. But the length of displayed lines must not
77 * change, use NOT_VALID then.
78 *
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 * Commands that move the window position must call redraw_later(NOT_VALID).
80 * TODO: should minimize redrawing by scrolling when possible.
81 *
82 * Commands that change everything (e.g., resizing the screen) must call
83 * redraw_all_later(NOT_VALID) or redraw_all_later(CLEAR).
84 *
85 * Things that are handled indirectly:
86 * - When messages scroll the screen up, msg_scrolled will be set and
87 * update_screen() called to redraw.
88 */
89
90#include "vim.h"
91
92/*
93 * The attributes that are actually active for writing to the screen.
94 */
95static int screen_attr = 0;
96
97/*
98 * Positioning the cursor is reduced by remembering the last position.
99 * Mostly used by windgoto() and screen_char().
100 */
101static int screen_cur_row, screen_cur_col; /* last known cursor position */
102
103#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104static match_T search_hl; /* used for 'hlsearch' highlight matching */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105#endif
106
107#ifdef FEAT_FOLDING
108static foldinfo_T win_foldinfo; /* info for 'foldcolumn' */
109#endif
110
111/*
112 * Buffer for one screen line (characters and attributes).
113 */
114static schar_T *current_ScreenLine;
115
116static void win_update __ARGS((win_T *wp));
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000117static void win_draw_end __ARGS((win_T *wp, int c1, int c2, int row, int endrow, hlf_T hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118#ifdef FEAT_FOLDING
119static void fold_line __ARGS((win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row));
120static void fill_foldcolumn __ARGS((char_u *p, win_T *wp, int closed, linenr_T lnum));
121static void copy_text_attr __ARGS((int off, char_u *buf, int len, int attr));
122#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000123static int win_line __ARGS((win_T *, linenr_T, int, int, int nochange));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124static int char_needs_redraw __ARGS((int off_from, int off_to, int cols));
125#ifdef FEAT_RIGHTLEFT
126static void screen_line __ARGS((int row, int coloff, int endcol, int clear_width, int rlflag));
127# define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c), (rl))
128#else
129static void screen_line __ARGS((int row, int coloff, int endcol, int clear_width));
130# define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c))
131#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132#ifdef FEAT_VERTSPLIT
133static void draw_vsep_win __ARGS((win_T *wp, int row));
134#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +0000135#ifdef FEAT_STL_OPT
Bram Moolenaar362f3562009-11-03 16:20:34 +0000136static void redraw_custom_statusline __ARGS((win_T *wp));
Bram Moolenaar238a5642006-02-21 22:12:05 +0000137#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000139#define SEARCH_HL_PRIORITY 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140static void start_search_hl __ARGS((void));
141static void end_search_hl __ARGS((void));
142static void prepare_search_hl __ARGS((win_T *wp, linenr_T lnum));
143static void next_search_hl __ARGS((win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol));
144#endif
145static void screen_start_highlight __ARGS((int attr));
146static void screen_char __ARGS((unsigned off, int row, int col));
147#ifdef FEAT_MBYTE
148static void screen_char_2 __ARGS((unsigned off, int row, int col));
149#endif
150static void screenclear2 __ARGS((void));
151static void lineclear __ARGS((unsigned off, int width));
152static void lineinvalid __ARGS((unsigned off, int width));
153#ifdef FEAT_VERTSPLIT
154static void linecopy __ARGS((int to, int from, win_T *wp));
155static void redraw_block __ARGS((int row, int end, win_T *wp));
156#endif
157static int win_do_lines __ARGS((win_T *wp, int row, int line_count, int mayclear, int del));
158static void win_rest_invalid __ARGS((win_T *wp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159static void msg_pos_mode __ARGS((void));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000160#if defined(FEAT_WINDOWS)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000161static void draw_tabline __ARGS((void));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000162#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
164static int fillchar_status __ARGS((int *attr, int is_curwin));
165#endif
166#ifdef FEAT_VERTSPLIT
167static int fillchar_vsep __ARGS((int *attr));
168#endif
169#ifdef FEAT_STL_OPT
Bram Moolenaar9372a112005-12-06 19:59:18 +0000170static void win_redr_custom __ARGS((win_T *wp, int draw_ruler));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171#endif
172#ifdef FEAT_CMDL_INFO
173static void win_redr_ruler __ARGS((win_T *wp, int always));
174#endif
175
176#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
177/* Ugly global: overrule attribute used by screen_char() */
178static int screen_char_attr = 0;
179#endif
180
181/*
182 * Redraw the current window later, with update_screen(type).
183 * Set must_redraw only if not already set to a higher value.
184 * e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
185 */
186 void
187redraw_later(type)
188 int type;
189{
190 redraw_win_later(curwin, type);
191}
192
193 void
194redraw_win_later(wp, type)
195 win_T *wp;
196 int type;
197{
198 if (wp->w_redr_type < type)
199 {
200 wp->w_redr_type = type;
201 if (type >= NOT_VALID)
202 wp->w_lines_valid = 0;
203 if (must_redraw < type) /* must_redraw is the maximum of all windows */
204 must_redraw = type;
205 }
206}
207
208/*
209 * Force a complete redraw later. Also resets the highlighting. To be used
210 * after executing a shell command that messes up the screen.
211 */
212 void
213redraw_later_clear()
214{
215 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000216#ifdef FEAT_GUI
217 if (gui.in_use)
218 /* Use a code that will reset gui.highlight_mask in
219 * gui_stop_highlight(). */
220 screen_attr = HL_ALL + 1;
221 else
222#endif
223 /* Use attributes that is very unlikely to appear in text. */
224 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225}
226
227/*
228 * Mark all windows to be redrawn later.
229 */
230 void
231redraw_all_later(type)
232 int type;
233{
234 win_T *wp;
235
236 FOR_ALL_WINDOWS(wp)
237 {
238 redraw_win_later(wp, type);
239 }
240}
241
242/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000243 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244 */
245 void
246redraw_curbuf_later(type)
247 int type;
248{
249 redraw_buf_later(curbuf, type);
250}
251
252 void
253redraw_buf_later(buf, type)
254 buf_T *buf;
255 int type;
256{
257 win_T *wp;
258
259 FOR_ALL_WINDOWS(wp)
260 {
261 if (wp->w_buffer == buf)
262 redraw_win_later(wp, type);
263 }
264}
265
266/*
267 * Changed something in the current window, at buffer line "lnum", that
268 * requires that line and possibly other lines to be redrawn.
269 * Used when entering/leaving Insert mode with the cursor on a folded line.
270 * Used to remove the "$" from a change command.
271 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
272 * may become invalid and the whole window will have to be redrawn.
273 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 void
275redrawWinline(lnum, invalid)
276 linenr_T lnum;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +0000277 int invalid UNUSED; /* window line height is invalid now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278{
279#ifdef FEAT_FOLDING
280 int i;
281#endif
282
283 if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
284 curwin->w_redraw_top = lnum;
285 if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
286 curwin->w_redraw_bot = lnum;
287 redraw_later(VALID);
288
289#ifdef FEAT_FOLDING
290 if (invalid)
291 {
292 /* A w_lines[] entry for this lnum has become invalid. */
293 i = find_wl_entry(curwin, lnum);
294 if (i >= 0)
295 curwin->w_lines[i].wl_valid = FALSE;
296 }
297#endif
298}
299
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +0200300#if defined(FEAT_RUBY) || defined(FEAT_VISUAL) || \
301 (defined(FEAT_CLIPBOARD) && defined(FEAT_X11)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302/*
303 * update all windows that are editing the current buffer
304 */
305 void
306update_curbuf(type)
307 int type;
308{
309 redraw_curbuf_later(type);
310 update_screen(type);
311}
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +0200312#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313
314/*
315 * update_screen()
316 *
317 * Based on the current value of curwin->w_topline, transfer a screenfull
318 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
319 */
320 void
321update_screen(type)
322 int type;
323{
324 win_T *wp;
325 static int did_intro = FALSE;
326#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
327 int did_one;
328#endif
329
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000330 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331 if (!screen_valid(TRUE))
332 return;
333
334 if (must_redraw)
335 {
336 if (type < must_redraw) /* use maximal type */
337 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000338
339 /* must_redraw is reset here, so that when we run into some weird
340 * reason to redraw while busy redrawing (e.g., asynchronous
341 * scrolling), or update_topline() in win_update() will cause a
342 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343 must_redraw = 0;
344 }
345
346 /* Need to update w_lines[]. */
347 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
348 type = NOT_VALID;
349
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000350 /* Postpone the redrawing when it's not needed and when being called
351 * recursively. */
352 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 {
354 redraw_later(type); /* remember type for next time */
355 must_redraw = type;
356 if (type > INVERTED_ALL)
357 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
358 return;
359 }
360
361 updating_screen = TRUE;
362#ifdef FEAT_SYN_HL
363 ++display_tick; /* let syntax code know we're in a next round of
364 * display updating */
365#endif
366
367 /*
368 * if the screen was scrolled up when displaying a message, scroll it down
369 */
370 if (msg_scrolled)
371 {
372 clear_cmdline = TRUE;
373 if (msg_scrolled > Rows - 5) /* clearing is faster */
374 type = CLEAR;
375 else if (type != CLEAR)
376 {
377 check_for_delay(FALSE);
378 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, NULL) == FAIL)
379 type = CLEAR;
380 FOR_ALL_WINDOWS(wp)
381 {
382 if (W_WINROW(wp) < msg_scrolled)
383 {
384 if (W_WINROW(wp) + wp->w_height > msg_scrolled
385 && wp->w_redr_type < REDRAW_TOP
386 && wp->w_lines_valid > 0
387 && wp->w_topline == wp->w_lines[0].wl_lnum)
388 {
389 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
390 wp->w_redr_type = REDRAW_TOP;
391 }
392 else
393 {
394 wp->w_redr_type = NOT_VALID;
395#ifdef FEAT_WINDOWS
396 if (W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp)
397 <= msg_scrolled)
398 wp->w_redr_status = TRUE;
399#endif
400 }
401 }
402 }
403 redraw_cmdline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000404#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000405 redraw_tabline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000406#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407 }
408 msg_scrolled = 0;
409 need_wait_return = FALSE;
410 }
411
412 /* reset cmdline_row now (may have been changed temporarily) */
413 compute_cmdrow();
414
415 /* Check for changed highlighting */
416 if (need_highlight_changed)
417 highlight_changed();
418
419 if (type == CLEAR) /* first clear screen */
420 {
421 screenclear(); /* will reset clear_cmdline */
422 type = NOT_VALID;
423 }
424
425 if (clear_cmdline) /* going to clear cmdline (done below) */
426 check_for_delay(FALSE);
427
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000428#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200429 /* Force redraw when width of 'number' or 'relativenumber' column
430 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000431 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200432 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
433 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000434 curwin->w_redr_type = NOT_VALID;
435#endif
436
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437 /*
438 * Only start redrawing if there is really something to do.
439 */
440 if (type == INVERTED)
441 update_curswant();
442 if (curwin->w_redr_type < type
443 && !((type == VALID
444 && curwin->w_lines[0].wl_valid
445#ifdef FEAT_DIFF
446 && curwin->w_topfill == curwin->w_old_topfill
447 && curwin->w_botfill == curwin->w_old_botfill
448#endif
449 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
450#ifdef FEAT_VISUAL
451 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000452 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
454 && curwin->w_old_visual_mode == VIsual_mode
455 && (curwin->w_valid & VALID_VIRTCOL)
456 && curwin->w_old_curswant == curwin->w_curswant)
457#endif
458 ))
459 curwin->w_redr_type = type;
460
Bram Moolenaar5a305422006-04-28 22:38:25 +0000461#ifdef FEAT_WINDOWS
462 /* Redraw the tab pages line if needed. */
463 if (redraw_tabline || type >= NOT_VALID)
464 draw_tabline();
465#endif
466
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467#ifdef FEAT_SYN_HL
468 /*
469 * Correct stored syntax highlighting info for changes in each displayed
470 * buffer. Each buffer must only be done once.
471 */
472 FOR_ALL_WINDOWS(wp)
473 {
474 if (wp->w_buffer->b_mod_set)
475 {
476# ifdef FEAT_WINDOWS
477 win_T *wwp;
478
479 /* Check if we already did this buffer. */
480 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
481 if (wwp->w_buffer == wp->w_buffer)
482 break;
483# endif
484 if (
485# ifdef FEAT_WINDOWS
486 wwp == wp &&
487# endif
488 syntax_present(wp->w_buffer))
489 syn_stack_apply_changes(wp->w_buffer);
490 }
491 }
492#endif
493
494 /*
495 * Go from top to bottom through the windows, redrawing the ones that need
496 * it.
497 */
498#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
499 did_one = FALSE;
500#endif
501#ifdef FEAT_SEARCH_EXTRA
502 search_hl.rm.regprog = NULL;
503#endif
504 FOR_ALL_WINDOWS(wp)
505 {
506 if (wp->w_redr_type != 0)
507 {
508 cursor_off();
509#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
510 if (!did_one)
511 {
512 did_one = TRUE;
513# ifdef FEAT_SEARCH_EXTRA
514 start_search_hl();
515# endif
516# ifdef FEAT_CLIPBOARD
517 /* When Visual area changed, may have to update selection. */
518 if (clip_star.available && clip_isautosel())
519 clip_update_selection();
520# endif
521#ifdef FEAT_GUI
522 /* Remove the cursor before starting to do anything, because
523 * scrolling may make it difficult to redraw the text under
524 * it. */
525 if (gui.in_use)
526 gui_undraw_cursor();
527#endif
528 }
529#endif
530 win_update(wp);
531 }
532
533#ifdef FEAT_WINDOWS
534 /* redraw status line after the window to minimize cursor movement */
535 if (wp->w_redr_status)
536 {
537 cursor_off();
538 win_redr_status(wp);
539 }
540#endif
541 }
542#if defined(FEAT_SEARCH_EXTRA)
543 end_search_hl();
544#endif
545
546#ifdef FEAT_WINDOWS
547 /* Reset b_mod_set flags. Going through all windows is probably faster
548 * than going through all buffers (there could be many buffers). */
549 for (wp = firstwin; wp != NULL; wp = wp->w_next)
550 wp->w_buffer->b_mod_set = FALSE;
551#else
552 curbuf->b_mod_set = FALSE;
553#endif
554
555 updating_screen = FALSE;
556#ifdef FEAT_GUI
557 gui_may_resize_shell();
558#endif
559
560 /* Clear or redraw the command line. Done last, because scrolling may
561 * mess up the command line. */
562 if (clear_cmdline || redraw_cmdline)
563 showmode();
564
565 /* May put up an introductory message when not editing a file */
566 if (!did_intro && bufempty()
567 && curbuf->b_fname == NULL
568#ifdef FEAT_WINDOWS
569 && firstwin->w_next == NULL
570#endif
571 && vim_strchr(p_shm, SHM_INTRO) == NULL)
572 intro_message(FALSE);
573 did_intro = TRUE;
574
575#ifdef FEAT_GUI
576 /* Redraw the cursor and update the scrollbars when all screen updating is
577 * done. */
578 if (gui.in_use)
579 {
580 out_flush(); /* required before updating the cursor */
581 if (did_one)
582 gui_update_cursor(FALSE, FALSE);
583 gui_update_scrollbars(FALSE);
584 }
585#endif
586}
587
588#if defined(FEAT_SIGNS) || defined(FEAT_GUI)
589static void update_prepare __ARGS((void));
590static void update_finish __ARGS((void));
591
592/*
593 * Prepare for updating one or more windows.
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000594 * Caller must check for "updating_screen" already set to avoid recursiveness.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 */
596 static void
597update_prepare()
598{
599 cursor_off();
600 updating_screen = TRUE;
601#ifdef FEAT_GUI
602 /* Remove the cursor before starting to do anything, because scrolling may
603 * make it difficult to redraw the text under it. */
604 if (gui.in_use)
605 gui_undraw_cursor();
606#endif
607#ifdef FEAT_SEARCH_EXTRA
608 start_search_hl();
609#endif
610}
611
612/*
613 * Finish updating one or more windows.
614 */
615 static void
616update_finish()
617{
618 if (redraw_cmdline)
619 showmode();
620
621# ifdef FEAT_SEARCH_EXTRA
622 end_search_hl();
623# endif
624
625 updating_screen = FALSE;
626
627# ifdef FEAT_GUI
628 gui_may_resize_shell();
629
630 /* Redraw the cursor and update the scrollbars when all screen updating is
631 * done. */
632 if (gui.in_use)
633 {
634 out_flush(); /* required before updating the cursor */
635 gui_update_cursor(FALSE, FALSE);
636 gui_update_scrollbars(FALSE);
637 }
638# endif
639}
640#endif
641
642#if defined(FEAT_SIGNS) || defined(PROTO)
643 void
644update_debug_sign(buf, lnum)
645 buf_T *buf;
646 linenr_T lnum;
647{
648 win_T *wp;
649 int doit = FALSE;
650
651# ifdef FEAT_FOLDING
652 win_foldinfo.fi_level = 0;
653# endif
654
655 /* update/delete a specific mark */
656 FOR_ALL_WINDOWS(wp)
657 {
658 if (buf != NULL && lnum > 0)
659 {
660 if (wp->w_buffer == buf && lnum >= wp->w_topline
661 && lnum < wp->w_botline)
662 {
663 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
664 wp->w_redraw_top = lnum;
665 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
666 wp->w_redraw_bot = lnum;
667 redraw_win_later(wp, VALID);
668 }
669 }
670 else
671 redraw_win_later(wp, VALID);
672 if (wp->w_redr_type != 0)
673 doit = TRUE;
674 }
675
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000676 /* Return when there is nothing to do or screen updating already
677 * happening. */
678 if (!doit || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 return;
680
681 /* update all windows that need updating */
682 update_prepare();
683
684# ifdef FEAT_WINDOWS
685 for (wp = firstwin; wp; wp = wp->w_next)
686 {
687 if (wp->w_redr_type != 0)
688 win_update(wp);
689 if (wp->w_redr_status)
690 win_redr_status(wp);
691 }
692# else
693 if (curwin->w_redr_type != 0)
694 win_update(curwin);
695# endif
696
697 update_finish();
698}
699#endif
700
701
702#if defined(FEAT_GUI) || defined(PROTO)
703/*
704 * Update a single window, its status line and maybe the command line msg.
705 * Used for the GUI scrollbar.
706 */
707 void
708updateWindow(wp)
709 win_T *wp;
710{
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000711 /* return if already busy updating */
712 if (updating_screen)
713 return;
714
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 update_prepare();
716
717#ifdef FEAT_CLIPBOARD
718 /* When Visual area changed, may have to update selection. */
719 if (clip_star.available && clip_isautosel())
720 clip_update_selection();
721#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000722
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000724
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725#ifdef FEAT_WINDOWS
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000726 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000727 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000728 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000729
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 if (wp->w_redr_status
731# ifdef FEAT_CMDL_INFO
732 || p_ru
733# endif
734# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000735 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736# endif
737 )
738 win_redr_status(wp);
739#endif
740
741 update_finish();
742}
743#endif
744
745/*
746 * Update a single window.
747 *
748 * This may cause the windows below it also to be redrawn (when clearing the
749 * screen or scrolling lines).
750 *
751 * How the window is redrawn depends on wp->w_redr_type. Each type also
752 * implies the one below it.
753 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000754 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
756 * INVERTED redraw the changed part of the Visual area
757 * INVERTED_ALL redraw the whole Visual area
758 * VALID 1. scroll up/down to adjust for a changed w_topline
759 * 2. update lines at the top when scrolled down
760 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000761 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 * b_mod_top and b_mod_bot.
763 * - if wp->w_redraw_top non-zero, redraw lines between
764 * wp->w_redraw_top and wp->w_redr_bot.
765 * - continue redrawing when syntax status is invalid.
766 * 4. if scrolled up, update lines at the bottom.
767 * This results in three areas that may need updating:
768 * top: from first row to top_end (when scrolled down)
769 * mid: from mid_start to mid_end (update inversion or changed text)
770 * bot: from bot_start to last row (when scrolled up)
771 */
772 static void
773win_update(wp)
774 win_T *wp;
775{
776 buf_T *buf = wp->w_buffer;
777 int type;
778 int top_end = 0; /* Below last row of the top area that needs
779 updating. 0 when no top area updating. */
780 int mid_start = 999;/* first row of the mid area that needs
781 updating. 999 when no mid area updating. */
782 int mid_end = 0; /* Below last row of the mid area that needs
783 updating. 0 when no mid area updating. */
784 int bot_start = 999;/* first row of the bot area that needs
785 updating. 999 when no bot area updating */
786#ifdef FEAT_VISUAL
787 int scrolled_down = FALSE; /* TRUE when scrolled down when
788 w_topline got smaller a bit */
789#endif
790#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000791 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 int top_to_mod = FALSE; /* redraw above mod_top */
793#endif
794
795 int row; /* current window row to display */
796 linenr_T lnum; /* current buffer lnum to display */
797 int idx; /* current index in w_lines[] */
798 int srow; /* starting row of the current line */
799
800 int eof = FALSE; /* if TRUE, we hit the end of the file */
801 int didline = FALSE; /* if TRUE, we finished the last line */
802 int i;
803 long j;
804 static int recursive = FALSE; /* being called recursively */
805 int old_botline = wp->w_botline;
806#ifdef FEAT_FOLDING
807 long fold_count;
808#endif
809#ifdef FEAT_SYN_HL
810 /* remember what happened to the previous line, to know if
811 * check_visual_highlight() can be used */
812#define DID_NONE 1 /* didn't update a line */
813#define DID_LINE 2 /* updated a normal line */
814#define DID_FOLD 3 /* updated a folded line */
815 int did_update = DID_NONE;
816 linenr_T syntax_last_parsed = 0; /* last parsed text line */
817#endif
818 linenr_T mod_top = 0;
819 linenr_T mod_bot = 0;
820#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
821 int save_got_int;
822#endif
823
824 type = wp->w_redr_type;
825
826 if (type == NOT_VALID)
827 {
828#ifdef FEAT_WINDOWS
829 wp->w_redr_status = TRUE;
830#endif
831 wp->w_lines_valid = 0;
832 }
833
834 /* Window is zero-height: nothing to draw. */
835 if (wp->w_height == 0)
836 {
837 wp->w_redr_type = 0;
838 return;
839 }
840
841#ifdef FEAT_VERTSPLIT
842 /* Window is zero-width: Only need to draw the separator. */
843 if (wp->w_width == 0)
844 {
845 /* draw the vertical separator right of this window */
846 draw_vsep_win(wp, 0);
847 wp->w_redr_type = 0;
848 return;
849 }
850#endif
851
852#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000853 /* Setup for match and 'hlsearch' highlighting. Disable any previous
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000854 * match */
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000855 cur = wp->w_match_head;
856 while (cur != NULL)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000857 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000858 cur->hl.rm = cur->match;
859 if (cur->hlg_id == 0)
860 cur->hl.attr = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000861 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000862 cur->hl.attr = syn_id2attr(cur->hlg_id);
863 cur->hl.buf = buf;
864 cur->hl.lnum = 0;
865 cur->hl.first_lnum = 0;
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000866# ifdef FEAT_RELTIME
867 /* Set the time limit to 'redrawtime'. */
868 profile_setlimit(p_rdt, &(cur->hl.tm));
869# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000870 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000871 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 search_hl.buf = buf;
873 search_hl.lnum = 0;
874 search_hl.first_lnum = 0;
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000875 /* time limit is set at the toplevel, for all windows */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876#endif
877
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000878#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200879 /* Force redraw when width of 'number' or 'relativenumber' column
880 * changes. */
881 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000882 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000883 {
884 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000885 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000886 }
887 else
888#endif
889
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
891 {
892 /*
893 * When there are both inserted/deleted lines and specific lines to be
894 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
895 * everything (only happens when redrawing is off for while).
896 */
897 type = NOT_VALID;
898 }
899 else
900 {
901 /*
902 * Set mod_top to the first line that needs displaying because of
903 * changes. Set mod_bot to the first line after the changes.
904 */
905 mod_top = wp->w_redraw_top;
906 if (wp->w_redraw_bot != 0)
907 mod_bot = wp->w_redraw_bot + 1;
908 else
909 mod_bot = 0;
910 wp->w_redraw_top = 0; /* reset for next time */
911 wp->w_redraw_bot = 0;
912 if (buf->b_mod_set)
913 {
914 if (mod_top == 0 || mod_top > buf->b_mod_top)
915 {
916 mod_top = buf->b_mod_top;
917#ifdef FEAT_SYN_HL
918 /* Need to redraw lines above the change that may be included
919 * in a pattern match. */
920 if (syntax_present(buf))
921 {
922 mod_top -= buf->b_syn_sync_linebreaks;
923 if (mod_top < 1)
924 mod_top = 1;
925 }
926#endif
927 }
928 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
929 mod_bot = buf->b_mod_bot;
930
931#ifdef FEAT_SEARCH_EXTRA
932 /* When 'hlsearch' is on and using a multi-line search pattern, a
933 * change in one line may make the Search highlighting in a
934 * previous line invalid. Simple solution: redraw all visible
935 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000936 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000938 if (search_hl.rm.regprog != NULL
939 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000941 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000942 {
943 cur = wp->w_match_head;
944 while (cur != NULL)
945 {
946 if (cur->match.regprog != NULL
947 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000948 {
949 top_to_mod = TRUE;
950 break;
951 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000952 cur = cur->next;
953 }
954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955#endif
956 }
957#ifdef FEAT_FOLDING
958 if (mod_top != 0 && hasAnyFolding(wp))
959 {
960 linenr_T lnumt, lnumb;
961
962 /*
963 * A change in a line can cause lines above it to become folded or
964 * unfolded. Find the top most buffer line that may be affected.
965 * If the line was previously folded and displayed, get the first
966 * line of that fold. If the line is folded now, get the first
967 * folded line. Use the minimum of these two.
968 */
969
970 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
971 * the line below it. If there is no valid entry, use w_topline.
972 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
973 * to this line. If there is no valid entry, use MAXLNUM. */
974 lnumt = wp->w_topline;
975 lnumb = MAXLNUM;
976 for (i = 0; i < wp->w_lines_valid; ++i)
977 if (wp->w_lines[i].wl_valid)
978 {
979 if (wp->w_lines[i].wl_lastlnum < mod_top)
980 lnumt = wp->w_lines[i].wl_lastlnum + 1;
981 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
982 {
983 lnumb = wp->w_lines[i].wl_lnum;
984 /* When there is a fold column it might need updating
985 * in the next line ("J" just above an open fold). */
986 if (wp->w_p_fdc > 0)
987 ++lnumb;
988 }
989 }
990
991 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
992 if (mod_top > lnumt)
993 mod_top = lnumt;
994
995 /* Now do the same for the bottom line (one above mod_bot). */
996 --mod_bot;
997 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
998 ++mod_bot;
999 if (mod_bot < lnumb)
1000 mod_bot = lnumb;
1001 }
1002#endif
1003
1004 /* When a change starts above w_topline and the end is below
1005 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001006 * If the end of the change is above w_topline: do like no change was
1007 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 if (mod_top != 0 && mod_top < wp->w_topline)
1009 {
1010 if (mod_bot > wp->w_topline)
1011 mod_top = wp->w_topline;
1012#ifdef FEAT_SYN_HL
1013 else if (syntax_present(buf))
1014 top_end = 1;
1015#endif
1016 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001017
1018 /* When line numbers are displayed need to redraw all lines below
1019 * inserted/deleted lines. */
1020 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1021 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 }
1023
1024 /*
1025 * When only displaying the lines at the top, set top_end. Used when
1026 * window has scrolled down for msg_scrolled.
1027 */
1028 if (type == REDRAW_TOP)
1029 {
1030 j = 0;
1031 for (i = 0; i < wp->w_lines_valid; ++i)
1032 {
1033 j += wp->w_lines[i].wl_size;
1034 if (j >= wp->w_upd_rows)
1035 {
1036 top_end = j;
1037 break;
1038 }
1039 }
1040 if (top_end == 0)
1041 /* not found (cannot happen?): redraw everything */
1042 type = NOT_VALID;
1043 else
1044 /* top area defined, the rest is VALID */
1045 type = VALID;
1046 }
1047
Bram Moolenaar367329b2007-08-30 11:53:22 +00001048 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001049 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1050 * non-zero and thus not FALSE) will indicate that screenclear() was not
1051 * called. */
1052 if (screen_cleared)
1053 screen_cleared = MAYBE;
1054
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 /*
1056 * If there are no changes on the screen that require a complete redraw,
1057 * handle three cases:
1058 * 1: we are off the top of the screen by a few lines: scroll down
1059 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1060 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1061 * w_lines[] that needs updating.
1062 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001063 if ((type == VALID || type == SOME_VALID
1064 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065#ifdef FEAT_DIFF
1066 && !wp->w_botfill && !wp->w_old_botfill
1067#endif
1068 )
1069 {
1070 if (mod_top != 0 && wp->w_topline == mod_top)
1071 {
1072 /*
1073 * w_topline is the first changed line, the scrolling will be done
1074 * further down.
1075 */
1076 }
1077 else if (wp->w_lines[0].wl_valid
1078 && (wp->w_topline < wp->w_lines[0].wl_lnum
1079#ifdef FEAT_DIFF
1080 || (wp->w_topline == wp->w_lines[0].wl_lnum
1081 && wp->w_topfill > wp->w_old_topfill)
1082#endif
1083 ))
1084 {
1085 /*
1086 * New topline is above old topline: May scroll down.
1087 */
1088#ifdef FEAT_FOLDING
1089 if (hasAnyFolding(wp))
1090 {
1091 linenr_T ln;
1092
1093 /* count the number of lines we are off, counting a sequence
1094 * of folded lines as one */
1095 j = 0;
1096 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1097 {
1098 ++j;
1099 if (j >= wp->w_height - 2)
1100 break;
1101 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1102 }
1103 }
1104 else
1105#endif
1106 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1107 if (j < wp->w_height - 2) /* not too far off */
1108 {
1109 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1110#ifdef FEAT_DIFF
1111 /* insert extra lines for previously invisible filler lines */
1112 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1113 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1114 - wp->w_old_topfill;
1115#endif
1116 if (i < wp->w_height - 2) /* less than a screen off */
1117 {
1118 /*
1119 * Try to insert the correct number of lines.
1120 * If not the last window, delete the lines at the bottom.
1121 * win_ins_lines may fail when the terminal can't do it.
1122 */
1123 if (i > 0)
1124 check_for_delay(FALSE);
1125 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1126 {
1127 if (wp->w_lines_valid != 0)
1128 {
1129 /* Need to update rows that are new, stop at the
1130 * first one that scrolled down. */
1131 top_end = i;
1132#ifdef FEAT_VISUAL
1133 scrolled_down = TRUE;
1134#endif
1135
1136 /* Move the entries that were scrolled, disable
1137 * the entries for the lines to be redrawn. */
1138 if ((wp->w_lines_valid += j) > wp->w_height)
1139 wp->w_lines_valid = wp->w_height;
1140 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1141 wp->w_lines[idx] = wp->w_lines[idx - j];
1142 while (idx >= 0)
1143 wp->w_lines[idx--].wl_valid = FALSE;
1144 }
1145 }
1146 else
1147 mid_start = 0; /* redraw all lines */
1148 }
1149 else
1150 mid_start = 0; /* redraw all lines */
1151 }
1152 else
1153 mid_start = 0; /* redraw all lines */
1154 }
1155 else
1156 {
1157 /*
1158 * New topline is at or below old topline: May scroll up.
1159 * When topline didn't change, find first entry in w_lines[] that
1160 * needs updating.
1161 */
1162
1163 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1164 j = -1;
1165 row = 0;
1166 for (i = 0; i < wp->w_lines_valid; i++)
1167 {
1168 if (wp->w_lines[i].wl_valid
1169 && wp->w_lines[i].wl_lnum == wp->w_topline)
1170 {
1171 j = i;
1172 break;
1173 }
1174 row += wp->w_lines[i].wl_size;
1175 }
1176 if (j == -1)
1177 {
1178 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1179 * lines */
1180 mid_start = 0;
1181 }
1182 else
1183 {
1184 /*
1185 * Try to delete the correct number of lines.
1186 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1187 */
1188#ifdef FEAT_DIFF
1189 /* If the topline didn't change, delete old filler lines,
1190 * otherwise delete filler lines of the new topline... */
1191 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1192 row += wp->w_old_topfill;
1193 else
1194 row += diff_check_fill(wp, wp->w_topline);
1195 /* ... but don't delete new filler lines. */
1196 row -= wp->w_topfill;
1197#endif
1198 if (row > 0)
1199 {
1200 check_for_delay(FALSE);
1201 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1202 bot_start = wp->w_height - row;
1203 else
1204 mid_start = 0; /* redraw all lines */
1205 }
1206 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1207 {
1208 /*
1209 * Skip the lines (below the deleted lines) that are still
1210 * valid and don't need redrawing. Copy their info
1211 * upwards, to compensate for the deleted lines. Set
1212 * bot_start to the first row that needs redrawing.
1213 */
1214 bot_start = 0;
1215 idx = 0;
1216 for (;;)
1217 {
1218 wp->w_lines[idx] = wp->w_lines[j];
1219 /* stop at line that didn't fit, unless it is still
1220 * valid (no lines deleted) */
1221 if (row > 0 && bot_start + row
1222 + (int)wp->w_lines[j].wl_size > wp->w_height)
1223 {
1224 wp->w_lines_valid = idx + 1;
1225 break;
1226 }
1227 bot_start += wp->w_lines[idx++].wl_size;
1228
1229 /* stop at the last valid entry in w_lines[].wl_size */
1230 if (++j >= wp->w_lines_valid)
1231 {
1232 wp->w_lines_valid = idx;
1233 break;
1234 }
1235 }
1236#ifdef FEAT_DIFF
1237 /* Correct the first entry for filler lines at the top
1238 * when it won't get updated below. */
1239 if (wp->w_p_diff && bot_start > 0)
1240 wp->w_lines[0].wl_size =
1241 plines_win_nofill(wp, wp->w_topline, TRUE)
1242 + wp->w_topfill;
1243#endif
1244 }
1245 }
1246 }
1247
1248 /* When starting redraw in the first line, redraw all lines. When
1249 * there is only one window it's probably faster to clear the screen
1250 * first. */
1251 if (mid_start == 0)
1252 {
1253 mid_end = wp->w_height;
1254 if (lastwin == firstwin)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001255 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001256 /* Clear the screen when it was not done by win_del_lines() or
1257 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1258 * then. */
1259 if (screen_cleared != TRUE)
1260 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001261#ifdef FEAT_WINDOWS
1262 /* The screen was cleared, redraw the tab pages line. */
1263 if (redraw_tabline)
1264 draw_tabline();
1265#endif
1266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001268
1269 /* When win_del_lines() or win_ins_lines() caused the screen to be
1270 * cleared (only happens for the first window) or when screenclear()
1271 * was called directly above, "must_redraw" will have been set to
1272 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1273 if (screen_cleared == TRUE)
1274 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 }
1276 else
1277 {
1278 /* Not VALID or INVERTED: redraw all lines. */
1279 mid_start = 0;
1280 mid_end = wp->w_height;
1281 }
1282
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001283 if (type == SOME_VALID)
1284 {
1285 /* SOME_VALID: redraw all lines. */
1286 mid_start = 0;
1287 mid_end = wp->w_height;
1288 type = NOT_VALID;
1289 }
1290
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291#ifdef FEAT_VISUAL
1292 /* check if we are updating or removing the inverted part */
1293 if ((VIsual_active && buf == curwin->w_buffer)
1294 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1295 {
1296 linenr_T from, to;
1297
1298 if (VIsual_active)
1299 {
1300 if (VIsual_active
1301 && (VIsual_mode != wp->w_old_visual_mode
1302 || type == INVERTED_ALL))
1303 {
1304 /*
1305 * If the type of Visual selection changed, redraw the whole
1306 * selection. Also when the ownership of the X selection is
1307 * gained or lost.
1308 */
1309 if (curwin->w_cursor.lnum < VIsual.lnum)
1310 {
1311 from = curwin->w_cursor.lnum;
1312 to = VIsual.lnum;
1313 }
1314 else
1315 {
1316 from = VIsual.lnum;
1317 to = curwin->w_cursor.lnum;
1318 }
1319 /* redraw more when the cursor moved as well */
1320 if (wp->w_old_cursor_lnum < from)
1321 from = wp->w_old_cursor_lnum;
1322 if (wp->w_old_cursor_lnum > to)
1323 to = wp->w_old_cursor_lnum;
1324 if (wp->w_old_visual_lnum < from)
1325 from = wp->w_old_visual_lnum;
1326 if (wp->w_old_visual_lnum > to)
1327 to = wp->w_old_visual_lnum;
1328 }
1329 else
1330 {
1331 /*
1332 * Find the line numbers that need to be updated: The lines
1333 * between the old cursor position and the current cursor
1334 * position. Also check if the Visual position changed.
1335 */
1336 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1337 {
1338 from = curwin->w_cursor.lnum;
1339 to = wp->w_old_cursor_lnum;
1340 }
1341 else
1342 {
1343 from = wp->w_old_cursor_lnum;
1344 to = curwin->w_cursor.lnum;
1345 if (from == 0) /* Visual mode just started */
1346 from = to;
1347 }
1348
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001349 if (VIsual.lnum != wp->w_old_visual_lnum
1350 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 {
1352 if (wp->w_old_visual_lnum < from
1353 && wp->w_old_visual_lnum != 0)
1354 from = wp->w_old_visual_lnum;
1355 if (wp->w_old_visual_lnum > to)
1356 to = wp->w_old_visual_lnum;
1357 if (VIsual.lnum < from)
1358 from = VIsual.lnum;
1359 if (VIsual.lnum > to)
1360 to = VIsual.lnum;
1361 }
1362 }
1363
1364 /*
1365 * If in block mode and changed column or curwin->w_curswant:
1366 * update all lines.
1367 * First compute the actual start and end column.
1368 */
1369 if (VIsual_mode == Ctrl_V)
1370 {
1371 colnr_T fromc, toc;
1372
1373 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
1374 ++toc;
1375 if (curwin->w_curswant == MAXCOL)
1376 toc = MAXCOL;
1377
1378 if (fromc != wp->w_old_cursor_fcol
1379 || toc != wp->w_old_cursor_lcol)
1380 {
1381 if (from > VIsual.lnum)
1382 from = VIsual.lnum;
1383 if (to < VIsual.lnum)
1384 to = VIsual.lnum;
1385 }
1386 wp->w_old_cursor_fcol = fromc;
1387 wp->w_old_cursor_lcol = toc;
1388 }
1389 }
1390 else
1391 {
1392 /* Use the line numbers of the old Visual area. */
1393 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1394 {
1395 from = wp->w_old_cursor_lnum;
1396 to = wp->w_old_visual_lnum;
1397 }
1398 else
1399 {
1400 from = wp->w_old_visual_lnum;
1401 to = wp->w_old_cursor_lnum;
1402 }
1403 }
1404
1405 /*
1406 * There is no need to update lines above the top of the window.
1407 */
1408 if (from < wp->w_topline)
1409 from = wp->w_topline;
1410
1411 /*
1412 * If we know the value of w_botline, use it to restrict the update to
1413 * the lines that are visible in the window.
1414 */
1415 if (wp->w_valid & VALID_BOTLINE)
1416 {
1417 if (from >= wp->w_botline)
1418 from = wp->w_botline - 1;
1419 if (to >= wp->w_botline)
1420 to = wp->w_botline - 1;
1421 }
1422
1423 /*
1424 * Find the minimal part to be updated.
1425 * Watch out for scrolling that made entries in w_lines[] invalid.
1426 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1427 * top_end; need to redraw from top_end to the "to" line.
1428 * A middle mouse click with a Visual selection may change the text
1429 * above the Visual area and reset wl_valid, do count these for
1430 * mid_end (in srow).
1431 */
1432 if (mid_start > 0)
1433 {
1434 lnum = wp->w_topline;
1435 idx = 0;
1436 srow = 0;
1437 if (scrolled_down)
1438 mid_start = top_end;
1439 else
1440 mid_start = 0;
1441 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1442 {
1443 if (wp->w_lines[idx].wl_valid)
1444 mid_start += wp->w_lines[idx].wl_size;
1445 else if (!scrolled_down)
1446 srow += wp->w_lines[idx].wl_size;
1447 ++idx;
1448# ifdef FEAT_FOLDING
1449 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1450 lnum = wp->w_lines[idx].wl_lnum;
1451 else
1452# endif
1453 ++lnum;
1454 }
1455 srow += mid_start;
1456 mid_end = wp->w_height;
1457 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1458 {
1459 if (wp->w_lines[idx].wl_valid
1460 && wp->w_lines[idx].wl_lnum >= to + 1)
1461 {
1462 /* Only update until first row of this line */
1463 mid_end = srow;
1464 break;
1465 }
1466 srow += wp->w_lines[idx].wl_size;
1467 }
1468 }
1469 }
1470
1471 if (VIsual_active && buf == curwin->w_buffer)
1472 {
1473 wp->w_old_visual_mode = VIsual_mode;
1474 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1475 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001476 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 wp->w_old_curswant = curwin->w_curswant;
1478 }
1479 else
1480 {
1481 wp->w_old_visual_mode = 0;
1482 wp->w_old_cursor_lnum = 0;
1483 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001484 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 }
1486#endif /* FEAT_VISUAL */
1487
1488#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1489 /* reset got_int, otherwise regexp won't work */
1490 save_got_int = got_int;
1491 got_int = 0;
1492#endif
1493#ifdef FEAT_FOLDING
1494 win_foldinfo.fi_level = 0;
1495#endif
1496
1497 /*
1498 * Update all the window rows.
1499 */
1500 idx = 0; /* first entry in w_lines[].wl_size */
1501 row = 0;
1502 srow = 0;
1503 lnum = wp->w_topline; /* first line shown in window */
1504 for (;;)
1505 {
1506 /* stop updating when reached the end of the window (check for _past_
1507 * the end of the window is at the end of the loop) */
1508 if (row == wp->w_height)
1509 {
1510 didline = TRUE;
1511 break;
1512 }
1513
1514 /* stop updating when hit the end of the file */
1515 if (lnum > buf->b_ml.ml_line_count)
1516 {
1517 eof = TRUE;
1518 break;
1519 }
1520
1521 /* Remember the starting row of the line that is going to be dealt
1522 * with. It is used further down when the line doesn't fit. */
1523 srow = row;
1524
1525 /*
1526 * Update a line when it is in an area that needs updating, when it
1527 * has changes or w_lines[idx] is invalid.
1528 * bot_start may be halfway a wrapped line after using
1529 * win_del_lines(), check if the current line includes it.
1530 * When syntax folding is being used, the saved syntax states will
1531 * already have been updated, we can't see where the syntax state is
1532 * the same again, just update until the end of the window.
1533 */
1534 if (row < top_end
1535 || (row >= mid_start && row < mid_end)
1536#ifdef FEAT_SEARCH_EXTRA
1537 || top_to_mod
1538#endif
1539 || idx >= wp->w_lines_valid
1540 || (row + wp->w_lines[idx].wl_size > bot_start)
1541 || (mod_top != 0
1542 && (lnum == mod_top
1543 || (lnum >= mod_top
1544 && (lnum < mod_bot
1545#ifdef FEAT_SYN_HL
1546 || did_update == DID_FOLD
1547 || (did_update == DID_LINE
1548 && syntax_present(buf)
1549 && (
1550# ifdef FEAT_FOLDING
1551 (foldmethodIsSyntax(wp)
1552 && hasAnyFolding(wp)) ||
1553# endif
1554 syntax_check_changed(lnum)))
1555#endif
1556 )))))
1557 {
1558#ifdef FEAT_SEARCH_EXTRA
1559 if (lnum == mod_top)
1560 top_to_mod = FALSE;
1561#endif
1562
1563 /*
1564 * When at start of changed lines: May scroll following lines
1565 * up or down to minimize redrawing.
1566 * Don't do this when the change continues until the end.
1567 * Don't scroll when dollar_vcol is non-zero, keep the "$".
1568 */
1569 if (lnum == mod_top
1570 && mod_bot != MAXLNUM
1571 && !(dollar_vcol != 0 && mod_bot == mod_top + 1))
1572 {
1573 int old_rows = 0;
1574 int new_rows = 0;
1575 int xtra_rows;
1576 linenr_T l;
1577
1578 /* Count the old number of window rows, using w_lines[], which
1579 * should still contain the sizes for the lines as they are
1580 * currently displayed. */
1581 for (i = idx; i < wp->w_lines_valid; ++i)
1582 {
1583 /* Only valid lines have a meaningful wl_lnum. Invalid
1584 * lines are part of the changed area. */
1585 if (wp->w_lines[i].wl_valid
1586 && wp->w_lines[i].wl_lnum == mod_bot)
1587 break;
1588 old_rows += wp->w_lines[i].wl_size;
1589#ifdef FEAT_FOLDING
1590 if (wp->w_lines[i].wl_valid
1591 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1592 {
1593 /* Must have found the last valid entry above mod_bot.
1594 * Add following invalid entries. */
1595 ++i;
1596 while (i < wp->w_lines_valid
1597 && !wp->w_lines[i].wl_valid)
1598 old_rows += wp->w_lines[i++].wl_size;
1599 break;
1600 }
1601#endif
1602 }
1603
1604 if (i >= wp->w_lines_valid)
1605 {
1606 /* We can't find a valid line below the changed lines,
1607 * need to redraw until the end of the window.
1608 * Inserting/deleting lines has no use. */
1609 bot_start = 0;
1610 }
1611 else
1612 {
1613 /* Able to count old number of rows: Count new window
1614 * rows, and may insert/delete lines */
1615 j = idx;
1616 for (l = lnum; l < mod_bot; ++l)
1617 {
1618#ifdef FEAT_FOLDING
1619 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1620 ++new_rows;
1621 else
1622#endif
1623#ifdef FEAT_DIFF
1624 if (l == wp->w_topline)
1625 new_rows += plines_win_nofill(wp, l, TRUE)
1626 + wp->w_topfill;
1627 else
1628#endif
1629 new_rows += plines_win(wp, l, TRUE);
1630 ++j;
1631 if (new_rows > wp->w_height - row - 2)
1632 {
1633 /* it's getting too much, must redraw the rest */
1634 new_rows = 9999;
1635 break;
1636 }
1637 }
1638 xtra_rows = new_rows - old_rows;
1639 if (xtra_rows < 0)
1640 {
1641 /* May scroll text up. If there is not enough
1642 * remaining text or scrolling fails, must redraw the
1643 * rest. If scrolling works, must redraw the text
1644 * below the scrolled text. */
1645 if (row - xtra_rows >= wp->w_height - 2)
1646 mod_bot = MAXLNUM;
1647 else
1648 {
1649 check_for_delay(FALSE);
1650 if (win_del_lines(wp, row,
1651 -xtra_rows, FALSE, FALSE) == FAIL)
1652 mod_bot = MAXLNUM;
1653 else
1654 bot_start = wp->w_height + xtra_rows;
1655 }
1656 }
1657 else if (xtra_rows > 0)
1658 {
1659 /* May scroll text down. If there is not enough
1660 * remaining text of scrolling fails, must redraw the
1661 * rest. */
1662 if (row + xtra_rows >= wp->w_height - 2)
1663 mod_bot = MAXLNUM;
1664 else
1665 {
1666 check_for_delay(FALSE);
1667 if (win_ins_lines(wp, row + old_rows,
1668 xtra_rows, FALSE, FALSE) == FAIL)
1669 mod_bot = MAXLNUM;
1670 else if (top_end > row + old_rows)
1671 /* Scrolled the part at the top that requires
1672 * updating down. */
1673 top_end += xtra_rows;
1674 }
1675 }
1676
1677 /* When not updating the rest, may need to move w_lines[]
1678 * entries. */
1679 if (mod_bot != MAXLNUM && i != j)
1680 {
1681 if (j < i)
1682 {
1683 int x = row + new_rows;
1684
1685 /* move entries in w_lines[] upwards */
1686 for (;;)
1687 {
1688 /* stop at last valid entry in w_lines[] */
1689 if (i >= wp->w_lines_valid)
1690 {
1691 wp->w_lines_valid = j;
1692 break;
1693 }
1694 wp->w_lines[j] = wp->w_lines[i];
1695 /* stop at a line that won't fit */
1696 if (x + (int)wp->w_lines[j].wl_size
1697 > wp->w_height)
1698 {
1699 wp->w_lines_valid = j + 1;
1700 break;
1701 }
1702 x += wp->w_lines[j++].wl_size;
1703 ++i;
1704 }
1705 if (bot_start > x)
1706 bot_start = x;
1707 }
1708 else /* j > i */
1709 {
1710 /* move entries in w_lines[] downwards */
1711 j -= i;
1712 wp->w_lines_valid += j;
1713 if (wp->w_lines_valid > wp->w_height)
1714 wp->w_lines_valid = wp->w_height;
1715 for (i = wp->w_lines_valid; i - j >= idx; --i)
1716 wp->w_lines[i] = wp->w_lines[i - j];
1717
1718 /* The w_lines[] entries for inserted lines are
1719 * now invalid, but wl_size may be used above.
1720 * Reset to zero. */
1721 while (i >= idx)
1722 {
1723 wp->w_lines[i].wl_size = 0;
1724 wp->w_lines[i--].wl_valid = FALSE;
1725 }
1726 }
1727 }
1728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 }
1730
1731#ifdef FEAT_FOLDING
1732 /*
1733 * When lines are folded, display one line for all of them.
1734 * Otherwise, display normally (can be several display lines when
1735 * 'wrap' is on).
1736 */
1737 fold_count = foldedCount(wp, lnum, &win_foldinfo);
1738 if (fold_count != 0)
1739 {
1740 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
1741 ++row;
1742 --fold_count;
1743 wp->w_lines[idx].wl_folded = TRUE;
1744 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
1745# ifdef FEAT_SYN_HL
1746 did_update = DID_FOLD;
1747# endif
1748 }
1749 else
1750#endif
1751 if (idx < wp->w_lines_valid
1752 && wp->w_lines[idx].wl_valid
1753 && wp->w_lines[idx].wl_lnum == lnum
1754 && lnum > wp->w_topline
1755 && !(dy_flags & DY_LASTLINE)
1756 && srow + wp->w_lines[idx].wl_size > wp->w_height
1757#ifdef FEAT_DIFF
1758 && diff_check_fill(wp, lnum) == 0
1759#endif
1760 )
1761 {
1762 /* This line is not going to fit. Don't draw anything here,
1763 * will draw "@ " lines below. */
1764 row = wp->w_height + 1;
1765 }
1766 else
1767 {
1768#ifdef FEAT_SEARCH_EXTRA
1769 prepare_search_hl(wp, lnum);
1770#endif
1771#ifdef FEAT_SYN_HL
1772 /* Let the syntax stuff know we skipped a few lines. */
1773 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
1774 && syntax_present(buf))
1775 syntax_end_parsing(syntax_last_parsed + 1);
1776#endif
1777
1778 /*
1779 * Display one line.
1780 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00001781 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782
1783#ifdef FEAT_FOLDING
1784 wp->w_lines[idx].wl_folded = FALSE;
1785 wp->w_lines[idx].wl_lastlnum = lnum;
1786#endif
1787#ifdef FEAT_SYN_HL
1788 did_update = DID_LINE;
1789 syntax_last_parsed = lnum;
1790#endif
1791 }
1792
1793 wp->w_lines[idx].wl_lnum = lnum;
1794 wp->w_lines[idx].wl_valid = TRUE;
1795 if (row > wp->w_height) /* past end of screen */
1796 {
1797 /* we may need the size of that too long line later on */
1798 if (dollar_vcol == 0)
1799 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
1800 ++idx;
1801 break;
1802 }
1803 if (dollar_vcol == 0)
1804 wp->w_lines[idx].wl_size = row - srow;
1805 ++idx;
1806#ifdef FEAT_FOLDING
1807 lnum += fold_count + 1;
1808#else
1809 ++lnum;
1810#endif
1811 }
1812 else
1813 {
1814 /* This line does not need updating, advance to the next one */
1815 row += wp->w_lines[idx++].wl_size;
1816 if (row > wp->w_height) /* past end of screen */
1817 break;
1818#ifdef FEAT_FOLDING
1819 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
1820#else
1821 ++lnum;
1822#endif
1823#ifdef FEAT_SYN_HL
1824 did_update = DID_NONE;
1825#endif
1826 }
1827
1828 if (lnum > buf->b_ml.ml_line_count)
1829 {
1830 eof = TRUE;
1831 break;
1832 }
1833 }
1834 /*
1835 * End of loop over all window lines.
1836 */
1837
1838
1839 if (idx > wp->w_lines_valid)
1840 wp->w_lines_valid = idx;
1841
1842#ifdef FEAT_SYN_HL
1843 /*
1844 * Let the syntax stuff know we stop parsing here.
1845 */
1846 if (syntax_last_parsed != 0 && syntax_present(buf))
1847 syntax_end_parsing(syntax_last_parsed + 1);
1848#endif
1849
1850 /*
1851 * If we didn't hit the end of the file, and we didn't finish the last
1852 * line we were working on, then the line didn't fit.
1853 */
1854 wp->w_empty_rows = 0;
1855#ifdef FEAT_DIFF
1856 wp->w_filler_rows = 0;
1857#endif
1858 if (!eof && !didline)
1859 {
1860 if (lnum == wp->w_topline)
1861 {
1862 /*
1863 * Single line that does not fit!
1864 * Don't overwrite it, it can be edited.
1865 */
1866 wp->w_botline = lnum + 1;
1867 }
1868#ifdef FEAT_DIFF
1869 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
1870 {
1871 /* Window ends in filler lines. */
1872 wp->w_botline = lnum;
1873 wp->w_filler_rows = wp->w_height - srow;
1874 }
1875#endif
1876 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
1877 {
1878 /*
1879 * Last line isn't finished: Display "@@@" at the end.
1880 */
1881 screen_fill(W_WINROW(wp) + wp->w_height - 1,
1882 W_WINROW(wp) + wp->w_height,
1883 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
1884 '@', '@', hl_attr(HLF_AT));
1885 set_empty_rows(wp, srow);
1886 wp->w_botline = lnum;
1887 }
1888 else
1889 {
1890 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
1891 wp->w_botline = lnum;
1892 }
1893 }
1894 else
1895 {
1896#ifdef FEAT_VERTSPLIT
1897 draw_vsep_win(wp, row);
1898#endif
1899 if (eof) /* we hit the end of the file */
1900 {
1901 wp->w_botline = buf->b_ml.ml_line_count + 1;
1902#ifdef FEAT_DIFF
1903 j = diff_check_fill(wp, wp->w_botline);
1904 if (j > 0 && !wp->w_botfill)
1905 {
1906 /*
1907 * Display filler lines at the end of the file
1908 */
1909 if (char2cells(fill_diff) > 1)
1910 i = '-';
1911 else
1912 i = fill_diff;
1913 if (row + j > wp->w_height)
1914 j = wp->w_height - row;
1915 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
1916 row += j;
1917 }
1918#endif
1919 }
1920 else if (dollar_vcol == 0)
1921 wp->w_botline = lnum;
1922
1923 /* make sure the rest of the screen is blank */
1924 /* put '~'s on rows that aren't part of the file. */
1925 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_AT);
1926 }
1927
1928 /* Reset the type of redrawing required, the window has been updated. */
1929 wp->w_redr_type = 0;
1930#ifdef FEAT_DIFF
1931 wp->w_old_topfill = wp->w_topfill;
1932 wp->w_old_botfill = wp->w_botfill;
1933#endif
1934
1935 if (dollar_vcol == 0)
1936 {
1937 /*
1938 * There is a trick with w_botline. If we invalidate it on each
1939 * change that might modify it, this will cause a lot of expensive
1940 * calls to plines() in update_topline() each time. Therefore the
1941 * value of w_botline is often approximated, and this value is used to
1942 * compute the value of w_topline. If the value of w_botline was
1943 * wrong, check that the value of w_topline is correct (cursor is on
1944 * the visible part of the text). If it's not, we need to redraw
1945 * again. Mostly this just means scrolling up a few lines, so it
1946 * doesn't look too bad. Only do this for the current window (where
1947 * changes are relevant).
1948 */
1949 wp->w_valid |= VALID_BOTLINE;
1950 if (wp == curwin && wp->w_botline != old_botline && !recursive)
1951 {
1952 recursive = TRUE;
1953 curwin->w_valid &= ~VALID_TOPLINE;
1954 update_topline(); /* may invalidate w_botline again */
1955 if (must_redraw != 0)
1956 {
1957 /* Don't update for changes in buffer again. */
1958 i = curbuf->b_mod_set;
1959 curbuf->b_mod_set = FALSE;
1960 win_update(curwin);
1961 must_redraw = 0;
1962 curbuf->b_mod_set = i;
1963 }
1964 recursive = FALSE;
1965 }
1966 }
1967
1968#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1969 /* restore got_int, unless CTRL-C was hit while redrawing */
1970 if (!got_int)
1971 got_int = save_got_int;
1972#endif
1973}
1974
1975#ifdef FEAT_SIGNS
1976static int draw_signcolumn __ARGS((win_T *wp));
1977
1978/*
1979 * Return TRUE when window "wp" has a column to draw signs in.
1980 */
1981 static int
1982draw_signcolumn(wp)
1983 win_T *wp;
1984{
1985 return (wp->w_buffer->b_signlist != NULL
1986# ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001987 || netbeans_active()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988# endif
1989 );
1990}
1991#endif
1992
1993/*
1994 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
1995 * as the filler character.
1996 */
1997 static void
1998win_draw_end(wp, c1, c2, row, endrow, hl)
1999 win_T *wp;
2000 int c1;
2001 int c2;
2002 int row;
2003 int endrow;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002004 hlf_T hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005{
2006#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2007 int n = 0;
2008# define FDC_OFF n
2009#else
2010# define FDC_OFF 0
2011#endif
2012
2013#ifdef FEAT_RIGHTLEFT
2014 if (wp->w_p_rl)
2015 {
2016 /* No check for cmdline window: should never be right-left. */
2017# ifdef FEAT_FOLDING
2018 n = wp->w_p_fdc;
2019
2020 if (n > 0)
2021 {
2022 /* draw the fold column at the right */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002023 if (n > W_WIDTH(wp))
2024 n = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2026 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
2027 ' ', ' ', hl_attr(HLF_FC));
2028 }
2029# endif
2030# ifdef FEAT_SIGNS
2031 if (draw_signcolumn(wp))
2032 {
2033 int nn = n + 2;
2034
2035 /* draw the sign column left of the fold column */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002036 if (nn > W_WIDTH(wp))
2037 nn = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2039 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
2040 ' ', ' ', hl_attr(HLF_SC));
2041 n = nn;
2042 }
2043# endif
2044 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2045 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
2046 c2, c2, hl_attr(hl));
2047 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2048 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
2049 c1, c2, hl_attr(hl));
2050 }
2051 else
2052#endif
2053 {
2054#ifdef FEAT_CMDWIN
2055 if (cmdwin_type != 0 && wp == curwin)
2056 {
2057 /* draw the cmdline character in the leftmost column */
2058 n = 1;
2059 if (n > wp->w_width)
2060 n = wp->w_width;
2061 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2062 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
2063 cmdwin_type, ' ', hl_attr(HLF_AT));
2064 }
2065#endif
2066#ifdef FEAT_FOLDING
2067 if (wp->w_p_fdc > 0)
2068 {
2069 int nn = n + wp->w_p_fdc;
2070
2071 /* draw the fold column at the left */
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_FC));
2077 n = nn;
2078 }
2079#endif
2080#ifdef FEAT_SIGNS
2081 if (draw_signcolumn(wp))
2082 {
2083 int nn = n + 2;
2084
2085 /* draw the sign column after the fold column */
2086 if (nn > W_WIDTH(wp))
2087 nn = W_WIDTH(wp);
2088 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2089 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2090 ' ', ' ', hl_attr(HLF_SC));
2091 n = nn;
2092 }
2093#endif
2094 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2095 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
2096 c1, c2, hl_attr(hl));
2097 }
2098 set_empty_rows(wp, row);
2099}
2100
2101#ifdef FEAT_FOLDING
2102/*
2103 * Display one folded line.
2104 */
2105 static void
2106fold_line(wp, fold_count, foldinfo, lnum, row)
2107 win_T *wp;
2108 long fold_count;
2109 foldinfo_T *foldinfo;
2110 linenr_T lnum;
2111 int row;
2112{
2113 char_u buf[51];
2114 pos_T *top, *bot;
2115 linenr_T lnume = lnum + fold_count - 1;
2116 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002117 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 int col;
2120 int txtcol;
2121 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002122 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123
2124 /* Build the fold line:
2125 * 1. Add the cmdwin_type for the command-line window
2126 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002127 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 * 4. Compose the text
2129 * 5. Add the text
2130 * 6. set highlighting for the Visual area an other text
2131 */
2132 col = 0;
2133
2134 /*
2135 * 1. Add the cmdwin_type for the command-line window
2136 * Ignores 'rightleft', this window is never right-left.
2137 */
2138#ifdef FEAT_CMDWIN
2139 if (cmdwin_type != 0 && wp == curwin)
2140 {
2141 ScreenLines[off] = cmdwin_type;
2142 ScreenAttrs[off] = hl_attr(HLF_AT);
2143#ifdef FEAT_MBYTE
2144 if (enc_utf8)
2145 ScreenLinesUC[off] = 0;
2146#endif
2147 ++col;
2148 }
2149#endif
2150
2151 /*
2152 * 2. Add the 'foldcolumn'
2153 */
2154 fdc = wp->w_p_fdc;
2155 if (fdc > W_WIDTH(wp) - col)
2156 fdc = W_WIDTH(wp) - col;
2157 if (fdc > 0)
2158 {
2159 fill_foldcolumn(buf, wp, TRUE, lnum);
2160#ifdef FEAT_RIGHTLEFT
2161 if (wp->w_p_rl)
2162 {
2163 int i;
2164
2165 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
2166 hl_attr(HLF_FC));
2167 /* reverse the fold column */
2168 for (i = 0; i < fdc; ++i)
2169 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2170 }
2171 else
2172#endif
2173 copy_text_attr(off + col, buf, fdc, hl_attr(HLF_FC));
2174 col += fdc;
2175 }
2176
2177#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002178# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2179 for (ri = 0; ri < l; ++ri) \
2180 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2181 else \
2182 for (ri = 0; ri < l; ++ri) \
2183 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002185# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2186 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187#endif
2188
Bram Moolenaar64486672010-05-16 15:46:46 +02002189 /* Set all attributes of the 'number' or 'relativenumber' column and the
2190 * text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002191 RL_MEMSET(col, hl_attr(HLF_FL), W_WIDTH(wp) - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192
2193#ifdef FEAT_SIGNS
2194 /* If signs are being displayed, add two spaces. */
2195 if (draw_signcolumn(wp))
2196 {
2197 len = W_WIDTH(wp) - col;
2198 if (len > 0)
2199 {
2200 if (len > 2)
2201 len = 2;
2202# ifdef FEAT_RIGHTLEFT
2203 if (wp->w_p_rl)
2204 /* the line number isn't reversed */
2205 copy_text_attr(off + W_WIDTH(wp) - len - col,
2206 (char_u *)" ", len, hl_attr(HLF_FL));
2207 else
2208# endif
2209 copy_text_attr(off + col, (char_u *)" ", len, hl_attr(HLF_FL));
2210 col += len;
2211 }
2212 }
2213#endif
2214
2215 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002216 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002218 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 {
2220 len = W_WIDTH(wp) - col;
2221 if (len > 0)
2222 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002223 int w = number_width(wp);
Bram Moolenaar64486672010-05-16 15:46:46 +02002224 long num;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002225
2226 if (len > w + 1)
2227 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002228
2229 if (wp->w_p_nu)
2230 /* 'number' */
2231 num = (long)lnum;
2232 else
2233 /* 'relativenumber', don't use negative numbers */
2234 num = (long)abs((int)get_cursor_rel_lnum(wp, lnum));
2235
2236 sprintf((char *)buf, "%*ld ", w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237#ifdef FEAT_RIGHTLEFT
2238 if (wp->w_p_rl)
2239 /* the line number isn't reversed */
2240 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
2241 hl_attr(HLF_FL));
2242 else
2243#endif
2244 copy_text_attr(off + col, buf, len, hl_attr(HLF_FL));
2245 col += len;
2246 }
2247 }
2248
2249 /*
2250 * 4. Compose the folded-line string with 'foldtext', if set.
2251 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002252 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253
2254 txtcol = col; /* remember where text starts */
2255
2256 /*
2257 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2258 * Right-left text is put in columns 0 - number-col, normal text is put
2259 * in columns number-col - window-width.
2260 */
2261#ifdef FEAT_MBYTE
2262 if (has_mbyte)
2263 {
2264 int cells;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002265 int u8c, u8cc[MAX_MCO];
2266 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 int idx;
2268 int c_len;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002269 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270# ifdef FEAT_ARABIC
2271 int prev_c = 0; /* previous Arabic character */
2272 int prev_c1 = 0; /* first composing char for prev_c */
2273# endif
2274
2275# ifdef FEAT_RIGHTLEFT
2276 if (wp->w_p_rl)
2277 idx = off;
2278 else
2279# endif
2280 idx = off + col;
2281
2282 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2283 for (p = text; *p != NUL; )
2284 {
2285 cells = (*mb_ptr2cells)(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002286 c_len = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 if (col + cells > W_WIDTH(wp)
2288# ifdef FEAT_RIGHTLEFT
2289 - (wp->w_p_rl ? col : 0)
2290# endif
2291 )
2292 break;
2293 ScreenLines[idx] = *p;
2294 if (enc_utf8)
2295 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002296 u8c = utfc_ptr2char(p, u8cc);
2297 if (*p < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 {
2299 ScreenLinesUC[idx] = 0;
2300#ifdef FEAT_ARABIC
2301 prev_c = u8c;
2302#endif
2303 }
2304 else
2305 {
2306#ifdef FEAT_ARABIC
2307 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2308 {
2309 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002310 int pc, pc1, nc;
2311 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 int firstbyte = *p;
2313
2314 /* The idea of what is the previous and next
2315 * character depends on 'rightleft'. */
2316 if (wp->w_p_rl)
2317 {
2318 pc = prev_c;
2319 pc1 = prev_c1;
2320 nc = utf_ptr2char(p + c_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002321 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 }
2323 else
2324 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002325 pc = utfc_ptr2char(p + c_len, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002327 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 }
2329 prev_c = u8c;
2330
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002331 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 pc, pc1, nc);
2333 ScreenLines[idx] = firstbyte;
2334 }
2335 else
2336 prev_c = u8c;
2337#endif
2338 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar11936362007-09-17 20:39:42 +00002339#ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 if (u8c >= 0x10000)
2341 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2342 else
Bram Moolenaar11936362007-09-17 20:39:42 +00002343#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 ScreenLinesUC[idx] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002345 for (i = 0; i < Screen_mco; ++i)
2346 {
2347 ScreenLinesC[i][idx] = u8cc[i];
2348 if (u8cc[i] == 0)
2349 break;
2350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 }
2352 if (cells > 1)
2353 ScreenLines[idx + 1] = 0;
2354 }
Bram Moolenaar990bb662010-02-03 15:48:04 +01002355 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2356 /* double-byte single width character */
2357 ScreenLines2[idx] = p[1];
2358 else if (cells > 1)
2359 /* double-width character */
2360 ScreenLines[idx + 1] = p[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 col += cells;
2362 idx += cells;
2363 p += c_len;
2364 }
2365 }
2366 else
2367#endif
2368 {
2369 len = (int)STRLEN(text);
2370 if (len > W_WIDTH(wp) - col)
2371 len = W_WIDTH(wp) - col;
2372 if (len > 0)
2373 {
2374#ifdef FEAT_RIGHTLEFT
2375 if (wp->w_p_rl)
2376 STRNCPY(current_ScreenLine, text, len);
2377 else
2378#endif
2379 STRNCPY(current_ScreenLine + col, text, len);
2380 col += len;
2381 }
2382 }
2383
2384 /* Fill the rest of the line with the fold filler */
2385#ifdef FEAT_RIGHTLEFT
2386 if (wp->w_p_rl)
2387 col -= txtcol;
2388#endif
2389 while (col < W_WIDTH(wp)
2390#ifdef FEAT_RIGHTLEFT
2391 - (wp->w_p_rl ? txtcol : 0)
2392#endif
2393 )
2394 {
2395#ifdef FEAT_MBYTE
2396 if (enc_utf8)
2397 {
2398 if (fill_fold >= 0x80)
2399 {
2400 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002401 ScreenLinesC[0][off + col] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 }
2403 else
2404 ScreenLinesUC[off + col] = 0;
2405 }
2406#endif
2407 ScreenLines[off + col++] = fill_fold;
2408 }
2409
2410 if (text != buf)
2411 vim_free(text);
2412
2413 /*
2414 * 6. set highlighting for the Visual area an other text.
2415 * If all folded lines are in the Visual area, highlight the line.
2416 */
2417#ifdef FEAT_VISUAL
2418 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2419 {
2420 if (ltoreq(curwin->w_cursor, VIsual))
2421 {
2422 /* Visual is after curwin->w_cursor */
2423 top = &curwin->w_cursor;
2424 bot = &VIsual;
2425 }
2426 else
2427 {
2428 /* Visual is before curwin->w_cursor */
2429 top = &VIsual;
2430 bot = &curwin->w_cursor;
2431 }
2432 if (lnum >= top->lnum
2433 && lnume <= bot->lnum
2434 && (VIsual_mode != 'v'
2435 || ((lnum > top->lnum
2436 || (lnum == top->lnum
2437 && top->col == 0))
2438 && (lnume < bot->lnum
2439 || (lnume == bot->lnum
2440 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002441 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 {
2443 if (VIsual_mode == Ctrl_V)
2444 {
2445 /* Visual block mode: highlight the chars part of the block */
2446 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2447 {
2448 if (wp->w_old_cursor_lcol + txtcol < (colnr_T)W_WIDTH(wp))
2449 len = wp->w_old_cursor_lcol;
2450 else
2451 len = W_WIDTH(wp) - txtcol;
2452 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, hl_attr(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002453 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 }
2455 }
2456 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002457 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 /* Set all attributes of the text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002459 RL_MEMSET(txtcol, hl_attr(HLF_V), W_WIDTH(wp) - txtcol);
2460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 }
2462 }
2463#endif
2464
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002465#ifdef FEAT_SYN_HL
2466 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002467 if (wp->w_p_cuc)
2468 {
2469 txtcol += wp->w_virtcol;
2470 if (wp->w_p_wrap)
2471 txtcol -= wp->w_skipcol;
2472 else
2473 txtcol -= wp->w_leftcol;
2474 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2475 ScreenAttrs[off + txtcol] = hl_combine_attr(
2476 ScreenAttrs[off + txtcol], hl_attr(HLF_CUC));
2477 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002478#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479
2480 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2481 (int)W_WIDTH(wp), FALSE);
2482
2483 /*
2484 * Update w_cline_height and w_cline_folded if the cursor line was
2485 * updated (saves a call to plines() later).
2486 */
2487 if (wp == curwin
2488 && lnum <= curwin->w_cursor.lnum
2489 && lnume >= curwin->w_cursor.lnum)
2490 {
2491 curwin->w_cline_row = row;
2492 curwin->w_cline_height = 1;
2493 curwin->w_cline_folded = TRUE;
2494 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2495 }
2496}
2497
2498/*
2499 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2500 */
2501 static void
2502copy_text_attr(off, buf, len, attr)
2503 int off;
2504 char_u *buf;
2505 int len;
2506 int attr;
2507{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002508 int i;
2509
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 mch_memmove(ScreenLines + off, buf, (size_t)len);
2511# ifdef FEAT_MBYTE
2512 if (enc_utf8)
2513 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2514# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002515 for (i = 0; i < len; ++i)
2516 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517}
2518
2519/*
2520 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002521 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 */
2523 static void
2524fill_foldcolumn(p, wp, closed, lnum)
2525 char_u *p;
2526 win_T *wp;
2527 int closed; /* TRUE of FALSE */
2528 linenr_T lnum; /* current line number */
2529{
2530 int i = 0;
2531 int level;
2532 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002533 int empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534
2535 /* Init to all spaces. */
2536 copy_spaces(p, (size_t)wp->w_p_fdc);
2537
2538 level = win_foldinfo.fi_level;
2539 if (level > 0)
2540 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002541 /* If there is only one column put more info in it. */
2542 empty = (wp->w_p_fdc == 1) ? 0 : 1;
2543
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 /* If the column is too narrow, we start at the lowest level that
2545 * fits and use numbers to indicated the depth. */
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002546 first_level = level - wp->w_p_fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 if (first_level < 1)
2548 first_level = 1;
2549
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002550 for (i = 0; i + empty < wp->w_p_fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 {
2552 if (win_foldinfo.fi_lnum == lnum
2553 && first_level + i >= win_foldinfo.fi_low_level)
2554 p[i] = '-';
2555 else if (first_level == 1)
2556 p[i] = '|';
2557 else if (first_level + i <= 9)
2558 p[i] = '0' + first_level + i;
2559 else
2560 p[i] = '>';
2561 if (first_level + i == level)
2562 break;
2563 }
2564 }
2565 if (closed)
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002566 p[i >= wp->w_p_fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567}
2568#endif /* FEAT_FOLDING */
2569
2570/*
2571 * Display line "lnum" of window 'wp' on the screen.
2572 * Start at row "startrow", stop when "endrow" is reached.
2573 * wp->w_virtcol needs to be valid.
2574 *
2575 * Return the number of last row the line occupies.
2576 */
2577 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +00002578win_line(wp, lnum, startrow, endrow, nochange)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 win_T *wp;
2580 linenr_T lnum;
2581 int startrow;
2582 int endrow;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002583 int nochange UNUSED; /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584{
2585 int col; /* visual column on screen */
2586 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2587 int c = 0; /* init for GCC */
2588 long vcol = 0; /* virtual column (for tabs) */
2589 long vcol_prev = -1; /* "vcol" of previous character */
2590 char_u *line; /* current line */
2591 char_u *ptr; /* current position in "line" */
2592 int row; /* row in the window, excl w_winrow */
2593 int screen_row; /* row on the screen, incl w_winrow */
2594
2595 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2596 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00002597 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 int c_extra = NUL; /* extra chars, all the same */
2599 int extra_attr = 0; /* attributes when n_extra != 0 */
2600 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2601 displaying lcs_eol at end-of-line */
2602 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2603 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2604
2605 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2606 int saved_n_extra = 0;
2607 char_u *saved_p_extra = NULL;
2608 int saved_c_extra = 0;
2609 int saved_char_attr = 0;
2610
2611 int n_attr = 0; /* chars with special attr */
2612 int saved_attr2 = 0; /* char_attr saved for n_attr */
2613 int n_attr3 = 0; /* chars with overruling special attr */
2614 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2615
2616 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2617
2618 int fromcol, tocol; /* start/end of inverting */
2619 int fromcol_prev = -2; /* start of inverting after cursor */
2620 int noinvcur = FALSE; /* don't invert the cursor */
2621#ifdef FEAT_VISUAL
2622 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002623 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624#endif
2625 pos_T pos;
2626 long v;
2627
2628 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002629 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2631 in this line */
2632 int attr = 0; /* attributes for area highlighting */
2633 int area_attr = 0; /* attributes desired by highlighting */
2634 int search_attr = 0; /* attributes desired by 'hlsearch' */
2635#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002636 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 int syntax_attr = 0; /* attributes desired by syntax */
2638 int has_syntax = FALSE; /* this buffer has syntax highl. */
2639 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00002640 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002641#endif
2642#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002643 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002644# define SPWORDLEN 150
2645 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00002646 int nextlinecol = 0; /* column where nextline[] starts */
2647 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00002648 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00002649 int spell_attr = 0; /* attributes desired by spelling */
2650 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00002651 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2652 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00002653 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002654 static int cap_col = -1; /* column to check for Cap word */
2655 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002656 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657#endif
2658 int extra_check; /* has syntax or linebreak */
2659#ifdef FEAT_MBYTE
2660 int multi_attr = 0; /* attributes desired by multibyte */
2661 int mb_l = 1; /* multi-byte byte length */
2662 int mb_c = 0; /* decoded multi-byte character */
2663 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002664 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665#endif
2666#ifdef FEAT_DIFF
2667 int filler_lines; /* nr of filler lines to be drawn */
2668 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002669 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 int change_start = MAXCOL; /* first col of changed area */
2671 int change_end = -1; /* last col of changed area */
2672#endif
2673 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
2674#ifdef FEAT_LINEBREAK
2675 int need_showbreak = FALSE;
2676#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002677#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
2678 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00002680 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681#endif
2682#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00002683 matchitem_T *cur; /* points to the match list */
2684 match_T *shl; /* points to search_hl or a match */
2685 int shl_flag; /* flag to indicate whether search_hl
2686 has been processed or not */
2687 int prevcol_hl_flag; /* flag to indicate whether prevcol
2688 equals startcol of search_hl or one
2689 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690#endif
2691#ifdef FEAT_ARABIC
2692 int prev_c = 0; /* previous Arabic character */
2693 int prev_c1 = 0; /* first composing char for prev_c */
2694#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002695#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00002696 int did_line_attr = 0;
2697#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698
2699 /* draw_state: items that are drawn in sequence: */
2700#define WL_START 0 /* nothing done yet */
2701#ifdef FEAT_CMDWIN
2702# define WL_CMDLINE WL_START + 1 /* cmdline window column */
2703#else
2704# define WL_CMDLINE WL_START
2705#endif
2706#ifdef FEAT_FOLDING
2707# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
2708#else
2709# define WL_FOLD WL_CMDLINE
2710#endif
2711#ifdef FEAT_SIGNS
2712# define WL_SIGN WL_FOLD + 1 /* column for signs */
2713#else
2714# define WL_SIGN WL_FOLD /* column for signs */
2715#endif
2716#define WL_NR WL_SIGN + 1 /* line number */
2717#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
2718# define WL_SBR WL_NR + 1 /* 'showbreak' or 'diff' */
2719#else
2720# define WL_SBR WL_NR
2721#endif
2722#define WL_LINE WL_SBR + 1 /* text in the line */
2723 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00002724#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 int feedback_col = 0;
2726 int feedback_old_attr = -1;
2727#endif
2728
2729
2730 if (startrow > endrow) /* past the end already! */
2731 return startrow;
2732
2733 row = startrow;
2734 screen_row = row + W_WINROW(wp);
2735
2736 /*
2737 * To speed up the loop below, set extra_check when there is linebreak,
2738 * trailing white space and/or syntax processing to be done.
2739 */
2740#ifdef FEAT_LINEBREAK
2741 extra_check = wp->w_p_lbr;
2742#else
2743 extra_check = 0;
2744#endif
2745#ifdef FEAT_SYN_HL
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00002746 if (syntax_present(wp->w_buffer) && !wp->w_buffer->b_syn_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 {
2748 /* Prepare for syntax highlighting in this line. When there is an
2749 * error, stop syntax highlighting. */
2750 save_did_emsg = did_emsg;
2751 did_emsg = FALSE;
2752 syntax_start(wp, lnum);
2753 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00002754 wp->w_buffer->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 else
2756 {
2757 did_emsg = save_did_emsg;
2758 has_syntax = TRUE;
2759 extra_check = TRUE;
2760 }
2761 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002762#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00002763
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002764#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00002765 if (wp->w_p_spell
2766 && *wp->w_buffer->b_p_spl != NUL
2767 && wp->w_buffer->b_langp.ga_len > 0
2768 && *(char **)(wp->w_buffer->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00002769 {
2770 /* Prepare for spell checking. */
2771 has_spell = TRUE;
2772 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00002773
2774 /* Get the start of the next line, so that words that wrap to the next
2775 * line are found too: "et<line-break>al.".
2776 * Trick: skip a few chars for C/shell/Vim comments */
2777 nextline[SPWORDLEN] = NUL;
2778 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2779 {
2780 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
2781 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
2782 }
2783
2784 /* When a word wrapped from the previous line the start of the current
2785 * line is valid. */
2786 if (lnum == checked_lnum)
2787 cur_checked_col = checked_col;
2788 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002789
2790 /* When there was a sentence end in the previous line may require a
2791 * word starting with capital in this line. In line 1 always check
2792 * the first word. */
2793 if (lnum != capcol_lnum)
2794 cap_col = -1;
2795 if (lnum == 1)
2796 cap_col = 0;
2797 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00002798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799#endif
2800
2801 /*
2802 * handle visual active in this window
2803 */
2804 fromcol = -10;
2805 tocol = MAXCOL;
2806#ifdef FEAT_VISUAL
2807 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2808 {
2809 /* Visual is after curwin->w_cursor */
2810 if (ltoreq(curwin->w_cursor, VIsual))
2811 {
2812 top = &curwin->w_cursor;
2813 bot = &VIsual;
2814 }
2815 else /* Visual is before curwin->w_cursor */
2816 {
2817 top = &VIsual;
2818 bot = &curwin->w_cursor;
2819 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002820 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 if (VIsual_mode == Ctrl_V) /* block mode */
2822 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002823 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 {
2825 fromcol = wp->w_old_cursor_fcol;
2826 tocol = wp->w_old_cursor_lcol;
2827 }
2828 }
2829 else /* non-block mode */
2830 {
2831 if (lnum > top->lnum && lnum <= bot->lnum)
2832 fromcol = 0;
2833 else if (lnum == top->lnum)
2834 {
2835 if (VIsual_mode == 'V') /* linewise */
2836 fromcol = 0;
2837 else
2838 {
2839 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
2840 if (gchar_pos(top) == NUL)
2841 tocol = fromcol + 1;
2842 }
2843 }
2844 if (VIsual_mode != 'V' && lnum == bot->lnum)
2845 {
2846 if (*p_sel == 'e' && bot->col == 0
2847#ifdef FEAT_VIRTUALEDIT
2848 && bot->coladd == 0
2849#endif
2850 )
2851 {
2852 fromcol = -10;
2853 tocol = MAXCOL;
2854 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002855 else if (bot->col == MAXCOL)
2856 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 else
2858 {
2859 pos = *bot;
2860 if (*p_sel == 'e')
2861 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
2862 else
2863 {
2864 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
2865 ++tocol;
2866 }
2867 }
2868 }
2869 }
2870
2871#ifndef MSDOS
2872 /* Check if the character under the cursor should not be inverted */
2873 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
2874# ifdef FEAT_GUI
2875 && !gui.in_use
2876# endif
2877 )
2878 noinvcur = TRUE;
2879#endif
2880
2881 /* if inverting in this line set area_highlighting */
2882 if (fromcol >= 0)
2883 {
2884 area_highlighting = TRUE;
2885 attr = hl_attr(HLF_V);
2886#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
2887 if (clip_star.available && !clip_star.owned && clip_isautosel())
2888 attr = hl_attr(HLF_VNC);
2889#endif
2890 }
2891 }
2892
2893 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002894 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 */
2896 else
2897#endif /* FEAT_VISUAL */
2898 if (highlight_match
2899 && wp == curwin
2900 && lnum >= curwin->w_cursor.lnum
2901 && lnum <= curwin->w_cursor.lnum + search_match_lines)
2902 {
2903 if (lnum == curwin->w_cursor.lnum)
2904 getvcol(curwin, &(curwin->w_cursor),
2905 (colnr_T *)&fromcol, NULL, NULL);
2906 else
2907 fromcol = 0;
2908 if (lnum == curwin->w_cursor.lnum + search_match_lines)
2909 {
2910 pos.lnum = lnum;
2911 pos.col = search_match_endcol;
2912 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
2913 }
2914 else
2915 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00002916 /* do at least one character; happens when past end of line */
2917 if (fromcol == tocol)
2918 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 area_highlighting = TRUE;
2920 attr = hl_attr(HLF_I);
2921 }
2922
2923#ifdef FEAT_DIFF
2924 filler_lines = diff_check(wp, lnum);
2925 if (filler_lines < 0)
2926 {
2927 if (filler_lines == -1)
2928 {
2929 if (diff_find_change(wp, lnum, &change_start, &change_end))
2930 diff_hlf = HLF_ADD; /* added line */
2931 else if (change_start == 0)
2932 diff_hlf = HLF_TXD; /* changed text */
2933 else
2934 diff_hlf = HLF_CHD; /* changed line */
2935 }
2936 else
2937 diff_hlf = HLF_ADD; /* added line */
2938 filler_lines = 0;
2939 area_highlighting = TRUE;
2940 }
2941 if (lnum == wp->w_topline)
2942 filler_lines = wp->w_topfill;
2943 filler_todo = filler_lines;
2944#endif
2945
2946#ifdef LINE_ATTR
2947# ifdef FEAT_SIGNS
2948 /* If this line has a sign with line highlighting set line_attr. */
2949 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
2950 if (v != 0)
2951 line_attr = sign_get_attr((int)v, TRUE);
2952# endif
2953# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
2954 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002955 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 line_attr = hl_attr(HLF_L);
2957# endif
2958 if (line_attr != 0)
2959 area_highlighting = TRUE;
2960#endif
2961
2962 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
2963 ptr = line;
2964
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002965#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00002966 if (has_spell)
2967 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002968 /* For checking first word with a capital skip white space. */
2969 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002970 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002971
Bram Moolenaar30abd282005-06-22 22:35:10 +00002972 /* To be able to spell-check over line boundaries copy the end of the
2973 * current line into nextline[]. Above the start of the next line was
2974 * copied to nextline[SPWORDLEN]. */
2975 if (nextline[SPWORDLEN] == NUL)
2976 {
2977 /* No next line or it is empty. */
2978 nextlinecol = MAXCOL;
2979 nextline_idx = 0;
2980 }
2981 else
2982 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002983 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00002984 if (v < SPWORDLEN)
2985 {
2986 /* Short line, use it completely and append the start of the
2987 * next line. */
2988 nextlinecol = 0;
2989 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00002990 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00002991 nextline_idx = v + 1;
2992 }
2993 else
2994 {
2995 /* Long line, use only the last SPWORDLEN bytes. */
2996 nextlinecol = v - SPWORDLEN;
2997 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
2998 nextline_idx = SPWORDLEN + 1;
2999 }
3000 }
3001 }
3002#endif
3003
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 /* find start of trailing whitespace */
3005 if (wp->w_p_list && lcs_trail)
3006 {
3007 trailcol = (colnr_T)STRLEN(ptr);
3008 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
3009 --trailcol;
3010 trailcol += (colnr_T) (ptr - line);
3011 extra_check = TRUE;
3012 }
3013
3014 /*
3015 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3016 * first character to be displayed.
3017 */
3018 if (wp->w_p_wrap)
3019 v = wp->w_skipcol;
3020 else
3021 v = wp->w_leftcol;
3022 if (v > 0)
3023 {
3024#ifdef FEAT_MBYTE
3025 char_u *prev_ptr = ptr;
3026#endif
3027 while (vcol < v && *ptr != NUL)
3028 {
3029 c = win_lbr_chartabsize(wp, ptr, (colnr_T)vcol, NULL);
3030 vcol += c;
3031#ifdef FEAT_MBYTE
3032 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003034 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 }
3036
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003037#if defined(FEAT_SYN_HL) || defined(FEAT_VIRTUALEDIT) || defined(FEAT_VISUAL)
3038 /* When:
3039 * - 'cuc' is set, or
3040 * - 'virtualedit' is set, or
3041 * - the visual mode is active,
3042 * the end of the line may be before the start of the displayed part.
3043 */
3044 if (vcol < v && (
3045# ifdef FEAT_SYN_HL
3046 wp->w_p_cuc
3047# if defined(FEAT_VIRTUALEDIT) || defined(FEAT_VISUAL)
3048 ||
3049# endif
3050# endif
3051# ifdef FEAT_VIRTUALEDIT
3052 virtual_active()
3053# ifdef FEAT_VISUAL
3054 ||
3055# endif
3056# endif
3057# ifdef FEAT_VISUAL
3058 (VIsual_active && wp->w_buffer == curwin->w_buffer)
3059# endif
3060 ))
3061 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003063 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064#endif
3065
3066 /* Handle a character that's not completely on the screen: Put ptr at
3067 * that character but skip the first few screen characters. */
3068 if (vcol > v)
3069 {
3070 vcol -= c;
3071#ifdef FEAT_MBYTE
3072 ptr = prev_ptr;
3073#else
3074 --ptr;
3075#endif
3076 n_skip = v - vcol;
3077 }
3078
3079 /*
3080 * Adjust for when the inverted text is before the screen,
3081 * and when the start of the inverted text is before the screen.
3082 */
3083 if (tocol <= vcol)
3084 fromcol = 0;
3085 else if (fromcol >= 0 && fromcol < vcol)
3086 fromcol = vcol;
3087
3088#ifdef FEAT_LINEBREAK
3089 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3090 if (wp->w_p_wrap)
3091 need_showbreak = TRUE;
3092#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003093#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003094 /* When spell checking a word we need to figure out the start of the
3095 * word and if it's badly spelled or not. */
3096 if (has_spell)
3097 {
3098 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003099 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003100 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003101
3102 pos = wp->w_cursor;
3103 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003104 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003105 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003106
3107 /* spell_move_to() may call ml_get() and make "line" invalid */
3108 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3109 ptr = line + linecol;
3110
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003111 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003112 {
3113 /* no bad word found at line start, don't check until end of a
3114 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003115 spell_hlf = HLF_COUNT;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003116 word_end = (int)(spell_to_word_end(ptr, wp->w_buffer)
3117 - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003118 }
3119 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003120 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003121 /* bad word found, use attributes until end of word */
3122 word_end = wp->w_cursor.col + len + 1;
3123
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003124 /* Turn index into actual attributes. */
3125 if (spell_hlf != HLF_COUNT)
3126 spell_attr = highlight_attr[spell_hlf];
3127 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003128 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003129
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003130# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003131 /* Need to restart syntax highlighting for this line. */
3132 if (has_syntax)
3133 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003134# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003135 }
3136#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 }
3138
3139 /*
3140 * Correct highlighting for cursor that can't be disabled.
3141 * Avoids having to check this for each character.
3142 */
3143 if (fromcol >= 0)
3144 {
3145 if (noinvcur)
3146 {
3147 if ((colnr_T)fromcol == wp->w_virtcol)
3148 {
3149 /* highlighting starts at cursor, let it start just after the
3150 * cursor */
3151 fromcol_prev = fromcol;
3152 fromcol = -1;
3153 }
3154 else if ((colnr_T)fromcol < wp->w_virtcol)
3155 /* restart highlighting after the cursor */
3156 fromcol_prev = wp->w_virtcol;
3157 }
3158 if (fromcol >= tocol)
3159 fromcol = -1;
3160 }
3161
3162#ifdef FEAT_SEARCH_EXTRA
3163 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003164 * Handle highlighting the last used search pattern and matches.
3165 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003167 cur = wp->w_match_head;
3168 shl_flag = FALSE;
3169 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003171 if (shl_flag == FALSE)
3172 {
3173 shl = &search_hl;
3174 shl_flag = TRUE;
3175 }
3176 else
3177 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003178 shl->startcol = MAXCOL;
3179 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 shl->attr_cur = 0;
3181 if (shl->rm.regprog != NULL)
3182 {
3183 v = (long)(ptr - line);
3184 next_search_hl(wp, shl, lnum, (colnr_T)v);
3185
3186 /* Need to get the line again, a multi-line regexp may have made it
3187 * invalid. */
3188 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3189 ptr = line + v;
3190
3191 if (shl->lnum != 0 && shl->lnum <= lnum)
3192 {
3193 if (shl->lnum == lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003194 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003196 shl->startcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3198 - shl->rm.startpos[0].lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003199 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003201 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 /* Highlight one character for an empty match. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003203 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 {
3205#ifdef FEAT_MBYTE
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003206 if (has_mbyte && line[shl->endcol] != NUL)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003207 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 else
3209#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003210 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003212 if ((long)shl->startcol < v) /* match at leftcol */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 {
3214 shl->attr_cur = shl->attr;
3215 search_attr = shl->attr;
3216 }
3217 area_highlighting = TRUE;
3218 }
3219 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003220 if (shl != &search_hl && cur != NULL)
3221 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 }
3223#endif
3224
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003225#ifdef FEAT_SYN_HL
Bram Moolenaare2f98b92006-03-29 21:18:24 +00003226 /* Cursor line highlighting for 'cursorline'. Not when Visual mode is
3227 * active, because it's not clear what is selected then. */
3228 if (wp->w_p_cul && lnum == wp->w_cursor.lnum && !VIsual_active)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003229 {
3230 line_attr = hl_attr(HLF_CUL);
3231 area_highlighting = TRUE;
3232 }
3233#endif
3234
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003235 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 col = 0;
3237#ifdef FEAT_RIGHTLEFT
3238 if (wp->w_p_rl)
3239 {
3240 /* Rightleft window: process the text in the normal direction, but put
3241 * it in current_ScreenLine[] from right to left. Start at the
3242 * rightmost column of the window. */
3243 col = W_WIDTH(wp) - 1;
3244 off += col;
3245 }
3246#endif
3247
3248 /*
3249 * Repeat for the whole displayed line.
3250 */
3251 for (;;)
3252 {
3253 /* Skip this quickly when working on the text. */
3254 if (draw_state != WL_LINE)
3255 {
3256#ifdef FEAT_CMDWIN
3257 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3258 {
3259 draw_state = WL_CMDLINE;
3260 if (cmdwin_type != 0 && wp == curwin)
3261 {
3262 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003264 c_extra = cmdwin_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 char_attr = hl_attr(HLF_AT);
3266 }
3267 }
3268#endif
3269
3270#ifdef FEAT_FOLDING
3271 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3272 {
3273 draw_state = WL_FOLD;
3274 if (wp->w_p_fdc > 0)
3275 {
3276 /* Draw the 'foldcolumn'. */
3277 fill_foldcolumn(extra, wp, FALSE, lnum);
3278 n_extra = wp->w_p_fdc;
3279 p_extra = extra;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003280 p_extra[n_extra] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 c_extra = NUL;
3282 char_attr = hl_attr(HLF_FC);
3283 }
3284 }
3285#endif
3286
3287#ifdef FEAT_SIGNS
3288 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3289 {
3290 draw_state = WL_SIGN;
3291 /* Show the sign column when there are any signs in this
3292 * buffer or when using Netbeans. */
3293 if (draw_signcolumn(wp)
3294# ifdef FEAT_DIFF
3295 && filler_todo <= 0
3296# endif
3297 )
3298 {
3299 int_u text_sign;
3300# ifdef FEAT_SIGN_ICONS
3301 int_u icon_sign;
3302# endif
3303
3304 /* Draw two cells with the sign value or blank. */
3305 c_extra = ' ';
3306 char_attr = hl_attr(HLF_SC);
3307 n_extra = 2;
3308
3309 if (row == startrow)
3310 {
3311 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3312 SIGN_TEXT);
3313# ifdef FEAT_SIGN_ICONS
3314 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3315 SIGN_ICON);
3316 if (gui.in_use && icon_sign != 0)
3317 {
3318 /* Use the image in this position. */
3319 c_extra = SIGN_BYTE;
3320# ifdef FEAT_NETBEANS_INTG
3321 if (buf_signcount(wp->w_buffer, lnum) > 1)
3322 c_extra = MULTISIGN_BYTE;
3323# endif
3324 char_attr = icon_sign;
3325 }
3326 else
3327# endif
3328 if (text_sign != 0)
3329 {
3330 p_extra = sign_get_text(text_sign);
3331 if (p_extra != NULL)
3332 {
3333 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003334 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 }
3336 char_attr = sign_get_attr(text_sign, FALSE);
3337 }
3338 }
3339 }
3340 }
3341#endif
3342
3343 if (draw_state == WL_NR - 1 && n_extra == 0)
3344 {
3345 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003346 /* Display the absolute or relative line number. After the
3347 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3348 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 && (row == startrow
3350#ifdef FEAT_DIFF
3351 + filler_lines
3352#endif
3353 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3354 {
3355 /* Draw the line number (empty space after wrapping). */
3356 if (row == startrow
3357#ifdef FEAT_DIFF
3358 + filler_lines
3359#endif
3360 )
3361 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003362 long num;
3363
3364 if (wp->w_p_nu)
3365 /* 'number' */
3366 num = (long)lnum;
3367 else
3368 /* 'relativenumber', don't use negative numbers */
3369 num = (long)abs((int)get_cursor_rel_lnum(wp,
3370 lnum));
3371
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003372 sprintf((char *)extra, "%*ld ",
Bram Moolenaar64486672010-05-16 15:46:46 +02003373 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 if (wp->w_skipcol > 0)
3375 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3376 *p_extra = '-';
3377#ifdef FEAT_RIGHTLEFT
3378 if (wp->w_p_rl) /* reverse line numbers */
3379 rl_mirror(extra);
3380#endif
3381 p_extra = extra;
3382 c_extra = NUL;
3383 }
3384 else
3385 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003386 n_extra = number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 char_attr = hl_attr(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003388#ifdef FEAT_SYN_HL
3389 /* When 'cursorline' is set highlight the line number of
3390 * the current line differently. */
3391 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3392 char_attr = hl_combine_attr(hl_attr(HLF_CUL), char_attr);
3393#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 }
3395 }
3396
3397#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3398 if (draw_state == WL_SBR - 1 && n_extra == 0)
3399 {
3400 draw_state = WL_SBR;
3401# ifdef FEAT_DIFF
3402 if (filler_todo > 0)
3403 {
3404 /* Draw "deleted" diff line(s). */
3405 if (char2cells(fill_diff) > 1)
3406 c_extra = '-';
3407 else
3408 c_extra = fill_diff;
3409# ifdef FEAT_RIGHTLEFT
3410 if (wp->w_p_rl)
3411 n_extra = col + 1;
3412 else
3413# endif
3414 n_extra = W_WIDTH(wp) - col;
3415 char_attr = hl_attr(HLF_DED);
3416 }
3417# endif
3418# ifdef FEAT_LINEBREAK
3419 if (*p_sbr != NUL && need_showbreak)
3420 {
3421 /* Draw 'showbreak' at the start of each broken line. */
3422 p_extra = p_sbr;
3423 c_extra = NUL;
3424 n_extra = (int)STRLEN(p_sbr);
3425 char_attr = hl_attr(HLF_AT);
3426 need_showbreak = FALSE;
3427 /* Correct end of highlighted area for 'showbreak',
3428 * required when 'linebreak' is also set. */
3429 if (tocol == vcol)
3430 tocol += n_extra;
3431 }
3432# endif
3433 }
3434#endif
3435
3436 if (draw_state == WL_LINE - 1 && n_extra == 0)
3437 {
3438 draw_state = WL_LINE;
3439 if (saved_n_extra)
3440 {
3441 /* Continue item from end of wrapped line. */
3442 n_extra = saved_n_extra;
3443 c_extra = saved_c_extra;
3444 p_extra = saved_p_extra;
3445 char_attr = saved_char_attr;
3446 }
3447 else
3448 char_attr = 0;
3449 }
3450 }
3451
3452 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003453 if (dollar_vcol != 0 && wp == curwin
3454 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455#ifdef FEAT_DIFF
3456 && filler_todo <= 0
3457#endif
3458 )
3459 {
3460 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3461 wp->w_p_rl);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003462 /* Pretend we have finished updating the window. Except when
3463 * 'cursorcolumn' is set. */
3464#ifdef FEAT_SYN_HL
3465 if (wp->w_p_cuc)
3466 row = wp->w_cline_row + wp->w_cline_height;
3467 else
3468#endif
3469 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 break;
3471 }
3472
3473 if (draw_state == WL_LINE && area_highlighting)
3474 {
3475 /* handle Visual or match highlighting in this line */
3476 if (vcol == fromcol
3477#ifdef FEAT_MBYTE
3478 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3479 && (*mb_ptr2cells)(ptr) > 1)
3480#endif
3481 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00003482 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 && vcol < tocol))
3484 area_attr = attr; /* start highlighting */
3485 else if (area_attr != 0
3486 && (vcol == tocol
3487 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489
3490#ifdef FEAT_SEARCH_EXTRA
3491 if (!n_extra)
3492 {
3493 /*
3494 * Check for start/end of search pattern match.
3495 * After end, check for start/end of next match.
3496 * When another match, have to check for start again.
3497 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003498 * Do this for 'search_hl' and the match list (ordered by
3499 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003501 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003502 cur = wp->w_match_head;
3503 shl_flag = FALSE;
3504 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003506 if (shl_flag == FALSE
3507 && ((cur != NULL
3508 && cur->priority > SEARCH_HL_PRIORITY)
3509 || cur == NULL))
3510 {
3511 shl = &search_hl;
3512 shl_flag = TRUE;
3513 }
3514 else
3515 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 while (shl->rm.regprog != NULL)
3517 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003518 if (shl->startcol != MAXCOL
3519 && v >= (long)shl->startcol
3520 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 {
3522 shl->attr_cur = shl->attr;
3523 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003524 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 {
3526 shl->attr_cur = 0;
3527
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 next_search_hl(wp, shl, lnum, (colnr_T)v);
3529
3530 /* Need to get the line again, a multi-line regexp
3531 * may have made it invalid. */
3532 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3533 ptr = line + v;
3534
3535 if (shl->lnum == lnum)
3536 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003537 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003539 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003541 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003543 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 {
3545 /* highlight empty match, try again after
3546 * it */
3547#ifdef FEAT_MBYTE
3548 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003549 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003550 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 else
3552#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003553 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 }
3555
3556 /* Loop to check if the match starts at the
3557 * current position */
3558 continue;
3559 }
3560 }
3561 break;
3562 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003563 if (shl != &search_hl && cur != NULL)
3564 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003566
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003567 /* Use attributes from match with highest priority among
3568 * 'search_hl' and the match list. */
3569 search_attr = search_hl.attr_cur;
3570 cur = wp->w_match_head;
3571 shl_flag = FALSE;
3572 while (cur != NULL || shl_flag == FALSE)
3573 {
3574 if (shl_flag == FALSE
3575 && ((cur != NULL
3576 && cur->priority > SEARCH_HL_PRIORITY)
3577 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003578 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003579 shl = &search_hl;
3580 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003581 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003582 else
3583 shl = &cur->hl;
3584 if (shl->attr_cur != 0)
3585 search_attr = shl->attr_cur;
3586 if (shl != &search_hl && cur != NULL)
3587 cur = cur->next;
3588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 }
3590#endif
3591
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003593 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003595 if (diff_hlf == HLF_CHD && ptr - line >= change_start
3596 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003598 if (diff_hlf == HLF_TXD && ptr - line > change_end
3599 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003601 line_attr = hl_attr(diff_hlf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 }
3603#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003604
3605 /* Decide which of the highlight attributes to use. */
3606 attr_pri = TRUE;
3607 if (area_attr != 0)
3608 char_attr = area_attr;
3609 else if (search_attr != 0)
3610 char_attr = search_attr;
3611#ifdef LINE_ATTR
3612 /* Use line_attr when not in the Visual or 'incsearch' area
3613 * (area_attr may be 0 when "noinvcur" is set). */
3614 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00003615 || vcol < fromcol || vcol_prev < fromcol_prev
3616 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003617 char_attr = line_attr;
3618#endif
3619 else
3620 {
3621 attr_pri = FALSE;
3622#ifdef FEAT_SYN_HL
3623 if (has_syntax)
3624 char_attr = syntax_attr;
3625 else
3626#endif
3627 char_attr = 0;
3628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 }
3630
3631 /*
3632 * Get the next character to put on the screen.
3633 */
3634 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00003635 * The "p_extra" points to the extra stuff that is inserted to
3636 * represent special characters (non-printable stuff) and other
3637 * things. When all characters are the same, c_extra is used.
3638 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
3639 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
3641 */
3642 if (n_extra > 0)
3643 {
3644 if (c_extra != NUL)
3645 {
3646 c = c_extra;
3647#ifdef FEAT_MBYTE
3648 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
3649 if (enc_utf8 && (*mb_char2len)(c) > 1)
3650 {
3651 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003652 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003653 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 }
3655 else
3656 mb_utf8 = FALSE;
3657#endif
3658 }
3659 else
3660 {
3661 c = *p_extra;
3662#ifdef FEAT_MBYTE
3663 if (has_mbyte)
3664 {
3665 mb_c = c;
3666 if (enc_utf8)
3667 {
3668 /* If the UTF-8 character is more than one byte:
3669 * Decode it into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003670 mb_l = (*mb_ptr2len)(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 mb_utf8 = FALSE;
3672 if (mb_l > n_extra)
3673 mb_l = 1;
3674 else if (mb_l > 1)
3675 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003676 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003678 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 }
3680 }
3681 else
3682 {
3683 /* if this is a DBCS character, put it in "mb_c" */
3684 mb_l = MB_BYTE2LEN(c);
3685 if (mb_l >= n_extra)
3686 mb_l = 1;
3687 else if (mb_l > 1)
3688 mb_c = (c << 8) + p_extra[1];
3689 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003690 if (mb_l == 0) /* at the NUL at end-of-line */
3691 mb_l = 1;
3692
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 /* If a double-width char doesn't fit display a '>' in the
3694 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003695 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696# ifdef FEAT_RIGHTLEFT
3697 wp->w_p_rl ? (col <= 0) :
3698# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003699 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 && (*mb_char2cells)(mb_c) == 2)
3701 {
3702 c = '>';
3703 mb_c = c;
3704 mb_l = 1;
3705 mb_utf8 = FALSE;
3706 multi_attr = hl_attr(HLF_AT);
3707 /* put the pointer back to output the double-width
3708 * character at the start of the next line. */
3709 ++n_extra;
3710 --p_extra;
3711 }
3712 else
3713 {
3714 n_extra -= mb_l - 1;
3715 p_extra += mb_l - 1;
3716 }
3717 }
3718#endif
3719 ++p_extra;
3720 }
3721 --n_extra;
3722 }
3723 else
3724 {
3725 /*
3726 * Get a character from the line itself.
3727 */
3728 c = *ptr;
3729#ifdef FEAT_MBYTE
3730 if (has_mbyte)
3731 {
3732 mb_c = c;
3733 if (enc_utf8)
3734 {
3735 /* If the UTF-8 character is more than one byte: Decode it
3736 * into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003737 mb_l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 mb_utf8 = FALSE;
3739 if (mb_l > 1)
3740 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003741 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 /* Overlong encoded ASCII or ASCII with composing char
3743 * is displayed normally, except a NUL. */
3744 if (mb_c < 0x80)
3745 c = mb_c;
3746 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003747
3748 /* At start of the line we can have a composing char.
3749 * Draw it as a space with a composing char. */
3750 if (utf_iscomposing(mb_c))
3751 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003752 int i;
3753
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003754 for (i = Screen_mco - 1; i > 0; --i)
3755 u8cc[i] = u8cc[i - 1];
3756 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003757 mb_c = ' ';
3758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 }
3760
3761 if ((mb_l == 1 && c >= 0x80)
3762 || (mb_l >= 1 && mb_c == 0)
3763 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00003764# ifdef UNICODE16
3765 || mb_c >= 0x10000
3766# endif
3767 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 {
3769 /*
3770 * Illegal UTF-8 byte: display as <xx>.
3771 * Non-BMP character : display as ? or fullwidth ?.
3772 */
Bram Moolenaar11936362007-09-17 20:39:42 +00003773# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00003775# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 {
3777 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003778# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 if (wp->w_p_rl) /* reverse */
3780 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003781# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 }
Bram Moolenaar11936362007-09-17 20:39:42 +00003783# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 else if (utf_char2cells(mb_c) != 2)
3785 STRCPY(extra, "?");
3786 else
3787 /* 0xff1f in UTF-8: full-width '?' */
3788 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00003789# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790
3791 p_extra = extra;
3792 c = *p_extra;
3793 mb_c = mb_ptr2char_adv(&p_extra);
3794 mb_utf8 = (c >= 0x80);
3795 n_extra = (int)STRLEN(p_extra);
3796 c_extra = NUL;
3797 if (area_attr == 0 && search_attr == 0)
3798 {
3799 n_attr = n_extra + 1;
3800 extra_attr = hl_attr(HLF_8);
3801 saved_attr2 = char_attr; /* save current attr */
3802 }
3803 }
3804 else if (mb_l == 0) /* at the NUL at end-of-line */
3805 mb_l = 1;
3806#ifdef FEAT_ARABIC
3807 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
3808 {
3809 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003810 int pc, pc1, nc;
3811 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812
3813 /* The idea of what is the previous and next
3814 * character depends on 'rightleft'. */
3815 if (wp->w_p_rl)
3816 {
3817 pc = prev_c;
3818 pc1 = prev_c1;
3819 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003820 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 }
3822 else
3823 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003824 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003826 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 }
3828 prev_c = mb_c;
3829
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003830 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 }
3832 else
3833 prev_c = mb_c;
3834#endif
3835 }
3836 else /* enc_dbcs */
3837 {
3838 mb_l = MB_BYTE2LEN(c);
3839 if (mb_l == 0) /* at the NUL at end-of-line */
3840 mb_l = 1;
3841 else if (mb_l > 1)
3842 {
3843 /* We assume a second byte below 32 is illegal.
3844 * Hopefully this is OK for all double-byte encodings!
3845 */
3846 if (ptr[1] >= 32)
3847 mb_c = (c << 8) + ptr[1];
3848 else
3849 {
3850 if (ptr[1] == NUL)
3851 {
3852 /* head byte at end of line */
3853 mb_l = 1;
3854 transchar_nonprint(extra, c);
3855 }
3856 else
3857 {
3858 /* illegal tail byte */
3859 mb_l = 2;
3860 STRCPY(extra, "XX");
3861 }
3862 p_extra = extra;
3863 n_extra = (int)STRLEN(extra) - 1;
3864 c_extra = NUL;
3865 c = *p_extra++;
3866 if (area_attr == 0 && search_attr == 0)
3867 {
3868 n_attr = n_extra + 1;
3869 extra_attr = hl_attr(HLF_8);
3870 saved_attr2 = char_attr; /* save current attr */
3871 }
3872 mb_c = c;
3873 }
3874 }
3875 }
3876 /* If a double-width char doesn't fit display a '>' in the
3877 * last column; the character is displayed at the start of the
3878 * next line. */
3879 if ((
3880# ifdef FEAT_RIGHTLEFT
3881 wp->w_p_rl ? (col <= 0) :
3882# endif
3883 (col >= W_WIDTH(wp) - 1))
3884 && (*mb_char2cells)(mb_c) == 2)
3885 {
3886 c = '>';
3887 mb_c = c;
3888 mb_utf8 = FALSE;
3889 mb_l = 1;
3890 multi_attr = hl_attr(HLF_AT);
3891 /* Put pointer back so that the character will be
3892 * displayed at the start of the next line. */
3893 --ptr;
3894 }
3895 else if (*ptr != NUL)
3896 ptr += mb_l - 1;
3897
3898 /* If a double-width char doesn't fit at the left side display
3899 * a '<' in the first column. */
3900 if (n_skip > 0 && mb_l > 1)
3901 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003903 c_extra = '<';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 c = ' ';
3905 if (area_attr == 0 && search_attr == 0)
3906 {
3907 n_attr = n_extra + 1;
3908 extra_attr = hl_attr(HLF_AT);
3909 saved_attr2 = char_attr; /* save current attr */
3910 }
3911 mb_c = c;
3912 mb_utf8 = FALSE;
3913 mb_l = 1;
3914 }
3915
3916 }
3917#endif
3918 ++ptr;
3919
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003920 /* 'list' : change char 160 to lcs_nbsp. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003921 if (wp->w_p_list && (c == 160
3922#ifdef FEAT_MBYTE
3923 || (mb_utf8 && mb_c == 160)
3924#endif
3925 ) && lcs_nbsp)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003926 {
3927 c = lcs_nbsp;
3928 if (area_attr == 0 && search_attr == 0)
3929 {
3930 n_attr = 1;
3931 extra_attr = hl_attr(HLF_8);
3932 saved_attr2 = char_attr; /* save current attr */
3933 }
3934#ifdef FEAT_MBYTE
3935 mb_c = c;
3936 if (enc_utf8 && (*mb_char2len)(c) > 1)
3937 {
3938 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003939 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003940 c = 0xc0;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003941 }
3942 else
3943 mb_utf8 = FALSE;
3944#endif
3945 }
3946
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 if (extra_check)
3948 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003949#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003950 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003951#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003952
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003953#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 /* Get syntax attribute, unless still at the start of the line
3955 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003956 v = (long)(ptr - line);
3957 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 {
3959 /* Get the syntax attribute for the character. If there
3960 * is an error, disable syntax highlighting. */
3961 save_did_emsg = did_emsg;
3962 did_emsg = FALSE;
3963
Bram Moolenaar217ad922005-03-20 22:37:15 +00003964 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003965# ifdef FEAT_SPELL
3966 has_spell ? &can_spell :
3967# endif
Bram Moolenaar56cefaf2008-01-12 15:47:10 +00003968 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969
3970 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00003971 {
3972 wp->w_buffer->b_syn_error = TRUE;
3973 has_syntax = FALSE;
3974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 else
3976 did_emsg = save_did_emsg;
3977
3978 /* Need to get the line again, a multi-line regexp may
3979 * have made it invalid. */
3980 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3981 ptr = line + v;
3982
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003983 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003985 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00003986 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003988#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003989
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003990#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003991 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00003992 * Only do this when there is no syntax highlighting, the
3993 * @Spell cluster is not used or the current syntax item
3994 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003995 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003996 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003997 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003998# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003999 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004000 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004001# endif
4002 if (c != 0 && (
4003# ifdef FEAT_SYN_HL
4004 !has_syntax ||
4005# endif
4006 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004007 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004008 char_u *prev_ptr, *p;
4009 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004010 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004011# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004012 if (has_mbyte)
4013 {
4014 prev_ptr = ptr - mb_l;
4015 v -= mb_l - 1;
4016 }
4017 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004018# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004019 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004020
4021 /* Use nextline[] if possible, it has the start of the
4022 * next line concatenated. */
4023 if ((prev_ptr - line) - nextlinecol >= 0)
4024 p = nextline + (prev_ptr - line) - nextlinecol;
4025 else
4026 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004027 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004028 len = spell_check(wp, p, &spell_hlf, &cap_col,
4029 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004030 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004031
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004032 /* In Insert mode only highlight a word that
4033 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004034 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004035 && (State & INSERT) != 0
4036 && wp->w_cursor.lnum == lnum
4037 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004038 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004039 && wp->w_cursor.col < (colnr_T)word_end)
4040 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004041 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004042 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004043 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004044
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004045 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004046 && (p - nextline) + len > nextline_idx)
4047 {
4048 /* Remember that the good word continues at the
4049 * start of the next line. */
4050 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004051 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004052 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004053
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004054 /* Turn index into actual attributes. */
4055 if (spell_hlf != HLF_COUNT)
4056 spell_attr = highlight_attr[spell_hlf];
4057
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004058 if (cap_col > 0)
4059 {
4060 if (p != prev_ptr
4061 && (p - nextline) + cap_col >= nextline_idx)
4062 {
4063 /* Remember that the word in the next line
4064 * must start with a capital. */
4065 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004066 cap_col = (int)((p - nextline) + cap_col
4067 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004068 }
4069 else
4070 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004071 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004072 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004073 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004074 }
4075 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004076 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004077 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004078 char_attr = hl_combine_attr(char_attr, spell_attr);
4079 else
4080 char_attr = hl_combine_attr(spell_attr, char_attr);
4081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082#endif
4083#ifdef FEAT_LINEBREAK
4084 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004085 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 */
4087 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004088 && !wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 {
4090 n_extra = win_lbr_chartabsize(wp, ptr - (
4091# ifdef FEAT_MBYTE
4092 has_mbyte ? mb_l :
4093# endif
4094 1), (colnr_T)vcol, NULL) - 1;
4095 c_extra = ' ';
4096 if (vim_iswhite(c))
4097 c = ' ';
4098 }
4099#endif
4100
4101 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4102 {
4103 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004104 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 {
4106 n_attr = 1;
4107 extra_attr = hl_attr(HLF_8);
4108 saved_attr2 = char_attr; /* save current attr */
4109 }
4110#ifdef FEAT_MBYTE
4111 mb_c = c;
4112 if (enc_utf8 && (*mb_char2len)(c) > 1)
4113 {
4114 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004115 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004116 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 }
4118 else
4119 mb_utf8 = FALSE;
4120#endif
4121 }
4122 }
4123
4124 /*
4125 * Handling of non-printable characters.
4126 */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004127 if (!(chartab[c & 0xff] & CT_PRINT_CHAR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 {
4129 /*
4130 * when getting a character from the file, we may have to
4131 * turn it into something else on the way to putting it
4132 * into "ScreenLines".
4133 */
4134 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4135 {
4136 /* tab amount depends on current column */
4137 n_extra = (int)wp->w_buffer->b_p_ts
4138 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4139#ifdef FEAT_MBYTE
4140 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4141#endif
4142 if (wp->w_p_list)
4143 {
4144 c = lcs_tab1;
4145 c_extra = lcs_tab2;
4146 n_attr = n_extra + 1;
4147 extra_attr = hl_attr(HLF_8);
4148 saved_attr2 = char_attr; /* save current attr */
4149#ifdef FEAT_MBYTE
4150 mb_c = c;
4151 if (enc_utf8 && (*mb_char2len)(c) > 1)
4152 {
4153 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004154 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004155 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 }
4157#endif
4158 }
4159 else
4160 {
4161 c_extra = ' ';
4162 c = ' ';
4163 }
4164 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004165 else if (c == NUL
4166 && ((wp->w_p_list && lcs_eol > 0)
4167 || ((fromcol >= 0 || fromcol_prev >= 0)
4168 && tocol > vcol
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004169#ifdef FEAT_VISUAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004170 && VIsual_mode != Ctrl_V
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004171#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004172 && (
4173# ifdef FEAT_RIGHTLEFT
4174 wp->w_p_rl ? (col >= 0) :
4175# endif
4176 (col < W_WIDTH(wp)))
4177 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004178 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004179 && (colnr_T)vcol == wp->w_virtcol)))
4180 && lcs_eol_one >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004182 /* Display a '$' after the line or highlight an extra
4183 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4185 /* For a diff line the highlighting continues after the
4186 * "$". */
4187 if (
4188# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004189 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190# ifdef LINE_ATTR
4191 &&
4192# endif
4193# endif
4194# ifdef LINE_ATTR
4195 line_attr == 0
4196# endif
4197 )
4198#endif
4199 {
4200#ifdef FEAT_VIRTUALEDIT
4201 /* In virtualedit, visual selections may extend
4202 * beyond end of line. */
4203 if (area_highlighting && virtual_active()
4204 && tocol != MAXCOL && vcol < tocol)
4205 n_extra = 0;
4206 else
4207#endif
4208 {
4209 p_extra = at_end_str;
4210 n_extra = 1;
4211 c_extra = NUL;
4212 }
4213 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004214 if (wp->w_p_list)
4215 c = lcs_eol;
4216 else
4217 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 lcs_eol_one = -1;
4219 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004220 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 {
4222 extra_attr = hl_attr(HLF_AT);
4223 n_attr = 1;
4224 }
4225#ifdef FEAT_MBYTE
4226 mb_c = c;
4227 if (enc_utf8 && (*mb_char2len)(c) > 1)
4228 {
4229 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004230 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004231 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 }
4233 else
4234 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4235#endif
4236 }
4237 else if (c != NUL)
4238 {
4239 p_extra = transchar(c);
4240#ifdef FEAT_RIGHTLEFT
4241 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4242 rl_mirror(p_extra); /* reverse "<12>" */
4243#endif
4244 n_extra = byte2cells(c) - 1;
4245 c_extra = NUL;
4246 c = *p_extra++;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004247 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 {
4249 n_attr = n_extra + 1;
4250 extra_attr = hl_attr(HLF_8);
4251 saved_attr2 = char_attr; /* save current attr */
4252 }
4253#ifdef FEAT_MBYTE
4254 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4255#endif
4256 }
4257#ifdef FEAT_VIRTUALEDIT
4258 else if (VIsual_active
4259 && (VIsual_mode == Ctrl_V
4260 || VIsual_mode == 'v')
4261 && virtual_active()
4262 && tocol != MAXCOL
4263 && vcol < tocol
4264 && (
4265# ifdef FEAT_RIGHTLEFT
4266 wp->w_p_rl ? (col >= 0) :
4267# endif
4268 (col < W_WIDTH(wp))))
4269 {
4270 c = ' ';
4271 --ptr; /* put it back at the NUL */
4272 }
4273#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004274#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 else if ((
4276# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004277 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 ) && (
4281# ifdef FEAT_RIGHTLEFT
4282 wp->w_p_rl ? (col >= 0) :
4283# endif
4284 (col < W_WIDTH(wp))))
4285 {
4286 /* Highlight until the right side of the window */
4287 c = ' ';
4288 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004289
4290 /* Remember we do the char for line highlighting. */
4291 ++did_line_attr;
4292
4293 /* don't do search HL for the rest of the line */
4294 if (line_attr != 0 && char_attr == search_attr && col > 0)
4295 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296# ifdef FEAT_DIFF
4297 if (diff_hlf == HLF_TXD)
4298 {
4299 diff_hlf = HLF_CHD;
4300 if (attr == 0 || char_attr != attr)
4301 char_attr = hl_attr(diff_hlf);
4302 }
4303# endif
4304 }
4305#endif
4306 }
4307 }
4308
4309 /* Don't override visual selection highlighting. */
4310 if (n_attr > 0
4311 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004312 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 char_attr = extra_attr;
4314
Bram Moolenaar81695252004-12-29 20:58:21 +00004315#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 /* XIM don't send preedit_start and preedit_end, but they send
4317 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
4318 * im_is_preediting() here. */
4319 if (xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004320 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 && (State & INSERT)
4322 && !p_imdisable
4323 && im_is_preediting()
4324 && draw_state == WL_LINE)
4325 {
4326 colnr_T tcol;
4327
4328 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004329 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 else
4331 tcol = preedit_end_col;
4332 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
4333 {
4334 if (feedback_old_attr < 0)
4335 {
4336 feedback_col = 0;
4337 feedback_old_attr = char_attr;
4338 }
4339 char_attr = im_get_feedback_attr(feedback_col);
4340 if (char_attr < 0)
4341 char_attr = feedback_old_attr;
4342 feedback_col++;
4343 }
4344 else if (feedback_old_attr >= 0)
4345 {
4346 char_attr = feedback_old_attr;
4347 feedback_old_attr = -1;
4348 feedback_col = 0;
4349 }
4350 }
4351#endif
4352 /*
4353 * Handle the case where we are in column 0 but not on the first
4354 * character of the line and the user wants us to show us a
4355 * special character (via 'listchars' option "precedes:<char>".
4356 */
4357 if (lcs_prec_todo != NUL
4358 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
4359#ifdef FEAT_DIFF
4360 && filler_todo <= 0
4361#endif
4362 && draw_state > WL_NR
4363 && c != NUL)
4364 {
4365 c = lcs_prec;
4366 lcs_prec_todo = NUL;
4367#ifdef FEAT_MBYTE
4368 mb_c = c;
4369 if (enc_utf8 && (*mb_char2len)(c) > 1)
4370 {
4371 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004372 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004373 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 }
4375 else
4376 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4377#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004378 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 {
4380 saved_attr3 = char_attr; /* save current attr */
4381 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
4382 n_attr3 = 1;
4383 }
4384 }
4385
4386 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00004387 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004389 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004390#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004391 || did_line_attr == 1
4392#endif
4393 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00004395#ifdef FEAT_SEARCH_EXTRA
4396 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00004397
4398 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00004399 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00004400 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004401#endif
4402
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 /* invert at least one char, used for Visual and empty line or
4404 * highlight match at end of line. If it's beyond the last
4405 * char on the screen, just overwrite that one (tricky!) Not
4406 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004407#ifdef FEAT_SEARCH_EXTRA
4408 prevcol_hl_flag = FALSE;
4409 if (prevcol == (long)search_hl.startcol)
4410 prevcol_hl_flag = TRUE;
4411 else
4412 {
4413 cur = wp->w_match_head;
4414 while (cur != NULL)
4415 {
4416 if (prevcol == (long)cur->hl.startcol)
4417 {
4418 prevcol_hl_flag = TRUE;
4419 break;
4420 }
4421 cur = cur->next;
4422 }
4423 }
4424#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004426 && ((area_attr != 0 && vcol == fromcol
4427#ifdef FEAT_VISUAL
4428 && (VIsual_mode != Ctrl_V
4429 || lnum == VIsual.lnum
4430 || lnum == curwin->w_cursor.lnum)
4431#endif
4432 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433#ifdef FEAT_SEARCH_EXTRA
4434 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004435 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004436# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004437 && did_line_attr <= 1
4438# endif
4439 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440#endif
4441 ))
4442 {
4443 int n = 0;
4444
4445#ifdef FEAT_RIGHTLEFT
4446 if (wp->w_p_rl)
4447 {
4448 if (col < 0)
4449 n = 1;
4450 }
4451 else
4452#endif
4453 {
4454 if (col >= W_WIDTH(wp))
4455 n = -1;
4456 }
4457 if (n != 0)
4458 {
4459 /* At the window boundary, highlight the last character
4460 * instead (better than nothing). */
4461 off += n;
4462 col += n;
4463 }
4464 else
4465 {
4466 /* Add a blank character to highlight. */
4467 ScreenLines[off] = ' ';
4468#ifdef FEAT_MBYTE
4469 if (enc_utf8)
4470 ScreenLinesUC[off] = 0;
4471#endif
4472 }
4473#ifdef FEAT_SEARCH_EXTRA
4474 if (area_attr == 0)
4475 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004476 /* Use attributes from match with highest priority among
4477 * 'search_hl' and the match list. */
4478 char_attr = search_hl.attr;
4479 cur = wp->w_match_head;
4480 shl_flag = FALSE;
4481 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004482 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004483 if (shl_flag == FALSE
4484 && ((cur != NULL
4485 && cur->priority > SEARCH_HL_PRIORITY)
4486 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004487 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004488 shl = &search_hl;
4489 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004490 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004491 else
4492 shl = &cur->hl;
4493 if ((ptr - line) - 1 == (long)shl->startcol)
4494 char_attr = shl->attr;
4495 if (shl != &search_hl && cur != NULL)
4496 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 }
4499#endif
4500 ScreenAttrs[off] = char_attr;
4501#ifdef FEAT_RIGHTLEFT
4502 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00004503 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00004505 --off;
4506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 else
4508#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00004509 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00004511 ++off;
4512 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004513 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00004514#ifdef FEAT_SYN_HL
4515 eol_hl_off = 1;
4516#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00004518 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519
Bram Moolenaar91170f82006-05-05 21:15:17 +00004520 /*
4521 * At end of the text line.
4522 */
4523 if (c == NUL)
4524 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004525#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004526 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
4527 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00004528 {
4529 /* highlight last char after line */
4530 --col;
4531 --off;
4532 --vcol;
4533 }
4534
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004535 /* Highlight 'cursorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00004536 if (wp->w_p_wrap)
4537 v = wp->w_skipcol;
4538 else
4539 v = wp->w_leftcol;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004540 /* check if line ends before left margin */
4541 if (vcol < v + col - win_col_off(wp))
4542
4543 vcol = v + col - win_col_off(wp);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004544 if (wp->w_p_cuc
Bram Moolenaara443af82007-11-08 13:51:42 +00004545 && (int)wp->w_virtcol >= vcol - eol_hl_off
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004546 && (int)wp->w_virtcol < W_WIDTH(wp) * (row - startrow + 1)
4547 + v
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004548 && lnum != wp->w_cursor.lnum
4549# ifdef FEAT_RIGHTLEFT
4550 && !wp->w_p_rl
4551# endif
4552 )
4553 {
4554 while (col < W_WIDTH(wp))
4555 {
4556 ScreenLines[off] = ' ';
4557#ifdef FEAT_MBYTE
4558 if (enc_utf8)
4559 ScreenLinesUC[off] = 0;
4560#endif
4561 ++col;
Bram Moolenaarca003e12006-03-17 23:19:38 +00004562 if (vcol == (long)wp->w_virtcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004563 {
4564 ScreenAttrs[off] = hl_attr(HLF_CUC);
4565 break;
4566 }
4567 ScreenAttrs[off++] = 0;
4568 ++vcol;
4569 }
4570 }
4571#endif
4572
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 SCREEN_LINE(screen_row, W_WINCOL(wp), col, (int)W_WIDTH(wp),
4574 wp->w_p_rl);
4575 row++;
4576
4577 /*
4578 * Update w_cline_height and w_cline_folded if the cursor line was
4579 * updated (saves a call to plines() later).
4580 */
4581 if (wp == curwin && lnum == curwin->w_cursor.lnum)
4582 {
4583 curwin->w_cline_row = startrow;
4584 curwin->w_cline_height = row - startrow;
4585#ifdef FEAT_FOLDING
4586 curwin->w_cline_folded = FALSE;
4587#endif
4588 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
4589 }
4590
4591 break;
4592 }
4593
4594 /* line continues beyond line end */
4595 if (lcs_ext
4596 && !wp->w_p_wrap
4597#ifdef FEAT_DIFF
4598 && filler_todo <= 0
4599#endif
4600 && (
4601#ifdef FEAT_RIGHTLEFT
4602 wp->w_p_rl ? col == 0 :
4603#endif
4604 col == W_WIDTH(wp) - 1)
4605 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00004606 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
4608 {
4609 c = lcs_ext;
4610 char_attr = hl_attr(HLF_AT);
4611#ifdef FEAT_MBYTE
4612 mb_c = c;
4613 if (enc_utf8 && (*mb_char2len)(c) > 1)
4614 {
4615 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004616 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004617 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 }
4619 else
4620 mb_utf8 = FALSE;
4621#endif
4622 }
4623
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004624#ifdef FEAT_SYN_HL
4625 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
4626 * highlight the cursor position itself. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00004627 if (wp->w_p_cuc && vcol == (long)wp->w_virtcol
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004628 && lnum != wp->w_cursor.lnum
Bram Moolenaar54ef7112009-02-21 20:11:41 +00004629 && draw_state == WL_LINE
4630 && !lnum_in_visual_area)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004631 {
4632 vcol_save_attr = char_attr;
4633 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
4634 }
4635 else
4636 vcol_save_attr = -1;
4637#endif
4638
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 /*
4640 * Store character to be displayed.
4641 * Skip characters that are left of the screen for 'nowrap'.
4642 */
4643 vcol_prev = vcol;
4644 if (draw_state < WL_LINE || n_skip <= 0)
4645 {
4646 /*
4647 * Store the character.
4648 */
4649#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
4650 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
4651 {
4652 /* A double-wide character is: put first halve in left cell. */
4653 --off;
4654 --col;
4655 }
4656#endif
4657 ScreenLines[off] = c;
4658#ifdef FEAT_MBYTE
4659 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01004660 {
4661 if ((mb_c & 0xff00) == 0x8e00)
4662 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01004664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 else if (enc_utf8)
4666 {
4667 if (mb_utf8)
4668 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004669 int i;
4670
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004672 if ((c & 0xff) == 0)
4673 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004674 for (i = 0; i < Screen_mco; ++i)
4675 {
4676 ScreenLinesC[i][off] = u8cc[i];
4677 if (u8cc[i] == 0)
4678 break;
4679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 }
4681 else
4682 ScreenLinesUC[off] = 0;
4683 }
4684 if (multi_attr)
4685 {
4686 ScreenAttrs[off] = multi_attr;
4687 multi_attr = 0;
4688 }
4689 else
4690#endif
4691 ScreenAttrs[off] = char_attr;
4692
4693#ifdef FEAT_MBYTE
4694 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
4695 {
4696 /* Need to fill two screen columns. */
4697 ++off;
4698 ++col;
4699 if (enc_utf8)
4700 /* UTF-8: Put a 0 in the second screen char. */
4701 ScreenLines[off] = 0;
4702 else
4703 /* DBCS: Put second byte in the second screen char. */
4704 ScreenLines[off] = mb_c & 0xff;
4705 ++vcol;
4706 /* When "tocol" is halfway a character, set it to the end of
4707 * the character, otherwise highlighting won't stop. */
4708 if (tocol == vcol)
4709 ++tocol;
4710#ifdef FEAT_RIGHTLEFT
4711 if (wp->w_p_rl)
4712 {
4713 /* now it's time to backup one cell */
4714 --off;
4715 --col;
4716 }
4717#endif
4718 }
4719#endif
4720#ifdef FEAT_RIGHTLEFT
4721 if (wp->w_p_rl)
4722 {
4723 --off;
4724 --col;
4725 }
4726 else
4727#endif
4728 {
4729 ++off;
4730 ++col;
4731 }
4732 }
4733 else
4734 --n_skip;
4735
Bram Moolenaar64486672010-05-16 15:46:46 +02004736 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
4737 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00004738 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739#ifdef FEAT_DIFF
4740 && filler_todo <= 0
4741#endif
4742 )
4743 ++vcol;
4744
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004745#ifdef FEAT_SYN_HL
4746 if (vcol_save_attr >= 0)
4747 char_attr = vcol_save_attr;
4748#endif
4749
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 /* restore attributes after "predeces" in 'listchars' */
4751 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
4752 char_attr = saved_attr3;
4753
4754 /* restore attributes after last 'listchars' or 'number' char */
4755 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
4756 char_attr = saved_attr2;
4757
4758 /*
4759 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00004760 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 */
4762 if ((
4763#ifdef FEAT_RIGHTLEFT
4764 wp->w_p_rl ? (col < 0) :
4765#endif
4766 (col >= W_WIDTH(wp)))
4767 && (*ptr != NUL
4768#ifdef FEAT_DIFF
4769 || filler_todo > 0
4770#endif
4771 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
4772 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
4773 )
4774 {
4775 SCREEN_LINE(screen_row, W_WINCOL(wp), col, (int)W_WIDTH(wp),
4776 wp->w_p_rl);
4777 ++row;
4778 ++screen_row;
4779
4780 /* When not wrapping and finished diff lines, or when displayed
4781 * '$' and highlighting until last column, break here. */
4782 if ((!wp->w_p_wrap
4783#ifdef FEAT_DIFF
4784 && filler_todo <= 0
4785#endif
4786 ) || lcs_eol_one == -1)
4787 break;
4788
4789 /* When the window is too narrow draw all "@" lines. */
4790 if (draw_state != WL_LINE
4791#ifdef FEAT_DIFF
4792 && filler_todo <= 0
4793#endif
4794 )
4795 {
4796 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
4797#ifdef FEAT_VERTSPLIT
4798 draw_vsep_win(wp, row);
4799#endif
4800 row = endrow;
4801 }
4802
4803 /* When line got too long for screen break here. */
4804 if (row == endrow)
4805 {
4806 ++row;
4807 break;
4808 }
4809
4810 if (screen_cur_row == screen_row - 1
4811#ifdef FEAT_DIFF
4812 && filler_todo <= 0
4813#endif
4814 && W_WIDTH(wp) == Columns)
4815 {
4816 /* Remember that the line wraps, used for modeless copy. */
4817 LineWraps[screen_row - 1] = TRUE;
4818
4819 /*
4820 * Special trick to make copy/paste of wrapped lines work with
4821 * xterm/screen: write an extra character beyond the end of
4822 * the line. This will work with all terminal types
4823 * (regardless of the xn,am settings).
4824 * Only do this on a fast tty.
4825 * Only do this if the cursor is on the current line
4826 * (something has been written in it).
4827 * Don't do this for the GUI.
4828 * Don't do this for double-width characters.
4829 * Don't do this for a window not at the right screen border.
4830 */
4831 if (p_tf
4832#ifdef FEAT_GUI
4833 && !gui.in_use
4834#endif
4835#ifdef FEAT_MBYTE
4836 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00004837 && ((*mb_off2cells)(LineOffset[screen_row],
4838 LineOffset[screen_row] + screen_Columns)
4839 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00004841 + (int)Columns - 2,
4842 LineOffset[screen_row] + screen_Columns)
4843 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844#endif
4845 )
4846 {
4847 /* First make sure we are at the end of the screen line,
4848 * then output the same character again to let the
4849 * terminal know about the wrap. If the terminal doesn't
4850 * auto-wrap, we overwrite the character. */
4851 if (screen_cur_col != W_WIDTH(wp))
4852 screen_char(LineOffset[screen_row - 1]
4853 + (unsigned)Columns - 1,
4854 screen_row - 1, (int)(Columns - 1));
4855
4856#ifdef FEAT_MBYTE
4857 /* When there is a multi-byte character, just output a
4858 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00004859 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
4860 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 out_char(' ');
4862 else
4863#endif
4864 out_char(ScreenLines[LineOffset[screen_row - 1]
4865 + (Columns - 1)]);
4866 /* force a redraw of the first char on the next line */
4867 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
4868 screen_start(); /* don't know where cursor is now */
4869 }
4870 }
4871
4872 col = 0;
4873 off = (unsigned)(current_ScreenLine - ScreenLines);
4874#ifdef FEAT_RIGHTLEFT
4875 if (wp->w_p_rl)
4876 {
4877 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
4878 off += col;
4879 }
4880#endif
4881
4882 /* reset the drawing state for the start of a wrapped line */
4883 draw_state = WL_START;
4884 saved_n_extra = n_extra;
4885 saved_p_extra = p_extra;
4886 saved_c_extra = c_extra;
4887 saved_char_attr = char_attr;
4888 n_extra = 0;
4889 lcs_prec_todo = lcs_prec;
4890#ifdef FEAT_LINEBREAK
4891# ifdef FEAT_DIFF
4892 if (filler_todo <= 0)
4893# endif
4894 need_showbreak = TRUE;
4895#endif
4896#ifdef FEAT_DIFF
4897 --filler_todo;
4898 /* When the filler lines are actually below the last line of the
4899 * file, don't draw the line itself, break here. */
4900 if (filler_todo == 0 && wp->w_botfill)
4901 break;
4902#endif
4903 }
4904
4905 } /* for every character in the line */
4906
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004907#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004908 /* After an empty line check first word for capital. */
4909 if (*skipwhite(line) == NUL)
4910 {
4911 capcol_lnum = lnum + 1;
4912 cap_col = 0;
4913 }
4914#endif
4915
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 return row;
4917}
4918
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004919#ifdef FEAT_MBYTE
4920static int comp_char_differs __ARGS((int, int));
4921
4922/*
4923 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01004924 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004925 */
4926 static int
4927comp_char_differs(off_from, off_to)
4928 int off_from;
4929 int off_to;
4930{
4931 int i;
4932
4933 for (i = 0; i < Screen_mco; ++i)
4934 {
4935 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
4936 return TRUE;
4937 if (ScreenLinesC[i][off_from] == 0)
4938 break;
4939 }
4940 return FALSE;
4941}
4942#endif
4943
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944/*
4945 * Check whether the given character needs redrawing:
4946 * - the (first byte of the) character is different
4947 * - the attributes are different
4948 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00004949 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 */
4951 static int
4952char_needs_redraw(off_from, off_to, cols)
4953 int off_from;
4954 int off_to;
4955 int cols;
4956{
4957 if (cols > 0
4958 && ((ScreenLines[off_from] != ScreenLines[off_to]
4959 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
4960
4961#ifdef FEAT_MBYTE
4962 || (enc_dbcs != 0
4963 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
4964 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
4965 ? ScreenLines2[off_from] != ScreenLines2[off_to]
4966 : (cols > 1 && ScreenLines[off_from + 1]
4967 != ScreenLines[off_to + 1])))
4968 || (enc_utf8
4969 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
4970 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00004971 && comp_char_differs(off_from, off_to))
4972 || (cols > 1 && ScreenLines[off_from + 1]
4973 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974#endif
4975 ))
4976 return TRUE;
4977 return FALSE;
4978}
4979
4980/*
4981 * Move one "cooked" screen line to the screen, but only the characters that
4982 * have actually changed. Handle insert/delete character.
4983 * "coloff" gives the first column on the screen for this line.
4984 * "endcol" gives the columns where valid characters are.
4985 * "clear_width" is the width of the window. It's > 0 if the rest of the line
4986 * needs to be cleared, negative otherwise.
4987 * "rlflag" is TRUE in a rightleft window:
4988 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
4989 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
4990 */
4991 static void
4992screen_line(row, coloff, endcol, clear_width
4993#ifdef FEAT_RIGHTLEFT
4994 , rlflag
4995#endif
4996 )
4997 int row;
4998 int coloff;
4999 int endcol;
5000 int clear_width;
5001#ifdef FEAT_RIGHTLEFT
5002 int rlflag;
5003#endif
5004{
5005 unsigned off_from;
5006 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005007#ifdef FEAT_MBYTE
5008 unsigned max_off_from;
5009 unsigned max_off_to;
5010#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 int col = 0;
5012#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_VERTSPLIT)
5013 int hl;
5014#endif
5015 int force = FALSE; /* force update rest of the line */
5016 int redraw_this /* bool: does character need redraw? */
5017#ifdef FEAT_GUI
5018 = TRUE /* For GUI when while-loop empty */
5019#endif
5020 ;
5021 int redraw_next; /* redraw_this for next character */
5022#ifdef FEAT_MBYTE
5023 int clear_next = FALSE;
5024 int char_cells; /* 1: normal char */
5025 /* 2: occupies two display cells */
5026# define CHAR_CELLS char_cells
5027#else
5028# define CHAR_CELLS 1
5029#endif
5030
5031# ifdef FEAT_CLIPBOARD
5032 clip_may_clear_selection(row, row);
5033# endif
5034
5035 off_from = (unsigned)(current_ScreenLine - ScreenLines);
5036 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005037#ifdef FEAT_MBYTE
5038 max_off_from = off_from + screen_Columns;
5039 max_off_to = LineOffset[row] + screen_Columns;
5040#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041
5042#ifdef FEAT_RIGHTLEFT
5043 if (rlflag)
5044 {
5045 /* Clear rest first, because it's left of the text. */
5046 if (clear_width > 0)
5047 {
5048 while (col <= endcol && ScreenLines[off_to] == ' '
5049 && ScreenAttrs[off_to] == 0
5050# ifdef FEAT_MBYTE
5051 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5052# endif
5053 )
5054 {
5055 ++off_to;
5056 ++col;
5057 }
5058 if (col <= endcol)
5059 screen_fill(row, row + 1, col + coloff,
5060 endcol + coloff + 1, ' ', ' ', 0);
5061 }
5062 col = endcol + 1;
5063 off_to = LineOffset[row] + col + coloff;
5064 off_from += col;
5065 endcol = (clear_width > 0 ? clear_width : -clear_width);
5066 }
5067#endif /* FEAT_RIGHTLEFT */
5068
5069 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
5070
5071 while (col < endcol)
5072 {
5073#ifdef FEAT_MBYTE
5074 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00005075 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 else
5077 char_cells = 1;
5078#endif
5079
5080 redraw_this = redraw_next;
5081 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
5082 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
5083
5084#ifdef FEAT_GUI
5085 /* If the next character was bold, then redraw the current character to
5086 * remove any pixels that might have spilt over into us. This only
5087 * happens in the GUI.
5088 */
5089 if (redraw_next && gui.in_use)
5090 {
5091 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005092 if (hl > HL_ALL)
5093 hl = syn_attr2attr(hl);
5094 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 redraw_this = TRUE;
5096 }
5097#endif
5098
5099 if (redraw_this)
5100 {
5101 /*
5102 * Special handling when 'xs' termcap flag set (hpterm):
5103 * Attributes for characters are stored at the position where the
5104 * cursor is when writing the highlighting code. The
5105 * start-highlighting code must be written with the cursor on the
5106 * first highlighted character. The stop-highlighting code must
5107 * be written with the cursor just after the last highlighted
5108 * character.
5109 * Overwriting a character doesn't remove it's highlighting. Need
5110 * to clear the rest of the line, and force redrawing it
5111 * completely.
5112 */
5113 if ( p_wiv
5114 && !force
5115#ifdef FEAT_GUI
5116 && !gui.in_use
5117#endif
5118 && ScreenAttrs[off_to] != 0
5119 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
5120 {
5121 /*
5122 * Need to remove highlighting attributes here.
5123 */
5124 windgoto(row, col + coloff);
5125 out_str(T_CE); /* clear rest of this screen line */
5126 screen_start(); /* don't know where cursor is now */
5127 force = TRUE; /* force redraw of rest of the line */
5128 redraw_next = TRUE; /* or else next char would miss out */
5129
5130 /*
5131 * If the previous character was highlighted, need to stop
5132 * highlighting at this character.
5133 */
5134 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
5135 {
5136 screen_attr = ScreenAttrs[off_to - 1];
5137 term_windgoto(row, col + coloff);
5138 screen_stop_highlight();
5139 }
5140 else
5141 screen_attr = 0; /* highlighting has stopped */
5142 }
5143#ifdef FEAT_MBYTE
5144 if (enc_dbcs != 0)
5145 {
5146 /* Check if overwriting a double-byte with a single-byte or
5147 * the other way around requires another character to be
5148 * redrawn. For UTF-8 this isn't needed, because comparing
5149 * ScreenLinesUC[] is sufficient. */
5150 if (char_cells == 1
5151 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005152 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 {
5154 /* Writing a single-cell character over a double-cell
5155 * character: need to redraw the next cell. */
5156 ScreenLines[off_to + 1] = 0;
5157 redraw_next = TRUE;
5158 }
5159 else if (char_cells == 2
5160 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005161 && (*mb_off2cells)(off_to, max_off_to) == 1
5162 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 {
5164 /* Writing the second half of a double-cell character over
5165 * a double-cell character: need to redraw the second
5166 * cell. */
5167 ScreenLines[off_to + 2] = 0;
5168 redraw_next = TRUE;
5169 }
5170
5171 if (enc_dbcs == DBCS_JPNU)
5172 ScreenLines2[off_to] = ScreenLines2[off_from];
5173 }
5174 /* When writing a single-width character over a double-width
5175 * character and at the end of the redrawn text, need to clear out
5176 * the right halve of the old character.
5177 * Also required when writing the right halve of a double-width
5178 * char over the left halve of an existing one. */
5179 if (has_mbyte && col + char_cells == endcol
5180 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00005181 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00005183 && (*mb_off2cells)(off_to, max_off_to) == 1
5184 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 clear_next = TRUE;
5186#endif
5187
5188 ScreenLines[off_to] = ScreenLines[off_from];
5189#ifdef FEAT_MBYTE
5190 if (enc_utf8)
5191 {
5192 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
5193 if (ScreenLinesUC[off_from] != 0)
5194 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005195 int i;
5196
5197 for (i = 0; i < Screen_mco; ++i)
5198 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 }
5200 }
5201 if (char_cells == 2)
5202 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
5203#endif
5204
5205#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00005206 /* The bold trick makes a single column of pixels appear in the
5207 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 * character should be redrawn too. This happens for our own GUI
5209 * and for some xterms. */
5210 if (
5211# ifdef FEAT_GUI
5212 gui.in_use
5213# endif
5214# if defined(FEAT_GUI) && defined(UNIX)
5215 ||
5216# endif
5217# ifdef UNIX
5218 term_is_xterm
5219# endif
5220 )
5221 {
5222 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005223 if (hl > HL_ALL)
5224 hl = syn_attr2attr(hl);
5225 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 redraw_next = TRUE;
5227 }
5228#endif
5229 ScreenAttrs[off_to] = ScreenAttrs[off_from];
5230#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005231 /* For simplicity set the attributes of second half of a
5232 * double-wide character equal to the first half. */
5233 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005235
5236 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 else
5239#endif
5240 screen_char(off_to, row, col + coloff);
5241 }
5242 else if ( p_wiv
5243#ifdef FEAT_GUI
5244 && !gui.in_use
5245#endif
5246 && col + coloff > 0)
5247 {
5248 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
5249 {
5250 /*
5251 * Don't output stop-highlight when moving the cursor, it will
5252 * stop the highlighting when it should continue.
5253 */
5254 screen_attr = 0;
5255 }
5256 else if (screen_attr != 0)
5257 screen_stop_highlight();
5258 }
5259
5260 off_to += CHAR_CELLS;
5261 off_from += CHAR_CELLS;
5262 col += CHAR_CELLS;
5263 }
5264
5265#ifdef FEAT_MBYTE
5266 if (clear_next)
5267 {
5268 /* Clear the second half of a double-wide character of which the left
5269 * half was overwritten with a single-wide character. */
5270 ScreenLines[off_to] = ' ';
5271 if (enc_utf8)
5272 ScreenLinesUC[off_to] = 0;
5273 screen_char(off_to, row, col + coloff);
5274 }
5275#endif
5276
5277 if (clear_width > 0
5278#ifdef FEAT_RIGHTLEFT
5279 && !rlflag
5280#endif
5281 )
5282 {
5283#ifdef FEAT_GUI
5284 int startCol = col;
5285#endif
5286
5287 /* blank out the rest of the line */
5288 while (col < clear_width && ScreenLines[off_to] == ' '
5289 && ScreenAttrs[off_to] == 0
5290#ifdef FEAT_MBYTE
5291 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5292#endif
5293 )
5294 {
5295 ++off_to;
5296 ++col;
5297 }
5298 if (col < clear_width)
5299 {
5300#ifdef FEAT_GUI
5301 /*
5302 * In the GUI, clearing the rest of the line may leave pixels
5303 * behind if the first character cleared was bold. Some bold
5304 * fonts spill over the left. In this case we redraw the previous
5305 * character too. If we didn't skip any blanks above, then we
5306 * only redraw if the character wasn't already redrawn anyway.
5307 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00005308 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 {
5310 hl = ScreenAttrs[off_to];
5311 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00005312 {
5313 int prev_cells = 1;
5314# ifdef FEAT_MBYTE
5315 if (enc_utf8)
5316 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
5317 * that its width is 2. */
5318 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
5319 else if (enc_dbcs != 0)
5320 {
5321 /* find previous character by counting from first
5322 * column and get its width. */
5323 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00005324 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00005325
5326 while (off < off_to)
5327 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00005328 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00005329 off += prev_cells;
5330 }
5331 }
5332
5333 if (enc_dbcs != 0 && prev_cells > 1)
5334 screen_char_2(off_to - prev_cells, row,
5335 col + coloff - prev_cells);
5336 else
5337# endif
5338 screen_char(off_to - prev_cells, row,
5339 col + coloff - prev_cells);
5340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 }
5342#endif
5343 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
5344 ' ', ' ', 0);
5345#ifdef FEAT_VERTSPLIT
5346 off_to += clear_width - col;
5347 col = clear_width;
5348#endif
5349 }
5350 }
5351
5352 if (clear_width > 0)
5353 {
5354#ifdef FEAT_VERTSPLIT
5355 /* For a window that's left of another, draw the separator char. */
5356 if (col + coloff < Columns)
5357 {
5358 int c;
5359
5360 c = fillchar_vsep(&hl);
5361 if (ScreenLines[off_to] != c
5362# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005363 || (enc_utf8 && (int)ScreenLinesUC[off_to]
5364 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365# endif
5366 || ScreenAttrs[off_to] != hl)
5367 {
5368 ScreenLines[off_to] = c;
5369 ScreenAttrs[off_to] = hl;
5370# ifdef FEAT_MBYTE
5371 if (enc_utf8)
5372 {
5373 if (c >= 0x80)
5374 {
5375 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005376 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 }
5378 else
5379 ScreenLinesUC[off_to] = 0;
5380 }
5381# endif
5382 screen_char(off_to, row, col + coloff);
5383 }
5384 }
5385 else
5386#endif
5387 LineWraps[row] = FALSE;
5388 }
5389}
5390
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005391#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005393 * Mirror text "str" for right-left displaying.
5394 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005396 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397rl_mirror(str)
5398 char_u *str;
5399{
5400 char_u *p1, *p2;
5401 int t;
5402
5403 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
5404 {
5405 t = *p1;
5406 *p1 = *p2;
5407 *p2 = t;
5408 }
5409}
5410#endif
5411
5412#if defined(FEAT_WINDOWS) || defined(PROTO)
5413/*
5414 * mark all status lines for redraw; used after first :cd
5415 */
5416 void
5417status_redraw_all()
5418{
5419 win_T *wp;
5420
5421 for (wp = firstwin; wp; wp = wp->w_next)
5422 if (wp->w_status_height)
5423 {
5424 wp->w_redr_status = TRUE;
5425 redraw_later(VALID);
5426 }
5427}
5428
5429/*
5430 * mark all status lines of the current buffer for redraw
5431 */
5432 void
5433status_redraw_curbuf()
5434{
5435 win_T *wp;
5436
5437 for (wp = firstwin; wp; wp = wp->w_next)
5438 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
5439 {
5440 wp->w_redr_status = TRUE;
5441 redraw_later(VALID);
5442 }
5443}
5444
5445/*
5446 * Redraw all status lines that need to be redrawn.
5447 */
5448 void
5449redraw_statuslines()
5450{
5451 win_T *wp;
5452
5453 for (wp = firstwin; wp; wp = wp->w_next)
5454 if (wp->w_redr_status)
5455 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00005456 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005457 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458}
5459#endif
5460
5461#if (defined(FEAT_WILDMENU) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
5462/*
5463 * Redraw all status lines at the bottom of frame "frp".
5464 */
5465 void
5466win_redraw_last_status(frp)
5467 frame_T *frp;
5468{
5469 if (frp->fr_layout == FR_LEAF)
5470 frp->fr_win->w_redr_status = TRUE;
5471 else if (frp->fr_layout == FR_ROW)
5472 {
5473 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
5474 win_redraw_last_status(frp);
5475 }
5476 else /* frp->fr_layout == FR_COL */
5477 {
5478 frp = frp->fr_child;
5479 while (frp->fr_next != NULL)
5480 frp = frp->fr_next;
5481 win_redraw_last_status(frp);
5482 }
5483}
5484#endif
5485
5486#ifdef FEAT_VERTSPLIT
5487/*
5488 * Draw the verticap separator right of window "wp" starting with line "row".
5489 */
5490 static void
5491draw_vsep_win(wp, row)
5492 win_T *wp;
5493 int row;
5494{
5495 int hl;
5496 int c;
5497
5498 if (wp->w_vsep_width)
5499 {
5500 /* draw the vertical separator right of this window */
5501 c = fillchar_vsep(&hl);
5502 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
5503 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
5504 c, ' ', hl);
5505 }
5506}
5507#endif
5508
5509#ifdef FEAT_WILDMENU
5510static int status_match_len __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005511static int skip_status_match_char __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512
5513/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00005514 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 */
5516 static int
5517status_match_len(xp, s)
5518 expand_T *xp;
5519 char_u *s;
5520{
5521 int len = 0;
5522
5523#ifdef FEAT_MENU
5524 int emenu = (xp->xp_context == EXPAND_MENUS
5525 || xp->xp_context == EXPAND_MENUNAMES);
5526
5527 /* Check for menu separators - replace with '|'. */
5528 if (emenu && menu_is_separator(s))
5529 return 1;
5530#endif
5531
5532 while (*s != NUL)
5533 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005534 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00005535 len += ptr2cells(s);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005536 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 }
5538
5539 return len;
5540}
5541
5542/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005543 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005544 * These are backslashes used for escaping. Do show backslashes in help tags.
5545 */
5546 static int
5547skip_status_match_char(xp, s)
5548 expand_T *xp;
5549 char_u *s;
5550{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005551 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005552#ifdef FEAT_MENU
5553 || ((xp->xp_context == EXPAND_MENUS
5554 || xp->xp_context == EXPAND_MENUNAMES)
5555 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
5556#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005557 )
5558 {
5559#ifndef BACKSLASH_IN_FILENAME
5560 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
5561 return 2;
5562#endif
5563 return 1;
5564 }
5565 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005566}
5567
5568/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 * Show wildchar matches in the status line.
5570 * Show at least the "match" item.
5571 * We start at item 'first_match' in the list and show all matches that fit.
5572 *
5573 * If inversion is possible we use it. Else '=' characters are used.
5574 */
5575 void
5576win_redr_status_matches(xp, num_matches, matches, match, showtail)
5577 expand_T *xp;
5578 int num_matches;
5579 char_u **matches; /* list of matches */
5580 int match;
5581 int showtail;
5582{
5583#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
5584 int row;
5585 char_u *buf;
5586 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005587 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 int fillchar;
5589 int attr;
5590 int i;
5591 int highlight = TRUE;
5592 char_u *selstart = NULL;
5593 int selstart_col = 0;
5594 char_u *selend = NULL;
5595 static int first_match = 0;
5596 int add_left = FALSE;
5597 char_u *s;
5598#ifdef FEAT_MENU
5599 int emenu;
5600#endif
5601#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
5602 int l;
5603#endif
5604
5605 if (matches == NULL) /* interrupted completion? */
5606 return;
5607
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005608#ifdef FEAT_MBYTE
5609 if (has_mbyte)
5610 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
5611 else
5612#endif
5613 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 if (buf == NULL)
5615 return;
5616
5617 if (match == -1) /* don't show match but original text */
5618 {
5619 match = 0;
5620 highlight = FALSE;
5621 }
5622 /* count 1 for the ending ">" */
5623 clen = status_match_len(xp, L_MATCH(match)) + 3;
5624 if (match == 0)
5625 first_match = 0;
5626 else if (match < first_match)
5627 {
5628 /* jumping left, as far as we can go */
5629 first_match = match;
5630 add_left = TRUE;
5631 }
5632 else
5633 {
5634 /* check if match fits on the screen */
5635 for (i = first_match; i < match; ++i)
5636 clen += status_match_len(xp, L_MATCH(i)) + 2;
5637 if (first_match > 0)
5638 clen += 2;
5639 /* jumping right, put match at the left */
5640 if ((long)clen > Columns)
5641 {
5642 first_match = match;
5643 /* if showing the last match, we can add some on the left */
5644 clen = 2;
5645 for (i = match; i < num_matches; ++i)
5646 {
5647 clen += status_match_len(xp, L_MATCH(i)) + 2;
5648 if ((long)clen >= Columns)
5649 break;
5650 }
5651 if (i == num_matches)
5652 add_left = TRUE;
5653 }
5654 }
5655 if (add_left)
5656 while (first_match > 0)
5657 {
5658 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
5659 if ((long)clen >= Columns)
5660 break;
5661 --first_match;
5662 }
5663
5664 fillchar = fillchar_status(&attr, TRUE);
5665
5666 if (first_match == 0)
5667 {
5668 *buf = NUL;
5669 len = 0;
5670 }
5671 else
5672 {
5673 STRCPY(buf, "< ");
5674 len = 2;
5675 }
5676 clen = len;
5677
5678 i = first_match;
5679 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
5680 {
5681 if (i == match)
5682 {
5683 selstart = buf + len;
5684 selstart_col = clen;
5685 }
5686
5687 s = L_MATCH(i);
5688 /* Check for menu separators - replace with '|' */
5689#ifdef FEAT_MENU
5690 emenu = (xp->xp_context == EXPAND_MENUS
5691 || xp->xp_context == EXPAND_MENUNAMES);
5692 if (emenu && menu_is_separator(s))
5693 {
5694 STRCPY(buf + len, transchar('|'));
5695 l = (int)STRLEN(buf + len);
5696 len += l;
5697 clen += l;
5698 }
5699 else
5700#endif
5701 for ( ; *s != NUL; ++s)
5702 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005703 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 clen += ptr2cells(s);
5705#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005706 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 {
5708 STRNCPY(buf + len, s, l);
5709 s += l - 1;
5710 len += l;
5711 }
5712 else
5713#endif
5714 {
5715 STRCPY(buf + len, transchar_byte(*s));
5716 len += (int)STRLEN(buf + len);
5717 }
5718 }
5719 if (i == match)
5720 selend = buf + len;
5721
5722 *(buf + len++) = ' ';
5723 *(buf + len++) = ' ';
5724 clen += 2;
5725 if (++i == num_matches)
5726 break;
5727 }
5728
5729 if (i != num_matches)
5730 {
5731 *(buf + len++) = '>';
5732 ++clen;
5733 }
5734
5735 buf[len] = NUL;
5736
5737 row = cmdline_row - 1;
5738 if (row >= 0)
5739 {
5740 if (wild_menu_showing == 0)
5741 {
5742 if (msg_scrolled > 0)
5743 {
5744 /* Put the wildmenu just above the command line. If there is
5745 * no room, scroll the screen one line up. */
5746 if (cmdline_row == Rows - 1)
5747 {
5748 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
5749 ++msg_scrolled;
5750 }
5751 else
5752 {
5753 ++cmdline_row;
5754 ++row;
5755 }
5756 wild_menu_showing = WM_SCROLLED;
5757 }
5758 else
5759 {
5760 /* Create status line if needed by setting 'laststatus' to 2.
5761 * Set 'winminheight' to zero to avoid that the window is
5762 * resized. */
5763 if (lastwin->w_status_height == 0)
5764 {
5765 save_p_ls = p_ls;
5766 save_p_wmh = p_wmh;
5767 p_ls = 2;
5768 p_wmh = 0;
5769 last_status(FALSE);
5770 }
5771 wild_menu_showing = WM_SHOWN;
5772 }
5773 }
5774
5775 screen_puts(buf, row, 0, attr);
5776 if (selstart != NULL && highlight)
5777 {
5778 *selend = NUL;
5779 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
5780 }
5781
5782 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
5783 }
5784
5785#ifdef FEAT_VERTSPLIT
5786 win_redraw_last_status(topframe);
5787#else
5788 lastwin->w_redr_status = TRUE;
5789#endif
5790 vim_free(buf);
5791}
5792#endif
5793
5794#if defined(FEAT_WINDOWS) || defined(PROTO)
5795/*
5796 * Redraw the status line of window wp.
5797 *
5798 * If inversion is possible we use it. Else '=' characters are used.
5799 */
5800 void
5801win_redr_status(wp)
5802 win_T *wp;
5803{
5804 int row;
5805 char_u *p;
5806 int len;
5807 int fillchar;
5808 int attr;
5809 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00005810 static int busy = FALSE;
5811
5812 /* It's possible to get here recursively when 'statusline' (indirectly)
5813 * invokes ":redrawstatus". Simply ignore the call then. */
5814 if (busy)
5815 return;
5816 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817
5818 wp->w_redr_status = FALSE;
5819 if (wp->w_status_height == 0)
5820 {
5821 /* no status line, can only be last window */
5822 redraw_cmdline = TRUE;
5823 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005824 else if (!redrawing()
5825#ifdef FEAT_INS_EXPAND
5826 /* don't update status line when popup menu is visible and may be
5827 * drawn over it */
5828 || pum_visible()
5829#endif
5830 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 {
5832 /* Don't redraw right now, do it later. */
5833 wp->w_redr_status = TRUE;
5834 }
5835#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005836 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 {
5838 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00005839 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 }
5841#endif
5842 else
5843 {
5844 fillchar = fillchar_status(&attr, wp == curwin);
5845
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005846 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847 p = NameBuff;
5848 len = (int)STRLEN(p);
5849
5850 if (wp->w_buffer->b_help
5851#ifdef FEAT_QUICKFIX
5852 || wp->w_p_pvw
5853#endif
5854 || bufIsChanged(wp->w_buffer)
5855 || wp->w_buffer->b_p_ro)
5856 *(p + len++) = ' ';
5857 if (wp->w_buffer->b_help)
5858 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005859 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 len += (int)STRLEN(p + len);
5861 }
5862#ifdef FEAT_QUICKFIX
5863 if (wp->w_p_pvw)
5864 {
5865 STRCPY(p + len, _("[Preview]"));
5866 len += (int)STRLEN(p + len);
5867 }
5868#endif
5869 if (bufIsChanged(wp->w_buffer))
5870 {
5871 STRCPY(p + len, "[+]");
5872 len += 3;
5873 }
5874 if (wp->w_buffer->b_p_ro)
5875 {
5876 STRCPY(p + len, "[RO]");
5877 len += 4;
5878 }
5879
5880#ifndef FEAT_VERTSPLIT
5881 this_ru_col = ru_col;
5882 if (this_ru_col < (Columns + 1) / 2)
5883 this_ru_col = (Columns + 1) / 2;
5884#else
5885 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
5886 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
5887 this_ru_col = (W_WIDTH(wp) + 1) / 2;
5888 if (this_ru_col <= 1)
5889 {
5890 p = (char_u *)"<"; /* No room for file name! */
5891 len = 1;
5892 }
5893 else
5894#endif
5895#ifdef FEAT_MBYTE
5896 if (has_mbyte)
5897 {
5898 int clen = 0, i;
5899
5900 /* Count total number of display cells. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005901 for (i = 0; p[i] != NUL; i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 clen += (*mb_ptr2cells)(p + i);
5903 /* Find first character that will fit.
5904 * Going from start to end is much faster for DBCS. */
5905 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005906 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907 clen -= (*mb_ptr2cells)(p + i);
5908 len = clen;
5909 if (i > 0)
5910 {
5911 p = p + i - 1;
5912 *p = '<';
5913 ++len;
5914 }
5915
5916 }
5917 else
5918#endif
5919 if (len > this_ru_col - 1)
5920 {
5921 p += len - (this_ru_col - 1);
5922 *p = '<';
5923 len = this_ru_col - 1;
5924 }
5925
5926 row = W_WINROW(wp) + wp->w_height;
5927 screen_puts(p, row, W_WINCOL(wp), attr);
5928 screen_fill(row, row + 1, len + W_WINCOL(wp),
5929 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
5930
5931 if (get_keymap_str(wp, NameBuff, MAXPATHL)
5932 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
5933 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
5934 - 1 + W_WINCOL(wp)), attr);
5935
5936#ifdef FEAT_CMDL_INFO
5937 win_redr_ruler(wp, TRUE);
5938#endif
5939 }
5940
5941#ifdef FEAT_VERTSPLIT
5942 /*
5943 * May need to draw the character below the vertical separator.
5944 */
5945 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
5946 {
5947 if (stl_connected(wp))
5948 fillchar = fillchar_status(&attr, wp == curwin);
5949 else
5950 fillchar = fillchar_vsep(&attr);
5951 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
5952 attr);
5953 }
5954#endif
Bram Moolenaaradb09c22009-06-16 15:22:12 +00005955 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956}
5957
Bram Moolenaar238a5642006-02-21 22:12:05 +00005958#ifdef FEAT_STL_OPT
5959/*
5960 * Redraw the status line according to 'statusline' and take care of any
5961 * errors encountered.
5962 */
5963 static void
Bram Moolenaar362f3562009-11-03 16:20:34 +00005964redraw_custom_statusline(wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00005965 win_T *wp;
5966{
Bram Moolenaar362f3562009-11-03 16:20:34 +00005967 static int entered = FALSE;
5968 int save_called_emsg = called_emsg;
5969
5970 /* When called recursively return. This can happen when the statusline
5971 * contains an expression that triggers a redraw. */
5972 if (entered)
5973 return;
5974 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00005975
5976 called_emsg = FALSE;
5977 win_redr_custom(wp, FALSE);
5978 if (called_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00005979 {
5980 /* When there is an error disable the statusline, otherwise the
5981 * display is messed up with errors and a redraw triggers the problem
5982 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00005983 set_string_option_direct((char_u *)"statusline", -1,
5984 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005985 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00005986 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00005987 called_emsg |= save_called_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00005988 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00005989}
5990#endif
5991
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992# ifdef FEAT_VERTSPLIT
5993/*
5994 * Return TRUE if the status line of window "wp" is connected to the status
5995 * line of the window right of it. If not, then it's a vertical separator.
5996 * Only call if (wp->w_vsep_width != 0).
5997 */
5998 int
5999stl_connected(wp)
6000 win_T *wp;
6001{
6002 frame_T *fr;
6003
6004 fr = wp->w_frame;
6005 while (fr->fr_parent != NULL)
6006 {
6007 if (fr->fr_parent->fr_layout == FR_COL)
6008 {
6009 if (fr->fr_next != NULL)
6010 break;
6011 }
6012 else
6013 {
6014 if (fr->fr_next != NULL)
6015 return TRUE;
6016 }
6017 fr = fr->fr_parent;
6018 }
6019 return FALSE;
6020}
6021# endif
6022
6023#endif /* FEAT_WINDOWS */
6024
6025#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
6026/*
6027 * Get the value to show for the language mappings, active 'keymap'.
6028 */
6029 int
6030get_keymap_str(wp, buf, len)
6031 win_T *wp;
6032 char_u *buf; /* buffer for the result */
6033 int len; /* length of buffer */
6034{
6035 char_u *p;
6036
6037 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
6038 return FALSE;
6039
6040 {
6041#ifdef FEAT_EVAL
6042 buf_T *old_curbuf = curbuf;
6043 win_T *old_curwin = curwin;
6044 char_u *s;
6045
6046 curbuf = wp->w_buffer;
6047 curwin = wp;
6048 STRCPY(buf, "b:keymap_name"); /* must be writable */
6049 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006050 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 --emsg_skip;
6052 curbuf = old_curbuf;
6053 curwin = old_curwin;
6054 if (p == NULL || *p == NUL)
6055#endif
6056 {
6057#ifdef FEAT_KEYMAP
6058 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
6059 p = wp->w_buffer->b_p_keymap;
6060 else
6061#endif
6062 p = (char_u *)"lang";
6063 }
6064 if ((int)(STRLEN(p) + 3) < len)
6065 sprintf((char *)buf, "<%s>", p);
6066 else
6067 buf[0] = NUL;
6068#ifdef FEAT_EVAL
6069 vim_free(s);
6070#endif
6071 }
6072 return buf[0] != NUL;
6073}
6074#endif
6075
6076#if defined(FEAT_STL_OPT) || defined(PROTO)
6077/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006078 * Redraw the status line or ruler of window "wp".
6079 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080 */
6081 static void
Bram Moolenaar9372a112005-12-06 19:59:18 +00006082win_redr_custom(wp, draw_ruler)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083 win_T *wp;
Bram Moolenaar9372a112005-12-06 19:59:18 +00006084 int draw_ruler; /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085{
6086 int attr;
6087 int curattr;
6088 int row;
6089 int col = 0;
6090 int maxwidth;
6091 int width;
6092 int n;
6093 int len;
6094 int fillchar;
6095 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00006096 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006098 struct stl_hlrec hltab[STL_MAX_ITEM];
6099 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006100 int use_sandbox = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101
6102 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006103 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006105 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006106 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006107 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00006108 fillchar = ' ';
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006109 attr = hl_attr(HLF_TPF);
6110 maxwidth = Columns;
6111# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006112 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006113# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006115 else
6116 {
6117 row = W_WINROW(wp) + wp->w_height;
6118 fillchar = fillchar_status(&attr, wp == curwin);
6119 maxwidth = W_WIDTH(wp);
6120
6121 if (draw_ruler)
6122 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006123 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006124 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006125 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006126 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006127 if (*++stl == '-')
6128 stl++;
6129 if (atoi((char *)stl))
6130 while (VIM_ISDIGIT(*stl))
6131 stl++;
6132 if (*stl++ != '(')
6133 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006134 }
6135#ifdef FEAT_VERTSPLIT
6136 col = ru_col - (Columns - W_WIDTH(wp));
6137 if (col < (W_WIDTH(wp) + 1) / 2)
6138 col = (W_WIDTH(wp) + 1) / 2;
6139#else
6140 col = ru_col;
6141 if (col > (Columns + 1) / 2)
6142 col = (Columns + 1) / 2;
6143#endif
6144 maxwidth = W_WIDTH(wp) - col;
6145#ifdef FEAT_WINDOWS
6146 if (!wp->w_status_height)
6147#endif
6148 {
6149 row = Rows - 1;
6150 --maxwidth; /* writing in last column may cause scrolling */
6151 fillchar = ' ';
6152 attr = 0;
6153 }
6154
6155# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006156 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006157# endif
6158 }
6159 else
6160 {
6161 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006162 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006163 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00006164 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006165# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006166 use_sandbox = was_set_insecurely((char_u *)"statusline",
6167 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006168# endif
6169 }
6170
6171#ifdef FEAT_VERTSPLIT
6172 col += W_WINCOL(wp);
6173#endif
6174 }
6175
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176 if (maxwidth <= 0)
6177 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178
Bram Moolenaar362f3562009-11-03 16:20:34 +00006179 /* Make a copy, because the statusline may include a function call that
6180 * might change the option value and free the memory. */
6181 stl = vim_strsave(stl);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006182 width = build_stl_str_hl(wp == NULL ? curwin : wp,
6183 buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00006184 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006185 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006186 vim_free(stl);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006187 len = (int)STRLEN(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00006189 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 {
6191#ifdef FEAT_MBYTE
6192 len += (*mb_char2bytes)(fillchar, buf + len);
6193#else
6194 buf[len++] = fillchar;
6195#endif
6196 ++width;
6197 }
6198 buf[len] = NUL;
6199
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006200 /*
6201 * Draw each snippet with the specified highlighting.
6202 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 curattr = attr;
6204 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006205 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006207 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 screen_puts_len(p, len, row, col, curattr);
6209 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006210 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006212 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006214 else if (hltab[n].userhl < 0)
6215 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00006217 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006218 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219#endif
6220 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006221 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 }
6223 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006224
6225 if (wp == NULL)
6226 {
6227 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
6228 col = 0;
6229 len = 0;
6230 p = buf;
6231 fillchar = 0;
6232 for (n = 0; tabtab[n].start != NULL; n++)
6233 {
6234 len += vim_strnsize(p, (int)(tabtab[n].start - p));
6235 while (col < len)
6236 TabPageIdxs[col++] = fillchar;
6237 p = tabtab[n].start;
6238 fillchar = tabtab[n].userhl;
6239 }
6240 while (col < Columns)
6241 TabPageIdxs[col++] = fillchar;
6242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243}
6244
6245#endif /* FEAT_STL_OPT */
6246
6247/*
6248 * Output a single character directly to the screen and update ScreenLines.
6249 */
6250 void
6251screen_putchar(c, row, col, attr)
6252 int c;
6253 int row, col;
6254 int attr;
6255{
6256#ifdef FEAT_MBYTE
6257 char_u buf[MB_MAXBYTES + 1];
6258
6259 buf[(*mb_char2bytes)(c, buf)] = NUL;
6260#else
6261 char_u buf[2];
6262
6263 buf[0] = c;
6264 buf[1] = NUL;
6265#endif
6266 screen_puts(buf, row, col, attr);
6267}
6268
6269/*
6270 * Get a single character directly from ScreenLines into "bytes[]".
6271 * Also return its attribute in *attrp;
6272 */
6273 void
6274screen_getbytes(row, col, bytes, attrp)
6275 int row, col;
6276 char_u *bytes;
6277 int *attrp;
6278{
6279 unsigned off;
6280
6281 /* safety check */
6282 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
6283 {
6284 off = LineOffset[row] + col;
6285 *attrp = ScreenAttrs[off];
6286 bytes[0] = ScreenLines[off];
6287 bytes[1] = NUL;
6288
6289#ifdef FEAT_MBYTE
6290 if (enc_utf8 && ScreenLinesUC[off] != 0)
6291 bytes[utfc_char2bytes(off, bytes)] = NUL;
6292 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6293 {
6294 bytes[0] = ScreenLines[off];
6295 bytes[1] = ScreenLines2[off];
6296 bytes[2] = NUL;
6297 }
6298 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
6299 {
6300 bytes[1] = ScreenLines[off + 1];
6301 bytes[2] = NUL;
6302 }
6303#endif
6304 }
6305}
6306
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006307#ifdef FEAT_MBYTE
6308static int screen_comp_differs __ARGS((int, int*));
6309
6310/*
6311 * Return TRUE if composing characters for screen posn "off" differs from
6312 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006313 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006314 */
6315 static int
6316screen_comp_differs(off, u8cc)
6317 int off;
6318 int *u8cc;
6319{
6320 int i;
6321
6322 for (i = 0; i < Screen_mco; ++i)
6323 {
6324 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
6325 return TRUE;
6326 if (u8cc[i] == 0)
6327 break;
6328 }
6329 return FALSE;
6330}
6331#endif
6332
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333/*
6334 * Put string '*text' on the screen at position 'row' and 'col', with
6335 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
6336 * Note: only outputs within one row, message is truncated at screen boundary!
6337 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
6338 */
6339 void
6340screen_puts(text, row, col, attr)
6341 char_u *text;
6342 int row;
6343 int col;
6344 int attr;
6345{
6346 screen_puts_len(text, -1, row, col, attr);
6347}
6348
6349/*
6350 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
6351 * a NUL.
6352 */
6353 void
6354screen_puts_len(text, len, row, col, attr)
6355 char_u *text;
6356 int len;
6357 int row;
6358 int col;
6359 int attr;
6360{
6361 unsigned off;
6362 char_u *ptr = text;
6363 int c;
6364#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00006365 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366 int mbyte_blen = 1;
6367 int mbyte_cells = 1;
6368 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006369 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370 int clear_next_cell = FALSE;
6371# ifdef FEAT_ARABIC
6372 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006373 int pc, nc, nc1;
6374 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375# endif
6376#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006377#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6378 int force_redraw_this;
6379 int force_redraw_next = FALSE;
6380#endif
6381 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382
6383 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
6384 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006385 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386
Bram Moolenaarc236c162008-07-13 17:41:49 +00006387#ifdef FEAT_MBYTE
6388 /* When drawing over the right halve of a double-wide char clear out the
6389 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006390 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00006391# ifdef FEAT_GUI
6392 && !gui.in_use
6393# endif
6394 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006395 {
6396 ScreenLines[off - 1] = ' ';
6397 ScreenAttrs[off - 1] = 0;
6398 if (enc_utf8)
6399 {
6400 ScreenLinesUC[off - 1] = 0;
6401 ScreenLinesC[0][off - 1] = 0;
6402 }
6403 /* redraw the previous cell, make it empty */
6404 screen_char(off - 1, row, col - 1);
6405 /* force the cell at "col" to be redrawn */
6406 force_redraw_next = TRUE;
6407 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00006408#endif
6409
Bram Moolenaar367329b2007-08-30 11:53:22 +00006410#ifdef FEAT_MBYTE
6411 max_off = LineOffset[row] + screen_Columns;
6412#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00006413 while (col < screen_Columns
6414 && (len < 0 || (int)(ptr - text) < len)
6415 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 {
6417 c = *ptr;
6418#ifdef FEAT_MBYTE
6419 /* check if this is the first byte of a multibyte */
6420 if (has_mbyte)
6421 {
6422 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006423 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006425 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6427 mbyte_cells = 1;
6428 else if (enc_dbcs != 0)
6429 mbyte_cells = mbyte_blen;
6430 else /* enc_utf8 */
6431 {
6432 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006433 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 (int)((text + len) - ptr));
6435 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006436 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00006438# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439 /* Non-BMP character: display as ? or fullwidth ?. */
6440 if (u8c >= 0x10000)
6441 {
6442 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
6443 if (attr == 0)
6444 attr = hl_attr(HLF_8);
6445 }
Bram Moolenaar11936362007-09-17 20:39:42 +00006446# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447# ifdef FEAT_ARABIC
6448 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
6449 {
6450 /* Do Arabic shaping. */
6451 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
6452 {
6453 /* Past end of string to be displayed. */
6454 nc = NUL;
6455 nc1 = NUL;
6456 }
6457 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006458 {
Bram Moolenaar54620182009-11-11 16:07:20 +00006459 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
6460 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006461 nc1 = pcc[0];
6462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 pc = prev_c;
6464 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006465 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 }
6467 else
6468 prev_c = u8c;
6469# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01006470 if (col + mbyte_cells > screen_Columns)
6471 {
6472 /* Only 1 cell left, but character requires 2 cells:
6473 * display a '>' in the last column to avoid wrapping. */
6474 c = '>';
6475 mbyte_cells = 1;
6476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 }
6478 }
6479#endif
6480
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006481#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6482 force_redraw_this = force_redraw_next;
6483 force_redraw_next = FALSE;
6484#endif
6485
6486 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487#ifdef FEAT_MBYTE
6488 || (mbyte_cells == 2
6489 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
6490 || (enc_dbcs == DBCS_JPNU
6491 && c == 0x8e
6492 && ScreenLines2[off] != ptr[1])
6493 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006494 && (ScreenLinesUC[off] !=
6495 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
6496 || (ScreenLinesUC[off] != 0
6497 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498#endif
6499 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006500 || exmode_active;
6501
6502 if (need_redraw
6503#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6504 || force_redraw_this
6505#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506 )
6507 {
6508#if defined(FEAT_GUI) || defined(UNIX)
6509 /* The bold trick makes a single row of pixels appear in the next
6510 * character. When a bold character is removed, the next
6511 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006512 * and for some xterms. */
6513 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514# ifdef FEAT_GUI
6515 gui.in_use
6516# endif
6517# if defined(FEAT_GUI) && defined(UNIX)
6518 ||
6519# endif
6520# ifdef UNIX
6521 term_is_xterm
6522# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006523 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006525 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006527 if (n > HL_ALL)
6528 n = syn_attr2attr(n);
6529 if (n & HL_BOLD)
6530 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531 }
6532#endif
6533#ifdef FEAT_MBYTE
6534 /* When at the end of the text and overwriting a two-cell
6535 * character with a one-cell character, need to clear the next
6536 * cell. Also when overwriting the left halve of a two-cell char
6537 * with the right halve of a two-cell char. Do this only once
6538 * (mb_off2cells() may return 2 on the right halve). */
6539 if (clear_next_cell)
6540 clear_next_cell = FALSE;
6541 else if (has_mbyte
6542 && (len < 0 ? ptr[mbyte_blen] == NUL
6543 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00006544 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006546 && (*mb_off2cells)(off, max_off) == 1
6547 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548 clear_next_cell = TRUE;
6549
6550 /* Make sure we never leave a second byte of a double-byte behind,
6551 * it confuses mb_off2cells(). */
6552 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00006553 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006555 && (*mb_off2cells)(off, max_off) == 1
6556 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 ScreenLines[off + mbyte_blen] = 0;
6558#endif
6559 ScreenLines[off] = c;
6560 ScreenAttrs[off] = attr;
6561#ifdef FEAT_MBYTE
6562 if (enc_utf8)
6563 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006564 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565 ScreenLinesUC[off] = 0;
6566 else
6567 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006568 int i;
6569
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006571 for (i = 0; i < Screen_mco; ++i)
6572 {
6573 ScreenLinesC[i][off] = u8cc[i];
6574 if (u8cc[i] == 0)
6575 break;
6576 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577 }
6578 if (mbyte_cells == 2)
6579 {
6580 ScreenLines[off + 1] = 0;
6581 ScreenAttrs[off + 1] = attr;
6582 }
6583 screen_char(off, row, col);
6584 }
6585 else if (mbyte_cells == 2)
6586 {
6587 ScreenLines[off + 1] = ptr[1];
6588 ScreenAttrs[off + 1] = attr;
6589 screen_char_2(off, row, col);
6590 }
6591 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6592 {
6593 ScreenLines2[off] = ptr[1];
6594 screen_char(off, row, col);
6595 }
6596 else
6597#endif
6598 screen_char(off, row, col);
6599 }
6600#ifdef FEAT_MBYTE
6601 if (has_mbyte)
6602 {
6603 off += mbyte_cells;
6604 col += mbyte_cells;
6605 ptr += mbyte_blen;
6606 if (clear_next_cell)
6607 ptr = (char_u *)" ";
6608 }
6609 else
6610#endif
6611 {
6612 ++off;
6613 ++col;
6614 ++ptr;
6615 }
6616 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006617
6618#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6619 /* If we detected the next character needs to be redrawn, but the text
6620 * doesn't extend up to there, update the character here. */
6621 if (force_redraw_next && col < screen_Columns)
6622 {
6623# ifdef FEAT_MBYTE
6624 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
6625 screen_char_2(off, row, col);
6626 else
6627# endif
6628 screen_char(off, row, col);
6629 }
6630#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631}
6632
6633#ifdef FEAT_SEARCH_EXTRA
6634/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006635 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636 */
6637 static void
6638start_search_hl()
6639{
6640 if (p_hls && !no_hlsearch)
6641 {
6642 last_pat_prog(&search_hl.rm);
6643 search_hl.attr = hl_attr(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00006644# ifdef FEAT_RELTIME
6645 /* Set the time limit to 'redrawtime'. */
6646 profile_setlimit(p_rdt, &search_hl.tm);
6647# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 }
6649}
6650
6651/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006652 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 */
6654 static void
6655end_search_hl()
6656{
6657 if (search_hl.rm.regprog != NULL)
6658 {
6659 vim_free(search_hl.rm.regprog);
6660 search_hl.rm.regprog = NULL;
6661 }
6662}
6663
6664/*
6665 * Advance to the match in window "wp" line "lnum" or past it.
6666 */
6667 static void
6668prepare_search_hl(wp, lnum)
6669 win_T *wp;
6670 linenr_T lnum;
6671{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006672 matchitem_T *cur; /* points to the match list */
6673 match_T *shl; /* points to search_hl or a match */
6674 int shl_flag; /* flag to indicate whether search_hl
6675 has been processed or not */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676 int n;
6677
6678 /*
6679 * When using a multi-line pattern, start searching at the top
6680 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006681 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006683 cur = wp->w_match_head;
6684 shl_flag = FALSE;
6685 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006687 if (shl_flag == FALSE)
6688 {
6689 shl = &search_hl;
6690 shl_flag = TRUE;
6691 }
6692 else
6693 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694 if (shl->rm.regprog != NULL
6695 && shl->lnum == 0
6696 && re_multiline(shl->rm.regprog))
6697 {
6698 if (shl->first_lnum == 0)
6699 {
6700# ifdef FEAT_FOLDING
6701 for (shl->first_lnum = lnum;
6702 shl->first_lnum > wp->w_topline; --shl->first_lnum)
6703 if (hasFoldingWin(wp, shl->first_lnum - 1,
6704 NULL, NULL, TRUE, NULL))
6705 break;
6706# else
6707 shl->first_lnum = wp->w_topline;
6708# endif
6709 }
6710 n = 0;
6711 while (shl->first_lnum < lnum && shl->rm.regprog != NULL)
6712 {
6713 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n);
6714 if (shl->lnum != 0)
6715 {
6716 shl->first_lnum = shl->lnum
6717 + shl->rm.endpos[0].lnum
6718 - shl->rm.startpos[0].lnum;
6719 n = shl->rm.endpos[0].col;
6720 }
6721 else
6722 {
6723 ++shl->first_lnum;
6724 n = 0;
6725 }
6726 }
6727 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006728 if (shl != &search_hl && cur != NULL)
6729 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730 }
6731}
6732
6733/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006734 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735 * Uses shl->buf.
6736 * Sets shl->lnum and shl->rm contents.
6737 * Note: Assumes a previous match is always before "lnum", unless
6738 * shl->lnum is zero.
6739 * Careful: Any pointers for buffer lines will become invalid.
6740 */
6741 static void
6742next_search_hl(win, shl, lnum, mincol)
6743 win_T *win;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006744 match_T *shl; /* points to search_hl or a match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745 linenr_T lnum;
6746 colnr_T mincol; /* minimal column for a match */
6747{
6748 linenr_T l;
6749 colnr_T matchcol;
6750 long nmatched;
6751
6752 if (shl->lnum != 0)
6753 {
6754 /* Check for three situations:
6755 * 1. If the "lnum" is below a previous match, start a new search.
6756 * 2. If the previous match includes "mincol", use it.
6757 * 3. Continue after the previous match.
6758 */
6759 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
6760 if (lnum > l)
6761 shl->lnum = 0;
6762 else if (lnum < l || shl->rm.endpos[0].col > mincol)
6763 return;
6764 }
6765
6766 /*
6767 * Repeat searching for a match until one is found that includes "mincol"
6768 * or none is found in this line.
6769 */
6770 called_emsg = FALSE;
6771 for (;;)
6772 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00006773#ifdef FEAT_RELTIME
6774 /* Stop searching after passing the time limit. */
6775 if (profile_passed_limit(&(shl->tm)))
6776 {
6777 shl->lnum = 0; /* no match found in time */
6778 break;
6779 }
6780#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781 /* Three situations:
6782 * 1. No useful previous match: search from start of line.
6783 * 2. Not Vi compatible or empty match: continue at next character.
6784 * Break the loop if this is beyond the end of the line.
6785 * 3. Vi compatible searching: continue at end of previous match.
6786 */
6787 if (shl->lnum == 0)
6788 matchcol = 0;
6789 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
6790 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006791 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006793 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006794
6795 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006796 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006797 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006799 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 shl->lnum = 0;
6801 break;
6802 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006803#ifdef FEAT_MBYTE
6804 if (has_mbyte)
6805 matchcol += mb_ptr2len(ml);
6806 else
6807#endif
6808 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 }
6810 else
6811 matchcol = shl->rm.endpos[0].col;
6812
6813 shl->lnum = lnum;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00006814 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum, matchcol,
6815#ifdef FEAT_RELTIME
6816 &(shl->tm)
6817#else
6818 NULL
6819#endif
6820 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 if (called_emsg)
6822 {
6823 /* Error while handling regexp: stop using this regexp. */
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00006824 if (shl == &search_hl)
6825 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006826 /* don't free regprog in the match list, it's a copy */
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00006827 vim_free(shl->rm.regprog);
6828 no_hlsearch = TRUE;
6829 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 shl->rm.regprog = NULL;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00006831 shl->lnum = 0;
6832 got_int = FALSE; /* avoid the "Type :quit to exit Vim" message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833 break;
6834 }
6835 if (nmatched == 0)
6836 {
6837 shl->lnum = 0; /* no match found */
6838 break;
6839 }
6840 if (shl->rm.startpos[0].lnum > 0
6841 || shl->rm.startpos[0].col >= mincol
6842 || nmatched > 1
6843 || shl->rm.endpos[0].col > mincol)
6844 {
6845 shl->lnum += shl->rm.startpos[0].lnum;
6846 break; /* useful match found */
6847 }
6848 }
6849}
6850#endif
6851
6852 static void
6853screen_start_highlight(attr)
6854 int attr;
6855{
6856 attrentry_T *aep = NULL;
6857
6858 screen_attr = attr;
6859 if (full_screen
6860#ifdef WIN3264
6861 && termcap_active
6862#endif
6863 )
6864 {
6865#ifdef FEAT_GUI
6866 if (gui.in_use)
6867 {
6868 char buf[20];
6869
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006870 /* The GUI handles this internally. */
6871 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 OUT_STR(buf);
6873 }
6874 else
6875#endif
6876 {
6877 if (attr > HL_ALL) /* special HL attr. */
6878 {
6879 if (t_colors > 1)
6880 aep = syn_cterm_attr2entry(attr);
6881 else
6882 aep = syn_term_attr2entry(attr);
6883 if (aep == NULL) /* did ":syntax clear" */
6884 attr = 0;
6885 else
6886 attr = aep->ae_attr;
6887 }
6888 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
6889 out_str(T_MD);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006890 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
6891 && cterm_normal_fg_bold)
6892 /* If the Normal FG color has BOLD attribute and the new HL
6893 * has a FG color defined, clear BOLD. */
6894 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
6896 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006897 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
6898 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899 out_str(T_US);
6900 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
6901 out_str(T_CZH);
6902 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
6903 out_str(T_MR);
6904
6905 /*
6906 * Output the color or start string after bold etc., in case the
6907 * bold etc. override the color setting.
6908 */
6909 if (aep != NULL)
6910 {
6911 if (t_colors > 1)
6912 {
6913 if (aep->ae_u.cterm.fg_color)
6914 term_fg_color(aep->ae_u.cterm.fg_color - 1);
6915 if (aep->ae_u.cterm.bg_color)
6916 term_bg_color(aep->ae_u.cterm.bg_color - 1);
6917 }
6918 else
6919 {
6920 if (aep->ae_u.term.start != NULL)
6921 out_str(aep->ae_u.term.start);
6922 }
6923 }
6924 }
6925 }
6926}
6927
6928 void
6929screen_stop_highlight()
6930{
6931 int do_ME = FALSE; /* output T_ME code */
6932
6933 if (screen_attr != 0
6934#ifdef WIN3264
6935 && termcap_active
6936#endif
6937 )
6938 {
6939#ifdef FEAT_GUI
6940 if (gui.in_use)
6941 {
6942 char buf[20];
6943
6944 /* use internal GUI code */
6945 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
6946 OUT_STR(buf);
6947 }
6948 else
6949#endif
6950 {
6951 if (screen_attr > HL_ALL) /* special HL attr. */
6952 {
6953 attrentry_T *aep;
6954
6955 if (t_colors > 1)
6956 {
6957 /*
6958 * Assume that t_me restores the original colors!
6959 */
6960 aep = syn_cterm_attr2entry(screen_attr);
6961 if (aep != NULL && (aep->ae_u.cterm.fg_color
6962 || aep->ae_u.cterm.bg_color))
6963 do_ME = TRUE;
6964 }
6965 else
6966 {
6967 aep = syn_term_attr2entry(screen_attr);
6968 if (aep != NULL && aep->ae_u.term.stop != NULL)
6969 {
6970 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
6971 do_ME = TRUE;
6972 else
6973 out_str(aep->ae_u.term.stop);
6974 }
6975 }
6976 if (aep == NULL) /* did ":syntax clear" */
6977 screen_attr = 0;
6978 else
6979 screen_attr = aep->ae_attr;
6980 }
6981
6982 /*
6983 * Often all ending-codes are equal to T_ME. Avoid outputting the
6984 * same sequence several times.
6985 */
6986 if (screen_attr & HL_STANDOUT)
6987 {
6988 if (STRCMP(T_SE, T_ME) == 0)
6989 do_ME = TRUE;
6990 else
6991 out_str(T_SE);
6992 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006993 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 {
6995 if (STRCMP(T_UE, T_ME) == 0)
6996 do_ME = TRUE;
6997 else
6998 out_str(T_UE);
6999 }
7000 if (screen_attr & HL_ITALIC)
7001 {
7002 if (STRCMP(T_CZR, T_ME) == 0)
7003 do_ME = TRUE;
7004 else
7005 out_str(T_CZR);
7006 }
7007 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
7008 out_str(T_ME);
7009
7010 if (t_colors > 1)
7011 {
7012 /* set Normal cterm colors */
7013 if (cterm_normal_fg_color != 0)
7014 term_fg_color(cterm_normal_fg_color - 1);
7015 if (cterm_normal_bg_color != 0)
7016 term_bg_color(cterm_normal_bg_color - 1);
7017 if (cterm_normal_fg_bold)
7018 out_str(T_MD);
7019 }
7020 }
7021 }
7022 screen_attr = 0;
7023}
7024
7025/*
7026 * Reset the colors for a cterm. Used when leaving Vim.
7027 * The machine specific code may override this again.
7028 */
7029 void
7030reset_cterm_colors()
7031{
7032 if (t_colors > 1)
7033 {
7034 /* set Normal cterm colors */
7035 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
7036 {
7037 out_str(T_OP);
7038 screen_attr = -1;
7039 }
7040 if (cterm_normal_fg_bold)
7041 {
7042 out_str(T_ME);
7043 screen_attr = -1;
7044 }
7045 }
7046}
7047
7048/*
7049 * Put character ScreenLines["off"] on the screen at position "row" and "col",
7050 * using the attributes from ScreenAttrs["off"].
7051 */
7052 static void
7053screen_char(off, row, col)
7054 unsigned off;
7055 int row;
7056 int col;
7057{
7058 int attr;
7059
7060 /* Check for illegal values, just in case (could happen just after
7061 * resizing). */
7062 if (row >= screen_Rows || col >= screen_Columns)
7063 return;
7064
7065 /* Outputting the last character on the screen may scrollup the screen.
7066 * Don't to it! Mark the character invalid (update it when scrolled up) */
7067 if (row == screen_Rows - 1 && col == screen_Columns - 1
7068#ifdef FEAT_RIGHTLEFT
7069 /* account for first command-line character in rightleft mode */
7070 && !cmdmsg_rl
7071#endif
7072 )
7073 {
7074 ScreenAttrs[off] = (sattr_T)-1;
7075 return;
7076 }
7077
7078 /*
7079 * Stop highlighting first, so it's easier to move the cursor.
7080 */
7081#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
7082 if (screen_char_attr != 0)
7083 attr = screen_char_attr;
7084 else
7085#endif
7086 attr = ScreenAttrs[off];
7087 if (screen_attr != attr)
7088 screen_stop_highlight();
7089
7090 windgoto(row, col);
7091
7092 if (screen_attr != attr)
7093 screen_start_highlight(attr);
7094
7095#ifdef FEAT_MBYTE
7096 if (enc_utf8 && ScreenLinesUC[off] != 0)
7097 {
7098 char_u buf[MB_MAXBYTES + 1];
7099
7100 /* Convert UTF-8 character to bytes and write it. */
7101
7102 buf[utfc_char2bytes(off, buf)] = NUL;
7103
7104 out_str(buf);
7105 if (utf_char2cells(ScreenLinesUC[off]) > 1)
7106 ++screen_cur_col;
7107 }
7108 else
7109#endif
7110 {
7111#ifdef FEAT_MBYTE
7112 out_flush_check();
7113#endif
7114 out_char(ScreenLines[off]);
7115#ifdef FEAT_MBYTE
7116 /* double-byte character in single-width cell */
7117 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7118 out_char(ScreenLines2[off]);
7119#endif
7120 }
7121
7122 screen_cur_col++;
7123}
7124
7125#ifdef FEAT_MBYTE
7126
7127/*
7128 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
7129 * on the screen at position 'row' and 'col'.
7130 * The attributes of the first byte is used for all. This is required to
7131 * output the two bytes of a double-byte character with nothing in between.
7132 */
7133 static void
7134screen_char_2(off, row, col)
7135 unsigned off;
7136 int row;
7137 int col;
7138{
7139 /* Check for illegal values (could be wrong when screen was resized). */
7140 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
7141 return;
7142
7143 /* Outputting the last character on the screen may scrollup the screen.
7144 * Don't to it! Mark the character invalid (update it when scrolled up) */
7145 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
7146 {
7147 ScreenAttrs[off] = (sattr_T)-1;
7148 return;
7149 }
7150
7151 /* Output the first byte normally (positions the cursor), then write the
7152 * second byte directly. */
7153 screen_char(off, row, col);
7154 out_char(ScreenLines[off + 1]);
7155 ++screen_cur_col;
7156}
7157#endif
7158
7159#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
7160/*
7161 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
7162 * This uses the contents of ScreenLines[] and doesn't change it.
7163 */
7164 void
7165screen_draw_rectangle(row, col, height, width, invert)
7166 int row;
7167 int col;
7168 int height;
7169 int width;
7170 int invert;
7171{
7172 int r, c;
7173 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007174#ifdef FEAT_MBYTE
7175 int max_off;
7176#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007178 /* Can't use ScreenLines unless initialized */
7179 if (ScreenLines == NULL)
7180 return;
7181
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182 if (invert)
7183 screen_char_attr = HL_INVERSE;
7184 for (r = row; r < row + height; ++r)
7185 {
7186 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00007187#ifdef FEAT_MBYTE
7188 max_off = off + screen_Columns;
7189#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 for (c = col; c < col + width; ++c)
7191 {
7192#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007193 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194 {
7195 screen_char_2(off + c, r, c);
7196 ++c;
7197 }
7198 else
7199#endif
7200 {
7201 screen_char(off + c, r, c);
7202#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007203 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 ++c;
7205#endif
7206 }
7207 }
7208 }
7209 screen_char_attr = 0;
7210}
7211#endif
7212
7213#ifdef FEAT_VERTSPLIT
7214/*
7215 * Redraw the characters for a vertically split window.
7216 */
7217 static void
7218redraw_block(row, end, wp)
7219 int row;
7220 int end;
7221 win_T *wp;
7222{
7223 int col;
7224 int width;
7225
7226# ifdef FEAT_CLIPBOARD
7227 clip_may_clear_selection(row, end - 1);
7228# endif
7229
7230 if (wp == NULL)
7231 {
7232 col = 0;
7233 width = Columns;
7234 }
7235 else
7236 {
7237 col = wp->w_wincol;
7238 width = wp->w_width;
7239 }
7240 screen_draw_rectangle(row, col, end - row, width, FALSE);
7241}
7242#endif
7243
7244/*
7245 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
7246 * with character 'c1' in first column followed by 'c2' in the other columns.
7247 * Use attributes 'attr'.
7248 */
7249 void
7250screen_fill(start_row, end_row, start_col, end_col, c1, c2, attr)
7251 int start_row, end_row;
7252 int start_col, end_col;
7253 int c1, c2;
7254 int attr;
7255{
7256 int row;
7257 int col;
7258 int off;
7259 int end_off;
7260 int did_delete;
7261 int c;
7262 int norm_term;
7263#if defined(FEAT_GUI) || defined(UNIX)
7264 int force_next = FALSE;
7265#endif
7266
7267 if (end_row > screen_Rows) /* safety check */
7268 end_row = screen_Rows;
7269 if (end_col > screen_Columns) /* safety check */
7270 end_col = screen_Columns;
7271 if (ScreenLines == NULL
7272 || start_row >= end_row
7273 || start_col >= end_col) /* nothing to do */
7274 return;
7275
7276 /* it's a "normal" terminal when not in a GUI or cterm */
7277 norm_term = (
7278#ifdef FEAT_GUI
7279 !gui.in_use &&
7280#endif
7281 t_colors <= 1);
7282 for (row = start_row; row < end_row; ++row)
7283 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00007284#ifdef FEAT_MBYTE
7285 if (has_mbyte
7286# ifdef FEAT_GUI
7287 && !gui.in_use
7288# endif
7289 )
7290 {
7291 /* When drawing over the right halve of a double-wide char clear
7292 * out the left halve. When drawing over the left halve of a
7293 * double wide-char clear out the right halve. Only needed in a
7294 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007295 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00007296 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00007297 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00007298 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00007299 }
7300#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301 /*
7302 * Try to use delete-line termcap code, when no attributes or in a
7303 * "normal" terminal, where a bold/italic space is just a
7304 * space.
7305 */
7306 did_delete = FALSE;
7307 if (c2 == ' '
7308 && end_col == Columns
7309 && can_clear(T_CE)
7310 && (attr == 0
7311 || (norm_term
7312 && attr <= HL_ALL
7313 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
7314 {
7315 /*
7316 * check if we really need to clear something
7317 */
7318 col = start_col;
7319 if (c1 != ' ') /* don't clear first char */
7320 ++col;
7321
7322 off = LineOffset[row] + col;
7323 end_off = LineOffset[row] + end_col;
7324
7325 /* skip blanks (used often, keep it fast!) */
7326#ifdef FEAT_MBYTE
7327 if (enc_utf8)
7328 while (off < end_off && ScreenLines[off] == ' '
7329 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
7330 ++off;
7331 else
7332#endif
7333 while (off < end_off && ScreenLines[off] == ' '
7334 && ScreenAttrs[off] == 0)
7335 ++off;
7336 if (off < end_off) /* something to be cleared */
7337 {
7338 col = off - LineOffset[row];
7339 screen_stop_highlight();
7340 term_windgoto(row, col);/* clear rest of this screen line */
7341 out_str(T_CE);
7342 screen_start(); /* don't know where cursor is now */
7343 col = end_col - col;
7344 while (col--) /* clear chars in ScreenLines */
7345 {
7346 ScreenLines[off] = ' ';
7347#ifdef FEAT_MBYTE
7348 if (enc_utf8)
7349 ScreenLinesUC[off] = 0;
7350#endif
7351 ScreenAttrs[off] = 0;
7352 ++off;
7353 }
7354 }
7355 did_delete = TRUE; /* the chars are cleared now */
7356 }
7357
7358 off = LineOffset[row] + start_col;
7359 c = c1;
7360 for (col = start_col; col < end_col; ++col)
7361 {
7362 if (ScreenLines[off] != c
7363#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007364 || (enc_utf8 && (int)ScreenLinesUC[off]
7365 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366#endif
7367 || ScreenAttrs[off] != attr
7368#if defined(FEAT_GUI) || defined(UNIX)
7369 || force_next
7370#endif
7371 )
7372 {
7373#if defined(FEAT_GUI) || defined(UNIX)
7374 /* The bold trick may make a single row of pixels appear in
7375 * the next character. When a bold character is removed, the
7376 * next character should be redrawn too. This happens for our
7377 * own GUI and for some xterms. */
7378 if (
7379# ifdef FEAT_GUI
7380 gui.in_use
7381# endif
7382# if defined(FEAT_GUI) && defined(UNIX)
7383 ||
7384# endif
7385# ifdef UNIX
7386 term_is_xterm
7387# endif
7388 )
7389 {
7390 if (ScreenLines[off] != ' '
7391 && (ScreenAttrs[off] > HL_ALL
7392 || ScreenAttrs[off] & HL_BOLD))
7393 force_next = TRUE;
7394 else
7395 force_next = FALSE;
7396 }
7397#endif
7398 ScreenLines[off] = c;
7399#ifdef FEAT_MBYTE
7400 if (enc_utf8)
7401 {
7402 if (c >= 0x80)
7403 {
7404 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007405 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406 }
7407 else
7408 ScreenLinesUC[off] = 0;
7409 }
7410#endif
7411 ScreenAttrs[off] = attr;
7412 if (!did_delete || c != ' ')
7413 screen_char(off, row, col);
7414 }
7415 ++off;
7416 if (col == start_col)
7417 {
7418 if (did_delete)
7419 break;
7420 c = c2;
7421 }
7422 }
7423 if (end_col == Columns)
7424 LineWraps[row] = FALSE;
7425 if (row == Rows - 1) /* overwritten the command line */
7426 {
7427 redraw_cmdline = TRUE;
7428 if (c1 == ' ' && c2 == ' ')
7429 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007430 if (start_col == 0)
7431 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432 }
7433 }
7434}
7435
7436/*
7437 * Check if there should be a delay. Used before clearing or redrawing the
7438 * screen or the command line.
7439 */
7440 void
7441check_for_delay(check_msg_scroll)
7442 int check_msg_scroll;
7443{
7444 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
7445 && !did_wait_return
7446 && emsg_silent == 0)
7447 {
7448 out_flush();
7449 ui_delay(1000L, TRUE);
7450 emsg_on_display = FALSE;
7451 if (check_msg_scroll)
7452 msg_scroll = FALSE;
7453 }
7454}
7455
7456/*
7457 * screen_valid - allocate screen buffers if size changed
7458 * If "clear" is TRUE: clear screen if it has been resized.
7459 * Returns TRUE if there is a valid screen to write to.
7460 * Returns FALSE when starting up and screen not initialized yet.
7461 */
7462 int
7463screen_valid(clear)
7464 int clear;
7465{
7466 screenalloc(clear); /* allocate screen buffers if size changed */
7467 return (ScreenLines != NULL);
7468}
7469
7470/*
7471 * Resize the shell to Rows and Columns.
7472 * Allocate ScreenLines[] and associated items.
7473 *
7474 * There may be some time between setting Rows and Columns and (re)allocating
7475 * ScreenLines[]. This happens when starting up and when (manually) changing
7476 * the shell size. Always use screen_Rows and screen_Columns to access items
7477 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
7478 * final size of the shell is needed.
7479 */
7480 void
7481screenalloc(clear)
7482 int clear;
7483{
7484 int new_row, old_row;
7485#ifdef FEAT_GUI
7486 int old_Rows;
7487#endif
7488 win_T *wp;
7489 int outofmem = FALSE;
7490 int len;
7491 schar_T *new_ScreenLines;
7492#ifdef FEAT_MBYTE
7493 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007494 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007496 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497#endif
7498 sattr_T *new_ScreenAttrs;
7499 unsigned *new_LineOffset;
7500 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007501#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007502 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007503 tabpage_T *tp;
7504#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00007506 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00007507#ifdef FEAT_AUTOCMD
7508 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509
Bram Moolenaar87e817c2009-02-22 20:13:39 +00007510retry:
7511#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512 /*
7513 * Allocation of the screen buffers is done only when the size changes and
7514 * when Rows and Columns have been set and we have started doing full
7515 * screen stuff.
7516 */
7517 if ((ScreenLines != NULL
7518 && Rows == screen_Rows
7519 && Columns == screen_Columns
7520#ifdef FEAT_MBYTE
7521 && enc_utf8 == (ScreenLinesUC != NULL)
7522 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007523 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524#endif
7525 )
7526 || Rows == 0
7527 || Columns == 0
7528 || (!full_screen && ScreenLines == NULL))
7529 return;
7530
7531 /*
7532 * It's possible that we produce an out-of-memory message below, which
7533 * will cause this function to be called again. To break the loop, just
7534 * return here.
7535 */
7536 if (entered)
7537 return;
7538 entered = TRUE;
7539
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00007540 /*
7541 * Note that the window sizes are updated before reallocating the arrays,
7542 * thus we must not redraw here!
7543 */
7544 ++RedrawingDisabled;
7545
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546 win_new_shellsize(); /* fit the windows in the new sized shell */
7547
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548 comp_col(); /* recompute columns for shown command and ruler */
7549
7550 /*
7551 * We're changing the size of the screen.
7552 * - Allocate new arrays for ScreenLines and ScreenAttrs.
7553 * - Move lines from the old arrays into the new arrays, clear extra
7554 * lines (unless the screen is going to be cleared).
7555 * - Free the old arrays.
7556 *
7557 * If anything fails, make ScreenLines NULL, so we don't do anything!
7558 * Continuing with the old ScreenLines may result in a crash, because the
7559 * size is wrong.
7560 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007561 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00007563#ifdef FEAT_AUTOCMD
7564 if (aucmd_win != NULL)
7565 win_free_lsize(aucmd_win);
7566#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567
7568 new_ScreenLines = (schar_T *)lalloc((long_u)(
7569 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7570#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01007571 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572 if (enc_utf8)
7573 {
7574 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
7575 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007576 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007577 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
7579 }
7580 if (enc_dbcs == DBCS_JPNU)
7581 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
7582 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7583#endif
7584 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
7585 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
7586 new_LineOffset = (unsigned *)lalloc((long_u)(
7587 Rows * sizeof(unsigned)), FALSE);
7588 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007589#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007590 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007591#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007593 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594 {
7595 if (win_alloc_lines(wp) == FAIL)
7596 {
7597 outofmem = TRUE;
7598#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00007599 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600#endif
7601 }
7602 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00007603#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00007604 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
7605 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00007606 outofmem = TRUE;
7607#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00007608#ifdef FEAT_WINDOWS
7609give_up:
7610#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007612#ifdef FEAT_MBYTE
7613 for (i = 0; i < p_mco; ++i)
7614 if (new_ScreenLinesC[i] == NULL)
7615 break;
7616#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617 if (new_ScreenLines == NULL
7618#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007619 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
7621#endif
7622 || new_ScreenAttrs == NULL
7623 || new_LineOffset == NULL
7624 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00007625#ifdef FEAT_WINDOWS
7626 || new_TabPageIdxs == NULL
7627#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628 || outofmem)
7629 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00007630 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007631 {
7632 /* guess the size */
7633 do_outofmem_msg((long_u)((Rows + 1) * Columns));
7634
7635 /* Remember we did this to avoid getting outofmem messages over
7636 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00007637 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639 vim_free(new_ScreenLines);
7640 new_ScreenLines = NULL;
7641#ifdef FEAT_MBYTE
7642 vim_free(new_ScreenLinesUC);
7643 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007644 for (i = 0; i < p_mco; ++i)
7645 {
7646 vim_free(new_ScreenLinesC[i]);
7647 new_ScreenLinesC[i] = NULL;
7648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649 vim_free(new_ScreenLines2);
7650 new_ScreenLines2 = NULL;
7651#endif
7652 vim_free(new_ScreenAttrs);
7653 new_ScreenAttrs = NULL;
7654 vim_free(new_LineOffset);
7655 new_LineOffset = NULL;
7656 vim_free(new_LineWraps);
7657 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007658#ifdef FEAT_WINDOWS
7659 vim_free(new_TabPageIdxs);
7660 new_TabPageIdxs = NULL;
7661#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 }
7663 else
7664 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00007665 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007666
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 for (new_row = 0; new_row < Rows; ++new_row)
7668 {
7669 new_LineOffset[new_row] = new_row * Columns;
7670 new_LineWraps[new_row] = FALSE;
7671
7672 /*
7673 * If the screen is not going to be cleared, copy as much as
7674 * possible from the old screen to the new one and clear the rest
7675 * (used when resizing the window at the "--more--" prompt or when
7676 * executing an external command, for the GUI).
7677 */
7678 if (!clear)
7679 {
7680 (void)vim_memset(new_ScreenLines + new_row * Columns,
7681 ' ', (size_t)Columns * sizeof(schar_T));
7682#ifdef FEAT_MBYTE
7683 if (enc_utf8)
7684 {
7685 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
7686 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007687 for (i = 0; i < p_mco; ++i)
7688 (void)vim_memset(new_ScreenLinesC[i]
7689 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 0, (size_t)Columns * sizeof(u8char_T));
7691 }
7692 if (enc_dbcs == DBCS_JPNU)
7693 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
7694 0, (size_t)Columns * sizeof(schar_T));
7695#endif
7696 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
7697 0, (size_t)Columns * sizeof(sattr_T));
7698 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007699 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700 {
7701 if (screen_Columns < Columns)
7702 len = screen_Columns;
7703 else
7704 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007705#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00007706 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007707 * may be invalid now. Also when p_mco changes. */
7708 if (!(enc_utf8 && ScreenLinesUC == NULL)
7709 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007710#endif
7711 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
7712 ScreenLines + LineOffset[old_row],
7713 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007715 if (enc_utf8 && ScreenLinesUC != NULL
7716 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717 {
7718 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
7719 ScreenLinesUC + LineOffset[old_row],
7720 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007721 for (i = 0; i < p_mco; ++i)
7722 mch_memmove(new_ScreenLinesC[i]
7723 + new_LineOffset[new_row],
7724 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725 (size_t)len * sizeof(u8char_T));
7726 }
7727 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
7728 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
7729 ScreenLines2 + LineOffset[old_row],
7730 (size_t)len * sizeof(schar_T));
7731#endif
7732 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
7733 ScreenAttrs + LineOffset[old_row],
7734 (size_t)len * sizeof(sattr_T));
7735 }
7736 }
7737 }
7738 /* Use the last line of the screen for the current line. */
7739 current_ScreenLine = new_ScreenLines + Rows * Columns;
7740 }
7741
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007742 free_screenlines();
7743
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744 ScreenLines = new_ScreenLines;
7745#ifdef FEAT_MBYTE
7746 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007747 for (i = 0; i < p_mco; ++i)
7748 ScreenLinesC[i] = new_ScreenLinesC[i];
7749 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750 ScreenLines2 = new_ScreenLines2;
7751#endif
7752 ScreenAttrs = new_ScreenAttrs;
7753 LineOffset = new_LineOffset;
7754 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007755#ifdef FEAT_WINDOWS
7756 TabPageIdxs = new_TabPageIdxs;
7757#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758
7759 /* It's important that screen_Rows and screen_Columns reflect the actual
7760 * size of ScreenLines[]. Set them before calling anything. */
7761#ifdef FEAT_GUI
7762 old_Rows = screen_Rows;
7763#endif
7764 screen_Rows = Rows;
7765 screen_Columns = Columns;
7766
7767 must_redraw = CLEAR; /* need to clear the screen later */
7768 if (clear)
7769 screenclear2();
7770
7771#ifdef FEAT_GUI
7772 else if (gui.in_use
7773 && !gui.starting
7774 && ScreenLines != NULL
7775 && old_Rows != Rows)
7776 {
7777 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
7778 /*
7779 * Adjust the position of the cursor, for when executing an external
7780 * command.
7781 */
7782 if (msg_row >= Rows) /* Rows got smaller */
7783 msg_row = Rows - 1; /* put cursor at last row */
7784 else if (Rows > old_Rows) /* Rows got bigger */
7785 msg_row += Rows - old_Rows; /* put cursor in same place */
7786 if (msg_col >= Columns) /* Columns got smaller */
7787 msg_col = Columns - 1; /* put cursor at last column */
7788 }
7789#endif
7790
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00007792 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00007793
7794#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00007795 /*
7796 * Do not apply autocommands more than 3 times to avoid an endless loop
7797 * in case applying autocommands always changes Rows or Columns.
7798 */
7799 if (starting == 0 && ++retry_count <= 3)
7800 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00007801 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00007802 /* In rare cases, autocommands may have altered Rows or Columns,
7803 * jump back to check if we need to allocate the screen again. */
7804 goto retry;
7805 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00007806#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807}
7808
7809 void
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007810free_screenlines()
7811{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007812#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007813 int i;
7814
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007815 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007816 for (i = 0; i < Screen_mco; ++i)
7817 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007818 vim_free(ScreenLines2);
7819#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007820 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007821 vim_free(ScreenAttrs);
7822 vim_free(LineOffset);
7823 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007824#ifdef FEAT_WINDOWS
7825 vim_free(TabPageIdxs);
7826#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007827}
7828
7829 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830screenclear()
7831{
7832 check_for_delay(FALSE);
7833 screenalloc(FALSE); /* allocate screen buffers if size changed */
7834 screenclear2(); /* clear the screen */
7835}
7836
7837 static void
7838screenclear2()
7839{
7840 int i;
7841
7842 if (starting == NO_SCREEN || ScreenLines == NULL
7843#ifdef FEAT_GUI
7844 || (gui.in_use && gui.starting)
7845#endif
7846 )
7847 return;
7848
7849#ifdef FEAT_GUI
7850 if (!gui.in_use)
7851#endif
7852 screen_attr = -1; /* force setting the Normal colors */
7853 screen_stop_highlight(); /* don't want highlighting here */
7854
7855#ifdef FEAT_CLIPBOARD
7856 /* disable selection without redrawing it */
7857 clip_scroll_selection(9999);
7858#endif
7859
7860 /* blank out ScreenLines */
7861 for (i = 0; i < Rows; ++i)
7862 {
7863 lineclear(LineOffset[i], (int)Columns);
7864 LineWraps[i] = FALSE;
7865 }
7866
7867 if (can_clear(T_CL))
7868 {
7869 out_str(T_CL); /* clear the display */
7870 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007871 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872 }
7873 else
7874 {
7875 /* can't clear the screen, mark all chars with invalid attributes */
7876 for (i = 0; i < Rows; ++i)
7877 lineinvalid(LineOffset[i], (int)Columns);
7878 clear_cmdline = TRUE;
7879 }
7880
7881 screen_cleared = TRUE; /* can use contents of ScreenLines now */
7882
7883 win_rest_invalid(firstwin);
7884 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00007885#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00007886 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00007887#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 if (must_redraw == CLEAR) /* no need to clear again */
7889 must_redraw = NOT_VALID;
7890 compute_cmdrow();
7891 msg_row = cmdline_row; /* put cursor on last line for messages */
7892 msg_col = 0;
7893 screen_start(); /* don't know where cursor is now */
7894 msg_scrolled = 0; /* can't scroll back */
7895 msg_didany = FALSE;
7896 msg_didout = FALSE;
7897}
7898
7899/*
7900 * Clear one line in ScreenLines.
7901 */
7902 static void
7903lineclear(off, width)
7904 unsigned off;
7905 int width;
7906{
7907 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
7908#ifdef FEAT_MBYTE
7909 if (enc_utf8)
7910 (void)vim_memset(ScreenLinesUC + off, 0,
7911 (size_t)width * sizeof(u8char_T));
7912#endif
7913 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
7914}
7915
7916/*
7917 * Mark one line in ScreenLines invalid by setting the attributes to an
7918 * invalid value.
7919 */
7920 static void
7921lineinvalid(off, width)
7922 unsigned off;
7923 int width;
7924{
7925 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
7926}
7927
7928#ifdef FEAT_VERTSPLIT
7929/*
7930 * Copy part of a Screenline for vertically split window "wp".
7931 */
7932 static void
7933linecopy(to, from, wp)
7934 int to;
7935 int from;
7936 win_T *wp;
7937{
7938 unsigned off_to = LineOffset[to] + wp->w_wincol;
7939 unsigned off_from = LineOffset[from] + wp->w_wincol;
7940
7941 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
7942 wp->w_width * sizeof(schar_T));
7943# ifdef FEAT_MBYTE
7944 if (enc_utf8)
7945 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007946 int i;
7947
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
7949 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007950 for (i = 0; i < p_mco; ++i)
7951 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
7952 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953 }
7954 if (enc_dbcs == DBCS_JPNU)
7955 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
7956 wp->w_width * sizeof(schar_T));
7957# endif
7958 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
7959 wp->w_width * sizeof(sattr_T));
7960}
7961#endif
7962
7963/*
7964 * Return TRUE if clearing with term string "p" would work.
7965 * It can't work when the string is empty or it won't set the right background.
7966 */
7967 int
7968can_clear(p)
7969 char_u *p;
7970{
7971 return (*p != NUL && (t_colors <= 1
7972#ifdef FEAT_GUI
7973 || gui.in_use
7974#endif
7975 || cterm_normal_bg_color == 0 || *T_UT != NUL));
7976}
7977
7978/*
7979 * Reset cursor position. Use whenever cursor was moved because of outputting
7980 * something directly to the screen (shell commands) or a terminal control
7981 * code.
7982 */
7983 void
7984screen_start()
7985{
7986 screen_cur_row = screen_cur_col = 9999;
7987}
7988
7989/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990 * Move the cursor to position "row","col" in the screen.
7991 * This tries to find the most efficient way to move, minimizing the number of
7992 * characters sent to the terminal.
7993 */
7994 void
7995windgoto(row, col)
7996 int row;
7997 int col;
7998{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007999 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000 int i;
8001 int plan;
8002 int cost;
8003 int wouldbe_col;
8004 int noinvcurs;
8005 char_u *bs;
8006 int goto_cost;
8007 int attr;
8008
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008009#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
8011
8012#define PLAN_LE 1
8013#define PLAN_CR 2
8014#define PLAN_NL 3
8015#define PLAN_WRITE 4
8016 /* Can't use ScreenLines unless initialized */
8017 if (ScreenLines == NULL)
8018 return;
8019
8020 if (col != screen_cur_col || row != screen_cur_row)
8021 {
8022 /* Check for valid position. */
8023 if (row < 0) /* window without text lines? */
8024 row = 0;
8025 if (row >= screen_Rows)
8026 row = screen_Rows - 1;
8027 if (col >= screen_Columns)
8028 col = screen_Columns - 1;
8029
8030 /* check if no cursor movement is allowed in highlight mode */
8031 if (screen_attr && *T_MS == NUL)
8032 noinvcurs = HIGHL_COST;
8033 else
8034 noinvcurs = 0;
8035 goto_cost = GOTO_COST + noinvcurs;
8036
8037 /*
8038 * Plan how to do the positioning:
8039 * 1. Use CR to move it to column 0, same row.
8040 * 2. Use T_LE to move it a few columns to the left.
8041 * 3. Use NL to move a few lines down, column 0.
8042 * 4. Move a few columns to the right with T_ND or by writing chars.
8043 *
8044 * Don't do this if the cursor went beyond the last column, the cursor
8045 * position is unknown then (some terminals wrap, some don't )
8046 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008047 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00008048 * characters to move the cursor to the right.
8049 */
8050 if (row >= screen_cur_row && screen_cur_col < Columns)
8051 {
8052 /*
8053 * If the cursor is in the same row, bigger col, we can use CR
8054 * or T_LE.
8055 */
8056 bs = NULL; /* init for GCC */
8057 attr = screen_attr;
8058 if (row == screen_cur_row && col < screen_cur_col)
8059 {
8060 /* "le" is preferred over "bc", because "bc" is obsolete */
8061 if (*T_LE)
8062 bs = T_LE; /* "cursor left" */
8063 else
8064 bs = T_BC; /* "backspace character (old) */
8065 if (*bs)
8066 cost = (screen_cur_col - col) * (int)STRLEN(bs);
8067 else
8068 cost = 999;
8069 if (col + 1 < cost) /* using CR is less characters */
8070 {
8071 plan = PLAN_CR;
8072 wouldbe_col = 0;
8073 cost = 1; /* CR is just one character */
8074 }
8075 else
8076 {
8077 plan = PLAN_LE;
8078 wouldbe_col = col;
8079 }
8080 if (noinvcurs) /* will stop highlighting */
8081 {
8082 cost += noinvcurs;
8083 attr = 0;
8084 }
8085 }
8086
8087 /*
8088 * If the cursor is above where we want to be, we can use CR LF.
8089 */
8090 else if (row > screen_cur_row)
8091 {
8092 plan = PLAN_NL;
8093 wouldbe_col = 0;
8094 cost = (row - screen_cur_row) * 2; /* CR LF */
8095 if (noinvcurs) /* will stop highlighting */
8096 {
8097 cost += noinvcurs;
8098 attr = 0;
8099 }
8100 }
8101
8102 /*
8103 * If the cursor is in the same row, smaller col, just use write.
8104 */
8105 else
8106 {
8107 plan = PLAN_WRITE;
8108 wouldbe_col = screen_cur_col;
8109 cost = 0;
8110 }
8111
8112 /*
8113 * Check if any characters that need to be written have the
8114 * correct attributes. Also avoid UTF-8 characters.
8115 */
8116 i = col - wouldbe_col;
8117 if (i > 0)
8118 cost += i;
8119 if (cost < goto_cost && i > 0)
8120 {
8121 /*
8122 * Check if the attributes are correct without additionally
8123 * stopping highlighting.
8124 */
8125 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
8126 while (i && *p++ == attr)
8127 --i;
8128 if (i != 0)
8129 {
8130 /*
8131 * Try if it works when highlighting is stopped here.
8132 */
8133 if (*--p == 0)
8134 {
8135 cost += noinvcurs;
8136 while (i && *p++ == 0)
8137 --i;
8138 }
8139 if (i != 0)
8140 cost = 999; /* different attributes, don't do it */
8141 }
8142#ifdef FEAT_MBYTE
8143 if (enc_utf8)
8144 {
8145 /* Don't use an UTF-8 char for positioning, it's slow. */
8146 for (i = wouldbe_col; i < col; ++i)
8147 if (ScreenLinesUC[LineOffset[row] + i] != 0)
8148 {
8149 cost = 999;
8150 break;
8151 }
8152 }
8153#endif
8154 }
8155
8156 /*
8157 * We can do it without term_windgoto()!
8158 */
8159 if (cost < goto_cost)
8160 {
8161 if (plan == PLAN_LE)
8162 {
8163 if (noinvcurs)
8164 screen_stop_highlight();
8165 while (screen_cur_col > col)
8166 {
8167 out_str(bs);
8168 --screen_cur_col;
8169 }
8170 }
8171 else if (plan == PLAN_CR)
8172 {
8173 if (noinvcurs)
8174 screen_stop_highlight();
8175 out_char('\r');
8176 screen_cur_col = 0;
8177 }
8178 else if (plan == PLAN_NL)
8179 {
8180 if (noinvcurs)
8181 screen_stop_highlight();
8182 while (screen_cur_row < row)
8183 {
8184 out_char('\n');
8185 ++screen_cur_row;
8186 }
8187 screen_cur_col = 0;
8188 }
8189
8190 i = col - screen_cur_col;
8191 if (i > 0)
8192 {
8193 /*
8194 * Use cursor-right if it's one character only. Avoids
8195 * removing a line of pixels from the last bold char, when
8196 * using the bold trick in the GUI.
8197 */
8198 if (T_ND[0] != NUL && T_ND[1] == NUL)
8199 {
8200 while (i-- > 0)
8201 out_char(*T_ND);
8202 }
8203 else
8204 {
8205 int off;
8206
8207 off = LineOffset[row] + screen_cur_col;
8208 while (i-- > 0)
8209 {
8210 if (ScreenAttrs[off] != screen_attr)
8211 screen_stop_highlight();
8212#ifdef FEAT_MBYTE
8213 out_flush_check();
8214#endif
8215 out_char(ScreenLines[off]);
8216#ifdef FEAT_MBYTE
8217 if (enc_dbcs == DBCS_JPNU
8218 && ScreenLines[off] == 0x8e)
8219 out_char(ScreenLines2[off]);
8220#endif
8221 ++off;
8222 }
8223 }
8224 }
8225 }
8226 }
8227 else
8228 cost = 999;
8229
8230 if (cost >= goto_cost)
8231 {
8232 if (noinvcurs)
8233 screen_stop_highlight();
8234 if (row == screen_cur_row && (col > screen_cur_col) &&
8235 *T_CRI != NUL)
8236 term_cursor_right(col - screen_cur_col);
8237 else
8238 term_windgoto(row, col);
8239 }
8240 screen_cur_row = row;
8241 screen_cur_col = col;
8242 }
8243}
8244
8245/*
8246 * Set cursor to its position in the current window.
8247 */
8248 void
8249setcursor()
8250{
8251 if (redrawing())
8252 {
8253 validate_cursor();
8254 windgoto(W_WINROW(curwin) + curwin->w_wrow,
8255 W_WINCOL(curwin) + (
8256#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00008257 /* With 'rightleft' set and the cursor on a double-wide
8258 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
8260# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00008261 (has_mbyte
8262 && (*mb_ptr2cells)(ml_get_cursor()) == 2
8263 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264# endif
8265 1)) :
8266#endif
8267 curwin->w_wcol));
8268 }
8269}
8270
8271
8272/*
8273 * insert 'line_count' lines at 'row' in window 'wp'
8274 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
8275 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
8276 * scrolling.
8277 * Returns FAIL if the lines are not inserted, OK for success.
8278 */
8279 int
8280win_ins_lines(wp, row, line_count, invalid, mayclear)
8281 win_T *wp;
8282 int row;
8283 int line_count;
8284 int invalid;
8285 int mayclear;
8286{
8287 int did_delete;
8288 int nextrow;
8289 int lastrow;
8290 int retval;
8291
8292 if (invalid)
8293 wp->w_lines_valid = 0;
8294
8295 if (wp->w_height < 5)
8296 return FAIL;
8297
8298 if (line_count > wp->w_height - row)
8299 line_count = wp->w_height - row;
8300
8301 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
8302 if (retval != MAYBE)
8303 return retval;
8304
8305 /*
8306 * If there is a next window or a status line, we first try to delete the
8307 * lines at the bottom to avoid messing what is after the window.
8308 * If this fails and there are following windows, don't do anything to avoid
8309 * messing up those windows, better just redraw.
8310 */
8311 did_delete = FALSE;
8312#ifdef FEAT_WINDOWS
8313 if (wp->w_next != NULL || wp->w_status_height)
8314 {
8315 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8316 line_count, (int)Rows, FALSE, NULL) == OK)
8317 did_delete = TRUE;
8318 else if (wp->w_next)
8319 return FAIL;
8320 }
8321#endif
8322 /*
8323 * if no lines deleted, blank the lines that will end up below the window
8324 */
8325 if (!did_delete)
8326 {
8327#ifdef FEAT_WINDOWS
8328 wp->w_redr_status = TRUE;
8329#endif
8330 redraw_cmdline = TRUE;
8331 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
8332 lastrow = nextrow + line_count;
8333 if (lastrow > Rows)
8334 lastrow = Rows;
8335 screen_fill(nextrow - line_count, lastrow - line_count,
8336 W_WINCOL(wp), (int)W_ENDCOL(wp),
8337 ' ', ' ', 0);
8338 }
8339
8340 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
8341 == FAIL)
8342 {
8343 /* deletion will have messed up other windows */
8344 if (did_delete)
8345 {
8346#ifdef FEAT_WINDOWS
8347 wp->w_redr_status = TRUE;
8348#endif
8349 win_rest_invalid(W_NEXT(wp));
8350 }
8351 return FAIL;
8352 }
8353
8354 return OK;
8355}
8356
8357/*
8358 * delete "line_count" window lines at "row" in window "wp"
8359 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
8360 * If "mayclear" is TRUE the screen will be cleared if it is faster than
8361 * scrolling
8362 * Return OK for success, FAIL if the lines are not deleted.
8363 */
8364 int
8365win_del_lines(wp, row, line_count, invalid, mayclear)
8366 win_T *wp;
8367 int row;
8368 int line_count;
8369 int invalid;
8370 int mayclear;
8371{
8372 int retval;
8373
8374 if (invalid)
8375 wp->w_lines_valid = 0;
8376
8377 if (line_count > wp->w_height - row)
8378 line_count = wp->w_height - row;
8379
8380 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
8381 if (retval != MAYBE)
8382 return retval;
8383
8384 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
8385 (int)Rows, FALSE, NULL) == FAIL)
8386 return FAIL;
8387
8388#ifdef FEAT_WINDOWS
8389 /*
8390 * If there are windows or status lines below, try to put them at the
8391 * correct place. If we can't do that, they have to be redrawn.
8392 */
8393 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
8394 {
8395 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8396 line_count, (int)Rows, NULL) == FAIL)
8397 {
8398 wp->w_redr_status = TRUE;
8399 win_rest_invalid(wp->w_next);
8400 }
8401 }
8402 /*
8403 * If this is the last window and there is no status line, redraw the
8404 * command line later.
8405 */
8406 else
8407#endif
8408 redraw_cmdline = TRUE;
8409 return OK;
8410}
8411
8412/*
8413 * Common code for win_ins_lines() and win_del_lines().
8414 * Returns OK or FAIL when the work has been done.
8415 * Returns MAYBE when not finished yet.
8416 */
8417 static int
8418win_do_lines(wp, row, line_count, mayclear, del)
8419 win_T *wp;
8420 int row;
8421 int line_count;
8422 int mayclear;
8423 int del;
8424{
8425 int retval;
8426
8427 if (!redrawing() || line_count <= 0)
8428 return FAIL;
8429
8430 /* only a few lines left: redraw is faster */
8431 if (mayclear && Rows - line_count < 5
8432#ifdef FEAT_VERTSPLIT
8433 && wp->w_width == Columns
8434#endif
8435 )
8436 {
8437 screenclear(); /* will set wp->w_lines_valid to 0 */
8438 return FAIL;
8439 }
8440
8441 /*
8442 * Delete all remaining lines
8443 */
8444 if (row + line_count >= wp->w_height)
8445 {
8446 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
8447 W_WINCOL(wp), (int)W_ENDCOL(wp),
8448 ' ', ' ', 0);
8449 return OK;
8450 }
8451
8452 /*
8453 * when scrolling, the message on the command line should be cleared,
8454 * otherwise it will stay there forever.
8455 */
8456 clear_cmdline = TRUE;
8457
8458 /*
8459 * If the terminal can set a scroll region, use that.
8460 * Always do this in a vertically split window. This will redraw from
8461 * ScreenLines[] when t_CV isn't defined. That's faster than using
8462 * win_line().
8463 * Don't use a scroll region when we are going to redraw the text, writing
8464 * a character in the lower right corner of the scroll region causes a
8465 * scroll-up in the DJGPP version.
8466 */
8467 if (scroll_region
8468#ifdef FEAT_VERTSPLIT
8469 || W_WIDTH(wp) != Columns
8470#endif
8471 )
8472 {
8473#ifdef FEAT_VERTSPLIT
8474 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8475#endif
8476 scroll_region_set(wp, row);
8477 if (del)
8478 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
8479 wp->w_height - row, FALSE, wp);
8480 else
8481 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
8482 wp->w_height - row, wp);
8483#ifdef FEAT_VERTSPLIT
8484 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8485#endif
8486 scroll_region_reset();
8487 return retval;
8488 }
8489
8490#ifdef FEAT_WINDOWS
8491 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
8492 return FAIL;
8493#endif
8494
8495 return MAYBE;
8496}
8497
8498/*
8499 * window 'wp' and everything after it is messed up, mark it for redraw
8500 */
8501 static void
8502win_rest_invalid(wp)
8503 win_T *wp;
8504{
8505#ifdef FEAT_WINDOWS
8506 while (wp != NULL)
8507#else
8508 if (wp != NULL)
8509#endif
8510 {
8511 redraw_win_later(wp, NOT_VALID);
8512#ifdef FEAT_WINDOWS
8513 wp->w_redr_status = TRUE;
8514 wp = wp->w_next;
8515#endif
8516 }
8517 redraw_cmdline = TRUE;
8518}
8519
8520/*
8521 * The rest of the routines in this file perform screen manipulations. The
8522 * given operation is performed physically on the screen. The corresponding
8523 * change is also made to the internal screen image. In this way, the editor
8524 * anticipates the effect of editing changes on the appearance of the screen.
8525 * That way, when we call screenupdate a complete redraw isn't usually
8526 * necessary. Another advantage is that we can keep adding code to anticipate
8527 * screen changes, and in the meantime, everything still works.
8528 */
8529
8530/*
8531 * types for inserting or deleting lines
8532 */
8533#define USE_T_CAL 1
8534#define USE_T_CDL 2
8535#define USE_T_AL 3
8536#define USE_T_CE 4
8537#define USE_T_DL 5
8538#define USE_T_SR 6
8539#define USE_NL 7
8540#define USE_T_CD 8
8541#define USE_REDRAW 9
8542
8543/*
8544 * insert lines on the screen and update ScreenLines[]
8545 * 'end' is the line after the scrolled part. Normally it is Rows.
8546 * When scrolling region used 'off' is the offset from the top for the region.
8547 * 'row' and 'end' are relative to the start of the region.
8548 *
8549 * return FAIL for failure, OK for success.
8550 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008551 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552screen_ins_lines(off, row, line_count, end, wp)
8553 int off;
8554 int row;
8555 int line_count;
8556 int end;
8557 win_T *wp; /* NULL or window to use width from */
8558{
8559 int i;
8560 int j;
8561 unsigned temp;
8562 int cursor_row;
8563 int type;
8564 int result_empty;
8565 int can_ce = can_clear(T_CE);
8566
8567 /*
8568 * FAIL if
8569 * - there is no valid screen
8570 * - the screen has to be redrawn completely
8571 * - the line count is less than one
8572 * - the line count is more than 'ttyscroll'
8573 */
8574 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
8575 return FAIL;
8576
8577 /*
8578 * There are seven ways to insert lines:
8579 * 0. When in a vertically split window and t_CV isn't set, redraw the
8580 * characters from ScreenLines[].
8581 * 1. Use T_CD (clear to end of display) if it exists and the result of
8582 * the insert is just empty lines
8583 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
8584 * present or line_count > 1. It looks better if we do all the inserts
8585 * at once.
8586 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
8587 * insert is just empty lines and T_CE is not present or line_count >
8588 * 1.
8589 * 4. Use T_AL (insert line) if it exists.
8590 * 5. Use T_CE (erase line) if it exists and the result of the insert is
8591 * just empty lines.
8592 * 6. Use T_DL (delete line) if it exists and the result of the insert is
8593 * just empty lines.
8594 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
8595 * the 'da' flag is not set or we have clear line capability.
8596 * 8. redraw the characters from ScreenLines[].
8597 *
8598 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
8599 * the scrollbar for the window. It does have insert line, use that if it
8600 * exists.
8601 */
8602 result_empty = (row + line_count >= end);
8603#ifdef FEAT_VERTSPLIT
8604 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
8605 type = USE_REDRAW;
8606 else
8607#endif
8608 if (can_clear(T_CD) && result_empty)
8609 type = USE_T_CD;
8610 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
8611 type = USE_T_CAL;
8612 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
8613 type = USE_T_CDL;
8614 else if (*T_AL != NUL)
8615 type = USE_T_AL;
8616 else if (can_ce && result_empty)
8617 type = USE_T_CE;
8618 else if (*T_DL != NUL && result_empty)
8619 type = USE_T_DL;
8620 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
8621 type = USE_T_SR;
8622 else
8623 return FAIL;
8624
8625 /*
8626 * For clearing the lines screen_del_lines() is used. This will also take
8627 * care of t_db if necessary.
8628 */
8629 if (type == USE_T_CD || type == USE_T_CDL ||
8630 type == USE_T_CE || type == USE_T_DL)
8631 return screen_del_lines(off, row, line_count, end, FALSE, wp);
8632
8633 /*
8634 * If text is retained below the screen, first clear or delete as many
8635 * lines at the bottom of the window as are about to be inserted so that
8636 * the deleted lines won't later surface during a screen_del_lines.
8637 */
8638 if (*T_DB)
8639 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
8640
8641#ifdef FEAT_CLIPBOARD
8642 /* Remove a modeless selection when inserting lines halfway the screen
8643 * or not the full width of the screen. */
8644 if (off + row > 0
8645# ifdef FEAT_VERTSPLIT
8646 || (wp != NULL && wp->w_width != Columns)
8647# endif
8648 )
8649 clip_clear_selection();
8650 else
8651 clip_scroll_selection(-line_count);
8652#endif
8653
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654#ifdef FEAT_GUI
8655 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
8656 * scrolling is actually carried out. */
8657 gui_dont_update_cursor();
8658#endif
8659
8660 if (*T_CCS != NUL) /* cursor relative to region */
8661 cursor_row = row;
8662 else
8663 cursor_row = row + off;
8664
8665 /*
8666 * Shift LineOffset[] line_count down to reflect the inserted lines.
8667 * Clear the inserted lines in ScreenLines[].
8668 */
8669 row += off;
8670 end += off;
8671 for (i = 0; i < line_count; ++i)
8672 {
8673#ifdef FEAT_VERTSPLIT
8674 if (wp != NULL && wp->w_width != Columns)
8675 {
8676 /* need to copy part of a line */
8677 j = end - 1 - i;
8678 while ((j -= line_count) >= row)
8679 linecopy(j + line_count, j, wp);
8680 j += line_count;
8681 if (can_clear((char_u *)" "))
8682 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
8683 else
8684 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
8685 LineWraps[j] = FALSE;
8686 }
8687 else
8688#endif
8689 {
8690 j = end - 1 - i;
8691 temp = LineOffset[j];
8692 while ((j -= line_count) >= row)
8693 {
8694 LineOffset[j + line_count] = LineOffset[j];
8695 LineWraps[j + line_count] = LineWraps[j];
8696 }
8697 LineOffset[j + line_count] = temp;
8698 LineWraps[j + line_count] = FALSE;
8699 if (can_clear((char_u *)" "))
8700 lineclear(temp, (int)Columns);
8701 else
8702 lineinvalid(temp, (int)Columns);
8703 }
8704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705
8706 screen_stop_highlight();
8707 windgoto(cursor_row, 0);
8708
8709#ifdef FEAT_VERTSPLIT
8710 /* redraw the characters */
8711 if (type == USE_REDRAW)
8712 redraw_block(row, end, wp);
8713 else
8714#endif
8715 if (type == USE_T_CAL)
8716 {
8717 term_append_lines(line_count);
8718 screen_start(); /* don't know where cursor is now */
8719 }
8720 else
8721 {
8722 for (i = 0; i < line_count; i++)
8723 {
8724 if (type == USE_T_AL)
8725 {
8726 if (i && cursor_row != 0)
8727 windgoto(cursor_row, 0);
8728 out_str(T_AL);
8729 }
8730 else /* type == USE_T_SR */
8731 out_str(T_SR);
8732 screen_start(); /* don't know where cursor is now */
8733 }
8734 }
8735
8736 /*
8737 * With scroll-reverse and 'da' flag set we need to clear the lines that
8738 * have been scrolled down into the region.
8739 */
8740 if (type == USE_T_SR && *T_DA)
8741 {
8742 for (i = 0; i < line_count; ++i)
8743 {
8744 windgoto(off + i, 0);
8745 out_str(T_CE);
8746 screen_start(); /* don't know where cursor is now */
8747 }
8748 }
8749
8750#ifdef FEAT_GUI
8751 gui_can_update_cursor();
8752 if (gui.in_use)
8753 out_flush(); /* always flush after a scroll */
8754#endif
8755 return OK;
8756}
8757
8758/*
8759 * delete lines on the screen and update ScreenLines[]
8760 * 'end' is the line after the scrolled part. Normally it is Rows.
8761 * When scrolling region used 'off' is the offset from the top for the region.
8762 * 'row' and 'end' are relative to the start of the region.
8763 *
8764 * Return OK for success, FAIL if the lines are not deleted.
8765 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008766 int
8767screen_del_lines(off, row, line_count, end, force, wp)
8768 int off;
8769 int row;
8770 int line_count;
8771 int end;
8772 int force; /* even when line_count > p_ttyscroll */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00008773 win_T *wp UNUSED; /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774{
8775 int j;
8776 int i;
8777 unsigned temp;
8778 int cursor_row;
8779 int cursor_end;
8780 int result_empty; /* result is empty until end of region */
8781 int can_delete; /* deleting line codes can be used */
8782 int type;
8783
8784 /*
8785 * FAIL if
8786 * - there is no valid screen
8787 * - the screen has to be redrawn completely
8788 * - the line count is less than one
8789 * - the line count is more than 'ttyscroll'
8790 */
8791 if (!screen_valid(TRUE) || line_count <= 0 ||
8792 (!force && line_count > p_ttyscroll))
8793 return FAIL;
8794
8795 /*
8796 * Check if the rest of the current region will become empty.
8797 */
8798 result_empty = row + line_count >= end;
8799
8800 /*
8801 * We can delete lines only when 'db' flag not set or when 'ce' option
8802 * available.
8803 */
8804 can_delete = (*T_DB == NUL || can_clear(T_CE));
8805
8806 /*
8807 * There are six ways to delete lines:
8808 * 0. When in a vertically split window and t_CV isn't set, redraw the
8809 * characters from ScreenLines[].
8810 * 1. Use T_CD if it exists and the result is empty.
8811 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
8812 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
8813 * none of the other ways work.
8814 * 4. Use T_CE (erase line) if the result is empty.
8815 * 5. Use T_DL (delete line) if it exists.
8816 * 6. redraw the characters from ScreenLines[].
8817 */
8818#ifdef FEAT_VERTSPLIT
8819 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
8820 type = USE_REDRAW;
8821 else
8822#endif
8823 if (can_clear(T_CD) && result_empty)
8824 type = USE_T_CD;
8825#if defined(__BEOS__) && defined(BEOS_DR8)
8826 /*
8827 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
8828 * its internal termcap... this works okay for tests which test *T_DB !=
8829 * NUL. It has the disadvantage that the user cannot use any :set t_*
8830 * command to get T_DB (back) to empty_option, only :set term=... will do
8831 * the trick...
8832 * Anyway, this hack will hopefully go away with the next OS release.
8833 * (Olaf Seibert)
8834 */
8835 else if (row == 0 && T_DB == empty_option
8836 && (line_count == 1 || *T_CDL == NUL))
8837#else
8838 else if (row == 0 && (
8839#ifndef AMIGA
8840 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
8841 * up, so use delete-line command */
8842 line_count == 1 ||
8843#endif
8844 *T_CDL == NUL))
8845#endif
8846 type = USE_NL;
8847 else if (*T_CDL != NUL && line_count > 1 && can_delete)
8848 type = USE_T_CDL;
8849 else if (can_clear(T_CE) && result_empty
8850#ifdef FEAT_VERTSPLIT
8851 && (wp == NULL || wp->w_width == Columns)
8852#endif
8853 )
8854 type = USE_T_CE;
8855 else if (*T_DL != NUL && can_delete)
8856 type = USE_T_DL;
8857 else if (*T_CDL != NUL && can_delete)
8858 type = USE_T_CDL;
8859 else
8860 return FAIL;
8861
8862#ifdef FEAT_CLIPBOARD
8863 /* Remove a modeless selection when deleting lines halfway the screen or
8864 * not the full width of the screen. */
8865 if (off + row > 0
8866# ifdef FEAT_VERTSPLIT
8867 || (wp != NULL && wp->w_width != Columns)
8868# endif
8869 )
8870 clip_clear_selection();
8871 else
8872 clip_scroll_selection(line_count);
8873#endif
8874
Bram Moolenaar071d4272004-06-13 20:20:40 +00008875#ifdef FEAT_GUI
8876 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
8877 * scrolling is actually carried out. */
8878 gui_dont_update_cursor();
8879#endif
8880
8881 if (*T_CCS != NUL) /* cursor relative to region */
8882 {
8883 cursor_row = row;
8884 cursor_end = end;
8885 }
8886 else
8887 {
8888 cursor_row = row + off;
8889 cursor_end = end + off;
8890 }
8891
8892 /*
8893 * Now shift LineOffset[] line_count up to reflect the deleted lines.
8894 * Clear the inserted lines in ScreenLines[].
8895 */
8896 row += off;
8897 end += off;
8898 for (i = 0; i < line_count; ++i)
8899 {
8900#ifdef FEAT_VERTSPLIT
8901 if (wp != NULL && wp->w_width != Columns)
8902 {
8903 /* need to copy part of a line */
8904 j = row + i;
8905 while ((j += line_count) <= end - 1)
8906 linecopy(j - line_count, j, wp);
8907 j -= line_count;
8908 if (can_clear((char_u *)" "))
8909 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
8910 else
8911 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
8912 LineWraps[j] = FALSE;
8913 }
8914 else
8915#endif
8916 {
8917 /* whole width, moving the line pointers is faster */
8918 j = row + i;
8919 temp = LineOffset[j];
8920 while ((j += line_count) <= end - 1)
8921 {
8922 LineOffset[j - line_count] = LineOffset[j];
8923 LineWraps[j - line_count] = LineWraps[j];
8924 }
8925 LineOffset[j - line_count] = temp;
8926 LineWraps[j - line_count] = FALSE;
8927 if (can_clear((char_u *)" "))
8928 lineclear(temp, (int)Columns);
8929 else
8930 lineinvalid(temp, (int)Columns);
8931 }
8932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933
8934 screen_stop_highlight();
8935
8936#ifdef FEAT_VERTSPLIT
8937 /* redraw the characters */
8938 if (type == USE_REDRAW)
8939 redraw_block(row, end, wp);
8940 else
8941#endif
8942 if (type == USE_T_CD) /* delete the lines */
8943 {
8944 windgoto(cursor_row, 0);
8945 out_str(T_CD);
8946 screen_start(); /* don't know where cursor is now */
8947 }
8948 else if (type == USE_T_CDL)
8949 {
8950 windgoto(cursor_row, 0);
8951 term_delete_lines(line_count);
8952 screen_start(); /* don't know where cursor is now */
8953 }
8954 /*
8955 * Deleting lines at top of the screen or scroll region: Just scroll
8956 * the whole screen (scroll region) up by outputting newlines on the
8957 * last line.
8958 */
8959 else if (type == USE_NL)
8960 {
8961 windgoto(cursor_end - 1, 0);
8962 for (i = line_count; --i >= 0; )
8963 out_char('\n'); /* cursor will remain on same line */
8964 }
8965 else
8966 {
8967 for (i = line_count; --i >= 0; )
8968 {
8969 if (type == USE_T_DL)
8970 {
8971 windgoto(cursor_row, 0);
8972 out_str(T_DL); /* delete a line */
8973 }
8974 else /* type == USE_T_CE */
8975 {
8976 windgoto(cursor_row + i, 0);
8977 out_str(T_CE); /* erase a line */
8978 }
8979 screen_start(); /* don't know where cursor is now */
8980 }
8981 }
8982
8983 /*
8984 * If the 'db' flag is set, we need to clear the lines that have been
8985 * scrolled up at the bottom of the region.
8986 */
8987 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
8988 {
8989 for (i = line_count; i > 0; --i)
8990 {
8991 windgoto(cursor_end - i, 0);
8992 out_str(T_CE); /* erase a line */
8993 screen_start(); /* don't know where cursor is now */
8994 }
8995 }
8996
8997#ifdef FEAT_GUI
8998 gui_can_update_cursor();
8999 if (gui.in_use)
9000 out_flush(); /* always flush after a scroll */
9001#endif
9002
9003 return OK;
9004}
9005
9006/*
9007 * show the current mode and ruler
9008 *
9009 * If clear_cmdline is TRUE, clear the rest of the cmdline.
9010 * If clear_cmdline is FALSE there may be a message there that needs to be
9011 * cleared only if a mode is shown.
9012 * Return the length of the message (0 if no message).
9013 */
9014 int
9015showmode()
9016{
9017 int need_clear;
9018 int length = 0;
9019 int do_mode;
9020 int attr;
9021 int nwr_save;
9022#ifdef FEAT_INS_EXPAND
9023 int sub_attr;
9024#endif
9025
Bram Moolenaar7df351e2006-01-23 22:30:28 +00009026 do_mode = ((p_smd && msg_silent == 0)
9027 && ((State & INSERT)
9028 || restart_edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00009029#ifdef FEAT_VISUAL
9030 || VIsual_active
9031#endif
9032 ));
9033 if (do_mode || Recording)
9034 {
9035 /*
9036 * Don't show mode right now, when not redrawing or inside a mapping.
9037 * Call char_avail() only when we are going to show something, because
9038 * it takes a bit of time.
9039 */
9040 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
9041 {
9042 redraw_cmdline = TRUE; /* show mode later */
9043 return 0;
9044 }
9045
9046 nwr_save = need_wait_return;
9047
9048 /* wait a bit before overwriting an important message */
9049 check_for_delay(FALSE);
9050
9051 /* if the cmdline is more than one line high, erase top lines */
9052 need_clear = clear_cmdline;
9053 if (clear_cmdline && cmdline_row < Rows - 1)
9054 msg_clr_cmdline(); /* will reset clear_cmdline */
9055
9056 /* Position on the last line in the window, column 0 */
9057 msg_pos_mode();
9058 cursor_off();
9059 attr = hl_attr(HLF_CM); /* Highlight mode */
9060 if (do_mode)
9061 {
9062 MSG_PUTS_ATTR("--", attr);
9063#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00009064# if 0 /* old version, changed by SungHyun Nam July 2008 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065 if (xic != NULL && im_get_status() && !p_imdisable
9066 && curbuf->b_p_iminsert == B_IMODE_IM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00009067# else
9068 if (
9069# ifdef HAVE_GTK2
9070 preedit_get_status()
9071# else
9072 im_get_status()
9073# endif
9074 )
9075# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009076# ifdef HAVE_GTK2 /* most of the time, it's not XIM being used */
9077 MSG_PUTS_ATTR(" IM", attr);
9078# else
9079 MSG_PUTS_ATTR(" XIM", attr);
9080# endif
9081#endif
9082#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
9083 if (gui.in_use)
9084 {
9085 if (hangul_input_state_get())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009086 MSG_PUTS_ATTR(" \307\321\261\333", attr); /* HANGUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087 }
9088#endif
9089#ifdef FEAT_INS_EXPAND
9090 if (edit_submode != NULL) /* CTRL-X in Insert mode */
9091 {
9092 /* These messages can get long, avoid a wrap in a narrow
9093 * window. Prefer showing edit_submode_extra. */
9094 length = (Rows - msg_row) * Columns - 3;
9095 if (edit_submode_extra != NULL)
9096 length -= vim_strsize(edit_submode_extra);
9097 if (length > 0)
9098 {
9099 if (edit_submode_pre != NULL)
9100 length -= vim_strsize(edit_submode_pre);
9101 if (length - vim_strsize(edit_submode) > 0)
9102 {
9103 if (edit_submode_pre != NULL)
9104 msg_puts_attr(edit_submode_pre, attr);
9105 msg_puts_attr(edit_submode, attr);
9106 }
9107 if (edit_submode_extra != NULL)
9108 {
9109 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
9110 if ((int)edit_submode_highl < (int)HLF_COUNT)
9111 sub_attr = hl_attr(edit_submode_highl);
9112 else
9113 sub_attr = attr;
9114 msg_puts_attr(edit_submode_extra, sub_attr);
9115 }
9116 }
9117 length = 0;
9118 }
9119 else
9120#endif
9121 {
9122#ifdef FEAT_VREPLACE
9123 if (State & VREPLACE_FLAG)
9124 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
9125 else
9126#endif
9127 if (State & REPLACE_FLAG)
9128 MSG_PUTS_ATTR(_(" REPLACE"), attr);
9129 else if (State & INSERT)
9130 {
9131#ifdef FEAT_RIGHTLEFT
9132 if (p_ri)
9133 MSG_PUTS_ATTR(_(" REVERSE"), attr);
9134#endif
9135 MSG_PUTS_ATTR(_(" INSERT"), attr);
9136 }
9137 else if (restart_edit == 'I')
9138 MSG_PUTS_ATTR(_(" (insert)"), attr);
9139 else if (restart_edit == 'R')
9140 MSG_PUTS_ATTR(_(" (replace)"), attr);
9141 else if (restart_edit == 'V')
9142 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
9143#ifdef FEAT_RIGHTLEFT
9144 if (p_hkmap)
9145 MSG_PUTS_ATTR(_(" Hebrew"), attr);
9146# ifdef FEAT_FKMAP
9147 if (p_fkmap)
9148 MSG_PUTS_ATTR(farsi_text_5, attr);
9149# endif
9150#endif
9151#ifdef FEAT_KEYMAP
9152 if (State & LANGMAP)
9153 {
9154# ifdef FEAT_ARABIC
9155 if (curwin->w_p_arab)
9156 MSG_PUTS_ATTR(_(" Arabic"), attr);
9157 else
9158# endif
9159 MSG_PUTS_ATTR(_(" (lang)"), attr);
9160 }
9161#endif
9162 if ((State & INSERT) && p_paste)
9163 MSG_PUTS_ATTR(_(" (paste)"), attr);
9164
9165#ifdef FEAT_VISUAL
9166 if (VIsual_active)
9167 {
9168 char *p;
9169
9170 /* Don't concatenate separate words to avoid translation
9171 * problems. */
9172 switch ((VIsual_select ? 4 : 0)
9173 + (VIsual_mode == Ctrl_V) * 2
9174 + (VIsual_mode == 'V'))
9175 {
9176 case 0: p = N_(" VISUAL"); break;
9177 case 1: p = N_(" VISUAL LINE"); break;
9178 case 2: p = N_(" VISUAL BLOCK"); break;
9179 case 4: p = N_(" SELECT"); break;
9180 case 5: p = N_(" SELECT LINE"); break;
9181 default: p = N_(" SELECT BLOCK"); break;
9182 }
9183 MSG_PUTS_ATTR(_(p), attr);
9184 }
9185#endif
9186 MSG_PUTS_ATTR(" --", attr);
9187 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009188
Bram Moolenaar071d4272004-06-13 20:20:40 +00009189 need_clear = TRUE;
9190 }
9191 if (Recording
9192#ifdef FEAT_INS_EXPAND
9193 && edit_submode == NULL /* otherwise it gets too long */
9194#endif
9195 )
9196 {
9197 MSG_PUTS_ATTR(_("recording"), attr);
9198 need_clear = TRUE;
9199 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009200
9201 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202 if (need_clear || clear_cmdline)
9203 msg_clr_eos();
9204 msg_didout = FALSE; /* overwrite this message */
9205 length = msg_col;
9206 msg_col = 0;
9207 need_wait_return = nwr_save; /* never ask for hit-return for this */
9208 }
9209 else if (clear_cmdline && msg_silent == 0)
9210 /* Clear the whole command line. Will reset "clear_cmdline". */
9211 msg_clr_cmdline();
9212
9213#ifdef FEAT_CMDL_INFO
9214# ifdef FEAT_VISUAL
9215 /* In Visual mode the size of the selected area must be redrawn. */
9216 if (VIsual_active)
9217 clear_showcmd();
9218# endif
9219
9220 /* If the last window has no status line, the ruler is after the mode
9221 * message and must be redrawn */
9222 if (redrawing()
9223# ifdef FEAT_WINDOWS
9224 && lastwin->w_status_height == 0
9225# endif
9226 )
9227 win_redr_ruler(lastwin, TRUE);
9228#endif
9229 redraw_cmdline = FALSE;
9230 clear_cmdline = FALSE;
9231
9232 return length;
9233}
9234
9235/*
9236 * Position for a mode message.
9237 */
9238 static void
9239msg_pos_mode()
9240{
9241 msg_col = 0;
9242 msg_row = Rows - 1;
9243}
9244
9245/*
9246 * Delete mode message. Used when ESC is typed which is expected to end
9247 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009248 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009249 */
9250 void
9251unshowmode(force)
9252 int force;
9253{
9254 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +01009255 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009256 */
9257 if (!redrawing() || (!force && char_avail() && !KeyTyped))
9258 redraw_cmdline = TRUE; /* delete mode later */
9259 else
9260 {
9261 msg_pos_mode();
9262 if (Recording)
9263 MSG_PUTS_ATTR(_("recording"), hl_attr(HLF_CM));
9264 msg_clr_eos();
9265 }
9266}
9267
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009268#if defined(FEAT_WINDOWS)
9269/*
9270 * Draw the tab pages line at the top of the Vim window.
9271 */
9272 static void
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009273draw_tabline()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009274{
9275 int tabcount = 0;
9276 tabpage_T *tp;
9277 int tabwidth;
9278 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009279 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009280 int attr;
9281 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009282 win_T *cwp;
9283 int wincount;
9284 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009285 int c;
9286 int len;
9287 int attr_sel = hl_attr(HLF_TPS);
9288 int attr_nosel = hl_attr(HLF_TP);
9289 int attr_fill = hl_attr(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009290 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009291 int room;
9292 int use_sep_chars = (t_colors < 8
9293#ifdef FEAT_GUI
9294 && !gui.in_use
9295#endif
9296 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009297
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009298 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009299
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009300#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +00009301 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009302 if (gui_use_tabline())
9303 {
9304 gui_update_tabline();
9305 return;
9306 }
9307#endif
9308
9309 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009310 return;
9311
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009312#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009313
9314 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
9315 for (scol = 0; scol < Columns; ++scol)
9316 TabPageIdxs[scol] = 0;
9317
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009318 /* Use the 'tabline' option if it's set. */
9319 if (*p_tal != NUL)
9320 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009321 int save_called_emsg = called_emsg;
9322
9323 /* Check for an error. If there is one we would loop in redrawing the
9324 * screen. Avoid that by making 'tabline' empty. */
9325 called_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009326 win_redr_custom(NULL, FALSE);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009327 if (called_emsg)
9328 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00009329 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009330 called_emsg |= save_called_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009331 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00009332 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009333#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009334 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009335 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
9336 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009337
Bram Moolenaar238a5642006-02-21 22:12:05 +00009338 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
9339 if (tabwidth < 6)
9340 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009341
Bram Moolenaar238a5642006-02-21 22:12:05 +00009342 attr = attr_nosel;
9343 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009344 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009345 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
9346 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00009347 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009348 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009349
Bram Moolenaar238a5642006-02-21 22:12:05 +00009350 if (tp->tp_topframe == topframe)
9351 attr = attr_sel;
9352 if (use_sep_chars && col > 0)
9353 screen_putchar('|', 0, col++, attr);
9354
9355 if (tp->tp_topframe != topframe)
9356 attr = attr_nosel;
9357
9358 screen_putchar(' ', 0, col++, attr);
9359
9360 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00009361 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009362 cwp = curwin;
9363 wp = firstwin;
9364 }
9365 else
9366 {
9367 cwp = tp->tp_curwin;
9368 wp = tp->tp_firstwin;
9369 }
9370
9371 modified = FALSE;
9372 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
9373 if (bufIsChanged(wp->w_buffer))
9374 modified = TRUE;
9375 if (modified || wincount > 1)
9376 {
9377 if (wincount > 1)
9378 {
9379 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009380 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009381 if (col + len >= Columns - 3)
9382 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +00009383 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009384#if defined(FEAT_SYN_HL)
Bram Moolenaar238a5642006-02-21 22:12:05 +00009385 hl_combine_attr(attr, hl_attr(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009386#else
Bram Moolenaar238a5642006-02-21 22:12:05 +00009387 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009388#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00009389 );
9390 col += len;
9391 }
9392 if (modified)
9393 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
9394 screen_putchar(' ', 0, col++, attr);
9395 }
9396
9397 room = scol - col + tabwidth - 1;
9398 if (room > 0)
9399 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009400 /* Get buffer name in NameBuff[] */
9401 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009402 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009403 len = vim_strsize(NameBuff);
9404 p = NameBuff;
9405#ifdef FEAT_MBYTE
9406 if (has_mbyte)
9407 while (len > room)
9408 {
9409 len -= ptr2cells(p);
9410 mb_ptr_adv(p);
9411 }
9412 else
9413#endif
9414 if (len > room)
9415 {
9416 p += len - room;
9417 len = room;
9418 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009419 if (len > Columns - col - 1)
9420 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +00009421
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009422 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009423 col += len;
9424 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00009425 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009426
9427 /* Store the tab page number in TabPageIdxs[], so that
9428 * jump_to_mouse() knows where each one is. */
9429 ++tabcount;
9430 while (scol < col)
9431 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009432 }
9433
Bram Moolenaar238a5642006-02-21 22:12:05 +00009434 if (use_sep_chars)
9435 c = '_';
9436 else
9437 c = ' ';
9438 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009439
9440 /* Put an "X" for closing the current tab if there are several. */
9441 if (first_tabpage->tp_next != NULL)
9442 {
9443 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
9444 TabPageIdxs[Columns - 1] = -999;
9445 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009446 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +00009447
9448 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
9449 * set. */
9450 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009451}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009452
9453/*
9454 * Get buffer name for "buf" into NameBuff[].
9455 * Takes care of special buffer names and translates special characters.
9456 */
9457 void
9458get_trans_bufname(buf)
9459 buf_T *buf;
9460{
9461 if (buf_spname(buf) != NULL)
9462 STRCPY(NameBuff, buf_spname(buf));
9463 else
9464 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
9465 trans_characters(NameBuff, MAXPATHL);
9466}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009467#endif
9468
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
9470/*
9471 * Get the character to use in a status line. Get its attributes in "*attr".
9472 */
9473 static int
9474fillchar_status(attr, is_curwin)
9475 int *attr;
9476 int is_curwin;
9477{
9478 int fill;
9479 if (is_curwin)
9480 {
9481 *attr = hl_attr(HLF_S);
9482 fill = fill_stl;
9483 }
9484 else
9485 {
9486 *attr = hl_attr(HLF_SNC);
9487 fill = fill_stlnc;
9488 }
9489 /* Use fill when there is highlighting, and highlighting of current
9490 * window differs, or the fillchars differ, or this is not the
9491 * current window */
9492 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
9493 || !is_curwin || firstwin == lastwin)
9494 || (fill_stl != fill_stlnc)))
9495 return fill;
9496 if (is_curwin)
9497 return '^';
9498 return '=';
9499}
9500#endif
9501
9502#ifdef FEAT_VERTSPLIT
9503/*
9504 * Get the character to use in a separator between vertically split windows.
9505 * Get its attributes in "*attr".
9506 */
9507 static int
9508fillchar_vsep(attr)
9509 int *attr;
9510{
9511 *attr = hl_attr(HLF_C);
9512 if (*attr == 0 && fill_vert == ' ')
9513 return '|';
9514 else
9515 return fill_vert;
9516}
9517#endif
9518
9519/*
9520 * Return TRUE if redrawing should currently be done.
9521 */
9522 int
9523redrawing()
9524{
9525 return (!RedrawingDisabled
9526 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
9527}
9528
9529/*
9530 * Return TRUE if printing messages should currently be done.
9531 */
9532 int
9533messaging()
9534{
9535 return (!(p_lz && char_avail() && !KeyTyped));
9536}
9537
9538/*
9539 * Show current status info in ruler and various other places
9540 * If always is FALSE, only show ruler if position has changed.
9541 */
9542 void
9543showruler(always)
9544 int always;
9545{
9546 if (!always && !redrawing())
9547 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +00009548#ifdef FEAT_INS_EXPAND
9549 if (pum_visible())
9550 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00009551# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +00009552 /* Don't redraw right now, do it later. */
9553 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00009554# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +00009555 return;
9556 }
9557#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00009559 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +00009560 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00009561 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009563 else
9564#endif
9565#ifdef FEAT_CMDL_INFO
9566 win_redr_ruler(curwin, always);
9567#endif
9568
9569#ifdef FEAT_TITLE
9570 if (need_maketitle
9571# ifdef FEAT_STL_OPT
9572 || (p_icon && (stl_syntax & STL_IN_ICON))
9573 || (p_title && (stl_syntax & STL_IN_TITLE))
9574# endif
9575 )
9576 maketitle();
9577#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +00009578#ifdef FEAT_WINDOWS
9579 /* Redraw the tab pages line if needed. */
9580 if (redraw_tabline)
9581 draw_tabline();
9582#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009583}
9584
9585#ifdef FEAT_CMDL_INFO
9586 static void
9587win_redr_ruler(wp, always)
9588 win_T *wp;
9589 int always;
9590{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00009591#define RULER_BUF_LEN 70
9592 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009593 int row;
9594 int fillchar;
9595 int attr;
9596 int empty_line = FALSE;
9597 colnr_T virtcol;
9598 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00009599 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009600 int o;
9601#ifdef FEAT_VERTSPLIT
9602 int this_ru_col;
9603 int off = 0;
9604 int width = Columns;
9605# define WITH_OFF(x) x
9606# define WITH_WIDTH(x) x
9607#else
9608# define WITH_OFF(x) 0
9609# define WITH_WIDTH(x) Columns
9610# define this_ru_col ru_col
9611#endif
9612
9613 /* If 'ruler' off or redrawing disabled, don't do anything */
9614 if (!p_ru)
9615 return;
9616
9617 /*
9618 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
9619 * after deleting lines, before cursor.lnum is corrected.
9620 */
9621 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
9622 return;
9623
9624#ifdef FEAT_INS_EXPAND
9625 /* Don't draw the ruler while doing insert-completion, it might overwrite
9626 * the (long) mode message. */
9627# ifdef FEAT_WINDOWS
9628 if (wp == lastwin && lastwin->w_status_height == 0)
9629# endif
9630 if (edit_submode != NULL)
9631 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00009632 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
9633 if (pum_visible())
9634 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009635#endif
9636
9637#ifdef FEAT_STL_OPT
9638 if (*p_ruf)
9639 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009640 int save_called_emsg = called_emsg;
9641
9642 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009643 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009644 if (called_emsg)
9645 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00009646 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009647 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009648 return;
9649 }
9650#endif
9651
9652 /*
9653 * Check if not in Insert mode and the line is empty (will show "0-1").
9654 */
9655 if (!(State & INSERT)
9656 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
9657 empty_line = TRUE;
9658
9659 /*
9660 * Only draw the ruler when something changed.
9661 */
9662 validate_virtcol_win(wp);
9663 if ( redraw_cmdline
9664 || always
9665 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
9666 || wp->w_cursor.col != wp->w_ru_cursor.col
9667 || wp->w_virtcol != wp->w_ru_virtcol
9668#ifdef FEAT_VIRTUALEDIT
9669 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
9670#endif
9671 || wp->w_topline != wp->w_ru_topline
9672 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
9673#ifdef FEAT_DIFF
9674 || wp->w_topfill != wp->w_ru_topfill
9675#endif
9676 || empty_line != wp->w_ru_empty)
9677 {
9678 cursor_off();
9679#ifdef FEAT_WINDOWS
9680 if (wp->w_status_height)
9681 {
9682 row = W_WINROW(wp) + wp->w_height;
9683 fillchar = fillchar_status(&attr, wp == curwin);
9684# ifdef FEAT_VERTSPLIT
9685 off = W_WINCOL(wp);
9686 width = W_WIDTH(wp);
9687# endif
9688 }
9689 else
9690#endif
9691 {
9692 row = Rows - 1;
9693 fillchar = ' ';
9694 attr = 0;
9695#ifdef FEAT_VERTSPLIT
9696 width = Columns;
9697 off = 0;
9698#endif
9699 }
9700
9701 /* In list mode virtcol needs to be recomputed */
9702 virtcol = wp->w_virtcol;
9703 if (wp->w_p_list && lcs_tab1 == NUL)
9704 {
9705 wp->w_p_list = FALSE;
9706 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
9707 wp->w_p_list = TRUE;
9708 }
9709
9710 /*
9711 * Some sprintfs return the length, some return a pointer.
9712 * To avoid portability problems we use strlen() here.
9713 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00009714 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
9716 ? 0L
9717 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00009718 len = STRLEN(buffer);
9719 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009720 empty_line ? 0 : (int)wp->w_cursor.col + 1,
9721 (int)virtcol + 1);
9722
9723 /*
9724 * Add a "50%" if there is room for it.
9725 * On the last line, don't print in the last column (scrolls the
9726 * screen up on some terminals).
9727 */
9728 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00009729 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730 o = i + vim_strsize(buffer + i + 1);
9731#ifdef FEAT_WINDOWS
9732 if (wp->w_status_height == 0) /* can't use last char of screen */
9733#endif
9734 ++o;
9735#ifdef FEAT_VERTSPLIT
9736 this_ru_col = ru_col - (Columns - width);
9737 if (this_ru_col < 0)
9738 this_ru_col = 0;
9739#endif
9740 /* Never use more than half the window/screen width, leave the other
9741 * half for the filename. */
9742 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
9743 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
9744 if (this_ru_col + o < WITH_WIDTH(width))
9745 {
9746 while (this_ru_col + o < WITH_WIDTH(width))
9747 {
9748#ifdef FEAT_MBYTE
9749 if (has_mbyte)
9750 i += (*mb_char2bytes)(fillchar, buffer + i);
9751 else
9752#endif
9753 buffer[i++] = fillchar;
9754 ++o;
9755 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00009756 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757 }
9758 /* Truncate at window boundary. */
9759#ifdef FEAT_MBYTE
9760 if (has_mbyte)
9761 {
9762 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009763 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009764 {
9765 o += (*mb_ptr2cells)(buffer + i);
9766 if (this_ru_col + o > WITH_WIDTH(width))
9767 {
9768 buffer[i] = NUL;
9769 break;
9770 }
9771 }
9772 }
9773 else
9774#endif
9775 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
9776 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
9777
9778 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
9779 i = redraw_cmdline;
9780 screen_fill(row, row + 1,
9781 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
9782 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
9783 fillchar, fillchar, attr);
9784 /* don't redraw the cmdline because of showing the ruler */
9785 redraw_cmdline = i;
9786 wp->w_ru_cursor = wp->w_cursor;
9787 wp->w_ru_virtcol = wp->w_virtcol;
9788 wp->w_ru_empty = empty_line;
9789 wp->w_ru_topline = wp->w_topline;
9790 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
9791#ifdef FEAT_DIFF
9792 wp->w_ru_topfill = wp->w_topfill;
9793#endif
9794 }
9795}
9796#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +00009797
9798#if defined(FEAT_LINEBREAK) || defined(PROTO)
9799/*
Bram Moolenaar64486672010-05-16 15:46:46 +02009800 * Return the width of the 'number' and 'relativenumber' column.
9801 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +00009802 * Otherwise it depends on 'numberwidth' and the line count.
9803 */
9804 int
9805number_width(wp)
9806 win_T *wp;
9807{
9808 int n;
9809 linenr_T lnum;
9810
Bram Moolenaar64486672010-05-16 15:46:46 +02009811 if (wp->w_p_nu)
9812 /* 'number' */
9813 lnum = wp->w_buffer->b_ml.ml_line_count;
9814 else
9815 /* 'relativenumber' */
9816 lnum = wp->w_height;
9817
Bram Moolenaar592e0a22004-07-03 16:05:59 +00009818 if (lnum == wp->w_nrwidth_line_count)
9819 return wp->w_nrwidth_width;
9820 wp->w_nrwidth_line_count = lnum;
9821
9822 n = 0;
9823 do
9824 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009825 lnum /= 10;
9826 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00009827 } while (lnum > 0);
9828
9829 /* 'numberwidth' gives the minimal width plus one */
9830 if (n < wp->w_p_nuw - 1)
9831 n = wp->w_p_nuw - 1;
9832
9833 wp->w_nrwidth_width = n;
9834 return n;
9835}
9836#endif