blob: 1addd0ad41ad61dee684ebca22dacfb54e0bedc7 [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
2743 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002744 if (wp->w_p_cuc)
2745 {
2746 txtcol += wp->w_virtcol;
2747 if (wp->w_p_wrap)
2748 txtcol -= wp->w_skipcol;
2749 else
2750 txtcol -= wp->w_leftcol;
2751 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2752 ScreenAttrs[off + txtcol] = hl_combine_attr(
2753 ScreenAttrs[off + txtcol], hl_attr(HLF_CUC));
2754 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002755#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756
2757 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2758 (int)W_WIDTH(wp), FALSE);
2759
2760 /*
2761 * Update w_cline_height and w_cline_folded if the cursor line was
2762 * updated (saves a call to plines() later).
2763 */
2764 if (wp == curwin
2765 && lnum <= curwin->w_cursor.lnum
2766 && lnume >= curwin->w_cursor.lnum)
2767 {
2768 curwin->w_cline_row = row;
2769 curwin->w_cline_height = 1;
2770 curwin->w_cline_folded = TRUE;
2771 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2772 }
2773}
2774
2775/*
2776 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2777 */
2778 static void
2779copy_text_attr(off, buf, len, attr)
2780 int off;
2781 char_u *buf;
2782 int len;
2783 int attr;
2784{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002785 int i;
2786
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 mch_memmove(ScreenLines + off, buf, (size_t)len);
2788# ifdef FEAT_MBYTE
2789 if (enc_utf8)
2790 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2791# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002792 for (i = 0; i < len; ++i)
2793 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794}
2795
2796/*
2797 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002798 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 */
2800 static void
2801fill_foldcolumn(p, wp, closed, lnum)
2802 char_u *p;
2803 win_T *wp;
2804 int closed; /* TRUE of FALSE */
2805 linenr_T lnum; /* current line number */
2806{
2807 int i = 0;
2808 int level;
2809 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002810 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002811 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812
2813 /* Init to all spaces. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002814 copy_spaces(p, (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815
2816 level = win_foldinfo.fi_level;
2817 if (level > 0)
2818 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002819 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002820 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002821
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 /* If the column is too narrow, we start at the lowest level that
2823 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002824 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 if (first_level < 1)
2826 first_level = 1;
2827
Bram Moolenaar1c934292015-01-27 16:39:29 +01002828 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 {
2830 if (win_foldinfo.fi_lnum == lnum
2831 && first_level + i >= win_foldinfo.fi_low_level)
2832 p[i] = '-';
2833 else if (first_level == 1)
2834 p[i] = '|';
2835 else if (first_level + i <= 9)
2836 p[i] = '0' + first_level + i;
2837 else
2838 p[i] = '>';
2839 if (first_level + i == level)
2840 break;
2841 }
2842 }
2843 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002844 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845}
2846#endif /* FEAT_FOLDING */
2847
2848/*
2849 * Display line "lnum" of window 'wp' on the screen.
2850 * Start at row "startrow", stop when "endrow" is reached.
2851 * wp->w_virtcol needs to be valid.
2852 *
2853 * Return the number of last row the line occupies.
2854 */
2855 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +00002856win_line(wp, lnum, startrow, endrow, nochange)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 win_T *wp;
2858 linenr_T lnum;
2859 int startrow;
2860 int endrow;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002861 int nochange UNUSED; /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862{
2863 int col; /* visual column on screen */
2864 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2865 int c = 0; /* init for GCC */
2866 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01002867#ifdef FEAT_LINEBREAK
2868 long vcol_sbr = -1; /* virtual column after showbreak */
2869#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 long vcol_prev = -1; /* "vcol" of previous character */
2871 char_u *line; /* current line */
2872 char_u *ptr; /* current position in "line" */
2873 int row; /* row in the window, excl w_winrow */
2874 int screen_row; /* row on the screen, incl w_winrow */
2875
2876 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2877 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00002878 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02002879 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 int c_extra = NUL; /* extra chars, all the same */
2881 int extra_attr = 0; /* attributes when n_extra != 0 */
2882 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2883 displaying lcs_eol at end-of-line */
2884 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2885 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2886
2887 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2888 int saved_n_extra = 0;
2889 char_u *saved_p_extra = NULL;
2890 int saved_c_extra = 0;
2891 int saved_char_attr = 0;
2892
2893 int n_attr = 0; /* chars with special attr */
2894 int saved_attr2 = 0; /* char_attr saved for n_attr */
2895 int n_attr3 = 0; /* chars with overruling special attr */
2896 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2897
2898 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2899
2900 int fromcol, tocol; /* start/end of inverting */
2901 int fromcol_prev = -2; /* start of inverting after cursor */
2902 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002904 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 pos_T pos;
2906 long v;
2907
2908 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002909 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2911 in this line */
2912 int attr = 0; /* attributes for area highlighting */
2913 int area_attr = 0; /* attributes desired by highlighting */
2914 int search_attr = 0; /* attributes desired by 'hlsearch' */
2915#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002916 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 int syntax_attr = 0; /* attributes desired by syntax */
2918 int has_syntax = FALSE; /* this buffer has syntax highl. */
2919 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00002920 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02002921 int draw_color_col = FALSE; /* highlight colorcolumn */
2922 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002923#endif
2924#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002925 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002926# define SPWORDLEN 150
2927 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00002928 int nextlinecol = 0; /* column where nextline[] starts */
2929 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00002930 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00002931 int spell_attr = 0; /* attributes desired by spelling */
2932 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00002933 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2934 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00002935 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002936 static int cap_col = -1; /* column to check for Cap word */
2937 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002938 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939#endif
2940 int extra_check; /* has syntax or linebreak */
2941#ifdef FEAT_MBYTE
2942 int multi_attr = 0; /* attributes desired by multibyte */
2943 int mb_l = 1; /* multi-byte byte length */
2944 int mb_c = 0; /* decoded multi-byte character */
2945 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002946 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947#endif
2948#ifdef FEAT_DIFF
2949 int filler_lines; /* nr of filler lines to be drawn */
2950 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002951 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 int change_start = MAXCOL; /* first col of changed area */
2953 int change_end = -1; /* last col of changed area */
2954#endif
2955 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
2956#ifdef FEAT_LINEBREAK
2957 int need_showbreak = FALSE;
2958#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002959#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
2960 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00002962 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963#endif
2964#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00002965 matchitem_T *cur; /* points to the match list */
2966 match_T *shl; /* points to search_hl or a match */
2967 int shl_flag; /* flag to indicate whether search_hl
2968 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02002969 int pos_inprogress; /* marks that position match search is
2970 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00002971 int prevcol_hl_flag; /* flag to indicate whether prevcol
2972 equals startcol of search_hl or one
2973 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974#endif
2975#ifdef FEAT_ARABIC
2976 int prev_c = 0; /* previous Arabic character */
2977 int prev_c1 = 0; /* first composing char for prev_c */
2978#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002979#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00002980 int did_line_attr = 0;
2981#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982
2983 /* draw_state: items that are drawn in sequence: */
2984#define WL_START 0 /* nothing done yet */
2985#ifdef FEAT_CMDWIN
2986# define WL_CMDLINE WL_START + 1 /* cmdline window column */
2987#else
2988# define WL_CMDLINE WL_START
2989#endif
2990#ifdef FEAT_FOLDING
2991# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
2992#else
2993# define WL_FOLD WL_CMDLINE
2994#endif
2995#ifdef FEAT_SIGNS
2996# define WL_SIGN WL_FOLD + 1 /* column for signs */
2997#else
2998# define WL_SIGN WL_FOLD /* column for signs */
2999#endif
3000#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003001#ifdef FEAT_LINEBREAK
3002# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003004# define WL_BRI WL_NR
3005#endif
3006#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3007# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3008#else
3009# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010#endif
3011#define WL_LINE WL_SBR + 1 /* text in the line */
3012 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003013#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 int feedback_col = 0;
3015 int feedback_old_attr = -1;
3016#endif
3017
Bram Moolenaar860cae12010-06-05 23:22:07 +02003018#ifdef FEAT_CONCEAL
3019 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003020 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003021 int prev_syntax_id = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003022 int conceal_attr = hl_attr(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003023 int is_concealing = FALSE;
3024 int boguscols = 0; /* nonexistent columns added to force
3025 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003026 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003027 int did_wcol = FALSE;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003028 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003029# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003030# define FIX_FOR_BOGUSCOLS \
3031 { \
3032 n_extra += vcol_off; \
3033 vcol -= vcol_off; \
3034 vcol_off = 0; \
3035 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003036 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003037 boguscols = 0; \
3038 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003039#else
3040# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003041#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042
3043 if (startrow > endrow) /* past the end already! */
3044 return startrow;
3045
3046 row = startrow;
3047 screen_row = row + W_WINROW(wp);
3048
3049 /*
3050 * To speed up the loop below, set extra_check when there is linebreak,
3051 * trailing white space and/or syntax processing to be done.
3052 */
3053#ifdef FEAT_LINEBREAK
3054 extra_check = wp->w_p_lbr;
3055#else
3056 extra_check = 0;
3057#endif
3058#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02003059 if (syntax_present(wp) && !wp->w_s->b_syn_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 {
3061 /* Prepare for syntax highlighting in this line. When there is an
3062 * error, stop syntax highlighting. */
3063 save_did_emsg = did_emsg;
3064 did_emsg = FALSE;
3065 syntax_start(wp, lnum);
3066 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003067 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 else
3069 {
3070 did_emsg = save_did_emsg;
3071 has_syntax = TRUE;
3072 extra_check = TRUE;
3073 }
3074 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003075
3076 /* Check for columns to display for 'colorcolumn'. */
3077 color_cols = wp->w_p_cc_cols;
3078 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003079 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003080#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003081
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003082#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003083 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02003084 && *wp->w_s->b_p_spl != NUL
3085 && wp->w_s->b_langp.ga_len > 0
3086 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003087 {
3088 /* Prepare for spell checking. */
3089 has_spell = TRUE;
3090 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003091
3092 /* Get the start of the next line, so that words that wrap to the next
3093 * line are found too: "et<line-break>al.".
3094 * Trick: skip a few chars for C/shell/Vim comments */
3095 nextline[SPWORDLEN] = NUL;
3096 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3097 {
3098 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3099 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3100 }
3101
3102 /* When a word wrapped from the previous line the start of the current
3103 * line is valid. */
3104 if (lnum == checked_lnum)
3105 cur_checked_col = checked_col;
3106 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003107
3108 /* When there was a sentence end in the previous line may require a
3109 * word starting with capital in this line. In line 1 always check
3110 * the first word. */
3111 if (lnum != capcol_lnum)
3112 cap_col = -1;
3113 if (lnum == 1)
3114 cap_col = 0;
3115 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003116 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117#endif
3118
3119 /*
3120 * handle visual active in this window
3121 */
3122 fromcol = -10;
3123 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3125 {
3126 /* Visual is after curwin->w_cursor */
3127 if (ltoreq(curwin->w_cursor, VIsual))
3128 {
3129 top = &curwin->w_cursor;
3130 bot = &VIsual;
3131 }
3132 else /* Visual is before curwin->w_cursor */
3133 {
3134 top = &VIsual;
3135 bot = &curwin->w_cursor;
3136 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003137 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 if (VIsual_mode == Ctrl_V) /* block mode */
3139 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003140 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 {
3142 fromcol = wp->w_old_cursor_fcol;
3143 tocol = wp->w_old_cursor_lcol;
3144 }
3145 }
3146 else /* non-block mode */
3147 {
3148 if (lnum > top->lnum && lnum <= bot->lnum)
3149 fromcol = 0;
3150 else if (lnum == top->lnum)
3151 {
3152 if (VIsual_mode == 'V') /* linewise */
3153 fromcol = 0;
3154 else
3155 {
3156 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3157 if (gchar_pos(top) == NUL)
3158 tocol = fromcol + 1;
3159 }
3160 }
3161 if (VIsual_mode != 'V' && lnum == bot->lnum)
3162 {
3163 if (*p_sel == 'e' && bot->col == 0
3164#ifdef FEAT_VIRTUALEDIT
3165 && bot->coladd == 0
3166#endif
3167 )
3168 {
3169 fromcol = -10;
3170 tocol = MAXCOL;
3171 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003172 else if (bot->col == MAXCOL)
3173 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 else
3175 {
3176 pos = *bot;
3177 if (*p_sel == 'e')
3178 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3179 else
3180 {
3181 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3182 ++tocol;
3183 }
3184 }
3185 }
3186 }
3187
3188#ifndef MSDOS
3189 /* Check if the character under the cursor should not be inverted */
3190 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
3191# ifdef FEAT_GUI
3192 && !gui.in_use
3193# endif
3194 )
3195 noinvcur = TRUE;
3196#endif
3197
3198 /* if inverting in this line set area_highlighting */
3199 if (fromcol >= 0)
3200 {
3201 area_highlighting = TRUE;
3202 attr = hl_attr(HLF_V);
3203#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003204 if ((clip_star.available && !clip_star.owned
3205 && clip_isautosel_star())
3206 || (clip_plus.available && !clip_plus.owned
3207 && clip_isautosel_plus()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 attr = hl_attr(HLF_VNC);
3209#endif
3210 }
3211 }
3212
3213 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003214 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003216 else if (highlight_match
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 && wp == curwin
3218 && lnum >= curwin->w_cursor.lnum
3219 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3220 {
3221 if (lnum == curwin->w_cursor.lnum)
3222 getvcol(curwin, &(curwin->w_cursor),
3223 (colnr_T *)&fromcol, NULL, NULL);
3224 else
3225 fromcol = 0;
3226 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3227 {
3228 pos.lnum = lnum;
3229 pos.col = search_match_endcol;
3230 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3231 }
3232 else
3233 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003234 /* do at least one character; happens when past end of line */
3235 if (fromcol == tocol)
3236 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 area_highlighting = TRUE;
3238 attr = hl_attr(HLF_I);
3239 }
3240
3241#ifdef FEAT_DIFF
3242 filler_lines = diff_check(wp, lnum);
3243 if (filler_lines < 0)
3244 {
3245 if (filler_lines == -1)
3246 {
3247 if (diff_find_change(wp, lnum, &change_start, &change_end))
3248 diff_hlf = HLF_ADD; /* added line */
3249 else if (change_start == 0)
3250 diff_hlf = HLF_TXD; /* changed text */
3251 else
3252 diff_hlf = HLF_CHD; /* changed line */
3253 }
3254 else
3255 diff_hlf = HLF_ADD; /* added line */
3256 filler_lines = 0;
3257 area_highlighting = TRUE;
3258 }
3259 if (lnum == wp->w_topline)
3260 filler_lines = wp->w_topfill;
3261 filler_todo = filler_lines;
3262#endif
3263
3264#ifdef LINE_ATTR
3265# ifdef FEAT_SIGNS
3266 /* If this line has a sign with line highlighting set line_attr. */
3267 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3268 if (v != 0)
3269 line_attr = sign_get_attr((int)v, TRUE);
3270# endif
3271# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
3272 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003273 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 line_attr = hl_attr(HLF_L);
3275# endif
3276 if (line_attr != 0)
3277 area_highlighting = TRUE;
3278#endif
3279
3280 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3281 ptr = line;
3282
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003283#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003284 if (has_spell)
3285 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003286 /* For checking first word with a capital skip white space. */
3287 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003288 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003289
Bram Moolenaar30abd282005-06-22 22:35:10 +00003290 /* To be able to spell-check over line boundaries copy the end of the
3291 * current line into nextline[]. Above the start of the next line was
3292 * copied to nextline[SPWORDLEN]. */
3293 if (nextline[SPWORDLEN] == NUL)
3294 {
3295 /* No next line or it is empty. */
3296 nextlinecol = MAXCOL;
3297 nextline_idx = 0;
3298 }
3299 else
3300 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003301 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003302 if (v < SPWORDLEN)
3303 {
3304 /* Short line, use it completely and append the start of the
3305 * next line. */
3306 nextlinecol = 0;
3307 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003308 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003309 nextline_idx = v + 1;
3310 }
3311 else
3312 {
3313 /* Long line, use only the last SPWORDLEN bytes. */
3314 nextlinecol = v - SPWORDLEN;
3315 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3316 nextline_idx = SPWORDLEN + 1;
3317 }
3318 }
3319 }
3320#endif
3321
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 /* find start of trailing whitespace */
3323 if (wp->w_p_list && lcs_trail)
3324 {
3325 trailcol = (colnr_T)STRLEN(ptr);
3326 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
3327 --trailcol;
3328 trailcol += (colnr_T) (ptr - line);
3329 extra_check = TRUE;
3330 }
3331
3332 /*
3333 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3334 * first character to be displayed.
3335 */
3336 if (wp->w_p_wrap)
3337 v = wp->w_skipcol;
3338 else
3339 v = wp->w_leftcol;
3340 if (v > 0)
3341 {
3342#ifdef FEAT_MBYTE
3343 char_u *prev_ptr = ptr;
3344#endif
3345 while (vcol < v && *ptr != NUL)
3346 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003347 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 vcol += c;
3349#ifdef FEAT_MBYTE
3350 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003352 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 }
3354
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003355 /* When:
3356 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003357 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003358 * - 'virtualedit' is set, or
3359 * - the visual mode is active,
3360 * the end of the line may be before the start of the displayed part.
3361 */
3362 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003363#ifdef FEAT_SYN_HL
3364 wp->w_p_cuc || draw_color_col ||
3365#endif
3366#ifdef FEAT_VIRTUALEDIT
3367 virtual_active() ||
3368#endif
3369 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003370 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373
3374 /* Handle a character that's not completely on the screen: Put ptr at
3375 * that character but skip the first few screen characters. */
3376 if (vcol > v)
3377 {
3378 vcol -= c;
3379#ifdef FEAT_MBYTE
3380 ptr = prev_ptr;
3381#else
3382 --ptr;
3383#endif
3384 n_skip = v - vcol;
3385 }
3386
3387 /*
3388 * Adjust for when the inverted text is before the screen,
3389 * and when the start of the inverted text is before the screen.
3390 */
3391 if (tocol <= vcol)
3392 fromcol = 0;
3393 else if (fromcol >= 0 && fromcol < vcol)
3394 fromcol = vcol;
3395
3396#ifdef FEAT_LINEBREAK
3397 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3398 if (wp->w_p_wrap)
3399 need_showbreak = TRUE;
3400#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003401#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003402 /* When spell checking a word we need to figure out the start of the
3403 * word and if it's badly spelled or not. */
3404 if (has_spell)
3405 {
3406 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003407 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003408 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003409
3410 pos = wp->w_cursor;
3411 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003412 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003413 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003414
3415 /* spell_move_to() may call ml_get() and make "line" invalid */
3416 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3417 ptr = line + linecol;
3418
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003419 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003420 {
3421 /* no bad word found at line start, don't check until end of a
3422 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003423 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003424 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003425 }
3426 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003427 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003428 /* bad word found, use attributes until end of word */
3429 word_end = wp->w_cursor.col + len + 1;
3430
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003431 /* Turn index into actual attributes. */
3432 if (spell_hlf != HLF_COUNT)
3433 spell_attr = highlight_attr[spell_hlf];
3434 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003435 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003436
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003437# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003438 /* Need to restart syntax highlighting for this line. */
3439 if (has_syntax)
3440 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003441# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003442 }
3443#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 }
3445
3446 /*
3447 * Correct highlighting for cursor that can't be disabled.
3448 * Avoids having to check this for each character.
3449 */
3450 if (fromcol >= 0)
3451 {
3452 if (noinvcur)
3453 {
3454 if ((colnr_T)fromcol == wp->w_virtcol)
3455 {
3456 /* highlighting starts at cursor, let it start just after the
3457 * cursor */
3458 fromcol_prev = fromcol;
3459 fromcol = -1;
3460 }
3461 else if ((colnr_T)fromcol < wp->w_virtcol)
3462 /* restart highlighting after the cursor */
3463 fromcol_prev = wp->w_virtcol;
3464 }
3465 if (fromcol >= tocol)
3466 fromcol = -1;
3467 }
3468
3469#ifdef FEAT_SEARCH_EXTRA
3470 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003471 * Handle highlighting the last used search pattern and matches.
3472 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003474 cur = wp->w_match_head;
3475 shl_flag = FALSE;
3476 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003478 if (shl_flag == FALSE)
3479 {
3480 shl = &search_hl;
3481 shl_flag = TRUE;
3482 }
3483 else
3484 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003485 shl->startcol = MAXCOL;
3486 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 shl->attr_cur = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003488 v = (long)(ptr - line);
3489 if (cur != NULL)
3490 cur->pos.cur = 0;
3491 next_search_hl(wp, shl, lnum, (colnr_T)v, cur);
3492
3493 /* Need to get the line again, a multi-line regexp may have made it
3494 * invalid. */
3495 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3496 ptr = line + v;
3497
3498 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003500 if (shl->lnum == lnum)
3501 shl->startcol = shl->rm.startpos[0].col;
3502 else
3503 shl->startcol = 0;
3504 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3505 - shl->rm.startpos[0].lnum)
3506 shl->endcol = shl->rm.endpos[0].col;
3507 else
3508 shl->endcol = MAXCOL;
3509 /* Highlight one character for an empty match. */
3510 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003513 if (has_mbyte && line[shl->endcol] != NUL)
3514 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3515 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003517 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003519 if ((long)shl->startcol < v) /* match at leftcol */
3520 {
3521 shl->attr_cur = shl->attr;
3522 search_attr = shl->attr;
3523 }
3524 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003526 if (shl != &search_hl && cur != NULL)
3527 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 }
3529#endif
3530
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003531#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003532 /* Cursor line highlighting for 'cursorline' in the current window. Not
3533 * when Visual mode is active, because it's not clear what is selected
3534 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003535 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003536 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003537 {
3538 line_attr = hl_attr(HLF_CUL);
3539 area_highlighting = TRUE;
3540 }
3541#endif
3542
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003543 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 col = 0;
3545#ifdef FEAT_RIGHTLEFT
3546 if (wp->w_p_rl)
3547 {
3548 /* Rightleft window: process the text in the normal direction, but put
3549 * it in current_ScreenLine[] from right to left. Start at the
3550 * rightmost column of the window. */
3551 col = W_WIDTH(wp) - 1;
3552 off += col;
3553 }
3554#endif
3555
3556 /*
3557 * Repeat for the whole displayed line.
3558 */
3559 for (;;)
3560 {
3561 /* Skip this quickly when working on the text. */
3562 if (draw_state != WL_LINE)
3563 {
3564#ifdef FEAT_CMDWIN
3565 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3566 {
3567 draw_state = WL_CMDLINE;
3568 if (cmdwin_type != 0 && wp == curwin)
3569 {
3570 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003572 c_extra = cmdwin_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 char_attr = hl_attr(HLF_AT);
3574 }
3575 }
3576#endif
3577
3578#ifdef FEAT_FOLDING
3579 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3580 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003581 int fdc = compute_foldcolumn(wp, 0);
3582
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003584 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 {
3586 /* Draw the 'foldcolumn'. */
3587 fill_foldcolumn(extra, wp, FALSE, lnum);
Bram Moolenaar1c934292015-01-27 16:39:29 +01003588 n_extra = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 p_extra = extra;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003590 p_extra[n_extra] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 c_extra = NUL;
3592 char_attr = hl_attr(HLF_FC);
3593 }
3594 }
3595#endif
3596
3597#ifdef FEAT_SIGNS
3598 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3599 {
3600 draw_state = WL_SIGN;
3601 /* Show the sign column when there are any signs in this
3602 * buffer or when using Netbeans. */
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003603 if (draw_signcolumn(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003605 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003607 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608# endif
3609
3610 /* Draw two cells with the sign value or blank. */
3611 c_extra = ' ';
3612 char_attr = hl_attr(HLF_SC);
3613 n_extra = 2;
3614
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003615 if (row == startrow
3616#ifdef FEAT_DIFF
3617 + filler_lines && filler_todo <= 0
3618#endif
3619 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 {
3621 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3622 SIGN_TEXT);
3623# ifdef FEAT_SIGN_ICONS
3624 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3625 SIGN_ICON);
3626 if (gui.in_use && icon_sign != 0)
3627 {
3628 /* Use the image in this position. */
3629 c_extra = SIGN_BYTE;
3630# ifdef FEAT_NETBEANS_INTG
3631 if (buf_signcount(wp->w_buffer, lnum) > 1)
3632 c_extra = MULTISIGN_BYTE;
3633# endif
3634 char_attr = icon_sign;
3635 }
3636 else
3637# endif
3638 if (text_sign != 0)
3639 {
3640 p_extra = sign_get_text(text_sign);
3641 if (p_extra != NULL)
3642 {
3643 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003644 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 }
3646 char_attr = sign_get_attr(text_sign, FALSE);
3647 }
3648 }
3649 }
3650 }
3651#endif
3652
3653 if (draw_state == WL_NR - 1 && n_extra == 0)
3654 {
3655 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003656 /* Display the absolute or relative line number. After the
3657 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3658 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 && (row == startrow
3660#ifdef FEAT_DIFF
3661 + filler_lines
3662#endif
3663 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3664 {
3665 /* Draw the line number (empty space after wrapping). */
3666 if (row == startrow
3667#ifdef FEAT_DIFF
3668 + filler_lines
3669#endif
3670 )
3671 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003672 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003673 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003674
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003675 if (wp->w_p_nu && !wp->w_p_rnu)
3676 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003677 num = (long)lnum;
3678 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003679 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003680 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003681 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003682 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003683 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003684 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003685 num = lnum;
3686 fmt = "%-*ld ";
3687 }
3688 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003689
Bram Moolenaar700e7342013-01-30 12:31:36 +01003690 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003691 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 if (wp->w_skipcol > 0)
3693 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3694 *p_extra = '-';
3695#ifdef FEAT_RIGHTLEFT
3696 if (wp->w_p_rl) /* reverse line numbers */
3697 rl_mirror(extra);
3698#endif
3699 p_extra = extra;
3700 c_extra = NUL;
3701 }
3702 else
3703 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003704 n_extra = number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 char_attr = hl_attr(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003706#ifdef FEAT_SYN_HL
3707 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003708 * the current line differently.
3709 * TODO: Can we use CursorLine instead of CursorLineNr
3710 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003711 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003712 && lnum == wp->w_cursor.lnum)
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003713 char_attr = hl_attr(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003714#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 }
3716 }
3717
Bram Moolenaar597a4222014-06-25 14:39:50 +02003718#ifdef FEAT_LINEBREAK
3719 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3720 && n_extra == 0 && *p_sbr != NUL)
3721 /* draw indent after showbreak value */
3722 draw_state = WL_BRI;
3723 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3724 /* After the showbreak, draw the breakindent */
3725 draw_state = WL_BRI - 1;
3726
3727 /* draw 'breakindent': indent wrapped text accordingly */
3728 if (draw_state == WL_BRI - 1 && n_extra == 0)
3729 {
3730 draw_state = WL_BRI;
3731# ifdef FEAT_DIFF
3732# endif
3733 if (wp->w_p_bri && n_extra == 0 && row != startrow
3734#ifdef FEAT_DIFF
3735 && filler_lines == 0
3736#endif
3737 )
3738 {
3739 char_attr = 0; /* was: hl_attr(HLF_AT); */
3740#ifdef FEAT_DIFF
3741 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003742 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003743 char_attr = hl_attr(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02003744 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3745 char_attr = hl_combine_attr(char_attr,
3746 hl_attr(HLF_CUL));
3747 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02003748#endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02003749 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003750 c_extra = ' ';
3751 n_extra = get_breakindent_win(wp,
3752 ml_get_buf(wp->w_buffer, lnum, FALSE));
3753 /* Correct end of highlighted area for 'breakindent',
3754 * required when 'linebreak' is also set. */
3755 if (tocol == vcol)
3756 tocol += n_extra;
3757 }
3758 }
3759#endif
3760
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3762 if (draw_state == WL_SBR - 1 && n_extra == 0)
3763 {
3764 draw_state = WL_SBR;
3765# ifdef FEAT_DIFF
3766 if (filler_todo > 0)
3767 {
3768 /* Draw "deleted" diff line(s). */
3769 if (char2cells(fill_diff) > 1)
3770 c_extra = '-';
3771 else
3772 c_extra = fill_diff;
3773# ifdef FEAT_RIGHTLEFT
3774 if (wp->w_p_rl)
3775 n_extra = col + 1;
3776 else
3777# endif
3778 n_extra = W_WIDTH(wp) - col;
3779 char_attr = hl_attr(HLF_DED);
3780 }
3781# endif
3782# ifdef FEAT_LINEBREAK
3783 if (*p_sbr != NUL && need_showbreak)
3784 {
3785 /* Draw 'showbreak' at the start of each broken line. */
3786 p_extra = p_sbr;
3787 c_extra = NUL;
3788 n_extra = (int)STRLEN(p_sbr);
3789 char_attr = hl_attr(HLF_AT);
3790 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01003791 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 /* Correct end of highlighted area for 'showbreak',
3793 * required when 'linebreak' is also set. */
3794 if (tocol == vcol)
3795 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003796#ifdef FEAT_SYN_HL
3797 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003798 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003799 char_attr = hl_combine_attr(char_attr,
3800 hl_attr(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003801#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 }
3803# endif
3804 }
3805#endif
3806
3807 if (draw_state == WL_LINE - 1 && n_extra == 0)
3808 {
3809 draw_state = WL_LINE;
3810 if (saved_n_extra)
3811 {
3812 /* Continue item from end of wrapped line. */
3813 n_extra = saved_n_extra;
3814 c_extra = saved_c_extra;
3815 p_extra = saved_p_extra;
3816 char_attr = saved_char_attr;
3817 }
3818 else
3819 char_attr = 0;
3820 }
3821 }
3822
3823 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01003824 if (dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003825 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826#ifdef FEAT_DIFF
3827 && filler_todo <= 0
3828#endif
3829 )
3830 {
3831 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3832 wp->w_p_rl);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003833 /* Pretend we have finished updating the window. Except when
3834 * 'cursorcolumn' is set. */
3835#ifdef FEAT_SYN_HL
3836 if (wp->w_p_cuc)
3837 row = wp->w_cline_row + wp->w_cline_height;
3838 else
3839#endif
3840 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 break;
3842 }
3843
3844 if (draw_state == WL_LINE && area_highlighting)
3845 {
3846 /* handle Visual or match highlighting in this line */
3847 if (vcol == fromcol
3848#ifdef FEAT_MBYTE
3849 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3850 && (*mb_ptr2cells)(ptr) > 1)
3851#endif
3852 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00003853 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 && vcol < tocol))
3855 area_attr = attr; /* start highlighting */
3856 else if (area_attr != 0
3857 && (vcol == tocol
3858 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860
3861#ifdef FEAT_SEARCH_EXTRA
3862 if (!n_extra)
3863 {
3864 /*
3865 * Check for start/end of search pattern match.
3866 * After end, check for start/end of next match.
3867 * When another match, have to check for start again.
3868 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003869 * Do this for 'search_hl' and the match list (ordered by
3870 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003872 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003873 cur = wp->w_match_head;
3874 shl_flag = FALSE;
3875 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003877 if (shl_flag == FALSE
3878 && ((cur != NULL
3879 && cur->priority > SEARCH_HL_PRIORITY)
3880 || cur == NULL))
3881 {
3882 shl = &search_hl;
3883 shl_flag = TRUE;
3884 }
3885 else
3886 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003887 if (cur != NULL)
3888 cur->pos.cur = 0;
3889 pos_inprogress = TRUE;
3890 while (shl->rm.regprog != NULL
3891 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003893 if (shl->startcol != MAXCOL
3894 && v >= (long)shl->startcol
3895 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01003897#ifdef FEAT_MBYTE
3898 int tmp_col = v + MB_PTR2LEN(ptr);
3899
3900 if (shl->endcol < tmp_col)
3901 shl->endcol = tmp_col;
3902#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 shl->attr_cur = shl->attr;
3904 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01003905 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 {
3907 shl->attr_cur = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003908 next_search_hl(wp, shl, lnum, (colnr_T)v, cur);
3909 pos_inprogress = cur == NULL || cur->pos.cur == 0
3910 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911
3912 /* Need to get the line again, a multi-line regexp
3913 * may have made it invalid. */
3914 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3915 ptr = line + v;
3916
3917 if (shl->lnum == lnum)
3918 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003919 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003921 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003923 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003925 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 {
3927 /* highlight empty match, try again after
3928 * it */
3929#ifdef FEAT_MBYTE
3930 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003931 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003932 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 else
3934#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003935 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 }
3937
3938 /* Loop to check if the match starts at the
3939 * current position */
3940 continue;
3941 }
3942 }
3943 break;
3944 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003945 if (shl != &search_hl && cur != NULL)
3946 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003948
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003949 /* Use attributes from match with highest priority among
3950 * 'search_hl' and the match list. */
3951 search_attr = search_hl.attr_cur;
3952 cur = wp->w_match_head;
3953 shl_flag = FALSE;
3954 while (cur != NULL || shl_flag == FALSE)
3955 {
3956 if (shl_flag == FALSE
3957 && ((cur != NULL
3958 && cur->priority > SEARCH_HL_PRIORITY)
3959 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003960 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003961 shl = &search_hl;
3962 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003963 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003964 else
3965 shl = &cur->hl;
3966 if (shl->attr_cur != 0)
3967 search_attr = shl->attr_cur;
3968 if (shl != &search_hl && cur != NULL)
3969 cur = cur->next;
3970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 }
3972#endif
3973
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003975 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003977 if (diff_hlf == HLF_CHD && ptr - line >= change_start
3978 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003980 if (diff_hlf == HLF_TXD && ptr - line > change_end
3981 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003983 line_attr = hl_attr(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02003984 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3985 line_attr = hl_combine_attr(line_attr, hl_attr(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 }
3987#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003988
3989 /* Decide which of the highlight attributes to use. */
3990 attr_pri = TRUE;
3991 if (area_attr != 0)
3992 char_attr = area_attr;
3993 else if (search_attr != 0)
3994 char_attr = search_attr;
3995#ifdef LINE_ATTR
3996 /* Use line_attr when not in the Visual or 'incsearch' area
3997 * (area_attr may be 0 when "noinvcur" is set). */
3998 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00003999 || vcol < fromcol || vcol_prev < fromcol_prev
4000 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004001 char_attr = line_attr;
4002#endif
4003 else
4004 {
4005 attr_pri = FALSE;
4006#ifdef FEAT_SYN_HL
4007 if (has_syntax)
4008 char_attr = syntax_attr;
4009 else
4010#endif
4011 char_attr = 0;
4012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 }
4014
4015 /*
4016 * Get the next character to put on the screen.
4017 */
4018 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004019 * The "p_extra" points to the extra stuff that is inserted to
4020 * represent special characters (non-printable stuff) and other
4021 * things. When all characters are the same, c_extra is used.
4022 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4023 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4025 */
4026 if (n_extra > 0)
4027 {
4028 if (c_extra != NUL)
4029 {
4030 c = c_extra;
4031#ifdef FEAT_MBYTE
4032 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
4033 if (enc_utf8 && (*mb_char2len)(c) > 1)
4034 {
4035 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004036 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004037 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 }
4039 else
4040 mb_utf8 = FALSE;
4041#endif
4042 }
4043 else
4044 {
4045 c = *p_extra;
4046#ifdef FEAT_MBYTE
4047 if (has_mbyte)
4048 {
4049 mb_c = c;
4050 if (enc_utf8)
4051 {
4052 /* If the UTF-8 character is more than one byte:
4053 * Decode it into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004054 mb_l = (*mb_ptr2len)(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 mb_utf8 = FALSE;
4056 if (mb_l > n_extra)
4057 mb_l = 1;
4058 else if (mb_l > 1)
4059 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004060 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004062 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 }
4064 }
4065 else
4066 {
4067 /* if this is a DBCS character, put it in "mb_c" */
4068 mb_l = MB_BYTE2LEN(c);
4069 if (mb_l >= n_extra)
4070 mb_l = 1;
4071 else if (mb_l > 1)
4072 mb_c = (c << 8) + p_extra[1];
4073 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004074 if (mb_l == 0) /* at the NUL at end-of-line */
4075 mb_l = 1;
4076
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 /* If a double-width char doesn't fit display a '>' in the
4078 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004079 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080# ifdef FEAT_RIGHTLEFT
4081 wp->w_p_rl ? (col <= 0) :
4082# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004083 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 && (*mb_char2cells)(mb_c) == 2)
4085 {
4086 c = '>';
4087 mb_c = c;
4088 mb_l = 1;
4089 mb_utf8 = FALSE;
4090 multi_attr = hl_attr(HLF_AT);
4091 /* put the pointer back to output the double-width
4092 * character at the start of the next line. */
4093 ++n_extra;
4094 --p_extra;
4095 }
4096 else
4097 {
4098 n_extra -= mb_l - 1;
4099 p_extra += mb_l - 1;
4100 }
4101 }
4102#endif
4103 ++p_extra;
4104 }
4105 --n_extra;
4106 }
4107 else
4108 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004109 if (p_extra_free != NULL)
4110 {
4111 vim_free(p_extra_free);
4112 p_extra_free = NULL;
4113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 /*
4115 * Get a character from the line itself.
4116 */
4117 c = *ptr;
4118#ifdef FEAT_MBYTE
4119 if (has_mbyte)
4120 {
4121 mb_c = c;
4122 if (enc_utf8)
4123 {
4124 /* If the UTF-8 character is more than one byte: Decode it
4125 * into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004126 mb_l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 mb_utf8 = FALSE;
4128 if (mb_l > 1)
4129 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004130 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 /* Overlong encoded ASCII or ASCII with composing char
4132 * is displayed normally, except a NUL. */
4133 if (mb_c < 0x80)
4134 c = mb_c;
4135 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004136
4137 /* At start of the line we can have a composing char.
4138 * Draw it as a space with a composing char. */
4139 if (utf_iscomposing(mb_c))
4140 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004141 int i;
4142
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004143 for (i = Screen_mco - 1; i > 0; --i)
4144 u8cc[i] = u8cc[i - 1];
4145 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004146 mb_c = ' ';
4147 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 }
4149
4150 if ((mb_l == 1 && c >= 0x80)
4151 || (mb_l >= 1 && mb_c == 0)
4152 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004153# ifdef UNICODE16
4154 || mb_c >= 0x10000
4155# endif
4156 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 {
4158 /*
4159 * Illegal UTF-8 byte: display as <xx>.
4160 * Non-BMP character : display as ? or fullwidth ?.
4161 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004162# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004164# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 {
4166 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004167# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 if (wp->w_p_rl) /* reverse */
4169 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004170# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004172# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 else if (utf_char2cells(mb_c) != 2)
4174 STRCPY(extra, "?");
4175 else
4176 /* 0xff1f in UTF-8: full-width '?' */
4177 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004178# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179
4180 p_extra = extra;
4181 c = *p_extra;
4182 mb_c = mb_ptr2char_adv(&p_extra);
4183 mb_utf8 = (c >= 0x80);
4184 n_extra = (int)STRLEN(p_extra);
4185 c_extra = NUL;
4186 if (area_attr == 0 && search_attr == 0)
4187 {
4188 n_attr = n_extra + 1;
4189 extra_attr = hl_attr(HLF_8);
4190 saved_attr2 = char_attr; /* save current attr */
4191 }
4192 }
4193 else if (mb_l == 0) /* at the NUL at end-of-line */
4194 mb_l = 1;
4195#ifdef FEAT_ARABIC
4196 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4197 {
4198 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004199 int pc, pc1, nc;
4200 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201
4202 /* The idea of what is the previous and next
4203 * character depends on 'rightleft'. */
4204 if (wp->w_p_rl)
4205 {
4206 pc = prev_c;
4207 pc1 = prev_c1;
4208 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004209 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 }
4211 else
4212 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004213 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004215 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 }
4217 prev_c = mb_c;
4218
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004219 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 }
4221 else
4222 prev_c = mb_c;
4223#endif
4224 }
4225 else /* enc_dbcs */
4226 {
4227 mb_l = MB_BYTE2LEN(c);
4228 if (mb_l == 0) /* at the NUL at end-of-line */
4229 mb_l = 1;
4230 else if (mb_l > 1)
4231 {
4232 /* We assume a second byte below 32 is illegal.
4233 * Hopefully this is OK for all double-byte encodings!
4234 */
4235 if (ptr[1] >= 32)
4236 mb_c = (c << 8) + ptr[1];
4237 else
4238 {
4239 if (ptr[1] == NUL)
4240 {
4241 /* head byte at end of line */
4242 mb_l = 1;
4243 transchar_nonprint(extra, c);
4244 }
4245 else
4246 {
4247 /* illegal tail byte */
4248 mb_l = 2;
4249 STRCPY(extra, "XX");
4250 }
4251 p_extra = extra;
4252 n_extra = (int)STRLEN(extra) - 1;
4253 c_extra = NUL;
4254 c = *p_extra++;
4255 if (area_attr == 0 && search_attr == 0)
4256 {
4257 n_attr = n_extra + 1;
4258 extra_attr = hl_attr(HLF_8);
4259 saved_attr2 = char_attr; /* save current attr */
4260 }
4261 mb_c = c;
4262 }
4263 }
4264 }
4265 /* If a double-width char doesn't fit display a '>' in the
4266 * last column; the character is displayed at the start of the
4267 * next line. */
4268 if ((
4269# ifdef FEAT_RIGHTLEFT
4270 wp->w_p_rl ? (col <= 0) :
4271# endif
4272 (col >= W_WIDTH(wp) - 1))
4273 && (*mb_char2cells)(mb_c) == 2)
4274 {
4275 c = '>';
4276 mb_c = c;
4277 mb_utf8 = FALSE;
4278 mb_l = 1;
4279 multi_attr = hl_attr(HLF_AT);
4280 /* Put pointer back so that the character will be
4281 * displayed at the start of the next line. */
4282 --ptr;
4283 }
4284 else if (*ptr != NUL)
4285 ptr += mb_l - 1;
4286
4287 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004288 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004289 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004290 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004293 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 c = ' ';
4295 if (area_attr == 0 && search_attr == 0)
4296 {
4297 n_attr = n_extra + 1;
4298 extra_attr = hl_attr(HLF_AT);
4299 saved_attr2 = char_attr; /* save current attr */
4300 }
4301 mb_c = c;
4302 mb_utf8 = FALSE;
4303 mb_l = 1;
4304 }
4305
4306 }
4307#endif
4308 ++ptr;
4309
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004310 /* 'list' : change char 160 to lcs_nbsp. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004311 if (wp->w_p_list && (c == 160
4312#ifdef FEAT_MBYTE
4313 || (mb_utf8 && mb_c == 160)
4314#endif
4315 ) && lcs_nbsp)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004316 {
4317 c = lcs_nbsp;
4318 if (area_attr == 0 && search_attr == 0)
4319 {
4320 n_attr = 1;
4321 extra_attr = hl_attr(HLF_8);
4322 saved_attr2 = char_attr; /* save current attr */
4323 }
4324#ifdef FEAT_MBYTE
4325 mb_c = c;
4326 if (enc_utf8 && (*mb_char2len)(c) > 1)
4327 {
4328 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004329 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004330 c = 0xc0;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004331 }
4332 else
4333 mb_utf8 = FALSE;
4334#endif
4335 }
4336
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 if (extra_check)
4338 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004339#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004340 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004341#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004342
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004343#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 /* Get syntax attribute, unless still at the start of the line
4345 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004346 v = (long)(ptr - line);
4347 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 {
4349 /* Get the syntax attribute for the character. If there
4350 * is an error, disable syntax highlighting. */
4351 save_did_emsg = did_emsg;
4352 did_emsg = FALSE;
4353
Bram Moolenaar217ad922005-03-20 22:37:15 +00004354 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004355# ifdef FEAT_SPELL
4356 has_spell ? &can_spell :
4357# endif
4358 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359
4360 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004361 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004362 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004363 has_syntax = FALSE;
4364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 else
4366 did_emsg = save_did_emsg;
4367
4368 /* Need to get the line again, a multi-line regexp may
4369 * have made it invalid. */
4370 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4371 ptr = line + v;
4372
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004373 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004375 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004376 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004377# ifdef FEAT_CONCEAL
4378 /* no concealing past the end of the line, it interferes
4379 * with line highlighting */
4380 if (c == NUL)
4381 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004382 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004383 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004384# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004386#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004387
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004388#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004389 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004390 * Only do this when there is no syntax highlighting, the
4391 * @Spell cluster is not used or the current syntax item
4392 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004393 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004394 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004395 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004396# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004397 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004398 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004399# endif
4400 if (c != 0 && (
4401# ifdef FEAT_SYN_HL
4402 !has_syntax ||
4403# endif
4404 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004405 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004406 char_u *prev_ptr, *p;
4407 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004408 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004409# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004410 if (has_mbyte)
4411 {
4412 prev_ptr = ptr - mb_l;
4413 v -= mb_l - 1;
4414 }
4415 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004416# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004417 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004418
4419 /* Use nextline[] if possible, it has the start of the
4420 * next line concatenated. */
4421 if ((prev_ptr - line) - nextlinecol >= 0)
4422 p = nextline + (prev_ptr - line) - nextlinecol;
4423 else
4424 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004425 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004426 len = spell_check(wp, p, &spell_hlf, &cap_col,
4427 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004428 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004429
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004430 /* In Insert mode only highlight a word that
4431 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004432 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004433 && (State & INSERT) != 0
4434 && wp->w_cursor.lnum == lnum
4435 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004436 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004437 && wp->w_cursor.col < (colnr_T)word_end)
4438 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004439 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004440 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004441 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004442
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004443 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004444 && (p - nextline) + len > nextline_idx)
4445 {
4446 /* Remember that the good word continues at the
4447 * start of the next line. */
4448 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004449 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004450 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004451
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004452 /* Turn index into actual attributes. */
4453 if (spell_hlf != HLF_COUNT)
4454 spell_attr = highlight_attr[spell_hlf];
4455
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004456 if (cap_col > 0)
4457 {
4458 if (p != prev_ptr
4459 && (p - nextline) + cap_col >= nextline_idx)
4460 {
4461 /* Remember that the word in the next line
4462 * must start with a capital. */
4463 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004464 cap_col = (int)((p - nextline) + cap_col
4465 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004466 }
4467 else
4468 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004469 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004470 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004471 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004472 }
4473 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004474 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004475 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004476 char_attr = hl_combine_attr(char_attr, spell_attr);
4477 else
4478 char_attr = hl_combine_attr(spell_attr, char_attr);
4479 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480#endif
4481#ifdef FEAT_LINEBREAK
4482 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004483 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004485 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 {
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004487# ifdef FEAT_MBYTE
4488 int off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
4489# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004490 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491# ifdef FEAT_MBYTE
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004492 off +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004494 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004495
Bram Moolenaar597a4222014-06-25 14:39:50 +02004496 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004497 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004498 NULL) - 1;
Bram Moolenaara3650912014-11-19 13:21:57 +01004499 if (c == TAB && n_extra + col > W_WIDTH(wp))
4500 n_extra = (int)wp->w_buffer->b_p_ts
4501 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4502
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004503# ifdef FEAT_MBYTE
4504 c_extra = off > 0 ? MB_FILLER_CHAR : ' ';
4505# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 c_extra = ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004507# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 if (vim_iswhite(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004509 {
4510#ifdef FEAT_CONCEAL
4511 if (c == TAB)
4512 /* See "Tab alignment" below. */
4513 FIX_FOR_BOGUSCOLS;
4514#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004515 if (!wp->w_p_list)
4516 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 }
4519#endif
4520
4521 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4522 {
4523 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004524 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 {
4526 n_attr = 1;
4527 extra_attr = hl_attr(HLF_8);
4528 saved_attr2 = char_attr; /* save current attr */
4529 }
4530#ifdef FEAT_MBYTE
4531 mb_c = c;
4532 if (enc_utf8 && (*mb_char2len)(c) > 1)
4533 {
4534 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004535 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004536 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 }
4538 else
4539 mb_utf8 = FALSE;
4540#endif
4541 }
4542 }
4543
4544 /*
4545 * Handling of non-printable characters.
4546 */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004547 if (!(chartab[c & 0xff] & CT_PRINT_CHAR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 {
4549 /*
4550 * when getting a character from the file, we may have to
4551 * turn it into something else on the way to putting it
4552 * into "ScreenLines".
4553 */
4554 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4555 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004556 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004557 long vcol_adjusted = vcol; /* removed showbreak length */
4558#ifdef FEAT_LINEBREAK
4559 /* only adjust the tab_len, when at the first column
4560 * after the showbreak value was drawn */
4561 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4562 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4563#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 /* tab amount depends on current column */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004565 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaard574ea22015-01-14 19:35:14 +01004566 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4567
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004568#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004569 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004570#endif
4571 /* tab amount depends on current column */
4572 n_extra = tab_len;
4573#ifdef FEAT_LINEBREAK
4574 else
4575 {
4576 char_u *p;
4577 int len = n_extra;
4578 int i;
4579 int saved_nextra = n_extra;
4580
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004581#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004582 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004583 /* there are characters to conceal */
4584 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004585 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
4586 */
4587 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4588 && n_extra > tab_len)
4589 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004590#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004591
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004592 /* if n_extra > 0, it gives the number of chars, to
4593 * use for a tab, else we need to calculate the width
4594 * for a tab */
4595#ifdef FEAT_MBYTE
4596 len = (tab_len * mb_char2len(lcs_tab2));
4597 if (n_extra > 0)
4598 len += n_extra - tab_len;
4599#endif
4600 c = lcs_tab1;
4601 p = alloc((unsigned)(len + 1));
4602 vim_memset(p, ' ', len);
4603 p[len] = NUL;
4604 p_extra_free = p;
4605 for (i = 0; i < tab_len; i++)
4606 {
4607#ifdef FEAT_MBYTE
4608 mb_char2bytes(lcs_tab2, p);
4609 p += mb_char2len(lcs_tab2);
4610 n_extra += mb_char2len(lcs_tab2)
4611 - (saved_nextra > 0 ? 1 : 0);
4612#else
4613 p[i] = lcs_tab2;
4614#endif
4615 }
4616 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004617#ifdef FEAT_CONCEAL
4618 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
4619 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004620 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004621 n_extra -= vcol_off;
4622#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004623 }
4624#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004625#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004626 {
4627 int vc_saved = vcol_off;
4628
4629 /* Tab alignment should be identical regardless of
4630 * 'conceallevel' value. So tab compensates of all
4631 * previous concealed characters, and thus resets
4632 * vcol_off and boguscols accumulated so far in the
4633 * line. Note that the tab can be longer than
4634 * 'tabstop' when there are concealed characters. */
4635 FIX_FOR_BOGUSCOLS;
4636
4637 /* Make sure, the highlighting for the tab char will be
4638 * correctly set further below (effectively reverts the
4639 * FIX_FOR_BOGSUCOLS macro */
4640 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004641 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004642 tab_len += vc_saved;
4643 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004644#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645#ifdef FEAT_MBYTE
4646 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4647#endif
4648 if (wp->w_p_list)
4649 {
4650 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004651#ifdef FEAT_LINEBREAK
4652 if (wp->w_p_lbr)
4653 c_extra = NUL; /* using p_extra from above */
4654 else
4655#endif
4656 c_extra = lcs_tab2;
4657 n_attr = tab_len + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 extra_attr = hl_attr(HLF_8);
4659 saved_attr2 = char_attr; /* save current attr */
4660#ifdef FEAT_MBYTE
4661 mb_c = c;
4662 if (enc_utf8 && (*mb_char2len)(c) > 1)
4663 {
4664 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004665 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004666 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 }
4668#endif
4669 }
4670 else
4671 {
4672 c_extra = ' ';
4673 c = ' ';
4674 }
4675 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004676 else if (c == NUL
4677 && ((wp->w_p_list && lcs_eol > 0)
4678 || ((fromcol >= 0 || fromcol_prev >= 0)
4679 && tocol > vcol
4680 && VIsual_mode != Ctrl_V
4681 && (
4682# ifdef FEAT_RIGHTLEFT
4683 wp->w_p_rl ? (col >= 0) :
4684# endif
4685 (col < W_WIDTH(wp)))
4686 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004687 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004688 && (colnr_T)vcol == wp->w_virtcol)))
4689 && lcs_eol_one >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004691 /* Display a '$' after the line or highlight an extra
4692 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4694 /* For a diff line the highlighting continues after the
4695 * "$". */
4696 if (
4697# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004698 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699# ifdef LINE_ATTR
4700 &&
4701# endif
4702# endif
4703# ifdef LINE_ATTR
4704 line_attr == 0
4705# endif
4706 )
4707#endif
4708 {
4709#ifdef FEAT_VIRTUALEDIT
4710 /* In virtualedit, visual selections may extend
4711 * beyond end of line. */
4712 if (area_highlighting && virtual_active()
4713 && tocol != MAXCOL && vcol < tocol)
4714 n_extra = 0;
4715 else
4716#endif
4717 {
4718 p_extra = at_end_str;
4719 n_extra = 1;
4720 c_extra = NUL;
4721 }
4722 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004723 if (wp->w_p_list)
4724 c = lcs_eol;
4725 else
4726 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 lcs_eol_one = -1;
4728 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004729 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 {
4731 extra_attr = hl_attr(HLF_AT);
4732 n_attr = 1;
4733 }
4734#ifdef FEAT_MBYTE
4735 mb_c = c;
4736 if (enc_utf8 && (*mb_char2len)(c) > 1)
4737 {
4738 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004739 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004740 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 }
4742 else
4743 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4744#endif
4745 }
4746 else if (c != NUL)
4747 {
4748 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02004749 if (n_extra == 0)
4750 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751#ifdef FEAT_RIGHTLEFT
4752 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4753 rl_mirror(p_extra); /* reverse "<12>" */
4754#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004756#ifdef FEAT_LINEBREAK
4757 if (wp->w_p_lbr)
4758 {
4759 char_u *p;
4760
4761 c = *p_extra;
4762 p = alloc((unsigned)n_extra + 1);
4763 vim_memset(p, ' ', n_extra);
4764 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
4765 p[n_extra] = NUL;
4766 p_extra_free = p_extra = p;
4767 }
4768 else
4769#endif
4770 {
4771 n_extra = byte2cells(c) - 1;
4772 c = *p_extra++;
4773 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004774 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 {
4776 n_attr = n_extra + 1;
4777 extra_attr = hl_attr(HLF_8);
4778 saved_attr2 = char_attr; /* save current attr */
4779 }
4780#ifdef FEAT_MBYTE
4781 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4782#endif
4783 }
4784#ifdef FEAT_VIRTUALEDIT
4785 else if (VIsual_active
4786 && (VIsual_mode == Ctrl_V
4787 || VIsual_mode == 'v')
4788 && virtual_active()
4789 && tocol != MAXCOL
4790 && vcol < tocol
4791 && (
4792# ifdef FEAT_RIGHTLEFT
4793 wp->w_p_rl ? (col >= 0) :
4794# endif
4795 (col < W_WIDTH(wp))))
4796 {
4797 c = ' ';
4798 --ptr; /* put it back at the NUL */
4799 }
4800#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004801#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 else if ((
4803# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004804 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 ) && (
4808# ifdef FEAT_RIGHTLEFT
4809 wp->w_p_rl ? (col >= 0) :
4810# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02004811 (col
4812# ifdef FEAT_CONCEAL
4813 - boguscols
4814# endif
4815 < W_WIDTH(wp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 {
4817 /* Highlight until the right side of the window */
4818 c = ' ';
4819 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004820
4821 /* Remember we do the char for line highlighting. */
4822 ++did_line_attr;
4823
4824 /* don't do search HL for the rest of the line */
4825 if (line_attr != 0 && char_attr == search_attr && col > 0)
4826 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827# ifdef FEAT_DIFF
4828 if (diff_hlf == HLF_TXD)
4829 {
4830 diff_hlf = HLF_CHD;
4831 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004832 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 char_attr = hl_attr(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004834 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4835 char_attr = hl_combine_attr(char_attr,
4836 hl_attr(HLF_CUL));
4837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 }
4839# endif
4840 }
4841#endif
4842 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02004843
4844#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004845 if ( wp->w_p_cole > 0
4846 && (wp != curwin || lnum != wp->w_cursor.lnum ||
4847 conceal_cursor_line(wp))
Bram Moolenaarf70e3d62010-07-24 13:15:07 +02004848 && (syntax_flags & HL_CONCEAL) != 0
Bram Moolenaare6dc5732010-07-24 23:52:26 +02004849 && !(lnum_in_visual_area
4850 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02004851 {
4852 char_attr = conceal_attr;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004853 if (prev_syntax_id != syntax_seqnr
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004854 && (syn_get_sub_char() != NUL || wp->w_p_cole == 1)
4855 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004856 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004857 /* First time at this concealed item: display one
4858 * character. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004859 if (syn_get_sub_char() != NUL)
4860 c = syn_get_sub_char();
4861 else if (lcs_conceal != NUL)
4862 c = lcs_conceal;
4863 else
4864 c = ' ';
4865
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004866 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004867
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004868 if (n_extra > 0)
4869 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004870 vcol += n_extra;
4871 if (wp->w_p_wrap && n_extra > 0)
4872 {
4873# ifdef FEAT_RIGHTLEFT
4874 if (wp->w_p_rl)
4875 {
4876 col -= n_extra;
4877 boguscols -= n_extra;
4878 }
4879 else
4880# endif
4881 {
4882 boguscols += n_extra;
4883 col += n_extra;
4884 }
4885 }
4886 n_extra = 0;
4887 n_attr = 0;
4888 }
4889 else if (n_skip == 0)
4890 {
4891 is_concealing = TRUE;
4892 n_skip = 1;
4893 }
4894# ifdef FEAT_MBYTE
4895 mb_c = c;
4896 if (enc_utf8 && (*mb_char2len)(c) > 1)
4897 {
4898 mb_utf8 = TRUE;
4899 u8cc[0] = 0;
4900 c = 0xc0;
4901 }
4902 else
4903 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4904# endif
4905 }
4906 else
4907 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004908 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02004909 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004910 }
4911#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 }
4913
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004914#ifdef FEAT_CONCEAL
4915 /* In the cursor line and we may be concealing characters: correct
4916 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02004917 if (!did_wcol && draw_state == WL_LINE
4918 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004919 && conceal_cursor_line(wp)
4920 && (int)wp->w_virtcol <= vcol + n_skip)
4921 {
4922 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02004923 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004924 did_wcol = TRUE;
4925 }
4926#endif
4927
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 /* Don't override visual selection highlighting. */
4929 if (n_attr > 0
4930 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004931 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 char_attr = extra_attr;
4933
Bram Moolenaar81695252004-12-29 20:58:21 +00004934#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 /* XIM don't send preedit_start and preedit_end, but they send
4936 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
4937 * im_is_preediting() here. */
4938 if (xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004939 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 && (State & INSERT)
4941 && !p_imdisable
4942 && im_is_preediting()
4943 && draw_state == WL_LINE)
4944 {
4945 colnr_T tcol;
4946
4947 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004948 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 else
4950 tcol = preedit_end_col;
4951 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
4952 {
4953 if (feedback_old_attr < 0)
4954 {
4955 feedback_col = 0;
4956 feedback_old_attr = char_attr;
4957 }
4958 char_attr = im_get_feedback_attr(feedback_col);
4959 if (char_attr < 0)
4960 char_attr = feedback_old_attr;
4961 feedback_col++;
4962 }
4963 else if (feedback_old_attr >= 0)
4964 {
4965 char_attr = feedback_old_attr;
4966 feedback_old_attr = -1;
4967 feedback_col = 0;
4968 }
4969 }
4970#endif
4971 /*
4972 * Handle the case where we are in column 0 but not on the first
4973 * character of the line and the user wants us to show us a
4974 * special character (via 'listchars' option "precedes:<char>".
4975 */
4976 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02004977 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
4979#ifdef FEAT_DIFF
4980 && filler_todo <= 0
4981#endif
4982 && draw_state > WL_NR
4983 && c != NUL)
4984 {
4985 c = lcs_prec;
4986 lcs_prec_todo = NUL;
4987#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02004988 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
4989 {
4990 /* Double-width character being overwritten by the "precedes"
4991 * character, need to fill up half the character. */
4992 c_extra = MB_FILLER_CHAR;
4993 n_extra = 1;
4994 n_attr = 2;
4995 extra_attr = hl_attr(HLF_AT);
4996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 mb_c = c;
4998 if (enc_utf8 && (*mb_char2len)(c) > 1)
4999 {
5000 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005001 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005002 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 }
5004 else
5005 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5006#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005007 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 {
5009 saved_attr3 = char_attr; /* save current attr */
5010 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
5011 n_attr3 = 1;
5012 }
5013 }
5014
5015 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005016 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005018 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005019#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005020 || did_line_attr == 1
5021#endif
5022 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005024#ifdef FEAT_SEARCH_EXTRA
5025 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005026
5027 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005028 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005029 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005030#endif
5031
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005032 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 * highlight match at end of line. If it's beyond the last
5034 * char on the screen, just overwrite that one (tricky!) Not
5035 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005036#ifdef FEAT_SEARCH_EXTRA
5037 prevcol_hl_flag = FALSE;
5038 if (prevcol == (long)search_hl.startcol)
5039 prevcol_hl_flag = TRUE;
5040 else
5041 {
5042 cur = wp->w_match_head;
5043 while (cur != NULL)
5044 {
5045 if (prevcol == (long)cur->hl.startcol)
5046 {
5047 prevcol_hl_flag = TRUE;
5048 break;
5049 }
5050 cur = cur->next;
5051 }
5052 }
5053#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005055 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005056 && (VIsual_mode != Ctrl_V
5057 || lnum == VIsual.lnum
5058 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005059 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060#ifdef FEAT_SEARCH_EXTRA
5061 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005062 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005063# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005064 && did_line_attr <= 1
5065# endif
5066 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067#endif
5068 ))
5069 {
5070 int n = 0;
5071
5072#ifdef FEAT_RIGHTLEFT
5073 if (wp->w_p_rl)
5074 {
5075 if (col < 0)
5076 n = 1;
5077 }
5078 else
5079#endif
5080 {
5081 if (col >= W_WIDTH(wp))
5082 n = -1;
5083 }
5084 if (n != 0)
5085 {
5086 /* At the window boundary, highlight the last character
5087 * instead (better than nothing). */
5088 off += n;
5089 col += n;
5090 }
5091 else
5092 {
5093 /* Add a blank character to highlight. */
5094 ScreenLines[off] = ' ';
5095#ifdef FEAT_MBYTE
5096 if (enc_utf8)
5097 ScreenLinesUC[off] = 0;
5098#endif
5099 }
5100#ifdef FEAT_SEARCH_EXTRA
5101 if (area_attr == 0)
5102 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005103 /* Use attributes from match with highest priority among
5104 * 'search_hl' and the match list. */
5105 char_attr = search_hl.attr;
5106 cur = wp->w_match_head;
5107 shl_flag = FALSE;
5108 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005109 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005110 if (shl_flag == FALSE
5111 && ((cur != NULL
5112 && cur->priority > SEARCH_HL_PRIORITY)
5113 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005114 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005115 shl = &search_hl;
5116 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005117 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005118 else
5119 shl = &cur->hl;
5120 if ((ptr - line) - 1 == (long)shl->startcol)
5121 char_attr = shl->attr;
5122 if (shl != &search_hl && cur != NULL)
5123 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005124 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 }
5126#endif
5127 ScreenAttrs[off] = char_attr;
5128#ifdef FEAT_RIGHTLEFT
5129 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005130 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005132 --off;
5133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 else
5135#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005136 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005138 ++off;
5139 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005140 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005141#ifdef FEAT_SYN_HL
5142 eol_hl_off = 1;
5143#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146
Bram Moolenaar91170f82006-05-05 21:15:17 +00005147 /*
5148 * At end of the text line.
5149 */
5150 if (c == NUL)
5151 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005152#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005153 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
5154 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00005155 {
5156 /* highlight last char after line */
5157 --col;
5158 --off;
5159 --vcol;
5160 }
5161
Bram Moolenaar1a384422010-07-14 19:53:30 +02005162 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005163 if (wp->w_p_wrap)
5164 v = wp->w_skipcol;
5165 else
5166 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005167
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005168 /* check if line ends before left margin */
5169 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005170 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005171#ifdef FEAT_CONCEAL
5172 /* Get rid of the boguscols now, we want to draw until the right
5173 * edge for 'cursorcolumn'. */
5174 col -= boguscols;
5175 boguscols = 0;
5176#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005177
5178 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005179 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005180
5181 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005182 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5183 && (int)wp->w_virtcol <
5184 W_WIDTH(wp) * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005185 && lnum != wp->w_cursor.lnum)
5186 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005187# ifdef FEAT_RIGHTLEFT
5188 && !wp->w_p_rl
5189# endif
5190 )
5191 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005192 int rightmost_vcol = 0;
5193 int i;
5194
5195 if (wp->w_p_cuc)
5196 rightmost_vcol = wp->w_virtcol;
5197 if (draw_color_col)
5198 /* determine rightmost colorcolumn to possibly draw */
5199 for (i = 0; color_cols[i] >= 0; ++i)
5200 if (rightmost_vcol < color_cols[i])
5201 rightmost_vcol = color_cols[i];
5202
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005203 while (col < W_WIDTH(wp))
5204 {
5205 ScreenLines[off] = ' ';
5206#ifdef FEAT_MBYTE
5207 if (enc_utf8)
5208 ScreenLinesUC[off] = 0;
5209#endif
5210 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005211 if (draw_color_col)
5212 draw_color_col = advance_color_col(VCOL_HLC,
5213 &color_cols);
5214
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005215 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005216 ScreenAttrs[off++] = hl_attr(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005217 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005218 ScreenAttrs[off++] = hl_attr(HLF_MC);
5219 else
5220 ScreenAttrs[off++] = 0;
5221
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005222 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005223 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005224
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005225 ++vcol;
5226 }
5227 }
5228#endif
5229
Bram Moolenaar860cae12010-06-05 23:22:07 +02005230 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5231 (int)W_WIDTH(wp), wp->w_p_rl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 row++;
5233
5234 /*
5235 * Update w_cline_height and w_cline_folded if the cursor line was
5236 * updated (saves a call to plines() later).
5237 */
5238 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5239 {
5240 curwin->w_cline_row = startrow;
5241 curwin->w_cline_height = row - startrow;
5242#ifdef FEAT_FOLDING
5243 curwin->w_cline_folded = FALSE;
5244#endif
5245 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5246 }
5247
5248 break;
5249 }
5250
5251 /* line continues beyond line end */
5252 if (lcs_ext
5253 && !wp->w_p_wrap
5254#ifdef FEAT_DIFF
5255 && filler_todo <= 0
5256#endif
5257 && (
5258#ifdef FEAT_RIGHTLEFT
5259 wp->w_p_rl ? col == 0 :
5260#endif
5261 col == W_WIDTH(wp) - 1)
5262 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005263 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5265 {
5266 c = lcs_ext;
5267 char_attr = hl_attr(HLF_AT);
5268#ifdef FEAT_MBYTE
5269 mb_c = c;
5270 if (enc_utf8 && (*mb_char2len)(c) > 1)
5271 {
5272 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005273 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005274 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 }
5276 else
5277 mb_utf8 = FALSE;
5278#endif
5279 }
5280
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005281#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005282 /* advance to the next 'colorcolumn' */
5283 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005284 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005285
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005286 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005287 * highlight the cursor position itself.
5288 * Also highlight the 'colorcolumn' if it is different than
5289 * 'cursorcolumn' */
5290 vcol_save_attr = -1;
5291 if (draw_state == WL_LINE && !lnum_in_visual_area)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005292 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005293 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005294 && lnum != wp->w_cursor.lnum)
5295 {
5296 vcol_save_attr = char_attr;
5297 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
5298 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005299 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005300 {
5301 vcol_save_attr = char_attr;
5302 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_MC));
5303 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005304 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005305#endif
5306
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 /*
5308 * Store character to be displayed.
5309 * Skip characters that are left of the screen for 'nowrap'.
5310 */
5311 vcol_prev = vcol;
5312 if (draw_state < WL_LINE || n_skip <= 0)
5313 {
5314 /*
5315 * Store the character.
5316 */
5317#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5318 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5319 {
5320 /* A double-wide character is: put first halve in left cell. */
5321 --off;
5322 --col;
5323 }
5324#endif
5325 ScreenLines[off] = c;
5326#ifdef FEAT_MBYTE
5327 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005328 {
5329 if ((mb_c & 0xff00) == 0x8e00)
5330 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 else if (enc_utf8)
5334 {
5335 if (mb_utf8)
5336 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005337 int i;
5338
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005340 if ((c & 0xff) == 0)
5341 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005342 for (i = 0; i < Screen_mco; ++i)
5343 {
5344 ScreenLinesC[i][off] = u8cc[i];
5345 if (u8cc[i] == 0)
5346 break;
5347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 }
5349 else
5350 ScreenLinesUC[off] = 0;
5351 }
5352 if (multi_attr)
5353 {
5354 ScreenAttrs[off] = multi_attr;
5355 multi_attr = 0;
5356 }
5357 else
5358#endif
5359 ScreenAttrs[off] = char_attr;
5360
5361#ifdef FEAT_MBYTE
5362 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5363 {
5364 /* Need to fill two screen columns. */
5365 ++off;
5366 ++col;
5367 if (enc_utf8)
5368 /* UTF-8: Put a 0 in the second screen char. */
5369 ScreenLines[off] = 0;
5370 else
5371 /* DBCS: Put second byte in the second screen char. */
5372 ScreenLines[off] = mb_c & 0xff;
5373 ++vcol;
5374 /* When "tocol" is halfway a character, set it to the end of
5375 * the character, otherwise highlighting won't stop. */
5376 if (tocol == vcol)
5377 ++tocol;
5378#ifdef FEAT_RIGHTLEFT
5379 if (wp->w_p_rl)
5380 {
5381 /* now it's time to backup one cell */
5382 --off;
5383 --col;
5384 }
5385#endif
5386 }
5387#endif
5388#ifdef FEAT_RIGHTLEFT
5389 if (wp->w_p_rl)
5390 {
5391 --off;
5392 --col;
5393 }
5394 else
5395#endif
5396 {
5397 ++off;
5398 ++col;
5399 }
5400 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005401#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005402 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005403 {
5404 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005405 ++vcol_off;
5406 if (n_extra > 0)
5407 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005408 if (wp->w_p_wrap)
5409 {
5410 /*
5411 * Special voodoo required if 'wrap' is on.
5412 *
5413 * Advance the column indicator to force the line
5414 * drawing to wrap early. This will make the line
5415 * take up the same screen space when parts are concealed,
5416 * so that cursor line computations aren't messed up.
5417 *
5418 * To avoid the fictitious advance of 'col' causing
5419 * trailing junk to be written out of the screen line
5420 * we are building, 'boguscols' keeps track of the number
5421 * of bad columns we have advanced.
5422 */
5423 if (n_extra > 0)
5424 {
5425 vcol += n_extra;
5426# ifdef FEAT_RIGHTLEFT
5427 if (wp->w_p_rl)
5428 {
5429 col -= n_extra;
5430 boguscols -= n_extra;
5431 }
5432 else
5433# endif
5434 {
5435 col += n_extra;
5436 boguscols += n_extra;
5437 }
5438 n_extra = 0;
5439 n_attr = 0;
5440 }
5441
5442
5443# ifdef FEAT_MBYTE
5444 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5445 {
5446 /* Need to fill two screen columns. */
5447# ifdef FEAT_RIGHTLEFT
5448 if (wp->w_p_rl)
5449 {
5450 --boguscols;
5451 --col;
5452 }
5453 else
5454# endif
5455 {
5456 ++boguscols;
5457 ++col;
5458 }
5459 }
5460# endif
5461
5462# ifdef FEAT_RIGHTLEFT
5463 if (wp->w_p_rl)
5464 {
5465 --boguscols;
5466 --col;
5467 }
5468 else
5469# endif
5470 {
5471 ++boguscols;
5472 ++col;
5473 }
5474 }
5475 else
5476 {
5477 if (n_extra > 0)
5478 {
5479 vcol += n_extra;
5480 n_extra = 0;
5481 n_attr = 0;
5482 }
5483 }
5484
5485 }
5486#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487 else
5488 --n_skip;
5489
Bram Moolenaar64486672010-05-16 15:46:46 +02005490 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5491 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005492 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493#ifdef FEAT_DIFF
5494 && filler_todo <= 0
5495#endif
5496 )
5497 ++vcol;
5498
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005499#ifdef FEAT_SYN_HL
5500 if (vcol_save_attr >= 0)
5501 char_attr = vcol_save_attr;
5502#endif
5503
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 /* restore attributes after "predeces" in 'listchars' */
5505 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5506 char_attr = saved_attr3;
5507
5508 /* restore attributes after last 'listchars' or 'number' char */
5509 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5510 char_attr = saved_attr2;
5511
5512 /*
5513 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005514 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 */
5516 if ((
5517#ifdef FEAT_RIGHTLEFT
5518 wp->w_p_rl ? (col < 0) :
5519#endif
5520 (col >= W_WIDTH(wp)))
5521 && (*ptr != NUL
5522#ifdef FEAT_DIFF
5523 || filler_todo > 0
5524#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005525 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5527 )
5528 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005529#ifdef FEAT_CONCEAL
5530 SCREEN_LINE(screen_row, W_WINCOL(wp), col - boguscols,
5531 (int)W_WIDTH(wp), wp->w_p_rl);
5532 boguscols = 0;
5533#else
5534 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5535 (int)W_WIDTH(wp), wp->w_p_rl);
5536#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 ++row;
5538 ++screen_row;
5539
5540 /* When not wrapping and finished diff lines, or when displayed
5541 * '$' and highlighting until last column, break here. */
5542 if ((!wp->w_p_wrap
5543#ifdef FEAT_DIFF
5544 && filler_todo <= 0
5545#endif
5546 ) || lcs_eol_one == -1)
5547 break;
5548
5549 /* When the window is too narrow draw all "@" lines. */
5550 if (draw_state != WL_LINE
5551#ifdef FEAT_DIFF
5552 && filler_todo <= 0
5553#endif
5554 )
5555 {
5556 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
5557#ifdef FEAT_VERTSPLIT
5558 draw_vsep_win(wp, row);
5559#endif
5560 row = endrow;
5561 }
5562
5563 /* When line got too long for screen break here. */
5564 if (row == endrow)
5565 {
5566 ++row;
5567 break;
5568 }
5569
5570 if (screen_cur_row == screen_row - 1
5571#ifdef FEAT_DIFF
5572 && filler_todo <= 0
5573#endif
5574 && W_WIDTH(wp) == Columns)
5575 {
5576 /* Remember that the line wraps, used for modeless copy. */
5577 LineWraps[screen_row - 1] = TRUE;
5578
5579 /*
5580 * Special trick to make copy/paste of wrapped lines work with
5581 * xterm/screen: write an extra character beyond the end of
5582 * the line. This will work with all terminal types
5583 * (regardless of the xn,am settings).
5584 * Only do this on a fast tty.
5585 * Only do this if the cursor is on the current line
5586 * (something has been written in it).
5587 * Don't do this for the GUI.
5588 * Don't do this for double-width characters.
5589 * Don't do this for a window not at the right screen border.
5590 */
5591 if (p_tf
5592#ifdef FEAT_GUI
5593 && !gui.in_use
5594#endif
5595#ifdef FEAT_MBYTE
5596 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005597 && ((*mb_off2cells)(LineOffset[screen_row],
5598 LineOffset[screen_row] + screen_Columns)
5599 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005601 + (int)Columns - 2,
5602 LineOffset[screen_row] + screen_Columns)
5603 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604#endif
5605 )
5606 {
5607 /* First make sure we are at the end of the screen line,
5608 * then output the same character again to let the
5609 * terminal know about the wrap. If the terminal doesn't
5610 * auto-wrap, we overwrite the character. */
5611 if (screen_cur_col != W_WIDTH(wp))
5612 screen_char(LineOffset[screen_row - 1]
5613 + (unsigned)Columns - 1,
5614 screen_row - 1, (int)(Columns - 1));
5615
5616#ifdef FEAT_MBYTE
5617 /* When there is a multi-byte character, just output a
5618 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005619 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5620 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 out_char(' ');
5622 else
5623#endif
5624 out_char(ScreenLines[LineOffset[screen_row - 1]
5625 + (Columns - 1)]);
5626 /* force a redraw of the first char on the next line */
5627 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5628 screen_start(); /* don't know where cursor is now */
5629 }
5630 }
5631
5632 col = 0;
5633 off = (unsigned)(current_ScreenLine - ScreenLines);
5634#ifdef FEAT_RIGHTLEFT
5635 if (wp->w_p_rl)
5636 {
5637 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
5638 off += col;
5639 }
5640#endif
5641
5642 /* reset the drawing state for the start of a wrapped line */
5643 draw_state = WL_START;
5644 saved_n_extra = n_extra;
5645 saved_p_extra = p_extra;
5646 saved_c_extra = c_extra;
5647 saved_char_attr = char_attr;
5648 n_extra = 0;
5649 lcs_prec_todo = lcs_prec;
5650#ifdef FEAT_LINEBREAK
5651# ifdef FEAT_DIFF
5652 if (filler_todo <= 0)
5653# endif
5654 need_showbreak = TRUE;
5655#endif
5656#ifdef FEAT_DIFF
5657 --filler_todo;
5658 /* When the filler lines are actually below the last line of the
5659 * file, don't draw the line itself, break here. */
5660 if (filler_todo == 0 && wp->w_botfill)
5661 break;
5662#endif
5663 }
5664
5665 } /* for every character in the line */
5666
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005667#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005668 /* After an empty line check first word for capital. */
5669 if (*skipwhite(line) == NUL)
5670 {
5671 capcol_lnum = lnum + 1;
5672 cap_col = 0;
5673 }
5674#endif
5675
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 return row;
5677}
5678
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005679#ifdef FEAT_MBYTE
5680static int comp_char_differs __ARGS((int, int));
5681
5682/*
5683 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005684 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005685 */
5686 static int
5687comp_char_differs(off_from, off_to)
5688 int off_from;
5689 int off_to;
5690{
5691 int i;
5692
5693 for (i = 0; i < Screen_mco; ++i)
5694 {
5695 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5696 return TRUE;
5697 if (ScreenLinesC[i][off_from] == 0)
5698 break;
5699 }
5700 return FALSE;
5701}
5702#endif
5703
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704/*
5705 * Check whether the given character needs redrawing:
5706 * - the (first byte of the) character is different
5707 * - the attributes are different
5708 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005709 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 */
5711 static int
5712char_needs_redraw(off_from, off_to, cols)
5713 int off_from;
5714 int off_to;
5715 int cols;
5716{
5717 if (cols > 0
5718 && ((ScreenLines[off_from] != ScreenLines[off_to]
5719 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
5720
5721#ifdef FEAT_MBYTE
5722 || (enc_dbcs != 0
5723 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
5724 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
5725 ? ScreenLines2[off_from] != ScreenLines2[off_to]
5726 : (cols > 1 && ScreenLines[off_from + 1]
5727 != ScreenLines[off_to + 1])))
5728 || (enc_utf8
5729 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
5730 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005731 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02005732 || ((*mb_off2cells)(off_from, off_from + cols) > 1
5733 && ScreenLines[off_from + 1]
5734 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735#endif
5736 ))
5737 return TRUE;
5738 return FALSE;
5739}
5740
5741/*
5742 * Move one "cooked" screen line to the screen, but only the characters that
5743 * have actually changed. Handle insert/delete character.
5744 * "coloff" gives the first column on the screen for this line.
5745 * "endcol" gives the columns where valid characters are.
5746 * "clear_width" is the width of the window. It's > 0 if the rest of the line
5747 * needs to be cleared, negative otherwise.
5748 * "rlflag" is TRUE in a rightleft window:
5749 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
5750 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
5751 */
5752 static void
5753screen_line(row, coloff, endcol, clear_width
5754#ifdef FEAT_RIGHTLEFT
5755 , rlflag
5756#endif
5757 )
5758 int row;
5759 int coloff;
5760 int endcol;
5761 int clear_width;
5762#ifdef FEAT_RIGHTLEFT
5763 int rlflag;
5764#endif
5765{
5766 unsigned off_from;
5767 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005768#ifdef FEAT_MBYTE
5769 unsigned max_off_from;
5770 unsigned max_off_to;
5771#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 int col = 0;
5773#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_VERTSPLIT)
5774 int hl;
5775#endif
5776 int force = FALSE; /* force update rest of the line */
5777 int redraw_this /* bool: does character need redraw? */
5778#ifdef FEAT_GUI
5779 = TRUE /* For GUI when while-loop empty */
5780#endif
5781 ;
5782 int redraw_next; /* redraw_this for next character */
5783#ifdef FEAT_MBYTE
5784 int clear_next = FALSE;
5785 int char_cells; /* 1: normal char */
5786 /* 2: occupies two display cells */
5787# define CHAR_CELLS char_cells
5788#else
5789# define CHAR_CELLS 1
5790#endif
5791
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01005792 /* Check for illegal row and col, just in case. */
5793 if (row >= Rows)
5794 row = Rows - 1;
5795 if (endcol > Columns)
5796 endcol = Columns;
5797
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798# ifdef FEAT_CLIPBOARD
5799 clip_may_clear_selection(row, row);
5800# endif
5801
5802 off_from = (unsigned)(current_ScreenLine - ScreenLines);
5803 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005804#ifdef FEAT_MBYTE
5805 max_off_from = off_from + screen_Columns;
5806 max_off_to = LineOffset[row] + screen_Columns;
5807#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808
5809#ifdef FEAT_RIGHTLEFT
5810 if (rlflag)
5811 {
5812 /* Clear rest first, because it's left of the text. */
5813 if (clear_width > 0)
5814 {
5815 while (col <= endcol && ScreenLines[off_to] == ' '
5816 && ScreenAttrs[off_to] == 0
5817# ifdef FEAT_MBYTE
5818 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5819# endif
5820 )
5821 {
5822 ++off_to;
5823 ++col;
5824 }
5825 if (col <= endcol)
5826 screen_fill(row, row + 1, col + coloff,
5827 endcol + coloff + 1, ' ', ' ', 0);
5828 }
5829 col = endcol + 1;
5830 off_to = LineOffset[row] + col + coloff;
5831 off_from += col;
5832 endcol = (clear_width > 0 ? clear_width : -clear_width);
5833 }
5834#endif /* FEAT_RIGHTLEFT */
5835
5836 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
5837
5838 while (col < endcol)
5839 {
5840#ifdef FEAT_MBYTE
5841 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00005842 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 else
5844 char_cells = 1;
5845#endif
5846
5847 redraw_this = redraw_next;
5848 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
5849 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
5850
5851#ifdef FEAT_GUI
5852 /* If the next character was bold, then redraw the current character to
5853 * remove any pixels that might have spilt over into us. This only
5854 * happens in the GUI.
5855 */
5856 if (redraw_next && gui.in_use)
5857 {
5858 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005859 if (hl > HL_ALL)
5860 hl = syn_attr2attr(hl);
5861 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 redraw_this = TRUE;
5863 }
5864#endif
5865
5866 if (redraw_this)
5867 {
5868 /*
5869 * Special handling when 'xs' termcap flag set (hpterm):
5870 * Attributes for characters are stored at the position where the
5871 * cursor is when writing the highlighting code. The
5872 * start-highlighting code must be written with the cursor on the
5873 * first highlighted character. The stop-highlighting code must
5874 * be written with the cursor just after the last highlighted
5875 * character.
5876 * Overwriting a character doesn't remove it's highlighting. Need
5877 * to clear the rest of the line, and force redrawing it
5878 * completely.
5879 */
5880 if ( p_wiv
5881 && !force
5882#ifdef FEAT_GUI
5883 && !gui.in_use
5884#endif
5885 && ScreenAttrs[off_to] != 0
5886 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
5887 {
5888 /*
5889 * Need to remove highlighting attributes here.
5890 */
5891 windgoto(row, col + coloff);
5892 out_str(T_CE); /* clear rest of this screen line */
5893 screen_start(); /* don't know where cursor is now */
5894 force = TRUE; /* force redraw of rest of the line */
5895 redraw_next = TRUE; /* or else next char would miss out */
5896
5897 /*
5898 * If the previous character was highlighted, need to stop
5899 * highlighting at this character.
5900 */
5901 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
5902 {
5903 screen_attr = ScreenAttrs[off_to - 1];
5904 term_windgoto(row, col + coloff);
5905 screen_stop_highlight();
5906 }
5907 else
5908 screen_attr = 0; /* highlighting has stopped */
5909 }
5910#ifdef FEAT_MBYTE
5911 if (enc_dbcs != 0)
5912 {
5913 /* Check if overwriting a double-byte with a single-byte or
5914 * the other way around requires another character to be
5915 * redrawn. For UTF-8 this isn't needed, because comparing
5916 * ScreenLinesUC[] is sufficient. */
5917 if (char_cells == 1
5918 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005919 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920 {
5921 /* Writing a single-cell character over a double-cell
5922 * character: need to redraw the next cell. */
5923 ScreenLines[off_to + 1] = 0;
5924 redraw_next = TRUE;
5925 }
5926 else if (char_cells == 2
5927 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005928 && (*mb_off2cells)(off_to, max_off_to) == 1
5929 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930 {
5931 /* Writing the second half of a double-cell character over
5932 * a double-cell character: need to redraw the second
5933 * cell. */
5934 ScreenLines[off_to + 2] = 0;
5935 redraw_next = TRUE;
5936 }
5937
5938 if (enc_dbcs == DBCS_JPNU)
5939 ScreenLines2[off_to] = ScreenLines2[off_from];
5940 }
5941 /* When writing a single-width character over a double-width
5942 * character and at the end of the redrawn text, need to clear out
5943 * the right halve of the old character.
5944 * Also required when writing the right halve of a double-width
5945 * char over the left halve of an existing one. */
5946 if (has_mbyte && col + char_cells == endcol
5947 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00005948 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949 || (char_cells == 2
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 clear_next = TRUE;
5953#endif
5954
5955 ScreenLines[off_to] = ScreenLines[off_from];
5956#ifdef FEAT_MBYTE
5957 if (enc_utf8)
5958 {
5959 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
5960 if (ScreenLinesUC[off_from] != 0)
5961 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005962 int i;
5963
5964 for (i = 0; i < Screen_mco; ++i)
5965 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966 }
5967 }
5968 if (char_cells == 2)
5969 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
5970#endif
5971
5972#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00005973 /* The bold trick makes a single column of pixels appear in the
5974 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975 * character should be redrawn too. This happens for our own GUI
5976 * and for some xterms. */
5977 if (
5978# ifdef FEAT_GUI
5979 gui.in_use
5980# endif
5981# if defined(FEAT_GUI) && defined(UNIX)
5982 ||
5983# endif
5984# ifdef UNIX
5985 term_is_xterm
5986# endif
5987 )
5988 {
5989 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005990 if (hl > HL_ALL)
5991 hl = syn_attr2attr(hl);
5992 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993 redraw_next = TRUE;
5994 }
5995#endif
5996 ScreenAttrs[off_to] = ScreenAttrs[off_from];
5997#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005998 /* For simplicity set the attributes of second half of a
5999 * double-wide character equal to the first half. */
6000 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006002
6003 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005 else
6006#endif
6007 screen_char(off_to, row, col + coloff);
6008 }
6009 else if ( p_wiv
6010#ifdef FEAT_GUI
6011 && !gui.in_use
6012#endif
6013 && col + coloff > 0)
6014 {
6015 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6016 {
6017 /*
6018 * Don't output stop-highlight when moving the cursor, it will
6019 * stop the highlighting when it should continue.
6020 */
6021 screen_attr = 0;
6022 }
6023 else if (screen_attr != 0)
6024 screen_stop_highlight();
6025 }
6026
6027 off_to += CHAR_CELLS;
6028 off_from += CHAR_CELLS;
6029 col += CHAR_CELLS;
6030 }
6031
6032#ifdef FEAT_MBYTE
6033 if (clear_next)
6034 {
6035 /* Clear the second half of a double-wide character of which the left
6036 * half was overwritten with a single-wide character. */
6037 ScreenLines[off_to] = ' ';
6038 if (enc_utf8)
6039 ScreenLinesUC[off_to] = 0;
6040 screen_char(off_to, row, col + coloff);
6041 }
6042#endif
6043
6044 if (clear_width > 0
6045#ifdef FEAT_RIGHTLEFT
6046 && !rlflag
6047#endif
6048 )
6049 {
6050#ifdef FEAT_GUI
6051 int startCol = col;
6052#endif
6053
6054 /* blank out the rest of the line */
6055 while (col < clear_width && ScreenLines[off_to] == ' '
6056 && ScreenAttrs[off_to] == 0
6057#ifdef FEAT_MBYTE
6058 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6059#endif
6060 )
6061 {
6062 ++off_to;
6063 ++col;
6064 }
6065 if (col < clear_width)
6066 {
6067#ifdef FEAT_GUI
6068 /*
6069 * In the GUI, clearing the rest of the line may leave pixels
6070 * behind if the first character cleared was bold. Some bold
6071 * fonts spill over the left. In this case we redraw the previous
6072 * character too. If we didn't skip any blanks above, then we
6073 * only redraw if the character wasn't already redrawn anyway.
6074 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006075 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 {
6077 hl = ScreenAttrs[off_to];
6078 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006079 {
6080 int prev_cells = 1;
6081# ifdef FEAT_MBYTE
6082 if (enc_utf8)
6083 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6084 * that its width is 2. */
6085 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6086 else if (enc_dbcs != 0)
6087 {
6088 /* find previous character by counting from first
6089 * column and get its width. */
6090 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006091 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006092
6093 while (off < off_to)
6094 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006095 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006096 off += prev_cells;
6097 }
6098 }
6099
6100 if (enc_dbcs != 0 && prev_cells > 1)
6101 screen_char_2(off_to - prev_cells, row,
6102 col + coloff - prev_cells);
6103 else
6104# endif
6105 screen_char(off_to - prev_cells, row,
6106 col + coloff - prev_cells);
6107 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 }
6109#endif
6110 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6111 ' ', ' ', 0);
6112#ifdef FEAT_VERTSPLIT
6113 off_to += clear_width - col;
6114 col = clear_width;
6115#endif
6116 }
6117 }
6118
6119 if (clear_width > 0)
6120 {
6121#ifdef FEAT_VERTSPLIT
6122 /* For a window that's left of another, draw the separator char. */
6123 if (col + coloff < Columns)
6124 {
6125 int c;
6126
6127 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006128 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006130 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6131 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132# endif
6133 || ScreenAttrs[off_to] != hl)
6134 {
6135 ScreenLines[off_to] = c;
6136 ScreenAttrs[off_to] = hl;
6137# ifdef FEAT_MBYTE
6138 if (enc_utf8)
6139 {
6140 if (c >= 0x80)
6141 {
6142 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006143 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 }
6145 else
6146 ScreenLinesUC[off_to] = 0;
6147 }
6148# endif
6149 screen_char(off_to, row, col + coloff);
6150 }
6151 }
6152 else
6153#endif
6154 LineWraps[row] = FALSE;
6155 }
6156}
6157
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006158#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006160 * Mirror text "str" for right-left displaying.
6161 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006163 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164rl_mirror(str)
6165 char_u *str;
6166{
6167 char_u *p1, *p2;
6168 int t;
6169
6170 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6171 {
6172 t = *p1;
6173 *p1 = *p2;
6174 *p2 = t;
6175 }
6176}
6177#endif
6178
6179#if defined(FEAT_WINDOWS) || defined(PROTO)
6180/*
6181 * mark all status lines for redraw; used after first :cd
6182 */
6183 void
6184status_redraw_all()
6185{
6186 win_T *wp;
6187
6188 for (wp = firstwin; wp; wp = wp->w_next)
6189 if (wp->w_status_height)
6190 {
6191 wp->w_redr_status = TRUE;
6192 redraw_later(VALID);
6193 }
6194}
6195
6196/*
6197 * mark all status lines of the current buffer for redraw
6198 */
6199 void
6200status_redraw_curbuf()
6201{
6202 win_T *wp;
6203
6204 for (wp = firstwin; wp; wp = wp->w_next)
6205 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6206 {
6207 wp->w_redr_status = TRUE;
6208 redraw_later(VALID);
6209 }
6210}
6211
6212/*
6213 * Redraw all status lines that need to be redrawn.
6214 */
6215 void
6216redraw_statuslines()
6217{
6218 win_T *wp;
6219
6220 for (wp = firstwin; wp; wp = wp->w_next)
6221 if (wp->w_redr_status)
6222 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006223 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006224 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225}
6226#endif
6227
6228#if (defined(FEAT_WILDMENU) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6229/*
6230 * Redraw all status lines at the bottom of frame "frp".
6231 */
6232 void
6233win_redraw_last_status(frp)
6234 frame_T *frp;
6235{
6236 if (frp->fr_layout == FR_LEAF)
6237 frp->fr_win->w_redr_status = TRUE;
6238 else if (frp->fr_layout == FR_ROW)
6239 {
6240 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6241 win_redraw_last_status(frp);
6242 }
6243 else /* frp->fr_layout == FR_COL */
6244 {
6245 frp = frp->fr_child;
6246 while (frp->fr_next != NULL)
6247 frp = frp->fr_next;
6248 win_redraw_last_status(frp);
6249 }
6250}
6251#endif
6252
6253#ifdef FEAT_VERTSPLIT
6254/*
6255 * Draw the verticap separator right of window "wp" starting with line "row".
6256 */
6257 static void
6258draw_vsep_win(wp, row)
6259 win_T *wp;
6260 int row;
6261{
6262 int hl;
6263 int c;
6264
6265 if (wp->w_vsep_width)
6266 {
6267 /* draw the vertical separator right of this window */
6268 c = fillchar_vsep(&hl);
6269 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6270 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6271 c, ' ', hl);
6272 }
6273}
6274#endif
6275
6276#ifdef FEAT_WILDMENU
6277static int status_match_len __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006278static int skip_status_match_char __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279
6280/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006281 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282 */
6283 static int
6284status_match_len(xp, s)
6285 expand_T *xp;
6286 char_u *s;
6287{
6288 int len = 0;
6289
6290#ifdef FEAT_MENU
6291 int emenu = (xp->xp_context == EXPAND_MENUS
6292 || xp->xp_context == EXPAND_MENUNAMES);
6293
6294 /* Check for menu separators - replace with '|'. */
6295 if (emenu && menu_is_separator(s))
6296 return 1;
6297#endif
6298
6299 while (*s != NUL)
6300 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006301 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006302 len += ptr2cells(s);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006303 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304 }
6305
6306 return len;
6307}
6308
6309/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006310 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006311 * These are backslashes used for escaping. Do show backslashes in help tags.
6312 */
6313 static int
6314skip_status_match_char(xp, s)
6315 expand_T *xp;
6316 char_u *s;
6317{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006318 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006319#ifdef FEAT_MENU
6320 || ((xp->xp_context == EXPAND_MENUS
6321 || xp->xp_context == EXPAND_MENUNAMES)
6322 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6323#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006324 )
6325 {
6326#ifndef BACKSLASH_IN_FILENAME
6327 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6328 return 2;
6329#endif
6330 return 1;
6331 }
6332 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006333}
6334
6335/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336 * Show wildchar matches in the status line.
6337 * Show at least the "match" item.
6338 * We start at item 'first_match' in the list and show all matches that fit.
6339 *
6340 * If inversion is possible we use it. Else '=' characters are used.
6341 */
6342 void
6343win_redr_status_matches(xp, num_matches, matches, match, showtail)
6344 expand_T *xp;
6345 int num_matches;
6346 char_u **matches; /* list of matches */
6347 int match;
6348 int showtail;
6349{
6350#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6351 int row;
6352 char_u *buf;
6353 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006354 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355 int fillchar;
6356 int attr;
6357 int i;
6358 int highlight = TRUE;
6359 char_u *selstart = NULL;
6360 int selstart_col = 0;
6361 char_u *selend = NULL;
6362 static int first_match = 0;
6363 int add_left = FALSE;
6364 char_u *s;
6365#ifdef FEAT_MENU
6366 int emenu;
6367#endif
6368#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6369 int l;
6370#endif
6371
6372 if (matches == NULL) /* interrupted completion? */
6373 return;
6374
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006375#ifdef FEAT_MBYTE
6376 if (has_mbyte)
6377 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6378 else
6379#endif
6380 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 if (buf == NULL)
6382 return;
6383
6384 if (match == -1) /* don't show match but original text */
6385 {
6386 match = 0;
6387 highlight = FALSE;
6388 }
6389 /* count 1 for the ending ">" */
6390 clen = status_match_len(xp, L_MATCH(match)) + 3;
6391 if (match == 0)
6392 first_match = 0;
6393 else if (match < first_match)
6394 {
6395 /* jumping left, as far as we can go */
6396 first_match = match;
6397 add_left = TRUE;
6398 }
6399 else
6400 {
6401 /* check if match fits on the screen */
6402 for (i = first_match; i < match; ++i)
6403 clen += status_match_len(xp, L_MATCH(i)) + 2;
6404 if (first_match > 0)
6405 clen += 2;
6406 /* jumping right, put match at the left */
6407 if ((long)clen > Columns)
6408 {
6409 first_match = match;
6410 /* if showing the last match, we can add some on the left */
6411 clen = 2;
6412 for (i = match; i < num_matches; ++i)
6413 {
6414 clen += status_match_len(xp, L_MATCH(i)) + 2;
6415 if ((long)clen >= Columns)
6416 break;
6417 }
6418 if (i == num_matches)
6419 add_left = TRUE;
6420 }
6421 }
6422 if (add_left)
6423 while (first_match > 0)
6424 {
6425 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6426 if ((long)clen >= Columns)
6427 break;
6428 --first_match;
6429 }
6430
6431 fillchar = fillchar_status(&attr, TRUE);
6432
6433 if (first_match == 0)
6434 {
6435 *buf = NUL;
6436 len = 0;
6437 }
6438 else
6439 {
6440 STRCPY(buf, "< ");
6441 len = 2;
6442 }
6443 clen = len;
6444
6445 i = first_match;
6446 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6447 {
6448 if (i == match)
6449 {
6450 selstart = buf + len;
6451 selstart_col = clen;
6452 }
6453
6454 s = L_MATCH(i);
6455 /* Check for menu separators - replace with '|' */
6456#ifdef FEAT_MENU
6457 emenu = (xp->xp_context == EXPAND_MENUS
6458 || xp->xp_context == EXPAND_MENUNAMES);
6459 if (emenu && menu_is_separator(s))
6460 {
6461 STRCPY(buf + len, transchar('|'));
6462 l = (int)STRLEN(buf + len);
6463 len += l;
6464 clen += l;
6465 }
6466 else
6467#endif
6468 for ( ; *s != NUL; ++s)
6469 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006470 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471 clen += ptr2cells(s);
6472#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006473 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 {
6475 STRNCPY(buf + len, s, l);
6476 s += l - 1;
6477 len += l;
6478 }
6479 else
6480#endif
6481 {
6482 STRCPY(buf + len, transchar_byte(*s));
6483 len += (int)STRLEN(buf + len);
6484 }
6485 }
6486 if (i == match)
6487 selend = buf + len;
6488
6489 *(buf + len++) = ' ';
6490 *(buf + len++) = ' ';
6491 clen += 2;
6492 if (++i == num_matches)
6493 break;
6494 }
6495
6496 if (i != num_matches)
6497 {
6498 *(buf + len++) = '>';
6499 ++clen;
6500 }
6501
6502 buf[len] = NUL;
6503
6504 row = cmdline_row - 1;
6505 if (row >= 0)
6506 {
6507 if (wild_menu_showing == 0)
6508 {
6509 if (msg_scrolled > 0)
6510 {
6511 /* Put the wildmenu just above the command line. If there is
6512 * no room, scroll the screen one line up. */
6513 if (cmdline_row == Rows - 1)
6514 {
6515 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
6516 ++msg_scrolled;
6517 }
6518 else
6519 {
6520 ++cmdline_row;
6521 ++row;
6522 }
6523 wild_menu_showing = WM_SCROLLED;
6524 }
6525 else
6526 {
6527 /* Create status line if needed by setting 'laststatus' to 2.
6528 * Set 'winminheight' to zero to avoid that the window is
6529 * resized. */
6530 if (lastwin->w_status_height == 0)
6531 {
6532 save_p_ls = p_ls;
6533 save_p_wmh = p_wmh;
6534 p_ls = 2;
6535 p_wmh = 0;
6536 last_status(FALSE);
6537 }
6538 wild_menu_showing = WM_SHOWN;
6539 }
6540 }
6541
6542 screen_puts(buf, row, 0, attr);
6543 if (selstart != NULL && highlight)
6544 {
6545 *selend = NUL;
6546 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
6547 }
6548
6549 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6550 }
6551
6552#ifdef FEAT_VERTSPLIT
6553 win_redraw_last_status(topframe);
6554#else
6555 lastwin->w_redr_status = TRUE;
6556#endif
6557 vim_free(buf);
6558}
6559#endif
6560
6561#if defined(FEAT_WINDOWS) || defined(PROTO)
6562/*
6563 * Redraw the status line of window wp.
6564 *
6565 * If inversion is possible we use it. Else '=' characters are used.
6566 */
6567 void
6568win_redr_status(wp)
6569 win_T *wp;
6570{
6571 int row;
6572 char_u *p;
6573 int len;
6574 int fillchar;
6575 int attr;
6576 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006577 static int busy = FALSE;
6578
6579 /* It's possible to get here recursively when 'statusline' (indirectly)
6580 * invokes ":redrawstatus". Simply ignore the call then. */
6581 if (busy)
6582 return;
6583 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584
6585 wp->w_redr_status = FALSE;
6586 if (wp->w_status_height == 0)
6587 {
6588 /* no status line, can only be last window */
6589 redraw_cmdline = TRUE;
6590 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006591 else if (!redrawing()
6592#ifdef FEAT_INS_EXPAND
6593 /* don't update status line when popup menu is visible and may be
6594 * drawn over it */
6595 || pum_visible()
6596#endif
6597 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 {
6599 /* Don't redraw right now, do it later. */
6600 wp->w_redr_status = TRUE;
6601 }
6602#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006603 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604 {
6605 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006606 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607 }
6608#endif
6609 else
6610 {
6611 fillchar = fillchar_status(&attr, wp == curwin);
6612
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006613 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614 p = NameBuff;
6615 len = (int)STRLEN(p);
6616
6617 if (wp->w_buffer->b_help
6618#ifdef FEAT_QUICKFIX
6619 || wp->w_p_pvw
6620#endif
6621 || bufIsChanged(wp->w_buffer)
6622 || wp->w_buffer->b_p_ro)
6623 *(p + len++) = ' ';
6624 if (wp->w_buffer->b_help)
6625 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006626 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627 len += (int)STRLEN(p + len);
6628 }
6629#ifdef FEAT_QUICKFIX
6630 if (wp->w_p_pvw)
6631 {
6632 STRCPY(p + len, _("[Preview]"));
6633 len += (int)STRLEN(p + len);
6634 }
6635#endif
6636 if (bufIsChanged(wp->w_buffer))
6637 {
6638 STRCPY(p + len, "[+]");
6639 len += 3;
6640 }
6641 if (wp->w_buffer->b_p_ro)
6642 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006643 STRCPY(p + len, _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 len += 4;
6645 }
6646
6647#ifndef FEAT_VERTSPLIT
6648 this_ru_col = ru_col;
6649 if (this_ru_col < (Columns + 1) / 2)
6650 this_ru_col = (Columns + 1) / 2;
6651#else
6652 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
6653 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
6654 this_ru_col = (W_WIDTH(wp) + 1) / 2;
6655 if (this_ru_col <= 1)
6656 {
6657 p = (char_u *)"<"; /* No room for file name! */
6658 len = 1;
6659 }
6660 else
6661#endif
6662#ifdef FEAT_MBYTE
6663 if (has_mbyte)
6664 {
6665 int clen = 0, i;
6666
6667 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006668 clen = mb_string2cells(p, -1);
6669
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 /* Find first character that will fit.
6671 * Going from start to end is much faster for DBCS. */
6672 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006673 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 clen -= (*mb_ptr2cells)(p + i);
6675 len = clen;
6676 if (i > 0)
6677 {
6678 p = p + i - 1;
6679 *p = '<';
6680 ++len;
6681 }
6682
6683 }
6684 else
6685#endif
6686 if (len > this_ru_col - 1)
6687 {
6688 p += len - (this_ru_col - 1);
6689 *p = '<';
6690 len = this_ru_col - 1;
6691 }
6692
6693 row = W_WINROW(wp) + wp->w_height;
6694 screen_puts(p, row, W_WINCOL(wp), attr);
6695 screen_fill(row, row + 1, len + W_WINCOL(wp),
6696 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
6697
6698 if (get_keymap_str(wp, NameBuff, MAXPATHL)
6699 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6700 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
6701 - 1 + W_WINCOL(wp)), attr);
6702
6703#ifdef FEAT_CMDL_INFO
6704 win_redr_ruler(wp, TRUE);
6705#endif
6706 }
6707
6708#ifdef FEAT_VERTSPLIT
6709 /*
6710 * May need to draw the character below the vertical separator.
6711 */
6712 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6713 {
6714 if (stl_connected(wp))
6715 fillchar = fillchar_status(&attr, wp == curwin);
6716 else
6717 fillchar = fillchar_vsep(&attr);
6718 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6719 attr);
6720 }
6721#endif
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006722 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723}
6724
Bram Moolenaar238a5642006-02-21 22:12:05 +00006725#ifdef FEAT_STL_OPT
6726/*
6727 * Redraw the status line according to 'statusline' and take care of any
6728 * errors encountered.
6729 */
6730 static void
Bram Moolenaar362f3562009-11-03 16:20:34 +00006731redraw_custom_statusline(wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006732 win_T *wp;
6733{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006734 static int entered = FALSE;
6735 int save_called_emsg = called_emsg;
6736
6737 /* When called recursively return. This can happen when the statusline
6738 * contains an expression that triggers a redraw. */
6739 if (entered)
6740 return;
6741 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006742
6743 called_emsg = FALSE;
6744 win_redr_custom(wp, FALSE);
6745 if (called_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006746 {
6747 /* When there is an error disable the statusline, otherwise the
6748 * display is messed up with errors and a redraw triggers the problem
6749 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00006750 set_string_option_direct((char_u *)"statusline", -1,
6751 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006752 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006753 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00006754 called_emsg |= save_called_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006755 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006756}
6757#endif
6758
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759# ifdef FEAT_VERTSPLIT
6760/*
6761 * Return TRUE if the status line of window "wp" is connected to the status
6762 * line of the window right of it. If not, then it's a vertical separator.
6763 * Only call if (wp->w_vsep_width != 0).
6764 */
6765 int
6766stl_connected(wp)
6767 win_T *wp;
6768{
6769 frame_T *fr;
6770
6771 fr = wp->w_frame;
6772 while (fr->fr_parent != NULL)
6773 {
6774 if (fr->fr_parent->fr_layout == FR_COL)
6775 {
6776 if (fr->fr_next != NULL)
6777 break;
6778 }
6779 else
6780 {
6781 if (fr->fr_next != NULL)
6782 return TRUE;
6783 }
6784 fr = fr->fr_parent;
6785 }
6786 return FALSE;
6787}
6788# endif
6789
6790#endif /* FEAT_WINDOWS */
6791
6792#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
6793/*
6794 * Get the value to show for the language mappings, active 'keymap'.
6795 */
6796 int
6797get_keymap_str(wp, buf, len)
6798 win_T *wp;
6799 char_u *buf; /* buffer for the result */
6800 int len; /* length of buffer */
6801{
6802 char_u *p;
6803
6804 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
6805 return FALSE;
6806
6807 {
6808#ifdef FEAT_EVAL
6809 buf_T *old_curbuf = curbuf;
6810 win_T *old_curwin = curwin;
6811 char_u *s;
6812
6813 curbuf = wp->w_buffer;
6814 curwin = wp;
6815 STRCPY(buf, "b:keymap_name"); /* must be writable */
6816 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006817 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 --emsg_skip;
6819 curbuf = old_curbuf;
6820 curwin = old_curwin;
6821 if (p == NULL || *p == NUL)
6822#endif
6823 {
6824#ifdef FEAT_KEYMAP
6825 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
6826 p = wp->w_buffer->b_p_keymap;
6827 else
6828#endif
6829 p = (char_u *)"lang";
6830 }
6831 if ((int)(STRLEN(p) + 3) < len)
6832 sprintf((char *)buf, "<%s>", p);
6833 else
6834 buf[0] = NUL;
6835#ifdef FEAT_EVAL
6836 vim_free(s);
6837#endif
6838 }
6839 return buf[0] != NUL;
6840}
6841#endif
6842
6843#if defined(FEAT_STL_OPT) || defined(PROTO)
6844/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006845 * Redraw the status line or ruler of window "wp".
6846 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847 */
6848 static void
Bram Moolenaar9372a112005-12-06 19:59:18 +00006849win_redr_custom(wp, draw_ruler)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850 win_T *wp;
Bram Moolenaar9372a112005-12-06 19:59:18 +00006851 int draw_ruler; /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852{
Bram Moolenaar1d633412013-12-11 15:52:01 +01006853 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854 int attr;
6855 int curattr;
6856 int row;
6857 int col = 0;
6858 int maxwidth;
6859 int width;
6860 int n;
6861 int len;
6862 int fillchar;
6863 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00006864 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006866 struct stl_hlrec hltab[STL_MAX_ITEM];
6867 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006868 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01006869 win_T *ewp;
6870 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871
Bram Moolenaar1d633412013-12-11 15:52:01 +01006872 /* There is a tiny chance that this gets called recursively: When
6873 * redrawing a status line triggers redrawing the ruler or tabline.
6874 * Avoid trouble by not allowing recursion. */
6875 if (entered)
6876 return;
6877 entered = TRUE;
6878
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006880 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006882 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006883 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006884 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00006885 fillchar = ' ';
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006886 attr = hl_attr(HLF_TPF);
6887 maxwidth = Columns;
6888# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006889 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006890# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006892 else
6893 {
6894 row = W_WINROW(wp) + wp->w_height;
6895 fillchar = fillchar_status(&attr, wp == curwin);
6896 maxwidth = W_WIDTH(wp);
6897
6898 if (draw_ruler)
6899 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006900 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006901 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006902 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006903 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006904 if (*++stl == '-')
6905 stl++;
6906 if (atoi((char *)stl))
6907 while (VIM_ISDIGIT(*stl))
6908 stl++;
6909 if (*stl++ != '(')
6910 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006911 }
6912#ifdef FEAT_VERTSPLIT
6913 col = ru_col - (Columns - W_WIDTH(wp));
6914 if (col < (W_WIDTH(wp) + 1) / 2)
6915 col = (W_WIDTH(wp) + 1) / 2;
6916#else
6917 col = ru_col;
6918 if (col > (Columns + 1) / 2)
6919 col = (Columns + 1) / 2;
6920#endif
6921 maxwidth = W_WIDTH(wp) - col;
6922#ifdef FEAT_WINDOWS
6923 if (!wp->w_status_height)
6924#endif
6925 {
6926 row = Rows - 1;
6927 --maxwidth; /* writing in last column may cause scrolling */
6928 fillchar = ' ';
6929 attr = 0;
6930 }
6931
6932# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006933 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006934# endif
6935 }
6936 else
6937 {
6938 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006939 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006940 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00006941 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006942# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006943 use_sandbox = was_set_insecurely((char_u *)"statusline",
6944 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006945# endif
6946 }
6947
6948#ifdef FEAT_VERTSPLIT
6949 col += W_WINCOL(wp);
6950#endif
6951 }
6952
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01006954 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955
Bram Moolenaar61452852011-02-01 18:01:11 +01006956 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
6957 * the cursor away and back. */
6958 ewp = wp == NULL ? curwin : wp;
6959 p_crb_save = ewp->w_p_crb;
6960 ewp->w_p_crb = FALSE;
6961
Bram Moolenaar362f3562009-11-03 16:20:34 +00006962 /* Make a copy, because the statusline may include a function call that
6963 * might change the option value and free the memory. */
6964 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01006965 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00006966 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006967 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006968 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01006969 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01006971 /* Make all characters printable. */
6972 p = transstr(buf);
6973 if (p != NULL)
6974 {
6975 vim_strncpy(buf, p, sizeof(buf) - 1);
6976 vim_free(p);
6977 }
6978
6979 /* fill up with "fillchar" */
6980 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00006981 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 {
6983#ifdef FEAT_MBYTE
6984 len += (*mb_char2bytes)(fillchar, buf + len);
6985#else
6986 buf[len++] = fillchar;
6987#endif
6988 ++width;
6989 }
6990 buf[len] = NUL;
6991
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006992 /*
6993 * Draw each snippet with the specified highlighting.
6994 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 curattr = attr;
6996 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006997 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006999 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000 screen_puts_len(p, len, row, col, curattr);
7001 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007002 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007004 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007006 else if (hltab[n].userhl < 0)
7007 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00007009 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007010 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011#endif
7012 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007013 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 }
7015 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007016
7017 if (wp == NULL)
7018 {
7019 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7020 col = 0;
7021 len = 0;
7022 p = buf;
7023 fillchar = 0;
7024 for (n = 0; tabtab[n].start != NULL; n++)
7025 {
7026 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7027 while (col < len)
7028 TabPageIdxs[col++] = fillchar;
7029 p = tabtab[n].start;
7030 fillchar = tabtab[n].userhl;
7031 }
7032 while (col < Columns)
7033 TabPageIdxs[col++] = fillchar;
7034 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007035
7036theend:
7037 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038}
7039
7040#endif /* FEAT_STL_OPT */
7041
7042/*
7043 * Output a single character directly to the screen and update ScreenLines.
7044 */
7045 void
7046screen_putchar(c, row, col, attr)
7047 int c;
7048 int row, col;
7049 int attr;
7050{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051 char_u buf[MB_MAXBYTES + 1];
7052
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007053#ifdef FEAT_MBYTE
7054 if (has_mbyte)
7055 buf[(*mb_char2bytes)(c, buf)] = NUL;
7056 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007058 {
7059 buf[0] = c;
7060 buf[1] = NUL;
7061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 screen_puts(buf, row, col, attr);
7063}
7064
7065/*
7066 * Get a single character directly from ScreenLines into "bytes[]".
7067 * Also return its attribute in *attrp;
7068 */
7069 void
7070screen_getbytes(row, col, bytes, attrp)
7071 int row, col;
7072 char_u *bytes;
7073 int *attrp;
7074{
7075 unsigned off;
7076
7077 /* safety check */
7078 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7079 {
7080 off = LineOffset[row] + col;
7081 *attrp = ScreenAttrs[off];
7082 bytes[0] = ScreenLines[off];
7083 bytes[1] = NUL;
7084
7085#ifdef FEAT_MBYTE
7086 if (enc_utf8 && ScreenLinesUC[off] != 0)
7087 bytes[utfc_char2bytes(off, bytes)] = NUL;
7088 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7089 {
7090 bytes[0] = ScreenLines[off];
7091 bytes[1] = ScreenLines2[off];
7092 bytes[2] = NUL;
7093 }
7094 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7095 {
7096 bytes[1] = ScreenLines[off + 1];
7097 bytes[2] = NUL;
7098 }
7099#endif
7100 }
7101}
7102
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007103#ifdef FEAT_MBYTE
7104static int screen_comp_differs __ARGS((int, int*));
7105
7106/*
7107 * Return TRUE if composing characters for screen posn "off" differs from
7108 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007109 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007110 */
7111 static int
7112screen_comp_differs(off, u8cc)
7113 int off;
7114 int *u8cc;
7115{
7116 int i;
7117
7118 for (i = 0; i < Screen_mco; ++i)
7119 {
7120 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7121 return TRUE;
7122 if (u8cc[i] == 0)
7123 break;
7124 }
7125 return FALSE;
7126}
7127#endif
7128
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129/*
7130 * Put string '*text' on the screen at position 'row' and 'col', with
7131 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7132 * Note: only outputs within one row, message is truncated at screen boundary!
7133 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7134 */
7135 void
7136screen_puts(text, row, col, attr)
7137 char_u *text;
7138 int row;
7139 int col;
7140 int attr;
7141{
7142 screen_puts_len(text, -1, row, col, attr);
7143}
7144
7145/*
7146 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7147 * a NUL.
7148 */
7149 void
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007150screen_puts_len(text, textlen, row, col, attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 char_u *text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007152 int textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153 int row;
7154 int col;
7155 int attr;
7156{
7157 unsigned off;
7158 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007159 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 int c;
7161#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007162 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 int mbyte_blen = 1;
7164 int mbyte_cells = 1;
7165 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007166 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167 int clear_next_cell = FALSE;
7168# ifdef FEAT_ARABIC
7169 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007170 int pc, nc, nc1;
7171 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172# endif
7173#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007174#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7175 int force_redraw_this;
7176 int force_redraw_next = FALSE;
7177#endif
7178 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179
7180 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7181 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007182 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183
Bram Moolenaarc236c162008-07-13 17:41:49 +00007184#ifdef FEAT_MBYTE
7185 /* When drawing over the right halve of a double-wide char clear out the
7186 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007187 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007188# ifdef FEAT_GUI
7189 && !gui.in_use
7190# endif
7191 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007192 {
7193 ScreenLines[off - 1] = ' ';
7194 ScreenAttrs[off - 1] = 0;
7195 if (enc_utf8)
7196 {
7197 ScreenLinesUC[off - 1] = 0;
7198 ScreenLinesC[0][off - 1] = 0;
7199 }
7200 /* redraw the previous cell, make it empty */
7201 screen_char(off - 1, row, col - 1);
7202 /* force the cell at "col" to be redrawn */
7203 force_redraw_next = TRUE;
7204 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007205#endif
7206
Bram Moolenaar367329b2007-08-30 11:53:22 +00007207#ifdef FEAT_MBYTE
7208 max_off = LineOffset[row] + screen_Columns;
7209#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007210 while (col < screen_Columns
7211 && (len < 0 || (int)(ptr - text) < len)
7212 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 {
7214 c = *ptr;
7215#ifdef FEAT_MBYTE
7216 /* check if this is the first byte of a multibyte */
7217 if (has_mbyte)
7218 {
7219 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007220 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007222 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7224 mbyte_cells = 1;
7225 else if (enc_dbcs != 0)
7226 mbyte_cells = mbyte_blen;
7227 else /* enc_utf8 */
7228 {
7229 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007230 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231 (int)((text + len) - ptr));
7232 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007233 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007235# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236 /* Non-BMP character: display as ? or fullwidth ?. */
7237 if (u8c >= 0x10000)
7238 {
7239 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7240 if (attr == 0)
7241 attr = hl_attr(HLF_8);
7242 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007243# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244# ifdef FEAT_ARABIC
7245 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7246 {
7247 /* Do Arabic shaping. */
7248 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7249 {
7250 /* Past end of string to be displayed. */
7251 nc = NUL;
7252 nc1 = NUL;
7253 }
7254 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007255 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007256 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7257 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007258 nc1 = pcc[0];
7259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 pc = prev_c;
7261 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007262 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263 }
7264 else
7265 prev_c = u8c;
7266# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007267 if (col + mbyte_cells > screen_Columns)
7268 {
7269 /* Only 1 cell left, but character requires 2 cells:
7270 * display a '>' in the last column to avoid wrapping. */
7271 c = '>';
7272 mbyte_cells = 1;
7273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274 }
7275 }
7276#endif
7277
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007278#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7279 force_redraw_this = force_redraw_next;
7280 force_redraw_next = FALSE;
7281#endif
7282
7283 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284#ifdef FEAT_MBYTE
7285 || (mbyte_cells == 2
7286 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7287 || (enc_dbcs == DBCS_JPNU
7288 && c == 0x8e
7289 && ScreenLines2[off] != ptr[1])
7290 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007291 && (ScreenLinesUC[off] !=
7292 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7293 || (ScreenLinesUC[off] != 0
7294 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295#endif
7296 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007297 || exmode_active;
7298
7299 if (need_redraw
7300#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7301 || force_redraw_this
7302#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303 )
7304 {
7305#if defined(FEAT_GUI) || defined(UNIX)
7306 /* The bold trick makes a single row of pixels appear in the next
7307 * character. When a bold character is removed, the next
7308 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007309 * and for some xterms. */
7310 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311# ifdef FEAT_GUI
7312 gui.in_use
7313# endif
7314# if defined(FEAT_GUI) && defined(UNIX)
7315 ||
7316# endif
7317# ifdef UNIX
7318 term_is_xterm
7319# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007320 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007322 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007324 if (n > HL_ALL)
7325 n = syn_attr2attr(n);
7326 if (n & HL_BOLD)
7327 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328 }
7329#endif
7330#ifdef FEAT_MBYTE
7331 /* When at the end of the text and overwriting a two-cell
7332 * character with a one-cell character, need to clear the next
7333 * cell. Also when overwriting the left halve of a two-cell char
7334 * with the right halve of a two-cell char. Do this only once
7335 * (mb_off2cells() may return 2 on the right halve). */
7336 if (clear_next_cell)
7337 clear_next_cell = FALSE;
7338 else if (has_mbyte
7339 && (len < 0 ? ptr[mbyte_blen] == NUL
7340 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007341 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007343 && (*mb_off2cells)(off, max_off) == 1
7344 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345 clear_next_cell = TRUE;
7346
7347 /* Make sure we never leave a second byte of a double-byte behind,
7348 * it confuses mb_off2cells(). */
7349 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007350 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007352 && (*mb_off2cells)(off, max_off) == 1
7353 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354 ScreenLines[off + mbyte_blen] = 0;
7355#endif
7356 ScreenLines[off] = c;
7357 ScreenAttrs[off] = attr;
7358#ifdef FEAT_MBYTE
7359 if (enc_utf8)
7360 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007361 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362 ScreenLinesUC[off] = 0;
7363 else
7364 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007365 int i;
7366
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007368 for (i = 0; i < Screen_mco; ++i)
7369 {
7370 ScreenLinesC[i][off] = u8cc[i];
7371 if (u8cc[i] == 0)
7372 break;
7373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 }
7375 if (mbyte_cells == 2)
7376 {
7377 ScreenLines[off + 1] = 0;
7378 ScreenAttrs[off + 1] = attr;
7379 }
7380 screen_char(off, row, col);
7381 }
7382 else if (mbyte_cells == 2)
7383 {
7384 ScreenLines[off + 1] = ptr[1];
7385 ScreenAttrs[off + 1] = attr;
7386 screen_char_2(off, row, col);
7387 }
7388 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7389 {
7390 ScreenLines2[off] = ptr[1];
7391 screen_char(off, row, col);
7392 }
7393 else
7394#endif
7395 screen_char(off, row, col);
7396 }
7397#ifdef FEAT_MBYTE
7398 if (has_mbyte)
7399 {
7400 off += mbyte_cells;
7401 col += mbyte_cells;
7402 ptr += mbyte_blen;
7403 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007404 {
7405 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007407 len = -1;
7408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 }
7410 else
7411#endif
7412 {
7413 ++off;
7414 ++col;
7415 ++ptr;
7416 }
7417 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007418
7419#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7420 /* If we detected the next character needs to be redrawn, but the text
7421 * doesn't extend up to there, update the character here. */
7422 if (force_redraw_next && col < screen_Columns)
7423 {
7424# ifdef FEAT_MBYTE
7425 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7426 screen_char_2(off, row, col);
7427 else
7428# endif
7429 screen_char(off, row, col);
7430 }
7431#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432}
7433
7434#ifdef FEAT_SEARCH_EXTRA
7435/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007436 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437 */
7438 static void
7439start_search_hl()
7440{
7441 if (p_hls && !no_hlsearch)
7442 {
7443 last_pat_prog(&search_hl.rm);
7444 search_hl.attr = hl_attr(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007445# ifdef FEAT_RELTIME
7446 /* Set the time limit to 'redrawtime'. */
7447 profile_setlimit(p_rdt, &search_hl.tm);
7448# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 }
7450}
7451
7452/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007453 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454 */
7455 static void
7456end_search_hl()
7457{
7458 if (search_hl.rm.regprog != NULL)
7459 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007460 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461 search_hl.rm.regprog = NULL;
7462 }
7463}
7464
7465/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007466 * Init for calling prepare_search_hl().
7467 */
7468 static void
7469init_search_hl(wp)
7470 win_T *wp;
7471{
7472 matchitem_T *cur;
7473
7474 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7475 * match */
7476 cur = wp->w_match_head;
7477 while (cur != NULL)
7478 {
7479 cur->hl.rm = cur->match;
7480 if (cur->hlg_id == 0)
7481 cur->hl.attr = 0;
7482 else
7483 cur->hl.attr = syn_id2attr(cur->hlg_id);
7484 cur->hl.buf = wp->w_buffer;
7485 cur->hl.lnum = 0;
7486 cur->hl.first_lnum = 0;
7487# ifdef FEAT_RELTIME
7488 /* Set the time limit to 'redrawtime'. */
7489 profile_setlimit(p_rdt, &(cur->hl.tm));
7490# endif
7491 cur = cur->next;
7492 }
7493 search_hl.buf = wp->w_buffer;
7494 search_hl.lnum = 0;
7495 search_hl.first_lnum = 0;
7496 /* time limit is set at the toplevel, for all windows */
7497}
7498
7499/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500 * Advance to the match in window "wp" line "lnum" or past it.
7501 */
7502 static void
7503prepare_search_hl(wp, lnum)
7504 win_T *wp;
7505 linenr_T lnum;
7506{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007507 matchitem_T *cur; /* points to the match list */
7508 match_T *shl; /* points to search_hl or a match */
7509 int shl_flag; /* flag to indicate whether search_hl
7510 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007511 int pos_inprogress; /* marks that position match search is
7512 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513 int n;
7514
7515 /*
7516 * When using a multi-line pattern, start searching at the top
7517 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007518 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007520 cur = wp->w_match_head;
7521 shl_flag = FALSE;
7522 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007524 if (shl_flag == FALSE)
7525 {
7526 shl = &search_hl;
7527 shl_flag = TRUE;
7528 }
7529 else
7530 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531 if (shl->rm.regprog != NULL
7532 && shl->lnum == 0
7533 && re_multiline(shl->rm.regprog))
7534 {
7535 if (shl->first_lnum == 0)
7536 {
7537# ifdef FEAT_FOLDING
7538 for (shl->first_lnum = lnum;
7539 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7540 if (hasFoldingWin(wp, shl->first_lnum - 1,
7541 NULL, NULL, TRUE, NULL))
7542 break;
7543# else
7544 shl->first_lnum = wp->w_topline;
7545# endif
7546 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007547 if (cur != NULL)
7548 cur->pos.cur = 0;
7549 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007550 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007551 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7552 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007554 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n, cur);
7555 pos_inprogress = cur == NULL || cur->pos.cur == 0
7556 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557 if (shl->lnum != 0)
7558 {
7559 shl->first_lnum = shl->lnum
7560 + shl->rm.endpos[0].lnum
7561 - shl->rm.startpos[0].lnum;
7562 n = shl->rm.endpos[0].col;
7563 }
7564 else
7565 {
7566 ++shl->first_lnum;
7567 n = 0;
7568 }
7569 }
7570 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007571 if (shl != &search_hl && cur != NULL)
7572 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573 }
7574}
7575
7576/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007577 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578 * Uses shl->buf.
7579 * Sets shl->lnum and shl->rm contents.
7580 * Note: Assumes a previous match is always before "lnum", unless
7581 * shl->lnum is zero.
7582 * Careful: Any pointers for buffer lines will become invalid.
7583 */
7584 static void
Bram Moolenaarb3414592014-06-17 17:48:32 +02007585next_search_hl(win, shl, lnum, mincol, cur)
7586 win_T *win;
7587 match_T *shl; /* points to search_hl or a match */
7588 linenr_T lnum;
7589 colnr_T mincol; /* minimal column for a match */
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007590 matchitem_T *cur; /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591{
7592 linenr_T l;
7593 colnr_T matchcol;
7594 long nmatched;
7595
7596 if (shl->lnum != 0)
7597 {
7598 /* Check for three situations:
7599 * 1. If the "lnum" is below a previous match, start a new search.
7600 * 2. If the previous match includes "mincol", use it.
7601 * 3. Continue after the previous match.
7602 */
7603 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7604 if (lnum > l)
7605 shl->lnum = 0;
7606 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7607 return;
7608 }
7609
7610 /*
7611 * Repeat searching for a match until one is found that includes "mincol"
7612 * or none is found in this line.
7613 */
7614 called_emsg = FALSE;
7615 for (;;)
7616 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007617#ifdef FEAT_RELTIME
7618 /* Stop searching after passing the time limit. */
7619 if (profile_passed_limit(&(shl->tm)))
7620 {
7621 shl->lnum = 0; /* no match found in time */
7622 break;
7623 }
7624#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625 /* Three situations:
7626 * 1. No useful previous match: search from start of line.
7627 * 2. Not Vi compatible or empty match: continue at next character.
7628 * Break the loop if this is beyond the end of the line.
7629 * 3. Vi compatible searching: continue at end of previous match.
7630 */
7631 if (shl->lnum == 0)
7632 matchcol = 0;
7633 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7634 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007635 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007637 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007638
7639 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007640 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007641 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007643 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644 shl->lnum = 0;
7645 break;
7646 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007647#ifdef FEAT_MBYTE
7648 if (has_mbyte)
7649 matchcol += mb_ptr2len(ml);
7650 else
7651#endif
7652 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653 }
7654 else
7655 matchcol = shl->rm.endpos[0].col;
7656
7657 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007658 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007660 /* Remember whether shl->rm is using a copy of the regprog in
7661 * cur->match. */
7662 int regprog_is_copy = (shl != &search_hl && cur != NULL
7663 && shl == &cur->hl
7664 && cur->match.regprog == cur->hl.rm.regprog);
7665
Bram Moolenaarb3414592014-06-17 17:48:32 +02007666 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7667 matchcol,
7668#ifdef FEAT_RELTIME
7669 &(shl->tm)
7670#else
7671 NULL
7672#endif
7673 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007674 /* Copy the regprog, in case it got freed and recompiled. */
7675 if (regprog_is_copy)
7676 cur->match.regprog = cur->hl.rm.regprog;
7677
Bram Moolenaarb3414592014-06-17 17:48:32 +02007678 if (called_emsg || got_int)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007679 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007680 /* Error while handling regexp: stop using this regexp. */
7681 if (shl == &search_hl)
7682 {
7683 /* don't free regprog in the match list, it's a copy */
7684 vim_regfree(shl->rm.regprog);
7685 SET_NO_HLSEARCH(TRUE);
7686 }
7687 shl->rm.regprog = NULL;
7688 shl->lnum = 0;
7689 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
7690 message */
7691 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007692 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007693 }
7694 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007695 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007696 else
7697 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698 if (nmatched == 0)
7699 {
7700 shl->lnum = 0; /* no match found */
7701 break;
7702 }
7703 if (shl->rm.startpos[0].lnum > 0
7704 || shl->rm.startpos[0].col >= mincol
7705 || nmatched > 1
7706 || shl->rm.endpos[0].col > mincol)
7707 {
7708 shl->lnum += shl->rm.startpos[0].lnum;
7709 break; /* useful match found */
7710 }
7711 }
7712}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713
Bram Moolenaarb3414592014-06-17 17:48:32 +02007714 static int
7715next_search_hl_pos(shl, lnum, posmatch, mincol)
7716 match_T *shl; /* points to a match */
7717 linenr_T lnum;
7718 posmatch_T *posmatch; /* match positions */
7719 colnr_T mincol; /* minimal column for a match */
7720{
7721 int i;
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02007722 int bot = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007723
7724 shl->lnum = 0;
7725 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
7726 {
7727 if (posmatch->pos[i].lnum == 0)
7728 break;
7729 if (posmatch->pos[i].col < mincol)
7730 continue;
7731 if (posmatch->pos[i].lnum == lnum)
7732 {
7733 if (shl->lnum == lnum)
7734 {
7735 /* partially sort positions by column numbers
7736 * on the same line */
7737 if (posmatch->pos[i].col < posmatch->pos[bot].col)
7738 {
7739 llpos_T tmp = posmatch->pos[i];
7740
7741 posmatch->pos[i] = posmatch->pos[bot];
7742 posmatch->pos[bot] = tmp;
7743 }
7744 }
7745 else
7746 {
7747 bot = i;
7748 shl->lnum = lnum;
7749 }
7750 }
7751 }
7752 posmatch->cur = 0;
7753 if (shl->lnum == lnum)
7754 {
7755 colnr_T start = posmatch->pos[bot].col == 0
7756 ? 0 : posmatch->pos[bot].col - 1;
7757 colnr_T end = posmatch->pos[bot].col == 0
7758 ? MAXCOL : start + posmatch->pos[bot].len;
7759
7760 shl->rm.startpos[0].lnum = 0;
7761 shl->rm.startpos[0].col = start;
7762 shl->rm.endpos[0].lnum = 0;
7763 shl->rm.endpos[0].col = end;
7764 posmatch->cur = bot + 1;
7765 return TRUE;
7766 }
7767 return FALSE;
7768}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02007769#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02007770
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771 static void
7772screen_start_highlight(attr)
7773 int attr;
7774{
7775 attrentry_T *aep = NULL;
7776
7777 screen_attr = attr;
7778 if (full_screen
7779#ifdef WIN3264
7780 && termcap_active
7781#endif
7782 )
7783 {
7784#ifdef FEAT_GUI
7785 if (gui.in_use)
7786 {
7787 char buf[20];
7788
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007789 /* The GUI handles this internally. */
7790 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791 OUT_STR(buf);
7792 }
7793 else
7794#endif
7795 {
7796 if (attr > HL_ALL) /* special HL attr. */
7797 {
7798 if (t_colors > 1)
7799 aep = syn_cterm_attr2entry(attr);
7800 else
7801 aep = syn_term_attr2entry(attr);
7802 if (aep == NULL) /* did ":syntax clear" */
7803 attr = 0;
7804 else
7805 attr = aep->ae_attr;
7806 }
7807 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
7808 out_str(T_MD);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007809 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
7810 && cterm_normal_fg_bold)
7811 /* If the Normal FG color has BOLD attribute and the new HL
7812 * has a FG color defined, clear BOLD. */
7813 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
7815 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007816 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
7817 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818 out_str(T_US);
7819 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
7820 out_str(T_CZH);
7821 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
7822 out_str(T_MR);
7823
7824 /*
7825 * Output the color or start string after bold etc., in case the
7826 * bold etc. override the color setting.
7827 */
7828 if (aep != NULL)
7829 {
7830 if (t_colors > 1)
7831 {
7832 if (aep->ae_u.cterm.fg_color)
7833 term_fg_color(aep->ae_u.cterm.fg_color - 1);
7834 if (aep->ae_u.cterm.bg_color)
7835 term_bg_color(aep->ae_u.cterm.bg_color - 1);
7836 }
7837 else
7838 {
7839 if (aep->ae_u.term.start != NULL)
7840 out_str(aep->ae_u.term.start);
7841 }
7842 }
7843 }
7844 }
7845}
7846
7847 void
7848screen_stop_highlight()
7849{
7850 int do_ME = FALSE; /* output T_ME code */
7851
7852 if (screen_attr != 0
7853#ifdef WIN3264
7854 && termcap_active
7855#endif
7856 )
7857 {
7858#ifdef FEAT_GUI
7859 if (gui.in_use)
7860 {
7861 char buf[20];
7862
7863 /* use internal GUI code */
7864 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
7865 OUT_STR(buf);
7866 }
7867 else
7868#endif
7869 {
7870 if (screen_attr > HL_ALL) /* special HL attr. */
7871 {
7872 attrentry_T *aep;
7873
7874 if (t_colors > 1)
7875 {
7876 /*
7877 * Assume that t_me restores the original colors!
7878 */
7879 aep = syn_cterm_attr2entry(screen_attr);
7880 if (aep != NULL && (aep->ae_u.cterm.fg_color
7881 || aep->ae_u.cterm.bg_color))
7882 do_ME = TRUE;
7883 }
7884 else
7885 {
7886 aep = syn_term_attr2entry(screen_attr);
7887 if (aep != NULL && aep->ae_u.term.stop != NULL)
7888 {
7889 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
7890 do_ME = TRUE;
7891 else
7892 out_str(aep->ae_u.term.stop);
7893 }
7894 }
7895 if (aep == NULL) /* did ":syntax clear" */
7896 screen_attr = 0;
7897 else
7898 screen_attr = aep->ae_attr;
7899 }
7900
7901 /*
7902 * Often all ending-codes are equal to T_ME. Avoid outputting the
7903 * same sequence several times.
7904 */
7905 if (screen_attr & HL_STANDOUT)
7906 {
7907 if (STRCMP(T_SE, T_ME) == 0)
7908 do_ME = TRUE;
7909 else
7910 out_str(T_SE);
7911 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007912 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913 {
7914 if (STRCMP(T_UE, T_ME) == 0)
7915 do_ME = TRUE;
7916 else
7917 out_str(T_UE);
7918 }
7919 if (screen_attr & HL_ITALIC)
7920 {
7921 if (STRCMP(T_CZR, T_ME) == 0)
7922 do_ME = TRUE;
7923 else
7924 out_str(T_CZR);
7925 }
7926 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
7927 out_str(T_ME);
7928
7929 if (t_colors > 1)
7930 {
7931 /* set Normal cterm colors */
7932 if (cterm_normal_fg_color != 0)
7933 term_fg_color(cterm_normal_fg_color - 1);
7934 if (cterm_normal_bg_color != 0)
7935 term_bg_color(cterm_normal_bg_color - 1);
7936 if (cterm_normal_fg_bold)
7937 out_str(T_MD);
7938 }
7939 }
7940 }
7941 screen_attr = 0;
7942}
7943
7944/*
7945 * Reset the colors for a cterm. Used when leaving Vim.
7946 * The machine specific code may override this again.
7947 */
7948 void
7949reset_cterm_colors()
7950{
7951 if (t_colors > 1)
7952 {
7953 /* set Normal cterm colors */
7954 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
7955 {
7956 out_str(T_OP);
7957 screen_attr = -1;
7958 }
7959 if (cterm_normal_fg_bold)
7960 {
7961 out_str(T_ME);
7962 screen_attr = -1;
7963 }
7964 }
7965}
7966
7967/*
7968 * Put character ScreenLines["off"] on the screen at position "row" and "col",
7969 * using the attributes from ScreenAttrs["off"].
7970 */
7971 static void
7972screen_char(off, row, col)
7973 unsigned off;
7974 int row;
7975 int col;
7976{
7977 int attr;
7978
7979 /* Check for illegal values, just in case (could happen just after
7980 * resizing). */
7981 if (row >= screen_Rows || col >= screen_Columns)
7982 return;
7983
Bram Moolenaar494838a2015-02-10 19:20:37 +01007984 /* Outputting a character in the last cell on the screen may scroll the
7985 * screen up. Only do it when the "xn" termcap property is set, otherwise
7986 * mark the character invalid (update it when scrolled up). */
7987 if (*T_XN == NUL
7988 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989#ifdef FEAT_RIGHTLEFT
7990 /* account for first command-line character in rightleft mode */
7991 && !cmdmsg_rl
7992#endif
7993 )
7994 {
7995 ScreenAttrs[off] = (sattr_T)-1;
7996 return;
7997 }
7998
7999 /*
8000 * Stop highlighting first, so it's easier to move the cursor.
8001 */
8002#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
8003 if (screen_char_attr != 0)
8004 attr = screen_char_attr;
8005 else
8006#endif
8007 attr = ScreenAttrs[off];
8008 if (screen_attr != attr)
8009 screen_stop_highlight();
8010
8011 windgoto(row, col);
8012
8013 if (screen_attr != attr)
8014 screen_start_highlight(attr);
8015
8016#ifdef FEAT_MBYTE
8017 if (enc_utf8 && ScreenLinesUC[off] != 0)
8018 {
8019 char_u buf[MB_MAXBYTES + 1];
8020
8021 /* Convert UTF-8 character to bytes and write it. */
8022
8023 buf[utfc_char2bytes(off, buf)] = NUL;
8024
8025 out_str(buf);
8026 if (utf_char2cells(ScreenLinesUC[off]) > 1)
8027 ++screen_cur_col;
8028 }
8029 else
8030#endif
8031 {
8032#ifdef FEAT_MBYTE
8033 out_flush_check();
8034#endif
8035 out_char(ScreenLines[off]);
8036#ifdef FEAT_MBYTE
8037 /* double-byte character in single-width cell */
8038 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8039 out_char(ScreenLines2[off]);
8040#endif
8041 }
8042
8043 screen_cur_col++;
8044}
8045
8046#ifdef FEAT_MBYTE
8047
8048/*
8049 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8050 * on the screen at position 'row' and 'col'.
8051 * The attributes of the first byte is used for all. This is required to
8052 * output the two bytes of a double-byte character with nothing in between.
8053 */
8054 static void
8055screen_char_2(off, row, col)
8056 unsigned off;
8057 int row;
8058 int col;
8059{
8060 /* Check for illegal values (could be wrong when screen was resized). */
8061 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8062 return;
8063
8064 /* Outputting the last character on the screen may scrollup the screen.
8065 * Don't to it! Mark the character invalid (update it when scrolled up) */
8066 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8067 {
8068 ScreenAttrs[off] = (sattr_T)-1;
8069 return;
8070 }
8071
8072 /* Output the first byte normally (positions the cursor), then write the
8073 * second byte directly. */
8074 screen_char(off, row, col);
8075 out_char(ScreenLines[off + 1]);
8076 ++screen_cur_col;
8077}
8078#endif
8079
8080#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
8081/*
8082 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8083 * This uses the contents of ScreenLines[] and doesn't change it.
8084 */
8085 void
8086screen_draw_rectangle(row, col, height, width, invert)
8087 int row;
8088 int col;
8089 int height;
8090 int width;
8091 int invert;
8092{
8093 int r, c;
8094 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008095#ifdef FEAT_MBYTE
8096 int max_off;
8097#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008099 /* Can't use ScreenLines unless initialized */
8100 if (ScreenLines == NULL)
8101 return;
8102
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 if (invert)
8104 screen_char_attr = HL_INVERSE;
8105 for (r = row; r < row + height; ++r)
8106 {
8107 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008108#ifdef FEAT_MBYTE
8109 max_off = off + screen_Columns;
8110#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111 for (c = col; c < col + width; ++c)
8112 {
8113#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008114 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115 {
8116 screen_char_2(off + c, r, c);
8117 ++c;
8118 }
8119 else
8120#endif
8121 {
8122 screen_char(off + c, r, c);
8123#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008124 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125 ++c;
8126#endif
8127 }
8128 }
8129 }
8130 screen_char_attr = 0;
8131}
8132#endif
8133
8134#ifdef FEAT_VERTSPLIT
8135/*
8136 * Redraw the characters for a vertically split window.
8137 */
8138 static void
8139redraw_block(row, end, wp)
8140 int row;
8141 int end;
8142 win_T *wp;
8143{
8144 int col;
8145 int width;
8146
8147# ifdef FEAT_CLIPBOARD
8148 clip_may_clear_selection(row, end - 1);
8149# endif
8150
8151 if (wp == NULL)
8152 {
8153 col = 0;
8154 width = Columns;
8155 }
8156 else
8157 {
8158 col = wp->w_wincol;
8159 width = wp->w_width;
8160 }
8161 screen_draw_rectangle(row, col, end - row, width, FALSE);
8162}
8163#endif
8164
8165/*
8166 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8167 * with character 'c1' in first column followed by 'c2' in the other columns.
8168 * Use attributes 'attr'.
8169 */
8170 void
8171screen_fill(start_row, end_row, start_col, end_col, c1, c2, attr)
8172 int start_row, end_row;
8173 int start_col, end_col;
8174 int c1, c2;
8175 int attr;
8176{
8177 int row;
8178 int col;
8179 int off;
8180 int end_off;
8181 int did_delete;
8182 int c;
8183 int norm_term;
8184#if defined(FEAT_GUI) || defined(UNIX)
8185 int force_next = FALSE;
8186#endif
8187
8188 if (end_row > screen_Rows) /* safety check */
8189 end_row = screen_Rows;
8190 if (end_col > screen_Columns) /* safety check */
8191 end_col = screen_Columns;
8192 if (ScreenLines == NULL
8193 || start_row >= end_row
8194 || start_col >= end_col) /* nothing to do */
8195 return;
8196
8197 /* it's a "normal" terminal when not in a GUI or cterm */
8198 norm_term = (
8199#ifdef FEAT_GUI
8200 !gui.in_use &&
8201#endif
8202 t_colors <= 1);
8203 for (row = start_row; row < end_row; ++row)
8204 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008205#ifdef FEAT_MBYTE
8206 if (has_mbyte
8207# ifdef FEAT_GUI
8208 && !gui.in_use
8209# endif
8210 )
8211 {
8212 /* When drawing over the right halve of a double-wide char clear
8213 * out the left halve. When drawing over the left halve of a
8214 * double wide-char clear out the right halve. Only needed in a
8215 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008216 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008217 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008218 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008219 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008220 }
8221#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222 /*
8223 * Try to use delete-line termcap code, when no attributes or in a
8224 * "normal" terminal, where a bold/italic space is just a
8225 * space.
8226 */
8227 did_delete = FALSE;
8228 if (c2 == ' '
8229 && end_col == Columns
8230 && can_clear(T_CE)
8231 && (attr == 0
8232 || (norm_term
8233 && attr <= HL_ALL
8234 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8235 {
8236 /*
8237 * check if we really need to clear something
8238 */
8239 col = start_col;
8240 if (c1 != ' ') /* don't clear first char */
8241 ++col;
8242
8243 off = LineOffset[row] + col;
8244 end_off = LineOffset[row] + end_col;
8245
8246 /* skip blanks (used often, keep it fast!) */
8247#ifdef FEAT_MBYTE
8248 if (enc_utf8)
8249 while (off < end_off && ScreenLines[off] == ' '
8250 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8251 ++off;
8252 else
8253#endif
8254 while (off < end_off && ScreenLines[off] == ' '
8255 && ScreenAttrs[off] == 0)
8256 ++off;
8257 if (off < end_off) /* something to be cleared */
8258 {
8259 col = off - LineOffset[row];
8260 screen_stop_highlight();
8261 term_windgoto(row, col);/* clear rest of this screen line */
8262 out_str(T_CE);
8263 screen_start(); /* don't know where cursor is now */
8264 col = end_col - col;
8265 while (col--) /* clear chars in ScreenLines */
8266 {
8267 ScreenLines[off] = ' ';
8268#ifdef FEAT_MBYTE
8269 if (enc_utf8)
8270 ScreenLinesUC[off] = 0;
8271#endif
8272 ScreenAttrs[off] = 0;
8273 ++off;
8274 }
8275 }
8276 did_delete = TRUE; /* the chars are cleared now */
8277 }
8278
8279 off = LineOffset[row] + start_col;
8280 c = c1;
8281 for (col = start_col; col < end_col; ++col)
8282 {
8283 if (ScreenLines[off] != c
8284#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008285 || (enc_utf8 && (int)ScreenLinesUC[off]
8286 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287#endif
8288 || ScreenAttrs[off] != attr
8289#if defined(FEAT_GUI) || defined(UNIX)
8290 || force_next
8291#endif
8292 )
8293 {
8294#if defined(FEAT_GUI) || defined(UNIX)
8295 /* The bold trick may make a single row of pixels appear in
8296 * the next character. When a bold character is removed, the
8297 * next character should be redrawn too. This happens for our
8298 * own GUI and for some xterms. */
8299 if (
8300# ifdef FEAT_GUI
8301 gui.in_use
8302# endif
8303# if defined(FEAT_GUI) && defined(UNIX)
8304 ||
8305# endif
8306# ifdef UNIX
8307 term_is_xterm
8308# endif
8309 )
8310 {
8311 if (ScreenLines[off] != ' '
8312 && (ScreenAttrs[off] > HL_ALL
8313 || ScreenAttrs[off] & HL_BOLD))
8314 force_next = TRUE;
8315 else
8316 force_next = FALSE;
8317 }
8318#endif
8319 ScreenLines[off] = c;
8320#ifdef FEAT_MBYTE
8321 if (enc_utf8)
8322 {
8323 if (c >= 0x80)
8324 {
8325 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008326 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 }
8328 else
8329 ScreenLinesUC[off] = 0;
8330 }
8331#endif
8332 ScreenAttrs[off] = attr;
8333 if (!did_delete || c != ' ')
8334 screen_char(off, row, col);
8335 }
8336 ++off;
8337 if (col == start_col)
8338 {
8339 if (did_delete)
8340 break;
8341 c = c2;
8342 }
8343 }
8344 if (end_col == Columns)
8345 LineWraps[row] = FALSE;
8346 if (row == Rows - 1) /* overwritten the command line */
8347 {
8348 redraw_cmdline = TRUE;
8349 if (c1 == ' ' && c2 == ' ')
8350 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008351 if (start_col == 0)
8352 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353 }
8354 }
8355}
8356
8357/*
8358 * Check if there should be a delay. Used before clearing or redrawing the
8359 * screen or the command line.
8360 */
8361 void
8362check_for_delay(check_msg_scroll)
8363 int check_msg_scroll;
8364{
8365 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8366 && !did_wait_return
8367 && emsg_silent == 0)
8368 {
8369 out_flush();
8370 ui_delay(1000L, TRUE);
8371 emsg_on_display = FALSE;
8372 if (check_msg_scroll)
8373 msg_scroll = FALSE;
8374 }
8375}
8376
8377/*
8378 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008379 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380 * Returns TRUE if there is a valid screen to write to.
8381 * Returns FALSE when starting up and screen not initialized yet.
8382 */
8383 int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008384screen_valid(doclear)
8385 int doclear;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008387 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388 return (ScreenLines != NULL);
8389}
8390
8391/*
8392 * Resize the shell to Rows and Columns.
8393 * Allocate ScreenLines[] and associated items.
8394 *
8395 * There may be some time between setting Rows and Columns and (re)allocating
8396 * ScreenLines[]. This happens when starting up and when (manually) changing
8397 * the shell size. Always use screen_Rows and screen_Columns to access items
8398 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8399 * final size of the shell is needed.
8400 */
8401 void
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008402screenalloc(doclear)
8403 int doclear;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404{
8405 int new_row, old_row;
8406#ifdef FEAT_GUI
8407 int old_Rows;
8408#endif
8409 win_T *wp;
8410 int outofmem = FALSE;
8411 int len;
8412 schar_T *new_ScreenLines;
8413#ifdef FEAT_MBYTE
8414 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008415 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008417 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418#endif
8419 sattr_T *new_ScreenAttrs;
8420 unsigned *new_LineOffset;
8421 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008422#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008423 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008424 tabpage_T *tp;
8425#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008427 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008428#ifdef FEAT_AUTOCMD
8429 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008431retry:
8432#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433 /*
8434 * Allocation of the screen buffers is done only when the size changes and
8435 * when Rows and Columns have been set and we have started doing full
8436 * screen stuff.
8437 */
8438 if ((ScreenLines != NULL
8439 && Rows == screen_Rows
8440 && Columns == screen_Columns
8441#ifdef FEAT_MBYTE
8442 && enc_utf8 == (ScreenLinesUC != NULL)
8443 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008444 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445#endif
8446 )
8447 || Rows == 0
8448 || Columns == 0
8449 || (!full_screen && ScreenLines == NULL))
8450 return;
8451
8452 /*
8453 * It's possible that we produce an out-of-memory message below, which
8454 * will cause this function to be called again. To break the loop, just
8455 * return here.
8456 */
8457 if (entered)
8458 return;
8459 entered = TRUE;
8460
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008461 /*
8462 * Note that the window sizes are updated before reallocating the arrays,
8463 * thus we must not redraw here!
8464 */
8465 ++RedrawingDisabled;
8466
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467 win_new_shellsize(); /* fit the windows in the new sized shell */
8468
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469 comp_col(); /* recompute columns for shown command and ruler */
8470
8471 /*
8472 * We're changing the size of the screen.
8473 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8474 * - Move lines from the old arrays into the new arrays, clear extra
8475 * lines (unless the screen is going to be cleared).
8476 * - Free the old arrays.
8477 *
8478 * If anything fails, make ScreenLines NULL, so we don't do anything!
8479 * Continuing with the old ScreenLines may result in a crash, because the
8480 * size is wrong.
8481 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008482 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008484#ifdef FEAT_AUTOCMD
8485 if (aucmd_win != NULL)
8486 win_free_lsize(aucmd_win);
8487#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488
8489 new_ScreenLines = (schar_T *)lalloc((long_u)(
8490 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8491#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008492 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493 if (enc_utf8)
8494 {
8495 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8496 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008497 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008498 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8500 }
8501 if (enc_dbcs == DBCS_JPNU)
8502 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8503 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8504#endif
8505 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8506 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8507 new_LineOffset = (unsigned *)lalloc((long_u)(
8508 Rows * sizeof(unsigned)), FALSE);
8509 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008510#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008511 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008512#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008514 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515 {
8516 if (win_alloc_lines(wp) == FAIL)
8517 {
8518 outofmem = TRUE;
8519#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008520 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521#endif
8522 }
8523 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008524#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008525 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8526 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008527 outofmem = TRUE;
8528#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008529#ifdef FEAT_WINDOWS
8530give_up:
8531#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008533#ifdef FEAT_MBYTE
8534 for (i = 0; i < p_mco; ++i)
8535 if (new_ScreenLinesC[i] == NULL)
8536 break;
8537#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538 if (new_ScreenLines == NULL
8539#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008540 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8542#endif
8543 || new_ScreenAttrs == NULL
8544 || new_LineOffset == NULL
8545 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008546#ifdef FEAT_WINDOWS
8547 || new_TabPageIdxs == NULL
8548#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008549 || outofmem)
8550 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008551 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008552 {
8553 /* guess the size */
8554 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8555
8556 /* Remember we did this to avoid getting outofmem messages over
8557 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008558 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560 vim_free(new_ScreenLines);
8561 new_ScreenLines = NULL;
8562#ifdef FEAT_MBYTE
8563 vim_free(new_ScreenLinesUC);
8564 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008565 for (i = 0; i < p_mco; ++i)
8566 {
8567 vim_free(new_ScreenLinesC[i]);
8568 new_ScreenLinesC[i] = NULL;
8569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008570 vim_free(new_ScreenLines2);
8571 new_ScreenLines2 = NULL;
8572#endif
8573 vim_free(new_ScreenAttrs);
8574 new_ScreenAttrs = NULL;
8575 vim_free(new_LineOffset);
8576 new_LineOffset = NULL;
8577 vim_free(new_LineWraps);
8578 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008579#ifdef FEAT_WINDOWS
8580 vim_free(new_TabPageIdxs);
8581 new_TabPageIdxs = NULL;
8582#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583 }
8584 else
8585 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008586 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008587
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588 for (new_row = 0; new_row < Rows; ++new_row)
8589 {
8590 new_LineOffset[new_row] = new_row * Columns;
8591 new_LineWraps[new_row] = FALSE;
8592
8593 /*
8594 * If the screen is not going to be cleared, copy as much as
8595 * possible from the old screen to the new one and clear the rest
8596 * (used when resizing the window at the "--more--" prompt or when
8597 * executing an external command, for the GUI).
8598 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008599 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600 {
8601 (void)vim_memset(new_ScreenLines + new_row * Columns,
8602 ' ', (size_t)Columns * sizeof(schar_T));
8603#ifdef FEAT_MBYTE
8604 if (enc_utf8)
8605 {
8606 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8607 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008608 for (i = 0; i < p_mco; ++i)
8609 (void)vim_memset(new_ScreenLinesC[i]
8610 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611 0, (size_t)Columns * sizeof(u8char_T));
8612 }
8613 if (enc_dbcs == DBCS_JPNU)
8614 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8615 0, (size_t)Columns * sizeof(schar_T));
8616#endif
8617 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8618 0, (size_t)Columns * sizeof(sattr_T));
8619 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008620 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621 {
8622 if (screen_Columns < Columns)
8623 len = screen_Columns;
8624 else
8625 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008626#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008627 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008628 * may be invalid now. Also when p_mco changes. */
8629 if (!(enc_utf8 && ScreenLinesUC == NULL)
8630 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008631#endif
8632 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8633 ScreenLines + LineOffset[old_row],
8634 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008636 if (enc_utf8 && ScreenLinesUC != NULL
8637 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638 {
8639 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8640 ScreenLinesUC + LineOffset[old_row],
8641 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008642 for (i = 0; i < p_mco; ++i)
8643 mch_memmove(new_ScreenLinesC[i]
8644 + new_LineOffset[new_row],
8645 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646 (size_t)len * sizeof(u8char_T));
8647 }
8648 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8649 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8650 ScreenLines2 + LineOffset[old_row],
8651 (size_t)len * sizeof(schar_T));
8652#endif
8653 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8654 ScreenAttrs + LineOffset[old_row],
8655 (size_t)len * sizeof(sattr_T));
8656 }
8657 }
8658 }
8659 /* Use the last line of the screen for the current line. */
8660 current_ScreenLine = new_ScreenLines + Rows * Columns;
8661 }
8662
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008663 free_screenlines();
8664
Bram Moolenaar071d4272004-06-13 20:20:40 +00008665 ScreenLines = new_ScreenLines;
8666#ifdef FEAT_MBYTE
8667 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008668 for (i = 0; i < p_mco; ++i)
8669 ScreenLinesC[i] = new_ScreenLinesC[i];
8670 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671 ScreenLines2 = new_ScreenLines2;
8672#endif
8673 ScreenAttrs = new_ScreenAttrs;
8674 LineOffset = new_LineOffset;
8675 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008676#ifdef FEAT_WINDOWS
8677 TabPageIdxs = new_TabPageIdxs;
8678#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008679
8680 /* It's important that screen_Rows and screen_Columns reflect the actual
8681 * size of ScreenLines[]. Set them before calling anything. */
8682#ifdef FEAT_GUI
8683 old_Rows = screen_Rows;
8684#endif
8685 screen_Rows = Rows;
8686 screen_Columns = Columns;
8687
8688 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008689 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008690 screenclear2();
8691
8692#ifdef FEAT_GUI
8693 else if (gui.in_use
8694 && !gui.starting
8695 && ScreenLines != NULL
8696 && old_Rows != Rows)
8697 {
8698 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8699 /*
8700 * Adjust the position of the cursor, for when executing an external
8701 * command.
8702 */
8703 if (msg_row >= Rows) /* Rows got smaller */
8704 msg_row = Rows - 1; /* put cursor at last row */
8705 else if (Rows > old_Rows) /* Rows got bigger */
8706 msg_row += Rows - old_Rows; /* put cursor in same place */
8707 if (msg_col >= Columns) /* Columns got smaller */
8708 msg_col = Columns - 1; /* put cursor at last column */
8709 }
8710#endif
8711
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008713 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008714
8715#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008716 /*
8717 * Do not apply autocommands more than 3 times to avoid an endless loop
8718 * in case applying autocommands always changes Rows or Columns.
8719 */
8720 if (starting == 0 && ++retry_count <= 3)
8721 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008722 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008723 /* In rare cases, autocommands may have altered Rows or Columns,
8724 * jump back to check if we need to allocate the screen again. */
8725 goto retry;
8726 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008727#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728}
8729
8730 void
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008731free_screenlines()
8732{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008733#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008734 int i;
8735
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008736 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008737 for (i = 0; i < Screen_mco; ++i)
8738 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008739 vim_free(ScreenLines2);
8740#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008741 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008742 vim_free(ScreenAttrs);
8743 vim_free(LineOffset);
8744 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008745#ifdef FEAT_WINDOWS
8746 vim_free(TabPageIdxs);
8747#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008748}
8749
8750 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751screenclear()
8752{
8753 check_for_delay(FALSE);
8754 screenalloc(FALSE); /* allocate screen buffers if size changed */
8755 screenclear2(); /* clear the screen */
8756}
8757
8758 static void
8759screenclear2()
8760{
8761 int i;
8762
8763 if (starting == NO_SCREEN || ScreenLines == NULL
8764#ifdef FEAT_GUI
8765 || (gui.in_use && gui.starting)
8766#endif
8767 )
8768 return;
8769
8770#ifdef FEAT_GUI
8771 if (!gui.in_use)
8772#endif
8773 screen_attr = -1; /* force setting the Normal colors */
8774 screen_stop_highlight(); /* don't want highlighting here */
8775
8776#ifdef FEAT_CLIPBOARD
8777 /* disable selection without redrawing it */
8778 clip_scroll_selection(9999);
8779#endif
8780
8781 /* blank out ScreenLines */
8782 for (i = 0; i < Rows; ++i)
8783 {
8784 lineclear(LineOffset[i], (int)Columns);
8785 LineWraps[i] = FALSE;
8786 }
8787
8788 if (can_clear(T_CL))
8789 {
8790 out_str(T_CL); /* clear the display */
8791 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008792 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793 }
8794 else
8795 {
8796 /* can't clear the screen, mark all chars with invalid attributes */
8797 for (i = 0; i < Rows; ++i)
8798 lineinvalid(LineOffset[i], (int)Columns);
8799 clear_cmdline = TRUE;
8800 }
8801
8802 screen_cleared = TRUE; /* can use contents of ScreenLines now */
8803
8804 win_rest_invalid(firstwin);
8805 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008806#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008807 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008808#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008809 if (must_redraw == CLEAR) /* no need to clear again */
8810 must_redraw = NOT_VALID;
8811 compute_cmdrow();
8812 msg_row = cmdline_row; /* put cursor on last line for messages */
8813 msg_col = 0;
8814 screen_start(); /* don't know where cursor is now */
8815 msg_scrolled = 0; /* can't scroll back */
8816 msg_didany = FALSE;
8817 msg_didout = FALSE;
8818}
8819
8820/*
8821 * Clear one line in ScreenLines.
8822 */
8823 static void
8824lineclear(off, width)
8825 unsigned off;
8826 int width;
8827{
8828 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
8829#ifdef FEAT_MBYTE
8830 if (enc_utf8)
8831 (void)vim_memset(ScreenLinesUC + off, 0,
8832 (size_t)width * sizeof(u8char_T));
8833#endif
8834 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
8835}
8836
8837/*
8838 * Mark one line in ScreenLines invalid by setting the attributes to an
8839 * invalid value.
8840 */
8841 static void
8842lineinvalid(off, width)
8843 unsigned off;
8844 int width;
8845{
8846 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
8847}
8848
8849#ifdef FEAT_VERTSPLIT
8850/*
8851 * Copy part of a Screenline for vertically split window "wp".
8852 */
8853 static void
8854linecopy(to, from, wp)
8855 int to;
8856 int from;
8857 win_T *wp;
8858{
8859 unsigned off_to = LineOffset[to] + wp->w_wincol;
8860 unsigned off_from = LineOffset[from] + wp->w_wincol;
8861
8862 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
8863 wp->w_width * sizeof(schar_T));
8864# ifdef FEAT_MBYTE
8865 if (enc_utf8)
8866 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008867 int i;
8868
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
8870 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008871 for (i = 0; i < p_mco; ++i)
8872 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
8873 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874 }
8875 if (enc_dbcs == DBCS_JPNU)
8876 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
8877 wp->w_width * sizeof(schar_T));
8878# endif
8879 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
8880 wp->w_width * sizeof(sattr_T));
8881}
8882#endif
8883
8884/*
8885 * Return TRUE if clearing with term string "p" would work.
8886 * It can't work when the string is empty or it won't set the right background.
8887 */
8888 int
8889can_clear(p)
8890 char_u *p;
8891{
8892 return (*p != NUL && (t_colors <= 1
8893#ifdef FEAT_GUI
8894 || gui.in_use
8895#endif
8896 || cterm_normal_bg_color == 0 || *T_UT != NUL));
8897}
8898
8899/*
8900 * Reset cursor position. Use whenever cursor was moved because of outputting
8901 * something directly to the screen (shell commands) or a terminal control
8902 * code.
8903 */
8904 void
8905screen_start()
8906{
8907 screen_cur_row = screen_cur_col = 9999;
8908}
8909
8910/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008911 * Move the cursor to position "row","col" in the screen.
8912 * This tries to find the most efficient way to move, minimizing the number of
8913 * characters sent to the terminal.
8914 */
8915 void
8916windgoto(row, col)
8917 int row;
8918 int col;
8919{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008920 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921 int i;
8922 int plan;
8923 int cost;
8924 int wouldbe_col;
8925 int noinvcurs;
8926 char_u *bs;
8927 int goto_cost;
8928 int attr;
8929
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008930#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
8932
8933#define PLAN_LE 1
8934#define PLAN_CR 2
8935#define PLAN_NL 3
8936#define PLAN_WRITE 4
8937 /* Can't use ScreenLines unless initialized */
8938 if (ScreenLines == NULL)
8939 return;
8940
8941 if (col != screen_cur_col || row != screen_cur_row)
8942 {
8943 /* Check for valid position. */
8944 if (row < 0) /* window without text lines? */
8945 row = 0;
8946 if (row >= screen_Rows)
8947 row = screen_Rows - 1;
8948 if (col >= screen_Columns)
8949 col = screen_Columns - 1;
8950
8951 /* check if no cursor movement is allowed in highlight mode */
8952 if (screen_attr && *T_MS == NUL)
8953 noinvcurs = HIGHL_COST;
8954 else
8955 noinvcurs = 0;
8956 goto_cost = GOTO_COST + noinvcurs;
8957
8958 /*
8959 * Plan how to do the positioning:
8960 * 1. Use CR to move it to column 0, same row.
8961 * 2. Use T_LE to move it a few columns to the left.
8962 * 3. Use NL to move a few lines down, column 0.
8963 * 4. Move a few columns to the right with T_ND or by writing chars.
8964 *
8965 * Don't do this if the cursor went beyond the last column, the cursor
8966 * position is unknown then (some terminals wrap, some don't )
8967 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008968 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969 * characters to move the cursor to the right.
8970 */
8971 if (row >= screen_cur_row && screen_cur_col < Columns)
8972 {
8973 /*
8974 * If the cursor is in the same row, bigger col, we can use CR
8975 * or T_LE.
8976 */
8977 bs = NULL; /* init for GCC */
8978 attr = screen_attr;
8979 if (row == screen_cur_row && col < screen_cur_col)
8980 {
8981 /* "le" is preferred over "bc", because "bc" is obsolete */
8982 if (*T_LE)
8983 bs = T_LE; /* "cursor left" */
8984 else
8985 bs = T_BC; /* "backspace character (old) */
8986 if (*bs)
8987 cost = (screen_cur_col - col) * (int)STRLEN(bs);
8988 else
8989 cost = 999;
8990 if (col + 1 < cost) /* using CR is less characters */
8991 {
8992 plan = PLAN_CR;
8993 wouldbe_col = 0;
8994 cost = 1; /* CR is just one character */
8995 }
8996 else
8997 {
8998 plan = PLAN_LE;
8999 wouldbe_col = col;
9000 }
9001 if (noinvcurs) /* will stop highlighting */
9002 {
9003 cost += noinvcurs;
9004 attr = 0;
9005 }
9006 }
9007
9008 /*
9009 * If the cursor is above where we want to be, we can use CR LF.
9010 */
9011 else if (row > screen_cur_row)
9012 {
9013 plan = PLAN_NL;
9014 wouldbe_col = 0;
9015 cost = (row - screen_cur_row) * 2; /* CR LF */
9016 if (noinvcurs) /* will stop highlighting */
9017 {
9018 cost += noinvcurs;
9019 attr = 0;
9020 }
9021 }
9022
9023 /*
9024 * If the cursor is in the same row, smaller col, just use write.
9025 */
9026 else
9027 {
9028 plan = PLAN_WRITE;
9029 wouldbe_col = screen_cur_col;
9030 cost = 0;
9031 }
9032
9033 /*
9034 * Check if any characters that need to be written have the
9035 * correct attributes. Also avoid UTF-8 characters.
9036 */
9037 i = col - wouldbe_col;
9038 if (i > 0)
9039 cost += i;
9040 if (cost < goto_cost && i > 0)
9041 {
9042 /*
9043 * Check if the attributes are correct without additionally
9044 * stopping highlighting.
9045 */
9046 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9047 while (i && *p++ == attr)
9048 --i;
9049 if (i != 0)
9050 {
9051 /*
9052 * Try if it works when highlighting is stopped here.
9053 */
9054 if (*--p == 0)
9055 {
9056 cost += noinvcurs;
9057 while (i && *p++ == 0)
9058 --i;
9059 }
9060 if (i != 0)
9061 cost = 999; /* different attributes, don't do it */
9062 }
9063#ifdef FEAT_MBYTE
9064 if (enc_utf8)
9065 {
9066 /* Don't use an UTF-8 char for positioning, it's slow. */
9067 for (i = wouldbe_col; i < col; ++i)
9068 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9069 {
9070 cost = 999;
9071 break;
9072 }
9073 }
9074#endif
9075 }
9076
9077 /*
9078 * We can do it without term_windgoto()!
9079 */
9080 if (cost < goto_cost)
9081 {
9082 if (plan == PLAN_LE)
9083 {
9084 if (noinvcurs)
9085 screen_stop_highlight();
9086 while (screen_cur_col > col)
9087 {
9088 out_str(bs);
9089 --screen_cur_col;
9090 }
9091 }
9092 else if (plan == PLAN_CR)
9093 {
9094 if (noinvcurs)
9095 screen_stop_highlight();
9096 out_char('\r');
9097 screen_cur_col = 0;
9098 }
9099 else if (plan == PLAN_NL)
9100 {
9101 if (noinvcurs)
9102 screen_stop_highlight();
9103 while (screen_cur_row < row)
9104 {
9105 out_char('\n');
9106 ++screen_cur_row;
9107 }
9108 screen_cur_col = 0;
9109 }
9110
9111 i = col - screen_cur_col;
9112 if (i > 0)
9113 {
9114 /*
9115 * Use cursor-right if it's one character only. Avoids
9116 * removing a line of pixels from the last bold char, when
9117 * using the bold trick in the GUI.
9118 */
9119 if (T_ND[0] != NUL && T_ND[1] == NUL)
9120 {
9121 while (i-- > 0)
9122 out_char(*T_ND);
9123 }
9124 else
9125 {
9126 int off;
9127
9128 off = LineOffset[row] + screen_cur_col;
9129 while (i-- > 0)
9130 {
9131 if (ScreenAttrs[off] != screen_attr)
9132 screen_stop_highlight();
9133#ifdef FEAT_MBYTE
9134 out_flush_check();
9135#endif
9136 out_char(ScreenLines[off]);
9137#ifdef FEAT_MBYTE
9138 if (enc_dbcs == DBCS_JPNU
9139 && ScreenLines[off] == 0x8e)
9140 out_char(ScreenLines2[off]);
9141#endif
9142 ++off;
9143 }
9144 }
9145 }
9146 }
9147 }
9148 else
9149 cost = 999;
9150
9151 if (cost >= goto_cost)
9152 {
9153 if (noinvcurs)
9154 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009155 if (row == screen_cur_row && (col > screen_cur_col)
9156 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157 term_cursor_right(col - screen_cur_col);
9158 else
9159 term_windgoto(row, col);
9160 }
9161 screen_cur_row = row;
9162 screen_cur_col = col;
9163 }
9164}
9165
9166/*
9167 * Set cursor to its position in the current window.
9168 */
9169 void
9170setcursor()
9171{
9172 if (redrawing())
9173 {
9174 validate_cursor();
9175 windgoto(W_WINROW(curwin) + curwin->w_wrow,
9176 W_WINCOL(curwin) + (
9177#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009178 /* With 'rightleft' set and the cursor on a double-wide
9179 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009180 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
9181# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009182 (has_mbyte
9183 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9184 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185# endif
9186 1)) :
9187#endif
9188 curwin->w_wcol));
9189 }
9190}
9191
9192
9193/*
9194 * insert 'line_count' lines at 'row' in window 'wp'
9195 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9196 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
9197 * scrolling.
9198 * Returns FAIL if the lines are not inserted, OK for success.
9199 */
9200 int
9201win_ins_lines(wp, row, line_count, invalid, mayclear)
9202 win_T *wp;
9203 int row;
9204 int line_count;
9205 int invalid;
9206 int mayclear;
9207{
9208 int did_delete;
9209 int nextrow;
9210 int lastrow;
9211 int retval;
9212
9213 if (invalid)
9214 wp->w_lines_valid = 0;
9215
9216 if (wp->w_height < 5)
9217 return FAIL;
9218
9219 if (line_count > wp->w_height - row)
9220 line_count = wp->w_height - row;
9221
9222 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
9223 if (retval != MAYBE)
9224 return retval;
9225
9226 /*
9227 * If there is a next window or a status line, we first try to delete the
9228 * lines at the bottom to avoid messing what is after the window.
9229 * If this fails and there are following windows, don't do anything to avoid
9230 * messing up those windows, better just redraw.
9231 */
9232 did_delete = FALSE;
9233#ifdef FEAT_WINDOWS
9234 if (wp->w_next != NULL || wp->w_status_height)
9235 {
9236 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9237 line_count, (int)Rows, FALSE, NULL) == OK)
9238 did_delete = TRUE;
9239 else if (wp->w_next)
9240 return FAIL;
9241 }
9242#endif
9243 /*
9244 * if no lines deleted, blank the lines that will end up below the window
9245 */
9246 if (!did_delete)
9247 {
9248#ifdef FEAT_WINDOWS
9249 wp->w_redr_status = TRUE;
9250#endif
9251 redraw_cmdline = TRUE;
9252 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
9253 lastrow = nextrow + line_count;
9254 if (lastrow > Rows)
9255 lastrow = Rows;
9256 screen_fill(nextrow - line_count, lastrow - line_count,
9257 W_WINCOL(wp), (int)W_ENDCOL(wp),
9258 ' ', ' ', 0);
9259 }
9260
9261 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
9262 == FAIL)
9263 {
9264 /* deletion will have messed up other windows */
9265 if (did_delete)
9266 {
9267#ifdef FEAT_WINDOWS
9268 wp->w_redr_status = TRUE;
9269#endif
9270 win_rest_invalid(W_NEXT(wp));
9271 }
9272 return FAIL;
9273 }
9274
9275 return OK;
9276}
9277
9278/*
9279 * delete "line_count" window lines at "row" in window "wp"
9280 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9281 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9282 * scrolling
9283 * Return OK for success, FAIL if the lines are not deleted.
9284 */
9285 int
9286win_del_lines(wp, row, line_count, invalid, mayclear)
9287 win_T *wp;
9288 int row;
9289 int line_count;
9290 int invalid;
9291 int mayclear;
9292{
9293 int retval;
9294
9295 if (invalid)
9296 wp->w_lines_valid = 0;
9297
9298 if (line_count > wp->w_height - row)
9299 line_count = wp->w_height - row;
9300
9301 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
9302 if (retval != MAYBE)
9303 return retval;
9304
9305 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
9306 (int)Rows, FALSE, NULL) == FAIL)
9307 return FAIL;
9308
9309#ifdef FEAT_WINDOWS
9310 /*
9311 * If there are windows or status lines below, try to put them at the
9312 * correct place. If we can't do that, they have to be redrawn.
9313 */
9314 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9315 {
9316 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9317 line_count, (int)Rows, NULL) == FAIL)
9318 {
9319 wp->w_redr_status = TRUE;
9320 win_rest_invalid(wp->w_next);
9321 }
9322 }
9323 /*
9324 * If this is the last window and there is no status line, redraw the
9325 * command line later.
9326 */
9327 else
9328#endif
9329 redraw_cmdline = TRUE;
9330 return OK;
9331}
9332
9333/*
9334 * Common code for win_ins_lines() and win_del_lines().
9335 * Returns OK or FAIL when the work has been done.
9336 * Returns MAYBE when not finished yet.
9337 */
9338 static int
9339win_do_lines(wp, row, line_count, mayclear, del)
9340 win_T *wp;
9341 int row;
9342 int line_count;
9343 int mayclear;
9344 int del;
9345{
9346 int retval;
9347
9348 if (!redrawing() || line_count <= 0)
9349 return FAIL;
9350
9351 /* only a few lines left: redraw is faster */
9352 if (mayclear && Rows - line_count < 5
9353#ifdef FEAT_VERTSPLIT
9354 && wp->w_width == Columns
9355#endif
9356 )
9357 {
9358 screenclear(); /* will set wp->w_lines_valid to 0 */
9359 return FAIL;
9360 }
9361
9362 /*
9363 * Delete all remaining lines
9364 */
9365 if (row + line_count >= wp->w_height)
9366 {
9367 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
9368 W_WINCOL(wp), (int)W_ENDCOL(wp),
9369 ' ', ' ', 0);
9370 return OK;
9371 }
9372
9373 /*
9374 * when scrolling, the message on the command line should be cleared,
9375 * otherwise it will stay there forever.
9376 */
9377 clear_cmdline = TRUE;
9378
9379 /*
9380 * If the terminal can set a scroll region, use that.
9381 * Always do this in a vertically split window. This will redraw from
9382 * ScreenLines[] when t_CV isn't defined. That's faster than using
9383 * win_line().
9384 * Don't use a scroll region when we are going to redraw the text, writing
9385 * a character in the lower right corner of the scroll region causes a
9386 * scroll-up in the DJGPP version.
9387 */
9388 if (scroll_region
9389#ifdef FEAT_VERTSPLIT
9390 || W_WIDTH(wp) != Columns
9391#endif
9392 )
9393 {
9394#ifdef FEAT_VERTSPLIT
9395 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9396#endif
9397 scroll_region_set(wp, row);
9398 if (del)
9399 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
9400 wp->w_height - row, FALSE, wp);
9401 else
9402 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
9403 wp->w_height - row, wp);
9404#ifdef FEAT_VERTSPLIT
9405 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9406#endif
9407 scroll_region_reset();
9408 return retval;
9409 }
9410
9411#ifdef FEAT_WINDOWS
9412 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9413 return FAIL;
9414#endif
9415
9416 return MAYBE;
9417}
9418
9419/*
9420 * window 'wp' and everything after it is messed up, mark it for redraw
9421 */
9422 static void
9423win_rest_invalid(wp)
9424 win_T *wp;
9425{
9426#ifdef FEAT_WINDOWS
9427 while (wp != NULL)
9428#else
9429 if (wp != NULL)
9430#endif
9431 {
9432 redraw_win_later(wp, NOT_VALID);
9433#ifdef FEAT_WINDOWS
9434 wp->w_redr_status = TRUE;
9435 wp = wp->w_next;
9436#endif
9437 }
9438 redraw_cmdline = TRUE;
9439}
9440
9441/*
9442 * The rest of the routines in this file perform screen manipulations. The
9443 * given operation is performed physically on the screen. The corresponding
9444 * change is also made to the internal screen image. In this way, the editor
9445 * anticipates the effect of editing changes on the appearance of the screen.
9446 * That way, when we call screenupdate a complete redraw isn't usually
9447 * necessary. Another advantage is that we can keep adding code to anticipate
9448 * screen changes, and in the meantime, everything still works.
9449 */
9450
9451/*
9452 * types for inserting or deleting lines
9453 */
9454#define USE_T_CAL 1
9455#define USE_T_CDL 2
9456#define USE_T_AL 3
9457#define USE_T_CE 4
9458#define USE_T_DL 5
9459#define USE_T_SR 6
9460#define USE_NL 7
9461#define USE_T_CD 8
9462#define USE_REDRAW 9
9463
9464/*
9465 * insert lines on the screen and update ScreenLines[]
9466 * 'end' is the line after the scrolled part. Normally it is Rows.
9467 * When scrolling region used 'off' is the offset from the top for the region.
9468 * 'row' and 'end' are relative to the start of the region.
9469 *
9470 * return FAIL for failure, OK for success.
9471 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009472 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00009473screen_ins_lines(off, row, line_count, end, wp)
9474 int off;
9475 int row;
9476 int line_count;
9477 int end;
9478 win_T *wp; /* NULL or window to use width from */
9479{
9480 int i;
9481 int j;
9482 unsigned temp;
9483 int cursor_row;
9484 int type;
9485 int result_empty;
9486 int can_ce = can_clear(T_CE);
9487
9488 /*
9489 * FAIL if
9490 * - there is no valid screen
9491 * - the screen has to be redrawn completely
9492 * - the line count is less than one
9493 * - the line count is more than 'ttyscroll'
9494 */
9495 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
9496 return FAIL;
9497
9498 /*
9499 * There are seven ways to insert lines:
9500 * 0. When in a vertically split window and t_CV isn't set, redraw the
9501 * characters from ScreenLines[].
9502 * 1. Use T_CD (clear to end of display) if it exists and the result of
9503 * the insert is just empty lines
9504 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9505 * present or line_count > 1. It looks better if we do all the inserts
9506 * at once.
9507 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9508 * insert is just empty lines and T_CE is not present or line_count >
9509 * 1.
9510 * 4. Use T_AL (insert line) if it exists.
9511 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9512 * just empty lines.
9513 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9514 * just empty lines.
9515 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9516 * the 'da' flag is not set or we have clear line capability.
9517 * 8. redraw the characters from ScreenLines[].
9518 *
9519 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9520 * the scrollbar for the window. It does have insert line, use that if it
9521 * exists.
9522 */
9523 result_empty = (row + line_count >= end);
9524#ifdef FEAT_VERTSPLIT
9525 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9526 type = USE_REDRAW;
9527 else
9528#endif
9529 if (can_clear(T_CD) && result_empty)
9530 type = USE_T_CD;
9531 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9532 type = USE_T_CAL;
9533 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9534 type = USE_T_CDL;
9535 else if (*T_AL != NUL)
9536 type = USE_T_AL;
9537 else if (can_ce && result_empty)
9538 type = USE_T_CE;
9539 else if (*T_DL != NUL && result_empty)
9540 type = USE_T_DL;
9541 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9542 type = USE_T_SR;
9543 else
9544 return FAIL;
9545
9546 /*
9547 * For clearing the lines screen_del_lines() is used. This will also take
9548 * care of t_db if necessary.
9549 */
9550 if (type == USE_T_CD || type == USE_T_CDL ||
9551 type == USE_T_CE || type == USE_T_DL)
9552 return screen_del_lines(off, row, line_count, end, FALSE, wp);
9553
9554 /*
9555 * If text is retained below the screen, first clear or delete as many
9556 * lines at the bottom of the window as are about to be inserted so that
9557 * the deleted lines won't later surface during a screen_del_lines.
9558 */
9559 if (*T_DB)
9560 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
9561
9562#ifdef FEAT_CLIPBOARD
9563 /* Remove a modeless selection when inserting lines halfway the screen
9564 * or not the full width of the screen. */
9565 if (off + row > 0
9566# ifdef FEAT_VERTSPLIT
9567 || (wp != NULL && wp->w_width != Columns)
9568# endif
9569 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009570 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009571 else
9572 clip_scroll_selection(-line_count);
9573#endif
9574
Bram Moolenaar071d4272004-06-13 20:20:40 +00009575#ifdef FEAT_GUI
9576 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9577 * scrolling is actually carried out. */
9578 gui_dont_update_cursor();
9579#endif
9580
9581 if (*T_CCS != NUL) /* cursor relative to region */
9582 cursor_row = row;
9583 else
9584 cursor_row = row + off;
9585
9586 /*
9587 * Shift LineOffset[] line_count down to reflect the inserted lines.
9588 * Clear the inserted lines in ScreenLines[].
9589 */
9590 row += off;
9591 end += off;
9592 for (i = 0; i < line_count; ++i)
9593 {
9594#ifdef FEAT_VERTSPLIT
9595 if (wp != NULL && wp->w_width != Columns)
9596 {
9597 /* need to copy part of a line */
9598 j = end - 1 - i;
9599 while ((j -= line_count) >= row)
9600 linecopy(j + line_count, j, wp);
9601 j += line_count;
9602 if (can_clear((char_u *)" "))
9603 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9604 else
9605 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9606 LineWraps[j] = FALSE;
9607 }
9608 else
9609#endif
9610 {
9611 j = end - 1 - i;
9612 temp = LineOffset[j];
9613 while ((j -= line_count) >= row)
9614 {
9615 LineOffset[j + line_count] = LineOffset[j];
9616 LineWraps[j + line_count] = LineWraps[j];
9617 }
9618 LineOffset[j + line_count] = temp;
9619 LineWraps[j + line_count] = FALSE;
9620 if (can_clear((char_u *)" "))
9621 lineclear(temp, (int)Columns);
9622 else
9623 lineinvalid(temp, (int)Columns);
9624 }
9625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009626
9627 screen_stop_highlight();
9628 windgoto(cursor_row, 0);
9629
9630#ifdef FEAT_VERTSPLIT
9631 /* redraw the characters */
9632 if (type == USE_REDRAW)
9633 redraw_block(row, end, wp);
9634 else
9635#endif
9636 if (type == USE_T_CAL)
9637 {
9638 term_append_lines(line_count);
9639 screen_start(); /* don't know where cursor is now */
9640 }
9641 else
9642 {
9643 for (i = 0; i < line_count; i++)
9644 {
9645 if (type == USE_T_AL)
9646 {
9647 if (i && cursor_row != 0)
9648 windgoto(cursor_row, 0);
9649 out_str(T_AL);
9650 }
9651 else /* type == USE_T_SR */
9652 out_str(T_SR);
9653 screen_start(); /* don't know where cursor is now */
9654 }
9655 }
9656
9657 /*
9658 * With scroll-reverse and 'da' flag set we need to clear the lines that
9659 * have been scrolled down into the region.
9660 */
9661 if (type == USE_T_SR && *T_DA)
9662 {
9663 for (i = 0; i < line_count; ++i)
9664 {
9665 windgoto(off + i, 0);
9666 out_str(T_CE);
9667 screen_start(); /* don't know where cursor is now */
9668 }
9669 }
9670
9671#ifdef FEAT_GUI
9672 gui_can_update_cursor();
9673 if (gui.in_use)
9674 out_flush(); /* always flush after a scroll */
9675#endif
9676 return OK;
9677}
9678
9679/*
9680 * delete lines on the screen and update ScreenLines[]
9681 * 'end' is the line after the scrolled part. Normally it is Rows.
9682 * When scrolling region used 'off' is the offset from the top for the region.
9683 * 'row' and 'end' are relative to the start of the region.
9684 *
9685 * Return OK for success, FAIL if the lines are not deleted.
9686 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009687 int
9688screen_del_lines(off, row, line_count, end, force, wp)
9689 int off;
9690 int row;
9691 int line_count;
9692 int end;
9693 int force; /* even when line_count > p_ttyscroll */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009694 win_T *wp UNUSED; /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009695{
9696 int j;
9697 int i;
9698 unsigned temp;
9699 int cursor_row;
9700 int cursor_end;
9701 int result_empty; /* result is empty until end of region */
9702 int can_delete; /* deleting line codes can be used */
9703 int type;
9704
9705 /*
9706 * FAIL if
9707 * - there is no valid screen
9708 * - the screen has to be redrawn completely
9709 * - the line count is less than one
9710 * - the line count is more than 'ttyscroll'
9711 */
9712 if (!screen_valid(TRUE) || line_count <= 0 ||
9713 (!force && line_count > p_ttyscroll))
9714 return FAIL;
9715
9716 /*
9717 * Check if the rest of the current region will become empty.
9718 */
9719 result_empty = row + line_count >= end;
9720
9721 /*
9722 * We can delete lines only when 'db' flag not set or when 'ce' option
9723 * available.
9724 */
9725 can_delete = (*T_DB == NUL || can_clear(T_CE));
9726
9727 /*
9728 * There are six ways to delete lines:
9729 * 0. When in a vertically split window and t_CV isn't set, redraw the
9730 * characters from ScreenLines[].
9731 * 1. Use T_CD if it exists and the result is empty.
9732 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9733 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9734 * none of the other ways work.
9735 * 4. Use T_CE (erase line) if the result is empty.
9736 * 5. Use T_DL (delete line) if it exists.
9737 * 6. redraw the characters from ScreenLines[].
9738 */
9739#ifdef FEAT_VERTSPLIT
9740 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9741 type = USE_REDRAW;
9742 else
9743#endif
9744 if (can_clear(T_CD) && result_empty)
9745 type = USE_T_CD;
9746#if defined(__BEOS__) && defined(BEOS_DR8)
9747 /*
9748 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9749 * its internal termcap... this works okay for tests which test *T_DB !=
9750 * NUL. It has the disadvantage that the user cannot use any :set t_*
9751 * command to get T_DB (back) to empty_option, only :set term=... will do
9752 * the trick...
9753 * Anyway, this hack will hopefully go away with the next OS release.
9754 * (Olaf Seibert)
9755 */
9756 else if (row == 0 && T_DB == empty_option
9757 && (line_count == 1 || *T_CDL == NUL))
9758#else
9759 else if (row == 0 && (
9760#ifndef AMIGA
9761 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9762 * up, so use delete-line command */
9763 line_count == 1 ||
9764#endif
9765 *T_CDL == NUL))
9766#endif
9767 type = USE_NL;
9768 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9769 type = USE_T_CDL;
9770 else if (can_clear(T_CE) && result_empty
9771#ifdef FEAT_VERTSPLIT
9772 && (wp == NULL || wp->w_width == Columns)
9773#endif
9774 )
9775 type = USE_T_CE;
9776 else if (*T_DL != NUL && can_delete)
9777 type = USE_T_DL;
9778 else if (*T_CDL != NUL && can_delete)
9779 type = USE_T_CDL;
9780 else
9781 return FAIL;
9782
9783#ifdef FEAT_CLIPBOARD
9784 /* Remove a modeless selection when deleting lines halfway the screen or
9785 * not the full width of the screen. */
9786 if (off + row > 0
9787# ifdef FEAT_VERTSPLIT
9788 || (wp != NULL && wp->w_width != Columns)
9789# endif
9790 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009791 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792 else
9793 clip_scroll_selection(line_count);
9794#endif
9795
Bram Moolenaar071d4272004-06-13 20:20:40 +00009796#ifdef FEAT_GUI
9797 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9798 * scrolling is actually carried out. */
9799 gui_dont_update_cursor();
9800#endif
9801
9802 if (*T_CCS != NUL) /* cursor relative to region */
9803 {
9804 cursor_row = row;
9805 cursor_end = end;
9806 }
9807 else
9808 {
9809 cursor_row = row + off;
9810 cursor_end = end + off;
9811 }
9812
9813 /*
9814 * Now shift LineOffset[] line_count up to reflect the deleted lines.
9815 * Clear the inserted lines in ScreenLines[].
9816 */
9817 row += off;
9818 end += off;
9819 for (i = 0; i < line_count; ++i)
9820 {
9821#ifdef FEAT_VERTSPLIT
9822 if (wp != NULL && wp->w_width != Columns)
9823 {
9824 /* need to copy part of a line */
9825 j = row + i;
9826 while ((j += line_count) <= end - 1)
9827 linecopy(j - line_count, j, wp);
9828 j -= line_count;
9829 if (can_clear((char_u *)" "))
9830 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9831 else
9832 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9833 LineWraps[j] = FALSE;
9834 }
9835 else
9836#endif
9837 {
9838 /* whole width, moving the line pointers is faster */
9839 j = row + i;
9840 temp = LineOffset[j];
9841 while ((j += line_count) <= end - 1)
9842 {
9843 LineOffset[j - line_count] = LineOffset[j];
9844 LineWraps[j - line_count] = LineWraps[j];
9845 }
9846 LineOffset[j - line_count] = temp;
9847 LineWraps[j - line_count] = FALSE;
9848 if (can_clear((char_u *)" "))
9849 lineclear(temp, (int)Columns);
9850 else
9851 lineinvalid(temp, (int)Columns);
9852 }
9853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009854
9855 screen_stop_highlight();
9856
9857#ifdef FEAT_VERTSPLIT
9858 /* redraw the characters */
9859 if (type == USE_REDRAW)
9860 redraw_block(row, end, wp);
9861 else
9862#endif
9863 if (type == USE_T_CD) /* delete the lines */
9864 {
9865 windgoto(cursor_row, 0);
9866 out_str(T_CD);
9867 screen_start(); /* don't know where cursor is now */
9868 }
9869 else if (type == USE_T_CDL)
9870 {
9871 windgoto(cursor_row, 0);
9872 term_delete_lines(line_count);
9873 screen_start(); /* don't know where cursor is now */
9874 }
9875 /*
9876 * Deleting lines at top of the screen or scroll region: Just scroll
9877 * the whole screen (scroll region) up by outputting newlines on the
9878 * last line.
9879 */
9880 else if (type == USE_NL)
9881 {
9882 windgoto(cursor_end - 1, 0);
9883 for (i = line_count; --i >= 0; )
9884 out_char('\n'); /* cursor will remain on same line */
9885 }
9886 else
9887 {
9888 for (i = line_count; --i >= 0; )
9889 {
9890 if (type == USE_T_DL)
9891 {
9892 windgoto(cursor_row, 0);
9893 out_str(T_DL); /* delete a line */
9894 }
9895 else /* type == USE_T_CE */
9896 {
9897 windgoto(cursor_row + i, 0);
9898 out_str(T_CE); /* erase a line */
9899 }
9900 screen_start(); /* don't know where cursor is now */
9901 }
9902 }
9903
9904 /*
9905 * If the 'db' flag is set, we need to clear the lines that have been
9906 * scrolled up at the bottom of the region.
9907 */
9908 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
9909 {
9910 for (i = line_count; i > 0; --i)
9911 {
9912 windgoto(cursor_end - i, 0);
9913 out_str(T_CE); /* erase a line */
9914 screen_start(); /* don't know where cursor is now */
9915 }
9916 }
9917
9918#ifdef FEAT_GUI
9919 gui_can_update_cursor();
9920 if (gui.in_use)
9921 out_flush(); /* always flush after a scroll */
9922#endif
9923
9924 return OK;
9925}
9926
9927/*
9928 * show the current mode and ruler
9929 *
9930 * If clear_cmdline is TRUE, clear the rest of the cmdline.
9931 * If clear_cmdline is FALSE there may be a message there that needs to be
9932 * cleared only if a mode is shown.
9933 * Return the length of the message (0 if no message).
9934 */
9935 int
9936showmode()
9937{
9938 int need_clear;
9939 int length = 0;
9940 int do_mode;
9941 int attr;
9942 int nwr_save;
9943#ifdef FEAT_INS_EXPAND
9944 int sub_attr;
9945#endif
9946
Bram Moolenaar7df351e2006-01-23 22:30:28 +00009947 do_mode = ((p_smd && msg_silent == 0)
9948 && ((State & INSERT)
9949 || restart_edit
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009950 || VIsual_active));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009951 if (do_mode || Recording)
9952 {
9953 /*
9954 * Don't show mode right now, when not redrawing or inside a mapping.
9955 * Call char_avail() only when we are going to show something, because
9956 * it takes a bit of time.
9957 */
9958 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
9959 {
9960 redraw_cmdline = TRUE; /* show mode later */
9961 return 0;
9962 }
9963
9964 nwr_save = need_wait_return;
9965
9966 /* wait a bit before overwriting an important message */
9967 check_for_delay(FALSE);
9968
9969 /* if the cmdline is more than one line high, erase top lines */
9970 need_clear = clear_cmdline;
9971 if (clear_cmdline && cmdline_row < Rows - 1)
9972 msg_clr_cmdline(); /* will reset clear_cmdline */
9973
9974 /* Position on the last line in the window, column 0 */
9975 msg_pos_mode();
9976 cursor_off();
9977 attr = hl_attr(HLF_CM); /* Highlight mode */
9978 if (do_mode)
9979 {
9980 MSG_PUTS_ATTR("--", attr);
9981#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00009982 if (
Bram Moolenaar09092152010-08-08 16:38:42 +02009983# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +00009984 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +02009985# else
Bram Moolenaarc236c162008-07-13 17:41:49 +00009986 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +00009987# endif
Bram Moolenaar09092152010-08-08 16:38:42 +02009988 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02009989# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009990 MSG_PUTS_ATTR(" IM", attr);
9991# else
9992 MSG_PUTS_ATTR(" XIM", attr);
9993# endif
9994#endif
9995#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
9996 if (gui.in_use)
9997 {
9998 if (hangul_input_state_get())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009999 MSG_PUTS_ATTR(" \307\321\261\333", attr); /* HANGUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010000 }
10001#endif
10002#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010003 /* CTRL-X in Insert mode */
10004 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010005 {
10006 /* These messages can get long, avoid a wrap in a narrow
10007 * window. Prefer showing edit_submode_extra. */
10008 length = (Rows - msg_row) * Columns - 3;
10009 if (edit_submode_extra != NULL)
10010 length -= vim_strsize(edit_submode_extra);
10011 if (length > 0)
10012 {
10013 if (edit_submode_pre != NULL)
10014 length -= vim_strsize(edit_submode_pre);
10015 if (length - vim_strsize(edit_submode) > 0)
10016 {
10017 if (edit_submode_pre != NULL)
10018 msg_puts_attr(edit_submode_pre, attr);
10019 msg_puts_attr(edit_submode, attr);
10020 }
10021 if (edit_submode_extra != NULL)
10022 {
10023 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10024 if ((int)edit_submode_highl < (int)HLF_COUNT)
10025 sub_attr = hl_attr(edit_submode_highl);
10026 else
10027 sub_attr = attr;
10028 msg_puts_attr(edit_submode_extra, sub_attr);
10029 }
10030 }
10031 length = 0;
10032 }
10033 else
10034#endif
10035 {
10036#ifdef FEAT_VREPLACE
10037 if (State & VREPLACE_FLAG)
10038 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
10039 else
10040#endif
10041 if (State & REPLACE_FLAG)
10042 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10043 else if (State & INSERT)
10044 {
10045#ifdef FEAT_RIGHTLEFT
10046 if (p_ri)
10047 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10048#endif
10049 MSG_PUTS_ATTR(_(" INSERT"), attr);
10050 }
10051 else if (restart_edit == 'I')
10052 MSG_PUTS_ATTR(_(" (insert)"), attr);
10053 else if (restart_edit == 'R')
10054 MSG_PUTS_ATTR(_(" (replace)"), attr);
10055 else if (restart_edit == 'V')
10056 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10057#ifdef FEAT_RIGHTLEFT
10058 if (p_hkmap)
10059 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10060# ifdef FEAT_FKMAP
10061 if (p_fkmap)
10062 MSG_PUTS_ATTR(farsi_text_5, attr);
10063# endif
10064#endif
10065#ifdef FEAT_KEYMAP
10066 if (State & LANGMAP)
10067 {
10068# ifdef FEAT_ARABIC
10069 if (curwin->w_p_arab)
10070 MSG_PUTS_ATTR(_(" Arabic"), attr);
10071 else
10072# endif
10073 MSG_PUTS_ATTR(_(" (lang)"), attr);
10074 }
10075#endif
10076 if ((State & INSERT) && p_paste)
10077 MSG_PUTS_ATTR(_(" (paste)"), attr);
10078
Bram Moolenaar071d4272004-06-13 20:20:40 +000010079 if (VIsual_active)
10080 {
10081 char *p;
10082
10083 /* Don't concatenate separate words to avoid translation
10084 * problems. */
10085 switch ((VIsual_select ? 4 : 0)
10086 + (VIsual_mode == Ctrl_V) * 2
10087 + (VIsual_mode == 'V'))
10088 {
10089 case 0: p = N_(" VISUAL"); break;
10090 case 1: p = N_(" VISUAL LINE"); break;
10091 case 2: p = N_(" VISUAL BLOCK"); break;
10092 case 4: p = N_(" SELECT"); break;
10093 case 5: p = N_(" SELECT LINE"); break;
10094 default: p = N_(" SELECT BLOCK"); break;
10095 }
10096 MSG_PUTS_ATTR(_(p), attr);
10097 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010098 MSG_PUTS_ATTR(" --", attr);
10099 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010100
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101 need_clear = TRUE;
10102 }
10103 if (Recording
10104#ifdef FEAT_INS_EXPAND
10105 && edit_submode == NULL /* otherwise it gets too long */
10106#endif
10107 )
10108 {
10109 MSG_PUTS_ATTR(_("recording"), attr);
10110 need_clear = TRUE;
10111 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010112
10113 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010114 if (need_clear || clear_cmdline)
10115 msg_clr_eos();
10116 msg_didout = FALSE; /* overwrite this message */
10117 length = msg_col;
10118 msg_col = 0;
10119 need_wait_return = nwr_save; /* never ask for hit-return for this */
10120 }
10121 else if (clear_cmdline && msg_silent == 0)
10122 /* Clear the whole command line. Will reset "clear_cmdline". */
10123 msg_clr_cmdline();
10124
10125#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010126 /* In Visual mode the size of the selected area must be redrawn. */
10127 if (VIsual_active)
10128 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010129
10130 /* If the last window has no status line, the ruler is after the mode
10131 * message and must be redrawn */
10132 if (redrawing()
10133# ifdef FEAT_WINDOWS
10134 && lastwin->w_status_height == 0
10135# endif
10136 )
10137 win_redr_ruler(lastwin, TRUE);
10138#endif
10139 redraw_cmdline = FALSE;
10140 clear_cmdline = FALSE;
10141
10142 return length;
10143}
10144
10145/*
10146 * Position for a mode message.
10147 */
10148 static void
10149msg_pos_mode()
10150{
10151 msg_col = 0;
10152 msg_row = Rows - 1;
10153}
10154
10155/*
10156 * Delete mode message. Used when ESC is typed which is expected to end
10157 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010158 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010159 */
10160 void
10161unshowmode(force)
10162 int force;
10163{
10164 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010165 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010166 */
10167 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10168 redraw_cmdline = TRUE; /* delete mode later */
10169 else
10170 {
10171 msg_pos_mode();
10172 if (Recording)
10173 MSG_PUTS_ATTR(_("recording"), hl_attr(HLF_CM));
10174 msg_clr_eos();
10175 }
10176}
10177
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010178#if defined(FEAT_WINDOWS)
10179/*
10180 * Draw the tab pages line at the top of the Vim window.
10181 */
10182 static void
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010183draw_tabline()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010184{
10185 int tabcount = 0;
10186 tabpage_T *tp;
10187 int tabwidth;
10188 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010189 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010190 int attr;
10191 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010192 win_T *cwp;
10193 int wincount;
10194 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010195 int c;
10196 int len;
10197 int attr_sel = hl_attr(HLF_TPS);
10198 int attr_nosel = hl_attr(HLF_TP);
10199 int attr_fill = hl_attr(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010200 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010201 int room;
10202 int use_sep_chars = (t_colors < 8
10203#ifdef FEAT_GUI
10204 && !gui.in_use
10205#endif
10206 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010207
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010208 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010209
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010210#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010211 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010212 if (gui_use_tabline())
10213 {
10214 gui_update_tabline();
10215 return;
10216 }
10217#endif
10218
10219 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010220 return;
10221
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010222#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010223
10224 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10225 for (scol = 0; scol < Columns; ++scol)
10226 TabPageIdxs[scol] = 0;
10227
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010228 /* Use the 'tabline' option if it's set. */
10229 if (*p_tal != NUL)
10230 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010231 int save_called_emsg = called_emsg;
10232
10233 /* Check for an error. If there is one we would loop in redrawing the
10234 * screen. Avoid that by making 'tabline' empty. */
10235 called_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010236 win_redr_custom(NULL, FALSE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010237 if (called_emsg)
10238 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010239 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010240 called_emsg |= save_called_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010241 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010242 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010243#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010244 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010245 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
10246 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010247
Bram Moolenaar238a5642006-02-21 22:12:05 +000010248 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10249 if (tabwidth < 6)
10250 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010251
Bram Moolenaar238a5642006-02-21 22:12:05 +000010252 attr = attr_nosel;
10253 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010254 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010255 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10256 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010257 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010258 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010259
Bram Moolenaar238a5642006-02-21 22:12:05 +000010260 if (tp->tp_topframe == topframe)
10261 attr = attr_sel;
10262 if (use_sep_chars && col > 0)
10263 screen_putchar('|', 0, col++, attr);
10264
10265 if (tp->tp_topframe != topframe)
10266 attr = attr_nosel;
10267
10268 screen_putchar(' ', 0, col++, attr);
10269
10270 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010271 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010272 cwp = curwin;
10273 wp = firstwin;
10274 }
10275 else
10276 {
10277 cwp = tp->tp_curwin;
10278 wp = tp->tp_firstwin;
10279 }
10280
10281 modified = FALSE;
10282 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10283 if (bufIsChanged(wp->w_buffer))
10284 modified = TRUE;
10285 if (modified || wincount > 1)
10286 {
10287 if (wincount > 1)
10288 {
10289 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010290 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010291 if (col + len >= Columns - 3)
10292 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010293 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010294#if defined(FEAT_SYN_HL)
Bram Moolenaare0f14822014-08-06 13:20:56 +020010295 hl_combine_attr(attr, hl_attr(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010296#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010297 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010298#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010299 );
10300 col += len;
10301 }
10302 if (modified)
10303 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10304 screen_putchar(' ', 0, col++, attr);
10305 }
10306
10307 room = scol - col + tabwidth - 1;
10308 if (room > 0)
10309 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010310 /* Get buffer name in NameBuff[] */
10311 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010312 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010313 len = vim_strsize(NameBuff);
10314 p = NameBuff;
10315#ifdef FEAT_MBYTE
10316 if (has_mbyte)
10317 while (len > room)
10318 {
10319 len -= ptr2cells(p);
10320 mb_ptr_adv(p);
10321 }
10322 else
10323#endif
10324 if (len > room)
10325 {
10326 p += len - room;
10327 len = room;
10328 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010329 if (len > Columns - col - 1)
10330 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010331
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010332 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010333 col += len;
10334 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010335 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010336
10337 /* Store the tab page number in TabPageIdxs[], so that
10338 * jump_to_mouse() knows where each one is. */
10339 ++tabcount;
10340 while (scol < col)
10341 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010342 }
10343
Bram Moolenaar238a5642006-02-21 22:12:05 +000010344 if (use_sep_chars)
10345 c = '_';
10346 else
10347 c = ' ';
10348 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010349
10350 /* Put an "X" for closing the current tab if there are several. */
10351 if (first_tabpage->tp_next != NULL)
10352 {
10353 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10354 TabPageIdxs[Columns - 1] = -999;
10355 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010356 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010357
10358 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10359 * set. */
10360 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010361}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010362
10363/*
10364 * Get buffer name for "buf" into NameBuff[].
10365 * Takes care of special buffer names and translates special characters.
10366 */
10367 void
10368get_trans_bufname(buf)
10369 buf_T *buf;
10370{
10371 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010372 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010373 else
10374 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10375 trans_characters(NameBuff, MAXPATHL);
10376}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010377#endif
10378
Bram Moolenaar071d4272004-06-13 20:20:40 +000010379#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
10380/*
10381 * Get the character to use in a status line. Get its attributes in "*attr".
10382 */
10383 static int
10384fillchar_status(attr, is_curwin)
10385 int *attr;
10386 int is_curwin;
10387{
10388 int fill;
10389 if (is_curwin)
10390 {
10391 *attr = hl_attr(HLF_S);
10392 fill = fill_stl;
10393 }
10394 else
10395 {
10396 *attr = hl_attr(HLF_SNC);
10397 fill = fill_stlnc;
10398 }
10399 /* Use fill when there is highlighting, and highlighting of current
10400 * window differs, or the fillchars differ, or this is not the
10401 * current window */
10402 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
10403 || !is_curwin || firstwin == lastwin)
10404 || (fill_stl != fill_stlnc)))
10405 return fill;
10406 if (is_curwin)
10407 return '^';
10408 return '=';
10409}
10410#endif
10411
10412#ifdef FEAT_VERTSPLIT
10413/*
10414 * Get the character to use in a separator between vertically split windows.
10415 * Get its attributes in "*attr".
10416 */
10417 static int
10418fillchar_vsep(attr)
10419 int *attr;
10420{
10421 *attr = hl_attr(HLF_C);
10422 if (*attr == 0 && fill_vert == ' ')
10423 return '|';
10424 else
10425 return fill_vert;
10426}
10427#endif
10428
10429/*
10430 * Return TRUE if redrawing should currently be done.
10431 */
10432 int
10433redrawing()
10434{
10435 return (!RedrawingDisabled
10436 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
10437}
10438
10439/*
10440 * Return TRUE if printing messages should currently be done.
10441 */
10442 int
10443messaging()
10444{
10445 return (!(p_lz && char_avail() && !KeyTyped));
10446}
10447
10448/*
10449 * Show current status info in ruler and various other places
10450 * If always is FALSE, only show ruler if position has changed.
10451 */
10452 void
10453showruler(always)
10454 int always;
10455{
10456 if (!always && !redrawing())
10457 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010458#ifdef FEAT_INS_EXPAND
10459 if (pum_visible())
10460 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010461# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +000010462 /* Don't redraw right now, do it later. */
10463 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010464# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +000010465 return;
10466 }
10467#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010468#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010469 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010470 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010471 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010473 else
10474#endif
10475#ifdef FEAT_CMDL_INFO
10476 win_redr_ruler(curwin, always);
10477#endif
10478
10479#ifdef FEAT_TITLE
10480 if (need_maketitle
10481# ifdef FEAT_STL_OPT
10482 || (p_icon && (stl_syntax & STL_IN_ICON))
10483 || (p_title && (stl_syntax & STL_IN_TITLE))
10484# endif
10485 )
10486 maketitle();
10487#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010488#ifdef FEAT_WINDOWS
10489 /* Redraw the tab pages line if needed. */
10490 if (redraw_tabline)
10491 draw_tabline();
10492#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010493}
10494
10495#ifdef FEAT_CMDL_INFO
10496 static void
10497win_redr_ruler(wp, always)
10498 win_T *wp;
10499 int always;
10500{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010501#define RULER_BUF_LEN 70
10502 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010503 int row;
10504 int fillchar;
10505 int attr;
10506 int empty_line = FALSE;
10507 colnr_T virtcol;
10508 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010509 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010510 int o;
10511#ifdef FEAT_VERTSPLIT
10512 int this_ru_col;
10513 int off = 0;
10514 int width = Columns;
10515# define WITH_OFF(x) x
10516# define WITH_WIDTH(x) x
10517#else
10518# define WITH_OFF(x) 0
10519# define WITH_WIDTH(x) Columns
10520# define this_ru_col ru_col
10521#endif
10522
10523 /* If 'ruler' off or redrawing disabled, don't do anything */
10524 if (!p_ru)
10525 return;
10526
10527 /*
10528 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10529 * after deleting lines, before cursor.lnum is corrected.
10530 */
10531 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10532 return;
10533
10534#ifdef FEAT_INS_EXPAND
10535 /* Don't draw the ruler while doing insert-completion, it might overwrite
10536 * the (long) mode message. */
10537# ifdef FEAT_WINDOWS
10538 if (wp == lastwin && lastwin->w_status_height == 0)
10539# endif
10540 if (edit_submode != NULL)
10541 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010542 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10543 if (pum_visible())
10544 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010545#endif
10546
10547#ifdef FEAT_STL_OPT
10548 if (*p_ruf)
10549 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010550 int save_called_emsg = called_emsg;
10551
10552 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010553 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010554 if (called_emsg)
10555 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010556 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010557 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010558 return;
10559 }
10560#endif
10561
10562 /*
10563 * Check if not in Insert mode and the line is empty (will show "0-1").
10564 */
10565 if (!(State & INSERT)
10566 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10567 empty_line = TRUE;
10568
10569 /*
10570 * Only draw the ruler when something changed.
10571 */
10572 validate_virtcol_win(wp);
10573 if ( redraw_cmdline
10574 || always
10575 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10576 || wp->w_cursor.col != wp->w_ru_cursor.col
10577 || wp->w_virtcol != wp->w_ru_virtcol
10578#ifdef FEAT_VIRTUALEDIT
10579 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10580#endif
10581 || wp->w_topline != wp->w_ru_topline
10582 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10583#ifdef FEAT_DIFF
10584 || wp->w_topfill != wp->w_ru_topfill
10585#endif
10586 || empty_line != wp->w_ru_empty)
10587 {
10588 cursor_off();
10589#ifdef FEAT_WINDOWS
10590 if (wp->w_status_height)
10591 {
10592 row = W_WINROW(wp) + wp->w_height;
10593 fillchar = fillchar_status(&attr, wp == curwin);
10594# ifdef FEAT_VERTSPLIT
10595 off = W_WINCOL(wp);
10596 width = W_WIDTH(wp);
10597# endif
10598 }
10599 else
10600#endif
10601 {
10602 row = Rows - 1;
10603 fillchar = ' ';
10604 attr = 0;
10605#ifdef FEAT_VERTSPLIT
10606 width = Columns;
10607 off = 0;
10608#endif
10609 }
10610
10611 /* In list mode virtcol needs to be recomputed */
10612 virtcol = wp->w_virtcol;
10613 if (wp->w_p_list && lcs_tab1 == NUL)
10614 {
10615 wp->w_p_list = FALSE;
10616 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10617 wp->w_p_list = TRUE;
10618 }
10619
10620 /*
10621 * Some sprintfs return the length, some return a pointer.
10622 * To avoid portability problems we use strlen() here.
10623 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010624 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010625 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10626 ? 0L
10627 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010628 len = STRLEN(buffer);
10629 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010630 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10631 (int)virtcol + 1);
10632
10633 /*
10634 * Add a "50%" if there is room for it.
10635 * On the last line, don't print in the last column (scrolls the
10636 * screen up on some terminals).
10637 */
10638 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010639 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010640 o = i + vim_strsize(buffer + i + 1);
10641#ifdef FEAT_WINDOWS
10642 if (wp->w_status_height == 0) /* can't use last char of screen */
10643#endif
10644 ++o;
10645#ifdef FEAT_VERTSPLIT
10646 this_ru_col = ru_col - (Columns - width);
10647 if (this_ru_col < 0)
10648 this_ru_col = 0;
10649#endif
10650 /* Never use more than half the window/screen width, leave the other
10651 * half for the filename. */
10652 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
10653 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
10654 if (this_ru_col + o < WITH_WIDTH(width))
10655 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010010656 /* need at least 3 chars left for get_rel_pos() + NUL */
10657 while (this_ru_col + o < WITH_WIDTH(width) && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010658 {
10659#ifdef FEAT_MBYTE
10660 if (has_mbyte)
10661 i += (*mb_char2bytes)(fillchar, buffer + i);
10662 else
10663#endif
10664 buffer[i++] = fillchar;
10665 ++o;
10666 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010667 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010668 }
10669 /* Truncate at window boundary. */
10670#ifdef FEAT_MBYTE
10671 if (has_mbyte)
10672 {
10673 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010674 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010675 {
10676 o += (*mb_ptr2cells)(buffer + i);
10677 if (this_ru_col + o > WITH_WIDTH(width))
10678 {
10679 buffer[i] = NUL;
10680 break;
10681 }
10682 }
10683 }
10684 else
10685#endif
10686 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
10687 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
10688
10689 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
10690 i = redraw_cmdline;
10691 screen_fill(row, row + 1,
10692 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
10693 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
10694 fillchar, fillchar, attr);
10695 /* don't redraw the cmdline because of showing the ruler */
10696 redraw_cmdline = i;
10697 wp->w_ru_cursor = wp->w_cursor;
10698 wp->w_ru_virtcol = wp->w_virtcol;
10699 wp->w_ru_empty = empty_line;
10700 wp->w_ru_topline = wp->w_topline;
10701 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10702#ifdef FEAT_DIFF
10703 wp->w_ru_topfill = wp->w_topfill;
10704#endif
10705 }
10706}
10707#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010708
10709#if defined(FEAT_LINEBREAK) || defined(PROTO)
10710/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010711 * Return the width of the 'number' and 'relativenumber' column.
10712 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010713 * Otherwise it depends on 'numberwidth' and the line count.
10714 */
10715 int
10716number_width(wp)
10717 win_T *wp;
10718{
10719 int n;
10720 linenr_T lnum;
10721
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020010722 if (wp->w_p_rnu && !wp->w_p_nu)
10723 /* cursor line shows "0" */
10724 lnum = wp->w_height;
10725 else
10726 /* cursor line shows absolute line number */
10727 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020010728
Bram Moolenaar6b314672015-03-20 15:42:10 +010010729 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010730 return wp->w_nrwidth_width;
10731 wp->w_nrwidth_line_count = lnum;
10732
10733 n = 0;
10734 do
10735 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010736 lnum /= 10;
10737 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010738 } while (lnum > 0);
10739
10740 /* 'numberwidth' gives the minimal width plus one */
10741 if (n < wp->w_p_nuw - 1)
10742 n = wp->w_p_nuw - 1;
10743
10744 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010010745 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010746 return n;
10747}
10748#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010749
10750/*
10751 * Return the current cursor column. This is the actual position on the
10752 * screen. First column is 0.
10753 */
10754 int
10755screen_screencol()
10756{
10757 return screen_cur_col;
10758}
10759
10760/*
10761 * Return the current cursor row. This is the actual position on the screen.
10762 * First row is 0.
10763 */
10764 int
10765screen_screenrow()
10766{
10767 return screen_cur_row;
10768}