blob: b767ec3c15ab029d3ec210639114eabfe6703e97 [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 Moolenaarac550fd2010-07-18 13:55:02 +02003006# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003007# define FIX_FOR_BOGUSCOLS \
3008 { \
3009 n_extra += vcol_off; \
3010 vcol -= vcol_off; \
3011 vcol_off = 0; \
3012 col -= boguscols; \
3013 boguscols = 0; \
3014 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003015#else
3016# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003017#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018
3019 if (startrow > endrow) /* past the end already! */
3020 return startrow;
3021
3022 row = startrow;
3023 screen_row = row + W_WINROW(wp);
3024
3025 /*
3026 * To speed up the loop below, set extra_check when there is linebreak,
3027 * trailing white space and/or syntax processing to be done.
3028 */
3029#ifdef FEAT_LINEBREAK
3030 extra_check = wp->w_p_lbr;
3031#else
3032 extra_check = 0;
3033#endif
3034#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02003035 if (syntax_present(wp) && !wp->w_s->b_syn_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 {
3037 /* Prepare for syntax highlighting in this line. When there is an
3038 * error, stop syntax highlighting. */
3039 save_did_emsg = did_emsg;
3040 did_emsg = FALSE;
3041 syntax_start(wp, lnum);
3042 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003043 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 else
3045 {
3046 did_emsg = save_did_emsg;
3047 has_syntax = TRUE;
3048 extra_check = TRUE;
3049 }
3050 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003051
3052 /* Check for columns to display for 'colorcolumn'. */
3053 color_cols = wp->w_p_cc_cols;
3054 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003055 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003056#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003057
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003058#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003059 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02003060 && *wp->w_s->b_p_spl != NUL
3061 && wp->w_s->b_langp.ga_len > 0
3062 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003063 {
3064 /* Prepare for spell checking. */
3065 has_spell = TRUE;
3066 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003067
3068 /* Get the start of the next line, so that words that wrap to the next
3069 * line are found too: "et<line-break>al.".
3070 * Trick: skip a few chars for C/shell/Vim comments */
3071 nextline[SPWORDLEN] = NUL;
3072 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3073 {
3074 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3075 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3076 }
3077
3078 /* When a word wrapped from the previous line the start of the current
3079 * line is valid. */
3080 if (lnum == checked_lnum)
3081 cur_checked_col = checked_col;
3082 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003083
3084 /* When there was a sentence end in the previous line may require a
3085 * word starting with capital in this line. In line 1 always check
3086 * the first word. */
3087 if (lnum != capcol_lnum)
3088 cap_col = -1;
3089 if (lnum == 1)
3090 cap_col = 0;
3091 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003092 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093#endif
3094
3095 /*
3096 * handle visual active in this window
3097 */
3098 fromcol = -10;
3099 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3101 {
3102 /* Visual is after curwin->w_cursor */
3103 if (ltoreq(curwin->w_cursor, VIsual))
3104 {
3105 top = &curwin->w_cursor;
3106 bot = &VIsual;
3107 }
3108 else /* Visual is before curwin->w_cursor */
3109 {
3110 top = &VIsual;
3111 bot = &curwin->w_cursor;
3112 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003113 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 if (VIsual_mode == Ctrl_V) /* block mode */
3115 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003116 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 {
3118 fromcol = wp->w_old_cursor_fcol;
3119 tocol = wp->w_old_cursor_lcol;
3120 }
3121 }
3122 else /* non-block mode */
3123 {
3124 if (lnum > top->lnum && lnum <= bot->lnum)
3125 fromcol = 0;
3126 else if (lnum == top->lnum)
3127 {
3128 if (VIsual_mode == 'V') /* linewise */
3129 fromcol = 0;
3130 else
3131 {
3132 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3133 if (gchar_pos(top) == NUL)
3134 tocol = fromcol + 1;
3135 }
3136 }
3137 if (VIsual_mode != 'V' && lnum == bot->lnum)
3138 {
3139 if (*p_sel == 'e' && bot->col == 0
3140#ifdef FEAT_VIRTUALEDIT
3141 && bot->coladd == 0
3142#endif
3143 )
3144 {
3145 fromcol = -10;
3146 tocol = MAXCOL;
3147 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003148 else if (bot->col == MAXCOL)
3149 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 else
3151 {
3152 pos = *bot;
3153 if (*p_sel == 'e')
3154 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3155 else
3156 {
3157 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3158 ++tocol;
3159 }
3160 }
3161 }
3162 }
3163
3164#ifndef MSDOS
3165 /* Check if the character under the cursor should not be inverted */
3166 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
3167# ifdef FEAT_GUI
3168 && !gui.in_use
3169# endif
3170 )
3171 noinvcur = TRUE;
3172#endif
3173
3174 /* if inverting in this line set area_highlighting */
3175 if (fromcol >= 0)
3176 {
3177 area_highlighting = TRUE;
3178 attr = hl_attr(HLF_V);
3179#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003180 if ((clip_star.available && !clip_star.owned
3181 && clip_isautosel_star())
3182 || (clip_plus.available && !clip_plus.owned
3183 && clip_isautosel_plus()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 attr = hl_attr(HLF_VNC);
3185#endif
3186 }
3187 }
3188
3189 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003190 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003192 else if (highlight_match
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 && wp == curwin
3194 && lnum >= curwin->w_cursor.lnum
3195 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3196 {
3197 if (lnum == curwin->w_cursor.lnum)
3198 getvcol(curwin, &(curwin->w_cursor),
3199 (colnr_T *)&fromcol, NULL, NULL);
3200 else
3201 fromcol = 0;
3202 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3203 {
3204 pos.lnum = lnum;
3205 pos.col = search_match_endcol;
3206 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3207 }
3208 else
3209 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003210 /* do at least one character; happens when past end of line */
3211 if (fromcol == tocol)
3212 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 area_highlighting = TRUE;
3214 attr = hl_attr(HLF_I);
3215 }
3216
3217#ifdef FEAT_DIFF
3218 filler_lines = diff_check(wp, lnum);
3219 if (filler_lines < 0)
3220 {
3221 if (filler_lines == -1)
3222 {
3223 if (diff_find_change(wp, lnum, &change_start, &change_end))
3224 diff_hlf = HLF_ADD; /* added line */
3225 else if (change_start == 0)
3226 diff_hlf = HLF_TXD; /* changed text */
3227 else
3228 diff_hlf = HLF_CHD; /* changed line */
3229 }
3230 else
3231 diff_hlf = HLF_ADD; /* added line */
3232 filler_lines = 0;
3233 area_highlighting = TRUE;
3234 }
3235 if (lnum == wp->w_topline)
3236 filler_lines = wp->w_topfill;
3237 filler_todo = filler_lines;
3238#endif
3239
3240#ifdef LINE_ATTR
3241# ifdef FEAT_SIGNS
3242 /* If this line has a sign with line highlighting set line_attr. */
3243 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3244 if (v != 0)
3245 line_attr = sign_get_attr((int)v, TRUE);
3246# endif
3247# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
3248 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003249 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 line_attr = hl_attr(HLF_L);
3251# endif
3252 if (line_attr != 0)
3253 area_highlighting = TRUE;
3254#endif
3255
3256 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3257 ptr = line;
3258
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003259#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003260 if (has_spell)
3261 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003262 /* For checking first word with a capital skip white space. */
3263 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003264 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003265
Bram Moolenaar30abd282005-06-22 22:35:10 +00003266 /* To be able to spell-check over line boundaries copy the end of the
3267 * current line into nextline[]. Above the start of the next line was
3268 * copied to nextline[SPWORDLEN]. */
3269 if (nextline[SPWORDLEN] == NUL)
3270 {
3271 /* No next line or it is empty. */
3272 nextlinecol = MAXCOL;
3273 nextline_idx = 0;
3274 }
3275 else
3276 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003277 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003278 if (v < SPWORDLEN)
3279 {
3280 /* Short line, use it completely and append the start of the
3281 * next line. */
3282 nextlinecol = 0;
3283 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003284 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003285 nextline_idx = v + 1;
3286 }
3287 else
3288 {
3289 /* Long line, use only the last SPWORDLEN bytes. */
3290 nextlinecol = v - SPWORDLEN;
3291 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3292 nextline_idx = SPWORDLEN + 1;
3293 }
3294 }
3295 }
3296#endif
3297
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 /* find start of trailing whitespace */
3299 if (wp->w_p_list && lcs_trail)
3300 {
3301 trailcol = (colnr_T)STRLEN(ptr);
3302 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
3303 --trailcol;
3304 trailcol += (colnr_T) (ptr - line);
3305 extra_check = TRUE;
3306 }
3307
3308 /*
3309 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3310 * first character to be displayed.
3311 */
3312 if (wp->w_p_wrap)
3313 v = wp->w_skipcol;
3314 else
3315 v = wp->w_leftcol;
3316 if (v > 0)
3317 {
3318#ifdef FEAT_MBYTE
3319 char_u *prev_ptr = ptr;
3320#endif
3321 while (vcol < v && *ptr != NUL)
3322 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003323 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 vcol += c;
3325#ifdef FEAT_MBYTE
3326 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003328 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 }
3330
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003331 /* When:
3332 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003333 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003334 * - 'virtualedit' is set, or
3335 * - the visual mode is active,
3336 * the end of the line may be before the start of the displayed part.
3337 */
3338 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003339#ifdef FEAT_SYN_HL
3340 wp->w_p_cuc || draw_color_col ||
3341#endif
3342#ifdef FEAT_VIRTUALEDIT
3343 virtual_active() ||
3344#endif
3345 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003346 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349
3350 /* Handle a character that's not completely on the screen: Put ptr at
3351 * that character but skip the first few screen characters. */
3352 if (vcol > v)
3353 {
3354 vcol -= c;
3355#ifdef FEAT_MBYTE
3356 ptr = prev_ptr;
3357#else
3358 --ptr;
3359#endif
3360 n_skip = v - vcol;
3361 }
3362
3363 /*
3364 * Adjust for when the inverted text is before the screen,
3365 * and when the start of the inverted text is before the screen.
3366 */
3367 if (tocol <= vcol)
3368 fromcol = 0;
3369 else if (fromcol >= 0 && fromcol < vcol)
3370 fromcol = vcol;
3371
3372#ifdef FEAT_LINEBREAK
3373 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3374 if (wp->w_p_wrap)
3375 need_showbreak = TRUE;
3376#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003377#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003378 /* When spell checking a word we need to figure out the start of the
3379 * word and if it's badly spelled or not. */
3380 if (has_spell)
3381 {
3382 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003383 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003384 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003385
3386 pos = wp->w_cursor;
3387 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003388 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003389 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003390
3391 /* spell_move_to() may call ml_get() and make "line" invalid */
3392 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3393 ptr = line + linecol;
3394
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003395 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003396 {
3397 /* no bad word found at line start, don't check until end of a
3398 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003399 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003400 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003401 }
3402 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003403 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003404 /* bad word found, use attributes until end of word */
3405 word_end = wp->w_cursor.col + len + 1;
3406
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003407 /* Turn index into actual attributes. */
3408 if (spell_hlf != HLF_COUNT)
3409 spell_attr = highlight_attr[spell_hlf];
3410 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003411 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003412
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003413# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003414 /* Need to restart syntax highlighting for this line. */
3415 if (has_syntax)
3416 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003417# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003418 }
3419#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 }
3421
3422 /*
3423 * Correct highlighting for cursor that can't be disabled.
3424 * Avoids having to check this for each character.
3425 */
3426 if (fromcol >= 0)
3427 {
3428 if (noinvcur)
3429 {
3430 if ((colnr_T)fromcol == wp->w_virtcol)
3431 {
3432 /* highlighting starts at cursor, let it start just after the
3433 * cursor */
3434 fromcol_prev = fromcol;
3435 fromcol = -1;
3436 }
3437 else if ((colnr_T)fromcol < wp->w_virtcol)
3438 /* restart highlighting after the cursor */
3439 fromcol_prev = wp->w_virtcol;
3440 }
3441 if (fromcol >= tocol)
3442 fromcol = -1;
3443 }
3444
3445#ifdef FEAT_SEARCH_EXTRA
3446 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003447 * Handle highlighting the last used search pattern and matches.
3448 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003450 cur = wp->w_match_head;
3451 shl_flag = FALSE;
3452 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003454 if (shl_flag == FALSE)
3455 {
3456 shl = &search_hl;
3457 shl_flag = TRUE;
3458 }
3459 else
3460 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003461 shl->startcol = MAXCOL;
3462 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 shl->attr_cur = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003464 v = (long)(ptr - line);
3465 if (cur != NULL)
3466 cur->pos.cur = 0;
3467 next_search_hl(wp, shl, lnum, (colnr_T)v, cur);
3468
3469 /* Need to get the line again, a multi-line regexp may have made it
3470 * invalid. */
3471 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3472 ptr = line + v;
3473
3474 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003476 if (shl->lnum == lnum)
3477 shl->startcol = shl->rm.startpos[0].col;
3478 else
3479 shl->startcol = 0;
3480 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3481 - shl->rm.startpos[0].lnum)
3482 shl->endcol = shl->rm.endpos[0].col;
3483 else
3484 shl->endcol = MAXCOL;
3485 /* Highlight one character for an empty match. */
3486 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003489 if (has_mbyte && line[shl->endcol] != NUL)
3490 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3491 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003493 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003495 if ((long)shl->startcol < v) /* match at leftcol */
3496 {
3497 shl->attr_cur = shl->attr;
3498 search_attr = shl->attr;
3499 }
3500 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003502 if (shl != &search_hl && cur != NULL)
3503 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 }
3505#endif
3506
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003507#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003508 /* Cursor line highlighting for 'cursorline' in the current window. Not
3509 * when Visual mode is active, because it's not clear what is selected
3510 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003511 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003512 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003513 {
3514 line_attr = hl_attr(HLF_CUL);
3515 area_highlighting = TRUE;
3516 }
3517#endif
3518
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003519 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 col = 0;
3521#ifdef FEAT_RIGHTLEFT
3522 if (wp->w_p_rl)
3523 {
3524 /* Rightleft window: process the text in the normal direction, but put
3525 * it in current_ScreenLine[] from right to left. Start at the
3526 * rightmost column of the window. */
3527 col = W_WIDTH(wp) - 1;
3528 off += col;
3529 }
3530#endif
3531
3532 /*
3533 * Repeat for the whole displayed line.
3534 */
3535 for (;;)
3536 {
3537 /* Skip this quickly when working on the text. */
3538 if (draw_state != WL_LINE)
3539 {
3540#ifdef FEAT_CMDWIN
3541 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3542 {
3543 draw_state = WL_CMDLINE;
3544 if (cmdwin_type != 0 && wp == curwin)
3545 {
3546 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003548 c_extra = cmdwin_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 char_attr = hl_attr(HLF_AT);
3550 }
3551 }
3552#endif
3553
3554#ifdef FEAT_FOLDING
3555 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3556 {
3557 draw_state = WL_FOLD;
3558 if (wp->w_p_fdc > 0)
3559 {
3560 /* Draw the 'foldcolumn'. */
3561 fill_foldcolumn(extra, wp, FALSE, lnum);
3562 n_extra = wp->w_p_fdc;
3563 p_extra = extra;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003564 p_extra[n_extra] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 c_extra = NUL;
3566 char_attr = hl_attr(HLF_FC);
3567 }
3568 }
3569#endif
3570
3571#ifdef FEAT_SIGNS
3572 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3573 {
3574 draw_state = WL_SIGN;
3575 /* Show the sign column when there are any signs in this
3576 * buffer or when using Netbeans. */
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003577 if (draw_signcolumn(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003579 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003581 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582# endif
3583
3584 /* Draw two cells with the sign value or blank. */
3585 c_extra = ' ';
3586 char_attr = hl_attr(HLF_SC);
3587 n_extra = 2;
3588
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003589 if (row == startrow
3590#ifdef FEAT_DIFF
3591 + filler_lines && filler_todo <= 0
3592#endif
3593 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 {
3595 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3596 SIGN_TEXT);
3597# ifdef FEAT_SIGN_ICONS
3598 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3599 SIGN_ICON);
3600 if (gui.in_use && icon_sign != 0)
3601 {
3602 /* Use the image in this position. */
3603 c_extra = SIGN_BYTE;
3604# ifdef FEAT_NETBEANS_INTG
3605 if (buf_signcount(wp->w_buffer, lnum) > 1)
3606 c_extra = MULTISIGN_BYTE;
3607# endif
3608 char_attr = icon_sign;
3609 }
3610 else
3611# endif
3612 if (text_sign != 0)
3613 {
3614 p_extra = sign_get_text(text_sign);
3615 if (p_extra != NULL)
3616 {
3617 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003618 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 }
3620 char_attr = sign_get_attr(text_sign, FALSE);
3621 }
3622 }
3623 }
3624 }
3625#endif
3626
3627 if (draw_state == WL_NR - 1 && n_extra == 0)
3628 {
3629 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003630 /* Display the absolute or relative line number. After the
3631 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3632 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 && (row == startrow
3634#ifdef FEAT_DIFF
3635 + filler_lines
3636#endif
3637 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3638 {
3639 /* Draw the line number (empty space after wrapping). */
3640 if (row == startrow
3641#ifdef FEAT_DIFF
3642 + filler_lines
3643#endif
3644 )
3645 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003646 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003647 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003648
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003649 if (wp->w_p_nu && !wp->w_p_rnu)
3650 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003651 num = (long)lnum;
3652 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003653 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003654 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003655 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003656 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003657 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003658 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003659 num = lnum;
3660 fmt = "%-*ld ";
3661 }
3662 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003663
Bram Moolenaar700e7342013-01-30 12:31:36 +01003664 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003665 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 if (wp->w_skipcol > 0)
3667 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3668 *p_extra = '-';
3669#ifdef FEAT_RIGHTLEFT
3670 if (wp->w_p_rl) /* reverse line numbers */
3671 rl_mirror(extra);
3672#endif
3673 p_extra = extra;
3674 c_extra = NUL;
3675 }
3676 else
3677 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003678 n_extra = number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 char_attr = hl_attr(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003680#ifdef FEAT_SYN_HL
3681 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003682 * the current line differently.
3683 * TODO: Can we use CursorLine instead of CursorLineNr
3684 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003685 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003686 && lnum == wp->w_cursor.lnum)
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003687 char_attr = hl_attr(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003688#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 }
3690 }
3691
Bram Moolenaar597a4222014-06-25 14:39:50 +02003692#ifdef FEAT_LINEBREAK
3693 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3694 && n_extra == 0 && *p_sbr != NUL)
3695 /* draw indent after showbreak value */
3696 draw_state = WL_BRI;
3697 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3698 /* After the showbreak, draw the breakindent */
3699 draw_state = WL_BRI - 1;
3700
3701 /* draw 'breakindent': indent wrapped text accordingly */
3702 if (draw_state == WL_BRI - 1 && n_extra == 0)
3703 {
3704 draw_state = WL_BRI;
3705# ifdef FEAT_DIFF
3706# endif
3707 if (wp->w_p_bri && n_extra == 0 && row != startrow
3708#ifdef FEAT_DIFF
3709 && filler_lines == 0
3710#endif
3711 )
3712 {
3713 char_attr = 0; /* was: hl_attr(HLF_AT); */
3714#ifdef FEAT_DIFF
3715 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003716 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003717 char_attr = hl_attr(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02003718 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3719 char_attr = hl_combine_attr(char_attr,
3720 hl_attr(HLF_CUL));
3721 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02003722#endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02003723 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003724 c_extra = ' ';
3725 n_extra = get_breakindent_win(wp,
3726 ml_get_buf(wp->w_buffer, lnum, FALSE));
3727 /* Correct end of highlighted area for 'breakindent',
3728 * required when 'linebreak' is also set. */
3729 if (tocol == vcol)
3730 tocol += n_extra;
3731 }
3732 }
3733#endif
3734
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3736 if (draw_state == WL_SBR - 1 && n_extra == 0)
3737 {
3738 draw_state = WL_SBR;
3739# ifdef FEAT_DIFF
3740 if (filler_todo > 0)
3741 {
3742 /* Draw "deleted" diff line(s). */
3743 if (char2cells(fill_diff) > 1)
3744 c_extra = '-';
3745 else
3746 c_extra = fill_diff;
3747# ifdef FEAT_RIGHTLEFT
3748 if (wp->w_p_rl)
3749 n_extra = col + 1;
3750 else
3751# endif
3752 n_extra = W_WIDTH(wp) - col;
3753 char_attr = hl_attr(HLF_DED);
3754 }
3755# endif
3756# ifdef FEAT_LINEBREAK
3757 if (*p_sbr != NUL && need_showbreak)
3758 {
3759 /* Draw 'showbreak' at the start of each broken line. */
3760 p_extra = p_sbr;
3761 c_extra = NUL;
3762 n_extra = (int)STRLEN(p_sbr);
3763 char_attr = hl_attr(HLF_AT);
3764 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01003765 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 /* Correct end of highlighted area for 'showbreak',
3767 * required when 'linebreak' is also set. */
3768 if (tocol == vcol)
3769 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003770#ifdef FEAT_SYN_HL
3771 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003772 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003773 char_attr = hl_combine_attr(char_attr,
3774 hl_attr(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003775#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 }
3777# endif
3778 }
3779#endif
3780
3781 if (draw_state == WL_LINE - 1 && n_extra == 0)
3782 {
3783 draw_state = WL_LINE;
3784 if (saved_n_extra)
3785 {
3786 /* Continue item from end of wrapped line. */
3787 n_extra = saved_n_extra;
3788 c_extra = saved_c_extra;
3789 p_extra = saved_p_extra;
3790 char_attr = saved_char_attr;
3791 }
3792 else
3793 char_attr = 0;
3794 }
3795 }
3796
3797 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01003798 if (dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003799 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800#ifdef FEAT_DIFF
3801 && filler_todo <= 0
3802#endif
3803 )
3804 {
3805 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3806 wp->w_p_rl);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003807 /* Pretend we have finished updating the window. Except when
3808 * 'cursorcolumn' is set. */
3809#ifdef FEAT_SYN_HL
3810 if (wp->w_p_cuc)
3811 row = wp->w_cline_row + wp->w_cline_height;
3812 else
3813#endif
3814 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 break;
3816 }
3817
3818 if (draw_state == WL_LINE && area_highlighting)
3819 {
3820 /* handle Visual or match highlighting in this line */
3821 if (vcol == fromcol
3822#ifdef FEAT_MBYTE
3823 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3824 && (*mb_ptr2cells)(ptr) > 1)
3825#endif
3826 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00003827 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 && vcol < tocol))
3829 area_attr = attr; /* start highlighting */
3830 else if (area_attr != 0
3831 && (vcol == tocol
3832 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834
3835#ifdef FEAT_SEARCH_EXTRA
3836 if (!n_extra)
3837 {
3838 /*
3839 * Check for start/end of search pattern match.
3840 * After end, check for start/end of next match.
3841 * When another match, have to check for start again.
3842 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003843 * Do this for 'search_hl' and the match list (ordered by
3844 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003846 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003847 cur = wp->w_match_head;
3848 shl_flag = FALSE;
3849 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003851 if (shl_flag == FALSE
3852 && ((cur != NULL
3853 && cur->priority > SEARCH_HL_PRIORITY)
3854 || cur == NULL))
3855 {
3856 shl = &search_hl;
3857 shl_flag = TRUE;
3858 }
3859 else
3860 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003861 if (cur != NULL)
3862 cur->pos.cur = 0;
3863 pos_inprogress = TRUE;
3864 while (shl->rm.regprog != NULL
3865 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003867 if (shl->startcol != MAXCOL
3868 && v >= (long)shl->startcol
3869 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01003871#ifdef FEAT_MBYTE
3872 int tmp_col = v + MB_PTR2LEN(ptr);
3873
3874 if (shl->endcol < tmp_col)
3875 shl->endcol = tmp_col;
3876#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 shl->attr_cur = shl->attr;
3878 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01003879 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 {
3881 shl->attr_cur = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003882 next_search_hl(wp, shl, lnum, (colnr_T)v, cur);
3883 pos_inprogress = cur == NULL || cur->pos.cur == 0
3884 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885
3886 /* Need to get the line again, a multi-line regexp
3887 * may have made it invalid. */
3888 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3889 ptr = line + v;
3890
3891 if (shl->lnum == lnum)
3892 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003893 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003895 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003897 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003899 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 {
3901 /* highlight empty match, try again after
3902 * it */
3903#ifdef FEAT_MBYTE
3904 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003905 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003906 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 else
3908#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003909 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 }
3911
3912 /* Loop to check if the match starts at the
3913 * current position */
3914 continue;
3915 }
3916 }
3917 break;
3918 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003919 if (shl != &search_hl && cur != NULL)
3920 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003922
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003923 /* Use attributes from match with highest priority among
3924 * 'search_hl' and the match list. */
3925 search_attr = search_hl.attr_cur;
3926 cur = wp->w_match_head;
3927 shl_flag = FALSE;
3928 while (cur != NULL || shl_flag == FALSE)
3929 {
3930 if (shl_flag == FALSE
3931 && ((cur != NULL
3932 && cur->priority > SEARCH_HL_PRIORITY)
3933 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003934 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003935 shl = &search_hl;
3936 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003937 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003938 else
3939 shl = &cur->hl;
3940 if (shl->attr_cur != 0)
3941 search_attr = shl->attr_cur;
3942 if (shl != &search_hl && cur != NULL)
3943 cur = cur->next;
3944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 }
3946#endif
3947
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003949 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003951 if (diff_hlf == HLF_CHD && ptr - line >= change_start
3952 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003954 if (diff_hlf == HLF_TXD && ptr - line > change_end
3955 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003957 line_attr = hl_attr(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02003958 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3959 line_attr = hl_combine_attr(line_attr, hl_attr(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 }
3961#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003962
3963 /* Decide which of the highlight attributes to use. */
3964 attr_pri = TRUE;
3965 if (area_attr != 0)
3966 char_attr = area_attr;
3967 else if (search_attr != 0)
3968 char_attr = search_attr;
3969#ifdef LINE_ATTR
3970 /* Use line_attr when not in the Visual or 'incsearch' area
3971 * (area_attr may be 0 when "noinvcur" is set). */
3972 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00003973 || vcol < fromcol || vcol_prev < fromcol_prev
3974 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003975 char_attr = line_attr;
3976#endif
3977 else
3978 {
3979 attr_pri = FALSE;
3980#ifdef FEAT_SYN_HL
3981 if (has_syntax)
3982 char_attr = syntax_attr;
3983 else
3984#endif
3985 char_attr = 0;
3986 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 }
3988
3989 /*
3990 * Get the next character to put on the screen.
3991 */
3992 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00003993 * The "p_extra" points to the extra stuff that is inserted to
3994 * represent special characters (non-printable stuff) and other
3995 * things. When all characters are the same, c_extra is used.
3996 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
3997 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
3999 */
4000 if (n_extra > 0)
4001 {
4002 if (c_extra != NUL)
4003 {
4004 c = c_extra;
4005#ifdef FEAT_MBYTE
4006 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
4007 if (enc_utf8 && (*mb_char2len)(c) > 1)
4008 {
4009 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004010 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004011 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 }
4013 else
4014 mb_utf8 = FALSE;
4015#endif
4016 }
4017 else
4018 {
4019 c = *p_extra;
4020#ifdef FEAT_MBYTE
4021 if (has_mbyte)
4022 {
4023 mb_c = c;
4024 if (enc_utf8)
4025 {
4026 /* If the UTF-8 character is more than one byte:
4027 * Decode it into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004028 mb_l = (*mb_ptr2len)(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 mb_utf8 = FALSE;
4030 if (mb_l > n_extra)
4031 mb_l = 1;
4032 else if (mb_l > 1)
4033 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004034 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004036 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 }
4038 }
4039 else
4040 {
4041 /* if this is a DBCS character, put it in "mb_c" */
4042 mb_l = MB_BYTE2LEN(c);
4043 if (mb_l >= n_extra)
4044 mb_l = 1;
4045 else if (mb_l > 1)
4046 mb_c = (c << 8) + p_extra[1];
4047 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004048 if (mb_l == 0) /* at the NUL at end-of-line */
4049 mb_l = 1;
4050
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 /* If a double-width char doesn't fit display a '>' in the
4052 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004053 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054# ifdef FEAT_RIGHTLEFT
4055 wp->w_p_rl ? (col <= 0) :
4056# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004057 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 && (*mb_char2cells)(mb_c) == 2)
4059 {
4060 c = '>';
4061 mb_c = c;
4062 mb_l = 1;
4063 mb_utf8 = FALSE;
4064 multi_attr = hl_attr(HLF_AT);
4065 /* put the pointer back to output the double-width
4066 * character at the start of the next line. */
4067 ++n_extra;
4068 --p_extra;
4069 }
4070 else
4071 {
4072 n_extra -= mb_l - 1;
4073 p_extra += mb_l - 1;
4074 }
4075 }
4076#endif
4077 ++p_extra;
4078 }
4079 --n_extra;
4080 }
4081 else
4082 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004083 if (p_extra_free != NULL)
4084 {
4085 vim_free(p_extra_free);
4086 p_extra_free = NULL;
4087 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 /*
4089 * Get a character from the line itself.
4090 */
4091 c = *ptr;
4092#ifdef FEAT_MBYTE
4093 if (has_mbyte)
4094 {
4095 mb_c = c;
4096 if (enc_utf8)
4097 {
4098 /* If the UTF-8 character is more than one byte: Decode it
4099 * into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004100 mb_l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 mb_utf8 = FALSE;
4102 if (mb_l > 1)
4103 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004104 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 /* Overlong encoded ASCII or ASCII with composing char
4106 * is displayed normally, except a NUL. */
4107 if (mb_c < 0x80)
4108 c = mb_c;
4109 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004110
4111 /* At start of the line we can have a composing char.
4112 * Draw it as a space with a composing char. */
4113 if (utf_iscomposing(mb_c))
4114 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004115 int i;
4116
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004117 for (i = Screen_mco - 1; i > 0; --i)
4118 u8cc[i] = u8cc[i - 1];
4119 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004120 mb_c = ' ';
4121 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 }
4123
4124 if ((mb_l == 1 && c >= 0x80)
4125 || (mb_l >= 1 && mb_c == 0)
4126 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004127# ifdef UNICODE16
4128 || mb_c >= 0x10000
4129# endif
4130 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 {
4132 /*
4133 * Illegal UTF-8 byte: display as <xx>.
4134 * Non-BMP character : display as ? or fullwidth ?.
4135 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004136# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004138# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 {
4140 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004141# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 if (wp->w_p_rl) /* reverse */
4143 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004144# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004146# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 else if (utf_char2cells(mb_c) != 2)
4148 STRCPY(extra, "?");
4149 else
4150 /* 0xff1f in UTF-8: full-width '?' */
4151 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004152# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153
4154 p_extra = extra;
4155 c = *p_extra;
4156 mb_c = mb_ptr2char_adv(&p_extra);
4157 mb_utf8 = (c >= 0x80);
4158 n_extra = (int)STRLEN(p_extra);
4159 c_extra = NUL;
4160 if (area_attr == 0 && search_attr == 0)
4161 {
4162 n_attr = n_extra + 1;
4163 extra_attr = hl_attr(HLF_8);
4164 saved_attr2 = char_attr; /* save current attr */
4165 }
4166 }
4167 else if (mb_l == 0) /* at the NUL at end-of-line */
4168 mb_l = 1;
4169#ifdef FEAT_ARABIC
4170 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4171 {
4172 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004173 int pc, pc1, nc;
4174 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175
4176 /* The idea of what is the previous and next
4177 * character depends on 'rightleft'. */
4178 if (wp->w_p_rl)
4179 {
4180 pc = prev_c;
4181 pc1 = prev_c1;
4182 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004183 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 }
4185 else
4186 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004187 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004189 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 }
4191 prev_c = mb_c;
4192
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004193 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 }
4195 else
4196 prev_c = mb_c;
4197#endif
4198 }
4199 else /* enc_dbcs */
4200 {
4201 mb_l = MB_BYTE2LEN(c);
4202 if (mb_l == 0) /* at the NUL at end-of-line */
4203 mb_l = 1;
4204 else if (mb_l > 1)
4205 {
4206 /* We assume a second byte below 32 is illegal.
4207 * Hopefully this is OK for all double-byte encodings!
4208 */
4209 if (ptr[1] >= 32)
4210 mb_c = (c << 8) + ptr[1];
4211 else
4212 {
4213 if (ptr[1] == NUL)
4214 {
4215 /* head byte at end of line */
4216 mb_l = 1;
4217 transchar_nonprint(extra, c);
4218 }
4219 else
4220 {
4221 /* illegal tail byte */
4222 mb_l = 2;
4223 STRCPY(extra, "XX");
4224 }
4225 p_extra = extra;
4226 n_extra = (int)STRLEN(extra) - 1;
4227 c_extra = NUL;
4228 c = *p_extra++;
4229 if (area_attr == 0 && search_attr == 0)
4230 {
4231 n_attr = n_extra + 1;
4232 extra_attr = hl_attr(HLF_8);
4233 saved_attr2 = char_attr; /* save current attr */
4234 }
4235 mb_c = c;
4236 }
4237 }
4238 }
4239 /* If a double-width char doesn't fit display a '>' in the
4240 * last column; the character is displayed at the start of the
4241 * next line. */
4242 if ((
4243# ifdef FEAT_RIGHTLEFT
4244 wp->w_p_rl ? (col <= 0) :
4245# endif
4246 (col >= W_WIDTH(wp) - 1))
4247 && (*mb_char2cells)(mb_c) == 2)
4248 {
4249 c = '>';
4250 mb_c = c;
4251 mb_utf8 = FALSE;
4252 mb_l = 1;
4253 multi_attr = hl_attr(HLF_AT);
4254 /* Put pointer back so that the character will be
4255 * displayed at the start of the next line. */
4256 --ptr;
4257 }
4258 else if (*ptr != NUL)
4259 ptr += mb_l - 1;
4260
4261 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004262 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004263 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004264 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004267 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 c = ' ';
4269 if (area_attr == 0 && search_attr == 0)
4270 {
4271 n_attr = n_extra + 1;
4272 extra_attr = hl_attr(HLF_AT);
4273 saved_attr2 = char_attr; /* save current attr */
4274 }
4275 mb_c = c;
4276 mb_utf8 = FALSE;
4277 mb_l = 1;
4278 }
4279
4280 }
4281#endif
4282 ++ptr;
4283
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004284 /* 'list' : change char 160 to lcs_nbsp. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004285 if (wp->w_p_list && (c == 160
4286#ifdef FEAT_MBYTE
4287 || (mb_utf8 && mb_c == 160)
4288#endif
4289 ) && lcs_nbsp)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004290 {
4291 c = lcs_nbsp;
4292 if (area_attr == 0 && search_attr == 0)
4293 {
4294 n_attr = 1;
4295 extra_attr = hl_attr(HLF_8);
4296 saved_attr2 = char_attr; /* save current attr */
4297 }
4298#ifdef FEAT_MBYTE
4299 mb_c = c;
4300 if (enc_utf8 && (*mb_char2len)(c) > 1)
4301 {
4302 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004303 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004304 c = 0xc0;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004305 }
4306 else
4307 mb_utf8 = FALSE;
4308#endif
4309 }
4310
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 if (extra_check)
4312 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004313#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004314 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004315#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004316
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004317#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 /* Get syntax attribute, unless still at the start of the line
4319 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004320 v = (long)(ptr - line);
4321 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 {
4323 /* Get the syntax attribute for the character. If there
4324 * is an error, disable syntax highlighting. */
4325 save_did_emsg = did_emsg;
4326 did_emsg = FALSE;
4327
Bram Moolenaar217ad922005-03-20 22:37:15 +00004328 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004329# ifdef FEAT_SPELL
4330 has_spell ? &can_spell :
4331# endif
4332 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333
4334 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004335 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004336 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004337 has_syntax = FALSE;
4338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 else
4340 did_emsg = save_did_emsg;
4341
4342 /* Need to get the line again, a multi-line regexp may
4343 * have made it invalid. */
4344 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4345 ptr = line + v;
4346
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004347 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004349 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004350 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004351# ifdef FEAT_CONCEAL
4352 /* no concealing past the end of the line, it interferes
4353 * with line highlighting */
4354 if (c == NUL)
4355 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004356 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004357 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004358# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004360#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004361
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004362#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004363 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004364 * Only do this when there is no syntax highlighting, the
4365 * @Spell cluster is not used or the current syntax item
4366 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004367 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004368 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004369 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004370# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004371 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004372 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004373# endif
4374 if (c != 0 && (
4375# ifdef FEAT_SYN_HL
4376 !has_syntax ||
4377# endif
4378 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004379 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004380 char_u *prev_ptr, *p;
4381 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004382 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004383# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004384 if (has_mbyte)
4385 {
4386 prev_ptr = ptr - mb_l;
4387 v -= mb_l - 1;
4388 }
4389 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004390# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004391 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004392
4393 /* Use nextline[] if possible, it has the start of the
4394 * next line concatenated. */
4395 if ((prev_ptr - line) - nextlinecol >= 0)
4396 p = nextline + (prev_ptr - line) - nextlinecol;
4397 else
4398 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004399 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004400 len = spell_check(wp, p, &spell_hlf, &cap_col,
4401 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004402 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004403
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004404 /* In Insert mode only highlight a word that
4405 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004406 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004407 && (State & INSERT) != 0
4408 && wp->w_cursor.lnum == lnum
4409 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004410 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004411 && wp->w_cursor.col < (colnr_T)word_end)
4412 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004413 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004414 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004415 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004416
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004417 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004418 && (p - nextline) + len > nextline_idx)
4419 {
4420 /* Remember that the good word continues at the
4421 * start of the next line. */
4422 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004423 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004424 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004425
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004426 /* Turn index into actual attributes. */
4427 if (spell_hlf != HLF_COUNT)
4428 spell_attr = highlight_attr[spell_hlf];
4429
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004430 if (cap_col > 0)
4431 {
4432 if (p != prev_ptr
4433 && (p - nextline) + cap_col >= nextline_idx)
4434 {
4435 /* Remember that the word in the next line
4436 * must start with a capital. */
4437 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004438 cap_col = (int)((p - nextline) + cap_col
4439 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004440 }
4441 else
4442 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004443 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004444 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004445 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004446 }
4447 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004448 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004449 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004450 char_attr = hl_combine_attr(char_attr, spell_attr);
4451 else
4452 char_attr = hl_combine_attr(spell_attr, char_attr);
4453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454#endif
4455#ifdef FEAT_LINEBREAK
4456 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004457 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004459 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02004461 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462# ifdef FEAT_MBYTE
4463 has_mbyte ? mb_l :
4464# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004465 1);
4466 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004467 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004468 NULL) - 1;
Bram Moolenaara3650912014-11-19 13:21:57 +01004469 if (c == TAB && n_extra + col > W_WIDTH(wp))
4470 n_extra = (int)wp->w_buffer->b_p_ts
4471 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4472
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 c_extra = ' ';
4474 if (vim_iswhite(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004475 {
4476#ifdef FEAT_CONCEAL
4477 if (c == TAB)
4478 /* See "Tab alignment" below. */
4479 FIX_FOR_BOGUSCOLS;
4480#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004481 if (!wp->w_p_list)
4482 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004483 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 }
4485#endif
4486
4487 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4488 {
4489 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004490 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 {
4492 n_attr = 1;
4493 extra_attr = hl_attr(HLF_8);
4494 saved_attr2 = char_attr; /* save current attr */
4495 }
4496#ifdef FEAT_MBYTE
4497 mb_c = c;
4498 if (enc_utf8 && (*mb_char2len)(c) > 1)
4499 {
4500 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004501 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004502 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 }
4504 else
4505 mb_utf8 = FALSE;
4506#endif
4507 }
4508 }
4509
4510 /*
4511 * Handling of non-printable characters.
4512 */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004513 if (!(chartab[c & 0xff] & CT_PRINT_CHAR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 {
4515 /*
4516 * when getting a character from the file, we may have to
4517 * turn it into something else on the way to putting it
4518 * into "ScreenLines".
4519 */
4520 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4521 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004522 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004523 long vcol_adjusted = vcol; /* removed showbreak length */
4524#ifdef FEAT_LINEBREAK
4525 /* only adjust the tab_len, when at the first column
4526 * after the showbreak value was drawn */
4527 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4528 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4529#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 /* tab amount depends on current column */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004531 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaard574ea22015-01-14 19:35:14 +01004532 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4533
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004534#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004535 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004536#endif
4537 /* tab amount depends on current column */
4538 n_extra = tab_len;
4539#ifdef FEAT_LINEBREAK
4540 else
4541 {
4542 char_u *p;
4543 int len = n_extra;
4544 int i;
4545 int saved_nextra = n_extra;
4546
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004547#ifdef FEAT_CONCEAL
4548 if (is_concealing && vcol_off > 0)
4549 /* there are characters to conceal */
4550 tab_len += vcol_off;
4551#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004552 /* if n_extra > 0, it gives the number of chars, to
4553 * use for a tab, else we need to calculate the width
4554 * for a tab */
4555#ifdef FEAT_MBYTE
4556 len = (tab_len * mb_char2len(lcs_tab2));
4557 if (n_extra > 0)
4558 len += n_extra - tab_len;
4559#endif
4560 c = lcs_tab1;
4561 p = alloc((unsigned)(len + 1));
4562 vim_memset(p, ' ', len);
4563 p[len] = NUL;
4564 p_extra_free = p;
4565 for (i = 0; i < tab_len; i++)
4566 {
4567#ifdef FEAT_MBYTE
4568 mb_char2bytes(lcs_tab2, p);
4569 p += mb_char2len(lcs_tab2);
4570 n_extra += mb_char2len(lcs_tab2)
4571 - (saved_nextra > 0 ? 1 : 0);
4572#else
4573 p[i] = lcs_tab2;
4574#endif
4575 }
4576 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004577#ifdef FEAT_CONCEAL
4578 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
4579 * macro below, so need to adjust for that here */
4580 if (is_concealing && vcol_off > 0)
4581 n_extra -= vcol_off;
4582#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004583 }
4584#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004585#ifdef FEAT_CONCEAL
4586 /* Tab alignment should be identical regardless of
4587 * 'conceallevel' value. So tab compensates of all
4588 * previous concealed characters, and thus resets vcol_off
4589 * and boguscols accumulated so far in the line. Note that
4590 * the tab can be longer than 'tabstop' when there
4591 * are concealed characters. */
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004592 FIX_FOR_BOGUSCOLS;
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004593#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594#ifdef FEAT_MBYTE
4595 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4596#endif
4597 if (wp->w_p_list)
4598 {
4599 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004600#ifdef FEAT_LINEBREAK
4601 if (wp->w_p_lbr)
4602 c_extra = NUL; /* using p_extra from above */
4603 else
4604#endif
4605 c_extra = lcs_tab2;
4606 n_attr = tab_len + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 extra_attr = hl_attr(HLF_8);
4608 saved_attr2 = char_attr; /* save current attr */
4609#ifdef FEAT_MBYTE
4610 mb_c = c;
4611 if (enc_utf8 && (*mb_char2len)(c) > 1)
4612 {
4613 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004614 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004615 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 }
4617#endif
4618 }
4619 else
4620 {
4621 c_extra = ' ';
4622 c = ' ';
4623 }
4624 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004625 else if (c == NUL
4626 && ((wp->w_p_list && lcs_eol > 0)
4627 || ((fromcol >= 0 || fromcol_prev >= 0)
4628 && tocol > vcol
4629 && VIsual_mode != Ctrl_V
4630 && (
4631# ifdef FEAT_RIGHTLEFT
4632 wp->w_p_rl ? (col >= 0) :
4633# endif
4634 (col < W_WIDTH(wp)))
4635 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004636 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004637 && (colnr_T)vcol == wp->w_virtcol)))
4638 && lcs_eol_one >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004640 /* Display a '$' after the line or highlight an extra
4641 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4643 /* For a diff line the highlighting continues after the
4644 * "$". */
4645 if (
4646# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004647 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648# ifdef LINE_ATTR
4649 &&
4650# endif
4651# endif
4652# ifdef LINE_ATTR
4653 line_attr == 0
4654# endif
4655 )
4656#endif
4657 {
4658#ifdef FEAT_VIRTUALEDIT
4659 /* In virtualedit, visual selections may extend
4660 * beyond end of line. */
4661 if (area_highlighting && virtual_active()
4662 && tocol != MAXCOL && vcol < tocol)
4663 n_extra = 0;
4664 else
4665#endif
4666 {
4667 p_extra = at_end_str;
4668 n_extra = 1;
4669 c_extra = NUL;
4670 }
4671 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004672 if (wp->w_p_list)
4673 c = lcs_eol;
4674 else
4675 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 lcs_eol_one = -1;
4677 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004678 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 {
4680 extra_attr = hl_attr(HLF_AT);
4681 n_attr = 1;
4682 }
4683#ifdef FEAT_MBYTE
4684 mb_c = c;
4685 if (enc_utf8 && (*mb_char2len)(c) > 1)
4686 {
4687 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004688 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004689 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 }
4691 else
4692 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4693#endif
4694 }
4695 else if (c != NUL)
4696 {
4697 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02004698 if (n_extra == 0)
4699 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700#ifdef FEAT_RIGHTLEFT
4701 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4702 rl_mirror(p_extra); /* reverse "<12>" */
4703#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004705#ifdef FEAT_LINEBREAK
4706 if (wp->w_p_lbr)
4707 {
4708 char_u *p;
4709
4710 c = *p_extra;
4711 p = alloc((unsigned)n_extra + 1);
4712 vim_memset(p, ' ', n_extra);
4713 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
4714 p[n_extra] = NUL;
4715 p_extra_free = p_extra = p;
4716 }
4717 else
4718#endif
4719 {
4720 n_extra = byte2cells(c) - 1;
4721 c = *p_extra++;
4722 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004723 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 {
4725 n_attr = n_extra + 1;
4726 extra_attr = hl_attr(HLF_8);
4727 saved_attr2 = char_attr; /* save current attr */
4728 }
4729#ifdef FEAT_MBYTE
4730 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4731#endif
4732 }
4733#ifdef FEAT_VIRTUALEDIT
4734 else if (VIsual_active
4735 && (VIsual_mode == Ctrl_V
4736 || VIsual_mode == 'v')
4737 && virtual_active()
4738 && tocol != MAXCOL
4739 && vcol < tocol
4740 && (
4741# ifdef FEAT_RIGHTLEFT
4742 wp->w_p_rl ? (col >= 0) :
4743# endif
4744 (col < W_WIDTH(wp))))
4745 {
4746 c = ' ';
4747 --ptr; /* put it back at the NUL */
4748 }
4749#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004750#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 else if ((
4752# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004753 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 ) && (
4757# ifdef FEAT_RIGHTLEFT
4758 wp->w_p_rl ? (col >= 0) :
4759# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02004760 (col
4761# ifdef FEAT_CONCEAL
4762 - boguscols
4763# endif
4764 < W_WIDTH(wp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 {
4766 /* Highlight until the right side of the window */
4767 c = ' ';
4768 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004769
4770 /* Remember we do the char for line highlighting. */
4771 ++did_line_attr;
4772
4773 /* don't do search HL for the rest of the line */
4774 if (line_attr != 0 && char_attr == search_attr && col > 0)
4775 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776# ifdef FEAT_DIFF
4777 if (diff_hlf == HLF_TXD)
4778 {
4779 diff_hlf = HLF_CHD;
4780 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004781 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 char_attr = hl_attr(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004783 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4784 char_attr = hl_combine_attr(char_attr,
4785 hl_attr(HLF_CUL));
4786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 }
4788# endif
4789 }
4790#endif
4791 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02004792
4793#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004794 if ( wp->w_p_cole > 0
4795 && (wp != curwin || lnum != wp->w_cursor.lnum ||
4796 conceal_cursor_line(wp))
Bram Moolenaarf70e3d62010-07-24 13:15:07 +02004797 && (syntax_flags & HL_CONCEAL) != 0
Bram Moolenaare6dc5732010-07-24 23:52:26 +02004798 && !(lnum_in_visual_area
4799 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02004800 {
4801 char_attr = conceal_attr;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004802 if (prev_syntax_id != syntax_seqnr
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004803 && (syn_get_sub_char() != NUL || wp->w_p_cole == 1)
4804 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004805 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004806 /* First time at this concealed item: display one
4807 * character. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004808 if (syn_get_sub_char() != NUL)
4809 c = syn_get_sub_char();
4810 else if (lcs_conceal != NUL)
4811 c = lcs_conceal;
4812 else
4813 c = ' ';
4814
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004815 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004816
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004817 if (n_extra > 0)
4818 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004819 vcol += n_extra;
4820 if (wp->w_p_wrap && n_extra > 0)
4821 {
4822# ifdef FEAT_RIGHTLEFT
4823 if (wp->w_p_rl)
4824 {
4825 col -= n_extra;
4826 boguscols -= n_extra;
4827 }
4828 else
4829# endif
4830 {
4831 boguscols += n_extra;
4832 col += n_extra;
4833 }
4834 }
4835 n_extra = 0;
4836 n_attr = 0;
4837 }
4838 else if (n_skip == 0)
4839 {
4840 is_concealing = TRUE;
4841 n_skip = 1;
4842 }
4843# ifdef FEAT_MBYTE
4844 mb_c = c;
4845 if (enc_utf8 && (*mb_char2len)(c) > 1)
4846 {
4847 mb_utf8 = TRUE;
4848 u8cc[0] = 0;
4849 c = 0xc0;
4850 }
4851 else
4852 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4853# endif
4854 }
4855 else
4856 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004857 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02004858 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004859 }
4860#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 }
4862
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004863#ifdef FEAT_CONCEAL
4864 /* In the cursor line and we may be concealing characters: correct
4865 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02004866 if (!did_wcol && draw_state == WL_LINE
4867 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004868 && conceal_cursor_line(wp)
4869 && (int)wp->w_virtcol <= vcol + n_skip)
4870 {
4871 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02004872 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004873 did_wcol = TRUE;
4874 }
4875#endif
4876
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 /* Don't override visual selection highlighting. */
4878 if (n_attr > 0
4879 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004880 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 char_attr = extra_attr;
4882
Bram Moolenaar81695252004-12-29 20:58:21 +00004883#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 /* XIM don't send preedit_start and preedit_end, but they send
4885 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
4886 * im_is_preediting() here. */
4887 if (xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004888 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 && (State & INSERT)
4890 && !p_imdisable
4891 && im_is_preediting()
4892 && draw_state == WL_LINE)
4893 {
4894 colnr_T tcol;
4895
4896 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004897 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 else
4899 tcol = preedit_end_col;
4900 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
4901 {
4902 if (feedback_old_attr < 0)
4903 {
4904 feedback_col = 0;
4905 feedback_old_attr = char_attr;
4906 }
4907 char_attr = im_get_feedback_attr(feedback_col);
4908 if (char_attr < 0)
4909 char_attr = feedback_old_attr;
4910 feedback_col++;
4911 }
4912 else if (feedback_old_attr >= 0)
4913 {
4914 char_attr = feedback_old_attr;
4915 feedback_old_attr = -1;
4916 feedback_col = 0;
4917 }
4918 }
4919#endif
4920 /*
4921 * Handle the case where we are in column 0 but not on the first
4922 * character of the line and the user wants us to show us a
4923 * special character (via 'listchars' option "precedes:<char>".
4924 */
4925 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02004926 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
4928#ifdef FEAT_DIFF
4929 && filler_todo <= 0
4930#endif
4931 && draw_state > WL_NR
4932 && c != NUL)
4933 {
4934 c = lcs_prec;
4935 lcs_prec_todo = NUL;
4936#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02004937 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
4938 {
4939 /* Double-width character being overwritten by the "precedes"
4940 * character, need to fill up half the character. */
4941 c_extra = MB_FILLER_CHAR;
4942 n_extra = 1;
4943 n_attr = 2;
4944 extra_attr = hl_attr(HLF_AT);
4945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 mb_c = c;
4947 if (enc_utf8 && (*mb_char2len)(c) > 1)
4948 {
4949 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004950 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004951 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 }
4953 else
4954 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4955#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004956 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 {
4958 saved_attr3 = char_attr; /* save current attr */
4959 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
4960 n_attr3 = 1;
4961 }
4962 }
4963
4964 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00004965 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004967 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004968#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004969 || did_line_attr == 1
4970#endif
4971 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00004973#ifdef FEAT_SEARCH_EXTRA
4974 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00004975
4976 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00004977 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00004978 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004979#endif
4980
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004981 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 * highlight match at end of line. If it's beyond the last
4983 * char on the screen, just overwrite that one (tricky!) Not
4984 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004985#ifdef FEAT_SEARCH_EXTRA
4986 prevcol_hl_flag = FALSE;
4987 if (prevcol == (long)search_hl.startcol)
4988 prevcol_hl_flag = TRUE;
4989 else
4990 {
4991 cur = wp->w_match_head;
4992 while (cur != NULL)
4993 {
4994 if (prevcol == (long)cur->hl.startcol)
4995 {
4996 prevcol_hl_flag = TRUE;
4997 break;
4998 }
4999 cur = cur->next;
5000 }
5001 }
5002#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005004 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005005 && (VIsual_mode != Ctrl_V
5006 || lnum == VIsual.lnum
5007 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005008 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009#ifdef FEAT_SEARCH_EXTRA
5010 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005011 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005012# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005013 && did_line_attr <= 1
5014# endif
5015 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016#endif
5017 ))
5018 {
5019 int n = 0;
5020
5021#ifdef FEAT_RIGHTLEFT
5022 if (wp->w_p_rl)
5023 {
5024 if (col < 0)
5025 n = 1;
5026 }
5027 else
5028#endif
5029 {
5030 if (col >= W_WIDTH(wp))
5031 n = -1;
5032 }
5033 if (n != 0)
5034 {
5035 /* At the window boundary, highlight the last character
5036 * instead (better than nothing). */
5037 off += n;
5038 col += n;
5039 }
5040 else
5041 {
5042 /* Add a blank character to highlight. */
5043 ScreenLines[off] = ' ';
5044#ifdef FEAT_MBYTE
5045 if (enc_utf8)
5046 ScreenLinesUC[off] = 0;
5047#endif
5048 }
5049#ifdef FEAT_SEARCH_EXTRA
5050 if (area_attr == 0)
5051 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005052 /* Use attributes from match with highest priority among
5053 * 'search_hl' and the match list. */
5054 char_attr = search_hl.attr;
5055 cur = wp->w_match_head;
5056 shl_flag = FALSE;
5057 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005058 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005059 if (shl_flag == FALSE
5060 && ((cur != NULL
5061 && cur->priority > SEARCH_HL_PRIORITY)
5062 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005063 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005064 shl = &search_hl;
5065 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005066 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005067 else
5068 shl = &cur->hl;
5069 if ((ptr - line) - 1 == (long)shl->startcol)
5070 char_attr = shl->attr;
5071 if (shl != &search_hl && cur != NULL)
5072 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005073 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 }
5075#endif
5076 ScreenAttrs[off] = char_attr;
5077#ifdef FEAT_RIGHTLEFT
5078 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005079 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005081 --off;
5082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 else
5084#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005085 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005087 ++off;
5088 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005089 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005090#ifdef FEAT_SYN_HL
5091 eol_hl_off = 1;
5092#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005094 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095
Bram Moolenaar91170f82006-05-05 21:15:17 +00005096 /*
5097 * At end of the text line.
5098 */
5099 if (c == NUL)
5100 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005101#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005102 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
5103 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00005104 {
5105 /* highlight last char after line */
5106 --col;
5107 --off;
5108 --vcol;
5109 }
5110
Bram Moolenaar1a384422010-07-14 19:53:30 +02005111 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005112 if (wp->w_p_wrap)
5113 v = wp->w_skipcol;
5114 else
5115 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005116
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005117 /* check if line ends before left margin */
5118 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005119 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005120#ifdef FEAT_CONCEAL
5121 /* Get rid of the boguscols now, we want to draw until the right
5122 * edge for 'cursorcolumn'. */
5123 col -= boguscols;
5124 boguscols = 0;
5125#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005126
5127 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005128 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005129
5130 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005131 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5132 && (int)wp->w_virtcol <
5133 W_WIDTH(wp) * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005134 && lnum != wp->w_cursor.lnum)
5135 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005136# ifdef FEAT_RIGHTLEFT
5137 && !wp->w_p_rl
5138# endif
5139 )
5140 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005141 int rightmost_vcol = 0;
5142 int i;
5143
5144 if (wp->w_p_cuc)
5145 rightmost_vcol = wp->w_virtcol;
5146 if (draw_color_col)
5147 /* determine rightmost colorcolumn to possibly draw */
5148 for (i = 0; color_cols[i] >= 0; ++i)
5149 if (rightmost_vcol < color_cols[i])
5150 rightmost_vcol = color_cols[i];
5151
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005152 while (col < W_WIDTH(wp))
5153 {
5154 ScreenLines[off] = ' ';
5155#ifdef FEAT_MBYTE
5156 if (enc_utf8)
5157 ScreenLinesUC[off] = 0;
5158#endif
5159 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005160 if (draw_color_col)
5161 draw_color_col = advance_color_col(VCOL_HLC,
5162 &color_cols);
5163
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005164 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005165 ScreenAttrs[off++] = hl_attr(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005166 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005167 ScreenAttrs[off++] = hl_attr(HLF_MC);
5168 else
5169 ScreenAttrs[off++] = 0;
5170
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005171 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005172 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005173
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005174 ++vcol;
5175 }
5176 }
5177#endif
5178
Bram Moolenaar860cae12010-06-05 23:22:07 +02005179 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5180 (int)W_WIDTH(wp), wp->w_p_rl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 row++;
5182
5183 /*
5184 * Update w_cline_height and w_cline_folded if the cursor line was
5185 * updated (saves a call to plines() later).
5186 */
5187 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5188 {
5189 curwin->w_cline_row = startrow;
5190 curwin->w_cline_height = row - startrow;
5191#ifdef FEAT_FOLDING
5192 curwin->w_cline_folded = FALSE;
5193#endif
5194 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5195 }
5196
5197 break;
5198 }
5199
5200 /* line continues beyond line end */
5201 if (lcs_ext
5202 && !wp->w_p_wrap
5203#ifdef FEAT_DIFF
5204 && filler_todo <= 0
5205#endif
5206 && (
5207#ifdef FEAT_RIGHTLEFT
5208 wp->w_p_rl ? col == 0 :
5209#endif
5210 col == W_WIDTH(wp) - 1)
5211 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005212 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5214 {
5215 c = lcs_ext;
5216 char_attr = hl_attr(HLF_AT);
5217#ifdef FEAT_MBYTE
5218 mb_c = c;
5219 if (enc_utf8 && (*mb_char2len)(c) > 1)
5220 {
5221 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005222 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005223 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 }
5225 else
5226 mb_utf8 = FALSE;
5227#endif
5228 }
5229
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005230#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005231 /* advance to the next 'colorcolumn' */
5232 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005233 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005234
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005235 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005236 * highlight the cursor position itself.
5237 * Also highlight the 'colorcolumn' if it is different than
5238 * 'cursorcolumn' */
5239 vcol_save_attr = -1;
5240 if (draw_state == WL_LINE && !lnum_in_visual_area)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005241 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005242 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005243 && lnum != wp->w_cursor.lnum)
5244 {
5245 vcol_save_attr = char_attr;
5246 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
5247 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005248 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005249 {
5250 vcol_save_attr = char_attr;
5251 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_MC));
5252 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005253 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005254#endif
5255
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 /*
5257 * Store character to be displayed.
5258 * Skip characters that are left of the screen for 'nowrap'.
5259 */
5260 vcol_prev = vcol;
5261 if (draw_state < WL_LINE || n_skip <= 0)
5262 {
5263 /*
5264 * Store the character.
5265 */
5266#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5267 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5268 {
5269 /* A double-wide character is: put first halve in left cell. */
5270 --off;
5271 --col;
5272 }
5273#endif
5274 ScreenLines[off] = c;
5275#ifdef FEAT_MBYTE
5276 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005277 {
5278 if ((mb_c & 0xff00) == 0x8e00)
5279 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 else if (enc_utf8)
5283 {
5284 if (mb_utf8)
5285 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005286 int i;
5287
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005289 if ((c & 0xff) == 0)
5290 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005291 for (i = 0; i < Screen_mco; ++i)
5292 {
5293 ScreenLinesC[i][off] = u8cc[i];
5294 if (u8cc[i] == 0)
5295 break;
5296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 }
5298 else
5299 ScreenLinesUC[off] = 0;
5300 }
5301 if (multi_attr)
5302 {
5303 ScreenAttrs[off] = multi_attr;
5304 multi_attr = 0;
5305 }
5306 else
5307#endif
5308 ScreenAttrs[off] = char_attr;
5309
5310#ifdef FEAT_MBYTE
5311 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5312 {
5313 /* Need to fill two screen columns. */
5314 ++off;
5315 ++col;
5316 if (enc_utf8)
5317 /* UTF-8: Put a 0 in the second screen char. */
5318 ScreenLines[off] = 0;
5319 else
5320 /* DBCS: Put second byte in the second screen char. */
5321 ScreenLines[off] = mb_c & 0xff;
5322 ++vcol;
5323 /* When "tocol" is halfway a character, set it to the end of
5324 * the character, otherwise highlighting won't stop. */
5325 if (tocol == vcol)
5326 ++tocol;
5327#ifdef FEAT_RIGHTLEFT
5328 if (wp->w_p_rl)
5329 {
5330 /* now it's time to backup one cell */
5331 --off;
5332 --col;
5333 }
5334#endif
5335 }
5336#endif
5337#ifdef FEAT_RIGHTLEFT
5338 if (wp->w_p_rl)
5339 {
5340 --off;
5341 --col;
5342 }
5343 else
5344#endif
5345 {
5346 ++off;
5347 ++col;
5348 }
5349 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005350#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005351 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005352 {
5353 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005354 ++vcol_off;
5355 if (n_extra > 0)
5356 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005357 if (wp->w_p_wrap)
5358 {
5359 /*
5360 * Special voodoo required if 'wrap' is on.
5361 *
5362 * Advance the column indicator to force the line
5363 * drawing to wrap early. This will make the line
5364 * take up the same screen space when parts are concealed,
5365 * so that cursor line computations aren't messed up.
5366 *
5367 * To avoid the fictitious advance of 'col' causing
5368 * trailing junk to be written out of the screen line
5369 * we are building, 'boguscols' keeps track of the number
5370 * of bad columns we have advanced.
5371 */
5372 if (n_extra > 0)
5373 {
5374 vcol += n_extra;
5375# ifdef FEAT_RIGHTLEFT
5376 if (wp->w_p_rl)
5377 {
5378 col -= n_extra;
5379 boguscols -= n_extra;
5380 }
5381 else
5382# endif
5383 {
5384 col += n_extra;
5385 boguscols += n_extra;
5386 }
5387 n_extra = 0;
5388 n_attr = 0;
5389 }
5390
5391
5392# ifdef FEAT_MBYTE
5393 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5394 {
5395 /* Need to fill two screen columns. */
5396# ifdef FEAT_RIGHTLEFT
5397 if (wp->w_p_rl)
5398 {
5399 --boguscols;
5400 --col;
5401 }
5402 else
5403# endif
5404 {
5405 ++boguscols;
5406 ++col;
5407 }
5408 }
5409# endif
5410
5411# ifdef FEAT_RIGHTLEFT
5412 if (wp->w_p_rl)
5413 {
5414 --boguscols;
5415 --col;
5416 }
5417 else
5418# endif
5419 {
5420 ++boguscols;
5421 ++col;
5422 }
5423 }
5424 else
5425 {
5426 if (n_extra > 0)
5427 {
5428 vcol += n_extra;
5429 n_extra = 0;
5430 n_attr = 0;
5431 }
5432 }
5433
5434 }
5435#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 else
5437 --n_skip;
5438
Bram Moolenaar64486672010-05-16 15:46:46 +02005439 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5440 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005441 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442#ifdef FEAT_DIFF
5443 && filler_todo <= 0
5444#endif
5445 )
5446 ++vcol;
5447
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005448#ifdef FEAT_SYN_HL
5449 if (vcol_save_attr >= 0)
5450 char_attr = vcol_save_attr;
5451#endif
5452
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 /* restore attributes after "predeces" in 'listchars' */
5454 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5455 char_attr = saved_attr3;
5456
5457 /* restore attributes after last 'listchars' or 'number' char */
5458 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5459 char_attr = saved_attr2;
5460
5461 /*
5462 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005463 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 */
5465 if ((
5466#ifdef FEAT_RIGHTLEFT
5467 wp->w_p_rl ? (col < 0) :
5468#endif
5469 (col >= W_WIDTH(wp)))
5470 && (*ptr != NUL
5471#ifdef FEAT_DIFF
5472 || filler_todo > 0
5473#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005474 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5476 )
5477 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005478#ifdef FEAT_CONCEAL
5479 SCREEN_LINE(screen_row, W_WINCOL(wp), col - boguscols,
5480 (int)W_WIDTH(wp), wp->w_p_rl);
5481 boguscols = 0;
5482#else
5483 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5484 (int)W_WIDTH(wp), wp->w_p_rl);
5485#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 ++row;
5487 ++screen_row;
5488
5489 /* When not wrapping and finished diff lines, or when displayed
5490 * '$' and highlighting until last column, break here. */
5491 if ((!wp->w_p_wrap
5492#ifdef FEAT_DIFF
5493 && filler_todo <= 0
5494#endif
5495 ) || lcs_eol_one == -1)
5496 break;
5497
5498 /* When the window is too narrow draw all "@" lines. */
5499 if (draw_state != WL_LINE
5500#ifdef FEAT_DIFF
5501 && filler_todo <= 0
5502#endif
5503 )
5504 {
5505 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
5506#ifdef FEAT_VERTSPLIT
5507 draw_vsep_win(wp, row);
5508#endif
5509 row = endrow;
5510 }
5511
5512 /* When line got too long for screen break here. */
5513 if (row == endrow)
5514 {
5515 ++row;
5516 break;
5517 }
5518
5519 if (screen_cur_row == screen_row - 1
5520#ifdef FEAT_DIFF
5521 && filler_todo <= 0
5522#endif
5523 && W_WIDTH(wp) == Columns)
5524 {
5525 /* Remember that the line wraps, used for modeless copy. */
5526 LineWraps[screen_row - 1] = TRUE;
5527
5528 /*
5529 * Special trick to make copy/paste of wrapped lines work with
5530 * xterm/screen: write an extra character beyond the end of
5531 * the line. This will work with all terminal types
5532 * (regardless of the xn,am settings).
5533 * Only do this on a fast tty.
5534 * Only do this if the cursor is on the current line
5535 * (something has been written in it).
5536 * Don't do this for the GUI.
5537 * Don't do this for double-width characters.
5538 * Don't do this for a window not at the right screen border.
5539 */
5540 if (p_tf
5541#ifdef FEAT_GUI
5542 && !gui.in_use
5543#endif
5544#ifdef FEAT_MBYTE
5545 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005546 && ((*mb_off2cells)(LineOffset[screen_row],
5547 LineOffset[screen_row] + screen_Columns)
5548 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005550 + (int)Columns - 2,
5551 LineOffset[screen_row] + screen_Columns)
5552 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553#endif
5554 )
5555 {
5556 /* First make sure we are at the end of the screen line,
5557 * then output the same character again to let the
5558 * terminal know about the wrap. If the terminal doesn't
5559 * auto-wrap, we overwrite the character. */
5560 if (screen_cur_col != W_WIDTH(wp))
5561 screen_char(LineOffset[screen_row - 1]
5562 + (unsigned)Columns - 1,
5563 screen_row - 1, (int)(Columns - 1));
5564
5565#ifdef FEAT_MBYTE
5566 /* When there is a multi-byte character, just output a
5567 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005568 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5569 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 out_char(' ');
5571 else
5572#endif
5573 out_char(ScreenLines[LineOffset[screen_row - 1]
5574 + (Columns - 1)]);
5575 /* force a redraw of the first char on the next line */
5576 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5577 screen_start(); /* don't know where cursor is now */
5578 }
5579 }
5580
5581 col = 0;
5582 off = (unsigned)(current_ScreenLine - ScreenLines);
5583#ifdef FEAT_RIGHTLEFT
5584 if (wp->w_p_rl)
5585 {
5586 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
5587 off += col;
5588 }
5589#endif
5590
5591 /* reset the drawing state for the start of a wrapped line */
5592 draw_state = WL_START;
5593 saved_n_extra = n_extra;
5594 saved_p_extra = p_extra;
5595 saved_c_extra = c_extra;
5596 saved_char_attr = char_attr;
5597 n_extra = 0;
5598 lcs_prec_todo = lcs_prec;
5599#ifdef FEAT_LINEBREAK
5600# ifdef FEAT_DIFF
5601 if (filler_todo <= 0)
5602# endif
5603 need_showbreak = TRUE;
5604#endif
5605#ifdef FEAT_DIFF
5606 --filler_todo;
5607 /* When the filler lines are actually below the last line of the
5608 * file, don't draw the line itself, break here. */
5609 if (filler_todo == 0 && wp->w_botfill)
5610 break;
5611#endif
5612 }
5613
5614 } /* for every character in the line */
5615
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005616#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005617 /* After an empty line check first word for capital. */
5618 if (*skipwhite(line) == NUL)
5619 {
5620 capcol_lnum = lnum + 1;
5621 cap_col = 0;
5622 }
5623#endif
5624
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 return row;
5626}
5627
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005628#ifdef FEAT_MBYTE
5629static int comp_char_differs __ARGS((int, int));
5630
5631/*
5632 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005633 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005634 */
5635 static int
5636comp_char_differs(off_from, off_to)
5637 int off_from;
5638 int off_to;
5639{
5640 int i;
5641
5642 for (i = 0; i < Screen_mco; ++i)
5643 {
5644 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5645 return TRUE;
5646 if (ScreenLinesC[i][off_from] == 0)
5647 break;
5648 }
5649 return FALSE;
5650}
5651#endif
5652
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653/*
5654 * Check whether the given character needs redrawing:
5655 * - the (first byte of the) character is different
5656 * - the attributes are different
5657 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005658 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 */
5660 static int
5661char_needs_redraw(off_from, off_to, cols)
5662 int off_from;
5663 int off_to;
5664 int cols;
5665{
5666 if (cols > 0
5667 && ((ScreenLines[off_from] != ScreenLines[off_to]
5668 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
5669
5670#ifdef FEAT_MBYTE
5671 || (enc_dbcs != 0
5672 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
5673 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
5674 ? ScreenLines2[off_from] != ScreenLines2[off_to]
5675 : (cols > 1 && ScreenLines[off_from + 1]
5676 != ScreenLines[off_to + 1])))
5677 || (enc_utf8
5678 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
5679 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005680 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02005681 || ((*mb_off2cells)(off_from, off_from + cols) > 1
5682 && ScreenLines[off_from + 1]
5683 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684#endif
5685 ))
5686 return TRUE;
5687 return FALSE;
5688}
5689
5690/*
5691 * Move one "cooked" screen line to the screen, but only the characters that
5692 * have actually changed. Handle insert/delete character.
5693 * "coloff" gives the first column on the screen for this line.
5694 * "endcol" gives the columns where valid characters are.
5695 * "clear_width" is the width of the window. It's > 0 if the rest of the line
5696 * needs to be cleared, negative otherwise.
5697 * "rlflag" is TRUE in a rightleft window:
5698 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
5699 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
5700 */
5701 static void
5702screen_line(row, coloff, endcol, clear_width
5703#ifdef FEAT_RIGHTLEFT
5704 , rlflag
5705#endif
5706 )
5707 int row;
5708 int coloff;
5709 int endcol;
5710 int clear_width;
5711#ifdef FEAT_RIGHTLEFT
5712 int rlflag;
5713#endif
5714{
5715 unsigned off_from;
5716 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005717#ifdef FEAT_MBYTE
5718 unsigned max_off_from;
5719 unsigned max_off_to;
5720#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 int col = 0;
5722#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_VERTSPLIT)
5723 int hl;
5724#endif
5725 int force = FALSE; /* force update rest of the line */
5726 int redraw_this /* bool: does character need redraw? */
5727#ifdef FEAT_GUI
5728 = TRUE /* For GUI when while-loop empty */
5729#endif
5730 ;
5731 int redraw_next; /* redraw_this for next character */
5732#ifdef FEAT_MBYTE
5733 int clear_next = FALSE;
5734 int char_cells; /* 1: normal char */
5735 /* 2: occupies two display cells */
5736# define CHAR_CELLS char_cells
5737#else
5738# define CHAR_CELLS 1
5739#endif
5740
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01005741 /* Check for illegal row and col, just in case. */
5742 if (row >= Rows)
5743 row = Rows - 1;
5744 if (endcol > Columns)
5745 endcol = Columns;
5746
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747# ifdef FEAT_CLIPBOARD
5748 clip_may_clear_selection(row, row);
5749# endif
5750
5751 off_from = (unsigned)(current_ScreenLine - ScreenLines);
5752 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005753#ifdef FEAT_MBYTE
5754 max_off_from = off_from + screen_Columns;
5755 max_off_to = LineOffset[row] + screen_Columns;
5756#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757
5758#ifdef FEAT_RIGHTLEFT
5759 if (rlflag)
5760 {
5761 /* Clear rest first, because it's left of the text. */
5762 if (clear_width > 0)
5763 {
5764 while (col <= endcol && ScreenLines[off_to] == ' '
5765 && ScreenAttrs[off_to] == 0
5766# ifdef FEAT_MBYTE
5767 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5768# endif
5769 )
5770 {
5771 ++off_to;
5772 ++col;
5773 }
5774 if (col <= endcol)
5775 screen_fill(row, row + 1, col + coloff,
5776 endcol + coloff + 1, ' ', ' ', 0);
5777 }
5778 col = endcol + 1;
5779 off_to = LineOffset[row] + col + coloff;
5780 off_from += col;
5781 endcol = (clear_width > 0 ? clear_width : -clear_width);
5782 }
5783#endif /* FEAT_RIGHTLEFT */
5784
5785 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
5786
5787 while (col < endcol)
5788 {
5789#ifdef FEAT_MBYTE
5790 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00005791 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 else
5793 char_cells = 1;
5794#endif
5795
5796 redraw_this = redraw_next;
5797 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
5798 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
5799
5800#ifdef FEAT_GUI
5801 /* If the next character was bold, then redraw the current character to
5802 * remove any pixels that might have spilt over into us. This only
5803 * happens in the GUI.
5804 */
5805 if (redraw_next && gui.in_use)
5806 {
5807 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005808 if (hl > HL_ALL)
5809 hl = syn_attr2attr(hl);
5810 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 redraw_this = TRUE;
5812 }
5813#endif
5814
5815 if (redraw_this)
5816 {
5817 /*
5818 * Special handling when 'xs' termcap flag set (hpterm):
5819 * Attributes for characters are stored at the position where the
5820 * cursor is when writing the highlighting code. The
5821 * start-highlighting code must be written with the cursor on the
5822 * first highlighted character. The stop-highlighting code must
5823 * be written with the cursor just after the last highlighted
5824 * character.
5825 * Overwriting a character doesn't remove it's highlighting. Need
5826 * to clear the rest of the line, and force redrawing it
5827 * completely.
5828 */
5829 if ( p_wiv
5830 && !force
5831#ifdef FEAT_GUI
5832 && !gui.in_use
5833#endif
5834 && ScreenAttrs[off_to] != 0
5835 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
5836 {
5837 /*
5838 * Need to remove highlighting attributes here.
5839 */
5840 windgoto(row, col + coloff);
5841 out_str(T_CE); /* clear rest of this screen line */
5842 screen_start(); /* don't know where cursor is now */
5843 force = TRUE; /* force redraw of rest of the line */
5844 redraw_next = TRUE; /* or else next char would miss out */
5845
5846 /*
5847 * If the previous character was highlighted, need to stop
5848 * highlighting at this character.
5849 */
5850 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
5851 {
5852 screen_attr = ScreenAttrs[off_to - 1];
5853 term_windgoto(row, col + coloff);
5854 screen_stop_highlight();
5855 }
5856 else
5857 screen_attr = 0; /* highlighting has stopped */
5858 }
5859#ifdef FEAT_MBYTE
5860 if (enc_dbcs != 0)
5861 {
5862 /* Check if overwriting a double-byte with a single-byte or
5863 * the other way around requires another character to be
5864 * redrawn. For UTF-8 this isn't needed, because comparing
5865 * ScreenLinesUC[] is sufficient. */
5866 if (char_cells == 1
5867 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005868 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 {
5870 /* Writing a single-cell character over a double-cell
5871 * character: need to redraw the next cell. */
5872 ScreenLines[off_to + 1] = 0;
5873 redraw_next = TRUE;
5874 }
5875 else if (char_cells == 2
5876 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005877 && (*mb_off2cells)(off_to, max_off_to) == 1
5878 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879 {
5880 /* Writing the second half of a double-cell character over
5881 * a double-cell character: need to redraw the second
5882 * cell. */
5883 ScreenLines[off_to + 2] = 0;
5884 redraw_next = TRUE;
5885 }
5886
5887 if (enc_dbcs == DBCS_JPNU)
5888 ScreenLines2[off_to] = ScreenLines2[off_from];
5889 }
5890 /* When writing a single-width character over a double-width
5891 * character and at the end of the redrawn text, need to clear out
5892 * the right halve of the old character.
5893 * Also required when writing the right halve of a double-width
5894 * char over the left halve of an existing one. */
5895 if (has_mbyte && col + char_cells == endcol
5896 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00005897 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00005899 && (*mb_off2cells)(off_to, max_off_to) == 1
5900 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901 clear_next = TRUE;
5902#endif
5903
5904 ScreenLines[off_to] = ScreenLines[off_from];
5905#ifdef FEAT_MBYTE
5906 if (enc_utf8)
5907 {
5908 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
5909 if (ScreenLinesUC[off_from] != 0)
5910 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005911 int i;
5912
5913 for (i = 0; i < Screen_mco; ++i)
5914 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915 }
5916 }
5917 if (char_cells == 2)
5918 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
5919#endif
5920
5921#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00005922 /* The bold trick makes a single column of pixels appear in the
5923 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924 * character should be redrawn too. This happens for our own GUI
5925 * and for some xterms. */
5926 if (
5927# ifdef FEAT_GUI
5928 gui.in_use
5929# endif
5930# if defined(FEAT_GUI) && defined(UNIX)
5931 ||
5932# endif
5933# ifdef UNIX
5934 term_is_xterm
5935# endif
5936 )
5937 {
5938 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005939 if (hl > HL_ALL)
5940 hl = syn_attr2attr(hl);
5941 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 redraw_next = TRUE;
5943 }
5944#endif
5945 ScreenAttrs[off_to] = ScreenAttrs[off_from];
5946#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005947 /* For simplicity set the attributes of second half of a
5948 * double-wide character equal to the first half. */
5949 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005951
5952 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 else
5955#endif
5956 screen_char(off_to, row, col + coloff);
5957 }
5958 else if ( p_wiv
5959#ifdef FEAT_GUI
5960 && !gui.in_use
5961#endif
5962 && col + coloff > 0)
5963 {
5964 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
5965 {
5966 /*
5967 * Don't output stop-highlight when moving the cursor, it will
5968 * stop the highlighting when it should continue.
5969 */
5970 screen_attr = 0;
5971 }
5972 else if (screen_attr != 0)
5973 screen_stop_highlight();
5974 }
5975
5976 off_to += CHAR_CELLS;
5977 off_from += CHAR_CELLS;
5978 col += CHAR_CELLS;
5979 }
5980
5981#ifdef FEAT_MBYTE
5982 if (clear_next)
5983 {
5984 /* Clear the second half of a double-wide character of which the left
5985 * half was overwritten with a single-wide character. */
5986 ScreenLines[off_to] = ' ';
5987 if (enc_utf8)
5988 ScreenLinesUC[off_to] = 0;
5989 screen_char(off_to, row, col + coloff);
5990 }
5991#endif
5992
5993 if (clear_width > 0
5994#ifdef FEAT_RIGHTLEFT
5995 && !rlflag
5996#endif
5997 )
5998 {
5999#ifdef FEAT_GUI
6000 int startCol = col;
6001#endif
6002
6003 /* blank out the rest of the line */
6004 while (col < clear_width && ScreenLines[off_to] == ' '
6005 && ScreenAttrs[off_to] == 0
6006#ifdef FEAT_MBYTE
6007 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6008#endif
6009 )
6010 {
6011 ++off_to;
6012 ++col;
6013 }
6014 if (col < clear_width)
6015 {
6016#ifdef FEAT_GUI
6017 /*
6018 * In the GUI, clearing the rest of the line may leave pixels
6019 * behind if the first character cleared was bold. Some bold
6020 * fonts spill over the left. In this case we redraw the previous
6021 * character too. If we didn't skip any blanks above, then we
6022 * only redraw if the character wasn't already redrawn anyway.
6023 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006024 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025 {
6026 hl = ScreenAttrs[off_to];
6027 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006028 {
6029 int prev_cells = 1;
6030# ifdef FEAT_MBYTE
6031 if (enc_utf8)
6032 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6033 * that its width is 2. */
6034 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6035 else if (enc_dbcs != 0)
6036 {
6037 /* find previous character by counting from first
6038 * column and get its width. */
6039 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006040 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006041
6042 while (off < off_to)
6043 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006044 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006045 off += prev_cells;
6046 }
6047 }
6048
6049 if (enc_dbcs != 0 && prev_cells > 1)
6050 screen_char_2(off_to - prev_cells, row,
6051 col + coloff - prev_cells);
6052 else
6053# endif
6054 screen_char(off_to - prev_cells, row,
6055 col + coloff - prev_cells);
6056 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057 }
6058#endif
6059 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6060 ' ', ' ', 0);
6061#ifdef FEAT_VERTSPLIT
6062 off_to += clear_width - col;
6063 col = clear_width;
6064#endif
6065 }
6066 }
6067
6068 if (clear_width > 0)
6069 {
6070#ifdef FEAT_VERTSPLIT
6071 /* For a window that's left of another, draw the separator char. */
6072 if (col + coloff < Columns)
6073 {
6074 int c;
6075
6076 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006077 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006079 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6080 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081# endif
6082 || ScreenAttrs[off_to] != hl)
6083 {
6084 ScreenLines[off_to] = c;
6085 ScreenAttrs[off_to] = hl;
6086# ifdef FEAT_MBYTE
6087 if (enc_utf8)
6088 {
6089 if (c >= 0x80)
6090 {
6091 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006092 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093 }
6094 else
6095 ScreenLinesUC[off_to] = 0;
6096 }
6097# endif
6098 screen_char(off_to, row, col + coloff);
6099 }
6100 }
6101 else
6102#endif
6103 LineWraps[row] = FALSE;
6104 }
6105}
6106
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006107#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006109 * Mirror text "str" for right-left displaying.
6110 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006112 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113rl_mirror(str)
6114 char_u *str;
6115{
6116 char_u *p1, *p2;
6117 int t;
6118
6119 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6120 {
6121 t = *p1;
6122 *p1 = *p2;
6123 *p2 = t;
6124 }
6125}
6126#endif
6127
6128#if defined(FEAT_WINDOWS) || defined(PROTO)
6129/*
6130 * mark all status lines for redraw; used after first :cd
6131 */
6132 void
6133status_redraw_all()
6134{
6135 win_T *wp;
6136
6137 for (wp = firstwin; wp; wp = wp->w_next)
6138 if (wp->w_status_height)
6139 {
6140 wp->w_redr_status = TRUE;
6141 redraw_later(VALID);
6142 }
6143}
6144
6145/*
6146 * mark all status lines of the current buffer for redraw
6147 */
6148 void
6149status_redraw_curbuf()
6150{
6151 win_T *wp;
6152
6153 for (wp = firstwin; wp; wp = wp->w_next)
6154 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6155 {
6156 wp->w_redr_status = TRUE;
6157 redraw_later(VALID);
6158 }
6159}
6160
6161/*
6162 * Redraw all status lines that need to be redrawn.
6163 */
6164 void
6165redraw_statuslines()
6166{
6167 win_T *wp;
6168
6169 for (wp = firstwin; wp; wp = wp->w_next)
6170 if (wp->w_redr_status)
6171 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006172 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006173 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174}
6175#endif
6176
6177#if (defined(FEAT_WILDMENU) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6178/*
6179 * Redraw all status lines at the bottom of frame "frp".
6180 */
6181 void
6182win_redraw_last_status(frp)
6183 frame_T *frp;
6184{
6185 if (frp->fr_layout == FR_LEAF)
6186 frp->fr_win->w_redr_status = TRUE;
6187 else if (frp->fr_layout == FR_ROW)
6188 {
6189 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6190 win_redraw_last_status(frp);
6191 }
6192 else /* frp->fr_layout == FR_COL */
6193 {
6194 frp = frp->fr_child;
6195 while (frp->fr_next != NULL)
6196 frp = frp->fr_next;
6197 win_redraw_last_status(frp);
6198 }
6199}
6200#endif
6201
6202#ifdef FEAT_VERTSPLIT
6203/*
6204 * Draw the verticap separator right of window "wp" starting with line "row".
6205 */
6206 static void
6207draw_vsep_win(wp, row)
6208 win_T *wp;
6209 int row;
6210{
6211 int hl;
6212 int c;
6213
6214 if (wp->w_vsep_width)
6215 {
6216 /* draw the vertical separator right of this window */
6217 c = fillchar_vsep(&hl);
6218 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6219 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6220 c, ' ', hl);
6221 }
6222}
6223#endif
6224
6225#ifdef FEAT_WILDMENU
6226static int status_match_len __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006227static int skip_status_match_char __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228
6229/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006230 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231 */
6232 static int
6233status_match_len(xp, s)
6234 expand_T *xp;
6235 char_u *s;
6236{
6237 int len = 0;
6238
6239#ifdef FEAT_MENU
6240 int emenu = (xp->xp_context == EXPAND_MENUS
6241 || xp->xp_context == EXPAND_MENUNAMES);
6242
6243 /* Check for menu separators - replace with '|'. */
6244 if (emenu && menu_is_separator(s))
6245 return 1;
6246#endif
6247
6248 while (*s != NUL)
6249 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006250 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006251 len += ptr2cells(s);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006252 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 }
6254
6255 return len;
6256}
6257
6258/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006259 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006260 * These are backslashes used for escaping. Do show backslashes in help tags.
6261 */
6262 static int
6263skip_status_match_char(xp, s)
6264 expand_T *xp;
6265 char_u *s;
6266{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006267 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006268#ifdef FEAT_MENU
6269 || ((xp->xp_context == EXPAND_MENUS
6270 || xp->xp_context == EXPAND_MENUNAMES)
6271 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6272#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006273 )
6274 {
6275#ifndef BACKSLASH_IN_FILENAME
6276 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6277 return 2;
6278#endif
6279 return 1;
6280 }
6281 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006282}
6283
6284/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 * Show wildchar matches in the status line.
6286 * Show at least the "match" item.
6287 * We start at item 'first_match' in the list and show all matches that fit.
6288 *
6289 * If inversion is possible we use it. Else '=' characters are used.
6290 */
6291 void
6292win_redr_status_matches(xp, num_matches, matches, match, showtail)
6293 expand_T *xp;
6294 int num_matches;
6295 char_u **matches; /* list of matches */
6296 int match;
6297 int showtail;
6298{
6299#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6300 int row;
6301 char_u *buf;
6302 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006303 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304 int fillchar;
6305 int attr;
6306 int i;
6307 int highlight = TRUE;
6308 char_u *selstart = NULL;
6309 int selstart_col = 0;
6310 char_u *selend = NULL;
6311 static int first_match = 0;
6312 int add_left = FALSE;
6313 char_u *s;
6314#ifdef FEAT_MENU
6315 int emenu;
6316#endif
6317#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6318 int l;
6319#endif
6320
6321 if (matches == NULL) /* interrupted completion? */
6322 return;
6323
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006324#ifdef FEAT_MBYTE
6325 if (has_mbyte)
6326 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6327 else
6328#endif
6329 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330 if (buf == NULL)
6331 return;
6332
6333 if (match == -1) /* don't show match but original text */
6334 {
6335 match = 0;
6336 highlight = FALSE;
6337 }
6338 /* count 1 for the ending ">" */
6339 clen = status_match_len(xp, L_MATCH(match)) + 3;
6340 if (match == 0)
6341 first_match = 0;
6342 else if (match < first_match)
6343 {
6344 /* jumping left, as far as we can go */
6345 first_match = match;
6346 add_left = TRUE;
6347 }
6348 else
6349 {
6350 /* check if match fits on the screen */
6351 for (i = first_match; i < match; ++i)
6352 clen += status_match_len(xp, L_MATCH(i)) + 2;
6353 if (first_match > 0)
6354 clen += 2;
6355 /* jumping right, put match at the left */
6356 if ((long)clen > Columns)
6357 {
6358 first_match = match;
6359 /* if showing the last match, we can add some on the left */
6360 clen = 2;
6361 for (i = match; i < num_matches; ++i)
6362 {
6363 clen += status_match_len(xp, L_MATCH(i)) + 2;
6364 if ((long)clen >= Columns)
6365 break;
6366 }
6367 if (i == num_matches)
6368 add_left = TRUE;
6369 }
6370 }
6371 if (add_left)
6372 while (first_match > 0)
6373 {
6374 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6375 if ((long)clen >= Columns)
6376 break;
6377 --first_match;
6378 }
6379
6380 fillchar = fillchar_status(&attr, TRUE);
6381
6382 if (first_match == 0)
6383 {
6384 *buf = NUL;
6385 len = 0;
6386 }
6387 else
6388 {
6389 STRCPY(buf, "< ");
6390 len = 2;
6391 }
6392 clen = len;
6393
6394 i = first_match;
6395 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6396 {
6397 if (i == match)
6398 {
6399 selstart = buf + len;
6400 selstart_col = clen;
6401 }
6402
6403 s = L_MATCH(i);
6404 /* Check for menu separators - replace with '|' */
6405#ifdef FEAT_MENU
6406 emenu = (xp->xp_context == EXPAND_MENUS
6407 || xp->xp_context == EXPAND_MENUNAMES);
6408 if (emenu && menu_is_separator(s))
6409 {
6410 STRCPY(buf + len, transchar('|'));
6411 l = (int)STRLEN(buf + len);
6412 len += l;
6413 clen += l;
6414 }
6415 else
6416#endif
6417 for ( ; *s != NUL; ++s)
6418 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006419 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420 clen += ptr2cells(s);
6421#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006422 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423 {
6424 STRNCPY(buf + len, s, l);
6425 s += l - 1;
6426 len += l;
6427 }
6428 else
6429#endif
6430 {
6431 STRCPY(buf + len, transchar_byte(*s));
6432 len += (int)STRLEN(buf + len);
6433 }
6434 }
6435 if (i == match)
6436 selend = buf + len;
6437
6438 *(buf + len++) = ' ';
6439 *(buf + len++) = ' ';
6440 clen += 2;
6441 if (++i == num_matches)
6442 break;
6443 }
6444
6445 if (i != num_matches)
6446 {
6447 *(buf + len++) = '>';
6448 ++clen;
6449 }
6450
6451 buf[len] = NUL;
6452
6453 row = cmdline_row - 1;
6454 if (row >= 0)
6455 {
6456 if (wild_menu_showing == 0)
6457 {
6458 if (msg_scrolled > 0)
6459 {
6460 /* Put the wildmenu just above the command line. If there is
6461 * no room, scroll the screen one line up. */
6462 if (cmdline_row == Rows - 1)
6463 {
6464 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
6465 ++msg_scrolled;
6466 }
6467 else
6468 {
6469 ++cmdline_row;
6470 ++row;
6471 }
6472 wild_menu_showing = WM_SCROLLED;
6473 }
6474 else
6475 {
6476 /* Create status line if needed by setting 'laststatus' to 2.
6477 * Set 'winminheight' to zero to avoid that the window is
6478 * resized. */
6479 if (lastwin->w_status_height == 0)
6480 {
6481 save_p_ls = p_ls;
6482 save_p_wmh = p_wmh;
6483 p_ls = 2;
6484 p_wmh = 0;
6485 last_status(FALSE);
6486 }
6487 wild_menu_showing = WM_SHOWN;
6488 }
6489 }
6490
6491 screen_puts(buf, row, 0, attr);
6492 if (selstart != NULL && highlight)
6493 {
6494 *selend = NUL;
6495 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
6496 }
6497
6498 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6499 }
6500
6501#ifdef FEAT_VERTSPLIT
6502 win_redraw_last_status(topframe);
6503#else
6504 lastwin->w_redr_status = TRUE;
6505#endif
6506 vim_free(buf);
6507}
6508#endif
6509
6510#if defined(FEAT_WINDOWS) || defined(PROTO)
6511/*
6512 * Redraw the status line of window wp.
6513 *
6514 * If inversion is possible we use it. Else '=' characters are used.
6515 */
6516 void
6517win_redr_status(wp)
6518 win_T *wp;
6519{
6520 int row;
6521 char_u *p;
6522 int len;
6523 int fillchar;
6524 int attr;
6525 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006526 static int busy = FALSE;
6527
6528 /* It's possible to get here recursively when 'statusline' (indirectly)
6529 * invokes ":redrawstatus". Simply ignore the call then. */
6530 if (busy)
6531 return;
6532 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533
6534 wp->w_redr_status = FALSE;
6535 if (wp->w_status_height == 0)
6536 {
6537 /* no status line, can only be last window */
6538 redraw_cmdline = TRUE;
6539 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006540 else if (!redrawing()
6541#ifdef FEAT_INS_EXPAND
6542 /* don't update status line when popup menu is visible and may be
6543 * drawn over it */
6544 || pum_visible()
6545#endif
6546 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547 {
6548 /* Don't redraw right now, do it later. */
6549 wp->w_redr_status = TRUE;
6550 }
6551#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006552 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553 {
6554 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006555 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556 }
6557#endif
6558 else
6559 {
6560 fillchar = fillchar_status(&attr, wp == curwin);
6561
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006562 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563 p = NameBuff;
6564 len = (int)STRLEN(p);
6565
6566 if (wp->w_buffer->b_help
6567#ifdef FEAT_QUICKFIX
6568 || wp->w_p_pvw
6569#endif
6570 || bufIsChanged(wp->w_buffer)
6571 || wp->w_buffer->b_p_ro)
6572 *(p + len++) = ' ';
6573 if (wp->w_buffer->b_help)
6574 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006575 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576 len += (int)STRLEN(p + len);
6577 }
6578#ifdef FEAT_QUICKFIX
6579 if (wp->w_p_pvw)
6580 {
6581 STRCPY(p + len, _("[Preview]"));
6582 len += (int)STRLEN(p + len);
6583 }
6584#endif
6585 if (bufIsChanged(wp->w_buffer))
6586 {
6587 STRCPY(p + len, "[+]");
6588 len += 3;
6589 }
6590 if (wp->w_buffer->b_p_ro)
6591 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006592 STRCPY(p + len, _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 len += 4;
6594 }
6595
6596#ifndef FEAT_VERTSPLIT
6597 this_ru_col = ru_col;
6598 if (this_ru_col < (Columns + 1) / 2)
6599 this_ru_col = (Columns + 1) / 2;
6600#else
6601 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
6602 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
6603 this_ru_col = (W_WIDTH(wp) + 1) / 2;
6604 if (this_ru_col <= 1)
6605 {
6606 p = (char_u *)"<"; /* No room for file name! */
6607 len = 1;
6608 }
6609 else
6610#endif
6611#ifdef FEAT_MBYTE
6612 if (has_mbyte)
6613 {
6614 int clen = 0, i;
6615
6616 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006617 clen = mb_string2cells(p, -1);
6618
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619 /* Find first character that will fit.
6620 * Going from start to end is much faster for DBCS. */
6621 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006622 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623 clen -= (*mb_ptr2cells)(p + i);
6624 len = clen;
6625 if (i > 0)
6626 {
6627 p = p + i - 1;
6628 *p = '<';
6629 ++len;
6630 }
6631
6632 }
6633 else
6634#endif
6635 if (len > this_ru_col - 1)
6636 {
6637 p += len - (this_ru_col - 1);
6638 *p = '<';
6639 len = this_ru_col - 1;
6640 }
6641
6642 row = W_WINROW(wp) + wp->w_height;
6643 screen_puts(p, row, W_WINCOL(wp), attr);
6644 screen_fill(row, row + 1, len + W_WINCOL(wp),
6645 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
6646
6647 if (get_keymap_str(wp, NameBuff, MAXPATHL)
6648 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6649 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
6650 - 1 + W_WINCOL(wp)), attr);
6651
6652#ifdef FEAT_CMDL_INFO
6653 win_redr_ruler(wp, TRUE);
6654#endif
6655 }
6656
6657#ifdef FEAT_VERTSPLIT
6658 /*
6659 * May need to draw the character below the vertical separator.
6660 */
6661 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6662 {
6663 if (stl_connected(wp))
6664 fillchar = fillchar_status(&attr, wp == curwin);
6665 else
6666 fillchar = fillchar_vsep(&attr);
6667 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6668 attr);
6669 }
6670#endif
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006671 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672}
6673
Bram Moolenaar238a5642006-02-21 22:12:05 +00006674#ifdef FEAT_STL_OPT
6675/*
6676 * Redraw the status line according to 'statusline' and take care of any
6677 * errors encountered.
6678 */
6679 static void
Bram Moolenaar362f3562009-11-03 16:20:34 +00006680redraw_custom_statusline(wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006681 win_T *wp;
6682{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006683 static int entered = FALSE;
6684 int save_called_emsg = called_emsg;
6685
6686 /* When called recursively return. This can happen when the statusline
6687 * contains an expression that triggers a redraw. */
6688 if (entered)
6689 return;
6690 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006691
6692 called_emsg = FALSE;
6693 win_redr_custom(wp, FALSE);
6694 if (called_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006695 {
6696 /* When there is an error disable the statusline, otherwise the
6697 * display is messed up with errors and a redraw triggers the problem
6698 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00006699 set_string_option_direct((char_u *)"statusline", -1,
6700 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006701 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006702 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00006703 called_emsg |= save_called_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006704 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006705}
6706#endif
6707
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708# ifdef FEAT_VERTSPLIT
6709/*
6710 * Return TRUE if the status line of window "wp" is connected to the status
6711 * line of the window right of it. If not, then it's a vertical separator.
6712 * Only call if (wp->w_vsep_width != 0).
6713 */
6714 int
6715stl_connected(wp)
6716 win_T *wp;
6717{
6718 frame_T *fr;
6719
6720 fr = wp->w_frame;
6721 while (fr->fr_parent != NULL)
6722 {
6723 if (fr->fr_parent->fr_layout == FR_COL)
6724 {
6725 if (fr->fr_next != NULL)
6726 break;
6727 }
6728 else
6729 {
6730 if (fr->fr_next != NULL)
6731 return TRUE;
6732 }
6733 fr = fr->fr_parent;
6734 }
6735 return FALSE;
6736}
6737# endif
6738
6739#endif /* FEAT_WINDOWS */
6740
6741#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
6742/*
6743 * Get the value to show for the language mappings, active 'keymap'.
6744 */
6745 int
6746get_keymap_str(wp, buf, len)
6747 win_T *wp;
6748 char_u *buf; /* buffer for the result */
6749 int len; /* length of buffer */
6750{
6751 char_u *p;
6752
6753 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
6754 return FALSE;
6755
6756 {
6757#ifdef FEAT_EVAL
6758 buf_T *old_curbuf = curbuf;
6759 win_T *old_curwin = curwin;
6760 char_u *s;
6761
6762 curbuf = wp->w_buffer;
6763 curwin = wp;
6764 STRCPY(buf, "b:keymap_name"); /* must be writable */
6765 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006766 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767 --emsg_skip;
6768 curbuf = old_curbuf;
6769 curwin = old_curwin;
6770 if (p == NULL || *p == NUL)
6771#endif
6772 {
6773#ifdef FEAT_KEYMAP
6774 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
6775 p = wp->w_buffer->b_p_keymap;
6776 else
6777#endif
6778 p = (char_u *)"lang";
6779 }
6780 if ((int)(STRLEN(p) + 3) < len)
6781 sprintf((char *)buf, "<%s>", p);
6782 else
6783 buf[0] = NUL;
6784#ifdef FEAT_EVAL
6785 vim_free(s);
6786#endif
6787 }
6788 return buf[0] != NUL;
6789}
6790#endif
6791
6792#if defined(FEAT_STL_OPT) || defined(PROTO)
6793/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006794 * Redraw the status line or ruler of window "wp".
6795 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 */
6797 static void
Bram Moolenaar9372a112005-12-06 19:59:18 +00006798win_redr_custom(wp, draw_ruler)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 win_T *wp;
Bram Moolenaar9372a112005-12-06 19:59:18 +00006800 int draw_ruler; /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801{
Bram Moolenaar1d633412013-12-11 15:52:01 +01006802 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 int attr;
6804 int curattr;
6805 int row;
6806 int col = 0;
6807 int maxwidth;
6808 int width;
6809 int n;
6810 int len;
6811 int fillchar;
6812 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00006813 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006815 struct stl_hlrec hltab[STL_MAX_ITEM];
6816 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006817 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01006818 win_T *ewp;
6819 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820
Bram Moolenaar1d633412013-12-11 15:52:01 +01006821 /* There is a tiny chance that this gets called recursively: When
6822 * redrawing a status line triggers redrawing the ruler or tabline.
6823 * Avoid trouble by not allowing recursion. */
6824 if (entered)
6825 return;
6826 entered = TRUE;
6827
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006829 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006831 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006832 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006833 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00006834 fillchar = ' ';
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006835 attr = hl_attr(HLF_TPF);
6836 maxwidth = Columns;
6837# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006838 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006839# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006841 else
6842 {
6843 row = W_WINROW(wp) + wp->w_height;
6844 fillchar = fillchar_status(&attr, wp == curwin);
6845 maxwidth = W_WIDTH(wp);
6846
6847 if (draw_ruler)
6848 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006849 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006850 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006851 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006852 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006853 if (*++stl == '-')
6854 stl++;
6855 if (atoi((char *)stl))
6856 while (VIM_ISDIGIT(*stl))
6857 stl++;
6858 if (*stl++ != '(')
6859 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006860 }
6861#ifdef FEAT_VERTSPLIT
6862 col = ru_col - (Columns - W_WIDTH(wp));
6863 if (col < (W_WIDTH(wp) + 1) / 2)
6864 col = (W_WIDTH(wp) + 1) / 2;
6865#else
6866 col = ru_col;
6867 if (col > (Columns + 1) / 2)
6868 col = (Columns + 1) / 2;
6869#endif
6870 maxwidth = W_WIDTH(wp) - col;
6871#ifdef FEAT_WINDOWS
6872 if (!wp->w_status_height)
6873#endif
6874 {
6875 row = Rows - 1;
6876 --maxwidth; /* writing in last column may cause scrolling */
6877 fillchar = ' ';
6878 attr = 0;
6879 }
6880
6881# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006882 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006883# endif
6884 }
6885 else
6886 {
6887 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006888 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006889 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00006890 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006891# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006892 use_sandbox = was_set_insecurely((char_u *)"statusline",
6893 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006894# endif
6895 }
6896
6897#ifdef FEAT_VERTSPLIT
6898 col += W_WINCOL(wp);
6899#endif
6900 }
6901
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01006903 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904
Bram Moolenaar61452852011-02-01 18:01:11 +01006905 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
6906 * the cursor away and back. */
6907 ewp = wp == NULL ? curwin : wp;
6908 p_crb_save = ewp->w_p_crb;
6909 ewp->w_p_crb = FALSE;
6910
Bram Moolenaar362f3562009-11-03 16:20:34 +00006911 /* Make a copy, because the statusline may include a function call that
6912 * might change the option value and free the memory. */
6913 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01006914 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00006915 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006916 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006917 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01006918 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01006920 /* Make all characters printable. */
6921 p = transstr(buf);
6922 if (p != NULL)
6923 {
6924 vim_strncpy(buf, p, sizeof(buf) - 1);
6925 vim_free(p);
6926 }
6927
6928 /* fill up with "fillchar" */
6929 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00006930 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 {
6932#ifdef FEAT_MBYTE
6933 len += (*mb_char2bytes)(fillchar, buf + len);
6934#else
6935 buf[len++] = fillchar;
6936#endif
6937 ++width;
6938 }
6939 buf[len] = NUL;
6940
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006941 /*
6942 * Draw each snippet with the specified highlighting.
6943 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944 curattr = attr;
6945 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006946 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006948 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949 screen_puts_len(p, len, row, col, curattr);
6950 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006951 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006953 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006955 else if (hltab[n].userhl < 0)
6956 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00006958 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006959 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960#endif
6961 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006962 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 }
6964 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006965
6966 if (wp == NULL)
6967 {
6968 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
6969 col = 0;
6970 len = 0;
6971 p = buf;
6972 fillchar = 0;
6973 for (n = 0; tabtab[n].start != NULL; n++)
6974 {
6975 len += vim_strnsize(p, (int)(tabtab[n].start - p));
6976 while (col < len)
6977 TabPageIdxs[col++] = fillchar;
6978 p = tabtab[n].start;
6979 fillchar = tabtab[n].userhl;
6980 }
6981 while (col < Columns)
6982 TabPageIdxs[col++] = fillchar;
6983 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01006984
6985theend:
6986 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987}
6988
6989#endif /* FEAT_STL_OPT */
6990
6991/*
6992 * Output a single character directly to the screen and update ScreenLines.
6993 */
6994 void
6995screen_putchar(c, row, col, attr)
6996 int c;
6997 int row, col;
6998 int attr;
6999{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000 char_u buf[MB_MAXBYTES + 1];
7001
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007002#ifdef FEAT_MBYTE
7003 if (has_mbyte)
7004 buf[(*mb_char2bytes)(c, buf)] = NUL;
7005 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007007 {
7008 buf[0] = c;
7009 buf[1] = NUL;
7010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 screen_puts(buf, row, col, attr);
7012}
7013
7014/*
7015 * Get a single character directly from ScreenLines into "bytes[]".
7016 * Also return its attribute in *attrp;
7017 */
7018 void
7019screen_getbytes(row, col, bytes, attrp)
7020 int row, col;
7021 char_u *bytes;
7022 int *attrp;
7023{
7024 unsigned off;
7025
7026 /* safety check */
7027 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7028 {
7029 off = LineOffset[row] + col;
7030 *attrp = ScreenAttrs[off];
7031 bytes[0] = ScreenLines[off];
7032 bytes[1] = NUL;
7033
7034#ifdef FEAT_MBYTE
7035 if (enc_utf8 && ScreenLinesUC[off] != 0)
7036 bytes[utfc_char2bytes(off, bytes)] = NUL;
7037 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7038 {
7039 bytes[0] = ScreenLines[off];
7040 bytes[1] = ScreenLines2[off];
7041 bytes[2] = NUL;
7042 }
7043 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7044 {
7045 bytes[1] = ScreenLines[off + 1];
7046 bytes[2] = NUL;
7047 }
7048#endif
7049 }
7050}
7051
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007052#ifdef FEAT_MBYTE
7053static int screen_comp_differs __ARGS((int, int*));
7054
7055/*
7056 * Return TRUE if composing characters for screen posn "off" differs from
7057 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007058 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007059 */
7060 static int
7061screen_comp_differs(off, u8cc)
7062 int off;
7063 int *u8cc;
7064{
7065 int i;
7066
7067 for (i = 0; i < Screen_mco; ++i)
7068 {
7069 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7070 return TRUE;
7071 if (u8cc[i] == 0)
7072 break;
7073 }
7074 return FALSE;
7075}
7076#endif
7077
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078/*
7079 * Put string '*text' on the screen at position 'row' and 'col', with
7080 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7081 * Note: only outputs within one row, message is truncated at screen boundary!
7082 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7083 */
7084 void
7085screen_puts(text, row, col, attr)
7086 char_u *text;
7087 int row;
7088 int col;
7089 int attr;
7090{
7091 screen_puts_len(text, -1, row, col, attr);
7092}
7093
7094/*
7095 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7096 * a NUL.
7097 */
7098 void
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007099screen_puts_len(text, textlen, row, col, attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 char_u *text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007101 int textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 int row;
7103 int col;
7104 int attr;
7105{
7106 unsigned off;
7107 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007108 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 int c;
7110#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007111 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112 int mbyte_blen = 1;
7113 int mbyte_cells = 1;
7114 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007115 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 int clear_next_cell = FALSE;
7117# ifdef FEAT_ARABIC
7118 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007119 int pc, nc, nc1;
7120 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121# endif
7122#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007123#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7124 int force_redraw_this;
7125 int force_redraw_next = FALSE;
7126#endif
7127 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128
7129 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7130 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007131 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132
Bram Moolenaarc236c162008-07-13 17:41:49 +00007133#ifdef FEAT_MBYTE
7134 /* When drawing over the right halve of a double-wide char clear out the
7135 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007136 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007137# ifdef FEAT_GUI
7138 && !gui.in_use
7139# endif
7140 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007141 {
7142 ScreenLines[off - 1] = ' ';
7143 ScreenAttrs[off - 1] = 0;
7144 if (enc_utf8)
7145 {
7146 ScreenLinesUC[off - 1] = 0;
7147 ScreenLinesC[0][off - 1] = 0;
7148 }
7149 /* redraw the previous cell, make it empty */
7150 screen_char(off - 1, row, col - 1);
7151 /* force the cell at "col" to be redrawn */
7152 force_redraw_next = TRUE;
7153 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007154#endif
7155
Bram Moolenaar367329b2007-08-30 11:53:22 +00007156#ifdef FEAT_MBYTE
7157 max_off = LineOffset[row] + screen_Columns;
7158#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007159 while (col < screen_Columns
7160 && (len < 0 || (int)(ptr - text) < len)
7161 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162 {
7163 c = *ptr;
7164#ifdef FEAT_MBYTE
7165 /* check if this is the first byte of a multibyte */
7166 if (has_mbyte)
7167 {
7168 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007169 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007171 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7173 mbyte_cells = 1;
7174 else if (enc_dbcs != 0)
7175 mbyte_cells = mbyte_blen;
7176 else /* enc_utf8 */
7177 {
7178 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007179 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 (int)((text + len) - ptr));
7181 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007182 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007184# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185 /* Non-BMP character: display as ? or fullwidth ?. */
7186 if (u8c >= 0x10000)
7187 {
7188 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7189 if (attr == 0)
7190 attr = hl_attr(HLF_8);
7191 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007192# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193# ifdef FEAT_ARABIC
7194 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7195 {
7196 /* Do Arabic shaping. */
7197 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7198 {
7199 /* Past end of string to be displayed. */
7200 nc = NUL;
7201 nc1 = NUL;
7202 }
7203 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007204 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007205 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7206 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007207 nc1 = pcc[0];
7208 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 pc = prev_c;
7210 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007211 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 }
7213 else
7214 prev_c = u8c;
7215# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007216 if (col + mbyte_cells > screen_Columns)
7217 {
7218 /* Only 1 cell left, but character requires 2 cells:
7219 * display a '>' in the last column to avoid wrapping. */
7220 c = '>';
7221 mbyte_cells = 1;
7222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223 }
7224 }
7225#endif
7226
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007227#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7228 force_redraw_this = force_redraw_next;
7229 force_redraw_next = FALSE;
7230#endif
7231
7232 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233#ifdef FEAT_MBYTE
7234 || (mbyte_cells == 2
7235 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7236 || (enc_dbcs == DBCS_JPNU
7237 && c == 0x8e
7238 && ScreenLines2[off] != ptr[1])
7239 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007240 && (ScreenLinesUC[off] !=
7241 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7242 || (ScreenLinesUC[off] != 0
7243 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244#endif
7245 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007246 || exmode_active;
7247
7248 if (need_redraw
7249#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7250 || force_redraw_this
7251#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 )
7253 {
7254#if defined(FEAT_GUI) || defined(UNIX)
7255 /* The bold trick makes a single row of pixels appear in the next
7256 * character. When a bold character is removed, the next
7257 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007258 * and for some xterms. */
7259 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260# ifdef FEAT_GUI
7261 gui.in_use
7262# endif
7263# if defined(FEAT_GUI) && defined(UNIX)
7264 ||
7265# endif
7266# ifdef UNIX
7267 term_is_xterm
7268# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007269 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007271 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007273 if (n > HL_ALL)
7274 n = syn_attr2attr(n);
7275 if (n & HL_BOLD)
7276 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 }
7278#endif
7279#ifdef FEAT_MBYTE
7280 /* When at the end of the text and overwriting a two-cell
7281 * character with a one-cell character, need to clear the next
7282 * cell. Also when overwriting the left halve of a two-cell char
7283 * with the right halve of a two-cell char. Do this only once
7284 * (mb_off2cells() may return 2 on the right halve). */
7285 if (clear_next_cell)
7286 clear_next_cell = FALSE;
7287 else if (has_mbyte
7288 && (len < 0 ? ptr[mbyte_blen] == NUL
7289 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007290 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007292 && (*mb_off2cells)(off, max_off) == 1
7293 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294 clear_next_cell = TRUE;
7295
7296 /* Make sure we never leave a second byte of a double-byte behind,
7297 * it confuses mb_off2cells(). */
7298 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007299 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007301 && (*mb_off2cells)(off, max_off) == 1
7302 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303 ScreenLines[off + mbyte_blen] = 0;
7304#endif
7305 ScreenLines[off] = c;
7306 ScreenAttrs[off] = attr;
7307#ifdef FEAT_MBYTE
7308 if (enc_utf8)
7309 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007310 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311 ScreenLinesUC[off] = 0;
7312 else
7313 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007314 int i;
7315
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007317 for (i = 0; i < Screen_mco; ++i)
7318 {
7319 ScreenLinesC[i][off] = u8cc[i];
7320 if (u8cc[i] == 0)
7321 break;
7322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323 }
7324 if (mbyte_cells == 2)
7325 {
7326 ScreenLines[off + 1] = 0;
7327 ScreenAttrs[off + 1] = attr;
7328 }
7329 screen_char(off, row, col);
7330 }
7331 else if (mbyte_cells == 2)
7332 {
7333 ScreenLines[off + 1] = ptr[1];
7334 ScreenAttrs[off + 1] = attr;
7335 screen_char_2(off, row, col);
7336 }
7337 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7338 {
7339 ScreenLines2[off] = ptr[1];
7340 screen_char(off, row, col);
7341 }
7342 else
7343#endif
7344 screen_char(off, row, col);
7345 }
7346#ifdef FEAT_MBYTE
7347 if (has_mbyte)
7348 {
7349 off += mbyte_cells;
7350 col += mbyte_cells;
7351 ptr += mbyte_blen;
7352 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007353 {
7354 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007356 len = -1;
7357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358 }
7359 else
7360#endif
7361 {
7362 ++off;
7363 ++col;
7364 ++ptr;
7365 }
7366 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007367
7368#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7369 /* If we detected the next character needs to be redrawn, but the text
7370 * doesn't extend up to there, update the character here. */
7371 if (force_redraw_next && col < screen_Columns)
7372 {
7373# ifdef FEAT_MBYTE
7374 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7375 screen_char_2(off, row, col);
7376 else
7377# endif
7378 screen_char(off, row, col);
7379 }
7380#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381}
7382
7383#ifdef FEAT_SEARCH_EXTRA
7384/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007385 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386 */
7387 static void
7388start_search_hl()
7389{
7390 if (p_hls && !no_hlsearch)
7391 {
7392 last_pat_prog(&search_hl.rm);
7393 search_hl.attr = hl_attr(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007394# ifdef FEAT_RELTIME
7395 /* Set the time limit to 'redrawtime'. */
7396 profile_setlimit(p_rdt, &search_hl.tm);
7397# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 }
7399}
7400
7401/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007402 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403 */
7404 static void
7405end_search_hl()
7406{
7407 if (search_hl.rm.regprog != NULL)
7408 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007409 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 search_hl.rm.regprog = NULL;
7411 }
7412}
7413
7414/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007415 * Init for calling prepare_search_hl().
7416 */
7417 static void
7418init_search_hl(wp)
7419 win_T *wp;
7420{
7421 matchitem_T *cur;
7422
7423 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7424 * match */
7425 cur = wp->w_match_head;
7426 while (cur != NULL)
7427 {
7428 cur->hl.rm = cur->match;
7429 if (cur->hlg_id == 0)
7430 cur->hl.attr = 0;
7431 else
7432 cur->hl.attr = syn_id2attr(cur->hlg_id);
7433 cur->hl.buf = wp->w_buffer;
7434 cur->hl.lnum = 0;
7435 cur->hl.first_lnum = 0;
7436# ifdef FEAT_RELTIME
7437 /* Set the time limit to 'redrawtime'. */
7438 profile_setlimit(p_rdt, &(cur->hl.tm));
7439# endif
7440 cur = cur->next;
7441 }
7442 search_hl.buf = wp->w_buffer;
7443 search_hl.lnum = 0;
7444 search_hl.first_lnum = 0;
7445 /* time limit is set at the toplevel, for all windows */
7446}
7447
7448/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 * Advance to the match in window "wp" line "lnum" or past it.
7450 */
7451 static void
7452prepare_search_hl(wp, lnum)
7453 win_T *wp;
7454 linenr_T lnum;
7455{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007456 matchitem_T *cur; /* points to the match list */
7457 match_T *shl; /* points to search_hl or a match */
7458 int shl_flag; /* flag to indicate whether search_hl
7459 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007460 int pos_inprogress; /* marks that position match search is
7461 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462 int n;
7463
7464 /*
7465 * When using a multi-line pattern, start searching at the top
7466 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007467 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007469 cur = wp->w_match_head;
7470 shl_flag = FALSE;
7471 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007473 if (shl_flag == FALSE)
7474 {
7475 shl = &search_hl;
7476 shl_flag = TRUE;
7477 }
7478 else
7479 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480 if (shl->rm.regprog != NULL
7481 && shl->lnum == 0
7482 && re_multiline(shl->rm.regprog))
7483 {
7484 if (shl->first_lnum == 0)
7485 {
7486# ifdef FEAT_FOLDING
7487 for (shl->first_lnum = lnum;
7488 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7489 if (hasFoldingWin(wp, shl->first_lnum - 1,
7490 NULL, NULL, TRUE, NULL))
7491 break;
7492# else
7493 shl->first_lnum = wp->w_topline;
7494# endif
7495 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007496 if (cur != NULL)
7497 cur->pos.cur = 0;
7498 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007500 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7501 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007503 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n, cur);
7504 pos_inprogress = cur == NULL || cur->pos.cur == 0
7505 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506 if (shl->lnum != 0)
7507 {
7508 shl->first_lnum = shl->lnum
7509 + shl->rm.endpos[0].lnum
7510 - shl->rm.startpos[0].lnum;
7511 n = shl->rm.endpos[0].col;
7512 }
7513 else
7514 {
7515 ++shl->first_lnum;
7516 n = 0;
7517 }
7518 }
7519 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007520 if (shl != &search_hl && cur != NULL)
7521 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522 }
7523}
7524
7525/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007526 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527 * Uses shl->buf.
7528 * Sets shl->lnum and shl->rm contents.
7529 * Note: Assumes a previous match is always before "lnum", unless
7530 * shl->lnum is zero.
7531 * Careful: Any pointers for buffer lines will become invalid.
7532 */
7533 static void
Bram Moolenaarb3414592014-06-17 17:48:32 +02007534next_search_hl(win, shl, lnum, mincol, cur)
7535 win_T *win;
7536 match_T *shl; /* points to search_hl or a match */
7537 linenr_T lnum;
7538 colnr_T mincol; /* minimal column for a match */
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007539 matchitem_T *cur; /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540{
7541 linenr_T l;
7542 colnr_T matchcol;
7543 long nmatched;
7544
7545 if (shl->lnum != 0)
7546 {
7547 /* Check for three situations:
7548 * 1. If the "lnum" is below a previous match, start a new search.
7549 * 2. If the previous match includes "mincol", use it.
7550 * 3. Continue after the previous match.
7551 */
7552 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7553 if (lnum > l)
7554 shl->lnum = 0;
7555 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7556 return;
7557 }
7558
7559 /*
7560 * Repeat searching for a match until one is found that includes "mincol"
7561 * or none is found in this line.
7562 */
7563 called_emsg = FALSE;
7564 for (;;)
7565 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007566#ifdef FEAT_RELTIME
7567 /* Stop searching after passing the time limit. */
7568 if (profile_passed_limit(&(shl->tm)))
7569 {
7570 shl->lnum = 0; /* no match found in time */
7571 break;
7572 }
7573#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 /* Three situations:
7575 * 1. No useful previous match: search from start of line.
7576 * 2. Not Vi compatible or empty match: continue at next character.
7577 * Break the loop if this is beyond the end of the line.
7578 * 3. Vi compatible searching: continue at end of previous match.
7579 */
7580 if (shl->lnum == 0)
7581 matchcol = 0;
7582 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7583 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007584 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007586 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007587
7588 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007589 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007590 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007592 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593 shl->lnum = 0;
7594 break;
7595 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007596#ifdef FEAT_MBYTE
7597 if (has_mbyte)
7598 matchcol += mb_ptr2len(ml);
7599 else
7600#endif
7601 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007602 }
7603 else
7604 matchcol = shl->rm.endpos[0].col;
7605
7606 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007607 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007609 /* Remember whether shl->rm is using a copy of the regprog in
7610 * cur->match. */
7611 int regprog_is_copy = (shl != &search_hl && cur != NULL
7612 && shl == &cur->hl
7613 && cur->match.regprog == cur->hl.rm.regprog);
7614
Bram Moolenaarb3414592014-06-17 17:48:32 +02007615 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7616 matchcol,
7617#ifdef FEAT_RELTIME
7618 &(shl->tm)
7619#else
7620 NULL
7621#endif
7622 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007623 /* Copy the regprog, in case it got freed and recompiled. */
7624 if (regprog_is_copy)
7625 cur->match.regprog = cur->hl.rm.regprog;
7626
Bram Moolenaarb3414592014-06-17 17:48:32 +02007627 if (called_emsg || got_int)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007628 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007629 /* Error while handling regexp: stop using this regexp. */
7630 if (shl == &search_hl)
7631 {
7632 /* don't free regprog in the match list, it's a copy */
7633 vim_regfree(shl->rm.regprog);
7634 SET_NO_HLSEARCH(TRUE);
7635 }
7636 shl->rm.regprog = NULL;
7637 shl->lnum = 0;
7638 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
7639 message */
7640 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007641 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007642 }
7643 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007644 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007645 else
7646 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647 if (nmatched == 0)
7648 {
7649 shl->lnum = 0; /* no match found */
7650 break;
7651 }
7652 if (shl->rm.startpos[0].lnum > 0
7653 || shl->rm.startpos[0].col >= mincol
7654 || nmatched > 1
7655 || shl->rm.endpos[0].col > mincol)
7656 {
7657 shl->lnum += shl->rm.startpos[0].lnum;
7658 break; /* useful match found */
7659 }
7660 }
7661}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662
Bram Moolenaarb3414592014-06-17 17:48:32 +02007663 static int
7664next_search_hl_pos(shl, lnum, posmatch, mincol)
7665 match_T *shl; /* points to a match */
7666 linenr_T lnum;
7667 posmatch_T *posmatch; /* match positions */
7668 colnr_T mincol; /* minimal column for a match */
7669{
7670 int i;
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02007671 int bot = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007672
7673 shl->lnum = 0;
7674 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
7675 {
7676 if (posmatch->pos[i].lnum == 0)
7677 break;
7678 if (posmatch->pos[i].col < mincol)
7679 continue;
7680 if (posmatch->pos[i].lnum == lnum)
7681 {
7682 if (shl->lnum == lnum)
7683 {
7684 /* partially sort positions by column numbers
7685 * on the same line */
7686 if (posmatch->pos[i].col < posmatch->pos[bot].col)
7687 {
7688 llpos_T tmp = posmatch->pos[i];
7689
7690 posmatch->pos[i] = posmatch->pos[bot];
7691 posmatch->pos[bot] = tmp;
7692 }
7693 }
7694 else
7695 {
7696 bot = i;
7697 shl->lnum = lnum;
7698 }
7699 }
7700 }
7701 posmatch->cur = 0;
7702 if (shl->lnum == lnum)
7703 {
7704 colnr_T start = posmatch->pos[bot].col == 0
7705 ? 0 : posmatch->pos[bot].col - 1;
7706 colnr_T end = posmatch->pos[bot].col == 0
7707 ? MAXCOL : start + posmatch->pos[bot].len;
7708
7709 shl->rm.startpos[0].lnum = 0;
7710 shl->rm.startpos[0].col = start;
7711 shl->rm.endpos[0].lnum = 0;
7712 shl->rm.endpos[0].col = end;
7713 posmatch->cur = bot + 1;
7714 return TRUE;
7715 }
7716 return FALSE;
7717}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02007718#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02007719
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720 static void
7721screen_start_highlight(attr)
7722 int attr;
7723{
7724 attrentry_T *aep = NULL;
7725
7726 screen_attr = attr;
7727 if (full_screen
7728#ifdef WIN3264
7729 && termcap_active
7730#endif
7731 )
7732 {
7733#ifdef FEAT_GUI
7734 if (gui.in_use)
7735 {
7736 char buf[20];
7737
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007738 /* The GUI handles this internally. */
7739 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740 OUT_STR(buf);
7741 }
7742 else
7743#endif
7744 {
7745 if (attr > HL_ALL) /* special HL attr. */
7746 {
7747 if (t_colors > 1)
7748 aep = syn_cterm_attr2entry(attr);
7749 else
7750 aep = syn_term_attr2entry(attr);
7751 if (aep == NULL) /* did ":syntax clear" */
7752 attr = 0;
7753 else
7754 attr = aep->ae_attr;
7755 }
7756 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
7757 out_str(T_MD);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007758 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
7759 && cterm_normal_fg_bold)
7760 /* If the Normal FG color has BOLD attribute and the new HL
7761 * has a FG color defined, clear BOLD. */
7762 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
7764 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007765 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
7766 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767 out_str(T_US);
7768 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
7769 out_str(T_CZH);
7770 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
7771 out_str(T_MR);
7772
7773 /*
7774 * Output the color or start string after bold etc., in case the
7775 * bold etc. override the color setting.
7776 */
7777 if (aep != NULL)
7778 {
7779 if (t_colors > 1)
7780 {
7781 if (aep->ae_u.cterm.fg_color)
7782 term_fg_color(aep->ae_u.cterm.fg_color - 1);
7783 if (aep->ae_u.cterm.bg_color)
7784 term_bg_color(aep->ae_u.cterm.bg_color - 1);
7785 }
7786 else
7787 {
7788 if (aep->ae_u.term.start != NULL)
7789 out_str(aep->ae_u.term.start);
7790 }
7791 }
7792 }
7793 }
7794}
7795
7796 void
7797screen_stop_highlight()
7798{
7799 int do_ME = FALSE; /* output T_ME code */
7800
7801 if (screen_attr != 0
7802#ifdef WIN3264
7803 && termcap_active
7804#endif
7805 )
7806 {
7807#ifdef FEAT_GUI
7808 if (gui.in_use)
7809 {
7810 char buf[20];
7811
7812 /* use internal GUI code */
7813 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
7814 OUT_STR(buf);
7815 }
7816 else
7817#endif
7818 {
7819 if (screen_attr > HL_ALL) /* special HL attr. */
7820 {
7821 attrentry_T *aep;
7822
7823 if (t_colors > 1)
7824 {
7825 /*
7826 * Assume that t_me restores the original colors!
7827 */
7828 aep = syn_cterm_attr2entry(screen_attr);
7829 if (aep != NULL && (aep->ae_u.cterm.fg_color
7830 || aep->ae_u.cterm.bg_color))
7831 do_ME = TRUE;
7832 }
7833 else
7834 {
7835 aep = syn_term_attr2entry(screen_attr);
7836 if (aep != NULL && aep->ae_u.term.stop != NULL)
7837 {
7838 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
7839 do_ME = TRUE;
7840 else
7841 out_str(aep->ae_u.term.stop);
7842 }
7843 }
7844 if (aep == NULL) /* did ":syntax clear" */
7845 screen_attr = 0;
7846 else
7847 screen_attr = aep->ae_attr;
7848 }
7849
7850 /*
7851 * Often all ending-codes are equal to T_ME. Avoid outputting the
7852 * same sequence several times.
7853 */
7854 if (screen_attr & HL_STANDOUT)
7855 {
7856 if (STRCMP(T_SE, T_ME) == 0)
7857 do_ME = TRUE;
7858 else
7859 out_str(T_SE);
7860 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007861 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862 {
7863 if (STRCMP(T_UE, T_ME) == 0)
7864 do_ME = TRUE;
7865 else
7866 out_str(T_UE);
7867 }
7868 if (screen_attr & HL_ITALIC)
7869 {
7870 if (STRCMP(T_CZR, T_ME) == 0)
7871 do_ME = TRUE;
7872 else
7873 out_str(T_CZR);
7874 }
7875 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
7876 out_str(T_ME);
7877
7878 if (t_colors > 1)
7879 {
7880 /* set Normal cterm colors */
7881 if (cterm_normal_fg_color != 0)
7882 term_fg_color(cterm_normal_fg_color - 1);
7883 if (cterm_normal_bg_color != 0)
7884 term_bg_color(cterm_normal_bg_color - 1);
7885 if (cterm_normal_fg_bold)
7886 out_str(T_MD);
7887 }
7888 }
7889 }
7890 screen_attr = 0;
7891}
7892
7893/*
7894 * Reset the colors for a cterm. Used when leaving Vim.
7895 * The machine specific code may override this again.
7896 */
7897 void
7898reset_cterm_colors()
7899{
7900 if (t_colors > 1)
7901 {
7902 /* set Normal cterm colors */
7903 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
7904 {
7905 out_str(T_OP);
7906 screen_attr = -1;
7907 }
7908 if (cterm_normal_fg_bold)
7909 {
7910 out_str(T_ME);
7911 screen_attr = -1;
7912 }
7913 }
7914}
7915
7916/*
7917 * Put character ScreenLines["off"] on the screen at position "row" and "col",
7918 * using the attributes from ScreenAttrs["off"].
7919 */
7920 static void
7921screen_char(off, row, col)
7922 unsigned off;
7923 int row;
7924 int col;
7925{
7926 int attr;
7927
7928 /* Check for illegal values, just in case (could happen just after
7929 * resizing). */
7930 if (row >= screen_Rows || col >= screen_Columns)
7931 return;
7932
7933 /* Outputting the last character on the screen may scrollup the screen.
7934 * Don't to it! Mark the character invalid (update it when scrolled up) */
7935 if (row == screen_Rows - 1 && col == screen_Columns - 1
7936#ifdef FEAT_RIGHTLEFT
7937 /* account for first command-line character in rightleft mode */
7938 && !cmdmsg_rl
7939#endif
7940 )
7941 {
7942 ScreenAttrs[off] = (sattr_T)-1;
7943 return;
7944 }
7945
7946 /*
7947 * Stop highlighting first, so it's easier to move the cursor.
7948 */
7949#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
7950 if (screen_char_attr != 0)
7951 attr = screen_char_attr;
7952 else
7953#endif
7954 attr = ScreenAttrs[off];
7955 if (screen_attr != attr)
7956 screen_stop_highlight();
7957
7958 windgoto(row, col);
7959
7960 if (screen_attr != attr)
7961 screen_start_highlight(attr);
7962
7963#ifdef FEAT_MBYTE
7964 if (enc_utf8 && ScreenLinesUC[off] != 0)
7965 {
7966 char_u buf[MB_MAXBYTES + 1];
7967
7968 /* Convert UTF-8 character to bytes and write it. */
7969
7970 buf[utfc_char2bytes(off, buf)] = NUL;
7971
7972 out_str(buf);
7973 if (utf_char2cells(ScreenLinesUC[off]) > 1)
7974 ++screen_cur_col;
7975 }
7976 else
7977#endif
7978 {
7979#ifdef FEAT_MBYTE
7980 out_flush_check();
7981#endif
7982 out_char(ScreenLines[off]);
7983#ifdef FEAT_MBYTE
7984 /* double-byte character in single-width cell */
7985 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7986 out_char(ScreenLines2[off]);
7987#endif
7988 }
7989
7990 screen_cur_col++;
7991}
7992
7993#ifdef FEAT_MBYTE
7994
7995/*
7996 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
7997 * on the screen at position 'row' and 'col'.
7998 * The attributes of the first byte is used for all. This is required to
7999 * output the two bytes of a double-byte character with nothing in between.
8000 */
8001 static void
8002screen_char_2(off, row, col)
8003 unsigned off;
8004 int row;
8005 int col;
8006{
8007 /* Check for illegal values (could be wrong when screen was resized). */
8008 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8009 return;
8010
8011 /* Outputting the last character on the screen may scrollup the screen.
8012 * Don't to it! Mark the character invalid (update it when scrolled up) */
8013 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8014 {
8015 ScreenAttrs[off] = (sattr_T)-1;
8016 return;
8017 }
8018
8019 /* Output the first byte normally (positions the cursor), then write the
8020 * second byte directly. */
8021 screen_char(off, row, col);
8022 out_char(ScreenLines[off + 1]);
8023 ++screen_cur_col;
8024}
8025#endif
8026
8027#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
8028/*
8029 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8030 * This uses the contents of ScreenLines[] and doesn't change it.
8031 */
8032 void
8033screen_draw_rectangle(row, col, height, width, invert)
8034 int row;
8035 int col;
8036 int height;
8037 int width;
8038 int invert;
8039{
8040 int r, c;
8041 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008042#ifdef FEAT_MBYTE
8043 int max_off;
8044#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008046 /* Can't use ScreenLines unless initialized */
8047 if (ScreenLines == NULL)
8048 return;
8049
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050 if (invert)
8051 screen_char_attr = HL_INVERSE;
8052 for (r = row; r < row + height; ++r)
8053 {
8054 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008055#ifdef FEAT_MBYTE
8056 max_off = off + screen_Columns;
8057#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058 for (c = col; c < col + width; ++c)
8059 {
8060#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008061 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062 {
8063 screen_char_2(off + c, r, c);
8064 ++c;
8065 }
8066 else
8067#endif
8068 {
8069 screen_char(off + c, r, c);
8070#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008071 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072 ++c;
8073#endif
8074 }
8075 }
8076 }
8077 screen_char_attr = 0;
8078}
8079#endif
8080
8081#ifdef FEAT_VERTSPLIT
8082/*
8083 * Redraw the characters for a vertically split window.
8084 */
8085 static void
8086redraw_block(row, end, wp)
8087 int row;
8088 int end;
8089 win_T *wp;
8090{
8091 int col;
8092 int width;
8093
8094# ifdef FEAT_CLIPBOARD
8095 clip_may_clear_selection(row, end - 1);
8096# endif
8097
8098 if (wp == NULL)
8099 {
8100 col = 0;
8101 width = Columns;
8102 }
8103 else
8104 {
8105 col = wp->w_wincol;
8106 width = wp->w_width;
8107 }
8108 screen_draw_rectangle(row, col, end - row, width, FALSE);
8109}
8110#endif
8111
8112/*
8113 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8114 * with character 'c1' in first column followed by 'c2' in the other columns.
8115 * Use attributes 'attr'.
8116 */
8117 void
8118screen_fill(start_row, end_row, start_col, end_col, c1, c2, attr)
8119 int start_row, end_row;
8120 int start_col, end_col;
8121 int c1, c2;
8122 int attr;
8123{
8124 int row;
8125 int col;
8126 int off;
8127 int end_off;
8128 int did_delete;
8129 int c;
8130 int norm_term;
8131#if defined(FEAT_GUI) || defined(UNIX)
8132 int force_next = FALSE;
8133#endif
8134
8135 if (end_row > screen_Rows) /* safety check */
8136 end_row = screen_Rows;
8137 if (end_col > screen_Columns) /* safety check */
8138 end_col = screen_Columns;
8139 if (ScreenLines == NULL
8140 || start_row >= end_row
8141 || start_col >= end_col) /* nothing to do */
8142 return;
8143
8144 /* it's a "normal" terminal when not in a GUI or cterm */
8145 norm_term = (
8146#ifdef FEAT_GUI
8147 !gui.in_use &&
8148#endif
8149 t_colors <= 1);
8150 for (row = start_row; row < end_row; ++row)
8151 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008152#ifdef FEAT_MBYTE
8153 if (has_mbyte
8154# ifdef FEAT_GUI
8155 && !gui.in_use
8156# endif
8157 )
8158 {
8159 /* When drawing over the right halve of a double-wide char clear
8160 * out the left halve. When drawing over the left halve of a
8161 * double wide-char clear out the right halve. Only needed in a
8162 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008163 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008164 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008165 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008166 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008167 }
8168#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169 /*
8170 * Try to use delete-line termcap code, when no attributes or in a
8171 * "normal" terminal, where a bold/italic space is just a
8172 * space.
8173 */
8174 did_delete = FALSE;
8175 if (c2 == ' '
8176 && end_col == Columns
8177 && can_clear(T_CE)
8178 && (attr == 0
8179 || (norm_term
8180 && attr <= HL_ALL
8181 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8182 {
8183 /*
8184 * check if we really need to clear something
8185 */
8186 col = start_col;
8187 if (c1 != ' ') /* don't clear first char */
8188 ++col;
8189
8190 off = LineOffset[row] + col;
8191 end_off = LineOffset[row] + end_col;
8192
8193 /* skip blanks (used often, keep it fast!) */
8194#ifdef FEAT_MBYTE
8195 if (enc_utf8)
8196 while (off < end_off && ScreenLines[off] == ' '
8197 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8198 ++off;
8199 else
8200#endif
8201 while (off < end_off && ScreenLines[off] == ' '
8202 && ScreenAttrs[off] == 0)
8203 ++off;
8204 if (off < end_off) /* something to be cleared */
8205 {
8206 col = off - LineOffset[row];
8207 screen_stop_highlight();
8208 term_windgoto(row, col);/* clear rest of this screen line */
8209 out_str(T_CE);
8210 screen_start(); /* don't know where cursor is now */
8211 col = end_col - col;
8212 while (col--) /* clear chars in ScreenLines */
8213 {
8214 ScreenLines[off] = ' ';
8215#ifdef FEAT_MBYTE
8216 if (enc_utf8)
8217 ScreenLinesUC[off] = 0;
8218#endif
8219 ScreenAttrs[off] = 0;
8220 ++off;
8221 }
8222 }
8223 did_delete = TRUE; /* the chars are cleared now */
8224 }
8225
8226 off = LineOffset[row] + start_col;
8227 c = c1;
8228 for (col = start_col; col < end_col; ++col)
8229 {
8230 if (ScreenLines[off] != c
8231#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008232 || (enc_utf8 && (int)ScreenLinesUC[off]
8233 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234#endif
8235 || ScreenAttrs[off] != attr
8236#if defined(FEAT_GUI) || defined(UNIX)
8237 || force_next
8238#endif
8239 )
8240 {
8241#if defined(FEAT_GUI) || defined(UNIX)
8242 /* The bold trick may make a single row of pixels appear in
8243 * the next character. When a bold character is removed, the
8244 * next character should be redrawn too. This happens for our
8245 * own GUI and for some xterms. */
8246 if (
8247# ifdef FEAT_GUI
8248 gui.in_use
8249# endif
8250# if defined(FEAT_GUI) && defined(UNIX)
8251 ||
8252# endif
8253# ifdef UNIX
8254 term_is_xterm
8255# endif
8256 )
8257 {
8258 if (ScreenLines[off] != ' '
8259 && (ScreenAttrs[off] > HL_ALL
8260 || ScreenAttrs[off] & HL_BOLD))
8261 force_next = TRUE;
8262 else
8263 force_next = FALSE;
8264 }
8265#endif
8266 ScreenLines[off] = c;
8267#ifdef FEAT_MBYTE
8268 if (enc_utf8)
8269 {
8270 if (c >= 0x80)
8271 {
8272 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008273 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274 }
8275 else
8276 ScreenLinesUC[off] = 0;
8277 }
8278#endif
8279 ScreenAttrs[off] = attr;
8280 if (!did_delete || c != ' ')
8281 screen_char(off, row, col);
8282 }
8283 ++off;
8284 if (col == start_col)
8285 {
8286 if (did_delete)
8287 break;
8288 c = c2;
8289 }
8290 }
8291 if (end_col == Columns)
8292 LineWraps[row] = FALSE;
8293 if (row == Rows - 1) /* overwritten the command line */
8294 {
8295 redraw_cmdline = TRUE;
8296 if (c1 == ' ' && c2 == ' ')
8297 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008298 if (start_col == 0)
8299 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 }
8301 }
8302}
8303
8304/*
8305 * Check if there should be a delay. Used before clearing or redrawing the
8306 * screen or the command line.
8307 */
8308 void
8309check_for_delay(check_msg_scroll)
8310 int check_msg_scroll;
8311{
8312 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8313 && !did_wait_return
8314 && emsg_silent == 0)
8315 {
8316 out_flush();
8317 ui_delay(1000L, TRUE);
8318 emsg_on_display = FALSE;
8319 if (check_msg_scroll)
8320 msg_scroll = FALSE;
8321 }
8322}
8323
8324/*
8325 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008326 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 * Returns TRUE if there is a valid screen to write to.
8328 * Returns FALSE when starting up and screen not initialized yet.
8329 */
8330 int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008331screen_valid(doclear)
8332 int doclear;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008334 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 return (ScreenLines != NULL);
8336}
8337
8338/*
8339 * Resize the shell to Rows and Columns.
8340 * Allocate ScreenLines[] and associated items.
8341 *
8342 * There may be some time between setting Rows and Columns and (re)allocating
8343 * ScreenLines[]. This happens when starting up and when (manually) changing
8344 * the shell size. Always use screen_Rows and screen_Columns to access items
8345 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8346 * final size of the shell is needed.
8347 */
8348 void
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008349screenalloc(doclear)
8350 int doclear;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351{
8352 int new_row, old_row;
8353#ifdef FEAT_GUI
8354 int old_Rows;
8355#endif
8356 win_T *wp;
8357 int outofmem = FALSE;
8358 int len;
8359 schar_T *new_ScreenLines;
8360#ifdef FEAT_MBYTE
8361 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008362 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008364 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365#endif
8366 sattr_T *new_ScreenAttrs;
8367 unsigned *new_LineOffset;
8368 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008369#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008370 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008371 tabpage_T *tp;
8372#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008374 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008375#ifdef FEAT_AUTOCMD
8376 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008378retry:
8379#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380 /*
8381 * Allocation of the screen buffers is done only when the size changes and
8382 * when Rows and Columns have been set and we have started doing full
8383 * screen stuff.
8384 */
8385 if ((ScreenLines != NULL
8386 && Rows == screen_Rows
8387 && Columns == screen_Columns
8388#ifdef FEAT_MBYTE
8389 && enc_utf8 == (ScreenLinesUC != NULL)
8390 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008391 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392#endif
8393 )
8394 || Rows == 0
8395 || Columns == 0
8396 || (!full_screen && ScreenLines == NULL))
8397 return;
8398
8399 /*
8400 * It's possible that we produce an out-of-memory message below, which
8401 * will cause this function to be called again. To break the loop, just
8402 * return here.
8403 */
8404 if (entered)
8405 return;
8406 entered = TRUE;
8407
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008408 /*
8409 * Note that the window sizes are updated before reallocating the arrays,
8410 * thus we must not redraw here!
8411 */
8412 ++RedrawingDisabled;
8413
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414 win_new_shellsize(); /* fit the windows in the new sized shell */
8415
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416 comp_col(); /* recompute columns for shown command and ruler */
8417
8418 /*
8419 * We're changing the size of the screen.
8420 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8421 * - Move lines from the old arrays into the new arrays, clear extra
8422 * lines (unless the screen is going to be cleared).
8423 * - Free the old arrays.
8424 *
8425 * If anything fails, make ScreenLines NULL, so we don't do anything!
8426 * Continuing with the old ScreenLines may result in a crash, because the
8427 * size is wrong.
8428 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008429 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008431#ifdef FEAT_AUTOCMD
8432 if (aucmd_win != NULL)
8433 win_free_lsize(aucmd_win);
8434#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435
8436 new_ScreenLines = (schar_T *)lalloc((long_u)(
8437 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8438#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008439 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440 if (enc_utf8)
8441 {
8442 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8443 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008444 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008445 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8447 }
8448 if (enc_dbcs == DBCS_JPNU)
8449 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8450 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8451#endif
8452 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8453 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8454 new_LineOffset = (unsigned *)lalloc((long_u)(
8455 Rows * sizeof(unsigned)), FALSE);
8456 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008457#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008458 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008459#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008461 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008462 {
8463 if (win_alloc_lines(wp) == FAIL)
8464 {
8465 outofmem = TRUE;
8466#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008467 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468#endif
8469 }
8470 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008471#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008472 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8473 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008474 outofmem = TRUE;
8475#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008476#ifdef FEAT_WINDOWS
8477give_up:
8478#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008480#ifdef FEAT_MBYTE
8481 for (i = 0; i < p_mco; ++i)
8482 if (new_ScreenLinesC[i] == NULL)
8483 break;
8484#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485 if (new_ScreenLines == NULL
8486#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008487 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8489#endif
8490 || new_ScreenAttrs == NULL
8491 || new_LineOffset == NULL
8492 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008493#ifdef FEAT_WINDOWS
8494 || new_TabPageIdxs == NULL
8495#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496 || outofmem)
8497 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008498 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008499 {
8500 /* guess the size */
8501 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8502
8503 /* Remember we did this to avoid getting outofmem messages over
8504 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008505 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507 vim_free(new_ScreenLines);
8508 new_ScreenLines = NULL;
8509#ifdef FEAT_MBYTE
8510 vim_free(new_ScreenLinesUC);
8511 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008512 for (i = 0; i < p_mco; ++i)
8513 {
8514 vim_free(new_ScreenLinesC[i]);
8515 new_ScreenLinesC[i] = NULL;
8516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 vim_free(new_ScreenLines2);
8518 new_ScreenLines2 = NULL;
8519#endif
8520 vim_free(new_ScreenAttrs);
8521 new_ScreenAttrs = NULL;
8522 vim_free(new_LineOffset);
8523 new_LineOffset = NULL;
8524 vim_free(new_LineWraps);
8525 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008526#ifdef FEAT_WINDOWS
8527 vim_free(new_TabPageIdxs);
8528 new_TabPageIdxs = NULL;
8529#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530 }
8531 else
8532 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008533 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008534
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535 for (new_row = 0; new_row < Rows; ++new_row)
8536 {
8537 new_LineOffset[new_row] = new_row * Columns;
8538 new_LineWraps[new_row] = FALSE;
8539
8540 /*
8541 * If the screen is not going to be cleared, copy as much as
8542 * possible from the old screen to the new one and clear the rest
8543 * (used when resizing the window at the "--more--" prompt or when
8544 * executing an external command, for the GUI).
8545 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008546 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547 {
8548 (void)vim_memset(new_ScreenLines + new_row * Columns,
8549 ' ', (size_t)Columns * sizeof(schar_T));
8550#ifdef FEAT_MBYTE
8551 if (enc_utf8)
8552 {
8553 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8554 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008555 for (i = 0; i < p_mco; ++i)
8556 (void)vim_memset(new_ScreenLinesC[i]
8557 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558 0, (size_t)Columns * sizeof(u8char_T));
8559 }
8560 if (enc_dbcs == DBCS_JPNU)
8561 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8562 0, (size_t)Columns * sizeof(schar_T));
8563#endif
8564 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8565 0, (size_t)Columns * sizeof(sattr_T));
8566 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008567 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568 {
8569 if (screen_Columns < Columns)
8570 len = screen_Columns;
8571 else
8572 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008573#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008574 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008575 * may be invalid now. Also when p_mco changes. */
8576 if (!(enc_utf8 && ScreenLinesUC == NULL)
8577 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008578#endif
8579 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8580 ScreenLines + LineOffset[old_row],
8581 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008583 if (enc_utf8 && ScreenLinesUC != NULL
8584 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585 {
8586 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8587 ScreenLinesUC + LineOffset[old_row],
8588 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008589 for (i = 0; i < p_mco; ++i)
8590 mch_memmove(new_ScreenLinesC[i]
8591 + new_LineOffset[new_row],
8592 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593 (size_t)len * sizeof(u8char_T));
8594 }
8595 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8596 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8597 ScreenLines2 + LineOffset[old_row],
8598 (size_t)len * sizeof(schar_T));
8599#endif
8600 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8601 ScreenAttrs + LineOffset[old_row],
8602 (size_t)len * sizeof(sattr_T));
8603 }
8604 }
8605 }
8606 /* Use the last line of the screen for the current line. */
8607 current_ScreenLine = new_ScreenLines + Rows * Columns;
8608 }
8609
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008610 free_screenlines();
8611
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612 ScreenLines = new_ScreenLines;
8613#ifdef FEAT_MBYTE
8614 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008615 for (i = 0; i < p_mco; ++i)
8616 ScreenLinesC[i] = new_ScreenLinesC[i];
8617 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618 ScreenLines2 = new_ScreenLines2;
8619#endif
8620 ScreenAttrs = new_ScreenAttrs;
8621 LineOffset = new_LineOffset;
8622 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008623#ifdef FEAT_WINDOWS
8624 TabPageIdxs = new_TabPageIdxs;
8625#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626
8627 /* It's important that screen_Rows and screen_Columns reflect the actual
8628 * size of ScreenLines[]. Set them before calling anything. */
8629#ifdef FEAT_GUI
8630 old_Rows = screen_Rows;
8631#endif
8632 screen_Rows = Rows;
8633 screen_Columns = Columns;
8634
8635 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008636 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637 screenclear2();
8638
8639#ifdef FEAT_GUI
8640 else if (gui.in_use
8641 && !gui.starting
8642 && ScreenLines != NULL
8643 && old_Rows != Rows)
8644 {
8645 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8646 /*
8647 * Adjust the position of the cursor, for when executing an external
8648 * command.
8649 */
8650 if (msg_row >= Rows) /* Rows got smaller */
8651 msg_row = Rows - 1; /* put cursor at last row */
8652 else if (Rows > old_Rows) /* Rows got bigger */
8653 msg_row += Rows - old_Rows; /* put cursor in same place */
8654 if (msg_col >= Columns) /* Columns got smaller */
8655 msg_col = Columns - 1; /* put cursor at last column */
8656 }
8657#endif
8658
Bram Moolenaar071d4272004-06-13 20:20:40 +00008659 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008660 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008661
8662#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008663 /*
8664 * Do not apply autocommands more than 3 times to avoid an endless loop
8665 * in case applying autocommands always changes Rows or Columns.
8666 */
8667 if (starting == 0 && ++retry_count <= 3)
8668 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008669 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008670 /* In rare cases, autocommands may have altered Rows or Columns,
8671 * jump back to check if we need to allocate the screen again. */
8672 goto retry;
8673 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008674#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675}
8676
8677 void
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008678free_screenlines()
8679{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008680#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008681 int i;
8682
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008683 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008684 for (i = 0; i < Screen_mco; ++i)
8685 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008686 vim_free(ScreenLines2);
8687#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008688 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008689 vim_free(ScreenAttrs);
8690 vim_free(LineOffset);
8691 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008692#ifdef FEAT_WINDOWS
8693 vim_free(TabPageIdxs);
8694#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008695}
8696
8697 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698screenclear()
8699{
8700 check_for_delay(FALSE);
8701 screenalloc(FALSE); /* allocate screen buffers if size changed */
8702 screenclear2(); /* clear the screen */
8703}
8704
8705 static void
8706screenclear2()
8707{
8708 int i;
8709
8710 if (starting == NO_SCREEN || ScreenLines == NULL
8711#ifdef FEAT_GUI
8712 || (gui.in_use && gui.starting)
8713#endif
8714 )
8715 return;
8716
8717#ifdef FEAT_GUI
8718 if (!gui.in_use)
8719#endif
8720 screen_attr = -1; /* force setting the Normal colors */
8721 screen_stop_highlight(); /* don't want highlighting here */
8722
8723#ifdef FEAT_CLIPBOARD
8724 /* disable selection without redrawing it */
8725 clip_scroll_selection(9999);
8726#endif
8727
8728 /* blank out ScreenLines */
8729 for (i = 0; i < Rows; ++i)
8730 {
8731 lineclear(LineOffset[i], (int)Columns);
8732 LineWraps[i] = FALSE;
8733 }
8734
8735 if (can_clear(T_CL))
8736 {
8737 out_str(T_CL); /* clear the display */
8738 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008739 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740 }
8741 else
8742 {
8743 /* can't clear the screen, mark all chars with invalid attributes */
8744 for (i = 0; i < Rows; ++i)
8745 lineinvalid(LineOffset[i], (int)Columns);
8746 clear_cmdline = TRUE;
8747 }
8748
8749 screen_cleared = TRUE; /* can use contents of ScreenLines now */
8750
8751 win_rest_invalid(firstwin);
8752 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008753#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008754 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008755#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756 if (must_redraw == CLEAR) /* no need to clear again */
8757 must_redraw = NOT_VALID;
8758 compute_cmdrow();
8759 msg_row = cmdline_row; /* put cursor on last line for messages */
8760 msg_col = 0;
8761 screen_start(); /* don't know where cursor is now */
8762 msg_scrolled = 0; /* can't scroll back */
8763 msg_didany = FALSE;
8764 msg_didout = FALSE;
8765}
8766
8767/*
8768 * Clear one line in ScreenLines.
8769 */
8770 static void
8771lineclear(off, width)
8772 unsigned off;
8773 int width;
8774{
8775 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
8776#ifdef FEAT_MBYTE
8777 if (enc_utf8)
8778 (void)vim_memset(ScreenLinesUC + off, 0,
8779 (size_t)width * sizeof(u8char_T));
8780#endif
8781 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
8782}
8783
8784/*
8785 * Mark one line in ScreenLines invalid by setting the attributes to an
8786 * invalid value.
8787 */
8788 static void
8789lineinvalid(off, width)
8790 unsigned off;
8791 int width;
8792{
8793 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
8794}
8795
8796#ifdef FEAT_VERTSPLIT
8797/*
8798 * Copy part of a Screenline for vertically split window "wp".
8799 */
8800 static void
8801linecopy(to, from, wp)
8802 int to;
8803 int from;
8804 win_T *wp;
8805{
8806 unsigned off_to = LineOffset[to] + wp->w_wincol;
8807 unsigned off_from = LineOffset[from] + wp->w_wincol;
8808
8809 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
8810 wp->w_width * sizeof(schar_T));
8811# ifdef FEAT_MBYTE
8812 if (enc_utf8)
8813 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008814 int i;
8815
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
8817 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008818 for (i = 0; i < p_mco; ++i)
8819 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
8820 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821 }
8822 if (enc_dbcs == DBCS_JPNU)
8823 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
8824 wp->w_width * sizeof(schar_T));
8825# endif
8826 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
8827 wp->w_width * sizeof(sattr_T));
8828}
8829#endif
8830
8831/*
8832 * Return TRUE if clearing with term string "p" would work.
8833 * It can't work when the string is empty or it won't set the right background.
8834 */
8835 int
8836can_clear(p)
8837 char_u *p;
8838{
8839 return (*p != NUL && (t_colors <= 1
8840#ifdef FEAT_GUI
8841 || gui.in_use
8842#endif
8843 || cterm_normal_bg_color == 0 || *T_UT != NUL));
8844}
8845
8846/*
8847 * Reset cursor position. Use whenever cursor was moved because of outputting
8848 * something directly to the screen (shell commands) or a terminal control
8849 * code.
8850 */
8851 void
8852screen_start()
8853{
8854 screen_cur_row = screen_cur_col = 9999;
8855}
8856
8857/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858 * Move the cursor to position "row","col" in the screen.
8859 * This tries to find the most efficient way to move, minimizing the number of
8860 * characters sent to the terminal.
8861 */
8862 void
8863windgoto(row, col)
8864 int row;
8865 int col;
8866{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008867 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868 int i;
8869 int plan;
8870 int cost;
8871 int wouldbe_col;
8872 int noinvcurs;
8873 char_u *bs;
8874 int goto_cost;
8875 int attr;
8876
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008877#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
8879
8880#define PLAN_LE 1
8881#define PLAN_CR 2
8882#define PLAN_NL 3
8883#define PLAN_WRITE 4
8884 /* Can't use ScreenLines unless initialized */
8885 if (ScreenLines == NULL)
8886 return;
8887
8888 if (col != screen_cur_col || row != screen_cur_row)
8889 {
8890 /* Check for valid position. */
8891 if (row < 0) /* window without text lines? */
8892 row = 0;
8893 if (row >= screen_Rows)
8894 row = screen_Rows - 1;
8895 if (col >= screen_Columns)
8896 col = screen_Columns - 1;
8897
8898 /* check if no cursor movement is allowed in highlight mode */
8899 if (screen_attr && *T_MS == NUL)
8900 noinvcurs = HIGHL_COST;
8901 else
8902 noinvcurs = 0;
8903 goto_cost = GOTO_COST + noinvcurs;
8904
8905 /*
8906 * Plan how to do the positioning:
8907 * 1. Use CR to move it to column 0, same row.
8908 * 2. Use T_LE to move it a few columns to the left.
8909 * 3. Use NL to move a few lines down, column 0.
8910 * 4. Move a few columns to the right with T_ND or by writing chars.
8911 *
8912 * Don't do this if the cursor went beyond the last column, the cursor
8913 * position is unknown then (some terminals wrap, some don't )
8914 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008915 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916 * characters to move the cursor to the right.
8917 */
8918 if (row >= screen_cur_row && screen_cur_col < Columns)
8919 {
8920 /*
8921 * If the cursor is in the same row, bigger col, we can use CR
8922 * or T_LE.
8923 */
8924 bs = NULL; /* init for GCC */
8925 attr = screen_attr;
8926 if (row == screen_cur_row && col < screen_cur_col)
8927 {
8928 /* "le" is preferred over "bc", because "bc" is obsolete */
8929 if (*T_LE)
8930 bs = T_LE; /* "cursor left" */
8931 else
8932 bs = T_BC; /* "backspace character (old) */
8933 if (*bs)
8934 cost = (screen_cur_col - col) * (int)STRLEN(bs);
8935 else
8936 cost = 999;
8937 if (col + 1 < cost) /* using CR is less characters */
8938 {
8939 plan = PLAN_CR;
8940 wouldbe_col = 0;
8941 cost = 1; /* CR is just one character */
8942 }
8943 else
8944 {
8945 plan = PLAN_LE;
8946 wouldbe_col = col;
8947 }
8948 if (noinvcurs) /* will stop highlighting */
8949 {
8950 cost += noinvcurs;
8951 attr = 0;
8952 }
8953 }
8954
8955 /*
8956 * If the cursor is above where we want to be, we can use CR LF.
8957 */
8958 else if (row > screen_cur_row)
8959 {
8960 plan = PLAN_NL;
8961 wouldbe_col = 0;
8962 cost = (row - screen_cur_row) * 2; /* CR LF */
8963 if (noinvcurs) /* will stop highlighting */
8964 {
8965 cost += noinvcurs;
8966 attr = 0;
8967 }
8968 }
8969
8970 /*
8971 * If the cursor is in the same row, smaller col, just use write.
8972 */
8973 else
8974 {
8975 plan = PLAN_WRITE;
8976 wouldbe_col = screen_cur_col;
8977 cost = 0;
8978 }
8979
8980 /*
8981 * Check if any characters that need to be written have the
8982 * correct attributes. Also avoid UTF-8 characters.
8983 */
8984 i = col - wouldbe_col;
8985 if (i > 0)
8986 cost += i;
8987 if (cost < goto_cost && i > 0)
8988 {
8989 /*
8990 * Check if the attributes are correct without additionally
8991 * stopping highlighting.
8992 */
8993 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
8994 while (i && *p++ == attr)
8995 --i;
8996 if (i != 0)
8997 {
8998 /*
8999 * Try if it works when highlighting is stopped here.
9000 */
9001 if (*--p == 0)
9002 {
9003 cost += noinvcurs;
9004 while (i && *p++ == 0)
9005 --i;
9006 }
9007 if (i != 0)
9008 cost = 999; /* different attributes, don't do it */
9009 }
9010#ifdef FEAT_MBYTE
9011 if (enc_utf8)
9012 {
9013 /* Don't use an UTF-8 char for positioning, it's slow. */
9014 for (i = wouldbe_col; i < col; ++i)
9015 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9016 {
9017 cost = 999;
9018 break;
9019 }
9020 }
9021#endif
9022 }
9023
9024 /*
9025 * We can do it without term_windgoto()!
9026 */
9027 if (cost < goto_cost)
9028 {
9029 if (plan == PLAN_LE)
9030 {
9031 if (noinvcurs)
9032 screen_stop_highlight();
9033 while (screen_cur_col > col)
9034 {
9035 out_str(bs);
9036 --screen_cur_col;
9037 }
9038 }
9039 else if (plan == PLAN_CR)
9040 {
9041 if (noinvcurs)
9042 screen_stop_highlight();
9043 out_char('\r');
9044 screen_cur_col = 0;
9045 }
9046 else if (plan == PLAN_NL)
9047 {
9048 if (noinvcurs)
9049 screen_stop_highlight();
9050 while (screen_cur_row < row)
9051 {
9052 out_char('\n');
9053 ++screen_cur_row;
9054 }
9055 screen_cur_col = 0;
9056 }
9057
9058 i = col - screen_cur_col;
9059 if (i > 0)
9060 {
9061 /*
9062 * Use cursor-right if it's one character only. Avoids
9063 * removing a line of pixels from the last bold char, when
9064 * using the bold trick in the GUI.
9065 */
9066 if (T_ND[0] != NUL && T_ND[1] == NUL)
9067 {
9068 while (i-- > 0)
9069 out_char(*T_ND);
9070 }
9071 else
9072 {
9073 int off;
9074
9075 off = LineOffset[row] + screen_cur_col;
9076 while (i-- > 0)
9077 {
9078 if (ScreenAttrs[off] != screen_attr)
9079 screen_stop_highlight();
9080#ifdef FEAT_MBYTE
9081 out_flush_check();
9082#endif
9083 out_char(ScreenLines[off]);
9084#ifdef FEAT_MBYTE
9085 if (enc_dbcs == DBCS_JPNU
9086 && ScreenLines[off] == 0x8e)
9087 out_char(ScreenLines2[off]);
9088#endif
9089 ++off;
9090 }
9091 }
9092 }
9093 }
9094 }
9095 else
9096 cost = 999;
9097
9098 if (cost >= goto_cost)
9099 {
9100 if (noinvcurs)
9101 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009102 if (row == screen_cur_row && (col > screen_cur_col)
9103 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009104 term_cursor_right(col - screen_cur_col);
9105 else
9106 term_windgoto(row, col);
9107 }
9108 screen_cur_row = row;
9109 screen_cur_col = col;
9110 }
9111}
9112
9113/*
9114 * Set cursor to its position in the current window.
9115 */
9116 void
9117setcursor()
9118{
9119 if (redrawing())
9120 {
9121 validate_cursor();
9122 windgoto(W_WINROW(curwin) + curwin->w_wrow,
9123 W_WINCOL(curwin) + (
9124#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009125 /* With 'rightleft' set and the cursor on a double-wide
9126 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009127 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
9128# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009129 (has_mbyte
9130 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9131 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009132# endif
9133 1)) :
9134#endif
9135 curwin->w_wcol));
9136 }
9137}
9138
9139
9140/*
9141 * insert 'line_count' lines at 'row' in window 'wp'
9142 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9143 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
9144 * scrolling.
9145 * Returns FAIL if the lines are not inserted, OK for success.
9146 */
9147 int
9148win_ins_lines(wp, row, line_count, invalid, mayclear)
9149 win_T *wp;
9150 int row;
9151 int line_count;
9152 int invalid;
9153 int mayclear;
9154{
9155 int did_delete;
9156 int nextrow;
9157 int lastrow;
9158 int retval;
9159
9160 if (invalid)
9161 wp->w_lines_valid = 0;
9162
9163 if (wp->w_height < 5)
9164 return FAIL;
9165
9166 if (line_count > wp->w_height - row)
9167 line_count = wp->w_height - row;
9168
9169 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
9170 if (retval != MAYBE)
9171 return retval;
9172
9173 /*
9174 * If there is a next window or a status line, we first try to delete the
9175 * lines at the bottom to avoid messing what is after the window.
9176 * If this fails and there are following windows, don't do anything to avoid
9177 * messing up those windows, better just redraw.
9178 */
9179 did_delete = FALSE;
9180#ifdef FEAT_WINDOWS
9181 if (wp->w_next != NULL || wp->w_status_height)
9182 {
9183 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9184 line_count, (int)Rows, FALSE, NULL) == OK)
9185 did_delete = TRUE;
9186 else if (wp->w_next)
9187 return FAIL;
9188 }
9189#endif
9190 /*
9191 * if no lines deleted, blank the lines that will end up below the window
9192 */
9193 if (!did_delete)
9194 {
9195#ifdef FEAT_WINDOWS
9196 wp->w_redr_status = TRUE;
9197#endif
9198 redraw_cmdline = TRUE;
9199 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
9200 lastrow = nextrow + line_count;
9201 if (lastrow > Rows)
9202 lastrow = Rows;
9203 screen_fill(nextrow - line_count, lastrow - line_count,
9204 W_WINCOL(wp), (int)W_ENDCOL(wp),
9205 ' ', ' ', 0);
9206 }
9207
9208 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
9209 == FAIL)
9210 {
9211 /* deletion will have messed up other windows */
9212 if (did_delete)
9213 {
9214#ifdef FEAT_WINDOWS
9215 wp->w_redr_status = TRUE;
9216#endif
9217 win_rest_invalid(W_NEXT(wp));
9218 }
9219 return FAIL;
9220 }
9221
9222 return OK;
9223}
9224
9225/*
9226 * delete "line_count" window lines at "row" in window "wp"
9227 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9228 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9229 * scrolling
9230 * Return OK for success, FAIL if the lines are not deleted.
9231 */
9232 int
9233win_del_lines(wp, row, line_count, invalid, mayclear)
9234 win_T *wp;
9235 int row;
9236 int line_count;
9237 int invalid;
9238 int mayclear;
9239{
9240 int retval;
9241
9242 if (invalid)
9243 wp->w_lines_valid = 0;
9244
9245 if (line_count > wp->w_height - row)
9246 line_count = wp->w_height - row;
9247
9248 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
9249 if (retval != MAYBE)
9250 return retval;
9251
9252 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
9253 (int)Rows, FALSE, NULL) == FAIL)
9254 return FAIL;
9255
9256#ifdef FEAT_WINDOWS
9257 /*
9258 * If there are windows or status lines below, try to put them at the
9259 * correct place. If we can't do that, they have to be redrawn.
9260 */
9261 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9262 {
9263 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9264 line_count, (int)Rows, NULL) == FAIL)
9265 {
9266 wp->w_redr_status = TRUE;
9267 win_rest_invalid(wp->w_next);
9268 }
9269 }
9270 /*
9271 * If this is the last window and there is no status line, redraw the
9272 * command line later.
9273 */
9274 else
9275#endif
9276 redraw_cmdline = TRUE;
9277 return OK;
9278}
9279
9280/*
9281 * Common code for win_ins_lines() and win_del_lines().
9282 * Returns OK or FAIL when the work has been done.
9283 * Returns MAYBE when not finished yet.
9284 */
9285 static int
9286win_do_lines(wp, row, line_count, mayclear, del)
9287 win_T *wp;
9288 int row;
9289 int line_count;
9290 int mayclear;
9291 int del;
9292{
9293 int retval;
9294
9295 if (!redrawing() || line_count <= 0)
9296 return FAIL;
9297
9298 /* only a few lines left: redraw is faster */
9299 if (mayclear && Rows - line_count < 5
9300#ifdef FEAT_VERTSPLIT
9301 && wp->w_width == Columns
9302#endif
9303 )
9304 {
9305 screenclear(); /* will set wp->w_lines_valid to 0 */
9306 return FAIL;
9307 }
9308
9309 /*
9310 * Delete all remaining lines
9311 */
9312 if (row + line_count >= wp->w_height)
9313 {
9314 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
9315 W_WINCOL(wp), (int)W_ENDCOL(wp),
9316 ' ', ' ', 0);
9317 return OK;
9318 }
9319
9320 /*
9321 * when scrolling, the message on the command line should be cleared,
9322 * otherwise it will stay there forever.
9323 */
9324 clear_cmdline = TRUE;
9325
9326 /*
9327 * If the terminal can set a scroll region, use that.
9328 * Always do this in a vertically split window. This will redraw from
9329 * ScreenLines[] when t_CV isn't defined. That's faster than using
9330 * win_line().
9331 * Don't use a scroll region when we are going to redraw the text, writing
9332 * a character in the lower right corner of the scroll region causes a
9333 * scroll-up in the DJGPP version.
9334 */
9335 if (scroll_region
9336#ifdef FEAT_VERTSPLIT
9337 || W_WIDTH(wp) != Columns
9338#endif
9339 )
9340 {
9341#ifdef FEAT_VERTSPLIT
9342 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9343#endif
9344 scroll_region_set(wp, row);
9345 if (del)
9346 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
9347 wp->w_height - row, FALSE, wp);
9348 else
9349 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
9350 wp->w_height - row, wp);
9351#ifdef FEAT_VERTSPLIT
9352 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9353#endif
9354 scroll_region_reset();
9355 return retval;
9356 }
9357
9358#ifdef FEAT_WINDOWS
9359 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9360 return FAIL;
9361#endif
9362
9363 return MAYBE;
9364}
9365
9366/*
9367 * window 'wp' and everything after it is messed up, mark it for redraw
9368 */
9369 static void
9370win_rest_invalid(wp)
9371 win_T *wp;
9372{
9373#ifdef FEAT_WINDOWS
9374 while (wp != NULL)
9375#else
9376 if (wp != NULL)
9377#endif
9378 {
9379 redraw_win_later(wp, NOT_VALID);
9380#ifdef FEAT_WINDOWS
9381 wp->w_redr_status = TRUE;
9382 wp = wp->w_next;
9383#endif
9384 }
9385 redraw_cmdline = TRUE;
9386}
9387
9388/*
9389 * The rest of the routines in this file perform screen manipulations. The
9390 * given operation is performed physically on the screen. The corresponding
9391 * change is also made to the internal screen image. In this way, the editor
9392 * anticipates the effect of editing changes on the appearance of the screen.
9393 * That way, when we call screenupdate a complete redraw isn't usually
9394 * necessary. Another advantage is that we can keep adding code to anticipate
9395 * screen changes, and in the meantime, everything still works.
9396 */
9397
9398/*
9399 * types for inserting or deleting lines
9400 */
9401#define USE_T_CAL 1
9402#define USE_T_CDL 2
9403#define USE_T_AL 3
9404#define USE_T_CE 4
9405#define USE_T_DL 5
9406#define USE_T_SR 6
9407#define USE_NL 7
9408#define USE_T_CD 8
9409#define USE_REDRAW 9
9410
9411/*
9412 * insert lines on the screen and update ScreenLines[]
9413 * 'end' is the line after the scrolled part. Normally it is Rows.
9414 * When scrolling region used 'off' is the offset from the top for the region.
9415 * 'row' and 'end' are relative to the start of the region.
9416 *
9417 * return FAIL for failure, OK for success.
9418 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009419 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00009420screen_ins_lines(off, row, line_count, end, wp)
9421 int off;
9422 int row;
9423 int line_count;
9424 int end;
9425 win_T *wp; /* NULL or window to use width from */
9426{
9427 int i;
9428 int j;
9429 unsigned temp;
9430 int cursor_row;
9431 int type;
9432 int result_empty;
9433 int can_ce = can_clear(T_CE);
9434
9435 /*
9436 * FAIL if
9437 * - there is no valid screen
9438 * - the screen has to be redrawn completely
9439 * - the line count is less than one
9440 * - the line count is more than 'ttyscroll'
9441 */
9442 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
9443 return FAIL;
9444
9445 /*
9446 * There are seven ways to insert lines:
9447 * 0. When in a vertically split window and t_CV isn't set, redraw the
9448 * characters from ScreenLines[].
9449 * 1. Use T_CD (clear to end of display) if it exists and the result of
9450 * the insert is just empty lines
9451 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9452 * present or line_count > 1. It looks better if we do all the inserts
9453 * at once.
9454 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9455 * insert is just empty lines and T_CE is not present or line_count >
9456 * 1.
9457 * 4. Use T_AL (insert line) if it exists.
9458 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9459 * just empty lines.
9460 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9461 * just empty lines.
9462 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9463 * the 'da' flag is not set or we have clear line capability.
9464 * 8. redraw the characters from ScreenLines[].
9465 *
9466 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9467 * the scrollbar for the window. It does have insert line, use that if it
9468 * exists.
9469 */
9470 result_empty = (row + line_count >= end);
9471#ifdef FEAT_VERTSPLIT
9472 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9473 type = USE_REDRAW;
9474 else
9475#endif
9476 if (can_clear(T_CD) && result_empty)
9477 type = USE_T_CD;
9478 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9479 type = USE_T_CAL;
9480 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9481 type = USE_T_CDL;
9482 else if (*T_AL != NUL)
9483 type = USE_T_AL;
9484 else if (can_ce && result_empty)
9485 type = USE_T_CE;
9486 else if (*T_DL != NUL && result_empty)
9487 type = USE_T_DL;
9488 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9489 type = USE_T_SR;
9490 else
9491 return FAIL;
9492
9493 /*
9494 * For clearing the lines screen_del_lines() is used. This will also take
9495 * care of t_db if necessary.
9496 */
9497 if (type == USE_T_CD || type == USE_T_CDL ||
9498 type == USE_T_CE || type == USE_T_DL)
9499 return screen_del_lines(off, row, line_count, end, FALSE, wp);
9500
9501 /*
9502 * If text is retained below the screen, first clear or delete as many
9503 * lines at the bottom of the window as are about to be inserted so that
9504 * the deleted lines won't later surface during a screen_del_lines.
9505 */
9506 if (*T_DB)
9507 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
9508
9509#ifdef FEAT_CLIPBOARD
9510 /* Remove a modeless selection when inserting lines halfway the screen
9511 * or not the full width of the screen. */
9512 if (off + row > 0
9513# ifdef FEAT_VERTSPLIT
9514 || (wp != NULL && wp->w_width != Columns)
9515# endif
9516 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009517 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009518 else
9519 clip_scroll_selection(-line_count);
9520#endif
9521
Bram Moolenaar071d4272004-06-13 20:20:40 +00009522#ifdef FEAT_GUI
9523 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9524 * scrolling is actually carried out. */
9525 gui_dont_update_cursor();
9526#endif
9527
9528 if (*T_CCS != NUL) /* cursor relative to region */
9529 cursor_row = row;
9530 else
9531 cursor_row = row + off;
9532
9533 /*
9534 * Shift LineOffset[] line_count down to reflect the inserted lines.
9535 * Clear the inserted lines in ScreenLines[].
9536 */
9537 row += off;
9538 end += off;
9539 for (i = 0; i < line_count; ++i)
9540 {
9541#ifdef FEAT_VERTSPLIT
9542 if (wp != NULL && wp->w_width != Columns)
9543 {
9544 /* need to copy part of a line */
9545 j = end - 1 - i;
9546 while ((j -= line_count) >= row)
9547 linecopy(j + line_count, j, wp);
9548 j += line_count;
9549 if (can_clear((char_u *)" "))
9550 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9551 else
9552 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9553 LineWraps[j] = FALSE;
9554 }
9555 else
9556#endif
9557 {
9558 j = end - 1 - i;
9559 temp = LineOffset[j];
9560 while ((j -= line_count) >= row)
9561 {
9562 LineOffset[j + line_count] = LineOffset[j];
9563 LineWraps[j + line_count] = LineWraps[j];
9564 }
9565 LineOffset[j + line_count] = temp;
9566 LineWraps[j + line_count] = FALSE;
9567 if (can_clear((char_u *)" "))
9568 lineclear(temp, (int)Columns);
9569 else
9570 lineinvalid(temp, (int)Columns);
9571 }
9572 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009573
9574 screen_stop_highlight();
9575 windgoto(cursor_row, 0);
9576
9577#ifdef FEAT_VERTSPLIT
9578 /* redraw the characters */
9579 if (type == USE_REDRAW)
9580 redraw_block(row, end, wp);
9581 else
9582#endif
9583 if (type == USE_T_CAL)
9584 {
9585 term_append_lines(line_count);
9586 screen_start(); /* don't know where cursor is now */
9587 }
9588 else
9589 {
9590 for (i = 0; i < line_count; i++)
9591 {
9592 if (type == USE_T_AL)
9593 {
9594 if (i && cursor_row != 0)
9595 windgoto(cursor_row, 0);
9596 out_str(T_AL);
9597 }
9598 else /* type == USE_T_SR */
9599 out_str(T_SR);
9600 screen_start(); /* don't know where cursor is now */
9601 }
9602 }
9603
9604 /*
9605 * With scroll-reverse and 'da' flag set we need to clear the lines that
9606 * have been scrolled down into the region.
9607 */
9608 if (type == USE_T_SR && *T_DA)
9609 {
9610 for (i = 0; i < line_count; ++i)
9611 {
9612 windgoto(off + i, 0);
9613 out_str(T_CE);
9614 screen_start(); /* don't know where cursor is now */
9615 }
9616 }
9617
9618#ifdef FEAT_GUI
9619 gui_can_update_cursor();
9620 if (gui.in_use)
9621 out_flush(); /* always flush after a scroll */
9622#endif
9623 return OK;
9624}
9625
9626/*
9627 * delete lines on the screen and update ScreenLines[]
9628 * 'end' is the line after the scrolled part. Normally it is Rows.
9629 * When scrolling region used 'off' is the offset from the top for the region.
9630 * 'row' and 'end' are relative to the start of the region.
9631 *
9632 * Return OK for success, FAIL if the lines are not deleted.
9633 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009634 int
9635screen_del_lines(off, row, line_count, end, force, wp)
9636 int off;
9637 int row;
9638 int line_count;
9639 int end;
9640 int force; /* even when line_count > p_ttyscroll */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009641 win_T *wp UNUSED; /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009642{
9643 int j;
9644 int i;
9645 unsigned temp;
9646 int cursor_row;
9647 int cursor_end;
9648 int result_empty; /* result is empty until end of region */
9649 int can_delete; /* deleting line codes can be used */
9650 int type;
9651
9652 /*
9653 * FAIL if
9654 * - there is no valid screen
9655 * - the screen has to be redrawn completely
9656 * - the line count is less than one
9657 * - the line count is more than 'ttyscroll'
9658 */
9659 if (!screen_valid(TRUE) || line_count <= 0 ||
9660 (!force && line_count > p_ttyscroll))
9661 return FAIL;
9662
9663 /*
9664 * Check if the rest of the current region will become empty.
9665 */
9666 result_empty = row + line_count >= end;
9667
9668 /*
9669 * We can delete lines only when 'db' flag not set or when 'ce' option
9670 * available.
9671 */
9672 can_delete = (*T_DB == NUL || can_clear(T_CE));
9673
9674 /*
9675 * There are six ways to delete lines:
9676 * 0. When in a vertically split window and t_CV isn't set, redraw the
9677 * characters from ScreenLines[].
9678 * 1. Use T_CD if it exists and the result is empty.
9679 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9680 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9681 * none of the other ways work.
9682 * 4. Use T_CE (erase line) if the result is empty.
9683 * 5. Use T_DL (delete line) if it exists.
9684 * 6. redraw the characters from ScreenLines[].
9685 */
9686#ifdef FEAT_VERTSPLIT
9687 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9688 type = USE_REDRAW;
9689 else
9690#endif
9691 if (can_clear(T_CD) && result_empty)
9692 type = USE_T_CD;
9693#if defined(__BEOS__) && defined(BEOS_DR8)
9694 /*
9695 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9696 * its internal termcap... this works okay for tests which test *T_DB !=
9697 * NUL. It has the disadvantage that the user cannot use any :set t_*
9698 * command to get T_DB (back) to empty_option, only :set term=... will do
9699 * the trick...
9700 * Anyway, this hack will hopefully go away with the next OS release.
9701 * (Olaf Seibert)
9702 */
9703 else if (row == 0 && T_DB == empty_option
9704 && (line_count == 1 || *T_CDL == NUL))
9705#else
9706 else if (row == 0 && (
9707#ifndef AMIGA
9708 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9709 * up, so use delete-line command */
9710 line_count == 1 ||
9711#endif
9712 *T_CDL == NUL))
9713#endif
9714 type = USE_NL;
9715 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9716 type = USE_T_CDL;
9717 else if (can_clear(T_CE) && result_empty
9718#ifdef FEAT_VERTSPLIT
9719 && (wp == NULL || wp->w_width == Columns)
9720#endif
9721 )
9722 type = USE_T_CE;
9723 else if (*T_DL != NUL && can_delete)
9724 type = USE_T_DL;
9725 else if (*T_CDL != NUL && can_delete)
9726 type = USE_T_CDL;
9727 else
9728 return FAIL;
9729
9730#ifdef FEAT_CLIPBOARD
9731 /* Remove a modeless selection when deleting lines halfway the screen or
9732 * not the full width of the screen. */
9733 if (off + row > 0
9734# ifdef FEAT_VERTSPLIT
9735 || (wp != NULL && wp->w_width != Columns)
9736# endif
9737 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009738 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009739 else
9740 clip_scroll_selection(line_count);
9741#endif
9742
Bram Moolenaar071d4272004-06-13 20:20:40 +00009743#ifdef FEAT_GUI
9744 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9745 * scrolling is actually carried out. */
9746 gui_dont_update_cursor();
9747#endif
9748
9749 if (*T_CCS != NUL) /* cursor relative to region */
9750 {
9751 cursor_row = row;
9752 cursor_end = end;
9753 }
9754 else
9755 {
9756 cursor_row = row + off;
9757 cursor_end = end + off;
9758 }
9759
9760 /*
9761 * Now shift LineOffset[] line_count up to reflect the deleted lines.
9762 * Clear the inserted lines in ScreenLines[].
9763 */
9764 row += off;
9765 end += off;
9766 for (i = 0; i < line_count; ++i)
9767 {
9768#ifdef FEAT_VERTSPLIT
9769 if (wp != NULL && wp->w_width != Columns)
9770 {
9771 /* need to copy part of a line */
9772 j = row + i;
9773 while ((j += line_count) <= end - 1)
9774 linecopy(j - line_count, j, wp);
9775 j -= line_count;
9776 if (can_clear((char_u *)" "))
9777 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9778 else
9779 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9780 LineWraps[j] = FALSE;
9781 }
9782 else
9783#endif
9784 {
9785 /* whole width, moving the line pointers is faster */
9786 j = row + i;
9787 temp = LineOffset[j];
9788 while ((j += line_count) <= end - 1)
9789 {
9790 LineOffset[j - line_count] = LineOffset[j];
9791 LineWraps[j - line_count] = LineWraps[j];
9792 }
9793 LineOffset[j - line_count] = temp;
9794 LineWraps[j - line_count] = FALSE;
9795 if (can_clear((char_u *)" "))
9796 lineclear(temp, (int)Columns);
9797 else
9798 lineinvalid(temp, (int)Columns);
9799 }
9800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009801
9802 screen_stop_highlight();
9803
9804#ifdef FEAT_VERTSPLIT
9805 /* redraw the characters */
9806 if (type == USE_REDRAW)
9807 redraw_block(row, end, wp);
9808 else
9809#endif
9810 if (type == USE_T_CD) /* delete the lines */
9811 {
9812 windgoto(cursor_row, 0);
9813 out_str(T_CD);
9814 screen_start(); /* don't know where cursor is now */
9815 }
9816 else if (type == USE_T_CDL)
9817 {
9818 windgoto(cursor_row, 0);
9819 term_delete_lines(line_count);
9820 screen_start(); /* don't know where cursor is now */
9821 }
9822 /*
9823 * Deleting lines at top of the screen or scroll region: Just scroll
9824 * the whole screen (scroll region) up by outputting newlines on the
9825 * last line.
9826 */
9827 else if (type == USE_NL)
9828 {
9829 windgoto(cursor_end - 1, 0);
9830 for (i = line_count; --i >= 0; )
9831 out_char('\n'); /* cursor will remain on same line */
9832 }
9833 else
9834 {
9835 for (i = line_count; --i >= 0; )
9836 {
9837 if (type == USE_T_DL)
9838 {
9839 windgoto(cursor_row, 0);
9840 out_str(T_DL); /* delete a line */
9841 }
9842 else /* type == USE_T_CE */
9843 {
9844 windgoto(cursor_row + i, 0);
9845 out_str(T_CE); /* erase a line */
9846 }
9847 screen_start(); /* don't know where cursor is now */
9848 }
9849 }
9850
9851 /*
9852 * If the 'db' flag is set, we need to clear the lines that have been
9853 * scrolled up at the bottom of the region.
9854 */
9855 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
9856 {
9857 for (i = line_count; i > 0; --i)
9858 {
9859 windgoto(cursor_end - i, 0);
9860 out_str(T_CE); /* erase a line */
9861 screen_start(); /* don't know where cursor is now */
9862 }
9863 }
9864
9865#ifdef FEAT_GUI
9866 gui_can_update_cursor();
9867 if (gui.in_use)
9868 out_flush(); /* always flush after a scroll */
9869#endif
9870
9871 return OK;
9872}
9873
9874/*
9875 * show the current mode and ruler
9876 *
9877 * If clear_cmdline is TRUE, clear the rest of the cmdline.
9878 * If clear_cmdline is FALSE there may be a message there that needs to be
9879 * cleared only if a mode is shown.
9880 * Return the length of the message (0 if no message).
9881 */
9882 int
9883showmode()
9884{
9885 int need_clear;
9886 int length = 0;
9887 int do_mode;
9888 int attr;
9889 int nwr_save;
9890#ifdef FEAT_INS_EXPAND
9891 int sub_attr;
9892#endif
9893
Bram Moolenaar7df351e2006-01-23 22:30:28 +00009894 do_mode = ((p_smd && msg_silent == 0)
9895 && ((State & INSERT)
9896 || restart_edit
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009897 || VIsual_active));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009898 if (do_mode || Recording)
9899 {
9900 /*
9901 * Don't show mode right now, when not redrawing or inside a mapping.
9902 * Call char_avail() only when we are going to show something, because
9903 * it takes a bit of time.
9904 */
9905 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
9906 {
9907 redraw_cmdline = TRUE; /* show mode later */
9908 return 0;
9909 }
9910
9911 nwr_save = need_wait_return;
9912
9913 /* wait a bit before overwriting an important message */
9914 check_for_delay(FALSE);
9915
9916 /* if the cmdline is more than one line high, erase top lines */
9917 need_clear = clear_cmdline;
9918 if (clear_cmdline && cmdline_row < Rows - 1)
9919 msg_clr_cmdline(); /* will reset clear_cmdline */
9920
9921 /* Position on the last line in the window, column 0 */
9922 msg_pos_mode();
9923 cursor_off();
9924 attr = hl_attr(HLF_CM); /* Highlight mode */
9925 if (do_mode)
9926 {
9927 MSG_PUTS_ATTR("--", attr);
9928#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00009929 if (
Bram Moolenaar09092152010-08-08 16:38:42 +02009930# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +00009931 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +02009932# else
Bram Moolenaarc236c162008-07-13 17:41:49 +00009933 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +00009934# endif
Bram Moolenaar09092152010-08-08 16:38:42 +02009935 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02009936# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009937 MSG_PUTS_ATTR(" IM", attr);
9938# else
9939 MSG_PUTS_ATTR(" XIM", attr);
9940# endif
9941#endif
9942#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
9943 if (gui.in_use)
9944 {
9945 if (hangul_input_state_get())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009946 MSG_PUTS_ATTR(" \307\321\261\333", attr); /* HANGUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009947 }
9948#endif
9949#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +02009950 /* CTRL-X in Insert mode */
9951 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009952 {
9953 /* These messages can get long, avoid a wrap in a narrow
9954 * window. Prefer showing edit_submode_extra. */
9955 length = (Rows - msg_row) * Columns - 3;
9956 if (edit_submode_extra != NULL)
9957 length -= vim_strsize(edit_submode_extra);
9958 if (length > 0)
9959 {
9960 if (edit_submode_pre != NULL)
9961 length -= vim_strsize(edit_submode_pre);
9962 if (length - vim_strsize(edit_submode) > 0)
9963 {
9964 if (edit_submode_pre != NULL)
9965 msg_puts_attr(edit_submode_pre, attr);
9966 msg_puts_attr(edit_submode, attr);
9967 }
9968 if (edit_submode_extra != NULL)
9969 {
9970 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
9971 if ((int)edit_submode_highl < (int)HLF_COUNT)
9972 sub_attr = hl_attr(edit_submode_highl);
9973 else
9974 sub_attr = attr;
9975 msg_puts_attr(edit_submode_extra, sub_attr);
9976 }
9977 }
9978 length = 0;
9979 }
9980 else
9981#endif
9982 {
9983#ifdef FEAT_VREPLACE
9984 if (State & VREPLACE_FLAG)
9985 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
9986 else
9987#endif
9988 if (State & REPLACE_FLAG)
9989 MSG_PUTS_ATTR(_(" REPLACE"), attr);
9990 else if (State & INSERT)
9991 {
9992#ifdef FEAT_RIGHTLEFT
9993 if (p_ri)
9994 MSG_PUTS_ATTR(_(" REVERSE"), attr);
9995#endif
9996 MSG_PUTS_ATTR(_(" INSERT"), attr);
9997 }
9998 else if (restart_edit == 'I')
9999 MSG_PUTS_ATTR(_(" (insert)"), attr);
10000 else if (restart_edit == 'R')
10001 MSG_PUTS_ATTR(_(" (replace)"), attr);
10002 else if (restart_edit == 'V')
10003 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10004#ifdef FEAT_RIGHTLEFT
10005 if (p_hkmap)
10006 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10007# ifdef FEAT_FKMAP
10008 if (p_fkmap)
10009 MSG_PUTS_ATTR(farsi_text_5, attr);
10010# endif
10011#endif
10012#ifdef FEAT_KEYMAP
10013 if (State & LANGMAP)
10014 {
10015# ifdef FEAT_ARABIC
10016 if (curwin->w_p_arab)
10017 MSG_PUTS_ATTR(_(" Arabic"), attr);
10018 else
10019# endif
10020 MSG_PUTS_ATTR(_(" (lang)"), attr);
10021 }
10022#endif
10023 if ((State & INSERT) && p_paste)
10024 MSG_PUTS_ATTR(_(" (paste)"), attr);
10025
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026 if (VIsual_active)
10027 {
10028 char *p;
10029
10030 /* Don't concatenate separate words to avoid translation
10031 * problems. */
10032 switch ((VIsual_select ? 4 : 0)
10033 + (VIsual_mode == Ctrl_V) * 2
10034 + (VIsual_mode == 'V'))
10035 {
10036 case 0: p = N_(" VISUAL"); break;
10037 case 1: p = N_(" VISUAL LINE"); break;
10038 case 2: p = N_(" VISUAL BLOCK"); break;
10039 case 4: p = N_(" SELECT"); break;
10040 case 5: p = N_(" SELECT LINE"); break;
10041 default: p = N_(" SELECT BLOCK"); break;
10042 }
10043 MSG_PUTS_ATTR(_(p), attr);
10044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010045 MSG_PUTS_ATTR(" --", attr);
10046 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010047
Bram Moolenaar071d4272004-06-13 20:20:40 +000010048 need_clear = TRUE;
10049 }
10050 if (Recording
10051#ifdef FEAT_INS_EXPAND
10052 && edit_submode == NULL /* otherwise it gets too long */
10053#endif
10054 )
10055 {
10056 MSG_PUTS_ATTR(_("recording"), attr);
10057 need_clear = TRUE;
10058 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010059
10060 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010061 if (need_clear || clear_cmdline)
10062 msg_clr_eos();
10063 msg_didout = FALSE; /* overwrite this message */
10064 length = msg_col;
10065 msg_col = 0;
10066 need_wait_return = nwr_save; /* never ask for hit-return for this */
10067 }
10068 else if (clear_cmdline && msg_silent == 0)
10069 /* Clear the whole command line. Will reset "clear_cmdline". */
10070 msg_clr_cmdline();
10071
10072#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010073 /* In Visual mode the size of the selected area must be redrawn. */
10074 if (VIsual_active)
10075 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010076
10077 /* If the last window has no status line, the ruler is after the mode
10078 * message and must be redrawn */
10079 if (redrawing()
10080# ifdef FEAT_WINDOWS
10081 && lastwin->w_status_height == 0
10082# endif
10083 )
10084 win_redr_ruler(lastwin, TRUE);
10085#endif
10086 redraw_cmdline = FALSE;
10087 clear_cmdline = FALSE;
10088
10089 return length;
10090}
10091
10092/*
10093 * Position for a mode message.
10094 */
10095 static void
10096msg_pos_mode()
10097{
10098 msg_col = 0;
10099 msg_row = Rows - 1;
10100}
10101
10102/*
10103 * Delete mode message. Used when ESC is typed which is expected to end
10104 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010105 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010106 */
10107 void
10108unshowmode(force)
10109 int force;
10110{
10111 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010112 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010113 */
10114 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10115 redraw_cmdline = TRUE; /* delete mode later */
10116 else
10117 {
10118 msg_pos_mode();
10119 if (Recording)
10120 MSG_PUTS_ATTR(_("recording"), hl_attr(HLF_CM));
10121 msg_clr_eos();
10122 }
10123}
10124
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010125#if defined(FEAT_WINDOWS)
10126/*
10127 * Draw the tab pages line at the top of the Vim window.
10128 */
10129 static void
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010130draw_tabline()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010131{
10132 int tabcount = 0;
10133 tabpage_T *tp;
10134 int tabwidth;
10135 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010136 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010137 int attr;
10138 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010139 win_T *cwp;
10140 int wincount;
10141 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010142 int c;
10143 int len;
10144 int attr_sel = hl_attr(HLF_TPS);
10145 int attr_nosel = hl_attr(HLF_TP);
10146 int attr_fill = hl_attr(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010147 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010148 int room;
10149 int use_sep_chars = (t_colors < 8
10150#ifdef FEAT_GUI
10151 && !gui.in_use
10152#endif
10153 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010154
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010155 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010156
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010157#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010158 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010159 if (gui_use_tabline())
10160 {
10161 gui_update_tabline();
10162 return;
10163 }
10164#endif
10165
10166 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010167 return;
10168
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010169#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010170
10171 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10172 for (scol = 0; scol < Columns; ++scol)
10173 TabPageIdxs[scol] = 0;
10174
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010175 /* Use the 'tabline' option if it's set. */
10176 if (*p_tal != NUL)
10177 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010178 int save_called_emsg = called_emsg;
10179
10180 /* Check for an error. If there is one we would loop in redrawing the
10181 * screen. Avoid that by making 'tabline' empty. */
10182 called_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010183 win_redr_custom(NULL, FALSE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010184 if (called_emsg)
10185 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010186 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010187 called_emsg |= save_called_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010188 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010189 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010190#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010191 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010192 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
10193 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010194
Bram Moolenaar238a5642006-02-21 22:12:05 +000010195 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10196 if (tabwidth < 6)
10197 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010198
Bram Moolenaar238a5642006-02-21 22:12:05 +000010199 attr = attr_nosel;
10200 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010201 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010202 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10203 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010204 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010205 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010206
Bram Moolenaar238a5642006-02-21 22:12:05 +000010207 if (tp->tp_topframe == topframe)
10208 attr = attr_sel;
10209 if (use_sep_chars && col > 0)
10210 screen_putchar('|', 0, col++, attr);
10211
10212 if (tp->tp_topframe != topframe)
10213 attr = attr_nosel;
10214
10215 screen_putchar(' ', 0, col++, attr);
10216
10217 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010218 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010219 cwp = curwin;
10220 wp = firstwin;
10221 }
10222 else
10223 {
10224 cwp = tp->tp_curwin;
10225 wp = tp->tp_firstwin;
10226 }
10227
10228 modified = FALSE;
10229 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10230 if (bufIsChanged(wp->w_buffer))
10231 modified = TRUE;
10232 if (modified || wincount > 1)
10233 {
10234 if (wincount > 1)
10235 {
10236 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010237 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010238 if (col + len >= Columns - 3)
10239 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010240 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010241#if defined(FEAT_SYN_HL)
Bram Moolenaare0f14822014-08-06 13:20:56 +020010242 hl_combine_attr(attr, hl_attr(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010243#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010244 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010245#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010246 );
10247 col += len;
10248 }
10249 if (modified)
10250 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10251 screen_putchar(' ', 0, col++, attr);
10252 }
10253
10254 room = scol - col + tabwidth - 1;
10255 if (room > 0)
10256 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010257 /* Get buffer name in NameBuff[] */
10258 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010259 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010260 len = vim_strsize(NameBuff);
10261 p = NameBuff;
10262#ifdef FEAT_MBYTE
10263 if (has_mbyte)
10264 while (len > room)
10265 {
10266 len -= ptr2cells(p);
10267 mb_ptr_adv(p);
10268 }
10269 else
10270#endif
10271 if (len > room)
10272 {
10273 p += len - room;
10274 len = room;
10275 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010276 if (len > Columns - col - 1)
10277 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010278
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010279 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010280 col += len;
10281 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010282 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010283
10284 /* Store the tab page number in TabPageIdxs[], so that
10285 * jump_to_mouse() knows where each one is. */
10286 ++tabcount;
10287 while (scol < col)
10288 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010289 }
10290
Bram Moolenaar238a5642006-02-21 22:12:05 +000010291 if (use_sep_chars)
10292 c = '_';
10293 else
10294 c = ' ';
10295 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010296
10297 /* Put an "X" for closing the current tab if there are several. */
10298 if (first_tabpage->tp_next != NULL)
10299 {
10300 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10301 TabPageIdxs[Columns - 1] = -999;
10302 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010303 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010304
10305 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10306 * set. */
10307 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010308}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010309
10310/*
10311 * Get buffer name for "buf" into NameBuff[].
10312 * Takes care of special buffer names and translates special characters.
10313 */
10314 void
10315get_trans_bufname(buf)
10316 buf_T *buf;
10317{
10318 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010319 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010320 else
10321 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10322 trans_characters(NameBuff, MAXPATHL);
10323}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010324#endif
10325
Bram Moolenaar071d4272004-06-13 20:20:40 +000010326#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
10327/*
10328 * Get the character to use in a status line. Get its attributes in "*attr".
10329 */
10330 static int
10331fillchar_status(attr, is_curwin)
10332 int *attr;
10333 int is_curwin;
10334{
10335 int fill;
10336 if (is_curwin)
10337 {
10338 *attr = hl_attr(HLF_S);
10339 fill = fill_stl;
10340 }
10341 else
10342 {
10343 *attr = hl_attr(HLF_SNC);
10344 fill = fill_stlnc;
10345 }
10346 /* Use fill when there is highlighting, and highlighting of current
10347 * window differs, or the fillchars differ, or this is not the
10348 * current window */
10349 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
10350 || !is_curwin || firstwin == lastwin)
10351 || (fill_stl != fill_stlnc)))
10352 return fill;
10353 if (is_curwin)
10354 return '^';
10355 return '=';
10356}
10357#endif
10358
10359#ifdef FEAT_VERTSPLIT
10360/*
10361 * Get the character to use in a separator between vertically split windows.
10362 * Get its attributes in "*attr".
10363 */
10364 static int
10365fillchar_vsep(attr)
10366 int *attr;
10367{
10368 *attr = hl_attr(HLF_C);
10369 if (*attr == 0 && fill_vert == ' ')
10370 return '|';
10371 else
10372 return fill_vert;
10373}
10374#endif
10375
10376/*
10377 * Return TRUE if redrawing should currently be done.
10378 */
10379 int
10380redrawing()
10381{
10382 return (!RedrawingDisabled
10383 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
10384}
10385
10386/*
10387 * Return TRUE if printing messages should currently be done.
10388 */
10389 int
10390messaging()
10391{
10392 return (!(p_lz && char_avail() && !KeyTyped));
10393}
10394
10395/*
10396 * Show current status info in ruler and various other places
10397 * If always is FALSE, only show ruler if position has changed.
10398 */
10399 void
10400showruler(always)
10401 int always;
10402{
10403 if (!always && !redrawing())
10404 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010405#ifdef FEAT_INS_EXPAND
10406 if (pum_visible())
10407 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010408# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +000010409 /* Don't redraw right now, do it later. */
10410 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010411# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +000010412 return;
10413 }
10414#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010415#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010416 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010417 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010418 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010420 else
10421#endif
10422#ifdef FEAT_CMDL_INFO
10423 win_redr_ruler(curwin, always);
10424#endif
10425
10426#ifdef FEAT_TITLE
10427 if (need_maketitle
10428# ifdef FEAT_STL_OPT
10429 || (p_icon && (stl_syntax & STL_IN_ICON))
10430 || (p_title && (stl_syntax & STL_IN_TITLE))
10431# endif
10432 )
10433 maketitle();
10434#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010435#ifdef FEAT_WINDOWS
10436 /* Redraw the tab pages line if needed. */
10437 if (redraw_tabline)
10438 draw_tabline();
10439#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010440}
10441
10442#ifdef FEAT_CMDL_INFO
10443 static void
10444win_redr_ruler(wp, always)
10445 win_T *wp;
10446 int always;
10447{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010448#define RULER_BUF_LEN 70
10449 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010450 int row;
10451 int fillchar;
10452 int attr;
10453 int empty_line = FALSE;
10454 colnr_T virtcol;
10455 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010456 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010457 int o;
10458#ifdef FEAT_VERTSPLIT
10459 int this_ru_col;
10460 int off = 0;
10461 int width = Columns;
10462# define WITH_OFF(x) x
10463# define WITH_WIDTH(x) x
10464#else
10465# define WITH_OFF(x) 0
10466# define WITH_WIDTH(x) Columns
10467# define this_ru_col ru_col
10468#endif
10469
10470 /* If 'ruler' off or redrawing disabled, don't do anything */
10471 if (!p_ru)
10472 return;
10473
10474 /*
10475 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10476 * after deleting lines, before cursor.lnum is corrected.
10477 */
10478 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10479 return;
10480
10481#ifdef FEAT_INS_EXPAND
10482 /* Don't draw the ruler while doing insert-completion, it might overwrite
10483 * the (long) mode message. */
10484# ifdef FEAT_WINDOWS
10485 if (wp == lastwin && lastwin->w_status_height == 0)
10486# endif
10487 if (edit_submode != NULL)
10488 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010489 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10490 if (pum_visible())
10491 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010492#endif
10493
10494#ifdef FEAT_STL_OPT
10495 if (*p_ruf)
10496 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010497 int save_called_emsg = called_emsg;
10498
10499 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010500 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010501 if (called_emsg)
10502 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010503 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010504 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010505 return;
10506 }
10507#endif
10508
10509 /*
10510 * Check if not in Insert mode and the line is empty (will show "0-1").
10511 */
10512 if (!(State & INSERT)
10513 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10514 empty_line = TRUE;
10515
10516 /*
10517 * Only draw the ruler when something changed.
10518 */
10519 validate_virtcol_win(wp);
10520 if ( redraw_cmdline
10521 || always
10522 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10523 || wp->w_cursor.col != wp->w_ru_cursor.col
10524 || wp->w_virtcol != wp->w_ru_virtcol
10525#ifdef FEAT_VIRTUALEDIT
10526 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10527#endif
10528 || wp->w_topline != wp->w_ru_topline
10529 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10530#ifdef FEAT_DIFF
10531 || wp->w_topfill != wp->w_ru_topfill
10532#endif
10533 || empty_line != wp->w_ru_empty)
10534 {
10535 cursor_off();
10536#ifdef FEAT_WINDOWS
10537 if (wp->w_status_height)
10538 {
10539 row = W_WINROW(wp) + wp->w_height;
10540 fillchar = fillchar_status(&attr, wp == curwin);
10541# ifdef FEAT_VERTSPLIT
10542 off = W_WINCOL(wp);
10543 width = W_WIDTH(wp);
10544# endif
10545 }
10546 else
10547#endif
10548 {
10549 row = Rows - 1;
10550 fillchar = ' ';
10551 attr = 0;
10552#ifdef FEAT_VERTSPLIT
10553 width = Columns;
10554 off = 0;
10555#endif
10556 }
10557
10558 /* In list mode virtcol needs to be recomputed */
10559 virtcol = wp->w_virtcol;
10560 if (wp->w_p_list && lcs_tab1 == NUL)
10561 {
10562 wp->w_p_list = FALSE;
10563 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10564 wp->w_p_list = TRUE;
10565 }
10566
10567 /*
10568 * Some sprintfs return the length, some return a pointer.
10569 * To avoid portability problems we use strlen() here.
10570 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010571 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010572 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10573 ? 0L
10574 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010575 len = STRLEN(buffer);
10576 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010577 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10578 (int)virtcol + 1);
10579
10580 /*
10581 * Add a "50%" if there is room for it.
10582 * On the last line, don't print in the last column (scrolls the
10583 * screen up on some terminals).
10584 */
10585 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010586 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010587 o = i + vim_strsize(buffer + i + 1);
10588#ifdef FEAT_WINDOWS
10589 if (wp->w_status_height == 0) /* can't use last char of screen */
10590#endif
10591 ++o;
10592#ifdef FEAT_VERTSPLIT
10593 this_ru_col = ru_col - (Columns - width);
10594 if (this_ru_col < 0)
10595 this_ru_col = 0;
10596#endif
10597 /* Never use more than half the window/screen width, leave the other
10598 * half for the filename. */
10599 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
10600 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
10601 if (this_ru_col + o < WITH_WIDTH(width))
10602 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010010603 /* need at least 3 chars left for get_rel_pos() + NUL */
10604 while (this_ru_col + o < WITH_WIDTH(width) && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010605 {
10606#ifdef FEAT_MBYTE
10607 if (has_mbyte)
10608 i += (*mb_char2bytes)(fillchar, buffer + i);
10609 else
10610#endif
10611 buffer[i++] = fillchar;
10612 ++o;
10613 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010614 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010615 }
10616 /* Truncate at window boundary. */
10617#ifdef FEAT_MBYTE
10618 if (has_mbyte)
10619 {
10620 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010621 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010622 {
10623 o += (*mb_ptr2cells)(buffer + i);
10624 if (this_ru_col + o > WITH_WIDTH(width))
10625 {
10626 buffer[i] = NUL;
10627 break;
10628 }
10629 }
10630 }
10631 else
10632#endif
10633 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
10634 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
10635
10636 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
10637 i = redraw_cmdline;
10638 screen_fill(row, row + 1,
10639 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
10640 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
10641 fillchar, fillchar, attr);
10642 /* don't redraw the cmdline because of showing the ruler */
10643 redraw_cmdline = i;
10644 wp->w_ru_cursor = wp->w_cursor;
10645 wp->w_ru_virtcol = wp->w_virtcol;
10646 wp->w_ru_empty = empty_line;
10647 wp->w_ru_topline = wp->w_topline;
10648 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10649#ifdef FEAT_DIFF
10650 wp->w_ru_topfill = wp->w_topfill;
10651#endif
10652 }
10653}
10654#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010655
10656#if defined(FEAT_LINEBREAK) || defined(PROTO)
10657/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010658 * Return the width of the 'number' and 'relativenumber' column.
10659 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010660 * Otherwise it depends on 'numberwidth' and the line count.
10661 */
10662 int
10663number_width(wp)
10664 win_T *wp;
10665{
10666 int n;
10667 linenr_T lnum;
10668
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020010669 if (wp->w_p_rnu && !wp->w_p_nu)
10670 /* cursor line shows "0" */
10671 lnum = wp->w_height;
10672 else
10673 /* cursor line shows absolute line number */
10674 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020010675
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010676 if (lnum == wp->w_nrwidth_line_count)
10677 return wp->w_nrwidth_width;
10678 wp->w_nrwidth_line_count = lnum;
10679
10680 n = 0;
10681 do
10682 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010683 lnum /= 10;
10684 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010685 } while (lnum > 0);
10686
10687 /* 'numberwidth' gives the minimal width plus one */
10688 if (n < wp->w_p_nuw - 1)
10689 n = wp->w_p_nuw - 1;
10690
10691 wp->w_nrwidth_width = n;
10692 return n;
10693}
10694#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010695
10696/*
10697 * Return the current cursor column. This is the actual position on the
10698 * screen. First column is 0.
10699 */
10700 int
10701screen_screencol()
10702{
10703 return screen_cur_col;
10704}
10705
10706/*
10707 * Return the current cursor row. This is the actual position on the screen.
10708 * First row is 0.
10709 */
10710 int
10711screen_screenrow()
10712{
10713 return screen_cur_row;
10714}