blob: 698f969687cd40615cdd94a681667acd3fe79a6a [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' */
112#endif
113
114/*
115 * Buffer for one screen line (characters and attributes).
116 */
117static schar_T *current_ScreenLine;
118
119static void win_update __ARGS((win_T *wp));
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000120static 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 +0000121#ifdef FEAT_FOLDING
122static void fold_line __ARGS((win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row));
123static void fill_foldcolumn __ARGS((char_u *p, win_T *wp, int closed, linenr_T lnum));
124static void copy_text_attr __ARGS((int off, char_u *buf, int len, int attr));
125#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000126static int win_line __ARGS((win_T *, linenr_T, int, int, int nochange));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127static int char_needs_redraw __ARGS((int off_from, int off_to, int cols));
128#ifdef FEAT_RIGHTLEFT
129static void screen_line __ARGS((int row, int coloff, int endcol, int clear_width, int rlflag));
130# define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c), (rl))
131#else
132static void screen_line __ARGS((int row, int coloff, int endcol, int clear_width));
133# define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c))
134#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135#ifdef FEAT_VERTSPLIT
136static void draw_vsep_win __ARGS((win_T *wp, int row));
137#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +0000138#ifdef FEAT_STL_OPT
Bram Moolenaar362f3562009-11-03 16:20:34 +0000139static void redraw_custom_statusline __ARGS((win_T *wp));
Bram Moolenaar238a5642006-02-21 22:12:05 +0000140#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarde993ea2014-06-17 23:18:01 +0200142# define SEARCH_HL_PRIORITY 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143static void start_search_hl __ARGS((void));
144static void end_search_hl __ARGS((void));
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200145static void init_search_hl __ARGS((win_T *wp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146static void prepare_search_hl __ARGS((win_T *wp, linenr_T lnum));
Bram Moolenaarb3414592014-06-17 17:48:32 +0200147static void next_search_hl __ARGS((win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol, matchitem_T *cur));
148static 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 +0000149#endif
150static void screen_start_highlight __ARGS((int attr));
151static void screen_char __ARGS((unsigned off, int row, int col));
152#ifdef FEAT_MBYTE
153static void screen_char_2 __ARGS((unsigned off, int row, int col));
154#endif
155static void screenclear2 __ARGS((void));
156static void lineclear __ARGS((unsigned off, int width));
157static void lineinvalid __ARGS((unsigned off, int width));
158#ifdef FEAT_VERTSPLIT
159static void linecopy __ARGS((int to, int from, win_T *wp));
160static void redraw_block __ARGS((int row, int end, win_T *wp));
161#endif
162static int win_do_lines __ARGS((win_T *wp, int row, int line_count, int mayclear, int del));
163static void win_rest_invalid __ARGS((win_T *wp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164static void msg_pos_mode __ARGS((void));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000165#if defined(FEAT_WINDOWS)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000166static void draw_tabline __ARGS((void));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000167#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
169static int fillchar_status __ARGS((int *attr, int is_curwin));
170#endif
171#ifdef FEAT_VERTSPLIT
172static int fillchar_vsep __ARGS((int *attr));
173#endif
174#ifdef FEAT_STL_OPT
Bram Moolenaar9372a112005-12-06 19:59:18 +0000175static void win_redr_custom __ARGS((win_T *wp, int draw_ruler));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176#endif
177#ifdef FEAT_CMDL_INFO
178static void win_redr_ruler __ARGS((win_T *wp, int always));
179#endif
180
181#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
182/* Ugly global: overrule attribute used by screen_char() */
183static int screen_char_attr = 0;
184#endif
185
186/*
187 * Redraw the current window later, with update_screen(type).
188 * Set must_redraw only if not already set to a higher value.
189 * e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
190 */
191 void
192redraw_later(type)
193 int type;
194{
195 redraw_win_later(curwin, type);
196}
197
198 void
199redraw_win_later(wp, type)
200 win_T *wp;
201 int type;
202{
203 if (wp->w_redr_type < type)
204 {
205 wp->w_redr_type = type;
206 if (type >= NOT_VALID)
207 wp->w_lines_valid = 0;
208 if (must_redraw < type) /* must_redraw is the maximum of all windows */
209 must_redraw = type;
210 }
211}
212
213/*
214 * Force a complete redraw later. Also resets the highlighting. To be used
215 * after executing a shell command that messes up the screen.
216 */
217 void
218redraw_later_clear()
219{
220 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000221#ifdef FEAT_GUI
222 if (gui.in_use)
223 /* Use a code that will reset gui.highlight_mask in
224 * gui_stop_highlight(). */
225 screen_attr = HL_ALL + 1;
226 else
227#endif
228 /* Use attributes that is very unlikely to appear in text. */
229 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230}
231
232/*
233 * Mark all windows to be redrawn later.
234 */
235 void
236redraw_all_later(type)
237 int type;
238{
239 win_T *wp;
240
241 FOR_ALL_WINDOWS(wp)
242 {
243 redraw_win_later(wp, type);
244 }
245}
246
247/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000248 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 */
250 void
251redraw_curbuf_later(type)
252 int type;
253{
254 redraw_buf_later(curbuf, type);
255}
256
257 void
258redraw_buf_later(buf, type)
259 buf_T *buf;
260 int type;
261{
262 win_T *wp;
263
264 FOR_ALL_WINDOWS(wp)
265 {
266 if (wp->w_buffer == buf)
267 redraw_win_later(wp, type);
268 }
269}
270
271/*
Bram Moolenaar2951b772013-07-03 12:45:31 +0200272 * Redraw as soon as possible. When the command line is not scrolled redraw
273 * right away and restore what was on the command line.
274 * Return a code indicating what happened.
275 */
276 int
277redraw_asap(type)
278 int type;
279{
280 int rows;
281 int r;
282 int ret = 0;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200283 schar_T *screenline; /* copy from ScreenLines[] */
284 sattr_T *screenattr; /* copy from ScreenAttrs[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200285#ifdef FEAT_MBYTE
286 int i;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200287 u8char_T *screenlineUC = NULL; /* copy from ScreenLinesUC[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200288 u8char_T *screenlineC[MAX_MCO]; /* copy from ScreenLinesC[][] */
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200289 schar_T *screenline2 = NULL; /* copy from ScreenLines2[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200290#endif
291
292 redraw_later(type);
293 if (msg_scrolled || (State != NORMAL && State != NORMAL_BUSY))
294 return ret;
295
296 /* Allocate space to save the text displayed in the command line area. */
297 rows = Rows - cmdline_row;
298 screenline = (schar_T *)lalloc(
299 (long_u)(rows * Columns * sizeof(schar_T)), FALSE);
300 screenattr = (sattr_T *)lalloc(
301 (long_u)(rows * Columns * sizeof(sattr_T)), FALSE);
302 if (screenline == NULL || screenattr == NULL)
303 ret = 2;
304#ifdef FEAT_MBYTE
305 if (enc_utf8)
306 {
307 screenlineUC = (u8char_T *)lalloc(
308 (long_u)(rows * Columns * sizeof(u8char_T)), FALSE);
309 if (screenlineUC == NULL)
310 ret = 2;
311 for (i = 0; i < p_mco; ++i)
312 {
313 screenlineC[i] = (u8char_T *)lalloc(
314 (long_u)(rows * Columns * sizeof(u8char_T)), FALSE);
315 if (screenlineC[i] == NULL)
316 ret = 2;
317 }
318 }
319 if (enc_dbcs == DBCS_JPNU)
320 {
321 screenline2 = (schar_T *)lalloc(
322 (long_u)(rows * Columns * sizeof(schar_T)), FALSE);
323 if (screenline2 == NULL)
324 ret = 2;
325 }
326#endif
327
328 if (ret != 2)
329 {
330 /* Save the text displayed in the command line area. */
331 for (r = 0; r < rows; ++r)
332 {
333 mch_memmove(screenline + r * Columns,
334 ScreenLines + LineOffset[cmdline_row + r],
335 (size_t)Columns * sizeof(schar_T));
336 mch_memmove(screenattr + r * Columns,
337 ScreenAttrs + LineOffset[cmdline_row + r],
338 (size_t)Columns * sizeof(sattr_T));
339#ifdef FEAT_MBYTE
340 if (enc_utf8)
341 {
342 mch_memmove(screenlineUC + r * Columns,
343 ScreenLinesUC + LineOffset[cmdline_row + r],
344 (size_t)Columns * sizeof(u8char_T));
345 for (i = 0; i < p_mco; ++i)
346 mch_memmove(screenlineC[i] + r * Columns,
347 ScreenLinesC[r] + LineOffset[cmdline_row + r],
348 (size_t)Columns * sizeof(u8char_T));
349 }
350 if (enc_dbcs == DBCS_JPNU)
351 mch_memmove(screenline2 + r * Columns,
352 ScreenLines2 + LineOffset[cmdline_row + r],
353 (size_t)Columns * sizeof(schar_T));
354#endif
355 }
356
357 update_screen(0);
358 ret = 3;
359
360 if (must_redraw == 0)
361 {
362 int off = (int)(current_ScreenLine - ScreenLines);
363
364 /* Restore the text displayed in the command line area. */
365 for (r = 0; r < rows; ++r)
366 {
367 mch_memmove(current_ScreenLine,
368 screenline + r * Columns,
369 (size_t)Columns * sizeof(schar_T));
370 mch_memmove(ScreenAttrs + off,
371 screenattr + r * Columns,
372 (size_t)Columns * sizeof(sattr_T));
373#ifdef FEAT_MBYTE
374 if (enc_utf8)
375 {
376 mch_memmove(ScreenLinesUC + off,
377 screenlineUC + r * Columns,
378 (size_t)Columns * sizeof(u8char_T));
379 for (i = 0; i < p_mco; ++i)
380 mch_memmove(ScreenLinesC[i] + off,
381 screenlineC[i] + r * Columns,
382 (size_t)Columns * sizeof(u8char_T));
383 }
384 if (enc_dbcs == DBCS_JPNU)
385 mch_memmove(ScreenLines2 + off,
386 screenline2 + r * Columns,
387 (size_t)Columns * sizeof(schar_T));
388#endif
389 SCREEN_LINE(cmdline_row + r, 0, Columns, Columns, FALSE);
390 }
391 ret = 4;
392 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200393 }
394
395 vim_free(screenline);
396 vim_free(screenattr);
397#ifdef FEAT_MBYTE
398 if (enc_utf8)
399 {
400 vim_free(screenlineUC);
401 for (i = 0; i < p_mco; ++i)
402 vim_free(screenlineC[i]);
403 }
404 if (enc_dbcs == DBCS_JPNU)
405 vim_free(screenline2);
406#endif
407
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200408 /* Show the intro message when appropriate. */
409 maybe_intro_message();
410
411 setcursor();
412
Bram Moolenaar2951b772013-07-03 12:45:31 +0200413 return ret;
414}
415
416/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417 * Changed something in the current window, at buffer line "lnum", that
418 * requires that line and possibly other lines to be redrawn.
419 * Used when entering/leaving Insert mode with the cursor on a folded line.
420 * Used to remove the "$" from a change command.
421 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
422 * may become invalid and the whole window will have to be redrawn.
423 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424 void
425redrawWinline(lnum, invalid)
426 linenr_T lnum;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +0000427 int invalid UNUSED; /* window line height is invalid now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428{
429#ifdef FEAT_FOLDING
430 int i;
431#endif
432
433 if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
434 curwin->w_redraw_top = lnum;
435 if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
436 curwin->w_redraw_bot = lnum;
437 redraw_later(VALID);
438
439#ifdef FEAT_FOLDING
440 if (invalid)
441 {
442 /* A w_lines[] entry for this lnum has become invalid. */
443 i = find_wl_entry(curwin, lnum);
444 if (i >= 0)
445 curwin->w_lines[i].wl_valid = FALSE;
446 }
447#endif
448}
449
450/*
451 * update all windows that are editing the current buffer
452 */
453 void
454update_curbuf(type)
455 int type;
456{
457 redraw_curbuf_later(type);
458 update_screen(type);
459}
460
461/*
462 * update_screen()
463 *
464 * Based on the current value of curwin->w_topline, transfer a screenfull
465 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
466 */
467 void
468update_screen(type)
469 int type;
470{
471 win_T *wp;
472 static int did_intro = FALSE;
473#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
474 int did_one;
475#endif
476
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000477 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478 if (!screen_valid(TRUE))
479 return;
480
481 if (must_redraw)
482 {
483 if (type < must_redraw) /* use maximal type */
484 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000485
486 /* must_redraw is reset here, so that when we run into some weird
487 * reason to redraw while busy redrawing (e.g., asynchronous
488 * scrolling), or update_topline() in win_update() will cause a
489 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 must_redraw = 0;
491 }
492
493 /* Need to update w_lines[]. */
494 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
495 type = NOT_VALID;
496
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000497 /* Postpone the redrawing when it's not needed and when being called
498 * recursively. */
499 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500 {
501 redraw_later(type); /* remember type for next time */
502 must_redraw = type;
503 if (type > INVERTED_ALL)
504 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
505 return;
506 }
507
508 updating_screen = TRUE;
509#ifdef FEAT_SYN_HL
510 ++display_tick; /* let syntax code know we're in a next round of
511 * display updating */
512#endif
513
514 /*
515 * if the screen was scrolled up when displaying a message, scroll it down
516 */
517 if (msg_scrolled)
518 {
519 clear_cmdline = TRUE;
520 if (msg_scrolled > Rows - 5) /* clearing is faster */
521 type = CLEAR;
522 else if (type != CLEAR)
523 {
524 check_for_delay(FALSE);
525 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, NULL) == FAIL)
526 type = CLEAR;
527 FOR_ALL_WINDOWS(wp)
528 {
529 if (W_WINROW(wp) < msg_scrolled)
530 {
531 if (W_WINROW(wp) + wp->w_height > msg_scrolled
532 && wp->w_redr_type < REDRAW_TOP
533 && wp->w_lines_valid > 0
534 && wp->w_topline == wp->w_lines[0].wl_lnum)
535 {
536 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
537 wp->w_redr_type = REDRAW_TOP;
538 }
539 else
540 {
541 wp->w_redr_type = NOT_VALID;
542#ifdef FEAT_WINDOWS
543 if (W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp)
544 <= msg_scrolled)
545 wp->w_redr_status = TRUE;
546#endif
547 }
548 }
549 }
550 redraw_cmdline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000551#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000552 redraw_tabline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000553#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 }
555 msg_scrolled = 0;
556 need_wait_return = FALSE;
557 }
558
559 /* reset cmdline_row now (may have been changed temporarily) */
560 compute_cmdrow();
561
562 /* Check for changed highlighting */
563 if (need_highlight_changed)
564 highlight_changed();
565
566 if (type == CLEAR) /* first clear screen */
567 {
568 screenclear(); /* will reset clear_cmdline */
569 type = NOT_VALID;
570 }
571
572 if (clear_cmdline) /* going to clear cmdline (done below) */
573 check_for_delay(FALSE);
574
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000575#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200576 /* Force redraw when width of 'number' or 'relativenumber' column
577 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000578 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200579 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
580 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000581 curwin->w_redr_type = NOT_VALID;
582#endif
583
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 /*
585 * Only start redrawing if there is really something to do.
586 */
587 if (type == INVERTED)
588 update_curswant();
589 if (curwin->w_redr_type < type
590 && !((type == VALID
591 && curwin->w_lines[0].wl_valid
592#ifdef FEAT_DIFF
593 && curwin->w_topfill == curwin->w_old_topfill
594 && curwin->w_botfill == curwin->w_old_botfill
595#endif
596 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000598 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
600 && curwin->w_old_visual_mode == VIsual_mode
601 && (curwin->w_valid & VALID_VIRTCOL)
602 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 ))
604 curwin->w_redr_type = type;
605
Bram Moolenaar5a305422006-04-28 22:38:25 +0000606#ifdef FEAT_WINDOWS
607 /* Redraw the tab pages line if needed. */
608 if (redraw_tabline || type >= NOT_VALID)
609 draw_tabline();
610#endif
611
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612#ifdef FEAT_SYN_HL
613 /*
614 * Correct stored syntax highlighting info for changes in each displayed
615 * buffer. Each buffer must only be done once.
616 */
617 FOR_ALL_WINDOWS(wp)
618 {
619 if (wp->w_buffer->b_mod_set)
620 {
621# ifdef FEAT_WINDOWS
622 win_T *wwp;
623
624 /* Check if we already did this buffer. */
625 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
626 if (wwp->w_buffer == wp->w_buffer)
627 break;
628# endif
629 if (
630# ifdef FEAT_WINDOWS
631 wwp == wp &&
632# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200633 syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 syn_stack_apply_changes(wp->w_buffer);
635 }
636 }
637#endif
638
639 /*
640 * Go from top to bottom through the windows, redrawing the ones that need
641 * it.
642 */
643#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
644 did_one = FALSE;
645#endif
646#ifdef FEAT_SEARCH_EXTRA
647 search_hl.rm.regprog = NULL;
648#endif
649 FOR_ALL_WINDOWS(wp)
650 {
651 if (wp->w_redr_type != 0)
652 {
653 cursor_off();
654#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
655 if (!did_one)
656 {
657 did_one = TRUE;
658# ifdef FEAT_SEARCH_EXTRA
659 start_search_hl();
660# endif
661# ifdef FEAT_CLIPBOARD
662 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200663 if (clip_star.available && clip_isautosel_star())
664 clip_update_selection(&clip_star);
665 if (clip_plus.available && clip_isautosel_plus())
666 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667# endif
668#ifdef FEAT_GUI
669 /* Remove the cursor before starting to do anything, because
670 * scrolling may make it difficult to redraw the text under
671 * it. */
672 if (gui.in_use)
673 gui_undraw_cursor();
674#endif
675 }
676#endif
677 win_update(wp);
678 }
679
680#ifdef FEAT_WINDOWS
681 /* redraw status line after the window to minimize cursor movement */
682 if (wp->w_redr_status)
683 {
684 cursor_off();
685 win_redr_status(wp);
686 }
687#endif
688 }
689#if defined(FEAT_SEARCH_EXTRA)
690 end_search_hl();
691#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100692#ifdef FEAT_INS_EXPAND
693 /* May need to redraw the popup menu. */
694 if (pum_visible())
695 pum_redraw();
696#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697
698#ifdef FEAT_WINDOWS
699 /* Reset b_mod_set flags. Going through all windows is probably faster
700 * than going through all buffers (there could be many buffers). */
701 for (wp = firstwin; wp != NULL; wp = wp->w_next)
702 wp->w_buffer->b_mod_set = FALSE;
703#else
704 curbuf->b_mod_set = FALSE;
705#endif
706
707 updating_screen = FALSE;
708#ifdef FEAT_GUI
709 gui_may_resize_shell();
710#endif
711
712 /* Clear or redraw the command line. Done last, because scrolling may
713 * mess up the command line. */
714 if (clear_cmdline || redraw_cmdline)
715 showmode();
716
717 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200718 if (!did_intro)
719 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 did_intro = TRUE;
721
722#ifdef FEAT_GUI
723 /* Redraw the cursor and update the scrollbars when all screen updating is
724 * done. */
725 if (gui.in_use)
726 {
727 out_flush(); /* required before updating the cursor */
728 if (did_one)
729 gui_update_cursor(FALSE, FALSE);
730 gui_update_scrollbars(FALSE);
731 }
732#endif
733}
734
Bram Moolenaar860cae12010-06-05 23:22:07 +0200735#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200736/*
737 * Return TRUE if the cursor line in window "wp" may be concealed, according
738 * to the 'concealcursor' option.
739 */
740 int
741conceal_cursor_line(wp)
742 win_T *wp;
743{
744 int c;
745
746 if (*wp->w_p_cocu == NUL)
747 return FALSE;
748 if (get_real_state() & VISUAL)
749 c = 'v';
750 else if (State & INSERT)
751 c = 'i';
752 else if (State & NORMAL)
753 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200754 else if (State & CMDLINE)
755 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200756 else
757 return FALSE;
758 return vim_strchr(wp->w_p_cocu, c) != NULL;
759}
760
761/*
762 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
763 */
764 void
Bram Moolenaar8e469272010-07-28 19:38:16 +0200765conceal_check_cursur_line()
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200766{
767 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
768 {
769 need_cursor_line_redraw = TRUE;
770 /* Need to recompute cursor column, e.g., when starting Visual mode
771 * without concealing. */
772 curs_columns(TRUE);
773 }
774}
775
Bram Moolenaar860cae12010-06-05 23:22:07 +0200776 void
777update_single_line(wp, lnum)
778 win_T *wp;
779 linenr_T lnum;
780{
781 int row;
782 int j;
783
784 if (lnum >= wp->w_topline && lnum < wp->w_botline
Bram Moolenaar370df582010-06-22 05:16:38 +0200785 && foldedCount(wp, lnum, &win_foldinfo) == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200786 {
787# ifdef FEAT_GUI
788 /* Remove the cursor before starting to do anything, because scrolling
789 * may make it difficult to redraw the text under it. */
790 if (gui.in_use)
791 gui_undraw_cursor();
792# endif
793 row = 0;
794 for (j = 0; j < wp->w_lines_valid; ++j)
795 {
796 if (lnum == wp->w_lines[j].wl_lnum)
797 {
798 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200799# ifdef FEAT_SEARCH_EXTRA
800 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200801 start_search_hl();
802 prepare_search_hl(wp, lnum);
803# endif
804 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size, FALSE);
805# if defined(FEAT_SEARCH_EXTRA)
806 end_search_hl();
807# endif
808 break;
809 }
810 row += wp->w_lines[j].wl_size;
811 }
812# ifdef FEAT_GUI
813 /* Redraw the cursor */
814 if (gui.in_use)
815 {
816 out_flush(); /* required before updating the cursor */
817 gui_update_cursor(FALSE, FALSE);
818 }
819# endif
820 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200821 need_cursor_line_redraw = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +0200822}
823#endif
824
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825#if defined(FEAT_SIGNS) || defined(FEAT_GUI)
826static void update_prepare __ARGS((void));
827static void update_finish __ARGS((void));
828
829/*
830 * Prepare for updating one or more windows.
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000831 * Caller must check for "updating_screen" already set to avoid recursiveness.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 */
833 static void
834update_prepare()
835{
836 cursor_off();
837 updating_screen = TRUE;
838#ifdef FEAT_GUI
839 /* Remove the cursor before starting to do anything, because scrolling may
840 * make it difficult to redraw the text under it. */
841 if (gui.in_use)
842 gui_undraw_cursor();
843#endif
844#ifdef FEAT_SEARCH_EXTRA
845 start_search_hl();
846#endif
847}
848
849/*
850 * Finish updating one or more windows.
851 */
852 static void
853update_finish()
854{
855 if (redraw_cmdline)
856 showmode();
857
858# ifdef FEAT_SEARCH_EXTRA
859 end_search_hl();
860# endif
861
862 updating_screen = FALSE;
863
864# ifdef FEAT_GUI
865 gui_may_resize_shell();
866
867 /* Redraw the cursor and update the scrollbars when all screen updating is
868 * done. */
869 if (gui.in_use)
870 {
871 out_flush(); /* required before updating the cursor */
872 gui_update_cursor(FALSE, FALSE);
873 gui_update_scrollbars(FALSE);
874 }
875# endif
876}
877#endif
878
879#if defined(FEAT_SIGNS) || defined(PROTO)
880 void
881update_debug_sign(buf, lnum)
882 buf_T *buf;
883 linenr_T lnum;
884{
885 win_T *wp;
886 int doit = FALSE;
887
888# ifdef FEAT_FOLDING
889 win_foldinfo.fi_level = 0;
890# endif
891
892 /* update/delete a specific mark */
893 FOR_ALL_WINDOWS(wp)
894 {
895 if (buf != NULL && lnum > 0)
896 {
897 if (wp->w_buffer == buf && lnum >= wp->w_topline
898 && lnum < wp->w_botline)
899 {
900 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
901 wp->w_redraw_top = lnum;
902 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
903 wp->w_redraw_bot = lnum;
904 redraw_win_later(wp, VALID);
905 }
906 }
907 else
908 redraw_win_later(wp, VALID);
909 if (wp->w_redr_type != 0)
910 doit = TRUE;
911 }
912
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100913 /* Return when there is nothing to do, screen updating is already
914 * happening (recursive call) or still starting up. */
915 if (!doit || updating_screen
916#ifdef FEAT_GUI
917 || gui.starting
918#endif
919 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 return;
921
922 /* update all windows that need updating */
923 update_prepare();
924
925# ifdef FEAT_WINDOWS
926 for (wp = firstwin; wp; wp = wp->w_next)
927 {
928 if (wp->w_redr_type != 0)
929 win_update(wp);
930 if (wp->w_redr_status)
931 win_redr_status(wp);
932 }
933# else
934 if (curwin->w_redr_type != 0)
935 win_update(curwin);
936# endif
937
938 update_finish();
939}
940#endif
941
942
943#if defined(FEAT_GUI) || defined(PROTO)
944/*
945 * Update a single window, its status line and maybe the command line msg.
946 * Used for the GUI scrollbar.
947 */
948 void
949updateWindow(wp)
950 win_T *wp;
951{
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000952 /* return if already busy updating */
953 if (updating_screen)
954 return;
955
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 update_prepare();
957
958#ifdef FEAT_CLIPBOARD
959 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200960 if (clip_star.available && clip_isautosel_star())
961 clip_update_selection(&clip_star);
962 if (clip_plus.available && clip_isautosel_plus())
963 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000965
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000967
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968#ifdef FEAT_WINDOWS
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000969 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000970 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000971 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000972
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 if (wp->w_redr_status
974# ifdef FEAT_CMDL_INFO
975 || p_ru
976# endif
977# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000978 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979# endif
980 )
981 win_redr_status(wp);
982#endif
983
984 update_finish();
985}
986#endif
987
988/*
989 * Update a single window.
990 *
991 * This may cause the windows below it also to be redrawn (when clearing the
992 * screen or scrolling lines).
993 *
994 * How the window is redrawn depends on wp->w_redr_type. Each type also
995 * implies the one below it.
996 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000997 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
999 * INVERTED redraw the changed part of the Visual area
1000 * INVERTED_ALL redraw the whole Visual area
1001 * VALID 1. scroll up/down to adjust for a changed w_topline
1002 * 2. update lines at the top when scrolled down
1003 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001004 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 * b_mod_top and b_mod_bot.
1006 * - if wp->w_redraw_top non-zero, redraw lines between
1007 * wp->w_redraw_top and wp->w_redr_bot.
1008 * - continue redrawing when syntax status is invalid.
1009 * 4. if scrolled up, update lines at the bottom.
1010 * This results in three areas that may need updating:
1011 * top: from first row to top_end (when scrolled down)
1012 * mid: from mid_start to mid_end (update inversion or changed text)
1013 * bot: from bot_start to last row (when scrolled up)
1014 */
1015 static void
1016win_update(wp)
1017 win_T *wp;
1018{
1019 buf_T *buf = wp->w_buffer;
1020 int type;
1021 int top_end = 0; /* Below last row of the top area that needs
1022 updating. 0 when no top area updating. */
1023 int mid_start = 999;/* first row of the mid area that needs
1024 updating. 999 when no mid area updating. */
1025 int mid_end = 0; /* Below last row of the mid area that needs
1026 updating. 0 when no mid area updating. */
1027 int bot_start = 999;/* first row of the bot area that needs
1028 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 int scrolled_down = FALSE; /* TRUE when scrolled down when
1030 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001032 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033 int top_to_mod = FALSE; /* redraw above mod_top */
1034#endif
1035
1036 int row; /* current window row to display */
1037 linenr_T lnum; /* current buffer lnum to display */
1038 int idx; /* current index in w_lines[] */
1039 int srow; /* starting row of the current line */
1040
1041 int eof = FALSE; /* if TRUE, we hit the end of the file */
1042 int didline = FALSE; /* if TRUE, we finished the last line */
1043 int i;
1044 long j;
1045 static int recursive = FALSE; /* being called recursively */
1046 int old_botline = wp->w_botline;
1047#ifdef FEAT_FOLDING
1048 long fold_count;
1049#endif
1050#ifdef FEAT_SYN_HL
1051 /* remember what happened to the previous line, to know if
1052 * check_visual_highlight() can be used */
1053#define DID_NONE 1 /* didn't update a line */
1054#define DID_LINE 2 /* updated a normal line */
1055#define DID_FOLD 3 /* updated a folded line */
1056 int did_update = DID_NONE;
1057 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1058#endif
1059 linenr_T mod_top = 0;
1060 linenr_T mod_bot = 0;
1061#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1062 int save_got_int;
1063#endif
1064
1065 type = wp->w_redr_type;
1066
1067 if (type == NOT_VALID)
1068 {
1069#ifdef FEAT_WINDOWS
1070 wp->w_redr_status = TRUE;
1071#endif
1072 wp->w_lines_valid = 0;
1073 }
1074
1075 /* Window is zero-height: nothing to draw. */
1076 if (wp->w_height == 0)
1077 {
1078 wp->w_redr_type = 0;
1079 return;
1080 }
1081
1082#ifdef FEAT_VERTSPLIT
1083 /* Window is zero-width: Only need to draw the separator. */
1084 if (wp->w_width == 0)
1085 {
1086 /* draw the vertical separator right of this window */
1087 draw_vsep_win(wp, 0);
1088 wp->w_redr_type = 0;
1089 return;
1090 }
1091#endif
1092
1093#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001094 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095#endif
1096
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001097#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001098 /* Force redraw when width of 'number' or 'relativenumber' column
1099 * changes. */
1100 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001101 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001102 {
1103 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001104 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001105 }
1106 else
1107#endif
1108
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1110 {
1111 /*
1112 * When there are both inserted/deleted lines and specific lines to be
1113 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1114 * everything (only happens when redrawing is off for while).
1115 */
1116 type = NOT_VALID;
1117 }
1118 else
1119 {
1120 /*
1121 * Set mod_top to the first line that needs displaying because of
1122 * changes. Set mod_bot to the first line after the changes.
1123 */
1124 mod_top = wp->w_redraw_top;
1125 if (wp->w_redraw_bot != 0)
1126 mod_bot = wp->w_redraw_bot + 1;
1127 else
1128 mod_bot = 0;
1129 wp->w_redraw_top = 0; /* reset for next time */
1130 wp->w_redraw_bot = 0;
1131 if (buf->b_mod_set)
1132 {
1133 if (mod_top == 0 || mod_top > buf->b_mod_top)
1134 {
1135 mod_top = buf->b_mod_top;
1136#ifdef FEAT_SYN_HL
1137 /* Need to redraw lines above the change that may be included
1138 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001139 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001141 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 if (mod_top < 1)
1143 mod_top = 1;
1144 }
1145#endif
1146 }
1147 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1148 mod_bot = buf->b_mod_bot;
1149
1150#ifdef FEAT_SEARCH_EXTRA
1151 /* When 'hlsearch' is on and using a multi-line search pattern, a
1152 * change in one line may make the Search highlighting in a
1153 * previous line invalid. Simple solution: redraw all visible
1154 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001155 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001157 if (search_hl.rm.regprog != NULL
1158 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001160 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001161 {
1162 cur = wp->w_match_head;
1163 while (cur != NULL)
1164 {
1165 if (cur->match.regprog != NULL
1166 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001167 {
1168 top_to_mod = TRUE;
1169 break;
1170 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001171 cur = cur->next;
1172 }
1173 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174#endif
1175 }
1176#ifdef FEAT_FOLDING
1177 if (mod_top != 0 && hasAnyFolding(wp))
1178 {
1179 linenr_T lnumt, lnumb;
1180
1181 /*
1182 * A change in a line can cause lines above it to become folded or
1183 * unfolded. Find the top most buffer line that may be affected.
1184 * If the line was previously folded and displayed, get the first
1185 * line of that fold. If the line is folded now, get the first
1186 * folded line. Use the minimum of these two.
1187 */
1188
1189 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1190 * the line below it. If there is no valid entry, use w_topline.
1191 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1192 * to this line. If there is no valid entry, use MAXLNUM. */
1193 lnumt = wp->w_topline;
1194 lnumb = MAXLNUM;
1195 for (i = 0; i < wp->w_lines_valid; ++i)
1196 if (wp->w_lines[i].wl_valid)
1197 {
1198 if (wp->w_lines[i].wl_lastlnum < mod_top)
1199 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1200 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1201 {
1202 lnumb = wp->w_lines[i].wl_lnum;
1203 /* When there is a fold column it might need updating
1204 * in the next line ("J" just above an open fold). */
1205 if (wp->w_p_fdc > 0)
1206 ++lnumb;
1207 }
1208 }
1209
1210 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1211 if (mod_top > lnumt)
1212 mod_top = lnumt;
1213
1214 /* Now do the same for the bottom line (one above mod_bot). */
1215 --mod_bot;
1216 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1217 ++mod_bot;
1218 if (mod_bot < lnumb)
1219 mod_bot = lnumb;
1220 }
1221#endif
1222
1223 /* When a change starts above w_topline and the end is below
1224 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001225 * If the end of the change is above w_topline: do like no change was
1226 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 if (mod_top != 0 && mod_top < wp->w_topline)
1228 {
1229 if (mod_bot > wp->w_topline)
1230 mod_top = wp->w_topline;
1231#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001232 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 top_end = 1;
1234#endif
1235 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001236
1237 /* When line numbers are displayed need to redraw all lines below
1238 * inserted/deleted lines. */
1239 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1240 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 }
1242
1243 /*
1244 * When only displaying the lines at the top, set top_end. Used when
1245 * window has scrolled down for msg_scrolled.
1246 */
1247 if (type == REDRAW_TOP)
1248 {
1249 j = 0;
1250 for (i = 0; i < wp->w_lines_valid; ++i)
1251 {
1252 j += wp->w_lines[i].wl_size;
1253 if (j >= wp->w_upd_rows)
1254 {
1255 top_end = j;
1256 break;
1257 }
1258 }
1259 if (top_end == 0)
1260 /* not found (cannot happen?): redraw everything */
1261 type = NOT_VALID;
1262 else
1263 /* top area defined, the rest is VALID */
1264 type = VALID;
1265 }
1266
Bram Moolenaar367329b2007-08-30 11:53:22 +00001267 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001268 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1269 * non-zero and thus not FALSE) will indicate that screenclear() was not
1270 * called. */
1271 if (screen_cleared)
1272 screen_cleared = MAYBE;
1273
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 /*
1275 * If there are no changes on the screen that require a complete redraw,
1276 * handle three cases:
1277 * 1: we are off the top of the screen by a few lines: scroll down
1278 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1279 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1280 * w_lines[] that needs updating.
1281 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001282 if ((type == VALID || type == SOME_VALID
1283 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284#ifdef FEAT_DIFF
1285 && !wp->w_botfill && !wp->w_old_botfill
1286#endif
1287 )
1288 {
1289 if (mod_top != 0 && wp->w_topline == mod_top)
1290 {
1291 /*
1292 * w_topline is the first changed line, the scrolling will be done
1293 * further down.
1294 */
1295 }
1296 else if (wp->w_lines[0].wl_valid
1297 && (wp->w_topline < wp->w_lines[0].wl_lnum
1298#ifdef FEAT_DIFF
1299 || (wp->w_topline == wp->w_lines[0].wl_lnum
1300 && wp->w_topfill > wp->w_old_topfill)
1301#endif
1302 ))
1303 {
1304 /*
1305 * New topline is above old topline: May scroll down.
1306 */
1307#ifdef FEAT_FOLDING
1308 if (hasAnyFolding(wp))
1309 {
1310 linenr_T ln;
1311
1312 /* count the number of lines we are off, counting a sequence
1313 * of folded lines as one */
1314 j = 0;
1315 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1316 {
1317 ++j;
1318 if (j >= wp->w_height - 2)
1319 break;
1320 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1321 }
1322 }
1323 else
1324#endif
1325 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1326 if (j < wp->w_height - 2) /* not too far off */
1327 {
1328 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1329#ifdef FEAT_DIFF
1330 /* insert extra lines for previously invisible filler lines */
1331 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1332 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1333 - wp->w_old_topfill;
1334#endif
1335 if (i < wp->w_height - 2) /* less than a screen off */
1336 {
1337 /*
1338 * Try to insert the correct number of lines.
1339 * If not the last window, delete the lines at the bottom.
1340 * win_ins_lines may fail when the terminal can't do it.
1341 */
1342 if (i > 0)
1343 check_for_delay(FALSE);
1344 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1345 {
1346 if (wp->w_lines_valid != 0)
1347 {
1348 /* Need to update rows that are new, stop at the
1349 * first one that scrolled down. */
1350 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352
1353 /* Move the entries that were scrolled, disable
1354 * the entries for the lines to be redrawn. */
1355 if ((wp->w_lines_valid += j) > wp->w_height)
1356 wp->w_lines_valid = wp->w_height;
1357 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1358 wp->w_lines[idx] = wp->w_lines[idx - j];
1359 while (idx >= 0)
1360 wp->w_lines[idx--].wl_valid = FALSE;
1361 }
1362 }
1363 else
1364 mid_start = 0; /* redraw all lines */
1365 }
1366 else
1367 mid_start = 0; /* redraw all lines */
1368 }
1369 else
1370 mid_start = 0; /* redraw all lines */
1371 }
1372 else
1373 {
1374 /*
1375 * New topline is at or below old topline: May scroll up.
1376 * When topline didn't change, find first entry in w_lines[] that
1377 * needs updating.
1378 */
1379
1380 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1381 j = -1;
1382 row = 0;
1383 for (i = 0; i < wp->w_lines_valid; i++)
1384 {
1385 if (wp->w_lines[i].wl_valid
1386 && wp->w_lines[i].wl_lnum == wp->w_topline)
1387 {
1388 j = i;
1389 break;
1390 }
1391 row += wp->w_lines[i].wl_size;
1392 }
1393 if (j == -1)
1394 {
1395 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1396 * lines */
1397 mid_start = 0;
1398 }
1399 else
1400 {
1401 /*
1402 * Try to delete the correct number of lines.
1403 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1404 */
1405#ifdef FEAT_DIFF
1406 /* If the topline didn't change, delete old filler lines,
1407 * otherwise delete filler lines of the new topline... */
1408 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1409 row += wp->w_old_topfill;
1410 else
1411 row += diff_check_fill(wp, wp->w_topline);
1412 /* ... but don't delete new filler lines. */
1413 row -= wp->w_topfill;
1414#endif
1415 if (row > 0)
1416 {
1417 check_for_delay(FALSE);
1418 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1419 bot_start = wp->w_height - row;
1420 else
1421 mid_start = 0; /* redraw all lines */
1422 }
1423 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1424 {
1425 /*
1426 * Skip the lines (below the deleted lines) that are still
1427 * valid and don't need redrawing. Copy their info
1428 * upwards, to compensate for the deleted lines. Set
1429 * bot_start to the first row that needs redrawing.
1430 */
1431 bot_start = 0;
1432 idx = 0;
1433 for (;;)
1434 {
1435 wp->w_lines[idx] = wp->w_lines[j];
1436 /* stop at line that didn't fit, unless it is still
1437 * valid (no lines deleted) */
1438 if (row > 0 && bot_start + row
1439 + (int)wp->w_lines[j].wl_size > wp->w_height)
1440 {
1441 wp->w_lines_valid = idx + 1;
1442 break;
1443 }
1444 bot_start += wp->w_lines[idx++].wl_size;
1445
1446 /* stop at the last valid entry in w_lines[].wl_size */
1447 if (++j >= wp->w_lines_valid)
1448 {
1449 wp->w_lines_valid = idx;
1450 break;
1451 }
1452 }
1453#ifdef FEAT_DIFF
1454 /* Correct the first entry for filler lines at the top
1455 * when it won't get updated below. */
1456 if (wp->w_p_diff && bot_start > 0)
1457 wp->w_lines[0].wl_size =
1458 plines_win_nofill(wp, wp->w_topline, TRUE)
1459 + wp->w_topfill;
1460#endif
1461 }
1462 }
1463 }
1464
1465 /* When starting redraw in the first line, redraw all lines. When
1466 * there is only one window it's probably faster to clear the screen
1467 * first. */
1468 if (mid_start == 0)
1469 {
1470 mid_end = wp->w_height;
1471 if (lastwin == firstwin)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001472 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001473 /* Clear the screen when it was not done by win_del_lines() or
1474 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1475 * then. */
1476 if (screen_cleared != TRUE)
1477 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001478#ifdef FEAT_WINDOWS
1479 /* The screen was cleared, redraw the tab pages line. */
1480 if (redraw_tabline)
1481 draw_tabline();
1482#endif
1483 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001485
1486 /* When win_del_lines() or win_ins_lines() caused the screen to be
1487 * cleared (only happens for the first window) or when screenclear()
1488 * was called directly above, "must_redraw" will have been set to
1489 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1490 if (screen_cleared == TRUE)
1491 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 }
1493 else
1494 {
1495 /* Not VALID or INVERTED: redraw all lines. */
1496 mid_start = 0;
1497 mid_end = wp->w_height;
1498 }
1499
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001500 if (type == SOME_VALID)
1501 {
1502 /* SOME_VALID: redraw all lines. */
1503 mid_start = 0;
1504 mid_end = wp->w_height;
1505 type = NOT_VALID;
1506 }
1507
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 /* check if we are updating or removing the inverted part */
1509 if ((VIsual_active && buf == curwin->w_buffer)
1510 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1511 {
1512 linenr_T from, to;
1513
1514 if (VIsual_active)
1515 {
1516 if (VIsual_active
1517 && (VIsual_mode != wp->w_old_visual_mode
1518 || type == INVERTED_ALL))
1519 {
1520 /*
1521 * If the type of Visual selection changed, redraw the whole
1522 * selection. Also when the ownership of the X selection is
1523 * gained or lost.
1524 */
1525 if (curwin->w_cursor.lnum < VIsual.lnum)
1526 {
1527 from = curwin->w_cursor.lnum;
1528 to = VIsual.lnum;
1529 }
1530 else
1531 {
1532 from = VIsual.lnum;
1533 to = curwin->w_cursor.lnum;
1534 }
1535 /* redraw more when the cursor moved as well */
1536 if (wp->w_old_cursor_lnum < from)
1537 from = wp->w_old_cursor_lnum;
1538 if (wp->w_old_cursor_lnum > to)
1539 to = wp->w_old_cursor_lnum;
1540 if (wp->w_old_visual_lnum < from)
1541 from = wp->w_old_visual_lnum;
1542 if (wp->w_old_visual_lnum > to)
1543 to = wp->w_old_visual_lnum;
1544 }
1545 else
1546 {
1547 /*
1548 * Find the line numbers that need to be updated: The lines
1549 * between the old cursor position and the current cursor
1550 * position. Also check if the Visual position changed.
1551 */
1552 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1553 {
1554 from = curwin->w_cursor.lnum;
1555 to = wp->w_old_cursor_lnum;
1556 }
1557 else
1558 {
1559 from = wp->w_old_cursor_lnum;
1560 to = curwin->w_cursor.lnum;
1561 if (from == 0) /* Visual mode just started */
1562 from = to;
1563 }
1564
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001565 if (VIsual.lnum != wp->w_old_visual_lnum
1566 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 {
1568 if (wp->w_old_visual_lnum < from
1569 && wp->w_old_visual_lnum != 0)
1570 from = wp->w_old_visual_lnum;
1571 if (wp->w_old_visual_lnum > to)
1572 to = wp->w_old_visual_lnum;
1573 if (VIsual.lnum < from)
1574 from = VIsual.lnum;
1575 if (VIsual.lnum > to)
1576 to = VIsual.lnum;
1577 }
1578 }
1579
1580 /*
1581 * If in block mode and changed column or curwin->w_curswant:
1582 * update all lines.
1583 * First compute the actual start and end column.
1584 */
1585 if (VIsual_mode == Ctrl_V)
1586 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001587 colnr_T fromc, toc;
1588#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1589 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590
Bram Moolenaar404406a2014-10-09 13:24:43 +02001591 if (curwin->w_p_lbr)
1592 ve_flags = VE_ALL;
1593#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar404406a2014-10-09 13:24:43 +02001595#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1596 ve_flags = save_ve_flags;
1597#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 ++toc;
1599 if (curwin->w_curswant == MAXCOL)
1600 toc = MAXCOL;
1601
1602 if (fromc != wp->w_old_cursor_fcol
1603 || toc != wp->w_old_cursor_lcol)
1604 {
1605 if (from > VIsual.lnum)
1606 from = VIsual.lnum;
1607 if (to < VIsual.lnum)
1608 to = VIsual.lnum;
1609 }
1610 wp->w_old_cursor_fcol = fromc;
1611 wp->w_old_cursor_lcol = toc;
1612 }
1613 }
1614 else
1615 {
1616 /* Use the line numbers of the old Visual area. */
1617 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1618 {
1619 from = wp->w_old_cursor_lnum;
1620 to = wp->w_old_visual_lnum;
1621 }
1622 else
1623 {
1624 from = wp->w_old_visual_lnum;
1625 to = wp->w_old_cursor_lnum;
1626 }
1627 }
1628
1629 /*
1630 * There is no need to update lines above the top of the window.
1631 */
1632 if (from < wp->w_topline)
1633 from = wp->w_topline;
1634
1635 /*
1636 * If we know the value of w_botline, use it to restrict the update to
1637 * the lines that are visible in the window.
1638 */
1639 if (wp->w_valid & VALID_BOTLINE)
1640 {
1641 if (from >= wp->w_botline)
1642 from = wp->w_botline - 1;
1643 if (to >= wp->w_botline)
1644 to = wp->w_botline - 1;
1645 }
1646
1647 /*
1648 * Find the minimal part to be updated.
1649 * Watch out for scrolling that made entries in w_lines[] invalid.
1650 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1651 * top_end; need to redraw from top_end to the "to" line.
1652 * A middle mouse click with a Visual selection may change the text
1653 * above the Visual area and reset wl_valid, do count these for
1654 * mid_end (in srow).
1655 */
1656 if (mid_start > 0)
1657 {
1658 lnum = wp->w_topline;
1659 idx = 0;
1660 srow = 0;
1661 if (scrolled_down)
1662 mid_start = top_end;
1663 else
1664 mid_start = 0;
1665 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1666 {
1667 if (wp->w_lines[idx].wl_valid)
1668 mid_start += wp->w_lines[idx].wl_size;
1669 else if (!scrolled_down)
1670 srow += wp->w_lines[idx].wl_size;
1671 ++idx;
1672# ifdef FEAT_FOLDING
1673 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1674 lnum = wp->w_lines[idx].wl_lnum;
1675 else
1676# endif
1677 ++lnum;
1678 }
1679 srow += mid_start;
1680 mid_end = wp->w_height;
1681 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1682 {
1683 if (wp->w_lines[idx].wl_valid
1684 && wp->w_lines[idx].wl_lnum >= to + 1)
1685 {
1686 /* Only update until first row of this line */
1687 mid_end = srow;
1688 break;
1689 }
1690 srow += wp->w_lines[idx].wl_size;
1691 }
1692 }
1693 }
1694
1695 if (VIsual_active && buf == curwin->w_buffer)
1696 {
1697 wp->w_old_visual_mode = VIsual_mode;
1698 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1699 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001700 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 wp->w_old_curswant = curwin->w_curswant;
1702 }
1703 else
1704 {
1705 wp->w_old_visual_mode = 0;
1706 wp->w_old_cursor_lnum = 0;
1707 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001708 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710
1711#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1712 /* reset got_int, otherwise regexp won't work */
1713 save_got_int = got_int;
1714 got_int = 0;
1715#endif
1716#ifdef FEAT_FOLDING
1717 win_foldinfo.fi_level = 0;
1718#endif
1719
1720 /*
1721 * Update all the window rows.
1722 */
1723 idx = 0; /* first entry in w_lines[].wl_size */
1724 row = 0;
1725 srow = 0;
1726 lnum = wp->w_topline; /* first line shown in window */
1727 for (;;)
1728 {
1729 /* stop updating when reached the end of the window (check for _past_
1730 * the end of the window is at the end of the loop) */
1731 if (row == wp->w_height)
1732 {
1733 didline = TRUE;
1734 break;
1735 }
1736
1737 /* stop updating when hit the end of the file */
1738 if (lnum > buf->b_ml.ml_line_count)
1739 {
1740 eof = TRUE;
1741 break;
1742 }
1743
1744 /* Remember the starting row of the line that is going to be dealt
1745 * with. It is used further down when the line doesn't fit. */
1746 srow = row;
1747
1748 /*
1749 * Update a line when it is in an area that needs updating, when it
1750 * has changes or w_lines[idx] is invalid.
1751 * bot_start may be halfway a wrapped line after using
1752 * win_del_lines(), check if the current line includes it.
1753 * When syntax folding is being used, the saved syntax states will
1754 * already have been updated, we can't see where the syntax state is
1755 * the same again, just update until the end of the window.
1756 */
1757 if (row < top_end
1758 || (row >= mid_start && row < mid_end)
1759#ifdef FEAT_SEARCH_EXTRA
1760 || top_to_mod
1761#endif
1762 || idx >= wp->w_lines_valid
1763 || (row + wp->w_lines[idx].wl_size > bot_start)
1764 || (mod_top != 0
1765 && (lnum == mod_top
1766 || (lnum >= mod_top
1767 && (lnum < mod_bot
1768#ifdef FEAT_SYN_HL
1769 || did_update == DID_FOLD
1770 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001771 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772 && (
1773# ifdef FEAT_FOLDING
1774 (foldmethodIsSyntax(wp)
1775 && hasAnyFolding(wp)) ||
1776# endif
1777 syntax_check_changed(lnum)))
1778#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001779#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001780 /* match in fixed position might need redraw
1781 * if lines were inserted or deleted */
1782 || (wp->w_match_head != NULL
1783 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001784#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 )))))
1786 {
1787#ifdef FEAT_SEARCH_EXTRA
1788 if (lnum == mod_top)
1789 top_to_mod = FALSE;
1790#endif
1791
1792 /*
1793 * When at start of changed lines: May scroll following lines
1794 * up or down to minimize redrawing.
1795 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001796 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 */
1798 if (lnum == mod_top
1799 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001800 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 {
1802 int old_rows = 0;
1803 int new_rows = 0;
1804 int xtra_rows;
1805 linenr_T l;
1806
1807 /* Count the old number of window rows, using w_lines[], which
1808 * should still contain the sizes for the lines as they are
1809 * currently displayed. */
1810 for (i = idx; i < wp->w_lines_valid; ++i)
1811 {
1812 /* Only valid lines have a meaningful wl_lnum. Invalid
1813 * lines are part of the changed area. */
1814 if (wp->w_lines[i].wl_valid
1815 && wp->w_lines[i].wl_lnum == mod_bot)
1816 break;
1817 old_rows += wp->w_lines[i].wl_size;
1818#ifdef FEAT_FOLDING
1819 if (wp->w_lines[i].wl_valid
1820 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1821 {
1822 /* Must have found the last valid entry above mod_bot.
1823 * Add following invalid entries. */
1824 ++i;
1825 while (i < wp->w_lines_valid
1826 && !wp->w_lines[i].wl_valid)
1827 old_rows += wp->w_lines[i++].wl_size;
1828 break;
1829 }
1830#endif
1831 }
1832
1833 if (i >= wp->w_lines_valid)
1834 {
1835 /* We can't find a valid line below the changed lines,
1836 * need to redraw until the end of the window.
1837 * Inserting/deleting lines has no use. */
1838 bot_start = 0;
1839 }
1840 else
1841 {
1842 /* Able to count old number of rows: Count new window
1843 * rows, and may insert/delete lines */
1844 j = idx;
1845 for (l = lnum; l < mod_bot; ++l)
1846 {
1847#ifdef FEAT_FOLDING
1848 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1849 ++new_rows;
1850 else
1851#endif
1852#ifdef FEAT_DIFF
1853 if (l == wp->w_topline)
1854 new_rows += plines_win_nofill(wp, l, TRUE)
1855 + wp->w_topfill;
1856 else
1857#endif
1858 new_rows += plines_win(wp, l, TRUE);
1859 ++j;
1860 if (new_rows > wp->w_height - row - 2)
1861 {
1862 /* it's getting too much, must redraw the rest */
1863 new_rows = 9999;
1864 break;
1865 }
1866 }
1867 xtra_rows = new_rows - old_rows;
1868 if (xtra_rows < 0)
1869 {
1870 /* May scroll text up. If there is not enough
1871 * remaining text or scrolling fails, must redraw the
1872 * rest. If scrolling works, must redraw the text
1873 * below the scrolled text. */
1874 if (row - xtra_rows >= wp->w_height - 2)
1875 mod_bot = MAXLNUM;
1876 else
1877 {
1878 check_for_delay(FALSE);
1879 if (win_del_lines(wp, row,
1880 -xtra_rows, FALSE, FALSE) == FAIL)
1881 mod_bot = MAXLNUM;
1882 else
1883 bot_start = wp->w_height + xtra_rows;
1884 }
1885 }
1886 else if (xtra_rows > 0)
1887 {
1888 /* May scroll text down. If there is not enough
1889 * remaining text of scrolling fails, must redraw the
1890 * rest. */
1891 if (row + xtra_rows >= wp->w_height - 2)
1892 mod_bot = MAXLNUM;
1893 else
1894 {
1895 check_for_delay(FALSE);
1896 if (win_ins_lines(wp, row + old_rows,
1897 xtra_rows, FALSE, FALSE) == FAIL)
1898 mod_bot = MAXLNUM;
1899 else if (top_end > row + old_rows)
1900 /* Scrolled the part at the top that requires
1901 * updating down. */
1902 top_end += xtra_rows;
1903 }
1904 }
1905
1906 /* When not updating the rest, may need to move w_lines[]
1907 * entries. */
1908 if (mod_bot != MAXLNUM && i != j)
1909 {
1910 if (j < i)
1911 {
1912 int x = row + new_rows;
1913
1914 /* move entries in w_lines[] upwards */
1915 for (;;)
1916 {
1917 /* stop at last valid entry in w_lines[] */
1918 if (i >= wp->w_lines_valid)
1919 {
1920 wp->w_lines_valid = j;
1921 break;
1922 }
1923 wp->w_lines[j] = wp->w_lines[i];
1924 /* stop at a line that won't fit */
1925 if (x + (int)wp->w_lines[j].wl_size
1926 > wp->w_height)
1927 {
1928 wp->w_lines_valid = j + 1;
1929 break;
1930 }
1931 x += wp->w_lines[j++].wl_size;
1932 ++i;
1933 }
1934 if (bot_start > x)
1935 bot_start = x;
1936 }
1937 else /* j > i */
1938 {
1939 /* move entries in w_lines[] downwards */
1940 j -= i;
1941 wp->w_lines_valid += j;
1942 if (wp->w_lines_valid > wp->w_height)
1943 wp->w_lines_valid = wp->w_height;
1944 for (i = wp->w_lines_valid; i - j >= idx; --i)
1945 wp->w_lines[i] = wp->w_lines[i - j];
1946
1947 /* The w_lines[] entries for inserted lines are
1948 * now invalid, but wl_size may be used above.
1949 * Reset to zero. */
1950 while (i >= idx)
1951 {
1952 wp->w_lines[i].wl_size = 0;
1953 wp->w_lines[i--].wl_valid = FALSE;
1954 }
1955 }
1956 }
1957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 }
1959
1960#ifdef FEAT_FOLDING
1961 /*
1962 * When lines are folded, display one line for all of them.
1963 * Otherwise, display normally (can be several display lines when
1964 * 'wrap' is on).
1965 */
1966 fold_count = foldedCount(wp, lnum, &win_foldinfo);
1967 if (fold_count != 0)
1968 {
1969 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
1970 ++row;
1971 --fold_count;
1972 wp->w_lines[idx].wl_folded = TRUE;
1973 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
1974# ifdef FEAT_SYN_HL
1975 did_update = DID_FOLD;
1976# endif
1977 }
1978 else
1979#endif
1980 if (idx < wp->w_lines_valid
1981 && wp->w_lines[idx].wl_valid
1982 && wp->w_lines[idx].wl_lnum == lnum
1983 && lnum > wp->w_topline
1984 && !(dy_flags & DY_LASTLINE)
1985 && srow + wp->w_lines[idx].wl_size > wp->w_height
1986#ifdef FEAT_DIFF
1987 && diff_check_fill(wp, lnum) == 0
1988#endif
1989 )
1990 {
1991 /* This line is not going to fit. Don't draw anything here,
1992 * will draw "@ " lines below. */
1993 row = wp->w_height + 1;
1994 }
1995 else
1996 {
1997#ifdef FEAT_SEARCH_EXTRA
1998 prepare_search_hl(wp, lnum);
1999#endif
2000#ifdef FEAT_SYN_HL
2001 /* Let the syntax stuff know we skipped a few lines. */
2002 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002003 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 syntax_end_parsing(syntax_last_parsed + 1);
2005#endif
2006
2007 /*
2008 * Display one line.
2009 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002010 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011
2012#ifdef FEAT_FOLDING
2013 wp->w_lines[idx].wl_folded = FALSE;
2014 wp->w_lines[idx].wl_lastlnum = lnum;
2015#endif
2016#ifdef FEAT_SYN_HL
2017 did_update = DID_LINE;
2018 syntax_last_parsed = lnum;
2019#endif
2020 }
2021
2022 wp->w_lines[idx].wl_lnum = lnum;
2023 wp->w_lines[idx].wl_valid = TRUE;
2024 if (row > wp->w_height) /* past end of screen */
2025 {
2026 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002027 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2029 ++idx;
2030 break;
2031 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002032 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 wp->w_lines[idx].wl_size = row - srow;
2034 ++idx;
2035#ifdef FEAT_FOLDING
2036 lnum += fold_count + 1;
2037#else
2038 ++lnum;
2039#endif
2040 }
2041 else
2042 {
2043 /* This line does not need updating, advance to the next one */
2044 row += wp->w_lines[idx++].wl_size;
2045 if (row > wp->w_height) /* past end of screen */
2046 break;
2047#ifdef FEAT_FOLDING
2048 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2049#else
2050 ++lnum;
2051#endif
2052#ifdef FEAT_SYN_HL
2053 did_update = DID_NONE;
2054#endif
2055 }
2056
2057 if (lnum > buf->b_ml.ml_line_count)
2058 {
2059 eof = TRUE;
2060 break;
2061 }
2062 }
2063 /*
2064 * End of loop over all window lines.
2065 */
2066
2067
2068 if (idx > wp->w_lines_valid)
2069 wp->w_lines_valid = idx;
2070
2071#ifdef FEAT_SYN_HL
2072 /*
2073 * Let the syntax stuff know we stop parsing here.
2074 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002075 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 syntax_end_parsing(syntax_last_parsed + 1);
2077#endif
2078
2079 /*
2080 * If we didn't hit the end of the file, and we didn't finish the last
2081 * line we were working on, then the line didn't fit.
2082 */
2083 wp->w_empty_rows = 0;
2084#ifdef FEAT_DIFF
2085 wp->w_filler_rows = 0;
2086#endif
2087 if (!eof && !didline)
2088 {
2089 if (lnum == wp->w_topline)
2090 {
2091 /*
2092 * Single line that does not fit!
2093 * Don't overwrite it, it can be edited.
2094 */
2095 wp->w_botline = lnum + 1;
2096 }
2097#ifdef FEAT_DIFF
2098 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2099 {
2100 /* Window ends in filler lines. */
2101 wp->w_botline = lnum;
2102 wp->w_filler_rows = wp->w_height - srow;
2103 }
2104#endif
2105 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2106 {
2107 /*
2108 * Last line isn't finished: Display "@@@" at the end.
2109 */
2110 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2111 W_WINROW(wp) + wp->w_height,
2112 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
2113 '@', '@', hl_attr(HLF_AT));
2114 set_empty_rows(wp, srow);
2115 wp->w_botline = lnum;
2116 }
2117 else
2118 {
2119 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
2120 wp->w_botline = lnum;
2121 }
2122 }
2123 else
2124 {
2125#ifdef FEAT_VERTSPLIT
2126 draw_vsep_win(wp, row);
2127#endif
2128 if (eof) /* we hit the end of the file */
2129 {
2130 wp->w_botline = buf->b_ml.ml_line_count + 1;
2131#ifdef FEAT_DIFF
2132 j = diff_check_fill(wp, wp->w_botline);
2133 if (j > 0 && !wp->w_botfill)
2134 {
2135 /*
2136 * Display filler lines at the end of the file
2137 */
2138 if (char2cells(fill_diff) > 1)
2139 i = '-';
2140 else
2141 i = fill_diff;
2142 if (row + j > wp->w_height)
2143 j = wp->w_height - row;
2144 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2145 row += j;
2146 }
2147#endif
2148 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002149 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 wp->w_botline = lnum;
2151
2152 /* make sure the rest of the screen is blank */
2153 /* put '~'s on rows that aren't part of the file. */
2154 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_AT);
2155 }
2156
2157 /* Reset the type of redrawing required, the window has been updated. */
2158 wp->w_redr_type = 0;
2159#ifdef FEAT_DIFF
2160 wp->w_old_topfill = wp->w_topfill;
2161 wp->w_old_botfill = wp->w_botfill;
2162#endif
2163
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002164 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 {
2166 /*
2167 * There is a trick with w_botline. If we invalidate it on each
2168 * change that might modify it, this will cause a lot of expensive
2169 * calls to plines() in update_topline() each time. Therefore the
2170 * value of w_botline is often approximated, and this value is used to
2171 * compute the value of w_topline. If the value of w_botline was
2172 * wrong, check that the value of w_topline is correct (cursor is on
2173 * the visible part of the text). If it's not, we need to redraw
2174 * again. Mostly this just means scrolling up a few lines, so it
2175 * doesn't look too bad. Only do this for the current window (where
2176 * changes are relevant).
2177 */
2178 wp->w_valid |= VALID_BOTLINE;
2179 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2180 {
2181 recursive = TRUE;
2182 curwin->w_valid &= ~VALID_TOPLINE;
2183 update_topline(); /* may invalidate w_botline again */
2184 if (must_redraw != 0)
2185 {
2186 /* Don't update for changes in buffer again. */
2187 i = curbuf->b_mod_set;
2188 curbuf->b_mod_set = FALSE;
2189 win_update(curwin);
2190 must_redraw = 0;
2191 curbuf->b_mod_set = i;
2192 }
2193 recursive = FALSE;
2194 }
2195 }
2196
2197#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2198 /* restore got_int, unless CTRL-C was hit while redrawing */
2199 if (!got_int)
2200 got_int = save_got_int;
2201#endif
2202}
2203
2204#ifdef FEAT_SIGNS
2205static int draw_signcolumn __ARGS((win_T *wp));
2206
2207/*
2208 * Return TRUE when window "wp" has a column to draw signs in.
2209 */
2210 static int
2211draw_signcolumn(wp)
2212 win_T *wp;
2213{
2214 return (wp->w_buffer->b_signlist != NULL
2215# ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002216 || netbeans_active()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217# endif
2218 );
2219}
2220#endif
2221
2222/*
2223 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2224 * as the filler character.
2225 */
2226 static void
2227win_draw_end(wp, c1, c2, row, endrow, hl)
2228 win_T *wp;
2229 int c1;
2230 int c2;
2231 int row;
2232 int endrow;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002233 hlf_T hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234{
2235#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2236 int n = 0;
2237# define FDC_OFF n
2238#else
2239# define FDC_OFF 0
2240#endif
2241
2242#ifdef FEAT_RIGHTLEFT
2243 if (wp->w_p_rl)
2244 {
2245 /* No check for cmdline window: should never be right-left. */
2246# ifdef FEAT_FOLDING
2247 n = wp->w_p_fdc;
2248
2249 if (n > 0)
2250 {
2251 /* draw the fold column at the right */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002252 if (n > W_WIDTH(wp))
2253 n = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2255 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
2256 ' ', ' ', hl_attr(HLF_FC));
2257 }
2258# endif
2259# ifdef FEAT_SIGNS
2260 if (draw_signcolumn(wp))
2261 {
2262 int nn = n + 2;
2263
2264 /* draw the sign column left of the fold column */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002265 if (nn > W_WIDTH(wp))
2266 nn = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2268 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
2269 ' ', ' ', hl_attr(HLF_SC));
2270 n = nn;
2271 }
2272# endif
2273 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2274 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
2275 c2, c2, hl_attr(hl));
2276 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2277 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
2278 c1, c2, hl_attr(hl));
2279 }
2280 else
2281#endif
2282 {
2283#ifdef FEAT_CMDWIN
2284 if (cmdwin_type != 0 && wp == curwin)
2285 {
2286 /* draw the cmdline character in the leftmost column */
2287 n = 1;
2288 if (n > wp->w_width)
2289 n = wp->w_width;
2290 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2291 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
2292 cmdwin_type, ' ', hl_attr(HLF_AT));
2293 }
2294#endif
2295#ifdef FEAT_FOLDING
2296 if (wp->w_p_fdc > 0)
2297 {
2298 int nn = n + wp->w_p_fdc;
2299
2300 /* draw the fold column at the left */
2301 if (nn > W_WIDTH(wp))
2302 nn = W_WIDTH(wp);
2303 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2304 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2305 ' ', ' ', hl_attr(HLF_FC));
2306 n = nn;
2307 }
2308#endif
2309#ifdef FEAT_SIGNS
2310 if (draw_signcolumn(wp))
2311 {
2312 int nn = n + 2;
2313
2314 /* draw the sign column after the fold column */
2315 if (nn > W_WIDTH(wp))
2316 nn = W_WIDTH(wp);
2317 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2318 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2319 ' ', ' ', hl_attr(HLF_SC));
2320 n = nn;
2321 }
2322#endif
2323 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2324 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
2325 c1, c2, hl_attr(hl));
2326 }
2327 set_empty_rows(wp, row);
2328}
2329
Bram Moolenaar1a384422010-07-14 19:53:30 +02002330#ifdef FEAT_SYN_HL
2331static int advance_color_col __ARGS((int vcol, int **color_cols));
2332
2333/*
2334 * Advance **color_cols and return TRUE when there are columns to draw.
2335 */
2336 static int
2337advance_color_col(vcol, color_cols)
2338 int vcol;
2339 int **color_cols;
2340{
2341 while (**color_cols >= 0 && vcol > **color_cols)
2342 ++*color_cols;
2343 return (**color_cols >= 0);
2344}
2345#endif
2346
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347#ifdef FEAT_FOLDING
2348/*
2349 * Display one folded line.
2350 */
2351 static void
2352fold_line(wp, fold_count, foldinfo, lnum, row)
2353 win_T *wp;
2354 long fold_count;
2355 foldinfo_T *foldinfo;
2356 linenr_T lnum;
2357 int row;
2358{
2359 char_u buf[51];
2360 pos_T *top, *bot;
2361 linenr_T lnume = lnum + fold_count - 1;
2362 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002363 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 int col;
2366 int txtcol;
2367 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002368 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369
2370 /* Build the fold line:
2371 * 1. Add the cmdwin_type for the command-line window
2372 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002373 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 * 4. Compose the text
2375 * 5. Add the text
2376 * 6. set highlighting for the Visual area an other text
2377 */
2378 col = 0;
2379
2380 /*
2381 * 1. Add the cmdwin_type for the command-line window
2382 * Ignores 'rightleft', this window is never right-left.
2383 */
2384#ifdef FEAT_CMDWIN
2385 if (cmdwin_type != 0 && wp == curwin)
2386 {
2387 ScreenLines[off] = cmdwin_type;
2388 ScreenAttrs[off] = hl_attr(HLF_AT);
2389#ifdef FEAT_MBYTE
2390 if (enc_utf8)
2391 ScreenLinesUC[off] = 0;
2392#endif
2393 ++col;
2394 }
2395#endif
2396
2397 /*
2398 * 2. Add the 'foldcolumn'
2399 */
2400 fdc = wp->w_p_fdc;
2401 if (fdc > W_WIDTH(wp) - col)
2402 fdc = W_WIDTH(wp) - col;
2403 if (fdc > 0)
2404 {
2405 fill_foldcolumn(buf, wp, TRUE, lnum);
2406#ifdef FEAT_RIGHTLEFT
2407 if (wp->w_p_rl)
2408 {
2409 int i;
2410
2411 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
2412 hl_attr(HLF_FC));
2413 /* reverse the fold column */
2414 for (i = 0; i < fdc; ++i)
2415 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2416 }
2417 else
2418#endif
2419 copy_text_attr(off + col, buf, fdc, hl_attr(HLF_FC));
2420 col += fdc;
2421 }
2422
2423#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002424# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2425 for (ri = 0; ri < l; ++ri) \
2426 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2427 else \
2428 for (ri = 0; ri < l; ++ri) \
2429 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002431# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2432 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433#endif
2434
Bram Moolenaar64486672010-05-16 15:46:46 +02002435 /* Set all attributes of the 'number' or 'relativenumber' column and the
2436 * text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002437 RL_MEMSET(col, hl_attr(HLF_FL), W_WIDTH(wp) - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438
2439#ifdef FEAT_SIGNS
2440 /* If signs are being displayed, add two spaces. */
2441 if (draw_signcolumn(wp))
2442 {
2443 len = W_WIDTH(wp) - col;
2444 if (len > 0)
2445 {
2446 if (len > 2)
2447 len = 2;
2448# ifdef FEAT_RIGHTLEFT
2449 if (wp->w_p_rl)
2450 /* the line number isn't reversed */
2451 copy_text_attr(off + W_WIDTH(wp) - len - col,
2452 (char_u *)" ", len, hl_attr(HLF_FL));
2453 else
2454# endif
2455 copy_text_attr(off + col, (char_u *)" ", len, hl_attr(HLF_FL));
2456 col += len;
2457 }
2458 }
2459#endif
2460
2461 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002462 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002464 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 {
2466 len = W_WIDTH(wp) - col;
2467 if (len > 0)
2468 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002469 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002470 long num;
2471 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002472
2473 if (len > w + 1)
2474 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002475
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002476 if (wp->w_p_nu && !wp->w_p_rnu)
2477 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002478 num = (long)lnum;
2479 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002480 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002481 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002482 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002483 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002484 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002485 /* 'number' + 'relativenumber': cursor line shows absolute
2486 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002487 num = lnum;
2488 fmt = "%-*ld ";
2489 }
2490 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002491
Bram Moolenaar700e7342013-01-30 12:31:36 +01002492 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493#ifdef FEAT_RIGHTLEFT
2494 if (wp->w_p_rl)
2495 /* the line number isn't reversed */
2496 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
2497 hl_attr(HLF_FL));
2498 else
2499#endif
2500 copy_text_attr(off + col, buf, len, hl_attr(HLF_FL));
2501 col += len;
2502 }
2503 }
2504
2505 /*
2506 * 4. Compose the folded-line string with 'foldtext', if set.
2507 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002508 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509
2510 txtcol = col; /* remember where text starts */
2511
2512 /*
2513 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2514 * Right-left text is put in columns 0 - number-col, normal text is put
2515 * in columns number-col - window-width.
2516 */
2517#ifdef FEAT_MBYTE
2518 if (has_mbyte)
2519 {
2520 int cells;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002521 int u8c, u8cc[MAX_MCO];
2522 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 int idx;
2524 int c_len;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002525 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526# ifdef FEAT_ARABIC
2527 int prev_c = 0; /* previous Arabic character */
2528 int prev_c1 = 0; /* first composing char for prev_c */
2529# endif
2530
2531# ifdef FEAT_RIGHTLEFT
2532 if (wp->w_p_rl)
2533 idx = off;
2534 else
2535# endif
2536 idx = off + col;
2537
2538 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2539 for (p = text; *p != NUL; )
2540 {
2541 cells = (*mb_ptr2cells)(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002542 c_len = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 if (col + cells > W_WIDTH(wp)
2544# ifdef FEAT_RIGHTLEFT
2545 - (wp->w_p_rl ? col : 0)
2546# endif
2547 )
2548 break;
2549 ScreenLines[idx] = *p;
2550 if (enc_utf8)
2551 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002552 u8c = utfc_ptr2char(p, u8cc);
2553 if (*p < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 {
2555 ScreenLinesUC[idx] = 0;
2556#ifdef FEAT_ARABIC
2557 prev_c = u8c;
2558#endif
2559 }
2560 else
2561 {
2562#ifdef FEAT_ARABIC
2563 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2564 {
2565 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002566 int pc, pc1, nc;
2567 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 int firstbyte = *p;
2569
2570 /* The idea of what is the previous and next
2571 * character depends on 'rightleft'. */
2572 if (wp->w_p_rl)
2573 {
2574 pc = prev_c;
2575 pc1 = prev_c1;
2576 nc = utf_ptr2char(p + c_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002577 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 }
2579 else
2580 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002581 pc = utfc_ptr2char(p + c_len, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002583 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 }
2585 prev_c = u8c;
2586
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002587 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 pc, pc1, nc);
2589 ScreenLines[idx] = firstbyte;
2590 }
2591 else
2592 prev_c = u8c;
2593#endif
2594 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar11936362007-09-17 20:39:42 +00002595#ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 if (u8c >= 0x10000)
2597 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2598 else
Bram Moolenaar11936362007-09-17 20:39:42 +00002599#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 ScreenLinesUC[idx] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002601 for (i = 0; i < Screen_mco; ++i)
2602 {
2603 ScreenLinesC[i][idx] = u8cc[i];
2604 if (u8cc[i] == 0)
2605 break;
2606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 }
2608 if (cells > 1)
2609 ScreenLines[idx + 1] = 0;
2610 }
Bram Moolenaar990bb662010-02-03 15:48:04 +01002611 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2612 /* double-byte single width character */
2613 ScreenLines2[idx] = p[1];
2614 else if (cells > 1)
2615 /* double-width character */
2616 ScreenLines[idx + 1] = p[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 col += cells;
2618 idx += cells;
2619 p += c_len;
2620 }
2621 }
2622 else
2623#endif
2624 {
2625 len = (int)STRLEN(text);
2626 if (len > W_WIDTH(wp) - col)
2627 len = W_WIDTH(wp) - col;
2628 if (len > 0)
2629 {
2630#ifdef FEAT_RIGHTLEFT
2631 if (wp->w_p_rl)
2632 STRNCPY(current_ScreenLine, text, len);
2633 else
2634#endif
2635 STRNCPY(current_ScreenLine + col, text, len);
2636 col += len;
2637 }
2638 }
2639
2640 /* Fill the rest of the line with the fold filler */
2641#ifdef FEAT_RIGHTLEFT
2642 if (wp->w_p_rl)
2643 col -= txtcol;
2644#endif
2645 while (col < W_WIDTH(wp)
2646#ifdef FEAT_RIGHTLEFT
2647 - (wp->w_p_rl ? txtcol : 0)
2648#endif
2649 )
2650 {
2651#ifdef FEAT_MBYTE
2652 if (enc_utf8)
2653 {
2654 if (fill_fold >= 0x80)
2655 {
2656 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002657 ScreenLinesC[0][off + col] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 }
2659 else
2660 ScreenLinesUC[off + col] = 0;
2661 }
2662#endif
2663 ScreenLines[off + col++] = fill_fold;
2664 }
2665
2666 if (text != buf)
2667 vim_free(text);
2668
2669 /*
2670 * 6. set highlighting for the Visual area an other text.
2671 * If all folded lines are in the Visual area, highlight the line.
2672 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2674 {
2675 if (ltoreq(curwin->w_cursor, VIsual))
2676 {
2677 /* Visual is after curwin->w_cursor */
2678 top = &curwin->w_cursor;
2679 bot = &VIsual;
2680 }
2681 else
2682 {
2683 /* Visual is before curwin->w_cursor */
2684 top = &VIsual;
2685 bot = &curwin->w_cursor;
2686 }
2687 if (lnum >= top->lnum
2688 && lnume <= bot->lnum
2689 && (VIsual_mode != 'v'
2690 || ((lnum > top->lnum
2691 || (lnum == top->lnum
2692 && top->col == 0))
2693 && (lnume < bot->lnum
2694 || (lnume == bot->lnum
2695 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002696 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 {
2698 if (VIsual_mode == Ctrl_V)
2699 {
2700 /* Visual block mode: highlight the chars part of the block */
2701 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2702 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002703 if (wp->w_old_cursor_lcol != MAXCOL
2704 && wp->w_old_cursor_lcol + txtcol
2705 < (colnr_T)W_WIDTH(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 len = wp->w_old_cursor_lcol;
2707 else
2708 len = W_WIDTH(wp) - txtcol;
2709 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, hl_attr(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002710 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 }
2712 }
2713 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002714 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 /* Set all attributes of the text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002716 RL_MEMSET(txtcol, hl_attr(HLF_V), W_WIDTH(wp) - txtcol);
2717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 }
2719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002721#ifdef FEAT_SYN_HL
2722 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002723 if (wp->w_p_cuc)
2724 {
2725 txtcol += wp->w_virtcol;
2726 if (wp->w_p_wrap)
2727 txtcol -= wp->w_skipcol;
2728 else
2729 txtcol -= wp->w_leftcol;
2730 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2731 ScreenAttrs[off + txtcol] = hl_combine_attr(
2732 ScreenAttrs[off + txtcol], hl_attr(HLF_CUC));
2733 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002734#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735
2736 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2737 (int)W_WIDTH(wp), FALSE);
2738
2739 /*
2740 * Update w_cline_height and w_cline_folded if the cursor line was
2741 * updated (saves a call to plines() later).
2742 */
2743 if (wp == curwin
2744 && lnum <= curwin->w_cursor.lnum
2745 && lnume >= curwin->w_cursor.lnum)
2746 {
2747 curwin->w_cline_row = row;
2748 curwin->w_cline_height = 1;
2749 curwin->w_cline_folded = TRUE;
2750 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2751 }
2752}
2753
2754/*
2755 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2756 */
2757 static void
2758copy_text_attr(off, buf, len, attr)
2759 int off;
2760 char_u *buf;
2761 int len;
2762 int attr;
2763{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002764 int i;
2765
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 mch_memmove(ScreenLines + off, buf, (size_t)len);
2767# ifdef FEAT_MBYTE
2768 if (enc_utf8)
2769 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2770# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002771 for (i = 0; i < len; ++i)
2772 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773}
2774
2775/*
2776 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002777 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 */
2779 static void
2780fill_foldcolumn(p, wp, closed, lnum)
2781 char_u *p;
2782 win_T *wp;
2783 int closed; /* TRUE of FALSE */
2784 linenr_T lnum; /* current line number */
2785{
2786 int i = 0;
2787 int level;
2788 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002789 int empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790
2791 /* Init to all spaces. */
2792 copy_spaces(p, (size_t)wp->w_p_fdc);
2793
2794 level = win_foldinfo.fi_level;
2795 if (level > 0)
2796 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002797 /* If there is only one column put more info in it. */
2798 empty = (wp->w_p_fdc == 1) ? 0 : 1;
2799
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 /* If the column is too narrow, we start at the lowest level that
2801 * fits and use numbers to indicated the depth. */
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002802 first_level = level - wp->w_p_fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 if (first_level < 1)
2804 first_level = 1;
2805
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002806 for (i = 0; i + empty < wp->w_p_fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 {
2808 if (win_foldinfo.fi_lnum == lnum
2809 && first_level + i >= win_foldinfo.fi_low_level)
2810 p[i] = '-';
2811 else if (first_level == 1)
2812 p[i] = '|';
2813 else if (first_level + i <= 9)
2814 p[i] = '0' + first_level + i;
2815 else
2816 p[i] = '>';
2817 if (first_level + i == level)
2818 break;
2819 }
2820 }
2821 if (closed)
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002822 p[i >= wp->w_p_fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823}
2824#endif /* FEAT_FOLDING */
2825
2826/*
2827 * Display line "lnum" of window 'wp' on the screen.
2828 * Start at row "startrow", stop when "endrow" is reached.
2829 * wp->w_virtcol needs to be valid.
2830 *
2831 * Return the number of last row the line occupies.
2832 */
2833 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +00002834win_line(wp, lnum, startrow, endrow, nochange)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 win_T *wp;
2836 linenr_T lnum;
2837 int startrow;
2838 int endrow;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002839 int nochange UNUSED; /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840{
2841 int col; /* visual column on screen */
2842 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2843 int c = 0; /* init for GCC */
2844 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01002845#ifdef FEAT_LINEBREAK
2846 long vcol_sbr = -1; /* virtual column after showbreak */
2847#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 long vcol_prev = -1; /* "vcol" of previous character */
2849 char_u *line; /* current line */
2850 char_u *ptr; /* current position in "line" */
2851 int row; /* row in the window, excl w_winrow */
2852 int screen_row; /* row on the screen, incl w_winrow */
2853
2854 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2855 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00002856 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02002857 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 int c_extra = NUL; /* extra chars, all the same */
2859 int extra_attr = 0; /* attributes when n_extra != 0 */
2860 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2861 displaying lcs_eol at end-of-line */
2862 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2863 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2864
2865 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2866 int saved_n_extra = 0;
2867 char_u *saved_p_extra = NULL;
2868 int saved_c_extra = 0;
2869 int saved_char_attr = 0;
2870
2871 int n_attr = 0; /* chars with special attr */
2872 int saved_attr2 = 0; /* char_attr saved for n_attr */
2873 int n_attr3 = 0; /* chars with overruling special attr */
2874 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2875
2876 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2877
2878 int fromcol, tocol; /* start/end of inverting */
2879 int fromcol_prev = -2; /* start of inverting after cursor */
2880 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002882 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 pos_T pos;
2884 long v;
2885
2886 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002887 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2889 in this line */
2890 int attr = 0; /* attributes for area highlighting */
2891 int area_attr = 0; /* attributes desired by highlighting */
2892 int search_attr = 0; /* attributes desired by 'hlsearch' */
2893#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002894 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 int syntax_attr = 0; /* attributes desired by syntax */
2896 int has_syntax = FALSE; /* this buffer has syntax highl. */
2897 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00002898 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02002899 int draw_color_col = FALSE; /* highlight colorcolumn */
2900 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002901#endif
2902#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002903 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002904# define SPWORDLEN 150
2905 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00002906 int nextlinecol = 0; /* column where nextline[] starts */
2907 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00002908 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00002909 int spell_attr = 0; /* attributes desired by spelling */
2910 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00002911 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2912 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00002913 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002914 static int cap_col = -1; /* column to check for Cap word */
2915 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002916 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917#endif
2918 int extra_check; /* has syntax or linebreak */
2919#ifdef FEAT_MBYTE
2920 int multi_attr = 0; /* attributes desired by multibyte */
2921 int mb_l = 1; /* multi-byte byte length */
2922 int mb_c = 0; /* decoded multi-byte character */
2923 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002924 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925#endif
2926#ifdef FEAT_DIFF
2927 int filler_lines; /* nr of filler lines to be drawn */
2928 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002929 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 int change_start = MAXCOL; /* first col of changed area */
2931 int change_end = -1; /* last col of changed area */
2932#endif
2933 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
2934#ifdef FEAT_LINEBREAK
2935 int need_showbreak = FALSE;
2936#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002937#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
2938 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00002940 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941#endif
2942#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00002943 matchitem_T *cur; /* points to the match list */
2944 match_T *shl; /* points to search_hl or a match */
2945 int shl_flag; /* flag to indicate whether search_hl
2946 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02002947 int pos_inprogress; /* marks that position match search is
2948 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00002949 int prevcol_hl_flag; /* flag to indicate whether prevcol
2950 equals startcol of search_hl or one
2951 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952#endif
2953#ifdef FEAT_ARABIC
2954 int prev_c = 0; /* previous Arabic character */
2955 int prev_c1 = 0; /* first composing char for prev_c */
2956#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002957#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00002958 int did_line_attr = 0;
2959#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960
2961 /* draw_state: items that are drawn in sequence: */
2962#define WL_START 0 /* nothing done yet */
2963#ifdef FEAT_CMDWIN
2964# define WL_CMDLINE WL_START + 1 /* cmdline window column */
2965#else
2966# define WL_CMDLINE WL_START
2967#endif
2968#ifdef FEAT_FOLDING
2969# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
2970#else
2971# define WL_FOLD WL_CMDLINE
2972#endif
2973#ifdef FEAT_SIGNS
2974# define WL_SIGN WL_FOLD + 1 /* column for signs */
2975#else
2976# define WL_SIGN WL_FOLD /* column for signs */
2977#endif
2978#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02002979#ifdef FEAT_LINEBREAK
2980# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02002982# define WL_BRI WL_NR
2983#endif
2984#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
2985# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
2986#else
2987# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988#endif
2989#define WL_LINE WL_SBR + 1 /* text in the line */
2990 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00002991#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 int feedback_col = 0;
2993 int feedback_old_attr = -1;
2994#endif
2995
Bram Moolenaar860cae12010-06-05 23:22:07 +02002996#ifdef FEAT_CONCEAL
2997 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02002998 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02002999 int prev_syntax_id = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003000 int conceal_attr = hl_attr(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003001 int is_concealing = FALSE;
3002 int boguscols = 0; /* nonexistent columns added to force
3003 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003004 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003005 int did_wcol = FALSE;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003006 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003007# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003008# define FIX_FOR_BOGUSCOLS \
3009 { \
3010 n_extra += vcol_off; \
3011 vcol -= vcol_off; \
3012 vcol_off = 0; \
3013 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003014 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003015 boguscols = 0; \
3016 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003017#else
3018# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003019#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020
3021 if (startrow > endrow) /* past the end already! */
3022 return startrow;
3023
3024 row = startrow;
3025 screen_row = row + W_WINROW(wp);
3026
3027 /*
3028 * To speed up the loop below, set extra_check when there is linebreak,
3029 * trailing white space and/or syntax processing to be done.
3030 */
3031#ifdef FEAT_LINEBREAK
3032 extra_check = wp->w_p_lbr;
3033#else
3034 extra_check = 0;
3035#endif
3036#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02003037 if (syntax_present(wp) && !wp->w_s->b_syn_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 {
3039 /* Prepare for syntax highlighting in this line. When there is an
3040 * error, stop syntax highlighting. */
3041 save_did_emsg = did_emsg;
3042 did_emsg = FALSE;
3043 syntax_start(wp, lnum);
3044 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003045 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 else
3047 {
3048 did_emsg = save_did_emsg;
3049 has_syntax = TRUE;
3050 extra_check = TRUE;
3051 }
3052 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003053
3054 /* Check for columns to display for 'colorcolumn'. */
3055 color_cols = wp->w_p_cc_cols;
3056 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003057 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003058#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003059
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003060#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003061 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02003062 && *wp->w_s->b_p_spl != NUL
3063 && wp->w_s->b_langp.ga_len > 0
3064 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003065 {
3066 /* Prepare for spell checking. */
3067 has_spell = TRUE;
3068 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003069
3070 /* Get the start of the next line, so that words that wrap to the next
3071 * line are found too: "et<line-break>al.".
3072 * Trick: skip a few chars for C/shell/Vim comments */
3073 nextline[SPWORDLEN] = NUL;
3074 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3075 {
3076 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3077 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3078 }
3079
3080 /* When a word wrapped from the previous line the start of the current
3081 * line is valid. */
3082 if (lnum == checked_lnum)
3083 cur_checked_col = checked_col;
3084 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003085
3086 /* When there was a sentence end in the previous line may require a
3087 * word starting with capital in this line. In line 1 always check
3088 * the first word. */
3089 if (lnum != capcol_lnum)
3090 cap_col = -1;
3091 if (lnum == 1)
3092 cap_col = 0;
3093 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003094 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095#endif
3096
3097 /*
3098 * handle visual active in this window
3099 */
3100 fromcol = -10;
3101 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3103 {
3104 /* Visual is after curwin->w_cursor */
3105 if (ltoreq(curwin->w_cursor, VIsual))
3106 {
3107 top = &curwin->w_cursor;
3108 bot = &VIsual;
3109 }
3110 else /* Visual is before curwin->w_cursor */
3111 {
3112 top = &VIsual;
3113 bot = &curwin->w_cursor;
3114 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003115 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 if (VIsual_mode == Ctrl_V) /* block mode */
3117 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003118 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 {
3120 fromcol = wp->w_old_cursor_fcol;
3121 tocol = wp->w_old_cursor_lcol;
3122 }
3123 }
3124 else /* non-block mode */
3125 {
3126 if (lnum > top->lnum && lnum <= bot->lnum)
3127 fromcol = 0;
3128 else if (lnum == top->lnum)
3129 {
3130 if (VIsual_mode == 'V') /* linewise */
3131 fromcol = 0;
3132 else
3133 {
3134 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3135 if (gchar_pos(top) == NUL)
3136 tocol = fromcol + 1;
3137 }
3138 }
3139 if (VIsual_mode != 'V' && lnum == bot->lnum)
3140 {
3141 if (*p_sel == 'e' && bot->col == 0
3142#ifdef FEAT_VIRTUALEDIT
3143 && bot->coladd == 0
3144#endif
3145 )
3146 {
3147 fromcol = -10;
3148 tocol = MAXCOL;
3149 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003150 else if (bot->col == MAXCOL)
3151 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 else
3153 {
3154 pos = *bot;
3155 if (*p_sel == 'e')
3156 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3157 else
3158 {
3159 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3160 ++tocol;
3161 }
3162 }
3163 }
3164 }
3165
3166#ifndef MSDOS
3167 /* Check if the character under the cursor should not be inverted */
3168 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
3169# ifdef FEAT_GUI
3170 && !gui.in_use
3171# endif
3172 )
3173 noinvcur = TRUE;
3174#endif
3175
3176 /* if inverting in this line set area_highlighting */
3177 if (fromcol >= 0)
3178 {
3179 area_highlighting = TRUE;
3180 attr = hl_attr(HLF_V);
3181#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003182 if ((clip_star.available && !clip_star.owned
3183 && clip_isautosel_star())
3184 || (clip_plus.available && !clip_plus.owned
3185 && clip_isautosel_plus()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 attr = hl_attr(HLF_VNC);
3187#endif
3188 }
3189 }
3190
3191 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003192 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003194 else if (highlight_match
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 && wp == curwin
3196 && lnum >= curwin->w_cursor.lnum
3197 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3198 {
3199 if (lnum == curwin->w_cursor.lnum)
3200 getvcol(curwin, &(curwin->w_cursor),
3201 (colnr_T *)&fromcol, NULL, NULL);
3202 else
3203 fromcol = 0;
3204 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3205 {
3206 pos.lnum = lnum;
3207 pos.col = search_match_endcol;
3208 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3209 }
3210 else
3211 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003212 /* do at least one character; happens when past end of line */
3213 if (fromcol == tocol)
3214 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 area_highlighting = TRUE;
3216 attr = hl_attr(HLF_I);
3217 }
3218
3219#ifdef FEAT_DIFF
3220 filler_lines = diff_check(wp, lnum);
3221 if (filler_lines < 0)
3222 {
3223 if (filler_lines == -1)
3224 {
3225 if (diff_find_change(wp, lnum, &change_start, &change_end))
3226 diff_hlf = HLF_ADD; /* added line */
3227 else if (change_start == 0)
3228 diff_hlf = HLF_TXD; /* changed text */
3229 else
3230 diff_hlf = HLF_CHD; /* changed line */
3231 }
3232 else
3233 diff_hlf = HLF_ADD; /* added line */
3234 filler_lines = 0;
3235 area_highlighting = TRUE;
3236 }
3237 if (lnum == wp->w_topline)
3238 filler_lines = wp->w_topfill;
3239 filler_todo = filler_lines;
3240#endif
3241
3242#ifdef LINE_ATTR
3243# ifdef FEAT_SIGNS
3244 /* If this line has a sign with line highlighting set line_attr. */
3245 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3246 if (v != 0)
3247 line_attr = sign_get_attr((int)v, TRUE);
3248# endif
3249# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
3250 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003251 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 line_attr = hl_attr(HLF_L);
3253# endif
3254 if (line_attr != 0)
3255 area_highlighting = TRUE;
3256#endif
3257
3258 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3259 ptr = line;
3260
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003261#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003262 if (has_spell)
3263 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003264 /* For checking first word with a capital skip white space. */
3265 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003266 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003267
Bram Moolenaar30abd282005-06-22 22:35:10 +00003268 /* To be able to spell-check over line boundaries copy the end of the
3269 * current line into nextline[]. Above the start of the next line was
3270 * copied to nextline[SPWORDLEN]. */
3271 if (nextline[SPWORDLEN] == NUL)
3272 {
3273 /* No next line or it is empty. */
3274 nextlinecol = MAXCOL;
3275 nextline_idx = 0;
3276 }
3277 else
3278 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003279 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003280 if (v < SPWORDLEN)
3281 {
3282 /* Short line, use it completely and append the start of the
3283 * next line. */
3284 nextlinecol = 0;
3285 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003286 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003287 nextline_idx = v + 1;
3288 }
3289 else
3290 {
3291 /* Long line, use only the last SPWORDLEN bytes. */
3292 nextlinecol = v - SPWORDLEN;
3293 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3294 nextline_idx = SPWORDLEN + 1;
3295 }
3296 }
3297 }
3298#endif
3299
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 /* find start of trailing whitespace */
3301 if (wp->w_p_list && lcs_trail)
3302 {
3303 trailcol = (colnr_T)STRLEN(ptr);
3304 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
3305 --trailcol;
3306 trailcol += (colnr_T) (ptr - line);
3307 extra_check = TRUE;
3308 }
3309
3310 /*
3311 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3312 * first character to be displayed.
3313 */
3314 if (wp->w_p_wrap)
3315 v = wp->w_skipcol;
3316 else
3317 v = wp->w_leftcol;
3318 if (v > 0)
3319 {
3320#ifdef FEAT_MBYTE
3321 char_u *prev_ptr = ptr;
3322#endif
3323 while (vcol < v && *ptr != NUL)
3324 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003325 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 vcol += c;
3327#ifdef FEAT_MBYTE
3328 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003330 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 }
3332
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003333 /* When:
3334 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003335 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003336 * - 'virtualedit' is set, or
3337 * - the visual mode is active,
3338 * the end of the line may be before the start of the displayed part.
3339 */
3340 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003341#ifdef FEAT_SYN_HL
3342 wp->w_p_cuc || draw_color_col ||
3343#endif
3344#ifdef FEAT_VIRTUALEDIT
3345 virtual_active() ||
3346#endif
3347 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003348 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351
3352 /* Handle a character that's not completely on the screen: Put ptr at
3353 * that character but skip the first few screen characters. */
3354 if (vcol > v)
3355 {
3356 vcol -= c;
3357#ifdef FEAT_MBYTE
3358 ptr = prev_ptr;
3359#else
3360 --ptr;
3361#endif
3362 n_skip = v - vcol;
3363 }
3364
3365 /*
3366 * Adjust for when the inverted text is before the screen,
3367 * and when the start of the inverted text is before the screen.
3368 */
3369 if (tocol <= vcol)
3370 fromcol = 0;
3371 else if (fromcol >= 0 && fromcol < vcol)
3372 fromcol = vcol;
3373
3374#ifdef FEAT_LINEBREAK
3375 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3376 if (wp->w_p_wrap)
3377 need_showbreak = TRUE;
3378#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003379#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003380 /* When spell checking a word we need to figure out the start of the
3381 * word and if it's badly spelled or not. */
3382 if (has_spell)
3383 {
3384 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003385 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003386 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003387
3388 pos = wp->w_cursor;
3389 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003390 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003391 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003392
3393 /* spell_move_to() may call ml_get() and make "line" invalid */
3394 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3395 ptr = line + linecol;
3396
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003397 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003398 {
3399 /* no bad word found at line start, don't check until end of a
3400 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003401 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003402 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003403 }
3404 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003405 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003406 /* bad word found, use attributes until end of word */
3407 word_end = wp->w_cursor.col + len + 1;
3408
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003409 /* Turn index into actual attributes. */
3410 if (spell_hlf != HLF_COUNT)
3411 spell_attr = highlight_attr[spell_hlf];
3412 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003413 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003414
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003415# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003416 /* Need to restart syntax highlighting for this line. */
3417 if (has_syntax)
3418 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003419# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003420 }
3421#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 }
3423
3424 /*
3425 * Correct highlighting for cursor that can't be disabled.
3426 * Avoids having to check this for each character.
3427 */
3428 if (fromcol >= 0)
3429 {
3430 if (noinvcur)
3431 {
3432 if ((colnr_T)fromcol == wp->w_virtcol)
3433 {
3434 /* highlighting starts at cursor, let it start just after the
3435 * cursor */
3436 fromcol_prev = fromcol;
3437 fromcol = -1;
3438 }
3439 else if ((colnr_T)fromcol < wp->w_virtcol)
3440 /* restart highlighting after the cursor */
3441 fromcol_prev = wp->w_virtcol;
3442 }
3443 if (fromcol >= tocol)
3444 fromcol = -1;
3445 }
3446
3447#ifdef FEAT_SEARCH_EXTRA
3448 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003449 * Handle highlighting the last used search pattern and matches.
3450 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003452 cur = wp->w_match_head;
3453 shl_flag = FALSE;
3454 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003456 if (shl_flag == FALSE)
3457 {
3458 shl = &search_hl;
3459 shl_flag = TRUE;
3460 }
3461 else
3462 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003463 shl->startcol = MAXCOL;
3464 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 shl->attr_cur = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003466 v = (long)(ptr - line);
3467 if (cur != NULL)
3468 cur->pos.cur = 0;
3469 next_search_hl(wp, shl, lnum, (colnr_T)v, cur);
3470
3471 /* Need to get the line again, a multi-line regexp may have made it
3472 * invalid. */
3473 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3474 ptr = line + v;
3475
3476 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003478 if (shl->lnum == lnum)
3479 shl->startcol = shl->rm.startpos[0].col;
3480 else
3481 shl->startcol = 0;
3482 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3483 - shl->rm.startpos[0].lnum)
3484 shl->endcol = shl->rm.endpos[0].col;
3485 else
3486 shl->endcol = MAXCOL;
3487 /* Highlight one character for an empty match. */
3488 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003491 if (has_mbyte && line[shl->endcol] != NUL)
3492 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3493 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003495 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003497 if ((long)shl->startcol < v) /* match at leftcol */
3498 {
3499 shl->attr_cur = shl->attr;
3500 search_attr = shl->attr;
3501 }
3502 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003504 if (shl != &search_hl && cur != NULL)
3505 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 }
3507#endif
3508
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003509#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003510 /* Cursor line highlighting for 'cursorline' in the current window. Not
3511 * when Visual mode is active, because it's not clear what is selected
3512 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003513 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003514 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003515 {
3516 line_attr = hl_attr(HLF_CUL);
3517 area_highlighting = TRUE;
3518 }
3519#endif
3520
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003521 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 col = 0;
3523#ifdef FEAT_RIGHTLEFT
3524 if (wp->w_p_rl)
3525 {
3526 /* Rightleft window: process the text in the normal direction, but put
3527 * it in current_ScreenLine[] from right to left. Start at the
3528 * rightmost column of the window. */
3529 col = W_WIDTH(wp) - 1;
3530 off += col;
3531 }
3532#endif
3533
3534 /*
3535 * Repeat for the whole displayed line.
3536 */
3537 for (;;)
3538 {
3539 /* Skip this quickly when working on the text. */
3540 if (draw_state != WL_LINE)
3541 {
3542#ifdef FEAT_CMDWIN
3543 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3544 {
3545 draw_state = WL_CMDLINE;
3546 if (cmdwin_type != 0 && wp == curwin)
3547 {
3548 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003550 c_extra = cmdwin_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 char_attr = hl_attr(HLF_AT);
3552 }
3553 }
3554#endif
3555
3556#ifdef FEAT_FOLDING
3557 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3558 {
3559 draw_state = WL_FOLD;
3560 if (wp->w_p_fdc > 0)
3561 {
3562 /* Draw the 'foldcolumn'. */
3563 fill_foldcolumn(extra, wp, FALSE, lnum);
3564 n_extra = wp->w_p_fdc;
3565 p_extra = extra;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003566 p_extra[n_extra] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 c_extra = NUL;
3568 char_attr = hl_attr(HLF_FC);
3569 }
3570 }
3571#endif
3572
3573#ifdef FEAT_SIGNS
3574 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3575 {
3576 draw_state = WL_SIGN;
3577 /* Show the sign column when there are any signs in this
3578 * buffer or when using Netbeans. */
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003579 if (draw_signcolumn(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003581 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003583 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584# endif
3585
3586 /* Draw two cells with the sign value or blank. */
3587 c_extra = ' ';
3588 char_attr = hl_attr(HLF_SC);
3589 n_extra = 2;
3590
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003591 if (row == startrow
3592#ifdef FEAT_DIFF
3593 + filler_lines && filler_todo <= 0
3594#endif
3595 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 {
3597 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3598 SIGN_TEXT);
3599# ifdef FEAT_SIGN_ICONS
3600 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3601 SIGN_ICON);
3602 if (gui.in_use && icon_sign != 0)
3603 {
3604 /* Use the image in this position. */
3605 c_extra = SIGN_BYTE;
3606# ifdef FEAT_NETBEANS_INTG
3607 if (buf_signcount(wp->w_buffer, lnum) > 1)
3608 c_extra = MULTISIGN_BYTE;
3609# endif
3610 char_attr = icon_sign;
3611 }
3612 else
3613# endif
3614 if (text_sign != 0)
3615 {
3616 p_extra = sign_get_text(text_sign);
3617 if (p_extra != NULL)
3618 {
3619 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003620 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 }
3622 char_attr = sign_get_attr(text_sign, FALSE);
3623 }
3624 }
3625 }
3626 }
3627#endif
3628
3629 if (draw_state == WL_NR - 1 && n_extra == 0)
3630 {
3631 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003632 /* Display the absolute or relative line number. After the
3633 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3634 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 && (row == startrow
3636#ifdef FEAT_DIFF
3637 + filler_lines
3638#endif
3639 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3640 {
3641 /* Draw the line number (empty space after wrapping). */
3642 if (row == startrow
3643#ifdef FEAT_DIFF
3644 + filler_lines
3645#endif
3646 )
3647 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003648 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003649 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003650
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003651 if (wp->w_p_nu && !wp->w_p_rnu)
3652 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003653 num = (long)lnum;
3654 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003655 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003656 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003657 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003658 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003659 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003660 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003661 num = lnum;
3662 fmt = "%-*ld ";
3663 }
3664 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003665
Bram Moolenaar700e7342013-01-30 12:31:36 +01003666 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003667 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 if (wp->w_skipcol > 0)
3669 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3670 *p_extra = '-';
3671#ifdef FEAT_RIGHTLEFT
3672 if (wp->w_p_rl) /* reverse line numbers */
3673 rl_mirror(extra);
3674#endif
3675 p_extra = extra;
3676 c_extra = NUL;
3677 }
3678 else
3679 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003680 n_extra = number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 char_attr = hl_attr(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003682#ifdef FEAT_SYN_HL
3683 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003684 * the current line differently.
3685 * TODO: Can we use CursorLine instead of CursorLineNr
3686 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003687 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003688 && lnum == wp->w_cursor.lnum)
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003689 char_attr = hl_attr(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003690#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 }
3692 }
3693
Bram Moolenaar597a4222014-06-25 14:39:50 +02003694#ifdef FEAT_LINEBREAK
3695 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3696 && n_extra == 0 && *p_sbr != NUL)
3697 /* draw indent after showbreak value */
3698 draw_state = WL_BRI;
3699 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3700 /* After the showbreak, draw the breakindent */
3701 draw_state = WL_BRI - 1;
3702
3703 /* draw 'breakindent': indent wrapped text accordingly */
3704 if (draw_state == WL_BRI - 1 && n_extra == 0)
3705 {
3706 draw_state = WL_BRI;
3707# ifdef FEAT_DIFF
3708# endif
3709 if (wp->w_p_bri && n_extra == 0 && row != startrow
3710#ifdef FEAT_DIFF
3711 && filler_lines == 0
3712#endif
3713 )
3714 {
3715 char_attr = 0; /* was: hl_attr(HLF_AT); */
3716#ifdef FEAT_DIFF
3717 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003718 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003719 char_attr = hl_attr(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02003720 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3721 char_attr = hl_combine_attr(char_attr,
3722 hl_attr(HLF_CUL));
3723 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02003724#endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02003725 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003726 c_extra = ' ';
3727 n_extra = get_breakindent_win(wp,
3728 ml_get_buf(wp->w_buffer, lnum, FALSE));
3729 /* Correct end of highlighted area for 'breakindent',
3730 * required when 'linebreak' is also set. */
3731 if (tocol == vcol)
3732 tocol += n_extra;
3733 }
3734 }
3735#endif
3736
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3738 if (draw_state == WL_SBR - 1 && n_extra == 0)
3739 {
3740 draw_state = WL_SBR;
3741# ifdef FEAT_DIFF
3742 if (filler_todo > 0)
3743 {
3744 /* Draw "deleted" diff line(s). */
3745 if (char2cells(fill_diff) > 1)
3746 c_extra = '-';
3747 else
3748 c_extra = fill_diff;
3749# ifdef FEAT_RIGHTLEFT
3750 if (wp->w_p_rl)
3751 n_extra = col + 1;
3752 else
3753# endif
3754 n_extra = W_WIDTH(wp) - col;
3755 char_attr = hl_attr(HLF_DED);
3756 }
3757# endif
3758# ifdef FEAT_LINEBREAK
3759 if (*p_sbr != NUL && need_showbreak)
3760 {
3761 /* Draw 'showbreak' at the start of each broken line. */
3762 p_extra = p_sbr;
3763 c_extra = NUL;
3764 n_extra = (int)STRLEN(p_sbr);
3765 char_attr = hl_attr(HLF_AT);
3766 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01003767 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 /* Correct end of highlighted area for 'showbreak',
3769 * required when 'linebreak' is also set. */
3770 if (tocol == vcol)
3771 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003772#ifdef FEAT_SYN_HL
3773 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003774 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003775 char_attr = hl_combine_attr(char_attr,
3776 hl_attr(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003777#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 }
3779# endif
3780 }
3781#endif
3782
3783 if (draw_state == WL_LINE - 1 && n_extra == 0)
3784 {
3785 draw_state = WL_LINE;
3786 if (saved_n_extra)
3787 {
3788 /* Continue item from end of wrapped line. */
3789 n_extra = saved_n_extra;
3790 c_extra = saved_c_extra;
3791 p_extra = saved_p_extra;
3792 char_attr = saved_char_attr;
3793 }
3794 else
3795 char_attr = 0;
3796 }
3797 }
3798
3799 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01003800 if (dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003801 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802#ifdef FEAT_DIFF
3803 && filler_todo <= 0
3804#endif
3805 )
3806 {
3807 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3808 wp->w_p_rl);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003809 /* Pretend we have finished updating the window. Except when
3810 * 'cursorcolumn' is set. */
3811#ifdef FEAT_SYN_HL
3812 if (wp->w_p_cuc)
3813 row = wp->w_cline_row + wp->w_cline_height;
3814 else
3815#endif
3816 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 break;
3818 }
3819
3820 if (draw_state == WL_LINE && area_highlighting)
3821 {
3822 /* handle Visual or match highlighting in this line */
3823 if (vcol == fromcol
3824#ifdef FEAT_MBYTE
3825 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3826 && (*mb_ptr2cells)(ptr) > 1)
3827#endif
3828 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00003829 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 && vcol < tocol))
3831 area_attr = attr; /* start highlighting */
3832 else if (area_attr != 0
3833 && (vcol == tocol
3834 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836
3837#ifdef FEAT_SEARCH_EXTRA
3838 if (!n_extra)
3839 {
3840 /*
3841 * Check for start/end of search pattern match.
3842 * After end, check for start/end of next match.
3843 * When another match, have to check for start again.
3844 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003845 * Do this for 'search_hl' and the match list (ordered by
3846 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003848 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003849 cur = wp->w_match_head;
3850 shl_flag = FALSE;
3851 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003853 if (shl_flag == FALSE
3854 && ((cur != NULL
3855 && cur->priority > SEARCH_HL_PRIORITY)
3856 || cur == NULL))
3857 {
3858 shl = &search_hl;
3859 shl_flag = TRUE;
3860 }
3861 else
3862 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003863 if (cur != NULL)
3864 cur->pos.cur = 0;
3865 pos_inprogress = TRUE;
3866 while (shl->rm.regprog != NULL
3867 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003869 if (shl->startcol != MAXCOL
3870 && v >= (long)shl->startcol
3871 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01003873#ifdef FEAT_MBYTE
3874 int tmp_col = v + MB_PTR2LEN(ptr);
3875
3876 if (shl->endcol < tmp_col)
3877 shl->endcol = tmp_col;
3878#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 shl->attr_cur = shl->attr;
3880 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01003881 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 {
3883 shl->attr_cur = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003884 next_search_hl(wp, shl, lnum, (colnr_T)v, cur);
3885 pos_inprogress = cur == NULL || cur->pos.cur == 0
3886 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887
3888 /* Need to get the line again, a multi-line regexp
3889 * may have made it invalid. */
3890 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3891 ptr = line + v;
3892
3893 if (shl->lnum == lnum)
3894 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003895 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003897 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003899 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003901 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 {
3903 /* highlight empty match, try again after
3904 * it */
3905#ifdef FEAT_MBYTE
3906 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003907 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003908 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 else
3910#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003911 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 }
3913
3914 /* Loop to check if the match starts at the
3915 * current position */
3916 continue;
3917 }
3918 }
3919 break;
3920 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003921 if (shl != &search_hl && cur != NULL)
3922 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003924
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003925 /* Use attributes from match with highest priority among
3926 * 'search_hl' and the match list. */
3927 search_attr = search_hl.attr_cur;
3928 cur = wp->w_match_head;
3929 shl_flag = FALSE;
3930 while (cur != NULL || shl_flag == FALSE)
3931 {
3932 if (shl_flag == FALSE
3933 && ((cur != NULL
3934 && cur->priority > SEARCH_HL_PRIORITY)
3935 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003936 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003937 shl = &search_hl;
3938 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003939 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003940 else
3941 shl = &cur->hl;
3942 if (shl->attr_cur != 0)
3943 search_attr = shl->attr_cur;
3944 if (shl != &search_hl && cur != NULL)
3945 cur = cur->next;
3946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 }
3948#endif
3949
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003951 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003953 if (diff_hlf == HLF_CHD && ptr - line >= change_start
3954 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003956 if (diff_hlf == HLF_TXD && ptr - line > change_end
3957 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003959 line_attr = hl_attr(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02003960 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3961 line_attr = hl_combine_attr(line_attr, hl_attr(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 }
3963#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003964
3965 /* Decide which of the highlight attributes to use. */
3966 attr_pri = TRUE;
3967 if (area_attr != 0)
3968 char_attr = area_attr;
3969 else if (search_attr != 0)
3970 char_attr = search_attr;
3971#ifdef LINE_ATTR
3972 /* Use line_attr when not in the Visual or 'incsearch' area
3973 * (area_attr may be 0 when "noinvcur" is set). */
3974 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00003975 || vcol < fromcol || vcol_prev < fromcol_prev
3976 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003977 char_attr = line_attr;
3978#endif
3979 else
3980 {
3981 attr_pri = FALSE;
3982#ifdef FEAT_SYN_HL
3983 if (has_syntax)
3984 char_attr = syntax_attr;
3985 else
3986#endif
3987 char_attr = 0;
3988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 }
3990
3991 /*
3992 * Get the next character to put on the screen.
3993 */
3994 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00003995 * The "p_extra" points to the extra stuff that is inserted to
3996 * represent special characters (non-printable stuff) and other
3997 * things. When all characters are the same, c_extra is used.
3998 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
3999 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4001 */
4002 if (n_extra > 0)
4003 {
4004 if (c_extra != NUL)
4005 {
4006 c = c_extra;
4007#ifdef FEAT_MBYTE
4008 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
4009 if (enc_utf8 && (*mb_char2len)(c) > 1)
4010 {
4011 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004012 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004013 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 }
4015 else
4016 mb_utf8 = FALSE;
4017#endif
4018 }
4019 else
4020 {
4021 c = *p_extra;
4022#ifdef FEAT_MBYTE
4023 if (has_mbyte)
4024 {
4025 mb_c = c;
4026 if (enc_utf8)
4027 {
4028 /* If the UTF-8 character is more than one byte:
4029 * Decode it into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004030 mb_l = (*mb_ptr2len)(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 mb_utf8 = FALSE;
4032 if (mb_l > n_extra)
4033 mb_l = 1;
4034 else if (mb_l > 1)
4035 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004036 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004038 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 }
4040 }
4041 else
4042 {
4043 /* if this is a DBCS character, put it in "mb_c" */
4044 mb_l = MB_BYTE2LEN(c);
4045 if (mb_l >= n_extra)
4046 mb_l = 1;
4047 else if (mb_l > 1)
4048 mb_c = (c << 8) + p_extra[1];
4049 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004050 if (mb_l == 0) /* at the NUL at end-of-line */
4051 mb_l = 1;
4052
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 /* If a double-width char doesn't fit display a '>' in the
4054 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004055 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056# ifdef FEAT_RIGHTLEFT
4057 wp->w_p_rl ? (col <= 0) :
4058# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004059 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 && (*mb_char2cells)(mb_c) == 2)
4061 {
4062 c = '>';
4063 mb_c = c;
4064 mb_l = 1;
4065 mb_utf8 = FALSE;
4066 multi_attr = hl_attr(HLF_AT);
4067 /* put the pointer back to output the double-width
4068 * character at the start of the next line. */
4069 ++n_extra;
4070 --p_extra;
4071 }
4072 else
4073 {
4074 n_extra -= mb_l - 1;
4075 p_extra += mb_l - 1;
4076 }
4077 }
4078#endif
4079 ++p_extra;
4080 }
4081 --n_extra;
4082 }
4083 else
4084 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004085 if (p_extra_free != NULL)
4086 {
4087 vim_free(p_extra_free);
4088 p_extra_free = NULL;
4089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 /*
4091 * Get a character from the line itself.
4092 */
4093 c = *ptr;
4094#ifdef FEAT_MBYTE
4095 if (has_mbyte)
4096 {
4097 mb_c = c;
4098 if (enc_utf8)
4099 {
4100 /* If the UTF-8 character is more than one byte: Decode it
4101 * into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004102 mb_l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 mb_utf8 = FALSE;
4104 if (mb_l > 1)
4105 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004106 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 /* Overlong encoded ASCII or ASCII with composing char
4108 * is displayed normally, except a NUL. */
4109 if (mb_c < 0x80)
4110 c = mb_c;
4111 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004112
4113 /* At start of the line we can have a composing char.
4114 * Draw it as a space with a composing char. */
4115 if (utf_iscomposing(mb_c))
4116 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004117 int i;
4118
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004119 for (i = Screen_mco - 1; i > 0; --i)
4120 u8cc[i] = u8cc[i - 1];
4121 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004122 mb_c = ' ';
4123 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 }
4125
4126 if ((mb_l == 1 && c >= 0x80)
4127 || (mb_l >= 1 && mb_c == 0)
4128 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004129# ifdef UNICODE16
4130 || mb_c >= 0x10000
4131# endif
4132 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 {
4134 /*
4135 * Illegal UTF-8 byte: display as <xx>.
4136 * Non-BMP character : display as ? or fullwidth ?.
4137 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004138# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004140# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 {
4142 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004143# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 if (wp->w_p_rl) /* reverse */
4145 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004146# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004148# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 else if (utf_char2cells(mb_c) != 2)
4150 STRCPY(extra, "?");
4151 else
4152 /* 0xff1f in UTF-8: full-width '?' */
4153 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004154# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155
4156 p_extra = extra;
4157 c = *p_extra;
4158 mb_c = mb_ptr2char_adv(&p_extra);
4159 mb_utf8 = (c >= 0x80);
4160 n_extra = (int)STRLEN(p_extra);
4161 c_extra = NUL;
4162 if (area_attr == 0 && search_attr == 0)
4163 {
4164 n_attr = n_extra + 1;
4165 extra_attr = hl_attr(HLF_8);
4166 saved_attr2 = char_attr; /* save current attr */
4167 }
4168 }
4169 else if (mb_l == 0) /* at the NUL at end-of-line */
4170 mb_l = 1;
4171#ifdef FEAT_ARABIC
4172 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4173 {
4174 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004175 int pc, pc1, nc;
4176 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177
4178 /* The idea of what is the previous and next
4179 * character depends on 'rightleft'. */
4180 if (wp->w_p_rl)
4181 {
4182 pc = prev_c;
4183 pc1 = prev_c1;
4184 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004185 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 }
4187 else
4188 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004189 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004191 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 }
4193 prev_c = mb_c;
4194
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004195 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 }
4197 else
4198 prev_c = mb_c;
4199#endif
4200 }
4201 else /* enc_dbcs */
4202 {
4203 mb_l = MB_BYTE2LEN(c);
4204 if (mb_l == 0) /* at the NUL at end-of-line */
4205 mb_l = 1;
4206 else if (mb_l > 1)
4207 {
4208 /* We assume a second byte below 32 is illegal.
4209 * Hopefully this is OK for all double-byte encodings!
4210 */
4211 if (ptr[1] >= 32)
4212 mb_c = (c << 8) + ptr[1];
4213 else
4214 {
4215 if (ptr[1] == NUL)
4216 {
4217 /* head byte at end of line */
4218 mb_l = 1;
4219 transchar_nonprint(extra, c);
4220 }
4221 else
4222 {
4223 /* illegal tail byte */
4224 mb_l = 2;
4225 STRCPY(extra, "XX");
4226 }
4227 p_extra = extra;
4228 n_extra = (int)STRLEN(extra) - 1;
4229 c_extra = NUL;
4230 c = *p_extra++;
4231 if (area_attr == 0 && search_attr == 0)
4232 {
4233 n_attr = n_extra + 1;
4234 extra_attr = hl_attr(HLF_8);
4235 saved_attr2 = char_attr; /* save current attr */
4236 }
4237 mb_c = c;
4238 }
4239 }
4240 }
4241 /* If a double-width char doesn't fit display a '>' in the
4242 * last column; the character is displayed at the start of the
4243 * next line. */
4244 if ((
4245# ifdef FEAT_RIGHTLEFT
4246 wp->w_p_rl ? (col <= 0) :
4247# endif
4248 (col >= W_WIDTH(wp) - 1))
4249 && (*mb_char2cells)(mb_c) == 2)
4250 {
4251 c = '>';
4252 mb_c = c;
4253 mb_utf8 = FALSE;
4254 mb_l = 1;
4255 multi_attr = hl_attr(HLF_AT);
4256 /* Put pointer back so that the character will be
4257 * displayed at the start of the next line. */
4258 --ptr;
4259 }
4260 else if (*ptr != NUL)
4261 ptr += mb_l - 1;
4262
4263 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004264 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004265 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004266 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004269 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 c = ' ';
4271 if (area_attr == 0 && search_attr == 0)
4272 {
4273 n_attr = n_extra + 1;
4274 extra_attr = hl_attr(HLF_AT);
4275 saved_attr2 = char_attr; /* save current attr */
4276 }
4277 mb_c = c;
4278 mb_utf8 = FALSE;
4279 mb_l = 1;
4280 }
4281
4282 }
4283#endif
4284 ++ptr;
4285
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004286 /* 'list' : change char 160 to lcs_nbsp. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004287 if (wp->w_p_list && (c == 160
4288#ifdef FEAT_MBYTE
4289 || (mb_utf8 && mb_c == 160)
4290#endif
4291 ) && lcs_nbsp)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004292 {
4293 c = lcs_nbsp;
4294 if (area_attr == 0 && search_attr == 0)
4295 {
4296 n_attr = 1;
4297 extra_attr = hl_attr(HLF_8);
4298 saved_attr2 = char_attr; /* save current attr */
4299 }
4300#ifdef FEAT_MBYTE
4301 mb_c = c;
4302 if (enc_utf8 && (*mb_char2len)(c) > 1)
4303 {
4304 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004305 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004306 c = 0xc0;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004307 }
4308 else
4309 mb_utf8 = FALSE;
4310#endif
4311 }
4312
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 if (extra_check)
4314 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004315#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004316 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004317#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004318
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004319#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 /* Get syntax attribute, unless still at the start of the line
4321 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004322 v = (long)(ptr - line);
4323 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 {
4325 /* Get the syntax attribute for the character. If there
4326 * is an error, disable syntax highlighting. */
4327 save_did_emsg = did_emsg;
4328 did_emsg = FALSE;
4329
Bram Moolenaar217ad922005-03-20 22:37:15 +00004330 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004331# ifdef FEAT_SPELL
4332 has_spell ? &can_spell :
4333# endif
4334 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335
4336 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004337 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004338 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004339 has_syntax = FALSE;
4340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 else
4342 did_emsg = save_did_emsg;
4343
4344 /* Need to get the line again, a multi-line regexp may
4345 * have made it invalid. */
4346 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4347 ptr = line + v;
4348
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004349 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004351 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004352 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004353# ifdef FEAT_CONCEAL
4354 /* no concealing past the end of the line, it interferes
4355 * with line highlighting */
4356 if (c == NUL)
4357 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004358 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004359 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004360# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004362#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004363
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004364#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004365 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004366 * Only do this when there is no syntax highlighting, the
4367 * @Spell cluster is not used or the current syntax item
4368 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004369 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004370 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004371 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004372# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004373 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004374 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004375# endif
4376 if (c != 0 && (
4377# ifdef FEAT_SYN_HL
4378 !has_syntax ||
4379# endif
4380 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004381 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004382 char_u *prev_ptr, *p;
4383 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004384 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004385# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004386 if (has_mbyte)
4387 {
4388 prev_ptr = ptr - mb_l;
4389 v -= mb_l - 1;
4390 }
4391 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004392# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004393 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004394
4395 /* Use nextline[] if possible, it has the start of the
4396 * next line concatenated. */
4397 if ((prev_ptr - line) - nextlinecol >= 0)
4398 p = nextline + (prev_ptr - line) - nextlinecol;
4399 else
4400 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004401 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004402 len = spell_check(wp, p, &spell_hlf, &cap_col,
4403 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004404 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004405
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004406 /* In Insert mode only highlight a word that
4407 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004408 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004409 && (State & INSERT) != 0
4410 && wp->w_cursor.lnum == lnum
4411 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004412 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004413 && wp->w_cursor.col < (colnr_T)word_end)
4414 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004415 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004416 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004417 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004418
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004419 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004420 && (p - nextline) + len > nextline_idx)
4421 {
4422 /* Remember that the good word continues at the
4423 * start of the next line. */
4424 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004425 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004426 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004427
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004428 /* Turn index into actual attributes. */
4429 if (spell_hlf != HLF_COUNT)
4430 spell_attr = highlight_attr[spell_hlf];
4431
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004432 if (cap_col > 0)
4433 {
4434 if (p != prev_ptr
4435 && (p - nextline) + cap_col >= nextline_idx)
4436 {
4437 /* Remember that the word in the next line
4438 * must start with a capital. */
4439 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004440 cap_col = (int)((p - nextline) + cap_col
4441 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004442 }
4443 else
4444 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004445 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004446 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004447 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004448 }
4449 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004450 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004451 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004452 char_attr = hl_combine_attr(char_attr, spell_attr);
4453 else
4454 char_attr = hl_combine_attr(spell_attr, char_attr);
4455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456#endif
4457#ifdef FEAT_LINEBREAK
4458 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004459 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004461 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02004463 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464# ifdef FEAT_MBYTE
4465 has_mbyte ? mb_l :
4466# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004467 1);
4468 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004469 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004470 NULL) - 1;
Bram Moolenaara3650912014-11-19 13:21:57 +01004471 if (c == TAB && n_extra + col > W_WIDTH(wp))
4472 n_extra = (int)wp->w_buffer->b_p_ts
4473 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4474
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 c_extra = ' ';
4476 if (vim_iswhite(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004477 {
4478#ifdef FEAT_CONCEAL
4479 if (c == TAB)
4480 /* See "Tab alignment" below. */
4481 FIX_FOR_BOGUSCOLS;
4482#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004483 if (!wp->w_p_list)
4484 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004485 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 }
4487#endif
4488
4489 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4490 {
4491 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004492 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 {
4494 n_attr = 1;
4495 extra_attr = hl_attr(HLF_8);
4496 saved_attr2 = char_attr; /* save current attr */
4497 }
4498#ifdef FEAT_MBYTE
4499 mb_c = c;
4500 if (enc_utf8 && (*mb_char2len)(c) > 1)
4501 {
4502 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004503 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004504 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 }
4506 else
4507 mb_utf8 = FALSE;
4508#endif
4509 }
4510 }
4511
4512 /*
4513 * Handling of non-printable characters.
4514 */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004515 if (!(chartab[c & 0xff] & CT_PRINT_CHAR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 {
4517 /*
4518 * when getting a character from the file, we may have to
4519 * turn it into something else on the way to putting it
4520 * into "ScreenLines".
4521 */
4522 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4523 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004524 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004525 long vcol_adjusted = vcol; /* removed showbreak length */
4526#ifdef FEAT_LINEBREAK
4527 /* only adjust the tab_len, when at the first column
4528 * after the showbreak value was drawn */
4529 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4530 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4531#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 /* tab amount depends on current column */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004533 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaard574ea22015-01-14 19:35:14 +01004534 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4535
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004536#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004537 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004538#endif
4539 /* tab amount depends on current column */
4540 n_extra = tab_len;
4541#ifdef FEAT_LINEBREAK
4542 else
4543 {
4544 char_u *p;
4545 int len = n_extra;
4546 int i;
4547 int saved_nextra = n_extra;
4548
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004549#ifdef FEAT_CONCEAL
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004550 if ((is_concealing || boguscols > 0) && vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004551 /* there are characters to conceal */
4552 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004553 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
4554 */
4555 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4556 && n_extra > tab_len)
4557 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004558#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004559
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004560 /* if n_extra > 0, it gives the number of chars, to
4561 * use for a tab, else we need to calculate the width
4562 * for a tab */
4563#ifdef FEAT_MBYTE
4564 len = (tab_len * mb_char2len(lcs_tab2));
4565 if (n_extra > 0)
4566 len += n_extra - tab_len;
4567#endif
4568 c = lcs_tab1;
4569 p = alloc((unsigned)(len + 1));
4570 vim_memset(p, ' ', len);
4571 p[len] = NUL;
4572 p_extra_free = p;
4573 for (i = 0; i < tab_len; i++)
4574 {
4575#ifdef FEAT_MBYTE
4576 mb_char2bytes(lcs_tab2, p);
4577 p += mb_char2len(lcs_tab2);
4578 n_extra += mb_char2len(lcs_tab2)
4579 - (saved_nextra > 0 ? 1 : 0);
4580#else
4581 p[i] = lcs_tab2;
4582#endif
4583 }
4584 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004585#ifdef FEAT_CONCEAL
4586 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
4587 * macro below, so need to adjust for that here */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004588 if ((is_concealing || boguscols > 0) && vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004589 n_extra -= vcol_off;
4590#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004591 }
4592#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004593#ifdef FEAT_CONCEAL
4594 /* Tab alignment should be identical regardless of
4595 * 'conceallevel' value. So tab compensates of all
4596 * previous concealed characters, and thus resets vcol_off
4597 * and boguscols accumulated so far in the line. Note that
4598 * the tab can be longer than 'tabstop' when there
4599 * are concealed characters. */
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004600 FIX_FOR_BOGUSCOLS;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004601 /* Make sure, the highlighting for the tab char will be
4602 * correctly set further below (effectively reverts the
4603 * FIX_FOR_BOGSUCOLS macro */
4604 if (old_boguscols > 0 && n_extra > tab_len && wp->w_p_list
4605 && lcs_tab1)
4606 tab_len += n_extra - tab_len;
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004607#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608#ifdef FEAT_MBYTE
4609 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4610#endif
4611 if (wp->w_p_list)
4612 {
4613 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004614#ifdef FEAT_LINEBREAK
4615 if (wp->w_p_lbr)
4616 c_extra = NUL; /* using p_extra from above */
4617 else
4618#endif
4619 c_extra = lcs_tab2;
4620 n_attr = tab_len + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 extra_attr = hl_attr(HLF_8);
4622 saved_attr2 = char_attr; /* save current attr */
4623#ifdef FEAT_MBYTE
4624 mb_c = c;
4625 if (enc_utf8 && (*mb_char2len)(c) > 1)
4626 {
4627 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004628 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004629 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 }
4631#endif
4632 }
4633 else
4634 {
4635 c_extra = ' ';
4636 c = ' ';
4637 }
4638 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004639 else if (c == NUL
4640 && ((wp->w_p_list && lcs_eol > 0)
4641 || ((fromcol >= 0 || fromcol_prev >= 0)
4642 && tocol > vcol
4643 && VIsual_mode != Ctrl_V
4644 && (
4645# ifdef FEAT_RIGHTLEFT
4646 wp->w_p_rl ? (col >= 0) :
4647# endif
4648 (col < W_WIDTH(wp)))
4649 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004650 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004651 && (colnr_T)vcol == wp->w_virtcol)))
4652 && lcs_eol_one >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004654 /* Display a '$' after the line or highlight an extra
4655 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4657 /* For a diff line the highlighting continues after the
4658 * "$". */
4659 if (
4660# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004661 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662# ifdef LINE_ATTR
4663 &&
4664# endif
4665# endif
4666# ifdef LINE_ATTR
4667 line_attr == 0
4668# endif
4669 )
4670#endif
4671 {
4672#ifdef FEAT_VIRTUALEDIT
4673 /* In virtualedit, visual selections may extend
4674 * beyond end of line. */
4675 if (area_highlighting && virtual_active()
4676 && tocol != MAXCOL && vcol < tocol)
4677 n_extra = 0;
4678 else
4679#endif
4680 {
4681 p_extra = at_end_str;
4682 n_extra = 1;
4683 c_extra = NUL;
4684 }
4685 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004686 if (wp->w_p_list)
4687 c = lcs_eol;
4688 else
4689 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 lcs_eol_one = -1;
4691 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004692 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 {
4694 extra_attr = hl_attr(HLF_AT);
4695 n_attr = 1;
4696 }
4697#ifdef FEAT_MBYTE
4698 mb_c = c;
4699 if (enc_utf8 && (*mb_char2len)(c) > 1)
4700 {
4701 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004702 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004703 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 }
4705 else
4706 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4707#endif
4708 }
4709 else if (c != NUL)
4710 {
4711 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02004712 if (n_extra == 0)
4713 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714#ifdef FEAT_RIGHTLEFT
4715 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4716 rl_mirror(p_extra); /* reverse "<12>" */
4717#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004719#ifdef FEAT_LINEBREAK
4720 if (wp->w_p_lbr)
4721 {
4722 char_u *p;
4723
4724 c = *p_extra;
4725 p = alloc((unsigned)n_extra + 1);
4726 vim_memset(p, ' ', n_extra);
4727 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
4728 p[n_extra] = NUL;
4729 p_extra_free = p_extra = p;
4730 }
4731 else
4732#endif
4733 {
4734 n_extra = byte2cells(c) - 1;
4735 c = *p_extra++;
4736 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004737 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 {
4739 n_attr = n_extra + 1;
4740 extra_attr = hl_attr(HLF_8);
4741 saved_attr2 = char_attr; /* save current attr */
4742 }
4743#ifdef FEAT_MBYTE
4744 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4745#endif
4746 }
4747#ifdef FEAT_VIRTUALEDIT
4748 else if (VIsual_active
4749 && (VIsual_mode == Ctrl_V
4750 || VIsual_mode == 'v')
4751 && virtual_active()
4752 && tocol != MAXCOL
4753 && vcol < tocol
4754 && (
4755# ifdef FEAT_RIGHTLEFT
4756 wp->w_p_rl ? (col >= 0) :
4757# endif
4758 (col < W_WIDTH(wp))))
4759 {
4760 c = ' ';
4761 --ptr; /* put it back at the NUL */
4762 }
4763#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004764#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 else if ((
4766# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004767 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 ) && (
4771# ifdef FEAT_RIGHTLEFT
4772 wp->w_p_rl ? (col >= 0) :
4773# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02004774 (col
4775# ifdef FEAT_CONCEAL
4776 - boguscols
4777# endif
4778 < W_WIDTH(wp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 {
4780 /* Highlight until the right side of the window */
4781 c = ' ';
4782 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004783
4784 /* Remember we do the char for line highlighting. */
4785 ++did_line_attr;
4786
4787 /* don't do search HL for the rest of the line */
4788 if (line_attr != 0 && char_attr == search_attr && col > 0)
4789 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790# ifdef FEAT_DIFF
4791 if (diff_hlf == HLF_TXD)
4792 {
4793 diff_hlf = HLF_CHD;
4794 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004795 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 char_attr = hl_attr(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004797 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4798 char_attr = hl_combine_attr(char_attr,
4799 hl_attr(HLF_CUL));
4800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 }
4802# endif
4803 }
4804#endif
4805 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02004806
4807#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004808 if ( wp->w_p_cole > 0
4809 && (wp != curwin || lnum != wp->w_cursor.lnum ||
4810 conceal_cursor_line(wp))
Bram Moolenaarf70e3d62010-07-24 13:15:07 +02004811 && (syntax_flags & HL_CONCEAL) != 0
Bram Moolenaare6dc5732010-07-24 23:52:26 +02004812 && !(lnum_in_visual_area
4813 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02004814 {
4815 char_attr = conceal_attr;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004816 if (prev_syntax_id != syntax_seqnr
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004817 && (syn_get_sub_char() != NUL || wp->w_p_cole == 1)
4818 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004819 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004820 /* First time at this concealed item: display one
4821 * character. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004822 if (syn_get_sub_char() != NUL)
4823 c = syn_get_sub_char();
4824 else if (lcs_conceal != NUL)
4825 c = lcs_conceal;
4826 else
4827 c = ' ';
4828
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004829 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004830
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004831 if (n_extra > 0)
4832 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004833 vcol += n_extra;
4834 if (wp->w_p_wrap && n_extra > 0)
4835 {
4836# ifdef FEAT_RIGHTLEFT
4837 if (wp->w_p_rl)
4838 {
4839 col -= n_extra;
4840 boguscols -= n_extra;
4841 }
4842 else
4843# endif
4844 {
4845 boguscols += n_extra;
4846 col += n_extra;
4847 }
4848 }
4849 n_extra = 0;
4850 n_attr = 0;
4851 }
4852 else if (n_skip == 0)
4853 {
4854 is_concealing = TRUE;
4855 n_skip = 1;
4856 }
4857# ifdef FEAT_MBYTE
4858 mb_c = c;
4859 if (enc_utf8 && (*mb_char2len)(c) > 1)
4860 {
4861 mb_utf8 = TRUE;
4862 u8cc[0] = 0;
4863 c = 0xc0;
4864 }
4865 else
4866 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4867# endif
4868 }
4869 else
4870 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004871 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02004872 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004873 }
4874#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 }
4876
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004877#ifdef FEAT_CONCEAL
4878 /* In the cursor line and we may be concealing characters: correct
4879 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02004880 if (!did_wcol && draw_state == WL_LINE
4881 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004882 && conceal_cursor_line(wp)
4883 && (int)wp->w_virtcol <= vcol + n_skip)
4884 {
4885 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02004886 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004887 did_wcol = TRUE;
4888 }
4889#endif
4890
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 /* Don't override visual selection highlighting. */
4892 if (n_attr > 0
4893 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004894 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 char_attr = extra_attr;
4896
Bram Moolenaar81695252004-12-29 20:58:21 +00004897#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 /* XIM don't send preedit_start and preedit_end, but they send
4899 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
4900 * im_is_preediting() here. */
4901 if (xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004902 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 && (State & INSERT)
4904 && !p_imdisable
4905 && im_is_preediting()
4906 && draw_state == WL_LINE)
4907 {
4908 colnr_T tcol;
4909
4910 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004911 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 else
4913 tcol = preedit_end_col;
4914 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
4915 {
4916 if (feedback_old_attr < 0)
4917 {
4918 feedback_col = 0;
4919 feedback_old_attr = char_attr;
4920 }
4921 char_attr = im_get_feedback_attr(feedback_col);
4922 if (char_attr < 0)
4923 char_attr = feedback_old_attr;
4924 feedback_col++;
4925 }
4926 else if (feedback_old_attr >= 0)
4927 {
4928 char_attr = feedback_old_attr;
4929 feedback_old_attr = -1;
4930 feedback_col = 0;
4931 }
4932 }
4933#endif
4934 /*
4935 * Handle the case where we are in column 0 but not on the first
4936 * character of the line and the user wants us to show us a
4937 * special character (via 'listchars' option "precedes:<char>".
4938 */
4939 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02004940 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
4942#ifdef FEAT_DIFF
4943 && filler_todo <= 0
4944#endif
4945 && draw_state > WL_NR
4946 && c != NUL)
4947 {
4948 c = lcs_prec;
4949 lcs_prec_todo = NUL;
4950#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02004951 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
4952 {
4953 /* Double-width character being overwritten by the "precedes"
4954 * character, need to fill up half the character. */
4955 c_extra = MB_FILLER_CHAR;
4956 n_extra = 1;
4957 n_attr = 2;
4958 extra_attr = hl_attr(HLF_AT);
4959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 mb_c = c;
4961 if (enc_utf8 && (*mb_char2len)(c) > 1)
4962 {
4963 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004964 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004965 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 }
4967 else
4968 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4969#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004970 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 {
4972 saved_attr3 = char_attr; /* save current attr */
4973 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
4974 n_attr3 = 1;
4975 }
4976 }
4977
4978 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00004979 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004981 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004982#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004983 || did_line_attr == 1
4984#endif
4985 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00004987#ifdef FEAT_SEARCH_EXTRA
4988 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00004989
4990 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00004991 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00004992 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004993#endif
4994
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004995 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 * highlight match at end of line. If it's beyond the last
4997 * char on the screen, just overwrite that one (tricky!) Not
4998 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004999#ifdef FEAT_SEARCH_EXTRA
5000 prevcol_hl_flag = FALSE;
5001 if (prevcol == (long)search_hl.startcol)
5002 prevcol_hl_flag = TRUE;
5003 else
5004 {
5005 cur = wp->w_match_head;
5006 while (cur != NULL)
5007 {
5008 if (prevcol == (long)cur->hl.startcol)
5009 {
5010 prevcol_hl_flag = TRUE;
5011 break;
5012 }
5013 cur = cur->next;
5014 }
5015 }
5016#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005018 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005019 && (VIsual_mode != Ctrl_V
5020 || lnum == VIsual.lnum
5021 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005022 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023#ifdef FEAT_SEARCH_EXTRA
5024 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005025 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005026# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005027 && did_line_attr <= 1
5028# endif
5029 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030#endif
5031 ))
5032 {
5033 int n = 0;
5034
5035#ifdef FEAT_RIGHTLEFT
5036 if (wp->w_p_rl)
5037 {
5038 if (col < 0)
5039 n = 1;
5040 }
5041 else
5042#endif
5043 {
5044 if (col >= W_WIDTH(wp))
5045 n = -1;
5046 }
5047 if (n != 0)
5048 {
5049 /* At the window boundary, highlight the last character
5050 * instead (better than nothing). */
5051 off += n;
5052 col += n;
5053 }
5054 else
5055 {
5056 /* Add a blank character to highlight. */
5057 ScreenLines[off] = ' ';
5058#ifdef FEAT_MBYTE
5059 if (enc_utf8)
5060 ScreenLinesUC[off] = 0;
5061#endif
5062 }
5063#ifdef FEAT_SEARCH_EXTRA
5064 if (area_attr == 0)
5065 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005066 /* Use attributes from match with highest priority among
5067 * 'search_hl' and the match list. */
5068 char_attr = search_hl.attr;
5069 cur = wp->w_match_head;
5070 shl_flag = FALSE;
5071 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005072 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005073 if (shl_flag == FALSE
5074 && ((cur != NULL
5075 && cur->priority > SEARCH_HL_PRIORITY)
5076 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005077 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005078 shl = &search_hl;
5079 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005080 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005081 else
5082 shl = &cur->hl;
5083 if ((ptr - line) - 1 == (long)shl->startcol)
5084 char_attr = shl->attr;
5085 if (shl != &search_hl && cur != NULL)
5086 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005087 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 }
5089#endif
5090 ScreenAttrs[off] = char_attr;
5091#ifdef FEAT_RIGHTLEFT
5092 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005093 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005095 --off;
5096 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 else
5098#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005099 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005101 ++off;
5102 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005103 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005104#ifdef FEAT_SYN_HL
5105 eol_hl_off = 1;
5106#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005108 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109
Bram Moolenaar91170f82006-05-05 21:15:17 +00005110 /*
5111 * At end of the text line.
5112 */
5113 if (c == NUL)
5114 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005115#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005116 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
5117 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00005118 {
5119 /* highlight last char after line */
5120 --col;
5121 --off;
5122 --vcol;
5123 }
5124
Bram Moolenaar1a384422010-07-14 19:53:30 +02005125 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005126 if (wp->w_p_wrap)
5127 v = wp->w_skipcol;
5128 else
5129 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005130
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005131 /* check if line ends before left margin */
5132 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005133 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005134#ifdef FEAT_CONCEAL
5135 /* Get rid of the boguscols now, we want to draw until the right
5136 * edge for 'cursorcolumn'. */
5137 col -= boguscols;
5138 boguscols = 0;
5139#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005140
5141 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005142 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005143
5144 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005145 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5146 && (int)wp->w_virtcol <
5147 W_WIDTH(wp) * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005148 && lnum != wp->w_cursor.lnum)
5149 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005150# ifdef FEAT_RIGHTLEFT
5151 && !wp->w_p_rl
5152# endif
5153 )
5154 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005155 int rightmost_vcol = 0;
5156 int i;
5157
5158 if (wp->w_p_cuc)
5159 rightmost_vcol = wp->w_virtcol;
5160 if (draw_color_col)
5161 /* determine rightmost colorcolumn to possibly draw */
5162 for (i = 0; color_cols[i] >= 0; ++i)
5163 if (rightmost_vcol < color_cols[i])
5164 rightmost_vcol = color_cols[i];
5165
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005166 while (col < W_WIDTH(wp))
5167 {
5168 ScreenLines[off] = ' ';
5169#ifdef FEAT_MBYTE
5170 if (enc_utf8)
5171 ScreenLinesUC[off] = 0;
5172#endif
5173 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005174 if (draw_color_col)
5175 draw_color_col = advance_color_col(VCOL_HLC,
5176 &color_cols);
5177
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005178 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005179 ScreenAttrs[off++] = hl_attr(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005180 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005181 ScreenAttrs[off++] = hl_attr(HLF_MC);
5182 else
5183 ScreenAttrs[off++] = 0;
5184
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005185 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005186 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005187
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005188 ++vcol;
5189 }
5190 }
5191#endif
5192
Bram Moolenaar860cae12010-06-05 23:22:07 +02005193 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5194 (int)W_WIDTH(wp), wp->w_p_rl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 row++;
5196
5197 /*
5198 * Update w_cline_height and w_cline_folded if the cursor line was
5199 * updated (saves a call to plines() later).
5200 */
5201 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5202 {
5203 curwin->w_cline_row = startrow;
5204 curwin->w_cline_height = row - startrow;
5205#ifdef FEAT_FOLDING
5206 curwin->w_cline_folded = FALSE;
5207#endif
5208 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5209 }
5210
5211 break;
5212 }
5213
5214 /* line continues beyond line end */
5215 if (lcs_ext
5216 && !wp->w_p_wrap
5217#ifdef FEAT_DIFF
5218 && filler_todo <= 0
5219#endif
5220 && (
5221#ifdef FEAT_RIGHTLEFT
5222 wp->w_p_rl ? col == 0 :
5223#endif
5224 col == W_WIDTH(wp) - 1)
5225 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005226 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5228 {
5229 c = lcs_ext;
5230 char_attr = hl_attr(HLF_AT);
5231#ifdef FEAT_MBYTE
5232 mb_c = c;
5233 if (enc_utf8 && (*mb_char2len)(c) > 1)
5234 {
5235 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005236 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005237 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 }
5239 else
5240 mb_utf8 = FALSE;
5241#endif
5242 }
5243
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005244#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005245 /* advance to the next 'colorcolumn' */
5246 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005247 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005248
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005249 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005250 * highlight the cursor position itself.
5251 * Also highlight the 'colorcolumn' if it is different than
5252 * 'cursorcolumn' */
5253 vcol_save_attr = -1;
5254 if (draw_state == WL_LINE && !lnum_in_visual_area)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005255 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005256 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005257 && lnum != wp->w_cursor.lnum)
5258 {
5259 vcol_save_attr = char_attr;
5260 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
5261 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005262 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005263 {
5264 vcol_save_attr = char_attr;
5265 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_MC));
5266 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005267 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005268#endif
5269
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 /*
5271 * Store character to be displayed.
5272 * Skip characters that are left of the screen for 'nowrap'.
5273 */
5274 vcol_prev = vcol;
5275 if (draw_state < WL_LINE || n_skip <= 0)
5276 {
5277 /*
5278 * Store the character.
5279 */
5280#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5281 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5282 {
5283 /* A double-wide character is: put first halve in left cell. */
5284 --off;
5285 --col;
5286 }
5287#endif
5288 ScreenLines[off] = c;
5289#ifdef FEAT_MBYTE
5290 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005291 {
5292 if ((mb_c & 0xff00) == 0x8e00)
5293 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 else if (enc_utf8)
5297 {
5298 if (mb_utf8)
5299 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005300 int i;
5301
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005303 if ((c & 0xff) == 0)
5304 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005305 for (i = 0; i < Screen_mco; ++i)
5306 {
5307 ScreenLinesC[i][off] = u8cc[i];
5308 if (u8cc[i] == 0)
5309 break;
5310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 }
5312 else
5313 ScreenLinesUC[off] = 0;
5314 }
5315 if (multi_attr)
5316 {
5317 ScreenAttrs[off] = multi_attr;
5318 multi_attr = 0;
5319 }
5320 else
5321#endif
5322 ScreenAttrs[off] = char_attr;
5323
5324#ifdef FEAT_MBYTE
5325 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5326 {
5327 /* Need to fill two screen columns. */
5328 ++off;
5329 ++col;
5330 if (enc_utf8)
5331 /* UTF-8: Put a 0 in the second screen char. */
5332 ScreenLines[off] = 0;
5333 else
5334 /* DBCS: Put second byte in the second screen char. */
5335 ScreenLines[off] = mb_c & 0xff;
5336 ++vcol;
5337 /* When "tocol" is halfway a character, set it to the end of
5338 * the character, otherwise highlighting won't stop. */
5339 if (tocol == vcol)
5340 ++tocol;
5341#ifdef FEAT_RIGHTLEFT
5342 if (wp->w_p_rl)
5343 {
5344 /* now it's time to backup one cell */
5345 --off;
5346 --col;
5347 }
5348#endif
5349 }
5350#endif
5351#ifdef FEAT_RIGHTLEFT
5352 if (wp->w_p_rl)
5353 {
5354 --off;
5355 --col;
5356 }
5357 else
5358#endif
5359 {
5360 ++off;
5361 ++col;
5362 }
5363 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005364#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005365 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005366 {
5367 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005368 ++vcol_off;
5369 if (n_extra > 0)
5370 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005371 if (wp->w_p_wrap)
5372 {
5373 /*
5374 * Special voodoo required if 'wrap' is on.
5375 *
5376 * Advance the column indicator to force the line
5377 * drawing to wrap early. This will make the line
5378 * take up the same screen space when parts are concealed,
5379 * so that cursor line computations aren't messed up.
5380 *
5381 * To avoid the fictitious advance of 'col' causing
5382 * trailing junk to be written out of the screen line
5383 * we are building, 'boguscols' keeps track of the number
5384 * of bad columns we have advanced.
5385 */
5386 if (n_extra > 0)
5387 {
5388 vcol += n_extra;
5389# ifdef FEAT_RIGHTLEFT
5390 if (wp->w_p_rl)
5391 {
5392 col -= n_extra;
5393 boguscols -= n_extra;
5394 }
5395 else
5396# endif
5397 {
5398 col += n_extra;
5399 boguscols += n_extra;
5400 }
5401 n_extra = 0;
5402 n_attr = 0;
5403 }
5404
5405
5406# ifdef FEAT_MBYTE
5407 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5408 {
5409 /* Need to fill two screen columns. */
5410# ifdef FEAT_RIGHTLEFT
5411 if (wp->w_p_rl)
5412 {
5413 --boguscols;
5414 --col;
5415 }
5416 else
5417# endif
5418 {
5419 ++boguscols;
5420 ++col;
5421 }
5422 }
5423# endif
5424
5425# ifdef FEAT_RIGHTLEFT
5426 if (wp->w_p_rl)
5427 {
5428 --boguscols;
5429 --col;
5430 }
5431 else
5432# endif
5433 {
5434 ++boguscols;
5435 ++col;
5436 }
5437 }
5438 else
5439 {
5440 if (n_extra > 0)
5441 {
5442 vcol += n_extra;
5443 n_extra = 0;
5444 n_attr = 0;
5445 }
5446 }
5447
5448 }
5449#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 else
5451 --n_skip;
5452
Bram Moolenaar64486672010-05-16 15:46:46 +02005453 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5454 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005455 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456#ifdef FEAT_DIFF
5457 && filler_todo <= 0
5458#endif
5459 )
5460 ++vcol;
5461
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005462#ifdef FEAT_SYN_HL
5463 if (vcol_save_attr >= 0)
5464 char_attr = vcol_save_attr;
5465#endif
5466
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 /* restore attributes after "predeces" in 'listchars' */
5468 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5469 char_attr = saved_attr3;
5470
5471 /* restore attributes after last 'listchars' or 'number' char */
5472 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5473 char_attr = saved_attr2;
5474
5475 /*
5476 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005477 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 */
5479 if ((
5480#ifdef FEAT_RIGHTLEFT
5481 wp->w_p_rl ? (col < 0) :
5482#endif
5483 (col >= W_WIDTH(wp)))
5484 && (*ptr != NUL
5485#ifdef FEAT_DIFF
5486 || filler_todo > 0
5487#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005488 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5490 )
5491 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005492#ifdef FEAT_CONCEAL
5493 SCREEN_LINE(screen_row, W_WINCOL(wp), col - boguscols,
5494 (int)W_WIDTH(wp), wp->w_p_rl);
5495 boguscols = 0;
5496#else
5497 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5498 (int)W_WIDTH(wp), wp->w_p_rl);
5499#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 ++row;
5501 ++screen_row;
5502
5503 /* When not wrapping and finished diff lines, or when displayed
5504 * '$' and highlighting until last column, break here. */
5505 if ((!wp->w_p_wrap
5506#ifdef FEAT_DIFF
5507 && filler_todo <= 0
5508#endif
5509 ) || lcs_eol_one == -1)
5510 break;
5511
5512 /* When the window is too narrow draw all "@" lines. */
5513 if (draw_state != WL_LINE
5514#ifdef FEAT_DIFF
5515 && filler_todo <= 0
5516#endif
5517 )
5518 {
5519 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
5520#ifdef FEAT_VERTSPLIT
5521 draw_vsep_win(wp, row);
5522#endif
5523 row = endrow;
5524 }
5525
5526 /* When line got too long for screen break here. */
5527 if (row == endrow)
5528 {
5529 ++row;
5530 break;
5531 }
5532
5533 if (screen_cur_row == screen_row - 1
5534#ifdef FEAT_DIFF
5535 && filler_todo <= 0
5536#endif
5537 && W_WIDTH(wp) == Columns)
5538 {
5539 /* Remember that the line wraps, used for modeless copy. */
5540 LineWraps[screen_row - 1] = TRUE;
5541
5542 /*
5543 * Special trick to make copy/paste of wrapped lines work with
5544 * xterm/screen: write an extra character beyond the end of
5545 * the line. This will work with all terminal types
5546 * (regardless of the xn,am settings).
5547 * Only do this on a fast tty.
5548 * Only do this if the cursor is on the current line
5549 * (something has been written in it).
5550 * Don't do this for the GUI.
5551 * Don't do this for double-width characters.
5552 * Don't do this for a window not at the right screen border.
5553 */
5554 if (p_tf
5555#ifdef FEAT_GUI
5556 && !gui.in_use
5557#endif
5558#ifdef FEAT_MBYTE
5559 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005560 && ((*mb_off2cells)(LineOffset[screen_row],
5561 LineOffset[screen_row] + screen_Columns)
5562 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005564 + (int)Columns - 2,
5565 LineOffset[screen_row] + screen_Columns)
5566 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567#endif
5568 )
5569 {
5570 /* First make sure we are at the end of the screen line,
5571 * then output the same character again to let the
5572 * terminal know about the wrap. If the terminal doesn't
5573 * auto-wrap, we overwrite the character. */
5574 if (screen_cur_col != W_WIDTH(wp))
5575 screen_char(LineOffset[screen_row - 1]
5576 + (unsigned)Columns - 1,
5577 screen_row - 1, (int)(Columns - 1));
5578
5579#ifdef FEAT_MBYTE
5580 /* When there is a multi-byte character, just output a
5581 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005582 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5583 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 out_char(' ');
5585 else
5586#endif
5587 out_char(ScreenLines[LineOffset[screen_row - 1]
5588 + (Columns - 1)]);
5589 /* force a redraw of the first char on the next line */
5590 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5591 screen_start(); /* don't know where cursor is now */
5592 }
5593 }
5594
5595 col = 0;
5596 off = (unsigned)(current_ScreenLine - ScreenLines);
5597#ifdef FEAT_RIGHTLEFT
5598 if (wp->w_p_rl)
5599 {
5600 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
5601 off += col;
5602 }
5603#endif
5604
5605 /* reset the drawing state for the start of a wrapped line */
5606 draw_state = WL_START;
5607 saved_n_extra = n_extra;
5608 saved_p_extra = p_extra;
5609 saved_c_extra = c_extra;
5610 saved_char_attr = char_attr;
5611 n_extra = 0;
5612 lcs_prec_todo = lcs_prec;
5613#ifdef FEAT_LINEBREAK
5614# ifdef FEAT_DIFF
5615 if (filler_todo <= 0)
5616# endif
5617 need_showbreak = TRUE;
5618#endif
5619#ifdef FEAT_DIFF
5620 --filler_todo;
5621 /* When the filler lines are actually below the last line of the
5622 * file, don't draw the line itself, break here. */
5623 if (filler_todo == 0 && wp->w_botfill)
5624 break;
5625#endif
5626 }
5627
5628 } /* for every character in the line */
5629
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005630#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005631 /* After an empty line check first word for capital. */
5632 if (*skipwhite(line) == NUL)
5633 {
5634 capcol_lnum = lnum + 1;
5635 cap_col = 0;
5636 }
5637#endif
5638
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 return row;
5640}
5641
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005642#ifdef FEAT_MBYTE
5643static int comp_char_differs __ARGS((int, int));
5644
5645/*
5646 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005647 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005648 */
5649 static int
5650comp_char_differs(off_from, off_to)
5651 int off_from;
5652 int off_to;
5653{
5654 int i;
5655
5656 for (i = 0; i < Screen_mco; ++i)
5657 {
5658 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5659 return TRUE;
5660 if (ScreenLinesC[i][off_from] == 0)
5661 break;
5662 }
5663 return FALSE;
5664}
5665#endif
5666
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667/*
5668 * Check whether the given character needs redrawing:
5669 * - the (first byte of the) character is different
5670 * - the attributes are different
5671 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005672 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 */
5674 static int
5675char_needs_redraw(off_from, off_to, cols)
5676 int off_from;
5677 int off_to;
5678 int cols;
5679{
5680 if (cols > 0
5681 && ((ScreenLines[off_from] != ScreenLines[off_to]
5682 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
5683
5684#ifdef FEAT_MBYTE
5685 || (enc_dbcs != 0
5686 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
5687 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
5688 ? ScreenLines2[off_from] != ScreenLines2[off_to]
5689 : (cols > 1 && ScreenLines[off_from + 1]
5690 != ScreenLines[off_to + 1])))
5691 || (enc_utf8
5692 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
5693 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005694 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02005695 || ((*mb_off2cells)(off_from, off_from + cols) > 1
5696 && ScreenLines[off_from + 1]
5697 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698#endif
5699 ))
5700 return TRUE;
5701 return FALSE;
5702}
5703
5704/*
5705 * Move one "cooked" screen line to the screen, but only the characters that
5706 * have actually changed. Handle insert/delete character.
5707 * "coloff" gives the first column on the screen for this line.
5708 * "endcol" gives the columns where valid characters are.
5709 * "clear_width" is the width of the window. It's > 0 if the rest of the line
5710 * needs to be cleared, negative otherwise.
5711 * "rlflag" is TRUE in a rightleft window:
5712 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
5713 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
5714 */
5715 static void
5716screen_line(row, coloff, endcol, clear_width
5717#ifdef FEAT_RIGHTLEFT
5718 , rlflag
5719#endif
5720 )
5721 int row;
5722 int coloff;
5723 int endcol;
5724 int clear_width;
5725#ifdef FEAT_RIGHTLEFT
5726 int rlflag;
5727#endif
5728{
5729 unsigned off_from;
5730 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005731#ifdef FEAT_MBYTE
5732 unsigned max_off_from;
5733 unsigned max_off_to;
5734#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 int col = 0;
5736#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_VERTSPLIT)
5737 int hl;
5738#endif
5739 int force = FALSE; /* force update rest of the line */
5740 int redraw_this /* bool: does character need redraw? */
5741#ifdef FEAT_GUI
5742 = TRUE /* For GUI when while-loop empty */
5743#endif
5744 ;
5745 int redraw_next; /* redraw_this for next character */
5746#ifdef FEAT_MBYTE
5747 int clear_next = FALSE;
5748 int char_cells; /* 1: normal char */
5749 /* 2: occupies two display cells */
5750# define CHAR_CELLS char_cells
5751#else
5752# define CHAR_CELLS 1
5753#endif
5754
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01005755 /* Check for illegal row and col, just in case. */
5756 if (row >= Rows)
5757 row = Rows - 1;
5758 if (endcol > Columns)
5759 endcol = Columns;
5760
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761# ifdef FEAT_CLIPBOARD
5762 clip_may_clear_selection(row, row);
5763# endif
5764
5765 off_from = (unsigned)(current_ScreenLine - ScreenLines);
5766 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005767#ifdef FEAT_MBYTE
5768 max_off_from = off_from + screen_Columns;
5769 max_off_to = LineOffset[row] + screen_Columns;
5770#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771
5772#ifdef FEAT_RIGHTLEFT
5773 if (rlflag)
5774 {
5775 /* Clear rest first, because it's left of the text. */
5776 if (clear_width > 0)
5777 {
5778 while (col <= endcol && ScreenLines[off_to] == ' '
5779 && ScreenAttrs[off_to] == 0
5780# ifdef FEAT_MBYTE
5781 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5782# endif
5783 )
5784 {
5785 ++off_to;
5786 ++col;
5787 }
5788 if (col <= endcol)
5789 screen_fill(row, row + 1, col + coloff,
5790 endcol + coloff + 1, ' ', ' ', 0);
5791 }
5792 col = endcol + 1;
5793 off_to = LineOffset[row] + col + coloff;
5794 off_from += col;
5795 endcol = (clear_width > 0 ? clear_width : -clear_width);
5796 }
5797#endif /* FEAT_RIGHTLEFT */
5798
5799 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
5800
5801 while (col < endcol)
5802 {
5803#ifdef FEAT_MBYTE
5804 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00005805 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 else
5807 char_cells = 1;
5808#endif
5809
5810 redraw_this = redraw_next;
5811 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
5812 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
5813
5814#ifdef FEAT_GUI
5815 /* If the next character was bold, then redraw the current character to
5816 * remove any pixels that might have spilt over into us. This only
5817 * happens in the GUI.
5818 */
5819 if (redraw_next && gui.in_use)
5820 {
5821 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005822 if (hl > HL_ALL)
5823 hl = syn_attr2attr(hl);
5824 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 redraw_this = TRUE;
5826 }
5827#endif
5828
5829 if (redraw_this)
5830 {
5831 /*
5832 * Special handling when 'xs' termcap flag set (hpterm):
5833 * Attributes for characters are stored at the position where the
5834 * cursor is when writing the highlighting code. The
5835 * start-highlighting code must be written with the cursor on the
5836 * first highlighted character. The stop-highlighting code must
5837 * be written with the cursor just after the last highlighted
5838 * character.
5839 * Overwriting a character doesn't remove it's highlighting. Need
5840 * to clear the rest of the line, and force redrawing it
5841 * completely.
5842 */
5843 if ( p_wiv
5844 && !force
5845#ifdef FEAT_GUI
5846 && !gui.in_use
5847#endif
5848 && ScreenAttrs[off_to] != 0
5849 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
5850 {
5851 /*
5852 * Need to remove highlighting attributes here.
5853 */
5854 windgoto(row, col + coloff);
5855 out_str(T_CE); /* clear rest of this screen line */
5856 screen_start(); /* don't know where cursor is now */
5857 force = TRUE; /* force redraw of rest of the line */
5858 redraw_next = TRUE; /* or else next char would miss out */
5859
5860 /*
5861 * If the previous character was highlighted, need to stop
5862 * highlighting at this character.
5863 */
5864 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
5865 {
5866 screen_attr = ScreenAttrs[off_to - 1];
5867 term_windgoto(row, col + coloff);
5868 screen_stop_highlight();
5869 }
5870 else
5871 screen_attr = 0; /* highlighting has stopped */
5872 }
5873#ifdef FEAT_MBYTE
5874 if (enc_dbcs != 0)
5875 {
5876 /* Check if overwriting a double-byte with a single-byte or
5877 * the other way around requires another character to be
5878 * redrawn. For UTF-8 this isn't needed, because comparing
5879 * ScreenLinesUC[] is sufficient. */
5880 if (char_cells == 1
5881 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005882 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883 {
5884 /* Writing a single-cell character over a double-cell
5885 * character: need to redraw the next cell. */
5886 ScreenLines[off_to + 1] = 0;
5887 redraw_next = TRUE;
5888 }
5889 else if (char_cells == 2
5890 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005891 && (*mb_off2cells)(off_to, max_off_to) == 1
5892 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893 {
5894 /* Writing the second half of a double-cell character over
5895 * a double-cell character: need to redraw the second
5896 * cell. */
5897 ScreenLines[off_to + 2] = 0;
5898 redraw_next = TRUE;
5899 }
5900
5901 if (enc_dbcs == DBCS_JPNU)
5902 ScreenLines2[off_to] = ScreenLines2[off_from];
5903 }
5904 /* When writing a single-width character over a double-width
5905 * character and at the end of the redrawn text, need to clear out
5906 * the right halve of the old character.
5907 * Also required when writing the right halve of a double-width
5908 * char over the left halve of an existing one. */
5909 if (has_mbyte && col + char_cells == endcol
5910 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00005911 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00005913 && (*mb_off2cells)(off_to, max_off_to) == 1
5914 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915 clear_next = TRUE;
5916#endif
5917
5918 ScreenLines[off_to] = ScreenLines[off_from];
5919#ifdef FEAT_MBYTE
5920 if (enc_utf8)
5921 {
5922 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
5923 if (ScreenLinesUC[off_from] != 0)
5924 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005925 int i;
5926
5927 for (i = 0; i < Screen_mco; ++i)
5928 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929 }
5930 }
5931 if (char_cells == 2)
5932 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
5933#endif
5934
5935#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00005936 /* The bold trick makes a single column of pixels appear in the
5937 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 * character should be redrawn too. This happens for our own GUI
5939 * and for some xterms. */
5940 if (
5941# ifdef FEAT_GUI
5942 gui.in_use
5943# endif
5944# if defined(FEAT_GUI) && defined(UNIX)
5945 ||
5946# endif
5947# ifdef UNIX
5948 term_is_xterm
5949# endif
5950 )
5951 {
5952 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005953 if (hl > HL_ALL)
5954 hl = syn_attr2attr(hl);
5955 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956 redraw_next = TRUE;
5957 }
5958#endif
5959 ScreenAttrs[off_to] = ScreenAttrs[off_from];
5960#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005961 /* For simplicity set the attributes of second half of a
5962 * double-wide character equal to the first half. */
5963 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005965
5966 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968 else
5969#endif
5970 screen_char(off_to, row, col + coloff);
5971 }
5972 else if ( p_wiv
5973#ifdef FEAT_GUI
5974 && !gui.in_use
5975#endif
5976 && col + coloff > 0)
5977 {
5978 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
5979 {
5980 /*
5981 * Don't output stop-highlight when moving the cursor, it will
5982 * stop the highlighting when it should continue.
5983 */
5984 screen_attr = 0;
5985 }
5986 else if (screen_attr != 0)
5987 screen_stop_highlight();
5988 }
5989
5990 off_to += CHAR_CELLS;
5991 off_from += CHAR_CELLS;
5992 col += CHAR_CELLS;
5993 }
5994
5995#ifdef FEAT_MBYTE
5996 if (clear_next)
5997 {
5998 /* Clear the second half of a double-wide character of which the left
5999 * half was overwritten with a single-wide character. */
6000 ScreenLines[off_to] = ' ';
6001 if (enc_utf8)
6002 ScreenLinesUC[off_to] = 0;
6003 screen_char(off_to, row, col + coloff);
6004 }
6005#endif
6006
6007 if (clear_width > 0
6008#ifdef FEAT_RIGHTLEFT
6009 && !rlflag
6010#endif
6011 )
6012 {
6013#ifdef FEAT_GUI
6014 int startCol = col;
6015#endif
6016
6017 /* blank out the rest of the line */
6018 while (col < clear_width && ScreenLines[off_to] == ' '
6019 && ScreenAttrs[off_to] == 0
6020#ifdef FEAT_MBYTE
6021 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6022#endif
6023 )
6024 {
6025 ++off_to;
6026 ++col;
6027 }
6028 if (col < clear_width)
6029 {
6030#ifdef FEAT_GUI
6031 /*
6032 * In the GUI, clearing the rest of the line may leave pixels
6033 * behind if the first character cleared was bold. Some bold
6034 * fonts spill over the left. In this case we redraw the previous
6035 * character too. If we didn't skip any blanks above, then we
6036 * only redraw if the character wasn't already redrawn anyway.
6037 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006038 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 {
6040 hl = ScreenAttrs[off_to];
6041 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006042 {
6043 int prev_cells = 1;
6044# ifdef FEAT_MBYTE
6045 if (enc_utf8)
6046 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6047 * that its width is 2. */
6048 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6049 else if (enc_dbcs != 0)
6050 {
6051 /* find previous character by counting from first
6052 * column and get its width. */
6053 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006054 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006055
6056 while (off < off_to)
6057 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006058 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006059 off += prev_cells;
6060 }
6061 }
6062
6063 if (enc_dbcs != 0 && prev_cells > 1)
6064 screen_char_2(off_to - prev_cells, row,
6065 col + coloff - prev_cells);
6066 else
6067# endif
6068 screen_char(off_to - prev_cells, row,
6069 col + coloff - prev_cells);
6070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 }
6072#endif
6073 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6074 ' ', ' ', 0);
6075#ifdef FEAT_VERTSPLIT
6076 off_to += clear_width - col;
6077 col = clear_width;
6078#endif
6079 }
6080 }
6081
6082 if (clear_width > 0)
6083 {
6084#ifdef FEAT_VERTSPLIT
6085 /* For a window that's left of another, draw the separator char. */
6086 if (col + coloff < Columns)
6087 {
6088 int c;
6089
6090 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006091 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006093 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6094 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095# endif
6096 || ScreenAttrs[off_to] != hl)
6097 {
6098 ScreenLines[off_to] = c;
6099 ScreenAttrs[off_to] = hl;
6100# ifdef FEAT_MBYTE
6101 if (enc_utf8)
6102 {
6103 if (c >= 0x80)
6104 {
6105 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006106 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 }
6108 else
6109 ScreenLinesUC[off_to] = 0;
6110 }
6111# endif
6112 screen_char(off_to, row, col + coloff);
6113 }
6114 }
6115 else
6116#endif
6117 LineWraps[row] = FALSE;
6118 }
6119}
6120
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006121#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006123 * Mirror text "str" for right-left displaying.
6124 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006126 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127rl_mirror(str)
6128 char_u *str;
6129{
6130 char_u *p1, *p2;
6131 int t;
6132
6133 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6134 {
6135 t = *p1;
6136 *p1 = *p2;
6137 *p2 = t;
6138 }
6139}
6140#endif
6141
6142#if defined(FEAT_WINDOWS) || defined(PROTO)
6143/*
6144 * mark all status lines for redraw; used after first :cd
6145 */
6146 void
6147status_redraw_all()
6148{
6149 win_T *wp;
6150
6151 for (wp = firstwin; wp; wp = wp->w_next)
6152 if (wp->w_status_height)
6153 {
6154 wp->w_redr_status = TRUE;
6155 redraw_later(VALID);
6156 }
6157}
6158
6159/*
6160 * mark all status lines of the current buffer for redraw
6161 */
6162 void
6163status_redraw_curbuf()
6164{
6165 win_T *wp;
6166
6167 for (wp = firstwin; wp; wp = wp->w_next)
6168 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6169 {
6170 wp->w_redr_status = TRUE;
6171 redraw_later(VALID);
6172 }
6173}
6174
6175/*
6176 * Redraw all status lines that need to be redrawn.
6177 */
6178 void
6179redraw_statuslines()
6180{
6181 win_T *wp;
6182
6183 for (wp = firstwin; wp; wp = wp->w_next)
6184 if (wp->w_redr_status)
6185 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006186 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006187 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188}
6189#endif
6190
6191#if (defined(FEAT_WILDMENU) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6192/*
6193 * Redraw all status lines at the bottom of frame "frp".
6194 */
6195 void
6196win_redraw_last_status(frp)
6197 frame_T *frp;
6198{
6199 if (frp->fr_layout == FR_LEAF)
6200 frp->fr_win->w_redr_status = TRUE;
6201 else if (frp->fr_layout == FR_ROW)
6202 {
6203 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6204 win_redraw_last_status(frp);
6205 }
6206 else /* frp->fr_layout == FR_COL */
6207 {
6208 frp = frp->fr_child;
6209 while (frp->fr_next != NULL)
6210 frp = frp->fr_next;
6211 win_redraw_last_status(frp);
6212 }
6213}
6214#endif
6215
6216#ifdef FEAT_VERTSPLIT
6217/*
6218 * Draw the verticap separator right of window "wp" starting with line "row".
6219 */
6220 static void
6221draw_vsep_win(wp, row)
6222 win_T *wp;
6223 int row;
6224{
6225 int hl;
6226 int c;
6227
6228 if (wp->w_vsep_width)
6229 {
6230 /* draw the vertical separator right of this window */
6231 c = fillchar_vsep(&hl);
6232 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6233 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6234 c, ' ', hl);
6235 }
6236}
6237#endif
6238
6239#ifdef FEAT_WILDMENU
6240static int status_match_len __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006241static int skip_status_match_char __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242
6243/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006244 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245 */
6246 static int
6247status_match_len(xp, s)
6248 expand_T *xp;
6249 char_u *s;
6250{
6251 int len = 0;
6252
6253#ifdef FEAT_MENU
6254 int emenu = (xp->xp_context == EXPAND_MENUS
6255 || xp->xp_context == EXPAND_MENUNAMES);
6256
6257 /* Check for menu separators - replace with '|'. */
6258 if (emenu && menu_is_separator(s))
6259 return 1;
6260#endif
6261
6262 while (*s != NUL)
6263 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006264 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006265 len += ptr2cells(s);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006266 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 }
6268
6269 return len;
6270}
6271
6272/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006273 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006274 * These are backslashes used for escaping. Do show backslashes in help tags.
6275 */
6276 static int
6277skip_status_match_char(xp, s)
6278 expand_T *xp;
6279 char_u *s;
6280{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006281 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006282#ifdef FEAT_MENU
6283 || ((xp->xp_context == EXPAND_MENUS
6284 || xp->xp_context == EXPAND_MENUNAMES)
6285 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6286#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006287 )
6288 {
6289#ifndef BACKSLASH_IN_FILENAME
6290 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6291 return 2;
6292#endif
6293 return 1;
6294 }
6295 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006296}
6297
6298/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 * Show wildchar matches in the status line.
6300 * Show at least the "match" item.
6301 * We start at item 'first_match' in the list and show all matches that fit.
6302 *
6303 * If inversion is possible we use it. Else '=' characters are used.
6304 */
6305 void
6306win_redr_status_matches(xp, num_matches, matches, match, showtail)
6307 expand_T *xp;
6308 int num_matches;
6309 char_u **matches; /* list of matches */
6310 int match;
6311 int showtail;
6312{
6313#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6314 int row;
6315 char_u *buf;
6316 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006317 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 int fillchar;
6319 int attr;
6320 int i;
6321 int highlight = TRUE;
6322 char_u *selstart = NULL;
6323 int selstart_col = 0;
6324 char_u *selend = NULL;
6325 static int first_match = 0;
6326 int add_left = FALSE;
6327 char_u *s;
6328#ifdef FEAT_MENU
6329 int emenu;
6330#endif
6331#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6332 int l;
6333#endif
6334
6335 if (matches == NULL) /* interrupted completion? */
6336 return;
6337
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006338#ifdef FEAT_MBYTE
6339 if (has_mbyte)
6340 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6341 else
6342#endif
6343 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 if (buf == NULL)
6345 return;
6346
6347 if (match == -1) /* don't show match but original text */
6348 {
6349 match = 0;
6350 highlight = FALSE;
6351 }
6352 /* count 1 for the ending ">" */
6353 clen = status_match_len(xp, L_MATCH(match)) + 3;
6354 if (match == 0)
6355 first_match = 0;
6356 else if (match < first_match)
6357 {
6358 /* jumping left, as far as we can go */
6359 first_match = match;
6360 add_left = TRUE;
6361 }
6362 else
6363 {
6364 /* check if match fits on the screen */
6365 for (i = first_match; i < match; ++i)
6366 clen += status_match_len(xp, L_MATCH(i)) + 2;
6367 if (first_match > 0)
6368 clen += 2;
6369 /* jumping right, put match at the left */
6370 if ((long)clen > Columns)
6371 {
6372 first_match = match;
6373 /* if showing the last match, we can add some on the left */
6374 clen = 2;
6375 for (i = match; i < num_matches; ++i)
6376 {
6377 clen += status_match_len(xp, L_MATCH(i)) + 2;
6378 if ((long)clen >= Columns)
6379 break;
6380 }
6381 if (i == num_matches)
6382 add_left = TRUE;
6383 }
6384 }
6385 if (add_left)
6386 while (first_match > 0)
6387 {
6388 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6389 if ((long)clen >= Columns)
6390 break;
6391 --first_match;
6392 }
6393
6394 fillchar = fillchar_status(&attr, TRUE);
6395
6396 if (first_match == 0)
6397 {
6398 *buf = NUL;
6399 len = 0;
6400 }
6401 else
6402 {
6403 STRCPY(buf, "< ");
6404 len = 2;
6405 }
6406 clen = len;
6407
6408 i = first_match;
6409 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6410 {
6411 if (i == match)
6412 {
6413 selstart = buf + len;
6414 selstart_col = clen;
6415 }
6416
6417 s = L_MATCH(i);
6418 /* Check for menu separators - replace with '|' */
6419#ifdef FEAT_MENU
6420 emenu = (xp->xp_context == EXPAND_MENUS
6421 || xp->xp_context == EXPAND_MENUNAMES);
6422 if (emenu && menu_is_separator(s))
6423 {
6424 STRCPY(buf + len, transchar('|'));
6425 l = (int)STRLEN(buf + len);
6426 len += l;
6427 clen += l;
6428 }
6429 else
6430#endif
6431 for ( ; *s != NUL; ++s)
6432 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006433 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 clen += ptr2cells(s);
6435#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006436 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 {
6438 STRNCPY(buf + len, s, l);
6439 s += l - 1;
6440 len += l;
6441 }
6442 else
6443#endif
6444 {
6445 STRCPY(buf + len, transchar_byte(*s));
6446 len += (int)STRLEN(buf + len);
6447 }
6448 }
6449 if (i == match)
6450 selend = buf + len;
6451
6452 *(buf + len++) = ' ';
6453 *(buf + len++) = ' ';
6454 clen += 2;
6455 if (++i == num_matches)
6456 break;
6457 }
6458
6459 if (i != num_matches)
6460 {
6461 *(buf + len++) = '>';
6462 ++clen;
6463 }
6464
6465 buf[len] = NUL;
6466
6467 row = cmdline_row - 1;
6468 if (row >= 0)
6469 {
6470 if (wild_menu_showing == 0)
6471 {
6472 if (msg_scrolled > 0)
6473 {
6474 /* Put the wildmenu just above the command line. If there is
6475 * no room, scroll the screen one line up. */
6476 if (cmdline_row == Rows - 1)
6477 {
6478 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
6479 ++msg_scrolled;
6480 }
6481 else
6482 {
6483 ++cmdline_row;
6484 ++row;
6485 }
6486 wild_menu_showing = WM_SCROLLED;
6487 }
6488 else
6489 {
6490 /* Create status line if needed by setting 'laststatus' to 2.
6491 * Set 'winminheight' to zero to avoid that the window is
6492 * resized. */
6493 if (lastwin->w_status_height == 0)
6494 {
6495 save_p_ls = p_ls;
6496 save_p_wmh = p_wmh;
6497 p_ls = 2;
6498 p_wmh = 0;
6499 last_status(FALSE);
6500 }
6501 wild_menu_showing = WM_SHOWN;
6502 }
6503 }
6504
6505 screen_puts(buf, row, 0, attr);
6506 if (selstart != NULL && highlight)
6507 {
6508 *selend = NUL;
6509 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
6510 }
6511
6512 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6513 }
6514
6515#ifdef FEAT_VERTSPLIT
6516 win_redraw_last_status(topframe);
6517#else
6518 lastwin->w_redr_status = TRUE;
6519#endif
6520 vim_free(buf);
6521}
6522#endif
6523
6524#if defined(FEAT_WINDOWS) || defined(PROTO)
6525/*
6526 * Redraw the status line of window wp.
6527 *
6528 * If inversion is possible we use it. Else '=' characters are used.
6529 */
6530 void
6531win_redr_status(wp)
6532 win_T *wp;
6533{
6534 int row;
6535 char_u *p;
6536 int len;
6537 int fillchar;
6538 int attr;
6539 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006540 static int busy = FALSE;
6541
6542 /* It's possible to get here recursively when 'statusline' (indirectly)
6543 * invokes ":redrawstatus". Simply ignore the call then. */
6544 if (busy)
6545 return;
6546 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547
6548 wp->w_redr_status = FALSE;
6549 if (wp->w_status_height == 0)
6550 {
6551 /* no status line, can only be last window */
6552 redraw_cmdline = TRUE;
6553 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006554 else if (!redrawing()
6555#ifdef FEAT_INS_EXPAND
6556 /* don't update status line when popup menu is visible and may be
6557 * drawn over it */
6558 || pum_visible()
6559#endif
6560 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561 {
6562 /* Don't redraw right now, do it later. */
6563 wp->w_redr_status = TRUE;
6564 }
6565#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006566 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567 {
6568 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006569 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570 }
6571#endif
6572 else
6573 {
6574 fillchar = fillchar_status(&attr, wp == curwin);
6575
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006576 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577 p = NameBuff;
6578 len = (int)STRLEN(p);
6579
6580 if (wp->w_buffer->b_help
6581#ifdef FEAT_QUICKFIX
6582 || wp->w_p_pvw
6583#endif
6584 || bufIsChanged(wp->w_buffer)
6585 || wp->w_buffer->b_p_ro)
6586 *(p + len++) = ' ';
6587 if (wp->w_buffer->b_help)
6588 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006589 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590 len += (int)STRLEN(p + len);
6591 }
6592#ifdef FEAT_QUICKFIX
6593 if (wp->w_p_pvw)
6594 {
6595 STRCPY(p + len, _("[Preview]"));
6596 len += (int)STRLEN(p + len);
6597 }
6598#endif
6599 if (bufIsChanged(wp->w_buffer))
6600 {
6601 STRCPY(p + len, "[+]");
6602 len += 3;
6603 }
6604 if (wp->w_buffer->b_p_ro)
6605 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006606 STRCPY(p + len, _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607 len += 4;
6608 }
6609
6610#ifndef FEAT_VERTSPLIT
6611 this_ru_col = ru_col;
6612 if (this_ru_col < (Columns + 1) / 2)
6613 this_ru_col = (Columns + 1) / 2;
6614#else
6615 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
6616 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
6617 this_ru_col = (W_WIDTH(wp) + 1) / 2;
6618 if (this_ru_col <= 1)
6619 {
6620 p = (char_u *)"<"; /* No room for file name! */
6621 len = 1;
6622 }
6623 else
6624#endif
6625#ifdef FEAT_MBYTE
6626 if (has_mbyte)
6627 {
6628 int clen = 0, i;
6629
6630 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006631 clen = mb_string2cells(p, -1);
6632
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633 /* Find first character that will fit.
6634 * Going from start to end is much faster for DBCS. */
6635 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006636 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637 clen -= (*mb_ptr2cells)(p + i);
6638 len = clen;
6639 if (i > 0)
6640 {
6641 p = p + i - 1;
6642 *p = '<';
6643 ++len;
6644 }
6645
6646 }
6647 else
6648#endif
6649 if (len > this_ru_col - 1)
6650 {
6651 p += len - (this_ru_col - 1);
6652 *p = '<';
6653 len = this_ru_col - 1;
6654 }
6655
6656 row = W_WINROW(wp) + wp->w_height;
6657 screen_puts(p, row, W_WINCOL(wp), attr);
6658 screen_fill(row, row + 1, len + W_WINCOL(wp),
6659 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
6660
6661 if (get_keymap_str(wp, NameBuff, MAXPATHL)
6662 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6663 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
6664 - 1 + W_WINCOL(wp)), attr);
6665
6666#ifdef FEAT_CMDL_INFO
6667 win_redr_ruler(wp, TRUE);
6668#endif
6669 }
6670
6671#ifdef FEAT_VERTSPLIT
6672 /*
6673 * May need to draw the character below the vertical separator.
6674 */
6675 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6676 {
6677 if (stl_connected(wp))
6678 fillchar = fillchar_status(&attr, wp == curwin);
6679 else
6680 fillchar = fillchar_vsep(&attr);
6681 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6682 attr);
6683 }
6684#endif
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006685 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686}
6687
Bram Moolenaar238a5642006-02-21 22:12:05 +00006688#ifdef FEAT_STL_OPT
6689/*
6690 * Redraw the status line according to 'statusline' and take care of any
6691 * errors encountered.
6692 */
6693 static void
Bram Moolenaar362f3562009-11-03 16:20:34 +00006694redraw_custom_statusline(wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006695 win_T *wp;
6696{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006697 static int entered = FALSE;
6698 int save_called_emsg = called_emsg;
6699
6700 /* When called recursively return. This can happen when the statusline
6701 * contains an expression that triggers a redraw. */
6702 if (entered)
6703 return;
6704 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006705
6706 called_emsg = FALSE;
6707 win_redr_custom(wp, FALSE);
6708 if (called_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006709 {
6710 /* When there is an error disable the statusline, otherwise the
6711 * display is messed up with errors and a redraw triggers the problem
6712 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00006713 set_string_option_direct((char_u *)"statusline", -1,
6714 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006715 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006716 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00006717 called_emsg |= save_called_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006718 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006719}
6720#endif
6721
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722# ifdef FEAT_VERTSPLIT
6723/*
6724 * Return TRUE if the status line of window "wp" is connected to the status
6725 * line of the window right of it. If not, then it's a vertical separator.
6726 * Only call if (wp->w_vsep_width != 0).
6727 */
6728 int
6729stl_connected(wp)
6730 win_T *wp;
6731{
6732 frame_T *fr;
6733
6734 fr = wp->w_frame;
6735 while (fr->fr_parent != NULL)
6736 {
6737 if (fr->fr_parent->fr_layout == FR_COL)
6738 {
6739 if (fr->fr_next != NULL)
6740 break;
6741 }
6742 else
6743 {
6744 if (fr->fr_next != NULL)
6745 return TRUE;
6746 }
6747 fr = fr->fr_parent;
6748 }
6749 return FALSE;
6750}
6751# endif
6752
6753#endif /* FEAT_WINDOWS */
6754
6755#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
6756/*
6757 * Get the value to show for the language mappings, active 'keymap'.
6758 */
6759 int
6760get_keymap_str(wp, buf, len)
6761 win_T *wp;
6762 char_u *buf; /* buffer for the result */
6763 int len; /* length of buffer */
6764{
6765 char_u *p;
6766
6767 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
6768 return FALSE;
6769
6770 {
6771#ifdef FEAT_EVAL
6772 buf_T *old_curbuf = curbuf;
6773 win_T *old_curwin = curwin;
6774 char_u *s;
6775
6776 curbuf = wp->w_buffer;
6777 curwin = wp;
6778 STRCPY(buf, "b:keymap_name"); /* must be writable */
6779 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006780 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781 --emsg_skip;
6782 curbuf = old_curbuf;
6783 curwin = old_curwin;
6784 if (p == NULL || *p == NUL)
6785#endif
6786 {
6787#ifdef FEAT_KEYMAP
6788 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
6789 p = wp->w_buffer->b_p_keymap;
6790 else
6791#endif
6792 p = (char_u *)"lang";
6793 }
6794 if ((int)(STRLEN(p) + 3) < len)
6795 sprintf((char *)buf, "<%s>", p);
6796 else
6797 buf[0] = NUL;
6798#ifdef FEAT_EVAL
6799 vim_free(s);
6800#endif
6801 }
6802 return buf[0] != NUL;
6803}
6804#endif
6805
6806#if defined(FEAT_STL_OPT) || defined(PROTO)
6807/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006808 * Redraw the status line or ruler of window "wp".
6809 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810 */
6811 static void
Bram Moolenaar9372a112005-12-06 19:59:18 +00006812win_redr_custom(wp, draw_ruler)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 win_T *wp;
Bram Moolenaar9372a112005-12-06 19:59:18 +00006814 int draw_ruler; /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815{
Bram Moolenaar1d633412013-12-11 15:52:01 +01006816 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817 int attr;
6818 int curattr;
6819 int row;
6820 int col = 0;
6821 int maxwidth;
6822 int width;
6823 int n;
6824 int len;
6825 int fillchar;
6826 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00006827 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006829 struct stl_hlrec hltab[STL_MAX_ITEM];
6830 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006831 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01006832 win_T *ewp;
6833 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834
Bram Moolenaar1d633412013-12-11 15:52:01 +01006835 /* There is a tiny chance that this gets called recursively: When
6836 * redrawing a status line triggers redrawing the ruler or tabline.
6837 * Avoid trouble by not allowing recursion. */
6838 if (entered)
6839 return;
6840 entered = TRUE;
6841
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006843 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006845 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006846 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006847 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00006848 fillchar = ' ';
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006849 attr = hl_attr(HLF_TPF);
6850 maxwidth = Columns;
6851# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006852 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006853# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006855 else
6856 {
6857 row = W_WINROW(wp) + wp->w_height;
6858 fillchar = fillchar_status(&attr, wp == curwin);
6859 maxwidth = W_WIDTH(wp);
6860
6861 if (draw_ruler)
6862 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006863 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006864 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006865 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006866 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006867 if (*++stl == '-')
6868 stl++;
6869 if (atoi((char *)stl))
6870 while (VIM_ISDIGIT(*stl))
6871 stl++;
6872 if (*stl++ != '(')
6873 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006874 }
6875#ifdef FEAT_VERTSPLIT
6876 col = ru_col - (Columns - W_WIDTH(wp));
6877 if (col < (W_WIDTH(wp) + 1) / 2)
6878 col = (W_WIDTH(wp) + 1) / 2;
6879#else
6880 col = ru_col;
6881 if (col > (Columns + 1) / 2)
6882 col = (Columns + 1) / 2;
6883#endif
6884 maxwidth = W_WIDTH(wp) - col;
6885#ifdef FEAT_WINDOWS
6886 if (!wp->w_status_height)
6887#endif
6888 {
6889 row = Rows - 1;
6890 --maxwidth; /* writing in last column may cause scrolling */
6891 fillchar = ' ';
6892 attr = 0;
6893 }
6894
6895# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006896 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006897# endif
6898 }
6899 else
6900 {
6901 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006902 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006903 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00006904 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006905# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006906 use_sandbox = was_set_insecurely((char_u *)"statusline",
6907 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006908# endif
6909 }
6910
6911#ifdef FEAT_VERTSPLIT
6912 col += W_WINCOL(wp);
6913#endif
6914 }
6915
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01006917 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918
Bram Moolenaar61452852011-02-01 18:01:11 +01006919 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
6920 * the cursor away and back. */
6921 ewp = wp == NULL ? curwin : wp;
6922 p_crb_save = ewp->w_p_crb;
6923 ewp->w_p_crb = FALSE;
6924
Bram Moolenaar362f3562009-11-03 16:20:34 +00006925 /* Make a copy, because the statusline may include a function call that
6926 * might change the option value and free the memory. */
6927 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01006928 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00006929 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006930 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006931 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01006932 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01006934 /* Make all characters printable. */
6935 p = transstr(buf);
6936 if (p != NULL)
6937 {
6938 vim_strncpy(buf, p, sizeof(buf) - 1);
6939 vim_free(p);
6940 }
6941
6942 /* fill up with "fillchar" */
6943 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00006944 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 {
6946#ifdef FEAT_MBYTE
6947 len += (*mb_char2bytes)(fillchar, buf + len);
6948#else
6949 buf[len++] = fillchar;
6950#endif
6951 ++width;
6952 }
6953 buf[len] = NUL;
6954
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006955 /*
6956 * Draw each snippet with the specified highlighting.
6957 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 curattr = attr;
6959 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006960 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006962 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 screen_puts_len(p, len, row, col, curattr);
6964 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006965 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006967 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006969 else if (hltab[n].userhl < 0)
6970 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00006972 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006973 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974#endif
6975 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006976 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 }
6978 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006979
6980 if (wp == NULL)
6981 {
6982 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
6983 col = 0;
6984 len = 0;
6985 p = buf;
6986 fillchar = 0;
6987 for (n = 0; tabtab[n].start != NULL; n++)
6988 {
6989 len += vim_strnsize(p, (int)(tabtab[n].start - p));
6990 while (col < len)
6991 TabPageIdxs[col++] = fillchar;
6992 p = tabtab[n].start;
6993 fillchar = tabtab[n].userhl;
6994 }
6995 while (col < Columns)
6996 TabPageIdxs[col++] = fillchar;
6997 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01006998
6999theend:
7000 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001}
7002
7003#endif /* FEAT_STL_OPT */
7004
7005/*
7006 * Output a single character directly to the screen and update ScreenLines.
7007 */
7008 void
7009screen_putchar(c, row, col, attr)
7010 int c;
7011 int row, col;
7012 int attr;
7013{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 char_u buf[MB_MAXBYTES + 1];
7015
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007016#ifdef FEAT_MBYTE
7017 if (has_mbyte)
7018 buf[(*mb_char2bytes)(c, buf)] = NUL;
7019 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007021 {
7022 buf[0] = c;
7023 buf[1] = NUL;
7024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 screen_puts(buf, row, col, attr);
7026}
7027
7028/*
7029 * Get a single character directly from ScreenLines into "bytes[]".
7030 * Also return its attribute in *attrp;
7031 */
7032 void
7033screen_getbytes(row, col, bytes, attrp)
7034 int row, col;
7035 char_u *bytes;
7036 int *attrp;
7037{
7038 unsigned off;
7039
7040 /* safety check */
7041 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7042 {
7043 off = LineOffset[row] + col;
7044 *attrp = ScreenAttrs[off];
7045 bytes[0] = ScreenLines[off];
7046 bytes[1] = NUL;
7047
7048#ifdef FEAT_MBYTE
7049 if (enc_utf8 && ScreenLinesUC[off] != 0)
7050 bytes[utfc_char2bytes(off, bytes)] = NUL;
7051 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7052 {
7053 bytes[0] = ScreenLines[off];
7054 bytes[1] = ScreenLines2[off];
7055 bytes[2] = NUL;
7056 }
7057 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7058 {
7059 bytes[1] = ScreenLines[off + 1];
7060 bytes[2] = NUL;
7061 }
7062#endif
7063 }
7064}
7065
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007066#ifdef FEAT_MBYTE
7067static int screen_comp_differs __ARGS((int, int*));
7068
7069/*
7070 * Return TRUE if composing characters for screen posn "off" differs from
7071 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007072 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007073 */
7074 static int
7075screen_comp_differs(off, u8cc)
7076 int off;
7077 int *u8cc;
7078{
7079 int i;
7080
7081 for (i = 0; i < Screen_mco; ++i)
7082 {
7083 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7084 return TRUE;
7085 if (u8cc[i] == 0)
7086 break;
7087 }
7088 return FALSE;
7089}
7090#endif
7091
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092/*
7093 * Put string '*text' on the screen at position 'row' and 'col', with
7094 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7095 * Note: only outputs within one row, message is truncated at screen boundary!
7096 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7097 */
7098 void
7099screen_puts(text, row, col, attr)
7100 char_u *text;
7101 int row;
7102 int col;
7103 int attr;
7104{
7105 screen_puts_len(text, -1, row, col, attr);
7106}
7107
7108/*
7109 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7110 * a NUL.
7111 */
7112 void
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007113screen_puts_len(text, textlen, row, col, attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 char_u *text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007115 int textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 int row;
7117 int col;
7118 int attr;
7119{
7120 unsigned off;
7121 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007122 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 int c;
7124#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007125 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126 int mbyte_blen = 1;
7127 int mbyte_cells = 1;
7128 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007129 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 int clear_next_cell = FALSE;
7131# ifdef FEAT_ARABIC
7132 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007133 int pc, nc, nc1;
7134 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135# endif
7136#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007137#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7138 int force_redraw_this;
7139 int force_redraw_next = FALSE;
7140#endif
7141 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142
7143 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7144 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007145 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146
Bram Moolenaarc236c162008-07-13 17:41:49 +00007147#ifdef FEAT_MBYTE
7148 /* When drawing over the right halve of a double-wide char clear out the
7149 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007150 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007151# ifdef FEAT_GUI
7152 && !gui.in_use
7153# endif
7154 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007155 {
7156 ScreenLines[off - 1] = ' ';
7157 ScreenAttrs[off - 1] = 0;
7158 if (enc_utf8)
7159 {
7160 ScreenLinesUC[off - 1] = 0;
7161 ScreenLinesC[0][off - 1] = 0;
7162 }
7163 /* redraw the previous cell, make it empty */
7164 screen_char(off - 1, row, col - 1);
7165 /* force the cell at "col" to be redrawn */
7166 force_redraw_next = TRUE;
7167 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007168#endif
7169
Bram Moolenaar367329b2007-08-30 11:53:22 +00007170#ifdef FEAT_MBYTE
7171 max_off = LineOffset[row] + screen_Columns;
7172#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007173 while (col < screen_Columns
7174 && (len < 0 || (int)(ptr - text) < len)
7175 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 {
7177 c = *ptr;
7178#ifdef FEAT_MBYTE
7179 /* check if this is the first byte of a multibyte */
7180 if (has_mbyte)
7181 {
7182 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007183 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007185 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7187 mbyte_cells = 1;
7188 else if (enc_dbcs != 0)
7189 mbyte_cells = mbyte_blen;
7190 else /* enc_utf8 */
7191 {
7192 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007193 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194 (int)((text + len) - ptr));
7195 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007196 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007198# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 /* Non-BMP character: display as ? or fullwidth ?. */
7200 if (u8c >= 0x10000)
7201 {
7202 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7203 if (attr == 0)
7204 attr = hl_attr(HLF_8);
7205 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007206# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207# ifdef FEAT_ARABIC
7208 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7209 {
7210 /* Do Arabic shaping. */
7211 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7212 {
7213 /* Past end of string to be displayed. */
7214 nc = NUL;
7215 nc1 = NUL;
7216 }
7217 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007218 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007219 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7220 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007221 nc1 = pcc[0];
7222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223 pc = prev_c;
7224 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007225 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 }
7227 else
7228 prev_c = u8c;
7229# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007230 if (col + mbyte_cells > screen_Columns)
7231 {
7232 /* Only 1 cell left, but character requires 2 cells:
7233 * display a '>' in the last column to avoid wrapping. */
7234 c = '>';
7235 mbyte_cells = 1;
7236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 }
7238 }
7239#endif
7240
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007241#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7242 force_redraw_this = force_redraw_next;
7243 force_redraw_next = FALSE;
7244#endif
7245
7246 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247#ifdef FEAT_MBYTE
7248 || (mbyte_cells == 2
7249 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7250 || (enc_dbcs == DBCS_JPNU
7251 && c == 0x8e
7252 && ScreenLines2[off] != ptr[1])
7253 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007254 && (ScreenLinesUC[off] !=
7255 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7256 || (ScreenLinesUC[off] != 0
7257 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258#endif
7259 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007260 || exmode_active;
7261
7262 if (need_redraw
7263#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7264 || force_redraw_this
7265#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 )
7267 {
7268#if defined(FEAT_GUI) || defined(UNIX)
7269 /* The bold trick makes a single row of pixels appear in the next
7270 * character. When a bold character is removed, the next
7271 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007272 * and for some xterms. */
7273 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274# ifdef FEAT_GUI
7275 gui.in_use
7276# endif
7277# if defined(FEAT_GUI) && defined(UNIX)
7278 ||
7279# endif
7280# ifdef UNIX
7281 term_is_xterm
7282# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007283 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007285 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007287 if (n > HL_ALL)
7288 n = syn_attr2attr(n);
7289 if (n & HL_BOLD)
7290 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291 }
7292#endif
7293#ifdef FEAT_MBYTE
7294 /* When at the end of the text and overwriting a two-cell
7295 * character with a one-cell character, need to clear the next
7296 * cell. Also when overwriting the left halve of a two-cell char
7297 * with the right halve of a two-cell char. Do this only once
7298 * (mb_off2cells() may return 2 on the right halve). */
7299 if (clear_next_cell)
7300 clear_next_cell = FALSE;
7301 else if (has_mbyte
7302 && (len < 0 ? ptr[mbyte_blen] == NUL
7303 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007304 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007306 && (*mb_off2cells)(off, max_off) == 1
7307 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 clear_next_cell = TRUE;
7309
7310 /* Make sure we never leave a second byte of a double-byte behind,
7311 * it confuses mb_off2cells(). */
7312 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007313 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007315 && (*mb_off2cells)(off, max_off) == 1
7316 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317 ScreenLines[off + mbyte_blen] = 0;
7318#endif
7319 ScreenLines[off] = c;
7320 ScreenAttrs[off] = attr;
7321#ifdef FEAT_MBYTE
7322 if (enc_utf8)
7323 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007324 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325 ScreenLinesUC[off] = 0;
7326 else
7327 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007328 int i;
7329
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007331 for (i = 0; i < Screen_mco; ++i)
7332 {
7333 ScreenLinesC[i][off] = u8cc[i];
7334 if (u8cc[i] == 0)
7335 break;
7336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337 }
7338 if (mbyte_cells == 2)
7339 {
7340 ScreenLines[off + 1] = 0;
7341 ScreenAttrs[off + 1] = attr;
7342 }
7343 screen_char(off, row, col);
7344 }
7345 else if (mbyte_cells == 2)
7346 {
7347 ScreenLines[off + 1] = ptr[1];
7348 ScreenAttrs[off + 1] = attr;
7349 screen_char_2(off, row, col);
7350 }
7351 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7352 {
7353 ScreenLines2[off] = ptr[1];
7354 screen_char(off, row, col);
7355 }
7356 else
7357#endif
7358 screen_char(off, row, col);
7359 }
7360#ifdef FEAT_MBYTE
7361 if (has_mbyte)
7362 {
7363 off += mbyte_cells;
7364 col += mbyte_cells;
7365 ptr += mbyte_blen;
7366 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007367 {
7368 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007370 len = -1;
7371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 }
7373 else
7374#endif
7375 {
7376 ++off;
7377 ++col;
7378 ++ptr;
7379 }
7380 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007381
7382#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7383 /* If we detected the next character needs to be redrawn, but the text
7384 * doesn't extend up to there, update the character here. */
7385 if (force_redraw_next && col < screen_Columns)
7386 {
7387# ifdef FEAT_MBYTE
7388 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7389 screen_char_2(off, row, col);
7390 else
7391# endif
7392 screen_char(off, row, col);
7393 }
7394#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395}
7396
7397#ifdef FEAT_SEARCH_EXTRA
7398/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007399 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400 */
7401 static void
7402start_search_hl()
7403{
7404 if (p_hls && !no_hlsearch)
7405 {
7406 last_pat_prog(&search_hl.rm);
7407 search_hl.attr = hl_attr(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007408# ifdef FEAT_RELTIME
7409 /* Set the time limit to 'redrawtime'. */
7410 profile_setlimit(p_rdt, &search_hl.tm);
7411# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412 }
7413}
7414
7415/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007416 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 */
7418 static void
7419end_search_hl()
7420{
7421 if (search_hl.rm.regprog != NULL)
7422 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007423 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 search_hl.rm.regprog = NULL;
7425 }
7426}
7427
7428/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007429 * Init for calling prepare_search_hl().
7430 */
7431 static void
7432init_search_hl(wp)
7433 win_T *wp;
7434{
7435 matchitem_T *cur;
7436
7437 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7438 * match */
7439 cur = wp->w_match_head;
7440 while (cur != NULL)
7441 {
7442 cur->hl.rm = cur->match;
7443 if (cur->hlg_id == 0)
7444 cur->hl.attr = 0;
7445 else
7446 cur->hl.attr = syn_id2attr(cur->hlg_id);
7447 cur->hl.buf = wp->w_buffer;
7448 cur->hl.lnum = 0;
7449 cur->hl.first_lnum = 0;
7450# ifdef FEAT_RELTIME
7451 /* Set the time limit to 'redrawtime'. */
7452 profile_setlimit(p_rdt, &(cur->hl.tm));
7453# endif
7454 cur = cur->next;
7455 }
7456 search_hl.buf = wp->w_buffer;
7457 search_hl.lnum = 0;
7458 search_hl.first_lnum = 0;
7459 /* time limit is set at the toplevel, for all windows */
7460}
7461
7462/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 * Advance to the match in window "wp" line "lnum" or past it.
7464 */
7465 static void
7466prepare_search_hl(wp, lnum)
7467 win_T *wp;
7468 linenr_T lnum;
7469{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007470 matchitem_T *cur; /* points to the match list */
7471 match_T *shl; /* points to search_hl or a match */
7472 int shl_flag; /* flag to indicate whether search_hl
7473 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007474 int pos_inprogress; /* marks that position match search is
7475 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476 int n;
7477
7478 /*
7479 * When using a multi-line pattern, start searching at the top
7480 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007481 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007483 cur = wp->w_match_head;
7484 shl_flag = FALSE;
7485 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007487 if (shl_flag == FALSE)
7488 {
7489 shl = &search_hl;
7490 shl_flag = TRUE;
7491 }
7492 else
7493 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494 if (shl->rm.regprog != NULL
7495 && shl->lnum == 0
7496 && re_multiline(shl->rm.regprog))
7497 {
7498 if (shl->first_lnum == 0)
7499 {
7500# ifdef FEAT_FOLDING
7501 for (shl->first_lnum = lnum;
7502 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7503 if (hasFoldingWin(wp, shl->first_lnum - 1,
7504 NULL, NULL, TRUE, NULL))
7505 break;
7506# else
7507 shl->first_lnum = wp->w_topline;
7508# endif
7509 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007510 if (cur != NULL)
7511 cur->pos.cur = 0;
7512 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007514 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7515 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007517 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n, cur);
7518 pos_inprogress = cur == NULL || cur->pos.cur == 0
7519 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520 if (shl->lnum != 0)
7521 {
7522 shl->first_lnum = shl->lnum
7523 + shl->rm.endpos[0].lnum
7524 - shl->rm.startpos[0].lnum;
7525 n = shl->rm.endpos[0].col;
7526 }
7527 else
7528 {
7529 ++shl->first_lnum;
7530 n = 0;
7531 }
7532 }
7533 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007534 if (shl != &search_hl && cur != NULL)
7535 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536 }
7537}
7538
7539/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007540 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541 * Uses shl->buf.
7542 * Sets shl->lnum and shl->rm contents.
7543 * Note: Assumes a previous match is always before "lnum", unless
7544 * shl->lnum is zero.
7545 * Careful: Any pointers for buffer lines will become invalid.
7546 */
7547 static void
Bram Moolenaarb3414592014-06-17 17:48:32 +02007548next_search_hl(win, shl, lnum, mincol, cur)
7549 win_T *win;
7550 match_T *shl; /* points to search_hl or a match */
7551 linenr_T lnum;
7552 colnr_T mincol; /* minimal column for a match */
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007553 matchitem_T *cur; /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554{
7555 linenr_T l;
7556 colnr_T matchcol;
7557 long nmatched;
7558
7559 if (shl->lnum != 0)
7560 {
7561 /* Check for three situations:
7562 * 1. If the "lnum" is below a previous match, start a new search.
7563 * 2. If the previous match includes "mincol", use it.
7564 * 3. Continue after the previous match.
7565 */
7566 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7567 if (lnum > l)
7568 shl->lnum = 0;
7569 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7570 return;
7571 }
7572
7573 /*
7574 * Repeat searching for a match until one is found that includes "mincol"
7575 * or none is found in this line.
7576 */
7577 called_emsg = FALSE;
7578 for (;;)
7579 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007580#ifdef FEAT_RELTIME
7581 /* Stop searching after passing the time limit. */
7582 if (profile_passed_limit(&(shl->tm)))
7583 {
7584 shl->lnum = 0; /* no match found in time */
7585 break;
7586 }
7587#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 /* Three situations:
7589 * 1. No useful previous match: search from start of line.
7590 * 2. Not Vi compatible or empty match: continue at next character.
7591 * Break the loop if this is beyond the end of the line.
7592 * 3. Vi compatible searching: continue at end of previous match.
7593 */
7594 if (shl->lnum == 0)
7595 matchcol = 0;
7596 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7597 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007598 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007600 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007601
7602 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007603 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007604 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007606 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607 shl->lnum = 0;
7608 break;
7609 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007610#ifdef FEAT_MBYTE
7611 if (has_mbyte)
7612 matchcol += mb_ptr2len(ml);
7613 else
7614#endif
7615 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616 }
7617 else
7618 matchcol = shl->rm.endpos[0].col;
7619
7620 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007621 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007623 /* Remember whether shl->rm is using a copy of the regprog in
7624 * cur->match. */
7625 int regprog_is_copy = (shl != &search_hl && cur != NULL
7626 && shl == &cur->hl
7627 && cur->match.regprog == cur->hl.rm.regprog);
7628
Bram Moolenaarb3414592014-06-17 17:48:32 +02007629 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7630 matchcol,
7631#ifdef FEAT_RELTIME
7632 &(shl->tm)
7633#else
7634 NULL
7635#endif
7636 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007637 /* Copy the regprog, in case it got freed and recompiled. */
7638 if (regprog_is_copy)
7639 cur->match.regprog = cur->hl.rm.regprog;
7640
Bram Moolenaarb3414592014-06-17 17:48:32 +02007641 if (called_emsg || got_int)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007642 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007643 /* Error while handling regexp: stop using this regexp. */
7644 if (shl == &search_hl)
7645 {
7646 /* don't free regprog in the match list, it's a copy */
7647 vim_regfree(shl->rm.regprog);
7648 SET_NO_HLSEARCH(TRUE);
7649 }
7650 shl->rm.regprog = NULL;
7651 shl->lnum = 0;
7652 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
7653 message */
7654 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007655 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007656 }
7657 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007658 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007659 else
7660 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661 if (nmatched == 0)
7662 {
7663 shl->lnum = 0; /* no match found */
7664 break;
7665 }
7666 if (shl->rm.startpos[0].lnum > 0
7667 || shl->rm.startpos[0].col >= mincol
7668 || nmatched > 1
7669 || shl->rm.endpos[0].col > mincol)
7670 {
7671 shl->lnum += shl->rm.startpos[0].lnum;
7672 break; /* useful match found */
7673 }
7674 }
7675}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676
Bram Moolenaarb3414592014-06-17 17:48:32 +02007677 static int
7678next_search_hl_pos(shl, lnum, posmatch, mincol)
7679 match_T *shl; /* points to a match */
7680 linenr_T lnum;
7681 posmatch_T *posmatch; /* match positions */
7682 colnr_T mincol; /* minimal column for a match */
7683{
7684 int i;
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02007685 int bot = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007686
7687 shl->lnum = 0;
7688 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
7689 {
7690 if (posmatch->pos[i].lnum == 0)
7691 break;
7692 if (posmatch->pos[i].col < mincol)
7693 continue;
7694 if (posmatch->pos[i].lnum == lnum)
7695 {
7696 if (shl->lnum == lnum)
7697 {
7698 /* partially sort positions by column numbers
7699 * on the same line */
7700 if (posmatch->pos[i].col < posmatch->pos[bot].col)
7701 {
7702 llpos_T tmp = posmatch->pos[i];
7703
7704 posmatch->pos[i] = posmatch->pos[bot];
7705 posmatch->pos[bot] = tmp;
7706 }
7707 }
7708 else
7709 {
7710 bot = i;
7711 shl->lnum = lnum;
7712 }
7713 }
7714 }
7715 posmatch->cur = 0;
7716 if (shl->lnum == lnum)
7717 {
7718 colnr_T start = posmatch->pos[bot].col == 0
7719 ? 0 : posmatch->pos[bot].col - 1;
7720 colnr_T end = posmatch->pos[bot].col == 0
7721 ? MAXCOL : start + posmatch->pos[bot].len;
7722
7723 shl->rm.startpos[0].lnum = 0;
7724 shl->rm.startpos[0].col = start;
7725 shl->rm.endpos[0].lnum = 0;
7726 shl->rm.endpos[0].col = end;
7727 posmatch->cur = bot + 1;
7728 return TRUE;
7729 }
7730 return FALSE;
7731}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02007732#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02007733
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734 static void
7735screen_start_highlight(attr)
7736 int attr;
7737{
7738 attrentry_T *aep = NULL;
7739
7740 screen_attr = attr;
7741 if (full_screen
7742#ifdef WIN3264
7743 && termcap_active
7744#endif
7745 )
7746 {
7747#ifdef FEAT_GUI
7748 if (gui.in_use)
7749 {
7750 char buf[20];
7751
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007752 /* The GUI handles this internally. */
7753 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754 OUT_STR(buf);
7755 }
7756 else
7757#endif
7758 {
7759 if (attr > HL_ALL) /* special HL attr. */
7760 {
7761 if (t_colors > 1)
7762 aep = syn_cterm_attr2entry(attr);
7763 else
7764 aep = syn_term_attr2entry(attr);
7765 if (aep == NULL) /* did ":syntax clear" */
7766 attr = 0;
7767 else
7768 attr = aep->ae_attr;
7769 }
7770 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
7771 out_str(T_MD);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007772 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
7773 && cterm_normal_fg_bold)
7774 /* If the Normal FG color has BOLD attribute and the new HL
7775 * has a FG color defined, clear BOLD. */
7776 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
7778 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007779 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
7780 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781 out_str(T_US);
7782 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
7783 out_str(T_CZH);
7784 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
7785 out_str(T_MR);
7786
7787 /*
7788 * Output the color or start string after bold etc., in case the
7789 * bold etc. override the color setting.
7790 */
7791 if (aep != NULL)
7792 {
7793 if (t_colors > 1)
7794 {
7795 if (aep->ae_u.cterm.fg_color)
7796 term_fg_color(aep->ae_u.cterm.fg_color - 1);
7797 if (aep->ae_u.cterm.bg_color)
7798 term_bg_color(aep->ae_u.cterm.bg_color - 1);
7799 }
7800 else
7801 {
7802 if (aep->ae_u.term.start != NULL)
7803 out_str(aep->ae_u.term.start);
7804 }
7805 }
7806 }
7807 }
7808}
7809
7810 void
7811screen_stop_highlight()
7812{
7813 int do_ME = FALSE; /* output T_ME code */
7814
7815 if (screen_attr != 0
7816#ifdef WIN3264
7817 && termcap_active
7818#endif
7819 )
7820 {
7821#ifdef FEAT_GUI
7822 if (gui.in_use)
7823 {
7824 char buf[20];
7825
7826 /* use internal GUI code */
7827 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
7828 OUT_STR(buf);
7829 }
7830 else
7831#endif
7832 {
7833 if (screen_attr > HL_ALL) /* special HL attr. */
7834 {
7835 attrentry_T *aep;
7836
7837 if (t_colors > 1)
7838 {
7839 /*
7840 * Assume that t_me restores the original colors!
7841 */
7842 aep = syn_cterm_attr2entry(screen_attr);
7843 if (aep != NULL && (aep->ae_u.cterm.fg_color
7844 || aep->ae_u.cterm.bg_color))
7845 do_ME = TRUE;
7846 }
7847 else
7848 {
7849 aep = syn_term_attr2entry(screen_attr);
7850 if (aep != NULL && aep->ae_u.term.stop != NULL)
7851 {
7852 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
7853 do_ME = TRUE;
7854 else
7855 out_str(aep->ae_u.term.stop);
7856 }
7857 }
7858 if (aep == NULL) /* did ":syntax clear" */
7859 screen_attr = 0;
7860 else
7861 screen_attr = aep->ae_attr;
7862 }
7863
7864 /*
7865 * Often all ending-codes are equal to T_ME. Avoid outputting the
7866 * same sequence several times.
7867 */
7868 if (screen_attr & HL_STANDOUT)
7869 {
7870 if (STRCMP(T_SE, T_ME) == 0)
7871 do_ME = TRUE;
7872 else
7873 out_str(T_SE);
7874 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007875 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876 {
7877 if (STRCMP(T_UE, T_ME) == 0)
7878 do_ME = TRUE;
7879 else
7880 out_str(T_UE);
7881 }
7882 if (screen_attr & HL_ITALIC)
7883 {
7884 if (STRCMP(T_CZR, T_ME) == 0)
7885 do_ME = TRUE;
7886 else
7887 out_str(T_CZR);
7888 }
7889 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
7890 out_str(T_ME);
7891
7892 if (t_colors > 1)
7893 {
7894 /* set Normal cterm colors */
7895 if (cterm_normal_fg_color != 0)
7896 term_fg_color(cterm_normal_fg_color - 1);
7897 if (cterm_normal_bg_color != 0)
7898 term_bg_color(cterm_normal_bg_color - 1);
7899 if (cterm_normal_fg_bold)
7900 out_str(T_MD);
7901 }
7902 }
7903 }
7904 screen_attr = 0;
7905}
7906
7907/*
7908 * Reset the colors for a cterm. Used when leaving Vim.
7909 * The machine specific code may override this again.
7910 */
7911 void
7912reset_cterm_colors()
7913{
7914 if (t_colors > 1)
7915 {
7916 /* set Normal cterm colors */
7917 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
7918 {
7919 out_str(T_OP);
7920 screen_attr = -1;
7921 }
7922 if (cterm_normal_fg_bold)
7923 {
7924 out_str(T_ME);
7925 screen_attr = -1;
7926 }
7927 }
7928}
7929
7930/*
7931 * Put character ScreenLines["off"] on the screen at position "row" and "col",
7932 * using the attributes from ScreenAttrs["off"].
7933 */
7934 static void
7935screen_char(off, row, col)
7936 unsigned off;
7937 int row;
7938 int col;
7939{
7940 int attr;
7941
7942 /* Check for illegal values, just in case (could happen just after
7943 * resizing). */
7944 if (row >= screen_Rows || col >= screen_Columns)
7945 return;
7946
7947 /* Outputting the last character on the screen may scrollup the screen.
7948 * Don't to it! Mark the character invalid (update it when scrolled up) */
7949 if (row == screen_Rows - 1 && col == screen_Columns - 1
7950#ifdef FEAT_RIGHTLEFT
7951 /* account for first command-line character in rightleft mode */
7952 && !cmdmsg_rl
7953#endif
7954 )
7955 {
7956 ScreenAttrs[off] = (sattr_T)-1;
7957 return;
7958 }
7959
7960 /*
7961 * Stop highlighting first, so it's easier to move the cursor.
7962 */
7963#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
7964 if (screen_char_attr != 0)
7965 attr = screen_char_attr;
7966 else
7967#endif
7968 attr = ScreenAttrs[off];
7969 if (screen_attr != attr)
7970 screen_stop_highlight();
7971
7972 windgoto(row, col);
7973
7974 if (screen_attr != attr)
7975 screen_start_highlight(attr);
7976
7977#ifdef FEAT_MBYTE
7978 if (enc_utf8 && ScreenLinesUC[off] != 0)
7979 {
7980 char_u buf[MB_MAXBYTES + 1];
7981
7982 /* Convert UTF-8 character to bytes and write it. */
7983
7984 buf[utfc_char2bytes(off, buf)] = NUL;
7985
7986 out_str(buf);
7987 if (utf_char2cells(ScreenLinesUC[off]) > 1)
7988 ++screen_cur_col;
7989 }
7990 else
7991#endif
7992 {
7993#ifdef FEAT_MBYTE
7994 out_flush_check();
7995#endif
7996 out_char(ScreenLines[off]);
7997#ifdef FEAT_MBYTE
7998 /* double-byte character in single-width cell */
7999 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8000 out_char(ScreenLines2[off]);
8001#endif
8002 }
8003
8004 screen_cur_col++;
8005}
8006
8007#ifdef FEAT_MBYTE
8008
8009/*
8010 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8011 * on the screen at position 'row' and 'col'.
8012 * The attributes of the first byte is used for all. This is required to
8013 * output the two bytes of a double-byte character with nothing in between.
8014 */
8015 static void
8016screen_char_2(off, row, col)
8017 unsigned off;
8018 int row;
8019 int col;
8020{
8021 /* Check for illegal values (could be wrong when screen was resized). */
8022 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8023 return;
8024
8025 /* Outputting the last character on the screen may scrollup the screen.
8026 * Don't to it! Mark the character invalid (update it when scrolled up) */
8027 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8028 {
8029 ScreenAttrs[off] = (sattr_T)-1;
8030 return;
8031 }
8032
8033 /* Output the first byte normally (positions the cursor), then write the
8034 * second byte directly. */
8035 screen_char(off, row, col);
8036 out_char(ScreenLines[off + 1]);
8037 ++screen_cur_col;
8038}
8039#endif
8040
8041#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
8042/*
8043 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8044 * This uses the contents of ScreenLines[] and doesn't change it.
8045 */
8046 void
8047screen_draw_rectangle(row, col, height, width, invert)
8048 int row;
8049 int col;
8050 int height;
8051 int width;
8052 int invert;
8053{
8054 int r, c;
8055 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008056#ifdef FEAT_MBYTE
8057 int max_off;
8058#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008060 /* Can't use ScreenLines unless initialized */
8061 if (ScreenLines == NULL)
8062 return;
8063
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064 if (invert)
8065 screen_char_attr = HL_INVERSE;
8066 for (r = row; r < row + height; ++r)
8067 {
8068 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008069#ifdef FEAT_MBYTE
8070 max_off = off + screen_Columns;
8071#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072 for (c = col; c < col + width; ++c)
8073 {
8074#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008075 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076 {
8077 screen_char_2(off + c, r, c);
8078 ++c;
8079 }
8080 else
8081#endif
8082 {
8083 screen_char(off + c, r, c);
8084#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008085 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 ++c;
8087#endif
8088 }
8089 }
8090 }
8091 screen_char_attr = 0;
8092}
8093#endif
8094
8095#ifdef FEAT_VERTSPLIT
8096/*
8097 * Redraw the characters for a vertically split window.
8098 */
8099 static void
8100redraw_block(row, end, wp)
8101 int row;
8102 int end;
8103 win_T *wp;
8104{
8105 int col;
8106 int width;
8107
8108# ifdef FEAT_CLIPBOARD
8109 clip_may_clear_selection(row, end - 1);
8110# endif
8111
8112 if (wp == NULL)
8113 {
8114 col = 0;
8115 width = Columns;
8116 }
8117 else
8118 {
8119 col = wp->w_wincol;
8120 width = wp->w_width;
8121 }
8122 screen_draw_rectangle(row, col, end - row, width, FALSE);
8123}
8124#endif
8125
8126/*
8127 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8128 * with character 'c1' in first column followed by 'c2' in the other columns.
8129 * Use attributes 'attr'.
8130 */
8131 void
8132screen_fill(start_row, end_row, start_col, end_col, c1, c2, attr)
8133 int start_row, end_row;
8134 int start_col, end_col;
8135 int c1, c2;
8136 int attr;
8137{
8138 int row;
8139 int col;
8140 int off;
8141 int end_off;
8142 int did_delete;
8143 int c;
8144 int norm_term;
8145#if defined(FEAT_GUI) || defined(UNIX)
8146 int force_next = FALSE;
8147#endif
8148
8149 if (end_row > screen_Rows) /* safety check */
8150 end_row = screen_Rows;
8151 if (end_col > screen_Columns) /* safety check */
8152 end_col = screen_Columns;
8153 if (ScreenLines == NULL
8154 || start_row >= end_row
8155 || start_col >= end_col) /* nothing to do */
8156 return;
8157
8158 /* it's a "normal" terminal when not in a GUI or cterm */
8159 norm_term = (
8160#ifdef FEAT_GUI
8161 !gui.in_use &&
8162#endif
8163 t_colors <= 1);
8164 for (row = start_row; row < end_row; ++row)
8165 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008166#ifdef FEAT_MBYTE
8167 if (has_mbyte
8168# ifdef FEAT_GUI
8169 && !gui.in_use
8170# endif
8171 )
8172 {
8173 /* When drawing over the right halve of a double-wide char clear
8174 * out the left halve. When drawing over the left halve of a
8175 * double wide-char clear out the right halve. Only needed in a
8176 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008177 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008178 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008179 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008180 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008181 }
8182#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183 /*
8184 * Try to use delete-line termcap code, when no attributes or in a
8185 * "normal" terminal, where a bold/italic space is just a
8186 * space.
8187 */
8188 did_delete = FALSE;
8189 if (c2 == ' '
8190 && end_col == Columns
8191 && can_clear(T_CE)
8192 && (attr == 0
8193 || (norm_term
8194 && attr <= HL_ALL
8195 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8196 {
8197 /*
8198 * check if we really need to clear something
8199 */
8200 col = start_col;
8201 if (c1 != ' ') /* don't clear first char */
8202 ++col;
8203
8204 off = LineOffset[row] + col;
8205 end_off = LineOffset[row] + end_col;
8206
8207 /* skip blanks (used often, keep it fast!) */
8208#ifdef FEAT_MBYTE
8209 if (enc_utf8)
8210 while (off < end_off && ScreenLines[off] == ' '
8211 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8212 ++off;
8213 else
8214#endif
8215 while (off < end_off && ScreenLines[off] == ' '
8216 && ScreenAttrs[off] == 0)
8217 ++off;
8218 if (off < end_off) /* something to be cleared */
8219 {
8220 col = off - LineOffset[row];
8221 screen_stop_highlight();
8222 term_windgoto(row, col);/* clear rest of this screen line */
8223 out_str(T_CE);
8224 screen_start(); /* don't know where cursor is now */
8225 col = end_col - col;
8226 while (col--) /* clear chars in ScreenLines */
8227 {
8228 ScreenLines[off] = ' ';
8229#ifdef FEAT_MBYTE
8230 if (enc_utf8)
8231 ScreenLinesUC[off] = 0;
8232#endif
8233 ScreenAttrs[off] = 0;
8234 ++off;
8235 }
8236 }
8237 did_delete = TRUE; /* the chars are cleared now */
8238 }
8239
8240 off = LineOffset[row] + start_col;
8241 c = c1;
8242 for (col = start_col; col < end_col; ++col)
8243 {
8244 if (ScreenLines[off] != c
8245#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008246 || (enc_utf8 && (int)ScreenLinesUC[off]
8247 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248#endif
8249 || ScreenAttrs[off] != attr
8250#if defined(FEAT_GUI) || defined(UNIX)
8251 || force_next
8252#endif
8253 )
8254 {
8255#if defined(FEAT_GUI) || defined(UNIX)
8256 /* The bold trick may make a single row of pixels appear in
8257 * the next character. When a bold character is removed, the
8258 * next character should be redrawn too. This happens for our
8259 * own GUI and for some xterms. */
8260 if (
8261# ifdef FEAT_GUI
8262 gui.in_use
8263# endif
8264# if defined(FEAT_GUI) && defined(UNIX)
8265 ||
8266# endif
8267# ifdef UNIX
8268 term_is_xterm
8269# endif
8270 )
8271 {
8272 if (ScreenLines[off] != ' '
8273 && (ScreenAttrs[off] > HL_ALL
8274 || ScreenAttrs[off] & HL_BOLD))
8275 force_next = TRUE;
8276 else
8277 force_next = FALSE;
8278 }
8279#endif
8280 ScreenLines[off] = c;
8281#ifdef FEAT_MBYTE
8282 if (enc_utf8)
8283 {
8284 if (c >= 0x80)
8285 {
8286 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008287 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288 }
8289 else
8290 ScreenLinesUC[off] = 0;
8291 }
8292#endif
8293 ScreenAttrs[off] = attr;
8294 if (!did_delete || c != ' ')
8295 screen_char(off, row, col);
8296 }
8297 ++off;
8298 if (col == start_col)
8299 {
8300 if (did_delete)
8301 break;
8302 c = c2;
8303 }
8304 }
8305 if (end_col == Columns)
8306 LineWraps[row] = FALSE;
8307 if (row == Rows - 1) /* overwritten the command line */
8308 {
8309 redraw_cmdline = TRUE;
8310 if (c1 == ' ' && c2 == ' ')
8311 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008312 if (start_col == 0)
8313 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314 }
8315 }
8316}
8317
8318/*
8319 * Check if there should be a delay. Used before clearing or redrawing the
8320 * screen or the command line.
8321 */
8322 void
8323check_for_delay(check_msg_scroll)
8324 int check_msg_scroll;
8325{
8326 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8327 && !did_wait_return
8328 && emsg_silent == 0)
8329 {
8330 out_flush();
8331 ui_delay(1000L, TRUE);
8332 emsg_on_display = FALSE;
8333 if (check_msg_scroll)
8334 msg_scroll = FALSE;
8335 }
8336}
8337
8338/*
8339 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008340 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341 * Returns TRUE if there is a valid screen to write to.
8342 * Returns FALSE when starting up and screen not initialized yet.
8343 */
8344 int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008345screen_valid(doclear)
8346 int doclear;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008348 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349 return (ScreenLines != NULL);
8350}
8351
8352/*
8353 * Resize the shell to Rows and Columns.
8354 * Allocate ScreenLines[] and associated items.
8355 *
8356 * There may be some time between setting Rows and Columns and (re)allocating
8357 * ScreenLines[]. This happens when starting up and when (manually) changing
8358 * the shell size. Always use screen_Rows and screen_Columns to access items
8359 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8360 * final size of the shell is needed.
8361 */
8362 void
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008363screenalloc(doclear)
8364 int doclear;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365{
8366 int new_row, old_row;
8367#ifdef FEAT_GUI
8368 int old_Rows;
8369#endif
8370 win_T *wp;
8371 int outofmem = FALSE;
8372 int len;
8373 schar_T *new_ScreenLines;
8374#ifdef FEAT_MBYTE
8375 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008376 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008378 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379#endif
8380 sattr_T *new_ScreenAttrs;
8381 unsigned *new_LineOffset;
8382 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008383#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008384 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008385 tabpage_T *tp;
8386#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008388 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008389#ifdef FEAT_AUTOCMD
8390 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008392retry:
8393#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394 /*
8395 * Allocation of the screen buffers is done only when the size changes and
8396 * when Rows and Columns have been set and we have started doing full
8397 * screen stuff.
8398 */
8399 if ((ScreenLines != NULL
8400 && Rows == screen_Rows
8401 && Columns == screen_Columns
8402#ifdef FEAT_MBYTE
8403 && enc_utf8 == (ScreenLinesUC != NULL)
8404 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008405 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406#endif
8407 )
8408 || Rows == 0
8409 || Columns == 0
8410 || (!full_screen && ScreenLines == NULL))
8411 return;
8412
8413 /*
8414 * It's possible that we produce an out-of-memory message below, which
8415 * will cause this function to be called again. To break the loop, just
8416 * return here.
8417 */
8418 if (entered)
8419 return;
8420 entered = TRUE;
8421
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008422 /*
8423 * Note that the window sizes are updated before reallocating the arrays,
8424 * thus we must not redraw here!
8425 */
8426 ++RedrawingDisabled;
8427
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428 win_new_shellsize(); /* fit the windows in the new sized shell */
8429
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430 comp_col(); /* recompute columns for shown command and ruler */
8431
8432 /*
8433 * We're changing the size of the screen.
8434 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8435 * - Move lines from the old arrays into the new arrays, clear extra
8436 * lines (unless the screen is going to be cleared).
8437 * - Free the old arrays.
8438 *
8439 * If anything fails, make ScreenLines NULL, so we don't do anything!
8440 * Continuing with the old ScreenLines may result in a crash, because the
8441 * size is wrong.
8442 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008443 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008445#ifdef FEAT_AUTOCMD
8446 if (aucmd_win != NULL)
8447 win_free_lsize(aucmd_win);
8448#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449
8450 new_ScreenLines = (schar_T *)lalloc((long_u)(
8451 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8452#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008453 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454 if (enc_utf8)
8455 {
8456 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8457 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008458 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008459 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8461 }
8462 if (enc_dbcs == DBCS_JPNU)
8463 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8464 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8465#endif
8466 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8467 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8468 new_LineOffset = (unsigned *)lalloc((long_u)(
8469 Rows * sizeof(unsigned)), FALSE);
8470 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008471#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008472 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008473#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008474
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008475 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476 {
8477 if (win_alloc_lines(wp) == FAIL)
8478 {
8479 outofmem = TRUE;
8480#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008481 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482#endif
8483 }
8484 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008485#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008486 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8487 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008488 outofmem = TRUE;
8489#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008490#ifdef FEAT_WINDOWS
8491give_up:
8492#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008494#ifdef FEAT_MBYTE
8495 for (i = 0; i < p_mco; ++i)
8496 if (new_ScreenLinesC[i] == NULL)
8497 break;
8498#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499 if (new_ScreenLines == NULL
8500#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008501 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8503#endif
8504 || new_ScreenAttrs == NULL
8505 || new_LineOffset == NULL
8506 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008507#ifdef FEAT_WINDOWS
8508 || new_TabPageIdxs == NULL
8509#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510 || outofmem)
8511 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008512 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008513 {
8514 /* guess the size */
8515 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8516
8517 /* Remember we did this to avoid getting outofmem messages over
8518 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008519 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521 vim_free(new_ScreenLines);
8522 new_ScreenLines = NULL;
8523#ifdef FEAT_MBYTE
8524 vim_free(new_ScreenLinesUC);
8525 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008526 for (i = 0; i < p_mco; ++i)
8527 {
8528 vim_free(new_ScreenLinesC[i]);
8529 new_ScreenLinesC[i] = NULL;
8530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531 vim_free(new_ScreenLines2);
8532 new_ScreenLines2 = NULL;
8533#endif
8534 vim_free(new_ScreenAttrs);
8535 new_ScreenAttrs = NULL;
8536 vim_free(new_LineOffset);
8537 new_LineOffset = NULL;
8538 vim_free(new_LineWraps);
8539 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008540#ifdef FEAT_WINDOWS
8541 vim_free(new_TabPageIdxs);
8542 new_TabPageIdxs = NULL;
8543#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544 }
8545 else
8546 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008547 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008548
Bram Moolenaar071d4272004-06-13 20:20:40 +00008549 for (new_row = 0; new_row < Rows; ++new_row)
8550 {
8551 new_LineOffset[new_row] = new_row * Columns;
8552 new_LineWraps[new_row] = FALSE;
8553
8554 /*
8555 * If the screen is not going to be cleared, copy as much as
8556 * possible from the old screen to the new one and clear the rest
8557 * (used when resizing the window at the "--more--" prompt or when
8558 * executing an external command, for the GUI).
8559 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008560 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561 {
8562 (void)vim_memset(new_ScreenLines + new_row * Columns,
8563 ' ', (size_t)Columns * sizeof(schar_T));
8564#ifdef FEAT_MBYTE
8565 if (enc_utf8)
8566 {
8567 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8568 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008569 for (i = 0; i < p_mco; ++i)
8570 (void)vim_memset(new_ScreenLinesC[i]
8571 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572 0, (size_t)Columns * sizeof(u8char_T));
8573 }
8574 if (enc_dbcs == DBCS_JPNU)
8575 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8576 0, (size_t)Columns * sizeof(schar_T));
8577#endif
8578 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8579 0, (size_t)Columns * sizeof(sattr_T));
8580 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008581 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582 {
8583 if (screen_Columns < Columns)
8584 len = screen_Columns;
8585 else
8586 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008587#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008588 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008589 * may be invalid now. Also when p_mco changes. */
8590 if (!(enc_utf8 && ScreenLinesUC == NULL)
8591 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008592#endif
8593 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8594 ScreenLines + LineOffset[old_row],
8595 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008597 if (enc_utf8 && ScreenLinesUC != NULL
8598 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599 {
8600 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8601 ScreenLinesUC + LineOffset[old_row],
8602 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008603 for (i = 0; i < p_mco; ++i)
8604 mch_memmove(new_ScreenLinesC[i]
8605 + new_LineOffset[new_row],
8606 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607 (size_t)len * sizeof(u8char_T));
8608 }
8609 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8610 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8611 ScreenLines2 + LineOffset[old_row],
8612 (size_t)len * sizeof(schar_T));
8613#endif
8614 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8615 ScreenAttrs + LineOffset[old_row],
8616 (size_t)len * sizeof(sattr_T));
8617 }
8618 }
8619 }
8620 /* Use the last line of the screen for the current line. */
8621 current_ScreenLine = new_ScreenLines + Rows * Columns;
8622 }
8623
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008624 free_screenlines();
8625
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626 ScreenLines = new_ScreenLines;
8627#ifdef FEAT_MBYTE
8628 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008629 for (i = 0; i < p_mco; ++i)
8630 ScreenLinesC[i] = new_ScreenLinesC[i];
8631 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632 ScreenLines2 = new_ScreenLines2;
8633#endif
8634 ScreenAttrs = new_ScreenAttrs;
8635 LineOffset = new_LineOffset;
8636 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008637#ifdef FEAT_WINDOWS
8638 TabPageIdxs = new_TabPageIdxs;
8639#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640
8641 /* It's important that screen_Rows and screen_Columns reflect the actual
8642 * size of ScreenLines[]. Set them before calling anything. */
8643#ifdef FEAT_GUI
8644 old_Rows = screen_Rows;
8645#endif
8646 screen_Rows = Rows;
8647 screen_Columns = Columns;
8648
8649 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008650 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651 screenclear2();
8652
8653#ifdef FEAT_GUI
8654 else if (gui.in_use
8655 && !gui.starting
8656 && ScreenLines != NULL
8657 && old_Rows != Rows)
8658 {
8659 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8660 /*
8661 * Adjust the position of the cursor, for when executing an external
8662 * command.
8663 */
8664 if (msg_row >= Rows) /* Rows got smaller */
8665 msg_row = Rows - 1; /* put cursor at last row */
8666 else if (Rows > old_Rows) /* Rows got bigger */
8667 msg_row += Rows - old_Rows; /* put cursor in same place */
8668 if (msg_col >= Columns) /* Columns got smaller */
8669 msg_col = Columns - 1; /* put cursor at last column */
8670 }
8671#endif
8672
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008674 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008675
8676#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008677 /*
8678 * Do not apply autocommands more than 3 times to avoid an endless loop
8679 * in case applying autocommands always changes Rows or Columns.
8680 */
8681 if (starting == 0 && ++retry_count <= 3)
8682 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008683 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008684 /* In rare cases, autocommands may have altered Rows or Columns,
8685 * jump back to check if we need to allocate the screen again. */
8686 goto retry;
8687 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008688#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689}
8690
8691 void
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008692free_screenlines()
8693{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008694#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008695 int i;
8696
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008697 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008698 for (i = 0; i < Screen_mco; ++i)
8699 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008700 vim_free(ScreenLines2);
8701#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008702 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008703 vim_free(ScreenAttrs);
8704 vim_free(LineOffset);
8705 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008706#ifdef FEAT_WINDOWS
8707 vim_free(TabPageIdxs);
8708#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008709}
8710
8711 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712screenclear()
8713{
8714 check_for_delay(FALSE);
8715 screenalloc(FALSE); /* allocate screen buffers if size changed */
8716 screenclear2(); /* clear the screen */
8717}
8718
8719 static void
8720screenclear2()
8721{
8722 int i;
8723
8724 if (starting == NO_SCREEN || ScreenLines == NULL
8725#ifdef FEAT_GUI
8726 || (gui.in_use && gui.starting)
8727#endif
8728 )
8729 return;
8730
8731#ifdef FEAT_GUI
8732 if (!gui.in_use)
8733#endif
8734 screen_attr = -1; /* force setting the Normal colors */
8735 screen_stop_highlight(); /* don't want highlighting here */
8736
8737#ifdef FEAT_CLIPBOARD
8738 /* disable selection without redrawing it */
8739 clip_scroll_selection(9999);
8740#endif
8741
8742 /* blank out ScreenLines */
8743 for (i = 0; i < Rows; ++i)
8744 {
8745 lineclear(LineOffset[i], (int)Columns);
8746 LineWraps[i] = FALSE;
8747 }
8748
8749 if (can_clear(T_CL))
8750 {
8751 out_str(T_CL); /* clear the display */
8752 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008753 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754 }
8755 else
8756 {
8757 /* can't clear the screen, mark all chars with invalid attributes */
8758 for (i = 0; i < Rows; ++i)
8759 lineinvalid(LineOffset[i], (int)Columns);
8760 clear_cmdline = TRUE;
8761 }
8762
8763 screen_cleared = TRUE; /* can use contents of ScreenLines now */
8764
8765 win_rest_invalid(firstwin);
8766 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008767#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008768 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008769#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008770 if (must_redraw == CLEAR) /* no need to clear again */
8771 must_redraw = NOT_VALID;
8772 compute_cmdrow();
8773 msg_row = cmdline_row; /* put cursor on last line for messages */
8774 msg_col = 0;
8775 screen_start(); /* don't know where cursor is now */
8776 msg_scrolled = 0; /* can't scroll back */
8777 msg_didany = FALSE;
8778 msg_didout = FALSE;
8779}
8780
8781/*
8782 * Clear one line in ScreenLines.
8783 */
8784 static void
8785lineclear(off, width)
8786 unsigned off;
8787 int width;
8788{
8789 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
8790#ifdef FEAT_MBYTE
8791 if (enc_utf8)
8792 (void)vim_memset(ScreenLinesUC + off, 0,
8793 (size_t)width * sizeof(u8char_T));
8794#endif
8795 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
8796}
8797
8798/*
8799 * Mark one line in ScreenLines invalid by setting the attributes to an
8800 * invalid value.
8801 */
8802 static void
8803lineinvalid(off, width)
8804 unsigned off;
8805 int width;
8806{
8807 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
8808}
8809
8810#ifdef FEAT_VERTSPLIT
8811/*
8812 * Copy part of a Screenline for vertically split window "wp".
8813 */
8814 static void
8815linecopy(to, from, wp)
8816 int to;
8817 int from;
8818 win_T *wp;
8819{
8820 unsigned off_to = LineOffset[to] + wp->w_wincol;
8821 unsigned off_from = LineOffset[from] + wp->w_wincol;
8822
8823 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
8824 wp->w_width * sizeof(schar_T));
8825# ifdef FEAT_MBYTE
8826 if (enc_utf8)
8827 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008828 int i;
8829
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
8831 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008832 for (i = 0; i < p_mco; ++i)
8833 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
8834 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835 }
8836 if (enc_dbcs == DBCS_JPNU)
8837 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
8838 wp->w_width * sizeof(schar_T));
8839# endif
8840 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
8841 wp->w_width * sizeof(sattr_T));
8842}
8843#endif
8844
8845/*
8846 * Return TRUE if clearing with term string "p" would work.
8847 * It can't work when the string is empty or it won't set the right background.
8848 */
8849 int
8850can_clear(p)
8851 char_u *p;
8852{
8853 return (*p != NUL && (t_colors <= 1
8854#ifdef FEAT_GUI
8855 || gui.in_use
8856#endif
8857 || cterm_normal_bg_color == 0 || *T_UT != NUL));
8858}
8859
8860/*
8861 * Reset cursor position. Use whenever cursor was moved because of outputting
8862 * something directly to the screen (shell commands) or a terminal control
8863 * code.
8864 */
8865 void
8866screen_start()
8867{
8868 screen_cur_row = screen_cur_col = 9999;
8869}
8870
8871/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872 * Move the cursor to position "row","col" in the screen.
8873 * This tries to find the most efficient way to move, minimizing the number of
8874 * characters sent to the terminal.
8875 */
8876 void
8877windgoto(row, col)
8878 int row;
8879 int col;
8880{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008881 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882 int i;
8883 int plan;
8884 int cost;
8885 int wouldbe_col;
8886 int noinvcurs;
8887 char_u *bs;
8888 int goto_cost;
8889 int attr;
8890
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008891#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
8893
8894#define PLAN_LE 1
8895#define PLAN_CR 2
8896#define PLAN_NL 3
8897#define PLAN_WRITE 4
8898 /* Can't use ScreenLines unless initialized */
8899 if (ScreenLines == NULL)
8900 return;
8901
8902 if (col != screen_cur_col || row != screen_cur_row)
8903 {
8904 /* Check for valid position. */
8905 if (row < 0) /* window without text lines? */
8906 row = 0;
8907 if (row >= screen_Rows)
8908 row = screen_Rows - 1;
8909 if (col >= screen_Columns)
8910 col = screen_Columns - 1;
8911
8912 /* check if no cursor movement is allowed in highlight mode */
8913 if (screen_attr && *T_MS == NUL)
8914 noinvcurs = HIGHL_COST;
8915 else
8916 noinvcurs = 0;
8917 goto_cost = GOTO_COST + noinvcurs;
8918
8919 /*
8920 * Plan how to do the positioning:
8921 * 1. Use CR to move it to column 0, same row.
8922 * 2. Use T_LE to move it a few columns to the left.
8923 * 3. Use NL to move a few lines down, column 0.
8924 * 4. Move a few columns to the right with T_ND or by writing chars.
8925 *
8926 * Don't do this if the cursor went beyond the last column, the cursor
8927 * position is unknown then (some terminals wrap, some don't )
8928 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008929 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00008930 * characters to move the cursor to the right.
8931 */
8932 if (row >= screen_cur_row && screen_cur_col < Columns)
8933 {
8934 /*
8935 * If the cursor is in the same row, bigger col, we can use CR
8936 * or T_LE.
8937 */
8938 bs = NULL; /* init for GCC */
8939 attr = screen_attr;
8940 if (row == screen_cur_row && col < screen_cur_col)
8941 {
8942 /* "le" is preferred over "bc", because "bc" is obsolete */
8943 if (*T_LE)
8944 bs = T_LE; /* "cursor left" */
8945 else
8946 bs = T_BC; /* "backspace character (old) */
8947 if (*bs)
8948 cost = (screen_cur_col - col) * (int)STRLEN(bs);
8949 else
8950 cost = 999;
8951 if (col + 1 < cost) /* using CR is less characters */
8952 {
8953 plan = PLAN_CR;
8954 wouldbe_col = 0;
8955 cost = 1; /* CR is just one character */
8956 }
8957 else
8958 {
8959 plan = PLAN_LE;
8960 wouldbe_col = col;
8961 }
8962 if (noinvcurs) /* will stop highlighting */
8963 {
8964 cost += noinvcurs;
8965 attr = 0;
8966 }
8967 }
8968
8969 /*
8970 * If the cursor is above where we want to be, we can use CR LF.
8971 */
8972 else if (row > screen_cur_row)
8973 {
8974 plan = PLAN_NL;
8975 wouldbe_col = 0;
8976 cost = (row - screen_cur_row) * 2; /* CR LF */
8977 if (noinvcurs) /* will stop highlighting */
8978 {
8979 cost += noinvcurs;
8980 attr = 0;
8981 }
8982 }
8983
8984 /*
8985 * If the cursor is in the same row, smaller col, just use write.
8986 */
8987 else
8988 {
8989 plan = PLAN_WRITE;
8990 wouldbe_col = screen_cur_col;
8991 cost = 0;
8992 }
8993
8994 /*
8995 * Check if any characters that need to be written have the
8996 * correct attributes. Also avoid UTF-8 characters.
8997 */
8998 i = col - wouldbe_col;
8999 if (i > 0)
9000 cost += i;
9001 if (cost < goto_cost && i > 0)
9002 {
9003 /*
9004 * Check if the attributes are correct without additionally
9005 * stopping highlighting.
9006 */
9007 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9008 while (i && *p++ == attr)
9009 --i;
9010 if (i != 0)
9011 {
9012 /*
9013 * Try if it works when highlighting is stopped here.
9014 */
9015 if (*--p == 0)
9016 {
9017 cost += noinvcurs;
9018 while (i && *p++ == 0)
9019 --i;
9020 }
9021 if (i != 0)
9022 cost = 999; /* different attributes, don't do it */
9023 }
9024#ifdef FEAT_MBYTE
9025 if (enc_utf8)
9026 {
9027 /* Don't use an UTF-8 char for positioning, it's slow. */
9028 for (i = wouldbe_col; i < col; ++i)
9029 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9030 {
9031 cost = 999;
9032 break;
9033 }
9034 }
9035#endif
9036 }
9037
9038 /*
9039 * We can do it without term_windgoto()!
9040 */
9041 if (cost < goto_cost)
9042 {
9043 if (plan == PLAN_LE)
9044 {
9045 if (noinvcurs)
9046 screen_stop_highlight();
9047 while (screen_cur_col > col)
9048 {
9049 out_str(bs);
9050 --screen_cur_col;
9051 }
9052 }
9053 else if (plan == PLAN_CR)
9054 {
9055 if (noinvcurs)
9056 screen_stop_highlight();
9057 out_char('\r');
9058 screen_cur_col = 0;
9059 }
9060 else if (plan == PLAN_NL)
9061 {
9062 if (noinvcurs)
9063 screen_stop_highlight();
9064 while (screen_cur_row < row)
9065 {
9066 out_char('\n');
9067 ++screen_cur_row;
9068 }
9069 screen_cur_col = 0;
9070 }
9071
9072 i = col - screen_cur_col;
9073 if (i > 0)
9074 {
9075 /*
9076 * Use cursor-right if it's one character only. Avoids
9077 * removing a line of pixels from the last bold char, when
9078 * using the bold trick in the GUI.
9079 */
9080 if (T_ND[0] != NUL && T_ND[1] == NUL)
9081 {
9082 while (i-- > 0)
9083 out_char(*T_ND);
9084 }
9085 else
9086 {
9087 int off;
9088
9089 off = LineOffset[row] + screen_cur_col;
9090 while (i-- > 0)
9091 {
9092 if (ScreenAttrs[off] != screen_attr)
9093 screen_stop_highlight();
9094#ifdef FEAT_MBYTE
9095 out_flush_check();
9096#endif
9097 out_char(ScreenLines[off]);
9098#ifdef FEAT_MBYTE
9099 if (enc_dbcs == DBCS_JPNU
9100 && ScreenLines[off] == 0x8e)
9101 out_char(ScreenLines2[off]);
9102#endif
9103 ++off;
9104 }
9105 }
9106 }
9107 }
9108 }
9109 else
9110 cost = 999;
9111
9112 if (cost >= goto_cost)
9113 {
9114 if (noinvcurs)
9115 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009116 if (row == screen_cur_row && (col > screen_cur_col)
9117 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009118 term_cursor_right(col - screen_cur_col);
9119 else
9120 term_windgoto(row, col);
9121 }
9122 screen_cur_row = row;
9123 screen_cur_col = col;
9124 }
9125}
9126
9127/*
9128 * Set cursor to its position in the current window.
9129 */
9130 void
9131setcursor()
9132{
9133 if (redrawing())
9134 {
9135 validate_cursor();
9136 windgoto(W_WINROW(curwin) + curwin->w_wrow,
9137 W_WINCOL(curwin) + (
9138#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009139 /* With 'rightleft' set and the cursor on a double-wide
9140 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009141 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
9142# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009143 (has_mbyte
9144 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9145 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009146# endif
9147 1)) :
9148#endif
9149 curwin->w_wcol));
9150 }
9151}
9152
9153
9154/*
9155 * insert 'line_count' lines at 'row' in window 'wp'
9156 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9157 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
9158 * scrolling.
9159 * Returns FAIL if the lines are not inserted, OK for success.
9160 */
9161 int
9162win_ins_lines(wp, row, line_count, invalid, mayclear)
9163 win_T *wp;
9164 int row;
9165 int line_count;
9166 int invalid;
9167 int mayclear;
9168{
9169 int did_delete;
9170 int nextrow;
9171 int lastrow;
9172 int retval;
9173
9174 if (invalid)
9175 wp->w_lines_valid = 0;
9176
9177 if (wp->w_height < 5)
9178 return FAIL;
9179
9180 if (line_count > wp->w_height - row)
9181 line_count = wp->w_height - row;
9182
9183 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
9184 if (retval != MAYBE)
9185 return retval;
9186
9187 /*
9188 * If there is a next window or a status line, we first try to delete the
9189 * lines at the bottom to avoid messing what is after the window.
9190 * If this fails and there are following windows, don't do anything to avoid
9191 * messing up those windows, better just redraw.
9192 */
9193 did_delete = FALSE;
9194#ifdef FEAT_WINDOWS
9195 if (wp->w_next != NULL || wp->w_status_height)
9196 {
9197 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9198 line_count, (int)Rows, FALSE, NULL) == OK)
9199 did_delete = TRUE;
9200 else if (wp->w_next)
9201 return FAIL;
9202 }
9203#endif
9204 /*
9205 * if no lines deleted, blank the lines that will end up below the window
9206 */
9207 if (!did_delete)
9208 {
9209#ifdef FEAT_WINDOWS
9210 wp->w_redr_status = TRUE;
9211#endif
9212 redraw_cmdline = TRUE;
9213 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
9214 lastrow = nextrow + line_count;
9215 if (lastrow > Rows)
9216 lastrow = Rows;
9217 screen_fill(nextrow - line_count, lastrow - line_count,
9218 W_WINCOL(wp), (int)W_ENDCOL(wp),
9219 ' ', ' ', 0);
9220 }
9221
9222 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
9223 == FAIL)
9224 {
9225 /* deletion will have messed up other windows */
9226 if (did_delete)
9227 {
9228#ifdef FEAT_WINDOWS
9229 wp->w_redr_status = TRUE;
9230#endif
9231 win_rest_invalid(W_NEXT(wp));
9232 }
9233 return FAIL;
9234 }
9235
9236 return OK;
9237}
9238
9239/*
9240 * delete "line_count" window lines at "row" in window "wp"
9241 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9242 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9243 * scrolling
9244 * Return OK for success, FAIL if the lines are not deleted.
9245 */
9246 int
9247win_del_lines(wp, row, line_count, invalid, mayclear)
9248 win_T *wp;
9249 int row;
9250 int line_count;
9251 int invalid;
9252 int mayclear;
9253{
9254 int retval;
9255
9256 if (invalid)
9257 wp->w_lines_valid = 0;
9258
9259 if (line_count > wp->w_height - row)
9260 line_count = wp->w_height - row;
9261
9262 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
9263 if (retval != MAYBE)
9264 return retval;
9265
9266 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
9267 (int)Rows, FALSE, NULL) == FAIL)
9268 return FAIL;
9269
9270#ifdef FEAT_WINDOWS
9271 /*
9272 * If there are windows or status lines below, try to put them at the
9273 * correct place. If we can't do that, they have to be redrawn.
9274 */
9275 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9276 {
9277 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9278 line_count, (int)Rows, NULL) == FAIL)
9279 {
9280 wp->w_redr_status = TRUE;
9281 win_rest_invalid(wp->w_next);
9282 }
9283 }
9284 /*
9285 * If this is the last window and there is no status line, redraw the
9286 * command line later.
9287 */
9288 else
9289#endif
9290 redraw_cmdline = TRUE;
9291 return OK;
9292}
9293
9294/*
9295 * Common code for win_ins_lines() and win_del_lines().
9296 * Returns OK or FAIL when the work has been done.
9297 * Returns MAYBE when not finished yet.
9298 */
9299 static int
9300win_do_lines(wp, row, line_count, mayclear, del)
9301 win_T *wp;
9302 int row;
9303 int line_count;
9304 int mayclear;
9305 int del;
9306{
9307 int retval;
9308
9309 if (!redrawing() || line_count <= 0)
9310 return FAIL;
9311
9312 /* only a few lines left: redraw is faster */
9313 if (mayclear && Rows - line_count < 5
9314#ifdef FEAT_VERTSPLIT
9315 && wp->w_width == Columns
9316#endif
9317 )
9318 {
9319 screenclear(); /* will set wp->w_lines_valid to 0 */
9320 return FAIL;
9321 }
9322
9323 /*
9324 * Delete all remaining lines
9325 */
9326 if (row + line_count >= wp->w_height)
9327 {
9328 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
9329 W_WINCOL(wp), (int)W_ENDCOL(wp),
9330 ' ', ' ', 0);
9331 return OK;
9332 }
9333
9334 /*
9335 * when scrolling, the message on the command line should be cleared,
9336 * otherwise it will stay there forever.
9337 */
9338 clear_cmdline = TRUE;
9339
9340 /*
9341 * If the terminal can set a scroll region, use that.
9342 * Always do this in a vertically split window. This will redraw from
9343 * ScreenLines[] when t_CV isn't defined. That's faster than using
9344 * win_line().
9345 * Don't use a scroll region when we are going to redraw the text, writing
9346 * a character in the lower right corner of the scroll region causes a
9347 * scroll-up in the DJGPP version.
9348 */
9349 if (scroll_region
9350#ifdef FEAT_VERTSPLIT
9351 || W_WIDTH(wp) != Columns
9352#endif
9353 )
9354 {
9355#ifdef FEAT_VERTSPLIT
9356 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9357#endif
9358 scroll_region_set(wp, row);
9359 if (del)
9360 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
9361 wp->w_height - row, FALSE, wp);
9362 else
9363 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
9364 wp->w_height - row, wp);
9365#ifdef FEAT_VERTSPLIT
9366 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9367#endif
9368 scroll_region_reset();
9369 return retval;
9370 }
9371
9372#ifdef FEAT_WINDOWS
9373 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9374 return FAIL;
9375#endif
9376
9377 return MAYBE;
9378}
9379
9380/*
9381 * window 'wp' and everything after it is messed up, mark it for redraw
9382 */
9383 static void
9384win_rest_invalid(wp)
9385 win_T *wp;
9386{
9387#ifdef FEAT_WINDOWS
9388 while (wp != NULL)
9389#else
9390 if (wp != NULL)
9391#endif
9392 {
9393 redraw_win_later(wp, NOT_VALID);
9394#ifdef FEAT_WINDOWS
9395 wp->w_redr_status = TRUE;
9396 wp = wp->w_next;
9397#endif
9398 }
9399 redraw_cmdline = TRUE;
9400}
9401
9402/*
9403 * The rest of the routines in this file perform screen manipulations. The
9404 * given operation is performed physically on the screen. The corresponding
9405 * change is also made to the internal screen image. In this way, the editor
9406 * anticipates the effect of editing changes on the appearance of the screen.
9407 * That way, when we call screenupdate a complete redraw isn't usually
9408 * necessary. Another advantage is that we can keep adding code to anticipate
9409 * screen changes, and in the meantime, everything still works.
9410 */
9411
9412/*
9413 * types for inserting or deleting lines
9414 */
9415#define USE_T_CAL 1
9416#define USE_T_CDL 2
9417#define USE_T_AL 3
9418#define USE_T_CE 4
9419#define USE_T_DL 5
9420#define USE_T_SR 6
9421#define USE_NL 7
9422#define USE_T_CD 8
9423#define USE_REDRAW 9
9424
9425/*
9426 * insert lines on the screen and update ScreenLines[]
9427 * 'end' is the line after the scrolled part. Normally it is Rows.
9428 * When scrolling region used 'off' is the offset from the top for the region.
9429 * 'row' and 'end' are relative to the start of the region.
9430 *
9431 * return FAIL for failure, OK for success.
9432 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009433 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00009434screen_ins_lines(off, row, line_count, end, wp)
9435 int off;
9436 int row;
9437 int line_count;
9438 int end;
9439 win_T *wp; /* NULL or window to use width from */
9440{
9441 int i;
9442 int j;
9443 unsigned temp;
9444 int cursor_row;
9445 int type;
9446 int result_empty;
9447 int can_ce = can_clear(T_CE);
9448
9449 /*
9450 * FAIL if
9451 * - there is no valid screen
9452 * - the screen has to be redrawn completely
9453 * - the line count is less than one
9454 * - the line count is more than 'ttyscroll'
9455 */
9456 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
9457 return FAIL;
9458
9459 /*
9460 * There are seven ways to insert lines:
9461 * 0. When in a vertically split window and t_CV isn't set, redraw the
9462 * characters from ScreenLines[].
9463 * 1. Use T_CD (clear to end of display) if it exists and the result of
9464 * the insert is just empty lines
9465 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9466 * present or line_count > 1. It looks better if we do all the inserts
9467 * at once.
9468 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9469 * insert is just empty lines and T_CE is not present or line_count >
9470 * 1.
9471 * 4. Use T_AL (insert line) if it exists.
9472 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9473 * just empty lines.
9474 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9475 * just empty lines.
9476 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9477 * the 'da' flag is not set or we have clear line capability.
9478 * 8. redraw the characters from ScreenLines[].
9479 *
9480 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9481 * the scrollbar for the window. It does have insert line, use that if it
9482 * exists.
9483 */
9484 result_empty = (row + line_count >= end);
9485#ifdef FEAT_VERTSPLIT
9486 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9487 type = USE_REDRAW;
9488 else
9489#endif
9490 if (can_clear(T_CD) && result_empty)
9491 type = USE_T_CD;
9492 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9493 type = USE_T_CAL;
9494 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9495 type = USE_T_CDL;
9496 else if (*T_AL != NUL)
9497 type = USE_T_AL;
9498 else if (can_ce && result_empty)
9499 type = USE_T_CE;
9500 else if (*T_DL != NUL && result_empty)
9501 type = USE_T_DL;
9502 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9503 type = USE_T_SR;
9504 else
9505 return FAIL;
9506
9507 /*
9508 * For clearing the lines screen_del_lines() is used. This will also take
9509 * care of t_db if necessary.
9510 */
9511 if (type == USE_T_CD || type == USE_T_CDL ||
9512 type == USE_T_CE || type == USE_T_DL)
9513 return screen_del_lines(off, row, line_count, end, FALSE, wp);
9514
9515 /*
9516 * If text is retained below the screen, first clear or delete as many
9517 * lines at the bottom of the window as are about to be inserted so that
9518 * the deleted lines won't later surface during a screen_del_lines.
9519 */
9520 if (*T_DB)
9521 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
9522
9523#ifdef FEAT_CLIPBOARD
9524 /* Remove a modeless selection when inserting lines halfway the screen
9525 * or not the full width of the screen. */
9526 if (off + row > 0
9527# ifdef FEAT_VERTSPLIT
9528 || (wp != NULL && wp->w_width != Columns)
9529# endif
9530 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009531 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009532 else
9533 clip_scroll_selection(-line_count);
9534#endif
9535
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536#ifdef FEAT_GUI
9537 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9538 * scrolling is actually carried out. */
9539 gui_dont_update_cursor();
9540#endif
9541
9542 if (*T_CCS != NUL) /* cursor relative to region */
9543 cursor_row = row;
9544 else
9545 cursor_row = row + off;
9546
9547 /*
9548 * Shift LineOffset[] line_count down to reflect the inserted lines.
9549 * Clear the inserted lines in ScreenLines[].
9550 */
9551 row += off;
9552 end += off;
9553 for (i = 0; i < line_count; ++i)
9554 {
9555#ifdef FEAT_VERTSPLIT
9556 if (wp != NULL && wp->w_width != Columns)
9557 {
9558 /* need to copy part of a line */
9559 j = end - 1 - i;
9560 while ((j -= line_count) >= row)
9561 linecopy(j + line_count, j, wp);
9562 j += line_count;
9563 if (can_clear((char_u *)" "))
9564 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9565 else
9566 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9567 LineWraps[j] = FALSE;
9568 }
9569 else
9570#endif
9571 {
9572 j = end - 1 - i;
9573 temp = LineOffset[j];
9574 while ((j -= line_count) >= row)
9575 {
9576 LineOffset[j + line_count] = LineOffset[j];
9577 LineWraps[j + line_count] = LineWraps[j];
9578 }
9579 LineOffset[j + line_count] = temp;
9580 LineWraps[j + line_count] = FALSE;
9581 if (can_clear((char_u *)" "))
9582 lineclear(temp, (int)Columns);
9583 else
9584 lineinvalid(temp, (int)Columns);
9585 }
9586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009587
9588 screen_stop_highlight();
9589 windgoto(cursor_row, 0);
9590
9591#ifdef FEAT_VERTSPLIT
9592 /* redraw the characters */
9593 if (type == USE_REDRAW)
9594 redraw_block(row, end, wp);
9595 else
9596#endif
9597 if (type == USE_T_CAL)
9598 {
9599 term_append_lines(line_count);
9600 screen_start(); /* don't know where cursor is now */
9601 }
9602 else
9603 {
9604 for (i = 0; i < line_count; i++)
9605 {
9606 if (type == USE_T_AL)
9607 {
9608 if (i && cursor_row != 0)
9609 windgoto(cursor_row, 0);
9610 out_str(T_AL);
9611 }
9612 else /* type == USE_T_SR */
9613 out_str(T_SR);
9614 screen_start(); /* don't know where cursor is now */
9615 }
9616 }
9617
9618 /*
9619 * With scroll-reverse and 'da' flag set we need to clear the lines that
9620 * have been scrolled down into the region.
9621 */
9622 if (type == USE_T_SR && *T_DA)
9623 {
9624 for (i = 0; i < line_count; ++i)
9625 {
9626 windgoto(off + i, 0);
9627 out_str(T_CE);
9628 screen_start(); /* don't know where cursor is now */
9629 }
9630 }
9631
9632#ifdef FEAT_GUI
9633 gui_can_update_cursor();
9634 if (gui.in_use)
9635 out_flush(); /* always flush after a scroll */
9636#endif
9637 return OK;
9638}
9639
9640/*
9641 * delete lines on the screen and update ScreenLines[]
9642 * 'end' is the line after the scrolled part. Normally it is Rows.
9643 * When scrolling region used 'off' is the offset from the top for the region.
9644 * 'row' and 'end' are relative to the start of the region.
9645 *
9646 * Return OK for success, FAIL if the lines are not deleted.
9647 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009648 int
9649screen_del_lines(off, row, line_count, end, force, wp)
9650 int off;
9651 int row;
9652 int line_count;
9653 int end;
9654 int force; /* even when line_count > p_ttyscroll */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009655 win_T *wp UNUSED; /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009656{
9657 int j;
9658 int i;
9659 unsigned temp;
9660 int cursor_row;
9661 int cursor_end;
9662 int result_empty; /* result is empty until end of region */
9663 int can_delete; /* deleting line codes can be used */
9664 int type;
9665
9666 /*
9667 * FAIL if
9668 * - there is no valid screen
9669 * - the screen has to be redrawn completely
9670 * - the line count is less than one
9671 * - the line count is more than 'ttyscroll'
9672 */
9673 if (!screen_valid(TRUE) || line_count <= 0 ||
9674 (!force && line_count > p_ttyscroll))
9675 return FAIL;
9676
9677 /*
9678 * Check if the rest of the current region will become empty.
9679 */
9680 result_empty = row + line_count >= end;
9681
9682 /*
9683 * We can delete lines only when 'db' flag not set or when 'ce' option
9684 * available.
9685 */
9686 can_delete = (*T_DB == NUL || can_clear(T_CE));
9687
9688 /*
9689 * There are six ways to delete lines:
9690 * 0. When in a vertically split window and t_CV isn't set, redraw the
9691 * characters from ScreenLines[].
9692 * 1. Use T_CD if it exists and the result is empty.
9693 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9694 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9695 * none of the other ways work.
9696 * 4. Use T_CE (erase line) if the result is empty.
9697 * 5. Use T_DL (delete line) if it exists.
9698 * 6. redraw the characters from ScreenLines[].
9699 */
9700#ifdef FEAT_VERTSPLIT
9701 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9702 type = USE_REDRAW;
9703 else
9704#endif
9705 if (can_clear(T_CD) && result_empty)
9706 type = USE_T_CD;
9707#if defined(__BEOS__) && defined(BEOS_DR8)
9708 /*
9709 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9710 * its internal termcap... this works okay for tests which test *T_DB !=
9711 * NUL. It has the disadvantage that the user cannot use any :set t_*
9712 * command to get T_DB (back) to empty_option, only :set term=... will do
9713 * the trick...
9714 * Anyway, this hack will hopefully go away with the next OS release.
9715 * (Olaf Seibert)
9716 */
9717 else if (row == 0 && T_DB == empty_option
9718 && (line_count == 1 || *T_CDL == NUL))
9719#else
9720 else if (row == 0 && (
9721#ifndef AMIGA
9722 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9723 * up, so use delete-line command */
9724 line_count == 1 ||
9725#endif
9726 *T_CDL == NUL))
9727#endif
9728 type = USE_NL;
9729 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9730 type = USE_T_CDL;
9731 else if (can_clear(T_CE) && result_empty
9732#ifdef FEAT_VERTSPLIT
9733 && (wp == NULL || wp->w_width == Columns)
9734#endif
9735 )
9736 type = USE_T_CE;
9737 else if (*T_DL != NUL && can_delete)
9738 type = USE_T_DL;
9739 else if (*T_CDL != NUL && can_delete)
9740 type = USE_T_CDL;
9741 else
9742 return FAIL;
9743
9744#ifdef FEAT_CLIPBOARD
9745 /* Remove a modeless selection when deleting lines halfway the screen or
9746 * not the full width of the screen. */
9747 if (off + row > 0
9748# ifdef FEAT_VERTSPLIT
9749 || (wp != NULL && wp->w_width != Columns)
9750# endif
9751 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009752 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009753 else
9754 clip_scroll_selection(line_count);
9755#endif
9756
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757#ifdef FEAT_GUI
9758 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9759 * scrolling is actually carried out. */
9760 gui_dont_update_cursor();
9761#endif
9762
9763 if (*T_CCS != NUL) /* cursor relative to region */
9764 {
9765 cursor_row = row;
9766 cursor_end = end;
9767 }
9768 else
9769 {
9770 cursor_row = row + off;
9771 cursor_end = end + off;
9772 }
9773
9774 /*
9775 * Now shift LineOffset[] line_count up to reflect the deleted lines.
9776 * Clear the inserted lines in ScreenLines[].
9777 */
9778 row += off;
9779 end += off;
9780 for (i = 0; i < line_count; ++i)
9781 {
9782#ifdef FEAT_VERTSPLIT
9783 if (wp != NULL && wp->w_width != Columns)
9784 {
9785 /* need to copy part of a line */
9786 j = row + i;
9787 while ((j += line_count) <= end - 1)
9788 linecopy(j - line_count, j, wp);
9789 j -= line_count;
9790 if (can_clear((char_u *)" "))
9791 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9792 else
9793 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9794 LineWraps[j] = FALSE;
9795 }
9796 else
9797#endif
9798 {
9799 /* whole width, moving the line pointers is faster */
9800 j = row + i;
9801 temp = LineOffset[j];
9802 while ((j += line_count) <= end - 1)
9803 {
9804 LineOffset[j - line_count] = LineOffset[j];
9805 LineWraps[j - line_count] = LineWraps[j];
9806 }
9807 LineOffset[j - line_count] = temp;
9808 LineWraps[j - line_count] = FALSE;
9809 if (can_clear((char_u *)" "))
9810 lineclear(temp, (int)Columns);
9811 else
9812 lineinvalid(temp, (int)Columns);
9813 }
9814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009815
9816 screen_stop_highlight();
9817
9818#ifdef FEAT_VERTSPLIT
9819 /* redraw the characters */
9820 if (type == USE_REDRAW)
9821 redraw_block(row, end, wp);
9822 else
9823#endif
9824 if (type == USE_T_CD) /* delete the lines */
9825 {
9826 windgoto(cursor_row, 0);
9827 out_str(T_CD);
9828 screen_start(); /* don't know where cursor is now */
9829 }
9830 else if (type == USE_T_CDL)
9831 {
9832 windgoto(cursor_row, 0);
9833 term_delete_lines(line_count);
9834 screen_start(); /* don't know where cursor is now */
9835 }
9836 /*
9837 * Deleting lines at top of the screen or scroll region: Just scroll
9838 * the whole screen (scroll region) up by outputting newlines on the
9839 * last line.
9840 */
9841 else if (type == USE_NL)
9842 {
9843 windgoto(cursor_end - 1, 0);
9844 for (i = line_count; --i >= 0; )
9845 out_char('\n'); /* cursor will remain on same line */
9846 }
9847 else
9848 {
9849 for (i = line_count; --i >= 0; )
9850 {
9851 if (type == USE_T_DL)
9852 {
9853 windgoto(cursor_row, 0);
9854 out_str(T_DL); /* delete a line */
9855 }
9856 else /* type == USE_T_CE */
9857 {
9858 windgoto(cursor_row + i, 0);
9859 out_str(T_CE); /* erase a line */
9860 }
9861 screen_start(); /* don't know where cursor is now */
9862 }
9863 }
9864
9865 /*
9866 * If the 'db' flag is set, we need to clear the lines that have been
9867 * scrolled up at the bottom of the region.
9868 */
9869 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
9870 {
9871 for (i = line_count; i > 0; --i)
9872 {
9873 windgoto(cursor_end - i, 0);
9874 out_str(T_CE); /* erase a line */
9875 screen_start(); /* don't know where cursor is now */
9876 }
9877 }
9878
9879#ifdef FEAT_GUI
9880 gui_can_update_cursor();
9881 if (gui.in_use)
9882 out_flush(); /* always flush after a scroll */
9883#endif
9884
9885 return OK;
9886}
9887
9888/*
9889 * show the current mode and ruler
9890 *
9891 * If clear_cmdline is TRUE, clear the rest of the cmdline.
9892 * If clear_cmdline is FALSE there may be a message there that needs to be
9893 * cleared only if a mode is shown.
9894 * Return the length of the message (0 if no message).
9895 */
9896 int
9897showmode()
9898{
9899 int need_clear;
9900 int length = 0;
9901 int do_mode;
9902 int attr;
9903 int nwr_save;
9904#ifdef FEAT_INS_EXPAND
9905 int sub_attr;
9906#endif
9907
Bram Moolenaar7df351e2006-01-23 22:30:28 +00009908 do_mode = ((p_smd && msg_silent == 0)
9909 && ((State & INSERT)
9910 || restart_edit
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009911 || VIsual_active));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009912 if (do_mode || Recording)
9913 {
9914 /*
9915 * Don't show mode right now, when not redrawing or inside a mapping.
9916 * Call char_avail() only when we are going to show something, because
9917 * it takes a bit of time.
9918 */
9919 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
9920 {
9921 redraw_cmdline = TRUE; /* show mode later */
9922 return 0;
9923 }
9924
9925 nwr_save = need_wait_return;
9926
9927 /* wait a bit before overwriting an important message */
9928 check_for_delay(FALSE);
9929
9930 /* if the cmdline is more than one line high, erase top lines */
9931 need_clear = clear_cmdline;
9932 if (clear_cmdline && cmdline_row < Rows - 1)
9933 msg_clr_cmdline(); /* will reset clear_cmdline */
9934
9935 /* Position on the last line in the window, column 0 */
9936 msg_pos_mode();
9937 cursor_off();
9938 attr = hl_attr(HLF_CM); /* Highlight mode */
9939 if (do_mode)
9940 {
9941 MSG_PUTS_ATTR("--", attr);
9942#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00009943 if (
Bram Moolenaar09092152010-08-08 16:38:42 +02009944# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +00009945 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +02009946# else
Bram Moolenaarc236c162008-07-13 17:41:49 +00009947 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +00009948# endif
Bram Moolenaar09092152010-08-08 16:38:42 +02009949 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02009950# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009951 MSG_PUTS_ATTR(" IM", attr);
9952# else
9953 MSG_PUTS_ATTR(" XIM", attr);
9954# endif
9955#endif
9956#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
9957 if (gui.in_use)
9958 {
9959 if (hangul_input_state_get())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009960 MSG_PUTS_ATTR(" \307\321\261\333", attr); /* HANGUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009961 }
9962#endif
9963#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +02009964 /* CTRL-X in Insert mode */
9965 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009966 {
9967 /* These messages can get long, avoid a wrap in a narrow
9968 * window. Prefer showing edit_submode_extra. */
9969 length = (Rows - msg_row) * Columns - 3;
9970 if (edit_submode_extra != NULL)
9971 length -= vim_strsize(edit_submode_extra);
9972 if (length > 0)
9973 {
9974 if (edit_submode_pre != NULL)
9975 length -= vim_strsize(edit_submode_pre);
9976 if (length - vim_strsize(edit_submode) > 0)
9977 {
9978 if (edit_submode_pre != NULL)
9979 msg_puts_attr(edit_submode_pre, attr);
9980 msg_puts_attr(edit_submode, attr);
9981 }
9982 if (edit_submode_extra != NULL)
9983 {
9984 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
9985 if ((int)edit_submode_highl < (int)HLF_COUNT)
9986 sub_attr = hl_attr(edit_submode_highl);
9987 else
9988 sub_attr = attr;
9989 msg_puts_attr(edit_submode_extra, sub_attr);
9990 }
9991 }
9992 length = 0;
9993 }
9994 else
9995#endif
9996 {
9997#ifdef FEAT_VREPLACE
9998 if (State & VREPLACE_FLAG)
9999 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
10000 else
10001#endif
10002 if (State & REPLACE_FLAG)
10003 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10004 else if (State & INSERT)
10005 {
10006#ifdef FEAT_RIGHTLEFT
10007 if (p_ri)
10008 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10009#endif
10010 MSG_PUTS_ATTR(_(" INSERT"), attr);
10011 }
10012 else if (restart_edit == 'I')
10013 MSG_PUTS_ATTR(_(" (insert)"), attr);
10014 else if (restart_edit == 'R')
10015 MSG_PUTS_ATTR(_(" (replace)"), attr);
10016 else if (restart_edit == 'V')
10017 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10018#ifdef FEAT_RIGHTLEFT
10019 if (p_hkmap)
10020 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10021# ifdef FEAT_FKMAP
10022 if (p_fkmap)
10023 MSG_PUTS_ATTR(farsi_text_5, attr);
10024# endif
10025#endif
10026#ifdef FEAT_KEYMAP
10027 if (State & LANGMAP)
10028 {
10029# ifdef FEAT_ARABIC
10030 if (curwin->w_p_arab)
10031 MSG_PUTS_ATTR(_(" Arabic"), attr);
10032 else
10033# endif
10034 MSG_PUTS_ATTR(_(" (lang)"), attr);
10035 }
10036#endif
10037 if ((State & INSERT) && p_paste)
10038 MSG_PUTS_ATTR(_(" (paste)"), attr);
10039
Bram Moolenaar071d4272004-06-13 20:20:40 +000010040 if (VIsual_active)
10041 {
10042 char *p;
10043
10044 /* Don't concatenate separate words to avoid translation
10045 * problems. */
10046 switch ((VIsual_select ? 4 : 0)
10047 + (VIsual_mode == Ctrl_V) * 2
10048 + (VIsual_mode == 'V'))
10049 {
10050 case 0: p = N_(" VISUAL"); break;
10051 case 1: p = N_(" VISUAL LINE"); break;
10052 case 2: p = N_(" VISUAL BLOCK"); break;
10053 case 4: p = N_(" SELECT"); break;
10054 case 5: p = N_(" SELECT LINE"); break;
10055 default: p = N_(" SELECT BLOCK"); break;
10056 }
10057 MSG_PUTS_ATTR(_(p), attr);
10058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010059 MSG_PUTS_ATTR(" --", attr);
10060 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010061
Bram Moolenaar071d4272004-06-13 20:20:40 +000010062 need_clear = TRUE;
10063 }
10064 if (Recording
10065#ifdef FEAT_INS_EXPAND
10066 && edit_submode == NULL /* otherwise it gets too long */
10067#endif
10068 )
10069 {
10070 MSG_PUTS_ATTR(_("recording"), attr);
10071 need_clear = TRUE;
10072 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010073
10074 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010075 if (need_clear || clear_cmdline)
10076 msg_clr_eos();
10077 msg_didout = FALSE; /* overwrite this message */
10078 length = msg_col;
10079 msg_col = 0;
10080 need_wait_return = nwr_save; /* never ask for hit-return for this */
10081 }
10082 else if (clear_cmdline && msg_silent == 0)
10083 /* Clear the whole command line. Will reset "clear_cmdline". */
10084 msg_clr_cmdline();
10085
10086#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010087 /* In Visual mode the size of the selected area must be redrawn. */
10088 if (VIsual_active)
10089 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010090
10091 /* If the last window has no status line, the ruler is after the mode
10092 * message and must be redrawn */
10093 if (redrawing()
10094# ifdef FEAT_WINDOWS
10095 && lastwin->w_status_height == 0
10096# endif
10097 )
10098 win_redr_ruler(lastwin, TRUE);
10099#endif
10100 redraw_cmdline = FALSE;
10101 clear_cmdline = FALSE;
10102
10103 return length;
10104}
10105
10106/*
10107 * Position for a mode message.
10108 */
10109 static void
10110msg_pos_mode()
10111{
10112 msg_col = 0;
10113 msg_row = Rows - 1;
10114}
10115
10116/*
10117 * Delete mode message. Used when ESC is typed which is expected to end
10118 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010119 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120 */
10121 void
10122unshowmode(force)
10123 int force;
10124{
10125 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010126 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010127 */
10128 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10129 redraw_cmdline = TRUE; /* delete mode later */
10130 else
10131 {
10132 msg_pos_mode();
10133 if (Recording)
10134 MSG_PUTS_ATTR(_("recording"), hl_attr(HLF_CM));
10135 msg_clr_eos();
10136 }
10137}
10138
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010139#if defined(FEAT_WINDOWS)
10140/*
10141 * Draw the tab pages line at the top of the Vim window.
10142 */
10143 static void
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010144draw_tabline()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010145{
10146 int tabcount = 0;
10147 tabpage_T *tp;
10148 int tabwidth;
10149 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010150 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010151 int attr;
10152 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010153 win_T *cwp;
10154 int wincount;
10155 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010156 int c;
10157 int len;
10158 int attr_sel = hl_attr(HLF_TPS);
10159 int attr_nosel = hl_attr(HLF_TP);
10160 int attr_fill = hl_attr(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010161 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010162 int room;
10163 int use_sep_chars = (t_colors < 8
10164#ifdef FEAT_GUI
10165 && !gui.in_use
10166#endif
10167 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010168
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010169 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010170
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010171#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010172 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010173 if (gui_use_tabline())
10174 {
10175 gui_update_tabline();
10176 return;
10177 }
10178#endif
10179
10180 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010181 return;
10182
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010183#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010184
10185 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10186 for (scol = 0; scol < Columns; ++scol)
10187 TabPageIdxs[scol] = 0;
10188
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010189 /* Use the 'tabline' option if it's set. */
10190 if (*p_tal != NUL)
10191 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010192 int save_called_emsg = called_emsg;
10193
10194 /* Check for an error. If there is one we would loop in redrawing the
10195 * screen. Avoid that by making 'tabline' empty. */
10196 called_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010197 win_redr_custom(NULL, FALSE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010198 if (called_emsg)
10199 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010200 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010201 called_emsg |= save_called_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010202 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010203 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010204#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010205 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010206 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
10207 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010208
Bram Moolenaar238a5642006-02-21 22:12:05 +000010209 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10210 if (tabwidth < 6)
10211 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010212
Bram Moolenaar238a5642006-02-21 22:12:05 +000010213 attr = attr_nosel;
10214 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010215 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010216 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10217 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010218 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010219 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010220
Bram Moolenaar238a5642006-02-21 22:12:05 +000010221 if (tp->tp_topframe == topframe)
10222 attr = attr_sel;
10223 if (use_sep_chars && col > 0)
10224 screen_putchar('|', 0, col++, attr);
10225
10226 if (tp->tp_topframe != topframe)
10227 attr = attr_nosel;
10228
10229 screen_putchar(' ', 0, col++, attr);
10230
10231 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010232 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010233 cwp = curwin;
10234 wp = firstwin;
10235 }
10236 else
10237 {
10238 cwp = tp->tp_curwin;
10239 wp = tp->tp_firstwin;
10240 }
10241
10242 modified = FALSE;
10243 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10244 if (bufIsChanged(wp->w_buffer))
10245 modified = TRUE;
10246 if (modified || wincount > 1)
10247 {
10248 if (wincount > 1)
10249 {
10250 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010251 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010252 if (col + len >= Columns - 3)
10253 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010254 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010255#if defined(FEAT_SYN_HL)
Bram Moolenaare0f14822014-08-06 13:20:56 +020010256 hl_combine_attr(attr, hl_attr(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010257#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010258 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010259#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010260 );
10261 col += len;
10262 }
10263 if (modified)
10264 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10265 screen_putchar(' ', 0, col++, attr);
10266 }
10267
10268 room = scol - col + tabwidth - 1;
10269 if (room > 0)
10270 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010271 /* Get buffer name in NameBuff[] */
10272 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010273 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010274 len = vim_strsize(NameBuff);
10275 p = NameBuff;
10276#ifdef FEAT_MBYTE
10277 if (has_mbyte)
10278 while (len > room)
10279 {
10280 len -= ptr2cells(p);
10281 mb_ptr_adv(p);
10282 }
10283 else
10284#endif
10285 if (len > room)
10286 {
10287 p += len - room;
10288 len = room;
10289 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010290 if (len > Columns - col - 1)
10291 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010292
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010293 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010294 col += len;
10295 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010296 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010297
10298 /* Store the tab page number in TabPageIdxs[], so that
10299 * jump_to_mouse() knows where each one is. */
10300 ++tabcount;
10301 while (scol < col)
10302 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010303 }
10304
Bram Moolenaar238a5642006-02-21 22:12:05 +000010305 if (use_sep_chars)
10306 c = '_';
10307 else
10308 c = ' ';
10309 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010310
10311 /* Put an "X" for closing the current tab if there are several. */
10312 if (first_tabpage->tp_next != NULL)
10313 {
10314 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10315 TabPageIdxs[Columns - 1] = -999;
10316 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010317 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010318
10319 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10320 * set. */
10321 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010322}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010323
10324/*
10325 * Get buffer name for "buf" into NameBuff[].
10326 * Takes care of special buffer names and translates special characters.
10327 */
10328 void
10329get_trans_bufname(buf)
10330 buf_T *buf;
10331{
10332 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010333 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010334 else
10335 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10336 trans_characters(NameBuff, MAXPATHL);
10337}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010338#endif
10339
Bram Moolenaar071d4272004-06-13 20:20:40 +000010340#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
10341/*
10342 * Get the character to use in a status line. Get its attributes in "*attr".
10343 */
10344 static int
10345fillchar_status(attr, is_curwin)
10346 int *attr;
10347 int is_curwin;
10348{
10349 int fill;
10350 if (is_curwin)
10351 {
10352 *attr = hl_attr(HLF_S);
10353 fill = fill_stl;
10354 }
10355 else
10356 {
10357 *attr = hl_attr(HLF_SNC);
10358 fill = fill_stlnc;
10359 }
10360 /* Use fill when there is highlighting, and highlighting of current
10361 * window differs, or the fillchars differ, or this is not the
10362 * current window */
10363 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
10364 || !is_curwin || firstwin == lastwin)
10365 || (fill_stl != fill_stlnc)))
10366 return fill;
10367 if (is_curwin)
10368 return '^';
10369 return '=';
10370}
10371#endif
10372
10373#ifdef FEAT_VERTSPLIT
10374/*
10375 * Get the character to use in a separator between vertically split windows.
10376 * Get its attributes in "*attr".
10377 */
10378 static int
10379fillchar_vsep(attr)
10380 int *attr;
10381{
10382 *attr = hl_attr(HLF_C);
10383 if (*attr == 0 && fill_vert == ' ')
10384 return '|';
10385 else
10386 return fill_vert;
10387}
10388#endif
10389
10390/*
10391 * Return TRUE if redrawing should currently be done.
10392 */
10393 int
10394redrawing()
10395{
10396 return (!RedrawingDisabled
10397 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
10398}
10399
10400/*
10401 * Return TRUE if printing messages should currently be done.
10402 */
10403 int
10404messaging()
10405{
10406 return (!(p_lz && char_avail() && !KeyTyped));
10407}
10408
10409/*
10410 * Show current status info in ruler and various other places
10411 * If always is FALSE, only show ruler if position has changed.
10412 */
10413 void
10414showruler(always)
10415 int always;
10416{
10417 if (!always && !redrawing())
10418 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010419#ifdef FEAT_INS_EXPAND
10420 if (pum_visible())
10421 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010422# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +000010423 /* Don't redraw right now, do it later. */
10424 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010425# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +000010426 return;
10427 }
10428#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010429#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010430 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010431 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010432 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010434 else
10435#endif
10436#ifdef FEAT_CMDL_INFO
10437 win_redr_ruler(curwin, always);
10438#endif
10439
10440#ifdef FEAT_TITLE
10441 if (need_maketitle
10442# ifdef FEAT_STL_OPT
10443 || (p_icon && (stl_syntax & STL_IN_ICON))
10444 || (p_title && (stl_syntax & STL_IN_TITLE))
10445# endif
10446 )
10447 maketitle();
10448#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010449#ifdef FEAT_WINDOWS
10450 /* Redraw the tab pages line if needed. */
10451 if (redraw_tabline)
10452 draw_tabline();
10453#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010454}
10455
10456#ifdef FEAT_CMDL_INFO
10457 static void
10458win_redr_ruler(wp, always)
10459 win_T *wp;
10460 int always;
10461{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010462#define RULER_BUF_LEN 70
10463 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010464 int row;
10465 int fillchar;
10466 int attr;
10467 int empty_line = FALSE;
10468 colnr_T virtcol;
10469 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010470 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010471 int o;
10472#ifdef FEAT_VERTSPLIT
10473 int this_ru_col;
10474 int off = 0;
10475 int width = Columns;
10476# define WITH_OFF(x) x
10477# define WITH_WIDTH(x) x
10478#else
10479# define WITH_OFF(x) 0
10480# define WITH_WIDTH(x) Columns
10481# define this_ru_col ru_col
10482#endif
10483
10484 /* If 'ruler' off or redrawing disabled, don't do anything */
10485 if (!p_ru)
10486 return;
10487
10488 /*
10489 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10490 * after deleting lines, before cursor.lnum is corrected.
10491 */
10492 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10493 return;
10494
10495#ifdef FEAT_INS_EXPAND
10496 /* Don't draw the ruler while doing insert-completion, it might overwrite
10497 * the (long) mode message. */
10498# ifdef FEAT_WINDOWS
10499 if (wp == lastwin && lastwin->w_status_height == 0)
10500# endif
10501 if (edit_submode != NULL)
10502 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010503 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10504 if (pum_visible())
10505 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010506#endif
10507
10508#ifdef FEAT_STL_OPT
10509 if (*p_ruf)
10510 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010511 int save_called_emsg = called_emsg;
10512
10513 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010514 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010515 if (called_emsg)
10516 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010517 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010518 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010519 return;
10520 }
10521#endif
10522
10523 /*
10524 * Check if not in Insert mode and the line is empty (will show "0-1").
10525 */
10526 if (!(State & INSERT)
10527 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10528 empty_line = TRUE;
10529
10530 /*
10531 * Only draw the ruler when something changed.
10532 */
10533 validate_virtcol_win(wp);
10534 if ( redraw_cmdline
10535 || always
10536 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10537 || wp->w_cursor.col != wp->w_ru_cursor.col
10538 || wp->w_virtcol != wp->w_ru_virtcol
10539#ifdef FEAT_VIRTUALEDIT
10540 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10541#endif
10542 || wp->w_topline != wp->w_ru_topline
10543 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10544#ifdef FEAT_DIFF
10545 || wp->w_topfill != wp->w_ru_topfill
10546#endif
10547 || empty_line != wp->w_ru_empty)
10548 {
10549 cursor_off();
10550#ifdef FEAT_WINDOWS
10551 if (wp->w_status_height)
10552 {
10553 row = W_WINROW(wp) + wp->w_height;
10554 fillchar = fillchar_status(&attr, wp == curwin);
10555# ifdef FEAT_VERTSPLIT
10556 off = W_WINCOL(wp);
10557 width = W_WIDTH(wp);
10558# endif
10559 }
10560 else
10561#endif
10562 {
10563 row = Rows - 1;
10564 fillchar = ' ';
10565 attr = 0;
10566#ifdef FEAT_VERTSPLIT
10567 width = Columns;
10568 off = 0;
10569#endif
10570 }
10571
10572 /* In list mode virtcol needs to be recomputed */
10573 virtcol = wp->w_virtcol;
10574 if (wp->w_p_list && lcs_tab1 == NUL)
10575 {
10576 wp->w_p_list = FALSE;
10577 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10578 wp->w_p_list = TRUE;
10579 }
10580
10581 /*
10582 * Some sprintfs return the length, some return a pointer.
10583 * To avoid portability problems we use strlen() here.
10584 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010585 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010586 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10587 ? 0L
10588 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010589 len = STRLEN(buffer);
10590 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010591 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10592 (int)virtcol + 1);
10593
10594 /*
10595 * Add a "50%" if there is room for it.
10596 * On the last line, don't print in the last column (scrolls the
10597 * screen up on some terminals).
10598 */
10599 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010600 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010601 o = i + vim_strsize(buffer + i + 1);
10602#ifdef FEAT_WINDOWS
10603 if (wp->w_status_height == 0) /* can't use last char of screen */
10604#endif
10605 ++o;
10606#ifdef FEAT_VERTSPLIT
10607 this_ru_col = ru_col - (Columns - width);
10608 if (this_ru_col < 0)
10609 this_ru_col = 0;
10610#endif
10611 /* Never use more than half the window/screen width, leave the other
10612 * half for the filename. */
10613 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
10614 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
10615 if (this_ru_col + o < WITH_WIDTH(width))
10616 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010010617 /* need at least 3 chars left for get_rel_pos() + NUL */
10618 while (this_ru_col + o < WITH_WIDTH(width) && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010619 {
10620#ifdef FEAT_MBYTE
10621 if (has_mbyte)
10622 i += (*mb_char2bytes)(fillchar, buffer + i);
10623 else
10624#endif
10625 buffer[i++] = fillchar;
10626 ++o;
10627 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010628 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010629 }
10630 /* Truncate at window boundary. */
10631#ifdef FEAT_MBYTE
10632 if (has_mbyte)
10633 {
10634 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010635 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010636 {
10637 o += (*mb_ptr2cells)(buffer + i);
10638 if (this_ru_col + o > WITH_WIDTH(width))
10639 {
10640 buffer[i] = NUL;
10641 break;
10642 }
10643 }
10644 }
10645 else
10646#endif
10647 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
10648 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
10649
10650 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
10651 i = redraw_cmdline;
10652 screen_fill(row, row + 1,
10653 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
10654 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
10655 fillchar, fillchar, attr);
10656 /* don't redraw the cmdline because of showing the ruler */
10657 redraw_cmdline = i;
10658 wp->w_ru_cursor = wp->w_cursor;
10659 wp->w_ru_virtcol = wp->w_virtcol;
10660 wp->w_ru_empty = empty_line;
10661 wp->w_ru_topline = wp->w_topline;
10662 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10663#ifdef FEAT_DIFF
10664 wp->w_ru_topfill = wp->w_topfill;
10665#endif
10666 }
10667}
10668#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010669
10670#if defined(FEAT_LINEBREAK) || defined(PROTO)
10671/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010672 * Return the width of the 'number' and 'relativenumber' column.
10673 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010674 * Otherwise it depends on 'numberwidth' and the line count.
10675 */
10676 int
10677number_width(wp)
10678 win_T *wp;
10679{
10680 int n;
10681 linenr_T lnum;
10682
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020010683 if (wp->w_p_rnu && !wp->w_p_nu)
10684 /* cursor line shows "0" */
10685 lnum = wp->w_height;
10686 else
10687 /* cursor line shows absolute line number */
10688 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020010689
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010690 if (lnum == wp->w_nrwidth_line_count)
10691 return wp->w_nrwidth_width;
10692 wp->w_nrwidth_line_count = lnum;
10693
10694 n = 0;
10695 do
10696 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010697 lnum /= 10;
10698 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010699 } while (lnum > 0);
10700
10701 /* 'numberwidth' gives the minimal width plus one */
10702 if (n < wp->w_p_nuw - 1)
10703 n = wp->w_p_nuw - 1;
10704
10705 wp->w_nrwidth_width = n;
10706 return n;
10707}
10708#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010709
10710/*
10711 * Return the current cursor column. This is the actual position on the
10712 * screen. First column is 0.
10713 */
10714 int
10715screen_screencol()
10716{
10717 return screen_cur_col;
10718}
10719
10720/*
10721 * Return the current cursor row. This is the actual position on the screen.
10722 * First row is 0.
10723 */
10724 int
10725screen_screenrow()
10726{
10727 return screen_cur_row;
10728}