blob: fd15ad18f3834c7083f18638be8c27866b38e99c [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)
Bram Moolenaarea389e92014-05-28 21:40:52 +020045 * - w_topfill (filler lines above the first line)
Bram Moolenaar071d4272004-06-13 20:20:40 +000046 * - 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
Bram Moolenaar5641f382012-06-13 18:06:36 +020092#define MB_FILLER_CHAR '<' /* character used when a double-width character
93 * doesn't fit. */
94
Bram Moolenaar071d4272004-06-13 20:20:40 +000095/*
96 * The attributes that are actually active for writing to the screen.
97 */
98static int screen_attr = 0;
99
100/*
101 * Positioning the cursor is reduced by remembering the last position.
102 * Mostly used by windgoto() and screen_char().
103 */
104static int screen_cur_row, screen_cur_col; /* last known cursor position */
105
106#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107static match_T search_hl; /* used for 'hlsearch' highlight matching */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108#endif
109
110#ifdef FEAT_FOLDING
111static foldinfo_T win_foldinfo; /* info for 'foldcolumn' */
Bram Moolenaar1c934292015-01-27 16:39:29 +0100112static int compute_foldcolumn __ARGS((win_T *wp, int col));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113#endif
114
115/*
116 * Buffer for one screen line (characters and attributes).
117 */
118static schar_T *current_ScreenLine;
119
120static void win_update __ARGS((win_T *wp));
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000121static 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 +0000122#ifdef FEAT_FOLDING
123static void fold_line __ARGS((win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row));
124static void fill_foldcolumn __ARGS((char_u *p, win_T *wp, int closed, linenr_T lnum));
125static void copy_text_attr __ARGS((int off, char_u *buf, int len, int attr));
126#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000127static int win_line __ARGS((win_T *, linenr_T, int, int, int nochange));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128static int char_needs_redraw __ARGS((int off_from, int off_to, int cols));
129#ifdef FEAT_RIGHTLEFT
130static void screen_line __ARGS((int row, int coloff, int endcol, int clear_width, int rlflag));
131# define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c), (rl))
132#else
133static void screen_line __ARGS((int row, int coloff, int endcol, int clear_width));
134# define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c))
135#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136#ifdef FEAT_VERTSPLIT
137static void draw_vsep_win __ARGS((win_T *wp, int row));
138#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +0000139#ifdef FEAT_STL_OPT
Bram Moolenaar362f3562009-11-03 16:20:34 +0000140static void redraw_custom_statusline __ARGS((win_T *wp));
Bram Moolenaar238a5642006-02-21 22:12:05 +0000141#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarde993ea2014-06-17 23:18:01 +0200143# define SEARCH_HL_PRIORITY 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144static void start_search_hl __ARGS((void));
145static void end_search_hl __ARGS((void));
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200146static void init_search_hl __ARGS((win_T *wp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147static void prepare_search_hl __ARGS((win_T *wp, linenr_T lnum));
Bram Moolenaarb3414592014-06-17 17:48:32 +0200148static void next_search_hl __ARGS((win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol, matchitem_T *cur));
149static int next_search_hl_pos __ARGS((match_T *shl, linenr_T lnum, posmatch_T *pos, colnr_T mincol));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150#endif
151static void screen_start_highlight __ARGS((int attr));
152static void screen_char __ARGS((unsigned off, int row, int col));
153#ifdef FEAT_MBYTE
154static void screen_char_2 __ARGS((unsigned off, int row, int col));
155#endif
156static void screenclear2 __ARGS((void));
157static void lineclear __ARGS((unsigned off, int width));
158static void lineinvalid __ARGS((unsigned off, int width));
159#ifdef FEAT_VERTSPLIT
160static void linecopy __ARGS((int to, int from, win_T *wp));
161static void redraw_block __ARGS((int row, int end, win_T *wp));
162#endif
163static int win_do_lines __ARGS((win_T *wp, int row, int line_count, int mayclear, int del));
164static void win_rest_invalid __ARGS((win_T *wp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165static void msg_pos_mode __ARGS((void));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000166#if defined(FEAT_WINDOWS)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000167static void draw_tabline __ARGS((void));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000168#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
170static int fillchar_status __ARGS((int *attr, int is_curwin));
171#endif
172#ifdef FEAT_VERTSPLIT
173static int fillchar_vsep __ARGS((int *attr));
174#endif
175#ifdef FEAT_STL_OPT
Bram Moolenaar9372a112005-12-06 19:59:18 +0000176static void win_redr_custom __ARGS((win_T *wp, int draw_ruler));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177#endif
178#ifdef FEAT_CMDL_INFO
179static void win_redr_ruler __ARGS((win_T *wp, int always));
180#endif
181
182#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
183/* Ugly global: overrule attribute used by screen_char() */
184static int screen_char_attr = 0;
185#endif
186
187/*
188 * Redraw the current window later, with update_screen(type).
189 * Set must_redraw only if not already set to a higher value.
190 * e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
191 */
192 void
193redraw_later(type)
194 int type;
195{
196 redraw_win_later(curwin, type);
197}
198
199 void
200redraw_win_later(wp, type)
201 win_T *wp;
202 int type;
203{
204 if (wp->w_redr_type < type)
205 {
206 wp->w_redr_type = type;
207 if (type >= NOT_VALID)
208 wp->w_lines_valid = 0;
209 if (must_redraw < type) /* must_redraw is the maximum of all windows */
210 must_redraw = type;
211 }
212}
213
214/*
215 * Force a complete redraw later. Also resets the highlighting. To be used
216 * after executing a shell command that messes up the screen.
217 */
218 void
219redraw_later_clear()
220{
221 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000222#ifdef FEAT_GUI
223 if (gui.in_use)
224 /* Use a code that will reset gui.highlight_mask in
225 * gui_stop_highlight(). */
226 screen_attr = HL_ALL + 1;
227 else
228#endif
229 /* Use attributes that is very unlikely to appear in text. */
230 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231}
232
233/*
234 * Mark all windows to be redrawn later.
235 */
236 void
237redraw_all_later(type)
238 int type;
239{
240 win_T *wp;
241
242 FOR_ALL_WINDOWS(wp)
243 {
244 redraw_win_later(wp, type);
245 }
246}
247
248/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000249 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250 */
251 void
252redraw_curbuf_later(type)
253 int type;
254{
255 redraw_buf_later(curbuf, type);
256}
257
258 void
259redraw_buf_later(buf, type)
260 buf_T *buf;
261 int type;
262{
263 win_T *wp;
264
265 FOR_ALL_WINDOWS(wp)
266 {
267 if (wp->w_buffer == buf)
268 redraw_win_later(wp, type);
269 }
270}
271
272/*
Bram Moolenaar2951b772013-07-03 12:45:31 +0200273 * Redraw as soon as possible. When the command line is not scrolled redraw
274 * right away and restore what was on the command line.
275 * Return a code indicating what happened.
276 */
277 int
278redraw_asap(type)
279 int type;
280{
281 int rows;
282 int r;
283 int ret = 0;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200284 schar_T *screenline; /* copy from ScreenLines[] */
285 sattr_T *screenattr; /* copy from ScreenAttrs[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200286#ifdef FEAT_MBYTE
287 int i;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200288 u8char_T *screenlineUC = NULL; /* copy from ScreenLinesUC[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200289 u8char_T *screenlineC[MAX_MCO]; /* copy from ScreenLinesC[][] */
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200290 schar_T *screenline2 = NULL; /* copy from ScreenLines2[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200291#endif
292
293 redraw_later(type);
294 if (msg_scrolled || (State != NORMAL && State != NORMAL_BUSY))
295 return ret;
296
297 /* Allocate space to save the text displayed in the command line area. */
298 rows = Rows - cmdline_row;
299 screenline = (schar_T *)lalloc(
300 (long_u)(rows * Columns * sizeof(schar_T)), FALSE);
301 screenattr = (sattr_T *)lalloc(
302 (long_u)(rows * Columns * sizeof(sattr_T)), FALSE);
303 if (screenline == NULL || screenattr == NULL)
304 ret = 2;
305#ifdef FEAT_MBYTE
306 if (enc_utf8)
307 {
308 screenlineUC = (u8char_T *)lalloc(
309 (long_u)(rows * Columns * sizeof(u8char_T)), FALSE);
310 if (screenlineUC == NULL)
311 ret = 2;
312 for (i = 0; i < p_mco; ++i)
313 {
314 screenlineC[i] = (u8char_T *)lalloc(
315 (long_u)(rows * Columns * sizeof(u8char_T)), FALSE);
316 if (screenlineC[i] == NULL)
317 ret = 2;
318 }
319 }
320 if (enc_dbcs == DBCS_JPNU)
321 {
322 screenline2 = (schar_T *)lalloc(
323 (long_u)(rows * Columns * sizeof(schar_T)), FALSE);
324 if (screenline2 == NULL)
325 ret = 2;
326 }
327#endif
328
329 if (ret != 2)
330 {
331 /* Save the text displayed in the command line area. */
332 for (r = 0; r < rows; ++r)
333 {
334 mch_memmove(screenline + r * Columns,
335 ScreenLines + LineOffset[cmdline_row + r],
336 (size_t)Columns * sizeof(schar_T));
337 mch_memmove(screenattr + r * Columns,
338 ScreenAttrs + LineOffset[cmdline_row + r],
339 (size_t)Columns * sizeof(sattr_T));
340#ifdef FEAT_MBYTE
341 if (enc_utf8)
342 {
343 mch_memmove(screenlineUC + r * Columns,
344 ScreenLinesUC + LineOffset[cmdline_row + r],
345 (size_t)Columns * sizeof(u8char_T));
346 for (i = 0; i < p_mco; ++i)
347 mch_memmove(screenlineC[i] + r * Columns,
348 ScreenLinesC[r] + LineOffset[cmdline_row + r],
349 (size_t)Columns * sizeof(u8char_T));
350 }
351 if (enc_dbcs == DBCS_JPNU)
352 mch_memmove(screenline2 + r * Columns,
353 ScreenLines2 + LineOffset[cmdline_row + r],
354 (size_t)Columns * sizeof(schar_T));
355#endif
356 }
357
358 update_screen(0);
359 ret = 3;
360
361 if (must_redraw == 0)
362 {
363 int off = (int)(current_ScreenLine - ScreenLines);
364
365 /* Restore the text displayed in the command line area. */
366 for (r = 0; r < rows; ++r)
367 {
368 mch_memmove(current_ScreenLine,
369 screenline + r * Columns,
370 (size_t)Columns * sizeof(schar_T));
371 mch_memmove(ScreenAttrs + off,
372 screenattr + r * Columns,
373 (size_t)Columns * sizeof(sattr_T));
374#ifdef FEAT_MBYTE
375 if (enc_utf8)
376 {
377 mch_memmove(ScreenLinesUC + off,
378 screenlineUC + r * Columns,
379 (size_t)Columns * sizeof(u8char_T));
380 for (i = 0; i < p_mco; ++i)
381 mch_memmove(ScreenLinesC[i] + off,
382 screenlineC[i] + r * Columns,
383 (size_t)Columns * sizeof(u8char_T));
384 }
385 if (enc_dbcs == DBCS_JPNU)
386 mch_memmove(ScreenLines2 + off,
387 screenline2 + r * Columns,
388 (size_t)Columns * sizeof(schar_T));
389#endif
390 SCREEN_LINE(cmdline_row + r, 0, Columns, Columns, FALSE);
391 }
392 ret = 4;
393 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200394 }
395
396 vim_free(screenline);
397 vim_free(screenattr);
398#ifdef FEAT_MBYTE
399 if (enc_utf8)
400 {
401 vim_free(screenlineUC);
402 for (i = 0; i < p_mco; ++i)
403 vim_free(screenlineC[i]);
404 }
405 if (enc_dbcs == DBCS_JPNU)
406 vim_free(screenline2);
407#endif
408
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200409 /* Show the intro message when appropriate. */
410 maybe_intro_message();
411
412 setcursor();
413
Bram Moolenaar2951b772013-07-03 12:45:31 +0200414 return ret;
415}
416
417/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418 * Changed something in the current window, at buffer line "lnum", that
419 * requires that line and possibly other lines to be redrawn.
420 * Used when entering/leaving Insert mode with the cursor on a folded line.
421 * Used to remove the "$" from a change command.
422 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
423 * may become invalid and the whole window will have to be redrawn.
424 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425 void
426redrawWinline(lnum, invalid)
427 linenr_T lnum;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +0000428 int invalid UNUSED; /* window line height is invalid now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429{
430#ifdef FEAT_FOLDING
431 int i;
432#endif
433
434 if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
435 curwin->w_redraw_top = lnum;
436 if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
437 curwin->w_redraw_bot = lnum;
438 redraw_later(VALID);
439
440#ifdef FEAT_FOLDING
441 if (invalid)
442 {
443 /* A w_lines[] entry for this lnum has become invalid. */
444 i = find_wl_entry(curwin, lnum);
445 if (i >= 0)
446 curwin->w_lines[i].wl_valid = FALSE;
447 }
448#endif
449}
450
451/*
452 * update all windows that are editing the current buffer
453 */
454 void
455update_curbuf(type)
456 int type;
457{
458 redraw_curbuf_later(type);
459 update_screen(type);
460}
461
462/*
463 * update_screen()
464 *
465 * Based on the current value of curwin->w_topline, transfer a screenfull
466 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
467 */
468 void
469update_screen(type)
470 int type;
471{
472 win_T *wp;
473 static int did_intro = FALSE;
474#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
475 int did_one;
476#endif
477
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000478 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479 if (!screen_valid(TRUE))
480 return;
481
482 if (must_redraw)
483 {
484 if (type < must_redraw) /* use maximal type */
485 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000486
487 /* must_redraw is reset here, so that when we run into some weird
488 * reason to redraw while busy redrawing (e.g., asynchronous
489 * scrolling), or update_topline() in win_update() will cause a
490 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491 must_redraw = 0;
492 }
493
494 /* Need to update w_lines[]. */
495 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
496 type = NOT_VALID;
497
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000498 /* Postpone the redrawing when it's not needed and when being called
499 * recursively. */
500 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501 {
502 redraw_later(type); /* remember type for next time */
503 must_redraw = type;
504 if (type > INVERTED_ALL)
505 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
506 return;
507 }
508
509 updating_screen = TRUE;
510#ifdef FEAT_SYN_HL
511 ++display_tick; /* let syntax code know we're in a next round of
512 * display updating */
513#endif
514
515 /*
516 * if the screen was scrolled up when displaying a message, scroll it down
517 */
518 if (msg_scrolled)
519 {
520 clear_cmdline = TRUE;
521 if (msg_scrolled > Rows - 5) /* clearing is faster */
522 type = CLEAR;
523 else if (type != CLEAR)
524 {
525 check_for_delay(FALSE);
526 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, NULL) == FAIL)
527 type = CLEAR;
528 FOR_ALL_WINDOWS(wp)
529 {
530 if (W_WINROW(wp) < msg_scrolled)
531 {
532 if (W_WINROW(wp) + wp->w_height > msg_scrolled
533 && wp->w_redr_type < REDRAW_TOP
534 && wp->w_lines_valid > 0
535 && wp->w_topline == wp->w_lines[0].wl_lnum)
536 {
537 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
538 wp->w_redr_type = REDRAW_TOP;
539 }
540 else
541 {
542 wp->w_redr_type = NOT_VALID;
543#ifdef FEAT_WINDOWS
544 if (W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp)
545 <= msg_scrolled)
546 wp->w_redr_status = TRUE;
547#endif
548 }
549 }
550 }
551 redraw_cmdline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000552#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000553 redraw_tabline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000554#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 }
556 msg_scrolled = 0;
557 need_wait_return = FALSE;
558 }
559
560 /* reset cmdline_row now (may have been changed temporarily) */
561 compute_cmdrow();
562
563 /* Check for changed highlighting */
564 if (need_highlight_changed)
565 highlight_changed();
566
567 if (type == CLEAR) /* first clear screen */
568 {
569 screenclear(); /* will reset clear_cmdline */
570 type = NOT_VALID;
571 }
572
573 if (clear_cmdline) /* going to clear cmdline (done below) */
574 check_for_delay(FALSE);
575
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000576#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200577 /* Force redraw when width of 'number' or 'relativenumber' column
578 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000579 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200580 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
581 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000582 curwin->w_redr_type = NOT_VALID;
583#endif
584
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585 /*
586 * Only start redrawing if there is really something to do.
587 */
588 if (type == INVERTED)
589 update_curswant();
590 if (curwin->w_redr_type < type
591 && !((type == VALID
592 && curwin->w_lines[0].wl_valid
593#ifdef FEAT_DIFF
594 && curwin->w_topfill == curwin->w_old_topfill
595 && curwin->w_botfill == curwin->w_old_botfill
596#endif
597 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000599 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
601 && curwin->w_old_visual_mode == VIsual_mode
602 && (curwin->w_valid & VALID_VIRTCOL)
603 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 ))
605 curwin->w_redr_type = type;
606
Bram Moolenaar5a305422006-04-28 22:38:25 +0000607#ifdef FEAT_WINDOWS
608 /* Redraw the tab pages line if needed. */
609 if (redraw_tabline || type >= NOT_VALID)
610 draw_tabline();
611#endif
612
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613#ifdef FEAT_SYN_HL
614 /*
615 * Correct stored syntax highlighting info for changes in each displayed
616 * buffer. Each buffer must only be done once.
617 */
618 FOR_ALL_WINDOWS(wp)
619 {
620 if (wp->w_buffer->b_mod_set)
621 {
622# ifdef FEAT_WINDOWS
623 win_T *wwp;
624
625 /* Check if we already did this buffer. */
626 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
627 if (wwp->w_buffer == wp->w_buffer)
628 break;
629# endif
630 if (
631# ifdef FEAT_WINDOWS
632 wwp == wp &&
633# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200634 syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 syn_stack_apply_changes(wp->w_buffer);
636 }
637 }
638#endif
639
640 /*
641 * Go from top to bottom through the windows, redrawing the ones that need
642 * it.
643 */
644#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
645 did_one = FALSE;
646#endif
647#ifdef FEAT_SEARCH_EXTRA
648 search_hl.rm.regprog = NULL;
649#endif
650 FOR_ALL_WINDOWS(wp)
651 {
652 if (wp->w_redr_type != 0)
653 {
654 cursor_off();
655#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
656 if (!did_one)
657 {
658 did_one = TRUE;
659# ifdef FEAT_SEARCH_EXTRA
660 start_search_hl();
661# endif
662# ifdef FEAT_CLIPBOARD
663 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200664 if (clip_star.available && clip_isautosel_star())
665 clip_update_selection(&clip_star);
666 if (clip_plus.available && clip_isautosel_plus())
667 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668# endif
669#ifdef FEAT_GUI
670 /* Remove the cursor before starting to do anything, because
671 * scrolling may make it difficult to redraw the text under
672 * it. */
673 if (gui.in_use)
674 gui_undraw_cursor();
675#endif
676 }
677#endif
678 win_update(wp);
679 }
680
681#ifdef FEAT_WINDOWS
682 /* redraw status line after the window to minimize cursor movement */
683 if (wp->w_redr_status)
684 {
685 cursor_off();
686 win_redr_status(wp);
687 }
688#endif
689 }
690#if defined(FEAT_SEARCH_EXTRA)
691 end_search_hl();
692#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100693#ifdef FEAT_INS_EXPAND
694 /* May need to redraw the popup menu. */
695 if (pum_visible())
696 pum_redraw();
697#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698
699#ifdef FEAT_WINDOWS
700 /* Reset b_mod_set flags. Going through all windows is probably faster
701 * than going through all buffers (there could be many buffers). */
702 for (wp = firstwin; wp != NULL; wp = wp->w_next)
703 wp->w_buffer->b_mod_set = FALSE;
704#else
705 curbuf->b_mod_set = FALSE;
706#endif
707
708 updating_screen = FALSE;
709#ifdef FEAT_GUI
710 gui_may_resize_shell();
711#endif
712
713 /* Clear or redraw the command line. Done last, because scrolling may
714 * mess up the command line. */
715 if (clear_cmdline || redraw_cmdline)
716 showmode();
717
718 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200719 if (!did_intro)
720 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 did_intro = TRUE;
722
723#ifdef FEAT_GUI
724 /* Redraw the cursor and update the scrollbars when all screen updating is
725 * done. */
726 if (gui.in_use)
727 {
728 out_flush(); /* required before updating the cursor */
729 if (did_one)
730 gui_update_cursor(FALSE, FALSE);
731 gui_update_scrollbars(FALSE);
732 }
733#endif
734}
735
Bram Moolenaar860cae12010-06-05 23:22:07 +0200736#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200737/*
738 * Return TRUE if the cursor line in window "wp" may be concealed, according
739 * to the 'concealcursor' option.
740 */
741 int
742conceal_cursor_line(wp)
743 win_T *wp;
744{
745 int c;
746
747 if (*wp->w_p_cocu == NUL)
748 return FALSE;
749 if (get_real_state() & VISUAL)
750 c = 'v';
751 else if (State & INSERT)
752 c = 'i';
753 else if (State & NORMAL)
754 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200755 else if (State & CMDLINE)
756 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200757 else
758 return FALSE;
759 return vim_strchr(wp->w_p_cocu, c) != NULL;
760}
761
762/*
763 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
764 */
765 void
Bram Moolenaar8e469272010-07-28 19:38:16 +0200766conceal_check_cursur_line()
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200767{
768 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
769 {
770 need_cursor_line_redraw = TRUE;
771 /* Need to recompute cursor column, e.g., when starting Visual mode
772 * without concealing. */
773 curs_columns(TRUE);
774 }
775}
776
Bram Moolenaar860cae12010-06-05 23:22:07 +0200777 void
778update_single_line(wp, lnum)
779 win_T *wp;
780 linenr_T lnum;
781{
782 int row;
783 int j;
784
785 if (lnum >= wp->w_topline && lnum < wp->w_botline
Bram Moolenaar370df582010-06-22 05:16:38 +0200786 && foldedCount(wp, lnum, &win_foldinfo) == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200787 {
788# ifdef FEAT_GUI
789 /* Remove the cursor before starting to do anything, because scrolling
790 * may make it difficult to redraw the text under it. */
791 if (gui.in_use)
792 gui_undraw_cursor();
793# endif
794 row = 0;
795 for (j = 0; j < wp->w_lines_valid; ++j)
796 {
797 if (lnum == wp->w_lines[j].wl_lnum)
798 {
799 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200800# ifdef FEAT_SEARCH_EXTRA
801 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200802 start_search_hl();
803 prepare_search_hl(wp, lnum);
804# endif
805 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size, FALSE);
806# if defined(FEAT_SEARCH_EXTRA)
807 end_search_hl();
808# endif
809 break;
810 }
811 row += wp->w_lines[j].wl_size;
812 }
813# ifdef FEAT_GUI
814 /* Redraw the cursor */
815 if (gui.in_use)
816 {
817 out_flush(); /* required before updating the cursor */
818 gui_update_cursor(FALSE, FALSE);
819 }
820# endif
821 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200822 need_cursor_line_redraw = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +0200823}
824#endif
825
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826#if defined(FEAT_SIGNS) || defined(FEAT_GUI)
827static void update_prepare __ARGS((void));
828static void update_finish __ARGS((void));
829
830/*
831 * Prepare for updating one or more windows.
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000832 * Caller must check for "updating_screen" already set to avoid recursiveness.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 */
834 static void
835update_prepare()
836{
837 cursor_off();
838 updating_screen = TRUE;
839#ifdef FEAT_GUI
840 /* Remove the cursor before starting to do anything, because scrolling may
841 * make it difficult to redraw the text under it. */
842 if (gui.in_use)
843 gui_undraw_cursor();
844#endif
845#ifdef FEAT_SEARCH_EXTRA
846 start_search_hl();
847#endif
848}
849
850/*
851 * Finish updating one or more windows.
852 */
853 static void
854update_finish()
855{
856 if (redraw_cmdline)
857 showmode();
858
859# ifdef FEAT_SEARCH_EXTRA
860 end_search_hl();
861# endif
862
863 updating_screen = FALSE;
864
865# ifdef FEAT_GUI
866 gui_may_resize_shell();
867
868 /* Redraw the cursor and update the scrollbars when all screen updating is
869 * done. */
870 if (gui.in_use)
871 {
872 out_flush(); /* required before updating the cursor */
873 gui_update_cursor(FALSE, FALSE);
874 gui_update_scrollbars(FALSE);
875 }
876# endif
877}
878#endif
879
880#if defined(FEAT_SIGNS) || defined(PROTO)
881 void
882update_debug_sign(buf, lnum)
883 buf_T *buf;
884 linenr_T lnum;
885{
886 win_T *wp;
887 int doit = FALSE;
888
889# ifdef FEAT_FOLDING
890 win_foldinfo.fi_level = 0;
891# endif
892
893 /* update/delete a specific mark */
894 FOR_ALL_WINDOWS(wp)
895 {
896 if (buf != NULL && lnum > 0)
897 {
898 if (wp->w_buffer == buf && lnum >= wp->w_topline
899 && lnum < wp->w_botline)
900 {
901 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
902 wp->w_redraw_top = lnum;
903 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
904 wp->w_redraw_bot = lnum;
905 redraw_win_later(wp, VALID);
906 }
907 }
908 else
909 redraw_win_later(wp, VALID);
910 if (wp->w_redr_type != 0)
911 doit = TRUE;
912 }
913
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100914 /* Return when there is nothing to do, screen updating is already
915 * happening (recursive call) or still starting up. */
916 if (!doit || updating_screen
917#ifdef FEAT_GUI
918 || gui.starting
919#endif
920 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 return;
922
923 /* update all windows that need updating */
924 update_prepare();
925
926# ifdef FEAT_WINDOWS
927 for (wp = firstwin; wp; wp = wp->w_next)
928 {
929 if (wp->w_redr_type != 0)
930 win_update(wp);
931 if (wp->w_redr_status)
932 win_redr_status(wp);
933 }
934# else
935 if (curwin->w_redr_type != 0)
936 win_update(curwin);
937# endif
938
939 update_finish();
940}
941#endif
942
943
944#if defined(FEAT_GUI) || defined(PROTO)
945/*
946 * Update a single window, its status line and maybe the command line msg.
947 * Used for the GUI scrollbar.
948 */
949 void
950updateWindow(wp)
951 win_T *wp;
952{
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000953 /* return if already busy updating */
954 if (updating_screen)
955 return;
956
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 update_prepare();
958
959#ifdef FEAT_CLIPBOARD
960 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200961 if (clip_star.available && clip_isautosel_star())
962 clip_update_selection(&clip_star);
963 if (clip_plus.available && clip_isautosel_plus())
964 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000966
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000968
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969#ifdef FEAT_WINDOWS
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000970 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000971 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000972 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000973
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 if (wp->w_redr_status
975# ifdef FEAT_CMDL_INFO
976 || p_ru
977# endif
978# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000979 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980# endif
981 )
982 win_redr_status(wp);
983#endif
984
985 update_finish();
986}
987#endif
988
989/*
990 * Update a single window.
991 *
992 * This may cause the windows below it also to be redrawn (when clearing the
993 * screen or scrolling lines).
994 *
995 * How the window is redrawn depends on wp->w_redr_type. Each type also
996 * implies the one below it.
997 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000998 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1000 * INVERTED redraw the changed part of the Visual area
1001 * INVERTED_ALL redraw the whole Visual area
1002 * VALID 1. scroll up/down to adjust for a changed w_topline
1003 * 2. update lines at the top when scrolled down
1004 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001005 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 * b_mod_top and b_mod_bot.
1007 * - if wp->w_redraw_top non-zero, redraw lines between
1008 * wp->w_redraw_top and wp->w_redr_bot.
1009 * - continue redrawing when syntax status is invalid.
1010 * 4. if scrolled up, update lines at the bottom.
1011 * This results in three areas that may need updating:
1012 * top: from first row to top_end (when scrolled down)
1013 * mid: from mid_start to mid_end (update inversion or changed text)
1014 * bot: from bot_start to last row (when scrolled up)
1015 */
1016 static void
1017win_update(wp)
1018 win_T *wp;
1019{
1020 buf_T *buf = wp->w_buffer;
1021 int type;
1022 int top_end = 0; /* Below last row of the top area that needs
1023 updating. 0 when no top area updating. */
1024 int mid_start = 999;/* first row of the mid area that needs
1025 updating. 999 when no mid area updating. */
1026 int mid_end = 0; /* Below last row of the mid area that needs
1027 updating. 0 when no mid area updating. */
1028 int bot_start = 999;/* first row of the bot area that needs
1029 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 int scrolled_down = FALSE; /* TRUE when scrolled down when
1031 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001033 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 int top_to_mod = FALSE; /* redraw above mod_top */
1035#endif
1036
1037 int row; /* current window row to display */
1038 linenr_T lnum; /* current buffer lnum to display */
1039 int idx; /* current index in w_lines[] */
1040 int srow; /* starting row of the current line */
1041
1042 int eof = FALSE; /* if TRUE, we hit the end of the file */
1043 int didline = FALSE; /* if TRUE, we finished the last line */
1044 int i;
1045 long j;
1046 static int recursive = FALSE; /* being called recursively */
1047 int old_botline = wp->w_botline;
1048#ifdef FEAT_FOLDING
1049 long fold_count;
1050#endif
1051#ifdef FEAT_SYN_HL
1052 /* remember what happened to the previous line, to know if
1053 * check_visual_highlight() can be used */
1054#define DID_NONE 1 /* didn't update a line */
1055#define DID_LINE 2 /* updated a normal line */
1056#define DID_FOLD 3 /* updated a folded line */
1057 int did_update = DID_NONE;
1058 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1059#endif
1060 linenr_T mod_top = 0;
1061 linenr_T mod_bot = 0;
1062#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1063 int save_got_int;
1064#endif
1065
1066 type = wp->w_redr_type;
1067
1068 if (type == NOT_VALID)
1069 {
1070#ifdef FEAT_WINDOWS
1071 wp->w_redr_status = TRUE;
1072#endif
1073 wp->w_lines_valid = 0;
1074 }
1075
1076 /* Window is zero-height: nothing to draw. */
1077 if (wp->w_height == 0)
1078 {
1079 wp->w_redr_type = 0;
1080 return;
1081 }
1082
1083#ifdef FEAT_VERTSPLIT
1084 /* Window is zero-width: Only need to draw the separator. */
1085 if (wp->w_width == 0)
1086 {
1087 /* draw the vertical separator right of this window */
1088 draw_vsep_win(wp, 0);
1089 wp->w_redr_type = 0;
1090 return;
1091 }
1092#endif
1093
1094#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001095 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096#endif
1097
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001098#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001099 /* Force redraw when width of 'number' or 'relativenumber' column
1100 * changes. */
1101 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001102 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001103 {
1104 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001105 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001106 }
1107 else
1108#endif
1109
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1111 {
1112 /*
1113 * When there are both inserted/deleted lines and specific lines to be
1114 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1115 * everything (only happens when redrawing is off for while).
1116 */
1117 type = NOT_VALID;
1118 }
1119 else
1120 {
1121 /*
1122 * Set mod_top to the first line that needs displaying because of
1123 * changes. Set mod_bot to the first line after the changes.
1124 */
1125 mod_top = wp->w_redraw_top;
1126 if (wp->w_redraw_bot != 0)
1127 mod_bot = wp->w_redraw_bot + 1;
1128 else
1129 mod_bot = 0;
1130 wp->w_redraw_top = 0; /* reset for next time */
1131 wp->w_redraw_bot = 0;
1132 if (buf->b_mod_set)
1133 {
1134 if (mod_top == 0 || mod_top > buf->b_mod_top)
1135 {
1136 mod_top = buf->b_mod_top;
1137#ifdef FEAT_SYN_HL
1138 /* Need to redraw lines above the change that may be included
1139 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001140 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001142 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 if (mod_top < 1)
1144 mod_top = 1;
1145 }
1146#endif
1147 }
1148 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1149 mod_bot = buf->b_mod_bot;
1150
1151#ifdef FEAT_SEARCH_EXTRA
1152 /* When 'hlsearch' is on and using a multi-line search pattern, a
1153 * change in one line may make the Search highlighting in a
1154 * previous line invalid. Simple solution: redraw all visible
1155 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001156 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001158 if (search_hl.rm.regprog != NULL
1159 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001161 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001162 {
1163 cur = wp->w_match_head;
1164 while (cur != NULL)
1165 {
1166 if (cur->match.regprog != NULL
1167 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001168 {
1169 top_to_mod = TRUE;
1170 break;
1171 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001172 cur = cur->next;
1173 }
1174 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175#endif
1176 }
1177#ifdef FEAT_FOLDING
1178 if (mod_top != 0 && hasAnyFolding(wp))
1179 {
1180 linenr_T lnumt, lnumb;
1181
1182 /*
1183 * A change in a line can cause lines above it to become folded or
1184 * unfolded. Find the top most buffer line that may be affected.
1185 * If the line was previously folded and displayed, get the first
1186 * line of that fold. If the line is folded now, get the first
1187 * folded line. Use the minimum of these two.
1188 */
1189
1190 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1191 * the line below it. If there is no valid entry, use w_topline.
1192 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1193 * to this line. If there is no valid entry, use MAXLNUM. */
1194 lnumt = wp->w_topline;
1195 lnumb = MAXLNUM;
1196 for (i = 0; i < wp->w_lines_valid; ++i)
1197 if (wp->w_lines[i].wl_valid)
1198 {
1199 if (wp->w_lines[i].wl_lastlnum < mod_top)
1200 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1201 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1202 {
1203 lnumb = wp->w_lines[i].wl_lnum;
1204 /* When there is a fold column it might need updating
1205 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001206 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 ++lnumb;
1208 }
1209 }
1210
1211 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1212 if (mod_top > lnumt)
1213 mod_top = lnumt;
1214
1215 /* Now do the same for the bottom line (one above mod_bot). */
1216 --mod_bot;
1217 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1218 ++mod_bot;
1219 if (mod_bot < lnumb)
1220 mod_bot = lnumb;
1221 }
1222#endif
1223
1224 /* When a change starts above w_topline and the end is below
1225 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001226 * If the end of the change is above w_topline: do like no change was
1227 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 if (mod_top != 0 && mod_top < wp->w_topline)
1229 {
1230 if (mod_bot > wp->w_topline)
1231 mod_top = wp->w_topline;
1232#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001233 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 top_end = 1;
1235#endif
1236 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001237
1238 /* When line numbers are displayed need to redraw all lines below
1239 * inserted/deleted lines. */
1240 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1241 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 }
1243
1244 /*
1245 * When only displaying the lines at the top, set top_end. Used when
1246 * window has scrolled down for msg_scrolled.
1247 */
1248 if (type == REDRAW_TOP)
1249 {
1250 j = 0;
1251 for (i = 0; i < wp->w_lines_valid; ++i)
1252 {
1253 j += wp->w_lines[i].wl_size;
1254 if (j >= wp->w_upd_rows)
1255 {
1256 top_end = j;
1257 break;
1258 }
1259 }
1260 if (top_end == 0)
1261 /* not found (cannot happen?): redraw everything */
1262 type = NOT_VALID;
1263 else
1264 /* top area defined, the rest is VALID */
1265 type = VALID;
1266 }
1267
Bram Moolenaar367329b2007-08-30 11:53:22 +00001268 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001269 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1270 * non-zero and thus not FALSE) will indicate that screenclear() was not
1271 * called. */
1272 if (screen_cleared)
1273 screen_cleared = MAYBE;
1274
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 /*
1276 * If there are no changes on the screen that require a complete redraw,
1277 * handle three cases:
1278 * 1: we are off the top of the screen by a few lines: scroll down
1279 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1280 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1281 * w_lines[] that needs updating.
1282 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001283 if ((type == VALID || type == SOME_VALID
1284 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285#ifdef FEAT_DIFF
1286 && !wp->w_botfill && !wp->w_old_botfill
1287#endif
1288 )
1289 {
1290 if (mod_top != 0 && wp->w_topline == mod_top)
1291 {
1292 /*
1293 * w_topline is the first changed line, the scrolling will be done
1294 * further down.
1295 */
1296 }
1297 else if (wp->w_lines[0].wl_valid
1298 && (wp->w_topline < wp->w_lines[0].wl_lnum
1299#ifdef FEAT_DIFF
1300 || (wp->w_topline == wp->w_lines[0].wl_lnum
1301 && wp->w_topfill > wp->w_old_topfill)
1302#endif
1303 ))
1304 {
1305 /*
1306 * New topline is above old topline: May scroll down.
1307 */
1308#ifdef FEAT_FOLDING
1309 if (hasAnyFolding(wp))
1310 {
1311 linenr_T ln;
1312
1313 /* count the number of lines we are off, counting a sequence
1314 * of folded lines as one */
1315 j = 0;
1316 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1317 {
1318 ++j;
1319 if (j >= wp->w_height - 2)
1320 break;
1321 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1322 }
1323 }
1324 else
1325#endif
1326 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1327 if (j < wp->w_height - 2) /* not too far off */
1328 {
1329 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1330#ifdef FEAT_DIFF
1331 /* insert extra lines for previously invisible filler lines */
1332 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1333 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1334 - wp->w_old_topfill;
1335#endif
1336 if (i < wp->w_height - 2) /* less than a screen off */
1337 {
1338 /*
1339 * Try to insert the correct number of lines.
1340 * If not the last window, delete the lines at the bottom.
1341 * win_ins_lines may fail when the terminal can't do it.
1342 */
1343 if (i > 0)
1344 check_for_delay(FALSE);
1345 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1346 {
1347 if (wp->w_lines_valid != 0)
1348 {
1349 /* Need to update rows that are new, stop at the
1350 * first one that scrolled down. */
1351 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353
1354 /* Move the entries that were scrolled, disable
1355 * the entries for the lines to be redrawn. */
1356 if ((wp->w_lines_valid += j) > wp->w_height)
1357 wp->w_lines_valid = wp->w_height;
1358 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1359 wp->w_lines[idx] = wp->w_lines[idx - j];
1360 while (idx >= 0)
1361 wp->w_lines[idx--].wl_valid = FALSE;
1362 }
1363 }
1364 else
1365 mid_start = 0; /* redraw all lines */
1366 }
1367 else
1368 mid_start = 0; /* redraw all lines */
1369 }
1370 else
1371 mid_start = 0; /* redraw all lines */
1372 }
1373 else
1374 {
1375 /*
1376 * New topline is at or below old topline: May scroll up.
1377 * When topline didn't change, find first entry in w_lines[] that
1378 * needs updating.
1379 */
1380
1381 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1382 j = -1;
1383 row = 0;
1384 for (i = 0; i < wp->w_lines_valid; i++)
1385 {
1386 if (wp->w_lines[i].wl_valid
1387 && wp->w_lines[i].wl_lnum == wp->w_topline)
1388 {
1389 j = i;
1390 break;
1391 }
1392 row += wp->w_lines[i].wl_size;
1393 }
1394 if (j == -1)
1395 {
1396 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1397 * lines */
1398 mid_start = 0;
1399 }
1400 else
1401 {
1402 /*
1403 * Try to delete the correct number of lines.
1404 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1405 */
1406#ifdef FEAT_DIFF
1407 /* If the topline didn't change, delete old filler lines,
1408 * otherwise delete filler lines of the new topline... */
1409 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1410 row += wp->w_old_topfill;
1411 else
1412 row += diff_check_fill(wp, wp->w_topline);
1413 /* ... but don't delete new filler lines. */
1414 row -= wp->w_topfill;
1415#endif
1416 if (row > 0)
1417 {
1418 check_for_delay(FALSE);
1419 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1420 bot_start = wp->w_height - row;
1421 else
1422 mid_start = 0; /* redraw all lines */
1423 }
1424 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1425 {
1426 /*
1427 * Skip the lines (below the deleted lines) that are still
1428 * valid and don't need redrawing. Copy their info
1429 * upwards, to compensate for the deleted lines. Set
1430 * bot_start to the first row that needs redrawing.
1431 */
1432 bot_start = 0;
1433 idx = 0;
1434 for (;;)
1435 {
1436 wp->w_lines[idx] = wp->w_lines[j];
1437 /* stop at line that didn't fit, unless it is still
1438 * valid (no lines deleted) */
1439 if (row > 0 && bot_start + row
1440 + (int)wp->w_lines[j].wl_size > wp->w_height)
1441 {
1442 wp->w_lines_valid = idx + 1;
1443 break;
1444 }
1445 bot_start += wp->w_lines[idx++].wl_size;
1446
1447 /* stop at the last valid entry in w_lines[].wl_size */
1448 if (++j >= wp->w_lines_valid)
1449 {
1450 wp->w_lines_valid = idx;
1451 break;
1452 }
1453 }
1454#ifdef FEAT_DIFF
1455 /* Correct the first entry for filler lines at the top
1456 * when it won't get updated below. */
1457 if (wp->w_p_diff && bot_start > 0)
1458 wp->w_lines[0].wl_size =
1459 plines_win_nofill(wp, wp->w_topline, TRUE)
1460 + wp->w_topfill;
1461#endif
1462 }
1463 }
1464 }
1465
1466 /* When starting redraw in the first line, redraw all lines. When
1467 * there is only one window it's probably faster to clear the screen
1468 * first. */
1469 if (mid_start == 0)
1470 {
1471 mid_end = wp->w_height;
1472 if (lastwin == firstwin)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001473 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001474 /* Clear the screen when it was not done by win_del_lines() or
1475 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1476 * then. */
1477 if (screen_cleared != TRUE)
1478 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001479#ifdef FEAT_WINDOWS
1480 /* The screen was cleared, redraw the tab pages line. */
1481 if (redraw_tabline)
1482 draw_tabline();
1483#endif
1484 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001486
1487 /* When win_del_lines() or win_ins_lines() caused the screen to be
1488 * cleared (only happens for the first window) or when screenclear()
1489 * was called directly above, "must_redraw" will have been set to
1490 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1491 if (screen_cleared == TRUE)
1492 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 }
1494 else
1495 {
1496 /* Not VALID or INVERTED: redraw all lines. */
1497 mid_start = 0;
1498 mid_end = wp->w_height;
1499 }
1500
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001501 if (type == SOME_VALID)
1502 {
1503 /* SOME_VALID: redraw all lines. */
1504 mid_start = 0;
1505 mid_end = wp->w_height;
1506 type = NOT_VALID;
1507 }
1508
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 /* check if we are updating or removing the inverted part */
1510 if ((VIsual_active && buf == curwin->w_buffer)
1511 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1512 {
1513 linenr_T from, to;
1514
1515 if (VIsual_active)
1516 {
1517 if (VIsual_active
1518 && (VIsual_mode != wp->w_old_visual_mode
1519 || type == INVERTED_ALL))
1520 {
1521 /*
1522 * If the type of Visual selection changed, redraw the whole
1523 * selection. Also when the ownership of the X selection is
1524 * gained or lost.
1525 */
1526 if (curwin->w_cursor.lnum < VIsual.lnum)
1527 {
1528 from = curwin->w_cursor.lnum;
1529 to = VIsual.lnum;
1530 }
1531 else
1532 {
1533 from = VIsual.lnum;
1534 to = curwin->w_cursor.lnum;
1535 }
1536 /* redraw more when the cursor moved as well */
1537 if (wp->w_old_cursor_lnum < from)
1538 from = wp->w_old_cursor_lnum;
1539 if (wp->w_old_cursor_lnum > to)
1540 to = wp->w_old_cursor_lnum;
1541 if (wp->w_old_visual_lnum < from)
1542 from = wp->w_old_visual_lnum;
1543 if (wp->w_old_visual_lnum > to)
1544 to = wp->w_old_visual_lnum;
1545 }
1546 else
1547 {
1548 /*
1549 * Find the line numbers that need to be updated: The lines
1550 * between the old cursor position and the current cursor
1551 * position. Also check if the Visual position changed.
1552 */
1553 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1554 {
1555 from = curwin->w_cursor.lnum;
1556 to = wp->w_old_cursor_lnum;
1557 }
1558 else
1559 {
1560 from = wp->w_old_cursor_lnum;
1561 to = curwin->w_cursor.lnum;
1562 if (from == 0) /* Visual mode just started */
1563 from = to;
1564 }
1565
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001566 if (VIsual.lnum != wp->w_old_visual_lnum
1567 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 {
1569 if (wp->w_old_visual_lnum < from
1570 && wp->w_old_visual_lnum != 0)
1571 from = wp->w_old_visual_lnum;
1572 if (wp->w_old_visual_lnum > to)
1573 to = wp->w_old_visual_lnum;
1574 if (VIsual.lnum < from)
1575 from = VIsual.lnum;
1576 if (VIsual.lnum > to)
1577 to = VIsual.lnum;
1578 }
1579 }
1580
1581 /*
1582 * If in block mode and changed column or curwin->w_curswant:
1583 * update all lines.
1584 * First compute the actual start and end column.
1585 */
1586 if (VIsual_mode == Ctrl_V)
1587 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001588 colnr_T fromc, toc;
1589#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1590 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591
Bram Moolenaar404406a2014-10-09 13:24:43 +02001592 if (curwin->w_p_lbr)
1593 ve_flags = VE_ALL;
1594#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar404406a2014-10-09 13:24:43 +02001596#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1597 ve_flags = save_ve_flags;
1598#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 ++toc;
1600 if (curwin->w_curswant == MAXCOL)
1601 toc = MAXCOL;
1602
1603 if (fromc != wp->w_old_cursor_fcol
1604 || toc != wp->w_old_cursor_lcol)
1605 {
1606 if (from > VIsual.lnum)
1607 from = VIsual.lnum;
1608 if (to < VIsual.lnum)
1609 to = VIsual.lnum;
1610 }
1611 wp->w_old_cursor_fcol = fromc;
1612 wp->w_old_cursor_lcol = toc;
1613 }
1614 }
1615 else
1616 {
1617 /* Use the line numbers of the old Visual area. */
1618 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1619 {
1620 from = wp->w_old_cursor_lnum;
1621 to = wp->w_old_visual_lnum;
1622 }
1623 else
1624 {
1625 from = wp->w_old_visual_lnum;
1626 to = wp->w_old_cursor_lnum;
1627 }
1628 }
1629
1630 /*
1631 * There is no need to update lines above the top of the window.
1632 */
1633 if (from < wp->w_topline)
1634 from = wp->w_topline;
1635
1636 /*
1637 * If we know the value of w_botline, use it to restrict the update to
1638 * the lines that are visible in the window.
1639 */
1640 if (wp->w_valid & VALID_BOTLINE)
1641 {
1642 if (from >= wp->w_botline)
1643 from = wp->w_botline - 1;
1644 if (to >= wp->w_botline)
1645 to = wp->w_botline - 1;
1646 }
1647
1648 /*
1649 * Find the minimal part to be updated.
1650 * Watch out for scrolling that made entries in w_lines[] invalid.
1651 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1652 * top_end; need to redraw from top_end to the "to" line.
1653 * A middle mouse click with a Visual selection may change the text
1654 * above the Visual area and reset wl_valid, do count these for
1655 * mid_end (in srow).
1656 */
1657 if (mid_start > 0)
1658 {
1659 lnum = wp->w_topline;
1660 idx = 0;
1661 srow = 0;
1662 if (scrolled_down)
1663 mid_start = top_end;
1664 else
1665 mid_start = 0;
1666 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1667 {
1668 if (wp->w_lines[idx].wl_valid)
1669 mid_start += wp->w_lines[idx].wl_size;
1670 else if (!scrolled_down)
1671 srow += wp->w_lines[idx].wl_size;
1672 ++idx;
1673# ifdef FEAT_FOLDING
1674 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1675 lnum = wp->w_lines[idx].wl_lnum;
1676 else
1677# endif
1678 ++lnum;
1679 }
1680 srow += mid_start;
1681 mid_end = wp->w_height;
1682 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1683 {
1684 if (wp->w_lines[idx].wl_valid
1685 && wp->w_lines[idx].wl_lnum >= to + 1)
1686 {
1687 /* Only update until first row of this line */
1688 mid_end = srow;
1689 break;
1690 }
1691 srow += wp->w_lines[idx].wl_size;
1692 }
1693 }
1694 }
1695
1696 if (VIsual_active && buf == curwin->w_buffer)
1697 {
1698 wp->w_old_visual_mode = VIsual_mode;
1699 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1700 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001701 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 wp->w_old_curswant = curwin->w_curswant;
1703 }
1704 else
1705 {
1706 wp->w_old_visual_mode = 0;
1707 wp->w_old_cursor_lnum = 0;
1708 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001709 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711
1712#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1713 /* reset got_int, otherwise regexp won't work */
1714 save_got_int = got_int;
1715 got_int = 0;
1716#endif
1717#ifdef FEAT_FOLDING
1718 win_foldinfo.fi_level = 0;
1719#endif
1720
1721 /*
1722 * Update all the window rows.
1723 */
1724 idx = 0; /* first entry in w_lines[].wl_size */
1725 row = 0;
1726 srow = 0;
1727 lnum = wp->w_topline; /* first line shown in window */
1728 for (;;)
1729 {
1730 /* stop updating when reached the end of the window (check for _past_
1731 * the end of the window is at the end of the loop) */
1732 if (row == wp->w_height)
1733 {
1734 didline = TRUE;
1735 break;
1736 }
1737
1738 /* stop updating when hit the end of the file */
1739 if (lnum > buf->b_ml.ml_line_count)
1740 {
1741 eof = TRUE;
1742 break;
1743 }
1744
1745 /* Remember the starting row of the line that is going to be dealt
1746 * with. It is used further down when the line doesn't fit. */
1747 srow = row;
1748
1749 /*
1750 * Update a line when it is in an area that needs updating, when it
1751 * has changes or w_lines[idx] is invalid.
1752 * bot_start may be halfway a wrapped line after using
1753 * win_del_lines(), check if the current line includes it.
1754 * When syntax folding is being used, the saved syntax states will
1755 * already have been updated, we can't see where the syntax state is
1756 * the same again, just update until the end of the window.
1757 */
1758 if (row < top_end
1759 || (row >= mid_start && row < mid_end)
1760#ifdef FEAT_SEARCH_EXTRA
1761 || top_to_mod
1762#endif
1763 || idx >= wp->w_lines_valid
1764 || (row + wp->w_lines[idx].wl_size > bot_start)
1765 || (mod_top != 0
1766 && (lnum == mod_top
1767 || (lnum >= mod_top
1768 && (lnum < mod_bot
1769#ifdef FEAT_SYN_HL
1770 || did_update == DID_FOLD
1771 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001772 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 && (
1774# ifdef FEAT_FOLDING
1775 (foldmethodIsSyntax(wp)
1776 && hasAnyFolding(wp)) ||
1777# endif
1778 syntax_check_changed(lnum)))
1779#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001780#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001781 /* match in fixed position might need redraw
1782 * if lines were inserted or deleted */
1783 || (wp->w_match_head != NULL
1784 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001785#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 )))))
1787 {
1788#ifdef FEAT_SEARCH_EXTRA
1789 if (lnum == mod_top)
1790 top_to_mod = FALSE;
1791#endif
1792
1793 /*
1794 * When at start of changed lines: May scroll following lines
1795 * up or down to minimize redrawing.
1796 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001797 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 */
1799 if (lnum == mod_top
1800 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001801 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 {
1803 int old_rows = 0;
1804 int new_rows = 0;
1805 int xtra_rows;
1806 linenr_T l;
1807
1808 /* Count the old number of window rows, using w_lines[], which
1809 * should still contain the sizes for the lines as they are
1810 * currently displayed. */
1811 for (i = idx; i < wp->w_lines_valid; ++i)
1812 {
1813 /* Only valid lines have a meaningful wl_lnum. Invalid
1814 * lines are part of the changed area. */
1815 if (wp->w_lines[i].wl_valid
1816 && wp->w_lines[i].wl_lnum == mod_bot)
1817 break;
1818 old_rows += wp->w_lines[i].wl_size;
1819#ifdef FEAT_FOLDING
1820 if (wp->w_lines[i].wl_valid
1821 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1822 {
1823 /* Must have found the last valid entry above mod_bot.
1824 * Add following invalid entries. */
1825 ++i;
1826 while (i < wp->w_lines_valid
1827 && !wp->w_lines[i].wl_valid)
1828 old_rows += wp->w_lines[i++].wl_size;
1829 break;
1830 }
1831#endif
1832 }
1833
1834 if (i >= wp->w_lines_valid)
1835 {
1836 /* We can't find a valid line below the changed lines,
1837 * need to redraw until the end of the window.
1838 * Inserting/deleting lines has no use. */
1839 bot_start = 0;
1840 }
1841 else
1842 {
1843 /* Able to count old number of rows: Count new window
1844 * rows, and may insert/delete lines */
1845 j = idx;
1846 for (l = lnum; l < mod_bot; ++l)
1847 {
1848#ifdef FEAT_FOLDING
1849 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1850 ++new_rows;
1851 else
1852#endif
1853#ifdef FEAT_DIFF
1854 if (l == wp->w_topline)
1855 new_rows += plines_win_nofill(wp, l, TRUE)
1856 + wp->w_topfill;
1857 else
1858#endif
1859 new_rows += plines_win(wp, l, TRUE);
1860 ++j;
1861 if (new_rows > wp->w_height - row - 2)
1862 {
1863 /* it's getting too much, must redraw the rest */
1864 new_rows = 9999;
1865 break;
1866 }
1867 }
1868 xtra_rows = new_rows - old_rows;
1869 if (xtra_rows < 0)
1870 {
1871 /* May scroll text up. If there is not enough
1872 * remaining text or scrolling fails, must redraw the
1873 * rest. If scrolling works, must redraw the text
1874 * below the scrolled text. */
1875 if (row - xtra_rows >= wp->w_height - 2)
1876 mod_bot = MAXLNUM;
1877 else
1878 {
1879 check_for_delay(FALSE);
1880 if (win_del_lines(wp, row,
1881 -xtra_rows, FALSE, FALSE) == FAIL)
1882 mod_bot = MAXLNUM;
1883 else
1884 bot_start = wp->w_height + xtra_rows;
1885 }
1886 }
1887 else if (xtra_rows > 0)
1888 {
1889 /* May scroll text down. If there is not enough
1890 * remaining text of scrolling fails, must redraw the
1891 * rest. */
1892 if (row + xtra_rows >= wp->w_height - 2)
1893 mod_bot = MAXLNUM;
1894 else
1895 {
1896 check_for_delay(FALSE);
1897 if (win_ins_lines(wp, row + old_rows,
1898 xtra_rows, FALSE, FALSE) == FAIL)
1899 mod_bot = MAXLNUM;
1900 else if (top_end > row + old_rows)
1901 /* Scrolled the part at the top that requires
1902 * updating down. */
1903 top_end += xtra_rows;
1904 }
1905 }
1906
1907 /* When not updating the rest, may need to move w_lines[]
1908 * entries. */
1909 if (mod_bot != MAXLNUM && i != j)
1910 {
1911 if (j < i)
1912 {
1913 int x = row + new_rows;
1914
1915 /* move entries in w_lines[] upwards */
1916 for (;;)
1917 {
1918 /* stop at last valid entry in w_lines[] */
1919 if (i >= wp->w_lines_valid)
1920 {
1921 wp->w_lines_valid = j;
1922 break;
1923 }
1924 wp->w_lines[j] = wp->w_lines[i];
1925 /* stop at a line that won't fit */
1926 if (x + (int)wp->w_lines[j].wl_size
1927 > wp->w_height)
1928 {
1929 wp->w_lines_valid = j + 1;
1930 break;
1931 }
1932 x += wp->w_lines[j++].wl_size;
1933 ++i;
1934 }
1935 if (bot_start > x)
1936 bot_start = x;
1937 }
1938 else /* j > i */
1939 {
1940 /* move entries in w_lines[] downwards */
1941 j -= i;
1942 wp->w_lines_valid += j;
1943 if (wp->w_lines_valid > wp->w_height)
1944 wp->w_lines_valid = wp->w_height;
1945 for (i = wp->w_lines_valid; i - j >= idx; --i)
1946 wp->w_lines[i] = wp->w_lines[i - j];
1947
1948 /* The w_lines[] entries for inserted lines are
1949 * now invalid, but wl_size may be used above.
1950 * Reset to zero. */
1951 while (i >= idx)
1952 {
1953 wp->w_lines[i].wl_size = 0;
1954 wp->w_lines[i--].wl_valid = FALSE;
1955 }
1956 }
1957 }
1958 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 }
1960
1961#ifdef FEAT_FOLDING
1962 /*
1963 * When lines are folded, display one line for all of them.
1964 * Otherwise, display normally (can be several display lines when
1965 * 'wrap' is on).
1966 */
1967 fold_count = foldedCount(wp, lnum, &win_foldinfo);
1968 if (fold_count != 0)
1969 {
1970 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
1971 ++row;
1972 --fold_count;
1973 wp->w_lines[idx].wl_folded = TRUE;
1974 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
1975# ifdef FEAT_SYN_HL
1976 did_update = DID_FOLD;
1977# endif
1978 }
1979 else
1980#endif
1981 if (idx < wp->w_lines_valid
1982 && wp->w_lines[idx].wl_valid
1983 && wp->w_lines[idx].wl_lnum == lnum
1984 && lnum > wp->w_topline
1985 && !(dy_flags & DY_LASTLINE)
1986 && srow + wp->w_lines[idx].wl_size > wp->w_height
1987#ifdef FEAT_DIFF
1988 && diff_check_fill(wp, lnum) == 0
1989#endif
1990 )
1991 {
1992 /* This line is not going to fit. Don't draw anything here,
1993 * will draw "@ " lines below. */
1994 row = wp->w_height + 1;
1995 }
1996 else
1997 {
1998#ifdef FEAT_SEARCH_EXTRA
1999 prepare_search_hl(wp, lnum);
2000#endif
2001#ifdef FEAT_SYN_HL
2002 /* Let the syntax stuff know we skipped a few lines. */
2003 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002004 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 syntax_end_parsing(syntax_last_parsed + 1);
2006#endif
2007
2008 /*
2009 * Display one line.
2010 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002011 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012
2013#ifdef FEAT_FOLDING
2014 wp->w_lines[idx].wl_folded = FALSE;
2015 wp->w_lines[idx].wl_lastlnum = lnum;
2016#endif
2017#ifdef FEAT_SYN_HL
2018 did_update = DID_LINE;
2019 syntax_last_parsed = lnum;
2020#endif
2021 }
2022
2023 wp->w_lines[idx].wl_lnum = lnum;
2024 wp->w_lines[idx].wl_valid = TRUE;
2025 if (row > wp->w_height) /* past end of screen */
2026 {
2027 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002028 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2030 ++idx;
2031 break;
2032 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002033 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 wp->w_lines[idx].wl_size = row - srow;
2035 ++idx;
2036#ifdef FEAT_FOLDING
2037 lnum += fold_count + 1;
2038#else
2039 ++lnum;
2040#endif
2041 }
2042 else
2043 {
2044 /* This line does not need updating, advance to the next one */
2045 row += wp->w_lines[idx++].wl_size;
2046 if (row > wp->w_height) /* past end of screen */
2047 break;
2048#ifdef FEAT_FOLDING
2049 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2050#else
2051 ++lnum;
2052#endif
2053#ifdef FEAT_SYN_HL
2054 did_update = DID_NONE;
2055#endif
2056 }
2057
2058 if (lnum > buf->b_ml.ml_line_count)
2059 {
2060 eof = TRUE;
2061 break;
2062 }
2063 }
2064 /*
2065 * End of loop over all window lines.
2066 */
2067
2068
2069 if (idx > wp->w_lines_valid)
2070 wp->w_lines_valid = idx;
2071
2072#ifdef FEAT_SYN_HL
2073 /*
2074 * Let the syntax stuff know we stop parsing here.
2075 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002076 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 syntax_end_parsing(syntax_last_parsed + 1);
2078#endif
2079
2080 /*
2081 * If we didn't hit the end of the file, and we didn't finish the last
2082 * line we were working on, then the line didn't fit.
2083 */
2084 wp->w_empty_rows = 0;
2085#ifdef FEAT_DIFF
2086 wp->w_filler_rows = 0;
2087#endif
2088 if (!eof && !didline)
2089 {
2090 if (lnum == wp->w_topline)
2091 {
2092 /*
2093 * Single line that does not fit!
2094 * Don't overwrite it, it can be edited.
2095 */
2096 wp->w_botline = lnum + 1;
2097 }
2098#ifdef FEAT_DIFF
2099 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2100 {
2101 /* Window ends in filler lines. */
2102 wp->w_botline = lnum;
2103 wp->w_filler_rows = wp->w_height - srow;
2104 }
2105#endif
2106 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2107 {
2108 /*
2109 * Last line isn't finished: Display "@@@" at the end.
2110 */
2111 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2112 W_WINROW(wp) + wp->w_height,
2113 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
2114 '@', '@', hl_attr(HLF_AT));
2115 set_empty_rows(wp, srow);
2116 wp->w_botline = lnum;
2117 }
2118 else
2119 {
2120 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
2121 wp->w_botline = lnum;
2122 }
2123 }
2124 else
2125 {
2126#ifdef FEAT_VERTSPLIT
2127 draw_vsep_win(wp, row);
2128#endif
2129 if (eof) /* we hit the end of the file */
2130 {
2131 wp->w_botline = buf->b_ml.ml_line_count + 1;
2132#ifdef FEAT_DIFF
2133 j = diff_check_fill(wp, wp->w_botline);
2134 if (j > 0 && !wp->w_botfill)
2135 {
2136 /*
2137 * Display filler lines at the end of the file
2138 */
2139 if (char2cells(fill_diff) > 1)
2140 i = '-';
2141 else
2142 i = fill_diff;
2143 if (row + j > wp->w_height)
2144 j = wp->w_height - row;
2145 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2146 row += j;
2147 }
2148#endif
2149 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002150 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 wp->w_botline = lnum;
2152
2153 /* make sure the rest of the screen is blank */
2154 /* put '~'s on rows that aren't part of the file. */
2155 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_AT);
2156 }
2157
2158 /* Reset the type of redrawing required, the window has been updated. */
2159 wp->w_redr_type = 0;
2160#ifdef FEAT_DIFF
2161 wp->w_old_topfill = wp->w_topfill;
2162 wp->w_old_botfill = wp->w_botfill;
2163#endif
2164
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002165 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 {
2167 /*
2168 * There is a trick with w_botline. If we invalidate it on each
2169 * change that might modify it, this will cause a lot of expensive
2170 * calls to plines() in update_topline() each time. Therefore the
2171 * value of w_botline is often approximated, and this value is used to
2172 * compute the value of w_topline. If the value of w_botline was
2173 * wrong, check that the value of w_topline is correct (cursor is on
2174 * the visible part of the text). If it's not, we need to redraw
2175 * again. Mostly this just means scrolling up a few lines, so it
2176 * doesn't look too bad. Only do this for the current window (where
2177 * changes are relevant).
2178 */
2179 wp->w_valid |= VALID_BOTLINE;
2180 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2181 {
2182 recursive = TRUE;
2183 curwin->w_valid &= ~VALID_TOPLINE;
2184 update_topline(); /* may invalidate w_botline again */
2185 if (must_redraw != 0)
2186 {
2187 /* Don't update for changes in buffer again. */
2188 i = curbuf->b_mod_set;
2189 curbuf->b_mod_set = FALSE;
2190 win_update(curwin);
2191 must_redraw = 0;
2192 curbuf->b_mod_set = i;
2193 }
2194 recursive = FALSE;
2195 }
2196 }
2197
2198#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2199 /* restore got_int, unless CTRL-C was hit while redrawing */
2200 if (!got_int)
2201 got_int = save_got_int;
2202#endif
2203}
2204
2205#ifdef FEAT_SIGNS
2206static int draw_signcolumn __ARGS((win_T *wp));
2207
2208/*
2209 * Return TRUE when window "wp" has a column to draw signs in.
2210 */
2211 static int
2212draw_signcolumn(wp)
2213 win_T *wp;
2214{
2215 return (wp->w_buffer->b_signlist != NULL
2216# ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002217 || netbeans_active()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218# endif
2219 );
2220}
2221#endif
2222
2223/*
2224 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2225 * as the filler character.
2226 */
2227 static void
2228win_draw_end(wp, c1, c2, row, endrow, hl)
2229 win_T *wp;
2230 int c1;
2231 int c2;
2232 int row;
2233 int endrow;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002234 hlf_T hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235{
2236#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2237 int n = 0;
2238# define FDC_OFF n
2239#else
2240# define FDC_OFF 0
2241#endif
Bram Moolenaar1c934292015-01-27 16:39:29 +01002242#ifdef FEAT_FOLDING
2243 int fdc = compute_foldcolumn(wp, 0);
2244#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245
2246#ifdef FEAT_RIGHTLEFT
2247 if (wp->w_p_rl)
2248 {
2249 /* No check for cmdline window: should never be right-left. */
2250# ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002251 n = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252
2253 if (n > 0)
2254 {
2255 /* draw the fold column at the right */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002256 if (n > W_WIDTH(wp))
2257 n = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2259 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
2260 ' ', ' ', hl_attr(HLF_FC));
2261 }
2262# endif
2263# ifdef FEAT_SIGNS
2264 if (draw_signcolumn(wp))
2265 {
2266 int nn = n + 2;
2267
2268 /* draw the sign column left of the fold column */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002269 if (nn > W_WIDTH(wp))
2270 nn = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2272 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
2273 ' ', ' ', hl_attr(HLF_SC));
2274 n = nn;
2275 }
2276# endif
2277 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2278 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
2279 c2, c2, hl_attr(hl));
2280 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2281 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
2282 c1, c2, hl_attr(hl));
2283 }
2284 else
2285#endif
2286 {
2287#ifdef FEAT_CMDWIN
2288 if (cmdwin_type != 0 && wp == curwin)
2289 {
2290 /* draw the cmdline character in the leftmost column */
2291 n = 1;
2292 if (n > wp->w_width)
2293 n = wp->w_width;
2294 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2295 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
2296 cmdwin_type, ' ', hl_attr(HLF_AT));
2297 }
2298#endif
2299#ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002300 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002302 int nn = n + fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303
2304 /* draw the fold column at the left */
2305 if (nn > W_WIDTH(wp))
2306 nn = W_WIDTH(wp);
2307 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2308 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2309 ' ', ' ', hl_attr(HLF_FC));
2310 n = nn;
2311 }
2312#endif
2313#ifdef FEAT_SIGNS
2314 if (draw_signcolumn(wp))
2315 {
2316 int nn = n + 2;
2317
2318 /* draw the sign column after the fold column */
2319 if (nn > W_WIDTH(wp))
2320 nn = W_WIDTH(wp);
2321 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2322 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2323 ' ', ' ', hl_attr(HLF_SC));
2324 n = nn;
2325 }
2326#endif
2327 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2328 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
2329 c1, c2, hl_attr(hl));
2330 }
2331 set_empty_rows(wp, row);
2332}
2333
Bram Moolenaar1a384422010-07-14 19:53:30 +02002334#ifdef FEAT_SYN_HL
2335static int advance_color_col __ARGS((int vcol, int **color_cols));
2336
2337/*
2338 * Advance **color_cols and return TRUE when there are columns to draw.
2339 */
2340 static int
2341advance_color_col(vcol, color_cols)
2342 int vcol;
2343 int **color_cols;
2344{
2345 while (**color_cols >= 0 && vcol > **color_cols)
2346 ++*color_cols;
2347 return (**color_cols >= 0);
2348}
2349#endif
2350
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351#ifdef FEAT_FOLDING
2352/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002353 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2354 * space is available for window "wp", minus "col".
2355 */
2356 static int
2357compute_foldcolumn(wp, col)
2358 win_T *wp;
2359 int col;
2360{
2361 int fdc = wp->w_p_fdc;
2362 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
2363 int wwidth = W_WIDTH(wp);
2364
2365 if (fdc > wwidth - (col + wmw))
2366 fdc = wwidth - (col + wmw);
2367 return fdc;
2368}
2369
2370/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 * Display one folded line.
2372 */
2373 static void
2374fold_line(wp, fold_count, foldinfo, lnum, row)
2375 win_T *wp;
2376 long fold_count;
2377 foldinfo_T *foldinfo;
2378 linenr_T lnum;
2379 int row;
2380{
2381 char_u buf[51];
2382 pos_T *top, *bot;
2383 linenr_T lnume = lnum + fold_count - 1;
2384 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002385 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 int col;
2388 int txtcol;
2389 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002390 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391
2392 /* Build the fold line:
2393 * 1. Add the cmdwin_type for the command-line window
2394 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002395 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 * 4. Compose the text
2397 * 5. Add the text
2398 * 6. set highlighting for the Visual area an other text
2399 */
2400 col = 0;
2401
2402 /*
2403 * 1. Add the cmdwin_type for the command-line window
2404 * Ignores 'rightleft', this window is never right-left.
2405 */
2406#ifdef FEAT_CMDWIN
2407 if (cmdwin_type != 0 && wp == curwin)
2408 {
2409 ScreenLines[off] = cmdwin_type;
2410 ScreenAttrs[off] = hl_attr(HLF_AT);
2411#ifdef FEAT_MBYTE
2412 if (enc_utf8)
2413 ScreenLinesUC[off] = 0;
2414#endif
2415 ++col;
2416 }
2417#endif
2418
2419 /*
2420 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002421 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002423 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 if (fdc > 0)
2425 {
2426 fill_foldcolumn(buf, wp, TRUE, lnum);
2427#ifdef FEAT_RIGHTLEFT
2428 if (wp->w_p_rl)
2429 {
2430 int i;
2431
2432 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
2433 hl_attr(HLF_FC));
2434 /* reverse the fold column */
2435 for (i = 0; i < fdc; ++i)
2436 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2437 }
2438 else
2439#endif
2440 copy_text_attr(off + col, buf, fdc, hl_attr(HLF_FC));
2441 col += fdc;
2442 }
2443
2444#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002445# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2446 for (ri = 0; ri < l; ++ri) \
2447 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2448 else \
2449 for (ri = 0; ri < l; ++ri) \
2450 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002452# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2453 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454#endif
2455
Bram Moolenaar64486672010-05-16 15:46:46 +02002456 /* Set all attributes of the 'number' or 'relativenumber' column and the
2457 * text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002458 RL_MEMSET(col, hl_attr(HLF_FL), W_WIDTH(wp) - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459
2460#ifdef FEAT_SIGNS
2461 /* If signs are being displayed, add two spaces. */
2462 if (draw_signcolumn(wp))
2463 {
2464 len = W_WIDTH(wp) - col;
2465 if (len > 0)
2466 {
2467 if (len > 2)
2468 len = 2;
2469# ifdef FEAT_RIGHTLEFT
2470 if (wp->w_p_rl)
2471 /* the line number isn't reversed */
2472 copy_text_attr(off + W_WIDTH(wp) - len - col,
2473 (char_u *)" ", len, hl_attr(HLF_FL));
2474 else
2475# endif
2476 copy_text_attr(off + col, (char_u *)" ", len, hl_attr(HLF_FL));
2477 col += len;
2478 }
2479 }
2480#endif
2481
2482 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002483 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002485 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 {
2487 len = W_WIDTH(wp) - col;
2488 if (len > 0)
2489 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002490 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002491 long num;
2492 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002493
2494 if (len > w + 1)
2495 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002496
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002497 if (wp->w_p_nu && !wp->w_p_rnu)
2498 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002499 num = (long)lnum;
2500 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002501 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002502 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002503 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002504 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002505 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002506 /* 'number' + 'relativenumber': cursor line shows absolute
2507 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002508 num = lnum;
2509 fmt = "%-*ld ";
2510 }
2511 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002512
Bram Moolenaar700e7342013-01-30 12:31:36 +01002513 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514#ifdef FEAT_RIGHTLEFT
2515 if (wp->w_p_rl)
2516 /* the line number isn't reversed */
2517 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
2518 hl_attr(HLF_FL));
2519 else
2520#endif
2521 copy_text_attr(off + col, buf, len, hl_attr(HLF_FL));
2522 col += len;
2523 }
2524 }
2525
2526 /*
2527 * 4. Compose the folded-line string with 'foldtext', if set.
2528 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002529 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530
2531 txtcol = col; /* remember where text starts */
2532
2533 /*
2534 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2535 * Right-left text is put in columns 0 - number-col, normal text is put
2536 * in columns number-col - window-width.
2537 */
2538#ifdef FEAT_MBYTE
2539 if (has_mbyte)
2540 {
2541 int cells;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002542 int u8c, u8cc[MAX_MCO];
2543 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 int idx;
2545 int c_len;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002546 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547# ifdef FEAT_ARABIC
2548 int prev_c = 0; /* previous Arabic character */
2549 int prev_c1 = 0; /* first composing char for prev_c */
2550# endif
2551
2552# ifdef FEAT_RIGHTLEFT
2553 if (wp->w_p_rl)
2554 idx = off;
2555 else
2556# endif
2557 idx = off + col;
2558
2559 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2560 for (p = text; *p != NUL; )
2561 {
2562 cells = (*mb_ptr2cells)(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002563 c_len = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 if (col + cells > W_WIDTH(wp)
2565# ifdef FEAT_RIGHTLEFT
2566 - (wp->w_p_rl ? col : 0)
2567# endif
2568 )
2569 break;
2570 ScreenLines[idx] = *p;
2571 if (enc_utf8)
2572 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002573 u8c = utfc_ptr2char(p, u8cc);
2574 if (*p < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 {
2576 ScreenLinesUC[idx] = 0;
2577#ifdef FEAT_ARABIC
2578 prev_c = u8c;
2579#endif
2580 }
2581 else
2582 {
2583#ifdef FEAT_ARABIC
2584 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2585 {
2586 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002587 int pc, pc1, nc;
2588 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 int firstbyte = *p;
2590
2591 /* The idea of what is the previous and next
2592 * character depends on 'rightleft'. */
2593 if (wp->w_p_rl)
2594 {
2595 pc = prev_c;
2596 pc1 = prev_c1;
2597 nc = utf_ptr2char(p + c_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002598 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 }
2600 else
2601 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002602 pc = utfc_ptr2char(p + c_len, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002604 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 }
2606 prev_c = u8c;
2607
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002608 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 pc, pc1, nc);
2610 ScreenLines[idx] = firstbyte;
2611 }
2612 else
2613 prev_c = u8c;
2614#endif
2615 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar11936362007-09-17 20:39:42 +00002616#ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 if (u8c >= 0x10000)
2618 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2619 else
Bram Moolenaar11936362007-09-17 20:39:42 +00002620#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 ScreenLinesUC[idx] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002622 for (i = 0; i < Screen_mco; ++i)
2623 {
2624 ScreenLinesC[i][idx] = u8cc[i];
2625 if (u8cc[i] == 0)
2626 break;
2627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 }
2629 if (cells > 1)
2630 ScreenLines[idx + 1] = 0;
2631 }
Bram Moolenaar990bb662010-02-03 15:48:04 +01002632 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2633 /* double-byte single width character */
2634 ScreenLines2[idx] = p[1];
2635 else if (cells > 1)
2636 /* double-width character */
2637 ScreenLines[idx + 1] = p[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 col += cells;
2639 idx += cells;
2640 p += c_len;
2641 }
2642 }
2643 else
2644#endif
2645 {
2646 len = (int)STRLEN(text);
2647 if (len > W_WIDTH(wp) - col)
2648 len = W_WIDTH(wp) - col;
2649 if (len > 0)
2650 {
2651#ifdef FEAT_RIGHTLEFT
2652 if (wp->w_p_rl)
2653 STRNCPY(current_ScreenLine, text, len);
2654 else
2655#endif
2656 STRNCPY(current_ScreenLine + col, text, len);
2657 col += len;
2658 }
2659 }
2660
2661 /* Fill the rest of the line with the fold filler */
2662#ifdef FEAT_RIGHTLEFT
2663 if (wp->w_p_rl)
2664 col -= txtcol;
2665#endif
2666 while (col < W_WIDTH(wp)
2667#ifdef FEAT_RIGHTLEFT
2668 - (wp->w_p_rl ? txtcol : 0)
2669#endif
2670 )
2671 {
2672#ifdef FEAT_MBYTE
2673 if (enc_utf8)
2674 {
2675 if (fill_fold >= 0x80)
2676 {
2677 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002678 ScreenLinesC[0][off + col] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 }
2680 else
2681 ScreenLinesUC[off + col] = 0;
2682 }
2683#endif
2684 ScreenLines[off + col++] = fill_fold;
2685 }
2686
2687 if (text != buf)
2688 vim_free(text);
2689
2690 /*
2691 * 6. set highlighting for the Visual area an other text.
2692 * If all folded lines are in the Visual area, highlight the line.
2693 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2695 {
2696 if (ltoreq(curwin->w_cursor, VIsual))
2697 {
2698 /* Visual is after curwin->w_cursor */
2699 top = &curwin->w_cursor;
2700 bot = &VIsual;
2701 }
2702 else
2703 {
2704 /* Visual is before curwin->w_cursor */
2705 top = &VIsual;
2706 bot = &curwin->w_cursor;
2707 }
2708 if (lnum >= top->lnum
2709 && lnume <= bot->lnum
2710 && (VIsual_mode != 'v'
2711 || ((lnum > top->lnum
2712 || (lnum == top->lnum
2713 && top->col == 0))
2714 && (lnume < bot->lnum
2715 || (lnume == bot->lnum
2716 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002717 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 {
2719 if (VIsual_mode == Ctrl_V)
2720 {
2721 /* Visual block mode: highlight the chars part of the block */
2722 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2723 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002724 if (wp->w_old_cursor_lcol != MAXCOL
2725 && wp->w_old_cursor_lcol + txtcol
2726 < (colnr_T)W_WIDTH(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 len = wp->w_old_cursor_lcol;
2728 else
2729 len = W_WIDTH(wp) - txtcol;
2730 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, hl_attr(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002731 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 }
2733 }
2734 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002735 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 /* Set all attributes of the text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002737 RL_MEMSET(txtcol, hl_attr(HLF_V), W_WIDTH(wp) - txtcol);
2738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 }
2740 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002742#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002743 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2744 if (wp->w_p_cc_cols)
2745 {
2746 int i = 0;
2747 int j = wp->w_p_cc_cols[i];
2748 int old_txtcol = txtcol;
2749
2750 while (j > -1)
2751 {
2752 txtcol += j;
2753 if (wp->w_p_wrap)
2754 txtcol -= wp->w_skipcol;
2755 else
2756 txtcol -= wp->w_leftcol;
2757 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2758 ScreenAttrs[off + txtcol] = hl_combine_attr(
2759 ScreenAttrs[off + txtcol], hl_attr(HLF_MC));
2760 txtcol = old_txtcol;
2761 j = wp->w_p_cc_cols[++i];
2762 }
2763 }
2764
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002765 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002766 if (wp->w_p_cuc)
2767 {
2768 txtcol += wp->w_virtcol;
2769 if (wp->w_p_wrap)
2770 txtcol -= wp->w_skipcol;
2771 else
2772 txtcol -= wp->w_leftcol;
2773 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2774 ScreenAttrs[off + txtcol] = hl_combine_attr(
2775 ScreenAttrs[off + txtcol], hl_attr(HLF_CUC));
2776 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002777#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778
2779 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2780 (int)W_WIDTH(wp), FALSE);
2781
2782 /*
2783 * Update w_cline_height and w_cline_folded if the cursor line was
2784 * updated (saves a call to plines() later).
2785 */
2786 if (wp == curwin
2787 && lnum <= curwin->w_cursor.lnum
2788 && lnume >= curwin->w_cursor.lnum)
2789 {
2790 curwin->w_cline_row = row;
2791 curwin->w_cline_height = 1;
2792 curwin->w_cline_folded = TRUE;
2793 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2794 }
2795}
2796
2797/*
2798 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2799 */
2800 static void
2801copy_text_attr(off, buf, len, attr)
2802 int off;
2803 char_u *buf;
2804 int len;
2805 int attr;
2806{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002807 int i;
2808
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 mch_memmove(ScreenLines + off, buf, (size_t)len);
2810# ifdef FEAT_MBYTE
2811 if (enc_utf8)
2812 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2813# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002814 for (i = 0; i < len; ++i)
2815 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816}
2817
2818/*
2819 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002820 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 */
2822 static void
2823fill_foldcolumn(p, wp, closed, lnum)
2824 char_u *p;
2825 win_T *wp;
2826 int closed; /* TRUE of FALSE */
2827 linenr_T lnum; /* current line number */
2828{
2829 int i = 0;
2830 int level;
2831 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002832 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002833 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834
2835 /* Init to all spaces. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002836 copy_spaces(p, (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837
2838 level = win_foldinfo.fi_level;
2839 if (level > 0)
2840 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002841 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002842 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002843
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 /* If the column is too narrow, we start at the lowest level that
2845 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002846 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 if (first_level < 1)
2848 first_level = 1;
2849
Bram Moolenaar1c934292015-01-27 16:39:29 +01002850 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 {
2852 if (win_foldinfo.fi_lnum == lnum
2853 && first_level + i >= win_foldinfo.fi_low_level)
2854 p[i] = '-';
2855 else if (first_level == 1)
2856 p[i] = '|';
2857 else if (first_level + i <= 9)
2858 p[i] = '0' + first_level + i;
2859 else
2860 p[i] = '>';
2861 if (first_level + i == level)
2862 break;
2863 }
2864 }
2865 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002866 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867}
2868#endif /* FEAT_FOLDING */
2869
2870/*
2871 * Display line "lnum" of window 'wp' on the screen.
2872 * Start at row "startrow", stop when "endrow" is reached.
2873 * wp->w_virtcol needs to be valid.
2874 *
2875 * Return the number of last row the line occupies.
2876 */
2877 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +00002878win_line(wp, lnum, startrow, endrow, nochange)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 win_T *wp;
2880 linenr_T lnum;
2881 int startrow;
2882 int endrow;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002883 int nochange UNUSED; /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884{
2885 int col; /* visual column on screen */
2886 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2887 int c = 0; /* init for GCC */
2888 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01002889#ifdef FEAT_LINEBREAK
2890 long vcol_sbr = -1; /* virtual column after showbreak */
2891#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 long vcol_prev = -1; /* "vcol" of previous character */
2893 char_u *line; /* current line */
2894 char_u *ptr; /* current position in "line" */
2895 int row; /* row in the window, excl w_winrow */
2896 int screen_row; /* row on the screen, incl w_winrow */
2897
2898 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2899 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00002900 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02002901 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 int c_extra = NUL; /* extra chars, all the same */
2903 int extra_attr = 0; /* attributes when n_extra != 0 */
2904 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2905 displaying lcs_eol at end-of-line */
2906 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2907 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2908
2909 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2910 int saved_n_extra = 0;
2911 char_u *saved_p_extra = NULL;
2912 int saved_c_extra = 0;
2913 int saved_char_attr = 0;
2914
2915 int n_attr = 0; /* chars with special attr */
2916 int saved_attr2 = 0; /* char_attr saved for n_attr */
2917 int n_attr3 = 0; /* chars with overruling special attr */
2918 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2919
2920 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2921
2922 int fromcol, tocol; /* start/end of inverting */
2923 int fromcol_prev = -2; /* start of inverting after cursor */
2924 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002926 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 pos_T pos;
2928 long v;
2929
2930 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002931 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2933 in this line */
2934 int attr = 0; /* attributes for area highlighting */
2935 int area_attr = 0; /* attributes desired by highlighting */
2936 int search_attr = 0; /* attributes desired by 'hlsearch' */
2937#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002938 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 int syntax_attr = 0; /* attributes desired by syntax */
2940 int has_syntax = FALSE; /* this buffer has syntax highl. */
2941 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00002942 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02002943 int draw_color_col = FALSE; /* highlight colorcolumn */
2944 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002945#endif
2946#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002947 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002948# define SPWORDLEN 150
2949 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00002950 int nextlinecol = 0; /* column where nextline[] starts */
2951 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00002952 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00002953 int spell_attr = 0; /* attributes desired by spelling */
2954 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00002955 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2956 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00002957 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002958 static int cap_col = -1; /* column to check for Cap word */
2959 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002960 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961#endif
2962 int extra_check; /* has syntax or linebreak */
2963#ifdef FEAT_MBYTE
2964 int multi_attr = 0; /* attributes desired by multibyte */
2965 int mb_l = 1; /* multi-byte byte length */
2966 int mb_c = 0; /* decoded multi-byte character */
2967 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002968 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969#endif
2970#ifdef FEAT_DIFF
2971 int filler_lines; /* nr of filler lines to be drawn */
2972 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002973 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 int change_start = MAXCOL; /* first col of changed area */
2975 int change_end = -1; /* last col of changed area */
2976#endif
2977 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
2978#ifdef FEAT_LINEBREAK
2979 int need_showbreak = FALSE;
2980#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002981#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
2982 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00002984 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985#endif
2986#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00002987 matchitem_T *cur; /* points to the match list */
2988 match_T *shl; /* points to search_hl or a match */
2989 int shl_flag; /* flag to indicate whether search_hl
2990 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02002991 int pos_inprogress; /* marks that position match search is
2992 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00002993 int prevcol_hl_flag; /* flag to indicate whether prevcol
2994 equals startcol of search_hl or one
2995 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996#endif
2997#ifdef FEAT_ARABIC
2998 int prev_c = 0; /* previous Arabic character */
2999 int prev_c1 = 0; /* first composing char for prev_c */
3000#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003001#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003002 int did_line_attr = 0;
3003#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004
3005 /* draw_state: items that are drawn in sequence: */
3006#define WL_START 0 /* nothing done yet */
3007#ifdef FEAT_CMDWIN
3008# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3009#else
3010# define WL_CMDLINE WL_START
3011#endif
3012#ifdef FEAT_FOLDING
3013# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3014#else
3015# define WL_FOLD WL_CMDLINE
3016#endif
3017#ifdef FEAT_SIGNS
3018# define WL_SIGN WL_FOLD + 1 /* column for signs */
3019#else
3020# define WL_SIGN WL_FOLD /* column for signs */
3021#endif
3022#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003023#ifdef FEAT_LINEBREAK
3024# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003026# define WL_BRI WL_NR
3027#endif
3028#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3029# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3030#else
3031# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032#endif
3033#define WL_LINE WL_SBR + 1 /* text in the line */
3034 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003035#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 int feedback_col = 0;
3037 int feedback_old_attr = -1;
3038#endif
3039
Bram Moolenaar860cae12010-06-05 23:22:07 +02003040#ifdef FEAT_CONCEAL
3041 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003042 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003043 int prev_syntax_id = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003044 int conceal_attr = hl_attr(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003045 int is_concealing = FALSE;
3046 int boguscols = 0; /* nonexistent columns added to force
3047 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003048 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003049 int did_wcol = FALSE;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003050 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003051# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003052# define FIX_FOR_BOGUSCOLS \
3053 { \
3054 n_extra += vcol_off; \
3055 vcol -= vcol_off; \
3056 vcol_off = 0; \
3057 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003058 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003059 boguscols = 0; \
3060 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003061#else
3062# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003063#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064
3065 if (startrow > endrow) /* past the end already! */
3066 return startrow;
3067
3068 row = startrow;
3069 screen_row = row + W_WINROW(wp);
3070
3071 /*
3072 * To speed up the loop below, set extra_check when there is linebreak,
3073 * trailing white space and/or syntax processing to be done.
3074 */
3075#ifdef FEAT_LINEBREAK
3076 extra_check = wp->w_p_lbr;
3077#else
3078 extra_check = 0;
3079#endif
3080#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02003081 if (syntax_present(wp) && !wp->w_s->b_syn_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 {
3083 /* Prepare for syntax highlighting in this line. When there is an
3084 * error, stop syntax highlighting. */
3085 save_did_emsg = did_emsg;
3086 did_emsg = FALSE;
3087 syntax_start(wp, lnum);
3088 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003089 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 else
3091 {
3092 did_emsg = save_did_emsg;
3093 has_syntax = TRUE;
3094 extra_check = TRUE;
3095 }
3096 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003097
3098 /* Check for columns to display for 'colorcolumn'. */
3099 color_cols = wp->w_p_cc_cols;
3100 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003101 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003102#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003103
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003104#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003105 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02003106 && *wp->w_s->b_p_spl != NUL
3107 && wp->w_s->b_langp.ga_len > 0
3108 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003109 {
3110 /* Prepare for spell checking. */
3111 has_spell = TRUE;
3112 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003113
3114 /* Get the start of the next line, so that words that wrap to the next
3115 * line are found too: "et<line-break>al.".
3116 * Trick: skip a few chars for C/shell/Vim comments */
3117 nextline[SPWORDLEN] = NUL;
3118 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3119 {
3120 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3121 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3122 }
3123
3124 /* When a word wrapped from the previous line the start of the current
3125 * line is valid. */
3126 if (lnum == checked_lnum)
3127 cur_checked_col = checked_col;
3128 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003129
3130 /* When there was a sentence end in the previous line may require a
3131 * word starting with capital in this line. In line 1 always check
3132 * the first word. */
3133 if (lnum != capcol_lnum)
3134 cap_col = -1;
3135 if (lnum == 1)
3136 cap_col = 0;
3137 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139#endif
3140
3141 /*
3142 * handle visual active in this window
3143 */
3144 fromcol = -10;
3145 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3147 {
3148 /* Visual is after curwin->w_cursor */
3149 if (ltoreq(curwin->w_cursor, VIsual))
3150 {
3151 top = &curwin->w_cursor;
3152 bot = &VIsual;
3153 }
3154 else /* Visual is before curwin->w_cursor */
3155 {
3156 top = &VIsual;
3157 bot = &curwin->w_cursor;
3158 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003159 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 if (VIsual_mode == Ctrl_V) /* block mode */
3161 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003162 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 {
3164 fromcol = wp->w_old_cursor_fcol;
3165 tocol = wp->w_old_cursor_lcol;
3166 }
3167 }
3168 else /* non-block mode */
3169 {
3170 if (lnum > top->lnum && lnum <= bot->lnum)
3171 fromcol = 0;
3172 else if (lnum == top->lnum)
3173 {
3174 if (VIsual_mode == 'V') /* linewise */
3175 fromcol = 0;
3176 else
3177 {
3178 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3179 if (gchar_pos(top) == NUL)
3180 tocol = fromcol + 1;
3181 }
3182 }
3183 if (VIsual_mode != 'V' && lnum == bot->lnum)
3184 {
3185 if (*p_sel == 'e' && bot->col == 0
3186#ifdef FEAT_VIRTUALEDIT
3187 && bot->coladd == 0
3188#endif
3189 )
3190 {
3191 fromcol = -10;
3192 tocol = MAXCOL;
3193 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003194 else if (bot->col == MAXCOL)
3195 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 else
3197 {
3198 pos = *bot;
3199 if (*p_sel == 'e')
3200 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3201 else
3202 {
3203 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3204 ++tocol;
3205 }
3206 }
3207 }
3208 }
3209
3210#ifndef MSDOS
3211 /* Check if the character under the cursor should not be inverted */
3212 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
3213# ifdef FEAT_GUI
3214 && !gui.in_use
3215# endif
3216 )
3217 noinvcur = TRUE;
3218#endif
3219
3220 /* if inverting in this line set area_highlighting */
3221 if (fromcol >= 0)
3222 {
3223 area_highlighting = TRUE;
3224 attr = hl_attr(HLF_V);
3225#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003226 if ((clip_star.available && !clip_star.owned
3227 && clip_isautosel_star())
3228 || (clip_plus.available && !clip_plus.owned
3229 && clip_isautosel_plus()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 attr = hl_attr(HLF_VNC);
3231#endif
3232 }
3233 }
3234
3235 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003236 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003238 else if (highlight_match
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 && wp == curwin
3240 && lnum >= curwin->w_cursor.lnum
3241 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3242 {
3243 if (lnum == curwin->w_cursor.lnum)
3244 getvcol(curwin, &(curwin->w_cursor),
3245 (colnr_T *)&fromcol, NULL, NULL);
3246 else
3247 fromcol = 0;
3248 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3249 {
3250 pos.lnum = lnum;
3251 pos.col = search_match_endcol;
3252 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3253 }
3254 else
3255 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003256 /* do at least one character; happens when past end of line */
3257 if (fromcol == tocol)
3258 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 area_highlighting = TRUE;
3260 attr = hl_attr(HLF_I);
3261 }
3262
3263#ifdef FEAT_DIFF
3264 filler_lines = diff_check(wp, lnum);
3265 if (filler_lines < 0)
3266 {
3267 if (filler_lines == -1)
3268 {
3269 if (diff_find_change(wp, lnum, &change_start, &change_end))
3270 diff_hlf = HLF_ADD; /* added line */
3271 else if (change_start == 0)
3272 diff_hlf = HLF_TXD; /* changed text */
3273 else
3274 diff_hlf = HLF_CHD; /* changed line */
3275 }
3276 else
3277 diff_hlf = HLF_ADD; /* added line */
3278 filler_lines = 0;
3279 area_highlighting = TRUE;
3280 }
3281 if (lnum == wp->w_topline)
3282 filler_lines = wp->w_topfill;
3283 filler_todo = filler_lines;
3284#endif
3285
3286#ifdef LINE_ATTR
3287# ifdef FEAT_SIGNS
3288 /* If this line has a sign with line highlighting set line_attr. */
3289 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3290 if (v != 0)
3291 line_attr = sign_get_attr((int)v, TRUE);
3292# endif
3293# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
3294 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003295 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 line_attr = hl_attr(HLF_L);
3297# endif
3298 if (line_attr != 0)
3299 area_highlighting = TRUE;
3300#endif
3301
3302 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3303 ptr = line;
3304
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003305#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003306 if (has_spell)
3307 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003308 /* For checking first word with a capital skip white space. */
3309 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003310 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003311
Bram Moolenaar30abd282005-06-22 22:35:10 +00003312 /* To be able to spell-check over line boundaries copy the end of the
3313 * current line into nextline[]. Above the start of the next line was
3314 * copied to nextline[SPWORDLEN]. */
3315 if (nextline[SPWORDLEN] == NUL)
3316 {
3317 /* No next line or it is empty. */
3318 nextlinecol = MAXCOL;
3319 nextline_idx = 0;
3320 }
3321 else
3322 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003323 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003324 if (v < SPWORDLEN)
3325 {
3326 /* Short line, use it completely and append the start of the
3327 * next line. */
3328 nextlinecol = 0;
3329 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003330 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003331 nextline_idx = v + 1;
3332 }
3333 else
3334 {
3335 /* Long line, use only the last SPWORDLEN bytes. */
3336 nextlinecol = v - SPWORDLEN;
3337 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3338 nextline_idx = SPWORDLEN + 1;
3339 }
3340 }
3341 }
3342#endif
3343
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 /* find start of trailing whitespace */
3345 if (wp->w_p_list && lcs_trail)
3346 {
3347 trailcol = (colnr_T)STRLEN(ptr);
3348 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
3349 --trailcol;
3350 trailcol += (colnr_T) (ptr - line);
3351 extra_check = TRUE;
3352 }
3353
3354 /*
3355 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3356 * first character to be displayed.
3357 */
3358 if (wp->w_p_wrap)
3359 v = wp->w_skipcol;
3360 else
3361 v = wp->w_leftcol;
3362 if (v > 0)
3363 {
3364#ifdef FEAT_MBYTE
3365 char_u *prev_ptr = ptr;
3366#endif
3367 while (vcol < v && *ptr != NUL)
3368 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003369 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 vcol += c;
3371#ifdef FEAT_MBYTE
3372 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003374 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 }
3376
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003377 /* When:
3378 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003379 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003380 * - 'virtualedit' is set, or
3381 * - the visual mode is active,
3382 * the end of the line may be before the start of the displayed part.
3383 */
3384 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003385#ifdef FEAT_SYN_HL
3386 wp->w_p_cuc || draw_color_col ||
3387#endif
3388#ifdef FEAT_VIRTUALEDIT
3389 virtual_active() ||
3390#endif
3391 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003392 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395
3396 /* Handle a character that's not completely on the screen: Put ptr at
3397 * that character but skip the first few screen characters. */
3398 if (vcol > v)
3399 {
3400 vcol -= c;
3401#ifdef FEAT_MBYTE
3402 ptr = prev_ptr;
3403#else
3404 --ptr;
3405#endif
3406 n_skip = v - vcol;
3407 }
3408
3409 /*
3410 * Adjust for when the inverted text is before the screen,
3411 * and when the start of the inverted text is before the screen.
3412 */
3413 if (tocol <= vcol)
3414 fromcol = 0;
3415 else if (fromcol >= 0 && fromcol < vcol)
3416 fromcol = vcol;
3417
3418#ifdef FEAT_LINEBREAK
3419 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3420 if (wp->w_p_wrap)
3421 need_showbreak = TRUE;
3422#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003423#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003424 /* When spell checking a word we need to figure out the start of the
3425 * word and if it's badly spelled or not. */
3426 if (has_spell)
3427 {
3428 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003429 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003430 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003431
3432 pos = wp->w_cursor;
3433 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003434 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003435 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003436
3437 /* spell_move_to() may call ml_get() and make "line" invalid */
3438 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3439 ptr = line + linecol;
3440
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003441 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003442 {
3443 /* no bad word found at line start, don't check until end of a
3444 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003445 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003446 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003447 }
3448 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003449 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003450 /* bad word found, use attributes until end of word */
3451 word_end = wp->w_cursor.col + len + 1;
3452
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003453 /* Turn index into actual attributes. */
3454 if (spell_hlf != HLF_COUNT)
3455 spell_attr = highlight_attr[spell_hlf];
3456 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003457 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003458
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003459# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003460 /* Need to restart syntax highlighting for this line. */
3461 if (has_syntax)
3462 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003463# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003464 }
3465#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 }
3467
3468 /*
3469 * Correct highlighting for cursor that can't be disabled.
3470 * Avoids having to check this for each character.
3471 */
3472 if (fromcol >= 0)
3473 {
3474 if (noinvcur)
3475 {
3476 if ((colnr_T)fromcol == wp->w_virtcol)
3477 {
3478 /* highlighting starts at cursor, let it start just after the
3479 * cursor */
3480 fromcol_prev = fromcol;
3481 fromcol = -1;
3482 }
3483 else if ((colnr_T)fromcol < wp->w_virtcol)
3484 /* restart highlighting after the cursor */
3485 fromcol_prev = wp->w_virtcol;
3486 }
3487 if (fromcol >= tocol)
3488 fromcol = -1;
3489 }
3490
3491#ifdef FEAT_SEARCH_EXTRA
3492 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003493 * Handle highlighting the last used search pattern and matches.
3494 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003496 cur = wp->w_match_head;
3497 shl_flag = FALSE;
3498 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003500 if (shl_flag == FALSE)
3501 {
3502 shl = &search_hl;
3503 shl_flag = TRUE;
3504 }
3505 else
3506 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003507 shl->startcol = MAXCOL;
3508 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 shl->attr_cur = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003510 v = (long)(ptr - line);
3511 if (cur != NULL)
3512 cur->pos.cur = 0;
3513 next_search_hl(wp, shl, lnum, (colnr_T)v, cur);
3514
3515 /* Need to get the line again, a multi-line regexp may have made it
3516 * invalid. */
3517 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3518 ptr = line + v;
3519
3520 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003522 if (shl->lnum == lnum)
3523 shl->startcol = shl->rm.startpos[0].col;
3524 else
3525 shl->startcol = 0;
3526 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3527 - shl->rm.startpos[0].lnum)
3528 shl->endcol = shl->rm.endpos[0].col;
3529 else
3530 shl->endcol = MAXCOL;
3531 /* Highlight one character for an empty match. */
3532 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003535 if (has_mbyte && line[shl->endcol] != NUL)
3536 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3537 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003539 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003541 if ((long)shl->startcol < v) /* match at leftcol */
3542 {
3543 shl->attr_cur = shl->attr;
3544 search_attr = shl->attr;
3545 }
3546 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003548 if (shl != &search_hl && cur != NULL)
3549 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 }
3551#endif
3552
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003553#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003554 /* Cursor line highlighting for 'cursorline' in the current window. Not
3555 * when Visual mode is active, because it's not clear what is selected
3556 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003557 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003558 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003559 {
3560 line_attr = hl_attr(HLF_CUL);
3561 area_highlighting = TRUE;
3562 }
3563#endif
3564
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003565 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 col = 0;
3567#ifdef FEAT_RIGHTLEFT
3568 if (wp->w_p_rl)
3569 {
3570 /* Rightleft window: process the text in the normal direction, but put
3571 * it in current_ScreenLine[] from right to left. Start at the
3572 * rightmost column of the window. */
3573 col = W_WIDTH(wp) - 1;
3574 off += col;
3575 }
3576#endif
3577
3578 /*
3579 * Repeat for the whole displayed line.
3580 */
3581 for (;;)
3582 {
3583 /* Skip this quickly when working on the text. */
3584 if (draw_state != WL_LINE)
3585 {
3586#ifdef FEAT_CMDWIN
3587 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3588 {
3589 draw_state = WL_CMDLINE;
3590 if (cmdwin_type != 0 && wp == curwin)
3591 {
3592 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003594 c_extra = cmdwin_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 char_attr = hl_attr(HLF_AT);
3596 }
3597 }
3598#endif
3599
3600#ifdef FEAT_FOLDING
3601 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3602 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003603 int fdc = compute_foldcolumn(wp, 0);
3604
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003606 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 {
3608 /* Draw the 'foldcolumn'. */
3609 fill_foldcolumn(extra, wp, FALSE, lnum);
Bram Moolenaar1c934292015-01-27 16:39:29 +01003610 n_extra = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 p_extra = extra;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003612 p_extra[n_extra] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 c_extra = NUL;
3614 char_attr = hl_attr(HLF_FC);
3615 }
3616 }
3617#endif
3618
3619#ifdef FEAT_SIGNS
3620 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3621 {
3622 draw_state = WL_SIGN;
3623 /* Show the sign column when there are any signs in this
3624 * buffer or when using Netbeans. */
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003625 if (draw_signcolumn(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003627 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003629 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630# endif
3631
3632 /* Draw two cells with the sign value or blank. */
3633 c_extra = ' ';
3634 char_attr = hl_attr(HLF_SC);
3635 n_extra = 2;
3636
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003637 if (row == startrow
3638#ifdef FEAT_DIFF
3639 + filler_lines && filler_todo <= 0
3640#endif
3641 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 {
3643 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3644 SIGN_TEXT);
3645# ifdef FEAT_SIGN_ICONS
3646 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3647 SIGN_ICON);
3648 if (gui.in_use && icon_sign != 0)
3649 {
3650 /* Use the image in this position. */
3651 c_extra = SIGN_BYTE;
3652# ifdef FEAT_NETBEANS_INTG
3653 if (buf_signcount(wp->w_buffer, lnum) > 1)
3654 c_extra = MULTISIGN_BYTE;
3655# endif
3656 char_attr = icon_sign;
3657 }
3658 else
3659# endif
3660 if (text_sign != 0)
3661 {
3662 p_extra = sign_get_text(text_sign);
3663 if (p_extra != NULL)
3664 {
3665 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003666 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 }
3668 char_attr = sign_get_attr(text_sign, FALSE);
3669 }
3670 }
3671 }
3672 }
3673#endif
3674
3675 if (draw_state == WL_NR - 1 && n_extra == 0)
3676 {
3677 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003678 /* Display the absolute or relative line number. After the
3679 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3680 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 && (row == startrow
3682#ifdef FEAT_DIFF
3683 + filler_lines
3684#endif
3685 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3686 {
3687 /* Draw the line number (empty space after wrapping). */
3688 if (row == startrow
3689#ifdef FEAT_DIFF
3690 + filler_lines
3691#endif
3692 )
3693 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003694 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003695 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003696
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003697 if (wp->w_p_nu && !wp->w_p_rnu)
3698 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003699 num = (long)lnum;
3700 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003701 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003702 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003703 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003704 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003705 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003706 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003707 num = lnum;
3708 fmt = "%-*ld ";
3709 }
3710 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003711
Bram Moolenaar700e7342013-01-30 12:31:36 +01003712 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003713 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 if (wp->w_skipcol > 0)
3715 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3716 *p_extra = '-';
3717#ifdef FEAT_RIGHTLEFT
3718 if (wp->w_p_rl) /* reverse line numbers */
3719 rl_mirror(extra);
3720#endif
3721 p_extra = extra;
3722 c_extra = NUL;
3723 }
3724 else
3725 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003726 n_extra = number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 char_attr = hl_attr(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003728#ifdef FEAT_SYN_HL
3729 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003730 * the current line differently.
3731 * TODO: Can we use CursorLine instead of CursorLineNr
3732 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003733 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003734 && lnum == wp->w_cursor.lnum)
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003735 char_attr = hl_attr(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003736#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 }
3738 }
3739
Bram Moolenaar597a4222014-06-25 14:39:50 +02003740#ifdef FEAT_LINEBREAK
3741 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3742 && n_extra == 0 && *p_sbr != NUL)
3743 /* draw indent after showbreak value */
3744 draw_state = WL_BRI;
3745 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3746 /* After the showbreak, draw the breakindent */
3747 draw_state = WL_BRI - 1;
3748
3749 /* draw 'breakindent': indent wrapped text accordingly */
3750 if (draw_state == WL_BRI - 1 && n_extra == 0)
3751 {
3752 draw_state = WL_BRI;
3753# ifdef FEAT_DIFF
3754# endif
3755 if (wp->w_p_bri && n_extra == 0 && row != startrow
3756#ifdef FEAT_DIFF
3757 && filler_lines == 0
3758#endif
3759 )
3760 {
3761 char_attr = 0; /* was: hl_attr(HLF_AT); */
3762#ifdef FEAT_DIFF
3763 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003764 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003765 char_attr = hl_attr(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02003766 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3767 char_attr = hl_combine_attr(char_attr,
3768 hl_attr(HLF_CUL));
3769 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02003770#endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02003771 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003772 c_extra = ' ';
3773 n_extra = get_breakindent_win(wp,
3774 ml_get_buf(wp->w_buffer, lnum, FALSE));
3775 /* Correct end of highlighted area for 'breakindent',
3776 * required when 'linebreak' is also set. */
3777 if (tocol == vcol)
3778 tocol += n_extra;
3779 }
3780 }
3781#endif
3782
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3784 if (draw_state == WL_SBR - 1 && n_extra == 0)
3785 {
3786 draw_state = WL_SBR;
3787# ifdef FEAT_DIFF
3788 if (filler_todo > 0)
3789 {
3790 /* Draw "deleted" diff line(s). */
3791 if (char2cells(fill_diff) > 1)
3792 c_extra = '-';
3793 else
3794 c_extra = fill_diff;
3795# ifdef FEAT_RIGHTLEFT
3796 if (wp->w_p_rl)
3797 n_extra = col + 1;
3798 else
3799# endif
3800 n_extra = W_WIDTH(wp) - col;
3801 char_attr = hl_attr(HLF_DED);
3802 }
3803# endif
3804# ifdef FEAT_LINEBREAK
3805 if (*p_sbr != NUL && need_showbreak)
3806 {
3807 /* Draw 'showbreak' at the start of each broken line. */
3808 p_extra = p_sbr;
3809 c_extra = NUL;
3810 n_extra = (int)STRLEN(p_sbr);
3811 char_attr = hl_attr(HLF_AT);
3812 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01003813 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 /* Correct end of highlighted area for 'showbreak',
3815 * required when 'linebreak' is also set. */
3816 if (tocol == vcol)
3817 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003818#ifdef FEAT_SYN_HL
3819 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003820 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003821 char_attr = hl_combine_attr(char_attr,
3822 hl_attr(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003823#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 }
3825# endif
3826 }
3827#endif
3828
3829 if (draw_state == WL_LINE - 1 && n_extra == 0)
3830 {
3831 draw_state = WL_LINE;
3832 if (saved_n_extra)
3833 {
3834 /* Continue item from end of wrapped line. */
3835 n_extra = saved_n_extra;
3836 c_extra = saved_c_extra;
3837 p_extra = saved_p_extra;
3838 char_attr = saved_char_attr;
3839 }
3840 else
3841 char_attr = 0;
3842 }
3843 }
3844
3845 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01003846 if (dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003847 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848#ifdef FEAT_DIFF
3849 && filler_todo <= 0
3850#endif
3851 )
3852 {
3853 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3854 wp->w_p_rl);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003855 /* Pretend we have finished updating the window. Except when
3856 * 'cursorcolumn' is set. */
3857#ifdef FEAT_SYN_HL
3858 if (wp->w_p_cuc)
3859 row = wp->w_cline_row + wp->w_cline_height;
3860 else
3861#endif
3862 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 break;
3864 }
3865
3866 if (draw_state == WL_LINE && area_highlighting)
3867 {
3868 /* handle Visual or match highlighting in this line */
3869 if (vcol == fromcol
3870#ifdef FEAT_MBYTE
3871 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3872 && (*mb_ptr2cells)(ptr) > 1)
3873#endif
3874 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00003875 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 && vcol < tocol))
3877 area_attr = attr; /* start highlighting */
3878 else if (area_attr != 0
3879 && (vcol == tocol
3880 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882
3883#ifdef FEAT_SEARCH_EXTRA
3884 if (!n_extra)
3885 {
3886 /*
3887 * Check for start/end of search pattern match.
3888 * After end, check for start/end of next match.
3889 * When another match, have to check for start again.
3890 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003891 * Do this for 'search_hl' and the match list (ordered by
3892 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003894 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003895 cur = wp->w_match_head;
3896 shl_flag = FALSE;
3897 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003899 if (shl_flag == FALSE
3900 && ((cur != NULL
3901 && cur->priority > SEARCH_HL_PRIORITY)
3902 || cur == NULL))
3903 {
3904 shl = &search_hl;
3905 shl_flag = TRUE;
3906 }
3907 else
3908 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003909 if (cur != NULL)
3910 cur->pos.cur = 0;
3911 pos_inprogress = TRUE;
3912 while (shl->rm.regprog != NULL
3913 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003915 if (shl->startcol != MAXCOL
3916 && v >= (long)shl->startcol
3917 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01003919#ifdef FEAT_MBYTE
3920 int tmp_col = v + MB_PTR2LEN(ptr);
3921
3922 if (shl->endcol < tmp_col)
3923 shl->endcol = tmp_col;
3924#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 shl->attr_cur = shl->attr;
3926 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01003927 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 {
3929 shl->attr_cur = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003930 next_search_hl(wp, shl, lnum, (colnr_T)v, cur);
3931 pos_inprogress = cur == NULL || cur->pos.cur == 0
3932 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933
3934 /* Need to get the line again, a multi-line regexp
3935 * may have made it invalid. */
3936 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3937 ptr = line + v;
3938
3939 if (shl->lnum == lnum)
3940 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003941 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003943 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003945 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003947 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 {
3949 /* highlight empty match, try again after
3950 * it */
3951#ifdef FEAT_MBYTE
3952 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003953 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003954 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 else
3956#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003957 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 }
3959
3960 /* Loop to check if the match starts at the
3961 * current position */
3962 continue;
3963 }
3964 }
3965 break;
3966 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003967 if (shl != &search_hl && cur != NULL)
3968 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003970
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003971 /* Use attributes from match with highest priority among
3972 * 'search_hl' and the match list. */
3973 search_attr = search_hl.attr_cur;
3974 cur = wp->w_match_head;
3975 shl_flag = FALSE;
3976 while (cur != NULL || shl_flag == FALSE)
3977 {
3978 if (shl_flag == FALSE
3979 && ((cur != NULL
3980 && cur->priority > SEARCH_HL_PRIORITY)
3981 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003982 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003983 shl = &search_hl;
3984 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003985 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003986 else
3987 shl = &cur->hl;
3988 if (shl->attr_cur != 0)
3989 search_attr = shl->attr_cur;
3990 if (shl != &search_hl && cur != NULL)
3991 cur = cur->next;
3992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 }
3994#endif
3995
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003997 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003999 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4000 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004002 if (diff_hlf == HLF_TXD && ptr - line > change_end
4003 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004005 line_attr = hl_attr(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004006 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4007 line_attr = hl_combine_attr(line_attr, hl_attr(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 }
4009#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004010
4011 /* Decide which of the highlight attributes to use. */
4012 attr_pri = TRUE;
4013 if (area_attr != 0)
4014 char_attr = area_attr;
4015 else if (search_attr != 0)
4016 char_attr = search_attr;
4017#ifdef LINE_ATTR
4018 /* Use line_attr when not in the Visual or 'incsearch' area
4019 * (area_attr may be 0 when "noinvcur" is set). */
4020 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004021 || vcol < fromcol || vcol_prev < fromcol_prev
4022 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004023 char_attr = line_attr;
4024#endif
4025 else
4026 {
4027 attr_pri = FALSE;
4028#ifdef FEAT_SYN_HL
4029 if (has_syntax)
4030 char_attr = syntax_attr;
4031 else
4032#endif
4033 char_attr = 0;
4034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 }
4036
4037 /*
4038 * Get the next character to put on the screen.
4039 */
4040 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004041 * The "p_extra" points to the extra stuff that is inserted to
4042 * represent special characters (non-printable stuff) and other
4043 * things. When all characters are the same, c_extra is used.
4044 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4045 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4047 */
4048 if (n_extra > 0)
4049 {
4050 if (c_extra != NUL)
4051 {
4052 c = c_extra;
4053#ifdef FEAT_MBYTE
4054 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
4055 if (enc_utf8 && (*mb_char2len)(c) > 1)
4056 {
4057 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004058 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004059 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 }
4061 else
4062 mb_utf8 = FALSE;
4063#endif
4064 }
4065 else
4066 {
4067 c = *p_extra;
4068#ifdef FEAT_MBYTE
4069 if (has_mbyte)
4070 {
4071 mb_c = c;
4072 if (enc_utf8)
4073 {
4074 /* If the UTF-8 character is more than one byte:
4075 * Decode it into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004076 mb_l = (*mb_ptr2len)(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 mb_utf8 = FALSE;
4078 if (mb_l > n_extra)
4079 mb_l = 1;
4080 else if (mb_l > 1)
4081 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004082 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004084 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 }
4086 }
4087 else
4088 {
4089 /* if this is a DBCS character, put it in "mb_c" */
4090 mb_l = MB_BYTE2LEN(c);
4091 if (mb_l >= n_extra)
4092 mb_l = 1;
4093 else if (mb_l > 1)
4094 mb_c = (c << 8) + p_extra[1];
4095 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004096 if (mb_l == 0) /* at the NUL at end-of-line */
4097 mb_l = 1;
4098
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 /* If a double-width char doesn't fit display a '>' in the
4100 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004101 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102# ifdef FEAT_RIGHTLEFT
4103 wp->w_p_rl ? (col <= 0) :
4104# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004105 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 && (*mb_char2cells)(mb_c) == 2)
4107 {
4108 c = '>';
4109 mb_c = c;
4110 mb_l = 1;
4111 mb_utf8 = FALSE;
4112 multi_attr = hl_attr(HLF_AT);
4113 /* put the pointer back to output the double-width
4114 * character at the start of the next line. */
4115 ++n_extra;
4116 --p_extra;
4117 }
4118 else
4119 {
4120 n_extra -= mb_l - 1;
4121 p_extra += mb_l - 1;
4122 }
4123 }
4124#endif
4125 ++p_extra;
4126 }
4127 --n_extra;
4128 }
4129 else
4130 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004131 if (p_extra_free != NULL)
4132 {
4133 vim_free(p_extra_free);
4134 p_extra_free = NULL;
4135 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 /*
4137 * Get a character from the line itself.
4138 */
4139 c = *ptr;
4140#ifdef FEAT_MBYTE
4141 if (has_mbyte)
4142 {
4143 mb_c = c;
4144 if (enc_utf8)
4145 {
4146 /* If the UTF-8 character is more than one byte: Decode it
4147 * into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004148 mb_l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 mb_utf8 = FALSE;
4150 if (mb_l > 1)
4151 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004152 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 /* Overlong encoded ASCII or ASCII with composing char
4154 * is displayed normally, except a NUL. */
4155 if (mb_c < 0x80)
4156 c = mb_c;
4157 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004158
4159 /* At start of the line we can have a composing char.
4160 * Draw it as a space with a composing char. */
4161 if (utf_iscomposing(mb_c))
4162 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004163 int i;
4164
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004165 for (i = Screen_mco - 1; i > 0; --i)
4166 u8cc[i] = u8cc[i - 1];
4167 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004168 mb_c = ' ';
4169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 }
4171
4172 if ((mb_l == 1 && c >= 0x80)
4173 || (mb_l >= 1 && mb_c == 0)
4174 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004175# ifdef UNICODE16
4176 || mb_c >= 0x10000
4177# endif
4178 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 {
4180 /*
4181 * Illegal UTF-8 byte: display as <xx>.
4182 * Non-BMP character : display as ? or fullwidth ?.
4183 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004184# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004186# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 {
4188 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004189# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 if (wp->w_p_rl) /* reverse */
4191 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004192# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004194# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 else if (utf_char2cells(mb_c) != 2)
4196 STRCPY(extra, "?");
4197 else
4198 /* 0xff1f in UTF-8: full-width '?' */
4199 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004200# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201
4202 p_extra = extra;
4203 c = *p_extra;
4204 mb_c = mb_ptr2char_adv(&p_extra);
4205 mb_utf8 = (c >= 0x80);
4206 n_extra = (int)STRLEN(p_extra);
4207 c_extra = NUL;
4208 if (area_attr == 0 && search_attr == 0)
4209 {
4210 n_attr = n_extra + 1;
4211 extra_attr = hl_attr(HLF_8);
4212 saved_attr2 = char_attr; /* save current attr */
4213 }
4214 }
4215 else if (mb_l == 0) /* at the NUL at end-of-line */
4216 mb_l = 1;
4217#ifdef FEAT_ARABIC
4218 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4219 {
4220 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004221 int pc, pc1, nc;
4222 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223
4224 /* The idea of what is the previous and next
4225 * character depends on 'rightleft'. */
4226 if (wp->w_p_rl)
4227 {
4228 pc = prev_c;
4229 pc1 = prev_c1;
4230 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004231 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 }
4233 else
4234 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004235 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004237 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 }
4239 prev_c = mb_c;
4240
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004241 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 }
4243 else
4244 prev_c = mb_c;
4245#endif
4246 }
4247 else /* enc_dbcs */
4248 {
4249 mb_l = MB_BYTE2LEN(c);
4250 if (mb_l == 0) /* at the NUL at end-of-line */
4251 mb_l = 1;
4252 else if (mb_l > 1)
4253 {
4254 /* We assume a second byte below 32 is illegal.
4255 * Hopefully this is OK for all double-byte encodings!
4256 */
4257 if (ptr[1] >= 32)
4258 mb_c = (c << 8) + ptr[1];
4259 else
4260 {
4261 if (ptr[1] == NUL)
4262 {
4263 /* head byte at end of line */
4264 mb_l = 1;
4265 transchar_nonprint(extra, c);
4266 }
4267 else
4268 {
4269 /* illegal tail byte */
4270 mb_l = 2;
4271 STRCPY(extra, "XX");
4272 }
4273 p_extra = extra;
4274 n_extra = (int)STRLEN(extra) - 1;
4275 c_extra = NUL;
4276 c = *p_extra++;
4277 if (area_attr == 0 && search_attr == 0)
4278 {
4279 n_attr = n_extra + 1;
4280 extra_attr = hl_attr(HLF_8);
4281 saved_attr2 = char_attr; /* save current attr */
4282 }
4283 mb_c = c;
4284 }
4285 }
4286 }
4287 /* If a double-width char doesn't fit display a '>' in the
4288 * last column; the character is displayed at the start of the
4289 * next line. */
4290 if ((
4291# ifdef FEAT_RIGHTLEFT
4292 wp->w_p_rl ? (col <= 0) :
4293# endif
4294 (col >= W_WIDTH(wp) - 1))
4295 && (*mb_char2cells)(mb_c) == 2)
4296 {
4297 c = '>';
4298 mb_c = c;
4299 mb_utf8 = FALSE;
4300 mb_l = 1;
4301 multi_attr = hl_attr(HLF_AT);
4302 /* Put pointer back so that the character will be
4303 * displayed at the start of the next line. */
4304 --ptr;
4305 }
4306 else if (*ptr != NUL)
4307 ptr += mb_l - 1;
4308
4309 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004310 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004311 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004312 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004315 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 c = ' ';
4317 if (area_attr == 0 && search_attr == 0)
4318 {
4319 n_attr = n_extra + 1;
4320 extra_attr = hl_attr(HLF_AT);
4321 saved_attr2 = char_attr; /* save current attr */
4322 }
4323 mb_c = c;
4324 mb_utf8 = FALSE;
4325 mb_l = 1;
4326 }
4327
4328 }
4329#endif
4330 ++ptr;
4331
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004332 /* 'list' : change char 160 to lcs_nbsp. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004333 if (wp->w_p_list && (c == 160
4334#ifdef FEAT_MBYTE
4335 || (mb_utf8 && mb_c == 160)
4336#endif
4337 ) && lcs_nbsp)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004338 {
4339 c = lcs_nbsp;
4340 if (area_attr == 0 && search_attr == 0)
4341 {
4342 n_attr = 1;
4343 extra_attr = hl_attr(HLF_8);
4344 saved_attr2 = char_attr; /* save current attr */
4345 }
4346#ifdef FEAT_MBYTE
4347 mb_c = c;
4348 if (enc_utf8 && (*mb_char2len)(c) > 1)
4349 {
4350 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004351 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004352 c = 0xc0;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004353 }
4354 else
4355 mb_utf8 = FALSE;
4356#endif
4357 }
4358
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 if (extra_check)
4360 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004361#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004362 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004363#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004364
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004365#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 /* Get syntax attribute, unless still at the start of the line
4367 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004368 v = (long)(ptr - line);
4369 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 {
4371 /* Get the syntax attribute for the character. If there
4372 * is an error, disable syntax highlighting. */
4373 save_did_emsg = did_emsg;
4374 did_emsg = FALSE;
4375
Bram Moolenaar217ad922005-03-20 22:37:15 +00004376 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004377# ifdef FEAT_SPELL
4378 has_spell ? &can_spell :
4379# endif
4380 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381
4382 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004383 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004384 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004385 has_syntax = FALSE;
4386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 else
4388 did_emsg = save_did_emsg;
4389
4390 /* Need to get the line again, a multi-line regexp may
4391 * have made it invalid. */
4392 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4393 ptr = line + v;
4394
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004395 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004397 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004398 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004399# ifdef FEAT_CONCEAL
4400 /* no concealing past the end of the line, it interferes
4401 * with line highlighting */
4402 if (c == NUL)
4403 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004404 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004405 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004406# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004408#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004409
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004410#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004411 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004412 * Only do this when there is no syntax highlighting, the
4413 * @Spell cluster is not used or the current syntax item
4414 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004415 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004416 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004417 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004418# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004419 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004420 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004421# endif
4422 if (c != 0 && (
4423# ifdef FEAT_SYN_HL
4424 !has_syntax ||
4425# endif
4426 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004427 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004428 char_u *prev_ptr, *p;
4429 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004430 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004431# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004432 if (has_mbyte)
4433 {
4434 prev_ptr = ptr - mb_l;
4435 v -= mb_l - 1;
4436 }
4437 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004438# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004439 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004440
4441 /* Use nextline[] if possible, it has the start of the
4442 * next line concatenated. */
4443 if ((prev_ptr - line) - nextlinecol >= 0)
4444 p = nextline + (prev_ptr - line) - nextlinecol;
4445 else
4446 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004447 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004448 len = spell_check(wp, p, &spell_hlf, &cap_col,
4449 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004450 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004451
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004452 /* In Insert mode only highlight a word that
4453 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004454 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004455 && (State & INSERT) != 0
4456 && wp->w_cursor.lnum == lnum
4457 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004458 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004459 && wp->w_cursor.col < (colnr_T)word_end)
4460 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004461 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004462 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004463 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004464
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004465 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004466 && (p - nextline) + len > nextline_idx)
4467 {
4468 /* Remember that the good word continues at the
4469 * start of the next line. */
4470 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004471 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004472 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004473
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004474 /* Turn index into actual attributes. */
4475 if (spell_hlf != HLF_COUNT)
4476 spell_attr = highlight_attr[spell_hlf];
4477
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004478 if (cap_col > 0)
4479 {
4480 if (p != prev_ptr
4481 && (p - nextline) + cap_col >= nextline_idx)
4482 {
4483 /* Remember that the word in the next line
4484 * must start with a capital. */
4485 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004486 cap_col = (int)((p - nextline) + cap_col
4487 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004488 }
4489 else
4490 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004491 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004492 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004493 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004494 }
4495 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004496 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004497 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004498 char_attr = hl_combine_attr(char_attr, spell_attr);
4499 else
4500 char_attr = hl_combine_attr(spell_attr, char_attr);
4501 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502#endif
4503#ifdef FEAT_LINEBREAK
4504 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004505 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004507 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 {
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004509# ifdef FEAT_MBYTE
4510 int off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
4511# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004512 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513# ifdef FEAT_MBYTE
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004514 off +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004516 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004517
Bram Moolenaar597a4222014-06-25 14:39:50 +02004518 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004519 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004520 NULL) - 1;
Bram Moolenaara3650912014-11-19 13:21:57 +01004521 if (c == TAB && n_extra + col > W_WIDTH(wp))
4522 n_extra = (int)wp->w_buffer->b_p_ts
4523 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4524
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004525# ifdef FEAT_MBYTE
4526 c_extra = off > 0 ? MB_FILLER_CHAR : ' ';
4527# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 c_extra = ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004529# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 if (vim_iswhite(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004531 {
4532#ifdef FEAT_CONCEAL
4533 if (c == TAB)
4534 /* See "Tab alignment" below. */
4535 FIX_FOR_BOGUSCOLS;
4536#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004537 if (!wp->w_p_list)
4538 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 }
4541#endif
4542
4543 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4544 {
4545 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004546 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 {
4548 n_attr = 1;
4549 extra_attr = hl_attr(HLF_8);
4550 saved_attr2 = char_attr; /* save current attr */
4551 }
4552#ifdef FEAT_MBYTE
4553 mb_c = c;
4554 if (enc_utf8 && (*mb_char2len)(c) > 1)
4555 {
4556 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004557 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004558 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 }
4560 else
4561 mb_utf8 = FALSE;
4562#endif
4563 }
4564 }
4565
4566 /*
4567 * Handling of non-printable characters.
4568 */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004569 if (!(chartab[c & 0xff] & CT_PRINT_CHAR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 {
4571 /*
4572 * when getting a character from the file, we may have to
4573 * turn it into something else on the way to putting it
4574 * into "ScreenLines".
4575 */
4576 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4577 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004578 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004579 long vcol_adjusted = vcol; /* removed showbreak length */
4580#ifdef FEAT_LINEBREAK
4581 /* only adjust the tab_len, when at the first column
4582 * after the showbreak value was drawn */
4583 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4584 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4585#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 /* tab amount depends on current column */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004587 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaard574ea22015-01-14 19:35:14 +01004588 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4589
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004590#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004591 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004592#endif
4593 /* tab amount depends on current column */
4594 n_extra = tab_len;
4595#ifdef FEAT_LINEBREAK
4596 else
4597 {
4598 char_u *p;
4599 int len = n_extra;
4600 int i;
4601 int saved_nextra = n_extra;
4602
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004603#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004604 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004605 /* there are characters to conceal */
4606 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004607 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
4608 */
4609 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4610 && n_extra > tab_len)
4611 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004612#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004613
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004614 /* if n_extra > 0, it gives the number of chars, to
4615 * use for a tab, else we need to calculate the width
4616 * for a tab */
4617#ifdef FEAT_MBYTE
4618 len = (tab_len * mb_char2len(lcs_tab2));
4619 if (n_extra > 0)
4620 len += n_extra - tab_len;
4621#endif
4622 c = lcs_tab1;
4623 p = alloc((unsigned)(len + 1));
4624 vim_memset(p, ' ', len);
4625 p[len] = NUL;
4626 p_extra_free = p;
4627 for (i = 0; i < tab_len; i++)
4628 {
4629#ifdef FEAT_MBYTE
4630 mb_char2bytes(lcs_tab2, p);
4631 p += mb_char2len(lcs_tab2);
4632 n_extra += mb_char2len(lcs_tab2)
4633 - (saved_nextra > 0 ? 1 : 0);
4634#else
4635 p[i] = lcs_tab2;
4636#endif
4637 }
4638 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004639#ifdef FEAT_CONCEAL
4640 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
4641 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004642 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004643 n_extra -= vcol_off;
4644#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004645 }
4646#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004647#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004648 {
4649 int vc_saved = vcol_off;
4650
4651 /* Tab alignment should be identical regardless of
4652 * 'conceallevel' value. So tab compensates of all
4653 * previous concealed characters, and thus resets
4654 * vcol_off and boguscols accumulated so far in the
4655 * line. Note that the tab can be longer than
4656 * 'tabstop' when there are concealed characters. */
4657 FIX_FOR_BOGUSCOLS;
4658
4659 /* Make sure, the highlighting for the tab char will be
4660 * correctly set further below (effectively reverts the
4661 * FIX_FOR_BOGSUCOLS macro */
4662 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004663 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004664 tab_len += vc_saved;
4665 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004666#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667#ifdef FEAT_MBYTE
4668 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4669#endif
4670 if (wp->w_p_list)
4671 {
4672 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004673#ifdef FEAT_LINEBREAK
4674 if (wp->w_p_lbr)
4675 c_extra = NUL; /* using p_extra from above */
4676 else
4677#endif
4678 c_extra = lcs_tab2;
4679 n_attr = tab_len + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 extra_attr = hl_attr(HLF_8);
4681 saved_attr2 = char_attr; /* save current attr */
4682#ifdef FEAT_MBYTE
4683 mb_c = c;
4684 if (enc_utf8 && (*mb_char2len)(c) > 1)
4685 {
4686 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004687 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004688 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 }
4690#endif
4691 }
4692 else
4693 {
4694 c_extra = ' ';
4695 c = ' ';
4696 }
4697 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004698 else if (c == NUL
4699 && ((wp->w_p_list && lcs_eol > 0)
4700 || ((fromcol >= 0 || fromcol_prev >= 0)
4701 && tocol > vcol
4702 && VIsual_mode != Ctrl_V
4703 && (
4704# ifdef FEAT_RIGHTLEFT
4705 wp->w_p_rl ? (col >= 0) :
4706# endif
4707 (col < W_WIDTH(wp)))
4708 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004709 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004710 && (colnr_T)vcol == wp->w_virtcol)))
4711 && lcs_eol_one >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004713 /* Display a '$' after the line or highlight an extra
4714 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4716 /* For a diff line the highlighting continues after the
4717 * "$". */
4718 if (
4719# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004720 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721# ifdef LINE_ATTR
4722 &&
4723# endif
4724# endif
4725# ifdef LINE_ATTR
4726 line_attr == 0
4727# endif
4728 )
4729#endif
4730 {
4731#ifdef FEAT_VIRTUALEDIT
4732 /* In virtualedit, visual selections may extend
4733 * beyond end of line. */
4734 if (area_highlighting && virtual_active()
4735 && tocol != MAXCOL && vcol < tocol)
4736 n_extra = 0;
4737 else
4738#endif
4739 {
4740 p_extra = at_end_str;
4741 n_extra = 1;
4742 c_extra = NUL;
4743 }
4744 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004745 if (wp->w_p_list)
4746 c = lcs_eol;
4747 else
4748 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 lcs_eol_one = -1;
4750 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004751 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 {
4753 extra_attr = hl_attr(HLF_AT);
4754 n_attr = 1;
4755 }
4756#ifdef FEAT_MBYTE
4757 mb_c = c;
4758 if (enc_utf8 && (*mb_char2len)(c) > 1)
4759 {
4760 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004761 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004762 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 }
4764 else
4765 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4766#endif
4767 }
4768 else if (c != NUL)
4769 {
4770 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02004771 if (n_extra == 0)
4772 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773#ifdef FEAT_RIGHTLEFT
4774 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4775 rl_mirror(p_extra); /* reverse "<12>" */
4776#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004778#ifdef FEAT_LINEBREAK
4779 if (wp->w_p_lbr)
4780 {
4781 char_u *p;
4782
4783 c = *p_extra;
4784 p = alloc((unsigned)n_extra + 1);
4785 vim_memset(p, ' ', n_extra);
4786 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
4787 p[n_extra] = NUL;
4788 p_extra_free = p_extra = p;
4789 }
4790 else
4791#endif
4792 {
4793 n_extra = byte2cells(c) - 1;
4794 c = *p_extra++;
4795 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004796 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 {
4798 n_attr = n_extra + 1;
4799 extra_attr = hl_attr(HLF_8);
4800 saved_attr2 = char_attr; /* save current attr */
4801 }
4802#ifdef FEAT_MBYTE
4803 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4804#endif
4805 }
4806#ifdef FEAT_VIRTUALEDIT
4807 else if (VIsual_active
4808 && (VIsual_mode == Ctrl_V
4809 || VIsual_mode == 'v')
4810 && virtual_active()
4811 && tocol != MAXCOL
4812 && vcol < tocol
4813 && (
4814# ifdef FEAT_RIGHTLEFT
4815 wp->w_p_rl ? (col >= 0) :
4816# endif
4817 (col < W_WIDTH(wp))))
4818 {
4819 c = ' ';
4820 --ptr; /* put it back at the NUL */
4821 }
4822#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004823#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 else if ((
4825# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004826 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 ) && (
4830# ifdef FEAT_RIGHTLEFT
4831 wp->w_p_rl ? (col >= 0) :
4832# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02004833 (col
4834# ifdef FEAT_CONCEAL
4835 - boguscols
4836# endif
4837 < W_WIDTH(wp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 {
4839 /* Highlight until the right side of the window */
4840 c = ' ';
4841 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004842
4843 /* Remember we do the char for line highlighting. */
4844 ++did_line_attr;
4845
4846 /* don't do search HL for the rest of the line */
4847 if (line_attr != 0 && char_attr == search_attr && col > 0)
4848 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849# ifdef FEAT_DIFF
4850 if (diff_hlf == HLF_TXD)
4851 {
4852 diff_hlf = HLF_CHD;
4853 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004854 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 char_attr = hl_attr(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004856 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4857 char_attr = hl_combine_attr(char_attr,
4858 hl_attr(HLF_CUL));
4859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 }
4861# endif
4862 }
4863#endif
4864 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02004865
4866#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004867 if ( wp->w_p_cole > 0
4868 && (wp != curwin || lnum != wp->w_cursor.lnum ||
4869 conceal_cursor_line(wp))
Bram Moolenaarf70e3d62010-07-24 13:15:07 +02004870 && (syntax_flags & HL_CONCEAL) != 0
Bram Moolenaare6dc5732010-07-24 23:52:26 +02004871 && !(lnum_in_visual_area
4872 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02004873 {
4874 char_attr = conceal_attr;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004875 if (prev_syntax_id != syntax_seqnr
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004876 && (syn_get_sub_char() != NUL || wp->w_p_cole == 1)
4877 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004878 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004879 /* First time at this concealed item: display one
4880 * character. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004881 if (syn_get_sub_char() != NUL)
4882 c = syn_get_sub_char();
4883 else if (lcs_conceal != NUL)
4884 c = lcs_conceal;
4885 else
4886 c = ' ';
4887
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004888 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004889
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004890 if (n_extra > 0)
4891 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004892 vcol += n_extra;
4893 if (wp->w_p_wrap && n_extra > 0)
4894 {
4895# ifdef FEAT_RIGHTLEFT
4896 if (wp->w_p_rl)
4897 {
4898 col -= n_extra;
4899 boguscols -= n_extra;
4900 }
4901 else
4902# endif
4903 {
4904 boguscols += n_extra;
4905 col += n_extra;
4906 }
4907 }
4908 n_extra = 0;
4909 n_attr = 0;
4910 }
4911 else if (n_skip == 0)
4912 {
4913 is_concealing = TRUE;
4914 n_skip = 1;
4915 }
4916# ifdef FEAT_MBYTE
4917 mb_c = c;
4918 if (enc_utf8 && (*mb_char2len)(c) > 1)
4919 {
4920 mb_utf8 = TRUE;
4921 u8cc[0] = 0;
4922 c = 0xc0;
4923 }
4924 else
4925 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4926# endif
4927 }
4928 else
4929 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004930 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02004931 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004932 }
4933#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 }
4935
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004936#ifdef FEAT_CONCEAL
4937 /* In the cursor line and we may be concealing characters: correct
4938 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02004939 if (!did_wcol && draw_state == WL_LINE
4940 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004941 && conceal_cursor_line(wp)
4942 && (int)wp->w_virtcol <= vcol + n_skip)
4943 {
4944 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02004945 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004946 did_wcol = TRUE;
4947 }
4948#endif
4949
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 /* Don't override visual selection highlighting. */
4951 if (n_attr > 0
4952 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004953 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 char_attr = extra_attr;
4955
Bram Moolenaar81695252004-12-29 20:58:21 +00004956#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 /* XIM don't send preedit_start and preedit_end, but they send
4958 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
4959 * im_is_preediting() here. */
4960 if (xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004961 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 && (State & INSERT)
4963 && !p_imdisable
4964 && im_is_preediting()
4965 && draw_state == WL_LINE)
4966 {
4967 colnr_T tcol;
4968
4969 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004970 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 else
4972 tcol = preedit_end_col;
4973 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
4974 {
4975 if (feedback_old_attr < 0)
4976 {
4977 feedback_col = 0;
4978 feedback_old_attr = char_attr;
4979 }
4980 char_attr = im_get_feedback_attr(feedback_col);
4981 if (char_attr < 0)
4982 char_attr = feedback_old_attr;
4983 feedback_col++;
4984 }
4985 else if (feedback_old_attr >= 0)
4986 {
4987 char_attr = feedback_old_attr;
4988 feedback_old_attr = -1;
4989 feedback_col = 0;
4990 }
4991 }
4992#endif
4993 /*
4994 * Handle the case where we are in column 0 but not on the first
4995 * character of the line and the user wants us to show us a
4996 * special character (via 'listchars' option "precedes:<char>".
4997 */
4998 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02004999 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5001#ifdef FEAT_DIFF
5002 && filler_todo <= 0
5003#endif
5004 && draw_state > WL_NR
5005 && c != NUL)
5006 {
5007 c = lcs_prec;
5008 lcs_prec_todo = NUL;
5009#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02005010 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5011 {
5012 /* Double-width character being overwritten by the "precedes"
5013 * character, need to fill up half the character. */
5014 c_extra = MB_FILLER_CHAR;
5015 n_extra = 1;
5016 n_attr = 2;
5017 extra_attr = hl_attr(HLF_AT);
5018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 mb_c = c;
5020 if (enc_utf8 && (*mb_char2len)(c) > 1)
5021 {
5022 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005023 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005024 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 }
5026 else
5027 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5028#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005029 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 {
5031 saved_attr3 = char_attr; /* save current attr */
5032 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
5033 n_attr3 = 1;
5034 }
5035 }
5036
5037 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005038 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005040 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005041#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005042 || did_line_attr == 1
5043#endif
5044 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005046#ifdef FEAT_SEARCH_EXTRA
5047 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005048
5049 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005050 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005051 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005052#endif
5053
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005054 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 * highlight match at end of line. If it's beyond the last
5056 * char on the screen, just overwrite that one (tricky!) Not
5057 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005058#ifdef FEAT_SEARCH_EXTRA
5059 prevcol_hl_flag = FALSE;
5060 if (prevcol == (long)search_hl.startcol)
5061 prevcol_hl_flag = TRUE;
5062 else
5063 {
5064 cur = wp->w_match_head;
5065 while (cur != NULL)
5066 {
5067 if (prevcol == (long)cur->hl.startcol)
5068 {
5069 prevcol_hl_flag = TRUE;
5070 break;
5071 }
5072 cur = cur->next;
5073 }
5074 }
5075#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005077 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005078 && (VIsual_mode != Ctrl_V
5079 || lnum == VIsual.lnum
5080 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005081 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082#ifdef FEAT_SEARCH_EXTRA
5083 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005084 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005085# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005086 && did_line_attr <= 1
5087# endif
5088 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089#endif
5090 ))
5091 {
5092 int n = 0;
5093
5094#ifdef FEAT_RIGHTLEFT
5095 if (wp->w_p_rl)
5096 {
5097 if (col < 0)
5098 n = 1;
5099 }
5100 else
5101#endif
5102 {
5103 if (col >= W_WIDTH(wp))
5104 n = -1;
5105 }
5106 if (n != 0)
5107 {
5108 /* At the window boundary, highlight the last character
5109 * instead (better than nothing). */
5110 off += n;
5111 col += n;
5112 }
5113 else
5114 {
5115 /* Add a blank character to highlight. */
5116 ScreenLines[off] = ' ';
5117#ifdef FEAT_MBYTE
5118 if (enc_utf8)
5119 ScreenLinesUC[off] = 0;
5120#endif
5121 }
5122#ifdef FEAT_SEARCH_EXTRA
5123 if (area_attr == 0)
5124 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005125 /* Use attributes from match with highest priority among
5126 * 'search_hl' and the match list. */
5127 char_attr = search_hl.attr;
5128 cur = wp->w_match_head;
5129 shl_flag = FALSE;
5130 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005131 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005132 if (shl_flag == FALSE
5133 && ((cur != NULL
5134 && cur->priority > SEARCH_HL_PRIORITY)
5135 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005136 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005137 shl = &search_hl;
5138 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005139 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005140 else
5141 shl = &cur->hl;
5142 if ((ptr - line) - 1 == (long)shl->startcol)
5143 char_attr = shl->attr;
5144 if (shl != &search_hl && cur != NULL)
5145 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005146 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 }
5148#endif
5149 ScreenAttrs[off] = char_attr;
5150#ifdef FEAT_RIGHTLEFT
5151 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005152 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005154 --off;
5155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 else
5157#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005158 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005160 ++off;
5161 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005162 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005163#ifdef FEAT_SYN_HL
5164 eol_hl_off = 1;
5165#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168
Bram Moolenaar91170f82006-05-05 21:15:17 +00005169 /*
5170 * At end of the text line.
5171 */
5172 if (c == NUL)
5173 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005174#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005175 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
5176 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00005177 {
5178 /* highlight last char after line */
5179 --col;
5180 --off;
5181 --vcol;
5182 }
5183
Bram Moolenaar1a384422010-07-14 19:53:30 +02005184 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005185 if (wp->w_p_wrap)
5186 v = wp->w_skipcol;
5187 else
5188 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005189
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005190 /* check if line ends before left margin */
5191 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005192 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005193#ifdef FEAT_CONCEAL
5194 /* Get rid of the boguscols now, we want to draw until the right
5195 * edge for 'cursorcolumn'. */
5196 col -= boguscols;
5197 boguscols = 0;
5198#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005199
5200 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005201 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005202
5203 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005204 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5205 && (int)wp->w_virtcol <
5206 W_WIDTH(wp) * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005207 && lnum != wp->w_cursor.lnum)
5208 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005209# ifdef FEAT_RIGHTLEFT
5210 && !wp->w_p_rl
5211# endif
5212 )
5213 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005214 int rightmost_vcol = 0;
5215 int i;
5216
5217 if (wp->w_p_cuc)
5218 rightmost_vcol = wp->w_virtcol;
5219 if (draw_color_col)
5220 /* determine rightmost colorcolumn to possibly draw */
5221 for (i = 0; color_cols[i] >= 0; ++i)
5222 if (rightmost_vcol < color_cols[i])
5223 rightmost_vcol = color_cols[i];
5224
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005225 while (col < W_WIDTH(wp))
5226 {
5227 ScreenLines[off] = ' ';
5228#ifdef FEAT_MBYTE
5229 if (enc_utf8)
5230 ScreenLinesUC[off] = 0;
5231#endif
5232 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005233 if (draw_color_col)
5234 draw_color_col = advance_color_col(VCOL_HLC,
5235 &color_cols);
5236
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005237 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005238 ScreenAttrs[off++] = hl_attr(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005239 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005240 ScreenAttrs[off++] = hl_attr(HLF_MC);
5241 else
5242 ScreenAttrs[off++] = 0;
5243
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005244 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005245 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005246
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005247 ++vcol;
5248 }
5249 }
5250#endif
5251
Bram Moolenaar860cae12010-06-05 23:22:07 +02005252 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5253 (int)W_WIDTH(wp), wp->w_p_rl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 row++;
5255
5256 /*
5257 * Update w_cline_height and w_cline_folded if the cursor line was
5258 * updated (saves a call to plines() later).
5259 */
5260 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5261 {
5262 curwin->w_cline_row = startrow;
5263 curwin->w_cline_height = row - startrow;
5264#ifdef FEAT_FOLDING
5265 curwin->w_cline_folded = FALSE;
5266#endif
5267 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5268 }
5269
5270 break;
5271 }
5272
5273 /* line continues beyond line end */
5274 if (lcs_ext
5275 && !wp->w_p_wrap
5276#ifdef FEAT_DIFF
5277 && filler_todo <= 0
5278#endif
5279 && (
5280#ifdef FEAT_RIGHTLEFT
5281 wp->w_p_rl ? col == 0 :
5282#endif
5283 col == W_WIDTH(wp) - 1)
5284 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005285 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5287 {
5288 c = lcs_ext;
5289 char_attr = hl_attr(HLF_AT);
5290#ifdef FEAT_MBYTE
5291 mb_c = c;
5292 if (enc_utf8 && (*mb_char2len)(c) > 1)
5293 {
5294 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005295 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005296 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 }
5298 else
5299 mb_utf8 = FALSE;
5300#endif
5301 }
5302
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005303#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005304 /* advance to the next 'colorcolumn' */
5305 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005306 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005307
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005308 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005309 * highlight the cursor position itself.
5310 * Also highlight the 'colorcolumn' if it is different than
5311 * 'cursorcolumn' */
5312 vcol_save_attr = -1;
5313 if (draw_state == WL_LINE && !lnum_in_visual_area)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005314 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005315 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005316 && lnum != wp->w_cursor.lnum)
5317 {
5318 vcol_save_attr = char_attr;
5319 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
5320 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005321 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005322 {
5323 vcol_save_attr = char_attr;
5324 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_MC));
5325 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005326 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005327#endif
5328
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 /*
5330 * Store character to be displayed.
5331 * Skip characters that are left of the screen for 'nowrap'.
5332 */
5333 vcol_prev = vcol;
5334 if (draw_state < WL_LINE || n_skip <= 0)
5335 {
5336 /*
5337 * Store the character.
5338 */
5339#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5340 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5341 {
5342 /* A double-wide character is: put first halve in left cell. */
5343 --off;
5344 --col;
5345 }
5346#endif
5347 ScreenLines[off] = c;
5348#ifdef FEAT_MBYTE
5349 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005350 {
5351 if ((mb_c & 0xff00) == 0x8e00)
5352 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 else if (enc_utf8)
5356 {
5357 if (mb_utf8)
5358 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005359 int i;
5360
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005362 if ((c & 0xff) == 0)
5363 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005364 for (i = 0; i < Screen_mco; ++i)
5365 {
5366 ScreenLinesC[i][off] = u8cc[i];
5367 if (u8cc[i] == 0)
5368 break;
5369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 }
5371 else
5372 ScreenLinesUC[off] = 0;
5373 }
5374 if (multi_attr)
5375 {
5376 ScreenAttrs[off] = multi_attr;
5377 multi_attr = 0;
5378 }
5379 else
5380#endif
5381 ScreenAttrs[off] = char_attr;
5382
5383#ifdef FEAT_MBYTE
5384 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5385 {
5386 /* Need to fill two screen columns. */
5387 ++off;
5388 ++col;
5389 if (enc_utf8)
5390 /* UTF-8: Put a 0 in the second screen char. */
5391 ScreenLines[off] = 0;
5392 else
5393 /* DBCS: Put second byte in the second screen char. */
5394 ScreenLines[off] = mb_c & 0xff;
5395 ++vcol;
5396 /* When "tocol" is halfway a character, set it to the end of
5397 * the character, otherwise highlighting won't stop. */
5398 if (tocol == vcol)
5399 ++tocol;
5400#ifdef FEAT_RIGHTLEFT
5401 if (wp->w_p_rl)
5402 {
5403 /* now it's time to backup one cell */
5404 --off;
5405 --col;
5406 }
5407#endif
5408 }
5409#endif
5410#ifdef FEAT_RIGHTLEFT
5411 if (wp->w_p_rl)
5412 {
5413 --off;
5414 --col;
5415 }
5416 else
5417#endif
5418 {
5419 ++off;
5420 ++col;
5421 }
5422 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005423#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005424 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005425 {
5426 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005427 ++vcol_off;
5428 if (n_extra > 0)
5429 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005430 if (wp->w_p_wrap)
5431 {
5432 /*
5433 * Special voodoo required if 'wrap' is on.
5434 *
5435 * Advance the column indicator to force the line
5436 * drawing to wrap early. This will make the line
5437 * take up the same screen space when parts are concealed,
5438 * so that cursor line computations aren't messed up.
5439 *
5440 * To avoid the fictitious advance of 'col' causing
5441 * trailing junk to be written out of the screen line
5442 * we are building, 'boguscols' keeps track of the number
5443 * of bad columns we have advanced.
5444 */
5445 if (n_extra > 0)
5446 {
5447 vcol += n_extra;
5448# ifdef FEAT_RIGHTLEFT
5449 if (wp->w_p_rl)
5450 {
5451 col -= n_extra;
5452 boguscols -= n_extra;
5453 }
5454 else
5455# endif
5456 {
5457 col += n_extra;
5458 boguscols += n_extra;
5459 }
5460 n_extra = 0;
5461 n_attr = 0;
5462 }
5463
5464
5465# ifdef FEAT_MBYTE
5466 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5467 {
5468 /* Need to fill two screen columns. */
5469# ifdef FEAT_RIGHTLEFT
5470 if (wp->w_p_rl)
5471 {
5472 --boguscols;
5473 --col;
5474 }
5475 else
5476# endif
5477 {
5478 ++boguscols;
5479 ++col;
5480 }
5481 }
5482# endif
5483
5484# ifdef FEAT_RIGHTLEFT
5485 if (wp->w_p_rl)
5486 {
5487 --boguscols;
5488 --col;
5489 }
5490 else
5491# endif
5492 {
5493 ++boguscols;
5494 ++col;
5495 }
5496 }
5497 else
5498 {
5499 if (n_extra > 0)
5500 {
5501 vcol += n_extra;
5502 n_extra = 0;
5503 n_attr = 0;
5504 }
5505 }
5506
5507 }
5508#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 else
5510 --n_skip;
5511
Bram Moolenaar64486672010-05-16 15:46:46 +02005512 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5513 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005514 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515#ifdef FEAT_DIFF
5516 && filler_todo <= 0
5517#endif
5518 )
5519 ++vcol;
5520
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005521#ifdef FEAT_SYN_HL
5522 if (vcol_save_attr >= 0)
5523 char_attr = vcol_save_attr;
5524#endif
5525
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 /* restore attributes after "predeces" in 'listchars' */
5527 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5528 char_attr = saved_attr3;
5529
5530 /* restore attributes after last 'listchars' or 'number' char */
5531 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5532 char_attr = saved_attr2;
5533
5534 /*
5535 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005536 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 */
5538 if ((
5539#ifdef FEAT_RIGHTLEFT
5540 wp->w_p_rl ? (col < 0) :
5541#endif
5542 (col >= W_WIDTH(wp)))
5543 && (*ptr != NUL
5544#ifdef FEAT_DIFF
5545 || filler_todo > 0
5546#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005547 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5549 )
5550 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005551#ifdef FEAT_CONCEAL
5552 SCREEN_LINE(screen_row, W_WINCOL(wp), col - boguscols,
5553 (int)W_WIDTH(wp), wp->w_p_rl);
5554 boguscols = 0;
5555#else
5556 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5557 (int)W_WIDTH(wp), wp->w_p_rl);
5558#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 ++row;
5560 ++screen_row;
5561
5562 /* When not wrapping and finished diff lines, or when displayed
5563 * '$' and highlighting until last column, break here. */
5564 if ((!wp->w_p_wrap
5565#ifdef FEAT_DIFF
5566 && filler_todo <= 0
5567#endif
5568 ) || lcs_eol_one == -1)
5569 break;
5570
5571 /* When the window is too narrow draw all "@" lines. */
5572 if (draw_state != WL_LINE
5573#ifdef FEAT_DIFF
5574 && filler_todo <= 0
5575#endif
5576 )
5577 {
5578 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
5579#ifdef FEAT_VERTSPLIT
5580 draw_vsep_win(wp, row);
5581#endif
5582 row = endrow;
5583 }
5584
5585 /* When line got too long for screen break here. */
5586 if (row == endrow)
5587 {
5588 ++row;
5589 break;
5590 }
5591
5592 if (screen_cur_row == screen_row - 1
5593#ifdef FEAT_DIFF
5594 && filler_todo <= 0
5595#endif
5596 && W_WIDTH(wp) == Columns)
5597 {
5598 /* Remember that the line wraps, used for modeless copy. */
5599 LineWraps[screen_row - 1] = TRUE;
5600
5601 /*
5602 * Special trick to make copy/paste of wrapped lines work with
5603 * xterm/screen: write an extra character beyond the end of
5604 * the line. This will work with all terminal types
5605 * (regardless of the xn,am settings).
5606 * Only do this on a fast tty.
5607 * Only do this if the cursor is on the current line
5608 * (something has been written in it).
5609 * Don't do this for the GUI.
5610 * Don't do this for double-width characters.
5611 * Don't do this for a window not at the right screen border.
5612 */
5613 if (p_tf
5614#ifdef FEAT_GUI
5615 && !gui.in_use
5616#endif
5617#ifdef FEAT_MBYTE
5618 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005619 && ((*mb_off2cells)(LineOffset[screen_row],
5620 LineOffset[screen_row] + screen_Columns)
5621 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005623 + (int)Columns - 2,
5624 LineOffset[screen_row] + screen_Columns)
5625 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626#endif
5627 )
5628 {
5629 /* First make sure we are at the end of the screen line,
5630 * then output the same character again to let the
5631 * terminal know about the wrap. If the terminal doesn't
5632 * auto-wrap, we overwrite the character. */
5633 if (screen_cur_col != W_WIDTH(wp))
5634 screen_char(LineOffset[screen_row - 1]
5635 + (unsigned)Columns - 1,
5636 screen_row - 1, (int)(Columns - 1));
5637
5638#ifdef FEAT_MBYTE
5639 /* When there is a multi-byte character, just output a
5640 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005641 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5642 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 out_char(' ');
5644 else
5645#endif
5646 out_char(ScreenLines[LineOffset[screen_row - 1]
5647 + (Columns - 1)]);
5648 /* force a redraw of the first char on the next line */
5649 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5650 screen_start(); /* don't know where cursor is now */
5651 }
5652 }
5653
5654 col = 0;
5655 off = (unsigned)(current_ScreenLine - ScreenLines);
5656#ifdef FEAT_RIGHTLEFT
5657 if (wp->w_p_rl)
5658 {
5659 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
5660 off += col;
5661 }
5662#endif
5663
5664 /* reset the drawing state for the start of a wrapped line */
5665 draw_state = WL_START;
5666 saved_n_extra = n_extra;
5667 saved_p_extra = p_extra;
5668 saved_c_extra = c_extra;
5669 saved_char_attr = char_attr;
5670 n_extra = 0;
5671 lcs_prec_todo = lcs_prec;
5672#ifdef FEAT_LINEBREAK
5673# ifdef FEAT_DIFF
5674 if (filler_todo <= 0)
5675# endif
5676 need_showbreak = TRUE;
5677#endif
5678#ifdef FEAT_DIFF
5679 --filler_todo;
5680 /* When the filler lines are actually below the last line of the
5681 * file, don't draw the line itself, break here. */
5682 if (filler_todo == 0 && wp->w_botfill)
5683 break;
5684#endif
5685 }
5686
5687 } /* for every character in the line */
5688
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005689#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005690 /* After an empty line check first word for capital. */
5691 if (*skipwhite(line) == NUL)
5692 {
5693 capcol_lnum = lnum + 1;
5694 cap_col = 0;
5695 }
5696#endif
5697
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 return row;
5699}
5700
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005701#ifdef FEAT_MBYTE
5702static int comp_char_differs __ARGS((int, int));
5703
5704/*
5705 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005706 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005707 */
5708 static int
5709comp_char_differs(off_from, off_to)
5710 int off_from;
5711 int off_to;
5712{
5713 int i;
5714
5715 for (i = 0; i < Screen_mco; ++i)
5716 {
5717 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5718 return TRUE;
5719 if (ScreenLinesC[i][off_from] == 0)
5720 break;
5721 }
5722 return FALSE;
5723}
5724#endif
5725
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726/*
5727 * Check whether the given character needs redrawing:
5728 * - the (first byte of the) character is different
5729 * - the attributes are different
5730 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005731 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 */
5733 static int
5734char_needs_redraw(off_from, off_to, cols)
5735 int off_from;
5736 int off_to;
5737 int cols;
5738{
5739 if (cols > 0
5740 && ((ScreenLines[off_from] != ScreenLines[off_to]
5741 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
5742
5743#ifdef FEAT_MBYTE
5744 || (enc_dbcs != 0
5745 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
5746 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
5747 ? ScreenLines2[off_from] != ScreenLines2[off_to]
5748 : (cols > 1 && ScreenLines[off_from + 1]
5749 != ScreenLines[off_to + 1])))
5750 || (enc_utf8
5751 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
5752 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005753 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02005754 || ((*mb_off2cells)(off_from, off_from + cols) > 1
5755 && ScreenLines[off_from + 1]
5756 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757#endif
5758 ))
5759 return TRUE;
5760 return FALSE;
5761}
5762
5763/*
5764 * Move one "cooked" screen line to the screen, but only the characters that
5765 * have actually changed. Handle insert/delete character.
5766 * "coloff" gives the first column on the screen for this line.
5767 * "endcol" gives the columns where valid characters are.
5768 * "clear_width" is the width of the window. It's > 0 if the rest of the line
5769 * needs to be cleared, negative otherwise.
5770 * "rlflag" is TRUE in a rightleft window:
5771 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
5772 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
5773 */
5774 static void
5775screen_line(row, coloff, endcol, clear_width
5776#ifdef FEAT_RIGHTLEFT
5777 , rlflag
5778#endif
5779 )
5780 int row;
5781 int coloff;
5782 int endcol;
5783 int clear_width;
5784#ifdef FEAT_RIGHTLEFT
5785 int rlflag;
5786#endif
5787{
5788 unsigned off_from;
5789 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005790#ifdef FEAT_MBYTE
5791 unsigned max_off_from;
5792 unsigned max_off_to;
5793#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 int col = 0;
5795#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_VERTSPLIT)
5796 int hl;
5797#endif
5798 int force = FALSE; /* force update rest of the line */
5799 int redraw_this /* bool: does character need redraw? */
5800#ifdef FEAT_GUI
5801 = TRUE /* For GUI when while-loop empty */
5802#endif
5803 ;
5804 int redraw_next; /* redraw_this for next character */
5805#ifdef FEAT_MBYTE
5806 int clear_next = FALSE;
5807 int char_cells; /* 1: normal char */
5808 /* 2: occupies two display cells */
5809# define CHAR_CELLS char_cells
5810#else
5811# define CHAR_CELLS 1
5812#endif
5813
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01005814 /* Check for illegal row and col, just in case. */
5815 if (row >= Rows)
5816 row = Rows - 1;
5817 if (endcol > Columns)
5818 endcol = Columns;
5819
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820# ifdef FEAT_CLIPBOARD
5821 clip_may_clear_selection(row, row);
5822# endif
5823
5824 off_from = (unsigned)(current_ScreenLine - ScreenLines);
5825 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005826#ifdef FEAT_MBYTE
5827 max_off_from = off_from + screen_Columns;
5828 max_off_to = LineOffset[row] + screen_Columns;
5829#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830
5831#ifdef FEAT_RIGHTLEFT
5832 if (rlflag)
5833 {
5834 /* Clear rest first, because it's left of the text. */
5835 if (clear_width > 0)
5836 {
5837 while (col <= endcol && ScreenLines[off_to] == ' '
5838 && ScreenAttrs[off_to] == 0
5839# ifdef FEAT_MBYTE
5840 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5841# endif
5842 )
5843 {
5844 ++off_to;
5845 ++col;
5846 }
5847 if (col <= endcol)
5848 screen_fill(row, row + 1, col + coloff,
5849 endcol + coloff + 1, ' ', ' ', 0);
5850 }
5851 col = endcol + 1;
5852 off_to = LineOffset[row] + col + coloff;
5853 off_from += col;
5854 endcol = (clear_width > 0 ? clear_width : -clear_width);
5855 }
5856#endif /* FEAT_RIGHTLEFT */
5857
5858 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
5859
5860 while (col < endcol)
5861 {
5862#ifdef FEAT_MBYTE
5863 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00005864 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 else
5866 char_cells = 1;
5867#endif
5868
5869 redraw_this = redraw_next;
5870 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
5871 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
5872
5873#ifdef FEAT_GUI
5874 /* If the next character was bold, then redraw the current character to
5875 * remove any pixels that might have spilt over into us. This only
5876 * happens in the GUI.
5877 */
5878 if (redraw_next && gui.in_use)
5879 {
5880 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005881 if (hl > HL_ALL)
5882 hl = syn_attr2attr(hl);
5883 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 redraw_this = TRUE;
5885 }
5886#endif
5887
5888 if (redraw_this)
5889 {
5890 /*
5891 * Special handling when 'xs' termcap flag set (hpterm):
5892 * Attributes for characters are stored at the position where the
5893 * cursor is when writing the highlighting code. The
5894 * start-highlighting code must be written with the cursor on the
5895 * first highlighted character. The stop-highlighting code must
5896 * be written with the cursor just after the last highlighted
5897 * character.
5898 * Overwriting a character doesn't remove it's highlighting. Need
5899 * to clear the rest of the line, and force redrawing it
5900 * completely.
5901 */
5902 if ( p_wiv
5903 && !force
5904#ifdef FEAT_GUI
5905 && !gui.in_use
5906#endif
5907 && ScreenAttrs[off_to] != 0
5908 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
5909 {
5910 /*
5911 * Need to remove highlighting attributes here.
5912 */
5913 windgoto(row, col + coloff);
5914 out_str(T_CE); /* clear rest of this screen line */
5915 screen_start(); /* don't know where cursor is now */
5916 force = TRUE; /* force redraw of rest of the line */
5917 redraw_next = TRUE; /* or else next char would miss out */
5918
5919 /*
5920 * If the previous character was highlighted, need to stop
5921 * highlighting at this character.
5922 */
5923 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
5924 {
5925 screen_attr = ScreenAttrs[off_to - 1];
5926 term_windgoto(row, col + coloff);
5927 screen_stop_highlight();
5928 }
5929 else
5930 screen_attr = 0; /* highlighting has stopped */
5931 }
5932#ifdef FEAT_MBYTE
5933 if (enc_dbcs != 0)
5934 {
5935 /* Check if overwriting a double-byte with a single-byte or
5936 * the other way around requires another character to be
5937 * redrawn. For UTF-8 this isn't needed, because comparing
5938 * ScreenLinesUC[] is sufficient. */
5939 if (char_cells == 1
5940 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005941 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 {
5943 /* Writing a single-cell character over a double-cell
5944 * character: need to redraw the next cell. */
5945 ScreenLines[off_to + 1] = 0;
5946 redraw_next = TRUE;
5947 }
5948 else if (char_cells == 2
5949 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005950 && (*mb_off2cells)(off_to, max_off_to) == 1
5951 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 {
5953 /* Writing the second half of a double-cell character over
5954 * a double-cell character: need to redraw the second
5955 * cell. */
5956 ScreenLines[off_to + 2] = 0;
5957 redraw_next = TRUE;
5958 }
5959
5960 if (enc_dbcs == DBCS_JPNU)
5961 ScreenLines2[off_to] = ScreenLines2[off_from];
5962 }
5963 /* When writing a single-width character over a double-width
5964 * character and at the end of the redrawn text, need to clear out
5965 * the right halve of the old character.
5966 * Also required when writing the right halve of a double-width
5967 * char over the left halve of an existing one. */
5968 if (has_mbyte && col + char_cells == endcol
5969 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00005970 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00005972 && (*mb_off2cells)(off_to, max_off_to) == 1
5973 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 clear_next = TRUE;
5975#endif
5976
5977 ScreenLines[off_to] = ScreenLines[off_from];
5978#ifdef FEAT_MBYTE
5979 if (enc_utf8)
5980 {
5981 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
5982 if (ScreenLinesUC[off_from] != 0)
5983 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005984 int i;
5985
5986 for (i = 0; i < Screen_mco; ++i)
5987 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988 }
5989 }
5990 if (char_cells == 2)
5991 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
5992#endif
5993
5994#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00005995 /* The bold trick makes a single column of pixels appear in the
5996 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997 * character should be redrawn too. This happens for our own GUI
5998 * and for some xterms. */
5999 if (
6000# ifdef FEAT_GUI
6001 gui.in_use
6002# endif
6003# if defined(FEAT_GUI) && defined(UNIX)
6004 ||
6005# endif
6006# ifdef UNIX
6007 term_is_xterm
6008# endif
6009 )
6010 {
6011 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006012 if (hl > HL_ALL)
6013 hl = syn_attr2attr(hl);
6014 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015 redraw_next = TRUE;
6016 }
6017#endif
6018 ScreenAttrs[off_to] = ScreenAttrs[off_from];
6019#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006020 /* For simplicity set the attributes of second half of a
6021 * double-wide character equal to the first half. */
6022 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006024
6025 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027 else
6028#endif
6029 screen_char(off_to, row, col + coloff);
6030 }
6031 else if ( p_wiv
6032#ifdef FEAT_GUI
6033 && !gui.in_use
6034#endif
6035 && col + coloff > 0)
6036 {
6037 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6038 {
6039 /*
6040 * Don't output stop-highlight when moving the cursor, it will
6041 * stop the highlighting when it should continue.
6042 */
6043 screen_attr = 0;
6044 }
6045 else if (screen_attr != 0)
6046 screen_stop_highlight();
6047 }
6048
6049 off_to += CHAR_CELLS;
6050 off_from += CHAR_CELLS;
6051 col += CHAR_CELLS;
6052 }
6053
6054#ifdef FEAT_MBYTE
6055 if (clear_next)
6056 {
6057 /* Clear the second half of a double-wide character of which the left
6058 * half was overwritten with a single-wide character. */
6059 ScreenLines[off_to] = ' ';
6060 if (enc_utf8)
6061 ScreenLinesUC[off_to] = 0;
6062 screen_char(off_to, row, col + coloff);
6063 }
6064#endif
6065
6066 if (clear_width > 0
6067#ifdef FEAT_RIGHTLEFT
6068 && !rlflag
6069#endif
6070 )
6071 {
6072#ifdef FEAT_GUI
6073 int startCol = col;
6074#endif
6075
6076 /* blank out the rest of the line */
6077 while (col < clear_width && ScreenLines[off_to] == ' '
6078 && ScreenAttrs[off_to] == 0
6079#ifdef FEAT_MBYTE
6080 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6081#endif
6082 )
6083 {
6084 ++off_to;
6085 ++col;
6086 }
6087 if (col < clear_width)
6088 {
6089#ifdef FEAT_GUI
6090 /*
6091 * In the GUI, clearing the rest of the line may leave pixels
6092 * behind if the first character cleared was bold. Some bold
6093 * fonts spill over the left. In this case we redraw the previous
6094 * character too. If we didn't skip any blanks above, then we
6095 * only redraw if the character wasn't already redrawn anyway.
6096 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006097 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 {
6099 hl = ScreenAttrs[off_to];
6100 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006101 {
6102 int prev_cells = 1;
6103# ifdef FEAT_MBYTE
6104 if (enc_utf8)
6105 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6106 * that its width is 2. */
6107 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6108 else if (enc_dbcs != 0)
6109 {
6110 /* find previous character by counting from first
6111 * column and get its width. */
6112 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006113 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006114
6115 while (off < off_to)
6116 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006117 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006118 off += prev_cells;
6119 }
6120 }
6121
6122 if (enc_dbcs != 0 && prev_cells > 1)
6123 screen_char_2(off_to - prev_cells, row,
6124 col + coloff - prev_cells);
6125 else
6126# endif
6127 screen_char(off_to - prev_cells, row,
6128 col + coloff - prev_cells);
6129 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130 }
6131#endif
6132 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6133 ' ', ' ', 0);
6134#ifdef FEAT_VERTSPLIT
6135 off_to += clear_width - col;
6136 col = clear_width;
6137#endif
6138 }
6139 }
6140
6141 if (clear_width > 0)
6142 {
6143#ifdef FEAT_VERTSPLIT
6144 /* For a window that's left of another, draw the separator char. */
6145 if (col + coloff < Columns)
6146 {
6147 int c;
6148
6149 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006150 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006152 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6153 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154# endif
6155 || ScreenAttrs[off_to] != hl)
6156 {
6157 ScreenLines[off_to] = c;
6158 ScreenAttrs[off_to] = hl;
6159# ifdef FEAT_MBYTE
6160 if (enc_utf8)
6161 {
6162 if (c >= 0x80)
6163 {
6164 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006165 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166 }
6167 else
6168 ScreenLinesUC[off_to] = 0;
6169 }
6170# endif
6171 screen_char(off_to, row, col + coloff);
6172 }
6173 }
6174 else
6175#endif
6176 LineWraps[row] = FALSE;
6177 }
6178}
6179
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006180#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006182 * Mirror text "str" for right-left displaying.
6183 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006185 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186rl_mirror(str)
6187 char_u *str;
6188{
6189 char_u *p1, *p2;
6190 int t;
6191
6192 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6193 {
6194 t = *p1;
6195 *p1 = *p2;
6196 *p2 = t;
6197 }
6198}
6199#endif
6200
6201#if defined(FEAT_WINDOWS) || defined(PROTO)
6202/*
6203 * mark all status lines for redraw; used after first :cd
6204 */
6205 void
6206status_redraw_all()
6207{
6208 win_T *wp;
6209
6210 for (wp = firstwin; wp; wp = wp->w_next)
6211 if (wp->w_status_height)
6212 {
6213 wp->w_redr_status = TRUE;
6214 redraw_later(VALID);
6215 }
6216}
6217
6218/*
6219 * mark all status lines of the current buffer for redraw
6220 */
6221 void
6222status_redraw_curbuf()
6223{
6224 win_T *wp;
6225
6226 for (wp = firstwin; wp; wp = wp->w_next)
6227 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6228 {
6229 wp->w_redr_status = TRUE;
6230 redraw_later(VALID);
6231 }
6232}
6233
6234/*
6235 * Redraw all status lines that need to be redrawn.
6236 */
6237 void
6238redraw_statuslines()
6239{
6240 win_T *wp;
6241
6242 for (wp = firstwin; wp; wp = wp->w_next)
6243 if (wp->w_redr_status)
6244 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006245 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006246 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247}
6248#endif
6249
6250#if (defined(FEAT_WILDMENU) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6251/*
6252 * Redraw all status lines at the bottom of frame "frp".
6253 */
6254 void
6255win_redraw_last_status(frp)
6256 frame_T *frp;
6257{
6258 if (frp->fr_layout == FR_LEAF)
6259 frp->fr_win->w_redr_status = TRUE;
6260 else if (frp->fr_layout == FR_ROW)
6261 {
6262 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6263 win_redraw_last_status(frp);
6264 }
6265 else /* frp->fr_layout == FR_COL */
6266 {
6267 frp = frp->fr_child;
6268 while (frp->fr_next != NULL)
6269 frp = frp->fr_next;
6270 win_redraw_last_status(frp);
6271 }
6272}
6273#endif
6274
6275#ifdef FEAT_VERTSPLIT
6276/*
6277 * Draw the verticap separator right of window "wp" starting with line "row".
6278 */
6279 static void
6280draw_vsep_win(wp, row)
6281 win_T *wp;
6282 int row;
6283{
6284 int hl;
6285 int c;
6286
6287 if (wp->w_vsep_width)
6288 {
6289 /* draw the vertical separator right of this window */
6290 c = fillchar_vsep(&hl);
6291 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6292 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6293 c, ' ', hl);
6294 }
6295}
6296#endif
6297
6298#ifdef FEAT_WILDMENU
6299static int status_match_len __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006300static int skip_status_match_char __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301
6302/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006303 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304 */
6305 static int
6306status_match_len(xp, s)
6307 expand_T *xp;
6308 char_u *s;
6309{
6310 int len = 0;
6311
6312#ifdef FEAT_MENU
6313 int emenu = (xp->xp_context == EXPAND_MENUS
6314 || xp->xp_context == EXPAND_MENUNAMES);
6315
6316 /* Check for menu separators - replace with '|'. */
6317 if (emenu && menu_is_separator(s))
6318 return 1;
6319#endif
6320
6321 while (*s != NUL)
6322 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006323 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006324 len += ptr2cells(s);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006325 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 }
6327
6328 return len;
6329}
6330
6331/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006332 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006333 * These are backslashes used for escaping. Do show backslashes in help tags.
6334 */
6335 static int
6336skip_status_match_char(xp, s)
6337 expand_T *xp;
6338 char_u *s;
6339{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006340 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006341#ifdef FEAT_MENU
6342 || ((xp->xp_context == EXPAND_MENUS
6343 || xp->xp_context == EXPAND_MENUNAMES)
6344 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6345#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006346 )
6347 {
6348#ifndef BACKSLASH_IN_FILENAME
6349 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6350 return 2;
6351#endif
6352 return 1;
6353 }
6354 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006355}
6356
6357/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358 * Show wildchar matches in the status line.
6359 * Show at least the "match" item.
6360 * We start at item 'first_match' in the list and show all matches that fit.
6361 *
6362 * If inversion is possible we use it. Else '=' characters are used.
6363 */
6364 void
6365win_redr_status_matches(xp, num_matches, matches, match, showtail)
6366 expand_T *xp;
6367 int num_matches;
6368 char_u **matches; /* list of matches */
6369 int match;
6370 int showtail;
6371{
6372#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6373 int row;
6374 char_u *buf;
6375 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006376 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377 int fillchar;
6378 int attr;
6379 int i;
6380 int highlight = TRUE;
6381 char_u *selstart = NULL;
6382 int selstart_col = 0;
6383 char_u *selend = NULL;
6384 static int first_match = 0;
6385 int add_left = FALSE;
6386 char_u *s;
6387#ifdef FEAT_MENU
6388 int emenu;
6389#endif
6390#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6391 int l;
6392#endif
6393
6394 if (matches == NULL) /* interrupted completion? */
6395 return;
6396
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006397#ifdef FEAT_MBYTE
6398 if (has_mbyte)
6399 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6400 else
6401#endif
6402 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 if (buf == NULL)
6404 return;
6405
6406 if (match == -1) /* don't show match but original text */
6407 {
6408 match = 0;
6409 highlight = FALSE;
6410 }
6411 /* count 1 for the ending ">" */
6412 clen = status_match_len(xp, L_MATCH(match)) + 3;
6413 if (match == 0)
6414 first_match = 0;
6415 else if (match < first_match)
6416 {
6417 /* jumping left, as far as we can go */
6418 first_match = match;
6419 add_left = TRUE;
6420 }
6421 else
6422 {
6423 /* check if match fits on the screen */
6424 for (i = first_match; i < match; ++i)
6425 clen += status_match_len(xp, L_MATCH(i)) + 2;
6426 if (first_match > 0)
6427 clen += 2;
6428 /* jumping right, put match at the left */
6429 if ((long)clen > Columns)
6430 {
6431 first_match = match;
6432 /* if showing the last match, we can add some on the left */
6433 clen = 2;
6434 for (i = match; i < num_matches; ++i)
6435 {
6436 clen += status_match_len(xp, L_MATCH(i)) + 2;
6437 if ((long)clen >= Columns)
6438 break;
6439 }
6440 if (i == num_matches)
6441 add_left = TRUE;
6442 }
6443 }
6444 if (add_left)
6445 while (first_match > 0)
6446 {
6447 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6448 if ((long)clen >= Columns)
6449 break;
6450 --first_match;
6451 }
6452
6453 fillchar = fillchar_status(&attr, TRUE);
6454
6455 if (first_match == 0)
6456 {
6457 *buf = NUL;
6458 len = 0;
6459 }
6460 else
6461 {
6462 STRCPY(buf, "< ");
6463 len = 2;
6464 }
6465 clen = len;
6466
6467 i = first_match;
6468 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6469 {
6470 if (i == match)
6471 {
6472 selstart = buf + len;
6473 selstart_col = clen;
6474 }
6475
6476 s = L_MATCH(i);
6477 /* Check for menu separators - replace with '|' */
6478#ifdef FEAT_MENU
6479 emenu = (xp->xp_context == EXPAND_MENUS
6480 || xp->xp_context == EXPAND_MENUNAMES);
6481 if (emenu && menu_is_separator(s))
6482 {
6483 STRCPY(buf + len, transchar('|'));
6484 l = (int)STRLEN(buf + len);
6485 len += l;
6486 clen += l;
6487 }
6488 else
6489#endif
6490 for ( ; *s != NUL; ++s)
6491 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006492 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493 clen += ptr2cells(s);
6494#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006495 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496 {
6497 STRNCPY(buf + len, s, l);
6498 s += l - 1;
6499 len += l;
6500 }
6501 else
6502#endif
6503 {
6504 STRCPY(buf + len, transchar_byte(*s));
6505 len += (int)STRLEN(buf + len);
6506 }
6507 }
6508 if (i == match)
6509 selend = buf + len;
6510
6511 *(buf + len++) = ' ';
6512 *(buf + len++) = ' ';
6513 clen += 2;
6514 if (++i == num_matches)
6515 break;
6516 }
6517
6518 if (i != num_matches)
6519 {
6520 *(buf + len++) = '>';
6521 ++clen;
6522 }
6523
6524 buf[len] = NUL;
6525
6526 row = cmdline_row - 1;
6527 if (row >= 0)
6528 {
6529 if (wild_menu_showing == 0)
6530 {
6531 if (msg_scrolled > 0)
6532 {
6533 /* Put the wildmenu just above the command line. If there is
6534 * no room, scroll the screen one line up. */
6535 if (cmdline_row == Rows - 1)
6536 {
6537 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
6538 ++msg_scrolled;
6539 }
6540 else
6541 {
6542 ++cmdline_row;
6543 ++row;
6544 }
6545 wild_menu_showing = WM_SCROLLED;
6546 }
6547 else
6548 {
6549 /* Create status line if needed by setting 'laststatus' to 2.
6550 * Set 'winminheight' to zero to avoid that the window is
6551 * resized. */
6552 if (lastwin->w_status_height == 0)
6553 {
6554 save_p_ls = p_ls;
6555 save_p_wmh = p_wmh;
6556 p_ls = 2;
6557 p_wmh = 0;
6558 last_status(FALSE);
6559 }
6560 wild_menu_showing = WM_SHOWN;
6561 }
6562 }
6563
6564 screen_puts(buf, row, 0, attr);
6565 if (selstart != NULL && highlight)
6566 {
6567 *selend = NUL;
6568 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
6569 }
6570
6571 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6572 }
6573
6574#ifdef FEAT_VERTSPLIT
6575 win_redraw_last_status(topframe);
6576#else
6577 lastwin->w_redr_status = TRUE;
6578#endif
6579 vim_free(buf);
6580}
6581#endif
6582
6583#if defined(FEAT_WINDOWS) || defined(PROTO)
6584/*
6585 * Redraw the status line of window wp.
6586 *
6587 * If inversion is possible we use it. Else '=' characters are used.
6588 */
6589 void
6590win_redr_status(wp)
6591 win_T *wp;
6592{
6593 int row;
6594 char_u *p;
6595 int len;
6596 int fillchar;
6597 int attr;
6598 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006599 static int busy = FALSE;
6600
6601 /* It's possible to get here recursively when 'statusline' (indirectly)
6602 * invokes ":redrawstatus". Simply ignore the call then. */
6603 if (busy)
6604 return;
6605 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606
6607 wp->w_redr_status = FALSE;
6608 if (wp->w_status_height == 0)
6609 {
6610 /* no status line, can only be last window */
6611 redraw_cmdline = TRUE;
6612 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006613 else if (!redrawing()
6614#ifdef FEAT_INS_EXPAND
6615 /* don't update status line when popup menu is visible and may be
6616 * drawn over it */
6617 || pum_visible()
6618#endif
6619 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620 {
6621 /* Don't redraw right now, do it later. */
6622 wp->w_redr_status = TRUE;
6623 }
6624#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006625 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626 {
6627 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006628 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629 }
6630#endif
6631 else
6632 {
6633 fillchar = fillchar_status(&attr, wp == curwin);
6634
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006635 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636 p = NameBuff;
6637 len = (int)STRLEN(p);
6638
6639 if (wp->w_buffer->b_help
6640#ifdef FEAT_QUICKFIX
6641 || wp->w_p_pvw
6642#endif
6643 || bufIsChanged(wp->w_buffer)
6644 || wp->w_buffer->b_p_ro)
6645 *(p + len++) = ' ';
6646 if (wp->w_buffer->b_help)
6647 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006648 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649 len += (int)STRLEN(p + len);
6650 }
6651#ifdef FEAT_QUICKFIX
6652 if (wp->w_p_pvw)
6653 {
6654 STRCPY(p + len, _("[Preview]"));
6655 len += (int)STRLEN(p + len);
6656 }
6657#endif
6658 if (bufIsChanged(wp->w_buffer))
6659 {
6660 STRCPY(p + len, "[+]");
6661 len += 3;
6662 }
6663 if (wp->w_buffer->b_p_ro)
6664 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006665 STRCPY(p + len, _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666 len += 4;
6667 }
6668
6669#ifndef FEAT_VERTSPLIT
6670 this_ru_col = ru_col;
6671 if (this_ru_col < (Columns + 1) / 2)
6672 this_ru_col = (Columns + 1) / 2;
6673#else
6674 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
6675 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
6676 this_ru_col = (W_WIDTH(wp) + 1) / 2;
6677 if (this_ru_col <= 1)
6678 {
6679 p = (char_u *)"<"; /* No room for file name! */
6680 len = 1;
6681 }
6682 else
6683#endif
6684#ifdef FEAT_MBYTE
6685 if (has_mbyte)
6686 {
6687 int clen = 0, i;
6688
6689 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006690 clen = mb_string2cells(p, -1);
6691
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692 /* Find first character that will fit.
6693 * Going from start to end is much faster for DBCS. */
6694 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006695 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 clen -= (*mb_ptr2cells)(p + i);
6697 len = clen;
6698 if (i > 0)
6699 {
6700 p = p + i - 1;
6701 *p = '<';
6702 ++len;
6703 }
6704
6705 }
6706 else
6707#endif
6708 if (len > this_ru_col - 1)
6709 {
6710 p += len - (this_ru_col - 1);
6711 *p = '<';
6712 len = this_ru_col - 1;
6713 }
6714
6715 row = W_WINROW(wp) + wp->w_height;
6716 screen_puts(p, row, W_WINCOL(wp), attr);
6717 screen_fill(row, row + 1, len + W_WINCOL(wp),
6718 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
6719
6720 if (get_keymap_str(wp, NameBuff, MAXPATHL)
6721 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6722 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
6723 - 1 + W_WINCOL(wp)), attr);
6724
6725#ifdef FEAT_CMDL_INFO
6726 win_redr_ruler(wp, TRUE);
6727#endif
6728 }
6729
6730#ifdef FEAT_VERTSPLIT
6731 /*
6732 * May need to draw the character below the vertical separator.
6733 */
6734 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6735 {
6736 if (stl_connected(wp))
6737 fillchar = fillchar_status(&attr, wp == curwin);
6738 else
6739 fillchar = fillchar_vsep(&attr);
6740 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6741 attr);
6742 }
6743#endif
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006744 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745}
6746
Bram Moolenaar238a5642006-02-21 22:12:05 +00006747#ifdef FEAT_STL_OPT
6748/*
6749 * Redraw the status line according to 'statusline' and take care of any
6750 * errors encountered.
6751 */
6752 static void
Bram Moolenaar362f3562009-11-03 16:20:34 +00006753redraw_custom_statusline(wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006754 win_T *wp;
6755{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006756 static int entered = FALSE;
6757 int save_called_emsg = called_emsg;
6758
6759 /* When called recursively return. This can happen when the statusline
6760 * contains an expression that triggers a redraw. */
6761 if (entered)
6762 return;
6763 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006764
6765 called_emsg = FALSE;
6766 win_redr_custom(wp, FALSE);
6767 if (called_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006768 {
6769 /* When there is an error disable the statusline, otherwise the
6770 * display is messed up with errors and a redraw triggers the problem
6771 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00006772 set_string_option_direct((char_u *)"statusline", -1,
6773 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006774 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006775 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00006776 called_emsg |= save_called_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006777 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006778}
6779#endif
6780
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781# ifdef FEAT_VERTSPLIT
6782/*
6783 * Return TRUE if the status line of window "wp" is connected to the status
6784 * line of the window right of it. If not, then it's a vertical separator.
6785 * Only call if (wp->w_vsep_width != 0).
6786 */
6787 int
6788stl_connected(wp)
6789 win_T *wp;
6790{
6791 frame_T *fr;
6792
6793 fr = wp->w_frame;
6794 while (fr->fr_parent != NULL)
6795 {
6796 if (fr->fr_parent->fr_layout == FR_COL)
6797 {
6798 if (fr->fr_next != NULL)
6799 break;
6800 }
6801 else
6802 {
6803 if (fr->fr_next != NULL)
6804 return TRUE;
6805 }
6806 fr = fr->fr_parent;
6807 }
6808 return FALSE;
6809}
6810# endif
6811
6812#endif /* FEAT_WINDOWS */
6813
6814#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
6815/*
6816 * Get the value to show for the language mappings, active 'keymap'.
6817 */
6818 int
6819get_keymap_str(wp, buf, len)
6820 win_T *wp;
6821 char_u *buf; /* buffer for the result */
6822 int len; /* length of buffer */
6823{
6824 char_u *p;
6825
6826 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
6827 return FALSE;
6828
6829 {
6830#ifdef FEAT_EVAL
6831 buf_T *old_curbuf = curbuf;
6832 win_T *old_curwin = curwin;
6833 char_u *s;
6834
6835 curbuf = wp->w_buffer;
6836 curwin = wp;
6837 STRCPY(buf, "b:keymap_name"); /* must be writable */
6838 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006839 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 --emsg_skip;
6841 curbuf = old_curbuf;
6842 curwin = old_curwin;
6843 if (p == NULL || *p == NUL)
6844#endif
6845 {
6846#ifdef FEAT_KEYMAP
6847 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
6848 p = wp->w_buffer->b_p_keymap;
6849 else
6850#endif
6851 p = (char_u *)"lang";
6852 }
6853 if ((int)(STRLEN(p) + 3) < len)
6854 sprintf((char *)buf, "<%s>", p);
6855 else
6856 buf[0] = NUL;
6857#ifdef FEAT_EVAL
6858 vim_free(s);
6859#endif
6860 }
6861 return buf[0] != NUL;
6862}
6863#endif
6864
6865#if defined(FEAT_STL_OPT) || defined(PROTO)
6866/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006867 * Redraw the status line or ruler of window "wp".
6868 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 */
6870 static void
Bram Moolenaar9372a112005-12-06 19:59:18 +00006871win_redr_custom(wp, draw_ruler)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 win_T *wp;
Bram Moolenaar9372a112005-12-06 19:59:18 +00006873 int draw_ruler; /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874{
Bram Moolenaar1d633412013-12-11 15:52:01 +01006875 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876 int attr;
6877 int curattr;
6878 int row;
6879 int col = 0;
6880 int maxwidth;
6881 int width;
6882 int n;
6883 int len;
6884 int fillchar;
6885 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00006886 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006888 struct stl_hlrec hltab[STL_MAX_ITEM];
6889 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006890 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01006891 win_T *ewp;
6892 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893
Bram Moolenaar1d633412013-12-11 15:52:01 +01006894 /* There is a tiny chance that this gets called recursively: When
6895 * redrawing a status line triggers redrawing the ruler or tabline.
6896 * Avoid trouble by not allowing recursion. */
6897 if (entered)
6898 return;
6899 entered = TRUE;
6900
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006902 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006904 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006905 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006906 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00006907 fillchar = ' ';
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006908 attr = hl_attr(HLF_TPF);
6909 maxwidth = Columns;
6910# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006911 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006912# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006914 else
6915 {
6916 row = W_WINROW(wp) + wp->w_height;
6917 fillchar = fillchar_status(&attr, wp == curwin);
6918 maxwidth = W_WIDTH(wp);
6919
6920 if (draw_ruler)
6921 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006922 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006923 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006924 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006925 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006926 if (*++stl == '-')
6927 stl++;
6928 if (atoi((char *)stl))
6929 while (VIM_ISDIGIT(*stl))
6930 stl++;
6931 if (*stl++ != '(')
6932 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006933 }
6934#ifdef FEAT_VERTSPLIT
6935 col = ru_col - (Columns - W_WIDTH(wp));
6936 if (col < (W_WIDTH(wp) + 1) / 2)
6937 col = (W_WIDTH(wp) + 1) / 2;
6938#else
6939 col = ru_col;
6940 if (col > (Columns + 1) / 2)
6941 col = (Columns + 1) / 2;
6942#endif
6943 maxwidth = W_WIDTH(wp) - col;
6944#ifdef FEAT_WINDOWS
6945 if (!wp->w_status_height)
6946#endif
6947 {
6948 row = Rows - 1;
6949 --maxwidth; /* writing in last column may cause scrolling */
6950 fillchar = ' ';
6951 attr = 0;
6952 }
6953
6954# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006955 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006956# endif
6957 }
6958 else
6959 {
6960 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006961 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006962 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00006963 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006964# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006965 use_sandbox = was_set_insecurely((char_u *)"statusline",
6966 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006967# endif
6968 }
6969
6970#ifdef FEAT_VERTSPLIT
6971 col += W_WINCOL(wp);
6972#endif
6973 }
6974
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01006976 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977
Bram Moolenaar61452852011-02-01 18:01:11 +01006978 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
6979 * the cursor away and back. */
6980 ewp = wp == NULL ? curwin : wp;
6981 p_crb_save = ewp->w_p_crb;
6982 ewp->w_p_crb = FALSE;
6983
Bram Moolenaar362f3562009-11-03 16:20:34 +00006984 /* Make a copy, because the statusline may include a function call that
6985 * might change the option value and free the memory. */
6986 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01006987 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00006988 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006989 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006990 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01006991 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01006993 /* Make all characters printable. */
6994 p = transstr(buf);
6995 if (p != NULL)
6996 {
6997 vim_strncpy(buf, p, sizeof(buf) - 1);
6998 vim_free(p);
6999 }
7000
7001 /* fill up with "fillchar" */
7002 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007003 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 {
7005#ifdef FEAT_MBYTE
7006 len += (*mb_char2bytes)(fillchar, buf + len);
7007#else
7008 buf[len++] = fillchar;
7009#endif
7010 ++width;
7011 }
7012 buf[len] = NUL;
7013
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007014 /*
7015 * Draw each snippet with the specified highlighting.
7016 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017 curattr = attr;
7018 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007019 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007021 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022 screen_puts_len(p, len, row, col, curattr);
7023 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007024 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007026 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007028 else if (hltab[n].userhl < 0)
7029 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00007031 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007032 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033#endif
7034 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007035 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036 }
7037 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007038
7039 if (wp == NULL)
7040 {
7041 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7042 col = 0;
7043 len = 0;
7044 p = buf;
7045 fillchar = 0;
7046 for (n = 0; tabtab[n].start != NULL; n++)
7047 {
7048 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7049 while (col < len)
7050 TabPageIdxs[col++] = fillchar;
7051 p = tabtab[n].start;
7052 fillchar = tabtab[n].userhl;
7053 }
7054 while (col < Columns)
7055 TabPageIdxs[col++] = fillchar;
7056 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007057
7058theend:
7059 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060}
7061
7062#endif /* FEAT_STL_OPT */
7063
7064/*
7065 * Output a single character directly to the screen and update ScreenLines.
7066 */
7067 void
7068screen_putchar(c, row, col, attr)
7069 int c;
7070 int row, col;
7071 int attr;
7072{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073 char_u buf[MB_MAXBYTES + 1];
7074
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007075#ifdef FEAT_MBYTE
7076 if (has_mbyte)
7077 buf[(*mb_char2bytes)(c, buf)] = NUL;
7078 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007080 {
7081 buf[0] = c;
7082 buf[1] = NUL;
7083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 screen_puts(buf, row, col, attr);
7085}
7086
7087/*
7088 * Get a single character directly from ScreenLines into "bytes[]".
7089 * Also return its attribute in *attrp;
7090 */
7091 void
7092screen_getbytes(row, col, bytes, attrp)
7093 int row, col;
7094 char_u *bytes;
7095 int *attrp;
7096{
7097 unsigned off;
7098
7099 /* safety check */
7100 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7101 {
7102 off = LineOffset[row] + col;
7103 *attrp = ScreenAttrs[off];
7104 bytes[0] = ScreenLines[off];
7105 bytes[1] = NUL;
7106
7107#ifdef FEAT_MBYTE
7108 if (enc_utf8 && ScreenLinesUC[off] != 0)
7109 bytes[utfc_char2bytes(off, bytes)] = NUL;
7110 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7111 {
7112 bytes[0] = ScreenLines[off];
7113 bytes[1] = ScreenLines2[off];
7114 bytes[2] = NUL;
7115 }
7116 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7117 {
7118 bytes[1] = ScreenLines[off + 1];
7119 bytes[2] = NUL;
7120 }
7121#endif
7122 }
7123}
7124
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007125#ifdef FEAT_MBYTE
7126static int screen_comp_differs __ARGS((int, int*));
7127
7128/*
7129 * Return TRUE if composing characters for screen posn "off" differs from
7130 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007131 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007132 */
7133 static int
7134screen_comp_differs(off, u8cc)
7135 int off;
7136 int *u8cc;
7137{
7138 int i;
7139
7140 for (i = 0; i < Screen_mco; ++i)
7141 {
7142 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7143 return TRUE;
7144 if (u8cc[i] == 0)
7145 break;
7146 }
7147 return FALSE;
7148}
7149#endif
7150
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151/*
7152 * Put string '*text' on the screen at position 'row' and 'col', with
7153 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7154 * Note: only outputs within one row, message is truncated at screen boundary!
7155 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7156 */
7157 void
7158screen_puts(text, row, col, attr)
7159 char_u *text;
7160 int row;
7161 int col;
7162 int attr;
7163{
7164 screen_puts_len(text, -1, row, col, attr);
7165}
7166
7167/*
7168 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7169 * a NUL.
7170 */
7171 void
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007172screen_puts_len(text, textlen, row, col, attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173 char_u *text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007174 int textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175 int row;
7176 int col;
7177 int attr;
7178{
7179 unsigned off;
7180 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007181 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182 int c;
7183#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007184 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185 int mbyte_blen = 1;
7186 int mbyte_cells = 1;
7187 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007188 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189 int clear_next_cell = FALSE;
7190# ifdef FEAT_ARABIC
7191 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007192 int pc, nc, nc1;
7193 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194# endif
7195#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007196#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7197 int force_redraw_this;
7198 int force_redraw_next = FALSE;
7199#endif
7200 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201
7202 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7203 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007204 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205
Bram Moolenaarc236c162008-07-13 17:41:49 +00007206#ifdef FEAT_MBYTE
7207 /* When drawing over the right halve of a double-wide char clear out the
7208 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007209 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007210# ifdef FEAT_GUI
7211 && !gui.in_use
7212# endif
7213 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007214 {
7215 ScreenLines[off - 1] = ' ';
7216 ScreenAttrs[off - 1] = 0;
7217 if (enc_utf8)
7218 {
7219 ScreenLinesUC[off - 1] = 0;
7220 ScreenLinesC[0][off - 1] = 0;
7221 }
7222 /* redraw the previous cell, make it empty */
7223 screen_char(off - 1, row, col - 1);
7224 /* force the cell at "col" to be redrawn */
7225 force_redraw_next = TRUE;
7226 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007227#endif
7228
Bram Moolenaar367329b2007-08-30 11:53:22 +00007229#ifdef FEAT_MBYTE
7230 max_off = LineOffset[row] + screen_Columns;
7231#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007232 while (col < screen_Columns
7233 && (len < 0 || (int)(ptr - text) < len)
7234 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 {
7236 c = *ptr;
7237#ifdef FEAT_MBYTE
7238 /* check if this is the first byte of a multibyte */
7239 if (has_mbyte)
7240 {
7241 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007242 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007244 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7246 mbyte_cells = 1;
7247 else if (enc_dbcs != 0)
7248 mbyte_cells = mbyte_blen;
7249 else /* enc_utf8 */
7250 {
7251 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007252 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 (int)((text + len) - ptr));
7254 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007255 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007257# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 /* Non-BMP character: display as ? or fullwidth ?. */
7259 if (u8c >= 0x10000)
7260 {
7261 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7262 if (attr == 0)
7263 attr = hl_attr(HLF_8);
7264 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007265# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266# ifdef FEAT_ARABIC
7267 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7268 {
7269 /* Do Arabic shaping. */
7270 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7271 {
7272 /* Past end of string to be displayed. */
7273 nc = NUL;
7274 nc1 = NUL;
7275 }
7276 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007277 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007278 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7279 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007280 nc1 = pcc[0];
7281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282 pc = prev_c;
7283 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007284 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 }
7286 else
7287 prev_c = u8c;
7288# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007289 if (col + mbyte_cells > screen_Columns)
7290 {
7291 /* Only 1 cell left, but character requires 2 cells:
7292 * display a '>' in the last column to avoid wrapping. */
7293 c = '>';
7294 mbyte_cells = 1;
7295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296 }
7297 }
7298#endif
7299
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007300#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7301 force_redraw_this = force_redraw_next;
7302 force_redraw_next = FALSE;
7303#endif
7304
7305 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306#ifdef FEAT_MBYTE
7307 || (mbyte_cells == 2
7308 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7309 || (enc_dbcs == DBCS_JPNU
7310 && c == 0x8e
7311 && ScreenLines2[off] != ptr[1])
7312 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007313 && (ScreenLinesUC[off] !=
7314 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7315 || (ScreenLinesUC[off] != 0
7316 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317#endif
7318 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007319 || exmode_active;
7320
7321 if (need_redraw
7322#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7323 || force_redraw_this
7324#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325 )
7326 {
7327#if defined(FEAT_GUI) || defined(UNIX)
7328 /* The bold trick makes a single row of pixels appear in the next
7329 * character. When a bold character is removed, the next
7330 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007331 * and for some xterms. */
7332 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333# ifdef FEAT_GUI
7334 gui.in_use
7335# endif
7336# if defined(FEAT_GUI) && defined(UNIX)
7337 ||
7338# endif
7339# ifdef UNIX
7340 term_is_xterm
7341# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007342 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007344 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007346 if (n > HL_ALL)
7347 n = syn_attr2attr(n);
7348 if (n & HL_BOLD)
7349 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350 }
7351#endif
7352#ifdef FEAT_MBYTE
7353 /* When at the end of the text and overwriting a two-cell
7354 * character with a one-cell character, need to clear the next
7355 * cell. Also when overwriting the left halve of a two-cell char
7356 * with the right halve of a two-cell char. Do this only once
7357 * (mb_off2cells() may return 2 on the right halve). */
7358 if (clear_next_cell)
7359 clear_next_cell = FALSE;
7360 else if (has_mbyte
7361 && (len < 0 ? ptr[mbyte_blen] == NUL
7362 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007363 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007365 && (*mb_off2cells)(off, max_off) == 1
7366 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 clear_next_cell = TRUE;
7368
7369 /* Make sure we never leave a second byte of a double-byte behind,
7370 * it confuses mb_off2cells(). */
7371 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007372 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007374 && (*mb_off2cells)(off, max_off) == 1
7375 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376 ScreenLines[off + mbyte_blen] = 0;
7377#endif
7378 ScreenLines[off] = c;
7379 ScreenAttrs[off] = attr;
7380#ifdef FEAT_MBYTE
7381 if (enc_utf8)
7382 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007383 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384 ScreenLinesUC[off] = 0;
7385 else
7386 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007387 int i;
7388
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007390 for (i = 0; i < Screen_mco; ++i)
7391 {
7392 ScreenLinesC[i][off] = u8cc[i];
7393 if (u8cc[i] == 0)
7394 break;
7395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396 }
7397 if (mbyte_cells == 2)
7398 {
7399 ScreenLines[off + 1] = 0;
7400 ScreenAttrs[off + 1] = attr;
7401 }
7402 screen_char(off, row, col);
7403 }
7404 else if (mbyte_cells == 2)
7405 {
7406 ScreenLines[off + 1] = ptr[1];
7407 ScreenAttrs[off + 1] = attr;
7408 screen_char_2(off, row, col);
7409 }
7410 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7411 {
7412 ScreenLines2[off] = ptr[1];
7413 screen_char(off, row, col);
7414 }
7415 else
7416#endif
7417 screen_char(off, row, col);
7418 }
7419#ifdef FEAT_MBYTE
7420 if (has_mbyte)
7421 {
7422 off += mbyte_cells;
7423 col += mbyte_cells;
7424 ptr += mbyte_blen;
7425 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007426 {
7427 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007429 len = -1;
7430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431 }
7432 else
7433#endif
7434 {
7435 ++off;
7436 ++col;
7437 ++ptr;
7438 }
7439 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007440
7441#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7442 /* If we detected the next character needs to be redrawn, but the text
7443 * doesn't extend up to there, update the character here. */
7444 if (force_redraw_next && col < screen_Columns)
7445 {
7446# ifdef FEAT_MBYTE
7447 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7448 screen_char_2(off, row, col);
7449 else
7450# endif
7451 screen_char(off, row, col);
7452 }
7453#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454}
7455
7456#ifdef FEAT_SEARCH_EXTRA
7457/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007458 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459 */
7460 static void
7461start_search_hl()
7462{
7463 if (p_hls && !no_hlsearch)
7464 {
7465 last_pat_prog(&search_hl.rm);
7466 search_hl.attr = hl_attr(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007467# ifdef FEAT_RELTIME
7468 /* Set the time limit to 'redrawtime'. */
7469 profile_setlimit(p_rdt, &search_hl.tm);
7470# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 }
7472}
7473
7474/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007475 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476 */
7477 static void
7478end_search_hl()
7479{
7480 if (search_hl.rm.regprog != NULL)
7481 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007482 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 search_hl.rm.regprog = NULL;
7484 }
7485}
7486
7487/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007488 * Init for calling prepare_search_hl().
7489 */
7490 static void
7491init_search_hl(wp)
7492 win_T *wp;
7493{
7494 matchitem_T *cur;
7495
7496 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7497 * match */
7498 cur = wp->w_match_head;
7499 while (cur != NULL)
7500 {
7501 cur->hl.rm = cur->match;
7502 if (cur->hlg_id == 0)
7503 cur->hl.attr = 0;
7504 else
7505 cur->hl.attr = syn_id2attr(cur->hlg_id);
7506 cur->hl.buf = wp->w_buffer;
7507 cur->hl.lnum = 0;
7508 cur->hl.first_lnum = 0;
7509# ifdef FEAT_RELTIME
7510 /* Set the time limit to 'redrawtime'. */
7511 profile_setlimit(p_rdt, &(cur->hl.tm));
7512# endif
7513 cur = cur->next;
7514 }
7515 search_hl.buf = wp->w_buffer;
7516 search_hl.lnum = 0;
7517 search_hl.first_lnum = 0;
7518 /* time limit is set at the toplevel, for all windows */
7519}
7520
7521/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522 * Advance to the match in window "wp" line "lnum" or past it.
7523 */
7524 static void
7525prepare_search_hl(wp, lnum)
7526 win_T *wp;
7527 linenr_T lnum;
7528{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007529 matchitem_T *cur; /* points to the match list */
7530 match_T *shl; /* points to search_hl or a match */
7531 int shl_flag; /* flag to indicate whether search_hl
7532 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007533 int pos_inprogress; /* marks that position match search is
7534 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535 int n;
7536
7537 /*
7538 * When using a multi-line pattern, start searching at the top
7539 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007540 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007542 cur = wp->w_match_head;
7543 shl_flag = FALSE;
7544 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007546 if (shl_flag == FALSE)
7547 {
7548 shl = &search_hl;
7549 shl_flag = TRUE;
7550 }
7551 else
7552 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553 if (shl->rm.regprog != NULL
7554 && shl->lnum == 0
7555 && re_multiline(shl->rm.regprog))
7556 {
7557 if (shl->first_lnum == 0)
7558 {
7559# ifdef FEAT_FOLDING
7560 for (shl->first_lnum = lnum;
7561 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7562 if (hasFoldingWin(wp, shl->first_lnum - 1,
7563 NULL, NULL, TRUE, NULL))
7564 break;
7565# else
7566 shl->first_lnum = wp->w_topline;
7567# endif
7568 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007569 if (cur != NULL)
7570 cur->pos.cur = 0;
7571 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007573 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7574 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007576 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n, cur);
7577 pos_inprogress = cur == NULL || cur->pos.cur == 0
7578 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579 if (shl->lnum != 0)
7580 {
7581 shl->first_lnum = shl->lnum
7582 + shl->rm.endpos[0].lnum
7583 - shl->rm.startpos[0].lnum;
7584 n = shl->rm.endpos[0].col;
7585 }
7586 else
7587 {
7588 ++shl->first_lnum;
7589 n = 0;
7590 }
7591 }
7592 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007593 if (shl != &search_hl && cur != NULL)
7594 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595 }
7596}
7597
7598/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007599 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600 * Uses shl->buf.
7601 * Sets shl->lnum and shl->rm contents.
7602 * Note: Assumes a previous match is always before "lnum", unless
7603 * shl->lnum is zero.
7604 * Careful: Any pointers for buffer lines will become invalid.
7605 */
7606 static void
Bram Moolenaarb3414592014-06-17 17:48:32 +02007607next_search_hl(win, shl, lnum, mincol, cur)
7608 win_T *win;
7609 match_T *shl; /* points to search_hl or a match */
7610 linenr_T lnum;
7611 colnr_T mincol; /* minimal column for a match */
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007612 matchitem_T *cur; /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613{
7614 linenr_T l;
7615 colnr_T matchcol;
7616 long nmatched;
7617
7618 if (shl->lnum != 0)
7619 {
7620 /* Check for three situations:
7621 * 1. If the "lnum" is below a previous match, start a new search.
7622 * 2. If the previous match includes "mincol", use it.
7623 * 3. Continue after the previous match.
7624 */
7625 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7626 if (lnum > l)
7627 shl->lnum = 0;
7628 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7629 return;
7630 }
7631
7632 /*
7633 * Repeat searching for a match until one is found that includes "mincol"
7634 * or none is found in this line.
7635 */
7636 called_emsg = FALSE;
7637 for (;;)
7638 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007639#ifdef FEAT_RELTIME
7640 /* Stop searching after passing the time limit. */
7641 if (profile_passed_limit(&(shl->tm)))
7642 {
7643 shl->lnum = 0; /* no match found in time */
7644 break;
7645 }
7646#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647 /* Three situations:
7648 * 1. No useful previous match: search from start of line.
7649 * 2. Not Vi compatible or empty match: continue at next character.
7650 * Break the loop if this is beyond the end of the line.
7651 * 3. Vi compatible searching: continue at end of previous match.
7652 */
7653 if (shl->lnum == 0)
7654 matchcol = 0;
7655 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7656 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007657 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007659 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007660
7661 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007662 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007663 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007665 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666 shl->lnum = 0;
7667 break;
7668 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007669#ifdef FEAT_MBYTE
7670 if (has_mbyte)
7671 matchcol += mb_ptr2len(ml);
7672 else
7673#endif
7674 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 }
7676 else
7677 matchcol = shl->rm.endpos[0].col;
7678
7679 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007680 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007682 /* Remember whether shl->rm is using a copy of the regprog in
7683 * cur->match. */
7684 int regprog_is_copy = (shl != &search_hl && cur != NULL
7685 && shl == &cur->hl
7686 && cur->match.regprog == cur->hl.rm.regprog);
7687
Bram Moolenaarb3414592014-06-17 17:48:32 +02007688 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7689 matchcol,
7690#ifdef FEAT_RELTIME
7691 &(shl->tm)
7692#else
7693 NULL
7694#endif
7695 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007696 /* Copy the regprog, in case it got freed and recompiled. */
7697 if (regprog_is_copy)
7698 cur->match.regprog = cur->hl.rm.regprog;
7699
Bram Moolenaarb3414592014-06-17 17:48:32 +02007700 if (called_emsg || got_int)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007701 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007702 /* Error while handling regexp: stop using this regexp. */
7703 if (shl == &search_hl)
7704 {
7705 /* don't free regprog in the match list, it's a copy */
7706 vim_regfree(shl->rm.regprog);
7707 SET_NO_HLSEARCH(TRUE);
7708 }
7709 shl->rm.regprog = NULL;
7710 shl->lnum = 0;
7711 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
7712 message */
7713 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007714 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007715 }
7716 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007717 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007718 else
7719 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720 if (nmatched == 0)
7721 {
7722 shl->lnum = 0; /* no match found */
7723 break;
7724 }
7725 if (shl->rm.startpos[0].lnum > 0
7726 || shl->rm.startpos[0].col >= mincol
7727 || nmatched > 1
7728 || shl->rm.endpos[0].col > mincol)
7729 {
7730 shl->lnum += shl->rm.startpos[0].lnum;
7731 break; /* useful match found */
7732 }
7733 }
7734}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735
Bram Moolenaarb3414592014-06-17 17:48:32 +02007736 static int
7737next_search_hl_pos(shl, lnum, posmatch, mincol)
7738 match_T *shl; /* points to a match */
7739 linenr_T lnum;
7740 posmatch_T *posmatch; /* match positions */
7741 colnr_T mincol; /* minimal column for a match */
7742{
7743 int i;
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02007744 int bot = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007745
7746 shl->lnum = 0;
7747 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
7748 {
7749 if (posmatch->pos[i].lnum == 0)
7750 break;
7751 if (posmatch->pos[i].col < mincol)
7752 continue;
7753 if (posmatch->pos[i].lnum == lnum)
7754 {
7755 if (shl->lnum == lnum)
7756 {
7757 /* partially sort positions by column numbers
7758 * on the same line */
7759 if (posmatch->pos[i].col < posmatch->pos[bot].col)
7760 {
7761 llpos_T tmp = posmatch->pos[i];
7762
7763 posmatch->pos[i] = posmatch->pos[bot];
7764 posmatch->pos[bot] = tmp;
7765 }
7766 }
7767 else
7768 {
7769 bot = i;
7770 shl->lnum = lnum;
7771 }
7772 }
7773 }
7774 posmatch->cur = 0;
7775 if (shl->lnum == lnum)
7776 {
7777 colnr_T start = posmatch->pos[bot].col == 0
7778 ? 0 : posmatch->pos[bot].col - 1;
7779 colnr_T end = posmatch->pos[bot].col == 0
7780 ? MAXCOL : start + posmatch->pos[bot].len;
7781
7782 shl->rm.startpos[0].lnum = 0;
7783 shl->rm.startpos[0].col = start;
7784 shl->rm.endpos[0].lnum = 0;
7785 shl->rm.endpos[0].col = end;
7786 posmatch->cur = bot + 1;
7787 return TRUE;
7788 }
7789 return FALSE;
7790}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02007791#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02007792
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793 static void
7794screen_start_highlight(attr)
7795 int attr;
7796{
7797 attrentry_T *aep = NULL;
7798
7799 screen_attr = attr;
7800 if (full_screen
7801#ifdef WIN3264
7802 && termcap_active
7803#endif
7804 )
7805 {
7806#ifdef FEAT_GUI
7807 if (gui.in_use)
7808 {
7809 char buf[20];
7810
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007811 /* The GUI handles this internally. */
7812 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813 OUT_STR(buf);
7814 }
7815 else
7816#endif
7817 {
7818 if (attr > HL_ALL) /* special HL attr. */
7819 {
7820 if (t_colors > 1)
7821 aep = syn_cterm_attr2entry(attr);
7822 else
7823 aep = syn_term_attr2entry(attr);
7824 if (aep == NULL) /* did ":syntax clear" */
7825 attr = 0;
7826 else
7827 attr = aep->ae_attr;
7828 }
7829 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
7830 out_str(T_MD);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007831 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
7832 && cterm_normal_fg_bold)
7833 /* If the Normal FG color has BOLD attribute and the new HL
7834 * has a FG color defined, clear BOLD. */
7835 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
7837 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007838 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
7839 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840 out_str(T_US);
7841 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
7842 out_str(T_CZH);
7843 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
7844 out_str(T_MR);
7845
7846 /*
7847 * Output the color or start string after bold etc., in case the
7848 * bold etc. override the color setting.
7849 */
7850 if (aep != NULL)
7851 {
7852 if (t_colors > 1)
7853 {
7854 if (aep->ae_u.cterm.fg_color)
7855 term_fg_color(aep->ae_u.cterm.fg_color - 1);
7856 if (aep->ae_u.cterm.bg_color)
7857 term_bg_color(aep->ae_u.cterm.bg_color - 1);
7858 }
7859 else
7860 {
7861 if (aep->ae_u.term.start != NULL)
7862 out_str(aep->ae_u.term.start);
7863 }
7864 }
7865 }
7866 }
7867}
7868
7869 void
7870screen_stop_highlight()
7871{
7872 int do_ME = FALSE; /* output T_ME code */
7873
7874 if (screen_attr != 0
7875#ifdef WIN3264
7876 && termcap_active
7877#endif
7878 )
7879 {
7880#ifdef FEAT_GUI
7881 if (gui.in_use)
7882 {
7883 char buf[20];
7884
7885 /* use internal GUI code */
7886 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
7887 OUT_STR(buf);
7888 }
7889 else
7890#endif
7891 {
7892 if (screen_attr > HL_ALL) /* special HL attr. */
7893 {
7894 attrentry_T *aep;
7895
7896 if (t_colors > 1)
7897 {
7898 /*
7899 * Assume that t_me restores the original colors!
7900 */
7901 aep = syn_cterm_attr2entry(screen_attr);
7902 if (aep != NULL && (aep->ae_u.cterm.fg_color
7903 || aep->ae_u.cterm.bg_color))
7904 do_ME = TRUE;
7905 }
7906 else
7907 {
7908 aep = syn_term_attr2entry(screen_attr);
7909 if (aep != NULL && aep->ae_u.term.stop != NULL)
7910 {
7911 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
7912 do_ME = TRUE;
7913 else
7914 out_str(aep->ae_u.term.stop);
7915 }
7916 }
7917 if (aep == NULL) /* did ":syntax clear" */
7918 screen_attr = 0;
7919 else
7920 screen_attr = aep->ae_attr;
7921 }
7922
7923 /*
7924 * Often all ending-codes are equal to T_ME. Avoid outputting the
7925 * same sequence several times.
7926 */
7927 if (screen_attr & HL_STANDOUT)
7928 {
7929 if (STRCMP(T_SE, T_ME) == 0)
7930 do_ME = TRUE;
7931 else
7932 out_str(T_SE);
7933 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007934 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935 {
7936 if (STRCMP(T_UE, T_ME) == 0)
7937 do_ME = TRUE;
7938 else
7939 out_str(T_UE);
7940 }
7941 if (screen_attr & HL_ITALIC)
7942 {
7943 if (STRCMP(T_CZR, T_ME) == 0)
7944 do_ME = TRUE;
7945 else
7946 out_str(T_CZR);
7947 }
7948 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
7949 out_str(T_ME);
7950
7951 if (t_colors > 1)
7952 {
7953 /* set Normal cterm colors */
7954 if (cterm_normal_fg_color != 0)
7955 term_fg_color(cterm_normal_fg_color - 1);
7956 if (cterm_normal_bg_color != 0)
7957 term_bg_color(cterm_normal_bg_color - 1);
7958 if (cterm_normal_fg_bold)
7959 out_str(T_MD);
7960 }
7961 }
7962 }
7963 screen_attr = 0;
7964}
7965
7966/*
7967 * Reset the colors for a cterm. Used when leaving Vim.
7968 * The machine specific code may override this again.
7969 */
7970 void
7971reset_cterm_colors()
7972{
7973 if (t_colors > 1)
7974 {
7975 /* set Normal cterm colors */
7976 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
7977 {
7978 out_str(T_OP);
7979 screen_attr = -1;
7980 }
7981 if (cterm_normal_fg_bold)
7982 {
7983 out_str(T_ME);
7984 screen_attr = -1;
7985 }
7986 }
7987}
7988
7989/*
7990 * Put character ScreenLines["off"] on the screen at position "row" and "col",
7991 * using the attributes from ScreenAttrs["off"].
7992 */
7993 static void
7994screen_char(off, row, col)
7995 unsigned off;
7996 int row;
7997 int col;
7998{
7999 int attr;
8000
8001 /* Check for illegal values, just in case (could happen just after
8002 * resizing). */
8003 if (row >= screen_Rows || col >= screen_Columns)
8004 return;
8005
Bram Moolenaar494838a2015-02-10 19:20:37 +01008006 /* Outputting a character in the last cell on the screen may scroll the
8007 * screen up. Only do it when the "xn" termcap property is set, otherwise
8008 * mark the character invalid (update it when scrolled up). */
8009 if (*T_XN == NUL
8010 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011#ifdef FEAT_RIGHTLEFT
8012 /* account for first command-line character in rightleft mode */
8013 && !cmdmsg_rl
8014#endif
8015 )
8016 {
8017 ScreenAttrs[off] = (sattr_T)-1;
8018 return;
8019 }
8020
8021 /*
8022 * Stop highlighting first, so it's easier to move the cursor.
8023 */
8024#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
8025 if (screen_char_attr != 0)
8026 attr = screen_char_attr;
8027 else
8028#endif
8029 attr = ScreenAttrs[off];
8030 if (screen_attr != attr)
8031 screen_stop_highlight();
8032
8033 windgoto(row, col);
8034
8035 if (screen_attr != attr)
8036 screen_start_highlight(attr);
8037
8038#ifdef FEAT_MBYTE
8039 if (enc_utf8 && ScreenLinesUC[off] != 0)
8040 {
8041 char_u buf[MB_MAXBYTES + 1];
8042
8043 /* Convert UTF-8 character to bytes and write it. */
8044
8045 buf[utfc_char2bytes(off, buf)] = NUL;
8046
8047 out_str(buf);
8048 if (utf_char2cells(ScreenLinesUC[off]) > 1)
8049 ++screen_cur_col;
8050 }
8051 else
8052#endif
8053 {
8054#ifdef FEAT_MBYTE
8055 out_flush_check();
8056#endif
8057 out_char(ScreenLines[off]);
8058#ifdef FEAT_MBYTE
8059 /* double-byte character in single-width cell */
8060 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8061 out_char(ScreenLines2[off]);
8062#endif
8063 }
8064
8065 screen_cur_col++;
8066}
8067
8068#ifdef FEAT_MBYTE
8069
8070/*
8071 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8072 * on the screen at position 'row' and 'col'.
8073 * The attributes of the first byte is used for all. This is required to
8074 * output the two bytes of a double-byte character with nothing in between.
8075 */
8076 static void
8077screen_char_2(off, row, col)
8078 unsigned off;
8079 int row;
8080 int col;
8081{
8082 /* Check for illegal values (could be wrong when screen was resized). */
8083 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8084 return;
8085
8086 /* Outputting the last character on the screen may scrollup the screen.
8087 * Don't to it! Mark the character invalid (update it when scrolled up) */
8088 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8089 {
8090 ScreenAttrs[off] = (sattr_T)-1;
8091 return;
8092 }
8093
8094 /* Output the first byte normally (positions the cursor), then write the
8095 * second byte directly. */
8096 screen_char(off, row, col);
8097 out_char(ScreenLines[off + 1]);
8098 ++screen_cur_col;
8099}
8100#endif
8101
8102#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
8103/*
8104 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8105 * This uses the contents of ScreenLines[] and doesn't change it.
8106 */
8107 void
8108screen_draw_rectangle(row, col, height, width, invert)
8109 int row;
8110 int col;
8111 int height;
8112 int width;
8113 int invert;
8114{
8115 int r, c;
8116 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008117#ifdef FEAT_MBYTE
8118 int max_off;
8119#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008121 /* Can't use ScreenLines unless initialized */
8122 if (ScreenLines == NULL)
8123 return;
8124
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125 if (invert)
8126 screen_char_attr = HL_INVERSE;
8127 for (r = row; r < row + height; ++r)
8128 {
8129 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008130#ifdef FEAT_MBYTE
8131 max_off = off + screen_Columns;
8132#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133 for (c = col; c < col + width; ++c)
8134 {
8135#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008136 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 {
8138 screen_char_2(off + c, r, c);
8139 ++c;
8140 }
8141 else
8142#endif
8143 {
8144 screen_char(off + c, r, c);
8145#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008146 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 ++c;
8148#endif
8149 }
8150 }
8151 }
8152 screen_char_attr = 0;
8153}
8154#endif
8155
8156#ifdef FEAT_VERTSPLIT
8157/*
8158 * Redraw the characters for a vertically split window.
8159 */
8160 static void
8161redraw_block(row, end, wp)
8162 int row;
8163 int end;
8164 win_T *wp;
8165{
8166 int col;
8167 int width;
8168
8169# ifdef FEAT_CLIPBOARD
8170 clip_may_clear_selection(row, end - 1);
8171# endif
8172
8173 if (wp == NULL)
8174 {
8175 col = 0;
8176 width = Columns;
8177 }
8178 else
8179 {
8180 col = wp->w_wincol;
8181 width = wp->w_width;
8182 }
8183 screen_draw_rectangle(row, col, end - row, width, FALSE);
8184}
8185#endif
8186
8187/*
8188 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8189 * with character 'c1' in first column followed by 'c2' in the other columns.
8190 * Use attributes 'attr'.
8191 */
8192 void
8193screen_fill(start_row, end_row, start_col, end_col, c1, c2, attr)
8194 int start_row, end_row;
8195 int start_col, end_col;
8196 int c1, c2;
8197 int attr;
8198{
8199 int row;
8200 int col;
8201 int off;
8202 int end_off;
8203 int did_delete;
8204 int c;
8205 int norm_term;
8206#if defined(FEAT_GUI) || defined(UNIX)
8207 int force_next = FALSE;
8208#endif
8209
8210 if (end_row > screen_Rows) /* safety check */
8211 end_row = screen_Rows;
8212 if (end_col > screen_Columns) /* safety check */
8213 end_col = screen_Columns;
8214 if (ScreenLines == NULL
8215 || start_row >= end_row
8216 || start_col >= end_col) /* nothing to do */
8217 return;
8218
8219 /* it's a "normal" terminal when not in a GUI or cterm */
8220 norm_term = (
8221#ifdef FEAT_GUI
8222 !gui.in_use &&
8223#endif
8224 t_colors <= 1);
8225 for (row = start_row; row < end_row; ++row)
8226 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008227#ifdef FEAT_MBYTE
8228 if (has_mbyte
8229# ifdef FEAT_GUI
8230 && !gui.in_use
8231# endif
8232 )
8233 {
8234 /* When drawing over the right halve of a double-wide char clear
8235 * out the left halve. When drawing over the left halve of a
8236 * double wide-char clear out the right halve. Only needed in a
8237 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008238 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008239 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008240 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008241 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008242 }
8243#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244 /*
8245 * Try to use delete-line termcap code, when no attributes or in a
8246 * "normal" terminal, where a bold/italic space is just a
8247 * space.
8248 */
8249 did_delete = FALSE;
8250 if (c2 == ' '
8251 && end_col == Columns
8252 && can_clear(T_CE)
8253 && (attr == 0
8254 || (norm_term
8255 && attr <= HL_ALL
8256 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8257 {
8258 /*
8259 * check if we really need to clear something
8260 */
8261 col = start_col;
8262 if (c1 != ' ') /* don't clear first char */
8263 ++col;
8264
8265 off = LineOffset[row] + col;
8266 end_off = LineOffset[row] + end_col;
8267
8268 /* skip blanks (used often, keep it fast!) */
8269#ifdef FEAT_MBYTE
8270 if (enc_utf8)
8271 while (off < end_off && ScreenLines[off] == ' '
8272 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8273 ++off;
8274 else
8275#endif
8276 while (off < end_off && ScreenLines[off] == ' '
8277 && ScreenAttrs[off] == 0)
8278 ++off;
8279 if (off < end_off) /* something to be cleared */
8280 {
8281 col = off - LineOffset[row];
8282 screen_stop_highlight();
8283 term_windgoto(row, col);/* clear rest of this screen line */
8284 out_str(T_CE);
8285 screen_start(); /* don't know where cursor is now */
8286 col = end_col - col;
8287 while (col--) /* clear chars in ScreenLines */
8288 {
8289 ScreenLines[off] = ' ';
8290#ifdef FEAT_MBYTE
8291 if (enc_utf8)
8292 ScreenLinesUC[off] = 0;
8293#endif
8294 ScreenAttrs[off] = 0;
8295 ++off;
8296 }
8297 }
8298 did_delete = TRUE; /* the chars are cleared now */
8299 }
8300
8301 off = LineOffset[row] + start_col;
8302 c = c1;
8303 for (col = start_col; col < end_col; ++col)
8304 {
8305 if (ScreenLines[off] != c
8306#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008307 || (enc_utf8 && (int)ScreenLinesUC[off]
8308 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309#endif
8310 || ScreenAttrs[off] != attr
8311#if defined(FEAT_GUI) || defined(UNIX)
8312 || force_next
8313#endif
8314 )
8315 {
8316#if defined(FEAT_GUI) || defined(UNIX)
8317 /* The bold trick may make a single row of pixels appear in
8318 * the next character. When a bold character is removed, the
8319 * next character should be redrawn too. This happens for our
8320 * own GUI and for some xterms. */
8321 if (
8322# ifdef FEAT_GUI
8323 gui.in_use
8324# endif
8325# if defined(FEAT_GUI) && defined(UNIX)
8326 ||
8327# endif
8328# ifdef UNIX
8329 term_is_xterm
8330# endif
8331 )
8332 {
8333 if (ScreenLines[off] != ' '
8334 && (ScreenAttrs[off] > HL_ALL
8335 || ScreenAttrs[off] & HL_BOLD))
8336 force_next = TRUE;
8337 else
8338 force_next = FALSE;
8339 }
8340#endif
8341 ScreenLines[off] = c;
8342#ifdef FEAT_MBYTE
8343 if (enc_utf8)
8344 {
8345 if (c >= 0x80)
8346 {
8347 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008348 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349 }
8350 else
8351 ScreenLinesUC[off] = 0;
8352 }
8353#endif
8354 ScreenAttrs[off] = attr;
8355 if (!did_delete || c != ' ')
8356 screen_char(off, row, col);
8357 }
8358 ++off;
8359 if (col == start_col)
8360 {
8361 if (did_delete)
8362 break;
8363 c = c2;
8364 }
8365 }
8366 if (end_col == Columns)
8367 LineWraps[row] = FALSE;
8368 if (row == Rows - 1) /* overwritten the command line */
8369 {
8370 redraw_cmdline = TRUE;
8371 if (c1 == ' ' && c2 == ' ')
8372 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008373 if (start_col == 0)
8374 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375 }
8376 }
8377}
8378
8379/*
8380 * Check if there should be a delay. Used before clearing or redrawing the
8381 * screen or the command line.
8382 */
8383 void
8384check_for_delay(check_msg_scroll)
8385 int check_msg_scroll;
8386{
8387 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8388 && !did_wait_return
8389 && emsg_silent == 0)
8390 {
8391 out_flush();
8392 ui_delay(1000L, TRUE);
8393 emsg_on_display = FALSE;
8394 if (check_msg_scroll)
8395 msg_scroll = FALSE;
8396 }
8397}
8398
8399/*
8400 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008401 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402 * Returns TRUE if there is a valid screen to write to.
8403 * Returns FALSE when starting up and screen not initialized yet.
8404 */
8405 int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008406screen_valid(doclear)
8407 int doclear;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008409 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410 return (ScreenLines != NULL);
8411}
8412
8413/*
8414 * Resize the shell to Rows and Columns.
8415 * Allocate ScreenLines[] and associated items.
8416 *
8417 * There may be some time between setting Rows and Columns and (re)allocating
8418 * ScreenLines[]. This happens when starting up and when (manually) changing
8419 * the shell size. Always use screen_Rows and screen_Columns to access items
8420 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8421 * final size of the shell is needed.
8422 */
8423 void
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008424screenalloc(doclear)
8425 int doclear;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426{
8427 int new_row, old_row;
8428#ifdef FEAT_GUI
8429 int old_Rows;
8430#endif
8431 win_T *wp;
8432 int outofmem = FALSE;
8433 int len;
8434 schar_T *new_ScreenLines;
8435#ifdef FEAT_MBYTE
8436 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008437 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008439 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440#endif
8441 sattr_T *new_ScreenAttrs;
8442 unsigned *new_LineOffset;
8443 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008444#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008445 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008446 tabpage_T *tp;
8447#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008449 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008450#ifdef FEAT_AUTOCMD
8451 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008453retry:
8454#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455 /*
8456 * Allocation of the screen buffers is done only when the size changes and
8457 * when Rows and Columns have been set and we have started doing full
8458 * screen stuff.
8459 */
8460 if ((ScreenLines != NULL
8461 && Rows == screen_Rows
8462 && Columns == screen_Columns
8463#ifdef FEAT_MBYTE
8464 && enc_utf8 == (ScreenLinesUC != NULL)
8465 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008466 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467#endif
8468 )
8469 || Rows == 0
8470 || Columns == 0
8471 || (!full_screen && ScreenLines == NULL))
8472 return;
8473
8474 /*
8475 * It's possible that we produce an out-of-memory message below, which
8476 * will cause this function to be called again. To break the loop, just
8477 * return here.
8478 */
8479 if (entered)
8480 return;
8481 entered = TRUE;
8482
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008483 /*
8484 * Note that the window sizes are updated before reallocating the arrays,
8485 * thus we must not redraw here!
8486 */
8487 ++RedrawingDisabled;
8488
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489 win_new_shellsize(); /* fit the windows in the new sized shell */
8490
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491 comp_col(); /* recompute columns for shown command and ruler */
8492
8493 /*
8494 * We're changing the size of the screen.
8495 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8496 * - Move lines from the old arrays into the new arrays, clear extra
8497 * lines (unless the screen is going to be cleared).
8498 * - Free the old arrays.
8499 *
8500 * If anything fails, make ScreenLines NULL, so we don't do anything!
8501 * Continuing with the old ScreenLines may result in a crash, because the
8502 * size is wrong.
8503 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008504 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008506#ifdef FEAT_AUTOCMD
8507 if (aucmd_win != NULL)
8508 win_free_lsize(aucmd_win);
8509#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510
8511 new_ScreenLines = (schar_T *)lalloc((long_u)(
8512 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8513#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008514 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515 if (enc_utf8)
8516 {
8517 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8518 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008519 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008520 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8522 }
8523 if (enc_dbcs == DBCS_JPNU)
8524 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8525 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8526#endif
8527 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8528 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8529 new_LineOffset = (unsigned *)lalloc((long_u)(
8530 Rows * sizeof(unsigned)), FALSE);
8531 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008532#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008533 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008534#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008536 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537 {
8538 if (win_alloc_lines(wp) == FAIL)
8539 {
8540 outofmem = TRUE;
8541#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008542 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543#endif
8544 }
8545 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008546#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008547 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8548 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008549 outofmem = TRUE;
8550#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008551#ifdef FEAT_WINDOWS
8552give_up:
8553#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008555#ifdef FEAT_MBYTE
8556 for (i = 0; i < p_mco; ++i)
8557 if (new_ScreenLinesC[i] == NULL)
8558 break;
8559#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560 if (new_ScreenLines == NULL
8561#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008562 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8564#endif
8565 || new_ScreenAttrs == NULL
8566 || new_LineOffset == NULL
8567 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008568#ifdef FEAT_WINDOWS
8569 || new_TabPageIdxs == NULL
8570#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571 || outofmem)
8572 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008573 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008574 {
8575 /* guess the size */
8576 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8577
8578 /* Remember we did this to avoid getting outofmem messages over
8579 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008580 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582 vim_free(new_ScreenLines);
8583 new_ScreenLines = NULL;
8584#ifdef FEAT_MBYTE
8585 vim_free(new_ScreenLinesUC);
8586 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008587 for (i = 0; i < p_mco; ++i)
8588 {
8589 vim_free(new_ScreenLinesC[i]);
8590 new_ScreenLinesC[i] = NULL;
8591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592 vim_free(new_ScreenLines2);
8593 new_ScreenLines2 = NULL;
8594#endif
8595 vim_free(new_ScreenAttrs);
8596 new_ScreenAttrs = NULL;
8597 vim_free(new_LineOffset);
8598 new_LineOffset = NULL;
8599 vim_free(new_LineWraps);
8600 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008601#ifdef FEAT_WINDOWS
8602 vim_free(new_TabPageIdxs);
8603 new_TabPageIdxs = NULL;
8604#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605 }
8606 else
8607 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008608 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008609
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610 for (new_row = 0; new_row < Rows; ++new_row)
8611 {
8612 new_LineOffset[new_row] = new_row * Columns;
8613 new_LineWraps[new_row] = FALSE;
8614
8615 /*
8616 * If the screen is not going to be cleared, copy as much as
8617 * possible from the old screen to the new one and clear the rest
8618 * (used when resizing the window at the "--more--" prompt or when
8619 * executing an external command, for the GUI).
8620 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008621 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622 {
8623 (void)vim_memset(new_ScreenLines + new_row * Columns,
8624 ' ', (size_t)Columns * sizeof(schar_T));
8625#ifdef FEAT_MBYTE
8626 if (enc_utf8)
8627 {
8628 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8629 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008630 for (i = 0; i < p_mco; ++i)
8631 (void)vim_memset(new_ScreenLinesC[i]
8632 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633 0, (size_t)Columns * sizeof(u8char_T));
8634 }
8635 if (enc_dbcs == DBCS_JPNU)
8636 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8637 0, (size_t)Columns * sizeof(schar_T));
8638#endif
8639 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8640 0, (size_t)Columns * sizeof(sattr_T));
8641 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008642 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643 {
8644 if (screen_Columns < Columns)
8645 len = screen_Columns;
8646 else
8647 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008648#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008649 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008650 * may be invalid now. Also when p_mco changes. */
8651 if (!(enc_utf8 && ScreenLinesUC == NULL)
8652 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008653#endif
8654 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8655 ScreenLines + LineOffset[old_row],
8656 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008657#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008658 if (enc_utf8 && ScreenLinesUC != NULL
8659 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660 {
8661 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8662 ScreenLinesUC + LineOffset[old_row],
8663 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008664 for (i = 0; i < p_mco; ++i)
8665 mch_memmove(new_ScreenLinesC[i]
8666 + new_LineOffset[new_row],
8667 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668 (size_t)len * sizeof(u8char_T));
8669 }
8670 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8671 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8672 ScreenLines2 + LineOffset[old_row],
8673 (size_t)len * sizeof(schar_T));
8674#endif
8675 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8676 ScreenAttrs + LineOffset[old_row],
8677 (size_t)len * sizeof(sattr_T));
8678 }
8679 }
8680 }
8681 /* Use the last line of the screen for the current line. */
8682 current_ScreenLine = new_ScreenLines + Rows * Columns;
8683 }
8684
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008685 free_screenlines();
8686
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687 ScreenLines = new_ScreenLines;
8688#ifdef FEAT_MBYTE
8689 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008690 for (i = 0; i < p_mco; ++i)
8691 ScreenLinesC[i] = new_ScreenLinesC[i];
8692 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693 ScreenLines2 = new_ScreenLines2;
8694#endif
8695 ScreenAttrs = new_ScreenAttrs;
8696 LineOffset = new_LineOffset;
8697 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008698#ifdef FEAT_WINDOWS
8699 TabPageIdxs = new_TabPageIdxs;
8700#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701
8702 /* It's important that screen_Rows and screen_Columns reflect the actual
8703 * size of ScreenLines[]. Set them before calling anything. */
8704#ifdef FEAT_GUI
8705 old_Rows = screen_Rows;
8706#endif
8707 screen_Rows = Rows;
8708 screen_Columns = Columns;
8709
8710 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008711 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712 screenclear2();
8713
8714#ifdef FEAT_GUI
8715 else if (gui.in_use
8716 && !gui.starting
8717 && ScreenLines != NULL
8718 && old_Rows != Rows)
8719 {
8720 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8721 /*
8722 * Adjust the position of the cursor, for when executing an external
8723 * command.
8724 */
8725 if (msg_row >= Rows) /* Rows got smaller */
8726 msg_row = Rows - 1; /* put cursor at last row */
8727 else if (Rows > old_Rows) /* Rows got bigger */
8728 msg_row += Rows - old_Rows; /* put cursor in same place */
8729 if (msg_col >= Columns) /* Columns got smaller */
8730 msg_col = Columns - 1; /* put cursor at last column */
8731 }
8732#endif
8733
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008735 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008736
8737#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008738 /*
8739 * Do not apply autocommands more than 3 times to avoid an endless loop
8740 * in case applying autocommands always changes Rows or Columns.
8741 */
8742 if (starting == 0 && ++retry_count <= 3)
8743 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008744 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008745 /* In rare cases, autocommands may have altered Rows or Columns,
8746 * jump back to check if we need to allocate the screen again. */
8747 goto retry;
8748 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008749#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750}
8751
8752 void
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008753free_screenlines()
8754{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008755#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008756 int i;
8757
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008758 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008759 for (i = 0; i < Screen_mco; ++i)
8760 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008761 vim_free(ScreenLines2);
8762#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008763 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008764 vim_free(ScreenAttrs);
8765 vim_free(LineOffset);
8766 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008767#ifdef FEAT_WINDOWS
8768 vim_free(TabPageIdxs);
8769#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008770}
8771
8772 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773screenclear()
8774{
8775 check_for_delay(FALSE);
8776 screenalloc(FALSE); /* allocate screen buffers if size changed */
8777 screenclear2(); /* clear the screen */
8778}
8779
8780 static void
8781screenclear2()
8782{
8783 int i;
8784
8785 if (starting == NO_SCREEN || ScreenLines == NULL
8786#ifdef FEAT_GUI
8787 || (gui.in_use && gui.starting)
8788#endif
8789 )
8790 return;
8791
8792#ifdef FEAT_GUI
8793 if (!gui.in_use)
8794#endif
8795 screen_attr = -1; /* force setting the Normal colors */
8796 screen_stop_highlight(); /* don't want highlighting here */
8797
8798#ifdef FEAT_CLIPBOARD
8799 /* disable selection without redrawing it */
8800 clip_scroll_selection(9999);
8801#endif
8802
8803 /* blank out ScreenLines */
8804 for (i = 0; i < Rows; ++i)
8805 {
8806 lineclear(LineOffset[i], (int)Columns);
8807 LineWraps[i] = FALSE;
8808 }
8809
8810 if (can_clear(T_CL))
8811 {
8812 out_str(T_CL); /* clear the display */
8813 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008814 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815 }
8816 else
8817 {
8818 /* can't clear the screen, mark all chars with invalid attributes */
8819 for (i = 0; i < Rows; ++i)
8820 lineinvalid(LineOffset[i], (int)Columns);
8821 clear_cmdline = TRUE;
8822 }
8823
8824 screen_cleared = TRUE; /* can use contents of ScreenLines now */
8825
8826 win_rest_invalid(firstwin);
8827 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008828#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008829 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008830#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831 if (must_redraw == CLEAR) /* no need to clear again */
8832 must_redraw = NOT_VALID;
8833 compute_cmdrow();
8834 msg_row = cmdline_row; /* put cursor on last line for messages */
8835 msg_col = 0;
8836 screen_start(); /* don't know where cursor is now */
8837 msg_scrolled = 0; /* can't scroll back */
8838 msg_didany = FALSE;
8839 msg_didout = FALSE;
8840}
8841
8842/*
8843 * Clear one line in ScreenLines.
8844 */
8845 static void
8846lineclear(off, width)
8847 unsigned off;
8848 int width;
8849{
8850 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
8851#ifdef FEAT_MBYTE
8852 if (enc_utf8)
8853 (void)vim_memset(ScreenLinesUC + off, 0,
8854 (size_t)width * sizeof(u8char_T));
8855#endif
8856 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
8857}
8858
8859/*
8860 * Mark one line in ScreenLines invalid by setting the attributes to an
8861 * invalid value.
8862 */
8863 static void
8864lineinvalid(off, width)
8865 unsigned off;
8866 int width;
8867{
8868 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
8869}
8870
8871#ifdef FEAT_VERTSPLIT
8872/*
8873 * Copy part of a Screenline for vertically split window "wp".
8874 */
8875 static void
8876linecopy(to, from, wp)
8877 int to;
8878 int from;
8879 win_T *wp;
8880{
8881 unsigned off_to = LineOffset[to] + wp->w_wincol;
8882 unsigned off_from = LineOffset[from] + wp->w_wincol;
8883
8884 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
8885 wp->w_width * sizeof(schar_T));
8886# ifdef FEAT_MBYTE
8887 if (enc_utf8)
8888 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008889 int i;
8890
Bram Moolenaar071d4272004-06-13 20:20:40 +00008891 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
8892 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008893 for (i = 0; i < p_mco; ++i)
8894 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
8895 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008896 }
8897 if (enc_dbcs == DBCS_JPNU)
8898 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
8899 wp->w_width * sizeof(schar_T));
8900# endif
8901 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
8902 wp->w_width * sizeof(sattr_T));
8903}
8904#endif
8905
8906/*
8907 * Return TRUE if clearing with term string "p" would work.
8908 * It can't work when the string is empty or it won't set the right background.
8909 */
8910 int
8911can_clear(p)
8912 char_u *p;
8913{
8914 return (*p != NUL && (t_colors <= 1
8915#ifdef FEAT_GUI
8916 || gui.in_use
8917#endif
8918 || cterm_normal_bg_color == 0 || *T_UT != NUL));
8919}
8920
8921/*
8922 * Reset cursor position. Use whenever cursor was moved because of outputting
8923 * something directly to the screen (shell commands) or a terminal control
8924 * code.
8925 */
8926 void
8927screen_start()
8928{
8929 screen_cur_row = screen_cur_col = 9999;
8930}
8931
8932/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933 * Move the cursor to position "row","col" in the screen.
8934 * This tries to find the most efficient way to move, minimizing the number of
8935 * characters sent to the terminal.
8936 */
8937 void
8938windgoto(row, col)
8939 int row;
8940 int col;
8941{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008942 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943 int i;
8944 int plan;
8945 int cost;
8946 int wouldbe_col;
8947 int noinvcurs;
8948 char_u *bs;
8949 int goto_cost;
8950 int attr;
8951
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008952#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
8954
8955#define PLAN_LE 1
8956#define PLAN_CR 2
8957#define PLAN_NL 3
8958#define PLAN_WRITE 4
8959 /* Can't use ScreenLines unless initialized */
8960 if (ScreenLines == NULL)
8961 return;
8962
8963 if (col != screen_cur_col || row != screen_cur_row)
8964 {
8965 /* Check for valid position. */
8966 if (row < 0) /* window without text lines? */
8967 row = 0;
8968 if (row >= screen_Rows)
8969 row = screen_Rows - 1;
8970 if (col >= screen_Columns)
8971 col = screen_Columns - 1;
8972
8973 /* check if no cursor movement is allowed in highlight mode */
8974 if (screen_attr && *T_MS == NUL)
8975 noinvcurs = HIGHL_COST;
8976 else
8977 noinvcurs = 0;
8978 goto_cost = GOTO_COST + noinvcurs;
8979
8980 /*
8981 * Plan how to do the positioning:
8982 * 1. Use CR to move it to column 0, same row.
8983 * 2. Use T_LE to move it a few columns to the left.
8984 * 3. Use NL to move a few lines down, column 0.
8985 * 4. Move a few columns to the right with T_ND or by writing chars.
8986 *
8987 * Don't do this if the cursor went beyond the last column, the cursor
8988 * position is unknown then (some terminals wrap, some don't )
8989 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008990 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00008991 * characters to move the cursor to the right.
8992 */
8993 if (row >= screen_cur_row && screen_cur_col < Columns)
8994 {
8995 /*
8996 * If the cursor is in the same row, bigger col, we can use CR
8997 * or T_LE.
8998 */
8999 bs = NULL; /* init for GCC */
9000 attr = screen_attr;
9001 if (row == screen_cur_row && col < screen_cur_col)
9002 {
9003 /* "le" is preferred over "bc", because "bc" is obsolete */
9004 if (*T_LE)
9005 bs = T_LE; /* "cursor left" */
9006 else
9007 bs = T_BC; /* "backspace character (old) */
9008 if (*bs)
9009 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9010 else
9011 cost = 999;
9012 if (col + 1 < cost) /* using CR is less characters */
9013 {
9014 plan = PLAN_CR;
9015 wouldbe_col = 0;
9016 cost = 1; /* CR is just one character */
9017 }
9018 else
9019 {
9020 plan = PLAN_LE;
9021 wouldbe_col = col;
9022 }
9023 if (noinvcurs) /* will stop highlighting */
9024 {
9025 cost += noinvcurs;
9026 attr = 0;
9027 }
9028 }
9029
9030 /*
9031 * If the cursor is above where we want to be, we can use CR LF.
9032 */
9033 else if (row > screen_cur_row)
9034 {
9035 plan = PLAN_NL;
9036 wouldbe_col = 0;
9037 cost = (row - screen_cur_row) * 2; /* CR LF */
9038 if (noinvcurs) /* will stop highlighting */
9039 {
9040 cost += noinvcurs;
9041 attr = 0;
9042 }
9043 }
9044
9045 /*
9046 * If the cursor is in the same row, smaller col, just use write.
9047 */
9048 else
9049 {
9050 plan = PLAN_WRITE;
9051 wouldbe_col = screen_cur_col;
9052 cost = 0;
9053 }
9054
9055 /*
9056 * Check if any characters that need to be written have the
9057 * correct attributes. Also avoid UTF-8 characters.
9058 */
9059 i = col - wouldbe_col;
9060 if (i > 0)
9061 cost += i;
9062 if (cost < goto_cost && i > 0)
9063 {
9064 /*
9065 * Check if the attributes are correct without additionally
9066 * stopping highlighting.
9067 */
9068 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9069 while (i && *p++ == attr)
9070 --i;
9071 if (i != 0)
9072 {
9073 /*
9074 * Try if it works when highlighting is stopped here.
9075 */
9076 if (*--p == 0)
9077 {
9078 cost += noinvcurs;
9079 while (i && *p++ == 0)
9080 --i;
9081 }
9082 if (i != 0)
9083 cost = 999; /* different attributes, don't do it */
9084 }
9085#ifdef FEAT_MBYTE
9086 if (enc_utf8)
9087 {
9088 /* Don't use an UTF-8 char for positioning, it's slow. */
9089 for (i = wouldbe_col; i < col; ++i)
9090 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9091 {
9092 cost = 999;
9093 break;
9094 }
9095 }
9096#endif
9097 }
9098
9099 /*
9100 * We can do it without term_windgoto()!
9101 */
9102 if (cost < goto_cost)
9103 {
9104 if (plan == PLAN_LE)
9105 {
9106 if (noinvcurs)
9107 screen_stop_highlight();
9108 while (screen_cur_col > col)
9109 {
9110 out_str(bs);
9111 --screen_cur_col;
9112 }
9113 }
9114 else if (plan == PLAN_CR)
9115 {
9116 if (noinvcurs)
9117 screen_stop_highlight();
9118 out_char('\r');
9119 screen_cur_col = 0;
9120 }
9121 else if (plan == PLAN_NL)
9122 {
9123 if (noinvcurs)
9124 screen_stop_highlight();
9125 while (screen_cur_row < row)
9126 {
9127 out_char('\n');
9128 ++screen_cur_row;
9129 }
9130 screen_cur_col = 0;
9131 }
9132
9133 i = col - screen_cur_col;
9134 if (i > 0)
9135 {
9136 /*
9137 * Use cursor-right if it's one character only. Avoids
9138 * removing a line of pixels from the last bold char, when
9139 * using the bold trick in the GUI.
9140 */
9141 if (T_ND[0] != NUL && T_ND[1] == NUL)
9142 {
9143 while (i-- > 0)
9144 out_char(*T_ND);
9145 }
9146 else
9147 {
9148 int off;
9149
9150 off = LineOffset[row] + screen_cur_col;
9151 while (i-- > 0)
9152 {
9153 if (ScreenAttrs[off] != screen_attr)
9154 screen_stop_highlight();
9155#ifdef FEAT_MBYTE
9156 out_flush_check();
9157#endif
9158 out_char(ScreenLines[off]);
9159#ifdef FEAT_MBYTE
9160 if (enc_dbcs == DBCS_JPNU
9161 && ScreenLines[off] == 0x8e)
9162 out_char(ScreenLines2[off]);
9163#endif
9164 ++off;
9165 }
9166 }
9167 }
9168 }
9169 }
9170 else
9171 cost = 999;
9172
9173 if (cost >= goto_cost)
9174 {
9175 if (noinvcurs)
9176 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009177 if (row == screen_cur_row && (col > screen_cur_col)
9178 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179 term_cursor_right(col - screen_cur_col);
9180 else
9181 term_windgoto(row, col);
9182 }
9183 screen_cur_row = row;
9184 screen_cur_col = col;
9185 }
9186}
9187
9188/*
9189 * Set cursor to its position in the current window.
9190 */
9191 void
9192setcursor()
9193{
9194 if (redrawing())
9195 {
9196 validate_cursor();
9197 windgoto(W_WINROW(curwin) + curwin->w_wrow,
9198 W_WINCOL(curwin) + (
9199#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009200 /* With 'rightleft' set and the cursor on a double-wide
9201 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
9203# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009204 (has_mbyte
9205 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9206 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009207# endif
9208 1)) :
9209#endif
9210 curwin->w_wcol));
9211 }
9212}
9213
9214
9215/*
9216 * insert 'line_count' lines at 'row' in window 'wp'
9217 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9218 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
9219 * scrolling.
9220 * Returns FAIL if the lines are not inserted, OK for success.
9221 */
9222 int
9223win_ins_lines(wp, row, line_count, invalid, mayclear)
9224 win_T *wp;
9225 int row;
9226 int line_count;
9227 int invalid;
9228 int mayclear;
9229{
9230 int did_delete;
9231 int nextrow;
9232 int lastrow;
9233 int retval;
9234
9235 if (invalid)
9236 wp->w_lines_valid = 0;
9237
9238 if (wp->w_height < 5)
9239 return FAIL;
9240
9241 if (line_count > wp->w_height - row)
9242 line_count = wp->w_height - row;
9243
9244 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
9245 if (retval != MAYBE)
9246 return retval;
9247
9248 /*
9249 * If there is a next window or a status line, we first try to delete the
9250 * lines at the bottom to avoid messing what is after the window.
9251 * If this fails and there are following windows, don't do anything to avoid
9252 * messing up those windows, better just redraw.
9253 */
9254 did_delete = FALSE;
9255#ifdef FEAT_WINDOWS
9256 if (wp->w_next != NULL || wp->w_status_height)
9257 {
9258 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9259 line_count, (int)Rows, FALSE, NULL) == OK)
9260 did_delete = TRUE;
9261 else if (wp->w_next)
9262 return FAIL;
9263 }
9264#endif
9265 /*
9266 * if no lines deleted, blank the lines that will end up below the window
9267 */
9268 if (!did_delete)
9269 {
9270#ifdef FEAT_WINDOWS
9271 wp->w_redr_status = TRUE;
9272#endif
9273 redraw_cmdline = TRUE;
9274 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
9275 lastrow = nextrow + line_count;
9276 if (lastrow > Rows)
9277 lastrow = Rows;
9278 screen_fill(nextrow - line_count, lastrow - line_count,
9279 W_WINCOL(wp), (int)W_ENDCOL(wp),
9280 ' ', ' ', 0);
9281 }
9282
9283 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
9284 == FAIL)
9285 {
9286 /* deletion will have messed up other windows */
9287 if (did_delete)
9288 {
9289#ifdef FEAT_WINDOWS
9290 wp->w_redr_status = TRUE;
9291#endif
9292 win_rest_invalid(W_NEXT(wp));
9293 }
9294 return FAIL;
9295 }
9296
9297 return OK;
9298}
9299
9300/*
9301 * delete "line_count" window lines at "row" in window "wp"
9302 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9303 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9304 * scrolling
9305 * Return OK for success, FAIL if the lines are not deleted.
9306 */
9307 int
9308win_del_lines(wp, row, line_count, invalid, mayclear)
9309 win_T *wp;
9310 int row;
9311 int line_count;
9312 int invalid;
9313 int mayclear;
9314{
9315 int retval;
9316
9317 if (invalid)
9318 wp->w_lines_valid = 0;
9319
9320 if (line_count > wp->w_height - row)
9321 line_count = wp->w_height - row;
9322
9323 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
9324 if (retval != MAYBE)
9325 return retval;
9326
9327 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
9328 (int)Rows, FALSE, NULL) == FAIL)
9329 return FAIL;
9330
9331#ifdef FEAT_WINDOWS
9332 /*
9333 * If there are windows or status lines below, try to put them at the
9334 * correct place. If we can't do that, they have to be redrawn.
9335 */
9336 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9337 {
9338 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9339 line_count, (int)Rows, NULL) == FAIL)
9340 {
9341 wp->w_redr_status = TRUE;
9342 win_rest_invalid(wp->w_next);
9343 }
9344 }
9345 /*
9346 * If this is the last window and there is no status line, redraw the
9347 * command line later.
9348 */
9349 else
9350#endif
9351 redraw_cmdline = TRUE;
9352 return OK;
9353}
9354
9355/*
9356 * Common code for win_ins_lines() and win_del_lines().
9357 * Returns OK or FAIL when the work has been done.
9358 * Returns MAYBE when not finished yet.
9359 */
9360 static int
9361win_do_lines(wp, row, line_count, mayclear, del)
9362 win_T *wp;
9363 int row;
9364 int line_count;
9365 int mayclear;
9366 int del;
9367{
9368 int retval;
9369
9370 if (!redrawing() || line_count <= 0)
9371 return FAIL;
9372
9373 /* only a few lines left: redraw is faster */
9374 if (mayclear && Rows - line_count < 5
9375#ifdef FEAT_VERTSPLIT
9376 && wp->w_width == Columns
9377#endif
9378 )
9379 {
9380 screenclear(); /* will set wp->w_lines_valid to 0 */
9381 return FAIL;
9382 }
9383
9384 /*
9385 * Delete all remaining lines
9386 */
9387 if (row + line_count >= wp->w_height)
9388 {
9389 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
9390 W_WINCOL(wp), (int)W_ENDCOL(wp),
9391 ' ', ' ', 0);
9392 return OK;
9393 }
9394
9395 /*
9396 * when scrolling, the message on the command line should be cleared,
9397 * otherwise it will stay there forever.
9398 */
9399 clear_cmdline = TRUE;
9400
9401 /*
9402 * If the terminal can set a scroll region, use that.
9403 * Always do this in a vertically split window. This will redraw from
9404 * ScreenLines[] when t_CV isn't defined. That's faster than using
9405 * win_line().
9406 * Don't use a scroll region when we are going to redraw the text, writing
9407 * a character in the lower right corner of the scroll region causes a
9408 * scroll-up in the DJGPP version.
9409 */
9410 if (scroll_region
9411#ifdef FEAT_VERTSPLIT
9412 || W_WIDTH(wp) != Columns
9413#endif
9414 )
9415 {
9416#ifdef FEAT_VERTSPLIT
9417 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9418#endif
9419 scroll_region_set(wp, row);
9420 if (del)
9421 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
9422 wp->w_height - row, FALSE, wp);
9423 else
9424 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
9425 wp->w_height - row, wp);
9426#ifdef FEAT_VERTSPLIT
9427 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9428#endif
9429 scroll_region_reset();
9430 return retval;
9431 }
9432
9433#ifdef FEAT_WINDOWS
9434 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9435 return FAIL;
9436#endif
9437
9438 return MAYBE;
9439}
9440
9441/*
9442 * window 'wp' and everything after it is messed up, mark it for redraw
9443 */
9444 static void
9445win_rest_invalid(wp)
9446 win_T *wp;
9447{
9448#ifdef FEAT_WINDOWS
9449 while (wp != NULL)
9450#else
9451 if (wp != NULL)
9452#endif
9453 {
9454 redraw_win_later(wp, NOT_VALID);
9455#ifdef FEAT_WINDOWS
9456 wp->w_redr_status = TRUE;
9457 wp = wp->w_next;
9458#endif
9459 }
9460 redraw_cmdline = TRUE;
9461}
9462
9463/*
9464 * The rest of the routines in this file perform screen manipulations. The
9465 * given operation is performed physically on the screen. The corresponding
9466 * change is also made to the internal screen image. In this way, the editor
9467 * anticipates the effect of editing changes on the appearance of the screen.
9468 * That way, when we call screenupdate a complete redraw isn't usually
9469 * necessary. Another advantage is that we can keep adding code to anticipate
9470 * screen changes, and in the meantime, everything still works.
9471 */
9472
9473/*
9474 * types for inserting or deleting lines
9475 */
9476#define USE_T_CAL 1
9477#define USE_T_CDL 2
9478#define USE_T_AL 3
9479#define USE_T_CE 4
9480#define USE_T_DL 5
9481#define USE_T_SR 6
9482#define USE_NL 7
9483#define USE_T_CD 8
9484#define USE_REDRAW 9
9485
9486/*
9487 * insert lines on the screen and update ScreenLines[]
9488 * 'end' is the line after the scrolled part. Normally it is Rows.
9489 * When scrolling region used 'off' is the offset from the top for the region.
9490 * 'row' and 'end' are relative to the start of the region.
9491 *
9492 * return FAIL for failure, OK for success.
9493 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009494 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495screen_ins_lines(off, row, line_count, end, wp)
9496 int off;
9497 int row;
9498 int line_count;
9499 int end;
9500 win_T *wp; /* NULL or window to use width from */
9501{
9502 int i;
9503 int j;
9504 unsigned temp;
9505 int cursor_row;
9506 int type;
9507 int result_empty;
9508 int can_ce = can_clear(T_CE);
9509
9510 /*
9511 * FAIL if
9512 * - there is no valid screen
9513 * - the screen has to be redrawn completely
9514 * - the line count is less than one
9515 * - the line count is more than 'ttyscroll'
9516 */
9517 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
9518 return FAIL;
9519
9520 /*
9521 * There are seven ways to insert lines:
9522 * 0. When in a vertically split window and t_CV isn't set, redraw the
9523 * characters from ScreenLines[].
9524 * 1. Use T_CD (clear to end of display) if it exists and the result of
9525 * the insert is just empty lines
9526 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9527 * present or line_count > 1. It looks better if we do all the inserts
9528 * at once.
9529 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9530 * insert is just empty lines and T_CE is not present or line_count >
9531 * 1.
9532 * 4. Use T_AL (insert line) if it exists.
9533 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9534 * just empty lines.
9535 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9536 * just empty lines.
9537 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9538 * the 'da' flag is not set or we have clear line capability.
9539 * 8. redraw the characters from ScreenLines[].
9540 *
9541 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9542 * the scrollbar for the window. It does have insert line, use that if it
9543 * exists.
9544 */
9545 result_empty = (row + line_count >= end);
9546#ifdef FEAT_VERTSPLIT
9547 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9548 type = USE_REDRAW;
9549 else
9550#endif
9551 if (can_clear(T_CD) && result_empty)
9552 type = USE_T_CD;
9553 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9554 type = USE_T_CAL;
9555 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9556 type = USE_T_CDL;
9557 else if (*T_AL != NUL)
9558 type = USE_T_AL;
9559 else if (can_ce && result_empty)
9560 type = USE_T_CE;
9561 else if (*T_DL != NUL && result_empty)
9562 type = USE_T_DL;
9563 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9564 type = USE_T_SR;
9565 else
9566 return FAIL;
9567
9568 /*
9569 * For clearing the lines screen_del_lines() is used. This will also take
9570 * care of t_db if necessary.
9571 */
9572 if (type == USE_T_CD || type == USE_T_CDL ||
9573 type == USE_T_CE || type == USE_T_DL)
9574 return screen_del_lines(off, row, line_count, end, FALSE, wp);
9575
9576 /*
9577 * If text is retained below the screen, first clear or delete as many
9578 * lines at the bottom of the window as are about to be inserted so that
9579 * the deleted lines won't later surface during a screen_del_lines.
9580 */
9581 if (*T_DB)
9582 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
9583
9584#ifdef FEAT_CLIPBOARD
9585 /* Remove a modeless selection when inserting lines halfway the screen
9586 * or not the full width of the screen. */
9587 if (off + row > 0
9588# ifdef FEAT_VERTSPLIT
9589 || (wp != NULL && wp->w_width != Columns)
9590# endif
9591 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009592 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009593 else
9594 clip_scroll_selection(-line_count);
9595#endif
9596
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597#ifdef FEAT_GUI
9598 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9599 * scrolling is actually carried out. */
9600 gui_dont_update_cursor();
9601#endif
9602
9603 if (*T_CCS != NUL) /* cursor relative to region */
9604 cursor_row = row;
9605 else
9606 cursor_row = row + off;
9607
9608 /*
9609 * Shift LineOffset[] line_count down to reflect the inserted lines.
9610 * Clear the inserted lines in ScreenLines[].
9611 */
9612 row += off;
9613 end += off;
9614 for (i = 0; i < line_count; ++i)
9615 {
9616#ifdef FEAT_VERTSPLIT
9617 if (wp != NULL && wp->w_width != Columns)
9618 {
9619 /* need to copy part of a line */
9620 j = end - 1 - i;
9621 while ((j -= line_count) >= row)
9622 linecopy(j + line_count, j, wp);
9623 j += line_count;
9624 if (can_clear((char_u *)" "))
9625 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9626 else
9627 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9628 LineWraps[j] = FALSE;
9629 }
9630 else
9631#endif
9632 {
9633 j = end - 1 - i;
9634 temp = LineOffset[j];
9635 while ((j -= line_count) >= row)
9636 {
9637 LineOffset[j + line_count] = LineOffset[j];
9638 LineWraps[j + line_count] = LineWraps[j];
9639 }
9640 LineOffset[j + line_count] = temp;
9641 LineWraps[j + line_count] = FALSE;
9642 if (can_clear((char_u *)" "))
9643 lineclear(temp, (int)Columns);
9644 else
9645 lineinvalid(temp, (int)Columns);
9646 }
9647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009648
9649 screen_stop_highlight();
9650 windgoto(cursor_row, 0);
9651
9652#ifdef FEAT_VERTSPLIT
9653 /* redraw the characters */
9654 if (type == USE_REDRAW)
9655 redraw_block(row, end, wp);
9656 else
9657#endif
9658 if (type == USE_T_CAL)
9659 {
9660 term_append_lines(line_count);
9661 screen_start(); /* don't know where cursor is now */
9662 }
9663 else
9664 {
9665 for (i = 0; i < line_count; i++)
9666 {
9667 if (type == USE_T_AL)
9668 {
9669 if (i && cursor_row != 0)
9670 windgoto(cursor_row, 0);
9671 out_str(T_AL);
9672 }
9673 else /* type == USE_T_SR */
9674 out_str(T_SR);
9675 screen_start(); /* don't know where cursor is now */
9676 }
9677 }
9678
9679 /*
9680 * With scroll-reverse and 'da' flag set we need to clear the lines that
9681 * have been scrolled down into the region.
9682 */
9683 if (type == USE_T_SR && *T_DA)
9684 {
9685 for (i = 0; i < line_count; ++i)
9686 {
9687 windgoto(off + i, 0);
9688 out_str(T_CE);
9689 screen_start(); /* don't know where cursor is now */
9690 }
9691 }
9692
9693#ifdef FEAT_GUI
9694 gui_can_update_cursor();
9695 if (gui.in_use)
9696 out_flush(); /* always flush after a scroll */
9697#endif
9698 return OK;
9699}
9700
9701/*
9702 * delete lines on the screen and update ScreenLines[]
9703 * 'end' is the line after the scrolled part. Normally it is Rows.
9704 * When scrolling region used 'off' is the offset from the top for the region.
9705 * 'row' and 'end' are relative to the start of the region.
9706 *
9707 * Return OK for success, FAIL if the lines are not deleted.
9708 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009709 int
9710screen_del_lines(off, row, line_count, end, force, wp)
9711 int off;
9712 int row;
9713 int line_count;
9714 int end;
9715 int force; /* even when line_count > p_ttyscroll */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009716 win_T *wp UNUSED; /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009717{
9718 int j;
9719 int i;
9720 unsigned temp;
9721 int cursor_row;
9722 int cursor_end;
9723 int result_empty; /* result is empty until end of region */
9724 int can_delete; /* deleting line codes can be used */
9725 int type;
9726
9727 /*
9728 * FAIL if
9729 * - there is no valid screen
9730 * - the screen has to be redrawn completely
9731 * - the line count is less than one
9732 * - the line count is more than 'ttyscroll'
9733 */
9734 if (!screen_valid(TRUE) || line_count <= 0 ||
9735 (!force && line_count > p_ttyscroll))
9736 return FAIL;
9737
9738 /*
9739 * Check if the rest of the current region will become empty.
9740 */
9741 result_empty = row + line_count >= end;
9742
9743 /*
9744 * We can delete lines only when 'db' flag not set or when 'ce' option
9745 * available.
9746 */
9747 can_delete = (*T_DB == NUL || can_clear(T_CE));
9748
9749 /*
9750 * There are six ways to delete lines:
9751 * 0. When in a vertically split window and t_CV isn't set, redraw the
9752 * characters from ScreenLines[].
9753 * 1. Use T_CD if it exists and the result is empty.
9754 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9755 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9756 * none of the other ways work.
9757 * 4. Use T_CE (erase line) if the result is empty.
9758 * 5. Use T_DL (delete line) if it exists.
9759 * 6. redraw the characters from ScreenLines[].
9760 */
9761#ifdef FEAT_VERTSPLIT
9762 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9763 type = USE_REDRAW;
9764 else
9765#endif
9766 if (can_clear(T_CD) && result_empty)
9767 type = USE_T_CD;
9768#if defined(__BEOS__) && defined(BEOS_DR8)
9769 /*
9770 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9771 * its internal termcap... this works okay for tests which test *T_DB !=
9772 * NUL. It has the disadvantage that the user cannot use any :set t_*
9773 * command to get T_DB (back) to empty_option, only :set term=... will do
9774 * the trick...
9775 * Anyway, this hack will hopefully go away with the next OS release.
9776 * (Olaf Seibert)
9777 */
9778 else if (row == 0 && T_DB == empty_option
9779 && (line_count == 1 || *T_CDL == NUL))
9780#else
9781 else if (row == 0 && (
9782#ifndef AMIGA
9783 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9784 * up, so use delete-line command */
9785 line_count == 1 ||
9786#endif
9787 *T_CDL == NUL))
9788#endif
9789 type = USE_NL;
9790 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9791 type = USE_T_CDL;
9792 else if (can_clear(T_CE) && result_empty
9793#ifdef FEAT_VERTSPLIT
9794 && (wp == NULL || wp->w_width == Columns)
9795#endif
9796 )
9797 type = USE_T_CE;
9798 else if (*T_DL != NUL && can_delete)
9799 type = USE_T_DL;
9800 else if (*T_CDL != NUL && can_delete)
9801 type = USE_T_CDL;
9802 else
9803 return FAIL;
9804
9805#ifdef FEAT_CLIPBOARD
9806 /* Remove a modeless selection when deleting lines halfway the screen or
9807 * not the full width of the screen. */
9808 if (off + row > 0
9809# ifdef FEAT_VERTSPLIT
9810 || (wp != NULL && wp->w_width != Columns)
9811# endif
9812 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009813 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009814 else
9815 clip_scroll_selection(line_count);
9816#endif
9817
Bram Moolenaar071d4272004-06-13 20:20:40 +00009818#ifdef FEAT_GUI
9819 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9820 * scrolling is actually carried out. */
9821 gui_dont_update_cursor();
9822#endif
9823
9824 if (*T_CCS != NUL) /* cursor relative to region */
9825 {
9826 cursor_row = row;
9827 cursor_end = end;
9828 }
9829 else
9830 {
9831 cursor_row = row + off;
9832 cursor_end = end + off;
9833 }
9834
9835 /*
9836 * Now shift LineOffset[] line_count up to reflect the deleted lines.
9837 * Clear the inserted lines in ScreenLines[].
9838 */
9839 row += off;
9840 end += off;
9841 for (i = 0; i < line_count; ++i)
9842 {
9843#ifdef FEAT_VERTSPLIT
9844 if (wp != NULL && wp->w_width != Columns)
9845 {
9846 /* need to copy part of a line */
9847 j = row + i;
9848 while ((j += line_count) <= end - 1)
9849 linecopy(j - line_count, j, wp);
9850 j -= line_count;
9851 if (can_clear((char_u *)" "))
9852 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9853 else
9854 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9855 LineWraps[j] = FALSE;
9856 }
9857 else
9858#endif
9859 {
9860 /* whole width, moving the line pointers is faster */
9861 j = row + i;
9862 temp = LineOffset[j];
9863 while ((j += line_count) <= end - 1)
9864 {
9865 LineOffset[j - line_count] = LineOffset[j];
9866 LineWraps[j - line_count] = LineWraps[j];
9867 }
9868 LineOffset[j - line_count] = temp;
9869 LineWraps[j - line_count] = FALSE;
9870 if (can_clear((char_u *)" "))
9871 lineclear(temp, (int)Columns);
9872 else
9873 lineinvalid(temp, (int)Columns);
9874 }
9875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009876
9877 screen_stop_highlight();
9878
9879#ifdef FEAT_VERTSPLIT
9880 /* redraw the characters */
9881 if (type == USE_REDRAW)
9882 redraw_block(row, end, wp);
9883 else
9884#endif
9885 if (type == USE_T_CD) /* delete the lines */
9886 {
9887 windgoto(cursor_row, 0);
9888 out_str(T_CD);
9889 screen_start(); /* don't know where cursor is now */
9890 }
9891 else if (type == USE_T_CDL)
9892 {
9893 windgoto(cursor_row, 0);
9894 term_delete_lines(line_count);
9895 screen_start(); /* don't know where cursor is now */
9896 }
9897 /*
9898 * Deleting lines at top of the screen or scroll region: Just scroll
9899 * the whole screen (scroll region) up by outputting newlines on the
9900 * last line.
9901 */
9902 else if (type == USE_NL)
9903 {
9904 windgoto(cursor_end - 1, 0);
9905 for (i = line_count; --i >= 0; )
9906 out_char('\n'); /* cursor will remain on same line */
9907 }
9908 else
9909 {
9910 for (i = line_count; --i >= 0; )
9911 {
9912 if (type == USE_T_DL)
9913 {
9914 windgoto(cursor_row, 0);
9915 out_str(T_DL); /* delete a line */
9916 }
9917 else /* type == USE_T_CE */
9918 {
9919 windgoto(cursor_row + i, 0);
9920 out_str(T_CE); /* erase a line */
9921 }
9922 screen_start(); /* don't know where cursor is now */
9923 }
9924 }
9925
9926 /*
9927 * If the 'db' flag is set, we need to clear the lines that have been
9928 * scrolled up at the bottom of the region.
9929 */
9930 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
9931 {
9932 for (i = line_count; i > 0; --i)
9933 {
9934 windgoto(cursor_end - i, 0);
9935 out_str(T_CE); /* erase a line */
9936 screen_start(); /* don't know where cursor is now */
9937 }
9938 }
9939
9940#ifdef FEAT_GUI
9941 gui_can_update_cursor();
9942 if (gui.in_use)
9943 out_flush(); /* always flush after a scroll */
9944#endif
9945
9946 return OK;
9947}
9948
9949/*
9950 * show the current mode and ruler
9951 *
9952 * If clear_cmdline is TRUE, clear the rest of the cmdline.
9953 * If clear_cmdline is FALSE there may be a message there that needs to be
9954 * cleared only if a mode is shown.
9955 * Return the length of the message (0 if no message).
9956 */
9957 int
9958showmode()
9959{
9960 int need_clear;
9961 int length = 0;
9962 int do_mode;
9963 int attr;
9964 int nwr_save;
9965#ifdef FEAT_INS_EXPAND
9966 int sub_attr;
9967#endif
9968
Bram Moolenaar7df351e2006-01-23 22:30:28 +00009969 do_mode = ((p_smd && msg_silent == 0)
9970 && ((State & INSERT)
9971 || restart_edit
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009972 || VIsual_active));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009973 if (do_mode || Recording)
9974 {
9975 /*
9976 * Don't show mode right now, when not redrawing or inside a mapping.
9977 * Call char_avail() only when we are going to show something, because
9978 * it takes a bit of time.
9979 */
9980 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
9981 {
9982 redraw_cmdline = TRUE; /* show mode later */
9983 return 0;
9984 }
9985
9986 nwr_save = need_wait_return;
9987
9988 /* wait a bit before overwriting an important message */
9989 check_for_delay(FALSE);
9990
9991 /* if the cmdline is more than one line high, erase top lines */
9992 need_clear = clear_cmdline;
9993 if (clear_cmdline && cmdline_row < Rows - 1)
9994 msg_clr_cmdline(); /* will reset clear_cmdline */
9995
9996 /* Position on the last line in the window, column 0 */
9997 msg_pos_mode();
9998 cursor_off();
9999 attr = hl_attr(HLF_CM); /* Highlight mode */
10000 if (do_mode)
10001 {
10002 MSG_PUTS_ATTR("--", attr);
10003#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010004 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010005# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010006 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010007# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010008 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010009# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010010 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010011# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010012 MSG_PUTS_ATTR(" IM", attr);
10013# else
10014 MSG_PUTS_ATTR(" XIM", attr);
10015# endif
10016#endif
10017#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10018 if (gui.in_use)
10019 {
10020 if (hangul_input_state_get())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000010021 MSG_PUTS_ATTR(" \307\321\261\333", attr); /* HANGUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010022 }
10023#endif
10024#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010025 /* CTRL-X in Insert mode */
10026 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010027 {
10028 /* These messages can get long, avoid a wrap in a narrow
10029 * window. Prefer showing edit_submode_extra. */
10030 length = (Rows - msg_row) * Columns - 3;
10031 if (edit_submode_extra != NULL)
10032 length -= vim_strsize(edit_submode_extra);
10033 if (length > 0)
10034 {
10035 if (edit_submode_pre != NULL)
10036 length -= vim_strsize(edit_submode_pre);
10037 if (length - vim_strsize(edit_submode) > 0)
10038 {
10039 if (edit_submode_pre != NULL)
10040 msg_puts_attr(edit_submode_pre, attr);
10041 msg_puts_attr(edit_submode, attr);
10042 }
10043 if (edit_submode_extra != NULL)
10044 {
10045 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10046 if ((int)edit_submode_highl < (int)HLF_COUNT)
10047 sub_attr = hl_attr(edit_submode_highl);
10048 else
10049 sub_attr = attr;
10050 msg_puts_attr(edit_submode_extra, sub_attr);
10051 }
10052 }
10053 length = 0;
10054 }
10055 else
10056#endif
10057 {
10058#ifdef FEAT_VREPLACE
10059 if (State & VREPLACE_FLAG)
10060 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
10061 else
10062#endif
10063 if (State & REPLACE_FLAG)
10064 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10065 else if (State & INSERT)
10066 {
10067#ifdef FEAT_RIGHTLEFT
10068 if (p_ri)
10069 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10070#endif
10071 MSG_PUTS_ATTR(_(" INSERT"), attr);
10072 }
10073 else if (restart_edit == 'I')
10074 MSG_PUTS_ATTR(_(" (insert)"), attr);
10075 else if (restart_edit == 'R')
10076 MSG_PUTS_ATTR(_(" (replace)"), attr);
10077 else if (restart_edit == 'V')
10078 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10079#ifdef FEAT_RIGHTLEFT
10080 if (p_hkmap)
10081 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10082# ifdef FEAT_FKMAP
10083 if (p_fkmap)
10084 MSG_PUTS_ATTR(farsi_text_5, attr);
10085# endif
10086#endif
10087#ifdef FEAT_KEYMAP
10088 if (State & LANGMAP)
10089 {
10090# ifdef FEAT_ARABIC
10091 if (curwin->w_p_arab)
10092 MSG_PUTS_ATTR(_(" Arabic"), attr);
10093 else
10094# endif
10095 MSG_PUTS_ATTR(_(" (lang)"), attr);
10096 }
10097#endif
10098 if ((State & INSERT) && p_paste)
10099 MSG_PUTS_ATTR(_(" (paste)"), attr);
10100
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101 if (VIsual_active)
10102 {
10103 char *p;
10104
10105 /* Don't concatenate separate words to avoid translation
10106 * problems. */
10107 switch ((VIsual_select ? 4 : 0)
10108 + (VIsual_mode == Ctrl_V) * 2
10109 + (VIsual_mode == 'V'))
10110 {
10111 case 0: p = N_(" VISUAL"); break;
10112 case 1: p = N_(" VISUAL LINE"); break;
10113 case 2: p = N_(" VISUAL BLOCK"); break;
10114 case 4: p = N_(" SELECT"); break;
10115 case 5: p = N_(" SELECT LINE"); break;
10116 default: p = N_(" SELECT BLOCK"); break;
10117 }
10118 MSG_PUTS_ATTR(_(p), attr);
10119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120 MSG_PUTS_ATTR(" --", attr);
10121 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010122
Bram Moolenaar071d4272004-06-13 20:20:40 +000010123 need_clear = TRUE;
10124 }
10125 if (Recording
10126#ifdef FEAT_INS_EXPAND
10127 && edit_submode == NULL /* otherwise it gets too long */
10128#endif
10129 )
10130 {
10131 MSG_PUTS_ATTR(_("recording"), attr);
10132 need_clear = TRUE;
10133 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010134
10135 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010136 if (need_clear || clear_cmdline)
10137 msg_clr_eos();
10138 msg_didout = FALSE; /* overwrite this message */
10139 length = msg_col;
10140 msg_col = 0;
10141 need_wait_return = nwr_save; /* never ask for hit-return for this */
10142 }
10143 else if (clear_cmdline && msg_silent == 0)
10144 /* Clear the whole command line. Will reset "clear_cmdline". */
10145 msg_clr_cmdline();
10146
10147#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010148 /* In Visual mode the size of the selected area must be redrawn. */
10149 if (VIsual_active)
10150 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010151
10152 /* If the last window has no status line, the ruler is after the mode
10153 * message and must be redrawn */
10154 if (redrawing()
10155# ifdef FEAT_WINDOWS
10156 && lastwin->w_status_height == 0
10157# endif
10158 )
10159 win_redr_ruler(lastwin, TRUE);
10160#endif
10161 redraw_cmdline = FALSE;
10162 clear_cmdline = FALSE;
10163
10164 return length;
10165}
10166
10167/*
10168 * Position for a mode message.
10169 */
10170 static void
10171msg_pos_mode()
10172{
10173 msg_col = 0;
10174 msg_row = Rows - 1;
10175}
10176
10177/*
10178 * Delete mode message. Used when ESC is typed which is expected to end
10179 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010180 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010181 */
10182 void
10183unshowmode(force)
10184 int force;
10185{
10186 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010187 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010188 */
10189 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10190 redraw_cmdline = TRUE; /* delete mode later */
10191 else
10192 {
10193 msg_pos_mode();
10194 if (Recording)
10195 MSG_PUTS_ATTR(_("recording"), hl_attr(HLF_CM));
10196 msg_clr_eos();
10197 }
10198}
10199
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010200#if defined(FEAT_WINDOWS)
10201/*
10202 * Draw the tab pages line at the top of the Vim window.
10203 */
10204 static void
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010205draw_tabline()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010206{
10207 int tabcount = 0;
10208 tabpage_T *tp;
10209 int tabwidth;
10210 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010211 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010212 int attr;
10213 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010214 win_T *cwp;
10215 int wincount;
10216 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010217 int c;
10218 int len;
10219 int attr_sel = hl_attr(HLF_TPS);
10220 int attr_nosel = hl_attr(HLF_TP);
10221 int attr_fill = hl_attr(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010222 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010223 int room;
10224 int use_sep_chars = (t_colors < 8
10225#ifdef FEAT_GUI
10226 && !gui.in_use
10227#endif
10228 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010229
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010230 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010231
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010232#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010233 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010234 if (gui_use_tabline())
10235 {
10236 gui_update_tabline();
10237 return;
10238 }
10239#endif
10240
10241 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010242 return;
10243
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010244#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010245
10246 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10247 for (scol = 0; scol < Columns; ++scol)
10248 TabPageIdxs[scol] = 0;
10249
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010250 /* Use the 'tabline' option if it's set. */
10251 if (*p_tal != NUL)
10252 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010253 int save_called_emsg = called_emsg;
10254
10255 /* Check for an error. If there is one we would loop in redrawing the
10256 * screen. Avoid that by making 'tabline' empty. */
10257 called_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010258 win_redr_custom(NULL, FALSE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010259 if (called_emsg)
10260 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010261 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010262 called_emsg |= save_called_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010263 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010264 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010265#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010266 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010267 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
10268 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010269
Bram Moolenaar238a5642006-02-21 22:12:05 +000010270 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10271 if (tabwidth < 6)
10272 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010273
Bram Moolenaar238a5642006-02-21 22:12:05 +000010274 attr = attr_nosel;
10275 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010276 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010277 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10278 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010279 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010280 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010281
Bram Moolenaar238a5642006-02-21 22:12:05 +000010282 if (tp->tp_topframe == topframe)
10283 attr = attr_sel;
10284 if (use_sep_chars && col > 0)
10285 screen_putchar('|', 0, col++, attr);
10286
10287 if (tp->tp_topframe != topframe)
10288 attr = attr_nosel;
10289
10290 screen_putchar(' ', 0, col++, attr);
10291
10292 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010293 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010294 cwp = curwin;
10295 wp = firstwin;
10296 }
10297 else
10298 {
10299 cwp = tp->tp_curwin;
10300 wp = tp->tp_firstwin;
10301 }
10302
10303 modified = FALSE;
10304 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10305 if (bufIsChanged(wp->w_buffer))
10306 modified = TRUE;
10307 if (modified || wincount > 1)
10308 {
10309 if (wincount > 1)
10310 {
10311 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010312 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010313 if (col + len >= Columns - 3)
10314 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010315 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010316#if defined(FEAT_SYN_HL)
Bram Moolenaare0f14822014-08-06 13:20:56 +020010317 hl_combine_attr(attr, hl_attr(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010318#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010319 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010320#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010321 );
10322 col += len;
10323 }
10324 if (modified)
10325 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10326 screen_putchar(' ', 0, col++, attr);
10327 }
10328
10329 room = scol - col + tabwidth - 1;
10330 if (room > 0)
10331 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010332 /* Get buffer name in NameBuff[] */
10333 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010334 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010335 len = vim_strsize(NameBuff);
10336 p = NameBuff;
10337#ifdef FEAT_MBYTE
10338 if (has_mbyte)
10339 while (len > room)
10340 {
10341 len -= ptr2cells(p);
10342 mb_ptr_adv(p);
10343 }
10344 else
10345#endif
10346 if (len > room)
10347 {
10348 p += len - room;
10349 len = room;
10350 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010351 if (len > Columns - col - 1)
10352 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010353
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010354 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010355 col += len;
10356 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010357 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010358
10359 /* Store the tab page number in TabPageIdxs[], so that
10360 * jump_to_mouse() knows where each one is. */
10361 ++tabcount;
10362 while (scol < col)
10363 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010364 }
10365
Bram Moolenaar238a5642006-02-21 22:12:05 +000010366 if (use_sep_chars)
10367 c = '_';
10368 else
10369 c = ' ';
10370 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010371
10372 /* Put an "X" for closing the current tab if there are several. */
10373 if (first_tabpage->tp_next != NULL)
10374 {
10375 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10376 TabPageIdxs[Columns - 1] = -999;
10377 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010378 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010379
10380 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10381 * set. */
10382 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010383}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010384
10385/*
10386 * Get buffer name for "buf" into NameBuff[].
10387 * Takes care of special buffer names and translates special characters.
10388 */
10389 void
10390get_trans_bufname(buf)
10391 buf_T *buf;
10392{
10393 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010394 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010395 else
10396 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10397 trans_characters(NameBuff, MAXPATHL);
10398}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010399#endif
10400
Bram Moolenaar071d4272004-06-13 20:20:40 +000010401#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
10402/*
10403 * Get the character to use in a status line. Get its attributes in "*attr".
10404 */
10405 static int
10406fillchar_status(attr, is_curwin)
10407 int *attr;
10408 int is_curwin;
10409{
10410 int fill;
10411 if (is_curwin)
10412 {
10413 *attr = hl_attr(HLF_S);
10414 fill = fill_stl;
10415 }
10416 else
10417 {
10418 *attr = hl_attr(HLF_SNC);
10419 fill = fill_stlnc;
10420 }
10421 /* Use fill when there is highlighting, and highlighting of current
10422 * window differs, or the fillchars differ, or this is not the
10423 * current window */
10424 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
10425 || !is_curwin || firstwin == lastwin)
10426 || (fill_stl != fill_stlnc)))
10427 return fill;
10428 if (is_curwin)
10429 return '^';
10430 return '=';
10431}
10432#endif
10433
10434#ifdef FEAT_VERTSPLIT
10435/*
10436 * Get the character to use in a separator between vertically split windows.
10437 * Get its attributes in "*attr".
10438 */
10439 static int
10440fillchar_vsep(attr)
10441 int *attr;
10442{
10443 *attr = hl_attr(HLF_C);
10444 if (*attr == 0 && fill_vert == ' ')
10445 return '|';
10446 else
10447 return fill_vert;
10448}
10449#endif
10450
10451/*
10452 * Return TRUE if redrawing should currently be done.
10453 */
10454 int
10455redrawing()
10456{
10457 return (!RedrawingDisabled
10458 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
10459}
10460
10461/*
10462 * Return TRUE if printing messages should currently be done.
10463 */
10464 int
10465messaging()
10466{
10467 return (!(p_lz && char_avail() && !KeyTyped));
10468}
10469
10470/*
10471 * Show current status info in ruler and various other places
10472 * If always is FALSE, only show ruler if position has changed.
10473 */
10474 void
10475showruler(always)
10476 int always;
10477{
10478 if (!always && !redrawing())
10479 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010480#ifdef FEAT_INS_EXPAND
10481 if (pum_visible())
10482 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010483# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +000010484 /* Don't redraw right now, do it later. */
10485 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010486# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +000010487 return;
10488 }
10489#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010490#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010491 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010492 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010493 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010495 else
10496#endif
10497#ifdef FEAT_CMDL_INFO
10498 win_redr_ruler(curwin, always);
10499#endif
10500
10501#ifdef FEAT_TITLE
10502 if (need_maketitle
10503# ifdef FEAT_STL_OPT
10504 || (p_icon && (stl_syntax & STL_IN_ICON))
10505 || (p_title && (stl_syntax & STL_IN_TITLE))
10506# endif
10507 )
10508 maketitle();
10509#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010510#ifdef FEAT_WINDOWS
10511 /* Redraw the tab pages line if needed. */
10512 if (redraw_tabline)
10513 draw_tabline();
10514#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010515}
10516
10517#ifdef FEAT_CMDL_INFO
10518 static void
10519win_redr_ruler(wp, always)
10520 win_T *wp;
10521 int always;
10522{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010523#define RULER_BUF_LEN 70
10524 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010525 int row;
10526 int fillchar;
10527 int attr;
10528 int empty_line = FALSE;
10529 colnr_T virtcol;
10530 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010531 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010532 int o;
10533#ifdef FEAT_VERTSPLIT
10534 int this_ru_col;
10535 int off = 0;
10536 int width = Columns;
10537# define WITH_OFF(x) x
10538# define WITH_WIDTH(x) x
10539#else
10540# define WITH_OFF(x) 0
10541# define WITH_WIDTH(x) Columns
10542# define this_ru_col ru_col
10543#endif
10544
10545 /* If 'ruler' off or redrawing disabled, don't do anything */
10546 if (!p_ru)
10547 return;
10548
10549 /*
10550 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10551 * after deleting lines, before cursor.lnum is corrected.
10552 */
10553 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10554 return;
10555
10556#ifdef FEAT_INS_EXPAND
10557 /* Don't draw the ruler while doing insert-completion, it might overwrite
10558 * the (long) mode message. */
10559# ifdef FEAT_WINDOWS
10560 if (wp == lastwin && lastwin->w_status_height == 0)
10561# endif
10562 if (edit_submode != NULL)
10563 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010564 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10565 if (pum_visible())
10566 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010567#endif
10568
10569#ifdef FEAT_STL_OPT
10570 if (*p_ruf)
10571 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010572 int save_called_emsg = called_emsg;
10573
10574 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010575 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010576 if (called_emsg)
10577 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010578 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010579 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010580 return;
10581 }
10582#endif
10583
10584 /*
10585 * Check if not in Insert mode and the line is empty (will show "0-1").
10586 */
10587 if (!(State & INSERT)
10588 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10589 empty_line = TRUE;
10590
10591 /*
10592 * Only draw the ruler when something changed.
10593 */
10594 validate_virtcol_win(wp);
10595 if ( redraw_cmdline
10596 || always
10597 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10598 || wp->w_cursor.col != wp->w_ru_cursor.col
10599 || wp->w_virtcol != wp->w_ru_virtcol
10600#ifdef FEAT_VIRTUALEDIT
10601 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10602#endif
10603 || wp->w_topline != wp->w_ru_topline
10604 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10605#ifdef FEAT_DIFF
10606 || wp->w_topfill != wp->w_ru_topfill
10607#endif
10608 || empty_line != wp->w_ru_empty)
10609 {
10610 cursor_off();
10611#ifdef FEAT_WINDOWS
10612 if (wp->w_status_height)
10613 {
10614 row = W_WINROW(wp) + wp->w_height;
10615 fillchar = fillchar_status(&attr, wp == curwin);
10616# ifdef FEAT_VERTSPLIT
10617 off = W_WINCOL(wp);
10618 width = W_WIDTH(wp);
10619# endif
10620 }
10621 else
10622#endif
10623 {
10624 row = Rows - 1;
10625 fillchar = ' ';
10626 attr = 0;
10627#ifdef FEAT_VERTSPLIT
10628 width = Columns;
10629 off = 0;
10630#endif
10631 }
10632
10633 /* In list mode virtcol needs to be recomputed */
10634 virtcol = wp->w_virtcol;
10635 if (wp->w_p_list && lcs_tab1 == NUL)
10636 {
10637 wp->w_p_list = FALSE;
10638 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10639 wp->w_p_list = TRUE;
10640 }
10641
10642 /*
10643 * Some sprintfs return the length, some return a pointer.
10644 * To avoid portability problems we use strlen() here.
10645 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010646 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010647 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10648 ? 0L
10649 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010650 len = STRLEN(buffer);
10651 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010652 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10653 (int)virtcol + 1);
10654
10655 /*
10656 * Add a "50%" if there is room for it.
10657 * On the last line, don't print in the last column (scrolls the
10658 * screen up on some terminals).
10659 */
10660 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010661 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010662 o = i + vim_strsize(buffer + i + 1);
10663#ifdef FEAT_WINDOWS
10664 if (wp->w_status_height == 0) /* can't use last char of screen */
10665#endif
10666 ++o;
10667#ifdef FEAT_VERTSPLIT
10668 this_ru_col = ru_col - (Columns - width);
10669 if (this_ru_col < 0)
10670 this_ru_col = 0;
10671#endif
10672 /* Never use more than half the window/screen width, leave the other
10673 * half for the filename. */
10674 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
10675 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
10676 if (this_ru_col + o < WITH_WIDTH(width))
10677 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010010678 /* need at least 3 chars left for get_rel_pos() + NUL */
10679 while (this_ru_col + o < WITH_WIDTH(width) && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010680 {
10681#ifdef FEAT_MBYTE
10682 if (has_mbyte)
10683 i += (*mb_char2bytes)(fillchar, buffer + i);
10684 else
10685#endif
10686 buffer[i++] = fillchar;
10687 ++o;
10688 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010689 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010690 }
10691 /* Truncate at window boundary. */
10692#ifdef FEAT_MBYTE
10693 if (has_mbyte)
10694 {
10695 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010696 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010697 {
10698 o += (*mb_ptr2cells)(buffer + i);
10699 if (this_ru_col + o > WITH_WIDTH(width))
10700 {
10701 buffer[i] = NUL;
10702 break;
10703 }
10704 }
10705 }
10706 else
10707#endif
10708 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
10709 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
10710
10711 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
10712 i = redraw_cmdline;
10713 screen_fill(row, row + 1,
10714 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
10715 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
10716 fillchar, fillchar, attr);
10717 /* don't redraw the cmdline because of showing the ruler */
10718 redraw_cmdline = i;
10719 wp->w_ru_cursor = wp->w_cursor;
10720 wp->w_ru_virtcol = wp->w_virtcol;
10721 wp->w_ru_empty = empty_line;
10722 wp->w_ru_topline = wp->w_topline;
10723 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10724#ifdef FEAT_DIFF
10725 wp->w_ru_topfill = wp->w_topfill;
10726#endif
10727 }
10728}
10729#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010730
10731#if defined(FEAT_LINEBREAK) || defined(PROTO)
10732/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010733 * Return the width of the 'number' and 'relativenumber' column.
10734 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010735 * Otherwise it depends on 'numberwidth' and the line count.
10736 */
10737 int
10738number_width(wp)
10739 win_T *wp;
10740{
10741 int n;
10742 linenr_T lnum;
10743
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020010744 if (wp->w_p_rnu && !wp->w_p_nu)
10745 /* cursor line shows "0" */
10746 lnum = wp->w_height;
10747 else
10748 /* cursor line shows absolute line number */
10749 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020010750
Bram Moolenaar6b314672015-03-20 15:42:10 +010010751 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010752 return wp->w_nrwidth_width;
10753 wp->w_nrwidth_line_count = lnum;
10754
10755 n = 0;
10756 do
10757 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010758 lnum /= 10;
10759 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010760 } while (lnum > 0);
10761
10762 /* 'numberwidth' gives the minimal width plus one */
10763 if (n < wp->w_p_nuw - 1)
10764 n = wp->w_p_nuw - 1;
10765
10766 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010010767 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010768 return n;
10769}
10770#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010771
10772/*
10773 * Return the current cursor column. This is the actual position on the
10774 * screen. First column is 0.
10775 */
10776 int
10777screen_screencol()
10778{
10779 return screen_cur_col;
10780}
10781
10782/*
10783 * Return the current cursor row. This is the actual position on the screen.
10784 * First row is 0.
10785 */
10786 int
10787screen_screenrow()
10788{
10789 return screen_cur_row;
10790}